sec-core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * sec-core.c
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd
  5. * http://www.samsung.com
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/i2c.h>
  19. #include <linux/of.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/mutex.h>
  24. #include <linux/mfd/core.h>
  25. #include <linux/mfd/samsung/core.h>
  26. #include <linux/mfd/samsung/irq.h>
  27. #include <linux/mfd/samsung/s2mpa01.h>
  28. #include <linux/mfd/samsung/s2mps11.h>
  29. #include <linux/mfd/samsung/s2mps13.h>
  30. #include <linux/mfd/samsung/s2mps14.h>
  31. #include <linux/mfd/samsung/s2mps15.h>
  32. #include <linux/mfd/samsung/s2mpu02.h>
  33. #include <linux/mfd/samsung/s5m8763.h>
  34. #include <linux/mfd/samsung/s5m8767.h>
  35. #include <linux/regmap.h>
  36. static const struct mfd_cell s5m8751_devs[] = {
  37. {
  38. .name = "s5m8751-pmic",
  39. }, {
  40. .name = "s5m-charger",
  41. }, {
  42. .name = "s5m8751-codec",
  43. },
  44. };
  45. static const struct mfd_cell s5m8763_devs[] = {
  46. {
  47. .name = "s5m8763-pmic",
  48. }, {
  49. .name = "s5m-rtc",
  50. }, {
  51. .name = "s5m-charger",
  52. },
  53. };
  54. static const struct mfd_cell s5m8767_devs[] = {
  55. {
  56. .name = "s5m8767-pmic",
  57. }, {
  58. .name = "s5m-rtc",
  59. }, {
  60. .name = "s5m8767-clk",
  61. .of_compatible = "samsung,s5m8767-clk",
  62. }
  63. };
  64. static const struct mfd_cell s2mps11_devs[] = {
  65. {
  66. .name = "s2mps11-regulator",
  67. }, {
  68. .name = "s2mps14-rtc",
  69. }, {
  70. .name = "s2mps11-clk",
  71. .of_compatible = "samsung,s2mps11-clk",
  72. }
  73. };
  74. static const struct mfd_cell s2mps13_devs[] = {
  75. { .name = "s2mps13-regulator", },
  76. { .name = "s2mps13-rtc", },
  77. {
  78. .name = "s2mps13-clk",
  79. .of_compatible = "samsung,s2mps13-clk",
  80. },
  81. };
  82. static const struct mfd_cell s2mps14_devs[] = {
  83. {
  84. .name = "s2mps14-regulator",
  85. }, {
  86. .name = "s2mps14-rtc",
  87. }, {
  88. .name = "s2mps14-clk",
  89. .of_compatible = "samsung,s2mps14-clk",
  90. }
  91. };
  92. static const struct mfd_cell s2mps15_devs[] = {
  93. {
  94. .name = "s2mps15-regulator",
  95. }, {
  96. .name = "s2mps15-rtc",
  97. }, {
  98. .name = "s2mps13-clk",
  99. .of_compatible = "samsung,s2mps13-clk",
  100. },
  101. };
  102. static const struct mfd_cell s2mpa01_devs[] = {
  103. {
  104. .name = "s2mpa01-pmic",
  105. },
  106. };
  107. static const struct mfd_cell s2mpu02_devs[] = {
  108. {
  109. .name = "s2mpu02-regulator",
  110. },
  111. };
  112. #ifdef CONFIG_OF
  113. static const struct of_device_id sec_dt_match[] = {
  114. { .compatible = "samsung,s5m8767-pmic",
  115. .data = (void *)S5M8767X,
  116. }, {
  117. .compatible = "samsung,s2mps11-pmic",
  118. .data = (void *)S2MPS11X,
  119. }, {
  120. .compatible = "samsung,s2mps13-pmic",
  121. .data = (void *)S2MPS13X,
  122. }, {
  123. .compatible = "samsung,s2mps14-pmic",
  124. .data = (void *)S2MPS14X,
  125. }, {
  126. .compatible = "samsung,s2mps15-pmic",
  127. .data = (void *)S2MPS15X,
  128. }, {
  129. .compatible = "samsung,s2mpa01-pmic",
  130. .data = (void *)S2MPA01,
  131. }, {
  132. .compatible = "samsung,s2mpu02-pmic",
  133. .data = (void *)S2MPU02,
  134. }, {
  135. /* Sentinel */
  136. },
  137. };
  138. #endif
  139. static bool s2mpa01_volatile(struct device *dev, unsigned int reg)
  140. {
  141. switch (reg) {
  142. case S2MPA01_REG_INT1M:
  143. case S2MPA01_REG_INT2M:
  144. case S2MPA01_REG_INT3M:
  145. return false;
  146. default:
  147. return true;
  148. }
  149. }
  150. static bool s2mps11_volatile(struct device *dev, unsigned int reg)
  151. {
  152. switch (reg) {
  153. case S2MPS11_REG_INT1M:
  154. case S2MPS11_REG_INT2M:
  155. case S2MPS11_REG_INT3M:
  156. return false;
  157. default:
  158. return true;
  159. }
  160. }
  161. static bool s2mpu02_volatile(struct device *dev, unsigned int reg)
  162. {
  163. switch (reg) {
  164. case S2MPU02_REG_INT1M:
  165. case S2MPU02_REG_INT2M:
  166. case S2MPU02_REG_INT3M:
  167. return false;
  168. default:
  169. return true;
  170. }
  171. }
  172. static bool s5m8763_volatile(struct device *dev, unsigned int reg)
  173. {
  174. switch (reg) {
  175. case S5M8763_REG_IRQM1:
  176. case S5M8763_REG_IRQM2:
  177. case S5M8763_REG_IRQM3:
  178. case S5M8763_REG_IRQM4:
  179. return false;
  180. default:
  181. return true;
  182. }
  183. }
  184. static const struct regmap_config sec_regmap_config = {
  185. .reg_bits = 8,
  186. .val_bits = 8,
  187. };
  188. static const struct regmap_config s2mpa01_regmap_config = {
  189. .reg_bits = 8,
  190. .val_bits = 8,
  191. .max_register = S2MPA01_REG_LDO_OVCB4,
  192. .volatile_reg = s2mpa01_volatile,
  193. .cache_type = REGCACHE_FLAT,
  194. };
  195. static const struct regmap_config s2mps11_regmap_config = {
  196. .reg_bits = 8,
  197. .val_bits = 8,
  198. .max_register = S2MPS11_REG_L38CTRL,
  199. .volatile_reg = s2mps11_volatile,
  200. .cache_type = REGCACHE_FLAT,
  201. };
  202. static const struct regmap_config s2mps13_regmap_config = {
  203. .reg_bits = 8,
  204. .val_bits = 8,
  205. .max_register = S2MPS13_REG_LDODSCH5,
  206. .volatile_reg = s2mps11_volatile,
  207. .cache_type = REGCACHE_FLAT,
  208. };
  209. static const struct regmap_config s2mps14_regmap_config = {
  210. .reg_bits = 8,
  211. .val_bits = 8,
  212. .max_register = S2MPS14_REG_LDODSCH3,
  213. .volatile_reg = s2mps11_volatile,
  214. .cache_type = REGCACHE_FLAT,
  215. };
  216. static const struct regmap_config s2mps15_regmap_config = {
  217. .reg_bits = 8,
  218. .val_bits = 8,
  219. .max_register = S2MPS15_REG_LDODSCH4,
  220. .volatile_reg = s2mps11_volatile,
  221. .cache_type = REGCACHE_FLAT,
  222. };
  223. static const struct regmap_config s2mpu02_regmap_config = {
  224. .reg_bits = 8,
  225. .val_bits = 8,
  226. .max_register = S2MPU02_REG_DVSDATA,
  227. .volatile_reg = s2mpu02_volatile,
  228. .cache_type = REGCACHE_FLAT,
  229. };
  230. static const struct regmap_config s5m8763_regmap_config = {
  231. .reg_bits = 8,
  232. .val_bits = 8,
  233. .max_register = S5M8763_REG_LBCNFG2,
  234. .volatile_reg = s5m8763_volatile,
  235. .cache_type = REGCACHE_FLAT,
  236. };
  237. static const struct regmap_config s5m8767_regmap_config = {
  238. .reg_bits = 8,
  239. .val_bits = 8,
  240. .max_register = S5M8767_REG_LDO28CTRL,
  241. .volatile_reg = s2mps11_volatile,
  242. .cache_type = REGCACHE_FLAT,
  243. };
  244. static void sec_pmic_dump_rev(struct sec_pmic_dev *sec_pmic)
  245. {
  246. unsigned int val;
  247. /* For each device type, the REG_ID is always the first register */
  248. if (!regmap_read(sec_pmic->regmap_pmic, S2MPS11_REG_ID, &val))
  249. dev_dbg(sec_pmic->dev, "Revision: 0x%x\n", val);
  250. }
  251. static void sec_pmic_configure(struct sec_pmic_dev *sec_pmic)
  252. {
  253. int err;
  254. if (sec_pmic->device_type != S2MPS13X)
  255. return;
  256. if (sec_pmic->pdata->disable_wrstbi) {
  257. /*
  258. * If WRSTBI pin is pulled down this feature must be disabled
  259. * because each Suspend to RAM will trigger buck voltage reset
  260. * to default values.
  261. */
  262. err = regmap_update_bits(sec_pmic->regmap_pmic,
  263. S2MPS13_REG_WRSTBI,
  264. S2MPS13_REG_WRSTBI_MASK, 0x0);
  265. if (err)
  266. dev_warn(sec_pmic->dev,
  267. "Cannot initialize WRSTBI config: %d\n",
  268. err);
  269. }
  270. }
  271. #ifdef CONFIG_OF
  272. /*
  273. * Only the common platform data elements for s5m8767 are parsed here from the
  274. * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
  275. * others have to parse their own platform data elements from device tree.
  276. *
  277. * The s5m8767 platform data structure is instantiated here and the drivers for
  278. * the sub-modules need not instantiate another instance while parsing their
  279. * platform data.
  280. */
  281. static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
  282. struct device *dev)
  283. {
  284. struct sec_platform_data *pd;
  285. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  286. if (!pd)
  287. return ERR_PTR(-ENOMEM);
  288. /*
  289. * ToDo: the 'wakeup' member in the platform data is more of a linux
  290. * specfic information. Hence, there is no binding for that yet and
  291. * not parsed here.
  292. */
  293. pd->manual_poweroff = of_property_read_bool(dev->of_node,
  294. "samsung,s2mps11-acokb-ground");
  295. pd->disable_wrstbi = of_property_read_bool(dev->of_node,
  296. "samsung,s2mps11-wrstbi-ground");
  297. return pd;
  298. }
  299. #else
  300. static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
  301. struct device *dev)
  302. {
  303. return NULL;
  304. }
  305. #endif
  306. static inline unsigned long sec_i2c_get_driver_data(struct i2c_client *i2c,
  307. const struct i2c_device_id *id)
  308. {
  309. #ifdef CONFIG_OF
  310. if (i2c->dev.of_node) {
  311. const struct of_device_id *match;
  312. match = of_match_node(sec_dt_match, i2c->dev.of_node);
  313. return (unsigned long)match->data;
  314. }
  315. #endif
  316. return id->driver_data;
  317. }
  318. static int sec_pmic_probe(struct i2c_client *i2c,
  319. const struct i2c_device_id *id)
  320. {
  321. struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
  322. const struct regmap_config *regmap;
  323. const struct mfd_cell *sec_devs;
  324. struct sec_pmic_dev *sec_pmic;
  325. unsigned long device_type;
  326. int ret, num_sec_devs;
  327. sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
  328. GFP_KERNEL);
  329. if (sec_pmic == NULL)
  330. return -ENOMEM;
  331. i2c_set_clientdata(i2c, sec_pmic);
  332. sec_pmic->dev = &i2c->dev;
  333. sec_pmic->i2c = i2c;
  334. sec_pmic->irq = i2c->irq;
  335. device_type = sec_i2c_get_driver_data(i2c, id);
  336. if (sec_pmic->dev->of_node) {
  337. pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
  338. if (IS_ERR(pdata)) {
  339. ret = PTR_ERR(pdata);
  340. return ret;
  341. }
  342. pdata->device_type = device_type;
  343. }
  344. if (pdata) {
  345. sec_pmic->device_type = pdata->device_type;
  346. sec_pmic->irq_base = pdata->irq_base;
  347. sec_pmic->wakeup = pdata->wakeup;
  348. sec_pmic->pdata = pdata;
  349. }
  350. switch (sec_pmic->device_type) {
  351. case S2MPA01:
  352. regmap = &s2mpa01_regmap_config;
  353. break;
  354. case S2MPS11X:
  355. regmap = &s2mps11_regmap_config;
  356. break;
  357. case S2MPS13X:
  358. regmap = &s2mps13_regmap_config;
  359. break;
  360. case S2MPS14X:
  361. regmap = &s2mps14_regmap_config;
  362. break;
  363. case S2MPS15X:
  364. regmap = &s2mps15_regmap_config;
  365. break;
  366. case S5M8763X:
  367. regmap = &s5m8763_regmap_config;
  368. break;
  369. case S5M8767X:
  370. regmap = &s5m8767_regmap_config;
  371. break;
  372. case S2MPU02:
  373. regmap = &s2mpu02_regmap_config;
  374. break;
  375. default:
  376. regmap = &sec_regmap_config;
  377. break;
  378. }
  379. sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
  380. if (IS_ERR(sec_pmic->regmap_pmic)) {
  381. ret = PTR_ERR(sec_pmic->regmap_pmic);
  382. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  383. ret);
  384. return ret;
  385. }
  386. if (pdata && pdata->cfg_pmic_irq)
  387. pdata->cfg_pmic_irq();
  388. sec_irq_init(sec_pmic);
  389. pm_runtime_set_active(sec_pmic->dev);
  390. switch (sec_pmic->device_type) {
  391. case S5M8751X:
  392. sec_devs = s5m8751_devs;
  393. num_sec_devs = ARRAY_SIZE(s5m8751_devs);
  394. break;
  395. case S5M8763X:
  396. sec_devs = s5m8763_devs;
  397. num_sec_devs = ARRAY_SIZE(s5m8763_devs);
  398. break;
  399. case S5M8767X:
  400. sec_devs = s5m8767_devs;
  401. num_sec_devs = ARRAY_SIZE(s5m8767_devs);
  402. break;
  403. case S2MPA01:
  404. sec_devs = s2mpa01_devs;
  405. num_sec_devs = ARRAY_SIZE(s2mpa01_devs);
  406. break;
  407. case S2MPS11X:
  408. sec_devs = s2mps11_devs;
  409. num_sec_devs = ARRAY_SIZE(s2mps11_devs);
  410. break;
  411. case S2MPS13X:
  412. sec_devs = s2mps13_devs;
  413. num_sec_devs = ARRAY_SIZE(s2mps13_devs);
  414. break;
  415. case S2MPS14X:
  416. sec_devs = s2mps14_devs;
  417. num_sec_devs = ARRAY_SIZE(s2mps14_devs);
  418. break;
  419. case S2MPS15X:
  420. sec_devs = s2mps15_devs;
  421. num_sec_devs = ARRAY_SIZE(s2mps15_devs);
  422. break;
  423. case S2MPU02:
  424. sec_devs = s2mpu02_devs;
  425. num_sec_devs = ARRAY_SIZE(s2mpu02_devs);
  426. break;
  427. default:
  428. /* If this happens the probe function is problem */
  429. BUG();
  430. }
  431. ret = devm_mfd_add_devices(sec_pmic->dev, -1, sec_devs, num_sec_devs,
  432. NULL, 0, NULL);
  433. if (ret)
  434. return ret;
  435. device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup);
  436. sec_pmic_configure(sec_pmic);
  437. sec_pmic_dump_rev(sec_pmic);
  438. return ret;
  439. }
  440. static void sec_pmic_shutdown(struct i2c_client *i2c)
  441. {
  442. struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
  443. unsigned int reg, mask;
  444. if (!sec_pmic->pdata->manual_poweroff)
  445. return;
  446. switch (sec_pmic->device_type) {
  447. case S2MPS11X:
  448. reg = S2MPS11_REG_CTRL1;
  449. mask = S2MPS11_CTRL1_PWRHOLD_MASK;
  450. break;
  451. default:
  452. /*
  453. * Currently only one board with S2MPS11 needs this, so just
  454. * ignore the rest.
  455. */
  456. dev_warn(sec_pmic->dev,
  457. "Unsupported device %lu for manual power off\n",
  458. sec_pmic->device_type);
  459. return;
  460. }
  461. regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, 0);
  462. }
  463. #ifdef CONFIG_PM_SLEEP
  464. static int sec_pmic_suspend(struct device *dev)
  465. {
  466. struct i2c_client *i2c = to_i2c_client(dev);
  467. struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
  468. if (device_may_wakeup(dev))
  469. enable_irq_wake(sec_pmic->irq);
  470. /*
  471. * PMIC IRQ must be disabled during suspend for RTC alarm
  472. * to work properly.
  473. * When device is woken up from suspend, an
  474. * interrupt occurs before resuming I2C bus controller.
  475. * The interrupt is handled by regmap_irq_thread which tries
  476. * to read RTC registers. This read fails (I2C is still
  477. * suspended) and RTC Alarm interrupt is disabled.
  478. */
  479. disable_irq(sec_pmic->irq);
  480. return 0;
  481. }
  482. static int sec_pmic_resume(struct device *dev)
  483. {
  484. struct i2c_client *i2c = to_i2c_client(dev);
  485. struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
  486. if (device_may_wakeup(dev))
  487. disable_irq_wake(sec_pmic->irq);
  488. enable_irq(sec_pmic->irq);
  489. return 0;
  490. }
  491. #endif /* CONFIG_PM_SLEEP */
  492. static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume);
  493. static const struct i2c_device_id sec_pmic_id[] = {
  494. { "sec_pmic", 0 },
  495. { }
  496. };
  497. MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
  498. static struct i2c_driver sec_pmic_driver = {
  499. .driver = {
  500. .name = "sec_pmic",
  501. .pm = &sec_pmic_pm_ops,
  502. .of_match_table = of_match_ptr(sec_dt_match),
  503. },
  504. .probe = sec_pmic_probe,
  505. .shutdown = sec_pmic_shutdown,
  506. .id_table = sec_pmic_id,
  507. };
  508. static int __init sec_pmic_init(void)
  509. {
  510. return i2c_add_driver(&sec_pmic_driver);
  511. }
  512. subsys_initcall(sec_pmic_init);
  513. static void __exit sec_pmic_exit(void)
  514. {
  515. i2c_del_driver(&sec_pmic_driver);
  516. }
  517. module_exit(sec_pmic_exit);
  518. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  519. MODULE_DESCRIPTION("Core support for the S5M MFD");
  520. MODULE_LICENSE("GPL");