xref: /openbmc/linux/sound/pci/oxygen/oxygen_io.c (revision 9c9cf6be)
19c9cf6beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d0ce9946SClemens Ladisch /*
3d0ce9946SClemens Ladisch  * C-Media CMI8788 driver - helper functions
4d0ce9946SClemens Ladisch  *
5d0ce9946SClemens Ladisch  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6d0ce9946SClemens Ladisch  */
7d0ce9946SClemens Ladisch 
8d0ce9946SClemens Ladisch #include <linux/delay.h>
9d0ce9946SClemens Ladisch #include <linux/sched.h>
10d81a6d71SPaul Gortmaker #include <linux/export.h>
116cbbfe1cSTakashi Iwai #include <linux/io.h>
12d0ce9946SClemens Ladisch #include <sound/core.h>
13397b1dccSClemens Ladisch #include <sound/mpu401.h>
14d0ce9946SClemens Ladisch #include "oxygen.h"
15d0ce9946SClemens Ladisch 
oxygen_read8(struct oxygen * chip,unsigned int reg)16d0ce9946SClemens Ladisch u8 oxygen_read8(struct oxygen *chip, unsigned int reg)
17d0ce9946SClemens Ladisch {
18d0ce9946SClemens Ladisch 	return inb(chip->addr + reg);
19d0ce9946SClemens Ladisch }
20d0ce9946SClemens Ladisch EXPORT_SYMBOL(oxygen_read8);
21d0ce9946SClemens Ladisch 
oxygen_read16(struct oxygen * chip,unsigned int reg)22d0ce9946SClemens Ladisch u16 oxygen_read16(struct oxygen *chip, unsigned int reg)
23d0ce9946SClemens Ladisch {
24d0ce9946SClemens Ladisch 	return inw(chip->addr + reg);
25d0ce9946SClemens Ladisch }
26d0ce9946SClemens Ladisch EXPORT_SYMBOL(oxygen_read16);
27d0ce9946SClemens Ladisch 
oxygen_read32(struct oxygen * chip,unsigned int reg)28d0ce9946SClemens Ladisch u32 oxygen_read32(struct oxygen *chip, unsigned int reg)
29d0ce9946SClemens Ladisch {
30d0ce9946SClemens Ladisch 	return inl(chip->addr + reg);
31d0ce9946SClemens Ladisch }
32d0ce9946SClemens Ladisch EXPORT_SYMBOL(oxygen_read32);
33d0ce9946SClemens Ladisch 
oxygen_write8(struct oxygen * chip,unsigned int reg,u8 value)34d0ce9946SClemens Ladisch void oxygen_write8(struct oxygen *chip, unsigned int reg, u8 value)
35d0ce9946SClemens Ladisch {
36d0ce9946SClemens Ladisch 	outb(value, chip->addr + reg);
37e58aee95SClemens Ladisch 	chip->saved_registers._8[reg] = value;
38d0ce9946SClemens Ladisch }
39d0ce9946SClemens Ladisch EXPORT_SYMBOL(oxygen_write8);
40d0ce9946SClemens Ladisch 
oxygen_write16(struct oxygen * chip,unsigned int reg,u16 value)41d0ce9946SClemens Ladisch void oxygen_write16(struct oxygen *chip, unsigned int reg, u16 value)
42d0ce9946SClemens Ladisch {
43d0ce9946SClemens Ladisch 	outw(value, chip->addr + reg);
44e58aee95SClemens Ladisch 	chip->saved_registers._16[reg / 2] = cpu_to_le16(value);
45d0ce9946SClemens Ladisch }
46d0ce9946SClemens Ladisch EXPORT_SYMBOL(oxygen_write16);
47d0ce9946SClemens Ladisch 
oxygen_write32(struct oxygen * chip,unsigned int reg,u32 value)48d0ce9946SClemens Ladisch void oxygen_write32(struct oxygen *chip, unsigned int reg, u32 value)
49d0ce9946SClemens Ladisch {
50d0ce9946SClemens Ladisch 	outl(value, chip->addr + reg);
51e58aee95SClemens Ladisch 	chip->saved_registers._32[reg / 4] = cpu_to_le32(value);
52d0ce9946SClemens Ladisch }
53d0ce9946SClemens Ladisch EXPORT_SYMBOL(oxygen_write32);
54d0ce9946SClemens Ladisch 
oxygen_write8_masked(struct oxygen * chip,unsigned int reg,u8 value,u8 mask)55d0ce9946SClemens Ladisch void oxygen_write8_masked(struct oxygen *chip, unsigned int reg,
56d0ce9946SClemens Ladisch 			  u8 value, u8 mask)
57d0ce9946SClemens Ladisch {
58d0ce9946SClemens Ladisch 	u8 tmp = inb(chip->addr + reg);
59e58aee95SClemens Ladisch 	tmp &= ~mask;
60e58aee95SClemens Ladisch 	tmp |= value & mask;
61e58aee95SClemens Ladisch 	outb(tmp, chip->addr + reg);
62e58aee95SClemens Ladisch 	chip->saved_registers._8[reg] = tmp;
63d0ce9946SClemens Ladisch }
64d0ce9946SClemens Ladisch EXPORT_SYMBOL(oxygen_write8_masked);
65d0ce9946SClemens Ladisch 
oxygen_write16_masked(struct oxygen * chip,unsigned int reg,u16 value,u16 mask)66d0ce9946SClemens Ladisch void oxygen_write16_masked(struct oxygen *chip, unsigned int reg,
67d0ce9946SClemens Ladisch 			   u16 value, u16 mask)
68d0ce9946SClemens Ladisch {
69d0ce9946SClemens Ladisch 	u16 tmp = inw(chip->addr + reg);
70e58aee95SClemens Ladisch 	tmp &= ~mask;
71e58aee95SClemens Ladisch 	tmp |= value & mask;
72e58aee95SClemens Ladisch 	outw(tmp, chip->addr + reg);
73e58aee95SClemens Ladisch 	chip->saved_registers._16[reg / 2] = cpu_to_le16(tmp);
74d0ce9946SClemens Ladisch }
75d0ce9946SClemens Ladisch EXPORT_SYMBOL(oxygen_write16_masked);
76d0ce9946SClemens Ladisch 
oxygen_write32_masked(struct oxygen * chip,unsigned int reg,u32 value,u32 mask)77d0ce9946SClemens Ladisch void oxygen_write32_masked(struct oxygen *chip, unsigned int reg,
78d0ce9946SClemens Ladisch 			   u32 value, u32 mask)
79d0ce9946SClemens Ladisch {
80d0ce9946SClemens Ladisch 	u32 tmp = inl(chip->addr + reg);
81e58aee95SClemens Ladisch 	tmp &= ~mask;
82e58aee95SClemens Ladisch 	tmp |= value & mask;
83e58aee95SClemens Ladisch 	outl(tmp, chip->addr + reg);
84e58aee95SClemens Ladisch 	chip->saved_registers._32[reg / 4] = cpu_to_le32(tmp);
85d0ce9946SClemens Ladisch }
86d0ce9946SClemens Ladisch EXPORT_SYMBOL(oxygen_write32_masked);
87d0ce9946SClemens Ladisch 
oxygen_ac97_wait(struct oxygen * chip,unsigned int mask)88d0ce9946SClemens Ladisch static int oxygen_ac97_wait(struct oxygen *chip, unsigned int mask)
89d0ce9946SClemens Ladisch {
901e821dd2SClemens Ladisch 	u8 status = 0;
911e821dd2SClemens Ladisch 
921e821dd2SClemens Ladisch 	/*
931e821dd2SClemens Ladisch 	 * Reading the status register also clears the bits, so we have to save
941e821dd2SClemens Ladisch 	 * the read bits in status.
951e821dd2SClemens Ladisch 	 */
961e821dd2SClemens Ladisch 	wait_event_timeout(chip->ac97_waitqueue,
971e821dd2SClemens Ladisch 			   ({ status |= oxygen_read8(chip, OXYGEN_AC97_INTERRUPT_STATUS);
981e821dd2SClemens Ladisch 			      status & mask; }),
991e821dd2SClemens Ladisch 			   msecs_to_jiffies(1) + 1);
1001e821dd2SClemens Ladisch 	/*
1011e821dd2SClemens Ladisch 	 * Check even after a timeout because this function should not require
1021e821dd2SClemens Ladisch 	 * the AC'97 interrupt to be enabled.
1031e821dd2SClemens Ladisch 	 */
1041e821dd2SClemens Ladisch 	status |= oxygen_read8(chip, OXYGEN_AC97_INTERRUPT_STATUS);
1051e821dd2SClemens Ladisch 	return status & mask ? 0 : -EIO;
106d0ce9946SClemens Ladisch }
107d0ce9946SClemens Ladisch 
108d0ce9946SClemens Ladisch /*
109d0ce9946SClemens Ladisch  * About 10% of AC'97 register reads or writes fail to complete, but even those
110d0ce9946SClemens Ladisch  * where the controller indicates completion aren't guaranteed to have actually
111d0ce9946SClemens Ladisch  * happened.
112d0ce9946SClemens Ladisch  *
113d0ce9946SClemens Ladisch  * It's hard to assign blame to either the controller or the codec because both
114d0ce9946SClemens Ladisch  * were made by C-Media ...
115d0ce9946SClemens Ladisch  */
116d0ce9946SClemens Ladisch 
oxygen_write_ac97(struct oxygen * chip,unsigned int codec,unsigned int index,u16 data)117d0ce9946SClemens Ladisch void oxygen_write_ac97(struct oxygen *chip, unsigned int codec,
118d0ce9946SClemens Ladisch 		       unsigned int index, u16 data)
119d0ce9946SClemens Ladisch {
120d0ce9946SClemens Ladisch 	unsigned int count, succeeded;
121d0ce9946SClemens Ladisch 	u32 reg;
122d0ce9946SClemens Ladisch 
123d0ce9946SClemens Ladisch 	reg = data;
124d0ce9946SClemens Ladisch 	reg |= index << OXYGEN_AC97_REG_ADDR_SHIFT;
125d0ce9946SClemens Ladisch 	reg |= OXYGEN_AC97_REG_DIR_WRITE;
126d0ce9946SClemens Ladisch 	reg |= codec << OXYGEN_AC97_REG_CODEC_SHIFT;
127d0ce9946SClemens Ladisch 	succeeded = 0;
128d0ce9946SClemens Ladisch 	for (count = 5; count > 0; --count) {
129d0ce9946SClemens Ladisch 		udelay(5);
130d0ce9946SClemens Ladisch 		oxygen_write32(chip, OXYGEN_AC97_REGS, reg);
131d0ce9946SClemens Ladisch 		/* require two "completed" writes, just to be sure */
132c2353a08SClemens Ladisch 		if (oxygen_ac97_wait(chip, OXYGEN_AC97_INT_WRITE_DONE) >= 0 &&
133e58aee95SClemens Ladisch 		    ++succeeded >= 2) {
134e58aee95SClemens Ladisch 			chip->saved_ac97_registers[codec][index / 2] = data;
135d0ce9946SClemens Ladisch 			return;
136d0ce9946SClemens Ladisch 		}
137e58aee95SClemens Ladisch 	}
13803d3ac21STakashi Iwai 	dev_err(chip->card->dev, "AC'97 write timeout\n");
139d0ce9946SClemens Ladisch }
140d0ce9946SClemens Ladisch EXPORT_SYMBOL(oxygen_write_ac97);
141d0ce9946SClemens Ladisch 
oxygen_read_ac97(struct oxygen * chip,unsigned int codec,unsigned int index)142d0ce9946SClemens Ladisch u16 oxygen_read_ac97(struct oxygen *chip, unsigned int codec,
143d0ce9946SClemens Ladisch 		     unsigned int index)
144d0ce9946SClemens Ladisch {
145d0ce9946SClemens Ladisch 	unsigned int count;
146d0ce9946SClemens Ladisch 	unsigned int last_read = UINT_MAX;
147d0ce9946SClemens Ladisch 	u32 reg;
148d0ce9946SClemens Ladisch 
149d0ce9946SClemens Ladisch 	reg = index << OXYGEN_AC97_REG_ADDR_SHIFT;
150d0ce9946SClemens Ladisch 	reg |= OXYGEN_AC97_REG_DIR_READ;
151d0ce9946SClemens Ladisch 	reg |= codec << OXYGEN_AC97_REG_CODEC_SHIFT;
152d0ce9946SClemens Ladisch 	for (count = 5; count > 0; --count) {
153d0ce9946SClemens Ladisch 		udelay(5);
154d0ce9946SClemens Ladisch 		oxygen_write32(chip, OXYGEN_AC97_REGS, reg);
155d0ce9946SClemens Ladisch 		udelay(10);
156c2353a08SClemens Ladisch 		if (oxygen_ac97_wait(chip, OXYGEN_AC97_INT_READ_DONE) >= 0) {
157d0ce9946SClemens Ladisch 			u16 value = oxygen_read16(chip, OXYGEN_AC97_REGS);
158d0ce9946SClemens Ladisch 			/* we require two consecutive reads of the same value */
159d0ce9946SClemens Ladisch 			if (value == last_read)
160d0ce9946SClemens Ladisch 				return value;
161d0ce9946SClemens Ladisch 			last_read = value;
162d0ce9946SClemens Ladisch 			/*
163d0ce9946SClemens Ladisch 			 * Invert the register value bits to make sure that two
164d0ce9946SClemens Ladisch 			 * consecutive unsuccessful reads do not return the same
165d0ce9946SClemens Ladisch 			 * value.
166d0ce9946SClemens Ladisch 			 */
167d0ce9946SClemens Ladisch 			reg ^= 0xffff;
168d0ce9946SClemens Ladisch 		}
169d0ce9946SClemens Ladisch 	}
17003d3ac21STakashi Iwai 	dev_err(chip->card->dev, "AC'97 read timeout on codec %u\n", codec);
171d0ce9946SClemens Ladisch 	return 0;
172d0ce9946SClemens Ladisch }
173d0ce9946SClemens Ladisch EXPORT_SYMBOL(oxygen_read_ac97);
174d0ce9946SClemens Ladisch 
oxygen_write_ac97_masked(struct oxygen * chip,unsigned int codec,unsigned int index,u16 data,u16 mask)175d0ce9946SClemens Ladisch void oxygen_write_ac97_masked(struct oxygen *chip, unsigned int codec,
176d0ce9946SClemens Ladisch 			      unsigned int index, u16 data, u16 mask)
177d0ce9946SClemens Ladisch {
178d0ce9946SClemens Ladisch 	u16 value = oxygen_read_ac97(chip, codec, index);
179d0ce9946SClemens Ladisch 	value &= ~mask;
180d0ce9946SClemens Ladisch 	value |= data & mask;
181d0ce9946SClemens Ladisch 	oxygen_write_ac97(chip, codec, index, value);
182d0ce9946SClemens Ladisch }
183d0ce9946SClemens Ladisch EXPORT_SYMBOL(oxygen_write_ac97_masked);
184d0ce9946SClemens Ladisch 
oxygen_wait_spi(struct oxygen * chip)18510dd44dcSRoman Volkov static int oxygen_wait_spi(struct oxygen *chip)
18610dd44dcSRoman Volkov {
18710dd44dcSRoman Volkov 	unsigned int count;
18810dd44dcSRoman Volkov 
18910dd44dcSRoman Volkov 	/*
19010dd44dcSRoman Volkov 	 * Higher timeout to be sure: 200 us;
19110dd44dcSRoman Volkov 	 * actual transaction should not need more than 40 us.
19210dd44dcSRoman Volkov 	 */
19310dd44dcSRoman Volkov 	for (count = 50; count > 0; count--) {
19410dd44dcSRoman Volkov 		udelay(4);
19510dd44dcSRoman Volkov 		if ((oxygen_read8(chip, OXYGEN_SPI_CONTROL) &
19610dd44dcSRoman Volkov 						OXYGEN_SPI_BUSY) == 0)
19710dd44dcSRoman Volkov 			return 0;
19810dd44dcSRoman Volkov 	}
19903d3ac21STakashi Iwai 	dev_err(chip->card->dev, "oxygen: SPI wait timeout\n");
20010dd44dcSRoman Volkov 	return -EIO;
20110dd44dcSRoman Volkov }
20210dd44dcSRoman Volkov 
oxygen_write_spi(struct oxygen * chip,u8 control,unsigned int data)203303cff30SRoman Volkov int oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data)
204d0ce9946SClemens Ladisch {
205303cff30SRoman Volkov 	/*
206303cff30SRoman Volkov 	 * We need to wait AFTER initiating the SPI transaction,
207303cff30SRoman Volkov 	 * otherwise read operations will not work.
208303cff30SRoman Volkov 	 */
209d0ce9946SClemens Ladisch 	oxygen_write8(chip, OXYGEN_SPI_DATA1, data);
210d0ce9946SClemens Ladisch 	oxygen_write8(chip, OXYGEN_SPI_DATA2, data >> 8);
211d0ce9946SClemens Ladisch 	if (control & OXYGEN_SPI_DATA_LENGTH_3)
212d0ce9946SClemens Ladisch 		oxygen_write8(chip, OXYGEN_SPI_DATA3, data >> 16);
213d0ce9946SClemens Ladisch 	oxygen_write8(chip, OXYGEN_SPI_CONTROL, control);
214303cff30SRoman Volkov 	return oxygen_wait_spi(chip);
215d0ce9946SClemens Ladisch }
216d0ce9946SClemens Ladisch EXPORT_SYMBOL(oxygen_write_spi);
21710e6d5f9SClemens Ladisch 
oxygen_write_i2c(struct oxygen * chip,u8 device,u8 map,u8 data)21810e6d5f9SClemens Ladisch void oxygen_write_i2c(struct oxygen *chip, u8 device, u8 map, u8 data)
21910e6d5f9SClemens Ladisch {
22010e6d5f9SClemens Ladisch 	/* should not need more than about 300 us */
221f1bc07afSClemens Ladisch 	msleep(1);
22210e6d5f9SClemens Ladisch 
22310e6d5f9SClemens Ladisch 	oxygen_write8(chip, OXYGEN_2WIRE_MAP, map);
22410e6d5f9SClemens Ladisch 	oxygen_write8(chip, OXYGEN_2WIRE_DATA, data);
22510e6d5f9SClemens Ladisch 	oxygen_write8(chip, OXYGEN_2WIRE_CONTROL,
22610e6d5f9SClemens Ladisch 		      device | OXYGEN_2WIRE_DIR_WRITE);
22710e6d5f9SClemens Ladisch }
22810e6d5f9SClemens Ladisch EXPORT_SYMBOL(oxygen_write_i2c);
229397b1dccSClemens Ladisch 
_write_uart(struct oxygen * chip,unsigned int port,u8 data)230397b1dccSClemens Ladisch static void _write_uart(struct oxygen *chip, unsigned int port, u8 data)
231397b1dccSClemens Ladisch {
232397b1dccSClemens Ladisch 	if (oxygen_read8(chip, OXYGEN_MPU401 + 1) & MPU401_TX_FULL)
233397b1dccSClemens Ladisch 		msleep(1);
234397b1dccSClemens Ladisch 	oxygen_write8(chip, OXYGEN_MPU401 + port, data);
235397b1dccSClemens Ladisch }
236397b1dccSClemens Ladisch 
oxygen_reset_uart(struct oxygen * chip)237397b1dccSClemens Ladisch void oxygen_reset_uart(struct oxygen *chip)
238397b1dccSClemens Ladisch {
239397b1dccSClemens Ladisch 	_write_uart(chip, 1, MPU401_RESET);
240cdad5b8dSClemens Ladisch 	msleep(1); /* wait for ACK */
241397b1dccSClemens Ladisch 	_write_uart(chip, 1, MPU401_ENTER_UART);
242397b1dccSClemens Ladisch }
243397b1dccSClemens Ladisch EXPORT_SYMBOL(oxygen_reset_uart);
244397b1dccSClemens Ladisch 
oxygen_write_uart(struct oxygen * chip,u8 data)245397b1dccSClemens Ladisch void oxygen_write_uart(struct oxygen *chip, u8 data)
246397b1dccSClemens Ladisch {
247397b1dccSClemens Ladisch 	_write_uart(chip, 0, data);
248397b1dccSClemens Ladisch }
249397b1dccSClemens Ladisch EXPORT_SYMBOL(oxygen_write_uart);
25030459d7bSClemens Ladisch 
oxygen_read_eeprom(struct oxygen * chip,unsigned int index)25130459d7bSClemens Ladisch u16 oxygen_read_eeprom(struct oxygen *chip, unsigned int index)
25230459d7bSClemens Ladisch {
25330459d7bSClemens Ladisch 	unsigned int timeout;
25430459d7bSClemens Ladisch 
25530459d7bSClemens Ladisch 	oxygen_write8(chip, OXYGEN_EEPROM_CONTROL,
25630459d7bSClemens Ladisch 		      index | OXYGEN_EEPROM_DIR_READ);
25730459d7bSClemens Ladisch 	for (timeout = 0; timeout < 100; ++timeout) {
25830459d7bSClemens Ladisch 		udelay(1);
25930459d7bSClemens Ladisch 		if (!(oxygen_read8(chip, OXYGEN_EEPROM_STATUS)
26030459d7bSClemens Ladisch 		      & OXYGEN_EEPROM_BUSY))
26130459d7bSClemens Ladisch 			break;
26230459d7bSClemens Ladisch 	}
26330459d7bSClemens Ladisch 	return oxygen_read16(chip, OXYGEN_EEPROM_DATA);
26430459d7bSClemens Ladisch }
2651275d6f6SClemens Ladisch 
oxygen_write_eeprom(struct oxygen * chip,unsigned int index,u16 value)2661275d6f6SClemens Ladisch void oxygen_write_eeprom(struct oxygen *chip, unsigned int index, u16 value)
2671275d6f6SClemens Ladisch {
2681275d6f6SClemens Ladisch 	unsigned int timeout;
2691275d6f6SClemens Ladisch 
2701275d6f6SClemens Ladisch 	oxygen_write16(chip, OXYGEN_EEPROM_DATA, value);
2711275d6f6SClemens Ladisch 	oxygen_write8(chip, OXYGEN_EEPROM_CONTROL,
2721275d6f6SClemens Ladisch 		      index | OXYGEN_EEPROM_DIR_WRITE);
2731275d6f6SClemens Ladisch 	for (timeout = 0; timeout < 10; ++timeout) {
2741275d6f6SClemens Ladisch 		msleep(1);
2751275d6f6SClemens Ladisch 		if (!(oxygen_read8(chip, OXYGEN_EEPROM_STATUS)
2761275d6f6SClemens Ladisch 		      & OXYGEN_EEPROM_BUSY))
2771275d6f6SClemens Ladisch 			return;
2781275d6f6SClemens Ladisch 	}
27903d3ac21STakashi Iwai 	dev_err(chip->card->dev, "EEPROM write timeout\n");
2801275d6f6SClemens Ladisch }
281