2 * APBridge ALSA SoC dummy codec driver
3 * Copyright 2016 Google Inc.
4 * Copyright 2016 Linaro Ltd.
6 * Released under the GPLv2 only.
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/pm_runtime.h>
11 #include <sound/soc.h>
12 #include <sound/pcm_params.h>
13 #include <uapi/linux/input.h>
15 #include "audio_codec.h"
16 #include "audio_apbridgea.h"
17 #include "audio_manager.h"
19 static struct gbaudio_codec_info
*gbcodec
;
21 static struct gbaudio_data_connection
*
22 find_data(struct gbaudio_module_info
*module
, const char *name
)
24 struct gbaudio_data_connection
*data
;
26 list_for_each_entry(data
, &module
->data_list
, list
) {
27 if (name
&& !strncmp(data
->name
, name
, NAME_SIZE
))
33 static int find_stream(const char *name
)
37 if (strnstr(name
, "SPK Amp", NAME_SIZE
))
38 stream
|= GB_PLAYBACK
;
43 static int gbaudio_module_disable(struct gbaudio_codec_info
*codec
,
44 struct gbaudio_module_info
*module
,
48 uint16_t data_cport
, cportid
, i2s_port
;
49 int codec_state
, module_state
;
50 struct gbaudio_data_connection
*data
;
53 mutex_lock(&codec
->lock
);
55 codec_state
= codec
->stream
[dir
].state
;
56 if (codec_state
== GBAUDIO_CODEC_SHUTDOWN
) {
57 mutex_unlock(&codec
->lock
);
61 dai_name
= codec
->stream
[dir
].dai_name
;
63 module_state
= module
->ctrlstate
[dir
];
64 if (module_state
== GBAUDIO_CODEC_SHUTDOWN
) {
65 dev_dbg(codec
->dev
, "%s: module already configured\n",
71 data
= find_data(module
, dai_name
);
73 dev_err(codec
->dev
, "%s:%s DATA connection missing\n",
74 dai_name
, module
->name
);
78 if (codec_state
> GBAUDIO_CODEC_HWPARAMS
) {
79 data_cport
= data
->connection
->intf_cport_id
;
81 case SNDRV_PCM_STREAM_CAPTURE
:
82 ret
= gb_audio_gb_deactivate_rx(
83 module
->mgmt_connection
,
86 case SNDRV_PCM_STREAM_PLAYBACK
:
87 ret
= gb_audio_gb_deactivate_tx(
88 module
->mgmt_connection
,
95 dev_err_ratelimited(codec
->dev
, "deactivate for %s failed:%d\n",
99 dev_dbg(codec
->dev
, "Dynamic deactivate %s:%d DAI\n", dai_name
,
102 if (codec_state
> GBAUDIO_CODEC_SHUTDOWN
) {
103 cportid
= data
->connection
->hd_cport_id
;
105 case SNDRV_PCM_STREAM_CAPTURE
:
106 ret
= gb_audio_apbridgea_unregister_cport(
109 AUDIO_APBRIDGEA_DIRECTION_RX
);
111 case SNDRV_PCM_STREAM_PLAYBACK
:
112 ret
= gb_audio_apbridgea_unregister_cport(
115 AUDIO_APBRIDGEA_DIRECTION_TX
);
121 dev_err_ratelimited(codec
->dev
, "unregister_cport for %s failed:%d\n",
125 dev_dbg(codec
->dev
, "Dynamic Unregister %s:%d DAI\n", dai_name
,
128 module
->ctrlstate
[dir
] = GBAUDIO_CODEC_SHUTDOWN
;
131 mutex_unlock(&codec
->lock
);
135 static int gbaudio_module_enable(struct gbaudio_codec_info
*codec
,
136 struct gbaudio_module_info
*module
, int dir
)
139 __u16 i2s_port
, cportid
;
140 int codec_state
, module_state
;
142 uint8_t sig_bits
, channels
;
143 uint32_t format
, rate
;
144 struct gbaudio_data_connection
*data
;
145 const char *dai_name
;
147 mutex_lock(&codec
->lock
);
149 codec_state
= codec
->stream
[dir
].state
;
150 if (codec_state
== GBAUDIO_CODEC_SHUTDOWN
) {
151 mutex_unlock(&codec
->lock
);
155 dai_name
= codec
->stream
[dir
].dai_name
;
156 format
= codec
->stream
[dir
].format
;
157 channels
= codec
->stream
[dir
].channels
;
158 rate
= codec
->stream
[dir
].rate
;
159 sig_bits
= codec
->stream
[dir
].sig_bits
;
161 module_state
= module
->ctrlstate
[dir
];
162 if (module_state
== codec_state
) {
163 dev_dbg(codec
->dev
, "%s: module already configured\n",
169 data
= find_data(module
, dai_name
);
171 dev_err(codec
->dev
, "%s:%s DATA connection missing\n",
172 dai_name
, module
->name
);
178 if (module_state
< codec_state
) {
179 i2s_port
= 0; /* fixed for now */
180 cportid
= data
->connection
->hd_cport_id
;
182 case SNDRV_PCM_STREAM_CAPTURE
:
183 ret
= gb_audio_apbridgea_register_cport(
186 AUDIO_APBRIDGEA_DIRECTION_RX
);
188 case SNDRV_PCM_STREAM_PLAYBACK
:
189 ret
= gb_audio_apbridgea_register_cport(
192 AUDIO_APBRIDGEA_DIRECTION_TX
);
198 dev_err_ratelimited(codec
->dev
, "reg_cport for %s\n", module
->name
);
201 module_state
= GBAUDIO_CODEC_STARTUP
;
202 dev_dbg(codec
->dev
, "Dynamic Register %s:%d DAI\n", dai_name
,
207 if (module_state
< codec_state
) {
208 data_cport
= data
->connection
->intf_cport_id
;
209 ret
= gb_audio_gb_set_pcm(module
->mgmt_connection
, data_cport
,
210 format
, rate
, channels
, sig_bits
);
212 dev_err_ratelimited(codec
->dev
, "set_pcm for %s\n", module
->name
);
215 module_state
= GBAUDIO_CODEC_HWPARAMS
;
216 dev_dbg(codec
->dev
, "Dynamic hw_params %s:%d DAI\n", dai_name
,
221 if (module_state
< codec_state
) {
222 data_cport
= data
->connection
->intf_cport_id
;
224 case SNDRV_PCM_STREAM_CAPTURE
:
225 ret
= gb_audio_gb_set_rx_data_size(
226 module
->mgmt_connection
,
229 dev_err_ratelimited(codec
->dev
,
230 "set_rx_data_size for %s\n",
234 ret
= gb_audio_gb_activate_rx(module
->mgmt_connection
,
237 dev_err_ratelimited(codec
->dev
, "activate_rx for %s\n",
242 case SNDRV_PCM_STREAM_PLAYBACK
:
243 ret
= gb_audio_gb_set_tx_data_size(
244 module
->mgmt_connection
,
247 dev_err_ratelimited(codec
->dev
,
248 "set_tx_data_size for %s\n",
252 ret
= gb_audio_gb_activate_tx(module
->mgmt_connection
,
255 dev_err_ratelimited(codec
->dev
, "activate_tx for %s\n",
261 dev_err(codec
->dev
, "Inavlid stream direction\n");
265 module_state
= GBAUDIO_CODEC_PREPARE
;
266 dev_dbg(codec
->dev
, "Dynamic prepare %s:%d DAI\n", dai_name
,
271 module
->ctrlstate
[dir
] = module_state
;
272 mutex_unlock(&codec
->lock
);
276 int gbaudio_module_update(struct gbaudio_codec_info
*codec
,
278 struct gbaudio_module_info
*module
, int enable
)
281 int pb_state
, cap_state
;
283 dev_dbg(module
->dev
, "Module update %s sequence\n",
284 enable
? "Enable":"Disable");
286 stream
= find_stream(w_name
);
288 dev_dbg(codec
->dev
, "No action required for %s\n", w_name
);
292 /* check if playback active */
293 pb_state
= codec
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].state
;
294 if ((stream
& GB_PLAYBACK
) && (pb_state
> GBAUDIO_CODEC_SHUTDOWN
)) {
296 ret
= gbaudio_module_enable(codec
, module
,
297 SNDRV_PCM_STREAM_PLAYBACK
);
299 ret
= gbaudio_module_disable(codec
, module
,
300 SNDRV_PCM_STREAM_PLAYBACK
);
303 /* check if capture active */
304 cap_state
= codec
->stream
[SNDRV_PCM_STREAM_CAPTURE
].state
;
305 if ((stream
& GB_CAPTURE
) && (cap_state
> GBAUDIO_CODEC_SHUTDOWN
)) {
307 ret
= gbaudio_module_enable(codec
, module
,
308 SNDRV_PCM_STREAM_CAPTURE
);
310 ret
= gbaudio_module_disable(codec
, module
,
311 SNDRV_PCM_STREAM_CAPTURE
);
316 EXPORT_SYMBOL(gbaudio_module_update
);
321 static int gbcodec_startup(struct snd_pcm_substream
*substream
,
322 struct snd_soc_dai
*dai
)
325 __u16 i2s_port
, cportid
;
327 struct gbaudio_data_connection
*data
;
328 struct gbaudio_module_info
*module
;
329 struct gbaudio_codec_info
*codec
= dev_get_drvdata(dai
->dev
);
331 mutex_lock(&codec
->lock
);
333 if (list_empty(&codec
->module_list
)) {
334 dev_err(codec
->dev
, "No codec module available\n");
335 mutex_unlock(&codec
->lock
);
339 state
= codec
->stream
[substream
->stream
].state
;
340 list_for_each_entry(module
, &codec
->module_list
, list
) {
342 data
= find_data(module
, dai
->name
);
344 dev_err(dai
->dev
, "%s:%s DATA connection missing\n",
345 dai
->name
, module
->name
);
350 i2s_port
= 0; /* fixed for now */
351 cportid
= data
->connection
->hd_cport_id
;
352 switch (substream
->stream
) {
353 case SNDRV_PCM_STREAM_CAPTURE
:
354 ret
= gb_audio_apbridgea_register_cport(
357 AUDIO_APBRIDGEA_DIRECTION_RX
);
359 case SNDRV_PCM_STREAM_PLAYBACK
:
360 ret
= gb_audio_apbridgea_register_cport(
363 AUDIO_APBRIDGEA_DIRECTION_TX
);
366 dev_err(dai
->dev
, "Inavlid stream\n");
367 mutex_unlock(&codec
->lock
);
370 dev_dbg(dai
->dev
, "Register %s:%d DAI, ret:%d\n", dai
->name
,
372 state
= GBAUDIO_CODEC_STARTUP
;
373 module
->ctrlstate
[substream
->stream
] = state
;
374 dev_dbg(dai
->dev
, "%s: state:%d\n", module
->name
, state
);
376 codec
->stream
[substream
->stream
].state
= state
;
377 codec
->stream
[substream
->stream
].dai_name
= dai
->name
;
378 mutex_unlock(&codec
->lock
);
379 /* to prevent suspend in case of active audio */
380 pm_stay_awake(dai
->dev
);
385 static int gbmodule_shutdown_tx(struct gbaudio_module_info
*module
,
386 struct gbaudio_data_connection
*data
,
387 int codec_state
, struct device
*dev
)
389 int ret
, module_state
;
390 __u16 i2s_port
, cportid
;
392 module_state
= module
->ctrlstate
[0];
393 if (module_state
== GBAUDIO_CODEC_SHUTDOWN
) {
394 dev_dbg(dev
, "%s: module already configured\n",
400 cportid
= data
->connection
->intf_cport_id
;
401 if (module_state
>= GBAUDIO_CODEC_PREPARE
) {
402 ret
= gb_audio_gb_deactivate_tx(module
->mgmt_connection
,
408 /* unregister cport */
409 i2s_port
= 0; /* fixed for now */
410 cportid
= data
->connection
->hd_cport_id
;
411 ret
= gb_audio_apbridgea_unregister_cport(data
->connection
, i2s_port
,
413 AUDIO_APBRIDGEA_DIRECTION_TX
);
418 static int gbmodule_shutdown_rx(struct gbaudio_module_info
*module
,
419 struct gbaudio_data_connection
*data
,
420 int codec_state
, struct device
*dev
)
422 int ret
, module_state
;
423 __u16 i2s_port
, cportid
;
425 module_state
= module
->ctrlstate
[1];
426 if (module_state
== GBAUDIO_CODEC_SHUTDOWN
) {
427 dev_dbg(dev
, "%s: module already configured\n",
433 cportid
= data
->connection
->intf_cport_id
;
434 if (module_state
>= GBAUDIO_CODEC_PREPARE
) {
435 ret
= gb_audio_gb_deactivate_rx(module
->mgmt_connection
,
441 /* unregister cport */
442 i2s_port
= 0; /* fixed for now */
443 cportid
= data
->connection
->hd_cport_id
;
444 ret
= gb_audio_apbridgea_unregister_cport(data
->connection
, i2s_port
,
446 AUDIO_APBRIDGEA_DIRECTION_RX
);
451 static void gbcodec_shutdown(struct snd_pcm_substream
*substream
,
452 struct snd_soc_dai
*dai
)
455 struct gbaudio_module_info
*module
;
456 struct gbaudio_data_connection
*data
;
457 struct gbaudio_codec_info
*codec
= dev_get_drvdata(dai
->dev
);
459 mutex_lock(&codec
->lock
);
461 if (list_empty(&codec
->module_list
)) {
462 dev_err(codec
->dev
, "No codec module available\n");
463 codec
->stream
[substream
->stream
].state
= GBAUDIO_CODEC_SHUTDOWN
;
464 codec
->stream
[substream
->stream
].dai_name
= NULL
;
465 mutex_unlock(&codec
->lock
);
470 state
= codec
->stream
[substream
->stream
].state
;
471 list_for_each_entry(module
, &codec
->module_list
, list
) {
473 data
= find_data(module
, dai
->name
);
475 dev_err(dai
->dev
, "%s:%s DATA connection missing\n",
476 dai
->name
, module
->name
);
480 switch (substream
->stream
) {
481 case SNDRV_PCM_STREAM_PLAYBACK
:
482 ret
= gbmodule_shutdown_tx(module
, data
, state
,
485 case SNDRV_PCM_STREAM_CAPTURE
:
486 ret
= gbmodule_shutdown_rx(module
, data
, state
,
490 dev_dbg(dai
->dev
, "Unregister %s DAI, ret:%d\n", dai
->name
,
492 state
= GBAUDIO_CODEC_SHUTDOWN
;
493 module
->ctrlstate
[substream
->stream
] = state
;
494 dev_dbg(dai
->dev
, "%s: state:%d\n", module
->name
, state
);
496 codec
->stream
[substream
->stream
].state
= state
;
497 codec
->stream
[substream
->stream
].dai_name
= NULL
;
498 mutex_unlock(&codec
->lock
);
503 static int gbcodec_hw_params(struct snd_pcm_substream
*substream
,
504 struct snd_pcm_hw_params
*hwparams
,
505 struct snd_soc_dai
*dai
)
508 uint8_t sig_bits
, channels
;
509 uint32_t format
, rate
;
511 struct gbaudio_module_info
*module
;
512 struct gbaudio_data_connection
*data
;
514 struct gbaudio_codec_info
*codec
= dev_get_drvdata(dai
->dev
);
516 mutex_lock(&codec
->lock
);
518 if (list_empty(&codec
->module_list
)) {
519 dev_err(codec
->dev
, "No codec module available\n");
520 mutex_unlock(&codec
->lock
);
525 * assuming, currently only 48000 Hz, 16BIT_LE, stereo
526 * is supported, validate params before configuring codec
528 if (params_channels(hwparams
) != 2) {
529 dev_err(dai
->dev
, "Invalid channel count:%d\n",
530 params_channels(hwparams
));
531 mutex_unlock(&codec
->lock
);
534 channels
= params_channels(hwparams
);
536 if (params_rate(hwparams
) != 48000) {
537 dev_err(dai
->dev
, "Invalid sampling rate:%d\n",
538 params_rate(hwparams
));
539 mutex_unlock(&codec
->lock
);
542 rate
= GB_AUDIO_PCM_RATE_48000
;
544 if (params_format(hwparams
) != SNDRV_PCM_FORMAT_S16_LE
) {
545 dev_err(dai
->dev
, "Invalid format:%d\n",
546 params_format(hwparams
));
547 mutex_unlock(&codec
->lock
);
550 format
= GB_AUDIO_PCM_FMT_S16_LE
;
552 state
= codec
->stream
[substream
->stream
].state
;
553 list_for_each_entry(module
, &codec
->module_list
, list
) {
554 /* find the data connection */
555 data
= find_data(module
, dai
->name
);
557 dev_err(dai
->dev
, "%s:%s DATA connection missing\n",
558 dai
->name
, module
->name
);
562 data_cport
= data
->connection
->intf_cport_id
;
563 /* XXX check impact of sig_bit
564 * it should not change ideally
567 "cport:%d, rate:%d, channel %d, format %d, sig_bits:%d\n",
568 data_cport
, rate
, channels
, format
, sig_bits
);
569 ret
= gb_audio_gb_set_pcm(module
->mgmt_connection
, data_cport
,
570 format
, rate
, channels
, sig_bits
);
572 dev_err_ratelimited(dai
->dev
, "%d: Error during set_pcm\n", ret
);
575 if (state
< GBAUDIO_CODEC_HWPARAMS
) {
576 ret
= gb_audio_apbridgea_set_config(data
->connection
, 0,
577 AUDIO_APBRIDGEA_PCM_FMT_16
,
578 AUDIO_APBRIDGEA_PCM_RATE_48000
,
581 dev_err_ratelimited(dai
->dev
,
582 "%d: Error during set_config\n", ret
);
586 state
= GBAUDIO_CODEC_HWPARAMS
;
587 module
->ctrlstate
[substream
->stream
] = state
;
588 dev_dbg(dai
->dev
, "%s: state:%d\n", module
->name
, state
);
590 codec
->stream
[substream
->stream
].state
= state
;
591 codec
->stream
[substream
->stream
].format
= format
;
592 codec
->stream
[substream
->stream
].rate
= rate
;
593 codec
->stream
[substream
->stream
].channels
= channels
;
594 codec
->stream
[substream
->stream
].sig_bits
= sig_bits
;
597 mutex_unlock(&codec
->lock
);
601 static int gbmodule_prepare_tx(struct gbaudio_module_info
*module
,
602 struct gbaudio_data_connection
*data
,
603 int codec_state
, struct device
*dev
)
608 data_cport
= data
->connection
->intf_cport_id
;
609 ret
= gb_audio_gb_set_tx_data_size(module
->mgmt_connection
, data_cport
,
612 dev_err_ratelimited(dev
, "%d:Error during set_tx_data_size, cport:%d\n",
616 if (codec_state
< GBAUDIO_CODEC_PREPARE
) {
617 ret
= gb_audio_apbridgea_set_tx_data_size(data
->connection
, 0,
620 dev_err_ratelimited(dev
,
621 "%d:Error during apbridgea set_tx_data_size, cport\n",
626 ret
= gb_audio_gb_activate_tx(module
->mgmt_connection
,
629 dev_err_ratelimited(dev
, "%s:Error during activate stream,%d\n",
635 static int gbmodule_prepare_rx(struct gbaudio_module_info
*module
,
636 struct gbaudio_data_connection
*data
,
637 int codec_state
, struct device
*dev
)
642 data_cport
= data
->connection
->intf_cport_id
;
644 ret
= gb_audio_gb_set_rx_data_size(module
->mgmt_connection
, data_cport
,
647 dev_err_ratelimited(dev
, "%d:Error during set_rx_data_size, cport:%d\n",
651 if (codec_state
< GBAUDIO_CODEC_PREPARE
) {
652 ret
= gb_audio_apbridgea_set_rx_data_size(data
->connection
, 0,
655 dev_err_ratelimited(dev
,
656 "%d:Error during apbridgea_set_rx_data_size\n",
661 ret
= gb_audio_gb_activate_rx(module
->mgmt_connection
,
664 dev_err_ratelimited(dev
, "%s:Error during activate stream,%d\n",
670 static int gbcodec_prepare(struct snd_pcm_substream
*substream
,
671 struct snd_soc_dai
*dai
)
674 struct gbaudio_module_info
*module
;
676 struct gbaudio_data_connection
*data
;
677 struct gbaudio_codec_info
*codec
= dev_get_drvdata(dai
->dev
);
679 mutex_lock(&codec
->lock
);
681 if (list_empty(&codec
->module_list
)) {
682 dev_err(codec
->dev
, "No codec module available\n");
683 mutex_unlock(&codec
->lock
);
687 state
= codec
->stream
[substream
->stream
].state
;
688 list_for_each_entry(module
, &codec
->module_list
, list
) {
690 data
= find_data(module
, dai
->name
);
692 dev_err(dai
->dev
, "%s:%s DATA connection missing\n",
693 dai
->name
, module
->name
);
697 switch (substream
->stream
) {
698 case SNDRV_PCM_STREAM_PLAYBACK
:
699 ret
= gbmodule_prepare_tx(module
, data
, state
,
702 case SNDRV_PCM_STREAM_CAPTURE
:
703 ret
= gbmodule_prepare_rx(module
, data
, state
,
713 state
= GBAUDIO_CODEC_PREPARE
;
714 module
->ctrlstate
[substream
->stream
] = state
;
715 dev_dbg(dai
->dev
, "%s: state:%d\n", module
->name
, state
);
717 codec
->stream
[substream
->stream
].state
= state
;
720 mutex_unlock(&codec
->lock
);
724 static int gbcodec_mute_stream(struct snd_soc_dai
*dai
, int mute
, int stream
)
727 struct gbaudio_data_connection
*data
;
728 struct gbaudio_module_info
*module
;
729 struct gbaudio_codec_info
*codec
= dev_get_drvdata(dai
->dev
);
732 dev_dbg(dai
->dev
, "Mute:%d, Direction:%s\n", mute
,
733 stream
? "CAPTURE":"PLAYBACK");
735 mutex_lock(&codec
->lock
);
736 if (list_empty(&codec
->module_list
)) {
737 dev_err(codec
->dev
, "No codec module available\n");
739 codec
->stream
[stream
].state
= GBAUDIO_CODEC_STOP
;
744 mutex_unlock(&codec
->lock
);
748 list_for_each_entry(module
, &codec
->module_list
, list
) {
750 data
= find_data(module
, dai
->name
);
755 dev_err(dai
->dev
, "%s:%s DATA connection missing\n",
756 dai
->name
, module
->name
);
761 if (!mute
&& !stream
) {/* start playback */
762 ret
= gb_audio_apbridgea_prepare_tx(data
->connection
,
765 ret
= gb_audio_apbridgea_start_tx(data
->connection
,
767 codec
->stream
[stream
].state
= GBAUDIO_CODEC_START
;
768 } else if (!mute
&& stream
) {/* start capture */
769 ret
= gb_audio_apbridgea_prepare_rx(data
->connection
,
772 ret
= gb_audio_apbridgea_start_rx(data
->connection
,
774 codec
->stream
[stream
].state
= GBAUDIO_CODEC_START
;
775 } else if (mute
&& !stream
) {/* stop playback */
776 ret
= gb_audio_apbridgea_stop_tx(data
->connection
, 0);
778 ret
= gb_audio_apbridgea_shutdown_tx(data
->connection
,
780 codec
->stream
[stream
].state
= GBAUDIO_CODEC_STOP
;
781 } else if (mute
&& stream
) {/* stop capture */
782 ret
= gb_audio_apbridgea_stop_rx(data
->connection
, 0);
784 ret
= gb_audio_apbridgea_shutdown_rx(data
->connection
,
786 codec
->stream
[stream
].state
= GBAUDIO_CODEC_STOP
;
790 dev_err_ratelimited(dai
->dev
,
791 "%s:Error during %s %s stream:%d\n",
792 module
->name
, mute
? "Mute" : "Unmute",
793 stream
? "Capture" : "Playback", ret
);
796 mutex_unlock(&codec
->lock
);
800 static struct snd_soc_dai_ops gbcodec_dai_ops
= {
801 .startup
= gbcodec_startup
,
802 .shutdown
= gbcodec_shutdown
,
803 .hw_params
= gbcodec_hw_params
,
804 .prepare
= gbcodec_prepare
,
805 .mute_stream
= gbcodec_mute_stream
,
808 static int gbaudio_init_jack(struct gbaudio_module_info
*module
,
809 struct snd_soc_codec
*codec
)
813 if (!module
->num_jacks
)
816 /* register jack(s) in case any */
817 if (module
->num_jacks
> 1) {
818 dev_err(module
->dev
, "Currently supports max=1 jack\n");
822 snprintf(module
->jack_name
, NAME_SIZE
, "GB %d Headset Jack",
824 ret
= snd_soc_jack_new(codec
, module
->jack_name
, GBCODEC_JACK_MASK
,
825 &module
->headset_jack
);
827 dev_err(module
->dev
, "Failed to create new jack\n");
831 snprintf(module
->button_name
, NAME_SIZE
, "GB %d Button Jack",
833 ret
= snd_soc_jack_new(codec
, module
->button_name
,
834 GBCODEC_JACK_BUTTON_MASK
, &module
->button_jack
);
836 dev_err(module
->dev
, "Failed to create button jack\n");
840 ret
= snd_jack_set_key(module
->button_jack
.jack
, SND_JACK_BTN_0
,
843 dev_err(module
->dev
, "Failed to set BTN_0\n");
847 ret
= snd_jack_set_key(module
->button_jack
.jack
, SND_JACK_BTN_1
,
850 dev_err(module
->dev
, "Failed to set BTN_1\n");
854 ret
= snd_jack_set_key(module
->button_jack
.jack
, SND_JACK_BTN_2
,
857 dev_err(module
->dev
, "Failed to set BTN_2\n");
861 ret
= snd_jack_set_key(module
->button_jack
.jack
, SND_JACK_BTN_3
,
864 dev_err(module
->dev
, "Failed to set BTN_0\n");
869 * verify if this is really required
870 set_bit(INPUT_PROP_NO_DUMMY_RELEASE,
871 module->button_jack.jack->input_dev->propbit);
877 int gbaudio_register_module(struct gbaudio_module_info
*module
)
880 struct snd_soc_codec
*codec
;
881 struct snd_card
*card
;
882 struct snd_soc_jack
*jack
= NULL
;
885 dev_err(module
->dev
, "GB Codec not yet probed\n");
889 codec
= gbcodec
->codec
;
890 card
= codec
->card
->snd_card
;
892 down_write(&card
->controls_rwsem
);
894 if (module
->num_dais
) {
895 dev_err(gbcodec
->dev
,
896 "%d:DAIs not supported via gbcodec driver\n",
898 up_write(&card
->controls_rwsem
);
902 ret
= gbaudio_init_jack(module
, codec
);
904 up_write(&card
->controls_rwsem
);
908 if (module
->dapm_widgets
)
909 snd_soc_dapm_new_controls(&codec
->dapm
, module
->dapm_widgets
,
910 module
->num_dapm_widgets
);
911 if (module
->controls
)
912 snd_soc_add_codec_controls(codec
, module
->controls
,
913 module
->num_controls
);
914 if (module
->dapm_routes
)
915 snd_soc_dapm_add_routes(&codec
->dapm
, module
->dapm_routes
,
916 module
->num_dapm_routes
);
918 /* card already instantiated, create widgets here only */
919 if (codec
->card
->instantiated
) {
920 snd_soc_dapm_link_component_dai_widgets(codec
->card
,
922 #ifdef CONFIG_SND_JACK
923 /* register jack devices for this module from codec->jack_list */
924 list_for_each_entry(jack
, &codec
->jack_list
, list
) {
925 if ((jack
== &module
->headset_jack
)
926 || (jack
== &module
->button_jack
))
927 snd_device_register(codec
->card
->snd_card
,
933 mutex_lock(&gbcodec
->lock
);
934 list_add(&module
->list
, &gbcodec
->module_list
);
935 mutex_unlock(&gbcodec
->lock
);
937 if (codec
->card
->instantiated
)
938 ret
= snd_soc_dapm_new_widgets(&codec
->dapm
);
939 dev_dbg(codec
->dev
, "Registered %s module\n", module
->name
);
941 up_write(&card
->controls_rwsem
);
944 EXPORT_SYMBOL(gbaudio_register_module
);
946 static void gbaudio_codec_cleanup(struct gbaudio_module_info
*module
)
948 struct gbaudio_data_connection
*data
;
949 int pb_state
= gbcodec
->stream
[0].state
;
950 int cap_state
= gbcodec
->stream
[1].state
;
952 uint16_t i2s_port
, cportid
;
954 /* locks already acquired */
955 if (!pb_state
&& !cap_state
)
958 dev_dbg(gbcodec
->dev
, "%s: removed, cleanup APBridge\n", module
->name
);
959 if (pb_state
== GBAUDIO_CODEC_START
) {
960 /* cleanup PB path, only APBridge specific */
961 data
= find_data(module
, gbcodec
->stream
[0].dai_name
);
963 dev_err(gbcodec
->dev
, "%s: Missing data pointer\n",
968 if (list_is_singular(&gbcodec
->module_list
)) {
969 ret
= gb_audio_apbridgea_stop_tx(data
->connection
, 0);
972 ret
= gb_audio_apbridgea_shutdown_tx(data
->connection
,
977 i2s_port
= 0; /* fixed for now */
978 cportid
= data
->connection
->hd_cport_id
;
979 ret
= gb_audio_apbridgea_unregister_cport(data
->connection
,
981 AUDIO_APBRIDGEA_DIRECTION_TX
);
982 module
->ctrlstate
[0] = GBAUDIO_CODEC_SHUTDOWN
;
985 if (cap_state
== GBAUDIO_CODEC_START
) {
986 /* cleanup CAP path, only APBridge specific */
987 data
= find_data(module
, gbcodec
->stream
[1].dai_name
);
989 dev_err(gbcodec
->dev
, "%s: Missing data pointer\n",
993 if (list_is_singular(&gbcodec
->module_list
)) {
994 ret
= gb_audio_apbridgea_stop_rx(data
->connection
, 0);
997 ret
= gb_audio_apbridgea_shutdown_rx(data
->connection
,
1002 i2s_port
= 0; /* fixed for now */
1003 cportid
= data
->connection
->hd_cport_id
;
1004 ret
= gb_audio_apbridgea_unregister_cport(data
->connection
,
1006 AUDIO_APBRIDGEA_DIRECTION_RX
);
1007 module
->ctrlstate
[1] = GBAUDIO_CODEC_SHUTDOWN
;
1011 void gbaudio_unregister_module(struct gbaudio_module_info
*module
)
1013 struct snd_soc_codec
*codec
= gbcodec
->codec
;
1014 struct snd_card
*card
= codec
->card
->snd_card
;
1015 struct snd_soc_jack
*jack
, *next_j
;
1017 dev_dbg(codec
->dev
, "Unregister %s module\n", module
->name
);
1019 down_write(&card
->controls_rwsem
);
1020 mutex_lock(&gbcodec
->lock
);
1021 gbaudio_codec_cleanup(module
);
1022 list_del(&module
->list
);
1023 dev_dbg(codec
->dev
, "Process Unregister %s module\n", module
->name
);
1024 mutex_unlock(&gbcodec
->lock
);
1026 #ifdef CONFIG_SND_JACK
1027 /* free jack devices for this module from codec->jack_list */
1028 list_for_each_entry_safe(jack
, next_j
, &codec
->jack_list
, list
) {
1029 if ((jack
== &module
->headset_jack
)
1030 || (jack
== &module
->button_jack
)) {
1031 snd_device_free(codec
->card
->snd_card
, jack
->jack
);
1032 list_del(&jack
->list
);
1037 if (module
->dapm_routes
) {
1038 dev_dbg(codec
->dev
, "Removing %d routes\n",
1039 module
->num_dapm_routes
);
1040 snd_soc_dapm_del_routes(&codec
->dapm
, module
->dapm_routes
,
1041 module
->num_dapm_routes
);
1043 if (module
->controls
) {
1044 dev_dbg(codec
->dev
, "Removing %d controls\n",
1045 module
->num_controls
);
1046 snd_soc_remove_codec_controls(codec
, module
->controls
,
1047 module
->num_controls
);
1049 if (module
->dapm_widgets
) {
1050 dev_dbg(codec
->dev
, "Removing %d widgets\n",
1051 module
->num_dapm_widgets
);
1052 snd_soc_dapm_free_controls(&codec
->dapm
, module
->dapm_widgets
,
1053 module
->num_dapm_widgets
);
1056 dev_dbg(codec
->dev
, "Unregistered %s module\n", module
->name
);
1058 up_write(&card
->controls_rwsem
);
1060 EXPORT_SYMBOL(gbaudio_unregister_module
);
1065 static int gbcodec_probe(struct snd_soc_codec
*codec
)
1067 struct gbaudio_codec_info
*info
;
1069 info
= devm_kzalloc(codec
->dev
, sizeof(*info
), GFP_KERNEL
);
1073 info
->dev
= codec
->dev
;
1074 INIT_LIST_HEAD(&info
->module_list
);
1075 mutex_init(&info
->lock
);
1076 info
->codec
= codec
;
1077 snd_soc_codec_set_drvdata(codec
, info
);
1080 device_init_wakeup(codec
->dev
, 1);
1084 static int gbcodec_remove(struct snd_soc_codec
*codec
)
1086 /* Empty function for now */
1090 static u8 gbcodec_reg
[GBCODEC_REG_COUNT
] = {
1091 [GBCODEC_CTL_REG
] = GBCODEC_CTL_REG_DEFAULT
,
1092 [GBCODEC_MUTE_REG
] = GBCODEC_MUTE_REG_DEFAULT
,
1093 [GBCODEC_PB_LVOL_REG
] = GBCODEC_PB_VOL_REG_DEFAULT
,
1094 [GBCODEC_PB_RVOL_REG
] = GBCODEC_PB_VOL_REG_DEFAULT
,
1095 [GBCODEC_CAP_LVOL_REG
] = GBCODEC_CAP_VOL_REG_DEFAULT
,
1096 [GBCODEC_CAP_RVOL_REG
] = GBCODEC_CAP_VOL_REG_DEFAULT
,
1097 [GBCODEC_APB1_MUX_REG
] = GBCODEC_APB1_MUX_REG_DEFAULT
,
1098 [GBCODEC_APB2_MUX_REG
] = GBCODEC_APB2_MUX_REG_DEFAULT
,
1101 static int gbcodec_write(struct snd_soc_codec
*codec
, unsigned int reg
,
1106 if (reg
== SND_SOC_NOPM
)
1109 BUG_ON(reg
>= GBCODEC_REG_COUNT
);
1111 gbcodec_reg
[reg
] = value
;
1112 dev_dbg(codec
->dev
, "reg[%d] = 0x%x\n", reg
, value
);
1117 static unsigned int gbcodec_read(struct snd_soc_codec
*codec
,
1120 unsigned int val
= 0;
1122 if (reg
== SND_SOC_NOPM
)
1125 BUG_ON(reg
>= GBCODEC_REG_COUNT
);
1127 val
= gbcodec_reg
[reg
];
1128 dev_dbg(codec
->dev
, "reg[%d] = 0x%x\n", reg
, val
);
1133 static struct snd_soc_dai_driver gbaudio_dai
[] = {
1135 .name
= "greybus-apb1",
1138 .stream_name
= "GB Audio Playback",
1139 .rates
= SNDRV_PCM_RATE_48000
,
1140 .formats
= SNDRV_PCM_FORMAT_S16_LE
,
1147 .stream_name
= "GB Audio Capture",
1148 .rates
= SNDRV_PCM_RATE_48000
,
1149 .formats
= SNDRV_PCM_FORMAT_S16_LE
,
1155 .ops
= &gbcodec_dai_ops
,
1159 static struct snd_soc_codec_driver soc_codec_dev_gbaudio
= {
1160 .probe
= gbcodec_probe
,
1161 .remove
= gbcodec_remove
,
1163 .read
= gbcodec_read
,
1164 .write
= gbcodec_write
,
1166 .reg_cache_size
= GBCODEC_REG_COUNT
,
1167 .reg_cache_default
= gbcodec_reg_defaults
,
1170 .idle_bias_off
= true,
1171 .ignore_pmdown_time
= 1,
1175 static int gbaudio_codec_suspend(struct device
*dev
)
1177 dev_dbg(dev
, "%s: suspend\n", __func__
);
1181 static int gbaudio_codec_resume(struct device
*dev
)
1183 dev_dbg(dev
, "%s: resume\n", __func__
);
1187 static const struct dev_pm_ops gbaudio_codec_pm_ops
= {
1188 .suspend
= gbaudio_codec_suspend
,
1189 .resume
= gbaudio_codec_resume
,
1193 static int gbaudio_codec_probe(struct platform_device
*pdev
)
1195 return snd_soc_register_codec(&pdev
->dev
, &soc_codec_dev_gbaudio
,
1196 gbaudio_dai
, ARRAY_SIZE(gbaudio_dai
));
1199 static int gbaudio_codec_remove(struct platform_device
*pdev
)
1201 snd_soc_unregister_codec(&pdev
->dev
);
1205 static const struct of_device_id greybus_asoc_machine_of_match
[] = {
1206 { .compatible
= "toshiba,apb-dummy-codec", },
1210 static struct platform_driver gbaudio_codec_driver
= {
1212 .name
= "apb-dummy-codec",
1213 .owner
= THIS_MODULE
,
1215 .pm
= &gbaudio_codec_pm_ops
,
1217 .of_match_table
= greybus_asoc_machine_of_match
,
1219 .probe
= gbaudio_codec_probe
,
1220 .remove
= gbaudio_codec_remove
,
1222 module_platform_driver(gbaudio_codec_driver
);
1224 MODULE_DESCRIPTION("APBridge ALSA SoC dummy codec driver");
1225 MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@linaro.org>");
1226 MODULE_LICENSE("GPL v2");
1227 MODULE_ALIAS("platform:apb-dummy-codec");