blob: be0d46da51a142113d2fc77fb015ba43cae1b304 [file] [log] [blame]
Axel Lindfc0c822019-04-17 22:16:29 +08001// SPDX-License-Identifier: GPL-2.0+
2//
3// arizona-micsupp.c -- Microphone supply for Arizona devices
4//
5// Copyright 2012 Wolfson Microelectronics PLC.
6//
7// Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
Mark Brownb667a452012-06-14 18:14:00 +01008
9#include <linux/module.h>
10#include <linux/moduleparam.h>
11#include <linux/init.h>
12#include <linux/bitops.h>
13#include <linux/err.h>
Charles Keepax6f1c9c52014-05-08 17:17:38 +010014#include <linux/of.h>
Mark Brownb667a452012-06-14 18:14:00 +010015#include <linux/platform_device.h>
16#include <linux/regulator/driver.h>
17#include <linux/regulator/machine.h>
Charles Keepax36bcdf12014-04-16 10:01:41 +010018#include <linux/regulator/of_regulator.h>
Mark Brownb667a452012-06-14 18:14:00 +010019#include <linux/gpio.h>
20#include <linux/slab.h>
Mark Browne6ed9052013-01-10 19:14:11 +000021#include <linux/workqueue.h>
22#include <sound/soc.h>
Mark Brownb667a452012-06-14 18:14:00 +010023
24#include <linux/mfd/arizona/core.h>
25#include <linux/mfd/arizona/pdata.h>
26#include <linux/mfd/arizona/registers.h>
27
Richard Fitzgerald22161f32017-04-18 11:43:49 +010028#include <linux/regulator/arizona-micsupp.h>
29
Mark Brownb667a452012-06-14 18:14:00 +010030struct arizona_micsupp {
31 struct regulator_dev *regulator;
Richard Fitzgeralde1659832017-04-18 11:43:50 +010032 struct regmap *regmap;
33 struct snd_soc_dapm_context **dapm;
34 unsigned int enable_reg;
35 struct device *dev;
Mark Brownb667a452012-06-14 18:14:00 +010036
37 struct regulator_consumer_supply supply;
38 struct regulator_init_data init_data;
Mark Browne6ed9052013-01-10 19:14:11 +000039
40 struct work_struct check_cp_work;
Mark Brownb667a452012-06-14 18:14:00 +010041};
42
Mark Browne6ed9052013-01-10 19:14:11 +000043static 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);
Richard Fitzgeralde1659832017-04-18 11:43:50 +010047 struct snd_soc_dapm_context *dapm = *micsupp->dapm;
48 struct snd_soc_component *component;
49 unsigned int val;
Mark Browne6ed9052013-01-10 19:14:11 +000050 int ret;
51
Richard Fitzgeralde1659832017-04-18 11:43:50 +010052 ret = regmap_read(micsupp->regmap, micsupp->enable_reg, &val);
Mark Browne6ed9052013-01-10 19:14:11 +000053 if (ret != 0) {
Richard Fitzgeralde1659832017-04-18 11:43:50 +010054 dev_err(micsupp->dev,
55 "Failed to read CP state: %d\n", ret);
Mark Browne6ed9052013-01-10 19:14:11 +000056 return;
57 }
58
59 if (dapm) {
Richard Fitzgeralde1659832017-04-18 11:43:50 +010060 component = snd_soc_dapm_to_component(dapm);
61
62 if ((val & (ARIZONA_CPMIC_ENA | ARIZONA_CPMIC_BYPASS)) ==
Mark Browne6ed9052013-01-10 19:14:11 +000063 ARIZONA_CPMIC_ENA)
Richard Fitzgerald98cf9962016-12-15 14:43:49 +000064 snd_soc_component_force_enable_pin(component,
65 "MICSUPP");
Mark Browne6ed9052013-01-10 19:14:11 +000066 else
Richard Fitzgerald98cf9962016-12-15 14:43:49 +000067 snd_soc_component_disable_pin(component, "MICSUPP");
Mark Browne6ed9052013-01-10 19:14:11 +000068
69 snd_soc_dapm_sync(dapm);
70 }
71}
72
73static int arizona_micsupp_enable(struct regulator_dev *rdev)
74{
75 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
76 int ret;
77
78 ret = regulator_enable_regmap(rdev);
79
80 if (ret == 0)
81 schedule_work(&micsupp->check_cp_work);
82
83 return ret;
84}
85
86static int arizona_micsupp_disable(struct regulator_dev *rdev)
87{
88 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
89 int ret;
90
91 ret = regulator_disable_regmap(rdev);
92 if (ret == 0)
93 schedule_work(&micsupp->check_cp_work);
94
95 return ret;
96}
97
98static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena)
99{
100 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
101 int ret;
102
103 ret = regulator_set_bypass_regmap(rdev, ena);
104 if (ret == 0)
105 schedule_work(&micsupp->check_cp_work);
106
107 return ret;
108}
109
Bhumika Goyal2773ead2017-01-28 19:10:00 +0530110static const struct regulator_ops arizona_micsupp_ops = {
Mark Browne6ed9052013-01-10 19:14:11 +0000111 .enable = arizona_micsupp_enable,
112 .disable = arizona_micsupp_disable,
Mark Brownb667a452012-06-14 18:14:00 +0100113 .is_enabled = regulator_is_enabled_regmap,
114
Charles Keepax71979aa2013-11-15 13:13:32 +0000115 .list_voltage = regulator_list_voltage_linear_range,
116 .map_voltage = regulator_map_voltage_linear_range,
Mark Brownb667a452012-06-14 18:14:00 +0100117
118 .get_voltage_sel = regulator_get_voltage_sel_regmap,
119 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Browne477ce02012-08-27 16:04:47 -0700120
121 .get_bypass = regulator_get_bypass_regmap,
Mark Browne6ed9052013-01-10 19:14:11 +0000122 .set_bypass = arizona_micsupp_set_bypass,
Mark Brownb667a452012-06-14 18:14:00 +0100123};
124
Charles Keepax71979aa2013-11-15 13:13:32 +0000125static const struct regulator_linear_range arizona_micsupp_ranges[] = {
126 REGULATOR_LINEAR_RANGE(1700000, 0, 0x1e, 50000),
127 REGULATOR_LINEAR_RANGE(3300000, 0x1f, 0x1f, 0),
128};
129
Mark Brownb667a452012-06-14 18:14:00 +0100130static const struct regulator_desc arizona_micsupp = {
131 .name = "MICVDD",
132 .supply_name = "CPVDD",
133 .type = REGULATOR_VOLTAGE,
Charles Keepax71979aa2013-11-15 13:13:32 +0000134 .n_voltages = 32,
Mark Brownb667a452012-06-14 18:14:00 +0100135 .ops = &arizona_micsupp_ops,
136
137 .vsel_reg = ARIZONA_LDO2_CONTROL_1,
138 .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
139 .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
140 .enable_mask = ARIZONA_CPMIC_ENA,
Mark Browne477ce02012-08-27 16:04:47 -0700141 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
142 .bypass_mask = ARIZONA_CPMIC_BYPASS,
Mark Brownb667a452012-06-14 18:14:00 +0100143
Charles Keepax71979aa2013-11-15 13:13:32 +0000144 .linear_ranges = arizona_micsupp_ranges,
145 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ranges),
146
Mark Brown95072812012-11-27 14:55:49 +0000147 .enable_time = 3000,
148
Mark Brownb667a452012-06-14 18:14:00 +0100149 .owner = THIS_MODULE,
150};
151
Charles Keepaxd2e74912013-11-15 13:48:23 +0000152static const struct regulator_linear_range arizona_micsupp_ext_ranges[] = {
153 REGULATOR_LINEAR_RANGE(900000, 0, 0x14, 25000),
154 REGULATOR_LINEAR_RANGE(1500000, 0x15, 0x27, 100000),
155};
156
157static const struct regulator_desc arizona_micsupp_ext = {
158 .name = "MICVDD",
159 .supply_name = "CPVDD",
160 .type = REGULATOR_VOLTAGE,
161 .n_voltages = 40,
162 .ops = &arizona_micsupp_ops,
163
164 .vsel_reg = ARIZONA_LDO2_CONTROL_1,
165 .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
166 .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
167 .enable_mask = ARIZONA_CPMIC_ENA,
168 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
169 .bypass_mask = ARIZONA_CPMIC_BYPASS,
170
171 .linear_ranges = arizona_micsupp_ext_ranges,
172 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ext_ranges),
173
174 .enable_time = 3000,
175
176 .owner = THIS_MODULE,
177};
178
Mark Brownb667a452012-06-14 18:14:00 +0100179static const struct regulator_init_data arizona_micsupp_default = {
180 .constraints = {
181 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
Mark Brown9fc50a22013-01-10 19:31:47 +0000182 REGULATOR_CHANGE_VOLTAGE |
183 REGULATOR_CHANGE_BYPASS,
Mark Brownb667a452012-06-14 18:14:00 +0100184 .min_uV = 1700000,
185 .max_uV = 3300000,
186 },
187
188 .num_consumer_supplies = 1,
189};
190
Charles Keepaxd2e74912013-11-15 13:48:23 +0000191static const struct regulator_init_data arizona_micsupp_ext_default = {
192 .constraints = {
193 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
194 REGULATOR_CHANGE_VOLTAGE |
195 REGULATOR_CHANGE_BYPASS,
196 .min_uV = 900000,
197 .max_uV = 3300000,
198 },
199
200 .num_consumer_supplies = 1,
201};
202
Richard Fitzgerald22161f32017-04-18 11:43:49 +0100203static int arizona_micsupp_of_get_pdata(struct arizona_micsupp_pdata *pdata,
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100204 struct regulator_config *config,
205 const struct regulator_desc *desc)
Charles Keepax36bcdf12014-04-16 10:01:41 +0100206{
Charles Keepax36bcdf12014-04-16 10:01:41 +0100207 struct arizona_micsupp *micsupp = config->driver_data;
208 struct device_node *np;
209 struct regulator_init_data *init_data;
210
Richard Fitzgerald22161f32017-04-18 11:43:49 +0100211 np = of_get_child_by_name(config->dev->of_node, "micvdd");
Charles Keepax36bcdf12014-04-16 10:01:41 +0100212
213 if (np) {
214 config->of_node = np;
215
Richard Fitzgerald22161f32017-04-18 11:43:49 +0100216 init_data = of_get_regulator_init_data(config->dev, np, desc);
Charles Keepax36bcdf12014-04-16 10:01:41 +0100217
218 if (init_data) {
219 init_data->consumer_supplies = &micsupp->supply;
220 init_data->num_consumer_supplies = 1;
221
Richard Fitzgerald22161f32017-04-18 11:43:49 +0100222 pdata->init_data = init_data;
Charles Keepax36bcdf12014-04-16 10:01:41 +0100223 }
224 }
225
226 return 0;
227}
228
Richard Fitzgerald7d8d14b2017-04-18 11:43:51 +0100229static int arizona_micsupp_common_init(struct platform_device *pdev,
230 struct arizona_micsupp *micsupp,
231 const struct regulator_desc *desc,
232 struct arizona_micsupp_pdata *pdata)
233{
234 struct regulator_config config = { };
235 int ret;
236
237 INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
238
239 micsupp->init_data.consumer_supplies = &micsupp->supply;
240 micsupp->supply.supply = "MICVDD";
241 micsupp->supply.dev_name = dev_name(micsupp->dev);
242 micsupp->enable_reg = desc->enable_reg;
243
244 config.dev = micsupp->dev;
245 config.driver_data = micsupp;
246 config.regmap = micsupp->regmap;
247
248 if (IS_ENABLED(CONFIG_OF)) {
249 if (!dev_get_platdata(micsupp->dev)) {
250 ret = arizona_micsupp_of_get_pdata(pdata, &config,
251 desc);
252 if (ret < 0)
253 return ret;
254 }
255 }
256
257 if (pdata->init_data)
258 config.init_data = pdata->init_data;
259 else
260 config.init_data = &micsupp->init_data;
261
262 /* Default to regulated mode */
263 regmap_update_bits(micsupp->regmap, micsupp->enable_reg,
264 ARIZONA_CPMIC_BYPASS, 0);
265
266 micsupp->regulator = devm_regulator_register(&pdev->dev,
267 desc,
268 &config);
269
270 of_node_put(config.of_node);
271
272 if (IS_ERR(micsupp->regulator)) {
273 ret = PTR_ERR(micsupp->regulator);
274 dev_err(micsupp->dev, "Failed to register mic supply: %d\n",
275 ret);
276 return ret;
277 }
278
279 platform_set_drvdata(pdev, micsupp);
280
281 return 0;
282}
283
Bill Pembertona5023572012-11-19 13:22:22 -0500284static int arizona_micsupp_probe(struct platform_device *pdev)
Mark Brownb667a452012-06-14 18:14:00 +0100285{
286 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
Charles Keepaxd2e74912013-11-15 13:48:23 +0000287 const struct regulator_desc *desc;
Mark Brownb667a452012-06-14 18:14:00 +0100288 struct arizona_micsupp *micsupp;
Mark Brownb667a452012-06-14 18:14:00 +0100289
290 micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
Sachin Kamat820cd312014-02-18 16:11:10 +0530291 if (!micsupp)
Mark Brownb667a452012-06-14 18:14:00 +0100292 return -ENOMEM;
Mark Brownb667a452012-06-14 18:14:00 +0100293
Richard Fitzgeralde1659832017-04-18 11:43:50 +0100294 micsupp->regmap = arizona->regmap;
295 micsupp->dapm = &arizona->dapm;
296 micsupp->dev = arizona->dev;
Mark Brownb667a452012-06-14 18:14:00 +0100297
298 /*
299 * Since the chip usually supplies itself we provide some
300 * default init_data for it. This will be overridden with
301 * platform data if provided.
302 */
Charles Keepaxd2e74912013-11-15 13:48:23 +0000303 switch (arizona->type) {
304 case WM5110:
Richard Fitzgeraldf7293112015-01-17 15:21:24 +0000305 case WM8280:
Charles Keepaxd2e74912013-11-15 13:48:23 +0000306 desc = &arizona_micsupp_ext;
307 micsupp->init_data = arizona_micsupp_ext_default;
308 break;
309 default:
310 desc = &arizona_micsupp;
311 micsupp->init_data = arizona_micsupp_default;
312 break;
313 }
314
Richard Fitzgerald7d8d14b2017-04-18 11:43:51 +0100315 return arizona_micsupp_common_init(pdev, micsupp, desc,
316 &arizona->pdata.micvdd);
Mark Brownb667a452012-06-14 18:14:00 +0100317}
318
Mark Brownb667a452012-06-14 18:14:00 +0100319static struct platform_driver arizona_micsupp_driver = {
320 .probe = arizona_micsupp_probe,
Mark Brownb667a452012-06-14 18:14:00 +0100321 .driver = {
322 .name = "arizona-micsupp",
Mark Brownb667a452012-06-14 18:14:00 +0100323 },
324};
325
326module_platform_driver(arizona_micsupp_driver);
327
328/* Module information */
329MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
330MODULE_DESCRIPTION("Arizona microphone supply driver");
331MODULE_LICENSE("GPL");
332MODULE_ALIAS("platform:arizona-micsupp");