hi6421v530-regulator.c 5.9 KB

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