aat2870-regulator.c 5.7 KB

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