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