max8998.c 8.3 KB

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