tps65910.c 7.6 KB

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