sec-core.c 8.9 KB

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