0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/i2c.h>
0009 #include <linux/gpio/consumer.h>
0010 #include <linux/module.h>
0011 #include <linux/of_device.h>
0012 #include <linux/of_gpio.h>
0013 #include <linux/regmap.h>
0014 #include <linux/slab.h>
0015
0016 #include <sound/asoundef.h>
0017 #include <sound/core.h>
0018 #include <sound/initval.h>
0019 #include <sound/soc.h>
0020
0021 #define AK4118_REG_CLK_PWR_CTL 0x00
0022 #define AK4118_REG_FORMAT_CTL 0x01
0023 #define AK4118_REG_IO_CTL0 0x02
0024 #define AK4118_REG_IO_CTL1 0x03
0025 #define AK4118_REG_INT0_MASK 0x04
0026 #define AK4118_REG_INT1_MASK 0x05
0027 #define AK4118_REG_RCV_STATUS0 0x06
0028 #define AK4118_REG_RCV_STATUS1 0x07
0029 #define AK4118_REG_RXCHAN_STATUS0 0x08
0030 #define AK4118_REG_RXCHAN_STATUS1 0x09
0031 #define AK4118_REG_RXCHAN_STATUS2 0x0a
0032 #define AK4118_REG_RXCHAN_STATUS3 0x0b
0033 #define AK4118_REG_RXCHAN_STATUS4 0x0c
0034 #define AK4118_REG_TXCHAN_STATUS0 0x0d
0035 #define AK4118_REG_TXCHAN_STATUS1 0x0e
0036 #define AK4118_REG_TXCHAN_STATUS2 0x0f
0037 #define AK4118_REG_TXCHAN_STATUS3 0x10
0038 #define AK4118_REG_TXCHAN_STATUS4 0x11
0039 #define AK4118_REG_BURST_PREAMB_PC0 0x12
0040 #define AK4118_REG_BURST_PREAMB_PC1 0x13
0041 #define AK4118_REG_BURST_PREAMB_PD0 0x14
0042 #define AK4118_REG_BURST_PREAMB_PD1 0x15
0043 #define AK4118_REG_QSUB_CTL 0x16
0044 #define AK4118_REG_QSUB_TRACK 0x17
0045 #define AK4118_REG_QSUB_INDEX 0x18
0046 #define AK4118_REG_QSUB_MIN 0x19
0047 #define AK4118_REG_QSUB_SEC 0x1a
0048 #define AK4118_REG_QSUB_FRAME 0x1b
0049 #define AK4118_REG_QSUB_ZERO 0x1c
0050 #define AK4118_REG_QSUB_ABS_MIN 0x1d
0051 #define AK4118_REG_QSUB_ABS_SEC 0x1e
0052 #define AK4118_REG_QSUB_ABS_FRAME 0x1f
0053 #define AK4118_REG_GPE 0x20
0054 #define AK4118_REG_GPDR 0x21
0055 #define AK4118_REG_GPSCR 0x22
0056 #define AK4118_REG_GPLR 0x23
0057 #define AK4118_REG_DAT_MASK_DTS 0x24
0058 #define AK4118_REG_RX_DETECT 0x25
0059 #define AK4118_REG_STC_DAT_DETECT 0x26
0060 #define AK4118_REG_RXCHAN_STATUS5 0x27
0061 #define AK4118_REG_TXCHAN_STATUS5 0x28
0062 #define AK4118_REG_MAX 0x29
0063
0064 #define AK4118_REG_FORMAT_CTL_DIF0 (1 << 4)
0065 #define AK4118_REG_FORMAT_CTL_DIF1 (1 << 5)
0066 #define AK4118_REG_FORMAT_CTL_DIF2 (1 << 6)
0067
0068 struct ak4118_priv {
0069 struct regmap *regmap;
0070 struct gpio_desc *reset;
0071 struct gpio_desc *irq;
0072 struct snd_soc_component *component;
0073 };
0074
0075 static const struct reg_default ak4118_reg_defaults[] = {
0076 {AK4118_REG_CLK_PWR_CTL, 0x43},
0077 {AK4118_REG_FORMAT_CTL, 0x6a},
0078 {AK4118_REG_IO_CTL0, 0x88},
0079 {AK4118_REG_IO_CTL1, 0x48},
0080 {AK4118_REG_INT0_MASK, 0xee},
0081 {AK4118_REG_INT1_MASK, 0xb5},
0082 {AK4118_REG_RCV_STATUS0, 0x00},
0083 {AK4118_REG_RCV_STATUS1, 0x10},
0084 {AK4118_REG_TXCHAN_STATUS0, 0x00},
0085 {AK4118_REG_TXCHAN_STATUS1, 0x00},
0086 {AK4118_REG_TXCHAN_STATUS2, 0x00},
0087 {AK4118_REG_TXCHAN_STATUS3, 0x00},
0088 {AK4118_REG_TXCHAN_STATUS4, 0x00},
0089 {AK4118_REG_GPE, 0x77},
0090 {AK4118_REG_GPDR, 0x00},
0091 {AK4118_REG_GPSCR, 0x00},
0092 {AK4118_REG_GPLR, 0x00},
0093 {AK4118_REG_DAT_MASK_DTS, 0x3f},
0094 {AK4118_REG_RX_DETECT, 0x00},
0095 {AK4118_REG_STC_DAT_DETECT, 0x00},
0096 {AK4118_REG_TXCHAN_STATUS5, 0x00},
0097 };
0098
0099 static const char * const ak4118_input_select_txt[] = {
0100 "RX0", "RX1", "RX2", "RX3", "RX4", "RX5", "RX6", "RX7",
0101 };
0102 static SOC_ENUM_SINGLE_DECL(ak4118_insel_enum, AK4118_REG_IO_CTL1, 0x0,
0103 ak4118_input_select_txt);
0104
0105 static const struct snd_kcontrol_new ak4118_input_mux_controls =
0106 SOC_DAPM_ENUM("Input Select", ak4118_insel_enum);
0107
0108 static const char * const ak4118_iec958_fs_txt[] = {
0109 "44100", "48000", "32000", "22050", "11025", "24000", "16000", "88200",
0110 "8000", "96000", "64000", "176400", "192000",
0111 };
0112
0113 static const int ak4118_iec958_fs_val[] = {
0114 0x0, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xE,
0115 };
0116
0117 static SOC_VALUE_ENUM_SINGLE_DECL(ak4118_iec958_fs_enum, AK4118_REG_RCV_STATUS1,
0118 0x4, 0x4, ak4118_iec958_fs_txt,
0119 ak4118_iec958_fs_val);
0120
0121 static struct snd_kcontrol_new ak4118_iec958_controls[] = {
0122 SOC_SINGLE("IEC958 Parity Errors", AK4118_REG_RCV_STATUS0, 0, 1, 0),
0123 SOC_SINGLE("IEC958 No Audio", AK4118_REG_RCV_STATUS0, 1, 1, 0),
0124 SOC_SINGLE("IEC958 PLL Lock", AK4118_REG_RCV_STATUS0, 4, 1, 1),
0125 SOC_SINGLE("IEC958 Non PCM", AK4118_REG_RCV_STATUS0, 6, 1, 0),
0126 SOC_ENUM("IEC958 Sampling Freq", ak4118_iec958_fs_enum),
0127 };
0128
0129 static const struct snd_soc_dapm_widget ak4118_dapm_widgets[] = {
0130 SND_SOC_DAPM_INPUT("INRX0"),
0131 SND_SOC_DAPM_INPUT("INRX1"),
0132 SND_SOC_DAPM_INPUT("INRX2"),
0133 SND_SOC_DAPM_INPUT("INRX3"),
0134 SND_SOC_DAPM_INPUT("INRX4"),
0135 SND_SOC_DAPM_INPUT("INRX5"),
0136 SND_SOC_DAPM_INPUT("INRX6"),
0137 SND_SOC_DAPM_INPUT("INRX7"),
0138 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0,
0139 &ak4118_input_mux_controls),
0140 };
0141
0142 static const struct snd_soc_dapm_route ak4118_dapm_routes[] = {
0143 {"Input Mux", "RX0", "INRX0"},
0144 {"Input Mux", "RX1", "INRX1"},
0145 {"Input Mux", "RX2", "INRX2"},
0146 {"Input Mux", "RX3", "INRX3"},
0147 {"Input Mux", "RX4", "INRX4"},
0148 {"Input Mux", "RX5", "INRX5"},
0149 {"Input Mux", "RX6", "INRX6"},
0150 {"Input Mux", "RX7", "INRX7"},
0151 };
0152
0153
0154 static int ak4118_set_dai_fmt_provider(struct ak4118_priv *ak4118,
0155 unsigned int format)
0156 {
0157 int dif;
0158
0159 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
0160 case SND_SOC_DAIFMT_I2S:
0161 dif = AK4118_REG_FORMAT_CTL_DIF0 | AK4118_REG_FORMAT_CTL_DIF2;
0162 break;
0163 case SND_SOC_DAIFMT_RIGHT_J:
0164 dif = AK4118_REG_FORMAT_CTL_DIF0 | AK4118_REG_FORMAT_CTL_DIF1;
0165 break;
0166 case SND_SOC_DAIFMT_LEFT_J:
0167 dif = AK4118_REG_FORMAT_CTL_DIF2;
0168 break;
0169 default:
0170 return -ENOTSUPP;
0171 }
0172
0173 return dif;
0174 }
0175
0176 static int ak4118_set_dai_fmt_consumer(struct ak4118_priv *ak4118,
0177 unsigned int format)
0178 {
0179 int dif;
0180
0181 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
0182 case SND_SOC_DAIFMT_I2S:
0183 dif = AK4118_REG_FORMAT_CTL_DIF0 | AK4118_REG_FORMAT_CTL_DIF1 |
0184 AK4118_REG_FORMAT_CTL_DIF2;
0185 break;
0186 case SND_SOC_DAIFMT_LEFT_J:
0187 dif = AK4118_REG_FORMAT_CTL_DIF1 | AK4118_REG_FORMAT_CTL_DIF2;
0188 break;
0189 default:
0190 return -ENOTSUPP;
0191 }
0192
0193 return dif;
0194 }
0195
0196 static int ak4118_set_dai_fmt(struct snd_soc_dai *dai,
0197 unsigned int format)
0198 {
0199 struct snd_soc_component *component = dai->component;
0200 struct ak4118_priv *ak4118 = snd_soc_component_get_drvdata(component);
0201 int dif;
0202 int ret = 0;
0203
0204 switch (format & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
0205 case SND_SOC_DAIFMT_CBP_CFP:
0206 dif = ak4118_set_dai_fmt_provider(ak4118, format);
0207 break;
0208 case SND_SOC_DAIFMT_CBC_CFC:
0209 dif = ak4118_set_dai_fmt_consumer(ak4118, format);
0210 break;
0211 default:
0212 ret = -ENOTSUPP;
0213 goto exit;
0214 }
0215
0216
0217 if (dif < 0) {
0218 ret = dif;
0219 goto exit;
0220 }
0221
0222 ret = regmap_update_bits(ak4118->regmap, AK4118_REG_FORMAT_CTL,
0223 AK4118_REG_FORMAT_CTL_DIF0 |
0224 AK4118_REG_FORMAT_CTL_DIF1 |
0225 AK4118_REG_FORMAT_CTL_DIF2, dif);
0226 if (ret < 0)
0227 goto exit;
0228
0229 exit:
0230 return ret;
0231 }
0232
0233 static int ak4118_hw_params(struct snd_pcm_substream *substream,
0234 struct snd_pcm_hw_params *params,
0235 struct snd_soc_dai *dai)
0236 {
0237 return 0;
0238 }
0239
0240 static const struct snd_soc_dai_ops ak4118_dai_ops = {
0241 .hw_params = ak4118_hw_params,
0242 .set_fmt = ak4118_set_dai_fmt,
0243 };
0244
0245 static struct snd_soc_dai_driver ak4118_dai = {
0246 .name = "ak4118-hifi",
0247 .capture = {
0248 .stream_name = "Capture",
0249 .channels_min = 2,
0250 .channels_max = 2,
0251 .rates = SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
0252 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
0253 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
0254 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
0255 .formats = SNDRV_PCM_FMTBIT_S16_LE |
0256 SNDRV_PCM_FMTBIT_S24_3LE |
0257 SNDRV_PCM_FMTBIT_S24_LE
0258 },
0259 .ops = &ak4118_dai_ops,
0260 };
0261
0262 static irqreturn_t ak4118_irq_handler(int irq, void *data)
0263 {
0264 struct ak4118_priv *ak4118 = data;
0265 struct snd_soc_component *component = ak4118->component;
0266 struct snd_kcontrol_new *kctl_new;
0267 struct snd_kcontrol *kctl;
0268 struct snd_ctl_elem_id *id;
0269 unsigned int i;
0270
0271 if (!component)
0272 return IRQ_NONE;
0273
0274 for (i = 0; i < ARRAY_SIZE(ak4118_iec958_controls); i++) {
0275 kctl_new = &ak4118_iec958_controls[i];
0276 kctl = snd_soc_card_get_kcontrol(component->card,
0277 kctl_new->name);
0278 if (!kctl)
0279 continue;
0280 id = &kctl->id;
0281 snd_ctl_notify(component->card->snd_card,
0282 SNDRV_CTL_EVENT_MASK_VALUE, id);
0283 }
0284
0285 return IRQ_HANDLED;
0286 }
0287
0288 static int ak4118_probe(struct snd_soc_component *component)
0289 {
0290 struct ak4118_priv *ak4118 = snd_soc_component_get_drvdata(component);
0291 int ret = 0;
0292
0293 ak4118->component = component;
0294
0295
0296 gpiod_set_value(ak4118->reset, 0);
0297
0298
0299 ret = regmap_write(ak4118->regmap, AK4118_REG_INT1_MASK, 0x00);
0300 if (ret < 0) {
0301 dev_err(component->dev,
0302 "failed to write regmap 0x%x 0x%x: %d\n",
0303 AK4118_REG_INT1_MASK, 0x00, ret);
0304 return ret;
0305 }
0306
0307
0308 ret = regmap_write(ak4118->regmap, AK4118_REG_RX_DETECT, 0xff);
0309 if (ret < 0) {
0310 dev_err(component->dev,
0311 "failed to write regmap 0x%x 0x%x: %d\n",
0312 AK4118_REG_RX_DETECT, 0xff, ret);
0313 return ret;
0314 }
0315
0316 ret = snd_soc_add_component_controls(component, ak4118_iec958_controls,
0317 ARRAY_SIZE(ak4118_iec958_controls));
0318 if (ret) {
0319 dev_err(component->dev,
0320 "failed to add component kcontrols: %d\n", ret);
0321 return ret;
0322 }
0323
0324 return 0;
0325 }
0326
0327 static void ak4118_remove(struct snd_soc_component *component)
0328 {
0329 struct ak4118_priv *ak4118 = snd_soc_component_get_drvdata(component);
0330
0331
0332 gpiod_set_value(ak4118->reset, 1);
0333 }
0334
0335 static const struct snd_soc_component_driver soc_component_drv_ak4118 = {
0336 .probe = ak4118_probe,
0337 .remove = ak4118_remove,
0338 .dapm_widgets = ak4118_dapm_widgets,
0339 .num_dapm_widgets = ARRAY_SIZE(ak4118_dapm_widgets),
0340 .dapm_routes = ak4118_dapm_routes,
0341 .num_dapm_routes = ARRAY_SIZE(ak4118_dapm_routes),
0342 .idle_bias_on = 1,
0343 .use_pmdown_time = 1,
0344 .endianness = 1,
0345 };
0346
0347 static const struct regmap_config ak4118_regmap = {
0348 .reg_bits = 8,
0349 .val_bits = 8,
0350
0351 .reg_defaults = ak4118_reg_defaults,
0352 .num_reg_defaults = ARRAY_SIZE(ak4118_reg_defaults),
0353
0354 .cache_type = REGCACHE_NONE,
0355 .max_register = AK4118_REG_MAX - 1,
0356 };
0357
0358 static int ak4118_i2c_probe(struct i2c_client *i2c)
0359 {
0360 struct ak4118_priv *ak4118;
0361 int ret;
0362
0363 ak4118 = devm_kzalloc(&i2c->dev, sizeof(struct ak4118_priv),
0364 GFP_KERNEL);
0365 if (ak4118 == NULL)
0366 return -ENOMEM;
0367
0368 ak4118->regmap = devm_regmap_init_i2c(i2c, &ak4118_regmap);
0369 if (IS_ERR(ak4118->regmap))
0370 return PTR_ERR(ak4118->regmap);
0371
0372 i2c_set_clientdata(i2c, ak4118);
0373
0374 ak4118->reset = devm_gpiod_get(&i2c->dev, "reset", GPIOD_OUT_HIGH);
0375 if (IS_ERR(ak4118->reset))
0376 return dev_err_probe(&i2c->dev, PTR_ERR(ak4118->reset),
0377 "Failed to get reset\n");
0378
0379 ak4118->irq = devm_gpiod_get(&i2c->dev, "irq", GPIOD_IN);
0380 if (IS_ERR(ak4118->irq))
0381 return dev_err_probe(&i2c->dev, PTR_ERR(ak4118->irq),
0382 "Failed to get IRQ\n");
0383
0384 ret = devm_request_threaded_irq(&i2c->dev, gpiod_to_irq(ak4118->irq),
0385 NULL, ak4118_irq_handler,
0386 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
0387 "ak4118-irq", ak4118);
0388 if (ret < 0) {
0389 dev_err(&i2c->dev, "Fail to request_irq: %d\n", ret);
0390 return ret;
0391 }
0392
0393 return devm_snd_soc_register_component(&i2c->dev,
0394 &soc_component_drv_ak4118, &ak4118_dai, 1);
0395 }
0396
0397 #ifdef CONFIG_OF
0398 static const struct of_device_id ak4118_of_match[] = {
0399 { .compatible = "asahi-kasei,ak4118", },
0400 {}
0401 };
0402 MODULE_DEVICE_TABLE(of, ak4118_of_match);
0403 #endif
0404
0405 static const struct i2c_device_id ak4118_id_table[] = {
0406 { "ak4118", 0 },
0407 {}
0408 };
0409 MODULE_DEVICE_TABLE(i2c, ak4118_id_table);
0410
0411 static struct i2c_driver ak4118_i2c_driver = {
0412 .driver = {
0413 .name = "ak4118",
0414 .of_match_table = of_match_ptr(ak4118_of_match),
0415 },
0416 .id_table = ak4118_id_table,
0417 .probe_new = ak4118_i2c_probe,
0418 };
0419
0420 module_i2c_driver(ak4118_i2c_driver);
0421
0422 MODULE_DESCRIPTION("Asahi Kasei AK4118 ALSA SoC driver");
0423 MODULE_AUTHOR("Adrien Charruel <adrien.charruel@devialet.com>");
0424 MODULE_LICENSE("GPL");