sec-core.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_irq.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/mutex.h>
  23. #include <linux/mfd/core.h>
  24. #include <linux/mfd/samsung/core.h>
  25. #include <linux/mfd/samsung/irq.h>
  26. #include <linux/mfd/samsung/rtc.h>
  27. #include <linux/mfd/samsung/s2mps11.h>
  28. #include <linux/mfd/samsung/s5m8763.h>
  29. #include <linux/mfd/samsung/s5m8767.h>
  30. #include <linux/regmap.h>
  31. static struct mfd_cell s5m8751_devs[] = {
  32. {
  33. .name = "s5m8751-pmic",
  34. }, {
  35. .name = "s5m-charger",
  36. }, {
  37. .name = "s5m8751-codec",
  38. },
  39. };
  40. static struct mfd_cell s5m8763_devs[] = {
  41. {
  42. .name = "s5m8763-pmic",
  43. }, {
  44. .name = "s5m-rtc",
  45. }, {
  46. .name = "s5m-charger",
  47. },
  48. };
  49. static struct mfd_cell s5m8767_devs[] = {
  50. {
  51. .name = "s5m8767-pmic",
  52. }, {
  53. .name = "s5m-rtc",
  54. },
  55. };
  56. static struct mfd_cell s2mps11_devs[] = {
  57. {
  58. .name = "s2mps11-pmic",
  59. },
  60. };
  61. #ifdef CONFIG_OF
  62. static struct of_device_id sec_dt_match[] = {
  63. { .compatible = "samsung,s5m8767-pmic",
  64. .data = (void *)S5M8767X,
  65. },
  66. { .compatible = "samsung,s2mps11-pmic",
  67. .data = (void *)S2MPS11X,
  68. },
  69. {},
  70. };
  71. #endif
  72. int sec_reg_read(struct sec_pmic_dev *sec_pmic, u8 reg, void *dest)
  73. {
  74. return regmap_read(sec_pmic->regmap, reg, dest);
  75. }
  76. EXPORT_SYMBOL_GPL(sec_reg_read);
  77. int sec_bulk_read(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
  78. {
  79. return regmap_bulk_read(sec_pmic->regmap, reg, buf, count);
  80. }
  81. EXPORT_SYMBOL_GPL(sec_bulk_read);
  82. int sec_reg_write(struct sec_pmic_dev *sec_pmic, u8 reg, u8 value)
  83. {
  84. return regmap_write(sec_pmic->regmap, reg, value);
  85. }
  86. EXPORT_SYMBOL_GPL(sec_reg_write);
  87. int sec_bulk_write(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
  88. {
  89. return regmap_raw_write(sec_pmic->regmap, reg, buf, count);
  90. }
  91. EXPORT_SYMBOL_GPL(sec_bulk_write);
  92. int sec_reg_update(struct sec_pmic_dev *sec_pmic, u8 reg, u8 val, u8 mask)
  93. {
  94. return regmap_update_bits(sec_pmic->regmap, reg, mask, val);
  95. }
  96. EXPORT_SYMBOL_GPL(sec_reg_update);
  97. static struct regmap_config sec_regmap_config = {
  98. .reg_bits = 8,
  99. .val_bits = 8,
  100. };
  101. static struct regmap_config s2mps11_regmap_config = {
  102. .reg_bits = 8,
  103. .val_bits = 8,
  104. .max_register = S2MPS11_REG_L38CTRL,
  105. };
  106. static struct regmap_config s5m8763_regmap_config = {
  107. .reg_bits = 8,
  108. .val_bits = 8,
  109. .max_register = S5M8763_REG_LBCNFG2,
  110. };
  111. static struct regmap_config s5m8767_regmap_config = {
  112. .reg_bits = 8,
  113. .val_bits = 8,
  114. .max_register = S5M8767_REG_LDO28CTRL,
  115. };
  116. #ifdef CONFIG_OF
  117. /*
  118. * Only the common platform data elements for s5m8767 are parsed here from the
  119. * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
  120. * others have to parse their own platform data elements from device tree.
  121. *
  122. * The s5m8767 platform data structure is instantiated here and the drivers for
  123. * the sub-modules need not instantiate another instance while parsing their
  124. * platform data.
  125. */
  126. static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
  127. struct device *dev)
  128. {
  129. struct sec_platform_data *pd;
  130. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  131. if (!pd) {
  132. dev_err(dev, "could not allocate memory for pdata\n");
  133. return ERR_PTR(-ENOMEM);
  134. }
  135. /*
  136. * ToDo: the 'wakeup' member in the platform data is more of a linux
  137. * specfic information. Hence, there is no binding for that yet and
  138. * not parsed here.
  139. */
  140. return pd;
  141. }
  142. #else
  143. static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
  144. struct device *dev)
  145. {
  146. return 0;
  147. }
  148. #endif
  149. static inline int sec_i2c_get_driver_data(struct i2c_client *i2c,
  150. const struct i2c_device_id *id)
  151. {
  152. #ifdef CONFIG_OF
  153. if (i2c->dev.of_node) {
  154. const struct of_device_id *match;
  155. match = of_match_node(sec_dt_match, i2c->dev.of_node);
  156. return (int)match->data;
  157. }
  158. #endif
  159. return (int)id->driver_data;
  160. }
  161. static int sec_pmic_probe(struct i2c_client *i2c,
  162. const struct i2c_device_id *id)
  163. {
  164. struct sec_platform_data *pdata = i2c->dev.platform_data;
  165. const struct regmap_config *regmap;
  166. struct sec_pmic_dev *sec_pmic;
  167. int ret;
  168. sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
  169. GFP_KERNEL);
  170. if (sec_pmic == NULL)
  171. return -ENOMEM;
  172. i2c_set_clientdata(i2c, sec_pmic);
  173. sec_pmic->dev = &i2c->dev;
  174. sec_pmic->i2c = i2c;
  175. sec_pmic->irq = i2c->irq;
  176. sec_pmic->type = sec_i2c_get_driver_data(i2c, id);
  177. if (sec_pmic->dev->of_node) {
  178. pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
  179. if (IS_ERR(pdata)) {
  180. ret = PTR_ERR(pdata);
  181. return ret;
  182. }
  183. pdata->device_type = sec_pmic->type;
  184. }
  185. if (pdata) {
  186. sec_pmic->device_type = pdata->device_type;
  187. sec_pmic->ono = pdata->ono;
  188. sec_pmic->irq_base = pdata->irq_base;
  189. sec_pmic->wakeup = pdata->wakeup;
  190. sec_pmic->pdata = pdata;
  191. }
  192. switch (sec_pmic->device_type) {
  193. case S2MPS11X:
  194. regmap = &s2mps11_regmap_config;
  195. break;
  196. case S5M8763X:
  197. regmap = &s5m8763_regmap_config;
  198. break;
  199. case S5M8767X:
  200. regmap = &s5m8767_regmap_config;
  201. break;
  202. default:
  203. regmap = &sec_regmap_config;
  204. break;
  205. }
  206. sec_pmic->regmap = devm_regmap_init_i2c(i2c, regmap);
  207. if (IS_ERR(sec_pmic->regmap)) {
  208. ret = PTR_ERR(sec_pmic->regmap);
  209. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  210. ret);
  211. return ret;
  212. }
  213. sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
  214. i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
  215. if (pdata && pdata->cfg_pmic_irq)
  216. pdata->cfg_pmic_irq();
  217. sec_irq_init(sec_pmic);
  218. pm_runtime_set_active(sec_pmic->dev);
  219. switch (sec_pmic->device_type) {
  220. case S5M8751X:
  221. ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
  222. ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
  223. break;
  224. case S5M8763X:
  225. ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
  226. ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
  227. break;
  228. case S5M8767X:
  229. ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
  230. ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
  231. break;
  232. case S2MPS11X:
  233. ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
  234. ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
  235. break;
  236. default:
  237. /* If this happens the probe function is problem */
  238. BUG();
  239. }
  240. if (ret)
  241. goto err;
  242. return ret;
  243. err:
  244. sec_irq_exit(sec_pmic);
  245. i2c_unregister_device(sec_pmic->rtc);
  246. return ret;
  247. }
  248. static int sec_pmic_remove(struct i2c_client *i2c)
  249. {
  250. struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
  251. mfd_remove_devices(sec_pmic->dev);
  252. sec_irq_exit(sec_pmic);
  253. i2c_unregister_device(sec_pmic->rtc);
  254. return 0;
  255. }
  256. static const struct i2c_device_id sec_pmic_id[] = {
  257. { "sec_pmic", 0 },
  258. { }
  259. };
  260. MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
  261. static struct i2c_driver sec_pmic_driver = {
  262. .driver = {
  263. .name = "sec_pmic",
  264. .owner = THIS_MODULE,
  265. .of_match_table = of_match_ptr(sec_dt_match),
  266. },
  267. .probe = sec_pmic_probe,
  268. .remove = sec_pmic_remove,
  269. .id_table = sec_pmic_id,
  270. };
  271. static int __init sec_pmic_init(void)
  272. {
  273. return i2c_add_driver(&sec_pmic_driver);
  274. }
  275. subsys_initcall(sec_pmic_init);
  276. static void __exit sec_pmic_exit(void)
  277. {
  278. i2c_del_driver(&sec_pmic_driver);
  279. }
  280. module_exit(sec_pmic_exit);
  281. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  282. MODULE_DESCRIPTION("Core support for the S5M MFD");
  283. MODULE_LICENSE("GPL");