1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * ak4118.c -- Asahi Kasei ALSA Soc Audio driver 4 * 5 * Copyright 2018 DEVIALET 6 */ 7 8 #include <linux/i2c.h> 9 #include <linux/module.h> 10 #include <linux/of_device.h> 11 #include <linux/of_gpio.h> 12 #include <linux/regmap.h> 13 #include <linux/slab.h> 14 15 #include <sound/asoundef.h> 16 #include <sound/core.h> 17 #include <sound/initval.h> 18 #include <sound/soc.h> 19 20 #define AK4118_REG_CLK_PWR_CTL 0x00 21 #define AK4118_REG_FORMAT_CTL 0x01 22 #define AK4118_REG_IO_CTL0 0x02 23 #define AK4118_REG_IO_CTL1 0x03 24 #define AK4118_REG_INT0_MASK 0x04 25 #define AK4118_REG_INT1_MASK 0x05 26 #define AK4118_REG_RCV_STATUS0 0x06 27 #define AK4118_REG_RCV_STATUS1 0x07 28 #define AK4118_REG_RXCHAN_STATUS0 0x08 29 #define AK4118_REG_RXCHAN_STATUS1 0x09 30 #define AK4118_REG_RXCHAN_STATUS2 0x0a 31 #define AK4118_REG_RXCHAN_STATUS3 0x0b 32 #define AK4118_REG_RXCHAN_STATUS4 0x0c 33 #define AK4118_REG_TXCHAN_STATUS0 0x0d 34 #define AK4118_REG_TXCHAN_STATUS1 0x0e 35 #define AK4118_REG_TXCHAN_STATUS2 0x0f 36 #define AK4118_REG_TXCHAN_STATUS3 0x10 37 #define AK4118_REG_TXCHAN_STATUS4 0x11 38 #define AK4118_REG_BURST_PREAMB_PC0 0x12 39 #define AK4118_REG_BURST_PREAMB_PC1 0x13 40 #define AK4118_REG_BURST_PREAMB_PD0 0x14 41 #define AK4118_REG_BURST_PREAMB_PD1 0x15 42 #define AK4118_REG_QSUB_CTL 0x16 43 #define AK4118_REG_QSUB_TRACK 0x17 44 #define AK4118_REG_QSUB_INDEX 0x18 45 #define AK4118_REG_QSUB_MIN 0x19 46 #define AK4118_REG_QSUB_SEC 0x1a 47 #define AK4118_REG_QSUB_FRAME 0x1b 48 #define AK4118_REG_QSUB_ZERO 0x1c 49 #define AK4118_REG_QSUB_ABS_MIN 0x1d 50 #define AK4118_REG_QSUB_ABS_SEC 0x1e 51 #define AK4118_REG_QSUB_ABS_FRAME 0x1f 52 #define AK4118_REG_GPE 0x20 53 #define AK4118_REG_GPDR 0x21 54 #define AK4118_REG_GPSCR 0x22 55 #define AK4118_REG_GPLR 0x23 56 #define AK4118_REG_DAT_MASK_DTS 0x24 57 #define AK4118_REG_RX_DETECT 0x25 58 #define AK4118_REG_STC_DAT_DETECT 0x26 59 #define AK4118_REG_RXCHAN_STATUS5 0x27 60 #define AK4118_REG_TXCHAN_STATUS5 0x28 61 #define AK4118_REG_MAX 0x29 62 63 #define AK4118_REG_FORMAT_CTL_DIF0 (1 << 4) 64 #define AK4118_REG_FORMAT_CTL_DIF1 (1 << 5) 65 #define AK4118_REG_FORMAT_CTL_DIF2 (1 << 6) 66 67 struct ak4118_priv { 68 struct regmap *regmap; 69 struct gpio_desc *reset; 70 struct gpio_desc *irq; 71 struct snd_soc_component *component; 72 }; 73 74 static const struct reg_default ak4118_reg_defaults[] = { 75 {AK4118_REG_CLK_PWR_CTL, 0x43}, 76 {AK4118_REG_FORMAT_CTL, 0x6a}, 77 {AK4118_REG_IO_CTL0, 0x88}, 78 {AK4118_REG_IO_CTL1, 0x48}, 79 {AK4118_REG_INT0_MASK, 0xee}, 80 {AK4118_REG_INT1_MASK, 0xb5}, 81 {AK4118_REG_RCV_STATUS0, 0x00}, 82 {AK4118_REG_RCV_STATUS1, 0x10}, 83 {AK4118_REG_TXCHAN_STATUS0, 0x00}, 84 {AK4118_REG_TXCHAN_STATUS1, 0x00}, 85 {AK4118_REG_TXCHAN_STATUS2, 0x00}, 86 {AK4118_REG_TXCHAN_STATUS3, 0x00}, 87 {AK4118_REG_TXCHAN_STATUS4, 0x00}, 88 {AK4118_REG_GPE, 0x77}, 89 {AK4118_REG_GPDR, 0x00}, 90 {AK4118_REG_GPSCR, 0x00}, 91 {AK4118_REG_GPLR, 0x00}, 92 {AK4118_REG_DAT_MASK_DTS, 0x3f}, 93 {AK4118_REG_RX_DETECT, 0x00}, 94 {AK4118_REG_STC_DAT_DETECT, 0x00}, 95 {AK4118_REG_TXCHAN_STATUS5, 0x00}, 96 }; 97 98 static const char * const ak4118_input_select_txt[] = { 99 "RX0", "RX1", "RX2", "RX3", "RX4", "RX5", "RX6", "RX7", 100 }; 101 static SOC_ENUM_SINGLE_DECL(ak4118_insel_enum, AK4118_REG_IO_CTL1, 0x0, 102 ak4118_input_select_txt); 103 104 static const struct snd_kcontrol_new ak4118_input_mux_controls = 105 SOC_DAPM_ENUM("Input Select", ak4118_insel_enum); 106 107 static const char * const ak4118_iec958_fs_txt[] = { 108 "44100", "48000", "32000", "22050", "11025", "24000", "16000", "88200", 109 "8000", "96000", "64000", "176400", "192000", 110 }; 111 112 static const int ak4118_iec958_fs_val[] = { 113 0x0, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xE, 114 }; 115 116 static SOC_VALUE_ENUM_SINGLE_DECL(ak4118_iec958_fs_enum, AK4118_REG_RCV_STATUS1, 117 0x4, 0x4, ak4118_iec958_fs_txt, 118 ak4118_iec958_fs_val); 119 120 static struct snd_kcontrol_new ak4118_iec958_controls[] = { 121 SOC_SINGLE("IEC958 Parity Errors", AK4118_REG_RCV_STATUS0, 0, 1, 0), 122 SOC_SINGLE("IEC958 No Audio", AK4118_REG_RCV_STATUS0, 1, 1, 0), 123 SOC_SINGLE("IEC958 PLL Lock", AK4118_REG_RCV_STATUS0, 4, 1, 1), 124 SOC_SINGLE("IEC958 Non PCM", AK4118_REG_RCV_STATUS0, 6, 1, 0), 125 SOC_ENUM("IEC958 Sampling Freq", ak4118_iec958_fs_enum), 126 }; 127 128 static const struct snd_soc_dapm_widget ak4118_dapm_widgets[] = { 129 SND_SOC_DAPM_INPUT("INRX0"), 130 SND_SOC_DAPM_INPUT("INRX1"), 131 SND_SOC_DAPM_INPUT("INRX2"), 132 SND_SOC_DAPM_INPUT("INRX3"), 133 SND_SOC_DAPM_INPUT("INRX4"), 134 SND_SOC_DAPM_INPUT("INRX5"), 135 SND_SOC_DAPM_INPUT("INRX6"), 136 SND_SOC_DAPM_INPUT("INRX7"), 137 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, 138 &ak4118_input_mux_controls), 139 }; 140 141 static const struct snd_soc_dapm_route ak4118_dapm_routes[] = { 142 {"Input Mux", "RX0", "INRX0"}, 143 {"Input Mux", "RX1", "INRX1"}, 144 {"Input Mux", "RX2", "INRX2"}, 145 {"Input Mux", "RX3", "INRX3"}, 146 {"Input Mux", "RX4", "INRX4"}, 147 {"Input Mux", "RX5", "INRX5"}, 148 {"Input Mux", "RX6", "INRX6"}, 149 {"Input Mux", "RX7", "INRX7"}, 150 }; 151 152 153 static int ak4118_set_dai_fmt_master(struct ak4118_priv *ak4118, 154 unsigned int format) 155 { 156 int dif; 157 158 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { 159 case SND_SOC_DAIFMT_I2S: 160 dif = AK4118_REG_FORMAT_CTL_DIF0 | AK4118_REG_FORMAT_CTL_DIF2; 161 break; 162 case SND_SOC_DAIFMT_RIGHT_J: 163 dif = AK4118_REG_FORMAT_CTL_DIF0 | AK4118_REG_FORMAT_CTL_DIF1; 164 break; 165 case SND_SOC_DAIFMT_LEFT_J: 166 dif = AK4118_REG_FORMAT_CTL_DIF2; 167 break; 168 default: 169 return -ENOTSUPP; 170 } 171 172 return dif; 173 } 174 175 static int ak4118_set_dai_fmt_slave(struct ak4118_priv *ak4118, 176 unsigned int format) 177 { 178 int dif; 179 180 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { 181 case SND_SOC_DAIFMT_I2S: 182 dif = AK4118_REG_FORMAT_CTL_DIF0 | AK4118_REG_FORMAT_CTL_DIF1 | 183 AK4118_REG_FORMAT_CTL_DIF2; 184 break; 185 case SND_SOC_DAIFMT_LEFT_J: 186 dif = AK4118_REG_FORMAT_CTL_DIF1 | AK4118_REG_FORMAT_CTL_DIF2; 187 break; 188 default: 189 return -ENOTSUPP; 190 } 191 192 return dif; 193 } 194 195 static int ak4118_set_dai_fmt(struct snd_soc_dai *dai, 196 unsigned int format) 197 { 198 struct snd_soc_component *component = dai->component; 199 struct ak4118_priv *ak4118 = snd_soc_component_get_drvdata(component); 200 int dif; 201 int ret = 0; 202 203 switch (format & SND_SOC_DAIFMT_MASTER_MASK) { 204 case SND_SOC_DAIFMT_CBM_CFM: 205 /* component is master */ 206 dif = ak4118_set_dai_fmt_master(ak4118, format); 207 break; 208 case SND_SOC_DAIFMT_CBS_CFS: 209 /*component is slave */ 210 dif = ak4118_set_dai_fmt_slave(ak4118, format); 211 break; 212 default: 213 ret = -ENOTSUPP; 214 goto exit; 215 } 216 217 /* format not supported */ 218 if (dif < 0) { 219 ret = dif; 220 goto exit; 221 } 222 223 ret = regmap_update_bits(ak4118->regmap, AK4118_REG_FORMAT_CTL, 224 AK4118_REG_FORMAT_CTL_DIF0 | 225 AK4118_REG_FORMAT_CTL_DIF1 | 226 AK4118_REG_FORMAT_CTL_DIF2, dif); 227 if (ret < 0) 228 goto exit; 229 230 exit: 231 return ret; 232 } 233 234 static int ak4118_hw_params(struct snd_pcm_substream *substream, 235 struct snd_pcm_hw_params *params, 236 struct snd_soc_dai *dai) 237 { 238 return 0; 239 } 240 241 static const struct snd_soc_dai_ops ak4118_dai_ops = { 242 .hw_params = ak4118_hw_params, 243 .set_fmt = ak4118_set_dai_fmt, 244 }; 245 246 static struct snd_soc_dai_driver ak4118_dai = { 247 .name = "ak4118-hifi", 248 .capture = { 249 .stream_name = "Capture", 250 .channels_min = 2, 251 .channels_max = 2, 252 .rates = SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | 253 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | 254 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | 255 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000, 256 .formats = SNDRV_PCM_FMTBIT_S16_LE | 257 SNDRV_PCM_FMTBIT_S24_3LE | 258 SNDRV_PCM_FMTBIT_S24_LE 259 }, 260 .ops = &ak4118_dai_ops, 261 }; 262 263 static irqreturn_t ak4118_irq_handler(int irq, void *data) 264 { 265 struct ak4118_priv *ak4118 = data; 266 struct snd_soc_component *component = ak4118->component; 267 struct snd_kcontrol_new *kctl_new; 268 struct snd_kcontrol *kctl; 269 struct snd_ctl_elem_id *id; 270 unsigned int i; 271 272 if (!component) 273 return IRQ_NONE; 274 275 for (i = 0; i < ARRAY_SIZE(ak4118_iec958_controls); i++) { 276 kctl_new = &ak4118_iec958_controls[i]; 277 kctl = snd_soc_card_get_kcontrol(component->card, 278 kctl_new->name); 279 if (!kctl) 280 continue; 281 id = &kctl->id; 282 snd_ctl_notify(component->card->snd_card, 283 SNDRV_CTL_EVENT_MASK_VALUE, id); 284 } 285 286 return IRQ_HANDLED; 287 } 288 289 static int ak4118_probe(struct snd_soc_component *component) 290 { 291 struct ak4118_priv *ak4118 = snd_soc_component_get_drvdata(component); 292 int ret = 0; 293 294 ak4118->component = component; 295 296 /* release reset */ 297 gpiod_set_value(ak4118->reset, 0); 298 299 /* unmask all int1 sources */ 300 ret = regmap_write(ak4118->regmap, AK4118_REG_INT1_MASK, 0x00); 301 if (ret < 0) { 302 dev_err(component->dev, 303 "failed to write regmap 0x%x 0x%x: %d\n", 304 AK4118_REG_INT1_MASK, 0x00, ret); 305 return ret; 306 } 307 308 /* rx detect enable on all channels */ 309 ret = regmap_write(ak4118->regmap, AK4118_REG_RX_DETECT, 0xff); 310 if (ret < 0) { 311 dev_err(component->dev, 312 "failed to write regmap 0x%x 0x%x: %d\n", 313 AK4118_REG_RX_DETECT, 0xff, ret); 314 return ret; 315 } 316 317 ret = snd_soc_add_component_controls(component, ak4118_iec958_controls, 318 ARRAY_SIZE(ak4118_iec958_controls)); 319 if (ret) { 320 dev_err(component->dev, 321 "failed to add component kcontrols: %d\n", ret); 322 return ret; 323 } 324 325 return 0; 326 } 327 328 static void ak4118_remove(struct snd_soc_component *component) 329 { 330 struct ak4118_priv *ak4118 = snd_soc_component_get_drvdata(component); 331 332 /* hold reset */ 333 gpiod_set_value(ak4118->reset, 1); 334 } 335 336 static const struct snd_soc_component_driver soc_component_drv_ak4118 = { 337 .probe = ak4118_probe, 338 .remove = ak4118_remove, 339 .dapm_widgets = ak4118_dapm_widgets, 340 .num_dapm_widgets = ARRAY_SIZE(ak4118_dapm_widgets), 341 .dapm_routes = ak4118_dapm_routes, 342 .num_dapm_routes = ARRAY_SIZE(ak4118_dapm_routes), 343 .idle_bias_on = 1, 344 .use_pmdown_time = 1, 345 .endianness = 1, 346 .non_legacy_dai_naming = 1, 347 }; 348 349 static const struct regmap_config ak4118_regmap = { 350 .reg_bits = 8, 351 .val_bits = 8, 352 353 .reg_defaults = ak4118_reg_defaults, 354 .num_reg_defaults = ARRAY_SIZE(ak4118_reg_defaults), 355 356 .cache_type = REGCACHE_NONE, 357 .max_register = AK4118_REG_MAX - 1, 358 }; 359 360 static int ak4118_i2c_probe(struct i2c_client *i2c, 361 const struct i2c_device_id *id) 362 { 363 struct ak4118_priv *ak4118; 364 int ret; 365 366 ak4118 = devm_kzalloc(&i2c->dev, sizeof(struct ak4118_priv), 367 GFP_KERNEL); 368 if (ak4118 == NULL) 369 return -ENOMEM; 370 371 ak4118->regmap = devm_regmap_init_i2c(i2c, &ak4118_regmap); 372 if (IS_ERR(ak4118->regmap)) 373 return PTR_ERR(ak4118->regmap); 374 375 i2c_set_clientdata(i2c, ak4118); 376 377 ak4118->reset = devm_gpiod_get(&i2c->dev, "reset", GPIOD_OUT_HIGH); 378 if (IS_ERR(ak4118->reset)) { 379 ret = PTR_ERR(ak4118->reset); 380 if (ret != -EPROBE_DEFER) 381 dev_err(&i2c->dev, "Failed to get reset: %d\n", ret); 382 return ret; 383 } 384 385 ak4118->irq = devm_gpiod_get(&i2c->dev, "irq", GPIOD_IN); 386 if (IS_ERR(ak4118->irq)) { 387 ret = PTR_ERR(ak4118->irq); 388 if (ret != -EPROBE_DEFER) 389 dev_err(&i2c->dev, "Failed to get IRQ: %d\n", ret); 390 return ret; 391 } 392 393 ret = devm_request_threaded_irq(&i2c->dev, gpiod_to_irq(ak4118->irq), 394 NULL, ak4118_irq_handler, 395 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 396 "ak4118-irq", ak4118); 397 if (ret < 0) { 398 dev_err(&i2c->dev, "Fail to request_irq: %d\n", ret); 399 return ret; 400 } 401 402 return snd_soc_register_component(&i2c->dev, &soc_component_drv_ak4118, 403 &ak4118_dai, 1); 404 } 405 406 static int ak4118_i2c_remove(struct i2c_client *i2c) 407 { 408 snd_soc_unregister_component(&i2c->dev); 409 return 0; 410 } 411 412 static const struct of_device_id ak4118_of_match[] = { 413 { .compatible = "asahi-kasei,ak4118", }, 414 {} 415 }; 416 MODULE_DEVICE_TABLE(of, ak4118_of_match); 417 418 static const struct i2c_device_id ak4118_id_table[] = { 419 { "ak4118", 0 }, 420 {} 421 }; 422 MODULE_DEVICE_TABLE(i2c, ak4118_id_table); 423 424 static struct i2c_driver ak4118_i2c_driver = { 425 .driver = { 426 .name = "ak4118", 427 .of_match_table = of_match_ptr(ak4118_of_match), 428 }, 429 .id_table = ak4118_id_table, 430 .probe = ak4118_i2c_probe, 431 .remove = ak4118_i2c_remove, 432 }; 433 434 module_i2c_driver(ak4118_i2c_driver); 435 436 MODULE_DESCRIPTION("Asahi Kasei AK4118 ALSA SoC driver"); 437 MODULE_AUTHOR("Adrien Charruel <adrien.charruel@devialet.com>"); 438 MODULE_LICENSE("GPL"); 439