1 // SPDX-License-Identifier: GPL-2.0
3 * Socionext UniPhier EVEA ADC/DAC codec driver.
5 * Copyright (c) 2016-2017 Socionext Inc.
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
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.
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/>.
21 #include <linux/clk.h>
22 #include <linux/module.h>
24 #include <linux/regmap.h>
25 #include <linux/reset.h>
26 #include <sound/pcm.h>
27 #include <sound/soc.h>
29 #define DRV_NAME "evea"
30 #define EVEA_RATES SNDRV_PCM_RATE_48000
31 #define EVEA_FORMATS SNDRV_PCM_FMTBIT_S32_LE
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)
60 struct clk *clk, *clk_exiv;
61 struct reset_control *rst, *rst_exiv, *rst_adamv;
62 struct regmap *regmap;
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"),
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"),
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" },
93 { "HP1_L", NULL, "DAC" },
94 { "HP1_R", NULL, "DAC" },
95 { "LO2_L", NULL, "DAC" },
96 { "LO2_R", NULL, "DAC" },
99 static void evea_set_power_state_on(struct evea_priv *evea)
101 struct regmap *map = evea->regmap;
103 regmap_update_bits(map, AANAPOW, AANAPOW_A_POWD,
106 regmap_update_bits(map, ADAC1ODC, ADAC1ODC_HP_DIS_RES_MASK,
107 ADAC1ODC_HP_DIS_RES_ON);
109 regmap_update_bits(map, ADAC1ODC, ADAC1ODC_ADAC_RAMPCLT_MASK,
110 ADAC1ODC_ADAC_RAMPCLT_REDUCE);
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);
117 static void evea_set_power_state_off(struct evea_priv *evea)
119 struct regmap *map = evea->regmap;
121 regmap_update_bits(map, ADAC1ODC, ADAC1ODC_HP_DIS_RES_MASK,
122 ADAC1ODC_HP_DIS_RES_ON);
124 regmap_update_bits(map, ADACSEQ1(0), ADACSEQ1_MMUTE,
126 regmap_update_bits(map, ADACSEQ1(1), ADACSEQ1_MMUTE,
128 regmap_update_bits(map, ADACSEQ1(2), ADACSEQ1_MMUTE,
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);
136 static int evea_update_switch_lin(struct evea_priv *evea)
138 struct regmap *map = evea->regmap;
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);
145 regmap_update_bits(map, AADCPOW(0), AADCPOW_AADC_POWD,
147 regmap_update_bits(map, AADCPOW(1), AADCPOW_AADC_POWD,
150 regmap_update_bits(map, AADCPOW(0), AADCPOW_AADC_POWD, 0);
151 regmap_update_bits(map, AADCPOW(1), AADCPOW_AADC_POWD, 0);
153 regmap_update_bits(map, ALINEPOW,
154 ALINEPOW_LIN2_POWD | ALINEPOW_LIN1_POWD, 0);
160 static int evea_update_switch_lo(struct evea_priv *evea)
162 struct regmap *map = evea->regmap;
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);
168 regmap_update_bits(map, ALO1OUTPOW, ALO1OUTPOW_LO1_ON,
170 regmap_update_bits(map, ALO2OUTPOW,
171 ALO2OUTPOW_ADAC2_MUTE | ALO2OUTPOW_LO2_ON,
172 ALO2OUTPOW_ADAC2_MUTE | ALO2OUTPOW_LO2_ON);
174 regmap_update_bits(map, ADACSEQ1(0), ADACSEQ1_MMUTE,
176 regmap_update_bits(map, ADACSEQ1(2), ADACSEQ1_MMUTE,
179 regmap_update_bits(map, ALO1OUTPOW, ALO1OUTPOW_LO1_ON, 0);
180 regmap_update_bits(map, ALO2OUTPOW,
181 ALO2OUTPOW_ADAC2_MUTE | ALO2OUTPOW_LO2_ON,
188 static int evea_update_switch_hp(struct evea_priv *evea)
190 struct regmap *map = evea->regmap;
192 if (evea->switch_hp) {
193 regmap_update_bits(map, ADACSEQ1(1), ADACSEQ1_MMUTE, 0);
195 regmap_update_bits(map, AHPOUTPOW, AHPOUTPOW_HP_ON,
198 regmap_update_bits(map, ADAC1ODC, ADAC1ODC_HP_DIS_RES_MASK,
199 ADAC1ODC_HP_DIS_RES_OFF);
201 regmap_update_bits(map, ADAC1ODC, ADAC1ODC_HP_DIS_RES_MASK,
202 ADAC1ODC_HP_DIS_RES_ON);
204 regmap_update_bits(map, ADACSEQ1(1), ADACSEQ1_MMUTE,
207 regmap_update_bits(map, AHPOUTPOW, AHPOUTPOW_HP_ON, 0);
213 static void evea_update_switch_all(struct evea_priv *evea)
215 evea_update_switch_lin(evea);
216 evea_update_switch_lo(evea);
217 evea_update_switch_hp(evea);
220 static int evea_get_switch_lin(struct snd_kcontrol *kcontrol,
221 struct snd_ctl_elem_value *ucontrol)
223 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
224 struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
226 ucontrol->value.integer.value[0] = evea->switch_lin;
231 static int evea_set_switch_lin(struct snd_kcontrol *kcontrol,
232 struct snd_ctl_elem_value *ucontrol)
234 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
235 struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
237 if (evea->switch_lin == ucontrol->value.integer.value[0])
240 evea->switch_lin = ucontrol->value.integer.value[0];
242 return evea_update_switch_lin(evea);
245 static int evea_get_switch_lo(struct snd_kcontrol *kcontrol,
246 struct snd_ctl_elem_value *ucontrol)
248 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
249 struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
251 ucontrol->value.integer.value[0] = evea->switch_lo;
256 static int evea_set_switch_lo(struct snd_kcontrol *kcontrol,
257 struct snd_ctl_elem_value *ucontrol)
259 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
260 struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
262 if (evea->switch_lo == ucontrol->value.integer.value[0])
265 evea->switch_lo = ucontrol->value.integer.value[0];
267 return evea_update_switch_lo(evea);
270 static int evea_get_switch_hp(struct snd_kcontrol *kcontrol,
271 struct snd_ctl_elem_value *ucontrol)
273 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
274 struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
276 ucontrol->value.integer.value[0] = evea->switch_hp;
281 static int evea_set_switch_hp(struct snd_kcontrol *kcontrol,
282 struct snd_ctl_elem_value *ucontrol)
284 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
285 struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
287 if (evea->switch_hp == ucontrol->value.integer.value[0])
290 evea->switch_hp = ucontrol->value.integer.value[0];
292 return evea_update_switch_hp(evea);
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),
304 static int evea_codec_probe(struct snd_soc_codec *codec)
306 struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
308 evea->switch_lin = 1;
312 evea_set_power_state_on(evea);
313 evea_update_switch_all(evea);
318 static int evea_codec_suspend(struct snd_soc_codec *codec)
320 struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
322 evea_set_power_state_off(evea);
324 reset_control_assert(evea->rst_adamv);
325 reset_control_assert(evea->rst_exiv);
326 reset_control_assert(evea->rst);
328 clk_disable_unprepare(evea->clk_exiv);
329 clk_disable_unprepare(evea->clk);
334 static int evea_codec_resume(struct snd_soc_codec *codec)
336 struct evea_priv *evea = snd_soc_codec_get_drvdata(codec);
339 ret = clk_prepare_enable(evea->clk);
343 ret = clk_prepare_enable(evea->clk_exiv);
347 ret = reset_control_deassert(evea->rst);
349 goto err_out_clock_exiv;
351 ret = reset_control_deassert(evea->rst_exiv);
355 ret = reset_control_deassert(evea->rst_adamv);
357 goto err_out_reset_exiv;
359 evea_set_power_state_on(evea);
360 evea_update_switch_all(evea);
365 reset_control_assert(evea->rst_exiv);
368 reset_control_assert(evea->rst);
371 clk_disable_unprepare(evea->clk_exiv);
374 clk_disable_unprepare(evea->clk);
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,
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),
394 static struct snd_soc_dai_driver soc_dai_evea[] = {
396 .name = DRV_NAME "-line1",
398 .stream_name = "Line Out 1",
399 .formats = EVEA_FORMATS,
405 .stream_name = "Line In 1",
406 .formats = EVEA_FORMATS,
413 .name = DRV_NAME "-hp1",
415 .stream_name = "Headphone 1",
416 .formats = EVEA_FORMATS,
423 .name = DRV_NAME "-lo2",
425 .stream_name = "Line Out 2",
426 .formats = EVEA_FORMATS,
434 static const struct regmap_config evea_regmap_config = {
438 .max_register = 0xffc,
439 .cache_type = REGCACHE_NONE,
442 static int evea_probe(struct platform_device *pdev)
444 struct evea_priv *evea;
445 struct resource *res;
449 evea = devm_kzalloc(&pdev->dev, sizeof(struct evea_priv), GFP_KERNEL);
453 evea->clk = devm_clk_get(&pdev->dev, "evea");
454 if (IS_ERR(evea->clk))
455 return PTR_ERR(evea->clk);
457 evea->clk_exiv = devm_clk_get(&pdev->dev, "exiv");
458 if (IS_ERR(evea->clk_exiv))
459 return PTR_ERR(evea->clk_exiv);
461 evea->rst = devm_reset_control_get_shared(&pdev->dev, "evea");
462 if (IS_ERR(evea->rst))
463 return PTR_ERR(evea->rst);
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);
469 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
470 preg = devm_ioremap_resource(&pdev->dev, res);
472 return PTR_ERR(preg);
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);
479 ret = clk_prepare_enable(evea->clk);
483 ret = clk_prepare_enable(evea->clk_exiv);
487 ret = reset_control_deassert(evea->rst);
489 goto err_out_clock_exiv;
491 ret = reset_control_deassert(evea->rst_exiv);
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;
502 ret = reset_control_deassert(evea->rst_adamv);
504 goto err_out_reset_exiv;
506 platform_set_drvdata(pdev, evea);
508 ret = snd_soc_register_codec(&pdev->dev, &soc_codec_evea,
509 soc_dai_evea, ARRAY_SIZE(soc_dai_evea));
511 goto err_out_reset_adamv;
516 reset_control_assert(evea->rst_adamv);
519 reset_control_assert(evea->rst_exiv);
522 reset_control_assert(evea->rst);
525 clk_disable_unprepare(evea->clk_exiv);
528 clk_disable_unprepare(evea->clk);
533 static int evea_remove(struct platform_device *pdev)
535 struct evea_priv *evea = platform_get_drvdata(pdev);
537 snd_soc_unregister_codec(&pdev->dev);
539 reset_control_assert(evea->rst_adamv);
540 reset_control_assert(evea->rst_exiv);
541 reset_control_assert(evea->rst);
543 clk_disable_unprepare(evea->clk_exiv);
544 clk_disable_unprepare(evea->clk);
549 static const struct of_device_id evea_of_match[] = {
550 { .compatible = "socionext,uniphier-evea", },
553 MODULE_DEVICE_TABLE(of, evea_of_match);
555 static struct platform_driver evea_codec_driver = {
558 .of_match_table = of_match_ptr(evea_of_match),
561 .remove = evea_remove,
563 module_platform_driver(evea_codec_driver);
565 MODULE_AUTHOR("Katsuhiro Suzuki <suzuki.katsuhiro@socionext.com>");
566 MODULE_DESCRIPTION("UniPhier EVEA codec driver");
567 MODULE_LICENSE("GPL v2");