aat2870-regulator.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. .map_voltage = regulator_map_voltage_ascend,
  86. .set_voltage_sel = aat2870_ldo_set_voltage_sel,
  87. .get_voltage_sel = aat2870_ldo_get_voltage_sel,
  88. .enable = aat2870_ldo_enable,
  89. .disable = aat2870_ldo_disable,
  90. .is_enabled = aat2870_ldo_is_enabled,
  91. };
  92. static const unsigned int aat2870_ldo_voltages[] = {
  93. 1200000, 1300000, 1500000, 1600000,
  94. 1800000, 2000000, 2200000, 2500000,
  95. 2600000, 2700000, 2800000, 2900000,
  96. 3000000, 3100000, 3200000, 3300000,
  97. };
  98. #define AAT2870_LDO(ids) \
  99. { \
  100. .desc = { \
  101. .name = #ids, \
  102. .id = AAT2870_ID_##ids, \
  103. .n_voltages = ARRAY_SIZE(aat2870_ldo_voltages), \
  104. .volt_table = aat2870_ldo_voltages, \
  105. .ops = &aat2870_ldo_ops, \
  106. .type = REGULATOR_VOLTAGE, \
  107. .owner = THIS_MODULE, \
  108. }, \
  109. }
  110. static struct aat2870_regulator aat2870_regulators[] = {
  111. AAT2870_LDO(LDOA),
  112. AAT2870_LDO(LDOB),
  113. AAT2870_LDO(LDOC),
  114. AAT2870_LDO(LDOD),
  115. };
  116. static struct aat2870_regulator *aat2870_get_regulator(int id)
  117. {
  118. struct aat2870_regulator *ri = NULL;
  119. int i;
  120. for (i = 0; i < ARRAY_SIZE(aat2870_regulators); i++) {
  121. ri = &aat2870_regulators[i];
  122. if (ri->desc.id == id)
  123. break;
  124. }
  125. if (i == ARRAY_SIZE(aat2870_regulators))
  126. return NULL;
  127. ri->enable_addr = AAT2870_LDO_EN;
  128. ri->enable_shift = id - AAT2870_ID_LDOA;
  129. ri->enable_mask = 0x1 << ri->enable_shift;
  130. ri->voltage_addr = (id - AAT2870_ID_LDOA) / 2 ?
  131. AAT2870_LDO_CD : AAT2870_LDO_AB;
  132. ri->voltage_shift = (id - AAT2870_ID_LDOA) % 2 ? 0 : 4;
  133. ri->voltage_mask = 0xF << ri->voltage_shift;
  134. return ri;
  135. }
  136. static int aat2870_regulator_probe(struct platform_device *pdev)
  137. {
  138. struct aat2870_regulator *ri;
  139. struct regulator_config config = { };
  140. struct regulator_dev *rdev;
  141. ri = aat2870_get_regulator(pdev->id);
  142. if (!ri) {
  143. dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id);
  144. return -EINVAL;
  145. }
  146. ri->aat2870 = dev_get_drvdata(pdev->dev.parent);
  147. config.dev = &pdev->dev;
  148. config.driver_data = ri;
  149. config.init_data = dev_get_platdata(&pdev->dev);
  150. rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
  151. if (IS_ERR(rdev)) {
  152. dev_err(&pdev->dev, "Failed to register regulator %s\n",
  153. ri->desc.name);
  154. return PTR_ERR(rdev);
  155. }
  156. platform_set_drvdata(pdev, rdev);
  157. return 0;
  158. }
  159. static struct platform_driver aat2870_regulator_driver = {
  160. .driver = {
  161. .name = "aat2870-regulator",
  162. },
  163. .probe = aat2870_regulator_probe,
  164. };
  165. static int __init aat2870_regulator_init(void)
  166. {
  167. return platform_driver_register(&aat2870_regulator_driver);
  168. }
  169. subsys_initcall(aat2870_regulator_init);
  170. static void __exit aat2870_regulator_exit(void)
  171. {
  172. platform_driver_unregister(&aat2870_regulator_driver);
  173. }
  174. module_exit(aat2870_regulator_exit);
  175. MODULE_DESCRIPTION("AnalogicTech AAT2870 Regulator");
  176. MODULE_LICENSE("GPL");
  177. MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");
  178. MODULE_ALIAS("platform:aat2870-regulator");