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