2417a1efd719a7cfd3cdea8af824b6efebc9cd8e
[platform/adaptation/renesas_rcar/renesas_kernel.git] / sound / pci / oxygen / xonar_dg_mixer.c
1 /*
2  * Mixer controls for the Xonar DG/DGX
3  *
4  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5  * Copyright (c) Roman Volkov <v1ron@mail.ru>
6  *
7  *  This driver is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License, version 2.
9  *
10  *  This driver is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this driver; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <linux/pci.h>
20 #include <linux/delay.h>
21 #include <sound/control.h>
22 #include <sound/core.h>
23 #include <sound/info.h>
24 #include <sound/pcm.h>
25 #include <sound/tlv.h>
26 #include "oxygen.h"
27 #include "xonar_dg.h"
28 #include "cs4245.h"
29
30 /* analog output select */
31
32 static int output_select_apply(struct oxygen *chip)
33 {
34         struct dg *data = chip->model_data;
35
36         data->cs4245_shadow[CS4245_SIGNAL_SEL] &= ~CS4245_A_OUT_SEL_MASK;
37         if (data->output_sel == PLAYBACK_DST_HP) {
38                 /* mute FP (aux output) amplifier, switch rear jack to CS4245 */
39                 oxygen_set_bits8(chip, OXYGEN_GPIO_DATA, GPIO_HP_REAR);
40         } else if (data->output_sel == PLAYBACK_DST_HP_FP) {
41                 /*
42                  * Unmute FP amplifier, switch rear jack to CS4361;
43                  * I2S channels 2,3,4 should be inactive.
44                  */
45                 oxygen_clear_bits8(chip, OXYGEN_GPIO_DATA, GPIO_HP_REAR);
46                 data->cs4245_shadow[CS4245_SIGNAL_SEL] |= CS4245_A_OUT_SEL_DAC;
47         } else {
48                 /*
49                  * 2.0, 4.0, 5.1: switch to CS4361, mute FP amp.,
50                  * and change playback routing.
51                  */
52                 oxygen_clear_bits8(chip, OXYGEN_GPIO_DATA, GPIO_HP_REAR);
53         }
54         return cs4245_write_spi(chip, CS4245_SIGNAL_SEL);
55 }
56
57 static int output_select_info(struct snd_kcontrol *ctl,
58                               struct snd_ctl_elem_info *info)
59 {
60         static const char *const names[3] = {
61                 "Stereo Headphones",
62                 "Stereo Headphones FP",
63                 "Multichannel",
64         };
65
66         return snd_ctl_enum_info(info, 1, 3, names);
67 }
68
69 static int output_select_get(struct snd_kcontrol *ctl,
70                              struct snd_ctl_elem_value *value)
71 {
72         struct oxygen *chip = ctl->private_data;
73         struct dg *data = chip->model_data;
74
75         mutex_lock(&chip->mutex);
76         value->value.enumerated.item[0] = data->output_sel;
77         mutex_unlock(&chip->mutex);
78         return 0;
79 }
80
81 static int output_select_put(struct snd_kcontrol *ctl,
82                              struct snd_ctl_elem_value *value)
83 {
84         struct oxygen *chip = ctl->private_data;
85         struct dg *data = chip->model_data;
86         unsigned int new = value->value.enumerated.item[0];
87         int changed = 0;
88         int ret;
89
90         mutex_lock(&chip->mutex);
91         if (data->output_sel != new) {
92                 data->output_sel = new;
93                 ret = output_select_apply(chip);
94                 changed = ret >= 0 ? 1 : ret;
95                 oxygen_update_dac_routing(chip);
96         }
97         mutex_unlock(&chip->mutex);
98
99         return changed;
100 }
101
102 /* CS4245 Headphone Channels A&B Volume Control */
103
104 static int hp_stereo_volume_info(struct snd_kcontrol *ctl,
105                                 struct snd_ctl_elem_info *info)
106 {
107         info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
108         info->count = 2;
109         info->value.integer.min = 0;
110         info->value.integer.max = 255;
111         return 0;
112 }
113
114 static int hp_stereo_volume_get(struct snd_kcontrol *ctl,
115                                 struct snd_ctl_elem_value *val)
116 {
117         struct oxygen *chip = ctl->private_data;
118         struct dg *data = chip->model_data;
119         unsigned int tmp;
120
121         mutex_lock(&chip->mutex);
122         tmp = (~data->cs4245_shadow[CS4245_DAC_A_CTRL]) & 255;
123         val->value.integer.value[0] = tmp;
124         tmp = (~data->cs4245_shadow[CS4245_DAC_B_CTRL]) & 255;
125         val->value.integer.value[1] = tmp;
126         mutex_unlock(&chip->mutex);
127         return 0;
128 }
129
130 static int hp_stereo_volume_put(struct snd_kcontrol *ctl,
131                                 struct snd_ctl_elem_value *val)
132 {
133         struct oxygen *chip = ctl->private_data;
134         struct dg *data = chip->model_data;
135         int ret;
136         int changed = 0;
137         long new1 = val->value.integer.value[0];
138         long new2 = val->value.integer.value[1];
139
140         if ((new1 > 255) || (new1 < 0) || (new2 > 255) || (new2 < 0))
141                 return -EINVAL;
142
143         mutex_lock(&chip->mutex);
144         if ((data->cs4245_shadow[CS4245_DAC_A_CTRL] != ~new1) ||
145             (data->cs4245_shadow[CS4245_DAC_B_CTRL] != ~new2)) {
146                 data->cs4245_shadow[CS4245_DAC_A_CTRL] = ~new1;
147                 data->cs4245_shadow[CS4245_DAC_B_CTRL] = ~new2;
148                 ret = cs4245_write_spi(chip, CS4245_DAC_A_CTRL);
149                 if (ret >= 0)
150                         ret = cs4245_write_spi(chip, CS4245_DAC_B_CTRL);
151                 changed = ret >= 0 ? 1 : ret;
152         }
153         mutex_unlock(&chip->mutex);
154
155         return changed;
156 }
157
158 /* Headphone Mute */
159
160 static int hp_mute_get(struct snd_kcontrol *ctl,
161                         struct snd_ctl_elem_value *val)
162 {
163         struct oxygen *chip = ctl->private_data;
164         struct dg *data = chip->model_data;
165
166         mutex_lock(&chip->mutex);
167         val->value.integer.value[0] =
168                 !(data->cs4245_shadow[CS4245_DAC_CTRL_1] & CS4245_MUTE_DAC);
169         mutex_unlock(&chip->mutex);
170         return 0;
171 }
172
173 static int hp_mute_put(struct snd_kcontrol *ctl,
174                         struct snd_ctl_elem_value *val)
175 {
176         struct oxygen *chip = ctl->private_data;
177         struct dg *data = chip->model_data;
178         int ret;
179         int changed;
180
181         if (val->value.integer.value[0] > 1)
182                 return -EINVAL;
183         mutex_lock(&chip->mutex);
184         data->cs4245_shadow[CS4245_DAC_CTRL_1] &= ~CS4245_MUTE_DAC;
185         data->cs4245_shadow[CS4245_DAC_CTRL_1] |=
186                 (~val->value.integer.value[0] << 2) & CS4245_MUTE_DAC;
187         ret = cs4245_write_spi(chip, CS4245_DAC_CTRL_1);
188         changed = ret >= 0 ? 1 : ret;
189         mutex_unlock(&chip->mutex);
190         return changed;
191 }
192
193 /* capture volume for all sources */
194
195 static int input_volume_apply(struct oxygen *chip, char left, char right)
196 {
197         struct dg *data = chip->model_data;
198         int ret;
199
200         data->cs4245_shadow[CS4245_PGA_A_CTRL] = left;
201         data->cs4245_shadow[CS4245_PGA_B_CTRL] = right;
202         ret = cs4245_write_spi(chip, CS4245_PGA_A_CTRL);
203         if (ret < 0)
204                 return ret;
205         return cs4245_write_spi(chip, CS4245_PGA_B_CTRL);
206 }
207
208 static int input_vol_info(struct snd_kcontrol *ctl,
209                           struct snd_ctl_elem_info *info)
210 {
211         info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
212         info->count = 2;
213         info->value.integer.min = 2 * -12;
214         info->value.integer.max = 2 * 12;
215         return 0;
216 }
217
218 static int input_vol_get(struct snd_kcontrol *ctl,
219                          struct snd_ctl_elem_value *value)
220 {
221         struct oxygen *chip = ctl->private_data;
222         struct dg *data = chip->model_data;
223         unsigned int idx = ctl->private_value;
224
225         mutex_lock(&chip->mutex);
226         value->value.integer.value[0] = data->input_vol[idx][0];
227         value->value.integer.value[1] = data->input_vol[idx][1];
228         mutex_unlock(&chip->mutex);
229         return 0;
230 }
231
232 static int input_vol_put(struct snd_kcontrol *ctl,
233                          struct snd_ctl_elem_value *value)
234 {
235         struct oxygen *chip = ctl->private_data;
236         struct dg *data = chip->model_data;
237         unsigned int idx = ctl->private_value;
238         int changed = 0;
239         int ret = 0;
240
241         if (value->value.integer.value[0] < 2 * -12 ||
242             value->value.integer.value[0] > 2 * 12 ||
243             value->value.integer.value[1] < 2 * -12 ||
244             value->value.integer.value[1] > 2 * 12)
245                 return -EINVAL;
246         mutex_lock(&chip->mutex);
247         changed = data->input_vol[idx][0] != value->value.integer.value[0] ||
248                   data->input_vol[idx][1] != value->value.integer.value[1];
249         if (changed) {
250                 data->input_vol[idx][0] = value->value.integer.value[0];
251                 data->input_vol[idx][1] = value->value.integer.value[1];
252                 if (idx == data->input_sel) {
253                         ret = input_volume_apply(chip,
254                                 data->input_vol[idx][0],
255                                 data->input_vol[idx][1]);
256                 }
257                 changed = ret >= 0 ? 1 : ret;
258         }
259         mutex_unlock(&chip->mutex);
260         return changed;
261 }
262
263 static int input_sel_info(struct snd_kcontrol *ctl,
264                           struct snd_ctl_elem_info *info)
265 {
266         static const char *const names[4] = {
267                 "Mic", "Aux", "Front Mic", "Line"
268         };
269
270         return snd_ctl_enum_info(info, 1, 4, names);
271 }
272
273 static int input_sel_get(struct snd_kcontrol *ctl,
274                          struct snd_ctl_elem_value *value)
275 {
276         struct oxygen *chip = ctl->private_data;
277         struct dg *data = chip->model_data;
278
279         mutex_lock(&chip->mutex);
280         value->value.enumerated.item[0] = data->input_sel;
281         mutex_unlock(&chip->mutex);
282         return 0;
283 }
284
285 static int input_sel_put(struct snd_kcontrol *ctl,
286                          struct snd_ctl_elem_value *value)
287 {
288         static const u8 sel_values[4] = {
289                 CS4245_SEL_MIC,
290                 CS4245_SEL_INPUT_1,
291                 CS4245_SEL_INPUT_2,
292                 CS4245_SEL_INPUT_4
293         };
294         struct oxygen *chip = ctl->private_data;
295         struct dg *data = chip->model_data;
296         int changed;
297
298         if (value->value.enumerated.item[0] > 3)
299                 return -EINVAL;
300
301         mutex_lock(&chip->mutex);
302         changed = value->value.enumerated.item[0] != data->input_sel;
303         if (changed) {
304                 data->input_sel = value->value.enumerated.item[0];
305
306                 cs4245_write(chip, CS4245_ANALOG_IN,
307                              (data->cs4245_shadow[CS4245_ANALOG_IN] &
308                                                         ~CS4245_SEL_MASK) |
309                              sel_values[data->input_sel]);
310
311                 cs4245_write_cached(chip, CS4245_PGA_A_CTRL,
312                                     data->input_vol[data->input_sel][0]);
313                 cs4245_write_cached(chip, CS4245_PGA_B_CTRL,
314                                     data->input_vol[data->input_sel][1]);
315
316                 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
317                                       data->input_sel ? 0 : GPIO_INPUT_ROUTE,
318                                       GPIO_INPUT_ROUTE);
319         }
320         mutex_unlock(&chip->mutex);
321         return changed;
322 }
323
324 static int hpf_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
325 {
326         static const char *const names[2] = { "Active", "Frozen" };
327
328         return snd_ctl_enum_info(info, 1, 2, names);
329 }
330
331 static int hpf_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
332 {
333         struct oxygen *chip = ctl->private_data;
334         struct dg *data = chip->model_data;
335
336         value->value.enumerated.item[0] =
337                 !!(data->cs4245_shadow[CS4245_ADC_CTRL] & CS4245_HPF_FREEZE);
338         return 0;
339 }
340
341 static int hpf_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
342 {
343         struct oxygen *chip = ctl->private_data;
344         struct dg *data = chip->model_data;
345         u8 reg;
346         int changed;
347
348         mutex_lock(&chip->mutex);
349         reg = data->cs4245_shadow[CS4245_ADC_CTRL] & ~CS4245_HPF_FREEZE;
350         if (value->value.enumerated.item[0])
351                 reg |= CS4245_HPF_FREEZE;
352         changed = reg != data->cs4245_shadow[CS4245_ADC_CTRL];
353         if (changed)
354                 cs4245_write(chip, CS4245_ADC_CTRL, reg);
355         mutex_unlock(&chip->mutex);
356         return changed;
357 }
358
359 #define INPUT_VOLUME(xname, index) { \
360         .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
361         .name = xname, \
362         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
363                   SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
364         .info = input_vol_info, \
365         .get = input_vol_get, \
366         .put = input_vol_put, \
367         .tlv = { .p = pga_db_scale }, \
368         .private_value = index, \
369 }
370 static const DECLARE_TLV_DB_MINMAX(hp_db_scale, -12550, 0);
371 static const DECLARE_TLV_DB_MINMAX(pga_db_scale, -1200, 1200);
372 static const struct snd_kcontrol_new dg_controls[] = {
373         {
374                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
375                 .name = "Analog Output Playback Enum",
376                 .info = output_select_info,
377                 .get = output_select_get,
378                 .put = output_select_put,
379         },
380         {
381                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
382                 .name = "Headphone Playback Volume",
383                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
384                           SNDRV_CTL_ELEM_ACCESS_TLV_READ,
385                 .info = hp_stereo_volume_info,
386                 .get = hp_stereo_volume_get,
387                 .put = hp_stereo_volume_put,
388                 .tlv = { .p = hp_db_scale, },
389         },
390         {
391                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
392                 .name = "Headphone Playback Switch",
393                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
394                 .info = snd_ctl_boolean_mono_info,
395                 .get = hp_mute_get,
396                 .put = hp_mute_put,
397         },
398         INPUT_VOLUME("Mic Capture Volume", 0),
399         INPUT_VOLUME("Aux Capture Volume", 1),
400         INPUT_VOLUME("Front Mic Capture Volume", 2),
401         INPUT_VOLUME("Line Capture Volume", 3),
402         {
403                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
404                 .name = "Capture Source",
405                 .info = input_sel_info,
406                 .get = input_sel_get,
407                 .put = input_sel_put,
408         },
409         {
410                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
411                 .name = "ADC High-pass Filter Capture Enum",
412                 .info = hpf_info,
413                 .get = hpf_get,
414                 .put = hpf_put,
415         },
416 };
417
418 static int dg_control_filter(struct snd_kcontrol_new *template)
419 {
420         if (!strncmp(template->name, "Master Playback ", 16))
421                 return 1;
422         return 0;
423 }
424
425 static int dg_mixer_init(struct oxygen *chip)
426 {
427         unsigned int i;
428         int err;
429
430         for (i = 0; i < ARRAY_SIZE(dg_controls); ++i) {
431                 err = snd_ctl_add(chip->card,
432                                   snd_ctl_new1(&dg_controls[i], chip));
433                 if (err < 0)
434                         return err;
435         }
436         return 0;
437 }
438
439 struct oxygen_model model_xonar_dg = {
440         .longname = "C-Media Oxygen HD Audio",
441         .chip = "CMI8786",
442         .init = dg_init,
443         .control_filter = dg_control_filter,
444         .mixer_init = dg_mixer_init,
445         .cleanup = dg_cleanup,
446         .suspend = dg_suspend,
447         .resume = dg_resume,
448         .set_dac_params = set_cs4245_dac_params,
449         .set_adc_params = set_cs4245_adc_params,
450         .adjust_dac_routing = adjust_dg_dac_routing,
451         .dump_registers = dump_cs4245_registers,
452         .model_data_size = sizeof(struct dg),
453         .device_config = PLAYBACK_0_TO_I2S |
454                          PLAYBACK_1_TO_SPDIF |
455                          CAPTURE_0_FROM_I2S_1 |
456                          CAPTURE_1_FROM_SPDIF,
457         .dac_channels_pcm = 6,
458         .dac_channels_mixer = 0,
459         .function_flags = OXYGEN_FUNCTION_SPI,
460         .dac_mclks = OXYGEN_MCLKS(256, 128, 128),
461         .adc_mclks = OXYGEN_MCLKS(256, 128, 128),
462         .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
463         .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
464 };