sec-core.c 6.6 KB

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