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/gpio.h>
20 #include <linux/slab.h>
21 #include <linux/workqueue.h>
22 #include <sound/soc.h>
24 #include <linux/mfd/arizona/core.h>
25 #include <linux/mfd/arizona/pdata.h>
26 #include <linux/mfd/arizona/registers.h>
28 #include <linux/regulator/arizona-micsupp.h>
30 struct arizona_micsupp
{
31 struct regulator_dev
*regulator
;
32 struct regmap
*regmap
;
33 struct snd_soc_dapm_context
**dapm
;
34 unsigned int enable_reg
;
37 struct regulator_consumer_supply supply
;
38 struct regulator_init_data init_data
;
40 struct work_struct check_cp_work
;
43 static void arizona_micsupp_check_cp(struct work_struct
*work
)
45 struct arizona_micsupp
*micsupp
=
46 container_of(work
, struct arizona_micsupp
, check_cp_work
);
47 struct snd_soc_dapm_context
*dapm
= *micsupp
->dapm
;
48 struct snd_soc_component
*component
;
52 ret
= regmap_read(micsupp
->regmap
, micsupp
->enable_reg
, &val
);
55 "Failed to read CP state: %d\n", ret
);
60 component
= snd_soc_dapm_to_component(dapm
);
62 if ((val
& (ARIZONA_CPMIC_ENA
| ARIZONA_CPMIC_BYPASS
)) ==
64 snd_soc_component_force_enable_pin(component
,
67 snd_soc_component_disable_pin(component
, "MICSUPP");
69 snd_soc_dapm_sync(dapm
);
73 static int arizona_micsupp_enable(struct regulator_dev
*rdev
)
75 struct arizona_micsupp
*micsupp
= rdev_get_drvdata(rdev
);
78 ret
= regulator_enable_regmap(rdev
);
81 schedule_work(&micsupp
->check_cp_work
);
86 static int arizona_micsupp_disable(struct regulator_dev
*rdev
)
88 struct arizona_micsupp
*micsupp
= rdev_get_drvdata(rdev
);
91 ret
= regulator_disable_regmap(rdev
);
93 schedule_work(&micsupp
->check_cp_work
);
98 static int arizona_micsupp_set_bypass(struct regulator_dev
*rdev
, bool ena
)
100 struct arizona_micsupp
*micsupp
= rdev_get_drvdata(rdev
);
103 ret
= regulator_set_bypass_regmap(rdev
, ena
);
105 schedule_work(&micsupp
->check_cp_work
);
110 static const struct regulator_ops arizona_micsupp_ops
= {
111 .enable
= arizona_micsupp_enable
,
112 .disable
= arizona_micsupp_disable
,
113 .is_enabled
= regulator_is_enabled_regmap
,
115 .list_voltage
= regulator_list_voltage_linear_range
,
116 .map_voltage
= regulator_map_voltage_linear_range
,
118 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
119 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
121 .get_bypass
= regulator_get_bypass_regmap
,
122 .set_bypass
= arizona_micsupp_set_bypass
,
125 static const struct regulator_linear_range arizona_micsupp_ranges
[] = {
126 REGULATOR_LINEAR_RANGE(1700000, 0, 0x1e, 50000),
127 REGULATOR_LINEAR_RANGE(3300000, 0x1f, 0x1f, 0),
130 static const struct regulator_desc arizona_micsupp
= {
132 .supply_name
= "CPVDD",
133 .type
= REGULATOR_VOLTAGE
,
135 .ops
= &arizona_micsupp_ops
,
137 .vsel_reg
= ARIZONA_LDO2_CONTROL_1
,
138 .vsel_mask
= ARIZONA_LDO2_VSEL_MASK
,
139 .enable_reg
= ARIZONA_MIC_CHARGE_PUMP_1
,
140 .enable_mask
= ARIZONA_CPMIC_ENA
,
141 .bypass_reg
= ARIZONA_MIC_CHARGE_PUMP_1
,
142 .bypass_mask
= ARIZONA_CPMIC_BYPASS
,
144 .linear_ranges
= arizona_micsupp_ranges
,
145 .n_linear_ranges
= ARRAY_SIZE(arizona_micsupp_ranges
),
149 .owner
= THIS_MODULE
,
152 static const struct regulator_linear_range arizona_micsupp_ext_ranges
[] = {
153 REGULATOR_LINEAR_RANGE(900000, 0, 0x14, 25000),
154 REGULATOR_LINEAR_RANGE(1500000, 0x15, 0x27, 100000),
157 static const struct regulator_desc arizona_micsupp_ext
= {
159 .supply_name
= "CPVDD",
160 .type
= REGULATOR_VOLTAGE
,
162 .ops
= &arizona_micsupp_ops
,
164 .vsel_reg
= ARIZONA_LDO2_CONTROL_1
,
165 .vsel_mask
= ARIZONA_LDO2_VSEL_MASK
,
166 .enable_reg
= ARIZONA_MIC_CHARGE_PUMP_1
,
167 .enable_mask
= ARIZONA_CPMIC_ENA
,
168 .bypass_reg
= ARIZONA_MIC_CHARGE_PUMP_1
,
169 .bypass_mask
= ARIZONA_CPMIC_BYPASS
,
171 .linear_ranges
= arizona_micsupp_ext_ranges
,
172 .n_linear_ranges
= ARRAY_SIZE(arizona_micsupp_ext_ranges
),
176 .owner
= THIS_MODULE
,
179 static const struct regulator_init_data arizona_micsupp_default
= {
181 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
|
182 REGULATOR_CHANGE_VOLTAGE
|
183 REGULATOR_CHANGE_BYPASS
,
188 .num_consumer_supplies
= 1,
191 static const struct regulator_init_data arizona_micsupp_ext_default
= {
193 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
|
194 REGULATOR_CHANGE_VOLTAGE
|
195 REGULATOR_CHANGE_BYPASS
,
200 .num_consumer_supplies
= 1,
203 static int arizona_micsupp_of_get_pdata(struct arizona_micsupp_pdata
*pdata
,
204 struct regulator_config
*config
,
205 const struct regulator_desc
*desc
)
207 struct arizona_micsupp
*micsupp
= config
->driver_data
;
208 struct device_node
*np
;
209 struct regulator_init_data
*init_data
;
211 np
= of_get_child_by_name(config
->dev
->of_node
, "micvdd");
214 config
->of_node
= np
;
216 init_data
= of_get_regulator_init_data(config
->dev
, np
, desc
);
219 init_data
->consumer_supplies
= &micsupp
->supply
;
220 init_data
->num_consumer_supplies
= 1;
222 pdata
->init_data
= init_data
;
229 static int arizona_micsupp_common_init(struct platform_device
*pdev
,
230 struct arizona_micsupp
*micsupp
,
231 const struct regulator_desc
*desc
,
232 struct arizona_micsupp_pdata
*pdata
)
234 struct regulator_config config
= { };
237 INIT_WORK(&micsupp
->check_cp_work
, arizona_micsupp_check_cp
);
239 micsupp
->init_data
.consumer_supplies
= &micsupp
->supply
;
240 micsupp
->supply
.supply
= "MICVDD";
241 micsupp
->supply
.dev_name
= dev_name(micsupp
->dev
);
242 micsupp
->enable_reg
= desc
->enable_reg
;
244 config
.dev
= micsupp
->dev
;
245 config
.driver_data
= micsupp
;
246 config
.regmap
= micsupp
->regmap
;
248 if (IS_ENABLED(CONFIG_OF
)) {
249 if (!dev_get_platdata(micsupp
->dev
)) {
250 ret
= arizona_micsupp_of_get_pdata(pdata
, &config
,
257 if (pdata
->init_data
)
258 config
.init_data
= pdata
->init_data
;
260 config
.init_data
= &micsupp
->init_data
;
262 /* Default to regulated mode */
263 regmap_update_bits(micsupp
->regmap
, micsupp
->enable_reg
,
264 ARIZONA_CPMIC_BYPASS
, 0);
266 micsupp
->regulator
= devm_regulator_register(&pdev
->dev
,
270 of_node_put(config
.of_node
);
272 if (IS_ERR(micsupp
->regulator
)) {
273 ret
= PTR_ERR(micsupp
->regulator
);
274 dev_err(micsupp
->dev
, "Failed to register mic supply: %d\n",
279 platform_set_drvdata(pdev
, micsupp
);
284 static int arizona_micsupp_probe(struct platform_device
*pdev
)
286 struct arizona
*arizona
= dev_get_drvdata(pdev
->dev
.parent
);
287 const struct regulator_desc
*desc
;
288 struct arizona_micsupp
*micsupp
;
290 micsupp
= devm_kzalloc(&pdev
->dev
, sizeof(*micsupp
), GFP_KERNEL
);
294 micsupp
->regmap
= arizona
->regmap
;
295 micsupp
->dapm
= &arizona
->dapm
;
296 micsupp
->dev
= arizona
->dev
;
299 * Since the chip usually supplies itself we provide some
300 * default init_data for it. This will be overridden with
301 * platform data if provided.
303 switch (arizona
->type
) {
306 desc
= &arizona_micsupp_ext
;
307 micsupp
->init_data
= arizona_micsupp_ext_default
;
310 desc
= &arizona_micsupp
;
311 micsupp
->init_data
= arizona_micsupp_default
;
315 return arizona_micsupp_common_init(pdev
, micsupp
, desc
,
316 &arizona
->pdata
.micvdd
);
319 static struct platform_driver arizona_micsupp_driver
= {
320 .probe
= arizona_micsupp_probe
,
322 .name
= "arizona-micsupp",
326 module_platform_driver(arizona_micsupp_driver
);
328 /* Module information */
329 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
330 MODULE_DESCRIPTION("Arizona microphone supply driver");
331 MODULE_LICENSE("GPL");
332 MODULE_ALIAS("platform:arizona-micsupp");