xref: /openbmc/linux/sound/soc/uniphier/evea.c (revision 09ad28a3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Socionext UniPhier EVEA ADC/DAC codec driver.
4  *
5  * Copyright (c) 2016-2017 Socionext Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; version 2
10  * of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <linux/clk.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/regmap.h>
25 #include <linux/reset.h>
26 #include <sound/pcm.h>
27 #include <sound/soc.h>
28 
29 #define DRV_NAME        "evea"
30 #define EVEA_RATES      SNDRV_PCM_RATE_48000
31 #define EVEA_FORMATS    SNDRV_PCM_FMTBIT_S32_LE
32 
33 #define AADCPOW(n)                           (0x0078 + 0x04 * (n))
34 #define   AADCPOW_AADC_POWD                   BIT(0)
35 #define AHPOUTPOW                            0x0098
36 #define   AHPOUTPOW_HP_ON                     BIT(4)
37 #define ALINEPOW                             0x009c
38 #define   ALINEPOW_LIN2_POWD                  BIT(3)
39 #define   ALINEPOW_LIN1_POWD                  BIT(4)
40 #define ALO1OUTPOW                           0x00a8
41 #define   ALO1OUTPOW_LO1_ON                   BIT(4)
42 #define ALO2OUTPOW                           0x00ac
43 #define   ALO2OUTPOW_ADAC2_MUTE               BIT(0)
44 #define   ALO2OUTPOW_LO2_ON                   BIT(4)
45 #define AANAPOW                              0x00b8
46 #define   AANAPOW_A_POWD                      BIT(4)
47 #define ADACSEQ1(n)                          (0x0144 + 0x40 * (n))
48 #define   ADACSEQ1_MMUTE                      BIT(1)
49 #define ADACSEQ2(n)                          (0x0160 + 0x40 * (n))
50 #define   ADACSEQ2_ADACIN_FIX                 BIT(0)
51 #define ADAC1ODC                             0x0200
52 #define   ADAC1ODC_HP_DIS_RES_MASK            GENMASK(2, 1)
53 #define   ADAC1ODC_HP_DIS_RES_OFF             (0x0 << 1)
54 #define   ADAC1ODC_HP_DIS_RES_ON              (0x3 << 1)
55 #define   ADAC1ODC_ADAC_RAMPCLT_MASK          GENMASK(8, 7)
56 #define   ADAC1ODC_ADAC_RAMPCLT_NORMAL        (0x0 << 7)
57 #define   ADAC1ODC_ADAC_RAMPCLT_REDUCE        (0x1 << 7)
58 
59 struct evea_priv {
60 	struct clk *clk, *clk_exiv;
61 	struct reset_control *rst, *rst_exiv, *rst_adamv;
62 	struct regmap *regmap;
63 
64 	int switch_lin;
65 	int switch_lo;
66 	int switch_hp;
67 };
68 
69 static const struct snd_soc_dapm_widget evea_widgets[] = {
70 	SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
71 	SND_SOC_DAPM_INPUT("LIN1_LP"),
72 	SND_SOC_DAPM_INPUT("LIN1_RP"),
73 	SND_SOC_DAPM_INPUT("LIN2_LP"),
74 	SND_SOC_DAPM_INPUT("LIN2_RP"),
75 	SND_SOC_DAPM_INPUT("LIN3_LP"),
76 	SND_SOC_DAPM_INPUT("LIN3_RP"),
77 
78 	SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
79 	SND_SOC_DAPM_OUTPUT("HP1_L"),
80 	SND_SOC_DAPM_OUTPUT("HP1_R"),
81 	SND_SOC_DAPM_OUTPUT("LO2_L"),
82 	SND_SOC_DAPM_OUTPUT("LO2_R"),
83 };
84 
85 static const struct snd_soc_dapm_route evea_routes[] = {
86 	{ "ADC", NULL, "LIN1_LP" },
87 	{ "ADC", NULL, "LIN1_RP" },
88 	{ "ADC", NULL, "LIN2_LP" },
89 	{ "ADC", NULL, "LIN2_RP" },
90 	{ "ADC", NULL, "LIN3_LP" },
91 	{ "ADC", NULL, "LIN3_RP" },
92 
93 	{ "HP1_L", NULL, "DAC" },
94 	{ "HP1_R", NULL, "DAC" },
95 	{ "LO2_L", NULL, "DAC" },
96 	{ "LO2_R", NULL, "DAC" },
97 };
98 
99 static void evea_set_power_state_on(struct evea_priv *evea)
100 {
101 	struct regmap *map = evea->regmap;
102 
103 	regmap_update_bits(map, AANAPOW, AANAPOW_A_POWD,
104 			   AANAPOW_A_POWD);
105 
106 	regmap_update_bits(map, ADAC1ODC, ADAC1ODC_HP_DIS_RES_MASK,
107 			   ADAC1ODC_HP_DIS_RES_ON);
108 
109 	regmap_update_bits(map, ADAC1ODC, ADAC1ODC_ADAC_RAMPCLT_MASK,
110 			   ADAC1ODC_ADAC_RAMPCLT_REDUCE);
111 
112 	regmap_update_bits(map, ADACSEQ2(0), ADACSEQ2_ADACIN_FIX, 0);
113 	regmap_update_bits(map, ADACSEQ2(1), ADACSEQ2_ADACIN_FIX, 0);
114 	regmap_update_bits(map, ADACSEQ2(2), ADACSEQ2_ADACIN_FIX, 0);
115 }
116 
117 static void evea_set_power_state_off(struct evea_priv *evea)
118 {
119 	struct regmap *map = evea->regmap;
120 
121 	regmap_update_bits(map, ADAC1ODC, ADAC1ODC_HP_DIS_RES_MASK,
122 			   ADAC1ODC_HP_DIS_RES_ON);
123 
124 	regmap_update_bits(map, ADACSEQ1(0), ADACSEQ1_MMUTE,
125 			   ADACSEQ1_MMUTE);
126 	regmap_update_bits(map, ADACSEQ1(1), ADACSEQ1_MMUTE,
127 			   ADACSEQ1_MMUTE);
128 	regmap_update_bits(map, ADACSEQ1(2), ADACSEQ1_MMUTE,
129 			   ADACSEQ1_MMUTE);
130 
131 	regmap_update_bits(map, ALO1OUTPOW, ALO1OUTPOW_LO1_ON, 0);
132 	regmap_update_bits(map, ALO2OUTPOW, ALO2OUTPOW_LO2_ON, 0);
133 	regmap_update_bits(map, AHPOUTPOW, AHPOUTPOW_HP_ON, 0);
134 }
135 
136 static int evea_update_switch_lin(struct evea_priv *evea)
137 {
138 	struct regmap *map = evea->regmap;
139 
140 	if (evea->switch_lin) {
141 		regmap_update_bits(map, ALINEPOW,
142 				   ALINEPOW_LIN2_POWD | ALINEPOW_LIN1_POWD,
143 				   ALINEPOW_LIN2_POWD | ALINEPOW_LIN1_POWD);
144 
145 		regmap_update_bits(map, AADCPOW(0), AADCPOW_AADC_POWD,
146 				   AADCPOW_AADC_POWD);
147 		regmap_update_bits(map, AADCPOW(1), AADCPOW_AADC_POWD,
148 				   AADCPOW_AADC_POWD);
149 	} else {
150 		regmap_update_bits(map, AADCPOW(0), AADCPOW_AADC_POWD, 0);
151 		regmap_update_bits(map, AADCPOW(1), AADCPOW_AADC_POWD, 0);
152 
153 		regmap_update_bits(map, ALINEPOW,
154 				   ALINEPOW_LIN2_POWD | ALINEPOW_LIN1_POWD, 0);
155 	}
156 
157 	return 0;
158 }
159 
160 static int evea_update_switch_lo(struct evea_priv *evea)
161 {
162 	struct regmap *map = evea->regmap;
163 
164 	if (evea->switch_lo) {
165 		regmap_update_bits(map, ADACSEQ1(0), ADACSEQ1_MMUTE, 0);
166 		regmap_update_bits(map, ADACSEQ1(2), ADACSEQ1_MMUTE, 0);
167 
168 		regmap_update_bits(map, ALO1OUTPOW, ALO1OUTPOW_LO1_ON,
169 				   ALO1OUTPOW_LO1_ON);
170 		regmap_update_bits(map, ALO2OUTPOW,
171 				   ALO2OUTPOW_ADAC2_MUTE | ALO2OUTPOW_LO2_ON,
172 				   ALO2OUTPOW_ADAC2_MUTE | ALO2OUTPOW_LO2_ON);
173 	} else {
174 		regmap_update_bits(map, ADACSEQ1(0), ADACSEQ1_MMUTE,
175 				   ADACSEQ1_MMUTE);
176 		regmap_update_bits(map, ADACSEQ1(2), ADACSEQ1_MMUTE,
177 				   ADACSEQ1_MMUTE);
178 
179 		regmap_update_bits(map, ALO1OUTPOW, ALO1OUTPOW_LO1_ON, 0);
180 		regmap_update_bits(map, ALO2OUTPOW,
181 				   ALO2OUTPOW_ADAC2_MUTE | ALO2OUTPOW_LO2_ON,
182 				   0);
183 	}
184 
185 	return 0;
186 }
187 
188 static int evea_update_switch_hp(struct evea_priv *evea)
189 {
190 	struct regmap *map = evea->regmap;
191 
192 	if (evea->switch_hp) {
193 		regmap_update_bits(map, ADACSEQ1(1), ADACSEQ1_MMUTE, 0);
194 
195 		regmap_update_bits(map, AHPOUTPOW, AHPOUTPOW_HP_ON,
196 				   AHPOUTPOW_HP_ON);
197 
198 		regmap_update_bits(map, ADAC1ODC, ADAC1ODC_HP_DIS_RES_MASK,
199 				   ADAC1ODC_HP_DIS_RES_OFF);
200 	} else {
201 		regmap_update_bits(map, ADAC1ODC, ADAC1ODC_HP_DIS_RES_MASK,
202 				   ADAC1ODC_HP_DIS_RES_ON);
203 
204 		regmap_update_bits(map, ADACSEQ1(1), ADACSEQ1_MMUTE,
205 				   ADACSEQ1_MMUTE);
206 
207 		regmap_update_bits(map, AHPOUTPOW, AHPOUTPOW_HP_ON, 0);
208 	}
209 
210 	return 0;
211 }
212 
213 static void evea_update_switch_all(struct evea_priv *evea)
214 {
215 	evea_update_switch_lin(evea);
216 	evea_update_switch_lo(evea);
217 	evea_update_switch_hp(evea);
218 }
219 
220 static int evea_get_switch_lin(struct snd_kcontrol *kcontrol,
221 			       struct snd_ctl_elem_value *ucontrol)
222 {
223 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
224 	struct evea_priv *evea = snd_soc_component_get_drvdata(component);
225 
226 	ucontrol->value.integer.value[0] = evea->switch_lin;
227 
228 	return 0;
229 }
230 
231 static int evea_set_switch_lin(struct snd_kcontrol *kcontrol,
232 			       struct snd_ctl_elem_value *ucontrol)
233 {
234 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
235 	struct evea_priv *evea = snd_soc_component_get_drvdata(component);
236 
237 	if (evea->switch_lin == ucontrol->value.integer.value[0])
238 		return 0;
239 
240 	evea->switch_lin = ucontrol->value.integer.value[0];
241 
242 	return evea_update_switch_lin(evea);
243 }
244 
245 static int evea_get_switch_lo(struct snd_kcontrol *kcontrol,
246 			      struct snd_ctl_elem_value *ucontrol)
247 {
248 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
249 	struct evea_priv *evea = snd_soc_component_get_drvdata(component);
250 
251 	ucontrol->value.integer.value[0] = evea->switch_lo;
252 
253 	return 0;
254 }
255 
256 static int evea_set_switch_lo(struct snd_kcontrol *kcontrol,
257 			      struct snd_ctl_elem_value *ucontrol)
258 {
259 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
260 	struct evea_priv *evea = snd_soc_component_get_drvdata(component);
261 
262 	if (evea->switch_lo == ucontrol->value.integer.value[0])
263 		return 0;
264 
265 	evea->switch_lo = ucontrol->value.integer.value[0];
266 
267 	return evea_update_switch_lo(evea);
268 }
269 
270 static int evea_get_switch_hp(struct snd_kcontrol *kcontrol,
271 			      struct snd_ctl_elem_value *ucontrol)
272 {
273 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
274 	struct evea_priv *evea = snd_soc_component_get_drvdata(component);
275 
276 	ucontrol->value.integer.value[0] = evea->switch_hp;
277 
278 	return 0;
279 }
280 
281 static int evea_set_switch_hp(struct snd_kcontrol *kcontrol,
282 			      struct snd_ctl_elem_value *ucontrol)
283 {
284 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
285 	struct evea_priv *evea = snd_soc_component_get_drvdata(component);
286 
287 	if (evea->switch_hp == ucontrol->value.integer.value[0])
288 		return 0;
289 
290 	evea->switch_hp = ucontrol->value.integer.value[0];
291 
292 	return evea_update_switch_hp(evea);
293 }
294 
295 static const struct snd_kcontrol_new evea_controls[] = {
296 	SOC_SINGLE_BOOL_EXT("Line Capture Switch", 0,
297 			    evea_get_switch_lin, evea_set_switch_lin),
298 	SOC_SINGLE_BOOL_EXT("Line Playback Switch", 0,
299 			    evea_get_switch_lo, evea_set_switch_lo),
300 	SOC_SINGLE_BOOL_EXT("Headphone Playback Switch", 0,
301 			    evea_get_switch_hp, evea_set_switch_hp),
302 };
303 
304 static int evea_codec_probe(struct snd_soc_component *component)
305 {
306 	struct evea_priv *evea = snd_soc_component_get_drvdata(component);
307 
308 	evea->switch_lin = 1;
309 	evea->switch_lo = 1;
310 	evea->switch_hp = 1;
311 
312 	evea_set_power_state_on(evea);
313 	evea_update_switch_all(evea);
314 
315 	return 0;
316 }
317 
318 static int evea_codec_suspend(struct snd_soc_component *component)
319 {
320 	struct evea_priv *evea = snd_soc_component_get_drvdata(component);
321 
322 	evea_set_power_state_off(evea);
323 
324 	reset_control_assert(evea->rst_adamv);
325 	reset_control_assert(evea->rst_exiv);
326 	reset_control_assert(evea->rst);
327 
328 	clk_disable_unprepare(evea->clk_exiv);
329 	clk_disable_unprepare(evea->clk);
330 
331 	return 0;
332 }
333 
334 static int evea_codec_resume(struct snd_soc_component *component)
335 {
336 	struct evea_priv *evea = snd_soc_component_get_drvdata(component);
337 	int ret;
338 
339 	ret = clk_prepare_enable(evea->clk);
340 	if (ret)
341 		return ret;
342 
343 	ret = clk_prepare_enable(evea->clk_exiv);
344 	if (ret)
345 		goto err_out_clock;
346 
347 	ret = reset_control_deassert(evea->rst);
348 	if (ret)
349 		goto err_out_clock_exiv;
350 
351 	ret = reset_control_deassert(evea->rst_exiv);
352 	if (ret)
353 		goto err_out_reset;
354 
355 	ret = reset_control_deassert(evea->rst_adamv);
356 	if (ret)
357 		goto err_out_reset_exiv;
358 
359 	evea_set_power_state_on(evea);
360 	evea_update_switch_all(evea);
361 
362 	return 0;
363 
364 err_out_reset_exiv:
365 	reset_control_assert(evea->rst_exiv);
366 
367 err_out_reset:
368 	reset_control_assert(evea->rst);
369 
370 err_out_clock_exiv:
371 	clk_disable_unprepare(evea->clk_exiv);
372 
373 err_out_clock:
374 	clk_disable_unprepare(evea->clk);
375 
376 	return ret;
377 }
378 
379 static struct snd_soc_component_driver soc_codec_evea = {
380 	.probe			= evea_codec_probe,
381 	.suspend		= evea_codec_suspend,
382 	.resume			= evea_codec_resume,
383 	.dapm_widgets		= evea_widgets,
384 	.num_dapm_widgets	= ARRAY_SIZE(evea_widgets),
385 	.dapm_routes		= evea_routes,
386 	.num_dapm_routes	= ARRAY_SIZE(evea_routes),
387 	.controls		= evea_controls,
388 	.num_controls		= ARRAY_SIZE(evea_controls),
389 	.idle_bias_on		= 1,
390 	.use_pmdown_time	= 1,
391 	.endianness		= 1,
392 	.non_legacy_dai_naming	= 1,
393 };
394 
395 static struct snd_soc_dai_driver soc_dai_evea[] = {
396 	{
397 		.name     = DRV_NAME "-line1",
398 		.playback = {
399 			.stream_name  = "Line Out 1",
400 			.formats      = EVEA_FORMATS,
401 			.rates        = EVEA_RATES,
402 			.channels_min = 2,
403 			.channels_max = 2,
404 		},
405 		.capture = {
406 			.stream_name  = "Line In 1",
407 			.formats      = EVEA_FORMATS,
408 			.rates        = EVEA_RATES,
409 			.channels_min = 2,
410 			.channels_max = 2,
411 		},
412 	},
413 	{
414 		.name     = DRV_NAME "-hp1",
415 		.playback = {
416 			.stream_name  = "Headphone 1",
417 			.formats      = EVEA_FORMATS,
418 			.rates        = EVEA_RATES,
419 			.channels_min = 2,
420 			.channels_max = 2,
421 		},
422 	},
423 	{
424 		.name     = DRV_NAME "-lo2",
425 		.playback = {
426 			.stream_name  = "Line Out 2",
427 			.formats      = EVEA_FORMATS,
428 			.rates        = EVEA_RATES,
429 			.channels_min = 2,
430 			.channels_max = 2,
431 		},
432 	},
433 };
434 
435 static const struct regmap_config evea_regmap_config = {
436 	.reg_bits      = 32,
437 	.reg_stride    = 4,
438 	.val_bits      = 32,
439 	.max_register  = 0xffc,
440 	.cache_type    = REGCACHE_NONE,
441 };
442 
443 static int evea_probe(struct platform_device *pdev)
444 {
445 	struct evea_priv *evea;
446 	struct resource *res;
447 	void __iomem *preg;
448 	int ret;
449 
450 	evea = devm_kzalloc(&pdev->dev, sizeof(struct evea_priv), GFP_KERNEL);
451 	if (!evea)
452 		return -ENOMEM;
453 
454 	evea->clk = devm_clk_get(&pdev->dev, "evea");
455 	if (IS_ERR(evea->clk))
456 		return PTR_ERR(evea->clk);
457 
458 	evea->clk_exiv = devm_clk_get(&pdev->dev, "exiv");
459 	if (IS_ERR(evea->clk_exiv))
460 		return PTR_ERR(evea->clk_exiv);
461 
462 	evea->rst = devm_reset_control_get_shared(&pdev->dev, "evea");
463 	if (IS_ERR(evea->rst))
464 		return PTR_ERR(evea->rst);
465 
466 	evea->rst_exiv = devm_reset_control_get_shared(&pdev->dev, "exiv");
467 	if (IS_ERR(evea->rst_exiv))
468 		return PTR_ERR(evea->rst_exiv);
469 
470 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
471 	preg = devm_ioremap_resource(&pdev->dev, res);
472 	if (IS_ERR(preg))
473 		return PTR_ERR(preg);
474 
475 	evea->regmap = devm_regmap_init_mmio(&pdev->dev, preg,
476 					     &evea_regmap_config);
477 	if (IS_ERR(evea->regmap))
478 		return PTR_ERR(evea->regmap);
479 
480 	ret = clk_prepare_enable(evea->clk);
481 	if (ret)
482 		return ret;
483 
484 	ret = clk_prepare_enable(evea->clk_exiv);
485 	if (ret)
486 		goto err_out_clock;
487 
488 	ret = reset_control_deassert(evea->rst);
489 	if (ret)
490 		goto err_out_clock_exiv;
491 
492 	ret = reset_control_deassert(evea->rst_exiv);
493 	if (ret)
494 		goto err_out_reset;
495 
496 	/* ADAMV will hangup if EXIV reset is asserted */
497 	evea->rst_adamv = devm_reset_control_get_shared(&pdev->dev, "adamv");
498 	if (IS_ERR(evea->rst_adamv)) {
499 		ret = PTR_ERR(evea->rst_adamv);
500 		goto err_out_reset_exiv;
501 	}
502 
503 	ret = reset_control_deassert(evea->rst_adamv);
504 	if (ret)
505 		goto err_out_reset_exiv;
506 
507 	platform_set_drvdata(pdev, evea);
508 
509 	ret = devm_snd_soc_register_component(&pdev->dev, &soc_codec_evea,
510 				     soc_dai_evea, ARRAY_SIZE(soc_dai_evea));
511 	if (ret)
512 		goto err_out_reset_adamv;
513 
514 	return 0;
515 
516 err_out_reset_adamv:
517 	reset_control_assert(evea->rst_adamv);
518 
519 err_out_reset_exiv:
520 	reset_control_assert(evea->rst_exiv);
521 
522 err_out_reset:
523 	reset_control_assert(evea->rst);
524 
525 err_out_clock_exiv:
526 	clk_disable_unprepare(evea->clk_exiv);
527 
528 err_out_clock:
529 	clk_disable_unprepare(evea->clk);
530 
531 	return ret;
532 }
533 
534 static int evea_remove(struct platform_device *pdev)
535 {
536 	struct evea_priv *evea = platform_get_drvdata(pdev);
537 
538 	reset_control_assert(evea->rst_adamv);
539 	reset_control_assert(evea->rst_exiv);
540 	reset_control_assert(evea->rst);
541 
542 	clk_disable_unprepare(evea->clk_exiv);
543 	clk_disable_unprepare(evea->clk);
544 
545 	return 0;
546 }
547 
548 static const struct of_device_id evea_of_match[] = {
549 	{ .compatible = "socionext,uniphier-evea", },
550 	{}
551 };
552 MODULE_DEVICE_TABLE(of, evea_of_match);
553 
554 static struct platform_driver evea_codec_driver = {
555 	.driver = {
556 		.name = DRV_NAME,
557 		.of_match_table = of_match_ptr(evea_of_match),
558 	},
559 	.probe  = evea_probe,
560 	.remove = evea_remove,
561 };
562 module_platform_driver(evea_codec_driver);
563 
564 MODULE_AUTHOR("Katsuhiro Suzuki <suzuki.katsuhiro@socionext.com>");
565 MODULE_DESCRIPTION("UniPhier EVEA codec driver");
566 MODULE_LICENSE("GPL v2");
567