blob: 6fdd9bf6927fcb39763156d34d2747c34ff2ebee [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>
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>
Mark Browne6ed9052013-01-10 19:14:11 +000024#include <linux/workqueue.h>
25#include <sound/soc.h>
Mark Brownb667a452012-06-14 18:14:00 +010026
27#include <linux/mfd/arizona/core.h>
28#include <linux/mfd/arizona/pdata.h>
29#include <linux/mfd/arizona/registers.h>
30
Mark Brownb667a452012-06-14 18:14:00 +010031struct arizona_micsupp {
32 struct regulator_dev *regulator;
33 struct arizona *arizona;
34
35 struct regulator_consumer_supply supply;
36 struct regulator_init_data init_data;
Mark Browne6ed9052013-01-10 19:14:11 +000037
38 struct work_struct check_cp_work;
Mark Brownb667a452012-06-14 18:14:00 +010039};
40
Mark Browne6ed9052013-01-10 19:14:11 +000041static void arizona_micsupp_check_cp(struct work_struct *work)
42{
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;
50
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;
55 }
56
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");
63
64 snd_soc_dapm_sync(dapm);
65 }
66}
67
68static int arizona_micsupp_enable(struct regulator_dev *rdev)
69{
70 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
71 int ret;
72
73 ret = regulator_enable_regmap(rdev);
74
75 if (ret == 0)
76 schedule_work(&micsupp->check_cp_work);
77
78 return ret;
79}
80
81static int arizona_micsupp_disable(struct regulator_dev *rdev)
82{
83 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
84 int ret;
85
86 ret = regulator_disable_regmap(rdev);
87 if (ret == 0)
88 schedule_work(&micsupp->check_cp_work);
89
90 return ret;
91}
92
93static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena)
94{
95 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
96 int ret;
97
98 ret = regulator_set_bypass_regmap(rdev, ena);
99 if (ret == 0)
100 schedule_work(&micsupp->check_cp_work);
101
102 return ret;
103}
104
Mark Brownb667a452012-06-14 18:14:00 +0100105static struct regulator_ops arizona_micsupp_ops = {
Mark Browne6ed9052013-01-10 19:14:11 +0000106 .enable = arizona_micsupp_enable,
107 .disable = arizona_micsupp_disable,
Mark Brownb667a452012-06-14 18:14:00 +0100108 .is_enabled = regulator_is_enabled_regmap,
109
Charles Keepax71979aa2013-11-15 13:13:32 +0000110 .list_voltage = regulator_list_voltage_linear_range,
111 .map_voltage = regulator_map_voltage_linear_range,
Mark Brownb667a452012-06-14 18:14:00 +0100112
113 .get_voltage_sel = regulator_get_voltage_sel_regmap,
114 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Browne477ce02012-08-27 16:04:47 -0700115
116 .get_bypass = regulator_get_bypass_regmap,
Mark Browne6ed9052013-01-10 19:14:11 +0000117 .set_bypass = arizona_micsupp_set_bypass,
Mark Brownb667a452012-06-14 18:14:00 +0100118};
119
Charles Keepax71979aa2013-11-15 13:13:32 +0000120static const struct regulator_linear_range arizona_micsupp_ranges[] = {
121 REGULATOR_LINEAR_RANGE(1700000, 0, 0x1e, 50000),
122 REGULATOR_LINEAR_RANGE(3300000, 0x1f, 0x1f, 0),
123};
124
Mark Brownb667a452012-06-14 18:14:00 +0100125static const struct regulator_desc arizona_micsupp = {
126 .name = "MICVDD",
127 .supply_name = "CPVDD",
128 .type = REGULATOR_VOLTAGE,
Charles Keepax71979aa2013-11-15 13:13:32 +0000129 .n_voltages = 32,
Mark Brownb667a452012-06-14 18:14:00 +0100130 .ops = &arizona_micsupp_ops,
131
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,
Mark Browne477ce02012-08-27 16:04:47 -0700136 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
137 .bypass_mask = ARIZONA_CPMIC_BYPASS,
Mark Brownb667a452012-06-14 18:14:00 +0100138
Charles Keepax71979aa2013-11-15 13:13:32 +0000139 .linear_ranges = arizona_micsupp_ranges,
140 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ranges),
141
Mark Brown95072812012-11-27 14:55:49 +0000142 .enable_time = 3000,
143
Mark Brownb667a452012-06-14 18:14:00 +0100144 .owner = THIS_MODULE,
145};
146
Charles Keepaxd2e74912013-11-15 13:48:23 +0000147static 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),
150};
151
152static 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,
158
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,
165
166 .linear_ranges = arizona_micsupp_ext_ranges,
167 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ext_ranges),
168
169 .enable_time = 3000,
170
171 .owner = THIS_MODULE,
172};
173
Mark Brownb667a452012-06-14 18:14:00 +0100174static const struct regulator_init_data arizona_micsupp_default = {
175 .constraints = {
176 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
Mark Brown9fc50a22013-01-10 19:31:47 +0000177 REGULATOR_CHANGE_VOLTAGE |
178 REGULATOR_CHANGE_BYPASS,
Mark Brownb667a452012-06-14 18:14:00 +0100179 .min_uV = 1700000,
180 .max_uV = 3300000,
181 },
182
183 .num_consumer_supplies = 1,
184};
185
Charles Keepaxd2e74912013-11-15 13:48:23 +0000186static 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,
193 },
194
195 .num_consumer_supplies = 1,
196};
197
Bill Pembertona5023572012-11-19 13:22:22 -0500198static int arizona_micsupp_probe(struct platform_device *pdev)
Mark Brownb667a452012-06-14 18:14:00 +0100199{
200 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
Charles Keepaxd2e74912013-11-15 13:48:23 +0000201 const struct regulator_desc *desc;
Mark Brownb667a452012-06-14 18:14:00 +0100202 struct regulator_config config = { };
203 struct arizona_micsupp *micsupp;
204 int ret;
205
206 micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
Sachin Kamat820cd312014-02-18 16:11:10 +0530207 if (!micsupp)
Mark Brownb667a452012-06-14 18:14:00 +0100208 return -ENOMEM;
Mark Brownb667a452012-06-14 18:14:00 +0100209
210 micsupp->arizona = arizona;
Mark Browne6ed9052013-01-10 19:14:11 +0000211 INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
Mark Brownb667a452012-06-14 18:14:00 +0100212
213 /*
214 * Since the chip usually supplies itself we provide some
215 * default init_data for it. This will be overridden with
216 * platform data if provided.
217 */
Charles Keepaxd2e74912013-11-15 13:48:23 +0000218 switch (arizona->type) {
219 case WM5110:
220 desc = &arizona_micsupp_ext;
221 micsupp->init_data = arizona_micsupp_ext_default;
222 break;
223 default:
224 desc = &arizona_micsupp;
225 micsupp->init_data = arizona_micsupp_default;
226 break;
227 }
228
Mark Brownb667a452012-06-14 18:14:00 +0100229 micsupp->init_data.consumer_supplies = &micsupp->supply;
230 micsupp->supply.supply = "MICVDD";
231 micsupp->supply.dev_name = dev_name(arizona->dev);
232
233 config.dev = arizona->dev;
234 config.driver_data = micsupp;
235 config.regmap = arizona->regmap;
236
237 if (arizona->pdata.micvdd)
238 config.init_data = arizona->pdata.micvdd;
239 else
240 config.init_data = &micsupp->init_data;
241
Mark Brown6dc027c2012-07-04 12:50:02 +0100242 /* Default to regulated mode until the API supports bypass */
243 regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1,
244 ARIZONA_CPMIC_BYPASS, 0);
245
Mark Brownb6b77092013-08-31 11:57:00 +0100246 micsupp->regulator = devm_regulator_register(&pdev->dev,
Charles Keepaxd2e74912013-11-15 13:48:23 +0000247 desc,
Mark Brownb6b77092013-08-31 11:57:00 +0100248 &config);
Mark Brownb667a452012-06-14 18:14:00 +0100249 if (IS_ERR(micsupp->regulator)) {
250 ret = PTR_ERR(micsupp->regulator);
251 dev_err(arizona->dev, "Failed to register mic supply: %d\n",
252 ret);
253 return ret;
254 }
255
256 platform_set_drvdata(pdev, micsupp);
257
258 return 0;
259}
260
Mark Brownb667a452012-06-14 18:14:00 +0100261static struct platform_driver arizona_micsupp_driver = {
262 .probe = arizona_micsupp_probe,
Mark Brownb667a452012-06-14 18:14:00 +0100263 .driver = {
264 .name = "arizona-micsupp",
265 .owner = THIS_MODULE,
266 },
267};
268
269module_platform_driver(arizona_micsupp_driver);
270
271/* Module information */
272MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
273MODULE_DESCRIPTION("Arizona microphone supply driver");
274MODULE_LICENSE("GPL");
275MODULE_ALIAS("platform:arizona-micsupp");