Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * C-Media CMI8788 driver for the MediaTek/TempoTec HiFier Fantasia |
| 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 | |
Clemens Ladisch | df91bc2 | 2008-08-29 13:08:34 +0200 | [diff] [blame] | 20 | #include <linux/delay.h> |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 21 | #include <linux/pci.h> |
| 22 | #include <sound/control.h> |
| 23 | #include <sound/core.h> |
| 24 | #include <sound/initval.h> |
| 25 | #include <sound/pcm.h> |
| 26 | #include <sound/tlv.h> |
| 27 | #include "oxygen.h" |
| 28 | #include "ak4396.h" |
| 29 | |
| 30 | MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); |
| 31 | MODULE_DESCRIPTION("TempoTec HiFier driver"); |
Clemens Ladisch | d023dc0 | 2008-05-13 09:18:27 +0200 | [diff] [blame] | 32 | MODULE_LICENSE("GPL v2"); |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 33 | |
| 34 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; |
| 35 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; |
| 36 | static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; |
| 37 | |
| 38 | module_param_array(index, int, NULL, 0444); |
| 39 | MODULE_PARM_DESC(index, "card index"); |
| 40 | module_param_array(id, charp, NULL, 0444); |
| 41 | MODULE_PARM_DESC(id, "ID string"); |
| 42 | module_param_array(enable, bool, NULL, 0444); |
| 43 | MODULE_PARM_DESC(enable, "enable card"); |
| 44 | |
| 45 | static struct pci_device_id hifier_ids[] __devinitdata = { |
| 46 | { OXYGEN_PCI_SUBID(0x14c3, 0x1710) }, |
| 47 | { OXYGEN_PCI_SUBID(0x14c3, 0x1711) }, |
Clemens Ladisch | 30459d7 | 2009-02-19 08:42:44 +0100 | [diff] [blame] | 48 | { OXYGEN_PCI_SUBID_BROKEN_EEPROM }, |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 49 | { } |
| 50 | }; |
| 51 | MODULE_DEVICE_TABLE(pci, hifier_ids); |
| 52 | |
| 53 | struct hifier_data { |
| 54 | u8 ak4396_ctl2; |
| 55 | }; |
| 56 | |
| 57 | static void ak4396_write(struct oxygen *chip, u8 reg, u8 value) |
| 58 | { |
| 59 | oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER | |
| 60 | OXYGEN_SPI_DATA_LENGTH_2 | |
Clemens Ladisch | 2ea8598 | 2008-01-30 08:38:30 +0100 | [diff] [blame] | 61 | OXYGEN_SPI_CLOCK_160 | |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 62 | (0 << OXYGEN_SPI_CODEC_SHIFT) | |
| 63 | OXYGEN_SPI_CEN_LATCH_CLOCK_HI, |
| 64 | AK4396_WRITE | (reg << 8) | value); |
| 65 | } |
| 66 | |
Clemens Ladisch | bbbfb55 | 2008-05-13 09:21:48 +0200 | [diff] [blame] | 67 | static void update_ak4396_volume(struct oxygen *chip) |
| 68 | { |
| 69 | ak4396_write(chip, AK4396_LCH_ATT, chip->dac_volume[0]); |
| 70 | ak4396_write(chip, AK4396_RCH_ATT, chip->dac_volume[1]); |
| 71 | } |
| 72 | |
Clemens Ladisch | 75146fc | 2008-05-13 09:22:43 +0200 | [diff] [blame] | 73 | static void hifier_registers_init(struct oxygen *chip) |
| 74 | { |
| 75 | struct hifier_data *data = chip->model_data; |
| 76 | |
| 77 | ak4396_write(chip, AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN); |
| 78 | ak4396_write(chip, AK4396_CONTROL_2, data->ak4396_ctl2); |
| 79 | ak4396_write(chip, AK4396_CONTROL_3, AK4396_PCM); |
| 80 | update_ak4396_volume(chip); |
| 81 | } |
| 82 | |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 83 | static void hifier_init(struct oxygen *chip) |
| 84 | { |
| 85 | struct hifier_data *data = chip->model_data; |
| 86 | |
Clemens Ladisch | e983532 | 2008-04-16 09:14:30 +0200 | [diff] [blame] | 87 | data->ak4396_ctl2 = AK4396_SMUTE | AK4396_DEM_OFF | AK4396_DFS_NORMAL; |
Clemens Ladisch | 75146fc | 2008-05-13 09:22:43 +0200 | [diff] [blame] | 88 | hifier_registers_init(chip); |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 89 | |
| 90 | snd_component_add(chip->card, "AK4396"); |
| 91 | snd_component_add(chip->card, "CS5340"); |
| 92 | } |
| 93 | |
| 94 | static void hifier_cleanup(struct oxygen *chip) |
| 95 | { |
| 96 | } |
| 97 | |
Clemens Ladisch | c2bc4ff | 2008-09-22 09:05:29 +0200 | [diff] [blame] | 98 | static void hifier_resume(struct oxygen *chip) |
| 99 | { |
| 100 | hifier_registers_init(chip); |
| 101 | } |
| 102 | |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 103 | static void set_ak4396_params(struct oxygen *chip, |
| 104 | struct snd_pcm_hw_params *params) |
| 105 | { |
| 106 | struct hifier_data *data = chip->model_data; |
| 107 | u8 value; |
| 108 | |
| 109 | value = data->ak4396_ctl2 & ~AK4396_DFS_MASK; |
| 110 | if (params_rate(params) <= 54000) |
| 111 | value |= AK4396_DFS_NORMAL; |
Clemens Ladisch | 236c492 | 2008-01-28 08:32:58 +0100 | [diff] [blame] | 112 | else if (params_rate(params) <= 108000) |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 113 | value |= AK4396_DFS_DOUBLE; |
| 114 | else |
| 115 | value |= AK4396_DFS_QUAD; |
| 116 | data->ak4396_ctl2 = value; |
Clemens Ladisch | df91bc2 | 2008-08-29 13:08:34 +0200 | [diff] [blame] | 117 | |
| 118 | msleep(1); /* wait for the new MCLK to become stable */ |
| 119 | |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 120 | ak4396_write(chip, AK4396_CONTROL_1, AK4396_DIF_24_MSB); |
| 121 | ak4396_write(chip, AK4396_CONTROL_2, value); |
| 122 | ak4396_write(chip, AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN); |
| 123 | } |
| 124 | |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 125 | static void update_ak4396_mute(struct oxygen *chip) |
| 126 | { |
| 127 | struct hifier_data *data = chip->model_data; |
| 128 | u8 value; |
| 129 | |
| 130 | value = data->ak4396_ctl2 & ~AK4396_SMUTE; |
| 131 | if (chip->dac_mute) |
| 132 | value |= AK4396_SMUTE; |
| 133 | data->ak4396_ctl2 = value; |
| 134 | ak4396_write(chip, AK4396_CONTROL_2, value); |
| 135 | } |
| 136 | |
| 137 | static void set_cs5340_params(struct oxygen *chip, |
| 138 | struct snd_pcm_hw_params *params) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0); |
| 143 | |
| 144 | static int hifier_control_filter(struct snd_kcontrol_new *template) |
| 145 | { |
Clemens Ladisch | 4972a17 | 2008-04-16 09:15:45 +0200 | [diff] [blame] | 146 | if (!strcmp(template->name, "Stereo Upmixing")) |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 147 | return 1; /* stereo only - we don't need upmixing */ |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 151 | static const struct oxygen_model model_hifier = { |
| 152 | .shortname = "C-Media CMI8787", |
| 153 | .longname = "C-Media Oxygen HD Audio", |
| 154 | .chip = "CMI8788", |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 155 | .init = hifier_init, |
| 156 | .control_filter = hifier_control_filter, |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 157 | .cleanup = hifier_cleanup, |
Clemens Ladisch | c2bc4ff | 2008-09-22 09:05:29 +0200 | [diff] [blame] | 158 | .resume = hifier_resume, |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 159 | .set_dac_params = set_ak4396_params, |
| 160 | .set_adc_params = set_cs5340_params, |
| 161 | .update_dac_volume = update_ak4396_volume, |
| 162 | .update_dac_mute = update_ak4396_mute, |
Clemens Ladisch | 4972a17 | 2008-04-16 09:15:45 +0200 | [diff] [blame] | 163 | .dac_tlv = ak4396_db_scale, |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 164 | .model_data_size = sizeof(struct hifier_data), |
Clemens Ladisch | d76596b | 2008-09-22 09:02:08 +0200 | [diff] [blame] | 165 | .device_config = PLAYBACK_0_TO_I2S | |
| 166 | PLAYBACK_1_TO_SPDIF | |
| 167 | CAPTURE_0_FROM_I2S_1, |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 168 | .dac_channels = 2, |
Clemens Ladisch | 193e813 | 2008-04-16 09:13:36 +0200 | [diff] [blame] | 169 | .dac_volume_min = 0, |
| 170 | .dac_volume_max = 255, |
Clemens Ladisch | 87eedd2 | 2008-03-19 08:20:13 +0100 | [diff] [blame] | 171 | .function_flags = OXYGEN_FUNCTION_SPI, |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 172 | .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST, |
| 173 | .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST, |
| 174 | }; |
| 175 | |
Clemens Ladisch | 30459d7 | 2009-02-19 08:42:44 +0100 | [diff] [blame] | 176 | static int __devinit get_hifier_model(struct oxygen *chip, |
| 177 | const struct pci_device_id *id) |
| 178 | { |
| 179 | chip->model = model_hifier; |
| 180 | return 0; |
| 181 | } |
| 182 | |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 183 | static int __devinit hifier_probe(struct pci_dev *pci, |
| 184 | const struct pci_device_id *pci_id) |
| 185 | { |
| 186 | static int dev; |
| 187 | int err; |
| 188 | |
| 189 | if (dev >= SNDRV_CARDS) |
| 190 | return -ENODEV; |
| 191 | if (!enable[dev]) { |
| 192 | ++dev; |
| 193 | return -ENOENT; |
| 194 | } |
Clemens Ladisch | 30459d7 | 2009-02-19 08:42:44 +0100 | [diff] [blame] | 195 | err = oxygen_pci_probe(pci, index[dev], id[dev], THIS_MODULE, |
| 196 | hifier_ids, get_hifier_model); |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 197 | if (err >= 0) |
| 198 | ++dev; |
| 199 | return err; |
| 200 | } |
| 201 | |
| 202 | static struct pci_driver hifier_driver = { |
| 203 | .name = "CMI8787HiFier", |
| 204 | .id_table = hifier_ids, |
| 205 | .probe = hifier_probe, |
| 206 | .remove = __devexit_p(oxygen_pci_remove), |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 207 | #ifdef CONFIG_PM |
| 208 | .suspend = oxygen_pci_suspend, |
| 209 | .resume = oxygen_pci_resume, |
| 210 | #endif |
Clemens Ladisch | c626026 | 2008-01-25 08:41:52 +0100 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | static int __init alsa_card_hifier_init(void) |
| 214 | { |
| 215 | return pci_register_driver(&hifier_driver); |
| 216 | } |
| 217 | |
| 218 | static void __exit alsa_card_hifier_exit(void) |
| 219 | { |
| 220 | pci_unregister_driver(&hifier_driver); |
| 221 | } |
| 222 | |
| 223 | module_init(alsa_card_hifier_init) |
| 224 | module_exit(alsa_card_hifier_exit) |