tps65910.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_sleepinit(struct tps65910 *tps65910,
  65. struct tps65910_board *pmic_pdata)
  66. {
  67. struct device *dev = NULL;
  68. int ret = 0;
  69. dev = tps65910->dev;
  70. if (!pmic_pdata->en_dev_slp)
  71. return 0;
  72. /* enabling SLEEP device state */
  73. ret = tps65910_reg_set_bits(tps65910, TPS65910_DEVCTRL,
  74. DEVCTRL_DEV_SLP_MASK);
  75. if (ret < 0) {
  76. dev_err(dev, "set dev_slp failed: %d\n", ret);
  77. goto err_sleep_init;
  78. }
  79. /* Return if there is no sleep keepon data. */
  80. if (!pmic_pdata->slp_keepon)
  81. return 0;
  82. if (pmic_pdata->slp_keepon->therm_keepon) {
  83. ret = tps65910_reg_set_bits(tps65910,
  84. TPS65910_SLEEP_KEEP_RES_ON,
  85. SLEEP_KEEP_RES_ON_THERM_KEEPON_MASK);
  86. if (ret < 0) {
  87. dev_err(dev, "set therm_keepon failed: %d\n", ret);
  88. goto disable_dev_slp;
  89. }
  90. }
  91. if (pmic_pdata->slp_keepon->clkout32k_keepon) {
  92. ret = tps65910_reg_set_bits(tps65910,
  93. TPS65910_SLEEP_KEEP_RES_ON,
  94. SLEEP_KEEP_RES_ON_CLKOUT32K_KEEPON_MASK);
  95. if (ret < 0) {
  96. dev_err(dev, "set clkout32k_keepon failed: %d\n", ret);
  97. goto disable_dev_slp;
  98. }
  99. }
  100. if (pmic_pdata->slp_keepon->i2chs_keepon) {
  101. ret = tps65910_reg_set_bits(tps65910,
  102. TPS65910_SLEEP_KEEP_RES_ON,
  103. SLEEP_KEEP_RES_ON_I2CHS_KEEPON_MASK);
  104. if (ret < 0) {
  105. dev_err(dev, "set i2chs_keepon failed: %d\n", ret);
  106. goto disable_dev_slp;
  107. }
  108. }
  109. return 0;
  110. disable_dev_slp:
  111. tps65910_reg_clear_bits(tps65910, TPS65910_DEVCTRL,
  112. DEVCTRL_DEV_SLP_MASK);
  113. err_sleep_init:
  114. return ret;
  115. }
  116. #ifdef CONFIG_OF
  117. static struct of_device_id tps65910_of_match[] = {
  118. { .compatible = "ti,tps65910", .data = (void *)TPS65910},
  119. { .compatible = "ti,tps65911", .data = (void *)TPS65911},
  120. { },
  121. };
  122. MODULE_DEVICE_TABLE(of, tps65910_of_match);
  123. static struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
  124. int *chip_id)
  125. {
  126. struct device_node *np = client->dev.of_node;
  127. struct tps65910_board *board_info;
  128. unsigned int prop;
  129. const struct of_device_id *match;
  130. unsigned int prop_array[TPS6591X_MAX_NUM_GPIO];
  131. int ret = 0;
  132. int idx;
  133. match = of_match_device(tps65910_of_match, &client->dev);
  134. if (!match) {
  135. dev_err(&client->dev, "Failed to find matching dt id\n");
  136. return NULL;
  137. }
  138. *chip_id = (int)match->data;
  139. board_info = devm_kzalloc(&client->dev, sizeof(*board_info),
  140. GFP_KERNEL);
  141. if (!board_info) {
  142. dev_err(&client->dev, "Failed to allocate pdata\n");
  143. return NULL;
  144. }
  145. ret = of_property_read_u32(np, "ti,vmbch-threshold", &prop);
  146. if (!ret)
  147. board_info->vmbch_threshold = prop;
  148. else if (*chip_id == TPS65911)
  149. dev_warn(&client->dev, "VMBCH-Threshold not specified");
  150. ret = of_property_read_u32(np, "ti,vmbch2-threshold", &prop);
  151. if (!ret)
  152. board_info->vmbch2_threshold = prop;
  153. else if (*chip_id == TPS65911)
  154. dev_warn(&client->dev, "VMBCH2-Threshold not specified");
  155. ret = of_property_read_u32_array(np, "ti,en-gpio-sleep",
  156. prop_array, TPS6591X_MAX_NUM_GPIO);
  157. if (!ret)
  158. for (idx = 0; idx < ARRAY_SIZE(prop_array); idx++)
  159. board_info->en_gpio_sleep[idx] = (prop_array[idx] != 0);
  160. else if (ret != -EINVAL) {
  161. dev_err(&client->dev,
  162. "error reading property ti,en-gpio-sleep: %d\n.", ret);
  163. return NULL;
  164. }
  165. board_info->irq = client->irq;
  166. board_info->irq_base = -1;
  167. board_info->gpio_base = -1;
  168. return board_info;
  169. }
  170. #else
  171. static inline
  172. struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
  173. int *chip_id)
  174. {
  175. return NULL;
  176. }
  177. #endif
  178. static __devinit int tps65910_i2c_probe(struct i2c_client *i2c,
  179. const struct i2c_device_id *id)
  180. {
  181. struct tps65910 *tps65910;
  182. struct tps65910_board *pmic_plat_data;
  183. struct tps65910_platform_data *init_data;
  184. int ret = 0;
  185. int chip_id = id->driver_data;
  186. pmic_plat_data = dev_get_platdata(&i2c->dev);
  187. if (!pmic_plat_data && i2c->dev.of_node)
  188. pmic_plat_data = tps65910_parse_dt(i2c, &chip_id);
  189. if (!pmic_plat_data)
  190. return -EINVAL;
  191. init_data = devm_kzalloc(&i2c->dev, sizeof(*init_data), GFP_KERNEL);
  192. if (init_data == NULL)
  193. return -ENOMEM;
  194. tps65910 = devm_kzalloc(&i2c->dev, sizeof(*tps65910), GFP_KERNEL);
  195. if (tps65910 == NULL)
  196. return -ENOMEM;
  197. i2c_set_clientdata(i2c, tps65910);
  198. tps65910->dev = &i2c->dev;
  199. tps65910->i2c_client = i2c;
  200. tps65910->id = chip_id;
  201. mutex_init(&tps65910->io_mutex);
  202. tps65910->regmap = devm_regmap_init_i2c(i2c, &tps65910_regmap_config);
  203. if (IS_ERR(tps65910->regmap)) {
  204. ret = PTR_ERR(tps65910->regmap);
  205. dev_err(&i2c->dev, "regmap initialization failed: %d\n", ret);
  206. return ret;
  207. }
  208. ret = mfd_add_devices(tps65910->dev, -1,
  209. tps65910s, ARRAY_SIZE(tps65910s),
  210. NULL, 0);
  211. if (ret < 0) {
  212. dev_err(&i2c->dev, "mfd_add_devices failed: %d\n", ret);
  213. return ret;
  214. }
  215. init_data->irq = pmic_plat_data->irq;
  216. init_data->irq_base = pmic_plat_data->irq_base;
  217. tps65910_irq_init(tps65910, init_data->irq, init_data);
  218. tps65910_sleepinit(tps65910, pmic_plat_data);
  219. return ret;
  220. }
  221. static __devexit int tps65910_i2c_remove(struct i2c_client *i2c)
  222. {
  223. struct tps65910 *tps65910 = i2c_get_clientdata(i2c);
  224. tps65910_irq_exit(tps65910);
  225. mfd_remove_devices(tps65910->dev);
  226. return 0;
  227. }
  228. static const struct i2c_device_id tps65910_i2c_id[] = {
  229. { "tps65910", TPS65910 },
  230. { "tps65911", TPS65911 },
  231. { }
  232. };
  233. MODULE_DEVICE_TABLE(i2c, tps65910_i2c_id);
  234. static struct i2c_driver tps65910_i2c_driver = {
  235. .driver = {
  236. .name = "tps65910",
  237. .owner = THIS_MODULE,
  238. .of_match_table = of_match_ptr(tps65910_of_match),
  239. },
  240. .probe = tps65910_i2c_probe,
  241. .remove = __devexit_p(tps65910_i2c_remove),
  242. .id_table = tps65910_i2c_id,
  243. };
  244. static int __init tps65910_i2c_init(void)
  245. {
  246. return i2c_add_driver(&tps65910_i2c_driver);
  247. }
  248. /* init early so consumer devices can complete system boot */
  249. subsys_initcall(tps65910_i2c_init);
  250. static void __exit tps65910_i2c_exit(void)
  251. {
  252. i2c_del_driver(&tps65910_i2c_driver);
  253. }
  254. module_exit(tps65910_i2c_exit);
  255. MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
  256. MODULE_AUTHOR("Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>");
  257. MODULE_DESCRIPTION("TPS6591x chip family multi-function driver");
  258. MODULE_LICENSE("GPL");