tps65910.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 tps65910_i2c_probe(struct i2c_client *i2c,
  83. const struct i2c_device_id *id)
  84. {
  85. struct tps65910 *tps65910;
  86. struct tps65910_board *pmic_plat_data;
  87. struct tps65910_platform_data *init_data;
  88. int ret = 0;
  89. pmic_plat_data = dev_get_platdata(&i2c->dev);
  90. if (!pmic_plat_data)
  91. return -EINVAL;
  92. init_data = kzalloc(sizeof(struct tps65910_platform_data), GFP_KERNEL);
  93. if (init_data == NULL)
  94. return -ENOMEM;
  95. tps65910 = kzalloc(sizeof(struct tps65910), GFP_KERNEL);
  96. if (tps65910 == NULL) {
  97. kfree(init_data);
  98. return -ENOMEM;
  99. }
  100. i2c_set_clientdata(i2c, tps65910);
  101. tps65910->dev = &i2c->dev;
  102. tps65910->i2c_client = i2c;
  103. tps65910->id = id->driver_data;
  104. tps65910->read = tps65910_i2c_read;
  105. tps65910->write = tps65910_i2c_write;
  106. mutex_init(&tps65910->io_mutex);
  107. tps65910->regmap = regmap_init_i2c(i2c, &tps65910_regmap_config);
  108. if (IS_ERR(tps65910->regmap)) {
  109. ret = PTR_ERR(tps65910->regmap);
  110. dev_err(&i2c->dev, "regmap initialization failed: %d\n", ret);
  111. goto regmap_err;
  112. }
  113. ret = mfd_add_devices(tps65910->dev, -1,
  114. tps65910s, ARRAY_SIZE(tps65910s),
  115. NULL, 0);
  116. if (ret < 0)
  117. goto err;
  118. init_data->irq = pmic_plat_data->irq;
  119. init_data->irq_base = pmic_plat_data->irq_base;
  120. tps65910_gpio_init(tps65910, pmic_plat_data->gpio_base);
  121. tps65910_irq_init(tps65910, init_data->irq, init_data);
  122. kfree(init_data);
  123. return ret;
  124. err:
  125. regmap_exit(tps65910->regmap);
  126. regmap_err:
  127. kfree(tps65910);
  128. kfree(init_data);
  129. return ret;
  130. }
  131. static int tps65910_i2c_remove(struct i2c_client *i2c)
  132. {
  133. struct tps65910 *tps65910 = i2c_get_clientdata(i2c);
  134. tps65910_irq_exit(tps65910);
  135. mfd_remove_devices(tps65910->dev);
  136. regmap_exit(tps65910->regmap);
  137. kfree(tps65910);
  138. return 0;
  139. }
  140. static const struct i2c_device_id tps65910_i2c_id[] = {
  141. { "tps65910", TPS65910 },
  142. { "tps65911", TPS65911 },
  143. { }
  144. };
  145. MODULE_DEVICE_TABLE(i2c, tps65910_i2c_id);
  146. static struct i2c_driver tps65910_i2c_driver = {
  147. .driver = {
  148. .name = "tps65910",
  149. .owner = THIS_MODULE,
  150. },
  151. .probe = tps65910_i2c_probe,
  152. .remove = tps65910_i2c_remove,
  153. .id_table = tps65910_i2c_id,
  154. };
  155. static int __init tps65910_i2c_init(void)
  156. {
  157. return i2c_add_driver(&tps65910_i2c_driver);
  158. }
  159. /* init early so consumer devices can complete system boot */
  160. subsys_initcall(tps65910_i2c_init);
  161. static void __exit tps65910_i2c_exit(void)
  162. {
  163. i2c_del_driver(&tps65910_i2c_driver);
  164. }
  165. module_exit(tps65910_i2c_exit);
  166. MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
  167. MODULE_AUTHOR("Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>");
  168. MODULE_DESCRIPTION("TPS6591x chip family multi-function driver");
  169. MODULE_LICENSE("GPL");