]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/regulator/arizona-micsupp.c
Merge branches 'ib-from-asoc-3.16', 'ib-from-pm-3.16', 'ib-from-regulator-3.16',...
[karo-tx-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/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>
27
28 #include <linux/mfd/arizona/core.h>
29 #include <linux/mfd/arizona/pdata.h>
30 #include <linux/mfd/arizona/registers.h>
31
32 struct arizona_micsupp {
33         struct regulator_dev *regulator;
34         struct arizona *arizona;
35
36         struct regulator_consumer_supply supply;
37         struct regulator_init_data init_data;
38
39         struct work_struct check_cp_work;
40 };
41
42 static void arizona_micsupp_check_cp(struct work_struct *work)
43 {
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;
49         unsigned int reg;
50         int ret;
51
52         ret = regmap_read(regmap, ARIZONA_MIC_CHARGE_PUMP_1, &reg);
53         if (ret != 0) {
54                 dev_err(arizona->dev, "Failed to read CP state: %d\n", ret);
55                 return;
56         }
57
58         if (dapm) {
59                 if ((reg & (ARIZONA_CPMIC_ENA | ARIZONA_CPMIC_BYPASS)) ==
60                     ARIZONA_CPMIC_ENA)
61                         snd_soc_dapm_force_enable_pin(dapm, "MICSUPP");
62                 else
63                         snd_soc_dapm_disable_pin(dapm, "MICSUPP");
64
65                 snd_soc_dapm_sync(dapm);
66         }
67 }
68
69 static int arizona_micsupp_enable(struct regulator_dev *rdev)
70 {
71         struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
72         int ret;
73
74         ret = regulator_enable_regmap(rdev);
75
76         if (ret == 0)
77                 schedule_work(&micsupp->check_cp_work);
78
79         return ret;
80 }
81
82 static int arizona_micsupp_disable(struct regulator_dev *rdev)
83 {
84         struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
85         int ret;
86
87         ret = regulator_disable_regmap(rdev);
88         if (ret == 0)
89                 schedule_work(&micsupp->check_cp_work);
90
91         return ret;
92 }
93
94 static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena)
95 {
96         struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
97         int ret;
98
99         ret = regulator_set_bypass_regmap(rdev, ena);
100         if (ret == 0)
101                 schedule_work(&micsupp->check_cp_work);
102
103         return ret;
104 }
105
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,
110
111         .list_voltage = regulator_list_voltage_linear_range,
112         .map_voltage = regulator_map_voltage_linear_range,
113
114         .get_voltage_sel = regulator_get_voltage_sel_regmap,
115         .set_voltage_sel = regulator_set_voltage_sel_regmap,
116
117         .get_bypass = regulator_get_bypass_regmap,
118         .set_bypass = arizona_micsupp_set_bypass,
119 };
120
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),
124 };
125
126 static const struct regulator_desc arizona_micsupp = {
127         .name = "MICVDD",
128         .supply_name = "CPVDD",
129         .type = REGULATOR_VOLTAGE,
130         .n_voltages = 32,
131         .ops = &arizona_micsupp_ops,
132
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,
139
140         .linear_ranges = arizona_micsupp_ranges,
141         .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ranges),
142
143         .enable_time = 3000,
144
145         .owner = THIS_MODULE,
146 };
147
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),
151 };
152
153 static const struct regulator_desc arizona_micsupp_ext = {
154         .name = "MICVDD",
155         .supply_name = "CPVDD",
156         .type = REGULATOR_VOLTAGE,
157         .n_voltages = 40,
158         .ops = &arizona_micsupp_ops,
159
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,
166
167         .linear_ranges = arizona_micsupp_ext_ranges,
168         .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ext_ranges),
169
170         .enable_time = 3000,
171
172         .owner = THIS_MODULE,
173 };
174
175 static const struct regulator_init_data arizona_micsupp_default = {
176         .constraints = {
177                 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
178                                 REGULATOR_CHANGE_VOLTAGE |
179                                 REGULATOR_CHANGE_BYPASS,
180                 .min_uV = 1700000,
181                 .max_uV = 3300000,
182         },
183
184         .num_consumer_supplies = 1,
185 };
186
187 static const struct regulator_init_data arizona_micsupp_ext_default = {
188         .constraints = {
189                 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
190                                 REGULATOR_CHANGE_VOLTAGE |
191                                 REGULATOR_CHANGE_BYPASS,
192                 .min_uV = 900000,
193                 .max_uV = 3300000,
194         },
195
196         .num_consumer_supplies = 1,
197 };
198
199 static int arizona_micsupp_of_get_pdata(struct arizona *arizona,
200                                         struct regulator_config *config)
201 {
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;
206
207         np = of_get_child_by_name(arizona->dev->of_node, "micvdd");
208
209         if (np) {
210                 config->of_node = np;
211
212                 init_data = of_get_regulator_init_data(arizona->dev, np);
213
214                 if (init_data) {
215                         init_data->consumer_supplies = &micsupp->supply;
216                         init_data->num_consumer_supplies = 1;
217
218                         pdata->micvdd = init_data;
219                 }
220         }
221
222         return 0;
223 }
224
225 static int arizona_micsupp_probe(struct platform_device *pdev)
226 {
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;
231         int ret;
232
233         micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
234         if (!micsupp)
235                 return -ENOMEM;
236
237         micsupp->arizona = arizona;
238         INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
239
240         /*
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.
244          */
245         switch (arizona->type) {
246         case WM5110:
247                 desc = &arizona_micsupp_ext;
248                 micsupp->init_data = arizona_micsupp_ext_default;
249                 break;
250         default:
251                 desc = &arizona_micsupp;
252                 micsupp->init_data = arizona_micsupp_default;
253                 break;
254         }
255
256         micsupp->init_data.consumer_supplies = &micsupp->supply;
257         micsupp->supply.supply = "MICVDD";
258         micsupp->supply.dev_name = dev_name(arizona->dev);
259
260         config.dev = arizona->dev;
261         config.driver_data = micsupp;
262         config.regmap = arizona->regmap;
263
264         if (IS_ENABLED(CONFIG_OF)) {
265                 if (!dev_get_platdata(arizona->dev)) {
266                         ret = arizona_micsupp_of_get_pdata(arizona, &config);
267                         if (ret < 0)
268                                 return ret;
269                 }
270         }
271
272         if (arizona->pdata.micvdd)
273                 config.init_data = arizona->pdata.micvdd;
274         else
275                 config.init_data = &micsupp->init_data;
276
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);
280
281         micsupp->regulator = devm_regulator_register(&pdev->dev,
282                                                      desc,
283                                                      &config);
284         if (IS_ERR(micsupp->regulator)) {
285                 ret = PTR_ERR(micsupp->regulator);
286                 dev_err(arizona->dev, "Failed to register mic supply: %d\n",
287                         ret);
288                 return ret;
289         }
290
291         of_node_put(config.of_node);
292
293         platform_set_drvdata(pdev, micsupp);
294
295         return 0;
296 }
297
298 static struct platform_driver arizona_micsupp_driver = {
299         .probe = arizona_micsupp_probe,
300         .driver         = {
301                 .name   = "arizona-micsupp",
302                 .owner  = THIS_MODULE,
303         },
304 };
305
306 module_platform_driver(arizona_micsupp_driver);
307
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");