1 // 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>
8 
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>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/machine.h>
18 #include <linux/regulator/of_regulator.h>
19 #include <linux/gpio.h>
20 #include <linux/slab.h>
21 #include <linux/workqueue.h>
22 #include <sound/soc.h>
23 
24 #include <linux/mfd/arizona/core.h>
25 #include <linux/mfd/arizona/pdata.h>
26 #include <linux/mfd/arizona/registers.h>
27 
28 #include <linux/regulator/arizona-micsupp.h>
29 
30 struct arizona_micsupp {
31 	struct regulator_dev *regulator;
32 	struct regmap *regmap;
33 	struct snd_soc_dapm_context **dapm;
34 	unsigned int enable_reg;
35 	struct device *dev;
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->dapm;
48 	struct snd_soc_component *component;
49 	unsigned int val;
50 	int ret;
51 
52 	ret = regmap_read(micsupp->regmap, micsupp->enable_reg, &val);
53 	if (ret != 0) {
54 		dev_err(micsupp->dev,
55 			"Failed to read CP state: %d\n", ret);
56 		return;
57 	}
58 
59 	if (dapm) {
60 		component = snd_soc_dapm_to_component(dapm);
61 
62 		if ((val & (ARIZONA_CPMIC_ENA | ARIZONA_CPMIC_BYPASS)) ==
63 		    ARIZONA_CPMIC_ENA)
64 			snd_soc_component_force_enable_pin(component,
65 							   "MICSUPP");
66 		else
67 			snd_soc_component_disable_pin(component, "MICSUPP");
68 
69 		snd_soc_dapm_sync(dapm);
70 	}
71 }
72 
73 static 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 
86 static 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 
98 static 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 
110 static const struct regulator_ops arizona_micsupp_ops = {
111 	.enable = arizona_micsupp_enable,
112 	.disable = arizona_micsupp_disable,
113 	.is_enabled = regulator_is_enabled_regmap,
114 
115 	.list_voltage = regulator_list_voltage_linear_range,
116 	.map_voltage = regulator_map_voltage_linear_range,
117 
118 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
119 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
120 
121 	.get_bypass = regulator_get_bypass_regmap,
122 	.set_bypass = arizona_micsupp_set_bypass,
123 };
124 
125 static 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 
130 static const struct regulator_desc arizona_micsupp = {
131 	.name = "MICVDD",
132 	.supply_name = "CPVDD",
133 	.type = REGULATOR_VOLTAGE,
134 	.n_voltages = 32,
135 	.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,
141 	.bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
142 	.bypass_mask = ARIZONA_CPMIC_BYPASS,
143 
144 	.linear_ranges = arizona_micsupp_ranges,
145 	.n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ranges),
146 
147 	.enable_time = 3000,
148 
149 	.owner = THIS_MODULE,
150 };
151 
152 static 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 
157 static 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 
179 static const struct regulator_init_data arizona_micsupp_default = {
180 	.constraints = {
181 		.valid_ops_mask = REGULATOR_CHANGE_STATUS |
182 				REGULATOR_CHANGE_VOLTAGE |
183 				REGULATOR_CHANGE_BYPASS,
184 		.min_uV = 1700000,
185 		.max_uV = 3300000,
186 	},
187 
188 	.num_consumer_supplies = 1,
189 };
190 
191 static 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 
203 static int arizona_micsupp_of_get_pdata(struct arizona_micsupp_pdata *pdata,
204 					struct regulator_config *config,
205 					const struct regulator_desc *desc)
206 {
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(config->dev->of_node, "micvdd");
212 
213 	if (np) {
214 		config->of_node = np;
215 
216 		init_data = of_get_regulator_init_data(config->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->init_data = init_data;
223 		}
224 	}
225 
226 	return 0;
227 }
228 
229 static 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 
284 static int arizona_micsupp_probe(struct platform_device *pdev)
285 {
286 	struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
287 	const struct regulator_desc *desc;
288 	struct arizona_micsupp *micsupp;
289 
290 	micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
291 	if (!micsupp)
292 		return -ENOMEM;
293 
294 	micsupp->regmap = arizona->regmap;
295 	micsupp->dapm = &arizona->dapm;
296 	micsupp->dev = arizona->dev;
297 
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 	 */
303 	switch (arizona->type) {
304 	case WM5110:
305 	case WM8280:
306 		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 
315 	return arizona_micsupp_common_init(pdev, micsupp, desc,
316 					   &arizona->pdata.micvdd);
317 }
318 
319 static struct platform_driver arizona_micsupp_driver = {
320 	.probe = arizona_micsupp_probe,
321 	.driver		= {
322 		.name	= "arizona-micsupp",
323 	},
324 };
325 
326 module_platform_driver(arizona_micsupp_driver);
327 
328 /* Module information */
329 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
330 MODULE_DESCRIPTION("Arizona microphone supply driver");
331 MODULE_LICENSE("GPL");
332 MODULE_ALIAS("platform:arizona-micsupp");
333