[ALSA] add CMI8788 driver
[linux-2.6] / 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 (side)
23  * SPI 2 -> 3rd AK4396 (center/LFE)
24  * SPI 3 -> WM8785
25  * SPI 4 -> 4th AK4396 (rear)
26  *
27  * GPIO 0 -> DFS0 of AK5385
28  * GPIO 1 -> DFS1 of AK5385
29  */
30
31 #include <sound/driver.h>
32 #include <linux/pci.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 #define AK4396_WRITE    0x2000
74
75 /* register 0 */
76 #define AK4396_RSTN             0x01
77 #define AK4396_DIF_24_MSB       0x04
78 /* register 1 */
79 #define AK4396_SMUTE            0x01
80 #define AK4396_DEM_OFF          0x02
81 #define AK4396_DFS_MASK         0x18
82 #define AK4396_DFS_NORMAL       0x00
83 #define AK4396_DFS_DOUBLE       0x08
84 #define AK4396_DFS_QUAD         0x10
85
86 /* register 0 */
87 #define WM8785_OSR_SINGLE       0x000
88 #define WM8785_OSR_DOUBLE       0x008
89 #define WM8785_OSR_QUAD         0x010
90 #define WM8785_FORMAT_LJUST     0x020
91 #define WM8785_FORMAT_I2S       0x040
92 /* register 1 */
93 #define WM8785_WL_16            0x000
94 #define WM8785_WL_20            0x001
95 #define WM8785_WL_24            0x002
96 #define WM8785_WL_32            0x003
97
98 static void ak4396_write(struct oxygen *chip, unsigned int codec,
99                          u8 reg, u8 value)
100 {
101         /* maps ALSA channel pair number to SPI output */
102         static const u8 codec_spi_map[4] = {
103                 0, 4, 2, 1
104         };
105         oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER_WRITE |
106                          OXYGEN_SPI_DATA_LENGTH_2 |
107                          (codec_spi_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
108                          OXYGEN_SPI_MAGIC,
109                          AK4396_WRITE | (reg << 8) | value);
110 }
111
112 static void wm8785_write(struct oxygen *chip, u8 reg, unsigned int value)
113 {
114         oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER_WRITE |
115                          OXYGEN_SPI_DATA_LENGTH_2 |
116                          (3 << OXYGEN_SPI_CODEC_SHIFT),
117                          (reg << 9) | value);
118 }
119
120 static void ak4396_init(struct oxygen *chip)
121 {
122         unsigned int i;
123
124         chip->ak4396_reg1 = AK4396_DEM_OFF | AK4396_DFS_NORMAL;
125         for (i = 0; i < 4; ++i) {
126                 ak4396_write(chip, i, 0, AK4396_DIF_24_MSB | AK4396_RSTN);
127                 ak4396_write(chip, i, 1, chip->ak4396_reg1);
128                 ak4396_write(chip, i, 2, 0);
129                 ak4396_write(chip, i, 3, 0xff);
130                 ak4396_write(chip, i, 4, 0xff);
131         }
132         snd_component_add(chip->card, "AK4396");
133 }
134
135 static void ak5385_init(struct oxygen *chip)
136 {
137         oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 0x0003);
138         oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, 0x0003);
139         snd_component_add(chip->card, "AK5385");
140 }
141
142 static void wm8785_init(struct oxygen *chip)
143 {
144         wm8785_write(chip, 7, 0);
145         wm8785_write(chip, 0, WM8785_FORMAT_LJUST | WM8785_OSR_SINGLE);
146         wm8785_write(chip, 1, WM8785_WL_24);
147         snd_component_add(chip->card, "WM8785");
148 }
149
150 static void generic_init(struct oxygen *chip)
151 {
152         ak4396_init(chip);
153         wm8785_init(chip);
154 }
155
156 static void meridian_init(struct oxygen *chip)
157 {
158         ak4396_init(chip);
159         ak5385_init(chip);
160 }
161
162 static void generic_cleanup(struct oxygen *chip)
163 {
164 }
165
166 static void set_ak4396_params(struct oxygen *chip,
167                               struct snd_pcm_hw_params *params)
168 {
169         unsigned int i;
170         u8 value;
171
172         value = chip->ak4396_reg1 & ~AK4396_DFS_MASK;
173         if (params_rate(params) <= 54000)
174                 value |= AK4396_DFS_NORMAL;
175         else if (params_rate(params) < 120000)
176                 value |= AK4396_DFS_DOUBLE;
177         else
178                 value |= AK4396_DFS_QUAD;
179         chip->ak4396_reg1 = value;
180         for (i = 0; i < 4; ++i) {
181                 ak4396_write(chip, i, 0, AK4396_DIF_24_MSB);
182                 ak4396_write(chip, i, 1, value);
183                 ak4396_write(chip, i, 0, AK4396_DIF_24_MSB | AK4396_RSTN);
184         }
185 }
186
187 static void update_ak4396_volume(struct oxygen *chip)
188 {
189         unsigned int i;
190
191         for (i = 0; i < 4; ++i) {
192                 ak4396_write(chip, i, 3, chip->dac_volume[i * 2]);
193                 ak4396_write(chip, i, 4, chip->dac_volume[i * 2 + 1]);
194         }
195 }
196
197 static void update_ak4396_mute(struct oxygen *chip)
198 {
199         unsigned int i;
200         u8 value;
201
202         value = chip->ak4396_reg1 & ~AK4396_SMUTE;
203         if (chip->dac_mute)
204                 value |= AK4396_SMUTE;
205         for (i = 0; i < 4; ++i)
206                 ak4396_write(chip, i, 1, value);
207 }
208
209 static void set_wm8785_params(struct oxygen *chip,
210                               struct snd_pcm_hw_params *params)
211 {
212         unsigned int value;
213
214         wm8785_write(chip, 7, 0);
215
216         value = WM8785_FORMAT_LJUST;
217         if (params_rate(params) == 96000)
218                 value |= WM8785_OSR_DOUBLE;
219         else if (params_rate(params) == 192000)
220                 value |= WM8785_OSR_QUAD;
221         else
222                 value |= WM8785_OSR_SINGLE;
223         wm8785_write(chip, 0, value);
224
225         if (snd_pcm_format_width(params_format(params)) <= 16)
226                 value = WM8785_WL_16;
227         else
228                 value = WM8785_WL_24;
229         wm8785_write(chip, 1, value);
230 }
231
232 static void set_ak5385_params(struct oxygen *chip,
233                               struct snd_pcm_hw_params *params)
234 {
235         unsigned int value;
236
237         if (params_rate(params) <= 54000)
238                 value = 0;
239         else if (params_rate(params) <= 108000)
240                 value = 1;
241         else
242                 value = 2;
243         oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, value, 0x0003);
244 }
245
246 static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);
247
248 static const struct oxygen_model model_generic = {
249         .shortname = "C-Media CMI8788",
250         .longname = "C-Media Oxygen HD Audio",
251         .chip = "CMI8788",
252         .owner = THIS_MODULE,
253         .init = generic_init,
254         .cleanup = generic_cleanup,
255         .set_dac_params = set_ak4396_params,
256         .set_adc_params = set_wm8785_params,
257         .update_dac_volume = update_ak4396_volume,
258         .update_dac_mute = update_ak4396_mute,
259         .dac_tlv = ak4396_db_scale,
260 };
261 static const struct oxygen_model model_meridian = {
262         .shortname = "C-Media CMI8788",
263         .longname = "C-Media Oxygen HD Audio",
264         .chip = "CMI8788",
265         .owner = THIS_MODULE,
266         .init = meridian_init,
267         .cleanup = generic_cleanup,
268         .set_dac_params = set_ak4396_params,
269         .set_adc_params = set_ak5385_params,
270         .update_dac_volume = update_ak4396_volume,
271         .update_dac_mute = update_ak4396_mute,
272         .dac_tlv = ak4396_db_scale,
273         .record_from_dma_b = 1,
274 };
275
276 static int __devinit generic_oxygen_probe(struct pci_dev *pci,
277                                           const struct pci_device_id *pci_id)
278 {
279         static int dev;
280         const struct oxygen_model *model;
281         int err;
282
283         if (dev >= SNDRV_CARDS)
284                 return -ENODEV;
285         if (!enable[dev]) {
286                 ++dev;
287                 return -ENOENT;
288         }
289         model = pci_id->driver_data ? &model_meridian : &model_generic;
290         err = oxygen_pci_probe(pci, index[dev], id[dev], model);
291         if (err >= 0)
292                 ++dev;
293         return err;
294 }
295
296 static struct pci_driver oxygen_driver = {
297         .name = "CMI8788",
298         .id_table = oxygen_ids,
299         .probe = generic_oxygen_probe,
300         .remove = __devexit_p(oxygen_pci_remove),
301 };
302
303 static int __init alsa_card_oxygen_init(void)
304 {
305         return pci_register_driver(&oxygen_driver);
306 }
307
308 static void __exit alsa_card_oxygen_exit(void)
309 {
310         pci_unregister_driver(&oxygen_driver);
311 }
312
313 module_init(alsa_card_oxygen_init)
314 module_exit(alsa_card_oxygen_exit)