1*227f609cSHerve Codina // SPDX-License-Identifier: GPL-2.0 2*227f609cSHerve Codina // 3*227f609cSHerve Codina // peb2466.c -- Infineon PEB2466 ALSA SoC driver 4*227f609cSHerve Codina // 5*227f609cSHerve Codina // Copyright 2023 CS GROUP France 6*227f609cSHerve Codina // 7*227f609cSHerve Codina // Author: Herve Codina <herve.codina@bootlin.com> 8*227f609cSHerve Codina 9*227f609cSHerve Codina #include <asm/unaligned.h> 10*227f609cSHerve Codina #include <linux/clk.h> 11*227f609cSHerve Codina #include <linux/firmware.h> 12*227f609cSHerve Codina #include <linux/gpio/consumer.h> 13*227f609cSHerve Codina #include <linux/gpio/driver.h> 14*227f609cSHerve Codina #include <linux/module.h> 15*227f609cSHerve Codina #include <linux/mutex.h> 16*227f609cSHerve Codina #include <linux/slab.h> 17*227f609cSHerve Codina #include <linux/spi/spi.h> 18*227f609cSHerve Codina #include <sound/pcm_params.h> 19*227f609cSHerve Codina #include <sound/soc.h> 20*227f609cSHerve Codina #include <sound/tlv.h> 21*227f609cSHerve Codina 22*227f609cSHerve Codina #define PEB2466_NB_CHANNEL 4 23*227f609cSHerve Codina 24*227f609cSHerve Codina struct peb2466_lookup { 25*227f609cSHerve Codina u8 (*table)[4]; 26*227f609cSHerve Codina unsigned int count; 27*227f609cSHerve Codina }; 28*227f609cSHerve Codina 29*227f609cSHerve Codina #define PEB2466_TLV_SIZE (sizeof((unsigned int []){TLV_DB_SCALE_ITEM(0, 0, 0)}) / \ 30*227f609cSHerve Codina sizeof(unsigned int)) 31*227f609cSHerve Codina 32*227f609cSHerve Codina struct peb2466_lkup_ctrl { 33*227f609cSHerve Codina int reg; 34*227f609cSHerve Codina unsigned int index; 35*227f609cSHerve Codina const struct peb2466_lookup *lookup; 36*227f609cSHerve Codina unsigned int tlv_array[PEB2466_TLV_SIZE]; 37*227f609cSHerve Codina }; 38*227f609cSHerve Codina 39*227f609cSHerve Codina struct peb2466 { 40*227f609cSHerve Codina struct spi_device *spi; 41*227f609cSHerve Codina struct clk *mclk; 42*227f609cSHerve Codina struct gpio_desc *reset_gpio; 43*227f609cSHerve Codina u8 spi_tx_buf[2 + 8]; /* Cannot use stack area for SPI (dma-safe memory) */ 44*227f609cSHerve Codina u8 spi_rx_buf[2 + 8]; /* Cannot use stack area for SPI (dma-safe memory) */ 45*227f609cSHerve Codina struct regmap *regmap; 46*227f609cSHerve Codina struct { 47*227f609cSHerve Codina struct peb2466_lookup ax_lookup; 48*227f609cSHerve Codina struct peb2466_lookup ar_lookup; 49*227f609cSHerve Codina struct peb2466_lkup_ctrl ax_lkup_ctrl; 50*227f609cSHerve Codina struct peb2466_lkup_ctrl ar_lkup_ctrl; 51*227f609cSHerve Codina unsigned int tg1_freq_item; 52*227f609cSHerve Codina unsigned int tg2_freq_item; 53*227f609cSHerve Codina } ch[PEB2466_NB_CHANNEL]; 54*227f609cSHerve Codina int max_chan_playback; 55*227f609cSHerve Codina int max_chan_capture; 56*227f609cSHerve Codina struct { 57*227f609cSHerve Codina struct gpio_chip gpio_chip; 58*227f609cSHerve Codina struct mutex lock; 59*227f609cSHerve Codina struct { 60*227f609cSHerve Codina unsigned int xr0; 61*227f609cSHerve Codina unsigned int xr1; 62*227f609cSHerve Codina unsigned int xr2; 63*227f609cSHerve Codina unsigned int xr3; 64*227f609cSHerve Codina } cache; 65*227f609cSHerve Codina } gpio; 66*227f609cSHerve Codina }; 67*227f609cSHerve Codina 68*227f609cSHerve Codina #define PEB2466_CMD_R (1 << 5) 69*227f609cSHerve Codina #define PEB2466_CMD_W (0 << 5) 70*227f609cSHerve Codina 71*227f609cSHerve Codina #define PEB2466_CMD_MASK 0x18 72*227f609cSHerve Codina #define PEB2466_CMD_XOP 0x18 /* XOP is 0bxxx11xxx */ 73*227f609cSHerve Codina #define PEB2466_CMD_SOP 0x10 /* SOP is 0bxxx10xxx */ 74*227f609cSHerve Codina #define PEB2466_CMD_COP 0x00 /* COP is 0bxxx0xxxx, handle 0bxxx00xxx */ 75*227f609cSHerve Codina #define PEB2466_CMD_COP1 0x08 /* COP is 0bxxx0xxxx, handle 0bxxx01xxx */ 76*227f609cSHerve Codina 77*227f609cSHerve Codina #define PEB2466_MAKE_XOP(_lsel) (PEB2466_CMD_XOP | (_lsel)) 78*227f609cSHerve Codina #define PEB2466_MAKE_SOP(_ad, _lsel) (PEB2466_CMD_SOP | ((_ad) << 6) | (_lsel)) 79*227f609cSHerve Codina #define PEB2466_MAKE_COP(_ad, _code) (PEB2466_CMD_COP | ((_ad) << 6) | (_code)) 80*227f609cSHerve Codina 81*227f609cSHerve Codina #define PEB2466_CR0(_ch) PEB2466_MAKE_SOP(_ch, 0x0) 82*227f609cSHerve Codina #define PEB2466_CR0_TH (1 << 7) 83*227f609cSHerve Codina #define PEB2466_CR0_IMR1 (1 << 6) 84*227f609cSHerve Codina #define PEB2466_CR0_FRX (1 << 5) 85*227f609cSHerve Codina #define PEB2466_CR0_FRR (1 << 4) 86*227f609cSHerve Codina #define PEB2466_CR0_AX (1 << 3) 87*227f609cSHerve Codina #define PEB2466_CR0_AR (1 << 2) 88*227f609cSHerve Codina #define PEB2466_CR0_THSEL_MASK (0x3 << 0) 89*227f609cSHerve Codina #define PEB2466_CR0_THSEL(_set) ((_set) << 0) 90*227f609cSHerve Codina 91*227f609cSHerve Codina #define PEB2466_CR1(_ch) PEB2466_MAKE_SOP(_ch, 0x1) 92*227f609cSHerve Codina #define PEB2466_CR1_ETG2 (1 << 7) 93*227f609cSHerve Codina #define PEB2466_CR1_ETG1 (1 << 6) 94*227f609cSHerve Codina #define PEB2466_CR1_PTG2 (1 << 5) 95*227f609cSHerve Codina #define PEB2466_CR1_PTG1 (1 << 4) 96*227f609cSHerve Codina #define PEB2466_CR1_LAW_MASK (1 << 3) 97*227f609cSHerve Codina #define PEB2466_CR1_LAW_ALAW (0 << 3) 98*227f609cSHerve Codina #define PEB2466_CR1_LAW_MULAW (1 << 3) 99*227f609cSHerve Codina #define PEB2466_CR1_PU (1 << 0) 100*227f609cSHerve Codina 101*227f609cSHerve Codina #define PEB2466_CR2(_ch) PEB2466_MAKE_SOP(_ch, 0x2) 102*227f609cSHerve Codina #define PEB2466_CR3(_ch) PEB2466_MAKE_SOP(_ch, 0x3) 103*227f609cSHerve Codina #define PEB2466_CR4(_ch) PEB2466_MAKE_SOP(_ch, 0x4) 104*227f609cSHerve Codina #define PEB2466_CR5(_ch) PEB2466_MAKE_SOP(_ch, 0x5) 105*227f609cSHerve Codina 106*227f609cSHerve Codina #define PEB2466_XR0 PEB2466_MAKE_XOP(0x0) 107*227f609cSHerve Codina #define PEB2466_XR1 PEB2466_MAKE_XOP(0x1) 108*227f609cSHerve Codina #define PEB2466_XR2 PEB2466_MAKE_XOP(0x2) 109*227f609cSHerve Codina #define PEB2466_XR3 PEB2466_MAKE_XOP(0x3) 110*227f609cSHerve Codina #define PEB2466_XR4 PEB2466_MAKE_XOP(0x4) 111*227f609cSHerve Codina #define PEB2466_XR5 PEB2466_MAKE_XOP(0x5) 112*227f609cSHerve Codina #define PEB2466_XR5_MCLK_1536 (0x0 << 6) 113*227f609cSHerve Codina #define PEB2466_XR5_MCLK_2048 (0x1 << 6) 114*227f609cSHerve Codina #define PEB2466_XR5_MCLK_4096 (0x2 << 6) 115*227f609cSHerve Codina #define PEB2466_XR5_MCLK_8192 (0x3 << 6) 116*227f609cSHerve Codina 117*227f609cSHerve Codina #define PEB2466_XR6 PEB2466_MAKE_XOP(0x6) 118*227f609cSHerve Codina #define PEB2466_XR6_PCM_OFFSET(_off) ((_off) << 0) 119*227f609cSHerve Codina 120*227f609cSHerve Codina #define PEB2466_XR7 PEB2466_MAKE_XOP(0x7) 121*227f609cSHerve Codina 122*227f609cSHerve Codina #define PEB2466_TH_FILTER_P1(_ch) PEB2466_MAKE_COP(_ch, 0x0) 123*227f609cSHerve Codina #define PEB2466_TH_FILTER_P2(_ch) PEB2466_MAKE_COP(_ch, 0x1) 124*227f609cSHerve Codina #define PEB2466_TH_FILTER_P3(_ch) PEB2466_MAKE_COP(_ch, 0x2) 125*227f609cSHerve Codina #define PEB2466_IMR1_FILTER_P1(_ch) PEB2466_MAKE_COP(_ch, 0x4) 126*227f609cSHerve Codina #define PEB2466_IMR1_FILTER_P2(_ch) PEB2466_MAKE_COP(_ch, 0x5) 127*227f609cSHerve Codina #define PEB2466_FRX_FILTER(_ch) PEB2466_MAKE_COP(_ch, 0x6) 128*227f609cSHerve Codina #define PEB2466_FRR_FILTER(_ch) PEB2466_MAKE_COP(_ch, 0x7) 129*227f609cSHerve Codina #define PEB2466_AX_FILTER(_ch) PEB2466_MAKE_COP(_ch, 0x8) 130*227f609cSHerve Codina #define PEB2466_AR_FILTER(_ch) PEB2466_MAKE_COP(_ch, 0x9) 131*227f609cSHerve Codina #define PEB2466_TG1(_ch) PEB2466_MAKE_COP(_ch, 0xc) 132*227f609cSHerve Codina #define PEB2466_TG2(_ch) PEB2466_MAKE_COP(_ch, 0xd) 133*227f609cSHerve Codina 134*227f609cSHerve Codina static int peb2466_write_byte(struct peb2466 *peb2466, u8 cmd, u8 val) 135*227f609cSHerve Codina { 136*227f609cSHerve Codina struct spi_transfer xfer = { 137*227f609cSHerve Codina .tx_buf = &peb2466->spi_tx_buf, 138*227f609cSHerve Codina .len = 2, 139*227f609cSHerve Codina }; 140*227f609cSHerve Codina 141*227f609cSHerve Codina peb2466->spi_tx_buf[0] = cmd | PEB2466_CMD_W; 142*227f609cSHerve Codina peb2466->spi_tx_buf[1] = val; 143*227f609cSHerve Codina 144*227f609cSHerve Codina dev_dbg(&peb2466->spi->dev, "write byte (cmd %02x) %02x\n", 145*227f609cSHerve Codina peb2466->spi_tx_buf[0], peb2466->spi_tx_buf[1]); 146*227f609cSHerve Codina 147*227f609cSHerve Codina return spi_sync_transfer(peb2466->spi, &xfer, 1); 148*227f609cSHerve Codina } 149*227f609cSHerve Codina 150*227f609cSHerve Codina static int peb2466_read_byte(struct peb2466 *peb2466, u8 cmd, u8 *val) 151*227f609cSHerve Codina { 152*227f609cSHerve Codina struct spi_transfer xfer = { 153*227f609cSHerve Codina .tx_buf = &peb2466->spi_tx_buf, 154*227f609cSHerve Codina .rx_buf = &peb2466->spi_rx_buf, 155*227f609cSHerve Codina .len = 3, 156*227f609cSHerve Codina }; 157*227f609cSHerve Codina int ret; 158*227f609cSHerve Codina 159*227f609cSHerve Codina peb2466->spi_tx_buf[0] = cmd | PEB2466_CMD_R; 160*227f609cSHerve Codina 161*227f609cSHerve Codina ret = spi_sync_transfer(peb2466->spi, &xfer, 1); 162*227f609cSHerve Codina if (ret) 163*227f609cSHerve Codina return ret; 164*227f609cSHerve Codina 165*227f609cSHerve Codina if (peb2466->spi_rx_buf[1] != 0x81) { 166*227f609cSHerve Codina dev_err(&peb2466->spi->dev, 167*227f609cSHerve Codina "spi xfer rd (cmd %02x) invalid ident byte (0x%02x)\n", 168*227f609cSHerve Codina peb2466->spi_tx_buf[0], peb2466->spi_rx_buf[1]); 169*227f609cSHerve Codina return -EILSEQ; 170*227f609cSHerve Codina } 171*227f609cSHerve Codina 172*227f609cSHerve Codina *val = peb2466->spi_rx_buf[2]; 173*227f609cSHerve Codina 174*227f609cSHerve Codina dev_dbg(&peb2466->spi->dev, "read byte (cmd %02x) %02x\n", 175*227f609cSHerve Codina peb2466->spi_tx_buf[0], *val); 176*227f609cSHerve Codina 177*227f609cSHerve Codina return 0; 178*227f609cSHerve Codina } 179*227f609cSHerve Codina 180*227f609cSHerve Codina static int peb2466_write_buf(struct peb2466 *peb2466, u8 cmd, const u8 *buf, unsigned int len) 181*227f609cSHerve Codina { 182*227f609cSHerve Codina struct spi_transfer xfer = { 183*227f609cSHerve Codina .tx_buf = &peb2466->spi_tx_buf, 184*227f609cSHerve Codina .len = len + 1, 185*227f609cSHerve Codina }; 186*227f609cSHerve Codina 187*227f609cSHerve Codina if (len > 8) 188*227f609cSHerve Codina return -EINVAL; 189*227f609cSHerve Codina 190*227f609cSHerve Codina peb2466->spi_tx_buf[0] = cmd | PEB2466_CMD_W; 191*227f609cSHerve Codina memcpy(&peb2466->spi_tx_buf[1], buf, len); 192*227f609cSHerve Codina 193*227f609cSHerve Codina dev_dbg(&peb2466->spi->dev, "write buf (cmd %02x, %u) %*ph\n", 194*227f609cSHerve Codina peb2466->spi_tx_buf[0], len, len, &peb2466->spi_tx_buf[1]); 195*227f609cSHerve Codina 196*227f609cSHerve Codina return spi_sync_transfer(peb2466->spi, &xfer, 1); 197*227f609cSHerve Codina } 198*227f609cSHerve Codina 199*227f609cSHerve Codina static int peb2466_reg_write(void *context, unsigned int reg, unsigned int val) 200*227f609cSHerve Codina { 201*227f609cSHerve Codina struct peb2466 *peb2466 = context; 202*227f609cSHerve Codina int ret; 203*227f609cSHerve Codina 204*227f609cSHerve Codina /* 205*227f609cSHerve Codina * Only XOP and SOP commands can be handled as registers. 206*227f609cSHerve Codina * COP commands are handled using direct peb2466_write_buf() calls. 207*227f609cSHerve Codina */ 208*227f609cSHerve Codina switch (reg & PEB2466_CMD_MASK) { 209*227f609cSHerve Codina case PEB2466_CMD_XOP: 210*227f609cSHerve Codina case PEB2466_CMD_SOP: 211*227f609cSHerve Codina ret = peb2466_write_byte(peb2466, reg, val); 212*227f609cSHerve Codina break; 213*227f609cSHerve Codina default: 214*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "Not a XOP or SOP command\n"); 215*227f609cSHerve Codina ret = -EINVAL; 216*227f609cSHerve Codina break; 217*227f609cSHerve Codina } 218*227f609cSHerve Codina return ret; 219*227f609cSHerve Codina } 220*227f609cSHerve Codina 221*227f609cSHerve Codina static int peb2466_reg_read(void *context, unsigned int reg, unsigned int *val) 222*227f609cSHerve Codina { 223*227f609cSHerve Codina struct peb2466 *peb2466 = context; 224*227f609cSHerve Codina int ret; 225*227f609cSHerve Codina u8 tmp; 226*227f609cSHerve Codina 227*227f609cSHerve Codina /* Only XOP and SOP commands can be handled as registers */ 228*227f609cSHerve Codina switch (reg & PEB2466_CMD_MASK) { 229*227f609cSHerve Codina case PEB2466_CMD_XOP: 230*227f609cSHerve Codina case PEB2466_CMD_SOP: 231*227f609cSHerve Codina ret = peb2466_read_byte(peb2466, reg, &tmp); 232*227f609cSHerve Codina *val = tmp; 233*227f609cSHerve Codina break; 234*227f609cSHerve Codina default: 235*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "Not a XOP or SOP command\n"); 236*227f609cSHerve Codina ret = -EINVAL; 237*227f609cSHerve Codina break; 238*227f609cSHerve Codina } 239*227f609cSHerve Codina return ret; 240*227f609cSHerve Codina } 241*227f609cSHerve Codina 242*227f609cSHerve Codina static const struct regmap_config peb2466_regmap_config = { 243*227f609cSHerve Codina .reg_bits = 8, 244*227f609cSHerve Codina .val_bits = 8, 245*227f609cSHerve Codina .max_register = 0xFF, 246*227f609cSHerve Codina .reg_write = peb2466_reg_write, 247*227f609cSHerve Codina .reg_read = peb2466_reg_read, 248*227f609cSHerve Codina .cache_type = REGCACHE_NONE, 249*227f609cSHerve Codina }; 250*227f609cSHerve Codina 251*227f609cSHerve Codina static int peb2466_lkup_ctrl_info(struct snd_kcontrol *kcontrol, 252*227f609cSHerve Codina struct snd_ctl_elem_info *uinfo) 253*227f609cSHerve Codina { 254*227f609cSHerve Codina struct peb2466_lkup_ctrl *lkup_ctrl = 255*227f609cSHerve Codina (struct peb2466_lkup_ctrl *)kcontrol->private_value; 256*227f609cSHerve Codina 257*227f609cSHerve Codina uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 258*227f609cSHerve Codina uinfo->count = 1; 259*227f609cSHerve Codina uinfo->value.integer.min = 0; 260*227f609cSHerve Codina uinfo->value.integer.max = lkup_ctrl->lookup->count - 1; 261*227f609cSHerve Codina return 0; 262*227f609cSHerve Codina } 263*227f609cSHerve Codina 264*227f609cSHerve Codina static int peb2466_lkup_ctrl_get(struct snd_kcontrol *kcontrol, 265*227f609cSHerve Codina struct snd_ctl_elem_value *ucontrol) 266*227f609cSHerve Codina { 267*227f609cSHerve Codina struct peb2466_lkup_ctrl *lkup_ctrl = 268*227f609cSHerve Codina (struct peb2466_lkup_ctrl *)kcontrol->private_value; 269*227f609cSHerve Codina 270*227f609cSHerve Codina ucontrol->value.integer.value[0] = lkup_ctrl->index; 271*227f609cSHerve Codina return 0; 272*227f609cSHerve Codina } 273*227f609cSHerve Codina 274*227f609cSHerve Codina static int peb2466_lkup_ctrl_put(struct snd_kcontrol *kcontrol, 275*227f609cSHerve Codina struct snd_ctl_elem_value *ucontrol) 276*227f609cSHerve Codina { 277*227f609cSHerve Codina struct peb2466_lkup_ctrl *lkup_ctrl = 278*227f609cSHerve Codina (struct peb2466_lkup_ctrl *)kcontrol->private_value; 279*227f609cSHerve Codina struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 280*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component); 281*227f609cSHerve Codina unsigned int index; 282*227f609cSHerve Codina int ret; 283*227f609cSHerve Codina 284*227f609cSHerve Codina index = ucontrol->value.integer.value[0]; 285*227f609cSHerve Codina if (index >= lkup_ctrl->lookup->count) 286*227f609cSHerve Codina return -EINVAL; 287*227f609cSHerve Codina 288*227f609cSHerve Codina if (index == lkup_ctrl->index) 289*227f609cSHerve Codina return 0; 290*227f609cSHerve Codina 291*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, lkup_ctrl->reg, 292*227f609cSHerve Codina lkup_ctrl->lookup->table[index], 4); 293*227f609cSHerve Codina if (ret) 294*227f609cSHerve Codina return ret; 295*227f609cSHerve Codina 296*227f609cSHerve Codina lkup_ctrl->index = index; 297*227f609cSHerve Codina return 1; /* The value changed */ 298*227f609cSHerve Codina } 299*227f609cSHerve Codina 300*227f609cSHerve Codina static int peb2466_add_lkup_ctrl(struct snd_soc_component *component, 301*227f609cSHerve Codina struct peb2466_lkup_ctrl *lkup_ctrl, 302*227f609cSHerve Codina const char *name, int min_val, int step) 303*227f609cSHerve Codina { 304*227f609cSHerve Codina DECLARE_TLV_DB_SCALE(tlv_array, min_val, step, 0); 305*227f609cSHerve Codina struct snd_kcontrol_new control = {0}; 306*227f609cSHerve Codina 307*227f609cSHerve Codina BUILD_BUG_ON(sizeof(lkup_ctrl->tlv_array) < sizeof(tlv_array)); 308*227f609cSHerve Codina memcpy(lkup_ctrl->tlv_array, tlv_array, sizeof(tlv_array)); 309*227f609cSHerve Codina 310*227f609cSHerve Codina control.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 311*227f609cSHerve Codina control.name = name; 312*227f609cSHerve Codina control.access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | 313*227f609cSHerve Codina SNDRV_CTL_ELEM_ACCESS_READWRITE; 314*227f609cSHerve Codina control.tlv.p = lkup_ctrl->tlv_array; 315*227f609cSHerve Codina control.info = peb2466_lkup_ctrl_info; 316*227f609cSHerve Codina control.get = peb2466_lkup_ctrl_get; 317*227f609cSHerve Codina control.put = peb2466_lkup_ctrl_put; 318*227f609cSHerve Codina control.private_value = (unsigned long)lkup_ctrl; 319*227f609cSHerve Codina 320*227f609cSHerve Codina return snd_soc_add_component_controls(component, &control, 1); 321*227f609cSHerve Codina } 322*227f609cSHerve Codina 323*227f609cSHerve Codina enum peb2466_tone_freq { 324*227f609cSHerve Codina PEB2466_TONE_697HZ, 325*227f609cSHerve Codina PEB2466_TONE_800HZ, 326*227f609cSHerve Codina PEB2466_TONE_950HZ, 327*227f609cSHerve Codina PEB2466_TONE_1000HZ, 328*227f609cSHerve Codina PEB2466_TONE_1008HZ, 329*227f609cSHerve Codina PEB2466_TONE_2000HZ, 330*227f609cSHerve Codina }; 331*227f609cSHerve Codina 332*227f609cSHerve Codina static const u8 peb2466_tone_lookup[][4] = { 333*227f609cSHerve Codina [PEB2466_TONE_697HZ] = {0x0a, 0x33, 0x5a, 0x2c}, 334*227f609cSHerve Codina [PEB2466_TONE_800HZ] = {0x12, 0xD6, 0x5a, 0xc0}, 335*227f609cSHerve Codina [PEB2466_TONE_950HZ] = {0x1c, 0xf0, 0x5c, 0xc0}, 336*227f609cSHerve Codina [PEB2466_TONE_1000HZ] = {0}, /* lookup value not used for 1000Hz */ 337*227f609cSHerve Codina [PEB2466_TONE_1008HZ] = {0x1a, 0xae, 0x57, 0x70}, 338*227f609cSHerve Codina [PEB2466_TONE_2000HZ] = {0x00, 0x80, 0x50, 0x09}, 339*227f609cSHerve Codina }; 340*227f609cSHerve Codina 341*227f609cSHerve Codina static const char * const peb2466_tone_freq_txt[] = { 342*227f609cSHerve Codina [PEB2466_TONE_697HZ] = "697Hz", 343*227f609cSHerve Codina [PEB2466_TONE_800HZ] = "800Hz", 344*227f609cSHerve Codina [PEB2466_TONE_950HZ] = "950Hz", 345*227f609cSHerve Codina [PEB2466_TONE_1000HZ] = "1000Hz", 346*227f609cSHerve Codina [PEB2466_TONE_1008HZ] = "1008Hz", 347*227f609cSHerve Codina [PEB2466_TONE_2000HZ] = "2000Hz" 348*227f609cSHerve Codina }; 349*227f609cSHerve Codina 350*227f609cSHerve Codina static const struct soc_enum peb2466_tg_freq[][2] = { 351*227f609cSHerve Codina [0] = { 352*227f609cSHerve Codina SOC_ENUM_SINGLE(PEB2466_TG1(0), 0, ARRAY_SIZE(peb2466_tone_freq_txt), 353*227f609cSHerve Codina peb2466_tone_freq_txt), 354*227f609cSHerve Codina SOC_ENUM_SINGLE(PEB2466_TG2(0), 0, ARRAY_SIZE(peb2466_tone_freq_txt), 355*227f609cSHerve Codina peb2466_tone_freq_txt) 356*227f609cSHerve Codina }, 357*227f609cSHerve Codina [1] = { 358*227f609cSHerve Codina SOC_ENUM_SINGLE(PEB2466_TG1(1), 0, ARRAY_SIZE(peb2466_tone_freq_txt), 359*227f609cSHerve Codina peb2466_tone_freq_txt), 360*227f609cSHerve Codina SOC_ENUM_SINGLE(PEB2466_TG2(1), 0, ARRAY_SIZE(peb2466_tone_freq_txt), 361*227f609cSHerve Codina peb2466_tone_freq_txt) 362*227f609cSHerve Codina }, 363*227f609cSHerve Codina [2] = { 364*227f609cSHerve Codina SOC_ENUM_SINGLE(PEB2466_TG1(2), 0, ARRAY_SIZE(peb2466_tone_freq_txt), 365*227f609cSHerve Codina peb2466_tone_freq_txt), 366*227f609cSHerve Codina SOC_ENUM_SINGLE(PEB2466_TG2(2), 0, ARRAY_SIZE(peb2466_tone_freq_txt), 367*227f609cSHerve Codina peb2466_tone_freq_txt) 368*227f609cSHerve Codina }, 369*227f609cSHerve Codina [3] = { 370*227f609cSHerve Codina SOC_ENUM_SINGLE(PEB2466_TG1(3), 0, ARRAY_SIZE(peb2466_tone_freq_txt), 371*227f609cSHerve Codina peb2466_tone_freq_txt), 372*227f609cSHerve Codina SOC_ENUM_SINGLE(PEB2466_TG2(3), 0, ARRAY_SIZE(peb2466_tone_freq_txt), 373*227f609cSHerve Codina peb2466_tone_freq_txt) 374*227f609cSHerve Codina } 375*227f609cSHerve Codina }; 376*227f609cSHerve Codina 377*227f609cSHerve Codina static int peb2466_tg_freq_get(struct snd_kcontrol *kcontrol, 378*227f609cSHerve Codina struct snd_ctl_elem_value *ucontrol) 379*227f609cSHerve Codina { 380*227f609cSHerve Codina struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 381*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component); 382*227f609cSHerve Codina struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 383*227f609cSHerve Codina 384*227f609cSHerve Codina switch (e->reg) { 385*227f609cSHerve Codina case PEB2466_TG1(0): 386*227f609cSHerve Codina ucontrol->value.enumerated.item[0] = peb2466->ch[0].tg1_freq_item; 387*227f609cSHerve Codina break; 388*227f609cSHerve Codina case PEB2466_TG2(0): 389*227f609cSHerve Codina ucontrol->value.enumerated.item[0] = peb2466->ch[0].tg2_freq_item; 390*227f609cSHerve Codina break; 391*227f609cSHerve Codina case PEB2466_TG1(1): 392*227f609cSHerve Codina ucontrol->value.enumerated.item[0] = peb2466->ch[1].tg1_freq_item; 393*227f609cSHerve Codina break; 394*227f609cSHerve Codina case PEB2466_TG2(1): 395*227f609cSHerve Codina ucontrol->value.enumerated.item[0] = peb2466->ch[1].tg2_freq_item; 396*227f609cSHerve Codina break; 397*227f609cSHerve Codina case PEB2466_TG1(2): 398*227f609cSHerve Codina ucontrol->value.enumerated.item[0] = peb2466->ch[2].tg1_freq_item; 399*227f609cSHerve Codina break; 400*227f609cSHerve Codina case PEB2466_TG2(2): 401*227f609cSHerve Codina ucontrol->value.enumerated.item[0] = peb2466->ch[2].tg2_freq_item; 402*227f609cSHerve Codina break; 403*227f609cSHerve Codina case PEB2466_TG1(3): 404*227f609cSHerve Codina ucontrol->value.enumerated.item[0] = peb2466->ch[3].tg1_freq_item; 405*227f609cSHerve Codina break; 406*227f609cSHerve Codina case PEB2466_TG2(3): 407*227f609cSHerve Codina ucontrol->value.enumerated.item[0] = peb2466->ch[3].tg2_freq_item; 408*227f609cSHerve Codina break; 409*227f609cSHerve Codina default: 410*227f609cSHerve Codina return -EINVAL; 411*227f609cSHerve Codina } 412*227f609cSHerve Codina return 0; 413*227f609cSHerve Codina } 414*227f609cSHerve Codina 415*227f609cSHerve Codina static int peb2466_tg_freq_put(struct snd_kcontrol *kcontrol, 416*227f609cSHerve Codina struct snd_ctl_elem_value *ucontrol) 417*227f609cSHerve Codina { 418*227f609cSHerve Codina struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 419*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component); 420*227f609cSHerve Codina struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 421*227f609cSHerve Codina unsigned int *tg_freq_item; 422*227f609cSHerve Codina u8 cr1_reg, cr1_mask; 423*227f609cSHerve Codina unsigned int index; 424*227f609cSHerve Codina int ret; 425*227f609cSHerve Codina 426*227f609cSHerve Codina index = ucontrol->value.enumerated.item[0]; 427*227f609cSHerve Codina 428*227f609cSHerve Codina if (index >= ARRAY_SIZE(peb2466_tone_lookup)) 429*227f609cSHerve Codina return -EINVAL; 430*227f609cSHerve Codina 431*227f609cSHerve Codina switch (e->reg) { 432*227f609cSHerve Codina case PEB2466_TG1(0): 433*227f609cSHerve Codina tg_freq_item = &peb2466->ch[0].tg1_freq_item; 434*227f609cSHerve Codina cr1_reg = PEB2466_CR1(0); 435*227f609cSHerve Codina cr1_mask = PEB2466_CR1_PTG1; 436*227f609cSHerve Codina break; 437*227f609cSHerve Codina case PEB2466_TG2(0): 438*227f609cSHerve Codina tg_freq_item = &peb2466->ch[0].tg2_freq_item; 439*227f609cSHerve Codina cr1_reg = PEB2466_CR1(0); 440*227f609cSHerve Codina cr1_mask = PEB2466_CR1_PTG2; 441*227f609cSHerve Codina break; 442*227f609cSHerve Codina case PEB2466_TG1(1): 443*227f609cSHerve Codina tg_freq_item = &peb2466->ch[1].tg1_freq_item; 444*227f609cSHerve Codina cr1_reg = PEB2466_CR1(1); 445*227f609cSHerve Codina cr1_mask = PEB2466_CR1_PTG1; 446*227f609cSHerve Codina break; 447*227f609cSHerve Codina case PEB2466_TG2(1): 448*227f609cSHerve Codina tg_freq_item = &peb2466->ch[1].tg2_freq_item; 449*227f609cSHerve Codina cr1_reg = PEB2466_CR1(1); 450*227f609cSHerve Codina cr1_mask = PEB2466_CR1_PTG2; 451*227f609cSHerve Codina break; 452*227f609cSHerve Codina case PEB2466_TG1(2): 453*227f609cSHerve Codina tg_freq_item = &peb2466->ch[2].tg1_freq_item; 454*227f609cSHerve Codina cr1_reg = PEB2466_CR1(2); 455*227f609cSHerve Codina cr1_mask = PEB2466_CR1_PTG1; 456*227f609cSHerve Codina break; 457*227f609cSHerve Codina case PEB2466_TG2(2): 458*227f609cSHerve Codina tg_freq_item = &peb2466->ch[2].tg2_freq_item; 459*227f609cSHerve Codina cr1_reg = PEB2466_CR1(2); 460*227f609cSHerve Codina cr1_mask = PEB2466_CR1_PTG2; 461*227f609cSHerve Codina break; 462*227f609cSHerve Codina case PEB2466_TG1(3): 463*227f609cSHerve Codina tg_freq_item = &peb2466->ch[3].tg1_freq_item; 464*227f609cSHerve Codina cr1_reg = PEB2466_CR1(3); 465*227f609cSHerve Codina cr1_mask = PEB2466_CR1_PTG1; 466*227f609cSHerve Codina break; 467*227f609cSHerve Codina case PEB2466_TG2(3): 468*227f609cSHerve Codina tg_freq_item = &peb2466->ch[3].tg2_freq_item; 469*227f609cSHerve Codina cr1_reg = PEB2466_CR1(3); 470*227f609cSHerve Codina cr1_mask = PEB2466_CR1_PTG2; 471*227f609cSHerve Codina break; 472*227f609cSHerve Codina default: 473*227f609cSHerve Codina return -EINVAL; 474*227f609cSHerve Codina } 475*227f609cSHerve Codina 476*227f609cSHerve Codina if (index == *tg_freq_item) 477*227f609cSHerve Codina return 0; 478*227f609cSHerve Codina 479*227f609cSHerve Codina if (index == PEB2466_TONE_1000HZ) { 480*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, cr1_reg, cr1_mask, 0); 481*227f609cSHerve Codina if (ret) 482*227f609cSHerve Codina return ret; 483*227f609cSHerve Codina } else { 484*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, e->reg, peb2466_tone_lookup[index], 4); 485*227f609cSHerve Codina if (ret) 486*227f609cSHerve Codina return ret; 487*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, cr1_reg, cr1_mask, cr1_mask); 488*227f609cSHerve Codina if (ret) 489*227f609cSHerve Codina return ret; 490*227f609cSHerve Codina } 491*227f609cSHerve Codina 492*227f609cSHerve Codina *tg_freq_item = index; 493*227f609cSHerve Codina return 1; /* The value changed */ 494*227f609cSHerve Codina } 495*227f609cSHerve Codina 496*227f609cSHerve Codina static const struct snd_kcontrol_new peb2466_ch0_out_mix_controls[] = { 497*227f609cSHerve Codina SOC_DAPM_SINGLE("TG1 Switch", PEB2466_CR1(0), 6, 1, 0), 498*227f609cSHerve Codina SOC_DAPM_SINGLE("TG2 Switch", PEB2466_CR1(0), 7, 1, 0), 499*227f609cSHerve Codina SOC_DAPM_SINGLE("Voice Switch", PEB2466_CR2(0), 0, 1, 0) 500*227f609cSHerve Codina }; 501*227f609cSHerve Codina 502*227f609cSHerve Codina static const struct snd_kcontrol_new peb2466_ch1_out_mix_controls[] = { 503*227f609cSHerve Codina SOC_DAPM_SINGLE("TG1 Switch", PEB2466_CR1(1), 6, 1, 0), 504*227f609cSHerve Codina SOC_DAPM_SINGLE("TG2 Switch", PEB2466_CR1(1), 7, 1, 0), 505*227f609cSHerve Codina SOC_DAPM_SINGLE("Voice Switch", PEB2466_CR2(1), 0, 1, 0) 506*227f609cSHerve Codina }; 507*227f609cSHerve Codina 508*227f609cSHerve Codina static const struct snd_kcontrol_new peb2466_ch2_out_mix_controls[] = { 509*227f609cSHerve Codina SOC_DAPM_SINGLE("TG1 Switch", PEB2466_CR1(2), 6, 1, 0), 510*227f609cSHerve Codina SOC_DAPM_SINGLE("TG2 Switch", PEB2466_CR1(2), 7, 1, 0), 511*227f609cSHerve Codina SOC_DAPM_SINGLE("Voice Switch", PEB2466_CR2(2), 0, 1, 0) 512*227f609cSHerve Codina }; 513*227f609cSHerve Codina 514*227f609cSHerve Codina static const struct snd_kcontrol_new peb2466_ch3_out_mix_controls[] = { 515*227f609cSHerve Codina SOC_DAPM_SINGLE("TG1 Switch", PEB2466_CR1(3), 6, 1, 0), 516*227f609cSHerve Codina SOC_DAPM_SINGLE("TG2 Switch", PEB2466_CR1(3), 7, 1, 0), 517*227f609cSHerve Codina SOC_DAPM_SINGLE("Voice Switch", PEB2466_CR2(3), 0, 1, 0) 518*227f609cSHerve Codina }; 519*227f609cSHerve Codina 520*227f609cSHerve Codina static const struct snd_kcontrol_new peb2466_controls[] = { 521*227f609cSHerve Codina /* Attenuators */ 522*227f609cSHerve Codina SOC_SINGLE("DAC0 -6dB Playback Switch", PEB2466_CR3(0), 2, 1, 0), 523*227f609cSHerve Codina SOC_SINGLE("DAC1 -6dB Playback Switch", PEB2466_CR3(1), 2, 1, 0), 524*227f609cSHerve Codina SOC_SINGLE("DAC2 -6dB Playback Switch", PEB2466_CR3(2), 2, 1, 0), 525*227f609cSHerve Codina SOC_SINGLE("DAC3 -6dB Playback Switch", PEB2466_CR3(3), 2, 1, 0), 526*227f609cSHerve Codina 527*227f609cSHerve Codina /* Amplifiers */ 528*227f609cSHerve Codina SOC_SINGLE("ADC0 +6dB Capture Switch", PEB2466_CR3(0), 3, 1, 0), 529*227f609cSHerve Codina SOC_SINGLE("ADC1 +6dB Capture Switch", PEB2466_CR3(1), 3, 1, 0), 530*227f609cSHerve Codina SOC_SINGLE("ADC2 +6dB Capture Switch", PEB2466_CR3(2), 3, 1, 0), 531*227f609cSHerve Codina SOC_SINGLE("ADC3 +6dB Capture Switch", PEB2466_CR3(3), 3, 1, 0), 532*227f609cSHerve Codina 533*227f609cSHerve Codina /* Tone generators */ 534*227f609cSHerve Codina SOC_ENUM_EXT("DAC0 TG1 Freq", peb2466_tg_freq[0][0], 535*227f609cSHerve Codina peb2466_tg_freq_get, peb2466_tg_freq_put), 536*227f609cSHerve Codina SOC_ENUM_EXT("DAC1 TG1 Freq", peb2466_tg_freq[1][0], 537*227f609cSHerve Codina peb2466_tg_freq_get, peb2466_tg_freq_put), 538*227f609cSHerve Codina SOC_ENUM_EXT("DAC2 TG1 Freq", peb2466_tg_freq[2][0], 539*227f609cSHerve Codina peb2466_tg_freq_get, peb2466_tg_freq_put), 540*227f609cSHerve Codina SOC_ENUM_EXT("DAC3 TG1 Freq", peb2466_tg_freq[3][0], 541*227f609cSHerve Codina peb2466_tg_freq_get, peb2466_tg_freq_put), 542*227f609cSHerve Codina 543*227f609cSHerve Codina SOC_ENUM_EXT("DAC0 TG2 Freq", peb2466_tg_freq[0][1], 544*227f609cSHerve Codina peb2466_tg_freq_get, peb2466_tg_freq_put), 545*227f609cSHerve Codina SOC_ENUM_EXT("DAC1 TG2 Freq", peb2466_tg_freq[1][1], 546*227f609cSHerve Codina peb2466_tg_freq_get, peb2466_tg_freq_put), 547*227f609cSHerve Codina SOC_ENUM_EXT("DAC2 TG2 Freq", peb2466_tg_freq[2][1], 548*227f609cSHerve Codina peb2466_tg_freq_get, peb2466_tg_freq_put), 549*227f609cSHerve Codina SOC_ENUM_EXT("DAC3 TG2 Freq", peb2466_tg_freq[3][1], 550*227f609cSHerve Codina peb2466_tg_freq_get, peb2466_tg_freq_put), 551*227f609cSHerve Codina }; 552*227f609cSHerve Codina 553*227f609cSHerve Codina static const struct snd_soc_dapm_widget peb2466_dapm_widgets[] = { 554*227f609cSHerve Codina SND_SOC_DAPM_SUPPLY("CH0 PWR", PEB2466_CR1(0), 0, 0, NULL, 0), 555*227f609cSHerve Codina SND_SOC_DAPM_SUPPLY("CH1 PWR", PEB2466_CR1(1), 0, 0, NULL, 0), 556*227f609cSHerve Codina SND_SOC_DAPM_SUPPLY("CH2 PWR", PEB2466_CR1(2), 0, 0, NULL, 0), 557*227f609cSHerve Codina SND_SOC_DAPM_SUPPLY("CH3 PWR", PEB2466_CR1(3), 0, 0, NULL, 0), 558*227f609cSHerve Codina 559*227f609cSHerve Codina SND_SOC_DAPM_DAC("CH0 DIN", "Playback", SND_SOC_NOPM, 0, 0), 560*227f609cSHerve Codina SND_SOC_DAPM_DAC("CH1 DIN", "Playback", SND_SOC_NOPM, 0, 0), 561*227f609cSHerve Codina SND_SOC_DAPM_DAC("CH2 DIN", "Playback", SND_SOC_NOPM, 0, 0), 562*227f609cSHerve Codina SND_SOC_DAPM_DAC("CH3 DIN", "Playback", SND_SOC_NOPM, 0, 0), 563*227f609cSHerve Codina 564*227f609cSHerve Codina SND_SOC_DAPM_SIGGEN("CH0 TG1"), 565*227f609cSHerve Codina SND_SOC_DAPM_SIGGEN("CH1 TG1"), 566*227f609cSHerve Codina SND_SOC_DAPM_SIGGEN("CH2 TG1"), 567*227f609cSHerve Codina SND_SOC_DAPM_SIGGEN("CH3 TG1"), 568*227f609cSHerve Codina 569*227f609cSHerve Codina SND_SOC_DAPM_SIGGEN("CH0 TG2"), 570*227f609cSHerve Codina SND_SOC_DAPM_SIGGEN("CH1 TG2"), 571*227f609cSHerve Codina SND_SOC_DAPM_SIGGEN("CH2 TG2"), 572*227f609cSHerve Codina SND_SOC_DAPM_SIGGEN("CH3 TG2"), 573*227f609cSHerve Codina 574*227f609cSHerve Codina SND_SOC_DAPM_MIXER("DAC0 Mixer", SND_SOC_NOPM, 0, 0, 575*227f609cSHerve Codina peb2466_ch0_out_mix_controls, 576*227f609cSHerve Codina ARRAY_SIZE(peb2466_ch0_out_mix_controls)), 577*227f609cSHerve Codina SND_SOC_DAPM_MIXER("DAC1 Mixer", SND_SOC_NOPM, 0, 0, 578*227f609cSHerve Codina peb2466_ch1_out_mix_controls, 579*227f609cSHerve Codina ARRAY_SIZE(peb2466_ch1_out_mix_controls)), 580*227f609cSHerve Codina SND_SOC_DAPM_MIXER("DAC2 Mixer", SND_SOC_NOPM, 0, 0, 581*227f609cSHerve Codina peb2466_ch2_out_mix_controls, 582*227f609cSHerve Codina ARRAY_SIZE(peb2466_ch2_out_mix_controls)), 583*227f609cSHerve Codina SND_SOC_DAPM_MIXER("DAC3 Mixer", SND_SOC_NOPM, 0, 0, 584*227f609cSHerve Codina peb2466_ch3_out_mix_controls, 585*227f609cSHerve Codina ARRAY_SIZE(peb2466_ch3_out_mix_controls)), 586*227f609cSHerve Codina 587*227f609cSHerve Codina SND_SOC_DAPM_PGA("DAC0 PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 588*227f609cSHerve Codina SND_SOC_DAPM_PGA("DAC1 PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 589*227f609cSHerve Codina SND_SOC_DAPM_PGA("DAC2 PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 590*227f609cSHerve Codina SND_SOC_DAPM_PGA("DAC3 PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 591*227f609cSHerve Codina 592*227f609cSHerve Codina SND_SOC_DAPM_OUTPUT("OUT0"), 593*227f609cSHerve Codina SND_SOC_DAPM_OUTPUT("OUT1"), 594*227f609cSHerve Codina SND_SOC_DAPM_OUTPUT("OUT2"), 595*227f609cSHerve Codina SND_SOC_DAPM_OUTPUT("OUT3"), 596*227f609cSHerve Codina 597*227f609cSHerve Codina SND_SOC_DAPM_INPUT("IN0"), 598*227f609cSHerve Codina SND_SOC_DAPM_INPUT("IN1"), 599*227f609cSHerve Codina SND_SOC_DAPM_INPUT("IN2"), 600*227f609cSHerve Codina SND_SOC_DAPM_INPUT("IN3"), 601*227f609cSHerve Codina 602*227f609cSHerve Codina SND_SOC_DAPM_DAC("ADC0", "Capture", SND_SOC_NOPM, 0, 0), 603*227f609cSHerve Codina SND_SOC_DAPM_DAC("ADC1", "Capture", SND_SOC_NOPM, 0, 0), 604*227f609cSHerve Codina SND_SOC_DAPM_DAC("ADC2", "Capture", SND_SOC_NOPM, 0, 0), 605*227f609cSHerve Codina SND_SOC_DAPM_DAC("ADC3", "Capture", SND_SOC_NOPM, 0, 0), 606*227f609cSHerve Codina }; 607*227f609cSHerve Codina 608*227f609cSHerve Codina static const struct snd_soc_dapm_route peb2466_dapm_routes[] = { 609*227f609cSHerve Codina { "CH0 DIN", NULL, "CH0 PWR" }, 610*227f609cSHerve Codina { "CH1 DIN", NULL, "CH1 PWR" }, 611*227f609cSHerve Codina { "CH2 DIN", NULL, "CH2 PWR" }, 612*227f609cSHerve Codina { "CH3 DIN", NULL, "CH3 PWR" }, 613*227f609cSHerve Codina 614*227f609cSHerve Codina { "CH0 TG1", NULL, "CH0 PWR" }, 615*227f609cSHerve Codina { "CH1 TG1", NULL, "CH1 PWR" }, 616*227f609cSHerve Codina { "CH2 TG1", NULL, "CH2 PWR" }, 617*227f609cSHerve Codina { "CH3 TG1", NULL, "CH3 PWR" }, 618*227f609cSHerve Codina 619*227f609cSHerve Codina { "CH0 TG2", NULL, "CH0 PWR" }, 620*227f609cSHerve Codina { "CH1 TG2", NULL, "CH1 PWR" }, 621*227f609cSHerve Codina { "CH2 TG2", NULL, "CH2 PWR" }, 622*227f609cSHerve Codina { "CH3 TG2", NULL, "CH3 PWR" }, 623*227f609cSHerve Codina 624*227f609cSHerve Codina { "DAC0 Mixer", "TG1 Switch", "CH0 TG1" }, 625*227f609cSHerve Codina { "DAC0 Mixer", "TG2 Switch", "CH0 TG2" }, 626*227f609cSHerve Codina { "DAC0 Mixer", "Voice Switch", "CH0 DIN" }, 627*227f609cSHerve Codina { "DAC0 Mixer", NULL, "CH0 DIN" }, 628*227f609cSHerve Codina 629*227f609cSHerve Codina { "DAC1 Mixer", "TG1 Switch", "CH1 TG1" }, 630*227f609cSHerve Codina { "DAC1 Mixer", "TG2 Switch", "CH1 TG2" }, 631*227f609cSHerve Codina { "DAC1 Mixer", "Voice Switch", "CH1 DIN" }, 632*227f609cSHerve Codina { "DAC1 Mixer", NULL, "CH1 DIN" }, 633*227f609cSHerve Codina 634*227f609cSHerve Codina { "DAC2 Mixer", "TG1 Switch", "CH2 TG1" }, 635*227f609cSHerve Codina { "DAC2 Mixer", "TG2 Switch", "CH2 TG2" }, 636*227f609cSHerve Codina { "DAC2 Mixer", "Voice Switch", "CH2 DIN" }, 637*227f609cSHerve Codina { "DAC2 Mixer", NULL, "CH2 DIN" }, 638*227f609cSHerve Codina 639*227f609cSHerve Codina { "DAC3 Mixer", "TG1 Switch", "CH3 TG1" }, 640*227f609cSHerve Codina { "DAC3 Mixer", "TG2 Switch", "CH3 TG2" }, 641*227f609cSHerve Codina { "DAC3 Mixer", "Voice Switch", "CH3 DIN" }, 642*227f609cSHerve Codina { "DAC3 Mixer", NULL, "CH3 DIN" }, 643*227f609cSHerve Codina 644*227f609cSHerve Codina { "DAC0 PGA", NULL, "DAC0 Mixer" }, 645*227f609cSHerve Codina { "DAC1 PGA", NULL, "DAC1 Mixer" }, 646*227f609cSHerve Codina { "DAC2 PGA", NULL, "DAC2 Mixer" }, 647*227f609cSHerve Codina { "DAC3 PGA", NULL, "DAC3 Mixer" }, 648*227f609cSHerve Codina 649*227f609cSHerve Codina { "OUT0", NULL, "DAC0 PGA" }, 650*227f609cSHerve Codina { "OUT1", NULL, "DAC1 PGA" }, 651*227f609cSHerve Codina { "OUT2", NULL, "DAC2 PGA" }, 652*227f609cSHerve Codina { "OUT3", NULL, "DAC3 PGA" }, 653*227f609cSHerve Codina 654*227f609cSHerve Codina { "ADC0", NULL, "IN0" }, 655*227f609cSHerve Codina { "ADC1", NULL, "IN1" }, 656*227f609cSHerve Codina { "ADC2", NULL, "IN2" }, 657*227f609cSHerve Codina { "ADC3", NULL, "IN3" }, 658*227f609cSHerve Codina 659*227f609cSHerve Codina { "ADC0", NULL, "CH0 PWR" }, 660*227f609cSHerve Codina { "ADC1", NULL, "CH1 PWR" }, 661*227f609cSHerve Codina { "ADC2", NULL, "CH2 PWR" }, 662*227f609cSHerve Codina { "ADC3", NULL, "CH3 PWR" }, 663*227f609cSHerve Codina }; 664*227f609cSHerve Codina 665*227f609cSHerve Codina static int peb2466_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask, 666*227f609cSHerve Codina unsigned int rx_mask, int slots, int width) 667*227f609cSHerve Codina { 668*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(dai->component); 669*227f609cSHerve Codina unsigned int chan; 670*227f609cSHerve Codina unsigned int mask; 671*227f609cSHerve Codina u8 slot; 672*227f609cSHerve Codina int ret; 673*227f609cSHerve Codina 674*227f609cSHerve Codina switch (width) { 675*227f609cSHerve Codina case 0: 676*227f609cSHerve Codina /* Not set -> default 8 */ 677*227f609cSHerve Codina case 8: 678*227f609cSHerve Codina break; 679*227f609cSHerve Codina default: 680*227f609cSHerve Codina dev_err(dai->dev, "tdm slot width %d not supported\n", width); 681*227f609cSHerve Codina return -EINVAL; 682*227f609cSHerve Codina } 683*227f609cSHerve Codina 684*227f609cSHerve Codina mask = tx_mask; 685*227f609cSHerve Codina slot = 0; 686*227f609cSHerve Codina chan = 0; 687*227f609cSHerve Codina while (mask && chan < PEB2466_NB_CHANNEL) { 688*227f609cSHerve Codina if (mask & 0x1) { 689*227f609cSHerve Codina ret = regmap_write(peb2466->regmap, PEB2466_CR5(chan), slot); 690*227f609cSHerve Codina if (ret) { 691*227f609cSHerve Codina dev_err(dai->dev, "chan %d set tx tdm slot failed (%d)\n", 692*227f609cSHerve Codina chan, ret); 693*227f609cSHerve Codina return ret; 694*227f609cSHerve Codina } 695*227f609cSHerve Codina chan++; 696*227f609cSHerve Codina } 697*227f609cSHerve Codina mask >>= 1; 698*227f609cSHerve Codina slot++; 699*227f609cSHerve Codina } 700*227f609cSHerve Codina if (mask) { 701*227f609cSHerve Codina dev_err(dai->dev, "too much tx slots defined (mask = 0x%x) support max %d\n", 702*227f609cSHerve Codina tx_mask, PEB2466_NB_CHANNEL); 703*227f609cSHerve Codina return -EINVAL; 704*227f609cSHerve Codina } 705*227f609cSHerve Codina peb2466->max_chan_playback = chan; 706*227f609cSHerve Codina 707*227f609cSHerve Codina mask = rx_mask; 708*227f609cSHerve Codina slot = 0; 709*227f609cSHerve Codina chan = 0; 710*227f609cSHerve Codina while (mask && chan < PEB2466_NB_CHANNEL) { 711*227f609cSHerve Codina if (mask & 0x1) { 712*227f609cSHerve Codina ret = regmap_write(peb2466->regmap, PEB2466_CR4(chan), slot); 713*227f609cSHerve Codina if (ret) { 714*227f609cSHerve Codina dev_err(dai->dev, "chan %d set rx tdm slot failed (%d)\n", 715*227f609cSHerve Codina chan, ret); 716*227f609cSHerve Codina return ret; 717*227f609cSHerve Codina } 718*227f609cSHerve Codina chan++; 719*227f609cSHerve Codina } 720*227f609cSHerve Codina mask >>= 1; 721*227f609cSHerve Codina slot++; 722*227f609cSHerve Codina } 723*227f609cSHerve Codina if (mask) { 724*227f609cSHerve Codina dev_err(dai->dev, "too much rx slots defined (mask = 0x%x) support max %d\n", 725*227f609cSHerve Codina rx_mask, PEB2466_NB_CHANNEL); 726*227f609cSHerve Codina return -EINVAL; 727*227f609cSHerve Codina } 728*227f609cSHerve Codina peb2466->max_chan_capture = chan; 729*227f609cSHerve Codina 730*227f609cSHerve Codina return 0; 731*227f609cSHerve Codina } 732*227f609cSHerve Codina 733*227f609cSHerve Codina static int peb2466_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 734*227f609cSHerve Codina { 735*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(dai->component); 736*227f609cSHerve Codina u8 xr6; 737*227f609cSHerve Codina 738*227f609cSHerve Codina switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 739*227f609cSHerve Codina case SND_SOC_DAIFMT_DSP_A: 740*227f609cSHerve Codina xr6 = PEB2466_XR6_PCM_OFFSET(1); 741*227f609cSHerve Codina break; 742*227f609cSHerve Codina case SND_SOC_DAIFMT_DSP_B: 743*227f609cSHerve Codina xr6 = PEB2466_XR6_PCM_OFFSET(0); 744*227f609cSHerve Codina break; 745*227f609cSHerve Codina default: 746*227f609cSHerve Codina dev_err(dai->dev, "Unsupported format 0x%x\n", 747*227f609cSHerve Codina fmt & SND_SOC_DAIFMT_FORMAT_MASK); 748*227f609cSHerve Codina return -EINVAL; 749*227f609cSHerve Codina } 750*227f609cSHerve Codina return regmap_write(peb2466->regmap, PEB2466_XR6, xr6); 751*227f609cSHerve Codina } 752*227f609cSHerve Codina 753*227f609cSHerve Codina static int peb2466_dai_hw_params(struct snd_pcm_substream *substream, 754*227f609cSHerve Codina struct snd_pcm_hw_params *params, 755*227f609cSHerve Codina struct snd_soc_dai *dai) 756*227f609cSHerve Codina { 757*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(dai->component); 758*227f609cSHerve Codina unsigned int ch; 759*227f609cSHerve Codina int ret; 760*227f609cSHerve Codina u8 cr1; 761*227f609cSHerve Codina 762*227f609cSHerve Codina switch (params_format(params)) { 763*227f609cSHerve Codina case SNDRV_PCM_FORMAT_MU_LAW: 764*227f609cSHerve Codina cr1 = PEB2466_CR1_LAW_MULAW; 765*227f609cSHerve Codina break; 766*227f609cSHerve Codina case SNDRV_PCM_FORMAT_A_LAW: 767*227f609cSHerve Codina cr1 = PEB2466_CR1_LAW_ALAW; 768*227f609cSHerve Codina break; 769*227f609cSHerve Codina default: 770*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "Unsupported format 0x%x\n", 771*227f609cSHerve Codina params_format(params)); 772*227f609cSHerve Codina return -EINVAL; 773*227f609cSHerve Codina } 774*227f609cSHerve Codina 775*227f609cSHerve Codina for (ch = 0; ch < PEB2466_NB_CHANNEL; ch++) { 776*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR1(ch), 777*227f609cSHerve Codina PEB2466_CR1_LAW_MASK, cr1); 778*227f609cSHerve Codina if (ret) 779*227f609cSHerve Codina return ret; 780*227f609cSHerve Codina } 781*227f609cSHerve Codina 782*227f609cSHerve Codina return 0; 783*227f609cSHerve Codina } 784*227f609cSHerve Codina 785*227f609cSHerve Codina static const unsigned int peb2466_sample_bits[] = {8}; 786*227f609cSHerve Codina 787*227f609cSHerve Codina static struct snd_pcm_hw_constraint_list peb2466_sample_bits_constr = { 788*227f609cSHerve Codina .list = peb2466_sample_bits, 789*227f609cSHerve Codina .count = ARRAY_SIZE(peb2466_sample_bits), 790*227f609cSHerve Codina }; 791*227f609cSHerve Codina 792*227f609cSHerve Codina static int peb2466_dai_startup(struct snd_pcm_substream *substream, 793*227f609cSHerve Codina struct snd_soc_dai *dai) 794*227f609cSHerve Codina { 795*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(dai->component); 796*227f609cSHerve Codina unsigned int max_ch; 797*227f609cSHerve Codina int ret; 798*227f609cSHerve Codina 799*227f609cSHerve Codina max_ch = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 800*227f609cSHerve Codina peb2466->max_chan_playback : peb2466->max_chan_capture; 801*227f609cSHerve Codina 802*227f609cSHerve Codina /* 803*227f609cSHerve Codina * Disable stream support (min = 0, max = 0) if no timeslots were 804*227f609cSHerve Codina * configured. 805*227f609cSHerve Codina */ 806*227f609cSHerve Codina ret = snd_pcm_hw_constraint_minmax(substream->runtime, 807*227f609cSHerve Codina SNDRV_PCM_HW_PARAM_CHANNELS, 808*227f609cSHerve Codina max_ch ? 1 : 0, max_ch); 809*227f609cSHerve Codina if (ret < 0) 810*227f609cSHerve Codina return ret; 811*227f609cSHerve Codina 812*227f609cSHerve Codina return snd_pcm_hw_constraint_list(substream->runtime, 0, 813*227f609cSHerve Codina SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 814*227f609cSHerve Codina &peb2466_sample_bits_constr); 815*227f609cSHerve Codina } 816*227f609cSHerve Codina 817*227f609cSHerve Codina static u64 peb2466_dai_formats[] = { 818*227f609cSHerve Codina SND_SOC_POSSIBLE_DAIFMT_DSP_A | 819*227f609cSHerve Codina SND_SOC_POSSIBLE_DAIFMT_DSP_B, 820*227f609cSHerve Codina }; 821*227f609cSHerve Codina 822*227f609cSHerve Codina static const struct snd_soc_dai_ops peb2466_dai_ops = { 823*227f609cSHerve Codina .startup = peb2466_dai_startup, 824*227f609cSHerve Codina .hw_params = peb2466_dai_hw_params, 825*227f609cSHerve Codina .set_tdm_slot = peb2466_dai_set_tdm_slot, 826*227f609cSHerve Codina .set_fmt = peb2466_dai_set_fmt, 827*227f609cSHerve Codina .auto_selectable_formats = peb2466_dai_formats, 828*227f609cSHerve Codina .num_auto_selectable_formats = ARRAY_SIZE(peb2466_dai_formats), 829*227f609cSHerve Codina }; 830*227f609cSHerve Codina 831*227f609cSHerve Codina static struct snd_soc_dai_driver peb2466_dai_driver = { 832*227f609cSHerve Codina .name = "peb2466", 833*227f609cSHerve Codina .playback = { 834*227f609cSHerve Codina .stream_name = "Playback", 835*227f609cSHerve Codina .channels_min = 1, 836*227f609cSHerve Codina .channels_max = PEB2466_NB_CHANNEL, 837*227f609cSHerve Codina .rates = SNDRV_PCM_RATE_8000, 838*227f609cSHerve Codina .formats = SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW, 839*227f609cSHerve Codina }, 840*227f609cSHerve Codina .capture = { 841*227f609cSHerve Codina .stream_name = "Capture", 842*227f609cSHerve Codina .channels_min = 1, 843*227f609cSHerve Codina .channels_max = PEB2466_NB_CHANNEL, 844*227f609cSHerve Codina .rates = SNDRV_PCM_RATE_8000, 845*227f609cSHerve Codina .formats = SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW, 846*227f609cSHerve Codina }, 847*227f609cSHerve Codina .ops = &peb2466_dai_ops, 848*227f609cSHerve Codina }; 849*227f609cSHerve Codina 850*227f609cSHerve Codina static int peb2466_reset_audio(struct peb2466 *peb2466) 851*227f609cSHerve Codina { 852*227f609cSHerve Codina static const struct reg_sequence reg_reset[] = { 853*227f609cSHerve Codina { .reg = PEB2466_XR6, .def = 0x00 }, 854*227f609cSHerve Codina 855*227f609cSHerve Codina { .reg = PEB2466_CR5(0), .def = 0x00 }, 856*227f609cSHerve Codina { .reg = PEB2466_CR4(0), .def = 0x00 }, 857*227f609cSHerve Codina { .reg = PEB2466_CR3(0), .def = 0x00 }, 858*227f609cSHerve Codina { .reg = PEB2466_CR2(0), .def = 0x00 }, 859*227f609cSHerve Codina { .reg = PEB2466_CR1(0), .def = 0x00 }, 860*227f609cSHerve Codina { .reg = PEB2466_CR0(0), .def = PEB2466_CR0_IMR1 }, 861*227f609cSHerve Codina 862*227f609cSHerve Codina { .reg = PEB2466_CR5(1), .def = 0x00 }, 863*227f609cSHerve Codina { .reg = PEB2466_CR4(1), .def = 0x00 }, 864*227f609cSHerve Codina { .reg = PEB2466_CR3(1), .def = 0x00 }, 865*227f609cSHerve Codina { .reg = PEB2466_CR2(1), .def = 0x00 }, 866*227f609cSHerve Codina { .reg = PEB2466_CR1(1), .def = 0x00 }, 867*227f609cSHerve Codina { .reg = PEB2466_CR0(1), .def = PEB2466_CR0_IMR1 }, 868*227f609cSHerve Codina 869*227f609cSHerve Codina { .reg = PEB2466_CR5(2), .def = 0x00 }, 870*227f609cSHerve Codina { .reg = PEB2466_CR4(2), .def = 0x00 }, 871*227f609cSHerve Codina { .reg = PEB2466_CR3(2), .def = 0x00 }, 872*227f609cSHerve Codina { .reg = PEB2466_CR2(2), .def = 0x00 }, 873*227f609cSHerve Codina { .reg = PEB2466_CR1(2), .def = 0x00 }, 874*227f609cSHerve Codina { .reg = PEB2466_CR0(2), .def = PEB2466_CR0_IMR1 }, 875*227f609cSHerve Codina 876*227f609cSHerve Codina { .reg = PEB2466_CR5(3), .def = 0x00 }, 877*227f609cSHerve Codina { .reg = PEB2466_CR4(3), .def = 0x00 }, 878*227f609cSHerve Codina { .reg = PEB2466_CR3(3), .def = 0x00 }, 879*227f609cSHerve Codina { .reg = PEB2466_CR2(3), .def = 0x00 }, 880*227f609cSHerve Codina { .reg = PEB2466_CR1(3), .def = 0x00 }, 881*227f609cSHerve Codina { .reg = PEB2466_CR0(3), .def = PEB2466_CR0_IMR1 }, 882*227f609cSHerve Codina }; 883*227f609cSHerve Codina static const u8 imr1_p1[8] = {0x00, 0x90, 0x09, 0x00, 0x90, 0x09, 0x00, 0x00}; 884*227f609cSHerve Codina static const u8 imr1_p2[8] = {0x7F, 0xFF, 0x00, 0x00, 0x90, 0x14, 0x40, 0x08}; 885*227f609cSHerve Codina static const u8 zero[8] = {0}; 886*227f609cSHerve Codina int ret; 887*227f609cSHerve Codina int i; 888*227f609cSHerve Codina 889*227f609cSHerve Codina for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) { 890*227f609cSHerve Codina peb2466->ch[i].tg1_freq_item = PEB2466_TONE_1000HZ; 891*227f609cSHerve Codina peb2466->ch[i].tg2_freq_item = PEB2466_TONE_1000HZ; 892*227f609cSHerve Codina 893*227f609cSHerve Codina /* 894*227f609cSHerve Codina * Even if not used, disabling IM/R1 filter is not recommended. 895*227f609cSHerve Codina * Instead, we must configure it with default coefficients and 896*227f609cSHerve Codina * enable it. 897*227f609cSHerve Codina * The filter will be enabled right after (in the following 898*227f609cSHerve Codina * regmap_multi_reg_write() call). 899*227f609cSHerve Codina */ 900*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_IMR1_FILTER_P1(i), imr1_p1, 8); 901*227f609cSHerve Codina if (ret) 902*227f609cSHerve Codina return ret; 903*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_IMR1_FILTER_P2(i), imr1_p2, 8); 904*227f609cSHerve Codina if (ret) 905*227f609cSHerve Codina return ret; 906*227f609cSHerve Codina 907*227f609cSHerve Codina /* Set all other filters coefficients to zero */ 908*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_TH_FILTER_P1(i), zero, 8); 909*227f609cSHerve Codina if (ret) 910*227f609cSHerve Codina return ret; 911*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_TH_FILTER_P2(i), zero, 8); 912*227f609cSHerve Codina if (ret) 913*227f609cSHerve Codina return ret; 914*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_TH_FILTER_P3(i), zero, 8); 915*227f609cSHerve Codina if (ret) 916*227f609cSHerve Codina return ret; 917*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_FRX_FILTER(i), zero, 8); 918*227f609cSHerve Codina if (ret) 919*227f609cSHerve Codina return ret; 920*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_FRR_FILTER(i), zero, 8); 921*227f609cSHerve Codina if (ret) 922*227f609cSHerve Codina return ret; 923*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_AX_FILTER(i), zero, 4); 924*227f609cSHerve Codina if (ret) 925*227f609cSHerve Codina return ret; 926*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_AR_FILTER(i), zero, 4); 927*227f609cSHerve Codina if (ret) 928*227f609cSHerve Codina return ret; 929*227f609cSHerve Codina } 930*227f609cSHerve Codina 931*227f609cSHerve Codina return regmap_multi_reg_write(peb2466->regmap, reg_reset, ARRAY_SIZE(reg_reset)); 932*227f609cSHerve Codina } 933*227f609cSHerve Codina 934*227f609cSHerve Codina static int peb2466_fw_parse_thfilter(struct snd_soc_component *component, 935*227f609cSHerve Codina u16 tag, u32 lng, const u8 *data) 936*227f609cSHerve Codina { 937*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component); 938*227f609cSHerve Codina u8 mask; 939*227f609cSHerve Codina int ret; 940*227f609cSHerve Codina int i; 941*227f609cSHerve Codina 942*227f609cSHerve Codina dev_info(component->dev, "fw TH filter: mask %x, %*phN\n", *data, 943*227f609cSHerve Codina lng - 1, data + 1); 944*227f609cSHerve Codina 945*227f609cSHerve Codina /* 946*227f609cSHerve Codina * TH_FILTER TLV data: 947*227f609cSHerve Codina * - @0 1 byte: Chan mask (bit set means related channel is concerned) 948*227f609cSHerve Codina * - @1 8 bytes: TH-Filter coefficients part1 949*227f609cSHerve Codina * - @9 8 bytes: TH-Filter coefficients part2 950*227f609cSHerve Codina * - @17 8 bytes: TH-Filter coefficients part3 951*227f609cSHerve Codina */ 952*227f609cSHerve Codina mask = *data; 953*227f609cSHerve Codina for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) { 954*227f609cSHerve Codina if (!(mask & (1 << i))) 955*227f609cSHerve Codina continue; 956*227f609cSHerve Codina 957*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 958*227f609cSHerve Codina PEB2466_CR0_TH, 0); 959*227f609cSHerve Codina if (ret) 960*227f609cSHerve Codina return ret; 961*227f609cSHerve Codina 962*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_TH_FILTER_P1(i), data + 1, 8); 963*227f609cSHerve Codina if (ret) 964*227f609cSHerve Codina return ret; 965*227f609cSHerve Codina 966*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_TH_FILTER_P2(i), data + 9, 8); 967*227f609cSHerve Codina if (ret) 968*227f609cSHerve Codina return ret; 969*227f609cSHerve Codina 970*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_TH_FILTER_P3(i), data + 17, 8); 971*227f609cSHerve Codina if (ret) 972*227f609cSHerve Codina return ret; 973*227f609cSHerve Codina 974*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 975*227f609cSHerve Codina PEB2466_CR0_TH | PEB2466_CR0_THSEL_MASK, 976*227f609cSHerve Codina PEB2466_CR0_TH | PEB2466_CR0_THSEL(i)); 977*227f609cSHerve Codina if (ret) 978*227f609cSHerve Codina return ret; 979*227f609cSHerve Codina } 980*227f609cSHerve Codina return 0; 981*227f609cSHerve Codina } 982*227f609cSHerve Codina 983*227f609cSHerve Codina static int peb2466_fw_parse_imr1filter(struct snd_soc_component *component, 984*227f609cSHerve Codina u16 tag, u32 lng, const u8 *data) 985*227f609cSHerve Codina { 986*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component); 987*227f609cSHerve Codina u8 mask; 988*227f609cSHerve Codina int ret; 989*227f609cSHerve Codina int i; 990*227f609cSHerve Codina 991*227f609cSHerve Codina dev_info(component->dev, "fw IM/R1 filter: mask %x, %*phN\n", *data, 992*227f609cSHerve Codina lng - 1, data + 1); 993*227f609cSHerve Codina 994*227f609cSHerve Codina /* 995*227f609cSHerve Codina * IMR1_FILTER TLV data: 996*227f609cSHerve Codina * - @0 1 byte: Chan mask (bit set means related channel is concerned) 997*227f609cSHerve Codina * - @1 8 bytes: IM/R1-Filter coefficients part1 998*227f609cSHerve Codina * - @9 8 bytes: IM/R1-Filter coefficients part2 999*227f609cSHerve Codina */ 1000*227f609cSHerve Codina mask = *data; 1001*227f609cSHerve Codina for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) { 1002*227f609cSHerve Codina if (!(mask & (1 << i))) 1003*227f609cSHerve Codina continue; 1004*227f609cSHerve Codina 1005*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1006*227f609cSHerve Codina PEB2466_CR0_IMR1, 0); 1007*227f609cSHerve Codina if (ret) 1008*227f609cSHerve Codina return ret; 1009*227f609cSHerve Codina 1010*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_IMR1_FILTER_P1(i), data + 1, 8); 1011*227f609cSHerve Codina if (ret) 1012*227f609cSHerve Codina return ret; 1013*227f609cSHerve Codina 1014*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_IMR1_FILTER_P2(i), data + 9, 8); 1015*227f609cSHerve Codina if (ret) 1016*227f609cSHerve Codina return ret; 1017*227f609cSHerve Codina 1018*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1019*227f609cSHerve Codina PEB2466_CR0_IMR1, PEB2466_CR0_IMR1); 1020*227f609cSHerve Codina if (ret) 1021*227f609cSHerve Codina return ret; 1022*227f609cSHerve Codina } 1023*227f609cSHerve Codina return 0; 1024*227f609cSHerve Codina } 1025*227f609cSHerve Codina 1026*227f609cSHerve Codina static int peb2466_fw_parse_frxfilter(struct snd_soc_component *component, 1027*227f609cSHerve Codina u16 tag, u32 lng, const u8 *data) 1028*227f609cSHerve Codina { 1029*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component); 1030*227f609cSHerve Codina u8 mask; 1031*227f609cSHerve Codina int ret; 1032*227f609cSHerve Codina int i; 1033*227f609cSHerve Codina 1034*227f609cSHerve Codina dev_info(component->dev, "fw FRX filter: mask %x, %*phN\n", *data, 1035*227f609cSHerve Codina lng - 1, data + 1); 1036*227f609cSHerve Codina 1037*227f609cSHerve Codina /* 1038*227f609cSHerve Codina * FRX_FILTER TLV data: 1039*227f609cSHerve Codina * - @0 1 byte: Chan mask (bit set means related channel is concerned) 1040*227f609cSHerve Codina * - @1 8 bytes: FRX-Filter coefficients 1041*227f609cSHerve Codina */ 1042*227f609cSHerve Codina mask = *data; 1043*227f609cSHerve Codina for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) { 1044*227f609cSHerve Codina if (!(mask & (1 << i))) 1045*227f609cSHerve Codina continue; 1046*227f609cSHerve Codina 1047*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1048*227f609cSHerve Codina PEB2466_CR0_FRX, 0); 1049*227f609cSHerve Codina if (ret) 1050*227f609cSHerve Codina return ret; 1051*227f609cSHerve Codina 1052*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_FRX_FILTER(i), data + 1, 8); 1053*227f609cSHerve Codina if (ret) 1054*227f609cSHerve Codina return ret; 1055*227f609cSHerve Codina 1056*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1057*227f609cSHerve Codina PEB2466_CR0_FRX, PEB2466_CR0_FRX); 1058*227f609cSHerve Codina if (ret) 1059*227f609cSHerve Codina return ret; 1060*227f609cSHerve Codina } 1061*227f609cSHerve Codina return 0; 1062*227f609cSHerve Codina } 1063*227f609cSHerve Codina 1064*227f609cSHerve Codina static int peb2466_fw_parse_frrfilter(struct snd_soc_component *component, 1065*227f609cSHerve Codina u16 tag, u32 lng, const u8 *data) 1066*227f609cSHerve Codina { 1067*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component); 1068*227f609cSHerve Codina u8 mask; 1069*227f609cSHerve Codina int ret; 1070*227f609cSHerve Codina int i; 1071*227f609cSHerve Codina 1072*227f609cSHerve Codina dev_info(component->dev, "fw FRR filter: mask %x, %*phN\n", *data, 1073*227f609cSHerve Codina lng - 1, data + 1); 1074*227f609cSHerve Codina 1075*227f609cSHerve Codina /* 1076*227f609cSHerve Codina * FRR_FILTER TLV data: 1077*227f609cSHerve Codina * - @0 1 byte: Chan mask (bit set means related channel is concerned) 1078*227f609cSHerve Codina * - @1 8 bytes: FRR-Filter coefficients 1079*227f609cSHerve Codina */ 1080*227f609cSHerve Codina mask = *data; 1081*227f609cSHerve Codina for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) { 1082*227f609cSHerve Codina if (!(mask & (1 << i))) 1083*227f609cSHerve Codina continue; 1084*227f609cSHerve Codina 1085*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1086*227f609cSHerve Codina PEB2466_CR0_FRR, 0); 1087*227f609cSHerve Codina if (ret) 1088*227f609cSHerve Codina return ret; 1089*227f609cSHerve Codina 1090*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_FRR_FILTER(i), data + 1, 8); 1091*227f609cSHerve Codina if (ret) 1092*227f609cSHerve Codina return ret; 1093*227f609cSHerve Codina 1094*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1095*227f609cSHerve Codina PEB2466_CR0_FRR, PEB2466_CR0_FRR); 1096*227f609cSHerve Codina if (ret) 1097*227f609cSHerve Codina return ret; 1098*227f609cSHerve Codina } 1099*227f609cSHerve Codina return 0; 1100*227f609cSHerve Codina } 1101*227f609cSHerve Codina 1102*227f609cSHerve Codina static int peb2466_fw_parse_axfilter(struct snd_soc_component *component, 1103*227f609cSHerve Codina u16 tag, u32 lng, const u8 *data) 1104*227f609cSHerve Codina { 1105*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component); 1106*227f609cSHerve Codina u8 mask; 1107*227f609cSHerve Codina int ret; 1108*227f609cSHerve Codina int i; 1109*227f609cSHerve Codina 1110*227f609cSHerve Codina dev_info(component->dev, "fw AX filter: mask %x, %*phN\n", *data, 1111*227f609cSHerve Codina lng - 1, data + 1); 1112*227f609cSHerve Codina 1113*227f609cSHerve Codina /* 1114*227f609cSHerve Codina * AX_FILTER TLV data: 1115*227f609cSHerve Codina * - @0 1 byte: Chan mask (bit set means related channel is concerned) 1116*227f609cSHerve Codina * - @1 4 bytes: AX-Filter coefficients 1117*227f609cSHerve Codina */ 1118*227f609cSHerve Codina mask = *data; 1119*227f609cSHerve Codina for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) { 1120*227f609cSHerve Codina if (!(mask & (1 << i))) 1121*227f609cSHerve Codina continue; 1122*227f609cSHerve Codina 1123*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1124*227f609cSHerve Codina PEB2466_CR0_AX, 0); 1125*227f609cSHerve Codina if (ret) 1126*227f609cSHerve Codina return ret; 1127*227f609cSHerve Codina 1128*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_AX_FILTER(i), data + 1, 4); 1129*227f609cSHerve Codina if (ret) 1130*227f609cSHerve Codina return ret; 1131*227f609cSHerve Codina 1132*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1133*227f609cSHerve Codina PEB2466_CR0_AX, PEB2466_CR0_AX); 1134*227f609cSHerve Codina if (ret) 1135*227f609cSHerve Codina return ret; 1136*227f609cSHerve Codina } 1137*227f609cSHerve Codina return 0; 1138*227f609cSHerve Codina } 1139*227f609cSHerve Codina 1140*227f609cSHerve Codina static int peb2466_fw_parse_arfilter(struct snd_soc_component *component, 1141*227f609cSHerve Codina u16 tag, u32 lng, const u8 *data) 1142*227f609cSHerve Codina { 1143*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component); 1144*227f609cSHerve Codina u8 mask; 1145*227f609cSHerve Codina int ret; 1146*227f609cSHerve Codina int i; 1147*227f609cSHerve Codina 1148*227f609cSHerve Codina dev_info(component->dev, "fw AR filter: mask %x, %*phN\n", *data, 1149*227f609cSHerve Codina lng - 1, data + 1); 1150*227f609cSHerve Codina 1151*227f609cSHerve Codina /* 1152*227f609cSHerve Codina * AR_FILTER TLV data: 1153*227f609cSHerve Codina * - @0 1 byte: Chan mask (bit set means related channel is concerned) 1154*227f609cSHerve Codina * - @1 4 bytes: AR-Filter coefficients 1155*227f609cSHerve Codina */ 1156*227f609cSHerve Codina mask = *data; 1157*227f609cSHerve Codina for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) { 1158*227f609cSHerve Codina if (!(mask & (1 << i))) 1159*227f609cSHerve Codina continue; 1160*227f609cSHerve Codina 1161*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1162*227f609cSHerve Codina PEB2466_CR0_AR, 0); 1163*227f609cSHerve Codina if (ret) 1164*227f609cSHerve Codina return ret; 1165*227f609cSHerve Codina 1166*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_AR_FILTER(i), data + 1, 4); 1167*227f609cSHerve Codina if (ret) 1168*227f609cSHerve Codina return ret; 1169*227f609cSHerve Codina 1170*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1171*227f609cSHerve Codina PEB2466_CR0_AR, PEB2466_CR0_AR); 1172*227f609cSHerve Codina if (ret) 1173*227f609cSHerve Codina return ret; 1174*227f609cSHerve Codina } 1175*227f609cSHerve Codina return 0; 1176*227f609cSHerve Codina } 1177*227f609cSHerve Codina 1178*227f609cSHerve Codina static const char * const peb2466_ax_ctrl_names[] = { 1179*227f609cSHerve Codina "ADC0 Capture Volume", 1180*227f609cSHerve Codina "ADC1 Capture Volume", 1181*227f609cSHerve Codina "ADC2 Capture Volume", 1182*227f609cSHerve Codina "ADC3 Capture Volume", 1183*227f609cSHerve Codina }; 1184*227f609cSHerve Codina 1185*227f609cSHerve Codina static int peb2466_fw_parse_axtable(struct snd_soc_component *component, 1186*227f609cSHerve Codina u16 tag, u32 lng, const u8 *data) 1187*227f609cSHerve Codina { 1188*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component); 1189*227f609cSHerve Codina struct peb2466_lkup_ctrl *lkup_ctrl; 1190*227f609cSHerve Codina struct peb2466_lookup *lookup; 1191*227f609cSHerve Codina u8 (*table)[4]; 1192*227f609cSHerve Codina u32 table_size; 1193*227f609cSHerve Codina u32 init_index; 1194*227f609cSHerve Codina s32 min_val; 1195*227f609cSHerve Codina s32 step; 1196*227f609cSHerve Codina u8 mask; 1197*227f609cSHerve Codina int ret; 1198*227f609cSHerve Codina int i; 1199*227f609cSHerve Codina 1200*227f609cSHerve Codina /* 1201*227f609cSHerve Codina * AX_TABLE TLV data: 1202*227f609cSHerve Codina * - @0 1 byte: Chan mask (bit set means related channel is concerned) 1203*227f609cSHerve Codina * - @1 32bits signed: Min table value in centi dB (MinVal) 1204*227f609cSHerve Codina * ie -300 means -3.0 dB 1205*227f609cSHerve Codina * - @5 32bits signed: Step from on item to other item in centi dB (Step) 1206*227f609cSHerve Codina * ie 25 means 0.25 dB) 1207*227f609cSHerve Codina * - @9 32bits unsigned: Item index in the table to use for the initial 1208*227f609cSHerve Codina * value 1209*227f609cSHerve Codina * - @13 N*4 bytes: Table composed of 4 bytes items. 1210*227f609cSHerve Codina * Each item correspond to an AX filter value. 1211*227f609cSHerve Codina * 1212*227f609cSHerve Codina * The conversion from raw value item in the table to/from the value in 1213*227f609cSHerve Codina * dB is: Raw value at index i <-> (MinVal + i * Step) in centi dB. 1214*227f609cSHerve Codina */ 1215*227f609cSHerve Codina 1216*227f609cSHerve Codina /* Check Lng and extract the table size. */ 1217*227f609cSHerve Codina if (lng < 13 || ((lng - 13) % 4)) { 1218*227f609cSHerve Codina dev_err(component->dev, "fw AX table lng %u invalid\n", lng); 1219*227f609cSHerve Codina return -EINVAL; 1220*227f609cSHerve Codina } 1221*227f609cSHerve Codina table_size = lng - 13; 1222*227f609cSHerve Codina 1223*227f609cSHerve Codina min_val = get_unaligned_be32(data + 1); 1224*227f609cSHerve Codina step = get_unaligned_be32(data + 5); 1225*227f609cSHerve Codina init_index = get_unaligned_be32(data + 9); 1226*227f609cSHerve Codina if (init_index >= (table_size / 4)) { 1227*227f609cSHerve Codina dev_err(component->dev, "fw AX table index %u out of table[%u]\n", 1228*227f609cSHerve Codina init_index, table_size / 4); 1229*227f609cSHerve Codina return -EINVAL; 1230*227f609cSHerve Codina } 1231*227f609cSHerve Codina 1232*227f609cSHerve Codina dev_info(component->dev, 1233*227f609cSHerve Codina "fw AX table: mask %x, min %d, step %d, %u items, tbl[%u] %*phN\n", 1234*227f609cSHerve Codina *data, min_val, step, table_size / 4, init_index, 1235*227f609cSHerve Codina 4, data + 13 + (init_index * 4)); 1236*227f609cSHerve Codina 1237*227f609cSHerve Codina BUILD_BUG_ON(sizeof(*table) != 4); 1238*227f609cSHerve Codina table = devm_kzalloc(&peb2466->spi->dev, table_size, GFP_KERNEL); 1239*227f609cSHerve Codina if (!table) 1240*227f609cSHerve Codina return -ENOMEM; 1241*227f609cSHerve Codina memcpy(table, data + 13, table_size); 1242*227f609cSHerve Codina 1243*227f609cSHerve Codina mask = *data; 1244*227f609cSHerve Codina BUILD_BUG_ON(ARRAY_SIZE(peb2466_ax_ctrl_names) != ARRAY_SIZE(peb2466->ch)); 1245*227f609cSHerve Codina for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) { 1246*227f609cSHerve Codina if (!(mask & (1 << i))) 1247*227f609cSHerve Codina continue; 1248*227f609cSHerve Codina 1249*227f609cSHerve Codina lookup = &peb2466->ch[i].ax_lookup; 1250*227f609cSHerve Codina lookup->table = table; 1251*227f609cSHerve Codina lookup->count = table_size / 4; 1252*227f609cSHerve Codina 1253*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1254*227f609cSHerve Codina PEB2466_CR0_AX, 0); 1255*227f609cSHerve Codina if (ret) 1256*227f609cSHerve Codina return ret; 1257*227f609cSHerve Codina 1258*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_AX_FILTER(i), 1259*227f609cSHerve Codina lookup->table[init_index], 4); 1260*227f609cSHerve Codina if (ret) 1261*227f609cSHerve Codina return ret; 1262*227f609cSHerve Codina 1263*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1264*227f609cSHerve Codina PEB2466_CR0_AX, PEB2466_CR0_AX); 1265*227f609cSHerve Codina if (ret) 1266*227f609cSHerve Codina return ret; 1267*227f609cSHerve Codina 1268*227f609cSHerve Codina lkup_ctrl = &peb2466->ch[i].ax_lkup_ctrl; 1269*227f609cSHerve Codina lkup_ctrl->lookup = lookup; 1270*227f609cSHerve Codina lkup_ctrl->reg = PEB2466_AX_FILTER(i); 1271*227f609cSHerve Codina lkup_ctrl->index = init_index; 1272*227f609cSHerve Codina 1273*227f609cSHerve Codina ret = peb2466_add_lkup_ctrl(component, lkup_ctrl, 1274*227f609cSHerve Codina peb2466_ax_ctrl_names[i], 1275*227f609cSHerve Codina min_val, step); 1276*227f609cSHerve Codina if (ret) 1277*227f609cSHerve Codina return ret; 1278*227f609cSHerve Codina } 1279*227f609cSHerve Codina return 0; 1280*227f609cSHerve Codina } 1281*227f609cSHerve Codina 1282*227f609cSHerve Codina static const char * const peb2466_ar_ctrl_names[] = { 1283*227f609cSHerve Codina "DAC0 Playback Volume", 1284*227f609cSHerve Codina "DAC1 Playback Volume", 1285*227f609cSHerve Codina "DAC2 Playback Volume", 1286*227f609cSHerve Codina "DAC3 Playback Volume", 1287*227f609cSHerve Codina }; 1288*227f609cSHerve Codina 1289*227f609cSHerve Codina static int peb2466_fw_parse_artable(struct snd_soc_component *component, 1290*227f609cSHerve Codina u16 tag, u32 lng, const u8 *data) 1291*227f609cSHerve Codina { 1292*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component); 1293*227f609cSHerve Codina struct peb2466_lkup_ctrl *lkup_ctrl; 1294*227f609cSHerve Codina struct peb2466_lookup *lookup; 1295*227f609cSHerve Codina u8 (*table)[4]; 1296*227f609cSHerve Codina u32 table_size; 1297*227f609cSHerve Codina u32 init_index; 1298*227f609cSHerve Codina s32 min_val; 1299*227f609cSHerve Codina s32 step; 1300*227f609cSHerve Codina u8 mask; 1301*227f609cSHerve Codina int ret; 1302*227f609cSHerve Codina int i; 1303*227f609cSHerve Codina 1304*227f609cSHerve Codina /* 1305*227f609cSHerve Codina * AR_TABLE TLV data: 1306*227f609cSHerve Codina * - @0 1 byte: Chan mask (bit set means related channel is concerned) 1307*227f609cSHerve Codina * - @1 32bits signed: Min table value in centi dB (MinVal) 1308*227f609cSHerve Codina * ie -300 means -3.0 dB 1309*227f609cSHerve Codina * - @5 32bits signed: Step from on item to other item in centi dB (Step) 1310*227f609cSHerve Codina * ie 25 means 0.25 dB) 1311*227f609cSHerve Codina * - @9 32bits unsigned: Item index in the table to use for the initial 1312*227f609cSHerve Codina * value 1313*227f609cSHerve Codina * - @13 N*4 bytes: Table composed of 4 bytes items. 1314*227f609cSHerve Codina * Each item correspond to an AR filter value. 1315*227f609cSHerve Codina * 1316*227f609cSHerve Codina * The conversion from raw value item in the table to/from the value in 1317*227f609cSHerve Codina * dB is: Raw value at index i <-> (MinVal + i * Step) in centi dB. 1318*227f609cSHerve Codina */ 1319*227f609cSHerve Codina 1320*227f609cSHerve Codina /* Check Lng and extract the table size. */ 1321*227f609cSHerve Codina if (lng < 13 || ((lng - 13) % 4)) { 1322*227f609cSHerve Codina dev_err(component->dev, "fw AR table lng %u invalid\n", lng); 1323*227f609cSHerve Codina return -EINVAL; 1324*227f609cSHerve Codina } 1325*227f609cSHerve Codina table_size = lng - 13; 1326*227f609cSHerve Codina 1327*227f609cSHerve Codina min_val = get_unaligned_be32(data + 1); 1328*227f609cSHerve Codina step = get_unaligned_be32(data + 5); 1329*227f609cSHerve Codina init_index = get_unaligned_be32(data + 9); 1330*227f609cSHerve Codina if (init_index >= (table_size / 4)) { 1331*227f609cSHerve Codina dev_err(component->dev, "fw AR table index %u out of table[%u]\n", 1332*227f609cSHerve Codina init_index, table_size / 4); 1333*227f609cSHerve Codina return -EINVAL; 1334*227f609cSHerve Codina } 1335*227f609cSHerve Codina 1336*227f609cSHerve Codina dev_info(component->dev, 1337*227f609cSHerve Codina "fw AR table: mask %x, min %d, step %d, %u items, tbl[%u] %*phN\n", 1338*227f609cSHerve Codina *data, min_val, step, table_size / 4, init_index, 1339*227f609cSHerve Codina 4, data + 13 + (init_index * 4)); 1340*227f609cSHerve Codina 1341*227f609cSHerve Codina BUILD_BUG_ON(sizeof(*table) != 4); 1342*227f609cSHerve Codina table = devm_kzalloc(&peb2466->spi->dev, table_size, GFP_KERNEL); 1343*227f609cSHerve Codina if (!table) 1344*227f609cSHerve Codina return -ENOMEM; 1345*227f609cSHerve Codina memcpy(table, data + 13, table_size); 1346*227f609cSHerve Codina 1347*227f609cSHerve Codina mask = *data; 1348*227f609cSHerve Codina BUILD_BUG_ON(ARRAY_SIZE(peb2466_ar_ctrl_names) != ARRAY_SIZE(peb2466->ch)); 1349*227f609cSHerve Codina for (i = 0; i < ARRAY_SIZE(peb2466->ch); i++) { 1350*227f609cSHerve Codina if (!(mask & (1 << i))) 1351*227f609cSHerve Codina continue; 1352*227f609cSHerve Codina 1353*227f609cSHerve Codina lookup = &peb2466->ch[i].ar_lookup; 1354*227f609cSHerve Codina lookup->table = table; 1355*227f609cSHerve Codina lookup->count = table_size / 4; 1356*227f609cSHerve Codina 1357*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1358*227f609cSHerve Codina PEB2466_CR0_AR, 0); 1359*227f609cSHerve Codina if (ret) 1360*227f609cSHerve Codina return ret; 1361*227f609cSHerve Codina 1362*227f609cSHerve Codina ret = peb2466_write_buf(peb2466, PEB2466_AR_FILTER(i), 1363*227f609cSHerve Codina lookup->table[init_index], 4); 1364*227f609cSHerve Codina if (ret) 1365*227f609cSHerve Codina return ret; 1366*227f609cSHerve Codina 1367*227f609cSHerve Codina ret = regmap_update_bits(peb2466->regmap, PEB2466_CR0(i), 1368*227f609cSHerve Codina PEB2466_CR0_AR, PEB2466_CR0_AR); 1369*227f609cSHerve Codina if (ret) 1370*227f609cSHerve Codina return ret; 1371*227f609cSHerve Codina 1372*227f609cSHerve Codina lkup_ctrl = &peb2466->ch[i].ar_lkup_ctrl; 1373*227f609cSHerve Codina lkup_ctrl->lookup = lookup; 1374*227f609cSHerve Codina lkup_ctrl->reg = PEB2466_AR_FILTER(i); 1375*227f609cSHerve Codina lkup_ctrl->index = init_index; 1376*227f609cSHerve Codina 1377*227f609cSHerve Codina ret = peb2466_add_lkup_ctrl(component, lkup_ctrl, 1378*227f609cSHerve Codina peb2466_ar_ctrl_names[i], 1379*227f609cSHerve Codina min_val, step); 1380*227f609cSHerve Codina if (ret) 1381*227f609cSHerve Codina return ret; 1382*227f609cSHerve Codina } 1383*227f609cSHerve Codina return 0; 1384*227f609cSHerve Codina } 1385*227f609cSHerve Codina 1386*227f609cSHerve Codina struct peb2466_fw_tag_def { 1387*227f609cSHerve Codina u16 tag; 1388*227f609cSHerve Codina u32 lng_min; 1389*227f609cSHerve Codina u32 lng_max; 1390*227f609cSHerve Codina int (*parse)(struct snd_soc_component *component, 1391*227f609cSHerve Codina u16 tag, u32 lng, const u8 *data); 1392*227f609cSHerve Codina }; 1393*227f609cSHerve Codina 1394*227f609cSHerve Codina #define PEB2466_TAG_DEF_LNG_EQ(__tag, __lng, __parse) { \ 1395*227f609cSHerve Codina .tag = __tag, \ 1396*227f609cSHerve Codina .lng_min = __lng, \ 1397*227f609cSHerve Codina .lng_max = __lng, \ 1398*227f609cSHerve Codina .parse = __parse, \ 1399*227f609cSHerve Codina } 1400*227f609cSHerve Codina 1401*227f609cSHerve Codina #define PEB2466_TAG_DEF_LNG_MIN(__tag, __lng_min, __parse) { \ 1402*227f609cSHerve Codina .tag = __tag, \ 1403*227f609cSHerve Codina .lng_min = __lng_min, \ 1404*227f609cSHerve Codina .lng_max = U32_MAX, \ 1405*227f609cSHerve Codina .parse = __parse, \ 1406*227f609cSHerve Codina } 1407*227f609cSHerve Codina 1408*227f609cSHerve Codina static const struct peb2466_fw_tag_def peb2466_fw_tag_defs[] = { 1409*227f609cSHerve Codina /* TH FILTER */ 1410*227f609cSHerve Codina PEB2466_TAG_DEF_LNG_EQ(0x0001, 1 + 3 * 8, peb2466_fw_parse_thfilter), 1411*227f609cSHerve Codina /* IMR1 FILTER */ 1412*227f609cSHerve Codina PEB2466_TAG_DEF_LNG_EQ(0x0002, 1 + 2 * 8, peb2466_fw_parse_imr1filter), 1413*227f609cSHerve Codina /* FRX FILTER */ 1414*227f609cSHerve Codina PEB2466_TAG_DEF_LNG_EQ(0x0003, 1 + 8, peb2466_fw_parse_frxfilter), 1415*227f609cSHerve Codina /* FRR FILTER */ 1416*227f609cSHerve Codina PEB2466_TAG_DEF_LNG_EQ(0x0004, 1 + 8, peb2466_fw_parse_frrfilter), 1417*227f609cSHerve Codina /* AX FILTER */ 1418*227f609cSHerve Codina PEB2466_TAG_DEF_LNG_EQ(0x0005, 1 + 4, peb2466_fw_parse_axfilter), 1419*227f609cSHerve Codina /* AR FILTER */ 1420*227f609cSHerve Codina PEB2466_TAG_DEF_LNG_EQ(0x0006, 1 + 4, peb2466_fw_parse_arfilter), 1421*227f609cSHerve Codina /* AX TABLE */ 1422*227f609cSHerve Codina PEB2466_TAG_DEF_LNG_MIN(0x0105, 1 + 3 * 4, peb2466_fw_parse_axtable), 1423*227f609cSHerve Codina /* AR TABLE */ 1424*227f609cSHerve Codina PEB2466_TAG_DEF_LNG_MIN(0x0106, 1 + 3 * 4, peb2466_fw_parse_artable), 1425*227f609cSHerve Codina }; 1426*227f609cSHerve Codina 1427*227f609cSHerve Codina static const struct peb2466_fw_tag_def *peb2466_fw_get_tag_def(u16 tag) 1428*227f609cSHerve Codina { 1429*227f609cSHerve Codina int i; 1430*227f609cSHerve Codina 1431*227f609cSHerve Codina for (i = 0; i < ARRAY_SIZE(peb2466_fw_tag_defs); i++) { 1432*227f609cSHerve Codina if (peb2466_fw_tag_defs[i].tag == tag) 1433*227f609cSHerve Codina return &peb2466_fw_tag_defs[i]; 1434*227f609cSHerve Codina } 1435*227f609cSHerve Codina return NULL; 1436*227f609cSHerve Codina } 1437*227f609cSHerve Codina 1438*227f609cSHerve Codina static int peb2466_fw_parse(struct snd_soc_component *component, 1439*227f609cSHerve Codina const u8 *data, size_t size) 1440*227f609cSHerve Codina { 1441*227f609cSHerve Codina const struct peb2466_fw_tag_def *tag_def; 1442*227f609cSHerve Codina size_t left; 1443*227f609cSHerve Codina const u8 *buf; 1444*227f609cSHerve Codina u16 val16; 1445*227f609cSHerve Codina u16 tag; 1446*227f609cSHerve Codina u32 lng; 1447*227f609cSHerve Codina int ret; 1448*227f609cSHerve Codina 1449*227f609cSHerve Codina /* 1450*227f609cSHerve Codina * Coefficients firmware binary structure (16bits and 32bits are 1451*227f609cSHerve Codina * big-endian values). 1452*227f609cSHerve Codina * 1453*227f609cSHerve Codina * @0, 16bits: Magic (0x2466) 1454*227f609cSHerve Codina * @2, 16bits: Version (0x0100 for version 1.0) 1455*227f609cSHerve Codina * @4, 2+4+N bytes: TLV block 1456*227f609cSHerve Codina * @4+(2+4+N) bytes: Next TLV block 1457*227f609cSHerve Codina * ... 1458*227f609cSHerve Codina * 1459*227f609cSHerve Codina * Detail of a TLV block: 1460*227f609cSHerve Codina * @0, 16bits: Tag 1461*227f609cSHerve Codina * @2, 32bits: Lng 1462*227f609cSHerve Codina * @6, lng bytes: Data 1463*227f609cSHerve Codina * 1464*227f609cSHerve Codina * The detail the Data for a given TLV Tag is provided in the related 1465*227f609cSHerve Codina * parser. 1466*227f609cSHerve Codina */ 1467*227f609cSHerve Codina 1468*227f609cSHerve Codina left = size; 1469*227f609cSHerve Codina buf = data; 1470*227f609cSHerve Codina 1471*227f609cSHerve Codina if (left < 4) { 1472*227f609cSHerve Codina dev_err(component->dev, "fw size %zu, exp at least 4\n", left); 1473*227f609cSHerve Codina return -EINVAL; 1474*227f609cSHerve Codina } 1475*227f609cSHerve Codina 1476*227f609cSHerve Codina /* Check magic */ 1477*227f609cSHerve Codina val16 = get_unaligned_be16(buf); 1478*227f609cSHerve Codina if (val16 != 0x2466) { 1479*227f609cSHerve Codina dev_err(component->dev, "fw magic 0x%04x exp 0x2466\n", val16); 1480*227f609cSHerve Codina return -EINVAL; 1481*227f609cSHerve Codina } 1482*227f609cSHerve Codina buf += 2; 1483*227f609cSHerve Codina left -= 2; 1484*227f609cSHerve Codina 1485*227f609cSHerve Codina /* Check version */ 1486*227f609cSHerve Codina val16 = get_unaligned_be16(buf); 1487*227f609cSHerve Codina if (val16 != 0x0100) { 1488*227f609cSHerve Codina dev_err(component->dev, "fw magic 0x%04x exp 0x0100\n", val16); 1489*227f609cSHerve Codina return -EINVAL; 1490*227f609cSHerve Codina } 1491*227f609cSHerve Codina buf += 2; 1492*227f609cSHerve Codina left -= 2; 1493*227f609cSHerve Codina 1494*227f609cSHerve Codina while (left) { 1495*227f609cSHerve Codina if (left < 6) { 1496*227f609cSHerve Codina dev_err(component->dev, "fw %td/%zu left %zu, exp at least 6\n", 1497*227f609cSHerve Codina buf - data, size, left); 1498*227f609cSHerve Codina return -EINVAL; 1499*227f609cSHerve Codina } 1500*227f609cSHerve Codina /* Check tag and lng */ 1501*227f609cSHerve Codina tag = get_unaligned_be16(buf); 1502*227f609cSHerve Codina lng = get_unaligned_be32(buf + 2); 1503*227f609cSHerve Codina tag_def = peb2466_fw_get_tag_def(tag); 1504*227f609cSHerve Codina if (!tag_def) { 1505*227f609cSHerve Codina dev_err(component->dev, "fw %td/%zu tag 0x%04x unknown\n", 1506*227f609cSHerve Codina buf - data, size, tag); 1507*227f609cSHerve Codina return -EINVAL; 1508*227f609cSHerve Codina } 1509*227f609cSHerve Codina if (lng < tag_def->lng_min || lng > tag_def->lng_max) { 1510*227f609cSHerve Codina dev_err(component->dev, "fw %td/%zu tag 0x%04x lng %u, exp [%u;%u]\n", 1511*227f609cSHerve Codina buf - data, size, tag, lng, tag_def->lng_min, tag_def->lng_max); 1512*227f609cSHerve Codina return -EINVAL; 1513*227f609cSHerve Codina } 1514*227f609cSHerve Codina buf += 6; 1515*227f609cSHerve Codina left -= 6; 1516*227f609cSHerve Codina if (left < lng) { 1517*227f609cSHerve Codina dev_err(component->dev, "fw %td/%zu tag 0x%04x lng %u, left %zu\n", 1518*227f609cSHerve Codina buf - data, size, tag, lng, left); 1519*227f609cSHerve Codina return -EINVAL; 1520*227f609cSHerve Codina } 1521*227f609cSHerve Codina 1522*227f609cSHerve Codina /* TLV block is valid -> parse the data part */ 1523*227f609cSHerve Codina ret = tag_def->parse(component, tag, lng, buf); 1524*227f609cSHerve Codina if (ret) { 1525*227f609cSHerve Codina dev_err(component->dev, "fw %td/%zu tag 0x%04x lng %u parse failed\n", 1526*227f609cSHerve Codina buf - data, size, tag, lng); 1527*227f609cSHerve Codina return ret; 1528*227f609cSHerve Codina } 1529*227f609cSHerve Codina 1530*227f609cSHerve Codina buf += lng; 1531*227f609cSHerve Codina left -= lng; 1532*227f609cSHerve Codina } 1533*227f609cSHerve Codina return 0; 1534*227f609cSHerve Codina } 1535*227f609cSHerve Codina 1536*227f609cSHerve Codina static int peb2466_load_coeffs(struct snd_soc_component *component, const char *fw_name) 1537*227f609cSHerve Codina { 1538*227f609cSHerve Codina const struct firmware *fw; 1539*227f609cSHerve Codina int ret; 1540*227f609cSHerve Codina 1541*227f609cSHerve Codina ret = request_firmware(&fw, fw_name, component->dev); 1542*227f609cSHerve Codina if (ret) 1543*227f609cSHerve Codina return ret; 1544*227f609cSHerve Codina 1545*227f609cSHerve Codina ret = peb2466_fw_parse(component, fw->data, fw->size); 1546*227f609cSHerve Codina release_firmware(fw); 1547*227f609cSHerve Codina 1548*227f609cSHerve Codina return ret; 1549*227f609cSHerve Codina } 1550*227f609cSHerve Codina 1551*227f609cSHerve Codina static int peb2466_component_probe(struct snd_soc_component *component) 1552*227f609cSHerve Codina { 1553*227f609cSHerve Codina struct peb2466 *peb2466 = snd_soc_component_get_drvdata(component); 1554*227f609cSHerve Codina const char *firmware_name; 1555*227f609cSHerve Codina int ret; 1556*227f609cSHerve Codina 1557*227f609cSHerve Codina /* reset peb2466 audio part */ 1558*227f609cSHerve Codina ret = peb2466_reset_audio(peb2466); 1559*227f609cSHerve Codina if (ret) 1560*227f609cSHerve Codina return ret; 1561*227f609cSHerve Codina 1562*227f609cSHerve Codina ret = of_property_read_string(peb2466->spi->dev.of_node, 1563*227f609cSHerve Codina "firmware-name", &firmware_name); 1564*227f609cSHerve Codina if (ret) 1565*227f609cSHerve Codina return (ret == -EINVAL) ? 0 : ret; 1566*227f609cSHerve Codina 1567*227f609cSHerve Codina return peb2466_load_coeffs(component, firmware_name); 1568*227f609cSHerve Codina } 1569*227f609cSHerve Codina 1570*227f609cSHerve Codina static const struct snd_soc_component_driver peb2466_component_driver = { 1571*227f609cSHerve Codina .probe = peb2466_component_probe, 1572*227f609cSHerve Codina .controls = peb2466_controls, 1573*227f609cSHerve Codina .num_controls = ARRAY_SIZE(peb2466_controls), 1574*227f609cSHerve Codina .dapm_widgets = peb2466_dapm_widgets, 1575*227f609cSHerve Codina .num_dapm_widgets = ARRAY_SIZE(peb2466_dapm_widgets), 1576*227f609cSHerve Codina .dapm_routes = peb2466_dapm_routes, 1577*227f609cSHerve Codina .num_dapm_routes = ARRAY_SIZE(peb2466_dapm_routes), 1578*227f609cSHerve Codina .endianness = 1, 1579*227f609cSHerve Codina }; 1580*227f609cSHerve Codina 1581*227f609cSHerve Codina /* 1582*227f609cSHerve Codina * The mapping used for the relationship between the gpio offset and the 1583*227f609cSHerve Codina * physical pin is the following: 1584*227f609cSHerve Codina * 1585*227f609cSHerve Codina * offset pin 1586*227f609cSHerve Codina * 0 SI1_0 1587*227f609cSHerve Codina * 1 SI1_1 1588*227f609cSHerve Codina * 2 SI2_0 1589*227f609cSHerve Codina * 3 SI2_1 1590*227f609cSHerve Codina * 4 SI3_0 1591*227f609cSHerve Codina * 5 SI3_1 1592*227f609cSHerve Codina * 6 SI4_0 1593*227f609cSHerve Codina * 7 SI4_1 1594*227f609cSHerve Codina * 8 SO1_0 1595*227f609cSHerve Codina * 9 SO1_1 1596*227f609cSHerve Codina * 10 SO2_0 1597*227f609cSHerve Codina * 11 SO2_1 1598*227f609cSHerve Codina * 12 SO3_0 1599*227f609cSHerve Codina * 13 SO3_1 1600*227f609cSHerve Codina * 14 SO4_0 1601*227f609cSHerve Codina * 15 SO4_1 1602*227f609cSHerve Codina * 16 SB1_0 1603*227f609cSHerve Codina * 17 SB1_1 1604*227f609cSHerve Codina * 18 SB2_0 1605*227f609cSHerve Codina * 19 SB2_1 1606*227f609cSHerve Codina * 20 SB3_0 1607*227f609cSHerve Codina * 21 SB3_1 1608*227f609cSHerve Codina * 22 SB4_0 1609*227f609cSHerve Codina * 23 SB4_1 1610*227f609cSHerve Codina * 24 SB1_2 1611*227f609cSHerve Codina * 25 SB2_2 1612*227f609cSHerve Codina * 26 SB3_2 1613*227f609cSHerve Codina * 27 SB4_2 1614*227f609cSHerve Codina */ 1615*227f609cSHerve Codina 1616*227f609cSHerve Codina static int peb2466_chip_gpio_offset_to_data_regmask(unsigned int offset, 1617*227f609cSHerve Codina unsigned int *xr_reg, 1618*227f609cSHerve Codina unsigned int *mask) 1619*227f609cSHerve Codina { 1620*227f609cSHerve Codina if (offset < 16) { 1621*227f609cSHerve Codina /* 1622*227f609cSHerve Codina * SIx_{0,1} and SOx_{0,1} 1623*227f609cSHerve Codina * Read accesses read SIx_{0,1} values 1624*227f609cSHerve Codina * Write accesses write SOx_{0,1} values 1625*227f609cSHerve Codina */ 1626*227f609cSHerve Codina *xr_reg = PEB2466_XR0; 1627*227f609cSHerve Codina *mask = (1 << (offset % 8)); 1628*227f609cSHerve Codina return 0; 1629*227f609cSHerve Codina } 1630*227f609cSHerve Codina if (offset < 24) { 1631*227f609cSHerve Codina /* SBx_{0,1} */ 1632*227f609cSHerve Codina *xr_reg = PEB2466_XR1; 1633*227f609cSHerve Codina *mask = (1 << (offset - 16)); 1634*227f609cSHerve Codina return 0; 1635*227f609cSHerve Codina } 1636*227f609cSHerve Codina if (offset < 28) { 1637*227f609cSHerve Codina /* SBx_2 */ 1638*227f609cSHerve Codina *xr_reg = PEB2466_XR3; 1639*227f609cSHerve Codina *mask = (1 << (offset - 24 + 4)); 1640*227f609cSHerve Codina return 0; 1641*227f609cSHerve Codina } 1642*227f609cSHerve Codina return -EINVAL; 1643*227f609cSHerve Codina } 1644*227f609cSHerve Codina 1645*227f609cSHerve Codina static int peb2466_chip_gpio_offset_to_dir_regmask(unsigned int offset, 1646*227f609cSHerve Codina unsigned int *xr_reg, 1647*227f609cSHerve Codina unsigned int *mask) 1648*227f609cSHerve Codina { 1649*227f609cSHerve Codina if (offset < 16) { 1650*227f609cSHerve Codina /* Direction cannot be changed for these GPIOs */ 1651*227f609cSHerve Codina return -EINVAL; 1652*227f609cSHerve Codina } 1653*227f609cSHerve Codina if (offset < 24) { 1654*227f609cSHerve Codina *xr_reg = PEB2466_XR2; 1655*227f609cSHerve Codina *mask = (1 << (offset - 16)); 1656*227f609cSHerve Codina return 0; 1657*227f609cSHerve Codina } 1658*227f609cSHerve Codina if (offset < 28) { 1659*227f609cSHerve Codina *xr_reg = PEB2466_XR3; 1660*227f609cSHerve Codina *mask = (1 << (offset - 24)); 1661*227f609cSHerve Codina return 0; 1662*227f609cSHerve Codina } 1663*227f609cSHerve Codina return -EINVAL; 1664*227f609cSHerve Codina } 1665*227f609cSHerve Codina 1666*227f609cSHerve Codina static unsigned int *peb2466_chip_gpio_get_cache(struct peb2466 *peb2466, 1667*227f609cSHerve Codina unsigned int xr_reg) 1668*227f609cSHerve Codina { 1669*227f609cSHerve Codina unsigned int *cache; 1670*227f609cSHerve Codina 1671*227f609cSHerve Codina switch (xr_reg) { 1672*227f609cSHerve Codina case PEB2466_XR0: 1673*227f609cSHerve Codina cache = &peb2466->gpio.cache.xr0; 1674*227f609cSHerve Codina break; 1675*227f609cSHerve Codina case PEB2466_XR1: 1676*227f609cSHerve Codina cache = &peb2466->gpio.cache.xr1; 1677*227f609cSHerve Codina break; 1678*227f609cSHerve Codina case PEB2466_XR2: 1679*227f609cSHerve Codina cache = &peb2466->gpio.cache.xr2; 1680*227f609cSHerve Codina break; 1681*227f609cSHerve Codina case PEB2466_XR3: 1682*227f609cSHerve Codina cache = &peb2466->gpio.cache.xr3; 1683*227f609cSHerve Codina break; 1684*227f609cSHerve Codina default: 1685*227f609cSHerve Codina cache = NULL; 1686*227f609cSHerve Codina break; 1687*227f609cSHerve Codina } 1688*227f609cSHerve Codina return cache; 1689*227f609cSHerve Codina } 1690*227f609cSHerve Codina 1691*227f609cSHerve Codina static int peb2466_chip_gpio_update_bits(struct peb2466 *peb2466, unsigned int xr_reg, 1692*227f609cSHerve Codina unsigned int mask, unsigned int val) 1693*227f609cSHerve Codina { 1694*227f609cSHerve Codina unsigned int tmp; 1695*227f609cSHerve Codina unsigned int *cache; 1696*227f609cSHerve Codina int ret; 1697*227f609cSHerve Codina 1698*227f609cSHerve Codina /* 1699*227f609cSHerve Codina * Read and write accesses use different peb2466 internal signals (input 1700*227f609cSHerve Codina * signals on reads and output signals on writes). regmap_update_bits 1701*227f609cSHerve Codina * cannot be used to read/modify/write the value. 1702*227f609cSHerve Codina * So, a specific cache value is used. 1703*227f609cSHerve Codina */ 1704*227f609cSHerve Codina 1705*227f609cSHerve Codina mutex_lock(&peb2466->gpio.lock); 1706*227f609cSHerve Codina 1707*227f609cSHerve Codina cache = peb2466_chip_gpio_get_cache(peb2466, xr_reg); 1708*227f609cSHerve Codina if (!cache) { 1709*227f609cSHerve Codina ret = -EINVAL; 1710*227f609cSHerve Codina goto end; 1711*227f609cSHerve Codina } 1712*227f609cSHerve Codina 1713*227f609cSHerve Codina tmp = *cache; 1714*227f609cSHerve Codina tmp &= ~mask; 1715*227f609cSHerve Codina tmp |= val; 1716*227f609cSHerve Codina 1717*227f609cSHerve Codina ret = regmap_write(peb2466->regmap, xr_reg, tmp); 1718*227f609cSHerve Codina if (ret) 1719*227f609cSHerve Codina goto end; 1720*227f609cSHerve Codina 1721*227f609cSHerve Codina *cache = tmp; 1722*227f609cSHerve Codina ret = 0; 1723*227f609cSHerve Codina 1724*227f609cSHerve Codina end: 1725*227f609cSHerve Codina mutex_unlock(&peb2466->gpio.lock); 1726*227f609cSHerve Codina return ret; 1727*227f609cSHerve Codina } 1728*227f609cSHerve Codina 1729*227f609cSHerve Codina static void peb2466_chip_gpio_set(struct gpio_chip *c, unsigned int offset, int val) 1730*227f609cSHerve Codina { 1731*227f609cSHerve Codina struct peb2466 *peb2466 = gpiochip_get_data(c); 1732*227f609cSHerve Codina unsigned int xr_reg; 1733*227f609cSHerve Codina unsigned int mask; 1734*227f609cSHerve Codina int ret; 1735*227f609cSHerve Codina 1736*227f609cSHerve Codina if (offset < 8) { 1737*227f609cSHerve Codina /* 1738*227f609cSHerve Codina * SIx_{0,1} signals cannot be set and writing the related 1739*227f609cSHerve Codina * register will change the SOx_{0,1} signals 1740*227f609cSHerve Codina */ 1741*227f609cSHerve Codina dev_warn(&peb2466->spi->dev, "cannot set gpio %d (read-only)\n", 1742*227f609cSHerve Codina offset); 1743*227f609cSHerve Codina return; 1744*227f609cSHerve Codina } 1745*227f609cSHerve Codina 1746*227f609cSHerve Codina ret = peb2466_chip_gpio_offset_to_data_regmask(offset, &xr_reg, &mask); 1747*227f609cSHerve Codina if (ret) { 1748*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "cannot set gpio %d (%d)\n", 1749*227f609cSHerve Codina offset, ret); 1750*227f609cSHerve Codina return; 1751*227f609cSHerve Codina } 1752*227f609cSHerve Codina 1753*227f609cSHerve Codina ret = peb2466_chip_gpio_update_bits(peb2466, xr_reg, mask, val ? mask : 0); 1754*227f609cSHerve Codina if (ret) { 1755*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "set gpio %d (0x%x, 0x%x) failed (%d)\n", 1756*227f609cSHerve Codina offset, xr_reg, mask, ret); 1757*227f609cSHerve Codina } 1758*227f609cSHerve Codina } 1759*227f609cSHerve Codina 1760*227f609cSHerve Codina static int peb2466_chip_gpio_get(struct gpio_chip *c, unsigned int offset) 1761*227f609cSHerve Codina { 1762*227f609cSHerve Codina struct peb2466 *peb2466 = gpiochip_get_data(c); 1763*227f609cSHerve Codina bool use_cache = false; 1764*227f609cSHerve Codina unsigned int *cache; 1765*227f609cSHerve Codina unsigned int xr_reg; 1766*227f609cSHerve Codina unsigned int mask; 1767*227f609cSHerve Codina unsigned int val; 1768*227f609cSHerve Codina int ret; 1769*227f609cSHerve Codina 1770*227f609cSHerve Codina if (offset >= 8 && offset < 16) { 1771*227f609cSHerve Codina /* 1772*227f609cSHerve Codina * SOx_{0,1} signals cannot be read. Reading the related 1773*227f609cSHerve Codina * register will read the SIx_{0,1} signals. 1774*227f609cSHerve Codina * Use the cache to get value; 1775*227f609cSHerve Codina */ 1776*227f609cSHerve Codina use_cache = true; 1777*227f609cSHerve Codina } 1778*227f609cSHerve Codina 1779*227f609cSHerve Codina ret = peb2466_chip_gpio_offset_to_data_regmask(offset, &xr_reg, &mask); 1780*227f609cSHerve Codina if (ret) { 1781*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "cannot get gpio %d (%d)\n", 1782*227f609cSHerve Codina offset, ret); 1783*227f609cSHerve Codina return -EINVAL; 1784*227f609cSHerve Codina } 1785*227f609cSHerve Codina 1786*227f609cSHerve Codina if (use_cache) { 1787*227f609cSHerve Codina cache = peb2466_chip_gpio_get_cache(peb2466, xr_reg); 1788*227f609cSHerve Codina if (!cache) 1789*227f609cSHerve Codina return -EINVAL; 1790*227f609cSHerve Codina val = *cache; 1791*227f609cSHerve Codina } else { 1792*227f609cSHerve Codina ret = regmap_read(peb2466->regmap, xr_reg, &val); 1793*227f609cSHerve Codina if (ret) { 1794*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "get gpio %d (0x%x, 0x%x) failed (%d)\n", 1795*227f609cSHerve Codina offset, xr_reg, mask, ret); 1796*227f609cSHerve Codina return ret; 1797*227f609cSHerve Codina } 1798*227f609cSHerve Codina } 1799*227f609cSHerve Codina 1800*227f609cSHerve Codina return !!(val & mask); 1801*227f609cSHerve Codina } 1802*227f609cSHerve Codina 1803*227f609cSHerve Codina static int peb2466_chip_get_direction(struct gpio_chip *c, unsigned int offset) 1804*227f609cSHerve Codina { 1805*227f609cSHerve Codina struct peb2466 *peb2466 = gpiochip_get_data(c); 1806*227f609cSHerve Codina unsigned int xr_reg; 1807*227f609cSHerve Codina unsigned int mask; 1808*227f609cSHerve Codina unsigned int val; 1809*227f609cSHerve Codina int ret; 1810*227f609cSHerve Codina 1811*227f609cSHerve Codina if (offset < 8) { 1812*227f609cSHerve Codina /* SIx_{0,1} */ 1813*227f609cSHerve Codina return GPIO_LINE_DIRECTION_IN; 1814*227f609cSHerve Codina } 1815*227f609cSHerve Codina if (offset < 16) { 1816*227f609cSHerve Codina /* SOx_{0,1} */ 1817*227f609cSHerve Codina return GPIO_LINE_DIRECTION_OUT; 1818*227f609cSHerve Codina } 1819*227f609cSHerve Codina 1820*227f609cSHerve Codina ret = peb2466_chip_gpio_offset_to_dir_regmask(offset, &xr_reg, &mask); 1821*227f609cSHerve Codina if (ret) { 1822*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "cannot get gpio %d direction (%d)\n", 1823*227f609cSHerve Codina offset, ret); 1824*227f609cSHerve Codina return ret; 1825*227f609cSHerve Codina } 1826*227f609cSHerve Codina 1827*227f609cSHerve Codina ret = regmap_read(peb2466->regmap, xr_reg, &val); 1828*227f609cSHerve Codina if (ret) { 1829*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "get dir gpio %d (0x%x, 0x%x) failed (%d)\n", 1830*227f609cSHerve Codina offset, xr_reg, mask, ret); 1831*227f609cSHerve Codina return ret; 1832*227f609cSHerve Codina } 1833*227f609cSHerve Codina 1834*227f609cSHerve Codina return val & mask ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN; 1835*227f609cSHerve Codina } 1836*227f609cSHerve Codina 1837*227f609cSHerve Codina static int peb2466_chip_direction_input(struct gpio_chip *c, unsigned int offset) 1838*227f609cSHerve Codina { 1839*227f609cSHerve Codina struct peb2466 *peb2466 = gpiochip_get_data(c); 1840*227f609cSHerve Codina unsigned int xr_reg; 1841*227f609cSHerve Codina unsigned int mask; 1842*227f609cSHerve Codina int ret; 1843*227f609cSHerve Codina 1844*227f609cSHerve Codina if (offset < 8) { 1845*227f609cSHerve Codina /* SIx_{0,1} */ 1846*227f609cSHerve Codina return 0; 1847*227f609cSHerve Codina } 1848*227f609cSHerve Codina if (offset < 16) { 1849*227f609cSHerve Codina /* SOx_{0,1} */ 1850*227f609cSHerve Codina return -EINVAL; 1851*227f609cSHerve Codina }; 1852*227f609cSHerve Codina 1853*227f609cSHerve Codina ret = peb2466_chip_gpio_offset_to_dir_regmask(offset, &xr_reg, &mask); 1854*227f609cSHerve Codina if (ret) { 1855*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "cannot set gpio %d direction (%d)\n", 1856*227f609cSHerve Codina offset, ret); 1857*227f609cSHerve Codina return ret; 1858*227f609cSHerve Codina } 1859*227f609cSHerve Codina 1860*227f609cSHerve Codina ret = peb2466_chip_gpio_update_bits(peb2466, xr_reg, mask, 0); 1861*227f609cSHerve Codina if (ret) { 1862*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "Set dir in gpio %d (0x%x, 0x%x) failed (%d)\n", 1863*227f609cSHerve Codina offset, xr_reg, mask, ret); 1864*227f609cSHerve Codina return ret; 1865*227f609cSHerve Codina } 1866*227f609cSHerve Codina 1867*227f609cSHerve Codina return 0; 1868*227f609cSHerve Codina } 1869*227f609cSHerve Codina 1870*227f609cSHerve Codina static int peb2466_chip_direction_output(struct gpio_chip *c, unsigned int offset, int val) 1871*227f609cSHerve Codina { 1872*227f609cSHerve Codina struct peb2466 *peb2466 = gpiochip_get_data(c); 1873*227f609cSHerve Codina unsigned int xr_reg; 1874*227f609cSHerve Codina unsigned int mask; 1875*227f609cSHerve Codina int ret; 1876*227f609cSHerve Codina 1877*227f609cSHerve Codina if (offset < 8) { 1878*227f609cSHerve Codina /* SIx_{0,1} */ 1879*227f609cSHerve Codina return -EINVAL; 1880*227f609cSHerve Codina } 1881*227f609cSHerve Codina 1882*227f609cSHerve Codina peb2466_chip_gpio_set(c, offset, val); 1883*227f609cSHerve Codina 1884*227f609cSHerve Codina if (offset < 16) { 1885*227f609cSHerve Codina /* SOx_{0,1} */ 1886*227f609cSHerve Codina return 0; 1887*227f609cSHerve Codina }; 1888*227f609cSHerve Codina 1889*227f609cSHerve Codina ret = peb2466_chip_gpio_offset_to_dir_regmask(offset, &xr_reg, &mask); 1890*227f609cSHerve Codina if (ret) { 1891*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "cannot set gpio %d direction (%d)\n", 1892*227f609cSHerve Codina offset, ret); 1893*227f609cSHerve Codina return ret; 1894*227f609cSHerve Codina } 1895*227f609cSHerve Codina 1896*227f609cSHerve Codina ret = peb2466_chip_gpio_update_bits(peb2466, xr_reg, mask, mask); 1897*227f609cSHerve Codina if (ret) { 1898*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "Set dir in gpio %d (0x%x, 0x%x) failed (%d)\n", 1899*227f609cSHerve Codina offset, xr_reg, mask, ret); 1900*227f609cSHerve Codina return ret; 1901*227f609cSHerve Codina } 1902*227f609cSHerve Codina 1903*227f609cSHerve Codina return 0; 1904*227f609cSHerve Codina } 1905*227f609cSHerve Codina 1906*227f609cSHerve Codina static int peb2466_reset_gpio(struct peb2466 *peb2466) 1907*227f609cSHerve Codina { 1908*227f609cSHerve Codina static const struct reg_sequence reg_reset[] = { 1909*227f609cSHerve Codina /* Output pins at 0, input/output pins as input */ 1910*227f609cSHerve Codina { .reg = PEB2466_XR0, .def = 0 }, 1911*227f609cSHerve Codina { .reg = PEB2466_XR1, .def = 0 }, 1912*227f609cSHerve Codina { .reg = PEB2466_XR2, .def = 0 }, 1913*227f609cSHerve Codina { .reg = PEB2466_XR3, .def = 0 }, 1914*227f609cSHerve Codina }; 1915*227f609cSHerve Codina 1916*227f609cSHerve Codina peb2466->gpio.cache.xr0 = 0; 1917*227f609cSHerve Codina peb2466->gpio.cache.xr1 = 0; 1918*227f609cSHerve Codina peb2466->gpio.cache.xr2 = 0; 1919*227f609cSHerve Codina peb2466->gpio.cache.xr3 = 0; 1920*227f609cSHerve Codina 1921*227f609cSHerve Codina return regmap_multi_reg_write(peb2466->regmap, reg_reset, ARRAY_SIZE(reg_reset)); 1922*227f609cSHerve Codina } 1923*227f609cSHerve Codina 1924*227f609cSHerve Codina static int peb2466_gpio_init(struct peb2466 *peb2466) 1925*227f609cSHerve Codina { 1926*227f609cSHerve Codina int ret; 1927*227f609cSHerve Codina 1928*227f609cSHerve Codina mutex_init(&peb2466->gpio.lock); 1929*227f609cSHerve Codina 1930*227f609cSHerve Codina ret = peb2466_reset_gpio(peb2466); 1931*227f609cSHerve Codina if (ret) 1932*227f609cSHerve Codina return ret; 1933*227f609cSHerve Codina 1934*227f609cSHerve Codina peb2466->gpio.gpio_chip.owner = THIS_MODULE; 1935*227f609cSHerve Codina peb2466->gpio.gpio_chip.label = dev_name(&peb2466->spi->dev); 1936*227f609cSHerve Codina peb2466->gpio.gpio_chip.parent = &peb2466->spi->dev; 1937*227f609cSHerve Codina peb2466->gpio.gpio_chip.base = -1; 1938*227f609cSHerve Codina peb2466->gpio.gpio_chip.ngpio = 28; 1939*227f609cSHerve Codina peb2466->gpio.gpio_chip.get_direction = peb2466_chip_get_direction; 1940*227f609cSHerve Codina peb2466->gpio.gpio_chip.direction_input = peb2466_chip_direction_input; 1941*227f609cSHerve Codina peb2466->gpio.gpio_chip.direction_output = peb2466_chip_direction_output; 1942*227f609cSHerve Codina peb2466->gpio.gpio_chip.get = peb2466_chip_gpio_get; 1943*227f609cSHerve Codina peb2466->gpio.gpio_chip.set = peb2466_chip_gpio_set; 1944*227f609cSHerve Codina peb2466->gpio.gpio_chip.can_sleep = true; 1945*227f609cSHerve Codina 1946*227f609cSHerve Codina return devm_gpiochip_add_data(&peb2466->spi->dev, &peb2466->gpio.gpio_chip, 1947*227f609cSHerve Codina peb2466); 1948*227f609cSHerve Codina } 1949*227f609cSHerve Codina 1950*227f609cSHerve Codina static int peb2466_spi_probe(struct spi_device *spi) 1951*227f609cSHerve Codina { 1952*227f609cSHerve Codina struct peb2466 *peb2466; 1953*227f609cSHerve Codina unsigned long mclk_rate; 1954*227f609cSHerve Codina int ret; 1955*227f609cSHerve Codina u8 xr5; 1956*227f609cSHerve Codina 1957*227f609cSHerve Codina spi->bits_per_word = 8; 1958*227f609cSHerve Codina ret = spi_setup(spi); 1959*227f609cSHerve Codina if (ret < 0) 1960*227f609cSHerve Codina return ret; 1961*227f609cSHerve Codina 1962*227f609cSHerve Codina peb2466 = devm_kzalloc(&spi->dev, sizeof(*peb2466), GFP_KERNEL); 1963*227f609cSHerve Codina if (!peb2466) 1964*227f609cSHerve Codina return -ENOMEM; 1965*227f609cSHerve Codina 1966*227f609cSHerve Codina peb2466->spi = spi; 1967*227f609cSHerve Codina 1968*227f609cSHerve Codina peb2466->regmap = devm_regmap_init(&peb2466->spi->dev, NULL, peb2466, 1969*227f609cSHerve Codina &peb2466_regmap_config); 1970*227f609cSHerve Codina if (IS_ERR(peb2466->regmap)) 1971*227f609cSHerve Codina return PTR_ERR(peb2466->regmap); 1972*227f609cSHerve Codina 1973*227f609cSHerve Codina peb2466->reset_gpio = devm_gpiod_get_optional(&peb2466->spi->dev, 1974*227f609cSHerve Codina "reset", GPIOD_OUT_LOW); 1975*227f609cSHerve Codina if (IS_ERR(peb2466->reset_gpio)) 1976*227f609cSHerve Codina return PTR_ERR(peb2466->reset_gpio); 1977*227f609cSHerve Codina 1978*227f609cSHerve Codina peb2466->mclk = devm_clk_get(&peb2466->spi->dev, "mclk"); 1979*227f609cSHerve Codina if (IS_ERR(peb2466->mclk)) 1980*227f609cSHerve Codina return PTR_ERR(peb2466->mclk); 1981*227f609cSHerve Codina ret = clk_prepare_enable(peb2466->mclk); 1982*227f609cSHerve Codina if (ret) 1983*227f609cSHerve Codina return ret; 1984*227f609cSHerve Codina 1985*227f609cSHerve Codina if (peb2466->reset_gpio) { 1986*227f609cSHerve Codina gpiod_set_value_cansleep(peb2466->reset_gpio, 1); 1987*227f609cSHerve Codina udelay(4); 1988*227f609cSHerve Codina gpiod_set_value_cansleep(peb2466->reset_gpio, 0); 1989*227f609cSHerve Codina udelay(4); 1990*227f609cSHerve Codina } 1991*227f609cSHerve Codina 1992*227f609cSHerve Codina spi_set_drvdata(spi, peb2466); 1993*227f609cSHerve Codina 1994*227f609cSHerve Codina mclk_rate = clk_get_rate(peb2466->mclk); 1995*227f609cSHerve Codina switch (mclk_rate) { 1996*227f609cSHerve Codina case 1536000: 1997*227f609cSHerve Codina xr5 = PEB2466_XR5_MCLK_1536; 1998*227f609cSHerve Codina break; 1999*227f609cSHerve Codina case 2048000: 2000*227f609cSHerve Codina xr5 = PEB2466_XR5_MCLK_2048; 2001*227f609cSHerve Codina break; 2002*227f609cSHerve Codina case 4096000: 2003*227f609cSHerve Codina xr5 = PEB2466_XR5_MCLK_4096; 2004*227f609cSHerve Codina break; 2005*227f609cSHerve Codina case 8192000: 2006*227f609cSHerve Codina xr5 = PEB2466_XR5_MCLK_8192; 2007*227f609cSHerve Codina break; 2008*227f609cSHerve Codina default: 2009*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "Unsupported clock rate %lu\n", 2010*227f609cSHerve Codina mclk_rate); 2011*227f609cSHerve Codina ret = -EINVAL; 2012*227f609cSHerve Codina goto failed; 2013*227f609cSHerve Codina } 2014*227f609cSHerve Codina ret = regmap_write(peb2466->regmap, PEB2466_XR5, xr5); 2015*227f609cSHerve Codina if (ret) { 2016*227f609cSHerve Codina dev_err(&peb2466->spi->dev, "Setting MCLK failed (%d)\n", ret); 2017*227f609cSHerve Codina goto failed; 2018*227f609cSHerve Codina } 2019*227f609cSHerve Codina 2020*227f609cSHerve Codina ret = devm_snd_soc_register_component(&spi->dev, &peb2466_component_driver, 2021*227f609cSHerve Codina &peb2466_dai_driver, 1); 2022*227f609cSHerve Codina if (ret) 2023*227f609cSHerve Codina goto failed; 2024*227f609cSHerve Codina 2025*227f609cSHerve Codina if (IS_ENABLED(CONFIG_GPIOLIB)) { 2026*227f609cSHerve Codina ret = peb2466_gpio_init(peb2466); 2027*227f609cSHerve Codina if (ret) 2028*227f609cSHerve Codina goto failed; 2029*227f609cSHerve Codina } 2030*227f609cSHerve Codina 2031*227f609cSHerve Codina return 0; 2032*227f609cSHerve Codina 2033*227f609cSHerve Codina failed: 2034*227f609cSHerve Codina clk_disable_unprepare(peb2466->mclk); 2035*227f609cSHerve Codina return ret; 2036*227f609cSHerve Codina } 2037*227f609cSHerve Codina 2038*227f609cSHerve Codina static void peb2466_spi_remove(struct spi_device *spi) 2039*227f609cSHerve Codina { 2040*227f609cSHerve Codina struct peb2466 *peb2466 = spi_get_drvdata(spi); 2041*227f609cSHerve Codina 2042*227f609cSHerve Codina clk_disable_unprepare(peb2466->mclk); 2043*227f609cSHerve Codina } 2044*227f609cSHerve Codina 2045*227f609cSHerve Codina static const struct of_device_id peb2466_of_match[] = { 2046*227f609cSHerve Codina { .compatible = "infineon,peb2466", }, 2047*227f609cSHerve Codina { } 2048*227f609cSHerve Codina }; 2049*227f609cSHerve Codina MODULE_DEVICE_TABLE(of, peb2466_of_match); 2050*227f609cSHerve Codina 2051*227f609cSHerve Codina static const struct spi_device_id peb2466_id_table[] = { 2052*227f609cSHerve Codina { "peb2466", 0 }, 2053*227f609cSHerve Codina { } 2054*227f609cSHerve Codina }; 2055*227f609cSHerve Codina MODULE_DEVICE_TABLE(spi, peb2466_id_table); 2056*227f609cSHerve Codina 2057*227f609cSHerve Codina static struct spi_driver peb2466_spi_driver = { 2058*227f609cSHerve Codina .driver = { 2059*227f609cSHerve Codina .name = "peb2466", 2060*227f609cSHerve Codina .of_match_table = peb2466_of_match, 2061*227f609cSHerve Codina }, 2062*227f609cSHerve Codina .id_table = peb2466_id_table, 2063*227f609cSHerve Codina .probe = peb2466_spi_probe, 2064*227f609cSHerve Codina .remove = peb2466_spi_remove, 2065*227f609cSHerve Codina }; 2066*227f609cSHerve Codina 2067*227f609cSHerve Codina module_spi_driver(peb2466_spi_driver); 2068*227f609cSHerve Codina 2069*227f609cSHerve Codina MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>"); 2070*227f609cSHerve Codina MODULE_DESCRIPTION("PEB2466 ALSA SoC driver"); 2071*227f609cSHerve Codina MODULE_LICENSE("GPL"); 2072