sec-core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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/s2mpu02.h>
  32. #include <linux/mfd/samsung/s5m8763.h>
  33. #include <linux/mfd/samsung/s5m8767.h>
  34. #include <linux/regmap.h>
  35. static const struct mfd_cell s5m8751_devs[] = {
  36. {
  37. .name = "s5m8751-pmic",
  38. }, {
  39. .name = "s5m-charger",
  40. }, {
  41. .name = "s5m8751-codec",
  42. },
  43. };
  44. static const struct mfd_cell s5m8763_devs[] = {
  45. {
  46. .name = "s5m8763-pmic",
  47. }, {
  48. .name = "s5m-rtc",
  49. }, {
  50. .name = "s5m-charger",
  51. },
  52. };
  53. static const struct mfd_cell s5m8767_devs[] = {
  54. {
  55. .name = "s5m8767-pmic",
  56. }, {
  57. .name = "s5m-rtc",
  58. }, {
  59. .name = "s5m8767-clk",
  60. .of_compatible = "samsung,s5m8767-clk",
  61. }
  62. };
  63. static const struct mfd_cell s2mps11_devs[] = {
  64. {
  65. .name = "s2mps11-pmic",
  66. }, {
  67. .name = "s2mps14-rtc",
  68. }, {
  69. .name = "s2mps11-clk",
  70. .of_compatible = "samsung,s2mps11-clk",
  71. }
  72. };
  73. static const struct mfd_cell s2mps13_devs[] = {
  74. { .name = "s2mps13-pmic", },
  75. { .name = "s2mps13-rtc", },
  76. {
  77. .name = "s2mps13-clk",
  78. .of_compatible = "samsung,s2mps13-clk",
  79. },
  80. };
  81. static const struct mfd_cell s2mps14_devs[] = {
  82. {
  83. .name = "s2mps14-pmic",
  84. }, {
  85. .name = "s2mps14-rtc",
  86. }, {
  87. .name = "s2mps14-clk",
  88. .of_compatible = "samsung,s2mps14-clk",
  89. }
  90. };
  91. static const struct mfd_cell s2mpa01_devs[] = {
  92. {
  93. .name = "s2mpa01-pmic",
  94. },
  95. };
  96. static const struct mfd_cell s2mpu02_devs[] = {
  97. { .name = "s2mpu02-pmic", },
  98. { .name = "s2mpu02-rtc", },
  99. {
  100. .name = "s2mpu02-clk",
  101. .of_compatible = "samsung,s2mpu02-clk",
  102. }
  103. };
  104. #ifdef CONFIG_OF
  105. static const struct of_device_id sec_dt_match[] = {
  106. { .compatible = "samsung,s5m8767-pmic",
  107. .data = (void *)S5M8767X,
  108. }, {
  109. .compatible = "samsung,s2mps11-pmic",
  110. .data = (void *)S2MPS11X,
  111. }, {
  112. .compatible = "samsung,s2mps13-pmic",
  113. .data = (void *)S2MPS13X,
  114. }, {
  115. .compatible = "samsung,s2mps14-pmic",
  116. .data = (void *)S2MPS14X,
  117. }, {
  118. .compatible = "samsung,s2mpa01-pmic",
  119. .data = (void *)S2MPA01,
  120. }, {
  121. .compatible = "samsung,s2mpu02-pmic",
  122. .data = (void *)S2MPU02,
  123. }, {
  124. /* Sentinel */
  125. },
  126. };
  127. #endif
  128. static bool s2mpa01_volatile(struct device *dev, unsigned int reg)
  129. {
  130. switch (reg) {
  131. case S2MPA01_REG_INT1M:
  132. case S2MPA01_REG_INT2M:
  133. case S2MPA01_REG_INT3M:
  134. return false;
  135. default:
  136. return true;
  137. }
  138. }
  139. static bool s2mps11_volatile(struct device *dev, unsigned int reg)
  140. {
  141. switch (reg) {
  142. case S2MPS11_REG_INT1M:
  143. case S2MPS11_REG_INT2M:
  144. case S2MPS11_REG_INT3M:
  145. return false;
  146. default:
  147. return true;
  148. }
  149. }
  150. static bool s2mpu02_volatile(struct device *dev, unsigned int reg)
  151. {
  152. switch (reg) {
  153. case S2MPU02_REG_INT1M:
  154. case S2MPU02_REG_INT2M:
  155. case S2MPU02_REG_INT3M:
  156. return false;
  157. default:
  158. return true;
  159. }
  160. }
  161. static bool s5m8763_volatile(struct device *dev, unsigned int reg)
  162. {
  163. switch (reg) {
  164. case S5M8763_REG_IRQM1:
  165. case S5M8763_REG_IRQM2:
  166. case S5M8763_REG_IRQM3:
  167. case S5M8763_REG_IRQM4:
  168. return false;
  169. default:
  170. return true;
  171. }
  172. }
  173. static const struct regmap_config sec_regmap_config = {
  174. .reg_bits = 8,
  175. .val_bits = 8,
  176. };
  177. static const struct regmap_config s2mpa01_regmap_config = {
  178. .reg_bits = 8,
  179. .val_bits = 8,
  180. .max_register = S2MPA01_REG_LDO_OVCB4,
  181. .volatile_reg = s2mpa01_volatile,
  182. .cache_type = REGCACHE_FLAT,
  183. };
  184. static const struct regmap_config s2mps11_regmap_config = {
  185. .reg_bits = 8,
  186. .val_bits = 8,
  187. .max_register = S2MPS11_REG_L38CTRL,
  188. .volatile_reg = s2mps11_volatile,
  189. .cache_type = REGCACHE_FLAT,
  190. };
  191. static const struct regmap_config s2mps13_regmap_config = {
  192. .reg_bits = 8,
  193. .val_bits = 8,
  194. .max_register = S2MPS13_REG_LDODSCH5,
  195. .volatile_reg = s2mps11_volatile,
  196. .cache_type = REGCACHE_FLAT,
  197. };
  198. static const struct regmap_config s2mps14_regmap_config = {
  199. .reg_bits = 8,
  200. .val_bits = 8,
  201. .max_register = S2MPS14_REG_LDODSCH3,
  202. .volatile_reg = s2mps11_volatile,
  203. .cache_type = REGCACHE_FLAT,
  204. };
  205. static const struct regmap_config s2mpu02_regmap_config = {
  206. .reg_bits = 8,
  207. .val_bits = 8,
  208. .max_register = S2MPU02_REG_DVSDATA,
  209. .volatile_reg = s2mpu02_volatile,
  210. .cache_type = REGCACHE_FLAT,
  211. };
  212. static const struct regmap_config s5m8763_regmap_config = {
  213. .reg_bits = 8,
  214. .val_bits = 8,
  215. .max_register = S5M8763_REG_LBCNFG2,
  216. .volatile_reg = s5m8763_volatile,
  217. .cache_type = REGCACHE_FLAT,
  218. };
  219. static const struct regmap_config s5m8767_regmap_config = {
  220. .reg_bits = 8,
  221. .val_bits = 8,
  222. .max_register = S5M8767_REG_LDO28CTRL,
  223. .volatile_reg = s2mps11_volatile,
  224. .cache_type = REGCACHE_FLAT,
  225. };
  226. #ifdef CONFIG_OF
  227. /*
  228. * Only the common platform data elements for s5m8767 are parsed here from the
  229. * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
  230. * others have to parse their own platform data elements from device tree.
  231. *
  232. * The s5m8767 platform data structure is instantiated here and the drivers for
  233. * the sub-modules need not instantiate another instance while parsing their
  234. * platform data.
  235. */
  236. static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
  237. struct device *dev)
  238. {
  239. struct sec_platform_data *pd;
  240. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  241. if (!pd)
  242. return ERR_PTR(-ENOMEM);
  243. /*
  244. * ToDo: the 'wakeup' member in the platform data is more of a linux
  245. * specfic information. Hence, there is no binding for that yet and
  246. * not parsed here.
  247. */
  248. return pd;
  249. }
  250. #else
  251. static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
  252. struct device *dev)
  253. {
  254. return NULL;
  255. }
  256. #endif
  257. static inline unsigned long sec_i2c_get_driver_data(struct i2c_client *i2c,
  258. const struct i2c_device_id *id)
  259. {
  260. #ifdef CONFIG_OF
  261. if (i2c->dev.of_node) {
  262. const struct of_device_id *match;
  263. match = of_match_node(sec_dt_match, i2c->dev.of_node);
  264. return (unsigned long)match->data;
  265. }
  266. #endif
  267. return id->driver_data;
  268. }
  269. static int sec_pmic_probe(struct i2c_client *i2c,
  270. const struct i2c_device_id *id)
  271. {
  272. struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
  273. const struct regmap_config *regmap;
  274. const struct mfd_cell *sec_devs;
  275. struct sec_pmic_dev *sec_pmic;
  276. unsigned long device_type;
  277. int ret, num_sec_devs;
  278. sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
  279. GFP_KERNEL);
  280. if (sec_pmic == NULL)
  281. return -ENOMEM;
  282. i2c_set_clientdata(i2c, sec_pmic);
  283. sec_pmic->dev = &i2c->dev;
  284. sec_pmic->i2c = i2c;
  285. sec_pmic->irq = i2c->irq;
  286. device_type = sec_i2c_get_driver_data(i2c, id);
  287. if (sec_pmic->dev->of_node) {
  288. pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
  289. if (IS_ERR(pdata)) {
  290. ret = PTR_ERR(pdata);
  291. return ret;
  292. }
  293. pdata->device_type = device_type;
  294. }
  295. if (pdata) {
  296. sec_pmic->device_type = pdata->device_type;
  297. sec_pmic->irq_base = pdata->irq_base;
  298. sec_pmic->wakeup = pdata->wakeup;
  299. sec_pmic->pdata = pdata;
  300. }
  301. switch (sec_pmic->device_type) {
  302. case S2MPA01:
  303. regmap = &s2mpa01_regmap_config;
  304. break;
  305. case S2MPS11X:
  306. regmap = &s2mps11_regmap_config;
  307. break;
  308. case S2MPS13X:
  309. regmap = &s2mps13_regmap_config;
  310. break;
  311. case S2MPS14X:
  312. regmap = &s2mps14_regmap_config;
  313. break;
  314. case S5M8763X:
  315. regmap = &s5m8763_regmap_config;
  316. break;
  317. case S5M8767X:
  318. regmap = &s5m8767_regmap_config;
  319. break;
  320. case S2MPU02:
  321. regmap = &s2mpu02_regmap_config;
  322. break;
  323. default:
  324. regmap = &sec_regmap_config;
  325. break;
  326. }
  327. sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
  328. if (IS_ERR(sec_pmic->regmap_pmic)) {
  329. ret = PTR_ERR(sec_pmic->regmap_pmic);
  330. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  331. ret);
  332. return ret;
  333. }
  334. if (pdata && pdata->cfg_pmic_irq)
  335. pdata->cfg_pmic_irq();
  336. sec_irq_init(sec_pmic);
  337. pm_runtime_set_active(sec_pmic->dev);
  338. switch (sec_pmic->device_type) {
  339. case S5M8751X:
  340. sec_devs = s5m8751_devs;
  341. num_sec_devs = ARRAY_SIZE(s5m8751_devs);
  342. break;
  343. case S5M8763X:
  344. sec_devs = s5m8763_devs;
  345. num_sec_devs = ARRAY_SIZE(s5m8763_devs);
  346. break;
  347. case S5M8767X:
  348. sec_devs = s5m8767_devs;
  349. num_sec_devs = ARRAY_SIZE(s5m8767_devs);
  350. break;
  351. case S2MPA01:
  352. sec_devs = s2mpa01_devs;
  353. num_sec_devs = ARRAY_SIZE(s2mpa01_devs);
  354. break;
  355. case S2MPS11X:
  356. sec_devs = s2mps11_devs;
  357. num_sec_devs = ARRAY_SIZE(s2mps11_devs);
  358. break;
  359. case S2MPS13X:
  360. sec_devs = s2mps13_devs;
  361. num_sec_devs = ARRAY_SIZE(s2mps13_devs);
  362. break;
  363. case S2MPS14X:
  364. sec_devs = s2mps14_devs;
  365. num_sec_devs = ARRAY_SIZE(s2mps14_devs);
  366. break;
  367. case S2MPU02:
  368. sec_devs = s2mpu02_devs;
  369. num_sec_devs = ARRAY_SIZE(s2mpu02_devs);
  370. break;
  371. default:
  372. /* If this happens the probe function is problem */
  373. BUG();
  374. }
  375. ret = mfd_add_devices(sec_pmic->dev, -1, sec_devs, num_sec_devs, NULL,
  376. 0, NULL);
  377. if (ret)
  378. goto err_mfd;
  379. device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup);
  380. return ret;
  381. err_mfd:
  382. sec_irq_exit(sec_pmic);
  383. return ret;
  384. }
  385. static int sec_pmic_remove(struct i2c_client *i2c)
  386. {
  387. struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
  388. mfd_remove_devices(sec_pmic->dev);
  389. sec_irq_exit(sec_pmic);
  390. return 0;
  391. }
  392. #ifdef CONFIG_PM_SLEEP
  393. static int sec_pmic_suspend(struct device *dev)
  394. {
  395. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  396. struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
  397. if (device_may_wakeup(dev))
  398. enable_irq_wake(sec_pmic->irq);
  399. /*
  400. * PMIC IRQ must be disabled during suspend for RTC alarm
  401. * to work properly.
  402. * When device is woken up from suspend, an
  403. * interrupt occurs before resuming I2C bus controller.
  404. * The interrupt is handled by regmap_irq_thread which tries
  405. * to read RTC registers. This read fails (I2C is still
  406. * suspended) and RTC Alarm interrupt is disabled.
  407. */
  408. disable_irq(sec_pmic->irq);
  409. return 0;
  410. }
  411. static int sec_pmic_resume(struct device *dev)
  412. {
  413. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  414. struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
  415. if (device_may_wakeup(dev))
  416. disable_irq_wake(sec_pmic->irq);
  417. enable_irq(sec_pmic->irq);
  418. return 0;
  419. }
  420. #endif /* CONFIG_PM_SLEEP */
  421. static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume);
  422. static const struct i2c_device_id sec_pmic_id[] = {
  423. { "sec_pmic", 0 },
  424. { }
  425. };
  426. MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
  427. static struct i2c_driver sec_pmic_driver = {
  428. .driver = {
  429. .name = "sec_pmic",
  430. .owner = THIS_MODULE,
  431. .pm = &sec_pmic_pm_ops,
  432. .of_match_table = of_match_ptr(sec_dt_match),
  433. },
  434. .probe = sec_pmic_probe,
  435. .remove = sec_pmic_remove,
  436. .id_table = sec_pmic_id,
  437. };
  438. static int __init sec_pmic_init(void)
  439. {
  440. return i2c_add_driver(&sec_pmic_driver);
  441. }
  442. subsys_initcall(sec_pmic_init);
  443. static void __exit sec_pmic_exit(void)
  444. {
  445. i2c_del_driver(&sec_pmic_driver);
  446. }
  447. module_exit(sec_pmic_exit);
  448. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  449. MODULE_DESCRIPTION("Core support for the S5M MFD");
  450. MODULE_LICENSE("GPL");