max14577.c 6.5 KB

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