ASoC: rt5640: Correct the judgement of data length
[deliverable/linux.git] / sound / soc / codecs / ak4104.c
CommitLineData
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>
385a4c2e
DM
18#include <linux/of_device.h>
19#include <linux/of_gpio.h>
a381934e
DM
20#include <sound/asoundef.h>
21
a381934e
DM
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
a381934e 48struct ak4104_private {
2901d6eb 49 struct regmap *regmap;
a381934e
DM
50};
51
2e61926c 52static const struct snd_soc_dapm_widget ak4104_dapm_widgets[] = {
a5db4d50
MB
53SND_SOC_DAPM_PGA("TXE", AK4104_REG_TX, AK4104_TX_TXE, 0, NULL, 0),
54
2e61926c
MB
55SND_SOC_DAPM_OUTPUT("TX"),
56};
57
58static const struct snd_soc_dapm_route ak4104_dapm_routes[] = {
a5db4d50
MB
59 { "TXE", NULL, "Playback" },
60 { "TX", NULL, "TXE" },
2e61926c
MB
61};
62
a381934e
DM
63static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai,
64 unsigned int format)
65{
66 struct snd_soc_codec *codec = codec_dai->codec;
b0ec761b 67 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
a381934e 68 int val = 0;
2901d6eb 69 int ret;
a381934e 70
a381934e
DM
71 /* set DAI format */
72 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
73 case SND_SOC_DAIFMT_RIGHT_J:
74 break;
75 case SND_SOC_DAIFMT_LEFT_J:
76 val |= AK4104_CONTROL1_DIF0;
77 break;
78 case SND_SOC_DAIFMT_I2S:
79 val |= AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1;
80 break;
81 default:
82 dev_err(codec->dev, "invalid dai format\n");
83 return -EINVAL;
84 }
85
86 /* This device can only be slave */
87 if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
88 return -EINVAL;
89
b0ec761b
DM
90 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
91 AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1,
92 val);
afad95f8
MB
93 if (ret < 0)
94 return ret;
95
96 return 0;
a381934e
DM
97}
98
99static int ak4104_hw_params(struct snd_pcm_substream *substream,
100 struct snd_pcm_hw_params *params,
101 struct snd_soc_dai *dai)
102{
e6968a17 103 struct snd_soc_codec *codec = dai->codec;
b0ec761b 104 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
b692a436 105 int ret, val = 0;
a381934e
DM
106
107 /* set the IEC958 bits: consumer mode, no copyright bit */
108 val |= IEC958_AES0_CON_NOT_COPYRIGHT;
b0ec761b 109 regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(0), val);
a381934e
DM
110
111 val = 0;
112
113 switch (params_rate(params)) {
08201deb
DM
114 case 22050:
115 val |= IEC958_AES3_CON_FS_22050;
116 break;
117 case 24000:
118 val |= IEC958_AES3_CON_FS_24000;
119 break;
120 case 32000:
121 val |= IEC958_AES3_CON_FS_32000;
122 break;
a381934e
DM
123 case 44100:
124 val |= IEC958_AES3_CON_FS_44100;
125 break;
126 case 48000:
127 val |= IEC958_AES3_CON_FS_48000;
128 break;
08201deb
DM
129 case 88200:
130 val |= IEC958_AES3_CON_FS_88200;
131 break;
132 case 96000:
133 val |= IEC958_AES3_CON_FS_96000;
134 break;
135 case 176400:
136 val |= IEC958_AES3_CON_FS_176400;
137 break;
138 case 192000:
139 val |= IEC958_AES3_CON_FS_192000;
a381934e
DM
140 break;
141 default:
142 dev_err(codec->dev, "unsupported sampling rate\n");
143 return -EINVAL;
144 }
145
b692a436
DM
146 ret = regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(3), val);
147 if (ret < 0)
148 return ret;
149
b692a436
DM
150 return 0;
151}
152
85e7652d 153static const struct snd_soc_dai_ops ak4101_dai_ops = {
65ec1cd1
MB
154 .hw_params = ak4104_hw_params,
155 .set_fmt = ak4104_set_dai_fmt,
156};
157
f0fba2ad
LG
158static struct snd_soc_dai_driver ak4104_dai = {
159 .name = "ak4104-hifi",
a381934e
DM
160 .playback = {
161 .stream_name = "Playback",
162 .channels_min = 2,
163 .channels_max = 2,
617b14c5 164 .rates = SNDRV_PCM_RATE_8000_192000,
a381934e
DM
165 .formats = SNDRV_PCM_FMTBIT_S16_LE |
166 SNDRV_PCM_FMTBIT_S24_3LE |
167 SNDRV_PCM_FMTBIT_S24_LE
168 },
65ec1cd1 169 .ops = &ak4101_dai_ops,
a381934e
DM
170};
171
f0fba2ad 172static int ak4104_probe(struct snd_soc_codec *codec)
a381934e 173{
f0fba2ad 174 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
2901d6eb 175 int ret;
a381934e 176
a381934e 177 /* set power-up and non-reset bits */
b0ec761b
DM
178 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
179 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN,
180 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
a381934e 181 if (ret < 0)
f0fba2ad 182 return ret;
a381934e
DM
183
184 /* enable transmitter */
b0ec761b
DM
185 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_TX,
186 AK4104_TX_TXE, AK4104_TX_TXE);
a381934e 187 if (ret < 0)
f0fba2ad 188 return ret;
a381934e 189
a381934e 190 return 0;
a381934e
DM
191}
192
f0fba2ad 193static int ak4104_remove(struct snd_soc_codec *codec)
a381934e 194{
b0ec761b
DM
195 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
196
197 regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
198 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0);
a381934e 199
afad95f8 200 return 0;
a381934e
DM
201}
202
f0fba2ad
LG
203static struct snd_soc_codec_driver soc_codec_device_ak4104 = {
204 .probe = ak4104_probe,
205 .remove = ak4104_remove,
2e61926c
MB
206
207 .dapm_widgets = ak4104_dapm_widgets,
208 .num_dapm_widgets = ARRAY_SIZE(ak4104_dapm_widgets),
209 .dapm_routes = ak4104_dapm_routes,
210 .num_dapm_routes = ARRAY_SIZE(ak4104_dapm_routes),
2901d6eb
MB
211};
212
213static const struct regmap_config ak4104_regmap = {
214 .reg_bits = 8,
215 .val_bits = 8,
216
217 .max_register = AK4104_NUM_REGS - 1,
218 .read_flag_mask = AK4104_READ,
219 .write_flag_mask = AK4104_WRITE,
220
221 .cache_type = REGCACHE_RBTREE,
f0fba2ad
LG
222};
223
224static int ak4104_spi_probe(struct spi_device *spi)
a381934e 225{
385a4c2e 226 struct device_node *np = spi->dev.of_node;
f0fba2ad 227 struct ak4104_private *ak4104;
2901d6eb 228 unsigned int val;
a381934e
DM
229 int ret;
230
f0fba2ad
LG
231 spi->bits_per_word = 8;
232 spi->mode = SPI_MODE_0;
233 ret = spi_setup(spi);
234 if (ret < 0)
a381934e 235 return ret;
a381934e 236
3922d518
AL
237 ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private),
238 GFP_KERNEL);
f0fba2ad
LG
239 if (ak4104 == NULL)
240 return -ENOMEM;
241
a273cd13 242 ak4104->regmap = devm_regmap_init_spi(spi, &ak4104_regmap);
2901d6eb
MB
243 if (IS_ERR(ak4104->regmap)) {
244 ret = PTR_ERR(ak4104->regmap);
245 return ret;
246 }
247
385a4c2e
DM
248 if (np) {
249 enum of_gpio_flags flags;
250 int gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &flags);
251
252 if (gpio_is_valid(gpio)) {
253 ret = devm_gpio_request_one(&spi->dev, gpio,
254 flags & OF_GPIO_ACTIVE_LOW ?
255 GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
256 "ak4104 reset");
257 if (ret < 0)
258 return ret;
259 }
260 }
261
2901d6eb
MB
262 /* read the 'reserved' register - according to the datasheet, it
263 * should contain 0x5b. Not a good way to verify the presence of
264 * the device, but there is no hardware ID register. */
265 ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val);
266 if (ret != 0)
a273cd13
TB
267 return ret;
268 if (val != AK4104_RESERVED_VAL)
269 return -ENODEV;
2901d6eb 270
f0fba2ad
LG
271 spi_set_drvdata(spi, ak4104);
272
273 ret = snd_soc_register_codec(&spi->dev,
274 &soc_codec_device_ak4104, &ak4104_dai, 1);
f0fba2ad 275 return ret;
a381934e
DM
276}
277
7a79e94e 278static int ak4104_spi_remove(struct spi_device *spi)
a381934e 279{
f0fba2ad 280 snd_soc_unregister_codec(&spi->dev);
a381934e 281 return 0;
f0fba2ad 282}
a381934e 283
ac5dbea0
DM
284static const struct of_device_id ak4104_of_match[] = {
285 { .compatible = "asahi-kasei,ak4104", },
286 { }
287};
288MODULE_DEVICE_TABLE(of, ak4104_of_match);
289
193b2f65
DM
290static const struct spi_device_id ak4104_id_table[] = {
291 { "ak4104", 0 },
292 { }
293};
294MODULE_DEVICE_TABLE(spi, ak4104_id_table);
295
a381934e
DM
296static struct spi_driver ak4104_spi_driver = {
297 .driver = {
193b2f65 298 .name = "ak4104",
a381934e 299 .owner = THIS_MODULE,
ac5dbea0 300 .of_match_table = ak4104_of_match,
a381934e 301 },
193b2f65 302 .id_table = ak4104_id_table,
a381934e 303 .probe = ak4104_spi_probe,
7a79e94e 304 .remove = ak4104_spi_remove,
a381934e
DM
305};
306
38d78baf 307module_spi_driver(ak4104_spi_driver);
a381934e
DM
308
309MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
310MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
311MODULE_LICENSE("GPL");
312
This page took 0.20851 seconds and 5 git commands to generate.