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> |
| 18 | #include <sound/asoundef.h> |
| 19 | |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 20 | /* AK4104 registers addresses */ |
| 21 | #define AK4104_REG_CONTROL1 0x00 |
| 22 | #define AK4104_REG_RESERVED 0x01 |
| 23 | #define AK4104_REG_CONTROL2 0x02 |
| 24 | #define AK4104_REG_TX 0x03 |
| 25 | #define AK4104_REG_CHN_STATUS(x) ((x) + 0x04) |
| 26 | #define AK4104_NUM_REGS 10 |
| 27 | |
| 28 | #define AK4104_REG_MASK 0x1f |
| 29 | #define AK4104_READ 0xc0 |
| 30 | #define AK4104_WRITE 0xe0 |
| 31 | #define AK4104_RESERVED_VAL 0x5b |
| 32 | |
| 33 | /* Bit masks for AK4104 registers */ |
| 34 | #define AK4104_CONTROL1_RSTN (1 << 0) |
| 35 | #define AK4104_CONTROL1_PW (1 << 1) |
| 36 | #define AK4104_CONTROL1_DIF0 (1 << 2) |
| 37 | #define AK4104_CONTROL1_DIF1 (1 << 3) |
| 38 | |
| 39 | #define AK4104_CONTROL2_SEL0 (1 << 0) |
| 40 | #define AK4104_CONTROL2_SEL1 (1 << 1) |
| 41 | #define AK4104_CONTROL2_MODE (1 << 2) |
| 42 | |
| 43 | #define AK4104_TX_TXE (1 << 0) |
| 44 | #define AK4104_TX_V (1 << 1) |
| 45 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 46 | #define DRV_NAME "ak4104-codec" |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 47 | |
| 48 | struct ak4104_private { |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 49 | struct regmap *regmap; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 52 | static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 53 | unsigned int format) |
| 54 | { |
| 55 | struct snd_soc_codec *codec = codec_dai->codec; |
| 56 | int val = 0; |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 57 | int ret; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 58 | |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 59 | /* set DAI format */ |
| 60 | switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 61 | case SND_SOC_DAIFMT_RIGHT_J: |
| 62 | break; |
| 63 | case SND_SOC_DAIFMT_LEFT_J: |
| 64 | val |= AK4104_CONTROL1_DIF0; |
| 65 | break; |
| 66 | case SND_SOC_DAIFMT_I2S: |
| 67 | val |= AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1; |
| 68 | break; |
| 69 | default: |
| 70 | dev_err(codec->dev, "invalid dai format\n"); |
| 71 | return -EINVAL; |
| 72 | } |
| 73 | |
| 74 | /* This device can only be slave */ |
| 75 | if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) |
| 76 | return -EINVAL; |
| 77 | |
Mark Brown | afad95f | 2012-02-17 12:04:41 -0800 | [diff] [blame] | 78 | ret = snd_soc_update_bits(codec, AK4104_REG_CONTROL1, |
| 79 | AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1, |
| 80 | val); |
| 81 | if (ret < 0) |
| 82 | return ret; |
| 83 | |
| 84 | return 0; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static int ak4104_hw_params(struct snd_pcm_substream *substream, |
| 88 | struct snd_pcm_hw_params *params, |
| 89 | struct snd_soc_dai *dai) |
| 90 | { |
| 91 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 92 | struct snd_soc_codec *codec = rtd->codec; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 93 | int val = 0; |
| 94 | |
| 95 | /* set the IEC958 bits: consumer mode, no copyright bit */ |
| 96 | val |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Mark Brown | 34baf22 | 2012-02-17 12:05:51 -0800 | [diff] [blame] | 97 | snd_soc_write(codec, AK4104_REG_CHN_STATUS(0), val); |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 98 | |
| 99 | val = 0; |
| 100 | |
| 101 | switch (params_rate(params)) { |
| 102 | case 44100: |
| 103 | val |= IEC958_AES3_CON_FS_44100; |
| 104 | break; |
| 105 | case 48000: |
| 106 | val |= IEC958_AES3_CON_FS_48000; |
| 107 | break; |
| 108 | case 32000: |
| 109 | val |= IEC958_AES3_CON_FS_32000; |
| 110 | break; |
| 111 | default: |
| 112 | dev_err(codec->dev, "unsupported sampling rate\n"); |
| 113 | return -EINVAL; |
| 114 | } |
| 115 | |
Mark Brown | 34baf22 | 2012-02-17 12:05:51 -0800 | [diff] [blame] | 116 | return snd_soc_write(codec, AK4104_REG_CHN_STATUS(3), val); |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 119 | static const struct snd_soc_dai_ops ak4101_dai_ops = { |
Mark Brown | 65ec1cd | 2009-03-11 16:51:31 +0000 | [diff] [blame] | 120 | .hw_params = ak4104_hw_params, |
| 121 | .set_fmt = ak4104_set_dai_fmt, |
| 122 | }; |
| 123 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 124 | static struct snd_soc_dai_driver ak4104_dai = { |
| 125 | .name = "ak4104-hifi", |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 126 | .playback = { |
| 127 | .stream_name = "Playback", |
| 128 | .channels_min = 2, |
| 129 | .channels_max = 2, |
Daniel Mack | 617b14c | 2010-01-13 11:25:05 +0100 | [diff] [blame] | 130 | .rates = SNDRV_PCM_RATE_8000_192000, |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 131 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
| 132 | SNDRV_PCM_FMTBIT_S24_3LE | |
| 133 | SNDRV_PCM_FMTBIT_S24_LE |
| 134 | }, |
Mark Brown | 65ec1cd | 2009-03-11 16:51:31 +0000 | [diff] [blame] | 135 | .ops = &ak4101_dai_ops, |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 136 | }; |
| 137 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 138 | static int ak4104_probe(struct snd_soc_codec *codec) |
| 139 | { |
| 140 | struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec); |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 141 | int ret; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 142 | |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 143 | codec->control_data = ak4104->regmap; |
| 144 | ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP); |
| 145 | if (ret != 0) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 146 | return ret; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 147 | |
| 148 | /* set power-up and non-reset bits */ |
Mark Brown | afad95f | 2012-02-17 12:04:41 -0800 | [diff] [blame] | 149 | ret = snd_soc_update_bits(codec, AK4104_REG_CONTROL1, |
| 150 | AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, |
| 151 | AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 152 | if (ret < 0) |
| 153 | return ret; |
| 154 | |
| 155 | /* enable transmitter */ |
Mark Brown | afad95f | 2012-02-17 12:04:41 -0800 | [diff] [blame] | 156 | ret = snd_soc_update_bits(codec, AK4104_REG_TX, |
| 157 | AK4104_TX_TXE, AK4104_TX_TXE); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 158 | if (ret < 0) |
| 159 | return ret; |
| 160 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int ak4104_remove(struct snd_soc_codec *codec) |
| 165 | { |
Mark Brown | afad95f | 2012-02-17 12:04:41 -0800 | [diff] [blame] | 166 | snd_soc_update_bits(codec, AK4104_REG_CONTROL1, |
| 167 | AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 168 | |
Mark Brown | afad95f | 2012-02-17 12:04:41 -0800 | [diff] [blame] | 169 | return 0; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static struct snd_soc_codec_driver soc_codec_device_ak4104 = { |
| 173 | .probe = ak4104_probe, |
| 174 | .remove = ak4104_remove, |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | static const struct regmap_config ak4104_regmap = { |
| 178 | .reg_bits = 8, |
| 179 | .val_bits = 8, |
| 180 | |
| 181 | .max_register = AK4104_NUM_REGS - 1, |
| 182 | .read_flag_mask = AK4104_READ, |
| 183 | .write_flag_mask = AK4104_WRITE, |
| 184 | |
| 185 | .cache_type = REGCACHE_RBTREE, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 186 | }; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 187 | |
| 188 | static int ak4104_spi_probe(struct spi_device *spi) |
| 189 | { |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 190 | struct ak4104_private *ak4104; |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 191 | unsigned int val; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 192 | int ret; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 193 | |
| 194 | spi->bits_per_word = 8; |
| 195 | spi->mode = SPI_MODE_0; |
| 196 | ret = spi_setup(spi); |
| 197 | if (ret < 0) |
| 198 | return ret; |
| 199 | |
Axel Lin | 3922d51 | 2011-12-20 14:37:12 +0800 | [diff] [blame] | 200 | ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private), |
| 201 | GFP_KERNEL); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 202 | if (ak4104 == NULL) |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 203 | return -ENOMEM; |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 204 | |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 205 | ak4104->regmap = regmap_init_spi(spi, &ak4104_regmap); |
| 206 | if (IS_ERR(ak4104->regmap)) { |
| 207 | ret = PTR_ERR(ak4104->regmap); |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | /* read the 'reserved' register - according to the datasheet, it |
| 212 | * should contain 0x5b. Not a good way to verify the presence of |
| 213 | * the device, but there is no hardware ID register. */ |
| 214 | ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val); |
| 215 | if (ret != 0) |
| 216 | goto err; |
| 217 | if (val != AK4104_RESERVED_VAL) { |
| 218 | ret = -ENODEV; |
| 219 | goto err; |
| 220 | } |
| 221 | |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 222 | spi_set_drvdata(spi, ak4104); |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 223 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 224 | ret = snd_soc_register_codec(&spi->dev, |
| 225 | &soc_codec_device_ak4104, &ak4104_dai, 1); |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 226 | if (ret != 0) |
| 227 | goto err; |
| 228 | |
| 229 | return 0; |
| 230 | |
| 231 | err: |
| 232 | regmap_exit(ak4104->regmap); |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | static int __devexit ak4104_spi_remove(struct spi_device *spi) |
| 237 | { |
Mark Brown | 2901d6e | 2012-02-17 12:14:18 -0800 | [diff] [blame] | 238 | struct ak4104_private *ak4101 = spi_get_drvdata(spi); |
| 239 | regmap_exit(ak4101->regmap); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 240 | snd_soc_unregister_codec(&spi->dev); |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 244 | static struct spi_driver ak4104_spi_driver = { |
| 245 | .driver = { |
| 246 | .name = DRV_NAME, |
| 247 | .owner = THIS_MODULE, |
| 248 | }, |
| 249 | .probe = ak4104_spi_probe, |
| 250 | .remove = __devexit_p(ak4104_spi_remove), |
| 251 | }; |
| 252 | |
Mark Brown | 38d78ba | 2012-02-16 22:50:35 -0800 | [diff] [blame] | 253 | module_spi_driver(ak4104_spi_driver); |
Daniel Mack | a381934 | 2009-03-09 02:13:17 +0100 | [diff] [blame] | 254 | |
| 255 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); |
| 256 | MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver"); |
| 257 | MODULE_LICENSE("GPL"); |
| 258 | |