rc5t583-regulator.c 8.5 KB

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