rc5t583-regulator.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Regulator driver for RICOH RC5T583 power management chip.
  3. *
  4. * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved.
  5. * Author: Laxman dewangan <ldewangan@nvidia.com>
  6. *
  7. * based on code
  8. * Copyright (C) 2011 RICOH COMPANY,LTD
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms and conditions of the GNU General Public License,
  13. * version 2, as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/err.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/regulator/driver.h>
  30. #include <linux/regulator/machine.h>
  31. #include <linux/gpio.h>
  32. #include <linux/mfd/rc5t583.h>
  33. struct rc5t583_regulator_info {
  34. int deepsleep_id;
  35. /* Regulator register address.*/
  36. uint8_t reg_disc_reg;
  37. uint8_t disc_bit;
  38. uint8_t deepsleep_reg;
  39. /* Regulator specific turn-on delay and voltage settling time*/
  40. int enable_uv_per_us;
  41. /* Used by regulator core */
  42. struct regulator_desc desc;
  43. };
  44. struct rc5t583_regulator {
  45. struct rc5t583_regulator_info *reg_info;
  46. struct regulator_dev *rdev;
  47. };
  48. static int rc5t583_regulator_enable_time(struct regulator_dev *rdev)
  49. {
  50. struct rc5t583_regulator *reg = rdev_get_drvdata(rdev);
  51. int vsel = regulator_get_voltage_sel_regmap(rdev);
  52. int curr_uV = regulator_list_voltage_linear(rdev, vsel);
  53. return DIV_ROUND_UP(curr_uV, reg->reg_info->enable_uv_per_us);
  54. }
  55. static const struct regulator_ops rc5t583_ops = {
  56. .is_enabled = regulator_is_enabled_regmap,
  57. .enable = regulator_enable_regmap,
  58. .disable = regulator_disable_regmap,
  59. .enable_time = rc5t583_regulator_enable_time,
  60. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  61. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  62. .list_voltage = regulator_list_voltage_linear,
  63. .map_voltage = regulator_map_voltage_linear,
  64. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  65. };
  66. #define RC5T583_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, \
  67. _vout_mask, _min_mv, _max_mv, _step_uV, _enable_mv) \
  68. { \
  69. .reg_disc_reg = RC5T583_REG_##_disc_reg, \
  70. .disc_bit = _disc_bit, \
  71. .deepsleep_reg = RC5T583_REG_##_id##DAC_DS, \
  72. .enable_uv_per_us = _enable_mv * 1000, \
  73. .deepsleep_id = RC5T583_DS_##_id, \
  74. .desc = { \
  75. .name = "rc5t583-regulator-"#_id, \
  76. .id = RC5T583_REGULATOR_##_id, \
  77. .n_voltages = (_max_mv - _min_mv) * 1000 / _step_uV + 1, \
  78. .ops = &rc5t583_ops, \
  79. .type = REGULATOR_VOLTAGE, \
  80. .owner = THIS_MODULE, \
  81. .vsel_reg = RC5T583_REG_##_id##DAC, \
  82. .vsel_mask = _vout_mask, \
  83. .enable_reg = RC5T583_REG_##_en_reg, \
  84. .enable_mask = BIT(_en_bit), \
  85. .min_uV = _min_mv * 1000, \
  86. .uV_step = _step_uV, \
  87. .ramp_delay = 40 * 1000, \
  88. }, \
  89. }
  90. static struct rc5t583_regulator_info rc5t583_reg_info[RC5T583_REGULATOR_MAX] = {
  91. RC5T583_REG(DC0, DC0CTL, 0, DC0CTL, 1, 0x7F, 700, 1500, 12500, 4),
  92. RC5T583_REG(DC1, DC1CTL, 0, DC1CTL, 1, 0x7F, 700, 1500, 12500, 14),
  93. RC5T583_REG(DC2, DC2CTL, 0, DC2CTL, 1, 0x7F, 900, 2400, 12500, 14),
  94. RC5T583_REG(DC3, DC3CTL, 0, DC3CTL, 1, 0x7F, 900, 2400, 12500, 14),
  95. RC5T583_REG(LDO0, LDOEN2, 0, LDODIS2, 0, 0x7F, 900, 3400, 25000, 160),
  96. RC5T583_REG(LDO1, LDOEN2, 1, LDODIS2, 1, 0x7F, 900, 3400, 25000, 160),
  97. RC5T583_REG(LDO2, LDOEN2, 2, LDODIS2, 2, 0x7F, 900, 3400, 25000, 160),
  98. RC5T583_REG(LDO3, LDOEN2, 3, LDODIS2, 3, 0x7F, 900, 3400, 25000, 160),
  99. RC5T583_REG(LDO4, LDOEN2, 4, LDODIS2, 4, 0x3F, 750, 1500, 12500, 133),
  100. RC5T583_REG(LDO5, LDOEN2, 5, LDODIS2, 5, 0x7F, 900, 3400, 25000, 267),
  101. RC5T583_REG(LDO6, LDOEN2, 6, LDODIS2, 6, 0x7F, 900, 3400, 25000, 133),
  102. RC5T583_REG(LDO7, LDOEN2, 7, LDODIS2, 7, 0x7F, 900, 3400, 25000, 233),
  103. RC5T583_REG(LDO8, LDOEN1, 0, LDODIS1, 0, 0x7F, 900, 3400, 25000, 233),
  104. RC5T583_REG(LDO9, LDOEN1, 1, LDODIS1, 1, 0x7F, 900, 3400, 25000, 133),
  105. };
  106. static int rc5t583_regulator_probe(struct platform_device *pdev)
  107. {
  108. struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
  109. struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev);
  110. struct regulator_config config = { };
  111. struct rc5t583_regulator *reg = NULL;
  112. struct rc5t583_regulator *regs;
  113. struct regulator_dev *rdev;
  114. struct rc5t583_regulator_info *ri;
  115. int ret;
  116. int id;
  117. if (!pdata) {
  118. dev_err(&pdev->dev, "No platform data, exiting...\n");
  119. return -ENODEV;
  120. }
  121. regs = devm_kzalloc(&pdev->dev, RC5T583_REGULATOR_MAX *
  122. sizeof(struct rc5t583_regulator), GFP_KERNEL);
  123. if (!regs)
  124. return -ENOMEM;
  125. for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) {
  126. reg = &regs[id];
  127. ri = &rc5t583_reg_info[id];
  128. reg->reg_info = ri;
  129. if (ri->deepsleep_id == RC5T583_DS_NONE)
  130. goto skip_ext_pwr_config;
  131. ret = rc5t583_ext_power_req_config(rc5t583->dev,
  132. ri->deepsleep_id,
  133. pdata->regulator_ext_pwr_control[id],
  134. pdata->regulator_deepsleep_slot[id]);
  135. /*
  136. * Configuring external control is not a major issue,
  137. * just give warning.
  138. */
  139. if (ret < 0)
  140. dev_warn(&pdev->dev,
  141. "Failed to configure ext control %d\n", id);
  142. skip_ext_pwr_config:
  143. config.dev = &pdev->dev;
  144. config.init_data = pdata->reg_init_data[id];
  145. config.driver_data = reg;
  146. config.regmap = rc5t583->regmap;
  147. rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
  148. if (IS_ERR(rdev)) {
  149. dev_err(&pdev->dev, "Failed to register regulator %s\n",
  150. ri->desc.name);
  151. return PTR_ERR(rdev);
  152. }
  153. reg->rdev = rdev;
  154. }
  155. platform_set_drvdata(pdev, regs);
  156. return 0;
  157. }
  158. static struct platform_driver rc5t583_regulator_driver = {
  159. .driver = {
  160. .name = "rc5t583-regulator",
  161. },
  162. .probe = rc5t583_regulator_probe,
  163. };
  164. static int __init rc5t583_regulator_init(void)
  165. {
  166. return platform_driver_register(&rc5t583_regulator_driver);
  167. }
  168. subsys_initcall(rc5t583_regulator_init);
  169. static void __exit rc5t583_regulator_exit(void)
  170. {
  171. platform_driver_unregister(&rc5t583_regulator_driver);
  172. }
  173. module_exit(rc5t583_regulator_exit);
  174. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  175. MODULE_DESCRIPTION("RC5T583 regulator driver");
  176. MODULE_ALIAS("platform:rc5t583-regulator");
  177. MODULE_LICENSE("GPL v2");