tps65910.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. static struct mfd_cell tps65910s[] = {
  26. {
  27. .name = "tps65910-pmic",
  28. },
  29. {
  30. .name = "tps65910-rtc",
  31. },
  32. {
  33. .name = "tps65910-power",
  34. },
  35. };
  36. static int tps65910_i2c_read(struct tps65910 *tps65910, u8 reg,
  37. int bytes, void *dest)
  38. {
  39. return regmap_bulk_read(tps65910->regmap, reg, dest, bytes);
  40. }
  41. static int tps65910_i2c_write(struct tps65910 *tps65910, u8 reg,
  42. int bytes, void *src)
  43. {
  44. return regmap_bulk_write(tps65910->regmap, reg, src, bytes);
  45. }
  46. int tps65910_set_bits(struct tps65910 *tps65910, u8 reg, u8 mask)
  47. {
  48. return regmap_update_bits(tps65910->regmap, reg, mask, mask);
  49. }
  50. EXPORT_SYMBOL_GPL(tps65910_set_bits);
  51. int tps65910_clear_bits(struct tps65910 *tps65910, u8 reg, u8 mask)
  52. {
  53. return regmap_update_bits(tps65910->regmap, reg, mask, 0);
  54. }
  55. EXPORT_SYMBOL_GPL(tps65910_clear_bits);
  56. static bool is_volatile_reg(struct device *dev, unsigned int reg)
  57. {
  58. struct tps65910 *tps65910 = dev_get_drvdata(dev);
  59. /*
  60. * Caching all regulator registers.
  61. * All regualator register address range is same for
  62. * TPS65910 and TPS65911
  63. */
  64. if ((reg >= TPS65910_VIO) && (reg <= TPS65910_VDAC)) {
  65. /* Check for non-existing register */
  66. if (tps65910_chip_id(tps65910) == TPS65910)
  67. if ((reg == TPS65911_VDDCTRL_OP) ||
  68. (reg == TPS65911_VDDCTRL_SR))
  69. return true;
  70. return false;
  71. }
  72. return true;
  73. }
  74. static const struct regmap_config tps65910_regmap_config = {
  75. .reg_bits = 8,
  76. .val_bits = 8,
  77. .volatile_reg = is_volatile_reg,
  78. .max_register = TPS65910_MAX_REGISTER,
  79. .num_reg_defaults_raw = TPS65910_MAX_REGISTER,
  80. .cache_type = REGCACHE_RBTREE,
  81. };
  82. static int __init tps65910_sleepinit(struct tps65910 *tps65910,
  83. struct tps65910_board *pmic_pdata)
  84. {
  85. struct device *dev = NULL;
  86. int ret = 0;
  87. dev = tps65910->dev;
  88. if (!pmic_pdata->en_dev_slp)
  89. return 0;
  90. /* enabling SLEEP device state */
  91. ret = tps65910_set_bits(tps65910, TPS65910_DEVCTRL,
  92. DEVCTRL_DEV_SLP_MASK);
  93. if (ret < 0) {
  94. dev_err(dev, "set dev_slp failed: %d\n", ret);
  95. goto err_sleep_init;
  96. }
  97. /* Return if there is no sleep keepon data. */
  98. if (!pmic_pdata->slp_keepon)
  99. return 0;
  100. if (pmic_pdata->slp_keepon->therm_keepon) {
  101. ret = tps65910_set_bits(tps65910, TPS65910_SLEEP_KEEP_RES_ON,
  102. SLEEP_KEEP_RES_ON_THERM_KEEPON_MASK);
  103. if (ret < 0) {
  104. dev_err(dev, "set therm_keepon failed: %d\n", ret);
  105. goto disable_dev_slp;
  106. }
  107. }
  108. if (pmic_pdata->slp_keepon->clkout32k_keepon) {
  109. ret = tps65910_set_bits(tps65910, TPS65910_SLEEP_KEEP_RES_ON,
  110. SLEEP_KEEP_RES_ON_CLKOUT32K_KEEPON_MASK);
  111. if (ret < 0) {
  112. dev_err(dev, "set clkout32k_keepon failed: %d\n", ret);
  113. goto disable_dev_slp;
  114. }
  115. }
  116. if (pmic_pdata->slp_keepon->i2chs_keepon) {
  117. ret = tps65910_set_bits(tps65910, TPS65910_SLEEP_KEEP_RES_ON,
  118. SLEEP_KEEP_RES_ON_I2CHS_KEEPON_MASK);
  119. if (ret < 0) {
  120. dev_err(dev, "set i2chs_keepon failed: %d\n", ret);
  121. goto disable_dev_slp;
  122. }
  123. }
  124. return 0;
  125. disable_dev_slp:
  126. tps65910_clear_bits(tps65910, TPS65910_DEVCTRL, DEVCTRL_DEV_SLP_MASK);
  127. err_sleep_init:
  128. return ret;
  129. }
  130. static int tps65910_i2c_probe(struct i2c_client *i2c,
  131. const struct i2c_device_id *id)
  132. {
  133. struct tps65910 *tps65910;
  134. struct tps65910_board *pmic_plat_data;
  135. struct tps65910_platform_data *init_data;
  136. int ret = 0;
  137. pmic_plat_data = dev_get_platdata(&i2c->dev);
  138. if (!pmic_plat_data)
  139. return -EINVAL;
  140. init_data = kzalloc(sizeof(struct tps65910_platform_data), GFP_KERNEL);
  141. if (init_data == NULL)
  142. return -ENOMEM;
  143. tps65910 = kzalloc(sizeof(struct tps65910), GFP_KERNEL);
  144. if (tps65910 == NULL) {
  145. kfree(init_data);
  146. return -ENOMEM;
  147. }
  148. i2c_set_clientdata(i2c, tps65910);
  149. tps65910->dev = &i2c->dev;
  150. tps65910->i2c_client = i2c;
  151. tps65910->id = id->driver_data;
  152. tps65910->read = tps65910_i2c_read;
  153. tps65910->write = tps65910_i2c_write;
  154. mutex_init(&tps65910->io_mutex);
  155. tps65910->regmap = regmap_init_i2c(i2c, &tps65910_regmap_config);
  156. if (IS_ERR(tps65910->regmap)) {
  157. ret = PTR_ERR(tps65910->regmap);
  158. dev_err(&i2c->dev, "regmap initialization failed: %d\n", ret);
  159. goto regmap_err;
  160. }
  161. ret = mfd_add_devices(tps65910->dev, -1,
  162. tps65910s, ARRAY_SIZE(tps65910s),
  163. NULL, 0);
  164. if (ret < 0)
  165. goto err;
  166. init_data->irq = pmic_plat_data->irq;
  167. init_data->irq_base = pmic_plat_data->irq_base;
  168. tps65910_gpio_init(tps65910, pmic_plat_data->gpio_base);
  169. tps65910_irq_init(tps65910, init_data->irq, init_data);
  170. tps65910_sleepinit(tps65910, pmic_plat_data);
  171. kfree(init_data);
  172. return ret;
  173. err:
  174. regmap_exit(tps65910->regmap);
  175. regmap_err:
  176. kfree(tps65910);
  177. kfree(init_data);
  178. return ret;
  179. }
  180. static int tps65910_i2c_remove(struct i2c_client *i2c)
  181. {
  182. struct tps65910 *tps65910 = i2c_get_clientdata(i2c);
  183. tps65910_irq_exit(tps65910);
  184. mfd_remove_devices(tps65910->dev);
  185. regmap_exit(tps65910->regmap);
  186. kfree(tps65910);
  187. return 0;
  188. }
  189. static const struct i2c_device_id tps65910_i2c_id[] = {
  190. { "tps65910", TPS65910 },
  191. { "tps65911", TPS65911 },
  192. { }
  193. };
  194. MODULE_DEVICE_TABLE(i2c, tps65910_i2c_id);
  195. static struct i2c_driver tps65910_i2c_driver = {
  196. .driver = {
  197. .name = "tps65910",
  198. .owner = THIS_MODULE,
  199. },
  200. .probe = tps65910_i2c_probe,
  201. .remove = tps65910_i2c_remove,
  202. .id_table = tps65910_i2c_id,
  203. };
  204. static int __init tps65910_i2c_init(void)
  205. {
  206. return i2c_add_driver(&tps65910_i2c_driver);
  207. }
  208. /* init early so consumer devices can complete system boot */
  209. subsys_initcall(tps65910_i2c_init);
  210. static void __exit tps65910_i2c_exit(void)
  211. {
  212. i2c_del_driver(&tps65910_i2c_driver);
  213. }
  214. module_exit(tps65910_i2c_exit);
  215. MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
  216. MODULE_AUTHOR("Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>");
  217. MODULE_DESCRIPTION("TPS6591x chip family multi-function driver");
  218. MODULE_LICENSE("GPL");