max8998.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/i2c.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/mutex.h>
  30. #include <linux/mfd/core.h>
  31. #include <linux/mfd/max8998.h>
  32. #include <linux/mfd/max8998-private.h>
  33. #define RTC_I2C_ADDR (0x0c >> 1)
  34. static struct mfd_cell max8998_devs[] = {
  35. {
  36. .name = "max8998-pmic",
  37. }, {
  38. .name = "max8998-rtc",
  39. }, {
  40. .name = "max8998-battery",
  41. },
  42. };
  43. static struct mfd_cell lp3974_devs[] = {
  44. {
  45. .name = "lp3974-pmic",
  46. }, {
  47. .name = "lp3974-rtc",
  48. },
  49. };
  50. int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
  51. {
  52. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  53. int ret;
  54. mutex_lock(&max8998->iolock);
  55. ret = i2c_smbus_read_byte_data(i2c, reg);
  56. mutex_unlock(&max8998->iolock);
  57. if (ret < 0)
  58. return ret;
  59. ret &= 0xff;
  60. *dest = ret;
  61. return 0;
  62. }
  63. EXPORT_SYMBOL(max8998_read_reg);
  64. int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  65. {
  66. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  67. int ret;
  68. mutex_lock(&max8998->iolock);
  69. ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
  70. mutex_unlock(&max8998->iolock);
  71. if (ret < 0)
  72. return ret;
  73. return 0;
  74. }
  75. EXPORT_SYMBOL(max8998_bulk_read);
  76. int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
  77. {
  78. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  79. int ret;
  80. mutex_lock(&max8998->iolock);
  81. ret = i2c_smbus_write_byte_data(i2c, reg, value);
  82. mutex_unlock(&max8998->iolock);
  83. return ret;
  84. }
  85. EXPORT_SYMBOL(max8998_write_reg);
  86. int max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  87. {
  88. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  89. int ret;
  90. mutex_lock(&max8998->iolock);
  91. ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
  92. mutex_unlock(&max8998->iolock);
  93. if (ret < 0)
  94. return ret;
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(max8998_bulk_write);
  98. int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
  99. {
  100. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  101. int ret;
  102. mutex_lock(&max8998->iolock);
  103. ret = i2c_smbus_read_byte_data(i2c, reg);
  104. if (ret >= 0) {
  105. u8 old_val = ret & 0xff;
  106. u8 new_val = (val & mask) | (old_val & (~mask));
  107. ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
  108. }
  109. mutex_unlock(&max8998->iolock);
  110. return ret;
  111. }
  112. EXPORT_SYMBOL(max8998_update_reg);
  113. static int max8998_i2c_probe(struct i2c_client *i2c,
  114. const struct i2c_device_id *id)
  115. {
  116. struct max8998_platform_data *pdata = i2c->dev.platform_data;
  117. struct max8998_dev *max8998;
  118. int ret = 0;
  119. max8998 = kzalloc(sizeof(struct max8998_dev), GFP_KERNEL);
  120. if (max8998 == NULL)
  121. return -ENOMEM;
  122. i2c_set_clientdata(i2c, max8998);
  123. max8998->dev = &i2c->dev;
  124. max8998->i2c = i2c;
  125. max8998->irq = i2c->irq;
  126. max8998->type = id->driver_data;
  127. if (pdata) {
  128. max8998->ono = pdata->ono;
  129. max8998->irq_base = pdata->irq_base;
  130. max8998->wakeup = pdata->wakeup;
  131. }
  132. mutex_init(&max8998->iolock);
  133. max8998->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
  134. if (!max8998->rtc) {
  135. dev_err(&i2c->dev, "Failed to allocate I2C device for RTC\n");
  136. return -ENODEV;
  137. }
  138. i2c_set_clientdata(max8998->rtc, max8998);
  139. max8998_irq_init(max8998);
  140. pm_runtime_set_active(max8998->dev);
  141. switch (id->driver_data) {
  142. case TYPE_LP3974:
  143. ret = mfd_add_devices(max8998->dev, -1,
  144. lp3974_devs, ARRAY_SIZE(lp3974_devs),
  145. NULL, 0);
  146. break;
  147. case TYPE_MAX8998:
  148. ret = mfd_add_devices(max8998->dev, -1,
  149. max8998_devs, ARRAY_SIZE(max8998_devs),
  150. NULL, 0);
  151. break;
  152. default:
  153. ret = -EINVAL;
  154. }
  155. if (ret < 0)
  156. goto err;
  157. device_init_wakeup(max8998->dev, max8998->wakeup);
  158. return ret;
  159. err:
  160. mfd_remove_devices(max8998->dev);
  161. max8998_irq_exit(max8998);
  162. i2c_unregister_device(max8998->rtc);
  163. kfree(max8998);
  164. return ret;
  165. }
  166. static int max8998_i2c_remove(struct i2c_client *i2c)
  167. {
  168. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  169. mfd_remove_devices(max8998->dev);
  170. max8998_irq_exit(max8998);
  171. i2c_unregister_device(max8998->rtc);
  172. kfree(max8998);
  173. return 0;
  174. }
  175. static const struct i2c_device_id max8998_i2c_id[] = {
  176. { "max8998", TYPE_MAX8998 },
  177. { "lp3974", TYPE_LP3974},
  178. { }
  179. };
  180. MODULE_DEVICE_TABLE(i2c, max8998_i2c_id);
  181. static int max8998_suspend(struct device *dev)
  182. {
  183. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  184. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  185. if (device_may_wakeup(dev))
  186. irq_set_irq_wake(max8998->irq, 1);
  187. return 0;
  188. }
  189. static int max8998_resume(struct device *dev)
  190. {
  191. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  192. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  193. if (device_may_wakeup(dev))
  194. irq_set_irq_wake(max8998->irq, 0);
  195. /*
  196. * In LP3974, if IRQ registers are not "read & clear"
  197. * when it's set during sleep, the interrupt becomes
  198. * disabled.
  199. */
  200. return max8998_irq_resume(i2c_get_clientdata(i2c));
  201. }
  202. struct max8998_reg_dump {
  203. u8 addr;
  204. u8 val;
  205. };
  206. #define SAVE_ITEM(x) { .addr = (x), .val = 0x0, }
  207. static struct max8998_reg_dump max8998_dump[] = {
  208. SAVE_ITEM(MAX8998_REG_IRQM1),
  209. SAVE_ITEM(MAX8998_REG_IRQM2),
  210. SAVE_ITEM(MAX8998_REG_IRQM3),
  211. SAVE_ITEM(MAX8998_REG_IRQM4),
  212. SAVE_ITEM(MAX8998_REG_STATUSM1),
  213. SAVE_ITEM(MAX8998_REG_STATUSM2),
  214. SAVE_ITEM(MAX8998_REG_CHGR1),
  215. SAVE_ITEM(MAX8998_REG_CHGR2),
  216. SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
  217. SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
  218. SAVE_ITEM(MAX8998_REG_BUCK_ACTIVE_DISCHARGE3),
  219. SAVE_ITEM(MAX8998_REG_ONOFF1),
  220. SAVE_ITEM(MAX8998_REG_ONOFF2),
  221. SAVE_ITEM(MAX8998_REG_ONOFF3),
  222. SAVE_ITEM(MAX8998_REG_ONOFF4),
  223. SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE1),
  224. SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE2),
  225. SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE3),
  226. SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE4),
  227. SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE1),
  228. SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE2),
  229. SAVE_ITEM(MAX8998_REG_LDO2_LDO3),
  230. SAVE_ITEM(MAX8998_REG_LDO4),
  231. SAVE_ITEM(MAX8998_REG_LDO5),
  232. SAVE_ITEM(MAX8998_REG_LDO6),
  233. SAVE_ITEM(MAX8998_REG_LDO7),
  234. SAVE_ITEM(MAX8998_REG_LDO8_LDO9),
  235. SAVE_ITEM(MAX8998_REG_LDO10_LDO11),
  236. SAVE_ITEM(MAX8998_REG_LDO12),
  237. SAVE_ITEM(MAX8998_REG_LDO13),
  238. SAVE_ITEM(MAX8998_REG_LDO14),
  239. SAVE_ITEM(MAX8998_REG_LDO15),
  240. SAVE_ITEM(MAX8998_REG_LDO16),
  241. SAVE_ITEM(MAX8998_REG_LDO17),
  242. SAVE_ITEM(MAX8998_REG_BKCHR),
  243. SAVE_ITEM(MAX8998_REG_LBCNFG1),
  244. SAVE_ITEM(MAX8998_REG_LBCNFG2),
  245. };
  246. /* Save registers before hibernation */
  247. static int max8998_freeze(struct device *dev)
  248. {
  249. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  250. int i;
  251. for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
  252. max8998_read_reg(i2c, max8998_dump[i].addr,
  253. &max8998_dump[i].val);
  254. return 0;
  255. }
  256. /* Restore registers after hibernation */
  257. static int max8998_restore(struct device *dev)
  258. {
  259. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  260. int i;
  261. for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
  262. max8998_write_reg(i2c, max8998_dump[i].addr,
  263. max8998_dump[i].val);
  264. return 0;
  265. }
  266. static const struct dev_pm_ops max8998_pm = {
  267. .suspend = max8998_suspend,
  268. .resume = max8998_resume,
  269. .freeze = max8998_freeze,
  270. .restore = max8998_restore,
  271. };
  272. static struct i2c_driver max8998_i2c_driver = {
  273. .driver = {
  274. .name = "max8998",
  275. .owner = THIS_MODULE,
  276. .pm = &max8998_pm,
  277. },
  278. .probe = max8998_i2c_probe,
  279. .remove = max8998_i2c_remove,
  280. .id_table = max8998_i2c_id,
  281. };
  282. static int __init max8998_i2c_init(void)
  283. {
  284. return i2c_add_driver(&max8998_i2c_driver);
  285. }
  286. /* init early so consumer devices can complete system boot */
  287. subsys_initcall(max8998_i2c_init);
  288. static void __exit max8998_i2c_exit(void)
  289. {
  290. i2c_del_driver(&max8998_i2c_driver);
  291. }
  292. module_exit(max8998_i2c_exit);
  293. MODULE_DESCRIPTION("MAXIM 8998 multi-function core driver");
  294. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  295. MODULE_LICENSE("GPL");