Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * AK4104 ALSA SoC (ASoC) driver |
| 3 | * |
| 4 | * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2 of the License, or (at your |
| 9 | * option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 14 | #include <sound/core.h> |
| 15 | #include <sound/soc.h> |
| 16 | #include <sound/initval.h> |
| 17 | #include <linux/spi/spi.h> |
Daniel Mack | 385a4c2 | 2012-11-14 18:28:39 +0800 | [diff] [blame] | 18 | #include <linux/of_device.h> |
| 19 | #include <linux/of_gpio.h> |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 20 | #include <sound/asoundef.h> |
| 21 | |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 22 | /* AK4104 registers addresses */ |
| 23 | #define AK4104_REG_CONTROL1 0x00 |
| 24 | #define AK4104_REG_RESERVED 0x01 |
| 25 | #define AK4104_REG_CONTROL2 0x02 |
| 26 | #define AK4104_REG_TX 0x03 |
| 27 | #define AK4104_REG_CHN_STATUS(x) ((x) + 0x04) |
| 28 | #define AK4104_NUM_REGS 10 |
| 29 | |
| 30 | #define AK4104_REG_MASK 0x1f |
| 31 | #define AK4104_READ 0xc0 |
| 32 | #define AK4104_WRITE 0xe0 |
| 33 | #define AK4104_RESERVED_VAL 0x5b |
| 34 | |
| 35 | /* Bit masks for AK4104 registers */ |
| 36 | #define AK4104_CONTROL1_RSTN (1 << 0) |
| 37 | #define AK4104_CONTROL1_PW (1 << 1) |
| 38 | #define AK4104_CONTROL1_DIF0 (1 << 2) |
| 39 | #define AK4104_CONTROL1_DIF1 (1 << 3) |
| 40 | |
| 41 | #define AK4104_CONTROL2_SEL0 (1 << 0) |
| 42 | #define AK4104_CONTROL2_SEL1 (1 << 1) |
| 43 | #define AK4104_CONTROL2_MODE (1 << 2) |
| 44 | |
| 45 | #define AK4104_TX_TXE (1 << 0) |
| 46 | #define AK4104_TX_V (1 << 1) |
| 47 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 48 | #define DRV_NAME "ak4104-codec" |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 49 | |
| 50 | struct ak4104_private { |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 51 | struct regmap *regmap; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
Mark Brown | 2e61926 | 2013-08-07 19:01:40 +0100 | [diff] [blame] | 54 | static const struct snd_soc_dapm_widget ak4104_dapm_widgets[] = { |
Mark Brown | a5db4d5 | 2013-08-07 19:05:47 +0100 | [diff] [blame] | 55 | SND_SOC_DAPM_PGA("TXE", AK4104_REG_TX, AK4104_TX_TXE, 0, NULL, 0), |
| 56 | |
Mark Brown | 2e61926 | 2013-08-07 19:01:40 +0100 | [diff] [blame] | 57 | SND_SOC_DAPM_OUTPUT("TX"), |
| 58 | }; |
| 59 | |
| 60 | static const struct snd_soc_dapm_route ak4104_dapm_routes[] = { |
Mark Brown | a5db4d5 | 2013-08-07 19:05:47 +0100 | [diff] [blame] | 61 | { "TXE", NULL, "Playback" }, |
| 62 | { "TX", NULL, "TXE" }, |
Mark Brown | 2e61926 | 2013-08-07 19:01:40 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 65 | static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 66 | unsigned int format) |
| 67 | { |
| 68 | struct snd_soc_codec *codec = codec_dai->codec; |
Daniel Mack | b0ec761 | 2013-03-06 22:22:15 +0100 | [diff] [blame] | 69 | struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec); |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 70 | int val = 0; |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 71 | int ret; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 72 | |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 73 | /* set DAI format */ |
| 74 | switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 75 | case SND_SOC_DAIFMT_RIGHT_J: |
| 76 | break; |
| 77 | case SND_SOC_DAIFMT_LEFT_J: |
| 78 | val |= AK4104_CONTROL1_DIF0; |
| 79 | break; |
| 80 | case SND_SOC_DAIFMT_I2S: |
| 81 | val |= AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1; |
| 82 | break; |
| 83 | default: |
| 84 | dev_err(codec->dev, "invalid dai format\n"); |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | |
| 88 | /* This device can only be slave */ |
| 89 | if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) |
| 90 | return -EINVAL; |
| 91 | |
Daniel Mack | b0ec761 | 2013-03-06 22:22:15 +0100 | [diff] [blame] | 92 | ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1, |
| 93 | AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1, |
| 94 | val); |
Mark Brown | afad95f | 2012-02-17 12:04:41 -0800 | [diff] [blame] | 95 | if (ret < 0) |
| 96 | return ret; |
| 97 | |
| 98 | return 0; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static int ak4104_hw_params(struct snd_pcm_substream *substream, |
| 102 | struct snd_pcm_hw_params *params, |
| 103 | struct snd_soc_dai *dai) |
| 104 | { |
Mark Brown | e6968a1 | 2012-04-04 15:58:16 +0100 | [diff] [blame] | 105 | struct snd_soc_codec *codec = dai->codec; |
Daniel Mack | b0ec761 | 2013-03-06 22:22:15 +0100 | [diff] [blame] | 106 | struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec); |
Daniel Mack | b692a43 | 2013-03-06 22:22:16 +0100 | [diff] [blame] | 107 | int ret, val = 0; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 108 | |
| 109 | /* set the IEC958 bits: consumer mode, no copyright bit */ |
| 110 | val |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Daniel Mack | b0ec761 | 2013-03-06 22:22:15 +0100 | [diff] [blame] | 111 | regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(0), val); |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 112 | |
| 113 | val = 0; |
| 114 | |
| 115 | switch (params_rate(params)) { |
Daniel Mack | 08201de | 2012-10-07 17:51:23 +0200 | [diff] [blame] | 116 | case 22050: |
| 117 | val |= IEC958_AES3_CON_FS_22050; |
| 118 | break; |
| 119 | case 24000: |
| 120 | val |= IEC958_AES3_CON_FS_24000; |
| 121 | break; |
| 122 | case 32000: |
| 123 | val |= IEC958_AES3_CON_FS_32000; |
| 124 | break; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 125 | case 44100: |
| 126 | val |= IEC958_AES3_CON_FS_44100; |
| 127 | break; |
| 128 | case 48000: |
| 129 | val |= IEC958_AES3_CON_FS_48000; |
| 130 | break; |
Daniel Mack | 08201de | 2012-10-07 17:51:23 +0200 | [diff] [blame] | 131 | case 88200: |
| 132 | val |= IEC958_AES3_CON_FS_88200; |
| 133 | break; |
| 134 | case 96000: |
| 135 | val |= IEC958_AES3_CON_FS_96000; |
| 136 | break; |
| 137 | case 176400: |
| 138 | val |= IEC958_AES3_CON_FS_176400; |
| 139 | break; |
| 140 | case 192000: |
| 141 | val |= IEC958_AES3_CON_FS_192000; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 142 | break; |
| 143 | default: |
| 144 | dev_err(codec->dev, "unsupported sampling rate\n"); |
| 145 | return -EINVAL; |
| 146 | } |
| 147 | |
Daniel Mack | b692a43 | 2013-03-06 22:22:16 +0100 | [diff] [blame] | 148 | ret = regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(3), val); |
| 149 | if (ret < 0) |
| 150 | return ret; |
| 151 | |
Daniel Mack | b692a43 | 2013-03-06 22:22:16 +0100 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 155 | static const struct snd_soc_dai_ops ak4101_dai_ops = { |
Mark Brown | 65ec1cd | 2009-03-11 16:51:31 +0000 | [diff] [blame] | 156 | .hw_params = ak4104_hw_params, |
| 157 | .set_fmt = ak4104_set_dai_fmt, |
| 158 | }; |
| 159 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 160 | static struct snd_soc_dai_driver ak4104_dai = { |
| 161 | .name = "ak4104-hifi", |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 162 | .playback = { |
| 163 | .stream_name = "Playback", |
| 164 | .channels_min = 2, |
| 165 | .channels_max = 2, |
Daniel Mack | 617b14c | 2010-01-13 11:25:05 +0100 | [diff] [blame] | 166 | .rates = SNDRV_PCM_RATE_8000_192000, |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 167 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
| 168 | SNDRV_PCM_FMTBIT_S24_3LE | |
| 169 | SNDRV_PCM_FMTBIT_S24_LE |
| 170 | }, |
Mark Brown | 65ec1cd | 2009-03-11 16:51:31 +0000 | [diff] [blame] | 171 | .ops = &ak4101_dai_ops, |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 172 | }; |
| 173 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 174 | static int ak4104_probe(struct snd_soc_codec *codec) |
| 175 | { |
| 176 | struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec); |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 177 | int ret; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 178 | |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 179 | codec->control_data = ak4104->regmap; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 180 | |
| 181 | /* set power-up and non-reset bits */ |
Daniel Mack | b0ec761 | 2013-03-06 22:22:15 +0100 | [diff] [blame] | 182 | ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1, |
| 183 | AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, |
| 184 | AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 185 | if (ret < 0) |
| 186 | return ret; |
| 187 | |
| 188 | /* enable transmitter */ |
Daniel Mack | b0ec761 | 2013-03-06 22:22:15 +0100 | [diff] [blame] | 189 | ret = regmap_update_bits(ak4104->regmap, AK4104_REG_TX, |
| 190 | AK4104_TX_TXE, AK4104_TX_TXE); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 191 | if (ret < 0) |
| 192 | return ret; |
| 193 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int ak4104_remove(struct snd_soc_codec *codec) |
| 198 | { |
Daniel Mack | b0ec761 | 2013-03-06 22:22:15 +0100 | [diff] [blame] | 199 | struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec); |
| 200 | |
| 201 | regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1, |
| 202 | AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 203 | |
Mark Brown | afad95f | 2012-02-17 12:04:41 -0800 | [diff] [blame] | 204 | return 0; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static struct snd_soc_codec_driver soc_codec_device_ak4104 = { |
| 208 | .probe = ak4104_probe, |
| 209 | .remove = ak4104_remove, |
Mark Brown | 2e61926 | 2013-08-07 19:01:40 +0100 | [diff] [blame] | 210 | |
| 211 | .dapm_widgets = ak4104_dapm_widgets, |
| 212 | .num_dapm_widgets = ARRAY_SIZE(ak4104_dapm_widgets), |
| 213 | .dapm_routes = ak4104_dapm_routes, |
| 214 | .num_dapm_routes = ARRAY_SIZE(ak4104_dapm_routes), |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | static const struct regmap_config ak4104_regmap = { |
| 218 | .reg_bits = 8, |
| 219 | .val_bits = 8, |
| 220 | |
| 221 | .max_register = AK4104_NUM_REGS - 1, |
| 222 | .read_flag_mask = AK4104_READ, |
| 223 | .write_flag_mask = AK4104_WRITE, |
| 224 | |
| 225 | .cache_type = REGCACHE_RBTREE, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 226 | }; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 227 | |
| 228 | static int ak4104_spi_probe(struct spi_device *spi) |
| 229 | { |
Daniel Mack | 385a4c2 | 2012-11-14 18:28:39 +0800 | [diff] [blame] | 230 | struct device_node *np = spi->dev.of_node; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 231 | struct ak4104_private *ak4104; |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 232 | unsigned int val; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 233 | int ret; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 234 | |
| 235 | spi->bits_per_word = 8; |
| 236 | spi->mode = SPI_MODE_0; |
| 237 | ret = spi_setup(spi); |
| 238 | if (ret < 0) |
| 239 | return ret; |
| 240 | |
Axel Lin | 3922d51 | 2011-12-20 14:37:12 +0800 | [diff] [blame] | 241 | ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private), |
| 242 | GFP_KERNEL); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 243 | if (ak4104 == NULL) |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 244 | return -ENOMEM; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 245 | |
Tushar Behera | a273cd1 | 2012-11-22 09:38:36 +0530 | [diff] [blame] | 246 | ak4104->regmap = devm_regmap_init_spi(spi, &ak4104_regmap); |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 247 | if (IS_ERR(ak4104->regmap)) { |
| 248 | ret = PTR_ERR(ak4104->regmap); |
| 249 | return ret; |
| 250 | } |
| 251 | |
Daniel Mack | 385a4c2 | 2012-11-14 18:28:39 +0800 | [diff] [blame] | 252 | if (np) { |
| 253 | enum of_gpio_flags flags; |
| 254 | int gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &flags); |
| 255 | |
| 256 | if (gpio_is_valid(gpio)) { |
| 257 | ret = devm_gpio_request_one(&spi->dev, gpio, |
| 258 | flags & OF_GPIO_ACTIVE_LOW ? |
| 259 | GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH, |
| 260 | "ak4104 reset"); |
| 261 | if (ret < 0) |
| 262 | return ret; |
| 263 | } |
| 264 | } |
| 265 | |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 266 | /* read the 'reserved' register - according to the datasheet, it |
| 267 | * should contain 0x5b. Not a good way to verify the presence of |
| 268 | * the device, but there is no hardware ID register. */ |
| 269 | ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val); |
| 270 | if (ret != 0) |
Tushar Behera | a273cd1 | 2012-11-22 09:38:36 +0530 | [diff] [blame] | 271 | return ret; |
| 272 | if (val != AK4104_RESERVED_VAL) |
| 273 | return -ENODEV; |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 274 | |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 275 | spi_set_drvdata(spi, ak4104); |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 276 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 277 | ret = snd_soc_register_codec(&spi->dev, |
| 278 | &soc_codec_device_ak4104, &ak4104_dai, 1); |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 279 | return ret; |
| 280 | } |
| 281 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 282 | static int ak4104_spi_remove(struct spi_device *spi) |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 283 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 284 | snd_soc_unregister_codec(&spi->dev); |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
Daniel Mack | ac5dbea | 2012-10-07 17:51:24 +0200 | [diff] [blame] | 288 | static const struct of_device_id ak4104_of_match[] = { |
| 289 | { .compatible = "asahi-kasei,ak4104", }, |
| 290 | { } |
| 291 | }; |
| 292 | MODULE_DEVICE_TABLE(of, ak4104_of_match); |
| 293 | |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 294 | static struct spi_driver ak4104_spi_driver = { |
| 295 | .driver = { |
| 296 | .name = DRV_NAME, |
| 297 | .owner = THIS_MODULE, |
Daniel Mack | ac5dbea | 2012-10-07 17:51:24 +0200 | [diff] [blame] | 298 | .of_match_table = ak4104_of_match, |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 299 | }, |
| 300 | .probe = ak4104_spi_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 301 | .remove = ak4104_spi_remove, |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 302 | }; |
| 303 | |
Mark Brown | 38d78ba | 2012-02-16 22:50:35 -0800 | [diff] [blame] | 304 | module_spi_driver(ak4104_spi_driver); |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 305 | |
| 306 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); |
| 307 | MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver"); |
| 308 | MODULE_LICENSE("GPL"); |
| 309 | |