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 #include <linux/regulator/arizona-micsupp.h>
35 struct arizona_micsupp {
36 struct regulator_dev *regulator;
37 struct arizona *arizona;
39 struct regulator_consumer_supply supply;
40 struct regulator_init_data init_data;
42 struct work_struct check_cp_work;
45 static void arizona_micsupp_check_cp(struct work_struct *work)
47 struct arizona_micsupp *micsupp =
48 container_of(work, struct arizona_micsupp, check_cp_work);
49 struct snd_soc_dapm_context *dapm = micsupp->arizona->dapm;
50 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
51 struct arizona *arizona = micsupp->arizona;
52 struct regmap *regmap = arizona->regmap;
56 ret = regmap_read(regmap, ARIZONA_MIC_CHARGE_PUMP_1, ®);
58 dev_err(arizona->dev, "Failed to read CP state: %d\n", ret);
63 if ((reg & (ARIZONA_CPMIC_ENA | ARIZONA_CPMIC_BYPASS)) ==
65 snd_soc_component_force_enable_pin(component,
68 snd_soc_component_disable_pin(component, "MICSUPP");
70 snd_soc_dapm_sync(dapm);
74 static int arizona_micsupp_enable(struct regulator_dev *rdev)
76 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
79 ret = regulator_enable_regmap(rdev);
82 schedule_work(&micsupp->check_cp_work);
87 static int arizona_micsupp_disable(struct regulator_dev *rdev)
89 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
92 ret = regulator_disable_regmap(rdev);
94 schedule_work(&micsupp->check_cp_work);
99 static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena)
101 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
104 ret = regulator_set_bypass_regmap(rdev, ena);
106 schedule_work(&micsupp->check_cp_work);
111 static const struct regulator_ops arizona_micsupp_ops = {
112 .enable = arizona_micsupp_enable,
113 .disable = arizona_micsupp_disable,
114 .is_enabled = regulator_is_enabled_regmap,
116 .list_voltage = regulator_list_voltage_linear_range,
117 .map_voltage = regulator_map_voltage_linear_range,
119 .get_voltage_sel = regulator_get_voltage_sel_regmap,
120 .set_voltage_sel = regulator_set_voltage_sel_regmap,
122 .get_bypass = regulator_get_bypass_regmap,
123 .set_bypass = arizona_micsupp_set_bypass,
126 static const struct regulator_linear_range arizona_micsupp_ranges[] = {
127 REGULATOR_LINEAR_RANGE(1700000, 0, 0x1e, 50000),
128 REGULATOR_LINEAR_RANGE(3300000, 0x1f, 0x1f, 0),
131 static const struct regulator_desc arizona_micsupp = {
133 .supply_name = "CPVDD",
134 .type = REGULATOR_VOLTAGE,
136 .ops = &arizona_micsupp_ops,
138 .vsel_reg = ARIZONA_LDO2_CONTROL_1,
139 .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
140 .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
141 .enable_mask = ARIZONA_CPMIC_ENA,
142 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
143 .bypass_mask = ARIZONA_CPMIC_BYPASS,
145 .linear_ranges = arizona_micsupp_ranges,
146 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ranges),
150 .owner = THIS_MODULE,
153 static const struct regulator_linear_range arizona_micsupp_ext_ranges[] = {
154 REGULATOR_LINEAR_RANGE(900000, 0, 0x14, 25000),
155 REGULATOR_LINEAR_RANGE(1500000, 0x15, 0x27, 100000),
158 static const struct regulator_desc arizona_micsupp_ext = {
160 .supply_name = "CPVDD",
161 .type = REGULATOR_VOLTAGE,
163 .ops = &arizona_micsupp_ops,
165 .vsel_reg = ARIZONA_LDO2_CONTROL_1,
166 .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
167 .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
168 .enable_mask = ARIZONA_CPMIC_ENA,
169 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
170 .bypass_mask = ARIZONA_CPMIC_BYPASS,
172 .linear_ranges = arizona_micsupp_ext_ranges,
173 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ext_ranges),
177 .owner = THIS_MODULE,
180 static const struct regulator_init_data arizona_micsupp_default = {
182 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
183 REGULATOR_CHANGE_VOLTAGE |
184 REGULATOR_CHANGE_BYPASS,
189 .num_consumer_supplies = 1,
192 static const struct regulator_init_data arizona_micsupp_ext_default = {
194 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
195 REGULATOR_CHANGE_VOLTAGE |
196 REGULATOR_CHANGE_BYPASS,
201 .num_consumer_supplies = 1,
204 static int arizona_micsupp_of_get_pdata(struct arizona_micsupp_pdata *pdata,
205 struct regulator_config *config,
206 const struct regulator_desc *desc)
208 struct arizona_micsupp *micsupp = config->driver_data;
209 struct device_node *np;
210 struct regulator_init_data *init_data;
212 np = of_get_child_by_name(config->dev->of_node, "micvdd");
215 config->of_node = np;
217 init_data = of_get_regulator_init_data(config->dev, np, desc);
220 init_data->consumer_supplies = &micsupp->supply;
221 init_data->num_consumer_supplies = 1;
223 pdata->init_data = init_data;
230 static int arizona_micsupp_probe(struct platform_device *pdev)
232 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
233 const struct regulator_desc *desc;
234 struct regulator_config config = { };
235 struct arizona_micsupp_pdata *pdata = &arizona->pdata.micvdd;
236 struct arizona_micsupp *micsupp;
239 micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
243 micsupp->arizona = arizona;
244 INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
247 * Since the chip usually supplies itself we provide some
248 * default init_data for it. This will be overridden with
249 * platform data if provided.
251 switch (arizona->type) {
254 desc = &arizona_micsupp_ext;
255 micsupp->init_data = arizona_micsupp_ext_default;
258 desc = &arizona_micsupp;
259 micsupp->init_data = arizona_micsupp_default;
263 micsupp->init_data.consumer_supplies = &micsupp->supply;
264 micsupp->supply.supply = "MICVDD";
265 micsupp->supply.dev_name = dev_name(arizona->dev);
267 config.dev = arizona->dev;
268 config.driver_data = micsupp;
269 config.regmap = arizona->regmap;
271 if (IS_ENABLED(CONFIG_OF)) {
272 if (!dev_get_platdata(arizona->dev)) {
273 ret = arizona_micsupp_of_get_pdata(pdata, &config,
280 if (pdata->init_data)
281 config.init_data = pdata->init_data;
283 config.init_data = &micsupp->init_data;
285 /* Default to regulated mode until the API supports bypass */
286 regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1,
287 ARIZONA_CPMIC_BYPASS, 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(arizona->dev, "Failed to register mic supply: %d\n",
302 platform_set_drvdata(pdev, micsupp);
307 static struct platform_driver arizona_micsupp_driver = {
308 .probe = arizona_micsupp_probe,
310 .name = "arizona-micsupp",
314 module_platform_driver(arizona_micsupp_driver);
316 /* Module information */
317 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
318 MODULE_DESCRIPTION("Arizona microphone supply driver");
319 MODULE_LICENSE("GPL");
320 MODULE_ALIAS("platform:arizona-micsupp");