max8998.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * max8998.c - mfd core driver for the Maxim 8998
  3. *
  4. * Copyright (C) 2009-2010 Samsung Electronics
  5. * Kyungmin Park <kyungmin.park@samsung.com>
  6. * Marek Szyprowski <m.szyprowski@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. #include <linux/err.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/i2c.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/of.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/mutex.h>
  33. #include <linux/mfd/core.h>
  34. #include <linux/mfd/max8998.h>
  35. #include <linux/mfd/max8998-private.h>
  36. #define RTC_I2C_ADDR (0x0c >> 1)
  37. static const struct mfd_cell max8998_devs[] = {
  38. {
  39. .name = "max8998-pmic",
  40. }, {
  41. .name = "max8998-rtc",
  42. }, {
  43. .name = "max8998-battery",
  44. },
  45. };
  46. static const struct mfd_cell lp3974_devs[] = {
  47. {
  48. .name = "lp3974-pmic",
  49. }, {
  50. .name = "lp3974-rtc",
  51. },
  52. };
  53. int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
  54. {
  55. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  56. int ret;
  57. mutex_lock(&max8998->iolock);
  58. ret = i2c_smbus_read_byte_data(i2c, reg);
  59. mutex_unlock(&max8998->iolock);
  60. if (ret < 0)
  61. return ret;
  62. ret &= 0xff;
  63. *dest = ret;
  64. return 0;
  65. }
  66. EXPORT_SYMBOL(max8998_read_reg);
  67. int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  68. {
  69. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  70. int ret;
  71. mutex_lock(&max8998->iolock);
  72. ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
  73. mutex_unlock(&max8998->iolock);
  74. if (ret < 0)
  75. return ret;
  76. return 0;
  77. }
  78. EXPORT_SYMBOL(max8998_bulk_read);
  79. int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
  80. {
  81. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  82. int ret;
  83. mutex_lock(&max8998->iolock);
  84. ret = i2c_smbus_write_byte_data(i2c, reg, value);
  85. mutex_unlock(&max8998->iolock);
  86. return ret;
  87. }
  88. EXPORT_SYMBOL(max8998_write_reg);
  89. int max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  90. {
  91. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  92. int ret;
  93. mutex_lock(&max8998->iolock);
  94. ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
  95. mutex_unlock(&max8998->iolock);
  96. if (ret < 0)
  97. return ret;
  98. return 0;
  99. }
  100. EXPORT_SYMBOL(max8998_bulk_write);
  101. int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
  102. {
  103. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  104. int ret;
  105. mutex_lock(&max8998->iolock);
  106. ret = i2c_smbus_read_byte_data(i2c, reg);
  107. if (ret >= 0) {
  108. u8 old_val = ret & 0xff;
  109. u8 new_val = (val & mask) | (old_val & (~mask));
  110. ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
  111. }
  112. mutex_unlock(&max8998->iolock);
  113. return ret;
  114. }
  115. EXPORT_SYMBOL(max8998_update_reg);
  116. #ifdef CONFIG_OF
  117. static const struct of_device_id max8998_dt_match[] = {
  118. { .compatible = "maxim,max8998", .data = (void *)TYPE_MAX8998 },
  119. { .compatible = "national,lp3974", .data = (void *)TYPE_LP3974 },
  120. { .compatible = "ti,lp3974", .data = (void *)TYPE_LP3974 },
  121. {},
  122. };
  123. MODULE_DEVICE_TABLE(of, max8998_dt_match);
  124. #endif
  125. /*
  126. * Only the common platform data elements for max8998 are parsed here from the
  127. * device tree. Other sub-modules of max8998 such as pmic, rtc and others have
  128. * to parse their own platform data elements from device tree.
  129. *
  130. * The max8998 platform data structure is instantiated here and the drivers for
  131. * the sub-modules need not instantiate another instance while parsing their
  132. * platform data.
  133. */
  134. static struct max8998_platform_data *max8998_i2c_parse_dt_pdata(
  135. struct device *dev)
  136. {
  137. struct max8998_platform_data *pd;
  138. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  139. if (!pd)
  140. return ERR_PTR(-ENOMEM);
  141. pd->ono = irq_of_parse_and_map(dev->of_node, 1);
  142. /*
  143. * ToDo: the 'wakeup' member in the platform data is more of a linux
  144. * specfic information. Hence, there is no binding for that yet and
  145. * not parsed here.
  146. */
  147. return pd;
  148. }
  149. static inline unsigned long max8998_i2c_get_driver_data(struct i2c_client *i2c,
  150. const struct i2c_device_id *id)
  151. {
  152. if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node) {
  153. const struct of_device_id *match;
  154. match = of_match_node(max8998_dt_match, i2c->dev.of_node);
  155. return (unsigned long)match->data;
  156. }
  157. return id->driver_data;
  158. }
  159. static int max8998_i2c_probe(struct i2c_client *i2c,
  160. const struct i2c_device_id *id)
  161. {
  162. struct max8998_platform_data *pdata = dev_get_platdata(&i2c->dev);
  163. struct max8998_dev *max8998;
  164. int ret = 0;
  165. max8998 = devm_kzalloc(&i2c->dev, sizeof(struct max8998_dev),
  166. GFP_KERNEL);
  167. if (max8998 == NULL)
  168. return -ENOMEM;
  169. if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node) {
  170. pdata = max8998_i2c_parse_dt_pdata(&i2c->dev);
  171. if (IS_ERR(pdata)) {
  172. ret = PTR_ERR(pdata);
  173. goto err;
  174. }
  175. }
  176. i2c_set_clientdata(i2c, max8998);
  177. max8998->dev = &i2c->dev;
  178. max8998->i2c = i2c;
  179. max8998->irq = i2c->irq;
  180. max8998->type = max8998_i2c_get_driver_data(i2c, id);
  181. max8998->pdata = pdata;
  182. if (pdata) {
  183. max8998->ono = pdata->ono;
  184. max8998->irq_base = pdata->irq_base;
  185. max8998->wakeup = pdata->wakeup;
  186. }
  187. mutex_init(&max8998->iolock);
  188. max8998->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
  189. if (!max8998->rtc) {
  190. dev_err(&i2c->dev, "Failed to allocate I2C device for RTC\n");
  191. return -ENODEV;
  192. }
  193. i2c_set_clientdata(max8998->rtc, max8998);
  194. max8998_irq_init(max8998);
  195. pm_runtime_set_active(max8998->dev);
  196. switch (max8998->type) {
  197. case TYPE_LP3974:
  198. ret = mfd_add_devices(max8998->dev, -1,
  199. lp3974_devs, ARRAY_SIZE(lp3974_devs),
  200. NULL, 0, NULL);
  201. break;
  202. case TYPE_MAX8998:
  203. ret = mfd_add_devices(max8998->dev, -1,
  204. max8998_devs, ARRAY_SIZE(max8998_devs),
  205. NULL, 0, NULL);
  206. break;
  207. default:
  208. ret = -EINVAL;
  209. }
  210. if (ret < 0)
  211. goto err;
  212. device_init_wakeup(max8998->dev, max8998->wakeup);
  213. return ret;
  214. err:
  215. mfd_remove_devices(max8998->dev);
  216. max8998_irq_exit(max8998);
  217. i2c_unregister_device(max8998->rtc);
  218. return ret;
  219. }
  220. static int max8998_i2c_remove(struct i2c_client *i2c)
  221. {
  222. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  223. mfd_remove_devices(max8998->dev);
  224. max8998_irq_exit(max8998);
  225. i2c_unregister_device(max8998->rtc);
  226. return 0;
  227. }
  228. static const struct i2c_device_id max8998_i2c_id[] = {
  229. { "max8998", TYPE_MAX8998 },
  230. { "lp3974", TYPE_LP3974},
  231. { }
  232. };
  233. MODULE_DEVICE_TABLE(i2c, max8998_i2c_id);
  234. static int max8998_suspend(struct device *dev)
  235. {
  236. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  237. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  238. if (device_may_wakeup(dev))
  239. irq_set_irq_wake(max8998->irq, 1);
  240. return 0;
  241. }
  242. static int max8998_resume(struct device *dev)
  243. {
  244. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  245. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  246. if (device_may_wakeup(dev))
  247. irq_set_irq_wake(max8998->irq, 0);
  248. /*
  249. * In LP3974, if IRQ registers are not "read & clear"
  250. * when it's set during sleep, the interrupt becomes
  251. * disabled.
  252. */
  253. return max8998_irq_resume(i2c_get_clientdata(i2c));
  254. }
  255. struct max8998_reg_dump {
  256. u8 addr;
  257. u8 val;
  258. };
  259. #define SAVE_ITEM(x) { .addr = (x), .val = 0x0, }
  260. static struct max8998_reg_dump max8998_dump[] = {
  261. SAVE_ITEM(MAX8998_REG_IRQM1),
  262. SAVE_ITEM(MAX8998_REG_IRQM2),
  263. SAVE_ITEM(MAX8998_REG_IRQM3),
  264. SAVE_ITEM(MAX8998_REG_IRQM4),
  265. SAVE_ITEM(MAX8998_REG_STATUSM1),
  266. SAVE_ITEM(MAX8998_REG_STATUSM2),
  267. SAVE_ITEM(MAX8998_REG_CHGR1),
  268. SAVE_ITEM(MAX8998_REG_CHGR2),
  269. SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
  270. SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
  271. SAVE_ITEM(MAX8998_REG_BUCK_ACTIVE_DISCHARGE3),
  272. SAVE_ITEM(MAX8998_REG_ONOFF1),
  273. SAVE_ITEM(MAX8998_REG_ONOFF2),
  274. SAVE_ITEM(MAX8998_REG_ONOFF3),
  275. SAVE_ITEM(MAX8998_REG_ONOFF4),
  276. SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE1),
  277. SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE2),
  278. SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE3),
  279. SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE4),
  280. SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE1),
  281. SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE2),
  282. SAVE_ITEM(MAX8998_REG_LDO2_LDO3),
  283. SAVE_ITEM(MAX8998_REG_LDO4),
  284. SAVE_ITEM(MAX8998_REG_LDO5),
  285. SAVE_ITEM(MAX8998_REG_LDO6),
  286. SAVE_ITEM(MAX8998_REG_LDO7),
  287. SAVE_ITEM(MAX8998_REG_LDO8_LDO9),
  288. SAVE_ITEM(MAX8998_REG_LDO10_LDO11),
  289. SAVE_ITEM(MAX8998_REG_LDO12),
  290. SAVE_ITEM(MAX8998_REG_LDO13),
  291. SAVE_ITEM(MAX8998_REG_LDO14),
  292. SAVE_ITEM(MAX8998_REG_LDO15),
  293. SAVE_ITEM(MAX8998_REG_LDO16),
  294. SAVE_ITEM(MAX8998_REG_LDO17),
  295. SAVE_ITEM(MAX8998_REG_BKCHR),
  296. SAVE_ITEM(MAX8998_REG_LBCNFG1),
  297. SAVE_ITEM(MAX8998_REG_LBCNFG2),
  298. };
  299. /* Save registers before hibernation */
  300. static int max8998_freeze(struct device *dev)
  301. {
  302. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  303. int i;
  304. for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
  305. max8998_read_reg(i2c, max8998_dump[i].addr,
  306. &max8998_dump[i].val);
  307. return 0;
  308. }
  309. /* Restore registers after hibernation */
  310. static int max8998_restore(struct device *dev)
  311. {
  312. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  313. int i;
  314. for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
  315. max8998_write_reg(i2c, max8998_dump[i].addr,
  316. max8998_dump[i].val);
  317. return 0;
  318. }
  319. static const struct dev_pm_ops max8998_pm = {
  320. .suspend = max8998_suspend,
  321. .resume = max8998_resume,
  322. .freeze = max8998_freeze,
  323. .restore = max8998_restore,
  324. };
  325. static struct i2c_driver max8998_i2c_driver = {
  326. .driver = {
  327. .name = "max8998",
  328. .pm = &max8998_pm,
  329. .of_match_table = of_match_ptr(max8998_dt_match),
  330. },
  331. .probe = max8998_i2c_probe,
  332. .remove = max8998_i2c_remove,
  333. .id_table = max8998_i2c_id,
  334. };
  335. static int __init max8998_i2c_init(void)
  336. {
  337. return i2c_add_driver(&max8998_i2c_driver);
  338. }
  339. /* init early so consumer devices can complete system boot */
  340. subsys_initcall(max8998_i2c_init);
  341. static void __exit max8998_i2c_exit(void)
  342. {
  343. i2c_del_driver(&max8998_i2c_driver);
  344. }
  345. module_exit(max8998_i2c_exit);
  346. MODULE_DESCRIPTION("MAXIM 8998 multi-function core driver");
  347. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  348. MODULE_LICENSE("GPL");