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 arizona *arizona,
203 					struct regulator_config *config,
204 					const struct regulator_desc *desc)
205 {
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 
216 		init_data = of_get_regulator_init_data(arizona->dev, np, desc);
217 
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 
229 static int arizona_micsupp_probe(struct platform_device *pdev)
230 {
231 	struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
232 	const struct regulator_desc *desc;
233 	struct regulator_config config = { };
234 	struct arizona_micsupp *micsupp;
235 	int ret;
236 
237 	micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
238 	if (!micsupp)
239 		return -ENOMEM;
240 
241 	micsupp->arizona = arizona;
242 	INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
243 
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 	 */
249 	switch (arizona->type) {
250 	case WM5110:
251 	case WM8280:
252 		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 
261 	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 
269 	if (IS_ENABLED(CONFIG_OF)) {
270 		if (!dev_get_platdata(arizona->dev)) {
271 			ret = arizona_micsupp_of_get_pdata(arizona, &config,
272 							   desc);
273 			if (ret < 0)
274 				return ret;
275 		}
276 	}
277 
278 	if (arizona->pdata.micvdd)
279 		config.init_data = arizona->pdata.micvdd;
280 	else
281 		config.init_data = &micsupp->init_data;
282 
283 	/* 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 
287 	micsupp->regulator = devm_regulator_register(&pdev->dev,
288 						     desc,
289 						     &config);
290 
291 	of_node_put(config.of_node);
292 
293 	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 
305 static struct platform_driver arizona_micsupp_driver = {
306 	.probe = arizona_micsupp_probe,
307 	.driver		= {
308 		.name	= "arizona-micsupp",
309 	},
310 };
311 
312 module_platform_driver(arizona_micsupp_driver);
313 
314 /* Module information */
315 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
316 MODULE_DESCRIPTION("Arizona microphone supply driver");
317 MODULE_LICENSE("GPL");
318 MODULE_ALIAS("platform:arizona-micsupp");
319