lochnagar-regulator.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Lochnagar regulator driver
  4. //
  5. // Copyright (c) 2017-2018 Cirrus Logic, Inc. and
  6. // Cirrus Logic International Semiconductor Ltd.
  7. //
  8. // Author: Charles Keepax <ckeepax@opensource.cirrus.com>
  9. #include <linux/bitops.h>
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/regulator/driver.h>
  19. #include <linux/regulator/machine.h>
  20. #include <linux/regulator/of_regulator.h>
  21. #include <linux/mfd/lochnagar.h>
  22. #include <linux/mfd/lochnagar1_regs.h>
  23. #include <linux/mfd/lochnagar2_regs.h>
  24. static const struct regulator_ops lochnagar_micvdd_ops = {
  25. .enable = regulator_enable_regmap,
  26. .disable = regulator_disable_regmap,
  27. .is_enabled = regulator_is_enabled_regmap,
  28. .list_voltage = regulator_list_voltage_linear_range,
  29. .map_voltage = regulator_map_voltage_linear_range,
  30. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  31. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  32. };
  33. static const struct regulator_linear_range lochnagar_micvdd_ranges[] = {
  34. REGULATOR_LINEAR_RANGE(1000000, 0, 0xC, 50000),
  35. REGULATOR_LINEAR_RANGE(1700000, 0xD, 0x1F, 100000),
  36. };
  37. static int lochnagar_micbias_enable(struct regulator_dev *rdev)
  38. {
  39. struct lochnagar *lochnagar = rdev_get_drvdata(rdev);
  40. int ret;
  41. mutex_lock(&lochnagar->analogue_config_lock);
  42. ret = regulator_enable_regmap(rdev);
  43. if (ret < 0)
  44. goto err;
  45. ret = lochnagar_update_config(lochnagar);
  46. err:
  47. mutex_unlock(&lochnagar->analogue_config_lock);
  48. return ret;
  49. }
  50. static int lochnagar_micbias_disable(struct regulator_dev *rdev)
  51. {
  52. struct lochnagar *lochnagar = rdev_get_drvdata(rdev);
  53. int ret;
  54. mutex_lock(&lochnagar->analogue_config_lock);
  55. ret = regulator_disable_regmap(rdev);
  56. if (ret < 0)
  57. goto err;
  58. ret = lochnagar_update_config(lochnagar);
  59. err:
  60. mutex_unlock(&lochnagar->analogue_config_lock);
  61. return ret;
  62. }
  63. static const struct regulator_ops lochnagar_micbias_ops = {
  64. .enable = lochnagar_micbias_enable,
  65. .disable = lochnagar_micbias_disable,
  66. .is_enabled = regulator_is_enabled_regmap,
  67. };
  68. static const struct regulator_ops lochnagar_vddcore_ops = {
  69. .enable = regulator_enable_regmap,
  70. .disable = regulator_disable_regmap,
  71. .is_enabled = regulator_is_enabled_regmap,
  72. .list_voltage = regulator_list_voltage_linear_range,
  73. .map_voltage = regulator_map_voltage_linear_range,
  74. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  75. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  76. };
  77. static const struct regulator_linear_range lochnagar_vddcore_ranges[] = {
  78. REGULATOR_LINEAR_RANGE(600000, 0x8, 0x41, 12500),
  79. };
  80. enum lochnagar_regulators {
  81. LOCHNAGAR_MICVDD,
  82. LOCHNAGAR_MIC1VDD,
  83. LOCHNAGAR_MIC2VDD,
  84. LOCHNAGAR_VDDCORE,
  85. };
  86. static int lochnagar_micbias_of_parse(struct device_node *np,
  87. const struct regulator_desc *desc,
  88. struct regulator_config *config)
  89. {
  90. struct lochnagar *lochnagar = config->driver_data;
  91. int shift = (desc->id - LOCHNAGAR_MIC1VDD) *
  92. LOCHNAGAR2_P2_MICBIAS_SRC_SHIFT;
  93. int mask = LOCHNAGAR2_P1_MICBIAS_SRC_MASK << shift;
  94. unsigned int val;
  95. int ret;
  96. ret = of_property_read_u32(np, "cirrus,micbias-input", &val);
  97. if (ret >= 0) {
  98. mutex_lock(&lochnagar->analogue_config_lock);
  99. ret = regmap_update_bits(lochnagar->regmap,
  100. LOCHNAGAR2_ANALOGUE_PATH_CTRL2,
  101. mask, val << shift);
  102. mutex_unlock(&lochnagar->analogue_config_lock);
  103. if (ret < 0) {
  104. dev_err(lochnagar->dev,
  105. "Failed to update micbias source: %d\n", ret);
  106. return ret;
  107. }
  108. }
  109. return 0;
  110. }
  111. static const struct regulator_desc lochnagar_regulators[] = {
  112. [LOCHNAGAR_MICVDD] = {
  113. .name = "MICVDD",
  114. .supply_name = "SYSVDD",
  115. .type = REGULATOR_VOLTAGE,
  116. .n_voltages = 32,
  117. .ops = &lochnagar_micvdd_ops,
  118. .id = LOCHNAGAR_MICVDD,
  119. .of_match = of_match_ptr("MICVDD"),
  120. .enable_reg = LOCHNAGAR2_MICVDD_CTRL1,
  121. .enable_mask = LOCHNAGAR2_MICVDD_REG_ENA_MASK,
  122. .vsel_reg = LOCHNAGAR2_MICVDD_CTRL2,
  123. .vsel_mask = LOCHNAGAR2_MICVDD_VSEL_MASK,
  124. .linear_ranges = lochnagar_micvdd_ranges,
  125. .n_linear_ranges = ARRAY_SIZE(lochnagar_micvdd_ranges),
  126. .enable_time = 3000,
  127. .ramp_delay = 1000,
  128. .owner = THIS_MODULE,
  129. },
  130. [LOCHNAGAR_MIC1VDD] = {
  131. .name = "MIC1VDD",
  132. .supply_name = "MICBIAS1",
  133. .type = REGULATOR_VOLTAGE,
  134. .ops = &lochnagar_micbias_ops,
  135. .id = LOCHNAGAR_MIC1VDD,
  136. .of_match = of_match_ptr("MIC1VDD"),
  137. .of_parse_cb = lochnagar_micbias_of_parse,
  138. .enable_reg = LOCHNAGAR2_ANALOGUE_PATH_CTRL2,
  139. .enable_mask = LOCHNAGAR2_P1_INPUT_BIAS_ENA_MASK,
  140. .owner = THIS_MODULE,
  141. },
  142. [LOCHNAGAR_MIC2VDD] = {
  143. .name = "MIC2VDD",
  144. .supply_name = "MICBIAS2",
  145. .type = REGULATOR_VOLTAGE,
  146. .ops = &lochnagar_micbias_ops,
  147. .id = LOCHNAGAR_MIC2VDD,
  148. .of_match = of_match_ptr("MIC2VDD"),
  149. .of_parse_cb = lochnagar_micbias_of_parse,
  150. .enable_reg = LOCHNAGAR2_ANALOGUE_PATH_CTRL2,
  151. .enable_mask = LOCHNAGAR2_P2_INPUT_BIAS_ENA_MASK,
  152. .owner = THIS_MODULE,
  153. },
  154. [LOCHNAGAR_VDDCORE] = {
  155. .name = "VDDCORE",
  156. .supply_name = "SYSVDD",
  157. .type = REGULATOR_VOLTAGE,
  158. .n_voltages = 66,
  159. .ops = &lochnagar_vddcore_ops,
  160. .id = LOCHNAGAR_VDDCORE,
  161. .of_match = of_match_ptr("VDDCORE"),
  162. .enable_reg = LOCHNAGAR2_VDDCORE_CDC_CTRL1,
  163. .enable_mask = LOCHNAGAR2_VDDCORE_CDC_REG_ENA_MASK,
  164. .vsel_reg = LOCHNAGAR2_VDDCORE_CDC_CTRL2,
  165. .vsel_mask = LOCHNAGAR2_VDDCORE_CDC_VSEL_MASK,
  166. .linear_ranges = lochnagar_vddcore_ranges,
  167. .n_linear_ranges = ARRAY_SIZE(lochnagar_vddcore_ranges),
  168. .enable_time = 3000,
  169. .ramp_delay = 1000,
  170. .off_on_delay = 15000,
  171. .owner = THIS_MODULE,
  172. },
  173. };
  174. static const struct of_device_id lochnagar_of_match[] = {
  175. {
  176. .compatible = "cirrus,lochnagar2-micvdd",
  177. .data = &lochnagar_regulators[LOCHNAGAR_MICVDD],
  178. },
  179. {
  180. .compatible = "cirrus,lochnagar2-mic1vdd",
  181. .data = &lochnagar_regulators[LOCHNAGAR_MIC1VDD],
  182. },
  183. {
  184. .compatible = "cirrus,lochnagar2-mic2vdd",
  185. .data = &lochnagar_regulators[LOCHNAGAR_MIC2VDD],
  186. },
  187. {
  188. .compatible = "cirrus,lochnagar2-vddcore",
  189. .data = &lochnagar_regulators[LOCHNAGAR_VDDCORE],
  190. },
  191. {}
  192. };
  193. MODULE_DEVICE_TABLE(of, lochnagar_of_match);
  194. static int lochnagar_regulator_probe(struct platform_device *pdev)
  195. {
  196. struct device *dev = &pdev->dev;
  197. struct lochnagar *lochnagar = dev_get_drvdata(dev->parent);
  198. struct regulator_config config = { };
  199. const struct of_device_id *of_id;
  200. const struct regulator_desc *desc;
  201. struct regulator_dev *rdev;
  202. int ret;
  203. config.dev = dev;
  204. config.regmap = lochnagar->regmap;
  205. config.driver_data = lochnagar;
  206. of_id = of_match_device(lochnagar_of_match, dev);
  207. if (!of_id)
  208. return -EINVAL;
  209. desc = of_id->data;
  210. rdev = devm_regulator_register(dev, desc, &config);
  211. if (IS_ERR(rdev)) {
  212. ret = PTR_ERR(rdev);
  213. dev_err(dev, "Failed to register %s regulator: %d\n",
  214. desc->name, ret);
  215. return ret;
  216. }
  217. return 0;
  218. }
  219. static struct platform_driver lochnagar_regulator_driver = {
  220. .driver = {
  221. .name = "lochnagar-regulator",
  222. .of_match_table = of_match_ptr(lochnagar_of_match),
  223. },
  224. .probe = lochnagar_regulator_probe,
  225. };
  226. module_platform_driver(lochnagar_regulator_driver);
  227. MODULE_AUTHOR("Charles Keepax <ckeepax@opensource.cirrus.com>");
  228. MODULE_DESCRIPTION("Regulator driver for Cirrus Logic Lochnagar Board");
  229. MODULE_LICENSE("GPL v2");
  230. MODULE_ALIAS("platform:lochnagar-regulator");