hi6421v530-regulator.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Device driver for regulators in Hi6421V530 IC
  4. //
  5. // Copyright (c) <2017> HiSilicon Technologies Co., Ltd.
  6. // http://www.hisilicon.com
  7. // Copyright (c) <2017> Linaro Ltd.
  8. // https://www.linaro.org
  9. //
  10. // Author: Wang Xiaoyin <hw.wangxiaoyin@hisilicon.com>
  11. // Guodong Xu <guodong.xu@linaro.org>
  12. #include <linux/mfd/hi6421-pmic.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/driver.h>
  18. /*
  19. * struct hi6421v530_regulator_info - hi6421v530 regulator information
  20. * @desc: regulator description
  21. * @mode_mask: ECO mode bitmask of LDOs; for BUCKs, this masks sleep
  22. * @eco_microamp: eco mode load upper limit (in uA), valid for LDOs only
  23. */
  24. struct hi6421v530_regulator_info {
  25. struct regulator_desc rdesc;
  26. u8 mode_mask;
  27. u32 eco_microamp;
  28. };
  29. /* HI6421v530 regulators */
  30. enum hi6421v530_regulator_id {
  31. HI6421V530_LDO3,
  32. HI6421V530_LDO9,
  33. HI6421V530_LDO11,
  34. HI6421V530_LDO15,
  35. HI6421V530_LDO16,
  36. };
  37. static const unsigned int ldo_3_voltages[] = {
  38. 1800000, 1825000, 1850000, 1875000,
  39. 1900000, 1925000, 1950000, 1975000,
  40. 2000000, 2025000, 2050000, 2075000,
  41. 2100000, 2125000, 2150000, 2200000,
  42. };
  43. static const unsigned int ldo_9_11_voltages[] = {
  44. 1750000, 1800000, 1825000, 2800000,
  45. 2850000, 2950000, 3000000, 3300000,
  46. };
  47. static const unsigned int ldo_15_16_voltages[] = {
  48. 1750000, 1800000, 2400000, 2600000,
  49. 2700000, 2850000, 2950000, 3000000,
  50. };
  51. static const struct regulator_ops hi6421v530_ldo_ops;
  52. #define HI6421V530_LDO_ENABLE_TIME (350)
  53. /*
  54. * _id - LDO id name string
  55. * v_table - voltage table
  56. * vreg - voltage select register
  57. * vmask - voltage select mask
  58. * ereg - enable register
  59. * emask - enable mask
  60. * odelay - off/on delay time in uS
  61. * ecomask - eco mode mask
  62. * ecoamp - eco mode load uppler limit in uA
  63. */
  64. #define HI6421V530_LDO(_ID, v_table, vreg, vmask, ereg, emask, \
  65. odelay, ecomask, ecoamp) { \
  66. .rdesc = { \
  67. .name = #_ID, \
  68. .of_match = of_match_ptr(#_ID), \
  69. .regulators_node = of_match_ptr("regulators"), \
  70. .ops = &hi6421v530_ldo_ops, \
  71. .type = REGULATOR_VOLTAGE, \
  72. .id = HI6421V530_##_ID, \
  73. .owner = THIS_MODULE, \
  74. .n_voltages = ARRAY_SIZE(v_table), \
  75. .volt_table = v_table, \
  76. .vsel_reg = HI6421_REG_TO_BUS_ADDR(vreg), \
  77. .vsel_mask = vmask, \
  78. .enable_reg = HI6421_REG_TO_BUS_ADDR(ereg), \
  79. .enable_mask = emask, \
  80. .enable_time = HI6421V530_LDO_ENABLE_TIME, \
  81. .off_on_delay = odelay, \
  82. }, \
  83. .mode_mask = ecomask, \
  84. .eco_microamp = ecoamp, \
  85. }
  86. /* HI6421V530 regulator information */
  87. static struct hi6421v530_regulator_info hi6421v530_regulator_info[] = {
  88. HI6421V530_LDO(LDO3, ldo_3_voltages, 0x061, 0xf, 0x060, 0x2,
  89. 20000, 0x6, 8000),
  90. HI6421V530_LDO(LDO9, ldo_9_11_voltages, 0x06b, 0x7, 0x06a, 0x2,
  91. 40000, 0x6, 8000),
  92. HI6421V530_LDO(LDO11, ldo_9_11_voltages, 0x06f, 0x7, 0x06e, 0x2,
  93. 40000, 0x6, 8000),
  94. HI6421V530_LDO(LDO15, ldo_15_16_voltages, 0x077, 0x7, 0x076, 0x2,
  95. 40000, 0x6, 8000),
  96. HI6421V530_LDO(LDO16, ldo_15_16_voltages, 0x079, 0x7, 0x078, 0x2,
  97. 40000, 0x6, 8000),
  98. };
  99. static unsigned int hi6421v530_regulator_ldo_get_mode(
  100. struct regulator_dev *rdev)
  101. {
  102. struct hi6421v530_regulator_info *info;
  103. unsigned int reg_val;
  104. info = rdev_get_drvdata(rdev);
  105. regmap_read(rdev->regmap, rdev->desc->enable_reg, &reg_val);
  106. if (reg_val & (info->mode_mask))
  107. return REGULATOR_MODE_IDLE;
  108. return REGULATOR_MODE_NORMAL;
  109. }
  110. static int hi6421v530_regulator_ldo_set_mode(struct regulator_dev *rdev,
  111. unsigned int mode)
  112. {
  113. struct hi6421v530_regulator_info *info;
  114. unsigned int new_mode;
  115. info = rdev_get_drvdata(rdev);
  116. switch (mode) {
  117. case REGULATOR_MODE_NORMAL:
  118. new_mode = 0;
  119. break;
  120. case REGULATOR_MODE_IDLE:
  121. new_mode = info->mode_mask;
  122. break;
  123. default:
  124. return -EINVAL;
  125. }
  126. regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  127. info->mode_mask, new_mode);
  128. return 0;
  129. }
  130. static const struct regulator_ops hi6421v530_ldo_ops = {
  131. .is_enabled = regulator_is_enabled_regmap,
  132. .enable = regulator_enable_regmap,
  133. .disable = regulator_disable_regmap,
  134. .list_voltage = regulator_list_voltage_table,
  135. .map_voltage = regulator_map_voltage_ascend,
  136. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  137. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  138. .get_mode = hi6421v530_regulator_ldo_get_mode,
  139. .set_mode = hi6421v530_regulator_ldo_set_mode,
  140. };
  141. static int hi6421v530_regulator_probe(struct platform_device *pdev)
  142. {
  143. struct hi6421_pmic *pmic;
  144. struct regulator_dev *rdev;
  145. struct regulator_config config = { };
  146. unsigned int i;
  147. pmic = dev_get_drvdata(pdev->dev.parent);
  148. if (!pmic) {
  149. dev_err(&pdev->dev, "no pmic in the regulator parent node\n");
  150. return -ENODEV;
  151. }
  152. for (i = 0; i < ARRAY_SIZE(hi6421v530_regulator_info); i++) {
  153. config.dev = pdev->dev.parent;
  154. config.regmap = pmic->regmap;
  155. config.driver_data = &hi6421v530_regulator_info[i];
  156. rdev = devm_regulator_register(&pdev->dev,
  157. &hi6421v530_regulator_info[i].rdesc,
  158. &config);
  159. if (IS_ERR(rdev)) {
  160. dev_err(&pdev->dev, "failed to register regulator %s\n",
  161. hi6421v530_regulator_info[i].rdesc.name);
  162. return PTR_ERR(rdev);
  163. }
  164. }
  165. return 0;
  166. }
  167. static const struct platform_device_id hi6421v530_regulator_table[] = {
  168. { .name = "hi6421v530-regulator" },
  169. {},
  170. };
  171. MODULE_DEVICE_TABLE(platform, hi6421v530_regulator_table);
  172. static struct platform_driver hi6421v530_regulator_driver = {
  173. .id_table = hi6421v530_regulator_table,
  174. .driver = {
  175. .name = "hi6421v530-regulator",
  176. },
  177. .probe = hi6421v530_regulator_probe,
  178. };
  179. module_platform_driver(hi6421v530_regulator_driver);
  180. MODULE_AUTHOR("Wang Xiaoyin <hw.wangxiaoyin@hisilicon.com>");
  181. MODULE_DESCRIPTION("Hi6421v530 regulator driver");
  182. MODULE_LICENSE("GPL v2");