max8997.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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@samsung.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/err.h>
  24. #include <linux/slab.h>
  25. #include <linux/i2c.h>
  26. #include <linux/of.h>
  27. #include <linux/of_irq.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/init.h>
  31. #include <linux/mutex.h>
  32. #include <linux/mfd/core.h>
  33. #include <linux/mfd/max8997.h>
  34. #include <linux/mfd/max8997-private.h>
  35. #define I2C_ADDR_PMIC (0xCC >> 1)
  36. #define I2C_ADDR_MUIC (0x4A >> 1)
  37. #define I2C_ADDR_BATTERY (0x6C >> 1)
  38. #define I2C_ADDR_RTC (0x0C >> 1)
  39. #define I2C_ADDR_HAPTIC (0x90 >> 1)
  40. static const struct mfd_cell max8997_devs[] = {
  41. { .name = "max8997-pmic", },
  42. { .name = "max8997-rtc", },
  43. { .name = "max8997-battery", },
  44. { .name = "max8997-haptic", },
  45. { .name = "max8997-muic", },
  46. { .name = "max8997-led", .id = 1 },
  47. { .name = "max8997-led", .id = 2 },
  48. };
  49. #ifdef CONFIG_OF
  50. static const struct of_device_id max8997_pmic_dt_match[] = {
  51. { .compatible = "maxim,max8997-pmic", .data = (void *)TYPE_MAX8997 },
  52. {},
  53. };
  54. #endif
  55. int max8997_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
  56. {
  57. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  58. int ret;
  59. mutex_lock(&max8997->iolock);
  60. ret = i2c_smbus_read_byte_data(i2c, reg);
  61. mutex_unlock(&max8997->iolock);
  62. if (ret < 0)
  63. return ret;
  64. ret &= 0xff;
  65. *dest = ret;
  66. return 0;
  67. }
  68. EXPORT_SYMBOL_GPL(max8997_read_reg);
  69. int max8997_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  70. {
  71. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  72. int ret;
  73. mutex_lock(&max8997->iolock);
  74. ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
  75. mutex_unlock(&max8997->iolock);
  76. if (ret < 0)
  77. return ret;
  78. return 0;
  79. }
  80. EXPORT_SYMBOL_GPL(max8997_bulk_read);
  81. int max8997_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
  82. {
  83. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  84. int ret;
  85. mutex_lock(&max8997->iolock);
  86. ret = i2c_smbus_write_byte_data(i2c, reg, value);
  87. mutex_unlock(&max8997->iolock);
  88. return ret;
  89. }
  90. EXPORT_SYMBOL_GPL(max8997_write_reg);
  91. int max8997_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  92. {
  93. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  94. int ret;
  95. mutex_lock(&max8997->iolock);
  96. ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
  97. mutex_unlock(&max8997->iolock);
  98. if (ret < 0)
  99. return ret;
  100. return 0;
  101. }
  102. EXPORT_SYMBOL_GPL(max8997_bulk_write);
  103. int max8997_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
  104. {
  105. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  106. int ret;
  107. mutex_lock(&max8997->iolock);
  108. ret = i2c_smbus_read_byte_data(i2c, reg);
  109. if (ret >= 0) {
  110. u8 old_val = ret & 0xff;
  111. u8 new_val = (val & mask) | (old_val & (~mask));
  112. ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
  113. }
  114. mutex_unlock(&max8997->iolock);
  115. return ret;
  116. }
  117. EXPORT_SYMBOL_GPL(max8997_update_reg);
  118. /*
  119. * Only the common platform data elements for max8997 are parsed here from the
  120. * device tree. Other sub-modules of max8997 such as pmic, rtc and others have
  121. * to parse their own platform data elements from device tree.
  122. *
  123. * The max8997 platform data structure is instantiated here and the drivers for
  124. * the sub-modules need not instantiate another instance while parsing their
  125. * platform data.
  126. */
  127. static struct max8997_platform_data *max8997_i2c_parse_dt_pdata(
  128. struct device *dev)
  129. {
  130. struct max8997_platform_data *pd;
  131. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  132. if (!pd) {
  133. dev_err(dev, "could not allocate memory for pdata\n");
  134. return ERR_PTR(-ENOMEM);
  135. }
  136. pd->ono = irq_of_parse_and_map(dev->of_node, 1);
  137. /*
  138. * ToDo: the 'wakeup' member in the platform data is more of a linux
  139. * specfic information. Hence, there is no binding for that yet and
  140. * not parsed here.
  141. */
  142. return pd;
  143. }
  144. static inline unsigned long max8997_i2c_get_driver_data(struct i2c_client *i2c,
  145. const struct i2c_device_id *id)
  146. {
  147. if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node) {
  148. const struct of_device_id *match;
  149. match = of_match_node(max8997_pmic_dt_match, i2c->dev.of_node);
  150. return (unsigned long)match->data;
  151. }
  152. return id->driver_data;
  153. }
  154. static int max8997_i2c_probe(struct i2c_client *i2c,
  155. const struct i2c_device_id *id)
  156. {
  157. struct max8997_dev *max8997;
  158. struct max8997_platform_data *pdata = dev_get_platdata(&i2c->dev);
  159. int ret = 0;
  160. max8997 = devm_kzalloc(&i2c->dev, sizeof(struct max8997_dev),
  161. GFP_KERNEL);
  162. if (max8997 == NULL)
  163. return -ENOMEM;
  164. i2c_set_clientdata(i2c, max8997);
  165. max8997->dev = &i2c->dev;
  166. max8997->i2c = i2c;
  167. max8997->type = max8997_i2c_get_driver_data(i2c, id);
  168. max8997->irq = i2c->irq;
  169. if (IS_ENABLED(CONFIG_OF) && max8997->dev->of_node) {
  170. pdata = max8997_i2c_parse_dt_pdata(max8997->dev);
  171. if (IS_ERR(pdata))
  172. return PTR_ERR(pdata);
  173. }
  174. if (!pdata)
  175. return ret;
  176. max8997->pdata = pdata;
  177. max8997->ono = pdata->ono;
  178. mutex_init(&max8997->iolock);
  179. max8997->rtc = i2c_new_dummy(i2c->adapter, I2C_ADDR_RTC);
  180. if (!max8997->rtc) {
  181. dev_err(max8997->dev, "Failed to allocate I2C device for RTC\n");
  182. return -ENODEV;
  183. }
  184. i2c_set_clientdata(max8997->rtc, max8997);
  185. max8997->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
  186. if (!max8997->haptic) {
  187. dev_err(max8997->dev, "Failed to allocate I2C device for Haptic\n");
  188. ret = -ENODEV;
  189. goto err_i2c_haptic;
  190. }
  191. i2c_set_clientdata(max8997->haptic, max8997);
  192. max8997->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
  193. if (!max8997->muic) {
  194. dev_err(max8997->dev, "Failed to allocate I2C device for MUIC\n");
  195. ret = -ENODEV;
  196. goto err_i2c_muic;
  197. }
  198. i2c_set_clientdata(max8997->muic, max8997);
  199. pm_runtime_set_active(max8997->dev);
  200. max8997_irq_init(max8997);
  201. ret = mfd_add_devices(max8997->dev, -1, max8997_devs,
  202. ARRAY_SIZE(max8997_devs),
  203. NULL, 0, NULL);
  204. if (ret < 0) {
  205. dev_err(max8997->dev, "failed to add MFD devices %d\n", ret);
  206. goto err_mfd;
  207. }
  208. /*
  209. * TODO: enable others (flash, muic, rtc, battery, ...) and
  210. * check the return value
  211. */
  212. /* MAX8997 has a power button input. */
  213. device_init_wakeup(max8997->dev, pdata->wakeup);
  214. return ret;
  215. err_mfd:
  216. mfd_remove_devices(max8997->dev);
  217. i2c_unregister_device(max8997->muic);
  218. err_i2c_muic:
  219. i2c_unregister_device(max8997->haptic);
  220. err_i2c_haptic:
  221. i2c_unregister_device(max8997->rtc);
  222. return ret;
  223. }
  224. static const struct i2c_device_id max8997_i2c_id[] = {
  225. { "max8997", TYPE_MAX8997 },
  226. { "max8966", TYPE_MAX8966 },
  227. { }
  228. };
  229. static u8 max8997_dumpaddr_pmic[] = {
  230. MAX8997_REG_INT1MSK,
  231. MAX8997_REG_INT2MSK,
  232. MAX8997_REG_INT3MSK,
  233. MAX8997_REG_INT4MSK,
  234. MAX8997_REG_MAINCON1,
  235. MAX8997_REG_MAINCON2,
  236. MAX8997_REG_BUCKRAMP,
  237. MAX8997_REG_BUCK1CTRL,
  238. MAX8997_REG_BUCK1DVS1,
  239. MAX8997_REG_BUCK1DVS2,
  240. MAX8997_REG_BUCK1DVS3,
  241. MAX8997_REG_BUCK1DVS4,
  242. MAX8997_REG_BUCK1DVS5,
  243. MAX8997_REG_BUCK1DVS6,
  244. MAX8997_REG_BUCK1DVS7,
  245. MAX8997_REG_BUCK1DVS8,
  246. MAX8997_REG_BUCK2CTRL,
  247. MAX8997_REG_BUCK2DVS1,
  248. MAX8997_REG_BUCK2DVS2,
  249. MAX8997_REG_BUCK2DVS3,
  250. MAX8997_REG_BUCK2DVS4,
  251. MAX8997_REG_BUCK2DVS5,
  252. MAX8997_REG_BUCK2DVS6,
  253. MAX8997_REG_BUCK2DVS7,
  254. MAX8997_REG_BUCK2DVS8,
  255. MAX8997_REG_BUCK3CTRL,
  256. MAX8997_REG_BUCK3DVS,
  257. MAX8997_REG_BUCK4CTRL,
  258. MAX8997_REG_BUCK4DVS,
  259. MAX8997_REG_BUCK5CTRL,
  260. MAX8997_REG_BUCK5DVS1,
  261. MAX8997_REG_BUCK5DVS2,
  262. MAX8997_REG_BUCK5DVS3,
  263. MAX8997_REG_BUCK5DVS4,
  264. MAX8997_REG_BUCK5DVS5,
  265. MAX8997_REG_BUCK5DVS6,
  266. MAX8997_REG_BUCK5DVS7,
  267. MAX8997_REG_BUCK5DVS8,
  268. MAX8997_REG_BUCK6CTRL,
  269. MAX8997_REG_BUCK6BPSKIPCTRL,
  270. MAX8997_REG_BUCK7CTRL,
  271. MAX8997_REG_BUCK7DVS,
  272. MAX8997_REG_LDO1CTRL,
  273. MAX8997_REG_LDO2CTRL,
  274. MAX8997_REG_LDO3CTRL,
  275. MAX8997_REG_LDO4CTRL,
  276. MAX8997_REG_LDO5CTRL,
  277. MAX8997_REG_LDO6CTRL,
  278. MAX8997_REG_LDO7CTRL,
  279. MAX8997_REG_LDO8CTRL,
  280. MAX8997_REG_LDO9CTRL,
  281. MAX8997_REG_LDO10CTRL,
  282. MAX8997_REG_LDO11CTRL,
  283. MAX8997_REG_LDO12CTRL,
  284. MAX8997_REG_LDO13CTRL,
  285. MAX8997_REG_LDO14CTRL,
  286. MAX8997_REG_LDO15CTRL,
  287. MAX8997_REG_LDO16CTRL,
  288. MAX8997_REG_LDO17CTRL,
  289. MAX8997_REG_LDO18CTRL,
  290. MAX8997_REG_LDO21CTRL,
  291. MAX8997_REG_MBCCTRL1,
  292. MAX8997_REG_MBCCTRL2,
  293. MAX8997_REG_MBCCTRL3,
  294. MAX8997_REG_MBCCTRL4,
  295. MAX8997_REG_MBCCTRL5,
  296. MAX8997_REG_MBCCTRL6,
  297. MAX8997_REG_OTPCGHCVS,
  298. MAX8997_REG_SAFEOUTCTRL,
  299. MAX8997_REG_LBCNFG1,
  300. MAX8997_REG_LBCNFG2,
  301. MAX8997_REG_BBCCTRL,
  302. MAX8997_REG_FLASH1_CUR,
  303. MAX8997_REG_FLASH2_CUR,
  304. MAX8997_REG_MOVIE_CUR,
  305. MAX8997_REG_GSMB_CUR,
  306. MAX8997_REG_BOOST_CNTL,
  307. MAX8997_REG_LEN_CNTL,
  308. MAX8997_REG_FLASH_CNTL,
  309. MAX8997_REG_WDT_CNTL,
  310. MAX8997_REG_MAXFLASH1,
  311. MAX8997_REG_MAXFLASH2,
  312. MAX8997_REG_FLASHSTATUSMASK,
  313. MAX8997_REG_GPIOCNTL1,
  314. MAX8997_REG_GPIOCNTL2,
  315. MAX8997_REG_GPIOCNTL3,
  316. MAX8997_REG_GPIOCNTL4,
  317. MAX8997_REG_GPIOCNTL5,
  318. MAX8997_REG_GPIOCNTL6,
  319. MAX8997_REG_GPIOCNTL7,
  320. MAX8997_REG_GPIOCNTL8,
  321. MAX8997_REG_GPIOCNTL9,
  322. MAX8997_REG_GPIOCNTL10,
  323. MAX8997_REG_GPIOCNTL11,
  324. MAX8997_REG_GPIOCNTL12,
  325. MAX8997_REG_LDO1CONFIG,
  326. MAX8997_REG_LDO2CONFIG,
  327. MAX8997_REG_LDO3CONFIG,
  328. MAX8997_REG_LDO4CONFIG,
  329. MAX8997_REG_LDO5CONFIG,
  330. MAX8997_REG_LDO6CONFIG,
  331. MAX8997_REG_LDO7CONFIG,
  332. MAX8997_REG_LDO8CONFIG,
  333. MAX8997_REG_LDO9CONFIG,
  334. MAX8997_REG_LDO10CONFIG,
  335. MAX8997_REG_LDO11CONFIG,
  336. MAX8997_REG_LDO12CONFIG,
  337. MAX8997_REG_LDO13CONFIG,
  338. MAX8997_REG_LDO14CONFIG,
  339. MAX8997_REG_LDO15CONFIG,
  340. MAX8997_REG_LDO16CONFIG,
  341. MAX8997_REG_LDO17CONFIG,
  342. MAX8997_REG_LDO18CONFIG,
  343. MAX8997_REG_LDO21CONFIG,
  344. MAX8997_REG_DVSOKTIMER1,
  345. MAX8997_REG_DVSOKTIMER2,
  346. MAX8997_REG_DVSOKTIMER4,
  347. MAX8997_REG_DVSOKTIMER5,
  348. };
  349. static u8 max8997_dumpaddr_muic[] = {
  350. MAX8997_MUIC_REG_INTMASK1,
  351. MAX8997_MUIC_REG_INTMASK2,
  352. MAX8997_MUIC_REG_INTMASK3,
  353. MAX8997_MUIC_REG_CDETCTRL,
  354. MAX8997_MUIC_REG_CONTROL1,
  355. MAX8997_MUIC_REG_CONTROL2,
  356. MAX8997_MUIC_REG_CONTROL3,
  357. };
  358. static u8 max8997_dumpaddr_haptic[] = {
  359. MAX8997_HAPTIC_REG_CONF1,
  360. MAX8997_HAPTIC_REG_CONF2,
  361. MAX8997_HAPTIC_REG_DRVCONF,
  362. MAX8997_HAPTIC_REG_CYCLECONF1,
  363. MAX8997_HAPTIC_REG_CYCLECONF2,
  364. MAX8997_HAPTIC_REG_SIGCONF1,
  365. MAX8997_HAPTIC_REG_SIGCONF2,
  366. MAX8997_HAPTIC_REG_SIGCONF3,
  367. MAX8997_HAPTIC_REG_SIGCONF4,
  368. MAX8997_HAPTIC_REG_SIGDC1,
  369. MAX8997_HAPTIC_REG_SIGDC2,
  370. MAX8997_HAPTIC_REG_SIGPWMDC1,
  371. MAX8997_HAPTIC_REG_SIGPWMDC2,
  372. MAX8997_HAPTIC_REG_SIGPWMDC3,
  373. MAX8997_HAPTIC_REG_SIGPWMDC4,
  374. };
  375. static int max8997_freeze(struct device *dev)
  376. {
  377. struct i2c_client *i2c = to_i2c_client(dev);
  378. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  379. int i;
  380. for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_pmic); i++)
  381. max8997_read_reg(i2c, max8997_dumpaddr_pmic[i],
  382. &max8997->reg_dump[i]);
  383. for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_muic); i++)
  384. max8997_read_reg(i2c, max8997_dumpaddr_muic[i],
  385. &max8997->reg_dump[i + MAX8997_REG_PMIC_END]);
  386. for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_haptic); i++)
  387. max8997_read_reg(i2c, max8997_dumpaddr_haptic[i],
  388. &max8997->reg_dump[i + MAX8997_REG_PMIC_END +
  389. MAX8997_MUIC_REG_END]);
  390. return 0;
  391. }
  392. static int max8997_restore(struct device *dev)
  393. {
  394. struct i2c_client *i2c = to_i2c_client(dev);
  395. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  396. int i;
  397. for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_pmic); i++)
  398. max8997_write_reg(i2c, max8997_dumpaddr_pmic[i],
  399. max8997->reg_dump[i]);
  400. for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_muic); i++)
  401. max8997_write_reg(i2c, max8997_dumpaddr_muic[i],
  402. max8997->reg_dump[i + MAX8997_REG_PMIC_END]);
  403. for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_haptic); i++)
  404. max8997_write_reg(i2c, max8997_dumpaddr_haptic[i],
  405. max8997->reg_dump[i + MAX8997_REG_PMIC_END +
  406. MAX8997_MUIC_REG_END]);
  407. return 0;
  408. }
  409. static int max8997_suspend(struct device *dev)
  410. {
  411. struct i2c_client *i2c = to_i2c_client(dev);
  412. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  413. if (device_may_wakeup(dev))
  414. irq_set_irq_wake(max8997->irq, 1);
  415. return 0;
  416. }
  417. static int max8997_resume(struct device *dev)
  418. {
  419. struct i2c_client *i2c = to_i2c_client(dev);
  420. struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
  421. if (device_may_wakeup(dev))
  422. irq_set_irq_wake(max8997->irq, 0);
  423. return max8997_irq_resume(max8997);
  424. }
  425. static const struct dev_pm_ops max8997_pm = {
  426. .suspend = max8997_suspend,
  427. .resume = max8997_resume,
  428. .freeze = max8997_freeze,
  429. .restore = max8997_restore,
  430. };
  431. static struct i2c_driver max8997_i2c_driver = {
  432. .driver = {
  433. .name = "max8997",
  434. .pm = &max8997_pm,
  435. .suppress_bind_attrs = true,
  436. .of_match_table = of_match_ptr(max8997_pmic_dt_match),
  437. },
  438. .probe = max8997_i2c_probe,
  439. .id_table = max8997_i2c_id,
  440. };
  441. static int __init max8997_i2c_init(void)
  442. {
  443. return i2c_add_driver(&max8997_i2c_driver);
  444. }
  445. /* init early so consumer devices can complete system boot */
  446. subsys_initcall(max8997_i2c_init);