max77843.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * MFD core driver for the Maxim MAX77843
  3. *
  4. * Copyright (C) 2015 Samsung Electronics
  5. * Author: Jaewon Kim <jaewon02.kim@samsung.com>
  6. * Author: Beomho Seo <beomho.seo@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. #include <linux/err.h>
  14. #include <linux/i2c.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/mfd/core.h>
  19. #include <linux/mfd/max77843-private.h>
  20. #include <linux/of_device.h>
  21. #include <linux/platform_device.h>
  22. static const struct mfd_cell max77843_devs[] = {
  23. {
  24. .name = "max77843-muic",
  25. .of_compatible = "maxim,max77843-muic",
  26. }, {
  27. .name = "max77843-regulator",
  28. .of_compatible = "maxim,max77843-regulator",
  29. }, {
  30. .name = "max77843-charger",
  31. .of_compatible = "maxim,max77843-charger"
  32. }, {
  33. .name = "max77843-fuelgauge",
  34. .of_compatible = "maxim,max77843-fuelgauge",
  35. }, {
  36. .name = "max77843-haptic",
  37. .of_compatible = "maxim,max77843-haptic",
  38. },
  39. };
  40. static const struct regmap_config max77843_charger_regmap_config = {
  41. .reg_bits = 8,
  42. .val_bits = 8,
  43. .max_register = MAX77843_CHG_REG_END,
  44. };
  45. static const struct regmap_config max77843_regmap_config = {
  46. .reg_bits = 8,
  47. .val_bits = 8,
  48. .max_register = MAX77843_SYS_REG_END,
  49. };
  50. static const struct regmap_irq max77843_irqs[] = {
  51. /* TOPSYS interrupts */
  52. { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_SYSUVLO_INT, },
  53. { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_SYSOVLO_INT, },
  54. { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_TSHDN_INT, },
  55. { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_TM_INT, },
  56. };
  57. static const struct regmap_irq_chip max77843_irq_chip = {
  58. .name = "max77843",
  59. .status_base = MAX77843_SYS_REG_SYSINTSRC,
  60. .mask_base = MAX77843_SYS_REG_SYSINTMASK,
  61. .mask_invert = false,
  62. .num_regs = 1,
  63. .irqs = max77843_irqs,
  64. .num_irqs = ARRAY_SIZE(max77843_irqs),
  65. };
  66. /* Charger and Charger regulator use same regmap. */
  67. static int max77843_chg_init(struct max77843 *max77843)
  68. {
  69. int ret;
  70. max77843->i2c_chg = i2c_new_dummy(max77843->i2c->adapter, I2C_ADDR_CHG);
  71. if (!max77843->i2c_chg) {
  72. dev_err(&max77843->i2c->dev,
  73. "Cannot allocate I2C device for Charger\n");
  74. return PTR_ERR(max77843->i2c_chg);
  75. }
  76. i2c_set_clientdata(max77843->i2c_chg, max77843);
  77. max77843->regmap_chg = devm_regmap_init_i2c(max77843->i2c_chg,
  78. &max77843_charger_regmap_config);
  79. if (IS_ERR(max77843->regmap_chg)) {
  80. ret = PTR_ERR(max77843->regmap_chg);
  81. goto err_chg_i2c;
  82. }
  83. return 0;
  84. err_chg_i2c:
  85. i2c_unregister_device(max77843->i2c_chg);
  86. return ret;
  87. }
  88. static int max77843_probe(struct i2c_client *i2c,
  89. const struct i2c_device_id *id)
  90. {
  91. struct max77843 *max77843;
  92. unsigned int reg_data;
  93. int ret;
  94. max77843 = devm_kzalloc(&i2c->dev, sizeof(*max77843), GFP_KERNEL);
  95. if (!max77843)
  96. return -ENOMEM;
  97. i2c_set_clientdata(i2c, max77843);
  98. max77843->dev = &i2c->dev;
  99. max77843->i2c = i2c;
  100. max77843->irq = i2c->irq;
  101. max77843->regmap = devm_regmap_init_i2c(i2c,
  102. &max77843_regmap_config);
  103. if (IS_ERR(max77843->regmap)) {
  104. dev_err(&i2c->dev, "Failed to allocate topsys register map\n");
  105. return PTR_ERR(max77843->regmap);
  106. }
  107. ret = regmap_add_irq_chip(max77843->regmap, max77843->irq,
  108. IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
  109. 0, &max77843_irq_chip, &max77843->irq_data);
  110. if (ret) {
  111. dev_err(&i2c->dev, "Failed to add TOPSYS IRQ chip\n");
  112. return ret;
  113. }
  114. ret = regmap_read(max77843->regmap,
  115. MAX77843_SYS_REG_PMICID, &reg_data);
  116. if (ret < 0) {
  117. dev_err(&i2c->dev, "Failed to read PMIC ID\n");
  118. goto err_pmic_id;
  119. }
  120. dev_info(&i2c->dev, "device ID: 0x%x\n", reg_data);
  121. ret = max77843_chg_init(max77843);
  122. if (ret) {
  123. dev_err(&i2c->dev, "Failed to init Charger\n");
  124. goto err_pmic_id;
  125. }
  126. ret = regmap_update_bits(max77843->regmap,
  127. MAX77843_SYS_REG_INTSRCMASK,
  128. MAX77843_INTSRC_MASK_MASK,
  129. (unsigned int)~MAX77843_INTSRC_MASK_MASK);
  130. if (ret < 0) {
  131. dev_err(&i2c->dev, "Failed to unmask interrupt source\n");
  132. goto err_pmic_id;
  133. }
  134. ret = mfd_add_devices(max77843->dev, -1, max77843_devs,
  135. ARRAY_SIZE(max77843_devs), NULL, 0, NULL);
  136. if (ret < 0) {
  137. dev_err(&i2c->dev, "Failed to add mfd device\n");
  138. goto err_pmic_id;
  139. }
  140. device_init_wakeup(max77843->dev, true);
  141. return 0;
  142. err_pmic_id:
  143. regmap_del_irq_chip(max77843->irq, max77843->irq_data);
  144. return ret;
  145. }
  146. static int max77843_remove(struct i2c_client *i2c)
  147. {
  148. struct max77843 *max77843 = i2c_get_clientdata(i2c);
  149. mfd_remove_devices(max77843->dev);
  150. regmap_del_irq_chip(max77843->irq, max77843->irq_data);
  151. i2c_unregister_device(max77843->i2c_chg);
  152. return 0;
  153. }
  154. static const struct of_device_id max77843_dt_match[] = {
  155. { .compatible = "maxim,max77843", },
  156. { },
  157. };
  158. static const struct i2c_device_id max77843_id[] = {
  159. { "max77843", },
  160. { },
  161. };
  162. MODULE_DEVICE_TABLE(i2c, max77843_id);
  163. static int __maybe_unused max77843_suspend(struct device *dev)
  164. {
  165. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  166. struct max77843 *max77843 = i2c_get_clientdata(i2c);
  167. disable_irq(max77843->irq);
  168. if (device_may_wakeup(dev))
  169. enable_irq_wake(max77843->irq);
  170. return 0;
  171. }
  172. static int __maybe_unused max77843_resume(struct device *dev)
  173. {
  174. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  175. struct max77843 *max77843 = i2c_get_clientdata(i2c);
  176. if (device_may_wakeup(dev))
  177. disable_irq_wake(max77843->irq);
  178. enable_irq(max77843->irq);
  179. return 0;
  180. }
  181. static SIMPLE_DEV_PM_OPS(max77843_pm, max77843_suspend, max77843_resume);
  182. static struct i2c_driver max77843_i2c_driver = {
  183. .driver = {
  184. .name = "max77843",
  185. .pm = &max77843_pm,
  186. .of_match_table = max77843_dt_match,
  187. },
  188. .probe = max77843_probe,
  189. .remove = max77843_remove,
  190. .id_table = max77843_id,
  191. };
  192. static int __init max77843_i2c_init(void)
  193. {
  194. return i2c_add_driver(&max77843_i2c_driver);
  195. }
  196. subsys_initcall(max77843_i2c_init);
  197. static void __exit max77843_i2c_exit(void)
  198. {
  199. i2c_del_driver(&max77843_i2c_driver);
  200. }
  201. module_exit(max77843_i2c_exit);