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>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/of_regulator.h>
23 #include <linux/gpio.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <sound/soc.h>
28 #include <linux/mfd/arizona/core.h>
29 #include <linux/mfd/arizona/pdata.h>
30 #include <linux/mfd/arizona/registers.h>
32 struct arizona_micsupp {
33 struct regulator_dev *regulator;
34 struct arizona *arizona;
36 struct regulator_consumer_supply supply;
37 struct regulator_init_data init_data;
39 struct work_struct check_cp_work;
42 static void arizona_micsupp_check_cp(struct work_struct *work)
44 struct arizona_micsupp *micsupp =
45 container_of(work, struct arizona_micsupp, check_cp_work);
46 struct snd_soc_dapm_context *dapm = micsupp->arizona->dapm;
47 struct arizona *arizona = micsupp->arizona;
48 struct regmap *regmap = arizona->regmap;
52 ret = regmap_read(regmap, ARIZONA_MIC_CHARGE_PUMP_1, ®);
54 dev_err(arizona->dev, "Failed to read CP state: %d\n", ret);
59 if ((reg & (ARIZONA_CPMIC_ENA | ARIZONA_CPMIC_BYPASS)) ==
61 snd_soc_dapm_force_enable_pin(dapm, "MICSUPP");
63 snd_soc_dapm_disable_pin(dapm, "MICSUPP");
65 snd_soc_dapm_sync(dapm);
69 static int arizona_micsupp_enable(struct regulator_dev *rdev)
71 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
74 ret = regulator_enable_regmap(rdev);
77 schedule_work(&micsupp->check_cp_work);
82 static int arizona_micsupp_disable(struct regulator_dev *rdev)
84 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
87 ret = regulator_disable_regmap(rdev);
89 schedule_work(&micsupp->check_cp_work);
94 static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena)
96 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
99 ret = regulator_set_bypass_regmap(rdev, ena);
101 schedule_work(&micsupp->check_cp_work);
106 static struct regulator_ops arizona_micsupp_ops = {
107 .enable = arizona_micsupp_enable,
108 .disable = arizona_micsupp_disable,
109 .is_enabled = regulator_is_enabled_regmap,
111 .list_voltage = regulator_list_voltage_linear_range,
112 .map_voltage = regulator_map_voltage_linear_range,
114 .get_voltage_sel = regulator_get_voltage_sel_regmap,
115 .set_voltage_sel = regulator_set_voltage_sel_regmap,
117 .get_bypass = regulator_get_bypass_regmap,
118 .set_bypass = arizona_micsupp_set_bypass,
121 static const struct regulator_linear_range arizona_micsupp_ranges[] = {
122 REGULATOR_LINEAR_RANGE(1700000, 0, 0x1e, 50000),
123 REGULATOR_LINEAR_RANGE(3300000, 0x1f, 0x1f, 0),
126 static const struct regulator_desc arizona_micsupp = {
128 .supply_name = "CPVDD",
129 .type = REGULATOR_VOLTAGE,
131 .ops = &arizona_micsupp_ops,
133 .vsel_reg = ARIZONA_LDO2_CONTROL_1,
134 .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
135 .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
136 .enable_mask = ARIZONA_CPMIC_ENA,
137 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
138 .bypass_mask = ARIZONA_CPMIC_BYPASS,
140 .linear_ranges = arizona_micsupp_ranges,
141 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ranges),
145 .owner = THIS_MODULE,
148 static const struct regulator_linear_range arizona_micsupp_ext_ranges[] = {
149 REGULATOR_LINEAR_RANGE(900000, 0, 0x14, 25000),
150 REGULATOR_LINEAR_RANGE(1500000, 0x15, 0x27, 100000),
153 static const struct regulator_desc arizona_micsupp_ext = {
155 .supply_name = "CPVDD",
156 .type = REGULATOR_VOLTAGE,
158 .ops = &arizona_micsupp_ops,
160 .vsel_reg = ARIZONA_LDO2_CONTROL_1,
161 .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
162 .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
163 .enable_mask = ARIZONA_CPMIC_ENA,
164 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
165 .bypass_mask = ARIZONA_CPMIC_BYPASS,
167 .linear_ranges = arizona_micsupp_ext_ranges,
168 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ext_ranges),
172 .owner = THIS_MODULE,
175 static const struct regulator_init_data arizona_micsupp_default = {
177 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
178 REGULATOR_CHANGE_VOLTAGE |
179 REGULATOR_CHANGE_BYPASS,
184 .num_consumer_supplies = 1,
187 static const struct regulator_init_data arizona_micsupp_ext_default = {
189 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
190 REGULATOR_CHANGE_VOLTAGE |
191 REGULATOR_CHANGE_BYPASS,
196 .num_consumer_supplies = 1,
199 static int arizona_micsupp_of_get_pdata(struct arizona *arizona,
200 struct regulator_config *config)
202 struct arizona_pdata *pdata = &arizona->pdata;
203 struct arizona_micsupp *micsupp = config->driver_data;
204 struct device_node *np;
205 struct regulator_init_data *init_data;
207 np = of_get_child_by_name(arizona->dev->of_node, "micvdd");
210 config->of_node = np;
212 init_data = of_get_regulator_init_data(arizona->dev, np);
215 init_data->consumer_supplies = &micsupp->supply;
216 init_data->num_consumer_supplies = 1;
218 pdata->micvdd = init_data;
225 static int arizona_micsupp_probe(struct platform_device *pdev)
227 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
228 const struct regulator_desc *desc;
229 struct regulator_config config = { };
230 struct arizona_micsupp *micsupp;
233 micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
237 micsupp->arizona = arizona;
238 INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
241 * Since the chip usually supplies itself we provide some
242 * default init_data for it. This will be overridden with
243 * platform data if provided.
245 switch (arizona->type) {
247 desc = &arizona_micsupp_ext;
248 micsupp->init_data = arizona_micsupp_ext_default;
251 desc = &arizona_micsupp;
252 micsupp->init_data = arizona_micsupp_default;
256 micsupp->init_data.consumer_supplies = &micsupp->supply;
257 micsupp->supply.supply = "MICVDD";
258 micsupp->supply.dev_name = dev_name(arizona->dev);
260 config.dev = arizona->dev;
261 config.driver_data = micsupp;
262 config.regmap = arizona->regmap;
264 if (IS_ENABLED(CONFIG_OF)) {
265 if (!dev_get_platdata(arizona->dev)) {
266 ret = arizona_micsupp_of_get_pdata(arizona, &config);
272 if (arizona->pdata.micvdd)
273 config.init_data = arizona->pdata.micvdd;
275 config.init_data = &micsupp->init_data;
277 /* Default to regulated mode until the API supports bypass */
278 regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1,
279 ARIZONA_CPMIC_BYPASS, 0);
281 micsupp->regulator = devm_regulator_register(&pdev->dev,
284 if (IS_ERR(micsupp->regulator)) {
285 ret = PTR_ERR(micsupp->regulator);
286 dev_err(arizona->dev, "Failed to register mic supply: %d\n",
291 of_node_put(config.of_node);
293 platform_set_drvdata(pdev, micsupp);
298 static struct platform_driver arizona_micsupp_driver = {
299 .probe = arizona_micsupp_probe,
301 .name = "arizona-micsupp",
302 .owner = THIS_MODULE,
306 module_platform_driver(arizona_micsupp_driver);
308 /* Module information */
309 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
310 MODULE_DESCRIPTION("Arizona microphone supply driver");
311 MODULE_LICENSE("GPL");
312 MODULE_ALIAS("platform:arizona-micsupp");