tps65910.c 8.2 KB

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