max77686.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * max77686.c - mfd core driver for the Maxim 77686/802
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * Chiwoong Byun <woong.byun@samsung.com>
  6. * Jonghwa Lee <jonghwa3.lee@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. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * This driver is based on max8997.c
  23. */
  24. #include <linux/export.h>
  25. #include <linux/slab.h>
  26. #include <linux/i2c.h>
  27. #include <linux/irq.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/module.h>
  31. #include <linux/mfd/core.h>
  32. #include <linux/mfd/max77686.h>
  33. #include <linux/mfd/max77686-private.h>
  34. #include <linux/err.h>
  35. #include <linux/of.h>
  36. static const struct mfd_cell max77686_devs[] = {
  37. { .name = "max77686-pmic", },
  38. { .name = "max77686-rtc", },
  39. { .name = "max77686-clk", },
  40. };
  41. static const struct mfd_cell max77802_devs[] = {
  42. { .name = "max77802-pmic", },
  43. { .name = "max77802-clk", },
  44. { .name = "max77802-rtc", },
  45. };
  46. static bool max77802_pmic_is_accessible_reg(struct device *dev,
  47. unsigned int reg)
  48. {
  49. return reg < MAX77802_REG_PMIC_END;
  50. }
  51. static bool max77802_rtc_is_accessible_reg(struct device *dev,
  52. unsigned int reg)
  53. {
  54. return (reg >= MAX77802_RTC_INT && reg < MAX77802_RTC_END);
  55. }
  56. static bool max77802_is_accessible_reg(struct device *dev, unsigned int reg)
  57. {
  58. return (max77802_pmic_is_accessible_reg(dev, reg) ||
  59. max77802_rtc_is_accessible_reg(dev, reg));
  60. }
  61. static bool max77802_pmic_is_precious_reg(struct device *dev, unsigned int reg)
  62. {
  63. return (reg == MAX77802_REG_INTSRC || reg == MAX77802_REG_INT1 ||
  64. reg == MAX77802_REG_INT2);
  65. }
  66. static bool max77802_rtc_is_precious_reg(struct device *dev, unsigned int reg)
  67. {
  68. return (reg == MAX77802_RTC_INT ||
  69. reg == MAX77802_RTC_UPDATE0 ||
  70. reg == MAX77802_RTC_UPDATE1);
  71. }
  72. static bool max77802_is_precious_reg(struct device *dev, unsigned int reg)
  73. {
  74. return (max77802_pmic_is_precious_reg(dev, reg) ||
  75. max77802_rtc_is_precious_reg(dev, reg));
  76. }
  77. static bool max77802_pmic_is_volatile_reg(struct device *dev, unsigned int reg)
  78. {
  79. return (max77802_is_precious_reg(dev, reg) ||
  80. reg == MAX77802_REG_STATUS1 || reg == MAX77802_REG_STATUS2 ||
  81. reg == MAX77802_REG_PWRON);
  82. }
  83. static bool max77802_rtc_is_volatile_reg(struct device *dev, unsigned int reg)
  84. {
  85. return (max77802_rtc_is_precious_reg(dev, reg) ||
  86. reg == MAX77802_RTC_SEC ||
  87. reg == MAX77802_RTC_MIN ||
  88. reg == MAX77802_RTC_HOUR ||
  89. reg == MAX77802_RTC_WEEKDAY ||
  90. reg == MAX77802_RTC_MONTH ||
  91. reg == MAX77802_RTC_YEAR ||
  92. reg == MAX77802_RTC_DATE);
  93. }
  94. static bool max77802_is_volatile_reg(struct device *dev, unsigned int reg)
  95. {
  96. return (max77802_pmic_is_volatile_reg(dev, reg) ||
  97. max77802_rtc_is_volatile_reg(dev, reg));
  98. }
  99. static const struct regmap_config max77686_regmap_config = {
  100. .reg_bits = 8,
  101. .val_bits = 8,
  102. };
  103. static const struct regmap_config max77802_regmap_config = {
  104. .reg_bits = 8,
  105. .val_bits = 8,
  106. .writeable_reg = max77802_is_accessible_reg,
  107. .readable_reg = max77802_is_accessible_reg,
  108. .precious_reg = max77802_is_precious_reg,
  109. .volatile_reg = max77802_is_volatile_reg,
  110. .name = "max77802-pmic",
  111. .cache_type = REGCACHE_RBTREE,
  112. };
  113. static const struct regmap_irq max77686_irqs[] = {
  114. /* INT1 interrupts */
  115. { .reg_offset = 0, .mask = MAX77686_INT1_PWRONF_MSK, },
  116. { .reg_offset = 0, .mask = MAX77686_INT1_PWRONR_MSK, },
  117. { .reg_offset = 0, .mask = MAX77686_INT1_JIGONBF_MSK, },
  118. { .reg_offset = 0, .mask = MAX77686_INT1_JIGONBR_MSK, },
  119. { .reg_offset = 0, .mask = MAX77686_INT1_ACOKBF_MSK, },
  120. { .reg_offset = 0, .mask = MAX77686_INT1_ACOKBR_MSK, },
  121. { .reg_offset = 0, .mask = MAX77686_INT1_ONKEY1S_MSK, },
  122. { .reg_offset = 0, .mask = MAX77686_INT1_MRSTB_MSK, },
  123. /* INT2 interrupts */
  124. { .reg_offset = 1, .mask = MAX77686_INT2_140C_MSK, },
  125. { .reg_offset = 1, .mask = MAX77686_INT2_120C_MSK, },
  126. };
  127. static const struct regmap_irq_chip max77686_irq_chip = {
  128. .name = "max77686-pmic",
  129. .status_base = MAX77686_REG_INT1,
  130. .mask_base = MAX77686_REG_INT1MSK,
  131. .num_regs = 2,
  132. .irqs = max77686_irqs,
  133. .num_irqs = ARRAY_SIZE(max77686_irqs),
  134. };
  135. static const struct regmap_irq_chip max77802_irq_chip = {
  136. .name = "max77802-pmic",
  137. .status_base = MAX77802_REG_INT1,
  138. .mask_base = MAX77802_REG_INT1MSK,
  139. .num_regs = 2,
  140. .irqs = max77686_irqs, /* same masks as 77686 */
  141. .num_irqs = ARRAY_SIZE(max77686_irqs),
  142. };
  143. static const struct of_device_id max77686_pmic_dt_match[] = {
  144. {
  145. .compatible = "maxim,max77686",
  146. .data = (void *)TYPE_MAX77686,
  147. },
  148. {
  149. .compatible = "maxim,max77802",
  150. .data = (void *)TYPE_MAX77802,
  151. },
  152. { },
  153. };
  154. MODULE_DEVICE_TABLE(of, max77686_pmic_dt_match);
  155. static int max77686_i2c_probe(struct i2c_client *i2c,
  156. const struct i2c_device_id *id)
  157. {
  158. struct max77686_dev *max77686 = NULL;
  159. const struct of_device_id *match;
  160. unsigned int data;
  161. int ret = 0;
  162. const struct regmap_config *config;
  163. const struct regmap_irq_chip *irq_chip;
  164. const struct mfd_cell *cells;
  165. int n_devs;
  166. max77686 = devm_kzalloc(&i2c->dev,
  167. sizeof(struct max77686_dev), GFP_KERNEL);
  168. if (!max77686)
  169. return -ENOMEM;
  170. if (i2c->dev.of_node) {
  171. match = of_match_node(max77686_pmic_dt_match, i2c->dev.of_node);
  172. if (!match)
  173. return -EINVAL;
  174. max77686->type = (unsigned long)match->data;
  175. } else
  176. max77686->type = id->driver_data;
  177. i2c_set_clientdata(i2c, max77686);
  178. max77686->dev = &i2c->dev;
  179. max77686->i2c = i2c;
  180. max77686->irq = i2c->irq;
  181. if (max77686->type == TYPE_MAX77686) {
  182. config = &max77686_regmap_config;
  183. irq_chip = &max77686_irq_chip;
  184. cells = max77686_devs;
  185. n_devs = ARRAY_SIZE(max77686_devs);
  186. } else {
  187. config = &max77802_regmap_config;
  188. irq_chip = &max77802_irq_chip;
  189. cells = max77802_devs;
  190. n_devs = ARRAY_SIZE(max77802_devs);
  191. }
  192. max77686->regmap = devm_regmap_init_i2c(i2c, config);
  193. if (IS_ERR(max77686->regmap)) {
  194. ret = PTR_ERR(max77686->regmap);
  195. dev_err(max77686->dev, "Failed to allocate register map: %d\n",
  196. ret);
  197. return ret;
  198. }
  199. ret = regmap_read(max77686->regmap, MAX77686_REG_DEVICE_ID, &data);
  200. if (ret < 0) {
  201. dev_err(max77686->dev,
  202. "device not found on this channel (this is not an error)\n");
  203. return -ENODEV;
  204. }
  205. ret = devm_regmap_add_irq_chip(&i2c->dev, max77686->regmap,
  206. max77686->irq,
  207. IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
  208. IRQF_SHARED, 0, irq_chip,
  209. &max77686->irq_data);
  210. if (ret < 0) {
  211. dev_err(&i2c->dev, "failed to add PMIC irq chip: %d\n", ret);
  212. return ret;
  213. }
  214. ret = devm_mfd_add_devices(max77686->dev, -1, cells, n_devs, NULL,
  215. 0, NULL);
  216. if (ret < 0) {
  217. dev_err(&i2c->dev, "failed to add MFD devices: %d\n", ret);
  218. return ret;
  219. }
  220. return 0;
  221. }
  222. static const struct i2c_device_id max77686_i2c_id[] = {
  223. { "max77686", TYPE_MAX77686 },
  224. { "max77802", TYPE_MAX77802 },
  225. { }
  226. };
  227. MODULE_DEVICE_TABLE(i2c, max77686_i2c_id);
  228. #ifdef CONFIG_PM_SLEEP
  229. static int max77686_suspend(struct device *dev)
  230. {
  231. struct i2c_client *i2c = to_i2c_client(dev);
  232. struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
  233. if (device_may_wakeup(dev))
  234. enable_irq_wake(max77686->irq);
  235. /*
  236. * IRQ must be disabled during suspend because if it happens
  237. * while suspended it will be handled before resuming I2C.
  238. *
  239. * When device is woken up from suspend (e.g. by RTC wake alarm),
  240. * an interrupt occurs before resuming I2C bus controller.
  241. * Interrupt handler tries to read registers but this read
  242. * will fail because I2C is still suspended.
  243. */
  244. disable_irq(max77686->irq);
  245. return 0;
  246. }
  247. static int max77686_resume(struct device *dev)
  248. {
  249. struct i2c_client *i2c = to_i2c_client(dev);
  250. struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
  251. if (device_may_wakeup(dev))
  252. disable_irq_wake(max77686->irq);
  253. enable_irq(max77686->irq);
  254. return 0;
  255. }
  256. #endif /* CONFIG_PM_SLEEP */
  257. static SIMPLE_DEV_PM_OPS(max77686_pm, max77686_suspend, max77686_resume);
  258. static struct i2c_driver max77686_i2c_driver = {
  259. .driver = {
  260. .name = "max77686",
  261. .pm = &max77686_pm,
  262. .of_match_table = of_match_ptr(max77686_pmic_dt_match),
  263. },
  264. .probe = max77686_i2c_probe,
  265. .id_table = max77686_i2c_id,
  266. };
  267. module_i2c_driver(max77686_i2c_driver);
  268. MODULE_DESCRIPTION("MAXIM 77686/802 multi-function core driver");
  269. MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
  270. MODULE_LICENSE("GPL");