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/gpio.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25 #include <sound/soc.h>
27 #include <linux/mfd/arizona/core.h>
28 #include <linux/mfd/arizona/pdata.h>
29 #include <linux/mfd/arizona/registers.h>
31 #define ARIZONA_MICSUPP_MAX_SELECTOR 0x1f
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 int arizona_micsupp_list_voltage(struct regulator_dev *rdev,
44 unsigned int selector)
46 if (selector > ARIZONA_MICSUPP_MAX_SELECTOR)
49 if (selector == ARIZONA_MICSUPP_MAX_SELECTOR)
52 return (selector * 50000) + 1700000;
55 static int arizona_micsupp_map_voltage(struct regulator_dev *rdev,
56 int min_uV, int max_uV)
65 selector = ARIZONA_MICSUPP_MAX_SELECTOR;
67 selector = DIV_ROUND_UP(min_uV - 1700000, 50000);
72 voltage = arizona_micsupp_list_voltage(rdev, selector);
73 if (voltage < min_uV || voltage > max_uV)
79 static void arizona_micsupp_check_cp(struct work_struct *work)
81 struct arizona_micsupp *micsupp =
82 container_of(work, struct arizona_micsupp, check_cp_work);
83 struct snd_soc_dapm_context *dapm = micsupp->arizona->dapm;
84 struct arizona *arizona = micsupp->arizona;
85 struct regmap *regmap = arizona->regmap;
89 ret = regmap_read(regmap, ARIZONA_MIC_CHARGE_PUMP_1, ®);
91 dev_err(arizona->dev, "Failed to read CP state: %d\n", ret);
96 if ((reg & (ARIZONA_CPMIC_ENA | ARIZONA_CPMIC_BYPASS)) ==
98 snd_soc_dapm_force_enable_pin(dapm, "MICSUPP");
100 snd_soc_dapm_disable_pin(dapm, "MICSUPP");
102 snd_soc_dapm_sync(dapm);
106 static int arizona_micsupp_enable(struct regulator_dev *rdev)
108 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
111 ret = regulator_enable_regmap(rdev);
114 schedule_work(&micsupp->check_cp_work);
119 static int arizona_micsupp_disable(struct regulator_dev *rdev)
121 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
124 ret = regulator_disable_regmap(rdev);
126 schedule_work(&micsupp->check_cp_work);
131 static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena)
133 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
136 ret = regulator_set_bypass_regmap(rdev, ena);
138 schedule_work(&micsupp->check_cp_work);
143 static struct regulator_ops arizona_micsupp_ops = {
144 .enable = arizona_micsupp_enable,
145 .disable = arizona_micsupp_disable,
146 .is_enabled = regulator_is_enabled_regmap,
148 .list_voltage = arizona_micsupp_list_voltage,
149 .map_voltage = arizona_micsupp_map_voltage,
151 .get_voltage_sel = regulator_get_voltage_sel_regmap,
152 .set_voltage_sel = regulator_set_voltage_sel_regmap,
154 .get_bypass = regulator_get_bypass_regmap,
155 .set_bypass = arizona_micsupp_set_bypass,
158 static const struct regulator_desc arizona_micsupp = {
160 .supply_name = "CPVDD",
161 .type = REGULATOR_VOLTAGE,
162 .n_voltages = ARIZONA_MICSUPP_MAX_SELECTOR + 1,
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,
174 .owner = THIS_MODULE,
177 static const struct regulator_linear_range arizona_micsupp_ext_ranges[] = {
178 REGULATOR_LINEAR_RANGE(900000, 0, 0x14, 25000),
179 REGULATOR_LINEAR_RANGE(1500000, 0x15, 0x27, 100000),
182 static const struct regulator_desc arizona_micsupp_ext = {
184 .supply_name = "CPVDD",
185 .type = REGULATOR_VOLTAGE,
187 .ops = &arizona_micsupp_ops,
189 .vsel_reg = ARIZONA_LDO2_CONTROL_1,
190 .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
191 .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
192 .enable_mask = ARIZONA_CPMIC_ENA,
193 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
194 .bypass_mask = ARIZONA_CPMIC_BYPASS,
196 .linear_ranges = arizona_micsupp_ext_ranges,
197 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ext_ranges),
201 .owner = THIS_MODULE,
204 static const struct regulator_init_data arizona_micsupp_default = {
206 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
207 REGULATOR_CHANGE_VOLTAGE |
208 REGULATOR_CHANGE_BYPASS,
213 .num_consumer_supplies = 1,
216 static const struct regulator_init_data arizona_micsupp_ext_default = {
218 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
219 REGULATOR_CHANGE_VOLTAGE |
220 REGULATOR_CHANGE_BYPASS,
225 .num_consumer_supplies = 1,
228 static int arizona_micsupp_probe(struct platform_device *pdev)
230 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
231 const struct regulator_desc *desc;
232 struct regulator_config config = { };
233 struct arizona_micsupp *micsupp;
236 micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
237 if (micsupp == NULL) {
238 dev_err(&pdev->dev, "Unable to allocate private data\n");
242 micsupp->arizona = arizona;
243 INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
246 * Since the chip usually supplies itself we provide some
247 * default init_data for it. This will be overridden with
248 * platform data if provided.
250 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 (arizona->pdata.micvdd)
270 config.init_data = arizona->pdata.micvdd;
272 config.init_data = &micsupp->init_data;
274 /* Default to regulated mode until the API supports bypass */
275 regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1,
276 ARIZONA_CPMIC_BYPASS, 0);
278 micsupp->regulator = devm_regulator_register(&pdev->dev,
281 if (IS_ERR(micsupp->regulator)) {
282 ret = PTR_ERR(micsupp->regulator);
283 dev_err(arizona->dev, "Failed to register mic supply: %d\n",
288 platform_set_drvdata(pdev, micsupp);
293 static struct platform_driver arizona_micsupp_driver = {
294 .probe = arizona_micsupp_probe,
296 .name = "arizona-micsupp",
297 .owner = THIS_MODULE,
301 module_platform_driver(arizona_micsupp_driver);
303 /* Module information */
304 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
305 MODULE_DESCRIPTION("Arizona microphone supply driver");
306 MODULE_LICENSE("GPL");
307 MODULE_ALIAS("platform:arizona-micsupp");