rc5t583-regulator.c 7.5 KB

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