max8998.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. ret = PTR_ERR(pdata);
  170. goto err;
  171. }
  172. }
  173. i2c_set_clientdata(i2c, max8998);
  174. max8998->dev = &i2c->dev;
  175. max8998->i2c = i2c;
  176. max8998->irq = i2c->irq;
  177. max8998->type = max8998_i2c_get_driver_data(i2c, id);
  178. max8998->pdata = pdata;
  179. if (pdata) {
  180. max8998->ono = pdata->ono;
  181. max8998->irq_base = pdata->irq_base;
  182. max8998->wakeup = pdata->wakeup;
  183. }
  184. mutex_init(&max8998->iolock);
  185. max8998->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
  186. if (!max8998->rtc) {
  187. dev_err(&i2c->dev, "Failed to allocate I2C device for RTC\n");
  188. return -ENODEV;
  189. }
  190. i2c_set_clientdata(max8998->rtc, max8998);
  191. max8998_irq_init(max8998);
  192. pm_runtime_set_active(max8998->dev);
  193. switch (max8998->type) {
  194. case TYPE_LP3974:
  195. ret = mfd_add_devices(max8998->dev, -1,
  196. lp3974_devs, ARRAY_SIZE(lp3974_devs),
  197. NULL, 0, NULL);
  198. break;
  199. case TYPE_MAX8998:
  200. ret = mfd_add_devices(max8998->dev, -1,
  201. max8998_devs, ARRAY_SIZE(max8998_devs),
  202. NULL, 0, NULL);
  203. break;
  204. default:
  205. ret = -EINVAL;
  206. }
  207. if (ret < 0)
  208. goto err;
  209. device_init_wakeup(max8998->dev, max8998->wakeup);
  210. return ret;
  211. err:
  212. mfd_remove_devices(max8998->dev);
  213. max8998_irq_exit(max8998);
  214. i2c_unregister_device(max8998->rtc);
  215. return ret;
  216. }
  217. static const struct i2c_device_id max8998_i2c_id[] = {
  218. { "max8998", TYPE_MAX8998 },
  219. { "lp3974", TYPE_LP3974},
  220. { }
  221. };
  222. static int max8998_suspend(struct device *dev)
  223. {
  224. struct i2c_client *i2c = to_i2c_client(dev);
  225. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  226. if (device_may_wakeup(dev))
  227. irq_set_irq_wake(max8998->irq, 1);
  228. return 0;
  229. }
  230. static int max8998_resume(struct device *dev)
  231. {
  232. struct i2c_client *i2c = to_i2c_client(dev);
  233. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  234. if (device_may_wakeup(dev))
  235. irq_set_irq_wake(max8998->irq, 0);
  236. /*
  237. * In LP3974, if IRQ registers are not "read & clear"
  238. * when it's set during sleep, the interrupt becomes
  239. * disabled.
  240. */
  241. return max8998_irq_resume(i2c_get_clientdata(i2c));
  242. }
  243. struct max8998_reg_dump {
  244. u8 addr;
  245. u8 val;
  246. };
  247. #define SAVE_ITEM(x) { .addr = (x), .val = 0x0, }
  248. static struct max8998_reg_dump max8998_dump[] = {
  249. SAVE_ITEM(MAX8998_REG_IRQM1),
  250. SAVE_ITEM(MAX8998_REG_IRQM2),
  251. SAVE_ITEM(MAX8998_REG_IRQM3),
  252. SAVE_ITEM(MAX8998_REG_IRQM4),
  253. SAVE_ITEM(MAX8998_REG_STATUSM1),
  254. SAVE_ITEM(MAX8998_REG_STATUSM2),
  255. SAVE_ITEM(MAX8998_REG_CHGR1),
  256. SAVE_ITEM(MAX8998_REG_CHGR2),
  257. SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
  258. SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
  259. SAVE_ITEM(MAX8998_REG_BUCK_ACTIVE_DISCHARGE3),
  260. SAVE_ITEM(MAX8998_REG_ONOFF1),
  261. SAVE_ITEM(MAX8998_REG_ONOFF2),
  262. SAVE_ITEM(MAX8998_REG_ONOFF3),
  263. SAVE_ITEM(MAX8998_REG_ONOFF4),
  264. SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE1),
  265. SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE2),
  266. SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE3),
  267. SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE4),
  268. SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE1),
  269. SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE2),
  270. SAVE_ITEM(MAX8998_REG_LDO2_LDO3),
  271. SAVE_ITEM(MAX8998_REG_LDO4),
  272. SAVE_ITEM(MAX8998_REG_LDO5),
  273. SAVE_ITEM(MAX8998_REG_LDO6),
  274. SAVE_ITEM(MAX8998_REG_LDO7),
  275. SAVE_ITEM(MAX8998_REG_LDO8_LDO9),
  276. SAVE_ITEM(MAX8998_REG_LDO10_LDO11),
  277. SAVE_ITEM(MAX8998_REG_LDO12),
  278. SAVE_ITEM(MAX8998_REG_LDO13),
  279. SAVE_ITEM(MAX8998_REG_LDO14),
  280. SAVE_ITEM(MAX8998_REG_LDO15),
  281. SAVE_ITEM(MAX8998_REG_LDO16),
  282. SAVE_ITEM(MAX8998_REG_LDO17),
  283. SAVE_ITEM(MAX8998_REG_BKCHR),
  284. SAVE_ITEM(MAX8998_REG_LBCNFG1),
  285. SAVE_ITEM(MAX8998_REG_LBCNFG2),
  286. };
  287. /* Save registers before hibernation */
  288. static int max8998_freeze(struct device *dev)
  289. {
  290. struct i2c_client *i2c = to_i2c_client(dev);
  291. int i;
  292. for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
  293. max8998_read_reg(i2c, max8998_dump[i].addr,
  294. &max8998_dump[i].val);
  295. return 0;
  296. }
  297. /* Restore registers after hibernation */
  298. static int max8998_restore(struct device *dev)
  299. {
  300. struct i2c_client *i2c = to_i2c_client(dev);
  301. int i;
  302. for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
  303. max8998_write_reg(i2c, max8998_dump[i].addr,
  304. max8998_dump[i].val);
  305. return 0;
  306. }
  307. static const struct dev_pm_ops max8998_pm = {
  308. .suspend = max8998_suspend,
  309. .resume = max8998_resume,
  310. .freeze = max8998_freeze,
  311. .restore = max8998_restore,
  312. };
  313. static struct i2c_driver max8998_i2c_driver = {
  314. .driver = {
  315. .name = "max8998",
  316. .pm = &max8998_pm,
  317. .suppress_bind_attrs = true,
  318. .of_match_table = of_match_ptr(max8998_dt_match),
  319. },
  320. .probe = max8998_i2c_probe,
  321. .id_table = max8998_i2c_id,
  322. };
  323. static int __init max8998_i2c_init(void)
  324. {
  325. return i2c_add_driver(&max8998_i2c_driver);
  326. }
  327. /* init early so consumer devices can complete system boot */
  328. subsys_initcall(max8998_i2c_init);