1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * AK4104 ALSA SoC (ASoC) driver 4 * 5 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de> 6 */ 7 8 #include <linux/module.h> 9 #include <linux/slab.h> 10 #include <linux/spi/spi.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/regulator/consumer.h> 13 #include <sound/asoundef.h> 14 #include <sound/core.h> 15 #include <sound/soc.h> 16 #include <sound/initval.h> 17 18 /* AK4104 registers addresses */ 19 #define AK4104_REG_CONTROL1 0x00 20 #define AK4104_REG_RESERVED 0x01 21 #define AK4104_REG_CONTROL2 0x02 22 #define AK4104_REG_TX 0x03 23 #define AK4104_REG_CHN_STATUS(x) ((x) + 0x04) 24 #define AK4104_NUM_REGS 10 25 26 #define AK4104_REG_MASK 0x1f 27 #define AK4104_READ 0xc0 28 #define AK4104_WRITE 0xe0 29 #define AK4104_RESERVED_VAL 0x5b 30 31 /* Bit masks for AK4104 registers */ 32 #define AK4104_CONTROL1_RSTN (1 << 0) 33 #define AK4104_CONTROL1_PW (1 << 1) 34 #define AK4104_CONTROL1_DIF0 (1 << 2) 35 #define AK4104_CONTROL1_DIF1 (1 << 3) 36 37 #define AK4104_CONTROL2_SEL0 (1 << 0) 38 #define AK4104_CONTROL2_SEL1 (1 << 1) 39 #define AK4104_CONTROL2_MODE (1 << 2) 40 41 #define AK4104_TX_TXE (1 << 0) 42 #define AK4104_TX_V (1 << 1) 43 44 struct ak4104_private { 45 struct regmap *regmap; 46 struct regulator *regulator; 47 }; 48 49 static const struct snd_soc_dapm_widget ak4104_dapm_widgets[] = { 50 SND_SOC_DAPM_PGA("TXE", AK4104_REG_TX, AK4104_TX_TXE, 0, NULL, 0), 51 52 SND_SOC_DAPM_OUTPUT("TX"), 53 }; 54 55 static const struct snd_soc_dapm_route ak4104_dapm_routes[] = { 56 { "TXE", NULL, "Playback" }, 57 { "TX", NULL, "TXE" }, 58 }; 59 60 static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai, 61 unsigned int format) 62 { 63 struct snd_soc_component *component = codec_dai->component; 64 struct ak4104_private *ak4104 = snd_soc_component_get_drvdata(component); 65 int val = 0; 66 int ret; 67 68 /* set DAI format */ 69 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { 70 case SND_SOC_DAIFMT_RIGHT_J: 71 break; 72 case SND_SOC_DAIFMT_LEFT_J: 73 val |= AK4104_CONTROL1_DIF0; 74 break; 75 case SND_SOC_DAIFMT_I2S: 76 val |= AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1; 77 break; 78 default: 79 dev_err(component->dev, "invalid dai format\n"); 80 return -EINVAL; 81 } 82 83 /* This device can only be consumer */ 84 if ((format & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) != SND_SOC_DAIFMT_CBC_CFC) 85 return -EINVAL; 86 87 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1, 88 AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1, 89 val); 90 if (ret < 0) 91 return ret; 92 93 return 0; 94 } 95 96 static int ak4104_hw_params(struct snd_pcm_substream *substream, 97 struct snd_pcm_hw_params *params, 98 struct snd_soc_dai *dai) 99 { 100 struct snd_soc_component *component = dai->component; 101 struct ak4104_private *ak4104 = snd_soc_component_get_drvdata(component); 102 int ret, val = 0; 103 104 /* set the IEC958 bits: consumer mode, no copyright bit */ 105 val |= IEC958_AES0_CON_NOT_COPYRIGHT; 106 regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(0), val); 107 108 val = 0; 109 110 switch (params_rate(params)) { 111 case 22050: 112 val |= IEC958_AES3_CON_FS_22050; 113 break; 114 case 24000: 115 val |= IEC958_AES3_CON_FS_24000; 116 break; 117 case 32000: 118 val |= IEC958_AES3_CON_FS_32000; 119 break; 120 case 44100: 121 val |= IEC958_AES3_CON_FS_44100; 122 break; 123 case 48000: 124 val |= IEC958_AES3_CON_FS_48000; 125 break; 126 case 88200: 127 val |= IEC958_AES3_CON_FS_88200; 128 break; 129 case 96000: 130 val |= IEC958_AES3_CON_FS_96000; 131 break; 132 case 176400: 133 val |= IEC958_AES3_CON_FS_176400; 134 break; 135 case 192000: 136 val |= IEC958_AES3_CON_FS_192000; 137 break; 138 default: 139 dev_err(component->dev, "unsupported sampling rate\n"); 140 return -EINVAL; 141 } 142 143 ret = regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(3), val); 144 if (ret < 0) 145 return ret; 146 147 return 0; 148 } 149 150 static const struct snd_soc_dai_ops ak4101_dai_ops = { 151 .hw_params = ak4104_hw_params, 152 .set_fmt = ak4104_set_dai_fmt, 153 }; 154 155 static struct snd_soc_dai_driver ak4104_dai = { 156 .name = "ak4104-hifi", 157 .playback = { 158 .stream_name = "Playback", 159 .channels_min = 2, 160 .channels_max = 2, 161 .rates = SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | 162 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | 163 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | 164 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000, 165 .formats = SNDRV_PCM_FMTBIT_S16_LE | 166 SNDRV_PCM_FMTBIT_S24_3LE | 167 SNDRV_PCM_FMTBIT_S24_LE 168 }, 169 .ops = &ak4101_dai_ops, 170 }; 171 172 static int ak4104_probe(struct snd_soc_component *component) 173 { 174 struct ak4104_private *ak4104 = snd_soc_component_get_drvdata(component); 175 int ret; 176 177 ret = regulator_enable(ak4104->regulator); 178 if (ret < 0) { 179 dev_err(component->dev, "Unable to enable regulator: %d\n", ret); 180 return ret; 181 } 182 183 /* set power-up and non-reset bits */ 184 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1, 185 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 186 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN); 187 if (ret < 0) 188 goto exit_disable_regulator; 189 190 /* enable transmitter */ 191 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_TX, 192 AK4104_TX_TXE, AK4104_TX_TXE); 193 if (ret < 0) 194 goto exit_disable_regulator; 195 196 return 0; 197 198 exit_disable_regulator: 199 regulator_disable(ak4104->regulator); 200 return ret; 201 } 202 203 static void ak4104_remove(struct snd_soc_component *component) 204 { 205 struct ak4104_private *ak4104 = snd_soc_component_get_drvdata(component); 206 207 regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1, 208 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0); 209 regulator_disable(ak4104->regulator); 210 } 211 212 #ifdef CONFIG_PM 213 static int ak4104_soc_suspend(struct snd_soc_component *component) 214 { 215 struct ak4104_private *priv = snd_soc_component_get_drvdata(component); 216 217 regulator_disable(priv->regulator); 218 219 return 0; 220 } 221 222 static int ak4104_soc_resume(struct snd_soc_component *component) 223 { 224 struct ak4104_private *priv = snd_soc_component_get_drvdata(component); 225 int ret; 226 227 ret = regulator_enable(priv->regulator); 228 if (ret < 0) 229 return ret; 230 231 return 0; 232 } 233 #else 234 #define ak4104_soc_suspend NULL 235 #define ak4104_soc_resume NULL 236 #endif /* CONFIG_PM */ 237 238 static const struct snd_soc_component_driver soc_component_device_ak4104 = { 239 .probe = ak4104_probe, 240 .remove = ak4104_remove, 241 .suspend = ak4104_soc_suspend, 242 .resume = ak4104_soc_resume, 243 .dapm_widgets = ak4104_dapm_widgets, 244 .num_dapm_widgets = ARRAY_SIZE(ak4104_dapm_widgets), 245 .dapm_routes = ak4104_dapm_routes, 246 .num_dapm_routes = ARRAY_SIZE(ak4104_dapm_routes), 247 .idle_bias_on = 1, 248 .use_pmdown_time = 1, 249 .endianness = 1, 250 }; 251 252 static const struct regmap_config ak4104_regmap = { 253 .reg_bits = 8, 254 .val_bits = 8, 255 256 .max_register = AK4104_NUM_REGS - 1, 257 .read_flag_mask = AK4104_READ, 258 .write_flag_mask = AK4104_WRITE, 259 260 .cache_type = REGCACHE_RBTREE, 261 }; 262 263 static int ak4104_spi_probe(struct spi_device *spi) 264 { 265 struct ak4104_private *ak4104; 266 struct gpio_desc *reset_gpiod; 267 unsigned int val; 268 int ret; 269 270 spi->bits_per_word = 8; 271 spi->mode = SPI_MODE_0; 272 ret = spi_setup(spi); 273 if (ret < 0) 274 return ret; 275 276 ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private), 277 GFP_KERNEL); 278 if (ak4104 == NULL) 279 return -ENOMEM; 280 281 ak4104->regulator = devm_regulator_get(&spi->dev, "vdd"); 282 if (IS_ERR(ak4104->regulator)) { 283 ret = PTR_ERR(ak4104->regulator); 284 dev_err(&spi->dev, "Unable to get Vdd regulator: %d\n", ret); 285 return ret; 286 } 287 288 ak4104->regmap = devm_regmap_init_spi(spi, &ak4104_regmap); 289 if (IS_ERR(ak4104->regmap)) { 290 ret = PTR_ERR(ak4104->regmap); 291 return ret; 292 } 293 294 reset_gpiod = devm_gpiod_get_optional(&spi->dev, "reset", 295 GPIOD_OUT_HIGH); 296 if (PTR_ERR(reset_gpiod) == -EPROBE_DEFER) 297 return -EPROBE_DEFER; 298 299 /* read the 'reserved' register - according to the datasheet, it 300 * should contain 0x5b. Not a good way to verify the presence of 301 * the device, but there is no hardware ID register. */ 302 ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val); 303 if (ret != 0) 304 return ret; 305 if (val != AK4104_RESERVED_VAL) 306 return -ENODEV; 307 308 spi_set_drvdata(spi, ak4104); 309 310 ret = devm_snd_soc_register_component(&spi->dev, 311 &soc_component_device_ak4104, &ak4104_dai, 1); 312 return ret; 313 } 314 315 static const struct of_device_id ak4104_of_match[] = { 316 { .compatible = "asahi-kasei,ak4104", }, 317 { } 318 }; 319 MODULE_DEVICE_TABLE(of, ak4104_of_match); 320 321 static const struct spi_device_id ak4104_id_table[] = { 322 { "ak4104", 0 }, 323 { } 324 }; 325 MODULE_DEVICE_TABLE(spi, ak4104_id_table); 326 327 static struct spi_driver ak4104_spi_driver = { 328 .driver = { 329 .name = "ak4104", 330 .of_match_table = ak4104_of_match, 331 }, 332 .id_table = ak4104_id_table, 333 .probe = ak4104_spi_probe, 334 }; 335 336 module_spi_driver(ak4104_spi_driver); 337 338 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); 339 MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver"); 340 MODULE_LICENSE("GPL"); 341 342