xref: /openbmc/linux/sound/pci/oxygen/oxygen.h (revision 2209fda3)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef OXYGEN_H_INCLUDED
3 #define OXYGEN_H_INCLUDED
4 
5 #include <linux/mutex.h>
6 #include <linux/spinlock.h>
7 #include <linux/wait.h>
8 #include <linux/workqueue.h>
9 #include "oxygen_regs.h"
10 
11 /* 1 << PCM_x == OXYGEN_CHANNEL_x */
12 #define PCM_A		0
13 #define PCM_B		1
14 #define PCM_C		2
15 #define PCM_SPDIF	3
16 #define PCM_MULTICH	4
17 #define PCM_AC97	5
18 #define PCM_COUNT	6
19 
20 #define OXYGEN_MCLKS(f_single, f_double, f_quad) ((MCLK_##f_single << 0) | \
21 						  (MCLK_##f_double << 2) | \
22 						  (MCLK_##f_quad   << 4))
23 
24 #define OXYGEN_IO_SIZE	0x100
25 
26 #define OXYGEN_EEPROM_ID	0x434d	/* "CM" */
27 
28 /* model-specific configuration of outputs/inputs */
29 #define PLAYBACK_0_TO_I2S	0x0001
30      /* PLAYBACK_0_TO_AC97_0		not implemented */
31 #define PLAYBACK_1_TO_SPDIF	0x0004
32 #define PLAYBACK_2_TO_AC97_1	0x0008
33 #define CAPTURE_0_FROM_I2S_1	0x0010
34 #define CAPTURE_0_FROM_I2S_2	0x0020
35      /* CAPTURE_0_FROM_AC97_0		not implemented */
36 #define CAPTURE_1_FROM_SPDIF	0x0080
37 #define CAPTURE_2_FROM_I2S_2	0x0100
38 #define CAPTURE_2_FROM_AC97_1	0x0200
39 #define CAPTURE_3_FROM_I2S_3	0x0400
40 #define MIDI_OUTPUT		0x0800
41 #define MIDI_INPUT		0x1000
42 #define AC97_CD_INPUT		0x2000
43 #define AC97_FMIC_SWITCH	0x4000
44 
45 enum {
46 	CONTROL_SPDIF_PCM,
47 	CONTROL_SPDIF_INPUT_BITS,
48 	CONTROL_MIC_CAPTURE_SWITCH,
49 	CONTROL_LINE_CAPTURE_SWITCH,
50 	CONTROL_CD_CAPTURE_SWITCH,
51 	CONTROL_AUX_CAPTURE_SWITCH,
52 	CONTROL_COUNT
53 };
54 
55 #define OXYGEN_PCI_SUBID(sv, sd) \
56 	.vendor = PCI_VENDOR_ID_CMEDIA, \
57 	.device = 0x8788, \
58 	.subvendor = sv, \
59 	.subdevice = sd
60 
61 #define BROKEN_EEPROM_DRIVER_DATA ((unsigned long)-1)
62 #define OXYGEN_PCI_SUBID_BROKEN_EEPROM \
63 	OXYGEN_PCI_SUBID(PCI_VENDOR_ID_CMEDIA, 0x8788), \
64 	.driver_data = BROKEN_EEPROM_DRIVER_DATA
65 
66 struct pci_dev;
67 struct pci_device_id;
68 struct snd_card;
69 struct snd_pcm_substream;
70 struct snd_pcm_hardware;
71 struct snd_pcm_hw_params;
72 struct snd_kcontrol_new;
73 struct snd_rawmidi;
74 struct snd_info_buffer;
75 struct oxygen;
76 
77 struct oxygen_model {
78 	const char *shortname;
79 	const char *longname;
80 	const char *chip;
81 	void (*init)(struct oxygen *chip);
82 	int (*control_filter)(struct snd_kcontrol_new *template);
83 	int (*mixer_init)(struct oxygen *chip);
84 	void (*cleanup)(struct oxygen *chip);
85 	void (*suspend)(struct oxygen *chip);
86 	void (*resume)(struct oxygen *chip);
87 	void (*pcm_hardware_filter)(unsigned int channel,
88 				    struct snd_pcm_hardware *hardware);
89 	void (*set_dac_params)(struct oxygen *chip,
90 			       struct snd_pcm_hw_params *params);
91 	void (*set_adc_params)(struct oxygen *chip,
92 			       struct snd_pcm_hw_params *params);
93 	void (*update_dac_volume)(struct oxygen *chip);
94 	void (*update_dac_mute)(struct oxygen *chip);
95 	void (*update_center_lfe_mix)(struct oxygen *chip, bool mixed);
96 	unsigned int (*adjust_dac_routing)(struct oxygen *chip,
97 					   unsigned int play_routing);
98 	void (*gpio_changed)(struct oxygen *chip);
99 	void (*uart_input)(struct oxygen *chip);
100 	void (*ac97_switch)(struct oxygen *chip,
101 			    unsigned int reg, unsigned int mute);
102 	void (*dump_registers)(struct oxygen *chip,
103 			       struct snd_info_buffer *buffer);
104 	const unsigned int *dac_tlv;
105 	size_t model_data_size;
106 	unsigned int device_config;
107 	u8 dac_channels_pcm;
108 	u8 dac_channels_mixer;
109 	u8 dac_volume_min;
110 	u8 dac_volume_max;
111 	u8 misc_flags;
112 	u8 function_flags;
113 	u8 dac_mclks;
114 	u8 adc_mclks;
115 	u16 dac_i2s_format;
116 	u16 adc_i2s_format;
117 };
118 
119 struct oxygen {
120 	unsigned long addr;
121 	spinlock_t reg_lock;
122 	struct mutex mutex;
123 	struct snd_card *card;
124 	struct pci_dev *pci;
125 	struct snd_rawmidi *midi;
126 	int irq;
127 	void *model_data;
128 	unsigned int interrupt_mask;
129 	u8 dac_volume[8];
130 	u8 dac_mute;
131 	u8 pcm_active;
132 	u8 pcm_running;
133 	u8 dac_routing;
134 	u8 spdif_playback_enable;
135 	u8 has_ac97_0;
136 	u8 has_ac97_1;
137 	u32 spdif_bits;
138 	u32 spdif_pcm_bits;
139 	struct snd_pcm_substream *streams[PCM_COUNT];
140 	struct snd_kcontrol *controls[CONTROL_COUNT];
141 	struct work_struct spdif_input_bits_work;
142 	struct work_struct gpio_work;
143 	wait_queue_head_t ac97_waitqueue;
144 	union {
145 		u8 _8[OXYGEN_IO_SIZE];
146 		__le16 _16[OXYGEN_IO_SIZE / 2];
147 		__le32 _32[OXYGEN_IO_SIZE / 4];
148 	} saved_registers;
149 	u16 saved_ac97_registers[2][0x40];
150 	unsigned int uart_input_count;
151 	u8 uart_input[32];
152 	struct oxygen_model model;
153 };
154 
155 /* oxygen_lib.c */
156 
157 int oxygen_pci_probe(struct pci_dev *pci, int index, char *id,
158 		     struct module *owner,
159 		     const struct pci_device_id *ids,
160 		     int (*get_model)(struct oxygen *chip,
161 				      const struct pci_device_id *id
162 				     )
163 		    );
164 void oxygen_pci_remove(struct pci_dev *pci);
165 #ifdef CONFIG_PM_SLEEP
166 extern const struct dev_pm_ops oxygen_pci_pm;
167 #endif
168 void oxygen_pci_shutdown(struct pci_dev *pci);
169 
170 /* oxygen_mixer.c */
171 
172 int oxygen_mixer_init(struct oxygen *chip);
173 void oxygen_update_dac_routing(struct oxygen *chip);
174 void oxygen_update_spdif_source(struct oxygen *chip);
175 
176 /* oxygen_pcm.c */
177 
178 int oxygen_pcm_init(struct oxygen *chip);
179 
180 /* oxygen_io.c */
181 
182 u8 oxygen_read8(struct oxygen *chip, unsigned int reg);
183 u16 oxygen_read16(struct oxygen *chip, unsigned int reg);
184 u32 oxygen_read32(struct oxygen *chip, unsigned int reg);
185 void oxygen_write8(struct oxygen *chip, unsigned int reg, u8 value);
186 void oxygen_write16(struct oxygen *chip, unsigned int reg, u16 value);
187 void oxygen_write32(struct oxygen *chip, unsigned int reg, u32 value);
188 void oxygen_write8_masked(struct oxygen *chip, unsigned int reg,
189 			  u8 value, u8 mask);
190 void oxygen_write16_masked(struct oxygen *chip, unsigned int reg,
191 			   u16 value, u16 mask);
192 void oxygen_write32_masked(struct oxygen *chip, unsigned int reg,
193 			   u32 value, u32 mask);
194 
195 u16 oxygen_read_ac97(struct oxygen *chip, unsigned int codec,
196 		     unsigned int index);
197 void oxygen_write_ac97(struct oxygen *chip, unsigned int codec,
198 		       unsigned int index, u16 data);
199 void oxygen_write_ac97_masked(struct oxygen *chip, unsigned int codec,
200 			      unsigned int index, u16 data, u16 mask);
201 
202 int oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data);
203 void oxygen_write_i2c(struct oxygen *chip, u8 device, u8 map, u8 data);
204 
205 void oxygen_reset_uart(struct oxygen *chip);
206 void oxygen_write_uart(struct oxygen *chip, u8 data);
207 
208 u16 oxygen_read_eeprom(struct oxygen *chip, unsigned int index);
209 void oxygen_write_eeprom(struct oxygen *chip, unsigned int index, u16 value);
210 
211 static inline void oxygen_set_bits8(struct oxygen *chip,
212 				    unsigned int reg, u8 value)
213 {
214 	oxygen_write8_masked(chip, reg, value, value);
215 }
216 
217 static inline void oxygen_set_bits16(struct oxygen *chip,
218 				     unsigned int reg, u16 value)
219 {
220 	oxygen_write16_masked(chip, reg, value, value);
221 }
222 
223 static inline void oxygen_set_bits32(struct oxygen *chip,
224 				     unsigned int reg, u32 value)
225 {
226 	oxygen_write32_masked(chip, reg, value, value);
227 }
228 
229 static inline void oxygen_clear_bits8(struct oxygen *chip,
230 				      unsigned int reg, u8 value)
231 {
232 	oxygen_write8_masked(chip, reg, 0, value);
233 }
234 
235 static inline void oxygen_clear_bits16(struct oxygen *chip,
236 				       unsigned int reg, u16 value)
237 {
238 	oxygen_write16_masked(chip, reg, 0, value);
239 }
240 
241 static inline void oxygen_clear_bits32(struct oxygen *chip,
242 				       unsigned int reg, u32 value)
243 {
244 	oxygen_write32_masked(chip, reg, 0, value);
245 }
246 
247 static inline void oxygen_ac97_set_bits(struct oxygen *chip, unsigned int codec,
248 					unsigned int index, u16 value)
249 {
250 	oxygen_write_ac97_masked(chip, codec, index, value, value);
251 }
252 
253 static inline void oxygen_ac97_clear_bits(struct oxygen *chip,
254 					  unsigned int codec,
255 					  unsigned int index, u16 value)
256 {
257 	oxygen_write_ac97_masked(chip, codec, index, 0, value);
258 }
259 
260 #endif
261