1 // SPDX-License-Identifier: GPL-2.0+
3 // arizona-micsupp.c -- Microphone supply for Arizona devices
5 // Copyright 2012 Wolfson Microelectronics PLC.
7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/init.h>
12 #include <linux/bitops.h>
13 #include <linux/err.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/machine.h>
18 #include <linux/regulator/of_regulator.h>
19 #include <linux/slab.h>
20 #include <linux/workqueue.h>
21 #include <sound/soc.h>
23 #include <linux/mfd/arizona/core.h>
24 #include <linux/mfd/arizona/pdata.h>
25 #include <linux/mfd/arizona/registers.h>
27 #include <linux/mfd/madera/core.h>
28 #include <linux/mfd/madera/pdata.h>
29 #include <linux/mfd/madera/registers.h>
31 #include <linux/regulator/arizona-micsupp.h>
33 struct arizona_micsupp {
34 struct regulator_dev *regulator;
35 struct regmap *regmap;
36 struct snd_soc_dapm_context **dapm;
37 const struct regulator_desc *desc;
40 struct regulator_consumer_supply supply;
41 struct regulator_init_data init_data;
43 struct work_struct check_cp_work;
46 static void arizona_micsupp_check_cp(struct work_struct *work)
48 struct arizona_micsupp *micsupp =
49 container_of(work, struct arizona_micsupp, check_cp_work);
50 struct snd_soc_dapm_context *dapm = *micsupp->dapm;
51 struct snd_soc_component *component;
52 const struct regulator_desc *desc = micsupp->desc;
56 ret = regmap_read(micsupp->regmap, desc->enable_reg, &val);
59 "Failed to read CP state: %d\n", ret);
64 component = snd_soc_dapm_to_component(dapm);
66 if ((val & (desc->enable_mask | desc->bypass_mask)) ==
68 snd_soc_component_force_enable_pin(component,
71 snd_soc_component_disable_pin(component, "MICSUPP");
73 snd_soc_dapm_sync(dapm);
77 static int arizona_micsupp_enable(struct regulator_dev *rdev)
79 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
82 ret = regulator_enable_regmap(rdev);
85 schedule_work(&micsupp->check_cp_work);
90 static int arizona_micsupp_disable(struct regulator_dev *rdev)
92 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
95 ret = regulator_disable_regmap(rdev);
97 schedule_work(&micsupp->check_cp_work);
102 static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena)
104 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
107 ret = regulator_set_bypass_regmap(rdev, ena);
109 schedule_work(&micsupp->check_cp_work);
114 static const struct regulator_ops arizona_micsupp_ops = {
115 .enable = arizona_micsupp_enable,
116 .disable = arizona_micsupp_disable,
117 .is_enabled = regulator_is_enabled_regmap,
119 .list_voltage = regulator_list_voltage_linear_range,
120 .map_voltage = regulator_map_voltage_linear_range,
122 .get_voltage_sel = regulator_get_voltage_sel_regmap,
123 .set_voltage_sel = regulator_set_voltage_sel_regmap,
125 .get_bypass = regulator_get_bypass_regmap,
126 .set_bypass = arizona_micsupp_set_bypass,
129 static const struct linear_range arizona_micsupp_ranges[] = {
130 REGULATOR_LINEAR_RANGE(1700000, 0, 0x1e, 50000),
131 REGULATOR_LINEAR_RANGE(3300000, 0x1f, 0x1f, 0),
134 static const struct regulator_desc arizona_micsupp = {
136 .supply_name = "CPVDD",
137 .type = REGULATOR_VOLTAGE,
139 .ops = &arizona_micsupp_ops,
141 .vsel_reg = ARIZONA_LDO2_CONTROL_1,
142 .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
143 .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
144 .enable_mask = ARIZONA_CPMIC_ENA,
145 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
146 .bypass_mask = ARIZONA_CPMIC_BYPASS,
148 .linear_ranges = arizona_micsupp_ranges,
149 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ranges),
153 .owner = THIS_MODULE,
156 static const struct linear_range arizona_micsupp_ext_ranges[] = {
157 REGULATOR_LINEAR_RANGE(900000, 0, 0x14, 25000),
158 REGULATOR_LINEAR_RANGE(1500000, 0x15, 0x27, 100000),
161 static const struct regulator_desc arizona_micsupp_ext = {
163 .supply_name = "CPVDD",
164 .type = REGULATOR_VOLTAGE,
166 .ops = &arizona_micsupp_ops,
168 .vsel_reg = ARIZONA_LDO2_CONTROL_1,
169 .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
170 .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
171 .enable_mask = ARIZONA_CPMIC_ENA,
172 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
173 .bypass_mask = ARIZONA_CPMIC_BYPASS,
175 .linear_ranges = arizona_micsupp_ext_ranges,
176 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ext_ranges),
180 .owner = THIS_MODULE,
183 static const struct regulator_init_data arizona_micsupp_default = {
185 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
186 REGULATOR_CHANGE_VOLTAGE |
187 REGULATOR_CHANGE_BYPASS,
192 .num_consumer_supplies = 1,
195 static const struct regulator_init_data arizona_micsupp_ext_default = {
197 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
198 REGULATOR_CHANGE_VOLTAGE |
199 REGULATOR_CHANGE_BYPASS,
204 .num_consumer_supplies = 1,
207 static const struct regulator_desc madera_micsupp = {
209 .supply_name = "CPVDD1",
210 .type = REGULATOR_VOLTAGE,
212 .ops = &arizona_micsupp_ops,
213 .vsel_reg = MADERA_LDO2_CONTROL_1,
214 .vsel_mask = MADERA_LDO2_VSEL_MASK,
215 .enable_reg = MADERA_MIC_CHARGE_PUMP_1,
216 .enable_mask = MADERA_CPMIC_ENA,
217 .bypass_reg = MADERA_MIC_CHARGE_PUMP_1,
218 .bypass_mask = MADERA_CPMIC_BYPASS,
220 .linear_ranges = arizona_micsupp_ext_ranges,
221 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ext_ranges),
225 .owner = THIS_MODULE,
228 static int arizona_micsupp_of_get_pdata(struct arizona_micsupp_pdata *pdata,
229 struct regulator_config *config,
230 const struct regulator_desc *desc)
232 struct arizona_micsupp *micsupp = config->driver_data;
233 struct device_node *np;
234 struct regulator_init_data *init_data;
236 np = of_get_child_by_name(config->dev->of_node, "micvdd");
239 config->of_node = np;
241 init_data = of_get_regulator_init_data(config->dev, np, desc);
244 init_data->consumer_supplies = &micsupp->supply;
245 init_data->num_consumer_supplies = 1;
247 pdata->init_data = init_data;
254 static int arizona_micsupp_common_init(struct platform_device *pdev,
255 struct arizona_micsupp *micsupp,
256 const struct regulator_desc *desc,
257 struct arizona_micsupp_pdata *pdata)
259 struct regulator_config config = { };
262 INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
264 micsupp->init_data.consumer_supplies = &micsupp->supply;
265 micsupp->supply.dev_name = dev_name(micsupp->dev);
266 micsupp->desc = desc;
268 config.dev = micsupp->dev;
269 config.driver_data = micsupp;
270 config.regmap = micsupp->regmap;
272 if (IS_ENABLED(CONFIG_OF)) {
273 if (!dev_get_platdata(micsupp->dev)) {
274 ret = arizona_micsupp_of_get_pdata(pdata, &config,
281 if (pdata->init_data)
282 config.init_data = pdata->init_data;
284 config.init_data = &micsupp->init_data;
286 /* Default to regulated mode */
287 regmap_update_bits(micsupp->regmap, desc->enable_reg, desc->bypass_mask, 0);
289 micsupp->regulator = devm_regulator_register(&pdev->dev,
293 of_node_put(config.of_node);
295 if (IS_ERR(micsupp->regulator)) {
296 ret = PTR_ERR(micsupp->regulator);
297 dev_err(micsupp->dev, "Failed to register mic supply: %d\n",
302 platform_set_drvdata(pdev, micsupp);
307 static int arizona_micsupp_probe(struct platform_device *pdev)
309 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
310 const struct regulator_desc *desc;
311 struct arizona_micsupp *micsupp;
313 micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
317 micsupp->regmap = arizona->regmap;
318 micsupp->dapm = &arizona->dapm;
319 micsupp->dev = arizona->dev;
321 micsupp->supply.supply = "MICVDD";
324 * Since the chip usually supplies itself we provide some
325 * default init_data for it. This will be overridden with
326 * platform data if provided.
328 switch (arizona->type) {
331 desc = &arizona_micsupp_ext;
332 micsupp->init_data = arizona_micsupp_ext_default;
335 desc = &arizona_micsupp;
336 micsupp->init_data = arizona_micsupp_default;
340 return arizona_micsupp_common_init(pdev, micsupp, desc,
341 &arizona->pdata.micvdd);
344 static int madera_micsupp_probe(struct platform_device *pdev)
346 struct madera *madera = dev_get_drvdata(pdev->dev.parent);
347 struct arizona_micsupp *micsupp;
349 micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
353 micsupp->regmap = madera->regmap;
354 micsupp->dapm = &madera->dapm;
355 micsupp->dev = madera->dev;
356 micsupp->init_data = arizona_micsupp_ext_default;
358 micsupp->supply.supply = "MICVDD";
360 return arizona_micsupp_common_init(pdev, micsupp, &madera_micsupp,
361 &madera->pdata.micvdd);
364 static struct platform_driver arizona_micsupp_driver = {
365 .probe = arizona_micsupp_probe,
367 .name = "arizona-micsupp",
371 static struct platform_driver madera_micsupp_driver = {
372 .probe = madera_micsupp_probe,
374 .name = "madera-micsupp",
378 static struct platform_driver * const arizona_micsupp_drivers[] = {
379 &arizona_micsupp_driver,
380 &madera_micsupp_driver,
383 static int __init arizona_micsupp_init(void)
385 return platform_register_drivers(arizona_micsupp_drivers,
386 ARRAY_SIZE(arizona_micsupp_drivers));
388 module_init(arizona_micsupp_init);
390 static void __exit arizona_micsupp_exit(void)
392 platform_unregister_drivers(arizona_micsupp_drivers,
393 ARRAY_SIZE(arizona_micsupp_drivers));
395 module_exit(arizona_micsupp_exit);
397 /* Module information */
398 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
399 MODULE_DESCRIPTION("Arizona microphone supply driver");
400 MODULE_LICENSE("GPL");
401 MODULE_ALIAS("platform:arizona-micsupp");
402 MODULE_ALIAS("platform:madera-micsupp");