[ALSA] virtuoso: move PCM1796 symbols to a header file
[firefly-linux-kernel-4.4.55.git] / sound / pci / oxygen / virtuoso.c
1 /*
2  * C-Media CMI8788 driver for Asus Xonar cards
3  *
4  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5  *
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, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  */
19
20 /*
21  * CMI8788:
22  *
23  * SPI 0 -> 1st PCM1796 (front)
24  * SPI 1 -> 2nd PCM1796 (surround)
25  * SPI 2 -> 3rd PCM1796 (center/LFE)
26  * SPI 4 -> 4th PCM1796 (back)
27  *
28  * GPIO 2 -> M0 of CS5381
29  * GPIO 3 -> M1 of CS5381
30  * GPIO 5 <- external power present (D2X only)
31  * GPIO 7 -> ALT
32  * GPIO 8 -> enable output to speakers
33  *
34  * CM9780:
35  *
36  * GPIO 0 -> enable AC'97 bypass (line in -> ADC)
37  */
38
39 #include <linux/pci.h>
40 #include <linux/delay.h>
41 #include <linux/mutex.h>
42 #include <sound/ac97_codec.h>
43 #include <sound/control.h>
44 #include <sound/core.h>
45 #include <sound/initval.h>
46 #include <sound/pcm.h>
47 #include <sound/tlv.h>
48 #include "oxygen.h"
49 #include "cm9780.h"
50 #include "pcm1796.h"
51
52 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
53 MODULE_DESCRIPTION("Asus AV200 driver");
54 MODULE_LICENSE("GPL");
55 MODULE_SUPPORTED_DEVICE("{{Asus,AV200}}");
56
57 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
58 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
59 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
60
61 module_param_array(index, int, NULL, 0444);
62 MODULE_PARM_DESC(index, "card index");
63 module_param_array(id, charp, NULL, 0444);
64 MODULE_PARM_DESC(id, "ID string");
65 module_param_array(enable, bool, NULL, 0444);
66 MODULE_PARM_DESC(enable, "enable card");
67
68 static struct pci_device_id xonar_ids[] __devinitdata = {
69         { OXYGEN_PCI_SUBID(0x1043, 0x8269) }, /* Asus Xonar D2 */
70         { OXYGEN_PCI_SUBID(0x1043, 0x82b7) }, /* Asus Xonar D2X */
71         { }
72 };
73 MODULE_DEVICE_TABLE(pci, xonar_ids);
74
75
76 #define GPIO_CS5381_M_MASK      0x000c
77 #define GPIO_CS5381_M_SINGLE    0x0000
78 #define GPIO_CS5381_M_DOUBLE    0x0004
79 #define GPIO_CS5381_M_QUAD      0x0008
80 #define GPIO_EXT_POWER          0x0020
81 #define GPIO_ALT                0x0080
82 #define GPIO_OUTPUT_ENABLE      0x0100
83
84 #define GPIO_LINE_MUTE          CM9780_GPO0
85
86 struct xonar_data {
87         u8 is_d2x;
88         u8 has_power;
89 };
90
91 static void pcm1796_write(struct oxygen *chip, unsigned int codec,
92                           u8 reg, u8 value)
93 {
94         /* maps ALSA channel pair number to SPI output */
95         static const u8 codec_map[4] = {
96                 0, 1, 2, 4
97         };
98         oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER  |
99                          OXYGEN_SPI_DATA_LENGTH_2 |
100                          OXYGEN_SPI_CLOCK_160 |
101                          (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
102                          OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
103                          (reg << 8) | value);
104 }
105
106 static void xonar_init(struct oxygen *chip)
107 {
108         struct xonar_data *data = chip->model_data;
109         unsigned int i;
110
111         data->is_d2x = chip->pci->subsystem_device == 0x82b7;
112
113         for (i = 0; i < 4; ++i) {
114                 pcm1796_write(chip, i, 18, PCM1796_FMT_24_LJUST | PCM1796_ATLD);
115                 pcm1796_write(chip, i, 19, PCM1796_FLT_SHARP | PCM1796_ATS_1);
116                 pcm1796_write(chip, i, 20, PCM1796_OS_64);
117                 pcm1796_write(chip, i, 21, 0);
118                 pcm1796_write(chip, i, 16, 0xff); /* set ATL/ATR after ATLD */
119                 pcm1796_write(chip, i, 17, 0xff);
120         }
121
122         oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
123                           GPIO_CS5381_M_MASK | GPIO_ALT);
124         oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
125                               GPIO_CS5381_M_SINGLE,
126                               GPIO_CS5381_M_MASK | GPIO_ALT);
127         if (data->is_d2x) {
128                 oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL,
129                                     GPIO_EXT_POWER);
130                 oxygen_set_bits16(chip, OXYGEN_GPIO_INTERRUPT_MASK,
131                                   GPIO_EXT_POWER);
132                 chip->interrupt_mask |= OXYGEN_INT_GPIO;
133                 data->has_power = !!(oxygen_read16(chip, OXYGEN_GPIO_DATA)
134                                      & GPIO_EXT_POWER);
135         }
136         oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC);
137         oxygen_ac97_clear_bits(chip, 0, CM9780_GPIO_STATUS, GPIO_LINE_MUTE);
138         msleep(300);
139         oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_OUTPUT_ENABLE);
140         oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
141
142         snd_component_add(chip->card, "PCM1796");
143         snd_component_add(chip->card, "CS5381");
144 }
145
146 static void xonar_cleanup(struct oxygen *chip)
147 {
148         oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
149 }
150
151 static void set_pcm1796_params(struct oxygen *chip,
152                                struct snd_pcm_hw_params *params)
153 {
154 #if 0
155         unsigned int i;
156         u8 value;
157
158         value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
159         for (i = 0; i < 4; ++i)
160                 pcm1796_write(chip, i, 20, value);
161 #endif
162 }
163
164 static void update_pcm1796_volume(struct oxygen *chip)
165 {
166         unsigned int i;
167
168         for (i = 0; i < 4; ++i) {
169                 pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]);
170                 pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]);
171         }
172 }
173
174 static void update_pcm1796_mute(struct oxygen *chip)
175 {
176         unsigned int i;
177         u8 value;
178
179         value = PCM1796_FMT_24_LJUST | PCM1796_ATLD;
180         if (chip->dac_mute)
181                 value |= PCM1796_MUTE;
182         for (i = 0; i < 4; ++i)
183                 pcm1796_write(chip, i, 18, value);
184 }
185
186 static void set_cs5381_params(struct oxygen *chip,
187                               struct snd_pcm_hw_params *params)
188 {
189         unsigned int value;
190
191         if (params_rate(params) <= 54000)
192                 value = GPIO_CS5381_M_SINGLE;
193         else if (params_rate(params) <= 108000)
194                 value = GPIO_CS5381_M_DOUBLE;
195         else
196                 value = GPIO_CS5381_M_QUAD;
197         oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
198                               value, GPIO_CS5381_M_MASK);
199 }
200
201 static void xonar_gpio_changed(struct oxygen *chip)
202 {
203         struct xonar_data *data = chip->model_data;
204         u8 has_power;
205
206         if (!data->is_d2x)
207                 return;
208         has_power = !!(oxygen_read16(chip, OXYGEN_GPIO_DATA)
209                        & GPIO_EXT_POWER);
210         if (has_power != data->has_power) {
211                 data->has_power = has_power;
212                 if (has_power) {
213                         snd_printk(KERN_NOTICE "power restored\n");
214                 } else {
215                         snd_printk(KERN_CRIT
216                                    "Hey! Don't unplug the power cable!\n");
217                         /* TODO: stop PCMs */
218                 }
219         }
220 }
221
222 static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
223 {
224         unsigned int priv_idx = chip->controls[control]->private_value & 0xff;
225         u16 value;
226
227         value = oxygen_read_ac97(chip, 0, priv_idx);
228         if (!(value & 0x8000)) {
229                 oxygen_write_ac97(chip, 0, priv_idx, value | 0x8000);
230                 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
231                                &chip->controls[control]->id);
232         }
233 }
234
235 static void xonar_ac97_switch_hook(struct oxygen *chip, unsigned int codec,
236                                    unsigned int reg, int mute)
237 {
238         if (codec != 0)
239                 return;
240         /* line-in is exclusive */
241         switch (reg) {
242         case AC97_LINE:
243                 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
244                                          mute ? GPIO_LINE_MUTE : 0,
245                                          GPIO_LINE_MUTE);
246                 if (!mute) {
247                         mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
248                         mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
249                         mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
250                 }
251                 break;
252         case AC97_MIC:
253         case AC97_CD:
254         case AC97_VIDEO:
255         case AC97_AUX:
256                 if (!mute) {
257                         oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_STATUS,
258                                              GPIO_LINE_MUTE);
259                         mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
260                 }
261                 break;
262         }
263 }
264
265 static int pcm1796_volume_info(struct snd_kcontrol *ctl,
266                                struct snd_ctl_elem_info *info)
267 {
268         info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
269         info->count = 8;
270         info->value.integer.min = 0x0f;
271         info->value.integer.max = 0xff;
272         return 0;
273 }
274
275 static int alt_switch_get(struct snd_kcontrol *ctl,
276                           struct snd_ctl_elem_value *value)
277 {
278         struct oxygen *chip = ctl->private_data;
279
280         value->value.integer.value[0] =
281                 !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_ALT);
282         return 0;
283 }
284
285 static int alt_switch_put(struct snd_kcontrol *ctl,
286                           struct snd_ctl_elem_value *value)
287 {
288         struct oxygen *chip = ctl->private_data;
289         u16 old_bits, new_bits;
290         int changed;
291
292         spin_lock_irq(&chip->reg_lock);
293         old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
294         if (value->value.integer.value[0])
295                 new_bits = old_bits | GPIO_ALT;
296         else
297                 new_bits = old_bits & ~GPIO_ALT;
298         changed = new_bits != old_bits;
299         if (changed)
300                 oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
301         spin_unlock_irq(&chip->reg_lock);
302         return changed;
303 }
304
305 static const struct snd_kcontrol_new alt_switch = {
306         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
307         .name = "Analog Loopback Switch",
308         .info = snd_ctl_boolean_mono_info,
309         .get = alt_switch_get,
310         .put = alt_switch_put,
311 };
312
313 static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
314
315 static int xonar_control_filter(struct snd_kcontrol_new *template)
316 {
317         if (!strcmp(template->name, "Master Playback Volume")) {
318                 template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
319                 template->info = pcm1796_volume_info,
320                 template->tlv.p = pcm1796_db_scale;
321         } else if (!strncmp(template->name, "CD Capture ", 11)) {
322                 /* CD in is actually connected to the video in pin */
323                 template->private_value ^= AC97_CD ^ AC97_VIDEO;
324         } else if (!strcmp(template->name, "Line Capture Volume")) {
325                 return 1; /* line-in bypasses the AC'97 mixer */
326         }
327         return 0;
328 }
329
330 static int xonar_mixer_init(struct oxygen *chip)
331 {
332         return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
333 }
334
335 static const struct oxygen_model model_xonar = {
336         .shortname = "Asus AV200",
337         .longname = "Asus Virtuoso 200",
338         .chip = "AV200",
339         .owner = THIS_MODULE,
340         .init = xonar_init,
341         .control_filter = xonar_control_filter,
342         .mixer_init = xonar_mixer_init,
343         .cleanup = xonar_cleanup,
344         .set_dac_params = set_pcm1796_params,
345         .set_adc_params = set_cs5381_params,
346         .update_dac_volume = update_pcm1796_volume,
347         .update_dac_mute = update_pcm1796_mute,
348         .ac97_switch_hook = xonar_ac97_switch_hook,
349         .gpio_changed = xonar_gpio_changed,
350         .model_data_size = sizeof(struct xonar_data),
351         .dac_channels = 8,
352         .used_channels = OXYGEN_CHANNEL_B |
353                          OXYGEN_CHANNEL_C |
354                          OXYGEN_CHANNEL_SPDIF |
355                          OXYGEN_CHANNEL_MULTICH,
356         .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
357         .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
358         .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
359 };
360
361 static int __devinit xonar_probe(struct pci_dev *pci,
362                                  const struct pci_device_id *pci_id)
363 {
364         static int dev;
365         int err;
366
367         if (dev >= SNDRV_CARDS)
368                 return -ENODEV;
369         if (!enable[dev]) {
370                 ++dev;
371                 return -ENOENT;
372         }
373         err = oxygen_pci_probe(pci, index[dev], id[dev], 1, &model_xonar);
374         if (err >= 0)
375                 ++dev;
376         return err;
377 }
378
379 static struct pci_driver xonar_driver = {
380         .name = "AV200",
381         .id_table = xonar_ids,
382         .probe = xonar_probe,
383         .remove = __devexit_p(oxygen_pci_remove),
384 };
385
386 static int __init alsa_card_xonar_init(void)
387 {
388         return pci_register_driver(&xonar_driver);
389 }
390
391 static void __exit alsa_card_xonar_exit(void)
392 {
393         pci_unregister_driver(&xonar_driver);
394 }
395
396 module_init(alsa_card_xonar_init)
397 module_exit(alsa_card_xonar_exit)