rc5t583-regulator.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /* Devices */
  47. struct device *dev;
  48. struct rc5t583 *mfd;
  49. struct regulator_dev *rdev;
  50. };
  51. static int rc5t583_regulator_enable_time(struct regulator_dev *rdev)
  52. {
  53. struct rc5t583_regulator *reg = rdev_get_drvdata(rdev);
  54. int vsel = regulator_get_voltage_sel_regmap(rdev);
  55. int curr_uV = regulator_list_voltage_linear(rdev, vsel);
  56. return DIV_ROUND_UP(curr_uV, reg->reg_info->enable_uv_per_us);
  57. }
  58. static struct regulator_ops rc5t583_ops = {
  59. .is_enabled = regulator_is_enabled_regmap,
  60. .enable = regulator_enable_regmap,
  61. .disable = regulator_disable_regmap,
  62. .enable_time = rc5t583_regulator_enable_time,
  63. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  64. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  65. .list_voltage = regulator_list_voltage_linear,
  66. .map_voltage = regulator_map_voltage_linear,
  67. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  68. };
  69. #define RC5T583_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, \
  70. _vout_mask, _min_mv, _max_mv, _step_uV, _enable_mv) \
  71. { \
  72. .reg_disc_reg = RC5T583_REG_##_disc_reg, \
  73. .disc_bit = _disc_bit, \
  74. .deepsleep_reg = RC5T583_REG_##_id##DAC_DS, \
  75. .enable_uv_per_us = _enable_mv * 1000, \
  76. .deepsleep_id = RC5T583_DS_##_id, \
  77. .desc = { \
  78. .name = "rc5t583-regulator-"#_id, \
  79. .id = RC5T583_REGULATOR_##_id, \
  80. .n_voltages = (_max_mv - _min_mv) * 1000 / _step_uV + 1, \
  81. .ops = &rc5t583_ops, \
  82. .type = REGULATOR_VOLTAGE, \
  83. .owner = THIS_MODULE, \
  84. .vsel_reg = RC5T583_REG_##_id##DAC, \
  85. .vsel_mask = _vout_mask, \
  86. .enable_reg = RC5T583_REG_##_en_reg, \
  87. .enable_mask = BIT(_en_bit), \
  88. .min_uV = _min_mv * 1000, \
  89. .uV_step = _step_uV, \
  90. .ramp_delay = 40 * 1000, \
  91. }, \
  92. }
  93. static struct rc5t583_regulator_info rc5t583_reg_info[RC5T583_REGULATOR_MAX] = {
  94. RC5T583_REG(DC0, DC0CTL, 0, DC0CTL, 1, 0x7F, 700, 1500, 12500, 4),
  95. RC5T583_REG(DC1, DC1CTL, 0, DC1CTL, 1, 0x7F, 700, 1500, 12500, 14),
  96. RC5T583_REG(DC2, DC2CTL, 0, DC2CTL, 1, 0x7F, 900, 2400, 12500, 14),
  97. RC5T583_REG(DC3, DC3CTL, 0, DC3CTL, 1, 0x7F, 900, 2400, 12500, 14),
  98. RC5T583_REG(LDO0, LDOEN2, 0, LDODIS2, 0, 0x7F, 900, 3400, 25000, 160),
  99. RC5T583_REG(LDO1, LDOEN2, 1, LDODIS2, 1, 0x7F, 900, 3400, 25000, 160),
  100. RC5T583_REG(LDO2, LDOEN2, 2, LDODIS2, 2, 0x7F, 900, 3400, 25000, 160),
  101. RC5T583_REG(LDO3, LDOEN2, 3, LDODIS2, 3, 0x7F, 900, 3400, 25000, 160),
  102. RC5T583_REG(LDO4, LDOEN2, 4, LDODIS2, 4, 0x3F, 750, 1500, 12500, 133),
  103. RC5T583_REG(LDO5, LDOEN2, 5, LDODIS2, 5, 0x7F, 900, 3400, 25000, 267),
  104. RC5T583_REG(LDO6, LDOEN2, 6, LDODIS2, 6, 0x7F, 900, 3400, 25000, 133),
  105. RC5T583_REG(LDO7, LDOEN2, 7, LDODIS2, 7, 0x7F, 900, 3400, 25000, 233),
  106. RC5T583_REG(LDO8, LDOEN1, 0, LDODIS1, 0, 0x7F, 900, 3400, 25000, 233),
  107. RC5T583_REG(LDO9, LDOEN1, 1, LDODIS1, 1, 0x7F, 900, 3400, 25000, 133),
  108. };
  109. static int rc5t583_regulator_probe(struct platform_device *pdev)
  110. {
  111. struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
  112. struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev);
  113. struct regulator_init_data *reg_data;
  114. struct regulator_config config = { };
  115. struct rc5t583_regulator *reg = NULL;
  116. struct rc5t583_regulator *regs;
  117. struct regulator_dev *rdev;
  118. struct rc5t583_regulator_info *ri;
  119. int ret;
  120. int id;
  121. if (!pdata) {
  122. dev_err(&pdev->dev, "No platform data, exiting...\n");
  123. return -ENODEV;
  124. }
  125. regs = devm_kzalloc(&pdev->dev, RC5T583_REGULATOR_MAX *
  126. sizeof(struct rc5t583_regulator), GFP_KERNEL);
  127. if (!regs) {
  128. dev_err(&pdev->dev, "Memory allocation failed exiting..\n");
  129. return -ENOMEM;
  130. }
  131. for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) {
  132. reg_data = pdata->reg_init_data[id];
  133. /* No need to register if there is no regulator data */
  134. if (!reg_data)
  135. continue;
  136. reg = &regs[id];
  137. ri = &rc5t583_reg_info[id];
  138. reg->reg_info = ri;
  139. reg->mfd = rc5t583;
  140. reg->dev = &pdev->dev;
  141. if (ri->deepsleep_id == RC5T583_DS_NONE)
  142. goto skip_ext_pwr_config;
  143. ret = rc5t583_ext_power_req_config(rc5t583->dev,
  144. ri->deepsleep_id,
  145. pdata->regulator_ext_pwr_control[id],
  146. pdata->regulator_deepsleep_slot[id]);
  147. /*
  148. * Configuring external control is not a major issue,
  149. * just give warning.
  150. */
  151. if (ret < 0)
  152. dev_warn(&pdev->dev,
  153. "Failed to configure ext control %d\n", id);
  154. skip_ext_pwr_config:
  155. config.dev = &pdev->dev;
  156. config.init_data = reg_data;
  157. config.driver_data = reg;
  158. config.regmap = rc5t583->regmap;
  159. rdev = regulator_register(&ri->desc, &config);
  160. if (IS_ERR(rdev)) {
  161. dev_err(&pdev->dev, "Failed to register regulator %s\n",
  162. ri->desc.name);
  163. ret = PTR_ERR(rdev);
  164. goto clean_exit;
  165. }
  166. reg->rdev = rdev;
  167. }
  168. platform_set_drvdata(pdev, regs);
  169. return 0;
  170. clean_exit:
  171. while (--id >= 0)
  172. regulator_unregister(regs[id].rdev);
  173. return ret;
  174. }
  175. static int rc5t583_regulator_remove(struct platform_device *pdev)
  176. {
  177. struct rc5t583_regulator *regs = platform_get_drvdata(pdev);
  178. int id;
  179. for (id = 0; id < RC5T583_REGULATOR_MAX; ++id)
  180. regulator_unregister(regs[id].rdev);
  181. return 0;
  182. }
  183. static struct platform_driver rc5t583_regulator_driver = {
  184. .driver = {
  185. .name = "rc5t583-regulator",
  186. .owner = THIS_MODULE,
  187. },
  188. .probe = rc5t583_regulator_probe,
  189. .remove = rc5t583_regulator_remove,
  190. };
  191. static int __init rc5t583_regulator_init(void)
  192. {
  193. return platform_driver_register(&rc5t583_regulator_driver);
  194. }
  195. subsys_initcall(rc5t583_regulator_init);
  196. static void __exit rc5t583_regulator_exit(void)
  197. {
  198. platform_driver_unregister(&rc5t583_regulator_driver);
  199. }
  200. module_exit(rc5t583_regulator_exit);
  201. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  202. MODULE_DESCRIPTION("RC5T583 regulator driver");
  203. MODULE_ALIAS("platform:rc5t583-regulator");
  204. MODULE_LICENSE("GPL v2");