rc5t583-regulator.c 5.4 KB

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