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/mutex.h> 32 #include <linux/pci.h> 33 #include <sound/ac97_codec.h> 34 #include <sound/control.h> 35 #include <sound/core.h> 36 #include <sound/initval.h> 37 #include <sound/pcm.h> 38 #include <sound/pcm_params.h> 39 #include <sound/tlv.h> 40 #include "oxygen.h" 41 #include "ak4396.h" 42 #include "wm8785.h" 43 44 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); 45 MODULE_DESCRIPTION("C-Media CMI8788 driver"); 46 MODULE_LICENSE("GPL v2"); 47 MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8788}}"); 48 49 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; 50 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; 51 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; 52 53 module_param_array(index, int, NULL, 0444); 54 MODULE_PARM_DESC(index, "card index"); 55 module_param_array(id, charp, NULL, 0444); 56 MODULE_PARM_DESC(id, "ID string"); 57 module_param_array(enable, bool, NULL, 0444); 58 MODULE_PARM_DESC(enable, "enable card"); 59 60 static struct pci_device_id oxygen_ids[] __devinitdata = { 61 { OXYGEN_PCI_SUBID(0x10b0, 0x0216) }, 62 { OXYGEN_PCI_SUBID(0x10b0, 0x0218) }, 63 { OXYGEN_PCI_SUBID(0x10b0, 0x0219) }, 64 { OXYGEN_PCI_SUBID(0x13f6, 0x0001) }, 65 { OXYGEN_PCI_SUBID(0x13f6, 0x0010) }, 66 { OXYGEN_PCI_SUBID(0x13f6, 0x8788) }, 67 { OXYGEN_PCI_SUBID(0x147a, 0xa017) }, 68 { OXYGEN_PCI_SUBID(0x1a58, 0x0910) }, 69 { OXYGEN_PCI_SUBID(0x415a, 0x5431), .driver_data = 1 }, 70 { OXYGEN_PCI_SUBID(0x7284, 0x9761) }, 71 { } 72 }; 73 MODULE_DEVICE_TABLE(pci, oxygen_ids); 74 75 76 #define GPIO_AK5385_DFS_MASK 0x0003 77 #define GPIO_AK5385_DFS_NORMAL 0x0000 78 #define GPIO_AK5385_DFS_DOUBLE 0x0001 79 #define GPIO_AK5385_DFS_QUAD 0x0002 80 81 struct generic_data { 82 u8 ak4396_ctl2; 83 u16 saved_wm8785_registers[2]; 84 }; 85 86 static void ak4396_write(struct oxygen *chip, unsigned int codec, 87 u8 reg, u8 value) 88 { 89 /* maps ALSA channel pair number to SPI output */ 90 static const u8 codec_spi_map[4] = { 91 0, 1, 2, 4 92 }; 93 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER | 94 OXYGEN_SPI_DATA_LENGTH_2 | 95 OXYGEN_SPI_CLOCK_160 | 96 (codec_spi_map[codec] << OXYGEN_SPI_CODEC_SHIFT) | 97 OXYGEN_SPI_CEN_LATCH_CLOCK_HI, 98 AK4396_WRITE | (reg << 8) | value); 99 } 100 101 static void wm8785_write(struct oxygen *chip, u8 reg, unsigned int value) 102 { 103 struct generic_data *data = chip->model_data; 104 105 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER | 106 OXYGEN_SPI_DATA_LENGTH_2 | 107 OXYGEN_SPI_CLOCK_160 | 108 (3 << OXYGEN_SPI_CODEC_SHIFT) | 109 OXYGEN_SPI_CEN_LATCH_CLOCK_LO, 110 (reg << 9) | value); 111 if (reg < ARRAY_SIZE(data->saved_wm8785_registers)) 112 data->saved_wm8785_registers[reg] = value; 113 } 114 115 static void update_ak4396_volume(struct oxygen *chip) 116 { 117 unsigned int i; 118 119 for (i = 0; i < 4; ++i) { 120 ak4396_write(chip, i, 121 AK4396_LCH_ATT, chip->dac_volume[i * 2]); 122 ak4396_write(chip, i, 123 AK4396_RCH_ATT, chip->dac_volume[i * 2 + 1]); 124 } 125 } 126 127 static void ak4396_registers_init(struct oxygen *chip) 128 { 129 struct generic_data *data = chip->model_data; 130 unsigned int i; 131 132 for (i = 0; i < 4; ++i) { 133 ak4396_write(chip, i, 134 AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN); 135 ak4396_write(chip, i, 136 AK4396_CONTROL_2, data->ak4396_ctl2); 137 ak4396_write(chip, i, 138 AK4396_CONTROL_3, AK4396_PCM); 139 } 140 update_ak4396_volume(chip); 141 } 142 143 static void ak4396_init(struct oxygen *chip) 144 { 145 struct generic_data *data = chip->model_data; 146 147 data->ak4396_ctl2 = AK4396_SMUTE | AK4396_DEM_OFF | AK4396_DFS_NORMAL; 148 ak4396_registers_init(chip); 149 snd_component_add(chip->card, "AK4396"); 150 } 151 152 static void ak5385_init(struct oxygen *chip) 153 { 154 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_AK5385_DFS_MASK); 155 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_AK5385_DFS_MASK); 156 snd_component_add(chip->card, "AK5385"); 157 } 158 159 static void wm8785_registers_init(struct oxygen *chip) 160 { 161 struct generic_data *data = chip->model_data; 162 163 wm8785_write(chip, WM8785_R7, 0); 164 wm8785_write(chip, WM8785_R0, data->saved_wm8785_registers[0]); 165 wm8785_write(chip, WM8785_R1, data->saved_wm8785_registers[1]); 166 } 167 168 static void wm8785_init(struct oxygen *chip) 169 { 170 struct generic_data *data = chip->model_data; 171 172 data->saved_wm8785_registers[0] = WM8785_MCR_SLAVE | 173 WM8785_OSR_SINGLE | WM8785_FORMAT_LJUST; 174 data->saved_wm8785_registers[1] = WM8785_WL_24; 175 wm8785_registers_init(chip); 176 snd_component_add(chip->card, "WM8785"); 177 } 178 179 static void generic_init(struct oxygen *chip) 180 { 181 ak4396_init(chip); 182 wm8785_init(chip); 183 } 184 185 static void meridian_init(struct oxygen *chip) 186 { 187 ak4396_init(chip); 188 ak5385_init(chip); 189 } 190 191 static void generic_cleanup(struct oxygen *chip) 192 { 193 } 194 195 static void generic_resume(struct oxygen *chip) 196 { 197 ak4396_registers_init(chip); 198 wm8785_registers_init(chip); 199 } 200 201 static void set_ak4396_params(struct oxygen *chip, 202 struct snd_pcm_hw_params *params) 203 { 204 struct generic_data *data = chip->model_data; 205 unsigned int i; 206 u8 value; 207 208 value = data->ak4396_ctl2 & ~AK4396_DFS_MASK; 209 if (params_rate(params) <= 54000) 210 value |= AK4396_DFS_NORMAL; 211 else if (params_rate(params) <= 108000) 212 value |= AK4396_DFS_DOUBLE; 213 else 214 value |= AK4396_DFS_QUAD; 215 data->ak4396_ctl2 = value; 216 for (i = 0; i < 4; ++i) { 217 ak4396_write(chip, i, 218 AK4396_CONTROL_1, AK4396_DIF_24_MSB); 219 ak4396_write(chip, i, 220 AK4396_CONTROL_2, value); 221 ak4396_write(chip, i, 222 AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN); 223 } 224 } 225 226 static void update_ak4396_mute(struct oxygen *chip) 227 { 228 struct generic_data *data = chip->model_data; 229 unsigned int i; 230 u8 value; 231 232 value = data->ak4396_ctl2 & ~AK4396_SMUTE; 233 if (chip->dac_mute) 234 value |= AK4396_SMUTE; 235 data->ak4396_ctl2 = value; 236 for (i = 0; i < 4; ++i) 237 ak4396_write(chip, i, AK4396_CONTROL_2, value); 238 } 239 240 static void set_wm8785_params(struct oxygen *chip, 241 struct snd_pcm_hw_params *params) 242 { 243 unsigned int value; 244 245 wm8785_write(chip, WM8785_R7, 0); 246 247 value = WM8785_MCR_SLAVE | WM8785_FORMAT_LJUST; 248 if (params_rate(params) <= 48000) 249 value |= WM8785_OSR_SINGLE; 250 else if (params_rate(params) <= 96000) 251 value |= WM8785_OSR_DOUBLE; 252 else 253 value |= WM8785_OSR_QUAD; 254 wm8785_write(chip, WM8785_R0, value); 255 256 if (snd_pcm_format_width(params_format(params)) <= 16) 257 value = WM8785_WL_16; 258 else 259 value = WM8785_WL_24; 260 wm8785_write(chip, WM8785_R1, value); 261 } 262 263 static void set_ak5385_params(struct oxygen *chip, 264 struct snd_pcm_hw_params *params) 265 { 266 unsigned int value; 267 268 if (params_rate(params) <= 54000) 269 value = GPIO_AK5385_DFS_NORMAL; 270 else if (params_rate(params) <= 108000) 271 value = GPIO_AK5385_DFS_DOUBLE; 272 else 273 value = GPIO_AK5385_DFS_QUAD; 274 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, 275 value, GPIO_AK5385_DFS_MASK); 276 } 277 278 static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0); 279 280 static const struct oxygen_model model_generic = { 281 .shortname = "C-Media CMI8788", 282 .longname = "C-Media Oxygen HD Audio", 283 .chip = "CMI8788", 284 .owner = THIS_MODULE, 285 .init = generic_init, 286 .cleanup = generic_cleanup, 287 .resume = generic_resume, 288 .set_dac_params = set_ak4396_params, 289 .set_adc_params = set_wm8785_params, 290 .update_dac_volume = update_ak4396_volume, 291 .update_dac_mute = update_ak4396_mute, 292 .dac_tlv = ak4396_db_scale, 293 .model_data_size = sizeof(struct generic_data), 294 .pcm_dev_cfg = PLAYBACK_0_TO_I2S | 295 PLAYBACK_1_TO_SPDIF | 296 PLAYBACK_2_TO_AC97_1 | 297 CAPTURE_0_FROM_I2S_1 | 298 CAPTURE_1_FROM_SPDIF | 299 CAPTURE_2_FROM_AC97_1, 300 .dac_channels = 8, 301 .dac_volume_min = 0, 302 .dac_volume_max = 255, 303 .function_flags = OXYGEN_FUNCTION_SPI | 304 OXYGEN_FUNCTION_ENABLE_SPI_4_5, 305 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 306 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 307 }; 308 static const struct oxygen_model model_meridian = { 309 .shortname = "C-Media CMI8788", 310 .longname = "C-Media Oxygen HD Audio", 311 .chip = "CMI8788", 312 .owner = THIS_MODULE, 313 .init = meridian_init, 314 .cleanup = generic_cleanup, 315 .resume = ak4396_registers_init, 316 .set_dac_params = set_ak4396_params, 317 .set_adc_params = set_ak5385_params, 318 .update_dac_volume = update_ak4396_volume, 319 .update_dac_mute = update_ak4396_mute, 320 .dac_tlv = ak4396_db_scale, 321 .model_data_size = sizeof(struct generic_data), 322 .pcm_dev_cfg = PLAYBACK_0_TO_I2S | 323 PLAYBACK_1_TO_SPDIF | 324 PLAYBACK_2_TO_AC97_1 | 325 CAPTURE_0_FROM_I2S_2 | 326 CAPTURE_1_FROM_SPDIF | 327 CAPTURE_2_FROM_AC97_1, 328 .dac_channels = 8, 329 .dac_volume_min = 0, 330 .dac_volume_max = 255, 331 .misc_flags = OXYGEN_MISC_MIDI, 332 .function_flags = OXYGEN_FUNCTION_SPI | 333 OXYGEN_FUNCTION_ENABLE_SPI_4_5, 334 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 335 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST, 336 }; 337 338 static int __devinit generic_oxygen_probe(struct pci_dev *pci, 339 const struct pci_device_id *pci_id) 340 { 341 static int dev; 342 int is_meridian; 343 int err; 344 345 if (dev >= SNDRV_CARDS) 346 return -ENODEV; 347 if (!enable[dev]) { 348 ++dev; 349 return -ENOENT; 350 } 351 is_meridian = pci_id->driver_data; 352 err = oxygen_pci_probe(pci, index[dev], id[dev], 353 is_meridian ? &model_meridian : &model_generic); 354 if (err >= 0) 355 ++dev; 356 return err; 357 } 358 359 static struct pci_driver oxygen_driver = { 360 .name = "CMI8788", 361 .id_table = oxygen_ids, 362 .probe = generic_oxygen_probe, 363 .remove = __devexit_p(oxygen_pci_remove), 364 #ifdef CONFIG_PM 365 .suspend = oxygen_pci_suspend, 366 .resume = oxygen_pci_resume, 367 #endif 368 }; 369 370 static int __init alsa_card_oxygen_init(void) 371 { 372 return pci_register_driver(&oxygen_driver); 373 } 374 375 static void __exit alsa_card_oxygen_exit(void) 376 { 377 pci_unregister_driver(&oxygen_driver); 378 } 379 380 module_init(alsa_card_oxygen_init) 381 module_exit(alsa_card_oxygen_exit) 382