max14577.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * max14577.c - mfd core driver for the Maxim 14577
  3. *
  4. * Copyright (C) 2013 Samsung Electrnoics
  5. * Chanwoo Choi <cw00.choi@samsung.com>
  6. * Krzysztof Kozlowski <k.kozlowski@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * This driver is based on max8997.c
  19. */
  20. #include <linux/err.h>
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/mfd/core.h>
  24. #include <linux/mfd/max14577.h>
  25. #include <linux/mfd/max14577-private.h>
  26. static struct mfd_cell max14577_devs[] = {
  27. {
  28. .name = "max14577-muic",
  29. .of_compatible = "maxim,max14577-muic",
  30. },
  31. {
  32. .name = "max14577-regulator",
  33. .of_compatible = "maxim,max14577-regulator",
  34. },
  35. { .name = "max14577-charger", },
  36. };
  37. static bool max14577_volatile_reg(struct device *dev, unsigned int reg)
  38. {
  39. switch (reg) {
  40. case MAX14577_REG_INT1 ... MAX14577_REG_STATUS3:
  41. return true;
  42. default:
  43. break;
  44. }
  45. return false;
  46. }
  47. static const struct regmap_config max14577_regmap_config = {
  48. .reg_bits = 8,
  49. .val_bits = 8,
  50. .volatile_reg = max14577_volatile_reg,
  51. .max_register = MAX14577_REG_END,
  52. };
  53. static const struct regmap_irq max14577_irqs[] = {
  54. /* INT1 interrupts */
  55. { .reg_offset = 0, .mask = INT1_ADC_MASK, },
  56. { .reg_offset = 0, .mask = INT1_ADCLOW_MASK, },
  57. { .reg_offset = 0, .mask = INT1_ADCERR_MASK, },
  58. /* INT2 interrupts */
  59. { .reg_offset = 1, .mask = INT2_CHGTYP_MASK, },
  60. { .reg_offset = 1, .mask = INT2_CHGDETRUN_MASK, },
  61. { .reg_offset = 1, .mask = INT2_DCDTMR_MASK, },
  62. { .reg_offset = 1, .mask = INT2_DBCHG_MASK, },
  63. { .reg_offset = 1, .mask = INT2_VBVOLT_MASK, },
  64. /* INT3 interrupts */
  65. { .reg_offset = 2, .mask = INT3_EOC_MASK, },
  66. { .reg_offset = 2, .mask = INT3_CGMBC_MASK, },
  67. { .reg_offset = 2, .mask = INT3_OVP_MASK, },
  68. { .reg_offset = 2, .mask = INT3_MBCCHGERR_MASK, },
  69. };
  70. static const struct regmap_irq_chip max14577_irq_chip = {
  71. .name = "max14577",
  72. .status_base = MAX14577_REG_INT1,
  73. .mask_base = MAX14577_REG_INTMASK1,
  74. .mask_invert = 1,
  75. .num_regs = 3,
  76. .irqs = max14577_irqs,
  77. .num_irqs = ARRAY_SIZE(max14577_irqs),
  78. };
  79. static int max14577_i2c_probe(struct i2c_client *i2c,
  80. const struct i2c_device_id *id)
  81. {
  82. struct max14577 *max14577;
  83. struct max14577_platform_data *pdata = dev_get_platdata(&i2c->dev);
  84. struct device_node *np = i2c->dev.of_node;
  85. u8 reg_data;
  86. int ret = 0;
  87. if (np) {
  88. pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
  89. if (!pdata)
  90. return -ENOMEM;
  91. i2c->dev.platform_data = pdata;
  92. }
  93. if (!pdata) {
  94. dev_err(&i2c->dev, "No platform data found.\n");
  95. return -EINVAL;
  96. }
  97. max14577 = devm_kzalloc(&i2c->dev, sizeof(*max14577), GFP_KERNEL);
  98. if (!max14577)
  99. return -ENOMEM;
  100. i2c_set_clientdata(i2c, max14577);
  101. max14577->dev = &i2c->dev;
  102. max14577->i2c = i2c;
  103. max14577->irq = i2c->irq;
  104. max14577->regmap = devm_regmap_init_i2c(i2c, &max14577_regmap_config);
  105. if (IS_ERR(max14577->regmap)) {
  106. ret = PTR_ERR(max14577->regmap);
  107. dev_err(max14577->dev, "Failed to allocate register map: %d\n",
  108. ret);
  109. return ret;
  110. }
  111. ret = max14577_read_reg(max14577->regmap, MAX14577_REG_DEVICEID,
  112. &reg_data);
  113. if (ret) {
  114. dev_err(max14577->dev, "Device not found on this channel: %d\n",
  115. ret);
  116. return ret;
  117. }
  118. max14577->vendor_id = ((reg_data & DEVID_VENDORID_MASK) >>
  119. DEVID_VENDORID_SHIFT);
  120. max14577->device_id = ((reg_data & DEVID_DEVICEID_MASK) >>
  121. DEVID_DEVICEID_SHIFT);
  122. dev_info(max14577->dev, "Device ID: 0x%x, vendor: 0x%x\n",
  123. max14577->device_id, max14577->vendor_id);
  124. ret = regmap_add_irq_chip(max14577->regmap, max14577->irq,
  125. IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 0,
  126. &max14577_irq_chip,
  127. &max14577->irq_data);
  128. if (ret != 0) {
  129. dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n",
  130. max14577->irq, ret);
  131. return ret;
  132. }
  133. ret = mfd_add_devices(max14577->dev, -1, max14577_devs,
  134. ARRAY_SIZE(max14577_devs), NULL, 0,
  135. regmap_irq_get_domain(max14577->irq_data));
  136. if (ret < 0)
  137. goto err_mfd;
  138. device_init_wakeup(max14577->dev, 1);
  139. return 0;
  140. err_mfd:
  141. regmap_del_irq_chip(max14577->irq, max14577->irq_data);
  142. return ret;
  143. }
  144. static int max14577_i2c_remove(struct i2c_client *i2c)
  145. {
  146. struct max14577 *max14577 = i2c_get_clientdata(i2c);
  147. mfd_remove_devices(max14577->dev);
  148. regmap_del_irq_chip(max14577->irq, max14577->irq_data);
  149. return 0;
  150. }
  151. static const struct i2c_device_id max14577_i2c_id[] = {
  152. { "max14577", 0 },
  153. { }
  154. };
  155. MODULE_DEVICE_TABLE(i2c, max14577_i2c_id);
  156. #ifdef CONFIG_PM_SLEEP
  157. static int max14577_suspend(struct device *dev)
  158. {
  159. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  160. struct max14577 *max14577 = i2c_get_clientdata(i2c);
  161. if (device_may_wakeup(dev)) {
  162. enable_irq_wake(max14577->irq);
  163. /*
  164. * MUIC IRQ must be disabled during suspend if this is
  165. * a wake up source because it will be handled before
  166. * resuming I2C.
  167. *
  168. * When device is woken up from suspend (e.g. by ADC change),
  169. * an interrupt occurs before resuming I2C bus controller.
  170. * Interrupt handler tries to read registers but this read
  171. * will fail because I2C is still suspended.
  172. */
  173. disable_irq(max14577->irq);
  174. }
  175. return 0;
  176. }
  177. static int max14577_resume(struct device *dev)
  178. {
  179. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  180. struct max14577 *max14577 = i2c_get_clientdata(i2c);
  181. if (device_may_wakeup(dev)) {
  182. disable_irq_wake(max14577->irq);
  183. enable_irq(max14577->irq);
  184. }
  185. return 0;
  186. }
  187. #endif /* CONFIG_PM_SLEEP */
  188. static struct of_device_id max14577_dt_match[] = {
  189. { .compatible = "maxim,max14577", },
  190. {},
  191. };
  192. static SIMPLE_DEV_PM_OPS(max14577_pm, max14577_suspend, max14577_resume);
  193. static struct i2c_driver max14577_i2c_driver = {
  194. .driver = {
  195. .name = "max14577",
  196. .owner = THIS_MODULE,
  197. .pm = &max14577_pm,
  198. .of_match_table = max14577_dt_match,
  199. },
  200. .probe = max14577_i2c_probe,
  201. .remove = max14577_i2c_remove,
  202. .id_table = max14577_i2c_id,
  203. };
  204. static int __init max14577_i2c_init(void)
  205. {
  206. return i2c_add_driver(&max14577_i2c_driver);
  207. }
  208. subsys_initcall(max14577_i2c_init);
  209. static void __exit max14577_i2c_exit(void)
  210. {
  211. i2c_del_driver(&max14577_i2c_driver);
  212. }
  213. module_exit(max14577_i2c_exit);
  214. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <k.kozlowski@samsung.com>");
  215. MODULE_DESCRIPTION("MAXIM 14577 multi-function core driver");
  216. MODULE_LICENSE("GPL");