rc5t583-regulator.c 8.9 KB

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