hi6421v530-regulator.c 5.6 KB

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