2 * cht-bsw-max98090.c - ASoc Machine driver for Intel Cherryview-based
3 * platforms Cherrytrail and Braswell, with max98090 & TI codec.
5 * Copyright (C) 2015 Intel Corp
6 * Author: Fang, Yang A <yang.a.fang@intel.com>
7 * This file is modified from cht_bsw_rt5645.c
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/acpi.h>
26 #include <linux/clk.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/jack.h>
31 #include "../../codecs/max98090.h"
32 #include "../atom/sst-atom-controls.h"
33 #include "../../codecs/ts3a227e.h"
35 #define CHT_PLAT_CLK_3_HZ 19200000
36 #define CHT_CODEC_DAI "HiFi"
38 struct cht_mc_private
{
40 struct snd_soc_jack jack
;
41 bool ts3a227e_present
;
44 static inline struct snd_soc_dai
*cht_get_codec_dai(struct snd_soc_card
*card
)
46 struct snd_soc_pcm_runtime
*rtd
;
48 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
49 if (!strncmp(rtd
->codec_dai
->name
, CHT_CODEC_DAI
,
50 strlen(CHT_CODEC_DAI
)))
51 return rtd
->codec_dai
;
56 static int platform_clock_control(struct snd_soc_dapm_widget
*w
,
57 struct snd_kcontrol
*k
, int event
)
59 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
60 struct snd_soc_card
*card
= dapm
->card
;
61 struct snd_soc_dai
*codec_dai
;
62 struct cht_mc_private
*ctx
= snd_soc_card_get_drvdata(card
);
65 codec_dai
= cht_get_codec_dai(card
);
67 dev_err(card
->dev
, "Codec dai not found; Unable to set platform clock\n");
71 if (SND_SOC_DAPM_EVENT_ON(event
)) {
72 ret
= clk_prepare_enable(ctx
->mclk
);
75 "could not configure MCLK state");
79 clk_disable_unprepare(ctx
->mclk
);
85 static const struct snd_soc_dapm_widget cht_dapm_widgets
[] = {
86 SND_SOC_DAPM_HP("Headphone", NULL
),
87 SND_SOC_DAPM_MIC("Headset Mic", NULL
),
88 SND_SOC_DAPM_MIC("Int Mic", NULL
),
89 SND_SOC_DAPM_SPK("Ext Spk", NULL
),
90 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM
, 0, 0,
91 platform_clock_control
, SND_SOC_DAPM_PRE_PMU
|
92 SND_SOC_DAPM_POST_PMD
),
95 static const struct snd_soc_dapm_route cht_audio_map
[] = {
96 {"IN34", NULL
, "Headset Mic"},
97 {"Headset Mic", NULL
, "MICBIAS"},
98 {"DMICL", NULL
, "Int Mic"},
99 {"Headphone", NULL
, "HPL"},
100 {"Headphone", NULL
, "HPR"},
101 {"Ext Spk", NULL
, "SPKL"},
102 {"Ext Spk", NULL
, "SPKR"},
103 {"HiFi Playback", NULL
, "ssp2 Tx"},
104 {"ssp2 Tx", NULL
, "codec_out0"},
105 {"ssp2 Tx", NULL
, "codec_out1"},
106 {"codec_in0", NULL
, "ssp2 Rx" },
107 {"codec_in1", NULL
, "ssp2 Rx" },
108 {"ssp2 Rx", NULL
, "HiFi Capture"},
109 {"Headphone", NULL
, "Platform Clock"},
110 {"Headset Mic", NULL
, "Platform Clock"},
111 {"Int Mic", NULL
, "Platform Clock"},
112 {"Ext Spk", NULL
, "Platform Clock"},
115 static const struct snd_kcontrol_new cht_mc_controls
[] = {
116 SOC_DAPM_PIN_SWITCH("Headphone"),
117 SOC_DAPM_PIN_SWITCH("Headset Mic"),
118 SOC_DAPM_PIN_SWITCH("Int Mic"),
119 SOC_DAPM_PIN_SWITCH("Ext Spk"),
122 static int cht_aif1_hw_params(struct snd_pcm_substream
*substream
,
123 struct snd_pcm_hw_params
*params
)
125 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
126 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
129 ret
= snd_soc_dai_set_sysclk(codec_dai
, M98090_REG_SYSTEM_CLOCK
,
130 CHT_PLAT_CLK_3_HZ
, SND_SOC_CLOCK_IN
);
132 dev_err(rtd
->dev
, "can't set codec sysclk: %d\n", ret
);
139 static int cht_ti_jack_event(struct notifier_block
*nb
,
140 unsigned long event
, void *data
)
142 struct snd_soc_jack
*jack
= (struct snd_soc_jack
*)data
;
143 struct snd_soc_dapm_context
*dapm
= &jack
->card
->dapm
;
145 if (event
& SND_JACK_MICROPHONE
) {
146 snd_soc_dapm_force_enable_pin(dapm
, "SHDN");
147 snd_soc_dapm_force_enable_pin(dapm
, "MICBIAS");
148 snd_soc_dapm_sync(dapm
);
150 snd_soc_dapm_disable_pin(dapm
, "MICBIAS");
151 snd_soc_dapm_disable_pin(dapm
, "SHDN");
152 snd_soc_dapm_sync(dapm
);
158 static struct notifier_block cht_jack_nb
= {
159 .notifier_call
= cht_ti_jack_event
,
162 static struct snd_soc_jack_pin hs_jack_pins
[] = {
165 .mask
= SND_JACK_HEADPHONE
,
168 .pin
= "Headset Mic",
169 .mask
= SND_JACK_MICROPHONE
,
173 static struct snd_soc_jack_gpio hs_jack_gpios
[] = {
176 .report
= SND_JACK_HEADPHONE
| SND_JACK_LINEOUT
,
177 .debounce_time
= 200,
182 .report
= SND_JACK_MICROPHONE
,
183 .debounce_time
= 200,
187 static const struct acpi_gpio_params hp_gpios
= { 0, 0, false };
188 static const struct acpi_gpio_params mic_gpios
= { 1, 0, false };
190 static const struct acpi_gpio_mapping acpi_max98090_gpios
[] = {
191 { "hp-gpios", &hp_gpios
, 1 },
192 { "mic-gpios", &mic_gpios
, 1 },
196 static int cht_codec_init(struct snd_soc_pcm_runtime
*runtime
)
200 struct cht_mc_private
*ctx
= snd_soc_card_get_drvdata(runtime
->card
);
201 struct snd_soc_jack
*jack
= &ctx
->jack
;
203 if (ctx
->ts3a227e_present
) {
205 * The jack has already been created in the
206 * cht_max98090_headset_init() function.
208 snd_soc_jack_notifier_register(jack
, &cht_jack_nb
);
212 jack_type
= SND_JACK_HEADPHONE
| SND_JACK_MICROPHONE
;
214 ret
= snd_soc_card_jack_new(runtime
->card
, "Headset Jack",
216 hs_jack_pins
, ARRAY_SIZE(hs_jack_pins
));
218 dev_err(runtime
->dev
, "Headset Jack creation failed %d\n", ret
);
222 ret
= snd_soc_jack_add_gpiods(runtime
->card
->dev
->parent
, jack
,
223 ARRAY_SIZE(hs_jack_gpios
),
227 * flag error but don't bail if jack detect is broken
228 * due to platform issues or bad BIOS/configuration
230 dev_err(runtime
->dev
,
231 "jack detection gpios not added, error %d\n", ret
);
235 * The firmware might enable the clock at
236 * boot (this information may or may not
237 * be reflected in the enable clock register).
238 * To change the rate we must disable the clock
239 * first to cover these cases. Due to common
240 * clock framework restrictions that do not allow
241 * to disable a clock that has not been enabled,
242 * we need to enable the clock first.
244 ret
= clk_prepare_enable(ctx
->mclk
);
246 clk_disable_unprepare(ctx
->mclk
);
248 ret
= clk_set_rate(ctx
->mclk
, CHT_PLAT_CLK_3_HZ
);
251 dev_err(runtime
->dev
, "unable to set MCLK rate\n");
256 static int cht_codec_fixup(struct snd_soc_pcm_runtime
*rtd
,
257 struct snd_pcm_hw_params
*params
)
259 struct snd_interval
*rate
= hw_param_interval(params
,
260 SNDRV_PCM_HW_PARAM_RATE
);
261 struct snd_interval
*channels
= hw_param_interval(params
,
262 SNDRV_PCM_HW_PARAM_CHANNELS
);
264 unsigned int fmt
= 0;
266 ret
= snd_soc_dai_set_tdm_slot(rtd
->cpu_dai
, 0x3, 0x3, 2, 16);
268 dev_err(rtd
->dev
, "can't set cpu_dai slot fmt: %d\n", ret
);
272 fmt
= SND_SOC_DAIFMT_LEFT_J
| SND_SOC_DAIFMT_NB_NF
273 | SND_SOC_DAIFMT_CBS_CFS
;
275 ret
= snd_soc_dai_set_fmt(rtd
->cpu_dai
, fmt
);
277 dev_err(rtd
->dev
, "can't set cpu_dai set fmt: %d\n", ret
);
281 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
282 rate
->min
= rate
->max
= 48000;
283 channels
->min
= channels
->max
= 2;
285 /* set SSP2 to 24-bit */
286 params_set_format(params
, SNDRV_PCM_FORMAT_S24_LE
);
290 static int cht_aif1_startup(struct snd_pcm_substream
*substream
)
292 return snd_pcm_hw_constraint_single(substream
->runtime
,
293 SNDRV_PCM_HW_PARAM_RATE
, 48000);
296 static int cht_max98090_headset_init(struct snd_soc_component
*component
)
298 struct snd_soc_card
*card
= component
->card
;
299 struct cht_mc_private
*ctx
= snd_soc_card_get_drvdata(card
);
300 struct snd_soc_jack
*jack
= &ctx
->jack
;
305 * TI supports 4 butons headset detection
311 jack_type
= SND_JACK_HEADPHONE
| SND_JACK_MICROPHONE
|
312 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
313 SND_JACK_BTN_2
| SND_JACK_BTN_3
;
315 ret
= snd_soc_card_jack_new(card
, "Headset Jack", jack_type
,
318 dev_err(card
->dev
, "Headset Jack creation failed %d\n", ret
);
322 return ts3a227e_enable_jack_detect(component
, jack
);
325 static const struct snd_soc_ops cht_aif1_ops
= {
326 .startup
= cht_aif1_startup
,
329 static const struct snd_soc_ops cht_be_ssp2_ops
= {
330 .hw_params
= cht_aif1_hw_params
,
333 static struct snd_soc_aux_dev cht_max98090_headset_dev
= {
334 .name
= "Headset Chip",
335 .init
= cht_max98090_headset_init
,
336 .codec_name
= "i2c-104C227E:00",
339 static struct snd_soc_dai_link cht_dailink
[] = {
340 [MERR_DPCM_AUDIO
] = {
341 .name
= "Audio Port",
342 .stream_name
= "Audio",
343 .cpu_dai_name
= "media-cpu-dai",
344 .codec_dai_name
= "snd-soc-dummy-dai",
345 .codec_name
= "snd-soc-dummy",
346 .platform_name
= "sst-mfld-platform",
351 .ops
= &cht_aif1_ops
,
353 [MERR_DPCM_DEEP_BUFFER
] = {
354 .name
= "Deep-Buffer Audio Port",
355 .stream_name
= "Deep-Buffer Audio",
356 .cpu_dai_name
= "deepbuffer-cpu-dai",
357 .codec_dai_name
= "snd-soc-dummy-dai",
358 .codec_name
= "snd-soc-dummy",
359 .platform_name
= "sst-mfld-platform",
363 .ops
= &cht_aif1_ops
,
365 [MERR_DPCM_COMPR
] = {
366 .name
= "Compressed Port",
367 .stream_name
= "Compress",
368 .cpu_dai_name
= "compress-cpu-dai",
369 .codec_dai_name
= "snd-soc-dummy-dai",
370 .codec_name
= "snd-soc-dummy",
371 .platform_name
= "sst-mfld-platform",
375 .name
= "SSP2-Codec",
377 .cpu_dai_name
= "ssp2-port",
378 .platform_name
= "sst-mfld-platform",
380 .codec_dai_name
= "HiFi",
381 .codec_name
= "i2c-193C9890:00",
382 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
383 | SND_SOC_DAIFMT_CBS_CFS
,
384 .init
= cht_codec_init
,
385 .be_hw_params_fixup
= cht_codec_fixup
,
388 .ops
= &cht_be_ssp2_ops
,
393 static struct snd_soc_card snd_soc_card_cht
= {
394 .name
= "chtmax98090",
395 .owner
= THIS_MODULE
,
396 .dai_link
= cht_dailink
,
397 .num_links
= ARRAY_SIZE(cht_dailink
),
398 .aux_dev
= &cht_max98090_headset_dev
,
400 .dapm_widgets
= cht_dapm_widgets
,
401 .num_dapm_widgets
= ARRAY_SIZE(cht_dapm_widgets
),
402 .dapm_routes
= cht_audio_map
,
403 .num_dapm_routes
= ARRAY_SIZE(cht_audio_map
),
404 .controls
= cht_mc_controls
,
405 .num_controls
= ARRAY_SIZE(cht_mc_controls
),
408 static int snd_cht_mc_probe(struct platform_device
*pdev
)
410 struct device
*dev
= &pdev
->dev
;
412 struct cht_mc_private
*drv
;
414 drv
= devm_kzalloc(&pdev
->dev
, sizeof(*drv
), GFP_ATOMIC
);
418 drv
->ts3a227e_present
= acpi_dev_found("104C227E");
419 if (!drv
->ts3a227e_present
) {
420 /* no need probe TI jack detection chip */
421 snd_soc_card_cht
.aux_dev
= NULL
;
422 snd_soc_card_cht
.num_aux_devs
= 0;
424 ret_val
= devm_acpi_dev_add_driver_gpios(dev
->parent
,
425 acpi_max98090_gpios
);
427 dev_dbg(dev
, "Unable to add GPIO mapping table\n");
430 /* register the soc card */
431 snd_soc_card_cht
.dev
= &pdev
->dev
;
432 snd_soc_card_set_drvdata(&snd_soc_card_cht
, drv
);
434 drv
->mclk
= devm_clk_get(&pdev
->dev
, "pmc_plt_clk_3");
435 if (IS_ERR(drv
->mclk
)) {
437 "Failed to get MCLK from pmc_plt_clk_3: %ld\n",
439 return PTR_ERR(drv
->mclk
);
442 ret_val
= devm_snd_soc_register_card(&pdev
->dev
, &snd_soc_card_cht
);
445 "snd_soc_register_card failed %d\n", ret_val
);
448 platform_set_drvdata(pdev
, &snd_soc_card_cht
);
452 static struct platform_driver snd_cht_mc_driver
= {
454 .name
= "cht-bsw-max98090",
456 .probe
= snd_cht_mc_probe
,
459 module_platform_driver(snd_cht_mc_driver
)
461 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
462 MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>");
463 MODULE_LICENSE("GPL v2");
464 MODULE_ALIAS("platform:cht-bsw-max98090");