aat2870-regulator.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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/slab.h>
  25. #include <linux/delay.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/regulator/driver.h>
  28. #include <linux/regulator/machine.h>
  29. #include <linux/mfd/aat2870.h>
  30. struct aat2870_regulator {
  31. struct platform_device *pdev;
  32. struct regulator_desc desc;
  33. const int *voltages; /* uV */
  34. int min_uV;
  35. int max_uV;
  36. u8 enable_addr;
  37. u8 enable_shift;
  38. u8 enable_mask;
  39. u8 voltage_addr;
  40. u8 voltage_shift;
  41. u8 voltage_mask;
  42. };
  43. static int aat2870_ldo_list_voltage(struct regulator_dev *rdev,
  44. unsigned selector)
  45. {
  46. struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
  47. return ri->voltages[selector];
  48. }
  49. static int aat2870_ldo_set_voltage_sel(struct regulator_dev *rdev,
  50. unsigned selector)
  51. {
  52. struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
  53. struct aat2870_data *aat2870 = dev_get_drvdata(ri->pdev->dev.parent);
  54. return aat2870->update(aat2870, ri->voltage_addr, ri->voltage_mask,
  55. (selector << ri->voltage_shift) & ri->voltage_mask);
  56. }
  57. static int aat2870_ldo_get_voltage_sel(struct regulator_dev *rdev)
  58. {
  59. struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
  60. struct aat2870_data *aat2870 = dev_get_drvdata(ri->pdev->dev.parent);
  61. u8 val;
  62. int ret;
  63. ret = aat2870->read(aat2870, ri->voltage_addr, &val);
  64. if (ret)
  65. return ret;
  66. return (val & ri->voltage_mask) >> ri->voltage_shift;
  67. }
  68. static int aat2870_ldo_enable(struct regulator_dev *rdev)
  69. {
  70. struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
  71. struct aat2870_data *aat2870 = dev_get_drvdata(ri->pdev->dev.parent);
  72. return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask,
  73. ri->enable_mask);
  74. }
  75. static int aat2870_ldo_disable(struct regulator_dev *rdev)
  76. {
  77. struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
  78. struct aat2870_data *aat2870 = dev_get_drvdata(ri->pdev->dev.parent);
  79. return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask, 0);
  80. }
  81. static int aat2870_ldo_is_enabled(struct regulator_dev *rdev)
  82. {
  83. struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
  84. struct aat2870_data *aat2870 = dev_get_drvdata(ri->pdev->dev.parent);
  85. u8 val;
  86. int ret;
  87. ret = aat2870->read(aat2870, ri->enable_addr, &val);
  88. if (ret)
  89. return ret;
  90. return val & ri->enable_mask ? 1 : 0;
  91. }
  92. static struct regulator_ops aat2870_ldo_ops = {
  93. .list_voltage = aat2870_ldo_list_voltage,
  94. .set_voltage_sel = aat2870_ldo_set_voltage_sel,
  95. .get_voltage_sel = aat2870_ldo_get_voltage_sel,
  96. .enable = aat2870_ldo_enable,
  97. .disable = aat2870_ldo_disable,
  98. .is_enabled = aat2870_ldo_is_enabled,
  99. };
  100. static const int aat2870_ldo_voltages[] = {
  101. 1200000, 1300000, 1500000, 1600000,
  102. 1800000, 2000000, 2200000, 2500000,
  103. 2600000, 2700000, 2800000, 2900000,
  104. 3000000, 3100000, 3200000, 3300000,
  105. };
  106. #define AAT2870_LDO(ids) \
  107. { \
  108. .desc = { \
  109. .name = #ids, \
  110. .id = AAT2870_ID_##ids, \
  111. .n_voltages = ARRAY_SIZE(aat2870_ldo_voltages), \
  112. .ops = &aat2870_ldo_ops, \
  113. .type = REGULATOR_VOLTAGE, \
  114. .owner = THIS_MODULE, \
  115. }, \
  116. .voltages = aat2870_ldo_voltages, \
  117. .min_uV = 1200000, \
  118. .max_uV = 3300000, \
  119. }
  120. static struct aat2870_regulator aat2870_regulators[] = {
  121. AAT2870_LDO(LDOA),
  122. AAT2870_LDO(LDOB),
  123. AAT2870_LDO(LDOC),
  124. AAT2870_LDO(LDOD),
  125. };
  126. static struct aat2870_regulator *aat2870_get_regulator(int id)
  127. {
  128. struct aat2870_regulator *ri = NULL;
  129. int i;
  130. for (i = 0; i < ARRAY_SIZE(aat2870_regulators); i++) {
  131. ri = &aat2870_regulators[i];
  132. if (ri->desc.id == id)
  133. break;
  134. }
  135. if (!ri)
  136. return NULL;
  137. ri->enable_addr = AAT2870_LDO_EN;
  138. ri->enable_shift = id - AAT2870_ID_LDOA;
  139. ri->enable_mask = 0x1 << ri->enable_shift;
  140. ri->voltage_addr = (id - AAT2870_ID_LDOA) / 2 ?
  141. AAT2870_LDO_CD : AAT2870_LDO_AB;
  142. ri->voltage_shift = (id - AAT2870_ID_LDOA) % 2 ? 0 : 4;
  143. ri->voltage_mask = 0xF << ri->voltage_shift;
  144. return ri;
  145. }
  146. static int aat2870_regulator_probe(struct platform_device *pdev)
  147. {
  148. struct aat2870_regulator *ri;
  149. struct regulator_dev *rdev;
  150. ri = aat2870_get_regulator(pdev->id);
  151. if (!ri) {
  152. dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id);
  153. return -EINVAL;
  154. }
  155. ri->pdev = pdev;
  156. rdev = regulator_register(&ri->desc, &pdev->dev,
  157. pdev->dev.platform_data, ri);
  158. if (IS_ERR(rdev)) {
  159. dev_err(&pdev->dev, "Failed to register regulator %s\n",
  160. ri->desc.name);
  161. return PTR_ERR(rdev);
  162. }
  163. platform_set_drvdata(pdev, rdev);
  164. return 0;
  165. }
  166. static int __devexit aat2870_regulator_remove(struct platform_device *pdev)
  167. {
  168. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  169. regulator_unregister(rdev);
  170. return 0;
  171. }
  172. static struct platform_driver aat2870_regulator_driver = {
  173. .driver = {
  174. .name = "aat2870-regulator",
  175. .owner = THIS_MODULE,
  176. },
  177. .probe = aat2870_regulator_probe,
  178. .remove = __devexit_p(aat2870_regulator_remove),
  179. };
  180. static int __init aat2870_regulator_init(void)
  181. {
  182. return platform_driver_register(&aat2870_regulator_driver);
  183. }
  184. subsys_initcall(aat2870_regulator_init);
  185. static void __exit aat2870_regulator_exit(void)
  186. {
  187. platform_driver_unregister(&aat2870_regulator_driver);
  188. }
  189. module_exit(aat2870_regulator_exit);
  190. MODULE_DESCRIPTION("AnalogicTech AAT2870 Regulator");
  191. MODULE_LICENSE("GPL");
  192. MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");