blob: 22bd714076229be6129d73436ac12611f4956f4a [file] [log] [blame]
Mark Brownb667a452012-06-14 18:14:00 +01001/*
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>
Charles Keepax6f1c9c52014-05-08 17:17:38 +010019#include <linux/of.h>
Mark Brownb667a452012-06-14 18:14:00 +010020#include <linux/platform_device.h>
21#include <linux/regulator/driver.h>
22#include <linux/regulator/machine.h>
Charles Keepax36bcdf12014-04-16 10:01:41 +010023#include <linux/regulator/of_regulator.h>
Mark Brownb667a452012-06-14 18:14:00 +010024#include <linux/gpio.h>
25#include <linux/slab.h>
Mark Browne6ed9052013-01-10 19:14:11 +000026#include <linux/workqueue.h>
27#include <sound/soc.h>
Mark Brownb667a452012-06-14 18:14:00 +010028
29#include <linux/mfd/arizona/core.h>
30#include <linux/mfd/arizona/pdata.h>
31#include <linux/mfd/arizona/registers.h>
32
Mark Brownb667a452012-06-14 18:14:00 +010033struct 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;
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);
47 struct snd_soc_dapm_context *dapm = micsupp->arizona->dapm;
Richard Fitzgerald98cf9962016-12-15 14:43:49 +000048 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
Mark Browne6ed9052013-01-10 19:14:11 +000049 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)
Richard Fitzgerald98cf9962016-12-15 14:43:49 +000063 snd_soc_component_force_enable_pin(component,
64 "MICSUPP");
Mark Browne6ed9052013-01-10 19:14:11 +000065 else
Richard Fitzgerald98cf9962016-12-15 14:43:49 +000066 snd_soc_component_disable_pin(component, "MICSUPP");
Mark Browne6ed9052013-01-10 19:14:11 +000067
68 snd_soc_dapm_sync(dapm);
69 }
70}
71
72static 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
85static 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
97static 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
Bhumika Goyal2773ead2017-01-28 19:10:00 +0530109static const struct regulator_ops arizona_micsupp_ops = {
Mark Browne6ed9052013-01-10 19:14:11 +0000110 .enable = arizona_micsupp_enable,
111 .disable = arizona_micsupp_disable,
Mark Brownb667a452012-06-14 18:14:00 +0100112 .is_enabled = regulator_is_enabled_regmap,
113
Charles Keepax71979aa2013-11-15 13:13:32 +0000114 .list_voltage = regulator_list_voltage_linear_range,
115 .map_voltage = regulator_map_voltage_linear_range,
Mark Brownb667a452012-06-14 18:14:00 +0100116
117 .get_voltage_sel = regulator_get_voltage_sel_regmap,
118 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Browne477ce02012-08-27 16:04:47 -0700119
120 .get_bypass = regulator_get_bypass_regmap,
Mark Browne6ed9052013-01-10 19:14:11 +0000121 .set_bypass = arizona_micsupp_set_bypass,
Mark Brownb667a452012-06-14 18:14:00 +0100122};
123
Charles Keepax71979aa2013-11-15 13:13:32 +0000124static 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
Mark Brownb667a452012-06-14 18:14:00 +0100129static const struct regulator_desc arizona_micsupp = {
130 .name = "MICVDD",
131 .supply_name = "CPVDD",
132 .type = REGULATOR_VOLTAGE,
Charles Keepax71979aa2013-11-15 13:13:32 +0000133 .n_voltages = 32,
Mark Brownb667a452012-06-14 18:14:00 +0100134 .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,
Mark Browne477ce02012-08-27 16:04:47 -0700140 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
141 .bypass_mask = ARIZONA_CPMIC_BYPASS,
Mark Brownb667a452012-06-14 18:14:00 +0100142
Charles Keepax71979aa2013-11-15 13:13:32 +0000143 .linear_ranges = arizona_micsupp_ranges,
144 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ranges),
145
Mark Brown95072812012-11-27 14:55:49 +0000146 .enable_time = 3000,
147
Mark Brownb667a452012-06-14 18:14:00 +0100148 .owner = THIS_MODULE,
149};
150
Charles Keepaxd2e74912013-11-15 13:48:23 +0000151static 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
156static 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
Mark Brownb667a452012-06-14 18:14:00 +0100178static const struct regulator_init_data arizona_micsupp_default = {
179 .constraints = {
180 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
Mark Brown9fc50a22013-01-10 19:31:47 +0000181 REGULATOR_CHANGE_VOLTAGE |
182 REGULATOR_CHANGE_BYPASS,
Mark Brownb667a452012-06-14 18:14:00 +0100183 .min_uV = 1700000,
184 .max_uV = 3300000,
185 },
186
187 .num_consumer_supplies = 1,
188};
189
Charles Keepaxd2e74912013-11-15 13:48:23 +0000190static 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
Charles Keepax36bcdf12014-04-16 10:01:41 +0100202static int arizona_micsupp_of_get_pdata(struct arizona *arizona,
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100203 struct regulator_config *config,
204 const struct regulator_desc *desc)
Charles Keepax36bcdf12014-04-16 10:01:41 +0100205{
206 struct arizona_pdata *pdata = &arizona->pdata;
207 struct arizona_micsupp *micsupp = config->driver_data;
208 struct device_node *np;
209 struct regulator_init_data *init_data;
210
211 np = of_get_child_by_name(arizona->dev->of_node, "micvdd");
212
213 if (np) {
214 config->of_node = np;
215
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100216 init_data = of_get_regulator_init_data(arizona->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
222 pdata->micvdd = init_data;
223 }
224 }
225
226 return 0;
227}
228
Bill Pembertona5023572012-11-19 13:22:22 -0500229static int arizona_micsupp_probe(struct platform_device *pdev)
Mark Brownb667a452012-06-14 18:14:00 +0100230{
231 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
Charles Keepaxd2e74912013-11-15 13:48:23 +0000232 const struct regulator_desc *desc;
Mark Brownb667a452012-06-14 18:14:00 +0100233 struct regulator_config config = { };
234 struct arizona_micsupp *micsupp;
235 int ret;
236
237 micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
Sachin Kamat820cd312014-02-18 16:11:10 +0530238 if (!micsupp)
Mark Brownb667a452012-06-14 18:14:00 +0100239 return -ENOMEM;
Mark Brownb667a452012-06-14 18:14:00 +0100240
241 micsupp->arizona = arizona;
Mark Browne6ed9052013-01-10 19:14:11 +0000242 INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
Mark Brownb667a452012-06-14 18:14:00 +0100243
244 /*
245 * Since the chip usually supplies itself we provide some
246 * default init_data for it. This will be overridden with
247 * platform data if provided.
248 */
Charles Keepaxd2e74912013-11-15 13:48:23 +0000249 switch (arizona->type) {
250 case WM5110:
Richard Fitzgeraldf7293112015-01-17 15:21:24 +0000251 case WM8280:
Charles Keepaxd2e74912013-11-15 13:48:23 +0000252 desc = &arizona_micsupp_ext;
253 micsupp->init_data = arizona_micsupp_ext_default;
254 break;
255 default:
256 desc = &arizona_micsupp;
257 micsupp->init_data = arizona_micsupp_default;
258 break;
259 }
260
Mark Brownb667a452012-06-14 18:14:00 +0100261 micsupp->init_data.consumer_supplies = &micsupp->supply;
262 micsupp->supply.supply = "MICVDD";
263 micsupp->supply.dev_name = dev_name(arizona->dev);
264
265 config.dev = arizona->dev;
266 config.driver_data = micsupp;
267 config.regmap = arizona->regmap;
268
Charles Keepax36bcdf12014-04-16 10:01:41 +0100269 if (IS_ENABLED(CONFIG_OF)) {
270 if (!dev_get_platdata(arizona->dev)) {
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100271 ret = arizona_micsupp_of_get_pdata(arizona, &config,
272 desc);
Charles Keepax36bcdf12014-04-16 10:01:41 +0100273 if (ret < 0)
274 return ret;
275 }
276 }
277
Mark Brownb667a452012-06-14 18:14:00 +0100278 if (arizona->pdata.micvdd)
279 config.init_data = arizona->pdata.micvdd;
280 else
281 config.init_data = &micsupp->init_data;
282
Mark Brown6dc027c2012-07-04 12:50:02 +0100283 /* Default to regulated mode until the API supports bypass */
284 regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1,
285 ARIZONA_CPMIC_BYPASS, 0);
286
Mark Brownb6b77092013-08-31 11:57:00 +0100287 micsupp->regulator = devm_regulator_register(&pdev->dev,
Charles Keepaxd2e74912013-11-15 13:48:23 +0000288 desc,
Mark Brownb6b77092013-08-31 11:57:00 +0100289 &config);
Charles Keepaxa7b976a2015-02-20 16:08:44 +0000290
291 of_node_put(config.of_node);
292
Mark Brownb667a452012-06-14 18:14:00 +0100293 if (IS_ERR(micsupp->regulator)) {
294 ret = PTR_ERR(micsupp->regulator);
295 dev_err(arizona->dev, "Failed to register mic supply: %d\n",
296 ret);
297 return ret;
298 }
299
300 platform_set_drvdata(pdev, micsupp);
301
302 return 0;
303}
304
Mark Brownb667a452012-06-14 18:14:00 +0100305static struct platform_driver arizona_micsupp_driver = {
306 .probe = arizona_micsupp_probe,
Mark Brownb667a452012-06-14 18:14:00 +0100307 .driver = {
308 .name = "arizona-micsupp",
Mark Brownb667a452012-06-14 18:14:00 +0100309 },
310};
311
312module_platform_driver(arizona_micsupp_driver);
313
314/* Module information */
315MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
316MODULE_DESCRIPTION("Arizona microphone supply driver");
317MODULE_LICENSE("GPL");
318MODULE_ALIAS("platform:arizona-micsupp");