2 * rt274.c -- RT274 ALSA SoC audio codec driver
4 * Copyright 2017 Realtek Semiconductor Corp.
5 * Author: Bard Liao <bardliao@realtek.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
17 #include <linux/i2c.h>
18 #include <linux/platform_device.h>
19 #include <linux/spi/spi.h>
20 #include <linux/dmi.h>
21 #include <linux/acpi.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
29 #include <sound/jack.h>
30 #include <linux/workqueue.h>
35 #define RT274_VENDOR_ID 0x10ec0274
38 struct reg_default *index_cache;
40 struct regmap *regmap;
41 struct snd_soc_component *component;
42 struct i2c_client *i2c;
43 struct snd_soc_jack *jack;
44 struct delayed_work jack_detect_work;
51 static const struct reg_default rt274_index_def[] = {
181 #define INDEX_CACHE_SIZE ARRAY_SIZE(rt274_index_def)
183 static const struct reg_default rt274_reg[] = {
184 { 0x00170500, 0x00000400 },
185 { 0x00220000, 0x00000031 },
186 { 0x00239000, 0x00000057 },
187 { 0x0023a000, 0x00000057 },
188 { 0x00270500, 0x00000400 },
189 { 0x00370500, 0x00000400 },
190 { 0x00870500, 0x00000400 },
191 { 0x00920000, 0x00000031 },
192 { 0x00935000, 0x00000097 },
193 { 0x00936000, 0x00000097 },
194 { 0x00970500, 0x00000400 },
195 { 0x00b37000, 0x00000400 },
196 { 0x00b37200, 0x00000400 },
197 { 0x00b37300, 0x00000400 },
198 { 0x00c37000, 0x00000400 },
199 { 0x00c37100, 0x00000400 },
200 { 0x01270500, 0x00000400 },
201 { 0x01370500, 0x00000400 },
202 { 0x01371f00, 0x411111f0 },
203 { 0x01937000, 0x00000000 },
204 { 0x01970500, 0x00000400 },
205 { 0x02050000, 0x0000001b },
206 { 0x02139000, 0x00000080 },
207 { 0x0213a000, 0x00000080 },
208 { 0x02170100, 0x00000001 },
209 { 0x02170500, 0x00000400 },
210 { 0x02170700, 0x00000000 },
211 { 0x02270100, 0x00000000 },
212 { 0x02370100, 0x00000000 },
213 { 0x01970700, 0x00000020 },
214 { 0x00830000, 0x00000097 },
215 { 0x00930000, 0x00000097 },
216 { 0x01270700, 0x00000000 },
219 static bool rt274_volatile_register(struct device *dev, unsigned int reg)
223 case RT274_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID):
224 case RT274_GET_HP_SENSE:
225 case RT274_GET_MIC_SENSE:
226 case RT274_PROC_COEF:
227 case VERB_CMD(AC_VERB_GET_EAPD_BTLENABLE, RT274_MIC, 0):
228 case VERB_CMD(AC_VERB_GET_EAPD_BTLENABLE, RT274_HP_OUT, 0):
229 case VERB_CMD(AC_VERB_GET_STREAM_FORMAT, RT274_DAC_OUT0, 0):
230 case VERB_CMD(AC_VERB_GET_STREAM_FORMAT, RT274_DAC_OUT1, 0):
231 case VERB_CMD(AC_VERB_GET_STREAM_FORMAT, RT274_ADC_IN1, 0):
232 case VERB_CMD(AC_VERB_GET_STREAM_FORMAT, RT274_ADC_IN2, 0):
233 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_DAC_OUT0, 0):
234 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_DAC_OUT1, 0):
235 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_ADC_IN1, 0):
236 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_ADC_IN2, 0):
237 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_DMIC1, 0):
238 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_DMIC2, 0):
239 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_MIC, 0):
240 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_LINE1, 0):
241 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_LINE2, 0):
242 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_HP_OUT, 0):
243 case VERB_CMD(AC_VERB_GET_CONNECT_SEL, RT274_HP_OUT, 0):
244 case VERB_CMD(AC_VERB_GET_CONNECT_SEL, RT274_MIXER_IN1, 0):
245 case VERB_CMD(AC_VERB_GET_CONNECT_SEL, RT274_MIXER_IN2, 0):
246 case VERB_CMD(AC_VERB_GET_PIN_WIDGET_CONTROL, RT274_DMIC1, 0):
247 case VERB_CMD(AC_VERB_GET_PIN_WIDGET_CONTROL, RT274_DMIC2, 0):
248 case VERB_CMD(AC_VERB_GET_PIN_WIDGET_CONTROL, RT274_MIC, 0):
249 case VERB_CMD(AC_VERB_GET_PIN_WIDGET_CONTROL, RT274_LINE1, 0):
250 case VERB_CMD(AC_VERB_GET_PIN_WIDGET_CONTROL, RT274_LINE2, 0):
251 case VERB_CMD(AC_VERB_GET_PIN_WIDGET_CONTROL, RT274_HP_OUT, 0):
252 case VERB_CMD(AC_VERB_GET_UNSOLICITED_RESPONSE, RT274_HP_OUT, 0):
253 case VERB_CMD(AC_VERB_GET_UNSOLICITED_RESPONSE, RT274_MIC, 0):
254 case VERB_CMD(AC_VERB_GET_UNSOLICITED_RESPONSE, RT274_INLINE_CMD, 0):
263 static bool rt274_readable_register(struct device *dev, unsigned int reg)
267 case RT274_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID):
268 case RT274_GET_HP_SENSE:
269 case RT274_GET_MIC_SENSE:
270 case RT274_SET_AUDIO_POWER:
271 case RT274_SET_HPO_POWER:
272 case RT274_SET_DMIC1_POWER:
278 case RT274_SET_PIN_HPO:
279 case RT274_SET_PIN_LOUT3:
280 case RT274_SET_PIN_DMIC1:
281 case RT274_SET_AMP_GAIN_HPO:
282 case RT274_SET_DMIC2_DEFAULT:
283 case RT274_DAC0L_GAIN:
284 case RT274_DAC0R_GAIN:
285 case RT274_DAC1L_GAIN:
286 case RT274_DAC1R_GAIN:
287 case RT274_ADCL_GAIN:
288 case RT274_ADCR_GAIN:
290 case RT274_HPOL_GAIN:
291 case RT274_HPOR_GAIN:
292 case RT274_LOUTL_GAIN:
293 case RT274_LOUTR_GAIN:
294 case RT274_DAC_FORMAT:
295 case RT274_ADC_FORMAT:
296 case RT274_COEF_INDEX:
297 case RT274_PROC_COEF:
298 case RT274_SET_AMP_GAIN_ADC_IN1:
299 case RT274_SET_AMP_GAIN_ADC_IN2:
300 case RT274_SET_POWER(RT274_DAC_OUT0):
301 case RT274_SET_POWER(RT274_DAC_OUT1):
302 case RT274_SET_POWER(RT274_ADC_IN1):
303 case RT274_SET_POWER(RT274_ADC_IN2):
304 case RT274_SET_POWER(RT274_DMIC2):
305 case RT274_SET_POWER(RT274_MIC):
306 case VERB_CMD(AC_VERB_GET_EAPD_BTLENABLE, RT274_MIC, 0):
307 case VERB_CMD(AC_VERB_GET_EAPD_BTLENABLE, RT274_HP_OUT, 0):
308 case VERB_CMD(AC_VERB_GET_STREAM_FORMAT, RT274_DAC_OUT0, 0):
309 case VERB_CMD(AC_VERB_GET_STREAM_FORMAT, RT274_DAC_OUT1, 0):
310 case VERB_CMD(AC_VERB_GET_STREAM_FORMAT, RT274_ADC_IN1, 0):
311 case VERB_CMD(AC_VERB_GET_STREAM_FORMAT, RT274_ADC_IN2, 0):
312 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_DAC_OUT0, 0):
313 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_DAC_OUT1, 0):
314 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_ADC_IN1, 0):
315 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_ADC_IN2, 0):
316 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_DMIC1, 0):
317 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_DMIC2, 0):
318 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_MIC, 0):
319 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_LINE1, 0):
320 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_LINE2, 0):
321 case VERB_CMD(AC_VERB_GET_AMP_GAIN_MUTE, RT274_HP_OUT, 0):
322 case VERB_CMD(AC_VERB_GET_CONNECT_SEL, RT274_HP_OUT, 0):
323 case VERB_CMD(AC_VERB_GET_CONNECT_SEL, RT274_MIXER_IN1, 0):
324 case VERB_CMD(AC_VERB_GET_CONNECT_SEL, RT274_MIXER_IN2, 0):
325 case VERB_CMD(AC_VERB_GET_PIN_WIDGET_CONTROL, RT274_DMIC1, 0):
326 case VERB_CMD(AC_VERB_GET_PIN_WIDGET_CONTROL, RT274_DMIC2, 0):
327 case VERB_CMD(AC_VERB_GET_PIN_WIDGET_CONTROL, RT274_MIC, 0):
328 case VERB_CMD(AC_VERB_GET_PIN_WIDGET_CONTROL, RT274_LINE1, 0):
329 case VERB_CMD(AC_VERB_GET_PIN_WIDGET_CONTROL, RT274_LINE2, 0):
330 case VERB_CMD(AC_VERB_GET_PIN_WIDGET_CONTROL, RT274_HP_OUT, 0):
331 case VERB_CMD(AC_VERB_GET_UNSOLICITED_RESPONSE, RT274_HP_OUT, 0):
332 case VERB_CMD(AC_VERB_GET_UNSOLICITED_RESPONSE, RT274_MIC, 0):
333 case VERB_CMD(AC_VERB_GET_UNSOLICITED_RESPONSE, RT274_INLINE_CMD, 0):
341 static void rt274_index_sync(struct snd_soc_component *component)
343 struct rt274_priv *rt274 = snd_soc_component_get_drvdata(component);
346 for (i = 0; i < INDEX_CACHE_SIZE; i++) {
347 snd_soc_component_write(component, rt274->index_cache[i].reg,
348 rt274->index_cache[i].def);
353 static int rt274_jack_detect(struct rt274_priv *rt274, bool *hp, bool *mic)
360 if (!rt274->component)
363 regmap_read(rt274->regmap, RT274_GET_HP_SENSE, &buf);
364 *hp = buf & 0x80000000;
365 regmap_read(rt274->regmap, RT274_GET_MIC_SENSE, &buf);
366 *mic = buf & 0x80000000;
368 pr_debug("*hp = %d *mic = %d\n", *hp, *mic);
373 static void rt274_jack_detect_work(struct work_struct *work)
375 struct rt274_priv *rt274 =
376 container_of(work, struct rt274_priv, jack_detect_work.work);
381 if (rt274_jack_detect(rt274, &hp, &mic) < 0)
385 status |= SND_JACK_HEADPHONE;
388 status |= SND_JACK_MICROPHONE;
390 snd_soc_jack_report(rt274->jack, status,
391 SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
394 static irqreturn_t rt274_irq(int irq, void *data);
396 static int rt274_mic_detect(struct snd_soc_component *component,
397 struct snd_soc_jack *jack, void *data)
399 struct rt274_priv *rt274 = snd_soc_component_get_drvdata(component);
404 /* Disable jack detection */
405 regmap_update_bits(rt274->regmap, RT274_EAPD_GPIO_IRQ_CTRL,
406 RT274_IRQ_EN, RT274_IRQ_DIS);
411 regmap_update_bits(rt274->regmap, RT274_EAPD_GPIO_IRQ_CTRL,
412 RT274_IRQ_EN, RT274_IRQ_EN);
414 /* Send an initial report */
420 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6350, 50, 0);
421 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0);
423 static const struct snd_kcontrol_new rt274_snd_controls[] = {
424 SOC_DOUBLE_R_TLV("DAC0 Playback Volume", RT274_DAC0L_GAIN,
425 RT274_DAC0R_GAIN, 0, 0x7f, 0, out_vol_tlv),
426 SOC_DOUBLE_R_TLV("DAC1 Playback Volume", RT274_DAC1L_GAIN,
427 RT274_DAC1R_GAIN, 0, 0x7f, 0, out_vol_tlv),
428 SOC_DOUBLE_R_TLV("ADC0 Capture Volume", RT274_ADCL_GAIN,
429 RT274_ADCR_GAIN, 0, 0x7f, 0, out_vol_tlv),
430 SOC_DOUBLE_R("ADC0 Capture Switch", RT274_ADCL_GAIN,
431 RT274_ADCR_GAIN, RT274_MUTE_SFT, 1, 1),
432 SOC_SINGLE_TLV("AMIC Volume", RT274_MIC_GAIN,
433 0, 0x3, 0, mic_vol_tlv),
436 static const struct snd_kcontrol_new hpol_enable_control =
437 SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT274_HPOL_GAIN,
438 RT274_MUTE_SFT, 1, 1);
440 static const struct snd_kcontrol_new hpor_enable_control =
441 SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT274_HPOR_GAIN,
442 RT274_MUTE_SFT, 1, 1);
444 static const struct snd_kcontrol_new loutl_enable_control =
445 SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT274_LOUTL_GAIN,
446 RT274_MUTE_SFT, 1, 1);
448 static const struct snd_kcontrol_new loutr_enable_control =
449 SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT274_LOUTR_GAIN,
450 RT274_MUTE_SFT, 1, 1);
453 static const char * const rt274_adc_src[] = {
454 "Mic", "Line1", "Line2", "Dmic"
457 static SOC_ENUM_SINGLE_DECL(
458 rt274_adc0_enum, RT274_ADC0_MUX, RT274_ADC_SEL_SFT,
461 static const struct snd_kcontrol_new rt274_adc0_mux =
462 SOC_DAPM_ENUM("ADC 0 source", rt274_adc0_enum);
464 static SOC_ENUM_SINGLE_DECL(
465 rt274_adc1_enum, RT274_ADC1_MUX, RT274_ADC_SEL_SFT,
468 static const struct snd_kcontrol_new rt274_adc1_mux =
469 SOC_DAPM_ENUM("ADC 1 source", rt274_adc1_enum);
471 static const char * const rt274_dac_src[] = {
472 "DAC OUT0", "DAC OUT1"
475 static SOC_ENUM_SINGLE_DECL(rt274_hpo_enum, RT274_HPO_MUX,
478 static const struct snd_kcontrol_new rt274_hpo_mux =
479 SOC_DAPM_ENUM("HPO source", rt274_hpo_enum);
481 /* Line out source */
482 static SOC_ENUM_SINGLE_DECL(rt274_lout_enum, RT274_LOUT_MUX,
485 static const struct snd_kcontrol_new rt274_lout_mux =
486 SOC_DAPM_ENUM("LOUT source", rt274_lout_enum);
488 static const struct snd_soc_dapm_widget rt274_dapm_widgets[] = {
490 SND_SOC_DAPM_INPUT("DMIC1 Pin"),
491 SND_SOC_DAPM_INPUT("DMIC2 Pin"),
492 SND_SOC_DAPM_INPUT("MIC"),
493 SND_SOC_DAPM_INPUT("LINE1"),
494 SND_SOC_DAPM_INPUT("LINE2"),
497 SND_SOC_DAPM_PGA("DMIC1", SND_SOC_NOPM, 0, 0, NULL, 0),
498 SND_SOC_DAPM_PGA("DMIC2", SND_SOC_NOPM, 0, 0, NULL, 0),
501 SND_SOC_DAPM_ADC("ADC 0", NULL, RT274_SET_STREAMID_ADC1, 4, 0),
502 SND_SOC_DAPM_ADC("ADC 1", NULL, RT274_SET_STREAMID_ADC2, 4, 0),
505 SND_SOC_DAPM_MUX("ADC 0 Mux", SND_SOC_NOPM, 0, 0,
507 SND_SOC_DAPM_MUX("ADC 1 Mux", SND_SOC_NOPM, 0, 0,
510 /* Audio Interface */
511 SND_SOC_DAPM_AIF_IN("AIF1RXL", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
512 SND_SOC_DAPM_AIF_IN("AIF1RXR", "AIF1 Playback", 1, SND_SOC_NOPM, 0, 0),
513 SND_SOC_DAPM_AIF_OUT("AIF1TXL", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
514 SND_SOC_DAPM_AIF_OUT("AIF1TXR", "AIF1 Capture", 1, SND_SOC_NOPM, 0, 0),
515 SND_SOC_DAPM_AIF_IN("AIF2RXL", "AIF1 Playback", 2, SND_SOC_NOPM, 0, 0),
516 SND_SOC_DAPM_AIF_IN("AIF2RXR", "AIF1 Playback", 3, SND_SOC_NOPM, 0, 0),
517 SND_SOC_DAPM_AIF_OUT("AIF2TXL", "AIF1 Capture", 2, SND_SOC_NOPM, 0, 0),
518 SND_SOC_DAPM_AIF_OUT("AIF2TXR", "AIF1 Capture", 3, SND_SOC_NOPM, 0, 0),
522 SND_SOC_DAPM_DAC("DAC 0", NULL, RT274_SET_STREAMID_DAC0, 4, 0),
523 SND_SOC_DAPM_DAC("DAC 1", NULL, RT274_SET_STREAMID_DAC1, 4, 0),
526 SND_SOC_DAPM_MUX("HPO Mux", SND_SOC_NOPM, 0, 0, &rt274_hpo_mux),
527 SND_SOC_DAPM_MUX("LOUT Mux", SND_SOC_NOPM, 0, 0, &rt274_lout_mux),
529 SND_SOC_DAPM_SUPPLY("HP Power", RT274_SET_PIN_HPO,
530 RT274_SET_PIN_SFT, 0, NULL, 0),
531 SND_SOC_DAPM_SUPPLY("LOUT Power", RT274_SET_PIN_LOUT3,
532 RT274_SET_PIN_SFT, 0, NULL, 0),
535 SND_SOC_DAPM_PGA("DAC OUT0", SND_SOC_NOPM, 0, 0,
537 SND_SOC_DAPM_PGA("DAC OUT1", SND_SOC_NOPM, 0, 0,
541 SND_SOC_DAPM_SWITCH("LOUT L", SND_SOC_NOPM, 0, 0,
542 &loutl_enable_control),
543 SND_SOC_DAPM_SWITCH("LOUT R", SND_SOC_NOPM, 0, 0,
544 &loutr_enable_control),
545 SND_SOC_DAPM_SWITCH("HPO L", SND_SOC_NOPM, 0, 0,
546 &hpol_enable_control),
547 SND_SOC_DAPM_SWITCH("HPO R", SND_SOC_NOPM, 0, 0,
548 &hpor_enable_control),
551 SND_SOC_DAPM_OUTPUT("HPO Pin"),
552 SND_SOC_DAPM_OUTPUT("SPDIF"),
553 SND_SOC_DAPM_OUTPUT("LINE3"),
556 static const struct snd_soc_dapm_route rt274_dapm_routes[] = {
557 {"DMIC1", NULL, "DMIC1 Pin"},
558 {"DMIC2", NULL, "DMIC2 Pin"},
560 {"ADC 0 Mux", "Mic", "MIC"},
561 {"ADC 0 Mux", "Dmic", "DMIC1"},
562 {"ADC 0 Mux", "Line1", "LINE1"},
563 {"ADC 0 Mux", "Line2", "LINE2"},
564 {"ADC 1 Mux", "Mic", "MIC"},
565 {"ADC 1 Mux", "Dmic", "DMIC2"},
566 {"ADC 1 Mux", "Line1", "LINE1"},
567 {"ADC 1 Mux", "Line2", "LINE2"},
569 {"ADC 0", NULL, "ADC 0 Mux"},
570 {"ADC 1", NULL, "ADC 1 Mux"},
572 {"AIF1TXL", NULL, "ADC 0"},
573 {"AIF1TXR", NULL, "ADC 0"},
574 {"AIF2TXL", NULL, "ADC 1"},
575 {"AIF2TXR", NULL, "ADC 1"},
577 {"DAC 0", NULL, "AIF1RXL"},
578 {"DAC 0", NULL, "AIF1RXR"},
579 {"DAC 1", NULL, "AIF2RXL"},
580 {"DAC 1", NULL, "AIF2RXR"},
582 {"DAC OUT0", NULL, "DAC 0"},
584 {"DAC OUT1", NULL, "DAC 1"},
586 {"LOUT Mux", "DAC OUT0", "DAC OUT0"},
587 {"LOUT Mux", "DAC OUT1", "DAC OUT1"},
589 {"LOUT L", "Switch", "LOUT Mux"},
590 {"LOUT R", "Switch", "LOUT Mux"},
591 {"LOUT L", NULL, "LOUT Power"},
592 {"LOUT R", NULL, "LOUT Power"},
594 {"LINE3", NULL, "LOUT L"},
595 {"LINE3", NULL, "LOUT R"},
597 {"HPO Mux", "DAC OUT0", "DAC OUT0"},
598 {"HPO Mux", "DAC OUT1", "DAC OUT1"},
600 {"HPO L", "Switch", "HPO Mux"},
601 {"HPO R", "Switch", "HPO Mux"},
602 {"HPO L", NULL, "HP Power"},
603 {"HPO R", NULL, "HP Power"},
605 {"HPO Pin", NULL, "HPO L"},
606 {"HPO Pin", NULL, "HPO R"},
609 static int rt274_hw_params(struct snd_pcm_substream *substream,
610 struct snd_pcm_hw_params *params,
611 struct snd_soc_dai *dai)
613 struct snd_soc_component *component = dai->component;
614 struct rt274_priv *rt274 = snd_soc_component_get_drvdata(component);
615 unsigned int val = 0;
616 int d_len_code = 0, c_len_code = 0;
618 switch (params_rate(params)) {
619 /* bit 14 0:48K 1:44.1K */
624 dev_err(component->dev, "Unsupported sample rate %d\n",
625 params_rate(params));
628 switch (rt274->sys_clk) {
631 if (params_rate(params) != 48000) {
632 dev_err(component->dev, "Sys_clk is not matched (%d %d)\n",
633 params_rate(params), rt274->sys_clk);
639 if (params_rate(params) != 44100) {
640 dev_err(component->dev, "Sys_clk is not matched (%d %d)\n",
641 params_rate(params), rt274->sys_clk);
647 if (params_channels(params) <= 16) {
648 /* bit 3:0 Number of Channel */
649 val |= (params_channels(params) - 1);
651 dev_err(component->dev, "Unsupported channels %d\n",
652 params_channels(params));
656 switch (params_width(params)) {
657 /* bit 6:4 Bits per Sample */
689 snd_soc_component_update_bits(component,
690 RT274_I2S_CTRL1, 0xc018, d_len_code << 3 | c_len_code << 14);
691 dev_dbg(component->dev, "format val = 0x%x\n", val);
693 snd_soc_component_update_bits(component, RT274_DAC_FORMAT, 0x407f, val);
694 snd_soc_component_update_bits(component, RT274_ADC_FORMAT, 0x407f, val);
699 static int rt274_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
701 struct snd_soc_component *component = dai->component;
702 struct rt274_priv *rt274 = snd_soc_component_get_drvdata(component);
704 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
705 case SND_SOC_DAIFMT_CBM_CFM:
706 snd_soc_component_update_bits(component,
707 RT274_I2S_CTRL1, RT274_I2S_MODE_MASK, RT274_I2S_MODE_M);
708 rt274->master = true;
710 case SND_SOC_DAIFMT_CBS_CFS:
711 snd_soc_component_update_bits(component,
712 RT274_I2S_CTRL1, RT274_I2S_MODE_MASK, RT274_I2S_MODE_S);
713 rt274->master = false;
719 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
720 case SND_SOC_DAIFMT_I2S:
721 snd_soc_component_update_bits(component, RT274_I2S_CTRL1,
722 RT274_I2S_FMT_MASK, RT274_I2S_FMT_I2S);
724 case SND_SOC_DAIFMT_LEFT_J:
725 snd_soc_component_update_bits(component, RT274_I2S_CTRL1,
726 RT274_I2S_FMT_MASK, RT274_I2S_FMT_LJ);
728 case SND_SOC_DAIFMT_DSP_A:
729 snd_soc_component_update_bits(component, RT274_I2S_CTRL1,
730 RT274_I2S_FMT_MASK, RT274_I2S_FMT_PCMA);
732 case SND_SOC_DAIFMT_DSP_B:
733 snd_soc_component_update_bits(component, RT274_I2S_CTRL1,
734 RT274_I2S_FMT_MASK, RT274_I2S_FMT_PCMB);
739 /* bit 15 Stream Type 0:PCM 1:Non-PCM */
740 snd_soc_component_update_bits(component, RT274_DAC_FORMAT, 0x8000, 0);
741 snd_soc_component_update_bits(component, RT274_ADC_FORMAT, 0x8000, 0);
746 static int rt274_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
747 unsigned int freq_in, unsigned int freq_out)
749 struct snd_soc_component *component = dai->component;
750 struct rt274_priv *rt274 = snd_soc_component_get_drvdata(component);
753 case RT274_PLL2_S_MCLK:
754 snd_soc_component_update_bits(component, RT274_PLL2_CTRL,
755 RT274_PLL2_SRC_MASK, RT274_PLL2_SRC_MCLK);
758 dev_warn(component->dev, "invalid pll source, use BCLK\n");
759 case RT274_PLL2_S_BCLK:
760 snd_soc_component_update_bits(component, RT274_PLL2_CTRL,
761 RT274_PLL2_SRC_MASK, RT274_PLL2_SRC_BCLK);
765 if (source == RT274_PLL2_S_BCLK) {
766 snd_soc_component_update_bits(component, RT274_MCLK_CTRL,
767 (0x3 << 12), (0x3 << 12));
770 snd_soc_component_write(component, 0x7a, 0xaab6);
771 snd_soc_component_write(component, 0x7b, 0x0301);
772 snd_soc_component_write(component, 0x7c, 0x04fe);
775 snd_soc_component_write(component, 0x7a, 0xaa96);
776 snd_soc_component_write(component, 0x7b, 0x8003);
777 snd_soc_component_write(component, 0x7c, 0x081e);
780 snd_soc_component_write(component, 0x7a, 0xaa96);
781 snd_soc_component_write(component, 0x7b, 0x8003);
782 snd_soc_component_write(component, 0x7c, 0x080e);
785 dev_warn(component->dev, "invalid freq_in, assume 4.8M\n");
787 snd_soc_component_write(component, 0x7a, 0xaab6);
788 snd_soc_component_write(component, 0x7b, 0x0301);
789 snd_soc_component_write(component, 0x7c, 0x047e);
797 static int rt274_set_dai_sysclk(struct snd_soc_dai *dai,
798 int clk_id, unsigned int freq, int dir)
800 struct snd_soc_component *component = dai->component;
801 struct rt274_priv *rt274 = snd_soc_component_get_drvdata(component);
802 unsigned int clk_src, mclk_en;
804 dev_dbg(component->dev, "%s freq=%d\n", __func__, freq);
807 case RT274_SCLK_S_MCLK:
808 mclk_en = RT274_MCLK_MODE_EN;
809 clk_src = RT274_CLK_SRC_MCLK;
811 case RT274_SCLK_S_PLL1:
812 mclk_en = RT274_MCLK_MODE_DIS;
813 clk_src = RT274_CLK_SRC_MCLK;
815 case RT274_SCLK_S_PLL2:
816 mclk_en = RT274_MCLK_MODE_EN;
817 clk_src = RT274_CLK_SRC_PLL2;
820 mclk_en = RT274_MCLK_MODE_DIS;
821 clk_src = RT274_CLK_SRC_MCLK;
822 dev_warn(component->dev, "invalid sysclk source, use PLL1\n");
825 snd_soc_component_update_bits(component, RT274_MCLK_CTRL,
826 RT274_MCLK_MODE_MASK, mclk_en);
827 snd_soc_component_update_bits(component, RT274_CLK_CTRL,
828 RT274_CLK_SRC_MASK, clk_src);
832 if (clk_id == RT274_SCLK_S_MCLK) {
833 dev_err(component->dev, "Should not use MCLK\n");
836 snd_soc_component_update_bits(component,
837 RT274_I2S_CTRL2, 0x40, 0x40);
840 if (clk_id == RT274_SCLK_S_MCLK) {
841 dev_err(component->dev, "Should not use MCLK\n");
844 snd_soc_component_update_bits(component,
845 RT274_I2S_CTRL2, 0x40, 0x0);
849 snd_soc_component_update_bits(component,
850 RT274_MCLK_CTRL, 0x1fcf, 0x0008);
854 snd_soc_component_update_bits(component,
855 RT274_MCLK_CTRL, 0x1fcf, 0x1543);
858 dev_err(component->dev, "Unsupported system clock\n");
862 rt274->sys_clk = freq;
863 rt274->clk_id = clk_id;
868 static int rt274_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
870 struct snd_soc_component *component = dai->component;
871 struct rt274_priv *rt274 = snd_soc_component_get_drvdata(component);
873 dev_dbg(component->dev, "%s ratio=%d\n", __func__, ratio);
875 if ((ratio / 50) == 0)
876 snd_soc_component_update_bits(component,
877 RT274_I2S_CTRL1, 0x1000, 0x1000);
879 snd_soc_component_update_bits(component,
880 RT274_I2S_CTRL1, 0x1000, 0x0);
886 static int rt274_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
887 unsigned int rx_mask, int slots, int slot_width)
890 struct snd_soc_component *component = dai->component;
892 if (rx_mask || tx_mask) {
893 snd_soc_component_update_bits(component,
894 RT274_I2S_CTRL1, RT274_TDM_EN, RT274_TDM_EN);
896 snd_soc_component_update_bits(component,
897 RT274_I2S_CTRL1, RT274_TDM_EN, RT274_TDM_DIS);
903 snd_soc_component_update_bits(component,
904 RT274_I2S_CTRL1, RT274_TDM_CH_NUM, RT274_TDM_4CH);
907 snd_soc_component_update_bits(component,
908 RT274_I2S_CTRL1, RT274_TDM_CH_NUM, RT274_TDM_2CH);
911 dev_err(component->dev,
912 "Support 2 or 4 slots TDM only\n");
919 static int rt274_set_bias_level(struct snd_soc_component *component,
920 enum snd_soc_bias_level level)
923 case SND_SOC_BIAS_PREPARE:
924 if (SND_SOC_BIAS_STANDBY ==
925 snd_soc_component_get_bias_level(component)) {
926 snd_soc_component_write(component,
927 RT274_SET_AUDIO_POWER, AC_PWRST_D0);
931 case SND_SOC_BIAS_STANDBY:
932 snd_soc_component_write(component,
933 RT274_SET_AUDIO_POWER, AC_PWRST_D3);
943 static irqreturn_t rt274_irq(int irq, void *data)
945 struct rt274_priv *rt274 = data;
951 regmap_update_bits(rt274->regmap, RT274_EAPD_GPIO_IRQ_CTRL,
952 RT274_IRQ_CLR, RT274_IRQ_CLR);
954 ret = rt274_jack_detect(rt274, &hp, &mic);
958 status |= SND_JACK_HEADPHONE;
961 status |= SND_JACK_MICROPHONE;
963 snd_soc_jack_report(rt274->jack, status,
964 SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
966 pm_wakeup_event(&rt274->i2c->dev, 300);
972 static int rt274_probe(struct snd_soc_component *component)
974 struct rt274_priv *rt274 = snd_soc_component_get_drvdata(component);
976 rt274->component = component;
978 if (rt274->i2c->irq) {
979 INIT_DELAYED_WORK(&rt274->jack_detect_work,
980 rt274_jack_detect_work);
981 schedule_delayed_work(&rt274->jack_detect_work,
982 msecs_to_jiffies(1250));
988 static void rt274_remove(struct snd_soc_component *component)
990 struct rt274_priv *rt274 = snd_soc_component_get_drvdata(component);
992 cancel_delayed_work_sync(&rt274->jack_detect_work);
996 static int rt274_suspend(struct snd_soc_component *component)
998 struct rt274_priv *rt274 = snd_soc_component_get_drvdata(component);
1000 regcache_cache_only(rt274->regmap, true);
1001 regcache_mark_dirty(rt274->regmap);
1006 static int rt274_resume(struct snd_soc_component *component)
1008 struct rt274_priv *rt274 = snd_soc_component_get_drvdata(component);
1010 regcache_cache_only(rt274->regmap, false);
1011 rt274_index_sync(component);
1012 regcache_sync(rt274->regmap);
1017 #define rt274_suspend NULL
1018 #define rt274_resume NULL
1021 #define RT274_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
1022 #define RT274_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1023 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1025 static const struct snd_soc_dai_ops rt274_aif_dai_ops = {
1026 .hw_params = rt274_hw_params,
1027 .set_fmt = rt274_set_dai_fmt,
1028 .set_sysclk = rt274_set_dai_sysclk,
1029 .set_pll = rt274_set_dai_pll,
1030 .set_bclk_ratio = rt274_set_bclk_ratio,
1031 .set_tdm_slot = rt274_set_tdm_slot,
1034 static struct snd_soc_dai_driver rt274_dai[] = {
1036 .name = "rt274-aif1",
1039 .stream_name = "AIF1 Playback",
1042 .rates = RT274_STEREO_RATES,
1043 .formats = RT274_FORMATS,
1046 .stream_name = "AIF1 Capture",
1049 .rates = RT274_STEREO_RATES,
1050 .formats = RT274_FORMATS,
1052 .ops = &rt274_aif_dai_ops,
1053 .symmetric_rates = 1,
1057 static const struct snd_soc_component_driver soc_component_dev_rt274 = {
1058 .probe = rt274_probe,
1059 .remove = rt274_remove,
1060 .suspend = rt274_suspend,
1061 .resume = rt274_resume,
1062 .set_bias_level = rt274_set_bias_level,
1063 .set_jack = rt274_mic_detect,
1064 .controls = rt274_snd_controls,
1065 .num_controls = ARRAY_SIZE(rt274_snd_controls),
1066 .dapm_widgets = rt274_dapm_widgets,
1067 .num_dapm_widgets = ARRAY_SIZE(rt274_dapm_widgets),
1068 .dapm_routes = rt274_dapm_routes,
1069 .num_dapm_routes = ARRAY_SIZE(rt274_dapm_routes),
1070 .use_pmdown_time = 1,
1072 .non_legacy_dai_naming = 1,
1075 static const struct regmap_config rt274_regmap = {
1078 .max_register = 0x05bfffff,
1079 .volatile_reg = rt274_volatile_register,
1080 .readable_reg = rt274_readable_register,
1081 .reg_write = rl6347a_hw_write,
1082 .reg_read = rl6347a_hw_read,
1083 .cache_type = REGCACHE_RBTREE,
1084 .reg_defaults = rt274_reg,
1085 .num_reg_defaults = ARRAY_SIZE(rt274_reg),
1089 static const struct of_device_id rt274_of_match[] = {
1090 {.compatible = "realtek,rt274"},
1093 MODULE_DEVICE_TABLE(of, rt274_of_match);
1096 static const struct i2c_device_id rt274_i2c_id[] = {
1100 MODULE_DEVICE_TABLE(i2c, rt274_i2c_id);
1102 static const struct acpi_device_id rt274_acpi_match[] = {
1107 MODULE_DEVICE_TABLE(acpi, rt274_acpi_match);
1109 static int rt274_i2c_probe(struct i2c_client *i2c,
1110 const struct i2c_device_id *id)
1112 struct rt274_priv *rt274;
1117 rt274 = devm_kzalloc(&i2c->dev, sizeof(*rt274),
1122 rt274->regmap = devm_regmap_init(&i2c->dev, NULL, i2c, &rt274_regmap);
1123 if (IS_ERR(rt274->regmap)) {
1124 ret = PTR_ERR(rt274->regmap);
1125 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1130 ret = regmap_read(rt274->regmap,
1131 RT274_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID), &val);
1135 if (val != RT274_VENDOR_ID) {
1137 "Device with ID register %#x is not rt274\n", val);
1141 rt274->index_cache = devm_kmemdup(&i2c->dev, rt274_index_def,
1142 sizeof(rt274_index_def), GFP_KERNEL);
1143 if (!rt274->index_cache)
1146 rt274->index_cache_size = INDEX_CACHE_SIZE;
1148 i2c_set_clientdata(i2c, rt274);
1151 regmap_write(rt274->regmap, RT274_RESET, 0);
1152 regmap_update_bits(rt274->regmap, 0x1a, 0x4000, 0x4000);
1154 /* Set Pad PDB is floating */
1155 regmap_update_bits(rt274->regmap, RT274_PAD_CTRL12, 0x3, 0x0);
1156 regmap_write(rt274->regmap, RT274_COEF5b_INDEX, 0x01);
1157 regmap_write(rt274->regmap, RT274_COEF5b_COEF, 0x8540);
1158 regmap_update_bits(rt274->regmap, 0x6f, 0x0100, 0x0100);
1159 /* Combo jack auto detect */
1160 regmap_write(rt274->regmap, 0x4a, 0x201b);
1162 regmap_update_bits(rt274->regmap, 0x6f, 0x3000, 0x2000);
1163 /* HP DC Calibration */
1164 regmap_update_bits(rt274->regmap, 0x6f, 0xf, 0x0);
1165 /* Set NID=58h.Index 00h [15]= 1b; */
1166 regmap_write(rt274->regmap, RT274_COEF58_INDEX, 0x00);
1167 regmap_write(rt274->regmap, RT274_COEF58_COEF, 0xb888);
1169 regmap_update_bits(rt274->regmap, 0x6f, 0xf, 0xb);
1170 regmap_write(rt274->regmap, RT274_COEF58_INDEX, 0x00);
1171 regmap_write(rt274->regmap, RT274_COEF58_COEF, 0x3888);
1172 /* Set pin widget */
1173 regmap_write(rt274->regmap, RT274_SET_PIN_HPO, 0x40);
1174 regmap_write(rt274->regmap, RT274_SET_PIN_LOUT3, 0x40);
1175 regmap_write(rt274->regmap, RT274_SET_MIC, 0x20);
1176 regmap_write(rt274->regmap, RT274_SET_PIN_DMIC1, 0x20);
1178 regmap_update_bits(rt274->regmap, RT274_I2S_CTRL2, 0xc004, 0x4004);
1179 regmap_update_bits(rt274->regmap, RT274_EAPD_GPIO_IRQ_CTRL,
1180 RT274_GPI2_SEL_MASK, RT274_GPI2_SEL_DMIC_CLK);
1182 /* jack detection */
1183 regmap_write(rt274->regmap, RT274_UNSOLICITED_HP_OUT, 0x81);
1184 regmap_write(rt274->regmap, RT274_UNSOLICITED_MIC, 0x82);
1186 if (rt274->i2c->irq) {
1187 ret = request_threaded_irq(rt274->i2c->irq, NULL, rt274_irq,
1188 IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "rt274", rt274);
1191 "Failed to reguest IRQ: %d\n", ret);
1196 ret = devm_snd_soc_register_component(&i2c->dev,
1197 &soc_component_dev_rt274,
1198 rt274_dai, ARRAY_SIZE(rt274_dai));
1203 static int rt274_i2c_remove(struct i2c_client *i2c)
1205 struct rt274_priv *rt274 = i2c_get_clientdata(i2c);
1208 free_irq(i2c->irq, rt274);
1214 static struct i2c_driver rt274_i2c_driver = {
1217 .acpi_match_table = ACPI_PTR(rt274_acpi_match),
1219 .of_match_table = of_match_ptr(rt274_of_match),
1222 .probe = rt274_i2c_probe,
1223 .remove = rt274_i2c_remove,
1224 .id_table = rt274_i2c_id,
1227 module_i2c_driver(rt274_i2c_driver);
1229 MODULE_DESCRIPTION("ASoC RT274 driver");
1230 MODULE_AUTHOR("Bard Liao <bardliao@realtek.com>");
1231 MODULE_LICENSE("GPL v2");