Commit | Line | Data |
---|---|---|
a381934e DM |
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> | |
5a0e3ad6 | 13 | #include <linux/slab.h> |
a381934e DM |
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 | ||
a381934e DM |
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 | ||
f0fba2ad | 46 | #define DRV_NAME "ak4104-codec" |
a381934e DM |
47 | |
48 | struct ak4104_private { | |
2901d6eb | 49 | struct regmap *regmap; |
a381934e DM |
50 | }; |
51 | ||
a381934e DM |
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; | |
2901d6eb | 57 | int ret; |
a381934e | 58 | |
a381934e DM |
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 | ||
afad95f8 MB |
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; | |
a381934e DM |
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 | { | |
e6968a17 | 91 | struct snd_soc_codec *codec = dai->codec; |
a381934e DM |
92 | int val = 0; |
93 | ||
94 | /* set the IEC958 bits: consumer mode, no copyright bit */ | |
95 | val |= IEC958_AES0_CON_NOT_COPYRIGHT; | |
34baf220 | 96 | snd_soc_write(codec, AK4104_REG_CHN_STATUS(0), val); |
a381934e DM |
97 | |
98 | val = 0; | |
99 | ||
100 | switch (params_rate(params)) { | |
101 | case 44100: | |
102 | val |= IEC958_AES3_CON_FS_44100; | |
103 | break; | |
104 | case 48000: | |
105 | val |= IEC958_AES3_CON_FS_48000; | |
106 | break; | |
107 | case 32000: | |
108 | val |= IEC958_AES3_CON_FS_32000; | |
109 | break; | |
110 | default: | |
111 | dev_err(codec->dev, "unsupported sampling rate\n"); | |
112 | return -EINVAL; | |
113 | } | |
114 | ||
34baf220 | 115 | return snd_soc_write(codec, AK4104_REG_CHN_STATUS(3), val); |
a381934e DM |
116 | } |
117 | ||
85e7652d | 118 | static const struct snd_soc_dai_ops ak4101_dai_ops = { |
65ec1cd1 MB |
119 | .hw_params = ak4104_hw_params, |
120 | .set_fmt = ak4104_set_dai_fmt, | |
121 | }; | |
122 | ||
f0fba2ad LG |
123 | static struct snd_soc_dai_driver ak4104_dai = { |
124 | .name = "ak4104-hifi", | |
a381934e DM |
125 | .playback = { |
126 | .stream_name = "Playback", | |
127 | .channels_min = 2, | |
128 | .channels_max = 2, | |
617b14c5 | 129 | .rates = SNDRV_PCM_RATE_8000_192000, |
a381934e DM |
130 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
131 | SNDRV_PCM_FMTBIT_S24_3LE | | |
132 | SNDRV_PCM_FMTBIT_S24_LE | |
133 | }, | |
65ec1cd1 | 134 | .ops = &ak4101_dai_ops, |
a381934e DM |
135 | }; |
136 | ||
f0fba2ad | 137 | static int ak4104_probe(struct snd_soc_codec *codec) |
a381934e | 138 | { |
f0fba2ad | 139 | struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec); |
2901d6eb | 140 | int ret; |
a381934e | 141 | |
2901d6eb MB |
142 | codec->control_data = ak4104->regmap; |
143 | ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP); | |
144 | if (ret != 0) | |
a381934e | 145 | return ret; |
a381934e DM |
146 | |
147 | /* set power-up and non-reset bits */ | |
afad95f8 MB |
148 | ret = snd_soc_update_bits(codec, AK4104_REG_CONTROL1, |
149 | AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, | |
150 | AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN); | |
a381934e | 151 | if (ret < 0) |
f0fba2ad | 152 | return ret; |
a381934e DM |
153 | |
154 | /* enable transmitter */ | |
afad95f8 MB |
155 | ret = snd_soc_update_bits(codec, AK4104_REG_TX, |
156 | AK4104_TX_TXE, AK4104_TX_TXE); | |
a381934e | 157 | if (ret < 0) |
f0fba2ad | 158 | return ret; |
a381934e | 159 | |
a381934e | 160 | return 0; |
a381934e DM |
161 | } |
162 | ||
f0fba2ad | 163 | static int ak4104_remove(struct snd_soc_codec *codec) |
a381934e | 164 | { |
afad95f8 MB |
165 | snd_soc_update_bits(codec, AK4104_REG_CONTROL1, |
166 | AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0); | |
a381934e | 167 | |
afad95f8 | 168 | return 0; |
a381934e DM |
169 | } |
170 | ||
f0fba2ad LG |
171 | static struct snd_soc_codec_driver soc_codec_device_ak4104 = { |
172 | .probe = ak4104_probe, | |
173 | .remove = ak4104_remove, | |
2901d6eb MB |
174 | }; |
175 | ||
176 | static const struct regmap_config ak4104_regmap = { | |
177 | .reg_bits = 8, | |
178 | .val_bits = 8, | |
179 | ||
180 | .max_register = AK4104_NUM_REGS - 1, | |
181 | .read_flag_mask = AK4104_READ, | |
182 | .write_flag_mask = AK4104_WRITE, | |
183 | ||
184 | .cache_type = REGCACHE_RBTREE, | |
f0fba2ad LG |
185 | }; |
186 | ||
187 | static int ak4104_spi_probe(struct spi_device *spi) | |
a381934e | 188 | { |
f0fba2ad | 189 | struct ak4104_private *ak4104; |
2901d6eb | 190 | unsigned int val; |
a381934e DM |
191 | int ret; |
192 | ||
f0fba2ad LG |
193 | spi->bits_per_word = 8; |
194 | spi->mode = SPI_MODE_0; | |
195 | ret = spi_setup(spi); | |
196 | if (ret < 0) | |
a381934e | 197 | return ret; |
a381934e | 198 | |
3922d518 AL |
199 | ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private), |
200 | GFP_KERNEL); | |
f0fba2ad LG |
201 | if (ak4104 == NULL) |
202 | return -ENOMEM; | |
203 | ||
2901d6eb MB |
204 | ak4104->regmap = regmap_init_spi(spi, &ak4104_regmap); |
205 | if (IS_ERR(ak4104->regmap)) { | |
206 | ret = PTR_ERR(ak4104->regmap); | |
207 | return ret; | |
208 | } | |
209 | ||
210 | /* read the 'reserved' register - according to the datasheet, it | |
211 | * should contain 0x5b. Not a good way to verify the presence of | |
212 | * the device, but there is no hardware ID register. */ | |
213 | ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val); | |
214 | if (ret != 0) | |
215 | goto err; | |
216 | if (val != AK4104_RESERVED_VAL) { | |
217 | ret = -ENODEV; | |
218 | goto err; | |
219 | } | |
220 | ||
f0fba2ad LG |
221 | spi_set_drvdata(spi, ak4104); |
222 | ||
223 | ret = snd_soc_register_codec(&spi->dev, | |
224 | &soc_codec_device_ak4104, &ak4104_dai, 1); | |
2901d6eb MB |
225 | if (ret != 0) |
226 | goto err; | |
227 | ||
228 | return 0; | |
229 | ||
230 | err: | |
231 | regmap_exit(ak4104->regmap); | |
f0fba2ad | 232 | return ret; |
a381934e DM |
233 | } |
234 | ||
f0fba2ad | 235 | static int __devexit ak4104_spi_remove(struct spi_device *spi) |
a381934e | 236 | { |
2901d6eb MB |
237 | struct ak4104_private *ak4101 = spi_get_drvdata(spi); |
238 | regmap_exit(ak4101->regmap); | |
f0fba2ad | 239 | snd_soc_unregister_codec(&spi->dev); |
a381934e | 240 | return 0; |
f0fba2ad | 241 | } |
a381934e DM |
242 | |
243 | static struct spi_driver ak4104_spi_driver = { | |
244 | .driver = { | |
245 | .name = DRV_NAME, | |
246 | .owner = THIS_MODULE, | |
247 | }, | |
248 | .probe = ak4104_spi_probe, | |
249 | .remove = __devexit_p(ak4104_spi_remove), | |
250 | }; | |
251 | ||
38d78baf | 252 | module_spi_driver(ak4104_spi_driver); |
a381934e DM |
253 | |
254 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); | |
255 | MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver"); | |
256 | MODULE_LICENSE("GPL"); | |
257 |