2 * ALSA ASoC Machine Driver for Allo Piano DAC Plus Subwoofer
4 * Author: Baswaraj K <jaikumar@cem-solutions.net>
6 * based on code by David Knell <david.knell@gmail.com)
7 * based on code by Daniel Matuschek <info@crazy-audio.com>
8 * based on code by Florian Meier <florian.meier@koalo.de>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
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.
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/gpio/consumer.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <linux/firmware.h>
28 #include <linux/delay.h>
29 #include <sound/tlv.h>
30 #include "../codecs/pcm512x.h"
32 #define P_DAC_LEFT_MUTE 0x10
33 #define P_DAC_RIGHT_MUTE 0x01
34 #define P_DAC_MUTE 0x11
35 #define P_DAC_UNMUTE 0x00
47 unsigned int dual_mode;
48 unsigned int set_lowpass;
49 unsigned int set_mode;
50 unsigned int set_rate;
51 unsigned int dsp_page_number;
54 static bool digital_gain_0db_limit = true;
57 static struct gpio_desc *mute_gpio[2];
59 static const char * const allo_piano_mode_texts[] = {
66 static SOC_ENUM_SINGLE_DECL(allo_piano_mode_enum,
67 0, 0, allo_piano_mode_texts);
69 static const char * const allo_piano_dual_mode_texts[] = {
75 static SOC_ENUM_SINGLE_DECL(allo_piano_dual_mode_enum,
76 0, 0, allo_piano_dual_mode_texts);
78 static const char * const allo_piano_dsp_low_pass_texts[] = {
96 static SOC_ENUM_SINGLE_DECL(allo_piano_enum,
97 0, 0, allo_piano_dsp_low_pass_texts);
99 static int __snd_allo_piano_dsp_program(struct snd_soc_pcm_runtime *rtd,
100 unsigned int mode, unsigned int rate, unsigned int lowpass)
102 const struct firmware *fw;
103 struct snd_soc_card *card = rtd->card;
104 struct glb_pool *glb_ptr = card->drvdata;
105 char firmware_name[60];
106 int ret = 0, dac = 0;
110 else if (rate <= 68000)
112 else if (rate <= 92000)
114 else if (rate <= 136000)
116 else if (rate <= 184000)
122 glb_ptr->set_lowpass = lowpass = 0;
125 glb_ptr->set_mode = mode = 0;
128 glb_ptr->dual_mode = 0;
130 /* same configuration loaded */
131 if ((rate == glb_ptr->set_rate) && (lowpass == glb_ptr->set_lowpass)
132 && (mode == glb_ptr->set_mode))
140 snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component,
141 PCM512x_MUTE, P_DAC_UNMUTE);
142 snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component,
143 PCM512x_MUTE, P_DAC_MUTE);
144 glb_ptr->set_rate = rate;
145 glb_ptr->set_mode = mode;
146 glb_ptr->set_lowpass = lowpass;
150 snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component,
151 PCM512x_MUTE, P_DAC_UNMUTE);
152 snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component,
153 PCM512x_MUTE, P_DAC_UNMUTE);
156 for (dac = 0; dac < rtd->num_codecs; dac++) {
157 struct dsp_code *dsp_code_read;
160 if (dac == 0) { /* high */
161 snprintf(firmware_name, sizeof(firmware_name),
162 "allo/piano/2.2/allo-piano-dsp-%d-%d-%d.bin",
163 rate, ((lowpass * 10) + 60), dac);
165 snprintf(firmware_name, sizeof(firmware_name),
166 "allo/piano/2.%d/allo-piano-dsp-%d-%d-%d.bin",
167 (mode - 1), rate, ((lowpass * 10) + 60), dac);
170 dev_info(rtd->card->dev, "Dsp Firmware File Name: %s\n",
173 ret = request_firmware(&fw, firmware_name, rtd->card->dev);
175 dev_err(rtd->card->dev,
176 "Error: Allo Piano Firmware %s missing. %d\n",
181 while (i < (fw->size - 1)) {
182 dsp_code_read = (struct dsp_code *)&fw->data[i];
184 if (dsp_code_read->offset == 0) {
185 glb_ptr->dsp_page_number = dsp_code_read->val;
186 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, dac)->component,
187 PCM512x_PAGE_BASE(0),
190 } else if (dsp_code_read->offset != 0) {
191 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, dac)->component,
193 glb_ptr->dsp_page_number) +
194 dsp_code_read->offset),
198 dev_err(rtd->card->dev,
199 "Failed to write Register: %d\n", ret);
200 release_firmware(fw);
205 release_firmware(fw);
207 glb_ptr->set_rate = rate;
208 glb_ptr->set_mode = mode;
209 glb_ptr->set_lowpass = lowpass;
216 static int snd_allo_piano_dsp_program(struct snd_soc_pcm_runtime *rtd,
217 unsigned int mode, unsigned int rate, unsigned int lowpass)
219 struct snd_soc_card *card = rtd->card;
220 struct glb_pool *glb_ptr = card->drvdata;
223 mutex_lock(&glb_ptr->lock);
225 ret = __snd_allo_piano_dsp_program(rtd, mode, rate, lowpass);
227 mutex_unlock(&glb_ptr->lock);
232 static int snd_allo_piano_dual_mode_get(struct snd_kcontrol *kcontrol,
233 struct snd_ctl_elem_value *ucontrol)
235 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
236 struct glb_pool *glb_ptr = card->drvdata;
238 ucontrol->value.integer.value[0] = glb_ptr->dual_mode;
243 static int snd_allo_piano_dual_mode_put(struct snd_kcontrol *kcontrol,
244 struct snd_ctl_elem_value *ucontrol)
246 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
247 struct glb_pool *glb_ptr = card->drvdata;
248 struct snd_soc_pcm_runtime *rtd;
249 struct snd_card *snd_card_ptr = card->snd_card;
250 struct snd_kcontrol *kctl;
251 struct soc_mixer_control *mc;
252 unsigned int left_val = 0, right_val = 0;
254 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
256 if (ucontrol->value.integer.value[0] > 0) {
257 glb_ptr->dual_mode = ucontrol->value.integer.value[0];
258 glb_ptr->set_mode = 0;
260 if (glb_ptr->set_mode <= 0) {
261 glb_ptr->dual_mode = 1;
262 glb_ptr->set_mode = 0;
264 glb_ptr->dual_mode = 0;
269 if (glb_ptr->dual_mode == 1) { // Dual Mono
270 snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component,
271 PCM512x_MUTE, P_DAC_RIGHT_MUTE);
272 snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component,
273 PCM512x_MUTE, P_DAC_LEFT_MUTE);
274 snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component,
275 PCM512x_DIGITAL_VOLUME_3, 0xff);
276 snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component,
277 PCM512x_DIGITAL_VOLUME_2, 0xff);
279 list_for_each_entry(kctl, &snd_card_ptr->controls, list) {
280 if (!strncmp(kctl->id.name, "Main Digital Playback Volume",
281 sizeof(kctl->id.name))) {
282 mc = (struct soc_mixer_control *)
287 if (!strncmp(kctl->id.name, "Sub Digital Playback Volume",
288 sizeof(kctl->id.name))) {
289 mc = (struct soc_mixer_control *)
296 left_val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 0)->component,
297 PCM512x_DIGITAL_VOLUME_2);
298 right_val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 1)->component,
299 PCM512x_DIGITAL_VOLUME_3);
301 list_for_each_entry(kctl, &snd_card_ptr->controls, list) {
302 if (!strncmp(kctl->id.name, "Main Digital Playback Volume",
303 sizeof(kctl->id.name))) {
304 mc = (struct soc_mixer_control *)
306 mc->rreg = PCM512x_DIGITAL_VOLUME_3;
309 if (!strncmp(kctl->id.name, "Sub Digital Playback Volume",
310 sizeof(kctl->id.name))) {
311 mc = (struct soc_mixer_control *)
313 mc->rreg = PCM512x_DIGITAL_VOLUME_2;
318 snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component,
319 PCM512x_DIGITAL_VOLUME_3, left_val);
320 snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component,
321 PCM512x_DIGITAL_VOLUME_2, right_val);
322 snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component,
323 PCM512x_MUTE, P_DAC_UNMUTE);
324 snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component,
325 PCM512x_MUTE, P_DAC_UNMUTE);
331 static int snd_allo_piano_mode_get(struct snd_kcontrol *kcontrol,
332 struct snd_ctl_elem_value *ucontrol)
334 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
335 struct glb_pool *glb_ptr = card->drvdata;
337 ucontrol->value.integer.value[0] = glb_ptr->set_mode;
341 static int snd_allo_piano_mode_put(struct snd_kcontrol *kcontrol,
342 struct snd_ctl_elem_value *ucontrol)
344 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
345 struct snd_soc_pcm_runtime *rtd;
346 struct glb_pool *glb_ptr = card->drvdata;
347 struct snd_card *snd_card_ptr = card->snd_card;
348 struct snd_kcontrol *kctl;
349 struct soc_mixer_control *mc;
350 unsigned int left_val = 0, right_val = 0;
352 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
354 if ((glb_ptr->dual_mode == 1) &&
355 (ucontrol->value.integer.value[0] > 0)) {
356 left_val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 0)->component,
357 PCM512x_DIGITAL_VOLUME_2);
358 right_val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 1)->component,
359 PCM512x_DIGITAL_VOLUME_2);
361 list_for_each_entry(kctl, &snd_card_ptr->controls, list) {
362 if (!strncmp(kctl->id.name, "Main Digital Playback Volume",
363 sizeof(kctl->id.name))) {
364 mc = (struct soc_mixer_control *)
366 mc->rreg = PCM512x_DIGITAL_VOLUME_3;
369 if (!strncmp(kctl->id.name, "Sub Digital Playback Volume",
370 sizeof(kctl->id.name))) {
371 mc = (struct soc_mixer_control *)
373 mc->rreg = PCM512x_DIGITAL_VOLUME_2;
377 snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component,
378 PCM512x_DIGITAL_VOLUME_3, left_val);
379 snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component,
380 PCM512x_DIGITAL_VOLUME_3, right_val);
383 return(snd_allo_piano_dsp_program(rtd,
384 ucontrol->value.integer.value[0],
385 glb_ptr->set_rate, glb_ptr->set_lowpass));
388 static int snd_allo_piano_lowpass_get(struct snd_kcontrol *kcontrol,
389 struct snd_ctl_elem_value *ucontrol)
391 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
392 struct glb_pool *glb_ptr = card->drvdata;
394 ucontrol->value.integer.value[0] = glb_ptr->set_lowpass;
398 static int snd_allo_piano_lowpass_put(struct snd_kcontrol *kcontrol,
399 struct snd_ctl_elem_value *ucontrol)
401 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
402 struct snd_soc_pcm_runtime *rtd;
403 struct glb_pool *glb_ptr = card->drvdata;
405 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
406 return(snd_allo_piano_dsp_program(rtd,
407 glb_ptr->set_mode, glb_ptr->set_rate,
408 ucontrol->value.integer.value[0]));
411 static int pcm512x_get_reg_sub(struct snd_kcontrol *kcontrol,
412 struct snd_ctl_elem_value *ucontrol)
414 struct soc_mixer_control *mc =
415 (struct soc_mixer_control *)kcontrol->private_value;
416 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
417 struct glb_pool *glb_ptr = card->drvdata;
418 struct snd_soc_pcm_runtime *rtd;
419 unsigned int left_val = 0;
420 unsigned int right_val = 0;
421 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
423 right_val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 1)->component,
424 PCM512x_DIGITAL_VOLUME_3);
425 if (glb_ptr->dual_mode != 1) {
426 left_val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 1)->component,
427 PCM512x_DIGITAL_VOLUME_2);
430 left_val = right_val;
433 ucontrol->value.integer.value[0] =
434 (~(left_val >> mc->shift)) & mc->max;
435 ucontrol->value.integer.value[1] =
436 (~(right_val >> mc->shift)) & mc->max;
441 static int pcm512x_set_reg_sub(struct snd_kcontrol *kcontrol,
442 struct snd_ctl_elem_value *ucontrol)
444 struct soc_mixer_control *mc =
445 (struct soc_mixer_control *)kcontrol->private_value;
446 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
447 struct glb_pool *glb_ptr = card->drvdata;
448 struct snd_soc_pcm_runtime *rtd;
449 unsigned int left_val = (ucontrol->value.integer.value[0] & mc->max);
450 unsigned int right_val = (ucontrol->value.integer.value[1] & mc->max);
453 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
455 if (digital_gain_0db_limit) {
456 ret = snd_soc_limit_volume(card, "Subwoofer Playback Volume",
459 dev_warn(card->dev, "Failed to set volume limit: %d\n",
463 // When in Dual Mono, Sub vol control should not set anything.
464 if (glb_ptr->dual_mode != 1) { //Not in Dual Mono mode
466 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component,
467 PCM512x_DIGITAL_VOLUME_2, (~left_val));
471 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component,
472 PCM512x_DIGITAL_VOLUME_3, (~right_val));
481 static int pcm512x_get_reg_sub_switch(struct snd_kcontrol *kcontrol,
482 struct snd_ctl_elem_value *ucontrol)
484 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
485 struct snd_soc_pcm_runtime *rtd;
488 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
489 val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 1)->component, PCM512x_MUTE);
491 ucontrol->value.integer.value[0] =
492 (val & P_DAC_LEFT_MUTE) ? P_UNMUTE : P_MUTE;
493 ucontrol->value.integer.value[1] =
494 (val & P_DAC_RIGHT_MUTE) ? P_UNMUTE : P_MUTE;
499 static int pcm512x_set_reg_sub_switch(struct snd_kcontrol *kcontrol,
500 struct snd_ctl_elem_value *ucontrol)
502 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
503 struct snd_soc_pcm_runtime *rtd;
504 struct glb_pool *glb_ptr = card->drvdata;
505 unsigned int left_val = (ucontrol->value.integer.value[0]);
506 unsigned int right_val = (ucontrol->value.integer.value[1]);
509 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
510 if (glb_ptr->set_mode != 1) {
511 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component, PCM512x_MUTE,
512 ~((left_val & 0x01)<<4 | (right_val & 0x01)));
520 static int pcm512x_get_reg_master(struct snd_kcontrol *kcontrol,
521 struct snd_ctl_elem_value *ucontrol)
523 struct soc_mixer_control *mc =
524 (struct soc_mixer_control *)kcontrol->private_value;
525 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
526 struct glb_pool *glb_ptr = card->drvdata;
527 struct snd_soc_pcm_runtime *rtd;
528 unsigned int left_val = 0, right_val = 0;
530 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
532 left_val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 0)->component,
533 PCM512x_DIGITAL_VOLUME_2);
535 if (glb_ptr->dual_mode == 1) { // in Dual Mono mode
536 right_val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 1)->component,
537 PCM512x_DIGITAL_VOLUME_3);
539 right_val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 0)->component,
540 PCM512x_DIGITAL_VOLUME_3);
543 ucontrol->value.integer.value[0] =
544 (~(left_val >> mc->shift)) & mc->max;
545 ucontrol->value.integer.value[1] =
546 (~(right_val >> mc->shift)) & mc->max;
551 static int pcm512x_set_reg_master(struct snd_kcontrol *kcontrol,
552 struct snd_ctl_elem_value *ucontrol)
554 struct soc_mixer_control *mc =
555 (struct soc_mixer_control *)kcontrol->private_value;
556 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
557 struct glb_pool *glb_ptr = card->drvdata;
558 struct snd_soc_pcm_runtime *rtd;
559 unsigned int left_val = (ucontrol->value.integer.value[0] & mc->max);
560 unsigned int right_val = (ucontrol->value.integer.value[1] & mc->max);
563 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
565 if (digital_gain_0db_limit) {
566 ret = snd_soc_limit_volume(card, "Master Playback Volume",
569 dev_warn(card->dev, "Failed to set volume limit: %d\n",
573 if (glb_ptr->dual_mode == 1) { //in Dual Mono Mode
575 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component,
576 PCM512x_DIGITAL_VOLUME_2, (~left_val));
580 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component,
581 PCM512x_DIGITAL_VOLUME_3, (~right_val));
587 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component,
588 PCM512x_DIGITAL_VOLUME_2, (~left_val));
592 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component,
593 PCM512x_DIGITAL_VOLUME_3, (~right_val));
601 static int pcm512x_get_reg_master_switch(struct snd_kcontrol *kcontrol,
602 struct snd_ctl_elem_value *ucontrol)
604 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
605 struct glb_pool *glb_ptr = card->drvdata;
606 struct snd_soc_pcm_runtime *rtd;
609 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
611 val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 0)->component, PCM512x_MUTE);
613 ucontrol->value.integer.value[0] =
614 (val & P_DAC_LEFT_MUTE) ? P_UNMUTE : P_MUTE;
616 if (glb_ptr->dual_mode == 1) {
617 val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 1)->component, PCM512x_MUTE);
619 ucontrol->value.integer.value[1] =
620 (val & P_DAC_RIGHT_MUTE) ? P_UNMUTE : P_MUTE;
625 static int pcm512x_set_reg_master_switch(struct snd_kcontrol *kcontrol,
626 struct snd_ctl_elem_value *ucontrol)
628 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
629 struct snd_soc_pcm_runtime *rtd;
630 struct glb_pool *glb_ptr = card->drvdata;
631 unsigned int left_val = (ucontrol->value.integer.value[0]);
632 unsigned int right_val = (ucontrol->value.integer.value[1]);
635 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
636 if (glb_ptr->dual_mode == 1) {
637 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component, PCM512x_MUTE,
638 ~((left_val & 0x01)<<4));
641 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component, PCM512x_MUTE,
642 ~((right_val & 0x01)));
646 } else if (glb_ptr->set_mode == 1) {
647 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component, PCM512x_MUTE,
648 ~((left_val & 0x01)<<4 | (right_val & 0x01)));
653 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component, PCM512x_MUTE,
654 ~((left_val & 0x01)<<4 | (right_val & 0x01)));
658 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component, PCM512x_MUTE,
659 ~((left_val & 0x01)<<4 | (right_val & 0x01)));
666 static const DECLARE_TLV_DB_SCALE(digital_tlv_sub, -10350, 50, 1);
667 static const DECLARE_TLV_DB_SCALE(digital_tlv_master, -10350, 50, 1);
669 static const struct snd_kcontrol_new allo_piano_controls[] = {
670 SOC_ENUM_EXT("Subwoofer mode Route",
671 allo_piano_mode_enum,
672 snd_allo_piano_mode_get,
673 snd_allo_piano_mode_put),
675 SOC_ENUM_EXT("Dual Mode Route",
676 allo_piano_dual_mode_enum,
677 snd_allo_piano_dual_mode_get,
678 snd_allo_piano_dual_mode_put),
680 SOC_ENUM_EXT("Lowpass Route", allo_piano_enum,
681 snd_allo_piano_lowpass_get,
682 snd_allo_piano_lowpass_put),
684 SOC_DOUBLE_R_EXT_TLV("Subwoofer Playback Volume",
685 PCM512x_DIGITAL_VOLUME_2,
686 PCM512x_DIGITAL_VOLUME_3, 0, 255, 1,
691 SOC_DOUBLE_EXT("Subwoofer Playback Switch",
694 PCM512x_RQMR_SHIFT, 1, 1,
695 pcm512x_get_reg_sub_switch,
696 pcm512x_set_reg_sub_switch),
698 SOC_DOUBLE_R_EXT_TLV("Master Playback Volume",
699 PCM512x_DIGITAL_VOLUME_2,
700 PCM512x_DIGITAL_VOLUME_3, 0, 255, 1,
701 pcm512x_get_reg_master,
702 pcm512x_set_reg_master,
705 SOC_DOUBLE_EXT("Master Playback Switch",
708 PCM512x_RQMR_SHIFT, 1, 1,
709 pcm512x_get_reg_master_switch,
710 pcm512x_set_reg_master_switch),
713 static const char * const codec_ctl_pfx[] = { "Main", "Sub" };
714 static const char * const codec_ctl_name[] = {
715 "Digital Playback Volume",
716 "Digital Playback Switch",
717 "Auto Mute Mono Switch",
719 "Auto Mute Time Left",
720 "Auto Mute Time Right",
721 "Clock Missing Period",
725 "Volume Ramp Down Emergency Rate",
726 "Volume Ramp Down Emergency Step",
727 "Volume Ramp Up Rate",
728 "Volume Ramp Down Rate",
729 "Volume Ramp Up Step",
730 "Volume Ramp Down Step"
733 static int snd_allo_piano_dac_init(struct snd_soc_pcm_runtime *rtd)
735 struct snd_soc_card *card = rtd->card;
736 struct glb_pool *glb_ptr;
737 struct snd_kcontrol *kctl;
740 glb_ptr = kzalloc(sizeof(struct glb_pool), GFP_KERNEL);
744 card->drvdata = glb_ptr;
745 glb_ptr->dual_mode = 2;
746 glb_ptr->set_mode = 0;
748 mutex_init(&glb_ptr->lock);
750 if (digital_gain_0db_limit) {
753 //Set volume limit on both dacs
754 for (i = 0; i < ARRAY_SIZE(codec_ctl_pfx); i++) {
757 sprintf(cname, "%s %s", codec_ctl_pfx[i], codec_ctl_name[0]);
758 ret = snd_soc_limit_volume(card, cname, 207);
760 dev_warn(card->dev, "Failed to set volume limit: %d\n",
765 // Remove codec controls
766 for (i = 0; i < ARRAY_SIZE(codec_ctl_pfx); i++) {
767 for (j = 0; j < ARRAY_SIZE(codec_ctl_name); j++) {
770 sprintf(cname, "%s %s", codec_ctl_pfx[i], codec_ctl_name[j]);
771 kctl = snd_soc_card_get_kcontrol(card, cname);
773 dev_err(rtd->card->dev, "Control %s not found\n",
777 SNDRV_CTL_ELEM_ACCESS_READWRITE;
778 snd_ctl_remove(card->snd_card, kctl);
786 static void snd_allo_piano_gpio_mute(struct snd_soc_card *card)
789 gpiod_set_value_cansleep(mute_gpio[0], P_MUTE);
792 gpiod_set_value_cansleep(mute_gpio[1], P_MUTE);
795 static void snd_allo_piano_gpio_unmute(struct snd_soc_card *card)
798 gpiod_set_value_cansleep(mute_gpio[0], P_UNMUTE);
801 gpiod_set_value_cansleep(mute_gpio[1], P_UNMUTE);
804 static int snd_allo_piano_set_bias_level(struct snd_soc_card *card,
805 struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
807 struct snd_soc_pcm_runtime *rtd;
808 struct snd_soc_dai *codec_dai;
810 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
811 codec_dai = asoc_rtd_to_codec(rtd, 0);
813 if (dapm->dev != codec_dai->dev)
817 case SND_SOC_BIAS_PREPARE:
818 if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
821 snd_allo_piano_gpio_unmute(card);
824 case SND_SOC_BIAS_STANDBY:
825 if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
828 snd_allo_piano_gpio_mute(card);
838 static int snd_allo_piano_dac_startup(
839 struct snd_pcm_substream *substream)
841 struct snd_soc_pcm_runtime *rtd = substream->private_data;
842 struct snd_soc_card *card = rtd->card;
844 snd_allo_piano_gpio_mute(card);
849 static int snd_allo_piano_dac_hw_params(
850 struct snd_pcm_substream *substream,
851 struct snd_pcm_hw_params *params)
853 struct snd_soc_pcm_runtime *rtd = substream->private_data;
854 unsigned int rate = params_rate(params);
855 struct snd_soc_card *card = rtd->card;
856 struct glb_pool *glb_ptr = card->drvdata;
857 int ret = 0, val = 0, dac;
859 for (dac = 0; (glb_mclk && dac < 2); dac++) {
860 /* Configure the PLL clock reference for both the Codecs */
861 val = snd_soc_component_read(asoc_rtd_to_codec(rtd, dac)->component,
865 snd_soc_component_write(asoc_rtd_to_codec(rtd, dac)->component,
869 dev_info(asoc_rtd_to_codec(rtd, dac)->component->dev,
870 "Setting BCLK as input clock & Enable PLL\n");
872 snd_soc_component_write(asoc_rtd_to_codec(rtd, dac)->component,
876 snd_soc_component_write(asoc_rtd_to_codec(rtd, dac)->component,
880 dev_info(asoc_rtd_to_codec(rtd, dac)->component->dev,
881 "Setting SCLK as input clock & disabled PLL\n");
885 ret = snd_allo_piano_dsp_program(rtd, glb_ptr->set_mode, rate,
886 glb_ptr->set_lowpass);
893 static int snd_allo_piano_dac_prepare(
894 struct snd_pcm_substream *substream)
896 struct snd_soc_pcm_runtime *rtd = substream->private_data;
897 struct snd_soc_card *card = rtd->card;
899 snd_allo_piano_gpio_unmute(card);
904 /* machine stream operations */
905 static struct snd_soc_ops snd_allo_piano_dac_ops = {
906 .startup = snd_allo_piano_dac_startup,
907 .hw_params = snd_allo_piano_dac_hw_params,
908 .prepare = snd_allo_piano_dac_prepare,
911 static struct snd_soc_dai_link_component allo_piano_2_1_codecs[] = {
913 .dai_name = "pcm512x-hifi",
916 .dai_name = "pcm512x-hifi",
920 SND_SOC_DAILINK_DEFS(allo_piano_dai_plus,
921 DAILINK_COMP_ARRAY(COMP_EMPTY()),
922 DAILINK_COMP_ARRAY(COMP_CODEC("pcm512x.1-004c", "pcm512x-hifi"),
923 COMP_CODEC("pcm512x.1-004d", "pcm512x-hifi")),
924 DAILINK_COMP_ARRAY(COMP_EMPTY()));
926 static struct snd_soc_dai_link snd_allo_piano_dac_dai[] = {
928 .name = "PianoDACPlus",
929 .stream_name = "PianoDACPlus",
930 .dai_fmt = SND_SOC_DAIFMT_I2S |
931 SND_SOC_DAIFMT_NB_NF |
932 SND_SOC_DAIFMT_CBS_CFS,
933 .ops = &snd_allo_piano_dac_ops,
934 .init = snd_allo_piano_dac_init,
935 SND_SOC_DAILINK_REG(allo_piano_dai_plus),
939 /* audio machine driver */
940 static struct snd_soc_card snd_allo_piano_dac = {
941 .name = "PianoDACPlus",
942 .owner = THIS_MODULE,
943 .dai_link = snd_allo_piano_dac_dai,
944 .num_links = ARRAY_SIZE(snd_allo_piano_dac_dai),
945 .controls = allo_piano_controls,
946 .num_controls = ARRAY_SIZE(allo_piano_controls),
949 static int snd_allo_piano_dac_probe(struct platform_device *pdev)
951 struct snd_soc_card *card = &snd_allo_piano_dac;
954 card->dev = &pdev->dev;
955 platform_set_drvdata(pdev, &snd_allo_piano_dac);
957 if (pdev->dev.of_node) {
958 struct device_node *i2s_node;
959 struct snd_soc_dai_link *dai;
961 dai = &snd_allo_piano_dac_dai[0];
962 i2s_node = of_parse_phandle(pdev->dev.of_node,
963 "i2s-controller", 0);
965 for (i = 0; i < card->num_links; i++) {
966 dai->cpus->dai_name = NULL;
967 dai->cpus->of_node = i2s_node;
968 dai->platforms->name = NULL;
969 dai->platforms->of_node = i2s_node;
972 digital_gain_0db_limit =
973 !of_property_read_bool(pdev->dev.of_node,
974 "allo,24db_digital_gain");
976 glb_mclk = of_property_read_bool(pdev->dev.of_node,
979 allo_piano_2_1_codecs[0].of_node =
980 of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
981 if (!allo_piano_2_1_codecs[0].of_node) {
983 "Property 'audio-codec' missing or invalid\n");
987 allo_piano_2_1_codecs[1].of_node =
988 of_parse_phandle(pdev->dev.of_node, "audio-codec", 1);
989 if (!allo_piano_2_1_codecs[1].of_node) {
991 "Property 'audio-codec' missing or invalid\n");
995 mute_gpio[0] = devm_gpiod_get_optional(&pdev->dev, "mute1",
997 if (IS_ERR(mute_gpio[0])) {
998 ret = PTR_ERR(mute_gpio[0]);
1000 "failed to get mute1 gpio6: %d\n", ret);
1004 mute_gpio[1] = devm_gpiod_get_optional(&pdev->dev, "mute2",
1006 if (IS_ERR(mute_gpio[1])) {
1007 ret = PTR_ERR(mute_gpio[1]);
1009 "failed to get mute2 gpio25: %d\n", ret);
1013 if (mute_gpio[0] && mute_gpio[1])
1014 snd_allo_piano_dac.set_bias_level =
1015 snd_allo_piano_set_bias_level;
1017 ret = snd_soc_register_card(&snd_allo_piano_dac);
1020 "snd_soc_register_card() failed: %d\n", ret);
1024 if ((mute_gpio[0]) && (mute_gpio[1]))
1025 snd_allo_piano_gpio_mute(&snd_allo_piano_dac);
1033 static int snd_allo_piano_dac_remove(struct platform_device *pdev)
1035 struct snd_soc_card *card = platform_get_drvdata(pdev);
1037 kfree(&card->drvdata);
1038 snd_allo_piano_gpio_mute(&snd_allo_piano_dac);
1039 return snd_soc_unregister_card(&snd_allo_piano_dac);
1042 static const struct of_device_id snd_allo_piano_dac_of_match[] = {
1043 { .compatible = "allo,piano-dac-plus", },
1047 MODULE_DEVICE_TABLE(of, snd_allo_piano_dac_of_match);
1049 static struct platform_driver snd_allo_piano_dac_driver = {
1051 .name = "snd-allo-piano-dac-plus",
1052 .owner = THIS_MODULE,
1053 .of_match_table = snd_allo_piano_dac_of_match,
1055 .probe = snd_allo_piano_dac_probe,
1056 .remove = snd_allo_piano_dac_remove,
1059 module_platform_driver(snd_allo_piano_dac_driver);
1061 MODULE_AUTHOR("Baswaraj K <jaikumar@cem-solutions.net>");
1062 MODULE_DESCRIPTION("ALSA ASoC Machine Driver for Allo Piano DAC Plus");
1063 MODULE_LICENSE("GPL v2");