xref: /openbmc/linux/sound/soc/uniphier/evea.c (revision ddc141e5)
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_codec *codec = snd_soc_kcontrol_codec(kcontrol);
224 	struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
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_codec *codec = snd_soc_kcontrol_codec(kcontrol);
235 	struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
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_codec *codec = snd_soc_kcontrol_codec(kcontrol);
249 	struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
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_codec *codec = snd_soc_kcontrol_codec(kcontrol);
260 	struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
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_codec *codec = snd_soc_kcontrol_codec(kcontrol);
274 	struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
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_codec *codec = snd_soc_kcontrol_codec(kcontrol);
285 	struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
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 eva_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_codec *codec)
305 {
306 	struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
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_codec *codec)
319 {
320 	struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
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_codec *codec)
335 {
336 	struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
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_codec_driver soc_codec_evea = {
380 	.probe   = evea_codec_probe,
381 	.suspend = evea_codec_suspend,
382 	.resume  = evea_codec_resume,
383 
384 	.component_driver = {
385 		.dapm_widgets = evea_widgets,
386 		.num_dapm_widgets = ARRAY_SIZE(evea_widgets),
387 		.dapm_routes = evea_routes,
388 		.num_dapm_routes = ARRAY_SIZE(evea_routes),
389 		.controls = eva_controls,
390 		.num_controls = ARRAY_SIZE(eva_controls),
391 	},
392 };
393 
394 static struct snd_soc_dai_driver soc_dai_evea[] = {
395 	{
396 		.name     = DRV_NAME "-line1",
397 		.playback = {
398 			.stream_name  = "Line Out 1",
399 			.formats      = EVEA_FORMATS,
400 			.rates        = EVEA_RATES,
401 			.channels_min = 2,
402 			.channels_max = 2,
403 		},
404 		.capture = {
405 			.stream_name  = "Line In 1",
406 			.formats      = EVEA_FORMATS,
407 			.rates        = EVEA_RATES,
408 			.channels_min = 2,
409 			.channels_max = 2,
410 		},
411 	},
412 	{
413 		.name     = DRV_NAME "-hp1",
414 		.playback = {
415 			.stream_name  = "Headphone 1",
416 			.formats      = EVEA_FORMATS,
417 			.rates        = EVEA_RATES,
418 			.channels_min = 2,
419 			.channels_max = 2,
420 		},
421 	},
422 	{
423 		.name     = DRV_NAME "-lo2",
424 		.playback = {
425 			.stream_name  = "Line Out 2",
426 			.formats      = EVEA_FORMATS,
427 			.rates        = EVEA_RATES,
428 			.channels_min = 2,
429 			.channels_max = 2,
430 		},
431 	},
432 };
433 
434 static const struct regmap_config evea_regmap_config = {
435 	.reg_bits      = 32,
436 	.reg_stride    = 4,
437 	.val_bits      = 32,
438 	.max_register  = 0xffc,
439 	.cache_type    = REGCACHE_NONE,
440 };
441 
442 static int evea_probe(struct platform_device *pdev)
443 {
444 	struct evea_priv *evea;
445 	struct resource *res;
446 	void __iomem *preg;
447 	int ret;
448 
449 	evea = devm_kzalloc(&pdev->dev, sizeof(struct evea_priv), GFP_KERNEL);
450 	if (!evea)
451 		return -ENOMEM;
452 
453 	evea->clk = devm_clk_get(&pdev->dev, "evea");
454 	if (IS_ERR(evea->clk))
455 		return PTR_ERR(evea->clk);
456 
457 	evea->clk_exiv = devm_clk_get(&pdev->dev, "exiv");
458 	if (IS_ERR(evea->clk_exiv))
459 		return PTR_ERR(evea->clk_exiv);
460 
461 	evea->rst = devm_reset_control_get_shared(&pdev->dev, "evea");
462 	if (IS_ERR(evea->rst))
463 		return PTR_ERR(evea->rst);
464 
465 	evea->rst_exiv = devm_reset_control_get_shared(&pdev->dev, "exiv");
466 	if (IS_ERR(evea->rst_exiv))
467 		return PTR_ERR(evea->rst_exiv);
468 
469 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
470 	preg = devm_ioremap_resource(&pdev->dev, res);
471 	if (IS_ERR(preg))
472 		return PTR_ERR(preg);
473 
474 	evea->regmap = devm_regmap_init_mmio(&pdev->dev, preg,
475 					     &evea_regmap_config);
476 	if (IS_ERR(evea->regmap))
477 		return PTR_ERR(evea->regmap);
478 
479 	ret = clk_prepare_enable(evea->clk);
480 	if (ret)
481 		return ret;
482 
483 	ret = clk_prepare_enable(evea->clk_exiv);
484 	if (ret)
485 		goto err_out_clock;
486 
487 	ret = reset_control_deassert(evea->rst);
488 	if (ret)
489 		goto err_out_clock_exiv;
490 
491 	ret = reset_control_deassert(evea->rst_exiv);
492 	if (ret)
493 		goto err_out_reset;
494 
495 	/* ADAMV will hangup if EXIV reset is asserted */
496 	evea->rst_adamv = devm_reset_control_get_shared(&pdev->dev, "adamv");
497 	if (IS_ERR(evea->rst_adamv)) {
498 		ret = PTR_ERR(evea->rst_adamv);
499 		goto err_out_reset_exiv;
500 	}
501 
502 	ret = reset_control_deassert(evea->rst_adamv);
503 	if (ret)
504 		goto err_out_reset_exiv;
505 
506 	platform_set_drvdata(pdev, evea);
507 
508 	ret = snd_soc_register_codec(&pdev->dev, &soc_codec_evea,
509 				     soc_dai_evea, ARRAY_SIZE(soc_dai_evea));
510 	if (ret)
511 		goto err_out_reset_adamv;
512 
513 	return 0;
514 
515 err_out_reset_adamv:
516 	reset_control_assert(evea->rst_adamv);
517 
518 err_out_reset_exiv:
519 	reset_control_assert(evea->rst_exiv);
520 
521 err_out_reset:
522 	reset_control_assert(evea->rst);
523 
524 err_out_clock_exiv:
525 	clk_disable_unprepare(evea->clk_exiv);
526 
527 err_out_clock:
528 	clk_disable_unprepare(evea->clk);
529 
530 	return ret;
531 }
532 
533 static int evea_remove(struct platform_device *pdev)
534 {
535 	struct evea_priv *evea = platform_get_drvdata(pdev);
536 
537 	snd_soc_unregister_codec(&pdev->dev);
538 
539 	reset_control_assert(evea->rst_adamv);
540 	reset_control_assert(evea->rst_exiv);
541 	reset_control_assert(evea->rst);
542 
543 	clk_disable_unprepare(evea->clk_exiv);
544 	clk_disable_unprepare(evea->clk);
545 
546 	return 0;
547 }
548 
549 static const struct of_device_id evea_of_match[] = {
550 	{ .compatible = "socionext,uniphier-evea", },
551 	{}
552 };
553 MODULE_DEVICE_TABLE(of, evea_of_match);
554 
555 static struct platform_driver evea_codec_driver = {
556 	.driver = {
557 		.name = DRV_NAME,
558 		.of_match_table = of_match_ptr(evea_of_match),
559 	},
560 	.probe  = evea_probe,
561 	.remove = evea_remove,
562 };
563 module_platform_driver(evea_codec_driver);
564 
565 MODULE_AUTHOR("Katsuhiro Suzuki <suzuki.katsuhiro@socionext.com>");
566 MODULE_DESCRIPTION("UniPhier EVEA codec driver");
567 MODULE_LICENSE("GPL v2");
568