aat2870-regulator.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * linux/drivers/regulator/aat2870-regulator.c
  3. *
  4. * Copyright (c) 2011, NVIDIA Corporation.
  5. * Author: Jin Park <jinyoungp@nvidia.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/err.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <linux/delay.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/regulator/driver.h>
  29. #include <linux/regulator/machine.h>
  30. #include <linux/mfd/aat2870.h>
  31. struct aat2870_regulator {
  32. struct aat2870_data *aat2870;
  33. struct regulator_desc desc;
  34. const int *voltages; /* uV */
  35. int min_uV;
  36. int max_uV;
  37. u8 enable_addr;
  38. u8 enable_shift;
  39. u8 enable_mask;
  40. u8 voltage_addr;
  41. u8 voltage_shift;
  42. u8 voltage_mask;
  43. };
  44. static int aat2870_ldo_list_voltage(struct regulator_dev *rdev,
  45. unsigned selector)
  46. {
  47. struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
  48. return ri->voltages[selector];
  49. }
  50. static int aat2870_ldo_set_voltage_sel(struct regulator_dev *rdev,
  51. unsigned selector)
  52. {
  53. struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
  54. struct aat2870_data *aat2870 = ri->aat2870;
  55. return aat2870->update(aat2870, ri->voltage_addr, ri->voltage_mask,
  56. selector << ri->voltage_shift);
  57. }
  58. static int aat2870_ldo_get_voltage_sel(struct regulator_dev *rdev)
  59. {
  60. struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
  61. struct aat2870_data *aat2870 = ri->aat2870;
  62. u8 val;
  63. int ret;
  64. ret = aat2870->read(aat2870, ri->voltage_addr, &val);
  65. if (ret)
  66. return ret;
  67. return (val & ri->voltage_mask) >> ri->voltage_shift;
  68. }
  69. static int aat2870_ldo_enable(struct regulator_dev *rdev)
  70. {
  71. struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
  72. struct aat2870_data *aat2870 = ri->aat2870;
  73. return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask,
  74. ri->enable_mask);
  75. }
  76. static int aat2870_ldo_disable(struct regulator_dev *rdev)
  77. {
  78. struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
  79. struct aat2870_data *aat2870 = ri->aat2870;
  80. return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask, 0);
  81. }
  82. static int aat2870_ldo_is_enabled(struct regulator_dev *rdev)
  83. {
  84. struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
  85. struct aat2870_data *aat2870 = ri->aat2870;
  86. u8 val;
  87. int ret;
  88. ret = aat2870->read(aat2870, ri->enable_addr, &val);
  89. if (ret)
  90. return ret;
  91. return val & ri->enable_mask ? 1 : 0;
  92. }
  93. static struct regulator_ops aat2870_ldo_ops = {
  94. .list_voltage = aat2870_ldo_list_voltage,
  95. .set_voltage_sel = aat2870_ldo_set_voltage_sel,
  96. .get_voltage_sel = aat2870_ldo_get_voltage_sel,
  97. .enable = aat2870_ldo_enable,
  98. .disable = aat2870_ldo_disable,
  99. .is_enabled = aat2870_ldo_is_enabled,
  100. };
  101. static const int aat2870_ldo_voltages[] = {
  102. 1200000, 1300000, 1500000, 1600000,
  103. 1800000, 2000000, 2200000, 2500000,
  104. 2600000, 2700000, 2800000, 2900000,
  105. 3000000, 3100000, 3200000, 3300000,
  106. };
  107. #define AAT2870_LDO(ids) \
  108. { \
  109. .desc = { \
  110. .name = #ids, \
  111. .id = AAT2870_ID_##ids, \
  112. .n_voltages = ARRAY_SIZE(aat2870_ldo_voltages), \
  113. .ops = &aat2870_ldo_ops, \
  114. .type = REGULATOR_VOLTAGE, \
  115. .owner = THIS_MODULE, \
  116. }, \
  117. .voltages = aat2870_ldo_voltages, \
  118. .min_uV = 1200000, \
  119. .max_uV = 3300000, \
  120. }
  121. static struct aat2870_regulator aat2870_regulators[] = {
  122. AAT2870_LDO(LDOA),
  123. AAT2870_LDO(LDOB),
  124. AAT2870_LDO(LDOC),
  125. AAT2870_LDO(LDOD),
  126. };
  127. static struct aat2870_regulator *aat2870_get_regulator(int id)
  128. {
  129. struct aat2870_regulator *ri = NULL;
  130. int i;
  131. for (i = 0; i < ARRAY_SIZE(aat2870_regulators); i++) {
  132. ri = &aat2870_regulators[i];
  133. if (ri->desc.id == id)
  134. break;
  135. }
  136. if (i == ARRAY_SIZE(aat2870_regulators))
  137. return NULL;
  138. ri->enable_addr = AAT2870_LDO_EN;
  139. ri->enable_shift = id - AAT2870_ID_LDOA;
  140. ri->enable_mask = 0x1 << ri->enable_shift;
  141. ri->voltage_addr = (id - AAT2870_ID_LDOA) / 2 ?
  142. AAT2870_LDO_CD : AAT2870_LDO_AB;
  143. ri->voltage_shift = (id - AAT2870_ID_LDOA) % 2 ? 0 : 4;
  144. ri->voltage_mask = 0xF << ri->voltage_shift;
  145. return ri;
  146. }
  147. static int aat2870_regulator_probe(struct platform_device *pdev)
  148. {
  149. struct aat2870_regulator *ri;
  150. struct regulator_dev *rdev;
  151. ri = aat2870_get_regulator(pdev->id);
  152. if (!ri) {
  153. dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id);
  154. return -EINVAL;
  155. }
  156. ri->aat2870 = dev_get_drvdata(pdev->dev.parent);
  157. rdev = regulator_register(&ri->desc, &pdev->dev,
  158. pdev->dev.platform_data, ri, NULL);
  159. if (IS_ERR(rdev)) {
  160. dev_err(&pdev->dev, "Failed to register regulator %s\n",
  161. ri->desc.name);
  162. return PTR_ERR(rdev);
  163. }
  164. platform_set_drvdata(pdev, rdev);
  165. return 0;
  166. }
  167. static int __devexit aat2870_regulator_remove(struct platform_device *pdev)
  168. {
  169. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  170. regulator_unregister(rdev);
  171. return 0;
  172. }
  173. static struct platform_driver aat2870_regulator_driver = {
  174. .driver = {
  175. .name = "aat2870-regulator",
  176. .owner = THIS_MODULE,
  177. },
  178. .probe = aat2870_regulator_probe,
  179. .remove = __devexit_p(aat2870_regulator_remove),
  180. };
  181. static int __init aat2870_regulator_init(void)
  182. {
  183. return platform_driver_register(&aat2870_regulator_driver);
  184. }
  185. subsys_initcall(aat2870_regulator_init);
  186. static void __exit aat2870_regulator_exit(void)
  187. {
  188. platform_driver_unregister(&aat2870_regulator_driver);
  189. }
  190. module_exit(aat2870_regulator_exit);
  191. MODULE_DESCRIPTION("AnalogicTech AAT2870 Regulator");
  192. MODULE_LICENSE("GPL");
  193. MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");