tps65910.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. return board_info;
  172. }
  173. #else
  174. static inline
  175. struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
  176. int *chip_id)
  177. {
  178. return NULL;
  179. }
  180. #endif
  181. static __devinit int tps65910_i2c_probe(struct i2c_client *i2c,
  182. const struct i2c_device_id *id)
  183. {
  184. struct tps65910 *tps65910;
  185. struct tps65910_board *pmic_plat_data;
  186. struct tps65910_board *of_pmic_plat_data = NULL;
  187. struct tps65910_platform_data *init_data;
  188. int ret = 0;
  189. int chip_id = id->driver_data;
  190. pmic_plat_data = dev_get_platdata(&i2c->dev);
  191. if (!pmic_plat_data && i2c->dev.of_node) {
  192. pmic_plat_data = tps65910_parse_dt(i2c, &chip_id);
  193. of_pmic_plat_data = pmic_plat_data;
  194. }
  195. if (!pmic_plat_data)
  196. return -EINVAL;
  197. init_data = devm_kzalloc(&i2c->dev, sizeof(*init_data), GFP_KERNEL);
  198. if (init_data == NULL)
  199. return -ENOMEM;
  200. tps65910 = devm_kzalloc(&i2c->dev, sizeof(*tps65910), GFP_KERNEL);
  201. if (tps65910 == NULL)
  202. return -ENOMEM;
  203. tps65910->of_plat_data = of_pmic_plat_data;
  204. i2c_set_clientdata(i2c, tps65910);
  205. tps65910->dev = &i2c->dev;
  206. tps65910->i2c_client = i2c;
  207. tps65910->id = chip_id;
  208. mutex_init(&tps65910->io_mutex);
  209. tps65910->regmap = devm_regmap_init_i2c(i2c, &tps65910_regmap_config);
  210. if (IS_ERR(tps65910->regmap)) {
  211. ret = PTR_ERR(tps65910->regmap);
  212. dev_err(&i2c->dev, "regmap initialization failed: %d\n", ret);
  213. return ret;
  214. }
  215. ret = mfd_add_devices(tps65910->dev, -1,
  216. tps65910s, ARRAY_SIZE(tps65910s),
  217. NULL, 0);
  218. if (ret < 0) {
  219. dev_err(&i2c->dev, "mfd_add_devices failed: %d\n", ret);
  220. return ret;
  221. }
  222. init_data->irq = pmic_plat_data->irq;
  223. init_data->irq_base = pmic_plat_data->irq_base;
  224. tps65910_irq_init(tps65910, init_data->irq, init_data);
  225. tps65910_ck32k_init(tps65910, pmic_plat_data);
  226. tps65910_sleepinit(tps65910, pmic_plat_data);
  227. return ret;
  228. }
  229. static __devexit int tps65910_i2c_remove(struct i2c_client *i2c)
  230. {
  231. struct tps65910 *tps65910 = i2c_get_clientdata(i2c);
  232. tps65910_irq_exit(tps65910);
  233. mfd_remove_devices(tps65910->dev);
  234. return 0;
  235. }
  236. static const struct i2c_device_id tps65910_i2c_id[] = {
  237. { "tps65910", TPS65910 },
  238. { "tps65911", TPS65911 },
  239. { }
  240. };
  241. MODULE_DEVICE_TABLE(i2c, tps65910_i2c_id);
  242. static struct i2c_driver tps65910_i2c_driver = {
  243. .driver = {
  244. .name = "tps65910",
  245. .owner = THIS_MODULE,
  246. .of_match_table = of_match_ptr(tps65910_of_match),
  247. },
  248. .probe = tps65910_i2c_probe,
  249. .remove = __devexit_p(tps65910_i2c_remove),
  250. .id_table = tps65910_i2c_id,
  251. };
  252. static int __init tps65910_i2c_init(void)
  253. {
  254. return i2c_add_driver(&tps65910_i2c_driver);
  255. }
  256. /* init early so consumer devices can complete system boot */
  257. subsys_initcall(tps65910_i2c_init);
  258. static void __exit tps65910_i2c_exit(void)
  259. {
  260. i2c_del_driver(&tps65910_i2c_driver);
  261. }
  262. module_exit(tps65910_i2c_exit);
  263. MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
  264. MODULE_AUTHOR("Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>");
  265. MODULE_DESCRIPTION("TPS6591x chip family multi-function driver");
  266. MODULE_LICENSE("GPL");