arizona-micsupp.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * arizona-micsupp.c -- Microphone supply for Arizona devices
  3. *
  4. * Copyright 2012 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  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/platform_device.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/machine.h>
  21. #include <linux/gpio.h>
  22. #include <linux/slab.h>
  23. #include <linux/workqueue.h>
  24. #include <sound/soc.h>
  25. #include <linux/mfd/arizona/core.h>
  26. #include <linux/mfd/arizona/pdata.h>
  27. #include <linux/mfd/arizona/registers.h>
  28. #define ARIZONA_MICSUPP_MAX_SELECTOR 0x1f
  29. struct arizona_micsupp {
  30. struct regulator_dev *regulator;
  31. struct arizona *arizona;
  32. struct regulator_consumer_supply supply;
  33. struct regulator_init_data init_data;
  34. struct work_struct check_cp_work;
  35. };
  36. static int arizona_micsupp_list_voltage(struct regulator_dev *rdev,
  37. unsigned int selector)
  38. {
  39. if (selector > ARIZONA_MICSUPP_MAX_SELECTOR)
  40. return -EINVAL;
  41. if (selector == ARIZONA_MICSUPP_MAX_SELECTOR)
  42. return 3300000;
  43. else
  44. return (selector * 50000) + 1700000;
  45. }
  46. static int arizona_micsupp_map_voltage(struct regulator_dev *rdev,
  47. int min_uV, int max_uV)
  48. {
  49. unsigned int voltage;
  50. int selector;
  51. if (min_uV < 1700000)
  52. min_uV = 1700000;
  53. if (min_uV > 3200000)
  54. selector = ARIZONA_MICSUPP_MAX_SELECTOR;
  55. else
  56. selector = DIV_ROUND_UP(min_uV - 1700000, 50000);
  57. if (selector < 0)
  58. return -EINVAL;
  59. voltage = arizona_micsupp_list_voltage(rdev, selector);
  60. if (voltage < min_uV || voltage > max_uV)
  61. return -EINVAL;
  62. return selector;
  63. }
  64. static void arizona_micsupp_check_cp(struct work_struct *work)
  65. {
  66. struct arizona_micsupp *micsupp =
  67. container_of(work, struct arizona_micsupp, check_cp_work);
  68. struct snd_soc_dapm_context *dapm = micsupp->arizona->dapm;
  69. struct arizona *arizona = micsupp->arizona;
  70. struct regmap *regmap = arizona->regmap;
  71. unsigned int reg;
  72. int ret;
  73. ret = regmap_read(regmap, ARIZONA_MIC_CHARGE_PUMP_1, &reg);
  74. if (ret != 0) {
  75. dev_err(arizona->dev, "Failed to read CP state: %d\n", ret);
  76. return;
  77. }
  78. if (dapm) {
  79. if ((reg & (ARIZONA_CPMIC_ENA | ARIZONA_CPMIC_BYPASS)) ==
  80. ARIZONA_CPMIC_ENA)
  81. snd_soc_dapm_force_enable_pin(dapm, "MICSUPP");
  82. else
  83. snd_soc_dapm_disable_pin(dapm, "MICSUPP");
  84. snd_soc_dapm_sync(dapm);
  85. }
  86. }
  87. static int arizona_micsupp_enable(struct regulator_dev *rdev)
  88. {
  89. struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
  90. int ret;
  91. ret = regulator_enable_regmap(rdev);
  92. if (ret == 0)
  93. schedule_work(&micsupp->check_cp_work);
  94. return ret;
  95. }
  96. static int arizona_micsupp_disable(struct regulator_dev *rdev)
  97. {
  98. struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
  99. int ret;
  100. ret = regulator_disable_regmap(rdev);
  101. if (ret == 0)
  102. schedule_work(&micsupp->check_cp_work);
  103. return ret;
  104. }
  105. static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena)
  106. {
  107. struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
  108. int ret;
  109. ret = regulator_set_bypass_regmap(rdev, ena);
  110. if (ret == 0)
  111. schedule_work(&micsupp->check_cp_work);
  112. return ret;
  113. }
  114. static struct regulator_ops arizona_micsupp_ops = {
  115. .enable = arizona_micsupp_enable,
  116. .disable = arizona_micsupp_disable,
  117. .is_enabled = regulator_is_enabled_regmap,
  118. .list_voltage = arizona_micsupp_list_voltage,
  119. .map_voltage = arizona_micsupp_map_voltage,
  120. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  121. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  122. .get_bypass = regulator_get_bypass_regmap,
  123. .set_bypass = arizona_micsupp_set_bypass,
  124. };
  125. static const struct regulator_desc arizona_micsupp = {
  126. .name = "MICVDD",
  127. .supply_name = "CPVDD",
  128. .type = REGULATOR_VOLTAGE,
  129. .n_voltages = ARIZONA_MICSUPP_MAX_SELECTOR + 1,
  130. .ops = &arizona_micsupp_ops,
  131. .vsel_reg = ARIZONA_LDO2_CONTROL_1,
  132. .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
  133. .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
  134. .enable_mask = ARIZONA_CPMIC_ENA,
  135. .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
  136. .bypass_mask = ARIZONA_CPMIC_BYPASS,
  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 int arizona_micsupp_probe(struct platform_device *pdev)
  151. {
  152. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  153. struct regulator_config config = { };
  154. struct arizona_micsupp *micsupp;
  155. int ret;
  156. micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
  157. if (micsupp == NULL) {
  158. dev_err(&pdev->dev, "Unable to allocate private data\n");
  159. return -ENOMEM;
  160. }
  161. micsupp->arizona = arizona;
  162. INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
  163. /*
  164. * Since the chip usually supplies itself we provide some
  165. * default init_data for it. This will be overridden with
  166. * platform data if provided.
  167. */
  168. micsupp->init_data = arizona_micsupp_default;
  169. micsupp->init_data.consumer_supplies = &micsupp->supply;
  170. micsupp->supply.supply = "MICVDD";
  171. micsupp->supply.dev_name = dev_name(arizona->dev);
  172. config.dev = arizona->dev;
  173. config.driver_data = micsupp;
  174. config.regmap = arizona->regmap;
  175. if (arizona->pdata.micvdd)
  176. config.init_data = arizona->pdata.micvdd;
  177. else
  178. config.init_data = &micsupp->init_data;
  179. /* Default to regulated mode until the API supports bypass */
  180. regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1,
  181. ARIZONA_CPMIC_BYPASS, 0);
  182. micsupp->regulator = regulator_register(&arizona_micsupp, &config);
  183. if (IS_ERR(micsupp->regulator)) {
  184. ret = PTR_ERR(micsupp->regulator);
  185. dev_err(arizona->dev, "Failed to register mic supply: %d\n",
  186. ret);
  187. return ret;
  188. }
  189. platform_set_drvdata(pdev, micsupp);
  190. return 0;
  191. }
  192. static int arizona_micsupp_remove(struct platform_device *pdev)
  193. {
  194. struct arizona_micsupp *micsupp = platform_get_drvdata(pdev);
  195. regulator_unregister(micsupp->regulator);
  196. return 0;
  197. }
  198. static struct platform_driver arizona_micsupp_driver = {
  199. .probe = arizona_micsupp_probe,
  200. .remove = arizona_micsupp_remove,
  201. .driver = {
  202. .name = "arizona-micsupp",
  203. .owner = THIS_MODULE,
  204. },
  205. };
  206. module_platform_driver(arizona_micsupp_driver);
  207. /* Module information */
  208. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  209. MODULE_DESCRIPTION("Arizona microphone supply driver");
  210. MODULE_LICENSE("GPL");
  211. MODULE_ALIAS("platform:arizona-micsupp");