sec-core.c 8.1 KB

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