[ALSA] oxygen: make the number of analog output configurable
[powerpc.git] / sound / pci / oxygen / oxygen.c
1 /*
2  * C-Media CMI8788 driver for C-Media's reference design and for the X-Meridian
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  * SPI 0 -> 1st AK4396 (front)
22  * SPI 1 -> 2nd AK4396 (surround)
23  * SPI 2 -> 3rd AK4396 (center/LFE)
24  * SPI 3 -> WM8785
25  * SPI 4 -> 4th AK4396 (back)
26  *
27  * GPIO 0 -> DFS0 of AK5385
28  * GPIO 1 -> DFS1 of AK5385
29  */
30
31 #include <linux/pci.h>
32 #include <sound/control.h>
33 #include <sound/core.h>
34 #include <sound/initval.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/tlv.h>
38 #include "oxygen.h"
39
40 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
41 MODULE_DESCRIPTION("C-Media CMI8788 driver");
42 MODULE_LICENSE("GPL");
43 MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8788}}");
44
45 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
46 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
47 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
48
49 module_param_array(index, int, NULL, 0444);
50 MODULE_PARM_DESC(index, "card index");
51 module_param_array(id, charp, NULL, 0444);
52 MODULE_PARM_DESC(id, "ID string");
53 module_param_array(enable, bool, NULL, 0444);
54 MODULE_PARM_DESC(enable, "enable card");
55
56 static struct pci_device_id oxygen_ids[] __devinitdata = {
57         { OXYGEN_PCI_SUBID(0x10b0, 0x0216) },
58         { OXYGEN_PCI_SUBID(0x10b0, 0x0218) },
59         { OXYGEN_PCI_SUBID(0x10b0, 0x0219) },
60         { OXYGEN_PCI_SUBID(0x13f6, 0x0001) },
61         { OXYGEN_PCI_SUBID(0x13f6, 0x0010) },
62         { OXYGEN_PCI_SUBID(0x13f6, 0x8788) },
63         { OXYGEN_PCI_SUBID(0x147a, 0xa017) },
64         { OXYGEN_PCI_SUBID(0x14c3, 0x1710) },
65         { OXYGEN_PCI_SUBID(0x14c3, 0x1711) },
66         { OXYGEN_PCI_SUBID(0x1a58, 0x0910) },
67         { OXYGEN_PCI_SUBID(0x415a, 0x5431), .driver_data = 1 },
68         { OXYGEN_PCI_SUBID(0x7284, 0x9761) },
69         { }
70 };
71 MODULE_DEVICE_TABLE(pci, oxygen_ids);
72
73
74 #define GPIO_AK5385_DFS_MASK    0x0003
75 #define GPIO_AK5385_DFS_NORMAL  0x0000
76 #define GPIO_AK5385_DFS_DOUBLE  0x0001
77 #define GPIO_AK5385_DFS_QUAD    0x0002
78
79 #define AK4396_WRITE    0x2000
80
81 #define AK4396_CONTROL_1        0
82 #define AK4396_CONTROL_2        1
83 #define AK4396_CONTROL_3        2
84 #define AK4396_LCH_ATT          3
85 #define AK4396_RCH_ATT          4
86
87 /* control 1 */
88 #define AK4396_RSTN             0x01
89 #define AK4396_DIF_MASK         0x0e
90 #define AK4396_DIF_16_LSB       0x00
91 #define AK4396_DIF_20_LSB       0x02
92 #define AK4396_DIF_24_MSB       0x04
93 #define AK4396_DIF_24_I2S       0x06
94 #define AK4396_DIF_24_LSB       0x08
95 #define AK4396_ACKS             0x80
96 /* control 2 */
97 #define AK4396_SMUTE            0x01
98 #define AK4396_DEM_MASK         0x06
99 #define AK4396_DEM_441          0x00
100 #define AK4396_DEM_OFF          0x02
101 #define AK4396_DEM_48           0x04
102 #define AK4396_DEM_32           0x06
103 #define AK4396_DFS_MASK         0x18
104 #define AK4396_DFS_NORMAL       0x00
105 #define AK4396_DFS_DOUBLE       0x08
106 #define AK4396_DFS_QUAD         0x10
107 #define AK4396_SLOW             0x20
108 #define AK4396_DZFM             0x40
109 #define AK4396_DZFE             0x80
110 /* control 3 */
111 #define AK4396_DZFB             0x04
112 #define AK4396_DCKB             0x10
113 #define AK4396_DCKS             0x20
114 #define AK4396_DSDM             0x40
115 #define AK4396_D_P_MASK         0x80
116 #define AK4396_PCM              0x00
117 #define AK4396_DSD              0x80
118
119 #define WM8785_R0       0
120 #define WM8785_R1       1
121 #define WM8785_R2       2
122 #define WM8785_R7       7
123
124 /* R0 */
125 #define WM8785_MCR_MASK         0x007
126 #define WM8785_MCR_SLAVE        0x000
127 #define WM8785_MCR_MASTER_128   0x001
128 #define WM8785_MCR_MASTER_192   0x002
129 #define WM8785_MCR_MASTER_256   0x003
130 #define WM8785_MCR_MASTER_384   0x004
131 #define WM8785_MCR_MASTER_512   0x005
132 #define WM8785_MCR_MASTER_768   0x006
133 #define WM8785_OSR_MASK         0x018
134 #define WM8785_OSR_SINGLE       0x000
135 #define WM8785_OSR_DOUBLE       0x008
136 #define WM8785_OSR_QUAD         0x010
137 #define WM8785_FORMAT_MASK      0x060
138 #define WM8785_FORMAT_RJUST     0x000
139 #define WM8785_FORMAT_LJUST     0x020
140 #define WM8785_FORMAT_I2S       0x040
141 #define WM8785_FORMAT_DSP       0x060
142 /* R1 */
143 #define WM8785_WL_MASK          0x003
144 #define WM8785_WL_16            0x000
145 #define WM8785_WL_20            0x001
146 #define WM8785_WL_24            0x002
147 #define WM8785_WL_32            0x003
148 #define WM8785_LRP              0x004
149 #define WM8785_BCLKINV          0x008
150 #define WM8785_LRSWAP           0x010
151 #define WM8785_DEVNO_MASK       0x0e0
152 /* R2 */
153 #define WM8785_HPFR             0x001
154 #define WM8785_HPFL             0x002
155 #define WM8785_SDODIS           0x004
156 #define WM8785_PWRDNR           0x008
157 #define WM8785_PWRDNL           0x010
158 #define WM8785_TDM_MASK         0x1c0
159
160 struct generic_data {
161         u8 ak4396_ctl2;
162 };
163
164 static void ak4396_write(struct oxygen *chip, unsigned int codec,
165                          u8 reg, u8 value)
166 {
167         /* maps ALSA channel pair number to SPI output */
168         static const u8 codec_spi_map[4] = {
169                 0, 1, 2, 4
170         };
171         oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
172                          OXYGEN_SPI_DATA_LENGTH_2 |
173                          OXYGEN_SPI_CLOCK_320 |
174                          (codec_spi_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
175                          OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
176                          AK4396_WRITE | (reg << 8) | value);
177 }
178
179 static void wm8785_write(struct oxygen *chip, u8 reg, unsigned int value)
180 {
181         oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
182                          OXYGEN_SPI_DATA_LENGTH_2 |
183                          OXYGEN_SPI_CLOCK_320 |
184                          (3 << OXYGEN_SPI_CODEC_SHIFT) |
185                          OXYGEN_SPI_CEN_LATCH_CLOCK_LO,
186                          (reg << 9) | value);
187 }
188
189 static void ak4396_init(struct oxygen *chip)
190 {
191         struct generic_data *data = chip->model_data;
192         unsigned int i;
193
194         data->ak4396_ctl2 = AK4396_DEM_OFF | AK4396_DFS_NORMAL;
195         for (i = 0; i < 4; ++i) {
196                 ak4396_write(chip, i,
197                              AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN);
198                 ak4396_write(chip, i,
199                              AK4396_CONTROL_2, data->ak4396_ctl2);
200                 ak4396_write(chip, i,
201                              AK4396_CONTROL_3, AK4396_PCM);
202                 ak4396_write(chip, i, AK4396_LCH_ATT, 0xff);
203                 ak4396_write(chip, i, AK4396_RCH_ATT, 0xff);
204         }
205         snd_component_add(chip->card, "AK4396");
206 }
207
208 static void ak5385_init(struct oxygen *chip)
209 {
210         oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_AK5385_DFS_MASK);
211         oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_AK5385_DFS_MASK);
212         snd_component_add(chip->card, "AK5385");
213 }
214
215 static void wm8785_init(struct oxygen *chip)
216 {
217         wm8785_write(chip, WM8785_R7, 0);
218         wm8785_write(chip, WM8785_R0, WM8785_MCR_SLAVE |
219                      WM8785_OSR_SINGLE | WM8785_FORMAT_LJUST);
220         wm8785_write(chip, WM8785_R1, WM8785_WL_24);
221         snd_component_add(chip->card, "WM8785");
222 }
223
224 static void generic_init(struct oxygen *chip)
225 {
226         ak4396_init(chip);
227         wm8785_init(chip);
228 }
229
230 static void meridian_init(struct oxygen *chip)
231 {
232         ak4396_init(chip);
233         ak5385_init(chip);
234 }
235
236 static void generic_cleanup(struct oxygen *chip)
237 {
238 }
239
240 static void set_ak4396_params(struct oxygen *chip,
241                               struct snd_pcm_hw_params *params)
242 {
243         struct generic_data *data = chip->model_data;
244         unsigned int i;
245         u8 value;
246
247         value = data->ak4396_ctl2 & ~AK4396_DFS_MASK;
248         if (params_rate(params) <= 54000)
249                 value |= AK4396_DFS_NORMAL;
250         else if (params_rate(params) < 120000)
251                 value |= AK4396_DFS_DOUBLE;
252         else
253                 value |= AK4396_DFS_QUAD;
254         data->ak4396_ctl2 = value;
255         for (i = 0; i < 4; ++i) {
256                 ak4396_write(chip, i,
257                              AK4396_CONTROL_1, AK4396_DIF_24_MSB);
258                 ak4396_write(chip, i,
259                              AK4396_CONTROL_2, value);
260                 ak4396_write(chip, i,
261                              AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN);
262         }
263 }
264
265 static void update_ak4396_volume(struct oxygen *chip)
266 {
267         unsigned int i;
268
269         for (i = 0; i < 4; ++i) {
270                 ak4396_write(chip, i,
271                              AK4396_LCH_ATT, chip->dac_volume[i * 2]);
272                 ak4396_write(chip, i,
273                              AK4396_RCH_ATT, chip->dac_volume[i * 2 + 1]);
274         }
275 }
276
277 static void update_ak4396_mute(struct oxygen *chip)
278 {
279         struct generic_data *data = chip->model_data;
280         unsigned int i;
281         u8 value;
282
283         value = data->ak4396_ctl2 & ~AK4396_SMUTE;
284         if (chip->dac_mute)
285                 value |= AK4396_SMUTE;
286         data->ak4396_ctl2 = value;
287         for (i = 0; i < 4; ++i)
288                 ak4396_write(chip, i, AK4396_CONTROL_2, value);
289 }
290
291 static void set_wm8785_params(struct oxygen *chip,
292                               struct snd_pcm_hw_params *params)
293 {
294         unsigned int value;
295
296         wm8785_write(chip, WM8785_R7, 0);
297
298         value = WM8785_MCR_SLAVE | WM8785_FORMAT_LJUST;
299         if (params_rate(params) <= 48000)
300                 value |= WM8785_OSR_SINGLE;
301         else if (params_rate(params) <= 96000)
302                 value |= WM8785_OSR_DOUBLE;
303         else
304                 value |= WM8785_OSR_QUAD;
305         wm8785_write(chip, WM8785_R0, value);
306
307         if (snd_pcm_format_width(params_format(params)) <= 16)
308                 value = WM8785_WL_16;
309         else
310                 value = WM8785_WL_24;
311         wm8785_write(chip, WM8785_R1, value);
312 }
313
314 static void set_ak5385_params(struct oxygen *chip,
315                               struct snd_pcm_hw_params *params)
316 {
317         unsigned int value;
318
319         if (params_rate(params) <= 54000)
320                 value = GPIO_AK5385_DFS_NORMAL;
321         else if (params_rate(params) <= 108000)
322                 value = GPIO_AK5385_DFS_DOUBLE;
323         else
324                 value = GPIO_AK5385_DFS_QUAD;
325         oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
326                               value, GPIO_AK5385_DFS_MASK);
327 }
328
329 static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);
330
331 static int ak4396_control_filter(struct snd_kcontrol_new *template)
332 {
333         if (!strcmp(template->name, "Master Playback Volume")) {
334                 template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
335                 template->tlv.p = ak4396_db_scale;
336         }
337         return 0;
338 }
339
340 static const struct oxygen_model model_generic = {
341         .shortname = "C-Media CMI8788",
342         .longname = "C-Media Oxygen HD Audio",
343         .chip = "CMI8788",
344         .owner = THIS_MODULE,
345         .init = generic_init,
346         .control_filter = ak4396_control_filter,
347         .cleanup = generic_cleanup,
348         .set_dac_params = set_ak4396_params,
349         .set_adc_params = set_wm8785_params,
350         .update_dac_volume = update_ak4396_volume,
351         .update_dac_mute = update_ak4396_mute,
352         .model_data_size = sizeof(struct generic_data),
353         .dac_channels = 8,
354         .used_channels = OXYGEN_CHANNEL_A |
355                          OXYGEN_CHANNEL_C |
356                          OXYGEN_CHANNEL_SPDIF |
357                          OXYGEN_CHANNEL_MULTICH |
358                          OXYGEN_CHANNEL_AC97,
359         .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
360         .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
361         .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
362 };
363 static const struct oxygen_model model_meridian = {
364         .shortname = "C-Media CMI8788",
365         .longname = "C-Media Oxygen HD Audio",
366         .chip = "CMI8788",
367         .owner = THIS_MODULE,
368         .init = meridian_init,
369         .control_filter = ak4396_control_filter,
370         .cleanup = generic_cleanup,
371         .set_dac_params = set_ak4396_params,
372         .set_adc_params = set_ak5385_params,
373         .update_dac_volume = update_ak4396_volume,
374         .update_dac_mute = update_ak4396_mute,
375         .model_data_size = sizeof(struct generic_data),
376         .dac_channels = 8,
377         .used_channels = OXYGEN_CHANNEL_B |
378                          OXYGEN_CHANNEL_C |
379                          OXYGEN_CHANNEL_SPDIF |
380                          OXYGEN_CHANNEL_MULTICH |
381                          OXYGEN_CHANNEL_AC97,
382         .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
383         .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
384         .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
385 };
386
387 static int __devinit generic_oxygen_probe(struct pci_dev *pci,
388                                           const struct pci_device_id *pci_id)
389 {
390         static int dev;
391         int is_meridian;
392         int err;
393
394         if (dev >= SNDRV_CARDS)
395                 return -ENODEV;
396         if (!enable[dev]) {
397                 ++dev;
398                 return -ENOENT;
399         }
400         is_meridian = pci_id->driver_data;
401         err = oxygen_pci_probe(pci, index[dev], id[dev], is_meridian,
402                                is_meridian ? &model_meridian : &model_generic);
403         if (err >= 0)
404                 ++dev;
405         return err;
406 }
407
408 static struct pci_driver oxygen_driver = {
409         .name = "CMI8788",
410         .id_table = oxygen_ids,
411         .probe = generic_oxygen_probe,
412         .remove = __devexit_p(oxygen_pci_remove),
413 };
414
415 static int __init alsa_card_oxygen_init(void)
416 {
417         return pci_register_driver(&oxygen_driver);
418 }
419
420 static void __exit alsa_card_oxygen_exit(void)
421 {
422         pci_unregister_driver(&oxygen_driver);
423 }
424
425 module_init(alsa_card_oxygen_init)
426 module_exit(alsa_card_oxygen_exit)