]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/regulator/arizona-micsupp.c
5e38861e71d8ef87a5c2e9764bf78514908e0179
[linux.git] / drivers / regulator / arizona-micsupp.c
1 /*
2  * arizona-micsupp.c  --  Microphone supply for Arizona devices
3  *
4  * Copyright 2012 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
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.
12  */
13
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/of.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>
28
29 #include <linux/mfd/arizona/core.h>
30 #include <linux/mfd/arizona/pdata.h>
31 #include <linux/mfd/arizona/registers.h>
32
33 struct arizona_micsupp {
34         struct regulator_dev *regulator;
35         struct arizona *arizona;
36
37         struct regulator_consumer_supply supply;
38         struct regulator_init_data init_data;
39
40         struct work_struct check_cp_work;
41 };
42
43 static void arizona_micsupp_check_cp(struct work_struct *work)
44 {
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;
51         unsigned int reg;
52         int ret;
53
54         ret = regmap_read(regmap, ARIZONA_MIC_CHARGE_PUMP_1, &reg);
55         if (ret != 0) {
56                 dev_err(arizona->dev, "Failed to read CP state: %d\n", ret);
57                 return;
58         }
59
60         if (dapm) {
61                 if ((reg & (ARIZONA_CPMIC_ENA | ARIZONA_CPMIC_BYPASS)) ==
62                     ARIZONA_CPMIC_ENA)
63                         snd_soc_component_force_enable_pin(component,
64                                                            "MICSUPP");
65                 else
66                         snd_soc_component_disable_pin(component, "MICSUPP");
67
68                 snd_soc_dapm_sync(dapm);
69         }
70 }
71
72 static int arizona_micsupp_enable(struct regulator_dev *rdev)
73 {
74         struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
75         int ret;
76
77         ret = regulator_enable_regmap(rdev);
78
79         if (ret == 0)
80                 schedule_work(&micsupp->check_cp_work);
81
82         return ret;
83 }
84
85 static int arizona_micsupp_disable(struct regulator_dev *rdev)
86 {
87         struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
88         int ret;
89
90         ret = regulator_disable_regmap(rdev);
91         if (ret == 0)
92                 schedule_work(&micsupp->check_cp_work);
93
94         return ret;
95 }
96
97 static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena)
98 {
99         struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
100         int ret;
101
102         ret = regulator_set_bypass_regmap(rdev, ena);
103         if (ret == 0)
104                 schedule_work(&micsupp->check_cp_work);
105
106         return ret;
107 }
108
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,
113
114         .list_voltage = regulator_list_voltage_linear_range,
115         .map_voltage = regulator_map_voltage_linear_range,
116
117         .get_voltage_sel = regulator_get_voltage_sel_regmap,
118         .set_voltage_sel = regulator_set_voltage_sel_regmap,
119
120         .get_bypass = regulator_get_bypass_regmap,
121         .set_bypass = arizona_micsupp_set_bypass,
122 };
123
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),
127 };
128
129 static const struct regulator_desc arizona_micsupp = {
130         .name = "MICVDD",
131         .supply_name = "CPVDD",
132         .type = REGULATOR_VOLTAGE,
133         .n_voltages = 32,
134         .ops = &arizona_micsupp_ops,
135
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,
142
143         .linear_ranges = arizona_micsupp_ranges,
144         .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ranges),
145
146         .enable_time = 3000,
147
148         .owner = THIS_MODULE,
149 };
150
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),
154 };
155
156 static const struct regulator_desc arizona_micsupp_ext = {
157         .name = "MICVDD",
158         .supply_name = "CPVDD",
159         .type = REGULATOR_VOLTAGE,
160         .n_voltages = 40,
161         .ops = &arizona_micsupp_ops,
162
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,
169
170         .linear_ranges = arizona_micsupp_ext_ranges,
171         .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ext_ranges),
172
173         .enable_time = 3000,
174
175         .owner = THIS_MODULE,
176 };
177
178 static const struct regulator_init_data arizona_micsupp_default = {
179         .constraints = {
180                 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
181                                 REGULATOR_CHANGE_VOLTAGE |
182                                 REGULATOR_CHANGE_BYPASS,
183                 .min_uV = 1700000,
184                 .max_uV = 3300000,
185         },
186
187         .num_consumer_supplies = 1,
188 };
189
190 static const struct regulator_init_data arizona_micsupp_ext_default = {
191         .constraints = {
192                 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
193                                 REGULATOR_CHANGE_VOLTAGE |
194                                 REGULATOR_CHANGE_BYPASS,
195                 .min_uV = 900000,
196                 .max_uV = 3300000,
197         },
198
199         .num_consumer_supplies = 1,
200 };
201
202 static int arizona_micsupp_of_get_pdata(struct device *dev,
203                                         struct arizona *arizona,
204                                         struct regulator_config *config,
205                                         const struct regulator_desc *desc)
206 {
207         struct arizona_pdata *pdata = &arizona->pdata;
208         struct arizona_micsupp *micsupp = config->driver_data;
209         struct device_node *np;
210         struct regulator_init_data *init_data;
211
212         np = of_get_child_by_name(arizona->dev->of_node, "micvdd");
213
214         if (np) {
215                 config->of_node = np;
216
217                 init_data = of_get_regulator_init_data(dev, np, desc);
218
219                 if (init_data) {
220                         init_data->consumer_supplies = &micsupp->supply;
221                         init_data->num_consumer_supplies = 1;
222
223                         pdata->micvdd = init_data;
224                 }
225         }
226
227         return 0;
228 }
229
230 static int arizona_micsupp_probe(struct platform_device *pdev)
231 {
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 *micsupp;
236         int ret;
237
238         micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
239         if (!micsupp)
240                 return -ENOMEM;
241
242         micsupp->arizona = arizona;
243         INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
244
245         /*
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.
249          */
250         switch (arizona->type) {
251         case WM5110:
252         case WM8280:
253                 desc = &arizona_micsupp_ext;
254                 micsupp->init_data = arizona_micsupp_ext_default;
255                 break;
256         default:
257                 desc = &arizona_micsupp;
258                 micsupp->init_data = arizona_micsupp_default;
259                 break;
260         }
261
262         micsupp->init_data.consumer_supplies = &micsupp->supply;
263         micsupp->supply.supply = "MICVDD";
264         micsupp->supply.dev_name = dev_name(arizona->dev);
265
266         config.dev = arizona->dev;
267         config.driver_data = micsupp;
268         config.regmap = arizona->regmap;
269
270         if (IS_ENABLED(CONFIG_OF)) {
271                 if (!dev_get_platdata(arizona->dev)) {
272                         ret = arizona_micsupp_of_get_pdata(&pdev->dev, arizona,
273                                                            &config, desc);
274                         if (ret < 0)
275                                 return ret;
276                 }
277         }
278
279         if (arizona->pdata.micvdd)
280                 config.init_data = arizona->pdata.micvdd;
281         else
282                 config.init_data = &micsupp->init_data;
283
284         /* Default to regulated mode until the API supports bypass */
285         regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1,
286                            ARIZONA_CPMIC_BYPASS, 0);
287
288         micsupp->regulator = devm_regulator_register(&pdev->dev,
289                                                      desc,
290                                                      &config);
291
292         of_node_put(config.of_node);
293
294         if (IS_ERR(micsupp->regulator)) {
295                 ret = PTR_ERR(micsupp->regulator);
296                 dev_err(arizona->dev, "Failed to register mic supply: %d\n",
297                         ret);
298                 return ret;
299         }
300
301         platform_set_drvdata(pdev, micsupp);
302
303         return 0;
304 }
305
306 static struct platform_driver arizona_micsupp_driver = {
307         .probe = arizona_micsupp_probe,
308         .driver         = {
309                 .name   = "arizona-micsupp",
310         },
311 };
312
313 module_platform_driver(arizona_micsupp_driver);
314
315 /* Module information */
316 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
317 MODULE_DESCRIPTION("Arizona microphone supply driver");
318 MODULE_LICENSE("GPL");
319 MODULE_ALIAS("platform:arizona-micsupp");