Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / sound / soc / bcm / allo-piano-dac-plus.c
1 /*
2  * ALSA ASoC Machine Driver for Allo Piano DAC Plus Subwoofer
3  *
4  * Author:      Baswaraj K <jaikumar@cem-solutions.net>
5  *              Copyright 2020
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>
9  *
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.
13  *
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.
18  */
19
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"
31
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
36 #define P_MUTE                  1
37 #define P_UNMUTE                0
38
39 struct dsp_code {
40         char i2c_addr;
41         char offset;
42         char val;
43 };
44
45 struct glb_pool {
46         struct mutex lock;
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;
52 };
53
54 static bool digital_gain_0db_limit = true;
55 bool glb_mclk;
56
57 static struct gpio_desc *mute_gpio[2];
58
59 static const char * const allo_piano_mode_texts[] = {
60         "None",
61         "2.0",
62         "2.1",
63         "2.2",
64 };
65
66 static SOC_ENUM_SINGLE_DECL(allo_piano_mode_enum,
67                 0, 0, allo_piano_mode_texts);
68
69 static const char * const allo_piano_dual_mode_texts[] = {
70         "None",
71         "Dual-Mono",
72         "Dual-Stereo",
73 };
74
75 static SOC_ENUM_SINGLE_DECL(allo_piano_dual_mode_enum,
76                 0, 0, allo_piano_dual_mode_texts);
77
78 static const char * const allo_piano_dsp_low_pass_texts[] = {
79         "60",
80         "70",
81         "80",
82         "90",
83         "100",
84         "110",
85         "120",
86         "130",
87         "140",
88         "150",
89         "160",
90         "170",
91         "180",
92         "190",
93         "200",
94 };
95
96 static SOC_ENUM_SINGLE_DECL(allo_piano_enum,
97                 0, 0, allo_piano_dsp_low_pass_texts);
98
99 static int __snd_allo_piano_dsp_program(struct snd_soc_pcm_runtime *rtd,
100                 unsigned int mode, unsigned int rate, unsigned int lowpass)
101 {
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;
107
108         if (rate <= 46000)
109                 rate = 44100;
110         else if (rate <= 68000)
111                 rate = 48000;
112         else if (rate <= 92000)
113                 rate = 88200;
114         else if (rate <= 136000)
115                 rate = 96000;
116         else if (rate <= 184000)
117                 rate = 176400;
118         else
119                 rate = 192000;
120
121         if (lowpass > 14)
122                 glb_ptr->set_lowpass = lowpass = 0;
123
124         if (mode > 3)
125                 glb_ptr->set_mode = mode = 0;
126
127         if (mode > 0)
128                 glb_ptr->dual_mode = 0;
129
130         /* same configuration loaded */
131         if ((rate == glb_ptr->set_rate) && (lowpass == glb_ptr->set_lowpass)
132                         && (mode == glb_ptr->set_mode))
133                 return 0;
134
135         switch (mode) {
136         case 0: /* None */
137                 return 1;
138
139         case 1: /* 2.0 */
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;
147                 return 1;
148
149         default:
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);
154         }
155
156         for (dac = 0; dac < rtd->num_codecs; dac++) {
157                 struct dsp_code *dsp_code_read;
158                 int i = 1;
159
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);
164                 } else { /* low */
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);
168                 }
169
170                 dev_info(rtd->card->dev, "Dsp Firmware File Name: %s\n",
171                                 firmware_name);
172
173                 ret = request_firmware(&fw, firmware_name, rtd->card->dev);
174                 if (ret < 0) {
175                         dev_err(rtd->card->dev,
176                                 "Error: Allo Piano Firmware %s missing. %d\n",
177                                 firmware_name, ret);
178                         goto err;
179                 }
180
181                 while (i < (fw->size - 1)) {
182                         dsp_code_read = (struct dsp_code *)&fw->data[i];
183
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),
188                                                 dsp_code_read->val);
189
190                         } else if (dsp_code_read->offset != 0) {
191                                 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, dac)->component,
192                                         (PCM512x_PAGE_BASE(
193                                                 glb_ptr->dsp_page_number) +
194                                         dsp_code_read->offset),
195                                         dsp_code_read->val);
196                         }
197                         if (ret < 0) {
198                                 dev_err(rtd->card->dev,
199                                         "Failed to write Register: %d\n", ret);
200                                 release_firmware(fw);
201                                 goto err;
202                         }
203                         i = i + 3;
204                 }
205                 release_firmware(fw);
206         }
207         glb_ptr->set_rate = rate;
208         glb_ptr->set_mode = mode;
209         glb_ptr->set_lowpass = lowpass;
210         return 1;
211
212 err:
213         return ret;
214 }
215
216 static int snd_allo_piano_dsp_program(struct snd_soc_pcm_runtime *rtd,
217                 unsigned int mode, unsigned int rate, unsigned int lowpass)
218 {
219         struct snd_soc_card *card = rtd->card;
220         struct glb_pool *glb_ptr = card->drvdata;
221         int ret = 0;
222
223         mutex_lock(&glb_ptr->lock);
224
225         ret = __snd_allo_piano_dsp_program(rtd, mode, rate, lowpass);
226
227         mutex_unlock(&glb_ptr->lock);
228
229         return ret;
230 }
231
232 static int snd_allo_piano_dual_mode_get(struct snd_kcontrol *kcontrol,
233                 struct snd_ctl_elem_value *ucontrol)
234 {
235         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
236         struct glb_pool *glb_ptr = card->drvdata;
237
238         ucontrol->value.integer.value[0] = glb_ptr->dual_mode;
239
240         return 0;
241 }
242
243 static int snd_allo_piano_dual_mode_put(struct snd_kcontrol *kcontrol,
244                 struct snd_ctl_elem_value *ucontrol)
245 {
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;
253
254         rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
255
256         if (ucontrol->value.integer.value[0] > 0) {
257                 glb_ptr->dual_mode = ucontrol->value.integer.value[0];
258                 glb_ptr->set_mode = 0;
259         } else {
260                 if (glb_ptr->set_mode <= 0) {
261                         glb_ptr->dual_mode = 1;
262                         glb_ptr->set_mode = 0;
263                 } else {
264                         glb_ptr->dual_mode = 0;
265                         return 0;
266                 }
267         }
268
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);
278
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 *)
283                                         kctl->private_value;
284                                 mc->rreg = mc->reg;
285                                 break;
286                         }
287                         if (!strncmp(kctl->id.name, "Sub Digital Playback Volume",
288                                 sizeof(kctl->id.name))) {
289                                 mc = (struct soc_mixer_control *)
290                                         kctl->private_value;
291                                 mc->rreg = mc->reg;
292                                 break;
293                         }
294                 }
295         } else {
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);
300
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 *)
305                                         kctl->private_value;
306                                 mc->rreg = PCM512x_DIGITAL_VOLUME_3;
307                                 break;
308                         }
309                         if (!strncmp(kctl->id.name, "Sub Digital Playback Volume",
310                                 sizeof(kctl->id.name))) {
311                                 mc = (struct soc_mixer_control *)
312                                         kctl->private_value;
313                                 mc->rreg = PCM512x_DIGITAL_VOLUME_2;
314                                 break;
315                         }
316                 }
317
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);
326         }
327
328         return 0;
329 }
330
331 static int snd_allo_piano_mode_get(struct snd_kcontrol *kcontrol,
332                 struct snd_ctl_elem_value *ucontrol)
333 {
334         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
335         struct glb_pool *glb_ptr = card->drvdata;
336
337         ucontrol->value.integer.value[0] = glb_ptr->set_mode;
338         return 0;
339 }
340
341 static int snd_allo_piano_mode_put(struct snd_kcontrol *kcontrol,
342                 struct snd_ctl_elem_value *ucontrol)
343 {
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;
351
352         rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
353
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);
360
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 *)
365                                         kctl->private_value;
366                                 mc->rreg = PCM512x_DIGITAL_VOLUME_3;
367                                 break;
368                         }
369                         if (!strncmp(kctl->id.name, "Sub Digital Playback Volume",
370                                 sizeof(kctl->id.name))) {
371                                 mc = (struct soc_mixer_control *)
372                                         kctl->private_value;
373                                 mc->rreg = PCM512x_DIGITAL_VOLUME_2;
374                                 break;
375                         }
376                 }
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);
381         }
382
383         return(snd_allo_piano_dsp_program(rtd,
384                                 ucontrol->value.integer.value[0],
385                                 glb_ptr->set_rate, glb_ptr->set_lowpass));
386 }
387
388 static int snd_allo_piano_lowpass_get(struct snd_kcontrol *kcontrol,
389                 struct snd_ctl_elem_value *ucontrol)
390 {
391         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
392         struct glb_pool *glb_ptr = card->drvdata;
393
394         ucontrol->value.integer.value[0] = glb_ptr->set_lowpass;
395         return 0;
396 }
397
398 static int snd_allo_piano_lowpass_put(struct snd_kcontrol *kcontrol,
399                 struct snd_ctl_elem_value *ucontrol)
400 {
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;
404
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]));
409 }
410
411 static int pcm512x_get_reg_sub(struct snd_kcontrol *kcontrol,
412                 struct snd_ctl_elem_value *ucontrol)
413 {
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]);
422
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);
428
429         } else {
430                 left_val = right_val;
431         }
432
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;
437
438         return 0;
439 }
440
441 static int pcm512x_set_reg_sub(struct snd_kcontrol *kcontrol,
442                 struct snd_ctl_elem_value *ucontrol)
443 {
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);
451         int ret = 0;
452
453         rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
454
455         if (digital_gain_0db_limit) {
456                 ret = snd_soc_limit_volume(card, "Subwoofer Playback Volume",
457                                         207);
458                 if (ret < 0)
459                         dev_warn(card->dev, "Failed to set volume limit: %d\n",
460                                 ret);
461         }
462
463         // When in Dual Mono, Sub vol control should not set anything.
464         if (glb_ptr->dual_mode != 1) { //Not in Dual Mono mode
465
466                 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component,
467                                 PCM512x_DIGITAL_VOLUME_2, (~left_val));
468                 if (ret < 0)
469                         return ret;
470
471                 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component,
472                                 PCM512x_DIGITAL_VOLUME_3, (~right_val));
473                 if (ret < 0)
474                         return ret;
475
476         }
477
478         return 1;
479 }
480
481 static int pcm512x_get_reg_sub_switch(struct snd_kcontrol *kcontrol,
482                 struct snd_ctl_elem_value *ucontrol)
483 {
484         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
485         struct snd_soc_pcm_runtime *rtd;
486         int val = 0;
487
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);
490
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;
495
496         return val;
497 }
498
499 static int pcm512x_set_reg_sub_switch(struct snd_kcontrol *kcontrol,
500                 struct snd_ctl_elem_value *ucontrol)
501 {
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]);
507         int ret = 0;
508
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)));
513                 if (ret < 0)
514                         return ret;
515         }
516         return 1;
517
518 }
519
520 static int pcm512x_get_reg_master(struct snd_kcontrol *kcontrol,
521                 struct snd_ctl_elem_value *ucontrol)
522 {
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;
529
530         rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
531
532         left_val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 0)->component,
533                         PCM512x_DIGITAL_VOLUME_2);
534
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);
538         } else {
539                 right_val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 0)->component,
540                                 PCM512x_DIGITAL_VOLUME_3);
541         }
542
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;
547
548         return 0;
549 }
550
551 static int pcm512x_set_reg_master(struct snd_kcontrol *kcontrol,
552                 struct snd_ctl_elem_value *ucontrol)
553 {
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);
561         int ret = 0;
562
563         rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
564
565         if (digital_gain_0db_limit) {
566                 ret = snd_soc_limit_volume(card, "Master Playback Volume",
567                                         207);
568                 if (ret < 0)
569                         dev_warn(card->dev, "Failed to set volume limit: %d\n",
570                                 ret);
571         }
572
573         if (glb_ptr->dual_mode == 1) { //in Dual Mono Mode
574
575                 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component,
576                                 PCM512x_DIGITAL_VOLUME_2, (~left_val));
577                 if (ret < 0)
578                         return ret;
579
580                 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component,
581                                 PCM512x_DIGITAL_VOLUME_3, (~right_val));
582                 if (ret < 0)
583                         return ret;
584
585         } else {
586
587                 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component,
588                                 PCM512x_DIGITAL_VOLUME_2, (~left_val));
589                 if (ret < 0)
590                         return ret;
591
592                 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component,
593                                 PCM512x_DIGITAL_VOLUME_3, (~right_val));
594                 if (ret < 0)
595                         return ret;
596
597         }
598         return 1;
599 }
600
601 static int pcm512x_get_reg_master_switch(struct snd_kcontrol *kcontrol,
602                 struct snd_ctl_elem_value *ucontrol)
603 {
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;
607         int val = 0;
608
609         rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
610
611         val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 0)->component, PCM512x_MUTE);
612
613         ucontrol->value.integer.value[0] =
614                         (val & P_DAC_LEFT_MUTE) ? P_UNMUTE : P_MUTE;
615
616         if (glb_ptr->dual_mode == 1) {
617                 val = snd_soc_component_read(asoc_rtd_to_codec(rtd, 1)->component, PCM512x_MUTE);
618         }
619         ucontrol->value.integer.value[1] =
620                         (val & P_DAC_RIGHT_MUTE) ? P_UNMUTE : P_MUTE;
621
622         return val;
623 }
624
625 static int pcm512x_set_reg_master_switch(struct snd_kcontrol *kcontrol,
626                 struct snd_ctl_elem_value *ucontrol)
627 {
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]);
633         int ret = 0;
634
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));
639                 if (ret < 0)
640                         return ret;
641                 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component, PCM512x_MUTE,
642                                 ~((right_val & 0x01)));
643                 if (ret < 0)
644                         return ret;
645
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)));
649                 if (ret < 0)
650                         return ret;
651
652         } else {
653                 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 0)->component, PCM512x_MUTE,
654                                 ~((left_val & 0x01)<<4 | (right_val & 0x01)));
655                 if (ret < 0)
656                         return ret;
657
658                 ret = snd_soc_component_write(asoc_rtd_to_codec(rtd, 1)->component, PCM512x_MUTE,
659                                 ~((left_val & 0x01)<<4 | (right_val & 0x01)));
660                 if (ret < 0)
661                         return ret;
662         }
663         return 1;
664 }
665
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);
668
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),
674
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),
679
680         SOC_ENUM_EXT("Lowpass Route", allo_piano_enum,
681                         snd_allo_piano_lowpass_get,
682                         snd_allo_piano_lowpass_put),
683
684         SOC_DOUBLE_R_EXT_TLV("Subwoofer Playback Volume",
685                         PCM512x_DIGITAL_VOLUME_2,
686                         PCM512x_DIGITAL_VOLUME_3, 0, 255, 1,
687                         pcm512x_get_reg_sub,
688                         pcm512x_set_reg_sub,
689                         digital_tlv_sub),
690
691         SOC_DOUBLE_EXT("Subwoofer Playback Switch",
692                         PCM512x_MUTE,
693                         PCM512x_RQML_SHIFT,
694                         PCM512x_RQMR_SHIFT, 1, 1,
695                         pcm512x_get_reg_sub_switch,
696                         pcm512x_set_reg_sub_switch),
697
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,
703                         digital_tlv_master),
704
705         SOC_DOUBLE_EXT("Master Playback Switch",
706                         PCM512x_MUTE,
707                         PCM512x_RQML_SHIFT,
708                         PCM512x_RQMR_SHIFT, 1, 1,
709                         pcm512x_get_reg_master_switch,
710                         pcm512x_set_reg_master_switch),
711 };
712
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",
718         "Auto Mute Switch",
719         "Auto Mute Time Left",
720         "Auto Mute Time Right",
721         "Clock Missing Period",
722         "Max Overclock DAC",
723         "Max Overclock DSP",
724         "Max Overclock PLL",
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"
731 };
732
733 static int snd_allo_piano_dac_init(struct snd_soc_pcm_runtime *rtd)
734 {
735         struct snd_soc_card *card = rtd->card;
736         struct glb_pool *glb_ptr;
737         struct snd_kcontrol *kctl;
738         int i, j;
739
740         glb_ptr = kzalloc(sizeof(struct glb_pool), GFP_KERNEL);
741         if (!glb_ptr)
742                 return -ENOMEM;
743
744         card->drvdata = glb_ptr;
745         glb_ptr->dual_mode = 2;
746         glb_ptr->set_mode = 0;
747
748         mutex_init(&glb_ptr->lock);
749
750         if (digital_gain_0db_limit) {
751                 int ret;
752
753                 //Set volume limit on both dacs
754                 for (i = 0; i < ARRAY_SIZE(codec_ctl_pfx); i++) {
755                         char cname[256];
756
757                         sprintf(cname, "%s %s", codec_ctl_pfx[i], codec_ctl_name[0]);
758                         ret = snd_soc_limit_volume(card, cname, 207);
759                         if (ret < 0)
760                                 dev_warn(card->dev, "Failed to set volume limit: %d\n",
761                                         ret);
762                 }
763         }
764
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++) {
768                         char cname[256];
769
770                         sprintf(cname, "%s %s", codec_ctl_pfx[i], codec_ctl_name[j]);
771                         kctl = snd_soc_card_get_kcontrol(card, cname);
772                         if (!kctl) {
773                                 dev_err(rtd->card->dev, "Control %s not found\n",
774                                        cname);
775                         } else {
776                                 kctl->vd[0].access =
777                                         SNDRV_CTL_ELEM_ACCESS_READWRITE;
778                                 snd_ctl_remove(card->snd_card, kctl);
779                         }
780                 }
781         }
782
783         return 0;
784 }
785
786 static void snd_allo_piano_gpio_mute(struct snd_soc_card *card)
787 {
788         if (mute_gpio[0])
789                 gpiod_set_value_cansleep(mute_gpio[0], P_MUTE);
790
791         if (mute_gpio[1])
792                 gpiod_set_value_cansleep(mute_gpio[1], P_MUTE);
793 }
794
795 static void snd_allo_piano_gpio_unmute(struct snd_soc_card *card)
796 {
797         if (mute_gpio[0])
798                 gpiod_set_value_cansleep(mute_gpio[0], P_UNMUTE);
799
800         if (mute_gpio[1])
801                 gpiod_set_value_cansleep(mute_gpio[1], P_UNMUTE);
802 }
803
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)
806 {
807         struct snd_soc_pcm_runtime *rtd;
808         struct snd_soc_dai *codec_dai;
809
810         rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
811         codec_dai = asoc_rtd_to_codec(rtd, 0);
812
813         if (dapm->dev != codec_dai->dev)
814                 return 0;
815
816         switch (level) {
817         case SND_SOC_BIAS_PREPARE:
818                 if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
819                         break;
820                 /* UNMUTE DAC */
821                 snd_allo_piano_gpio_unmute(card);
822                 break;
823
824         case SND_SOC_BIAS_STANDBY:
825                 if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
826                         break;
827                 /* MUTE DAC */
828                 snd_allo_piano_gpio_mute(card);
829                 break;
830
831         default:
832                 break;
833         }
834
835         return 0;
836 }
837
838 static int snd_allo_piano_dac_startup(
839         struct snd_pcm_substream *substream)
840 {
841         struct snd_soc_pcm_runtime *rtd = substream->private_data;
842         struct snd_soc_card *card = rtd->card;
843
844         snd_allo_piano_gpio_mute(card);
845
846         return 0;
847 }
848
849 static int snd_allo_piano_dac_hw_params(
850                 struct snd_pcm_substream *substream,
851                 struct snd_pcm_hw_params *params)
852 {
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;
858
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,
862                                         PCM512x_RATE_DET_4);
863
864                 if (val & 0x40) {
865                         snd_soc_component_write(asoc_rtd_to_codec(rtd, dac)->component,
866                                         PCM512x_PLL_REF,
867                                         PCM512x_SREF_BCK);
868
869                         dev_info(asoc_rtd_to_codec(rtd, dac)->component->dev,
870                                 "Setting BCLK as input clock & Enable PLL\n");
871                 } else {
872                         snd_soc_component_write(asoc_rtd_to_codec(rtd, dac)->component,
873                                         PCM512x_PLL_EN,
874                                         0x00);
875
876                         snd_soc_component_write(asoc_rtd_to_codec(rtd, dac)->component,
877                                         PCM512x_PLL_REF,
878                                         PCM512x_SREF_SCK);
879
880                         dev_info(asoc_rtd_to_codec(rtd, dac)->component->dev,
881                                 "Setting SCLK as input clock & disabled PLL\n");
882                 }
883         }
884
885         ret = snd_allo_piano_dsp_program(rtd, glb_ptr->set_mode, rate,
886                                                 glb_ptr->set_lowpass);
887         if (ret < 0)
888                 return ret;
889
890         return ret;
891 }
892
893 static int snd_allo_piano_dac_prepare(
894         struct snd_pcm_substream *substream)
895 {
896         struct snd_soc_pcm_runtime *rtd = substream->private_data;
897         struct snd_soc_card *card = rtd->card;
898
899         snd_allo_piano_gpio_unmute(card);
900
901         return 0;
902 }
903
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,
909 };
910
911 static struct snd_soc_dai_link_component allo_piano_2_1_codecs[] = {
912         {
913                 .dai_name = "pcm512x-hifi",
914         },
915         {
916                 .dai_name = "pcm512x-hifi",
917         },
918 };
919
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()));
925
926 static struct snd_soc_dai_link snd_allo_piano_dac_dai[] = {
927         {
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),
936         },
937 };
938
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),
947 };
948
949 static int snd_allo_piano_dac_probe(struct platform_device *pdev)
950 {
951         struct snd_soc_card *card = &snd_allo_piano_dac;
952         int ret = 0, i = 0;
953
954         card->dev = &pdev->dev;
955         platform_set_drvdata(pdev, &snd_allo_piano_dac);
956
957         if (pdev->dev.of_node) {
958                 struct device_node *i2s_node;
959                 struct snd_soc_dai_link *dai;
960
961                 dai = &snd_allo_piano_dac_dai[0];
962                 i2s_node = of_parse_phandle(pdev->dev.of_node,
963                                                 "i2s-controller", 0);
964                 if (i2s_node) {
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;
970                         }
971                 }
972                 digital_gain_0db_limit =
973                         !of_property_read_bool(pdev->dev.of_node,
974                                                 "allo,24db_digital_gain");
975
976                 glb_mclk = of_property_read_bool(pdev->dev.of_node,
977                                                 "allo,glb_mclk");
978
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) {
982                         dev_err(&pdev->dev,
983                                 "Property 'audio-codec' missing or invalid\n");
984                         return -EINVAL;
985                 }
986
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) {
990                         dev_err(&pdev->dev,
991                                 "Property 'audio-codec' missing or invalid\n");
992                         return -EINVAL;
993                 }
994
995                 mute_gpio[0] = devm_gpiod_get_optional(&pdev->dev, "mute1",
996                                                         GPIOD_OUT_LOW);
997                 if (IS_ERR(mute_gpio[0])) {
998                         ret = PTR_ERR(mute_gpio[0]);
999                         dev_err(&pdev->dev,
1000                                 "failed to get mute1 gpio6: %d\n", ret);
1001                         return ret;
1002                 }
1003
1004                 mute_gpio[1] = devm_gpiod_get_optional(&pdev->dev, "mute2",
1005                                                         GPIOD_OUT_LOW);
1006                 if (IS_ERR(mute_gpio[1])) {
1007                         ret = PTR_ERR(mute_gpio[1]);
1008                         dev_err(&pdev->dev,
1009                                 "failed to get mute2 gpio25: %d\n", ret);
1010                         return ret;
1011                 }
1012
1013                 if (mute_gpio[0] && mute_gpio[1])
1014                         snd_allo_piano_dac.set_bias_level =
1015                                 snd_allo_piano_set_bias_level;
1016
1017                 ret = snd_soc_register_card(&snd_allo_piano_dac);
1018                 if (ret < 0) {
1019                         dev_err(&pdev->dev,
1020                                 "snd_soc_register_card() failed: %d\n", ret);
1021                         return ret;
1022                 }
1023
1024                 if ((mute_gpio[0]) && (mute_gpio[1]))
1025                         snd_allo_piano_gpio_mute(&snd_allo_piano_dac);
1026
1027                 return 0;
1028         }
1029
1030         return -EINVAL;
1031 }
1032
1033 static int snd_allo_piano_dac_remove(struct platform_device *pdev)
1034 {
1035         struct snd_soc_card *card = platform_get_drvdata(pdev);
1036
1037         kfree(&card->drvdata);
1038         snd_allo_piano_gpio_mute(&snd_allo_piano_dac);
1039         return snd_soc_unregister_card(&snd_allo_piano_dac);
1040 }
1041
1042 static const struct of_device_id snd_allo_piano_dac_of_match[] = {
1043         { .compatible = "allo,piano-dac-plus", },
1044         { /* sentinel */ },
1045 };
1046
1047 MODULE_DEVICE_TABLE(of, snd_allo_piano_dac_of_match);
1048
1049 static struct platform_driver snd_allo_piano_dac_driver = {
1050         .driver = {
1051                 .name = "snd-allo-piano-dac-plus",
1052                 .owner = THIS_MODULE,
1053                 .of_match_table = snd_allo_piano_dac_of_match,
1054         },
1055         .probe = snd_allo_piano_dac_probe,
1056         .remove = snd_allo_piano_dac_remove,
1057 };
1058
1059 module_platform_driver(snd_allo_piano_dac_driver);
1060
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");