sec-core.c 9.9 KB

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