tps6105x-regulator.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Driver for TPS61050/61052 boost converters, typically used for white LEDs
  3. * or audio amplifiers.
  4. *
  5. * Copyright (C) 2011 ST-Ericsson SA
  6. * Written on behalf of Linaro for ST-Ericsson
  7. *
  8. * Author: Linus Walleij <linus.walleij@linaro.org>
  9. *
  10. * License terms: GNU General Public License (GPL) version 2
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/err.h>
  16. #include <linux/i2c.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regulator/driver.h>
  19. #include <linux/mfd/core.h>
  20. #include <linux/mfd/tps6105x.h>
  21. static const int tps6105x_voltages[] = {
  22. 4500000,
  23. 5000000,
  24. 5250000,
  25. 5000000, /* There is an additional 5V */
  26. };
  27. static int tps6105x_regulator_enable(struct regulator_dev *rdev)
  28. {
  29. struct tps6105x *tps6105x = rdev_get_drvdata(rdev);
  30. int ret;
  31. /* Activate voltage mode */
  32. ret = tps6105x_mask_and_set(tps6105x, TPS6105X_REG_0,
  33. TPS6105X_REG0_MODE_MASK,
  34. TPS6105X_REG0_MODE_VOLTAGE << TPS6105X_REG0_MODE_SHIFT);
  35. if (ret)
  36. return ret;
  37. return 0;
  38. }
  39. static int tps6105x_regulator_disable(struct regulator_dev *rdev)
  40. {
  41. struct tps6105x *tps6105x = rdev_get_drvdata(rdev);
  42. int ret;
  43. /* Set into shutdown mode */
  44. ret = tps6105x_mask_and_set(tps6105x, TPS6105X_REG_0,
  45. TPS6105X_REG0_MODE_MASK,
  46. TPS6105X_REG0_MODE_SHUTDOWN << TPS6105X_REG0_MODE_SHIFT);
  47. if (ret)
  48. return ret;
  49. return 0;
  50. }
  51. static int tps6105x_regulator_is_enabled(struct regulator_dev *rdev)
  52. {
  53. struct tps6105x *tps6105x = rdev_get_drvdata(rdev);
  54. u8 regval;
  55. int ret;
  56. ret = tps6105x_get(tps6105x, TPS6105X_REG_0, &regval);
  57. if (ret)
  58. return ret;
  59. regval &= TPS6105X_REG0_MODE_MASK;
  60. regval >>= TPS6105X_REG0_MODE_SHIFT;
  61. if (regval == TPS6105X_REG0_MODE_VOLTAGE)
  62. return 1;
  63. return 0;
  64. }
  65. static int tps6105x_regulator_get_voltage_sel(struct regulator_dev *rdev)
  66. {
  67. struct tps6105x *tps6105x = rdev_get_drvdata(rdev);
  68. u8 regval;
  69. int ret;
  70. ret = tps6105x_get(tps6105x, TPS6105X_REG_0, &regval);
  71. if (ret)
  72. return ret;
  73. regval &= TPS6105X_REG0_VOLTAGE_MASK;
  74. regval >>= TPS6105X_REG0_VOLTAGE_SHIFT;
  75. return (int) regval;
  76. }
  77. static int tps6105x_regulator_set_voltage_sel(struct regulator_dev *rdev,
  78. unsigned selector)
  79. {
  80. struct tps6105x *tps6105x = rdev_get_drvdata(rdev);
  81. int ret;
  82. ret = tps6105x_mask_and_set(tps6105x, TPS6105X_REG_0,
  83. TPS6105X_REG0_VOLTAGE_MASK,
  84. selector << TPS6105X_REG0_VOLTAGE_SHIFT);
  85. if (ret)
  86. return ret;
  87. return 0;
  88. }
  89. static int tps6105x_regulator_list_voltage(struct regulator_dev *rdev,
  90. unsigned selector)
  91. {
  92. if (selector >= ARRAY_SIZE(tps6105x_voltages))
  93. return -EINVAL;
  94. return tps6105x_voltages[selector];
  95. }
  96. static struct regulator_ops tps6105x_regulator_ops = {
  97. .enable = tps6105x_regulator_enable,
  98. .disable = tps6105x_regulator_disable,
  99. .is_enabled = tps6105x_regulator_is_enabled,
  100. .get_voltage_sel = tps6105x_regulator_get_voltage_sel,
  101. .set_voltage_sel = tps6105x_regulator_set_voltage_sel,
  102. .list_voltage = tps6105x_regulator_list_voltage,
  103. };
  104. static struct regulator_desc tps6105x_regulator_desc = {
  105. .name = "tps6105x-boost",
  106. .ops = &tps6105x_regulator_ops,
  107. .type = REGULATOR_VOLTAGE,
  108. .id = 0,
  109. .owner = THIS_MODULE,
  110. .n_voltages = ARRAY_SIZE(tps6105x_voltages),
  111. };
  112. /*
  113. * Registers the chip as a voltage regulator
  114. */
  115. static int __devinit tps6105x_regulator_probe(struct platform_device *pdev)
  116. {
  117. struct tps6105x *tps6105x = dev_get_platdata(&pdev->dev);
  118. struct tps6105x_platform_data *pdata = tps6105x->pdata;
  119. int ret;
  120. /* This instance is not set for regulator mode so bail out */
  121. if (pdata->mode != TPS6105X_MODE_VOLTAGE) {
  122. dev_info(&pdev->dev,
  123. "chip not in voltage mode mode, exit probe \n");
  124. return 0;
  125. }
  126. /* Register regulator with framework */
  127. tps6105x->regulator = regulator_register(&tps6105x_regulator_desc,
  128. &tps6105x->client->dev,
  129. pdata->regulator_data, tps6105x,
  130. NULL);
  131. if (IS_ERR(tps6105x->regulator)) {
  132. ret = PTR_ERR(tps6105x->regulator);
  133. dev_err(&tps6105x->client->dev,
  134. "failed to register regulator\n");
  135. return ret;
  136. }
  137. platform_set_drvdata(pdev, tps6105x);
  138. return 0;
  139. }
  140. static int __devexit tps6105x_regulator_remove(struct platform_device *pdev)
  141. {
  142. struct tps6105x *tps6105x = dev_get_platdata(&pdev->dev);
  143. regulator_unregister(tps6105x->regulator);
  144. return 0;
  145. }
  146. static struct platform_driver tps6105x_regulator_driver = {
  147. .driver = {
  148. .name = "tps6105x-regulator",
  149. .owner = THIS_MODULE,
  150. },
  151. .probe = tps6105x_regulator_probe,
  152. .remove = __devexit_p(tps6105x_regulator_remove),
  153. };
  154. static __init int tps6105x_regulator_init(void)
  155. {
  156. return platform_driver_register(&tps6105x_regulator_driver);
  157. }
  158. subsys_initcall(tps6105x_regulator_init);
  159. static __exit void tps6105x_regulator_exit(void)
  160. {
  161. platform_driver_unregister(&tps6105x_regulator_driver);
  162. }
  163. module_exit(tps6105x_regulator_exit);
  164. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
  165. MODULE_DESCRIPTION("TPS6105x regulator driver");
  166. MODULE_LICENSE("GPL v2");
  167. MODULE_ALIAS("platform:tps6105x-regulator");