PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / regulator / arizona-micsupp.c
blob034ece7070838991a59d45f88f0e0ace95c6abba
1 /*
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 struct arizona_micsupp {
32 struct regulator_dev *regulator;
33 struct arizona *arizona;
35 struct regulator_consumer_supply supply;
36 struct regulator_init_data init_data;
38 struct work_struct check_cp_work;
41 static void arizona_micsupp_check_cp(struct work_struct *work)
43 struct arizona_micsupp *micsupp =
44 container_of(work, struct arizona_micsupp, check_cp_work);
45 struct snd_soc_dapm_context *dapm = micsupp->arizona->dapm;
46 struct arizona *arizona = micsupp->arizona;
47 struct regmap *regmap = arizona->regmap;
48 unsigned int reg;
49 int ret;
51 ret = regmap_read(regmap, ARIZONA_MIC_CHARGE_PUMP_1, &reg);
52 if (ret != 0) {
53 dev_err(arizona->dev, "Failed to read CP state: %d\n", ret);
54 return;
57 if (dapm) {
58 if ((reg & (ARIZONA_CPMIC_ENA | ARIZONA_CPMIC_BYPASS)) ==
59 ARIZONA_CPMIC_ENA)
60 snd_soc_dapm_force_enable_pin(dapm, "MICSUPP");
61 else
62 snd_soc_dapm_disable_pin(dapm, "MICSUPP");
64 snd_soc_dapm_sync(dapm);
68 static int arizona_micsupp_enable(struct regulator_dev *rdev)
70 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
71 int ret;
73 ret = regulator_enable_regmap(rdev);
75 if (ret == 0)
76 schedule_work(&micsupp->check_cp_work);
78 return ret;
81 static int arizona_micsupp_disable(struct regulator_dev *rdev)
83 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
84 int ret;
86 ret = regulator_disable_regmap(rdev);
87 if (ret == 0)
88 schedule_work(&micsupp->check_cp_work);
90 return ret;
93 static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena)
95 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
96 int ret;
98 ret = regulator_set_bypass_regmap(rdev, ena);
99 if (ret == 0)
100 schedule_work(&micsupp->check_cp_work);
102 return ret;
105 static struct regulator_ops arizona_micsupp_ops = {
106 .enable = arizona_micsupp_enable,
107 .disable = arizona_micsupp_disable,
108 .is_enabled = regulator_is_enabled_regmap,
110 .list_voltage = regulator_list_voltage_linear_range,
111 .map_voltage = regulator_map_voltage_linear_range,
113 .get_voltage_sel = regulator_get_voltage_sel_regmap,
114 .set_voltage_sel = regulator_set_voltage_sel_regmap,
116 .get_bypass = regulator_get_bypass_regmap,
117 .set_bypass = arizona_micsupp_set_bypass,
120 static const struct regulator_linear_range arizona_micsupp_ranges[] = {
121 REGULATOR_LINEAR_RANGE(1700000, 0, 0x1e, 50000),
122 REGULATOR_LINEAR_RANGE(3300000, 0x1f, 0x1f, 0),
125 static const struct regulator_desc arizona_micsupp = {
126 .name = "MICVDD",
127 .supply_name = "CPVDD",
128 .type = REGULATOR_VOLTAGE,
129 .n_voltages = 32,
130 .ops = &arizona_micsupp_ops,
132 .vsel_reg = ARIZONA_LDO2_CONTROL_1,
133 .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
134 .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
135 .enable_mask = ARIZONA_CPMIC_ENA,
136 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
137 .bypass_mask = ARIZONA_CPMIC_BYPASS,
139 .linear_ranges = arizona_micsupp_ranges,
140 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ranges),
142 .enable_time = 3000,
144 .owner = THIS_MODULE,
147 static const struct regulator_linear_range arizona_micsupp_ext_ranges[] = {
148 REGULATOR_LINEAR_RANGE(900000, 0, 0x14, 25000),
149 REGULATOR_LINEAR_RANGE(1500000, 0x15, 0x27, 100000),
152 static const struct regulator_desc arizona_micsupp_ext = {
153 .name = "MICVDD",
154 .supply_name = "CPVDD",
155 .type = REGULATOR_VOLTAGE,
156 .n_voltages = 40,
157 .ops = &arizona_micsupp_ops,
159 .vsel_reg = ARIZONA_LDO2_CONTROL_1,
160 .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
161 .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
162 .enable_mask = ARIZONA_CPMIC_ENA,
163 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
164 .bypass_mask = ARIZONA_CPMIC_BYPASS,
166 .linear_ranges = arizona_micsupp_ext_ranges,
167 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ext_ranges),
169 .enable_time = 3000,
171 .owner = THIS_MODULE,
174 static const struct regulator_init_data arizona_micsupp_default = {
175 .constraints = {
176 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
177 REGULATOR_CHANGE_VOLTAGE |
178 REGULATOR_CHANGE_BYPASS,
179 .min_uV = 1700000,
180 .max_uV = 3300000,
183 .num_consumer_supplies = 1,
186 static const struct regulator_init_data arizona_micsupp_ext_default = {
187 .constraints = {
188 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
189 REGULATOR_CHANGE_VOLTAGE |
190 REGULATOR_CHANGE_BYPASS,
191 .min_uV = 900000,
192 .max_uV = 3300000,
195 .num_consumer_supplies = 1,
198 static int arizona_micsupp_probe(struct platform_device *pdev)
200 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
201 const struct regulator_desc *desc;
202 struct regulator_config config = { };
203 struct arizona_micsupp *micsupp;
204 int ret;
206 micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
207 if (micsupp == NULL) {
208 dev_err(&pdev->dev, "Unable to allocate private data\n");
209 return -ENOMEM;
212 micsupp->arizona = arizona;
213 INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
216 * Since the chip usually supplies itself we provide some
217 * default init_data for it. This will be overridden with
218 * platform data if provided.
220 switch (arizona->type) {
221 case WM5110:
222 desc = &arizona_micsupp_ext;
223 micsupp->init_data = arizona_micsupp_ext_default;
224 break;
225 default:
226 desc = &arizona_micsupp;
227 micsupp->init_data = arizona_micsupp_default;
228 break;
231 micsupp->init_data.consumer_supplies = &micsupp->supply;
232 micsupp->supply.supply = "MICVDD";
233 micsupp->supply.dev_name = dev_name(arizona->dev);
235 config.dev = arizona->dev;
236 config.driver_data = micsupp;
237 config.regmap = arizona->regmap;
239 if (arizona->pdata.micvdd)
240 config.init_data = arizona->pdata.micvdd;
241 else
242 config.init_data = &micsupp->init_data;
244 /* Default to regulated mode until the API supports bypass */
245 regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1,
246 ARIZONA_CPMIC_BYPASS, 0);
248 micsupp->regulator = devm_regulator_register(&pdev->dev,
249 desc,
250 &config);
251 if (IS_ERR(micsupp->regulator)) {
252 ret = PTR_ERR(micsupp->regulator);
253 dev_err(arizona->dev, "Failed to register mic supply: %d\n",
254 ret);
255 return ret;
258 platform_set_drvdata(pdev, micsupp);
260 return 0;
263 static struct platform_driver arizona_micsupp_driver = {
264 .probe = arizona_micsupp_probe,
265 .driver = {
266 .name = "arizona-micsupp",
267 .owner = THIS_MODULE,
271 module_platform_driver(arizona_micsupp_driver);
273 /* Module information */
274 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
275 MODULE_DESCRIPTION("Arizona microphone supply driver");
276 MODULE_LICENSE("GPL");
277 MODULE_ALIAS("platform:arizona-micsupp");