tps65910.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * tps65910.c -- TI TPS6591x
  3. *
  4. * Copyright 2010 Texas Instruments Inc.
  5. *
  6. * Author: Graeme Gregory <gg@slimlogic.co.uk>
  7. * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/init.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/i2c.h>
  21. #include <linux/mfd/core.h>
  22. #include <linux/regmap.h>
  23. #include <linux/mfd/tps65910.h>
  24. #include <linux/of_device.h>
  25. static struct resource rtc_resources[] = {
  26. {
  27. .start = TPS65910_IRQ_RTC_ALARM,
  28. .end = TPS65910_IRQ_RTC_ALARM,
  29. .flags = IORESOURCE_IRQ,
  30. }
  31. };
  32. static struct mfd_cell tps65910s[] = {
  33. {
  34. .name = "tps65910-gpio",
  35. },
  36. {
  37. .name = "tps65910-pmic",
  38. },
  39. {
  40. .name = "tps65910-rtc",
  41. .num_resources = ARRAY_SIZE(rtc_resources),
  42. .resources = &rtc_resources[0],
  43. },
  44. {
  45. .name = "tps65910-power",
  46. },
  47. };
  48. static bool is_volatile_reg(struct device *dev, unsigned int reg)
  49. {
  50. struct tps65910 *tps65910 = dev_get_drvdata(dev);
  51. /*
  52. * Caching all regulator registers.
  53. * All regualator register address range is same for
  54. * TPS65910 and TPS65911
  55. */
  56. if ((reg >= TPS65910_VIO) && (reg <= TPS65910_VDAC)) {
  57. /* Check for non-existing register */
  58. if (tps65910_chip_id(tps65910) == TPS65910)
  59. if ((reg == TPS65911_VDDCTRL_OP) ||
  60. (reg == TPS65911_VDDCTRL_SR))
  61. return true;
  62. return false;
  63. }
  64. return true;
  65. }
  66. static const struct regmap_config tps65910_regmap_config = {
  67. .reg_bits = 8,
  68. .val_bits = 8,
  69. .volatile_reg = is_volatile_reg,
  70. .max_register = TPS65910_MAX_REGISTER - 1,
  71. .cache_type = REGCACHE_RBTREE,
  72. };
  73. static int __devinit tps65910_ck32k_init(struct tps65910 *tps65910,
  74. struct tps65910_board *pmic_pdata)
  75. {
  76. int ret;
  77. if (!pmic_pdata->en_ck32k_xtal)
  78. return 0;
  79. ret = tps65910_reg_clear_bits(tps65910, TPS65910_DEVCTRL,
  80. DEVCTRL_CK32K_CTRL_MASK);
  81. if (ret < 0) {
  82. dev_err(tps65910->dev, "clear ck32k_ctrl failed: %d\n", ret);
  83. return ret;
  84. }
  85. return 0;
  86. }
  87. static int __devinit tps65910_sleepinit(struct tps65910 *tps65910,
  88. struct tps65910_board *pmic_pdata)
  89. {
  90. struct device *dev = NULL;
  91. int ret = 0;
  92. dev = tps65910->dev;
  93. if (!pmic_pdata->en_dev_slp)
  94. return 0;
  95. /* enabling SLEEP device state */
  96. ret = tps65910_reg_set_bits(tps65910, TPS65910_DEVCTRL,
  97. DEVCTRL_DEV_SLP_MASK);
  98. if (ret < 0) {
  99. dev_err(dev, "set dev_slp failed: %d\n", ret);
  100. goto err_sleep_init;
  101. }
  102. /* Return if there is no sleep keepon data. */
  103. if (!pmic_pdata->slp_keepon)
  104. return 0;
  105. if (pmic_pdata->slp_keepon->therm_keepon) {
  106. ret = tps65910_reg_set_bits(tps65910,
  107. TPS65910_SLEEP_KEEP_RES_ON,
  108. SLEEP_KEEP_RES_ON_THERM_KEEPON_MASK);
  109. if (ret < 0) {
  110. dev_err(dev, "set therm_keepon failed: %d\n", ret);
  111. goto disable_dev_slp;
  112. }
  113. }
  114. if (pmic_pdata->slp_keepon->clkout32k_keepon) {
  115. ret = tps65910_reg_set_bits(tps65910,
  116. TPS65910_SLEEP_KEEP_RES_ON,
  117. SLEEP_KEEP_RES_ON_CLKOUT32K_KEEPON_MASK);
  118. if (ret < 0) {
  119. dev_err(dev, "set clkout32k_keepon failed: %d\n", ret);
  120. goto disable_dev_slp;
  121. }
  122. }
  123. if (pmic_pdata->slp_keepon->i2chs_keepon) {
  124. ret = tps65910_reg_set_bits(tps65910,
  125. TPS65910_SLEEP_KEEP_RES_ON,
  126. SLEEP_KEEP_RES_ON_I2CHS_KEEPON_MASK);
  127. if (ret < 0) {
  128. dev_err(dev, "set i2chs_keepon failed: %d\n", ret);
  129. goto disable_dev_slp;
  130. }
  131. }
  132. return 0;
  133. disable_dev_slp:
  134. tps65910_reg_clear_bits(tps65910, TPS65910_DEVCTRL,
  135. DEVCTRL_DEV_SLP_MASK);
  136. err_sleep_init:
  137. return ret;
  138. }
  139. #ifdef CONFIG_OF
  140. static struct of_device_id tps65910_of_match[] = {
  141. { .compatible = "ti,tps65910", .data = (void *)TPS65910},
  142. { .compatible = "ti,tps65911", .data = (void *)TPS65911},
  143. { },
  144. };
  145. MODULE_DEVICE_TABLE(of, tps65910_of_match);
  146. static struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
  147. int *chip_id)
  148. {
  149. struct device_node *np = client->dev.of_node;
  150. struct tps65910_board *board_info;
  151. unsigned int prop;
  152. const struct of_device_id *match;
  153. int ret = 0;
  154. match = of_match_device(tps65910_of_match, &client->dev);
  155. if (!match) {
  156. dev_err(&client->dev, "Failed to find matching dt id\n");
  157. return NULL;
  158. }
  159. *chip_id = (int)match->data;
  160. board_info = devm_kzalloc(&client->dev, sizeof(*board_info),
  161. GFP_KERNEL);
  162. if (!board_info) {
  163. dev_err(&client->dev, "Failed to allocate pdata\n");
  164. return NULL;
  165. }
  166. ret = of_property_read_u32(np, "ti,vmbch-threshold", &prop);
  167. if (!ret)
  168. board_info->vmbch_threshold = prop;
  169. else if (*chip_id == TPS65911)
  170. dev_warn(&client->dev, "VMBCH-Threshold not specified");
  171. ret = of_property_read_u32(np, "ti,vmbch2-threshold", &prop);
  172. if (!ret)
  173. board_info->vmbch2_threshold = prop;
  174. else if (*chip_id == TPS65911)
  175. dev_warn(&client->dev, "VMBCH2-Threshold not specified");
  176. prop = of_property_read_bool(np, "ti,en-ck32k-xtal");
  177. board_info->en_ck32k_xtal = prop;
  178. board_info->irq = client->irq;
  179. board_info->irq_base = -1;
  180. board_info->pm_off = of_property_read_bool(np,
  181. "ti,system-power-controller");
  182. return board_info;
  183. }
  184. #else
  185. static inline
  186. struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
  187. int *chip_id)
  188. {
  189. return NULL;
  190. }
  191. #endif
  192. static struct i2c_client *tps65910_i2c_client;
  193. static void tps65910_power_off(void)
  194. {
  195. struct tps65910 *tps65910;
  196. tps65910 = dev_get_drvdata(&tps65910_i2c_client->dev);
  197. if (tps65910_reg_set_bits(tps65910, TPS65910_DEVCTRL,
  198. DEVCTRL_PWR_OFF_MASK) < 0)
  199. return;
  200. tps65910_reg_clear_bits(tps65910, TPS65910_DEVCTRL,
  201. DEVCTRL_DEV_ON_MASK);
  202. }
  203. static __devinit int tps65910_i2c_probe(struct i2c_client *i2c,
  204. const struct i2c_device_id *id)
  205. {
  206. struct tps65910 *tps65910;
  207. struct tps65910_board *pmic_plat_data;
  208. struct tps65910_board *of_pmic_plat_data = NULL;
  209. struct tps65910_platform_data *init_data;
  210. int ret = 0;
  211. int chip_id = id->driver_data;
  212. pmic_plat_data = dev_get_platdata(&i2c->dev);
  213. if (!pmic_plat_data && i2c->dev.of_node) {
  214. pmic_plat_data = tps65910_parse_dt(i2c, &chip_id);
  215. of_pmic_plat_data = pmic_plat_data;
  216. }
  217. if (!pmic_plat_data)
  218. return -EINVAL;
  219. init_data = devm_kzalloc(&i2c->dev, sizeof(*init_data), GFP_KERNEL);
  220. if (init_data == NULL)
  221. return -ENOMEM;
  222. tps65910 = devm_kzalloc(&i2c->dev, sizeof(*tps65910), GFP_KERNEL);
  223. if (tps65910 == NULL)
  224. return -ENOMEM;
  225. tps65910->of_plat_data = of_pmic_plat_data;
  226. i2c_set_clientdata(i2c, tps65910);
  227. tps65910->dev = &i2c->dev;
  228. tps65910->i2c_client = i2c;
  229. tps65910->id = chip_id;
  230. mutex_init(&tps65910->io_mutex);
  231. tps65910->regmap = devm_regmap_init_i2c(i2c, &tps65910_regmap_config);
  232. if (IS_ERR(tps65910->regmap)) {
  233. ret = PTR_ERR(tps65910->regmap);
  234. dev_err(&i2c->dev, "regmap initialization failed: %d\n", ret);
  235. return ret;
  236. }
  237. ret = mfd_add_devices(tps65910->dev, -1,
  238. tps65910s, ARRAY_SIZE(tps65910s),
  239. NULL, 0, NULL);
  240. if (ret < 0) {
  241. dev_err(&i2c->dev, "mfd_add_devices failed: %d\n", ret);
  242. return ret;
  243. }
  244. init_data->irq = pmic_plat_data->irq;
  245. init_data->irq_base = pmic_plat_data->irq_base;
  246. tps65910_irq_init(tps65910, init_data->irq, init_data);
  247. tps65910_ck32k_init(tps65910, pmic_plat_data);
  248. tps65910_sleepinit(tps65910, pmic_plat_data);
  249. if (pmic_plat_data->pm_off && !pm_power_off) {
  250. tps65910_i2c_client = i2c;
  251. pm_power_off = tps65910_power_off;
  252. }
  253. return ret;
  254. }
  255. static __devexit int tps65910_i2c_remove(struct i2c_client *i2c)
  256. {
  257. struct tps65910 *tps65910 = i2c_get_clientdata(i2c);
  258. tps65910_irq_exit(tps65910);
  259. mfd_remove_devices(tps65910->dev);
  260. return 0;
  261. }
  262. static const struct i2c_device_id tps65910_i2c_id[] = {
  263. { "tps65910", TPS65910 },
  264. { "tps65911", TPS65911 },
  265. { }
  266. };
  267. MODULE_DEVICE_TABLE(i2c, tps65910_i2c_id);
  268. static struct i2c_driver tps65910_i2c_driver = {
  269. .driver = {
  270. .name = "tps65910",
  271. .owner = THIS_MODULE,
  272. .of_match_table = of_match_ptr(tps65910_of_match),
  273. },
  274. .probe = tps65910_i2c_probe,
  275. .remove = __devexit_p(tps65910_i2c_remove),
  276. .id_table = tps65910_i2c_id,
  277. };
  278. static int __init tps65910_i2c_init(void)
  279. {
  280. return i2c_add_driver(&tps65910_i2c_driver);
  281. }
  282. /* init early so consumer devices can complete system boot */
  283. subsys_initcall(tps65910_i2c_init);
  284. static void __exit tps65910_i2c_exit(void)
  285. {
  286. i2c_del_driver(&tps65910_i2c_driver);
  287. }
  288. module_exit(tps65910_i2c_exit);
  289. MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
  290. MODULE_AUTHOR("Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>");
  291. MODULE_DESCRIPTION("TPS6591x chip family multi-function driver");
  292. MODULE_LICENSE("GPL");