max77686.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 struct regmap_config max77686_regmap_config = {
  101. .reg_bits = 8,
  102. .val_bits = 8,
  103. };
  104. static struct regmap_config max77686_rtc_regmap_config = {
  105. .reg_bits = 8,
  106. .val_bits = 8,
  107. };
  108. static 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 struct max77686_platform_data *max77686_i2c_parse_dt_pdata(struct device
  185. *dev)
  186. {
  187. struct max77686_platform_data *pd;
  188. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  189. if (!pd)
  190. return NULL;
  191. dev->platform_data = pd;
  192. return pd;
  193. }
  194. static int max77686_i2c_probe(struct i2c_client *i2c,
  195. const struct i2c_device_id *id)
  196. {
  197. struct max77686_dev *max77686 = NULL;
  198. struct max77686_platform_data *pdata = dev_get_platdata(&i2c->dev);
  199. const struct of_device_id *match;
  200. unsigned int data;
  201. int ret = 0;
  202. const struct regmap_config *config;
  203. const struct regmap_irq_chip *irq_chip;
  204. const struct regmap_irq_chip *rtc_irq_chip;
  205. struct regmap **rtc_regmap;
  206. const struct mfd_cell *cells;
  207. int n_devs;
  208. if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node && !pdata)
  209. pdata = max77686_i2c_parse_dt_pdata(&i2c->dev);
  210. if (!pdata) {
  211. dev_err(&i2c->dev, "No platform data found.\n");
  212. return -EINVAL;
  213. }
  214. max77686 = devm_kzalloc(&i2c->dev,
  215. sizeof(struct max77686_dev), GFP_KERNEL);
  216. if (!max77686)
  217. return -ENOMEM;
  218. if (i2c->dev.of_node) {
  219. match = of_match_node(max77686_pmic_dt_match, i2c->dev.of_node);
  220. if (!match)
  221. return -EINVAL;
  222. max77686->type = (unsigned long)match->data;
  223. } else
  224. max77686->type = id->driver_data;
  225. i2c_set_clientdata(i2c, max77686);
  226. max77686->dev = &i2c->dev;
  227. max77686->i2c = i2c;
  228. max77686->wakeup = pdata->wakeup;
  229. max77686->irq = i2c->irq;
  230. if (max77686->type == TYPE_MAX77686) {
  231. config = &max77686_regmap_config;
  232. irq_chip = &max77686_irq_chip;
  233. rtc_irq_chip = &max77686_rtc_irq_chip;
  234. rtc_regmap = &max77686->rtc_regmap;
  235. cells = max77686_devs;
  236. n_devs = ARRAY_SIZE(max77686_devs);
  237. } else {
  238. config = &max77802_regmap_config;
  239. irq_chip = &max77802_irq_chip;
  240. rtc_irq_chip = &max77802_rtc_irq_chip;
  241. rtc_regmap = &max77686->regmap;
  242. cells = max77802_devs;
  243. n_devs = ARRAY_SIZE(max77802_devs);
  244. }
  245. max77686->regmap = devm_regmap_init_i2c(i2c, config);
  246. if (IS_ERR(max77686->regmap)) {
  247. ret = PTR_ERR(max77686->regmap);
  248. dev_err(max77686->dev, "Failed to allocate register map: %d\n",
  249. ret);
  250. return ret;
  251. }
  252. ret = regmap_read(max77686->regmap, MAX77686_REG_DEVICE_ID, &data);
  253. if (ret < 0) {
  254. dev_err(max77686->dev,
  255. "device not found on this channel (this is not an error)\n");
  256. return -ENODEV;
  257. }
  258. if (max77686->type == TYPE_MAX77686) {
  259. max77686->rtc = i2c_new_dummy(i2c->adapter, I2C_ADDR_RTC);
  260. if (!max77686->rtc) {
  261. dev_err(max77686->dev,
  262. "Failed to allocate I2C device for RTC\n");
  263. return -ENODEV;
  264. }
  265. i2c_set_clientdata(max77686->rtc, max77686);
  266. max77686->rtc_regmap =
  267. devm_regmap_init_i2c(max77686->rtc,
  268. &max77686_rtc_regmap_config);
  269. if (IS_ERR(max77686->rtc_regmap)) {
  270. ret = PTR_ERR(max77686->rtc_regmap);
  271. dev_err(max77686->dev,
  272. "failed to allocate RTC regmap: %d\n",
  273. ret);
  274. goto err_unregister_i2c;
  275. }
  276. }
  277. ret = regmap_add_irq_chip(max77686->regmap, max77686->irq,
  278. IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
  279. IRQF_SHARED, 0, irq_chip,
  280. &max77686->irq_data);
  281. if (ret) {
  282. dev_err(&i2c->dev, "failed to add PMIC irq chip: %d\n", ret);
  283. goto err_unregister_i2c;
  284. }
  285. ret = regmap_add_irq_chip(*rtc_regmap, max77686->irq,
  286. IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
  287. IRQF_SHARED, 0, rtc_irq_chip,
  288. &max77686->rtc_irq_data);
  289. if (ret) {
  290. dev_err(&i2c->dev, "failed to add RTC irq chip: %d\n", ret);
  291. goto err_del_irqc;
  292. }
  293. ret = mfd_add_devices(max77686->dev, -1, cells, n_devs, NULL, 0, NULL);
  294. if (ret < 0) {
  295. dev_err(&i2c->dev, "failed to add MFD devices: %d\n", ret);
  296. goto err_del_rtc_irqc;
  297. }
  298. return 0;
  299. err_del_rtc_irqc:
  300. regmap_del_irq_chip(max77686->irq, max77686->rtc_irq_data);
  301. err_del_irqc:
  302. regmap_del_irq_chip(max77686->irq, max77686->irq_data);
  303. err_unregister_i2c:
  304. if (max77686->type == TYPE_MAX77686)
  305. i2c_unregister_device(max77686->rtc);
  306. return ret;
  307. }
  308. static int max77686_i2c_remove(struct i2c_client *i2c)
  309. {
  310. struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
  311. mfd_remove_devices(max77686->dev);
  312. regmap_del_irq_chip(max77686->irq, max77686->rtc_irq_data);
  313. regmap_del_irq_chip(max77686->irq, max77686->irq_data);
  314. if (max77686->type == TYPE_MAX77686)
  315. i2c_unregister_device(max77686->rtc);
  316. return 0;
  317. }
  318. static const struct i2c_device_id max77686_i2c_id[] = {
  319. { "max77686", TYPE_MAX77686 },
  320. { }
  321. };
  322. MODULE_DEVICE_TABLE(i2c, max77686_i2c_id);
  323. #ifdef CONFIG_PM_SLEEP
  324. static int max77686_suspend(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. enable_irq_wake(max77686->irq);
  330. /*
  331. * IRQ must be disabled during suspend because if it happens
  332. * while suspended it will be handled before resuming I2C.
  333. *
  334. * When device is woken up from suspend (e.g. by RTC wake alarm),
  335. * an interrupt occurs before resuming I2C bus controller.
  336. * Interrupt handler tries to read registers but this read
  337. * will fail because I2C is still suspended.
  338. */
  339. disable_irq(max77686->irq);
  340. return 0;
  341. }
  342. static int max77686_resume(struct device *dev)
  343. {
  344. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  345. struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
  346. if (device_may_wakeup(dev))
  347. disable_irq_wake(max77686->irq);
  348. enable_irq(max77686->irq);
  349. return 0;
  350. }
  351. #endif /* CONFIG_PM_SLEEP */
  352. static SIMPLE_DEV_PM_OPS(max77686_pm, max77686_suspend, max77686_resume);
  353. static struct i2c_driver max77686_i2c_driver = {
  354. .driver = {
  355. .name = "max77686",
  356. .owner = THIS_MODULE,
  357. .pm = &max77686_pm,
  358. .of_match_table = of_match_ptr(max77686_pmic_dt_match),
  359. },
  360. .probe = max77686_i2c_probe,
  361. .remove = max77686_i2c_remove,
  362. .id_table = max77686_i2c_id,
  363. };
  364. static int __init max77686_i2c_init(void)
  365. {
  366. return i2c_add_driver(&max77686_i2c_driver);
  367. }
  368. /* init early so consumer devices can complete system boot */
  369. subsys_initcall(max77686_i2c_init);
  370. static void __exit max77686_i2c_exit(void)
  371. {
  372. i2c_del_driver(&max77686_i2c_driver);
  373. }
  374. module_exit(max77686_i2c_exit);
  375. MODULE_DESCRIPTION("MAXIM 77686/802 multi-function core driver");
  376. MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
  377. MODULE_LICENSE("GPL");