1 // SPDX-License-Identifier: GPL-2.0-only
3 * C-Media CMI8788 driver - helper functions
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
8 #include <linux/delay.h>
9 #include <linux/sched.h>
10 #include <linux/export.h>
12 #include <sound/core.h>
13 #include <sound/mpu401.h>
16 u8
oxygen_read8(struct oxygen
*chip
, unsigned int reg
)
18 return inb(chip
->addr
+ reg
);
20 EXPORT_SYMBOL(oxygen_read8
);
22 u16
oxygen_read16(struct oxygen
*chip
, unsigned int reg
)
24 return inw(chip
->addr
+ reg
);
26 EXPORT_SYMBOL(oxygen_read16
);
28 u32
oxygen_read32(struct oxygen
*chip
, unsigned int reg
)
30 return inl(chip
->addr
+ reg
);
32 EXPORT_SYMBOL(oxygen_read32
);
34 void oxygen_write8(struct oxygen
*chip
, unsigned int reg
, u8 value
)
36 outb(value
, chip
->addr
+ reg
);
37 chip
->saved_registers
._8
[reg
] = value
;
39 EXPORT_SYMBOL(oxygen_write8
);
41 void oxygen_write16(struct oxygen
*chip
, unsigned int reg
, u16 value
)
43 outw(value
, chip
->addr
+ reg
);
44 chip
->saved_registers
._16
[reg
/ 2] = cpu_to_le16(value
);
46 EXPORT_SYMBOL(oxygen_write16
);
48 void oxygen_write32(struct oxygen
*chip
, unsigned int reg
, u32 value
)
50 outl(value
, chip
->addr
+ reg
);
51 chip
->saved_registers
._32
[reg
/ 4] = cpu_to_le32(value
);
53 EXPORT_SYMBOL(oxygen_write32
);
55 void oxygen_write8_masked(struct oxygen
*chip
, unsigned int reg
,
58 u8 tmp
= inb(chip
->addr
+ reg
);
61 outb(tmp
, chip
->addr
+ reg
);
62 chip
->saved_registers
._8
[reg
] = tmp
;
64 EXPORT_SYMBOL(oxygen_write8_masked
);
66 void oxygen_write16_masked(struct oxygen
*chip
, unsigned int reg
,
69 u16 tmp
= inw(chip
->addr
+ reg
);
72 outw(tmp
, chip
->addr
+ reg
);
73 chip
->saved_registers
._16
[reg
/ 2] = cpu_to_le16(tmp
);
75 EXPORT_SYMBOL(oxygen_write16_masked
);
77 void oxygen_write32_masked(struct oxygen
*chip
, unsigned int reg
,
80 u32 tmp
= inl(chip
->addr
+ reg
);
83 outl(tmp
, chip
->addr
+ reg
);
84 chip
->saved_registers
._32
[reg
/ 4] = cpu_to_le32(tmp
);
86 EXPORT_SYMBOL(oxygen_write32_masked
);
88 static int oxygen_ac97_wait(struct oxygen
*chip
, unsigned int mask
)
93 * Reading the status register also clears the bits, so we have to save
94 * the read bits in status.
96 wait_event_timeout(chip
->ac97_waitqueue
,
97 ({ status
|= oxygen_read8(chip
, OXYGEN_AC97_INTERRUPT_STATUS
);
99 msecs_to_jiffies(1) + 1);
101 * Check even after a timeout because this function should not require
102 * the AC'97 interrupt to be enabled.
104 status
|= oxygen_read8(chip
, OXYGEN_AC97_INTERRUPT_STATUS
);
105 return status
& mask
? 0 : -EIO
;
109 * About 10% of AC'97 register reads or writes fail to complete, but even those
110 * where the controller indicates completion aren't guaranteed to have actually
113 * It's hard to assign blame to either the controller or the codec because both
114 * were made by C-Media ...
117 void oxygen_write_ac97(struct oxygen
*chip
, unsigned int codec
,
118 unsigned int index
, u16 data
)
120 unsigned int count
, succeeded
;
124 reg
|= index
<< OXYGEN_AC97_REG_ADDR_SHIFT
;
125 reg
|= OXYGEN_AC97_REG_DIR_WRITE
;
126 reg
|= codec
<< OXYGEN_AC97_REG_CODEC_SHIFT
;
128 for (count
= 5; count
> 0; --count
) {
130 oxygen_write32(chip
, OXYGEN_AC97_REGS
, reg
);
131 /* require two "completed" writes, just to be sure */
132 if (oxygen_ac97_wait(chip
, OXYGEN_AC97_INT_WRITE_DONE
) >= 0 &&
134 chip
->saved_ac97_registers
[codec
][index
/ 2] = data
;
138 dev_err(chip
->card
->dev
, "AC'97 write timeout\n");
140 EXPORT_SYMBOL(oxygen_write_ac97
);
142 u16
oxygen_read_ac97(struct oxygen
*chip
, unsigned int codec
,
146 unsigned int last_read
= UINT_MAX
;
149 reg
= index
<< OXYGEN_AC97_REG_ADDR_SHIFT
;
150 reg
|= OXYGEN_AC97_REG_DIR_READ
;
151 reg
|= codec
<< OXYGEN_AC97_REG_CODEC_SHIFT
;
152 for (count
= 5; count
> 0; --count
) {
154 oxygen_write32(chip
, OXYGEN_AC97_REGS
, reg
);
156 if (oxygen_ac97_wait(chip
, OXYGEN_AC97_INT_READ_DONE
) >= 0) {
157 u16 value
= oxygen_read16(chip
, OXYGEN_AC97_REGS
);
158 /* we require two consecutive reads of the same value */
159 if (value
== last_read
)
163 * Invert the register value bits to make sure that two
164 * consecutive unsuccessful reads do not return the same
170 dev_err(chip
->card
->dev
, "AC'97 read timeout on codec %u\n", codec
);
173 EXPORT_SYMBOL(oxygen_read_ac97
);
175 void oxygen_write_ac97_masked(struct oxygen
*chip
, unsigned int codec
,
176 unsigned int index
, u16 data
, u16 mask
)
178 u16 value
= oxygen_read_ac97(chip
, codec
, index
);
180 value
|= data
& mask
;
181 oxygen_write_ac97(chip
, codec
, index
, value
);
183 EXPORT_SYMBOL(oxygen_write_ac97_masked
);
185 static int oxygen_wait_spi(struct oxygen
*chip
)
190 * Higher timeout to be sure: 200 us;
191 * actual transaction should not need more than 40 us.
193 for (count
= 50; count
> 0; count
--) {
195 if ((oxygen_read8(chip
, OXYGEN_SPI_CONTROL
) &
196 OXYGEN_SPI_BUSY
) == 0)
199 dev_err(chip
->card
->dev
, "oxygen: SPI wait timeout\n");
203 int oxygen_write_spi(struct oxygen
*chip
, u8 control
, unsigned int data
)
206 * We need to wait AFTER initiating the SPI transaction,
207 * otherwise read operations will not work.
209 oxygen_write8(chip
, OXYGEN_SPI_DATA1
, data
);
210 oxygen_write8(chip
, OXYGEN_SPI_DATA2
, data
>> 8);
211 if (control
& OXYGEN_SPI_DATA_LENGTH_3
)
212 oxygen_write8(chip
, OXYGEN_SPI_DATA3
, data
>> 16);
213 oxygen_write8(chip
, OXYGEN_SPI_CONTROL
, control
);
214 return oxygen_wait_spi(chip
);
216 EXPORT_SYMBOL(oxygen_write_spi
);
218 void oxygen_write_i2c(struct oxygen
*chip
, u8 device
, u8 map
, u8 data
)
220 /* should not need more than about 300 us */
223 oxygen_write8(chip
, OXYGEN_2WIRE_MAP
, map
);
224 oxygen_write8(chip
, OXYGEN_2WIRE_DATA
, data
);
225 oxygen_write8(chip
, OXYGEN_2WIRE_CONTROL
,
226 device
| OXYGEN_2WIRE_DIR_WRITE
);
228 EXPORT_SYMBOL(oxygen_write_i2c
);
230 static void _write_uart(struct oxygen
*chip
, unsigned int port
, u8 data
)
232 if (oxygen_read8(chip
, OXYGEN_MPU401
+ 1) & MPU401_TX_FULL
)
234 oxygen_write8(chip
, OXYGEN_MPU401
+ port
, data
);
237 void oxygen_reset_uart(struct oxygen
*chip
)
239 _write_uart(chip
, 1, MPU401_RESET
);
240 msleep(1); /* wait for ACK */
241 _write_uart(chip
, 1, MPU401_ENTER_UART
);
243 EXPORT_SYMBOL(oxygen_reset_uart
);
245 void oxygen_write_uart(struct oxygen
*chip
, u8 data
)
247 _write_uart(chip
, 0, data
);
249 EXPORT_SYMBOL(oxygen_write_uart
);
251 u16
oxygen_read_eeprom(struct oxygen
*chip
, unsigned int index
)
253 unsigned int timeout
;
255 oxygen_write8(chip
, OXYGEN_EEPROM_CONTROL
,
256 index
| OXYGEN_EEPROM_DIR_READ
);
257 for (timeout
= 0; timeout
< 100; ++timeout
) {
259 if (!(oxygen_read8(chip
, OXYGEN_EEPROM_STATUS
)
260 & OXYGEN_EEPROM_BUSY
))
263 return oxygen_read16(chip
, OXYGEN_EEPROM_DATA
);
266 void oxygen_write_eeprom(struct oxygen
*chip
, unsigned int index
, u16 value
)
268 unsigned int timeout
;
270 oxygen_write16(chip
, OXYGEN_EEPROM_DATA
, value
);
271 oxygen_write8(chip
, OXYGEN_EEPROM_CONTROL
,
272 index
| OXYGEN_EEPROM_DIR_WRITE
);
273 for (timeout
= 0; timeout
< 10; ++timeout
) {
275 if (!(oxygen_read8(chip
, OXYGEN_EEPROM_STATUS
)
276 & OXYGEN_EEPROM_BUSY
))
279 dev_err(chip
->card
->dev
, "EEPROM write timeout\n");