2 * arizona-micsupp.c -- Microphone supply for Arizona devices
4 * Copyright 2012 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/bitops.h>
18 #include <linux/err.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/gpio.h>
25 #include <linux/slab.h>
26 #include <linux/workqueue.h>
27 #include <sound/soc.h>
29 #include <linux/mfd/arizona/core.h>
30 #include <linux/mfd/arizona/pdata.h>
31 #include <linux/mfd/arizona/registers.h>
33 struct arizona_micsupp
{
34 struct regulator_dev
*regulator
;
35 struct arizona
*arizona
;
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
->arizona
->dapm
;
48 struct snd_soc_component
*component
= snd_soc_dapm_to_component(dapm
);
49 struct arizona
*arizona
= micsupp
->arizona
;
50 struct regmap
*regmap
= arizona
->regmap
;
54 ret
= regmap_read(regmap
, ARIZONA_MIC_CHARGE_PUMP_1
, ®
);
56 dev_err(arizona
->dev
, "Failed to read CP state: %d\n", ret
);
61 if ((reg
& (ARIZONA_CPMIC_ENA
| ARIZONA_CPMIC_BYPASS
)) ==
63 snd_soc_component_force_enable_pin(component
,
66 snd_soc_component_disable_pin(component
, "MICSUPP");
68 snd_soc_dapm_sync(dapm
);
72 static int arizona_micsupp_enable(struct regulator_dev
*rdev
)
74 struct arizona_micsupp
*micsupp
= rdev_get_drvdata(rdev
);
77 ret
= regulator_enable_regmap(rdev
);
80 schedule_work(&micsupp
->check_cp_work
);
85 static int arizona_micsupp_disable(struct regulator_dev
*rdev
)
87 struct arizona_micsupp
*micsupp
= rdev_get_drvdata(rdev
);
90 ret
= regulator_disable_regmap(rdev
);
92 schedule_work(&micsupp
->check_cp_work
);
97 static int arizona_micsupp_set_bypass(struct regulator_dev
*rdev
, bool ena
)
99 struct arizona_micsupp
*micsupp
= rdev_get_drvdata(rdev
);
102 ret
= regulator_set_bypass_regmap(rdev
, ena
);
104 schedule_work(&micsupp
->check_cp_work
);
109 static const struct regulator_ops arizona_micsupp_ops
= {
110 .enable
= arizona_micsupp_enable
,
111 .disable
= arizona_micsupp_disable
,
112 .is_enabled
= regulator_is_enabled_regmap
,
114 .list_voltage
= regulator_list_voltage_linear_range
,
115 .map_voltage
= regulator_map_voltage_linear_range
,
117 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
118 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
120 .get_bypass
= regulator_get_bypass_regmap
,
121 .set_bypass
= arizona_micsupp_set_bypass
,
124 static const struct regulator_linear_range arizona_micsupp_ranges
[] = {
125 REGULATOR_LINEAR_RANGE(1700000, 0, 0x1e, 50000),
126 REGULATOR_LINEAR_RANGE(3300000, 0x1f, 0x1f, 0),
129 static const struct regulator_desc arizona_micsupp
= {
131 .supply_name
= "CPVDD",
132 .type
= REGULATOR_VOLTAGE
,
134 .ops
= &arizona_micsupp_ops
,
136 .vsel_reg
= ARIZONA_LDO2_CONTROL_1
,
137 .vsel_mask
= ARIZONA_LDO2_VSEL_MASK
,
138 .enable_reg
= ARIZONA_MIC_CHARGE_PUMP_1
,
139 .enable_mask
= ARIZONA_CPMIC_ENA
,
140 .bypass_reg
= ARIZONA_MIC_CHARGE_PUMP_1
,
141 .bypass_mask
= ARIZONA_CPMIC_BYPASS
,
143 .linear_ranges
= arizona_micsupp_ranges
,
144 .n_linear_ranges
= ARRAY_SIZE(arizona_micsupp_ranges
),
148 .owner
= THIS_MODULE
,
151 static const struct regulator_linear_range arizona_micsupp_ext_ranges
[] = {
152 REGULATOR_LINEAR_RANGE(900000, 0, 0x14, 25000),
153 REGULATOR_LINEAR_RANGE(1500000, 0x15, 0x27, 100000),
156 static const struct regulator_desc arizona_micsupp_ext
= {
158 .supply_name
= "CPVDD",
159 .type
= REGULATOR_VOLTAGE
,
161 .ops
= &arizona_micsupp_ops
,
163 .vsel_reg
= ARIZONA_LDO2_CONTROL_1
,
164 .vsel_mask
= ARIZONA_LDO2_VSEL_MASK
,
165 .enable_reg
= ARIZONA_MIC_CHARGE_PUMP_1
,
166 .enable_mask
= ARIZONA_CPMIC_ENA
,
167 .bypass_reg
= ARIZONA_MIC_CHARGE_PUMP_1
,
168 .bypass_mask
= ARIZONA_CPMIC_BYPASS
,
170 .linear_ranges
= arizona_micsupp_ext_ranges
,
171 .n_linear_ranges
= ARRAY_SIZE(arizona_micsupp_ext_ranges
),
175 .owner
= THIS_MODULE
,
178 static const struct regulator_init_data arizona_micsupp_default
= {
180 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
|
181 REGULATOR_CHANGE_VOLTAGE
|
182 REGULATOR_CHANGE_BYPASS
,
187 .num_consumer_supplies
= 1,
190 static const struct regulator_init_data arizona_micsupp_ext_default
= {
192 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
|
193 REGULATOR_CHANGE_VOLTAGE
|
194 REGULATOR_CHANGE_BYPASS
,
199 .num_consumer_supplies
= 1,
202 static int arizona_micsupp_of_get_pdata(struct arizona
*arizona
,
203 struct regulator_config
*config
,
204 const struct regulator_desc
*desc
)
206 struct arizona_pdata
*pdata
= &arizona
->pdata
;
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(arizona
->dev
->of_node
, "micvdd");
214 config
->of_node
= np
;
216 init_data
= of_get_regulator_init_data(arizona
->dev
, np
, desc
);
219 init_data
->consumer_supplies
= &micsupp
->supply
;
220 init_data
->num_consumer_supplies
= 1;
222 pdata
->micvdd
= init_data
;
229 static int arizona_micsupp_probe(struct platform_device
*pdev
)
231 struct arizona
*arizona
= dev_get_drvdata(pdev
->dev
.parent
);
232 const struct regulator_desc
*desc
;
233 struct regulator_config config
= { };
234 struct arizona_micsupp
*micsupp
;
237 micsupp
= devm_kzalloc(&pdev
->dev
, sizeof(*micsupp
), GFP_KERNEL
);
241 micsupp
->arizona
= arizona
;
242 INIT_WORK(&micsupp
->check_cp_work
, arizona_micsupp_check_cp
);
245 * Since the chip usually supplies itself we provide some
246 * default init_data for it. This will be overridden with
247 * platform data if provided.
249 switch (arizona
->type
) {
252 desc
= &arizona_micsupp_ext
;
253 micsupp
->init_data
= arizona_micsupp_ext_default
;
256 desc
= &arizona_micsupp
;
257 micsupp
->init_data
= arizona_micsupp_default
;
261 micsupp
->init_data
.consumer_supplies
= &micsupp
->supply
;
262 micsupp
->supply
.supply
= "MICVDD";
263 micsupp
->supply
.dev_name
= dev_name(arizona
->dev
);
265 config
.dev
= arizona
->dev
;
266 config
.driver_data
= micsupp
;
267 config
.regmap
= arizona
->regmap
;
269 if (IS_ENABLED(CONFIG_OF
)) {
270 if (!dev_get_platdata(arizona
->dev
)) {
271 ret
= arizona_micsupp_of_get_pdata(arizona
, &config
,
278 if (arizona
->pdata
.micvdd
)
279 config
.init_data
= arizona
->pdata
.micvdd
;
281 config
.init_data
= &micsupp
->init_data
;
283 /* Default to regulated mode until the API supports bypass */
284 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_CHARGE_PUMP_1
,
285 ARIZONA_CPMIC_BYPASS
, 0);
287 micsupp
->regulator
= devm_regulator_register(&pdev
->dev
,
291 of_node_put(config
.of_node
);
293 if (IS_ERR(micsupp
->regulator
)) {
294 ret
= PTR_ERR(micsupp
->regulator
);
295 dev_err(arizona
->dev
, "Failed to register mic supply: %d\n",
300 platform_set_drvdata(pdev
, micsupp
);
305 static struct platform_driver arizona_micsupp_driver
= {
306 .probe
= arizona_micsupp_probe
,
308 .name
= "arizona-micsupp",
312 module_platform_driver(arizona_micsupp_driver
);
314 /* Module information */
315 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
316 MODULE_DESCRIPTION("Arizona microphone supply driver");
317 MODULE_LICENSE("GPL");
318 MODULE_ALIAS("platform:arizona-micsupp");