arizona-micsupp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * arizona-micsupp.c -- Microphone supply for Arizona devices
  3. *
  4. * Copyright 2012 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <[email protected]>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/bitops.h>
  17. #include <linux/err.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/machine.h>
  22. #include <linux/regulator/of_regulator.h>
  23. #include <linux/gpio.h>
  24. #include <linux/slab.h>
  25. #include <linux/workqueue.h>
  26. #include <sound/soc.h>
  27. #include <linux/mfd/arizona/core.h>
  28. #include <linux/mfd/arizona/pdata.h>
  29. #include <linux/mfd/arizona/registers.h>
  30. struct arizona_micsupp {
  31. struct regulator_dev *regulator;
  32. struct arizona *arizona;
  33. struct regulator_consumer_supply supply;
  34. struct regulator_init_data init_data;
  35. struct work_struct check_cp_work;
  36. };
  37. static void arizona_micsupp_check_cp(struct work_struct *work)
  38. {
  39. struct arizona_micsupp *micsupp =
  40. container_of(work, struct arizona_micsupp, check_cp_work);
  41. struct snd_soc_dapm_context *dapm = micsupp->arizona->dapm;
  42. struct arizona *arizona = micsupp->arizona;
  43. struct regmap *regmap = arizona->regmap;
  44. unsigned int reg;
  45. int ret;
  46. ret = regmap_read(regmap, ARIZONA_MIC_CHARGE_PUMP_1, &reg);
  47. if (ret != 0) {
  48. dev_err(arizona->dev, "Failed to read CP state: %d\n", ret);
  49. return;
  50. }
  51. if (dapm) {
  52. if ((reg & (ARIZONA_CPMIC_ENA | ARIZONA_CPMIC_BYPASS)) ==
  53. ARIZONA_CPMIC_ENA)
  54. snd_soc_dapm_force_enable_pin(dapm, "MICSUPP");
  55. else
  56. snd_soc_dapm_disable_pin(dapm, "MICSUPP");
  57. snd_soc_dapm_sync(dapm);
  58. }
  59. }
  60. static int arizona_micsupp_enable(struct regulator_dev *rdev)
  61. {
  62. struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
  63. int ret;
  64. ret = regulator_enable_regmap(rdev);
  65. if (ret == 0)
  66. schedule_work(&micsupp->check_cp_work);
  67. return ret;
  68. }
  69. static int arizona_micsupp_disable(struct regulator_dev *rdev)
  70. {
  71. struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
  72. int ret;
  73. ret = regulator_disable_regmap(rdev);
  74. if (ret == 0)
  75. schedule_work(&micsupp->check_cp_work);
  76. return ret;
  77. }
  78. static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena)
  79. {
  80. struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
  81. int ret;
  82. ret = regulator_set_bypass_regmap(rdev, ena);
  83. if (ret == 0)
  84. schedule_work(&micsupp->check_cp_work);
  85. return ret;
  86. }
  87. static struct regulator_ops arizona_micsupp_ops = {
  88. .enable = arizona_micsupp_enable,
  89. .disable = arizona_micsupp_disable,
  90. .is_enabled = regulator_is_enabled_regmap,
  91. .list_voltage = regulator_list_voltage_linear_range,
  92. .map_voltage = regulator_map_voltage_linear_range,
  93. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  94. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  95. .get_bypass = regulator_get_bypass_regmap,
  96. .set_bypass = arizona_micsupp_set_bypass,
  97. };
  98. static const struct regulator_linear_range arizona_micsupp_ranges[] = {
  99. REGULATOR_LINEAR_RANGE(1700000, 0, 0x1e, 50000),
  100. REGULATOR_LINEAR_RANGE(3300000, 0x1f, 0x1f, 0),
  101. };
  102. static const struct regulator_desc arizona_micsupp = {
  103. .name = "MICVDD",
  104. .supply_name = "CPVDD",
  105. .type = REGULATOR_VOLTAGE,
  106. .n_voltages = 32,
  107. .ops = &arizona_micsupp_ops,
  108. .vsel_reg = ARIZONA_LDO2_CONTROL_1,
  109. .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
  110. .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
  111. .enable_mask = ARIZONA_CPMIC_ENA,
  112. .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
  113. .bypass_mask = ARIZONA_CPMIC_BYPASS,
  114. .linear_ranges = arizona_micsupp_ranges,
  115. .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ranges),
  116. .enable_time = 3000,
  117. .owner = THIS_MODULE,
  118. };
  119. static const struct regulator_linear_range arizona_micsupp_ext_ranges[] = {
  120. REGULATOR_LINEAR_RANGE(900000, 0, 0x14, 25000),
  121. REGULATOR_LINEAR_RANGE(1500000, 0x15, 0x27, 100000),
  122. };
  123. static const struct regulator_desc arizona_micsupp_ext = {
  124. .name = "MICVDD",
  125. .supply_name = "CPVDD",
  126. .type = REGULATOR_VOLTAGE,
  127. .n_voltages = 40,
  128. .ops = &arizona_micsupp_ops,
  129. .vsel_reg = ARIZONA_LDO2_CONTROL_1,
  130. .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
  131. .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
  132. .enable_mask = ARIZONA_CPMIC_ENA,
  133. .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
  134. .bypass_mask = ARIZONA_CPMIC_BYPASS,
  135. .linear_ranges = arizona_micsupp_ext_ranges,
  136. .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ext_ranges),
  137. .enable_time = 3000,
  138. .owner = THIS_MODULE,
  139. };
  140. static const struct regulator_init_data arizona_micsupp_default = {
  141. .constraints = {
  142. .valid_ops_mask = REGULATOR_CHANGE_STATUS |
  143. REGULATOR_CHANGE_VOLTAGE |
  144. REGULATOR_CHANGE_BYPASS,
  145. .min_uV = 1700000,
  146. .max_uV = 3300000,
  147. },
  148. .num_consumer_supplies = 1,
  149. };
  150. static const struct regulator_init_data arizona_micsupp_ext_default = {
  151. .constraints = {
  152. .valid_ops_mask = REGULATOR_CHANGE_STATUS |
  153. REGULATOR_CHANGE_VOLTAGE |
  154. REGULATOR_CHANGE_BYPASS,
  155. .min_uV = 900000,
  156. .max_uV = 3300000,
  157. },
  158. .num_consumer_supplies = 1,
  159. };
  160. static int arizona_micsupp_of_get_pdata(struct arizona *arizona,
  161. struct regulator_config *config,
  162. const struct regulator_desc *desc)
  163. {
  164. struct arizona_pdata *pdata = &arizona->pdata;
  165. struct arizona_micsupp *micsupp = config->driver_data;
  166. struct device_node *np;
  167. struct regulator_init_data *init_data;
  168. np = of_get_child_by_name(arizona->dev->of_node, "micvdd");
  169. if (np) {
  170. config->of_node = np;
  171. init_data = of_get_regulator_init_data(arizona->dev, np, desc);
  172. if (init_data) {
  173. init_data->consumer_supplies = &micsupp->supply;
  174. init_data->num_consumer_supplies = 1;
  175. pdata->micvdd = init_data;
  176. }
  177. }
  178. return 0;
  179. }
  180. static int arizona_micsupp_probe(struct platform_device *pdev)
  181. {
  182. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  183. const struct regulator_desc *desc;
  184. struct regulator_config config = { };
  185. struct arizona_micsupp *micsupp;
  186. int ret;
  187. micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
  188. if (!micsupp)
  189. return -ENOMEM;
  190. micsupp->arizona = arizona;
  191. INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
  192. /*
  193. * Since the chip usually supplies itself we provide some
  194. * default init_data for it. This will be overridden with
  195. * platform data if provided.
  196. */
  197. switch (arizona->type) {
  198. case WM5110:
  199. case WM8280:
  200. desc = &arizona_micsupp_ext;
  201. micsupp->init_data = arizona_micsupp_ext_default;
  202. break;
  203. default:
  204. desc = &arizona_micsupp;
  205. micsupp->init_data = arizona_micsupp_default;
  206. break;
  207. }
  208. micsupp->init_data.consumer_supplies = &micsupp->supply;
  209. micsupp->supply.supply = "MICVDD";
  210. micsupp->supply.dev_name = dev_name(arizona->dev);
  211. config.dev = arizona->dev;
  212. config.driver_data = micsupp;
  213. config.regmap = arizona->regmap;
  214. if (IS_ENABLED(CONFIG_OF)) {
  215. if (!dev_get_platdata(arizona->dev)) {
  216. ret = arizona_micsupp_of_get_pdata(arizona, &config,
  217. desc);
  218. if (ret < 0)
  219. return ret;
  220. }
  221. }
  222. if (arizona->pdata.micvdd)
  223. config.init_data = arizona->pdata.micvdd;
  224. else
  225. config.init_data = &micsupp->init_data;
  226. /* Default to regulated mode until the API supports bypass */
  227. regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1,
  228. ARIZONA_CPMIC_BYPASS, 0);
  229. micsupp->regulator = devm_regulator_register(&pdev->dev,
  230. desc,
  231. &config);
  232. of_node_put(config.of_node);
  233. if (IS_ERR(micsupp->regulator)) {
  234. ret = PTR_ERR(micsupp->regulator);
  235. dev_err(arizona->dev, "Failed to register mic supply: %d\n",
  236. ret);
  237. return ret;
  238. }
  239. platform_set_drvdata(pdev, micsupp);
  240. return 0;
  241. }
  242. static struct platform_driver arizona_micsupp_driver = {
  243. .probe = arizona_micsupp_probe,
  244. .driver = {
  245. .name = "arizona-micsupp",
  246. },
  247. };
  248. module_platform_driver(arizona_micsupp_driver);
  249. /* Module information */
  250. MODULE_AUTHOR("Mark Brown <[email protected]>");
  251. MODULE_DESCRIPTION("Arizona microphone supply driver");
  252. MODULE_LICENSE("GPL");
  253. MODULE_ALIAS("platform:arizona-micsupp");