162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * C-Media CMI8788 driver - helper functions 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#include <linux/delay.h> 962306a36Sopenharmony_ci#include <linux/sched.h> 1062306a36Sopenharmony_ci#include <linux/export.h> 1162306a36Sopenharmony_ci#include <linux/io.h> 1262306a36Sopenharmony_ci#include <sound/core.h> 1362306a36Sopenharmony_ci#include <sound/mpu401.h> 1462306a36Sopenharmony_ci#include "oxygen.h" 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ciu8 oxygen_read8(struct oxygen *chip, unsigned int reg) 1762306a36Sopenharmony_ci{ 1862306a36Sopenharmony_ci return inb(chip->addr + reg); 1962306a36Sopenharmony_ci} 2062306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_read8); 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ciu16 oxygen_read16(struct oxygen *chip, unsigned int reg) 2362306a36Sopenharmony_ci{ 2462306a36Sopenharmony_ci return inw(chip->addr + reg); 2562306a36Sopenharmony_ci} 2662306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_read16); 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ciu32 oxygen_read32(struct oxygen *chip, unsigned int reg) 2962306a36Sopenharmony_ci{ 3062306a36Sopenharmony_ci return inl(chip->addr + reg); 3162306a36Sopenharmony_ci} 3262306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_read32); 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_civoid oxygen_write8(struct oxygen *chip, unsigned int reg, u8 value) 3562306a36Sopenharmony_ci{ 3662306a36Sopenharmony_ci outb(value, chip->addr + reg); 3762306a36Sopenharmony_ci chip->saved_registers._8[reg] = value; 3862306a36Sopenharmony_ci} 3962306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_write8); 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_civoid oxygen_write16(struct oxygen *chip, unsigned int reg, u16 value) 4262306a36Sopenharmony_ci{ 4362306a36Sopenharmony_ci outw(value, chip->addr + reg); 4462306a36Sopenharmony_ci chip->saved_registers._16[reg / 2] = cpu_to_le16(value); 4562306a36Sopenharmony_ci} 4662306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_write16); 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_civoid oxygen_write32(struct oxygen *chip, unsigned int reg, u32 value) 4962306a36Sopenharmony_ci{ 5062306a36Sopenharmony_ci outl(value, chip->addr + reg); 5162306a36Sopenharmony_ci chip->saved_registers._32[reg / 4] = cpu_to_le32(value); 5262306a36Sopenharmony_ci} 5362306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_write32); 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_civoid oxygen_write8_masked(struct oxygen *chip, unsigned int reg, 5662306a36Sopenharmony_ci u8 value, u8 mask) 5762306a36Sopenharmony_ci{ 5862306a36Sopenharmony_ci u8 tmp = inb(chip->addr + reg); 5962306a36Sopenharmony_ci tmp &= ~mask; 6062306a36Sopenharmony_ci tmp |= value & mask; 6162306a36Sopenharmony_ci outb(tmp, chip->addr + reg); 6262306a36Sopenharmony_ci chip->saved_registers._8[reg] = tmp; 6362306a36Sopenharmony_ci} 6462306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_write8_masked); 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_civoid oxygen_write16_masked(struct oxygen *chip, unsigned int reg, 6762306a36Sopenharmony_ci u16 value, u16 mask) 6862306a36Sopenharmony_ci{ 6962306a36Sopenharmony_ci u16 tmp = inw(chip->addr + reg); 7062306a36Sopenharmony_ci tmp &= ~mask; 7162306a36Sopenharmony_ci tmp |= value & mask; 7262306a36Sopenharmony_ci outw(tmp, chip->addr + reg); 7362306a36Sopenharmony_ci chip->saved_registers._16[reg / 2] = cpu_to_le16(tmp); 7462306a36Sopenharmony_ci} 7562306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_write16_masked); 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_civoid oxygen_write32_masked(struct oxygen *chip, unsigned int reg, 7862306a36Sopenharmony_ci u32 value, u32 mask) 7962306a36Sopenharmony_ci{ 8062306a36Sopenharmony_ci u32 tmp = inl(chip->addr + reg); 8162306a36Sopenharmony_ci tmp &= ~mask; 8262306a36Sopenharmony_ci tmp |= value & mask; 8362306a36Sopenharmony_ci outl(tmp, chip->addr + reg); 8462306a36Sopenharmony_ci chip->saved_registers._32[reg / 4] = cpu_to_le32(tmp); 8562306a36Sopenharmony_ci} 8662306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_write32_masked); 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_cistatic int oxygen_ac97_wait(struct oxygen *chip, unsigned int mask) 8962306a36Sopenharmony_ci{ 9062306a36Sopenharmony_ci u8 status = 0; 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci /* 9362306a36Sopenharmony_ci * Reading the status register also clears the bits, so we have to save 9462306a36Sopenharmony_ci * the read bits in status. 9562306a36Sopenharmony_ci */ 9662306a36Sopenharmony_ci wait_event_timeout(chip->ac97_waitqueue, 9762306a36Sopenharmony_ci ({ status |= oxygen_read8(chip, OXYGEN_AC97_INTERRUPT_STATUS); 9862306a36Sopenharmony_ci status & mask; }), 9962306a36Sopenharmony_ci msecs_to_jiffies(1) + 1); 10062306a36Sopenharmony_ci /* 10162306a36Sopenharmony_ci * Check even after a timeout because this function should not require 10262306a36Sopenharmony_ci * the AC'97 interrupt to be enabled. 10362306a36Sopenharmony_ci */ 10462306a36Sopenharmony_ci status |= oxygen_read8(chip, OXYGEN_AC97_INTERRUPT_STATUS); 10562306a36Sopenharmony_ci return status & mask ? 0 : -EIO; 10662306a36Sopenharmony_ci} 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci/* 10962306a36Sopenharmony_ci * About 10% of AC'97 register reads or writes fail to complete, but even those 11062306a36Sopenharmony_ci * where the controller indicates completion aren't guaranteed to have actually 11162306a36Sopenharmony_ci * happened. 11262306a36Sopenharmony_ci * 11362306a36Sopenharmony_ci * It's hard to assign blame to either the controller or the codec because both 11462306a36Sopenharmony_ci * were made by C-Media ... 11562306a36Sopenharmony_ci */ 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_civoid oxygen_write_ac97(struct oxygen *chip, unsigned int codec, 11862306a36Sopenharmony_ci unsigned int index, u16 data) 11962306a36Sopenharmony_ci{ 12062306a36Sopenharmony_ci unsigned int count, succeeded; 12162306a36Sopenharmony_ci u32 reg; 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci reg = data; 12462306a36Sopenharmony_ci reg |= index << OXYGEN_AC97_REG_ADDR_SHIFT; 12562306a36Sopenharmony_ci reg |= OXYGEN_AC97_REG_DIR_WRITE; 12662306a36Sopenharmony_ci reg |= codec << OXYGEN_AC97_REG_CODEC_SHIFT; 12762306a36Sopenharmony_ci succeeded = 0; 12862306a36Sopenharmony_ci for (count = 5; count > 0; --count) { 12962306a36Sopenharmony_ci udelay(5); 13062306a36Sopenharmony_ci oxygen_write32(chip, OXYGEN_AC97_REGS, reg); 13162306a36Sopenharmony_ci /* require two "completed" writes, just to be sure */ 13262306a36Sopenharmony_ci if (oxygen_ac97_wait(chip, OXYGEN_AC97_INT_WRITE_DONE) >= 0 && 13362306a36Sopenharmony_ci ++succeeded >= 2) { 13462306a36Sopenharmony_ci chip->saved_ac97_registers[codec][index / 2] = data; 13562306a36Sopenharmony_ci return; 13662306a36Sopenharmony_ci } 13762306a36Sopenharmony_ci } 13862306a36Sopenharmony_ci dev_err(chip->card->dev, "AC'97 write timeout\n"); 13962306a36Sopenharmony_ci} 14062306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_write_ac97); 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ciu16 oxygen_read_ac97(struct oxygen *chip, unsigned int codec, 14362306a36Sopenharmony_ci unsigned int index) 14462306a36Sopenharmony_ci{ 14562306a36Sopenharmony_ci unsigned int count; 14662306a36Sopenharmony_ci unsigned int last_read = UINT_MAX; 14762306a36Sopenharmony_ci u32 reg; 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci reg = index << OXYGEN_AC97_REG_ADDR_SHIFT; 15062306a36Sopenharmony_ci reg |= OXYGEN_AC97_REG_DIR_READ; 15162306a36Sopenharmony_ci reg |= codec << OXYGEN_AC97_REG_CODEC_SHIFT; 15262306a36Sopenharmony_ci for (count = 5; count > 0; --count) { 15362306a36Sopenharmony_ci udelay(5); 15462306a36Sopenharmony_ci oxygen_write32(chip, OXYGEN_AC97_REGS, reg); 15562306a36Sopenharmony_ci udelay(10); 15662306a36Sopenharmony_ci if (oxygen_ac97_wait(chip, OXYGEN_AC97_INT_READ_DONE) >= 0) { 15762306a36Sopenharmony_ci u16 value = oxygen_read16(chip, OXYGEN_AC97_REGS); 15862306a36Sopenharmony_ci /* we require two consecutive reads of the same value */ 15962306a36Sopenharmony_ci if (value == last_read) 16062306a36Sopenharmony_ci return value; 16162306a36Sopenharmony_ci last_read = value; 16262306a36Sopenharmony_ci /* 16362306a36Sopenharmony_ci * Invert the register value bits to make sure that two 16462306a36Sopenharmony_ci * consecutive unsuccessful reads do not return the same 16562306a36Sopenharmony_ci * value. 16662306a36Sopenharmony_ci */ 16762306a36Sopenharmony_ci reg ^= 0xffff; 16862306a36Sopenharmony_ci } 16962306a36Sopenharmony_ci } 17062306a36Sopenharmony_ci dev_err(chip->card->dev, "AC'97 read timeout on codec %u\n", codec); 17162306a36Sopenharmony_ci return 0; 17262306a36Sopenharmony_ci} 17362306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_read_ac97); 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_civoid oxygen_write_ac97_masked(struct oxygen *chip, unsigned int codec, 17662306a36Sopenharmony_ci unsigned int index, u16 data, u16 mask) 17762306a36Sopenharmony_ci{ 17862306a36Sopenharmony_ci u16 value = oxygen_read_ac97(chip, codec, index); 17962306a36Sopenharmony_ci value &= ~mask; 18062306a36Sopenharmony_ci value |= data & mask; 18162306a36Sopenharmony_ci oxygen_write_ac97(chip, codec, index, value); 18262306a36Sopenharmony_ci} 18362306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_write_ac97_masked); 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_cistatic int oxygen_wait_spi(struct oxygen *chip) 18662306a36Sopenharmony_ci{ 18762306a36Sopenharmony_ci unsigned int count; 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci /* 19062306a36Sopenharmony_ci * Higher timeout to be sure: 200 us; 19162306a36Sopenharmony_ci * actual transaction should not need more than 40 us. 19262306a36Sopenharmony_ci */ 19362306a36Sopenharmony_ci for (count = 50; count > 0; count--) { 19462306a36Sopenharmony_ci udelay(4); 19562306a36Sopenharmony_ci if ((oxygen_read8(chip, OXYGEN_SPI_CONTROL) & 19662306a36Sopenharmony_ci OXYGEN_SPI_BUSY) == 0) 19762306a36Sopenharmony_ci return 0; 19862306a36Sopenharmony_ci } 19962306a36Sopenharmony_ci dev_err(chip->card->dev, "oxygen: SPI wait timeout\n"); 20062306a36Sopenharmony_ci return -EIO; 20162306a36Sopenharmony_ci} 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ciint oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data) 20462306a36Sopenharmony_ci{ 20562306a36Sopenharmony_ci /* 20662306a36Sopenharmony_ci * We need to wait AFTER initiating the SPI transaction, 20762306a36Sopenharmony_ci * otherwise read operations will not work. 20862306a36Sopenharmony_ci */ 20962306a36Sopenharmony_ci oxygen_write8(chip, OXYGEN_SPI_DATA1, data); 21062306a36Sopenharmony_ci oxygen_write8(chip, OXYGEN_SPI_DATA2, data >> 8); 21162306a36Sopenharmony_ci if (control & OXYGEN_SPI_DATA_LENGTH_3) 21262306a36Sopenharmony_ci oxygen_write8(chip, OXYGEN_SPI_DATA3, data >> 16); 21362306a36Sopenharmony_ci oxygen_write8(chip, OXYGEN_SPI_CONTROL, control); 21462306a36Sopenharmony_ci return oxygen_wait_spi(chip); 21562306a36Sopenharmony_ci} 21662306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_write_spi); 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_civoid oxygen_write_i2c(struct oxygen *chip, u8 device, u8 map, u8 data) 21962306a36Sopenharmony_ci{ 22062306a36Sopenharmony_ci /* should not need more than about 300 us */ 22162306a36Sopenharmony_ci msleep(1); 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci oxygen_write8(chip, OXYGEN_2WIRE_MAP, map); 22462306a36Sopenharmony_ci oxygen_write8(chip, OXYGEN_2WIRE_DATA, data); 22562306a36Sopenharmony_ci oxygen_write8(chip, OXYGEN_2WIRE_CONTROL, 22662306a36Sopenharmony_ci device | OXYGEN_2WIRE_DIR_WRITE); 22762306a36Sopenharmony_ci} 22862306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_write_i2c); 22962306a36Sopenharmony_ci 23062306a36Sopenharmony_cistatic void _write_uart(struct oxygen *chip, unsigned int port, u8 data) 23162306a36Sopenharmony_ci{ 23262306a36Sopenharmony_ci if (oxygen_read8(chip, OXYGEN_MPU401 + 1) & MPU401_TX_FULL) 23362306a36Sopenharmony_ci msleep(1); 23462306a36Sopenharmony_ci oxygen_write8(chip, OXYGEN_MPU401 + port, data); 23562306a36Sopenharmony_ci} 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_civoid oxygen_reset_uart(struct oxygen *chip) 23862306a36Sopenharmony_ci{ 23962306a36Sopenharmony_ci _write_uart(chip, 1, MPU401_RESET); 24062306a36Sopenharmony_ci msleep(1); /* wait for ACK */ 24162306a36Sopenharmony_ci _write_uart(chip, 1, MPU401_ENTER_UART); 24262306a36Sopenharmony_ci} 24362306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_reset_uart); 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_civoid oxygen_write_uart(struct oxygen *chip, u8 data) 24662306a36Sopenharmony_ci{ 24762306a36Sopenharmony_ci _write_uart(chip, 0, data); 24862306a36Sopenharmony_ci} 24962306a36Sopenharmony_ciEXPORT_SYMBOL(oxygen_write_uart); 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_ciu16 oxygen_read_eeprom(struct oxygen *chip, unsigned int index) 25262306a36Sopenharmony_ci{ 25362306a36Sopenharmony_ci unsigned int timeout; 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ci oxygen_write8(chip, OXYGEN_EEPROM_CONTROL, 25662306a36Sopenharmony_ci index | OXYGEN_EEPROM_DIR_READ); 25762306a36Sopenharmony_ci for (timeout = 0; timeout < 100; ++timeout) { 25862306a36Sopenharmony_ci udelay(1); 25962306a36Sopenharmony_ci if (!(oxygen_read8(chip, OXYGEN_EEPROM_STATUS) 26062306a36Sopenharmony_ci & OXYGEN_EEPROM_BUSY)) 26162306a36Sopenharmony_ci break; 26262306a36Sopenharmony_ci } 26362306a36Sopenharmony_ci return oxygen_read16(chip, OXYGEN_EEPROM_DATA); 26462306a36Sopenharmony_ci} 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_civoid oxygen_write_eeprom(struct oxygen *chip, unsigned int index, u16 value) 26762306a36Sopenharmony_ci{ 26862306a36Sopenharmony_ci unsigned int timeout; 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_ci oxygen_write16(chip, OXYGEN_EEPROM_DATA, value); 27162306a36Sopenharmony_ci oxygen_write8(chip, OXYGEN_EEPROM_CONTROL, 27262306a36Sopenharmony_ci index | OXYGEN_EEPROM_DIR_WRITE); 27362306a36Sopenharmony_ci for (timeout = 0; timeout < 10; ++timeout) { 27462306a36Sopenharmony_ci msleep(1); 27562306a36Sopenharmony_ci if (!(oxygen_read8(chip, OXYGEN_EEPROM_STATUS) 27662306a36Sopenharmony_ci & OXYGEN_EEPROM_BUSY)) 27762306a36Sopenharmony_ci return; 27862306a36Sopenharmony_ci } 27962306a36Sopenharmony_ci dev_err(chip->card->dev, "EEPROM write timeout\n"); 28062306a36Sopenharmony_ci} 281