max8998.c 9.6 KB

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