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