max8997.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * max8997.c - mfd core driver for the Maxim 8966 and 8997
  3. *
  4. * Copyright (C) 2011 Samsung Electronics
  5. * MyungJoo Ham <myungjoo.ham@smasung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * This driver is based on max8998.c
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/i2c.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/module.h>
  28. #include <linux/mutex.h>
  29. #include <linux/mfd/core.h>
  30. #include <linux/mfd/max8997.h>
  31. #include <linux/mfd/max8997-private.h>
  32. #define I2C_ADDR_PMIC (0xCC >> 1)
  33. #define I2C_ADDR_MUIC (0x4A >> 1)
  34. #define I2C_ADDR_BATTERY (0x6C >> 1)
  35. #define I2C_ADDR_RTC (0x0C >> 1)
  36. #define I2C_ADDR_HAPTIC (0x90 >> 1)
  37. static struct mfd_cell max8997_devs[] = {
  38. { .name = "max8997-pmic", },
  39. { .name = "max8997-rtc", },
  40. { .name = "max8997-battery", },
  41. { .name = "max8997-haptic", },
  42. { .name = "max8997-muic", },
  43. { .name = "max8997-led", .id = 1 },
  44. { .name = "max8997-led", .id = 2 },
  45. };
  46. int max8997_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
  47. {
  48. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  49. int ret;
  50. mutex_lock(&max8997->iolock);
  51. ret = i2c_smbus_read_byte_data(i2c, reg);
  52. mutex_unlock(&max8997->iolock);
  53. if (ret < 0)
  54. return ret;
  55. ret &= 0xff;
  56. *dest = ret;
  57. return 0;
  58. }
  59. EXPORT_SYMBOL_GPL(max8997_read_reg);
  60. int max8997_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  61. {
  62. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  63. int ret;
  64. mutex_lock(&max8997->iolock);
  65. ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
  66. mutex_unlock(&max8997->iolock);
  67. if (ret < 0)
  68. return ret;
  69. return 0;
  70. }
  71. EXPORT_SYMBOL_GPL(max8997_bulk_read);
  72. int max8997_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
  73. {
  74. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  75. int ret;
  76. mutex_lock(&max8997->iolock);
  77. ret = i2c_smbus_write_byte_data(i2c, reg, value);
  78. mutex_unlock(&max8997->iolock);
  79. return ret;
  80. }
  81. EXPORT_SYMBOL_GPL(max8997_write_reg);
  82. int max8997_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  83. {
  84. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  85. int ret;
  86. mutex_lock(&max8997->iolock);
  87. ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
  88. mutex_unlock(&max8997->iolock);
  89. if (ret < 0)
  90. return ret;
  91. return 0;
  92. }
  93. EXPORT_SYMBOL_GPL(max8997_bulk_write);
  94. int max8997_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
  95. {
  96. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  97. int ret;
  98. mutex_lock(&max8997->iolock);
  99. ret = i2c_smbus_read_byte_data(i2c, reg);
  100. if (ret >= 0) {
  101. u8 old_val = ret & 0xff;
  102. u8 new_val = (val & mask) | (old_val & (~mask));
  103. ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
  104. }
  105. mutex_unlock(&max8997->iolock);
  106. return ret;
  107. }
  108. EXPORT_SYMBOL_GPL(max8997_update_reg);
  109. static int max8997_i2c_probe(struct i2c_client *i2c,
  110. const struct i2c_device_id *id)
  111. {
  112. struct max8997_dev *max8997;
  113. struct max8997_platform_data *pdata = i2c->dev.platform_data;
  114. int ret = 0;
  115. max8997 = kzalloc(sizeof(struct max8997_dev), GFP_KERNEL);
  116. if (max8997 == NULL)
  117. return -ENOMEM;
  118. i2c_set_clientdata(i2c, max8997);
  119. max8997->dev = &i2c->dev;
  120. max8997->i2c = i2c;
  121. max8997->type = id->driver_data;
  122. max8997->irq = i2c->irq;
  123. if (!pdata)
  124. goto err;
  125. max8997->irq_base = pdata->irq_base;
  126. max8997->ono = pdata->ono;
  127. mutex_init(&max8997->iolock);
  128. max8997->rtc = i2c_new_dummy(i2c->adapter, I2C_ADDR_RTC);
  129. if (!max8997->rtc) {
  130. dev_err(max8997->dev, "Failed to allocate I2C device for RTC\n");
  131. return -ENODEV;
  132. }
  133. i2c_set_clientdata(max8997->rtc, max8997);
  134. max8997->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
  135. if (!max8997->haptic) {
  136. dev_err(max8997->dev, "Failed to allocate I2C device for Haptic\n");
  137. ret = -ENODEV;
  138. goto err_i2c_haptic;
  139. }
  140. i2c_set_clientdata(max8997->haptic, max8997);
  141. max8997->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
  142. if (!max8997->muic) {
  143. dev_err(max8997->dev, "Failed to allocate I2C device for MUIC\n");
  144. ret = -ENODEV;
  145. goto err_i2c_muic;
  146. }
  147. i2c_set_clientdata(max8997->muic, max8997);
  148. pm_runtime_set_active(max8997->dev);
  149. max8997_irq_init(max8997);
  150. mfd_add_devices(max8997->dev, -1, max8997_devs,
  151. ARRAY_SIZE(max8997_devs),
  152. NULL, 0);
  153. /*
  154. * TODO: enable others (flash, muic, rtc, battery, ...) and
  155. * check the return value
  156. */
  157. if (ret < 0)
  158. goto err_mfd;
  159. /* MAX8997 has a power button input. */
  160. device_init_wakeup(max8997->dev, pdata->wakeup);
  161. return ret;
  162. err_mfd:
  163. mfd_remove_devices(max8997->dev);
  164. i2c_unregister_device(max8997->muic);
  165. err_i2c_muic:
  166. i2c_unregister_device(max8997->haptic);
  167. err_i2c_haptic:
  168. i2c_unregister_device(max8997->rtc);
  169. err:
  170. kfree(max8997);
  171. return ret;
  172. }
  173. static int max8997_i2c_remove(struct i2c_client *i2c)
  174. {
  175. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  176. mfd_remove_devices(max8997->dev);
  177. i2c_unregister_device(max8997->muic);
  178. i2c_unregister_device(max8997->haptic);
  179. i2c_unregister_device(max8997->rtc);
  180. kfree(max8997);
  181. return 0;
  182. }
  183. static const struct i2c_device_id max8997_i2c_id[] = {
  184. { "max8997", TYPE_MAX8997 },
  185. { "max8966", TYPE_MAX8966 },
  186. { }
  187. };
  188. MODULE_DEVICE_TABLE(i2c, max8998_i2c_id);
  189. u8 max8997_dumpaddr_pmic[] = {
  190. MAX8997_REG_INT1MSK,
  191. MAX8997_REG_INT2MSK,
  192. MAX8997_REG_INT3MSK,
  193. MAX8997_REG_INT4MSK,
  194. MAX8997_REG_MAINCON1,
  195. MAX8997_REG_MAINCON2,
  196. MAX8997_REG_BUCKRAMP,
  197. MAX8997_REG_BUCK1CTRL,
  198. MAX8997_REG_BUCK1DVS1,
  199. MAX8997_REG_BUCK1DVS2,
  200. MAX8997_REG_BUCK1DVS3,
  201. MAX8997_REG_BUCK1DVS4,
  202. MAX8997_REG_BUCK1DVS5,
  203. MAX8997_REG_BUCK1DVS6,
  204. MAX8997_REG_BUCK1DVS7,
  205. MAX8997_REG_BUCK1DVS8,
  206. MAX8997_REG_BUCK2CTRL,
  207. MAX8997_REG_BUCK2DVS1,
  208. MAX8997_REG_BUCK2DVS2,
  209. MAX8997_REG_BUCK2DVS3,
  210. MAX8997_REG_BUCK2DVS4,
  211. MAX8997_REG_BUCK2DVS5,
  212. MAX8997_REG_BUCK2DVS6,
  213. MAX8997_REG_BUCK2DVS7,
  214. MAX8997_REG_BUCK2DVS8,
  215. MAX8997_REG_BUCK3CTRL,
  216. MAX8997_REG_BUCK3DVS,
  217. MAX8997_REG_BUCK4CTRL,
  218. MAX8997_REG_BUCK4DVS,
  219. MAX8997_REG_BUCK5CTRL,
  220. MAX8997_REG_BUCK5DVS1,
  221. MAX8997_REG_BUCK5DVS2,
  222. MAX8997_REG_BUCK5DVS3,
  223. MAX8997_REG_BUCK5DVS4,
  224. MAX8997_REG_BUCK5DVS5,
  225. MAX8997_REG_BUCK5DVS6,
  226. MAX8997_REG_BUCK5DVS7,
  227. MAX8997_REG_BUCK5DVS8,
  228. MAX8997_REG_BUCK6CTRL,
  229. MAX8997_REG_BUCK6BPSKIPCTRL,
  230. MAX8997_REG_BUCK7CTRL,
  231. MAX8997_REG_BUCK7DVS,
  232. MAX8997_REG_LDO1CTRL,
  233. MAX8997_REG_LDO2CTRL,
  234. MAX8997_REG_LDO3CTRL,
  235. MAX8997_REG_LDO4CTRL,
  236. MAX8997_REG_LDO5CTRL,
  237. MAX8997_REG_LDO6CTRL,
  238. MAX8997_REG_LDO7CTRL,
  239. MAX8997_REG_LDO8CTRL,
  240. MAX8997_REG_LDO9CTRL,
  241. MAX8997_REG_LDO10CTRL,
  242. MAX8997_REG_LDO11CTRL,
  243. MAX8997_REG_LDO12CTRL,
  244. MAX8997_REG_LDO13CTRL,
  245. MAX8997_REG_LDO14CTRL,
  246. MAX8997_REG_LDO15CTRL,
  247. MAX8997_REG_LDO16CTRL,
  248. MAX8997_REG_LDO17CTRL,
  249. MAX8997_REG_LDO18CTRL,
  250. MAX8997_REG_LDO21CTRL,
  251. MAX8997_REG_MBCCTRL1,
  252. MAX8997_REG_MBCCTRL2,
  253. MAX8997_REG_MBCCTRL3,
  254. MAX8997_REG_MBCCTRL4,
  255. MAX8997_REG_MBCCTRL5,
  256. MAX8997_REG_MBCCTRL6,
  257. MAX8997_REG_OTPCGHCVS,
  258. MAX8997_REG_SAFEOUTCTRL,
  259. MAX8997_REG_LBCNFG1,
  260. MAX8997_REG_LBCNFG2,
  261. MAX8997_REG_BBCCTRL,
  262. MAX8997_REG_FLASH1_CUR,
  263. MAX8997_REG_FLASH2_CUR,
  264. MAX8997_REG_MOVIE_CUR,
  265. MAX8997_REG_GSMB_CUR,
  266. MAX8997_REG_BOOST_CNTL,
  267. MAX8997_REG_LEN_CNTL,
  268. MAX8997_REG_FLASH_CNTL,
  269. MAX8997_REG_WDT_CNTL,
  270. MAX8997_REG_MAXFLASH1,
  271. MAX8997_REG_MAXFLASH2,
  272. MAX8997_REG_FLASHSTATUSMASK,
  273. MAX8997_REG_GPIOCNTL1,
  274. MAX8997_REG_GPIOCNTL2,
  275. MAX8997_REG_GPIOCNTL3,
  276. MAX8997_REG_GPIOCNTL4,
  277. MAX8997_REG_GPIOCNTL5,
  278. MAX8997_REG_GPIOCNTL6,
  279. MAX8997_REG_GPIOCNTL7,
  280. MAX8997_REG_GPIOCNTL8,
  281. MAX8997_REG_GPIOCNTL9,
  282. MAX8997_REG_GPIOCNTL10,
  283. MAX8997_REG_GPIOCNTL11,
  284. MAX8997_REG_GPIOCNTL12,
  285. MAX8997_REG_LDO1CONFIG,
  286. MAX8997_REG_LDO2CONFIG,
  287. MAX8997_REG_LDO3CONFIG,
  288. MAX8997_REG_LDO4CONFIG,
  289. MAX8997_REG_LDO5CONFIG,
  290. MAX8997_REG_LDO6CONFIG,
  291. MAX8997_REG_LDO7CONFIG,
  292. MAX8997_REG_LDO8CONFIG,
  293. MAX8997_REG_LDO9CONFIG,
  294. MAX8997_REG_LDO10CONFIG,
  295. MAX8997_REG_LDO11CONFIG,
  296. MAX8997_REG_LDO12CONFIG,
  297. MAX8997_REG_LDO13CONFIG,
  298. MAX8997_REG_LDO14CONFIG,
  299. MAX8997_REG_LDO15CONFIG,
  300. MAX8997_REG_LDO16CONFIG,
  301. MAX8997_REG_LDO17CONFIG,
  302. MAX8997_REG_LDO18CONFIG,
  303. MAX8997_REG_LDO21CONFIG,
  304. MAX8997_REG_DVSOKTIMER1,
  305. MAX8997_REG_DVSOKTIMER2,
  306. MAX8997_REG_DVSOKTIMER4,
  307. MAX8997_REG_DVSOKTIMER5,
  308. };
  309. u8 max8997_dumpaddr_muic[] = {
  310. MAX8997_MUIC_REG_INTMASK1,
  311. MAX8997_MUIC_REG_INTMASK2,
  312. MAX8997_MUIC_REG_INTMASK3,
  313. MAX8997_MUIC_REG_CDETCTRL,
  314. MAX8997_MUIC_REG_CONTROL1,
  315. MAX8997_MUIC_REG_CONTROL2,
  316. MAX8997_MUIC_REG_CONTROL3,
  317. };
  318. u8 max8997_dumpaddr_haptic[] = {
  319. MAX8997_HAPTIC_REG_CONF1,
  320. MAX8997_HAPTIC_REG_CONF2,
  321. MAX8997_HAPTIC_REG_DRVCONF,
  322. MAX8997_HAPTIC_REG_CYCLECONF1,
  323. MAX8997_HAPTIC_REG_CYCLECONF2,
  324. MAX8997_HAPTIC_REG_SIGCONF1,
  325. MAX8997_HAPTIC_REG_SIGCONF2,
  326. MAX8997_HAPTIC_REG_SIGCONF3,
  327. MAX8997_HAPTIC_REG_SIGCONF4,
  328. MAX8997_HAPTIC_REG_SIGDC1,
  329. MAX8997_HAPTIC_REG_SIGDC2,
  330. MAX8997_HAPTIC_REG_SIGPWMDC1,
  331. MAX8997_HAPTIC_REG_SIGPWMDC2,
  332. MAX8997_HAPTIC_REG_SIGPWMDC3,
  333. MAX8997_HAPTIC_REG_SIGPWMDC4,
  334. };
  335. static int max8997_freeze(struct device *dev)
  336. {
  337. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  338. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  339. int i;
  340. for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_pmic); i++)
  341. max8997_read_reg(i2c, max8997_dumpaddr_pmic[i],
  342. &max8997->reg_dump[i]);
  343. for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_muic); i++)
  344. max8997_read_reg(i2c, max8997_dumpaddr_muic[i],
  345. &max8997->reg_dump[i + MAX8997_REG_PMIC_END]);
  346. for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_haptic); i++)
  347. max8997_read_reg(i2c, max8997_dumpaddr_haptic[i],
  348. &max8997->reg_dump[i + MAX8997_REG_PMIC_END +
  349. MAX8997_MUIC_REG_END]);
  350. return 0;
  351. }
  352. static int max8997_restore(struct device *dev)
  353. {
  354. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  355. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  356. int i;
  357. for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_pmic); i++)
  358. max8997_write_reg(i2c, max8997_dumpaddr_pmic[i],
  359. max8997->reg_dump[i]);
  360. for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_muic); i++)
  361. max8997_write_reg(i2c, max8997_dumpaddr_muic[i],
  362. max8997->reg_dump[i + MAX8997_REG_PMIC_END]);
  363. for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_haptic); i++)
  364. max8997_write_reg(i2c, max8997_dumpaddr_haptic[i],
  365. max8997->reg_dump[i + MAX8997_REG_PMIC_END +
  366. MAX8997_MUIC_REG_END]);
  367. return 0;
  368. }
  369. static int max8997_suspend(struct device *dev)
  370. {
  371. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  372. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  373. if (device_may_wakeup(dev))
  374. irq_set_irq_wake(max8997->irq, 1);
  375. return 0;
  376. }
  377. static int max8997_resume(struct device *dev)
  378. {
  379. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  380. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  381. if (device_may_wakeup(dev))
  382. irq_set_irq_wake(max8997->irq, 0);
  383. return max8997_irq_resume(max8997);
  384. }
  385. const struct dev_pm_ops max8997_pm = {
  386. .suspend = max8997_suspend,
  387. .resume = max8997_resume,
  388. .freeze = max8997_freeze,
  389. .restore = max8997_restore,
  390. };
  391. static struct i2c_driver max8997_i2c_driver = {
  392. .driver = {
  393. .name = "max8997",
  394. .owner = THIS_MODULE,
  395. .pm = &max8997_pm,
  396. },
  397. .probe = max8997_i2c_probe,
  398. .remove = max8997_i2c_remove,
  399. .id_table = max8997_i2c_id,
  400. };
  401. static int __init max8997_i2c_init(void)
  402. {
  403. return i2c_add_driver(&max8997_i2c_driver);
  404. }
  405. /* init early so consumer devices can complete system boot */
  406. subsys_initcall(max8997_i2c_init);
  407. static void __exit max8997_i2c_exit(void)
  408. {
  409. i2c_del_driver(&max8997_i2c_driver);
  410. }
  411. module_exit(max8997_i2c_exit);
  412. MODULE_DESCRIPTION("MAXIM 8997 multi-function core driver");
  413. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  414. MODULE_LICENSE("GPL");