hi655x-regulator.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Device driver for regulators in Hi655x IC
  3. *
  4. * Copyright (c) 2016 Hisilicon.
  5. *
  6. * Authors:
  7. * Chen Feng <puck.chen@hisilicon.com>
  8. * Fei Wang <w.f@huawei.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/regulator/driver.h>
  23. #include <linux/regulator/machine.h>
  24. #include <linux/regulator/of_regulator.h>
  25. #include <linux/mfd/hi655x-pmic.h>
  26. struct hi655x_regulator {
  27. unsigned int disable_reg;
  28. unsigned int status_reg;
  29. unsigned int ctrl_regs;
  30. unsigned int ctrl_mask;
  31. struct regulator_desc rdesc;
  32. };
  33. /* LDO7 & LDO10 */
  34. static const unsigned int ldo7_voltages[] = {
  35. 1800000, 1850000, 2850000, 2900000,
  36. 3000000, 3100000, 3200000, 3300000,
  37. };
  38. static const unsigned int ldo19_voltages[] = {
  39. 1800000, 1850000, 1900000, 1750000,
  40. 2800000, 2850000, 2900000, 3000000,
  41. };
  42. static const unsigned int ldo22_voltages[] = {
  43. 900000, 1000000, 1050000, 1100000,
  44. 1150000, 1175000, 1185000, 1200000,
  45. };
  46. enum hi655x_regulator_id {
  47. HI655X_LDO0,
  48. HI655X_LDO1,
  49. HI655X_LDO2,
  50. HI655X_LDO3,
  51. HI655X_LDO4,
  52. HI655X_LDO5,
  53. HI655X_LDO6,
  54. HI655X_LDO7,
  55. HI655X_LDO8,
  56. HI655X_LDO9,
  57. HI655X_LDO10,
  58. HI655X_LDO11,
  59. HI655X_LDO12,
  60. HI655X_LDO13,
  61. HI655X_LDO14,
  62. HI655X_LDO15,
  63. HI655X_LDO16,
  64. HI655X_LDO17,
  65. HI655X_LDO18,
  66. HI655X_LDO19,
  67. HI655X_LDO20,
  68. HI655X_LDO21,
  69. HI655X_LDO22,
  70. };
  71. static int hi655x_is_enabled(struct regulator_dev *rdev)
  72. {
  73. unsigned int value = 0;
  74. struct hi655x_regulator *regulator = rdev_get_drvdata(rdev);
  75. regmap_read(rdev->regmap, regulator->status_reg, &value);
  76. return (value & BIT(regulator->ctrl_mask));
  77. }
  78. static int hi655x_disable(struct regulator_dev *rdev)
  79. {
  80. int ret = 0;
  81. struct hi655x_regulator *regulator = rdev_get_drvdata(rdev);
  82. ret = regmap_write(rdev->regmap, regulator->disable_reg,
  83. BIT(regulator->ctrl_mask));
  84. return ret;
  85. }
  86. static struct regulator_ops hi655x_regulator_ops = {
  87. .enable = regulator_enable_regmap,
  88. .disable = hi655x_disable,
  89. .is_enabled = hi655x_is_enabled,
  90. .list_voltage = regulator_list_voltage_table,
  91. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  92. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  93. };
  94. static struct regulator_ops hi655x_ldo_linear_ops = {
  95. .enable = regulator_enable_regmap,
  96. .disable = hi655x_disable,
  97. .is_enabled = hi655x_is_enabled,
  98. .list_voltage = regulator_list_voltage_linear,
  99. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  100. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  101. };
  102. #define HI655X_LDO(_ID, vreg, vmask, ereg, dreg, \
  103. sreg, cmask, vtable) { \
  104. .rdesc = { \
  105. .name = #_ID, \
  106. .of_match = of_match_ptr(#_ID), \
  107. .ops = &hi655x_regulator_ops, \
  108. .regulators_node = of_match_ptr("regulators"), \
  109. .type = REGULATOR_VOLTAGE, \
  110. .id = HI655X_##_ID, \
  111. .owner = THIS_MODULE, \
  112. .n_voltages = ARRAY_SIZE(vtable), \
  113. .volt_table = vtable, \
  114. .vsel_reg = HI655X_BUS_ADDR(vreg), \
  115. .vsel_mask = vmask, \
  116. .enable_reg = HI655X_BUS_ADDR(ereg), \
  117. .enable_mask = BIT(cmask), \
  118. }, \
  119. .disable_reg = HI655X_BUS_ADDR(dreg), \
  120. .status_reg = HI655X_BUS_ADDR(sreg), \
  121. .ctrl_mask = cmask, \
  122. }
  123. #define HI655X_LDO_LINEAR(_ID, vreg, vmask, ereg, dreg, \
  124. sreg, cmask, minv, nvolt, vstep) { \
  125. .rdesc = { \
  126. .name = #_ID, \
  127. .of_match = of_match_ptr(#_ID), \
  128. .ops = &hi655x_ldo_linear_ops, \
  129. .regulators_node = of_match_ptr("regulators"), \
  130. .type = REGULATOR_VOLTAGE, \
  131. .id = HI655X_##_ID, \
  132. .owner = THIS_MODULE, \
  133. .min_uV = minv, \
  134. .n_voltages = nvolt, \
  135. .uV_step = vstep, \
  136. .vsel_reg = HI655X_BUS_ADDR(vreg), \
  137. .vsel_mask = vmask, \
  138. .enable_reg = HI655X_BUS_ADDR(ereg), \
  139. .enable_mask = BIT(cmask), \
  140. }, \
  141. .disable_reg = HI655X_BUS_ADDR(dreg), \
  142. .status_reg = HI655X_BUS_ADDR(sreg), \
  143. .ctrl_mask = cmask, \
  144. }
  145. static struct hi655x_regulator regulators[] = {
  146. HI655X_LDO_LINEAR(LDO2, 0x72, 0x07, 0x29, 0x2a, 0x2b, 0x01,
  147. 2500000, 8, 100000),
  148. HI655X_LDO(LDO7, 0x78, 0x07, 0x29, 0x2a, 0x2b, 0x06, ldo7_voltages),
  149. HI655X_LDO(LDO10, 0x78, 0x07, 0x29, 0x2a, 0x2b, 0x01, ldo7_voltages),
  150. HI655X_LDO_LINEAR(LDO13, 0x7e, 0x07, 0x2c, 0x2d, 0x2e, 0x04,
  151. 1600000, 8, 50000),
  152. HI655X_LDO_LINEAR(LDO14, 0x7f, 0x07, 0x2c, 0x2d, 0x2e, 0x05,
  153. 2500000, 8, 100000),
  154. HI655X_LDO_LINEAR(LDO15, 0x80, 0x07, 0x2c, 0x2d, 0x2e, 0x06,
  155. 1600000, 8, 50000),
  156. HI655X_LDO_LINEAR(LDO17, 0x82, 0x07, 0x2f, 0x30, 0x31, 0x00,
  157. 2500000, 8, 100000),
  158. HI655X_LDO(LDO19, 0x84, 0x07, 0x2f, 0x30, 0x31, 0x02, ldo19_voltages),
  159. HI655X_LDO_LINEAR(LDO21, 0x86, 0x07, 0x2f, 0x30, 0x31, 0x04,
  160. 1650000, 8, 50000),
  161. HI655X_LDO(LDO22, 0x87, 0x07, 0x2f, 0x30, 0x31, 0x05, ldo22_voltages),
  162. };
  163. static int hi655x_regulator_probe(struct platform_device *pdev)
  164. {
  165. unsigned int i;
  166. struct hi655x_regulator *regulator;
  167. struct hi655x_pmic *pmic;
  168. struct regulator_config config = { };
  169. struct regulator_dev *rdev;
  170. pmic = dev_get_drvdata(pdev->dev.parent);
  171. if (!pmic) {
  172. dev_err(&pdev->dev, "no pmic in the regulator parent node\n");
  173. return -ENODEV;
  174. }
  175. regulator = devm_kzalloc(&pdev->dev, sizeof(*regulator), GFP_KERNEL);
  176. if (!regulator)
  177. return -ENOMEM;
  178. platform_set_drvdata(pdev, regulator);
  179. config.dev = pdev->dev.parent;
  180. config.regmap = pmic->regmap;
  181. config.driver_data = regulator;
  182. for (i = 0; i < ARRAY_SIZE(regulators); i++) {
  183. rdev = devm_regulator_register(&pdev->dev,
  184. &regulators[i].rdesc,
  185. &config);
  186. if (IS_ERR(rdev)) {
  187. dev_err(&pdev->dev, "failed to register regulator %s\n",
  188. regulator->rdesc.name);
  189. return PTR_ERR(rdev);
  190. }
  191. }
  192. return 0;
  193. }
  194. static struct platform_driver hi655x_regulator_driver = {
  195. .driver = {
  196. .name = "hi655x-regulator",
  197. },
  198. .probe = hi655x_regulator_probe,
  199. };
  200. module_platform_driver(hi655x_regulator_driver);
  201. MODULE_AUTHOR("Chen Feng <puck.chen@hisilicon.com>");
  202. MODULE_DESCRIPTION("Hisilicon Hi655x regulator driver");
  203. MODULE_LICENSE("GPL v2");