max77686.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * max77686.c - mfd core driver for the Maxim 77686/802
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * Chiwoong Byun <woong.byun@smasung.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. #define I2C_ADDR_RTC (0x0C >> 1)
  37. static const struct mfd_cell max77686_devs[] = {
  38. { .name = "max77686-pmic", },
  39. { .name = "max77686-rtc", },
  40. { .name = "max77686-clk", },
  41. };
  42. static const struct mfd_cell max77802_devs[] = {
  43. { .name = "max77802-pmic", },
  44. { .name = "max77802-clk", },
  45. { .name = "max77802-rtc", },
  46. };
  47. static bool max77802_pmic_is_accessible_reg(struct device *dev,
  48. unsigned int reg)
  49. {
  50. return reg < MAX77802_REG_PMIC_END;
  51. }
  52. static bool max77802_rtc_is_accessible_reg(struct device *dev,
  53. unsigned int reg)
  54. {
  55. return (reg >= MAX77802_RTC_INT && reg < MAX77802_RTC_END);
  56. }
  57. static bool max77802_is_accessible_reg(struct device *dev, unsigned int reg)
  58. {
  59. return (max77802_pmic_is_accessible_reg(dev, reg) ||
  60. max77802_rtc_is_accessible_reg(dev, reg));
  61. }
  62. static bool max77802_pmic_is_precious_reg(struct device *dev, unsigned int reg)
  63. {
  64. return (reg == MAX77802_REG_INTSRC || reg == MAX77802_REG_INT1 ||
  65. reg == MAX77802_REG_INT2);
  66. }
  67. static bool max77802_rtc_is_precious_reg(struct device *dev, unsigned int reg)
  68. {
  69. return (reg == MAX77802_RTC_INT ||
  70. reg == MAX77802_RTC_UPDATE0 ||
  71. reg == MAX77802_RTC_UPDATE1);
  72. }
  73. static bool max77802_is_precious_reg(struct device *dev, unsigned int reg)
  74. {
  75. return (max77802_pmic_is_precious_reg(dev, reg) ||
  76. max77802_rtc_is_precious_reg(dev, reg));
  77. }
  78. static bool max77802_pmic_is_volatile_reg(struct device *dev, unsigned int reg)
  79. {
  80. return (max77802_is_precious_reg(dev, reg) ||
  81. reg == MAX77802_REG_STATUS1 || reg == MAX77802_REG_STATUS2 ||
  82. reg == MAX77802_REG_PWRON);
  83. }
  84. static bool max77802_rtc_is_volatile_reg(struct device *dev, unsigned int reg)
  85. {
  86. return (max77802_rtc_is_precious_reg(dev, reg) ||
  87. reg == MAX77802_RTC_SEC ||
  88. reg == MAX77802_RTC_MIN ||
  89. reg == MAX77802_RTC_HOUR ||
  90. reg == MAX77802_RTC_WEEKDAY ||
  91. reg == MAX77802_RTC_MONTH ||
  92. reg == MAX77802_RTC_YEAR ||
  93. reg == MAX77802_RTC_DATE);
  94. }
  95. static bool max77802_is_volatile_reg(struct device *dev, unsigned int reg)
  96. {
  97. return (max77802_pmic_is_volatile_reg(dev, reg) ||
  98. max77802_rtc_is_volatile_reg(dev, reg));
  99. }
  100. static const struct regmap_config max77686_regmap_config = {
  101. .reg_bits = 8,
  102. .val_bits = 8,
  103. };
  104. static const struct regmap_config max77686_rtc_regmap_config = {
  105. .reg_bits = 8,
  106. .val_bits = 8,
  107. };
  108. static const struct regmap_config max77802_regmap_config = {
  109. .reg_bits = 8,
  110. .val_bits = 8,
  111. .writeable_reg = max77802_is_accessible_reg,
  112. .readable_reg = max77802_is_accessible_reg,
  113. .precious_reg = max77802_is_precious_reg,
  114. .volatile_reg = max77802_is_volatile_reg,
  115. .name = "max77802-pmic",
  116. .cache_type = REGCACHE_RBTREE,
  117. };
  118. static const struct regmap_irq max77686_irqs[] = {
  119. /* INT1 interrupts */
  120. { .reg_offset = 0, .mask = MAX77686_INT1_PWRONF_MSK, },
  121. { .reg_offset = 0, .mask = MAX77686_INT1_PWRONR_MSK, },
  122. { .reg_offset = 0, .mask = MAX77686_INT1_JIGONBF_MSK, },
  123. { .reg_offset = 0, .mask = MAX77686_INT1_JIGONBR_MSK, },
  124. { .reg_offset = 0, .mask = MAX77686_INT1_ACOKBF_MSK, },
  125. { .reg_offset = 0, .mask = MAX77686_INT1_ACOKBR_MSK, },
  126. { .reg_offset = 0, .mask = MAX77686_INT1_ONKEY1S_MSK, },
  127. { .reg_offset = 0, .mask = MAX77686_INT1_MRSTB_MSK, },
  128. /* INT2 interrupts */
  129. { .reg_offset = 1, .mask = MAX77686_INT2_140C_MSK, },
  130. { .reg_offset = 1, .mask = MAX77686_INT2_120C_MSK, },
  131. };
  132. static const struct regmap_irq_chip max77686_irq_chip = {
  133. .name = "max77686-pmic",
  134. .status_base = MAX77686_REG_INT1,
  135. .mask_base = MAX77686_REG_INT1MSK,
  136. .num_regs = 2,
  137. .irqs = max77686_irqs,
  138. .num_irqs = ARRAY_SIZE(max77686_irqs),
  139. };
  140. static const struct regmap_irq max77686_rtc_irqs[] = {
  141. /* RTC interrupts */
  142. { .reg_offset = 0, .mask = MAX77686_RTCINT_RTC60S_MSK, },
  143. { .reg_offset = 0, .mask = MAX77686_RTCINT_RTCA1_MSK, },
  144. { .reg_offset = 0, .mask = MAX77686_RTCINT_RTCA2_MSK, },
  145. { .reg_offset = 0, .mask = MAX77686_RTCINT_SMPL_MSK, },
  146. { .reg_offset = 0, .mask = MAX77686_RTCINT_RTC1S_MSK, },
  147. { .reg_offset = 0, .mask = MAX77686_RTCINT_WTSR_MSK, },
  148. };
  149. static const struct regmap_irq_chip max77686_rtc_irq_chip = {
  150. .name = "max77686-rtc",
  151. .status_base = MAX77686_RTC_INT,
  152. .mask_base = MAX77686_RTC_INTM,
  153. .num_regs = 1,
  154. .irqs = max77686_rtc_irqs,
  155. .num_irqs = ARRAY_SIZE(max77686_rtc_irqs),
  156. };
  157. static const struct regmap_irq_chip max77802_irq_chip = {
  158. .name = "max77802-pmic",
  159. .status_base = MAX77802_REG_INT1,
  160. .mask_base = MAX77802_REG_INT1MSK,
  161. .num_regs = 2,
  162. .irqs = max77686_irqs, /* same masks as 77686 */
  163. .num_irqs = ARRAY_SIZE(max77686_irqs),
  164. };
  165. static const struct regmap_irq_chip max77802_rtc_irq_chip = {
  166. .name = "max77802-rtc",
  167. .status_base = MAX77802_RTC_INT,
  168. .mask_base = MAX77802_RTC_INTM,
  169. .num_regs = 1,
  170. .irqs = max77686_rtc_irqs, /* same masks as 77686 */
  171. .num_irqs = ARRAY_SIZE(max77686_rtc_irqs),
  172. };
  173. static const struct of_device_id max77686_pmic_dt_match[] = {
  174. {
  175. .compatible = "maxim,max77686",
  176. .data = (void *)TYPE_MAX77686,
  177. },
  178. {
  179. .compatible = "maxim,max77802",
  180. .data = (void *)TYPE_MAX77802,
  181. },
  182. { },
  183. };
  184. static int max77686_i2c_probe(struct i2c_client *i2c,
  185. const struct i2c_device_id *id)
  186. {
  187. struct max77686_dev *max77686 = NULL;
  188. const struct of_device_id *match;
  189. unsigned int data;
  190. int ret = 0;
  191. const struct regmap_config *config;
  192. const struct regmap_irq_chip *irq_chip;
  193. const struct regmap_irq_chip *rtc_irq_chip;
  194. struct regmap **rtc_regmap;
  195. const struct mfd_cell *cells;
  196. int n_devs;
  197. max77686 = devm_kzalloc(&i2c->dev,
  198. sizeof(struct max77686_dev), GFP_KERNEL);
  199. if (!max77686)
  200. return -ENOMEM;
  201. if (i2c->dev.of_node) {
  202. match = of_match_node(max77686_pmic_dt_match, i2c->dev.of_node);
  203. if (!match)
  204. return -EINVAL;
  205. max77686->type = (unsigned long)match->data;
  206. } else
  207. max77686->type = id->driver_data;
  208. i2c_set_clientdata(i2c, max77686);
  209. max77686->dev = &i2c->dev;
  210. max77686->i2c = i2c;
  211. max77686->irq = i2c->irq;
  212. if (max77686->type == TYPE_MAX77686) {
  213. config = &max77686_regmap_config;
  214. irq_chip = &max77686_irq_chip;
  215. rtc_irq_chip = &max77686_rtc_irq_chip;
  216. rtc_regmap = &max77686->rtc_regmap;
  217. cells = max77686_devs;
  218. n_devs = ARRAY_SIZE(max77686_devs);
  219. } else {
  220. config = &max77802_regmap_config;
  221. irq_chip = &max77802_irq_chip;
  222. rtc_irq_chip = &max77802_rtc_irq_chip;
  223. rtc_regmap = &max77686->regmap;
  224. cells = max77802_devs;
  225. n_devs = ARRAY_SIZE(max77802_devs);
  226. }
  227. max77686->regmap = devm_regmap_init_i2c(i2c, config);
  228. if (IS_ERR(max77686->regmap)) {
  229. ret = PTR_ERR(max77686->regmap);
  230. dev_err(max77686->dev, "Failed to allocate register map: %d\n",
  231. ret);
  232. return ret;
  233. }
  234. ret = regmap_read(max77686->regmap, MAX77686_REG_DEVICE_ID, &data);
  235. if (ret < 0) {
  236. dev_err(max77686->dev,
  237. "device not found on this channel (this is not an error)\n");
  238. return -ENODEV;
  239. }
  240. if (max77686->type == TYPE_MAX77686) {
  241. max77686->rtc = i2c_new_dummy(i2c->adapter, I2C_ADDR_RTC);
  242. if (!max77686->rtc) {
  243. dev_err(max77686->dev,
  244. "Failed to allocate I2C device for RTC\n");
  245. return -ENODEV;
  246. }
  247. i2c_set_clientdata(max77686->rtc, max77686);
  248. max77686->rtc_regmap =
  249. devm_regmap_init_i2c(max77686->rtc,
  250. &max77686_rtc_regmap_config);
  251. if (IS_ERR(max77686->rtc_regmap)) {
  252. ret = PTR_ERR(max77686->rtc_regmap);
  253. dev_err(max77686->dev,
  254. "failed to allocate RTC regmap: %d\n",
  255. ret);
  256. goto err_unregister_i2c;
  257. }
  258. }
  259. ret = regmap_add_irq_chip(max77686->regmap, max77686->irq,
  260. IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
  261. IRQF_SHARED, 0, irq_chip,
  262. &max77686->irq_data);
  263. if (ret) {
  264. dev_err(&i2c->dev, "failed to add PMIC irq chip: %d\n", ret);
  265. goto err_unregister_i2c;
  266. }
  267. ret = regmap_add_irq_chip(*rtc_regmap, max77686->irq,
  268. IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
  269. IRQF_SHARED, 0, rtc_irq_chip,
  270. &max77686->rtc_irq_data);
  271. if (ret) {
  272. dev_err(&i2c->dev, "failed to add RTC irq chip: %d\n", ret);
  273. goto err_del_irqc;
  274. }
  275. ret = mfd_add_devices(max77686->dev, -1, cells, n_devs, NULL, 0, NULL);
  276. if (ret < 0) {
  277. dev_err(&i2c->dev, "failed to add MFD devices: %d\n", ret);
  278. goto err_del_rtc_irqc;
  279. }
  280. return 0;
  281. err_del_rtc_irqc:
  282. regmap_del_irq_chip(max77686->irq, max77686->rtc_irq_data);
  283. err_del_irqc:
  284. regmap_del_irq_chip(max77686->irq, max77686->irq_data);
  285. err_unregister_i2c:
  286. if (max77686->type == TYPE_MAX77686)
  287. i2c_unregister_device(max77686->rtc);
  288. return ret;
  289. }
  290. static int max77686_i2c_remove(struct i2c_client *i2c)
  291. {
  292. struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
  293. mfd_remove_devices(max77686->dev);
  294. regmap_del_irq_chip(max77686->irq, max77686->rtc_irq_data);
  295. regmap_del_irq_chip(max77686->irq, max77686->irq_data);
  296. if (max77686->type == TYPE_MAX77686)
  297. i2c_unregister_device(max77686->rtc);
  298. return 0;
  299. }
  300. static const struct i2c_device_id max77686_i2c_id[] = {
  301. { "max77686", TYPE_MAX77686 },
  302. { }
  303. };
  304. MODULE_DEVICE_TABLE(i2c, max77686_i2c_id);
  305. #ifdef CONFIG_PM_SLEEP
  306. static int max77686_suspend(struct device *dev)
  307. {
  308. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  309. struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
  310. if (device_may_wakeup(dev))
  311. enable_irq_wake(max77686->irq);
  312. /*
  313. * IRQ must be disabled during suspend because if it happens
  314. * while suspended it will be handled before resuming I2C.
  315. *
  316. * When device is woken up from suspend (e.g. by RTC wake alarm),
  317. * an interrupt occurs before resuming I2C bus controller.
  318. * Interrupt handler tries to read registers but this read
  319. * will fail because I2C is still suspended.
  320. */
  321. disable_irq(max77686->irq);
  322. return 0;
  323. }
  324. static int max77686_resume(struct device *dev)
  325. {
  326. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  327. struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
  328. if (device_may_wakeup(dev))
  329. disable_irq_wake(max77686->irq);
  330. enable_irq(max77686->irq);
  331. return 0;
  332. }
  333. #endif /* CONFIG_PM_SLEEP */
  334. static SIMPLE_DEV_PM_OPS(max77686_pm, max77686_suspend, max77686_resume);
  335. static struct i2c_driver max77686_i2c_driver = {
  336. .driver = {
  337. .name = "max77686",
  338. .pm = &max77686_pm,
  339. .of_match_table = of_match_ptr(max77686_pmic_dt_match),
  340. },
  341. .probe = max77686_i2c_probe,
  342. .remove = max77686_i2c_remove,
  343. .id_table = max77686_i2c_id,
  344. };
  345. static int __init max77686_i2c_init(void)
  346. {
  347. return i2c_add_driver(&max77686_i2c_driver);
  348. }
  349. /* init early so consumer devices can complete system boot */
  350. subsys_initcall(max77686_i2c_init);
  351. static void __exit max77686_i2c_exit(void)
  352. {
  353. i2c_del_driver(&max77686_i2c_driver);
  354. }
  355. module_exit(max77686_i2c_exit);
  356. MODULE_DESCRIPTION("MAXIM 77686/802 multi-function core driver");
  357. MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
  358. MODULE_LICENSE("GPL");