sec-core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. MODULE_DEVICE_TABLE(of, sec_dt_match);
  139. #endif
  140. static bool s2mpa01_volatile(struct device *dev, unsigned int reg)
  141. {
  142. switch (reg) {
  143. case S2MPA01_REG_INT1M:
  144. case S2MPA01_REG_INT2M:
  145. case S2MPA01_REG_INT3M:
  146. return false;
  147. default:
  148. return true;
  149. }
  150. }
  151. static bool s2mps11_volatile(struct device *dev, unsigned int reg)
  152. {
  153. switch (reg) {
  154. case S2MPS11_REG_INT1M:
  155. case S2MPS11_REG_INT2M:
  156. case S2MPS11_REG_INT3M:
  157. return false;
  158. default:
  159. return true;
  160. }
  161. }
  162. static bool s2mpu02_volatile(struct device *dev, unsigned int reg)
  163. {
  164. switch (reg) {
  165. case S2MPU02_REG_INT1M:
  166. case S2MPU02_REG_INT2M:
  167. case S2MPU02_REG_INT3M:
  168. return false;
  169. default:
  170. return true;
  171. }
  172. }
  173. static bool s5m8763_volatile(struct device *dev, unsigned int reg)
  174. {
  175. switch (reg) {
  176. case S5M8763_REG_IRQM1:
  177. case S5M8763_REG_IRQM2:
  178. case S5M8763_REG_IRQM3:
  179. case S5M8763_REG_IRQM4:
  180. return false;
  181. default:
  182. return true;
  183. }
  184. }
  185. static const struct regmap_config sec_regmap_config = {
  186. .reg_bits = 8,
  187. .val_bits = 8,
  188. };
  189. static const struct regmap_config s2mpa01_regmap_config = {
  190. .reg_bits = 8,
  191. .val_bits = 8,
  192. .max_register = S2MPA01_REG_LDO_OVCB4,
  193. .volatile_reg = s2mpa01_volatile,
  194. .cache_type = REGCACHE_FLAT,
  195. };
  196. static const struct regmap_config s2mps11_regmap_config = {
  197. .reg_bits = 8,
  198. .val_bits = 8,
  199. .max_register = S2MPS11_REG_L38CTRL,
  200. .volatile_reg = s2mps11_volatile,
  201. .cache_type = REGCACHE_FLAT,
  202. };
  203. static const struct regmap_config s2mps13_regmap_config = {
  204. .reg_bits = 8,
  205. .val_bits = 8,
  206. .max_register = S2MPS13_REG_LDODSCH5,
  207. .volatile_reg = s2mps11_volatile,
  208. .cache_type = REGCACHE_FLAT,
  209. };
  210. static const struct regmap_config s2mps14_regmap_config = {
  211. .reg_bits = 8,
  212. .val_bits = 8,
  213. .max_register = S2MPS14_REG_LDODSCH3,
  214. .volatile_reg = s2mps11_volatile,
  215. .cache_type = REGCACHE_FLAT,
  216. };
  217. static const struct regmap_config s2mps15_regmap_config = {
  218. .reg_bits = 8,
  219. .val_bits = 8,
  220. .max_register = S2MPS15_REG_LDODSCH4,
  221. .volatile_reg = s2mps11_volatile,
  222. .cache_type = REGCACHE_FLAT,
  223. };
  224. static const struct regmap_config s2mpu02_regmap_config = {
  225. .reg_bits = 8,
  226. .val_bits = 8,
  227. .max_register = S2MPU02_REG_DVSDATA,
  228. .volatile_reg = s2mpu02_volatile,
  229. .cache_type = REGCACHE_FLAT,
  230. };
  231. static const struct regmap_config s5m8763_regmap_config = {
  232. .reg_bits = 8,
  233. .val_bits = 8,
  234. .max_register = S5M8763_REG_LBCNFG2,
  235. .volatile_reg = s5m8763_volatile,
  236. .cache_type = REGCACHE_FLAT,
  237. };
  238. static const struct regmap_config s5m8767_regmap_config = {
  239. .reg_bits = 8,
  240. .val_bits = 8,
  241. .max_register = S5M8767_REG_LDO28CTRL,
  242. .volatile_reg = s2mps11_volatile,
  243. .cache_type = REGCACHE_FLAT,
  244. };
  245. static void sec_pmic_dump_rev(struct sec_pmic_dev *sec_pmic)
  246. {
  247. unsigned int val;
  248. /* For each device type, the REG_ID is always the first register */
  249. if (!regmap_read(sec_pmic->regmap_pmic, S2MPS11_REG_ID, &val))
  250. dev_dbg(sec_pmic->dev, "Revision: 0x%x\n", val);
  251. }
  252. static void sec_pmic_configure(struct sec_pmic_dev *sec_pmic)
  253. {
  254. int err;
  255. if (sec_pmic->device_type != S2MPS13X)
  256. return;
  257. if (sec_pmic->pdata->disable_wrstbi) {
  258. /*
  259. * If WRSTBI pin is pulled down this feature must be disabled
  260. * because each Suspend to RAM will trigger buck voltage reset
  261. * to default values.
  262. */
  263. err = regmap_update_bits(sec_pmic->regmap_pmic,
  264. S2MPS13_REG_WRSTBI,
  265. S2MPS13_REG_WRSTBI_MASK, 0x0);
  266. if (err)
  267. dev_warn(sec_pmic->dev,
  268. "Cannot initialize WRSTBI config: %d\n",
  269. err);
  270. }
  271. }
  272. #ifdef CONFIG_OF
  273. /*
  274. * Only the common platform data elements for s5m8767 are parsed here from the
  275. * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
  276. * others have to parse their own platform data elements from device tree.
  277. *
  278. * The s5m8767 platform data structure is instantiated here and the drivers for
  279. * the sub-modules need not instantiate another instance while parsing their
  280. * platform data.
  281. */
  282. static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
  283. struct device *dev)
  284. {
  285. struct sec_platform_data *pd;
  286. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  287. if (!pd)
  288. return ERR_PTR(-ENOMEM);
  289. /*
  290. * ToDo: the 'wakeup' member in the platform data is more of a linux
  291. * specfic information. Hence, there is no binding for that yet and
  292. * not parsed here.
  293. */
  294. pd->manual_poweroff = of_property_read_bool(dev->of_node,
  295. "samsung,s2mps11-acokb-ground");
  296. pd->disable_wrstbi = of_property_read_bool(dev->of_node,
  297. "samsung,s2mps11-wrstbi-ground");
  298. return pd;
  299. }
  300. #else
  301. static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
  302. struct device *dev)
  303. {
  304. return NULL;
  305. }
  306. #endif
  307. static inline unsigned long sec_i2c_get_driver_data(struct i2c_client *i2c,
  308. const struct i2c_device_id *id)
  309. {
  310. #ifdef CONFIG_OF
  311. if (i2c->dev.of_node) {
  312. const struct of_device_id *match;
  313. match = of_match_node(sec_dt_match, i2c->dev.of_node);
  314. return (unsigned long)match->data;
  315. }
  316. #endif
  317. return id->driver_data;
  318. }
  319. static int sec_pmic_probe(struct i2c_client *i2c,
  320. const struct i2c_device_id *id)
  321. {
  322. struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
  323. const struct regmap_config *regmap;
  324. const struct mfd_cell *sec_devs;
  325. struct sec_pmic_dev *sec_pmic;
  326. unsigned long device_type;
  327. int ret, num_sec_devs;
  328. sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
  329. GFP_KERNEL);
  330. if (sec_pmic == NULL)
  331. return -ENOMEM;
  332. i2c_set_clientdata(i2c, sec_pmic);
  333. sec_pmic->dev = &i2c->dev;
  334. sec_pmic->i2c = i2c;
  335. sec_pmic->irq = i2c->irq;
  336. device_type = sec_i2c_get_driver_data(i2c, id);
  337. if (sec_pmic->dev->of_node) {
  338. pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
  339. if (IS_ERR(pdata)) {
  340. ret = PTR_ERR(pdata);
  341. return ret;
  342. }
  343. pdata->device_type = device_type;
  344. }
  345. if (pdata) {
  346. sec_pmic->device_type = pdata->device_type;
  347. sec_pmic->irq_base = pdata->irq_base;
  348. sec_pmic->wakeup = pdata->wakeup;
  349. sec_pmic->pdata = pdata;
  350. }
  351. switch (sec_pmic->device_type) {
  352. case S2MPA01:
  353. regmap = &s2mpa01_regmap_config;
  354. break;
  355. case S2MPS11X:
  356. regmap = &s2mps11_regmap_config;
  357. break;
  358. case S2MPS13X:
  359. regmap = &s2mps13_regmap_config;
  360. break;
  361. case S2MPS14X:
  362. regmap = &s2mps14_regmap_config;
  363. break;
  364. case S2MPS15X:
  365. regmap = &s2mps15_regmap_config;
  366. break;
  367. case S5M8763X:
  368. regmap = &s5m8763_regmap_config;
  369. break;
  370. case S5M8767X:
  371. regmap = &s5m8767_regmap_config;
  372. break;
  373. case S2MPU02:
  374. regmap = &s2mpu02_regmap_config;
  375. break;
  376. default:
  377. regmap = &sec_regmap_config;
  378. break;
  379. }
  380. sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
  381. if (IS_ERR(sec_pmic->regmap_pmic)) {
  382. ret = PTR_ERR(sec_pmic->regmap_pmic);
  383. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  384. ret);
  385. return ret;
  386. }
  387. if (pdata && pdata->cfg_pmic_irq)
  388. pdata->cfg_pmic_irq();
  389. sec_irq_init(sec_pmic);
  390. pm_runtime_set_active(sec_pmic->dev);
  391. switch (sec_pmic->device_type) {
  392. case S5M8751X:
  393. sec_devs = s5m8751_devs;
  394. num_sec_devs = ARRAY_SIZE(s5m8751_devs);
  395. break;
  396. case S5M8763X:
  397. sec_devs = s5m8763_devs;
  398. num_sec_devs = ARRAY_SIZE(s5m8763_devs);
  399. break;
  400. case S5M8767X:
  401. sec_devs = s5m8767_devs;
  402. num_sec_devs = ARRAY_SIZE(s5m8767_devs);
  403. break;
  404. case S2MPA01:
  405. sec_devs = s2mpa01_devs;
  406. num_sec_devs = ARRAY_SIZE(s2mpa01_devs);
  407. break;
  408. case S2MPS11X:
  409. sec_devs = s2mps11_devs;
  410. num_sec_devs = ARRAY_SIZE(s2mps11_devs);
  411. break;
  412. case S2MPS13X:
  413. sec_devs = s2mps13_devs;
  414. num_sec_devs = ARRAY_SIZE(s2mps13_devs);
  415. break;
  416. case S2MPS14X:
  417. sec_devs = s2mps14_devs;
  418. num_sec_devs = ARRAY_SIZE(s2mps14_devs);
  419. break;
  420. case S2MPS15X:
  421. sec_devs = s2mps15_devs;
  422. num_sec_devs = ARRAY_SIZE(s2mps15_devs);
  423. break;
  424. case S2MPU02:
  425. sec_devs = s2mpu02_devs;
  426. num_sec_devs = ARRAY_SIZE(s2mpu02_devs);
  427. break;
  428. default:
  429. /* If this happens the probe function is problem */
  430. BUG();
  431. }
  432. ret = devm_mfd_add_devices(sec_pmic->dev, -1, sec_devs, num_sec_devs,
  433. NULL, 0, NULL);
  434. if (ret)
  435. return ret;
  436. device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup);
  437. sec_pmic_configure(sec_pmic);
  438. sec_pmic_dump_rev(sec_pmic);
  439. return ret;
  440. }
  441. static void sec_pmic_shutdown(struct i2c_client *i2c)
  442. {
  443. struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
  444. unsigned int reg, mask;
  445. if (!sec_pmic->pdata->manual_poweroff)
  446. return;
  447. switch (sec_pmic->device_type) {
  448. case S2MPS11X:
  449. reg = S2MPS11_REG_CTRL1;
  450. mask = S2MPS11_CTRL1_PWRHOLD_MASK;
  451. break;
  452. default:
  453. /*
  454. * Currently only one board with S2MPS11 needs this, so just
  455. * ignore the rest.
  456. */
  457. dev_warn(sec_pmic->dev,
  458. "Unsupported device %lu for manual power off\n",
  459. sec_pmic->device_type);
  460. return;
  461. }
  462. regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, 0);
  463. }
  464. #ifdef CONFIG_PM_SLEEP
  465. static int sec_pmic_suspend(struct device *dev)
  466. {
  467. struct i2c_client *i2c = to_i2c_client(dev);
  468. struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
  469. if (device_may_wakeup(dev))
  470. enable_irq_wake(sec_pmic->irq);
  471. /*
  472. * PMIC IRQ must be disabled during suspend for RTC alarm
  473. * to work properly.
  474. * When device is woken up from suspend, an
  475. * interrupt occurs before resuming I2C bus controller.
  476. * The interrupt is handled by regmap_irq_thread which tries
  477. * to read RTC registers. This read fails (I2C is still
  478. * suspended) and RTC Alarm interrupt is disabled.
  479. */
  480. disable_irq(sec_pmic->irq);
  481. return 0;
  482. }
  483. static int sec_pmic_resume(struct device *dev)
  484. {
  485. struct i2c_client *i2c = to_i2c_client(dev);
  486. struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
  487. if (device_may_wakeup(dev))
  488. disable_irq_wake(sec_pmic->irq);
  489. enable_irq(sec_pmic->irq);
  490. return 0;
  491. }
  492. #endif /* CONFIG_PM_SLEEP */
  493. static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume);
  494. static const struct i2c_device_id sec_pmic_id[] = {
  495. { "sec_pmic", 0 },
  496. { }
  497. };
  498. MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
  499. static struct i2c_driver sec_pmic_driver = {
  500. .driver = {
  501. .name = "sec_pmic",
  502. .pm = &sec_pmic_pm_ops,
  503. .of_match_table = of_match_ptr(sec_dt_match),
  504. },
  505. .probe = sec_pmic_probe,
  506. .shutdown = sec_pmic_shutdown,
  507. .id_table = sec_pmic_id,
  508. };
  509. static int __init sec_pmic_init(void)
  510. {
  511. return i2c_add_driver(&sec_pmic_driver);
  512. }
  513. subsys_initcall(sec_pmic_init);
  514. static void __exit sec_pmic_exit(void)
  515. {
  516. i2c_del_driver(&sec_pmic_driver);
  517. }
  518. module_exit(sec_pmic_exit);
  519. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  520. MODULE_DESCRIPTION("Core support for the S5M MFD");
  521. MODULE_LICENSE("GPL");