Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 2 | /* |
| 3 | * CS4271 ASoC codec driver |
| 4 | * |
| 5 | * Copyright (c) 2010 Alexander Sverdlin <subaparts@yandex.ru> |
| 6 | * |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 7 | * This driver support CS4271 codec being master or slave, working |
| 8 | * in control port mode, connected either via SPI or I2C. |
| 9 | * The data format accepted is I2S or left-justified. |
| 10 | * DAPM support not implemented. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/delay.h> |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 16 | #include <linux/gpio.h> |
Sachin Kamat | a7ea1b7 | 2013-10-11 17:23:56 +0530 | [diff] [blame] | 17 | #include <linux/of.h> |
Daniel Mack | a31ebc3 | 2012-09-28 01:36:44 +0200 | [diff] [blame] | 18 | #include <linux/of_device.h> |
| 19 | #include <linux/of_gpio.h> |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 20 | #include <linux/regulator/consumer.h> |
Daniel Mack | a31ebc3 | 2012-09-28 01:36:44 +0200 | [diff] [blame] | 21 | #include <sound/pcm.h> |
| 22 | #include <sound/soc.h> |
| 23 | #include <sound/tlv.h> |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 24 | #include <sound/cs4271.h> |
Axel Lin | c973b8a | 2014-10-06 23:09:47 +0800 | [diff] [blame] | 25 | #include "cs4271.h" |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 26 | |
| 27 | #define CS4271_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \ |
| 28 | SNDRV_PCM_FMTBIT_S24_LE | \ |
| 29 | SNDRV_PCM_FMTBIT_S32_LE) |
Alexander Sverdlin | 383f846 | 2011-03-07 20:29:36 +0300 | [diff] [blame] | 30 | #define CS4271_PCM_RATES SNDRV_PCM_RATE_8000_192000 |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * CS4271 registers |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 34 | */ |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 35 | #define CS4271_MODE1 0x01 /* Mode Control 1 */ |
| 36 | #define CS4271_DACCTL 0x02 /* DAC Control */ |
| 37 | #define CS4271_DACVOL 0x03 /* DAC Volume & Mixing Control */ |
| 38 | #define CS4271_VOLA 0x04 /* DAC Channel A Volume Control */ |
| 39 | #define CS4271_VOLB 0x05 /* DAC Channel B Volume Control */ |
| 40 | #define CS4271_ADCCTL 0x06 /* ADC Control */ |
| 41 | #define CS4271_MODE2 0x07 /* Mode Control 2 */ |
| 42 | #define CS4271_CHIPID 0x08 /* Chip ID */ |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 43 | |
| 44 | #define CS4271_FIRSTREG CS4271_MODE1 |
| 45 | #define CS4271_LASTREG CS4271_MODE2 |
| 46 | #define CS4271_NR_REGS ((CS4271_LASTREG & 0xFF) + 1) |
| 47 | |
| 48 | /* Bit masks for the CS4271 registers */ |
| 49 | #define CS4271_MODE1_MODE_MASK 0xC0 |
| 50 | #define CS4271_MODE1_MODE_1X 0x00 |
| 51 | #define CS4271_MODE1_MODE_2X 0x80 |
| 52 | #define CS4271_MODE1_MODE_4X 0xC0 |
| 53 | |
| 54 | #define CS4271_MODE1_DIV_MASK 0x30 |
| 55 | #define CS4271_MODE1_DIV_1 0x00 |
| 56 | #define CS4271_MODE1_DIV_15 0x10 |
| 57 | #define CS4271_MODE1_DIV_2 0x20 |
| 58 | #define CS4271_MODE1_DIV_3 0x30 |
| 59 | |
| 60 | #define CS4271_MODE1_MASTER 0x08 |
| 61 | |
| 62 | #define CS4271_MODE1_DAC_DIF_MASK 0x07 |
| 63 | #define CS4271_MODE1_DAC_DIF_LJ 0x00 |
| 64 | #define CS4271_MODE1_DAC_DIF_I2S 0x01 |
| 65 | #define CS4271_MODE1_DAC_DIF_RJ16 0x02 |
| 66 | #define CS4271_MODE1_DAC_DIF_RJ24 0x03 |
| 67 | #define CS4271_MODE1_DAC_DIF_RJ20 0x04 |
| 68 | #define CS4271_MODE1_DAC_DIF_RJ18 0x05 |
| 69 | |
| 70 | #define CS4271_DACCTL_AMUTE 0x80 |
| 71 | #define CS4271_DACCTL_IF_SLOW 0x40 |
| 72 | |
| 73 | #define CS4271_DACCTL_DEM_MASK 0x30 |
| 74 | #define CS4271_DACCTL_DEM_DIS 0x00 |
| 75 | #define CS4271_DACCTL_DEM_441 0x10 |
| 76 | #define CS4271_DACCTL_DEM_48 0x20 |
| 77 | #define CS4271_DACCTL_DEM_32 0x30 |
| 78 | |
| 79 | #define CS4271_DACCTL_SVRU 0x08 |
| 80 | #define CS4271_DACCTL_SRD 0x04 |
| 81 | #define CS4271_DACCTL_INVA 0x02 |
| 82 | #define CS4271_DACCTL_INVB 0x01 |
| 83 | |
| 84 | #define CS4271_DACVOL_BEQUA 0x40 |
| 85 | #define CS4271_DACVOL_SOFT 0x20 |
| 86 | #define CS4271_DACVOL_ZEROC 0x10 |
| 87 | |
| 88 | #define CS4271_DACVOL_ATAPI_MASK 0x0F |
| 89 | #define CS4271_DACVOL_ATAPI_M_M 0x00 |
| 90 | #define CS4271_DACVOL_ATAPI_M_BR 0x01 |
| 91 | #define CS4271_DACVOL_ATAPI_M_BL 0x02 |
| 92 | #define CS4271_DACVOL_ATAPI_M_BLR2 0x03 |
| 93 | #define CS4271_DACVOL_ATAPI_AR_M 0x04 |
| 94 | #define CS4271_DACVOL_ATAPI_AR_BR 0x05 |
| 95 | #define CS4271_DACVOL_ATAPI_AR_BL 0x06 |
| 96 | #define CS4271_DACVOL_ATAPI_AR_BLR2 0x07 |
| 97 | #define CS4271_DACVOL_ATAPI_AL_M 0x08 |
| 98 | #define CS4271_DACVOL_ATAPI_AL_BR 0x09 |
| 99 | #define CS4271_DACVOL_ATAPI_AL_BL 0x0A |
| 100 | #define CS4271_DACVOL_ATAPI_AL_BLR2 0x0B |
| 101 | #define CS4271_DACVOL_ATAPI_ALR2_M 0x0C |
| 102 | #define CS4271_DACVOL_ATAPI_ALR2_BR 0x0D |
| 103 | #define CS4271_DACVOL_ATAPI_ALR2_BL 0x0E |
| 104 | #define CS4271_DACVOL_ATAPI_ALR2_BLR2 0x0F |
| 105 | |
| 106 | #define CS4271_VOLA_MUTE 0x80 |
| 107 | #define CS4271_VOLA_VOL_MASK 0x7F |
| 108 | #define CS4271_VOLB_MUTE 0x80 |
| 109 | #define CS4271_VOLB_VOL_MASK 0x7F |
| 110 | |
| 111 | #define CS4271_ADCCTL_DITHER16 0x20 |
| 112 | |
| 113 | #define CS4271_ADCCTL_ADC_DIF_MASK 0x10 |
| 114 | #define CS4271_ADCCTL_ADC_DIF_LJ 0x00 |
| 115 | #define CS4271_ADCCTL_ADC_DIF_I2S 0x10 |
| 116 | |
| 117 | #define CS4271_ADCCTL_MUTEA 0x08 |
| 118 | #define CS4271_ADCCTL_MUTEB 0x04 |
| 119 | #define CS4271_ADCCTL_HPFDA 0x02 |
| 120 | #define CS4271_ADCCTL_HPFDB 0x01 |
| 121 | |
| 122 | #define CS4271_MODE2_LOOP 0x10 |
| 123 | #define CS4271_MODE2_MUTECAEQUB 0x08 |
| 124 | #define CS4271_MODE2_FREEZE 0x04 |
| 125 | #define CS4271_MODE2_CPEN 0x02 |
| 126 | #define CS4271_MODE2_PDN 0x01 |
| 127 | |
| 128 | #define CS4271_CHIPID_PART_MASK 0xF0 |
| 129 | #define CS4271_CHIPID_REV_MASK 0x0F |
| 130 | |
| 131 | /* |
| 132 | * Default CS4271 power-up configuration |
| 133 | * Array contains non-existing in hw register at address 0 |
| 134 | * Array do not include Chip ID, as codec driver does not use |
| 135 | * registers read operations at all |
| 136 | */ |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 137 | static const struct reg_default cs4271_reg_defaults[] = { |
| 138 | { CS4271_MODE1, 0, }, |
| 139 | { CS4271_DACCTL, CS4271_DACCTL_AMUTE, }, |
| 140 | { CS4271_DACVOL, CS4271_DACVOL_SOFT | CS4271_DACVOL_ATAPI_AL_BR, }, |
| 141 | { CS4271_VOLA, 0, }, |
| 142 | { CS4271_VOLB, 0, }, |
| 143 | { CS4271_ADCCTL, 0, }, |
| 144 | { CS4271_MODE2, 0, }, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 145 | }; |
| 146 | |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 147 | static bool cs4271_volatile_reg(struct device *dev, unsigned int reg) |
| 148 | { |
| 149 | return reg == CS4271_CHIPID; |
| 150 | } |
| 151 | |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 152 | static const char * const supply_names[] = { |
| 153 | "vd", "vl", "va" |
| 154 | }; |
| 155 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 156 | struct cs4271_private { |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 157 | unsigned int mclk; |
| 158 | bool master; |
| 159 | bool deemph; |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 160 | struct regmap *regmap; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 161 | /* Current sample rate for de-emphasis control */ |
| 162 | int rate; |
| 163 | /* GPIO driving Reset pin, if any */ |
| 164 | int gpio_nreset; |
| 165 | /* GPIO that disable serial bus, if any */ |
| 166 | int gpio_disable; |
Daniel Mack | fd23fb9 | 2012-12-10 10:30:04 +0100 | [diff] [blame] | 167 | /* enable soft reset workaround */ |
| 168 | bool enable_soft_reset; |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 169 | struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)]; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 170 | }; |
| 171 | |
Mark Brown | 2e7fb94 | 2013-08-11 13:15:10 +0100 | [diff] [blame] | 172 | static const struct snd_soc_dapm_widget cs4271_dapm_widgets[] = { |
| 173 | SND_SOC_DAPM_INPUT("AINA"), |
| 174 | SND_SOC_DAPM_INPUT("AINB"), |
| 175 | |
| 176 | SND_SOC_DAPM_OUTPUT("AOUTA+"), |
| 177 | SND_SOC_DAPM_OUTPUT("AOUTA-"), |
| 178 | SND_SOC_DAPM_OUTPUT("AOUTB+"), |
| 179 | SND_SOC_DAPM_OUTPUT("AOUTB-"), |
| 180 | }; |
| 181 | |
| 182 | static const struct snd_soc_dapm_route cs4271_dapm_routes[] = { |
| 183 | { "Capture", NULL, "AINA" }, |
| 184 | { "Capture", NULL, "AINB" }, |
| 185 | |
| 186 | { "AOUTA+", NULL, "Playback" }, |
| 187 | { "AOUTA-", NULL, "Playback" }, |
| 188 | { "AOUTB+", NULL, "Playback" }, |
| 189 | { "AOUTB-", NULL, "Playback" }, |
| 190 | }; |
| 191 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 192 | /* |
| 193 | * @freq is the desired MCLK rate |
| 194 | * MCLK rate should (c) be the sample rate, multiplied by one of the |
| 195 | * ratios listed in cs4271_mclk_fs_ratios table |
| 196 | */ |
| 197 | static int cs4271_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 198 | int clk_id, unsigned int freq, int dir) |
| 199 | { |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 200 | struct snd_soc_component *component = codec_dai->component; |
| 201 | struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 202 | |
| 203 | cs4271->mclk = freq; |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int cs4271_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 208 | unsigned int format) |
| 209 | { |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 210 | struct snd_soc_component *component = codec_dai->component; |
| 211 | struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 212 | unsigned int val = 0; |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 213 | int ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 214 | |
| 215 | switch (format & SND_SOC_DAIFMT_MASTER_MASK) { |
| 216 | case SND_SOC_DAIFMT_CBS_CFS: |
Pierre-Louis Bossart | 3c17bcf | 2019-01-04 20:02:37 -0600 | [diff] [blame] | 217 | cs4271->master = false; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 218 | break; |
| 219 | case SND_SOC_DAIFMT_CBM_CFM: |
Pierre-Louis Bossart | 3c17bcf | 2019-01-04 20:02:37 -0600 | [diff] [blame] | 220 | cs4271->master = true; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 221 | val |= CS4271_MODE1_MASTER; |
| 222 | break; |
| 223 | default: |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 224 | dev_err(component->dev, "Invalid DAI format\n"); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 225 | return -EINVAL; |
| 226 | } |
| 227 | |
| 228 | switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 229 | case SND_SOC_DAIFMT_LEFT_J: |
| 230 | val |= CS4271_MODE1_DAC_DIF_LJ; |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 231 | ret = regmap_update_bits(cs4271->regmap, CS4271_ADCCTL, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 232 | CS4271_ADCCTL_ADC_DIF_MASK, CS4271_ADCCTL_ADC_DIF_LJ); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 233 | if (ret < 0) |
| 234 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 235 | break; |
| 236 | case SND_SOC_DAIFMT_I2S: |
| 237 | val |= CS4271_MODE1_DAC_DIF_I2S; |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 238 | ret = regmap_update_bits(cs4271->regmap, CS4271_ADCCTL, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 239 | CS4271_ADCCTL_ADC_DIF_MASK, CS4271_ADCCTL_ADC_DIF_I2S); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 240 | if (ret < 0) |
| 241 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 242 | break; |
| 243 | default: |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 244 | dev_err(component->dev, "Invalid DAI format\n"); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 245 | return -EINVAL; |
| 246 | } |
| 247 | |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 248 | ret = regmap_update_bits(cs4271->regmap, CS4271_MODE1, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 249 | CS4271_MODE1_DAC_DIF_MASK | CS4271_MODE1_MASTER, val); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 250 | if (ret < 0) |
| 251 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static int cs4271_deemph[] = {0, 44100, 48000, 32000}; |
| 256 | |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 257 | static int cs4271_set_deemph(struct snd_soc_component *component) |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 258 | { |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 259 | struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 260 | int i, ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 261 | int val = CS4271_DACCTL_DEM_DIS; |
| 262 | |
| 263 | if (cs4271->deemph) { |
| 264 | /* Find closest de-emphasis freq */ |
| 265 | val = 1; |
| 266 | for (i = 2; i < ARRAY_SIZE(cs4271_deemph); i++) |
| 267 | if (abs(cs4271_deemph[i] - cs4271->rate) < |
| 268 | abs(cs4271_deemph[val] - cs4271->rate)) |
| 269 | val = i; |
| 270 | val <<= 4; |
| 271 | } |
| 272 | |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 273 | ret = regmap_update_bits(cs4271->regmap, CS4271_DACCTL, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 274 | CS4271_DACCTL_DEM_MASK, val); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 275 | if (ret < 0) |
| 276 | return ret; |
| 277 | return 0; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static int cs4271_get_deemph(struct snd_kcontrol *kcontrol, |
| 281 | struct snd_ctl_elem_value *ucontrol) |
| 282 | { |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 283 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 284 | struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 285 | |
Takashi Iwai | e8371aa | 2015-03-10 12:39:05 +0100 | [diff] [blame] | 286 | ucontrol->value.integer.value[0] = cs4271->deemph; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static int cs4271_put_deemph(struct snd_kcontrol *kcontrol, |
| 291 | struct snd_ctl_elem_value *ucontrol) |
| 292 | { |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 293 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 294 | struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 295 | |
Takashi Iwai | e8371aa | 2015-03-10 12:39:05 +0100 | [diff] [blame] | 296 | cs4271->deemph = ucontrol->value.integer.value[0]; |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 297 | return cs4271_set_deemph(component); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 298 | } |
| 299 | |
Alexander Sverdlin | 5c3a12e | 2011-03-07 20:29:45 +0300 | [diff] [blame] | 300 | struct cs4271_clk_cfg { |
| 301 | bool master; /* codec mode */ |
| 302 | u8 speed_mode; /* codec speed mode: 1x, 2x, 4x */ |
| 303 | unsigned short ratio; /* MCLK / sample rate */ |
| 304 | u8 ratio_mask; /* ratio bit mask for Master mode */ |
| 305 | }; |
| 306 | |
| 307 | static struct cs4271_clk_cfg cs4271_clk_tab[] = { |
| 308 | {1, CS4271_MODE1_MODE_1X, 256, CS4271_MODE1_DIV_1}, |
| 309 | {1, CS4271_MODE1_MODE_1X, 384, CS4271_MODE1_DIV_15}, |
| 310 | {1, CS4271_MODE1_MODE_1X, 512, CS4271_MODE1_DIV_2}, |
| 311 | {1, CS4271_MODE1_MODE_1X, 768, CS4271_MODE1_DIV_3}, |
| 312 | {1, CS4271_MODE1_MODE_2X, 128, CS4271_MODE1_DIV_1}, |
| 313 | {1, CS4271_MODE1_MODE_2X, 192, CS4271_MODE1_DIV_15}, |
| 314 | {1, CS4271_MODE1_MODE_2X, 256, CS4271_MODE1_DIV_2}, |
| 315 | {1, CS4271_MODE1_MODE_2X, 384, CS4271_MODE1_DIV_3}, |
| 316 | {1, CS4271_MODE1_MODE_4X, 64, CS4271_MODE1_DIV_1}, |
| 317 | {1, CS4271_MODE1_MODE_4X, 96, CS4271_MODE1_DIV_15}, |
| 318 | {1, CS4271_MODE1_MODE_4X, 128, CS4271_MODE1_DIV_2}, |
| 319 | {1, CS4271_MODE1_MODE_4X, 192, CS4271_MODE1_DIV_3}, |
| 320 | {0, CS4271_MODE1_MODE_1X, 256, CS4271_MODE1_DIV_1}, |
| 321 | {0, CS4271_MODE1_MODE_1X, 384, CS4271_MODE1_DIV_1}, |
| 322 | {0, CS4271_MODE1_MODE_1X, 512, CS4271_MODE1_DIV_1}, |
| 323 | {0, CS4271_MODE1_MODE_1X, 768, CS4271_MODE1_DIV_2}, |
| 324 | {0, CS4271_MODE1_MODE_1X, 1024, CS4271_MODE1_DIV_2}, |
| 325 | {0, CS4271_MODE1_MODE_2X, 128, CS4271_MODE1_DIV_1}, |
| 326 | {0, CS4271_MODE1_MODE_2X, 192, CS4271_MODE1_DIV_1}, |
| 327 | {0, CS4271_MODE1_MODE_2X, 256, CS4271_MODE1_DIV_1}, |
| 328 | {0, CS4271_MODE1_MODE_2X, 384, CS4271_MODE1_DIV_2}, |
| 329 | {0, CS4271_MODE1_MODE_2X, 512, CS4271_MODE1_DIV_2}, |
| 330 | {0, CS4271_MODE1_MODE_4X, 64, CS4271_MODE1_DIV_1}, |
| 331 | {0, CS4271_MODE1_MODE_4X, 96, CS4271_MODE1_DIV_1}, |
| 332 | {0, CS4271_MODE1_MODE_4X, 128, CS4271_MODE1_DIV_1}, |
| 333 | {0, CS4271_MODE1_MODE_4X, 192, CS4271_MODE1_DIV_2}, |
| 334 | {0, CS4271_MODE1_MODE_4X, 256, CS4271_MODE1_DIV_2}, |
| 335 | }; |
| 336 | |
| 337 | #define CS4171_NR_RATIOS ARRAY_SIZE(cs4271_clk_tab) |
| 338 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 339 | static int cs4271_hw_params(struct snd_pcm_substream *substream, |
| 340 | struct snd_pcm_hw_params *params, |
| 341 | struct snd_soc_dai *dai) |
| 342 | { |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 343 | struct snd_soc_component *component = dai->component; |
| 344 | struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 345 | int i, ret; |
| 346 | unsigned int ratio, val; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 347 | |
Daniel Mack | fd23fb9 | 2012-12-10 10:30:04 +0100 | [diff] [blame] | 348 | if (cs4271->enable_soft_reset) { |
| 349 | /* |
| 350 | * Put the codec in soft reset and back again in case it's not |
| 351 | * currently streaming data. This way of bringing the codec in |
| 352 | * sync to the current clocks is not explicitly documented in |
| 353 | * the data sheet, but it seems to work fine, and in contrast |
| 354 | * to a read hardware reset, we don't have to sync back all |
| 355 | * registers every time. |
| 356 | */ |
| 357 | |
| 358 | if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 359 | !dai->capture_active) || |
| 360 | (substream->stream == SNDRV_PCM_STREAM_CAPTURE && |
| 361 | !dai->playback_active)) { |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 362 | ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2, |
| 363 | CS4271_MODE2_PDN, |
| 364 | CS4271_MODE2_PDN); |
Daniel Mack | fd23fb9 | 2012-12-10 10:30:04 +0100 | [diff] [blame] | 365 | if (ret < 0) |
| 366 | return ret; |
| 367 | |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 368 | ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2, |
| 369 | CS4271_MODE2_PDN, 0); |
Daniel Mack | fd23fb9 | 2012-12-10 10:30:04 +0100 | [diff] [blame] | 370 | if (ret < 0) |
| 371 | return ret; |
| 372 | } |
| 373 | } |
| 374 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 375 | cs4271->rate = params_rate(params); |
Alexander Sverdlin | 5c3a12e | 2011-03-07 20:29:45 +0300 | [diff] [blame] | 376 | |
| 377 | /* Configure DAC */ |
| 378 | if (cs4271->rate < 50000) |
| 379 | val = CS4271_MODE1_MODE_1X; |
| 380 | else if (cs4271->rate < 100000) |
| 381 | val = CS4271_MODE1_MODE_2X; |
| 382 | else |
| 383 | val = CS4271_MODE1_MODE_4X; |
| 384 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 385 | ratio = cs4271->mclk / cs4271->rate; |
| 386 | for (i = 0; i < CS4171_NR_RATIOS; i++) |
Alexander Sverdlin | 5c3a12e | 2011-03-07 20:29:45 +0300 | [diff] [blame] | 387 | if ((cs4271_clk_tab[i].master == cs4271->master) && |
| 388 | (cs4271_clk_tab[i].speed_mode == val) && |
| 389 | (cs4271_clk_tab[i].ratio == ratio)) |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 390 | break; |
| 391 | |
Alexander Sverdlin | 5c3a12e | 2011-03-07 20:29:45 +0300 | [diff] [blame] | 392 | if (i == CS4171_NR_RATIOS) { |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 393 | dev_err(component->dev, "Invalid sample rate\n"); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 394 | return -EINVAL; |
| 395 | } |
| 396 | |
Alexander Sverdlin | 5c3a12e | 2011-03-07 20:29:45 +0300 | [diff] [blame] | 397 | val |= cs4271_clk_tab[i].ratio_mask; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 398 | |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 399 | ret = regmap_update_bits(cs4271->regmap, CS4271_MODE1, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 400 | CS4271_MODE1_MODE_MASK | CS4271_MODE1_DIV_MASK, val); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 401 | if (ret < 0) |
| 402 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 403 | |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 404 | return cs4271_set_deemph(component); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 405 | } |
| 406 | |
Daniel Mack | c24a34d | 2013-03-21 20:43:54 +0100 | [diff] [blame] | 407 | static int cs4271_mute_stream(struct snd_soc_dai *dai, int mute, int stream) |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 408 | { |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 409 | struct snd_soc_component *component = dai->component; |
| 410 | struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 411 | int ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 412 | int val_a = 0; |
| 413 | int val_b = 0; |
| 414 | |
Daniel Mack | c24a34d | 2013-03-21 20:43:54 +0100 | [diff] [blame] | 415 | if (stream != SNDRV_PCM_STREAM_PLAYBACK) |
| 416 | return 0; |
| 417 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 418 | if (mute) { |
| 419 | val_a = CS4271_VOLA_MUTE; |
| 420 | val_b = CS4271_VOLB_MUTE; |
| 421 | } |
| 422 | |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 423 | ret = regmap_update_bits(cs4271->regmap, CS4271_VOLA, |
| 424 | CS4271_VOLA_MUTE, val_a); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 425 | if (ret < 0) |
| 426 | return ret; |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 427 | |
| 428 | ret = regmap_update_bits(cs4271->regmap, CS4271_VOLB, |
| 429 | CS4271_VOLB_MUTE, val_b); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 430 | if (ret < 0) |
| 431 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /* CS4271 controls */ |
| 437 | static DECLARE_TLV_DB_SCALE(cs4271_dac_tlv, -12700, 100, 0); |
| 438 | |
| 439 | static const struct snd_kcontrol_new cs4271_snd_controls[] = { |
| 440 | SOC_DOUBLE_R_TLV("Master Playback Volume", CS4271_VOLA, CS4271_VOLB, |
| 441 | 0, 0x7F, 1, cs4271_dac_tlv), |
| 442 | SOC_SINGLE("Digital Loopback Switch", CS4271_MODE2, 4, 1, 0), |
| 443 | SOC_SINGLE("Soft Ramp Switch", CS4271_DACVOL, 5, 1, 0), |
| 444 | SOC_SINGLE("Zero Cross Switch", CS4271_DACVOL, 4, 1, 0), |
| 445 | SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0, |
| 446 | cs4271_get_deemph, cs4271_put_deemph), |
| 447 | SOC_SINGLE("Auto-Mute Switch", CS4271_DACCTL, 7, 1, 0), |
| 448 | SOC_SINGLE("Slow Roll Off Filter Switch", CS4271_DACCTL, 6, 1, 0), |
| 449 | SOC_SINGLE("Soft Volume Ramp-Up Switch", CS4271_DACCTL, 3, 1, 0), |
| 450 | SOC_SINGLE("Soft Ramp-Down Switch", CS4271_DACCTL, 2, 1, 0), |
| 451 | SOC_SINGLE("Left Channel Inversion Switch", CS4271_DACCTL, 1, 1, 0), |
| 452 | SOC_SINGLE("Right Channel Inversion Switch", CS4271_DACCTL, 0, 1, 0), |
| 453 | SOC_DOUBLE("Master Capture Switch", CS4271_ADCCTL, 3, 2, 1, 1), |
| 454 | SOC_SINGLE("Dither 16-Bit Data Switch", CS4271_ADCCTL, 5, 1, 0), |
| 455 | SOC_DOUBLE("High Pass Filter Switch", CS4271_ADCCTL, 1, 0, 1, 1), |
| 456 | SOC_DOUBLE_R("Master Playback Switch", CS4271_VOLA, CS4271_VOLB, |
| 457 | 7, 1, 1), |
| 458 | }; |
| 459 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 460 | static const struct snd_soc_dai_ops cs4271_dai_ops = { |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 461 | .hw_params = cs4271_hw_params, |
| 462 | .set_sysclk = cs4271_set_dai_sysclk, |
| 463 | .set_fmt = cs4271_set_dai_fmt, |
Daniel Mack | c24a34d | 2013-03-21 20:43:54 +0100 | [diff] [blame] | 464 | .mute_stream = cs4271_mute_stream, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 465 | }; |
| 466 | |
Mark Brown | 16af7d6 | 2011-01-26 11:35:28 +0000 | [diff] [blame] | 467 | static struct snd_soc_dai_driver cs4271_dai = { |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 468 | .name = "cs4271-hifi", |
| 469 | .playback = { |
| 470 | .stream_name = "Playback", |
| 471 | .channels_min = 2, |
| 472 | .channels_max = 2, |
Alexander Sverdlin | 383f846 | 2011-03-07 20:29:36 +0300 | [diff] [blame] | 473 | .rates = CS4271_PCM_RATES, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 474 | .formats = CS4271_PCM_FORMATS, |
| 475 | }, |
| 476 | .capture = { |
| 477 | .stream_name = "Capture", |
| 478 | .channels_min = 2, |
| 479 | .channels_max = 2, |
Alexander Sverdlin | 383f846 | 2011-03-07 20:29:36 +0300 | [diff] [blame] | 480 | .rates = CS4271_PCM_RATES, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 481 | .formats = CS4271_PCM_FORMATS, |
| 482 | }, |
| 483 | .ops = &cs4271_dai_ops, |
| 484 | .symmetric_rates = 1, |
| 485 | }; |
| 486 | |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 487 | static int cs4271_reset(struct snd_soc_component *component) |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 488 | { |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 489 | struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 490 | |
| 491 | if (gpio_is_valid(cs4271->gpio_nreset)) { |
Alexander Sverdlin | 49b2e27 | 2017-04-29 12:19:33 +0200 | [diff] [blame] | 492 | gpio_direction_output(cs4271->gpio_nreset, 0); |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 493 | mdelay(1); |
| 494 | gpio_set_value(cs4271->gpio_nreset, 1); |
| 495 | mdelay(1); |
| 496 | } |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 501 | #ifdef CONFIG_PM |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 502 | static int cs4271_soc_suspend(struct snd_soc_component *component) |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 503 | { |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 504 | int ret; |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 505 | struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 506 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 507 | /* Set power-down bit */ |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 508 | ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2, |
| 509 | CS4271_MODE2_PDN, CS4271_MODE2_PDN); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 510 | if (ret < 0) |
| 511 | return ret; |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 512 | |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 513 | regcache_mark_dirty(cs4271->regmap); |
| 514 | regulator_bulk_disable(ARRAY_SIZE(cs4271->supplies), cs4271->supplies); |
| 515 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 516 | return 0; |
| 517 | } |
| 518 | |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 519 | static int cs4271_soc_resume(struct snd_soc_component *component) |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 520 | { |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 521 | int ret; |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 522 | struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 523 | |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 524 | ret = regulator_bulk_enable(ARRAY_SIZE(cs4271->supplies), |
| 525 | cs4271->supplies); |
| 526 | if (ret < 0) { |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 527 | dev_err(component->dev, "Failed to enable regulators: %d\n", ret); |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | /* Do a proper reset after power up */ |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 532 | cs4271_reset(component); |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 533 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 534 | /* Restore codec state */ |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 535 | ret = regcache_sync(cs4271->regmap); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 536 | if (ret < 0) |
| 537 | return ret; |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 538 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 539 | /* then disable the power-down bit */ |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 540 | ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2, |
| 541 | CS4271_MODE2_PDN, 0); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 542 | if (ret < 0) |
| 543 | return ret; |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 544 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 545 | return 0; |
| 546 | } |
| 547 | #else |
| 548 | #define cs4271_soc_suspend NULL |
| 549 | #define cs4271_soc_resume NULL |
| 550 | #endif /* CONFIG_PM */ |
| 551 | |
Daniel Mack | a31ebc3 | 2012-09-28 01:36:44 +0200 | [diff] [blame] | 552 | #ifdef CONFIG_OF |
Axel Lin | c973b8a | 2014-10-06 23:09:47 +0800 | [diff] [blame] | 553 | const struct of_device_id cs4271_dt_ids[] = { |
Daniel Mack | a31ebc3 | 2012-09-28 01:36:44 +0200 | [diff] [blame] | 554 | { .compatible = "cirrus,cs4271", }, |
| 555 | { } |
| 556 | }; |
| 557 | MODULE_DEVICE_TABLE(of, cs4271_dt_ids); |
Axel Lin | c973b8a | 2014-10-06 23:09:47 +0800 | [diff] [blame] | 558 | EXPORT_SYMBOL_GPL(cs4271_dt_ids); |
Daniel Mack | a31ebc3 | 2012-09-28 01:36:44 +0200 | [diff] [blame] | 559 | #endif |
| 560 | |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 561 | static int cs4271_component_probe(struct snd_soc_component *component) |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 562 | { |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 563 | struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); |
| 564 | struct cs4271_platform_data *cs4271plat = component->dev->platform_data; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 565 | int ret; |
Daniel Mack | 26047e2 | 2012-11-30 11:28:55 +0100 | [diff] [blame] | 566 | bool amutec_eq_bmutec = false; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 567 | |
Daniel Mack | a31ebc3 | 2012-09-28 01:36:44 +0200 | [diff] [blame] | 568 | #ifdef CONFIG_OF |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 569 | if (of_match_device(cs4271_dt_ids, component->dev)) { |
| 570 | if (of_get_property(component->dev->of_node, |
Daniel Mack | 293750f | 2012-10-04 14:03:23 +0200 | [diff] [blame] | 571 | "cirrus,amutec-eq-bmutec", NULL)) |
Daniel Mack | 26047e2 | 2012-11-30 11:28:55 +0100 | [diff] [blame] | 572 | amutec_eq_bmutec = true; |
Daniel Mack | fd23fb9 | 2012-12-10 10:30:04 +0100 | [diff] [blame] | 573 | |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 574 | if (of_get_property(component->dev->of_node, |
Daniel Mack | fd23fb9 | 2012-12-10 10:30:04 +0100 | [diff] [blame] | 575 | "cirrus,enable-soft-reset", NULL)) |
| 576 | cs4271->enable_soft_reset = true; |
Daniel Mack | 293750f | 2012-10-04 14:03:23 +0200 | [diff] [blame] | 577 | } |
Daniel Mack | a31ebc3 | 2012-09-28 01:36:44 +0200 | [diff] [blame] | 578 | #endif |
| 579 | |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 580 | ret = regulator_bulk_enable(ARRAY_SIZE(cs4271->supplies), |
| 581 | cs4271->supplies); |
| 582 | if (ret < 0) { |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 583 | dev_err(component->dev, "Failed to enable regulators: %d\n", ret); |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 584 | return ret; |
| 585 | } |
| 586 | |
Daniel Mack | 293750f | 2012-10-04 14:03:23 +0200 | [diff] [blame] | 587 | if (cs4271plat) { |
Daniel Mack | 293750f | 2012-10-04 14:03:23 +0200 | [diff] [blame] | 588 | amutec_eq_bmutec = cs4271plat->amutec_eq_bmutec; |
Daniel Mack | fd23fb9 | 2012-12-10 10:30:04 +0100 | [diff] [blame] | 589 | cs4271->enable_soft_reset = cs4271plat->enable_soft_reset; |
Daniel Mack | 293750f | 2012-10-04 14:03:23 +0200 | [diff] [blame] | 590 | } |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 591 | |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 592 | /* Reset codec */ |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 593 | cs4271_reset(component); |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 594 | |
| 595 | ret = regcache_sync(cs4271->regmap); |
| 596 | if (ret < 0) |
| 597 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 598 | |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 599 | ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2, |
| 600 | CS4271_MODE2_PDN | CS4271_MODE2_CPEN, |
| 601 | CS4271_MODE2_PDN | CS4271_MODE2_CPEN); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 602 | if (ret < 0) |
| 603 | return ret; |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 604 | ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2, |
| 605 | CS4271_MODE2_PDN, 0); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 606 | if (ret < 0) |
| 607 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 608 | /* Power-up sequence requires 85 uS */ |
| 609 | udelay(85); |
| 610 | |
Daniel Mack | 293750f | 2012-10-04 14:03:23 +0200 | [diff] [blame] | 611 | if (amutec_eq_bmutec) |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 612 | regmap_update_bits(cs4271->regmap, CS4271_MODE2, |
| 613 | CS4271_MODE2_MUTECAEQUB, |
| 614 | CS4271_MODE2_MUTECAEQUB); |
Daniel Mack | 293750f | 2012-10-04 14:03:23 +0200 | [diff] [blame] | 615 | |
Mark Brown | bad268f | 2013-08-11 13:12:13 +0100 | [diff] [blame] | 616 | return 0; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 617 | } |
| 618 | |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 619 | static void cs4271_component_remove(struct snd_soc_component *component) |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 620 | { |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 621 | struct cs4271_private *cs4271 = snd_soc_component_get_drvdata(component); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 622 | |
Daniel Mack | 5574f77 | 2012-11-10 19:52:50 +0100 | [diff] [blame] | 623 | if (gpio_is_valid(cs4271->gpio_nreset)) |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 624 | /* Set codec to the reset state */ |
Daniel Mack | 5574f77 | 2012-11-10 19:52:50 +0100 | [diff] [blame] | 625 | gpio_set_value(cs4271->gpio_nreset, 0); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 626 | |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 627 | regcache_mark_dirty(cs4271->regmap); |
| 628 | regulator_bulk_disable(ARRAY_SIZE(cs4271->supplies), cs4271->supplies); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 629 | }; |
| 630 | |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 631 | static const struct snd_soc_component_driver soc_component_dev_cs4271 = { |
| 632 | .probe = cs4271_component_probe, |
| 633 | .remove = cs4271_component_remove, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 634 | .suspend = cs4271_soc_suspend, |
| 635 | .resume = cs4271_soc_resume, |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 636 | .controls = cs4271_snd_controls, |
| 637 | .num_controls = ARRAY_SIZE(cs4271_snd_controls), |
| 638 | .dapm_widgets = cs4271_dapm_widgets, |
| 639 | .num_dapm_widgets = ARRAY_SIZE(cs4271_dapm_widgets), |
| 640 | .dapm_routes = cs4271_dapm_routes, |
| 641 | .num_dapm_routes = ARRAY_SIZE(cs4271_dapm_routes), |
| 642 | .idle_bias_on = 1, |
| 643 | .use_pmdown_time = 1, |
| 644 | .endianness = 1, |
| 645 | .non_legacy_dai_naming = 1, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 646 | }; |
| 647 | |
Daniel Mack | d6cf89e | 2014-02-19 14:05:54 +0100 | [diff] [blame] | 648 | static int cs4271_common_probe(struct device *dev, |
| 649 | struct cs4271_private **c) |
| 650 | { |
| 651 | struct cs4271_platform_data *cs4271plat = dev->platform_data; |
| 652 | struct cs4271_private *cs4271; |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 653 | int i, ret; |
Daniel Mack | d6cf89e | 2014-02-19 14:05:54 +0100 | [diff] [blame] | 654 | |
| 655 | cs4271 = devm_kzalloc(dev, sizeof(*cs4271), GFP_KERNEL); |
| 656 | if (!cs4271) |
| 657 | return -ENOMEM; |
| 658 | |
| 659 | if (of_match_device(cs4271_dt_ids, dev)) |
| 660 | cs4271->gpio_nreset = |
| 661 | of_get_named_gpio(dev->of_node, "reset-gpio", 0); |
| 662 | |
| 663 | if (cs4271plat) |
| 664 | cs4271->gpio_nreset = cs4271plat->gpio_nreset; |
| 665 | |
| 666 | if (gpio_is_valid(cs4271->gpio_nreset)) { |
Daniel Mack | d6cf89e | 2014-02-19 14:05:54 +0100 | [diff] [blame] | 667 | ret = devm_gpio_request(dev, cs4271->gpio_nreset, |
| 668 | "CS4271 Reset"); |
| 669 | if (ret < 0) |
| 670 | return ret; |
| 671 | } |
| 672 | |
Pascal Huerst | 9a397f4 | 2016-02-16 16:19:06 +0100 | [diff] [blame] | 673 | for (i = 0; i < ARRAY_SIZE(supply_names); i++) |
| 674 | cs4271->supplies[i].supply = supply_names[i]; |
| 675 | |
| 676 | ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(cs4271->supplies), |
| 677 | cs4271->supplies); |
| 678 | |
| 679 | if (ret < 0) { |
| 680 | dev_err(dev, "Failed to get regulators: %d\n", ret); |
| 681 | return ret; |
| 682 | } |
| 683 | |
Daniel Mack | d6cf89e | 2014-02-19 14:05:54 +0100 | [diff] [blame] | 684 | *c = cs4271; |
| 685 | return 0; |
| 686 | } |
| 687 | |
Axel Lin | c973b8a | 2014-10-06 23:09:47 +0800 | [diff] [blame] | 688 | const struct regmap_config cs4271_regmap_config = { |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 689 | .max_register = CS4271_LASTREG, |
| 690 | |
| 691 | .reg_defaults = cs4271_reg_defaults, |
| 692 | .num_reg_defaults = ARRAY_SIZE(cs4271_reg_defaults), |
| 693 | .cache_type = REGCACHE_RBTREE, |
| 694 | |
| 695 | .volatile_reg = cs4271_volatile_reg, |
| 696 | }; |
Axel Lin | c973b8a | 2014-10-06 23:09:47 +0800 | [diff] [blame] | 697 | EXPORT_SYMBOL_GPL(cs4271_regmap_config); |
Daniel Mack | 1b1861e | 2013-03-07 23:53:12 +0100 | [diff] [blame] | 698 | |
Axel Lin | c973b8a | 2014-10-06 23:09:47 +0800 | [diff] [blame] | 699 | int cs4271_probe(struct device *dev, struct regmap *regmap) |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 700 | { |
| 701 | struct cs4271_private *cs4271; |
Daniel Mack | d6cf89e | 2014-02-19 14:05:54 +0100 | [diff] [blame] | 702 | int ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 703 | |
Axel Lin | c973b8a | 2014-10-06 23:09:47 +0800 | [diff] [blame] | 704 | if (IS_ERR(regmap)) |
| 705 | return PTR_ERR(regmap); |
| 706 | |
| 707 | ret = cs4271_common_probe(dev, &cs4271); |
Daniel Mack | d6cf89e | 2014-02-19 14:05:54 +0100 | [diff] [blame] | 708 | if (ret < 0) |
| 709 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 710 | |
Axel Lin | c973b8a | 2014-10-06 23:09:47 +0800 | [diff] [blame] | 711 | dev_set_drvdata(dev, cs4271); |
| 712 | cs4271->regmap = regmap; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 713 | |
Kuninori Morimoto | cac308f | 2018-01-29 03:50:28 +0000 | [diff] [blame] | 714 | return devm_snd_soc_register_component(dev, &soc_component_dev_cs4271, |
| 715 | &cs4271_dai, 1); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 716 | } |
Axel Lin | c973b8a | 2014-10-06 23:09:47 +0800 | [diff] [blame] | 717 | EXPORT_SYMBOL_GPL(cs4271_probe); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 718 | |
| 719 | MODULE_AUTHOR("Alexander Sverdlin <subaparts@yandex.ru>"); |
| 720 | MODULE_DESCRIPTION("Cirrus Logic CS4271 ALSA SoC Codec Driver"); |
| 721 | MODULE_LICENSE("GPL"); |