sec-core.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 bool s2mps11_volatile(struct device *dev, unsigned int reg)
  98. {
  99. switch (reg) {
  100. case S2MPS11_REG_INT1M:
  101. case S2MPS11_REG_INT2M:
  102. case S2MPS11_REG_INT3M:
  103. return false;
  104. default:
  105. return true;
  106. }
  107. }
  108. static bool s5m8763_volatile(struct device *dev, unsigned int reg)
  109. {
  110. switch (reg) {
  111. case S5M8763_REG_IRQM1:
  112. case S5M8763_REG_IRQM2:
  113. case S5M8763_REG_IRQM3:
  114. case S5M8763_REG_IRQM4:
  115. return false;
  116. default:
  117. return true;
  118. }
  119. }
  120. static struct regmap_config sec_regmap_config = {
  121. .reg_bits = 8,
  122. .val_bits = 8,
  123. };
  124. static struct regmap_config s2mps11_regmap_config = {
  125. .reg_bits = 8,
  126. .val_bits = 8,
  127. .max_register = S2MPS11_REG_L38CTRL,
  128. .volatile_reg = s2mps11_volatile,
  129. .cache_type = REGCACHE_FLAT,
  130. };
  131. static struct regmap_config s5m8763_regmap_config = {
  132. .reg_bits = 8,
  133. .val_bits = 8,
  134. .max_register = S5M8763_REG_LBCNFG2,
  135. .volatile_reg = s5m8763_volatile,
  136. .cache_type = REGCACHE_FLAT,
  137. };
  138. static struct regmap_config s5m8767_regmap_config = {
  139. .reg_bits = 8,
  140. .val_bits = 8,
  141. .max_register = S5M8767_REG_LDO28CTRL,
  142. .volatile_reg = s2mps11_volatile,
  143. .cache_type = REGCACHE_FLAT,
  144. };
  145. #ifdef CONFIG_OF
  146. /*
  147. * Only the common platform data elements for s5m8767 are parsed here from the
  148. * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
  149. * others have to parse their own platform data elements from device tree.
  150. *
  151. * The s5m8767 platform data structure is instantiated here and the drivers for
  152. * the sub-modules need not instantiate another instance while parsing their
  153. * platform data.
  154. */
  155. static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
  156. struct device *dev)
  157. {
  158. struct sec_platform_data *pd;
  159. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  160. if (!pd) {
  161. dev_err(dev, "could not allocate memory for pdata\n");
  162. return ERR_PTR(-ENOMEM);
  163. }
  164. /*
  165. * ToDo: the 'wakeup' member in the platform data is more of a linux
  166. * specfic information. Hence, there is no binding for that yet and
  167. * not parsed here.
  168. */
  169. return pd;
  170. }
  171. #else
  172. static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
  173. struct device *dev)
  174. {
  175. return 0;
  176. }
  177. #endif
  178. static inline int sec_i2c_get_driver_data(struct i2c_client *i2c,
  179. const struct i2c_device_id *id)
  180. {
  181. #ifdef CONFIG_OF
  182. if (i2c->dev.of_node) {
  183. const struct of_device_id *match;
  184. match = of_match_node(sec_dt_match, i2c->dev.of_node);
  185. return (int)match->data;
  186. }
  187. #endif
  188. return (int)id->driver_data;
  189. }
  190. static int sec_pmic_probe(struct i2c_client *i2c,
  191. const struct i2c_device_id *id)
  192. {
  193. struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
  194. const struct regmap_config *regmap;
  195. struct sec_pmic_dev *sec_pmic;
  196. int ret;
  197. sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
  198. GFP_KERNEL);
  199. if (sec_pmic == NULL)
  200. return -ENOMEM;
  201. i2c_set_clientdata(i2c, sec_pmic);
  202. sec_pmic->dev = &i2c->dev;
  203. sec_pmic->i2c = i2c;
  204. sec_pmic->irq = i2c->irq;
  205. sec_pmic->type = sec_i2c_get_driver_data(i2c, id);
  206. if (sec_pmic->dev->of_node) {
  207. pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
  208. if (IS_ERR(pdata)) {
  209. ret = PTR_ERR(pdata);
  210. return ret;
  211. }
  212. pdata->device_type = sec_pmic->type;
  213. }
  214. if (pdata) {
  215. sec_pmic->device_type = pdata->device_type;
  216. sec_pmic->ono = pdata->ono;
  217. sec_pmic->irq_base = pdata->irq_base;
  218. sec_pmic->wakeup = pdata->wakeup;
  219. sec_pmic->pdata = pdata;
  220. }
  221. switch (sec_pmic->device_type) {
  222. case S2MPS11X:
  223. regmap = &s2mps11_regmap_config;
  224. break;
  225. case S5M8763X:
  226. regmap = &s5m8763_regmap_config;
  227. break;
  228. case S5M8767X:
  229. regmap = &s5m8767_regmap_config;
  230. break;
  231. default:
  232. regmap = &sec_regmap_config;
  233. break;
  234. }
  235. sec_pmic->regmap = devm_regmap_init_i2c(i2c, regmap);
  236. if (IS_ERR(sec_pmic->regmap)) {
  237. ret = PTR_ERR(sec_pmic->regmap);
  238. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  239. ret);
  240. return ret;
  241. }
  242. sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
  243. i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
  244. if (pdata && pdata->cfg_pmic_irq)
  245. pdata->cfg_pmic_irq();
  246. sec_irq_init(sec_pmic);
  247. pm_runtime_set_active(sec_pmic->dev);
  248. switch (sec_pmic->device_type) {
  249. case S5M8751X:
  250. ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
  251. ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
  252. break;
  253. case S5M8763X:
  254. ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
  255. ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
  256. break;
  257. case S5M8767X:
  258. ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
  259. ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
  260. break;
  261. case S2MPS11X:
  262. ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
  263. ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
  264. break;
  265. default:
  266. /* If this happens the probe function is problem */
  267. BUG();
  268. }
  269. if (ret)
  270. goto err;
  271. return ret;
  272. err:
  273. sec_irq_exit(sec_pmic);
  274. i2c_unregister_device(sec_pmic->rtc);
  275. return ret;
  276. }
  277. static int sec_pmic_remove(struct i2c_client *i2c)
  278. {
  279. struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
  280. mfd_remove_devices(sec_pmic->dev);
  281. sec_irq_exit(sec_pmic);
  282. i2c_unregister_device(sec_pmic->rtc);
  283. return 0;
  284. }
  285. static const struct i2c_device_id sec_pmic_id[] = {
  286. { "sec_pmic", 0 },
  287. { }
  288. };
  289. MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
  290. static struct i2c_driver sec_pmic_driver = {
  291. .driver = {
  292. .name = "sec_pmic",
  293. .owner = THIS_MODULE,
  294. .of_match_table = of_match_ptr(sec_dt_match),
  295. },
  296. .probe = sec_pmic_probe,
  297. .remove = sec_pmic_remove,
  298. .id_table = sec_pmic_id,
  299. };
  300. static int __init sec_pmic_init(void)
  301. {
  302. return i2c_add_driver(&sec_pmic_driver);
  303. }
  304. subsys_initcall(sec_pmic_init);
  305. static void __exit sec_pmic_exit(void)
  306. {
  307. i2c_del_driver(&sec_pmic_driver);
  308. }
  309. module_exit(sec_pmic_exit);
  310. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  311. MODULE_DESCRIPTION("Core support for the S5M MFD");
  312. MODULE_LICENSE("GPL");