blob: 173d6dddc937de3e947cf405b3d1cb9a3e164bd8 [file] [log] [blame]
Clemens Ladischc6260262008-01-25 08:41:52 +01001/*
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 Ladischdf91bc22008-08-29 13:08:34 +020020#include <linux/delay.h>
Clemens Ladischc6260262008-01-25 08:41:52 +010021#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
30MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
31MODULE_DESCRIPTION("TempoTec HiFier driver");
Clemens Ladischd023dc02008-05-13 09:18:27 +020032MODULE_LICENSE("GPL v2");
Clemens Ladischc6260262008-01-25 08:41:52 +010033
34static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
35static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
36static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
37
38module_param_array(index, int, NULL, 0444);
39MODULE_PARM_DESC(index, "card index");
40module_param_array(id, charp, NULL, 0444);
41MODULE_PARM_DESC(id, "ID string");
42module_param_array(enable, bool, NULL, 0444);
43MODULE_PARM_DESC(enable, "enable card");
44
45static struct pci_device_id hifier_ids[] __devinitdata = {
46 { OXYGEN_PCI_SUBID(0x14c3, 0x1710) },
47 { OXYGEN_PCI_SUBID(0x14c3, 0x1711) },
48 { }
49};
50MODULE_DEVICE_TABLE(pci, hifier_ids);
51
52struct hifier_data {
53 u8 ak4396_ctl2;
54};
55
56static void ak4396_write(struct oxygen *chip, u8 reg, u8 value)
57{
58 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
59 OXYGEN_SPI_DATA_LENGTH_2 |
Clemens Ladisch2ea85982008-01-30 08:38:30 +010060 OXYGEN_SPI_CLOCK_160 |
Clemens Ladischc6260262008-01-25 08:41:52 +010061 (0 << OXYGEN_SPI_CODEC_SHIFT) |
62 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
63 AK4396_WRITE | (reg << 8) | value);
64}
65
Clemens Ladischbbbfb552008-05-13 09:21:48 +020066static void update_ak4396_volume(struct oxygen *chip)
67{
68 ak4396_write(chip, AK4396_LCH_ATT, chip->dac_volume[0]);
69 ak4396_write(chip, AK4396_RCH_ATT, chip->dac_volume[1]);
70}
71
Clemens Ladisch75146fc2008-05-13 09:22:43 +020072static void hifier_registers_init(struct oxygen *chip)
73{
74 struct hifier_data *data = chip->model_data;
75
76 ak4396_write(chip, AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN);
77 ak4396_write(chip, AK4396_CONTROL_2, data->ak4396_ctl2);
78 ak4396_write(chip, AK4396_CONTROL_3, AK4396_PCM);
79 update_ak4396_volume(chip);
80}
81
Clemens Ladischc6260262008-01-25 08:41:52 +010082static void hifier_init(struct oxygen *chip)
83{
84 struct hifier_data *data = chip->model_data;
85
Clemens Ladische9835322008-04-16 09:14:30 +020086 data->ak4396_ctl2 = AK4396_SMUTE | AK4396_DEM_OFF | AK4396_DFS_NORMAL;
Clemens Ladisch75146fc2008-05-13 09:22:43 +020087 hifier_registers_init(chip);
Clemens Ladischc6260262008-01-25 08:41:52 +010088
89 snd_component_add(chip->card, "AK4396");
90 snd_component_add(chip->card, "CS5340");
91}
92
93static void hifier_cleanup(struct oxygen *chip)
94{
95}
96
97static void set_ak4396_params(struct oxygen *chip,
98 struct snd_pcm_hw_params *params)
99{
100 struct hifier_data *data = chip->model_data;
101 u8 value;
102
103 value = data->ak4396_ctl2 & ~AK4396_DFS_MASK;
104 if (params_rate(params) <= 54000)
105 value |= AK4396_DFS_NORMAL;
Clemens Ladisch236c4922008-01-28 08:32:58 +0100106 else if (params_rate(params) <= 108000)
Clemens Ladischc6260262008-01-25 08:41:52 +0100107 value |= AK4396_DFS_DOUBLE;
108 else
109 value |= AK4396_DFS_QUAD;
110 data->ak4396_ctl2 = value;
Clemens Ladischdf91bc22008-08-29 13:08:34 +0200111
112 msleep(1); /* wait for the new MCLK to become stable */
113
Clemens Ladischc6260262008-01-25 08:41:52 +0100114 ak4396_write(chip, AK4396_CONTROL_1, AK4396_DIF_24_MSB);
115 ak4396_write(chip, AK4396_CONTROL_2, value);
116 ak4396_write(chip, AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN);
117}
118
Clemens Ladischc6260262008-01-25 08:41:52 +0100119static void update_ak4396_mute(struct oxygen *chip)
120{
121 struct hifier_data *data = chip->model_data;
122 u8 value;
123
124 value = data->ak4396_ctl2 & ~AK4396_SMUTE;
125 if (chip->dac_mute)
126 value |= AK4396_SMUTE;
127 data->ak4396_ctl2 = value;
128 ak4396_write(chip, AK4396_CONTROL_2, value);
129}
130
131static void set_cs5340_params(struct oxygen *chip,
132 struct snd_pcm_hw_params *params)
133{
134}
135
136static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);
137
138static int hifier_control_filter(struct snd_kcontrol_new *template)
139{
Clemens Ladisch4972a172008-04-16 09:15:45 +0200140 if (!strcmp(template->name, "Stereo Upmixing"))
Clemens Ladischc6260262008-01-25 08:41:52 +0100141 return 1; /* stereo only - we don't need upmixing */
Clemens Ladischc6260262008-01-25 08:41:52 +0100142 return 0;
143}
144
Clemens Ladischc6260262008-01-25 08:41:52 +0100145static const struct oxygen_model model_hifier = {
146 .shortname = "C-Media CMI8787",
147 .longname = "C-Media Oxygen HD Audio",
148 .chip = "CMI8788",
Clemens Ladischb930b9f2008-02-26 08:40:57 +0100149 .owner = THIS_MODULE,
Clemens Ladischc6260262008-01-25 08:41:52 +0100150 .init = hifier_init,
151 .control_filter = hifier_control_filter,
Clemens Ladischc6260262008-01-25 08:41:52 +0100152 .cleanup = hifier_cleanup,
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200153 .resume = hifier_registers_init,
Clemens Ladischc6260262008-01-25 08:41:52 +0100154 .set_dac_params = set_ak4396_params,
155 .set_adc_params = set_cs5340_params,
156 .update_dac_volume = update_ak4396_volume,
157 .update_dac_mute = update_ak4396_mute,
Clemens Ladisch4972a172008-04-16 09:15:45 +0200158 .dac_tlv = ak4396_db_scale,
Clemens Ladischc6260262008-01-25 08:41:52 +0100159 .model_data_size = sizeof(struct hifier_data),
Clemens Ladischf009ad92008-03-19 08:19:41 +0100160 .pcm_dev_cfg = PLAYBACK_0_TO_I2S |
161 PLAYBACK_1_TO_SPDIF |
162 CAPTURE_0_FROM_I2S_1,
Clemens Ladischc6260262008-01-25 08:41:52 +0100163 .dac_channels = 2,
Clemens Ladisch193e8132008-04-16 09:13:36 +0200164 .dac_volume_min = 0,
165 .dac_volume_max = 255,
Clemens Ladisch87eedd22008-03-19 08:20:13 +0100166 .function_flags = OXYGEN_FUNCTION_SPI,
Clemens Ladischc6260262008-01-25 08:41:52 +0100167 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
168 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
169};
170
171static int __devinit hifier_probe(struct pci_dev *pci,
172 const struct pci_device_id *pci_id)
173{
174 static int dev;
175 int err;
176
177 if (dev >= SNDRV_CARDS)
178 return -ENODEV;
179 if (!enable[dev]) {
180 ++dev;
181 return -ENOENT;
182 }
Clemens Ladisch568c59e2008-09-22 08:56:01 +0200183 err = oxygen_pci_probe(pci, index[dev], id[dev], &model_hifier, 0);
Clemens Ladischc6260262008-01-25 08:41:52 +0100184 if (err >= 0)
185 ++dev;
186 return err;
187}
188
189static struct pci_driver hifier_driver = {
190 .name = "CMI8787HiFier",
191 .id_table = hifier_ids,
192 .probe = hifier_probe,
193 .remove = __devexit_p(oxygen_pci_remove),
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200194#ifdef CONFIG_PM
195 .suspend = oxygen_pci_suspend,
196 .resume = oxygen_pci_resume,
197#endif
Clemens Ladischc6260262008-01-25 08:41:52 +0100198};
199
200static int __init alsa_card_hifier_init(void)
201{
202 return pci_register_driver(&hifier_driver);
203}
204
205static void __exit alsa_card_hifier_exit(void)
206{
207 pci_unregister_driver(&hifier_driver);
208}
209
210module_init(alsa_card_hifier_init)
211module_exit(alsa_card_hifier_exit)