blob: 9a7c89b9cb35520c9e45cd06a34c5d40bfaee67d [file] [log] [blame]
Daniel Macka3819342009-03-09 02:13:17 +01001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Daniel Macka3819342009-03-09 02:13:17 +010014#include <sound/core.h>
15#include <sound/soc.h>
16#include <sound/initval.h>
17#include <linux/spi/spi.h>
Daniel Mack385a4c22012-11-14 18:28:39 +080018#include <linux/of_device.h>
19#include <linux/of_gpio.h>
Daniel Macka3819342009-03-09 02:13:17 +010020#include <sound/asoundef.h>
21
Daniel Macka3819342009-03-09 02:13:17 +010022/* 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 Girdwoodf0fba2a2010-03-17 20:15:21 +000048#define DRV_NAME "ak4104-codec"
Daniel Macka3819342009-03-09 02:13:17 +010049
50struct ak4104_private {
Mark Brown2901d6e2012-02-17 12:14:18 -080051 struct regmap *regmap;
Daniel Macka3819342009-03-09 02:13:17 +010052};
53
Mark Brown2e619262013-08-07 19:01:40 +010054static const struct snd_soc_dapm_widget ak4104_dapm_widgets[] = {
55SND_SOC_DAPM_OUTPUT("TX"),
56};
57
58static const struct snd_soc_dapm_route ak4104_dapm_routes[] = {
59 { "TX", NULL, "Playback" },
60};
61
Daniel Macka3819342009-03-09 02:13:17 +010062static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai,
63 unsigned int format)
64{
65 struct snd_soc_codec *codec = codec_dai->codec;
Daniel Mackb0ec7612013-03-06 22:22:15 +010066 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
Daniel Macka3819342009-03-09 02:13:17 +010067 int val = 0;
Mark Brown2901d6e2012-02-17 12:14:18 -080068 int ret;
Daniel Macka3819342009-03-09 02:13:17 +010069
Daniel Macka3819342009-03-09 02:13:17 +010070 /* set DAI format */
71 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
72 case SND_SOC_DAIFMT_RIGHT_J:
73 break;
74 case SND_SOC_DAIFMT_LEFT_J:
75 val |= AK4104_CONTROL1_DIF0;
76 break;
77 case SND_SOC_DAIFMT_I2S:
78 val |= AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1;
79 break;
80 default:
81 dev_err(codec->dev, "invalid dai format\n");
82 return -EINVAL;
83 }
84
85 /* This device can only be slave */
86 if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
87 return -EINVAL;
88
Daniel Mackb0ec7612013-03-06 22:22:15 +010089 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
90 AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1,
91 val);
Mark Brownafad95f2012-02-17 12:04:41 -080092 if (ret < 0)
93 return ret;
94
95 return 0;
Daniel Macka3819342009-03-09 02:13:17 +010096}
97
98static int ak4104_hw_params(struct snd_pcm_substream *substream,
99 struct snd_pcm_hw_params *params,
100 struct snd_soc_dai *dai)
101{
Mark Browne6968a12012-04-04 15:58:16 +0100102 struct snd_soc_codec *codec = dai->codec;
Daniel Mackb0ec7612013-03-06 22:22:15 +0100103 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
Daniel Mackb692a432013-03-06 22:22:16 +0100104 int ret, val = 0;
Daniel Macka3819342009-03-09 02:13:17 +0100105
106 /* set the IEC958 bits: consumer mode, no copyright bit */
107 val |= IEC958_AES0_CON_NOT_COPYRIGHT;
Daniel Mackb0ec7612013-03-06 22:22:15 +0100108 regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(0), val);
Daniel Macka3819342009-03-09 02:13:17 +0100109
110 val = 0;
111
112 switch (params_rate(params)) {
Daniel Mack08201de2012-10-07 17:51:23 +0200113 case 22050:
114 val |= IEC958_AES3_CON_FS_22050;
115 break;
116 case 24000:
117 val |= IEC958_AES3_CON_FS_24000;
118 break;
119 case 32000:
120 val |= IEC958_AES3_CON_FS_32000;
121 break;
Daniel Macka3819342009-03-09 02:13:17 +0100122 case 44100:
123 val |= IEC958_AES3_CON_FS_44100;
124 break;
125 case 48000:
126 val |= IEC958_AES3_CON_FS_48000;
127 break;
Daniel Mack08201de2012-10-07 17:51:23 +0200128 case 88200:
129 val |= IEC958_AES3_CON_FS_88200;
130 break;
131 case 96000:
132 val |= IEC958_AES3_CON_FS_96000;
133 break;
134 case 176400:
135 val |= IEC958_AES3_CON_FS_176400;
136 break;
137 case 192000:
138 val |= IEC958_AES3_CON_FS_192000;
Daniel Macka3819342009-03-09 02:13:17 +0100139 break;
140 default:
141 dev_err(codec->dev, "unsupported sampling rate\n");
142 return -EINVAL;
143 }
144
Daniel Mackb692a432013-03-06 22:22:16 +0100145 ret = regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(3), val);
146 if (ret < 0)
147 return ret;
148
149 /* enable transmitter */
150 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_TX,
151 AK4104_TX_TXE, AK4104_TX_TXE);
152 if (ret < 0)
153 return ret;
154
155 return 0;
156}
157
158static int ak4104_hw_free(struct snd_pcm_substream *substream,
159 struct snd_soc_dai *dai)
160{
161 struct snd_soc_codec *codec = dai->codec;
162 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
163
164 /* disable transmitter */
165 return regmap_update_bits(ak4104->regmap, AK4104_REG_TX,
166 AK4104_TX_TXE, 0);
Daniel Macka3819342009-03-09 02:13:17 +0100167}
168
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100169static const struct snd_soc_dai_ops ak4101_dai_ops = {
Mark Brown65ec1cd2009-03-11 16:51:31 +0000170 .hw_params = ak4104_hw_params,
Daniel Mackb692a432013-03-06 22:22:16 +0100171 .hw_free = ak4104_hw_free,
Mark Brown65ec1cd2009-03-11 16:51:31 +0000172 .set_fmt = ak4104_set_dai_fmt,
173};
174
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000175static struct snd_soc_dai_driver ak4104_dai = {
176 .name = "ak4104-hifi",
Daniel Macka3819342009-03-09 02:13:17 +0100177 .playback = {
178 .stream_name = "Playback",
179 .channels_min = 2,
180 .channels_max = 2,
Daniel Mack617b14c2010-01-13 11:25:05 +0100181 .rates = SNDRV_PCM_RATE_8000_192000,
Daniel Macka3819342009-03-09 02:13:17 +0100182 .formats = SNDRV_PCM_FMTBIT_S16_LE |
183 SNDRV_PCM_FMTBIT_S24_3LE |
184 SNDRV_PCM_FMTBIT_S24_LE
185 },
Mark Brown65ec1cd2009-03-11 16:51:31 +0000186 .ops = &ak4101_dai_ops,
Daniel Macka3819342009-03-09 02:13:17 +0100187};
188
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000189static int ak4104_probe(struct snd_soc_codec *codec)
190{
191 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
Mark Brown2901d6e2012-02-17 12:14:18 -0800192 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000193
Mark Brown2901d6e2012-02-17 12:14:18 -0800194 codec->control_data = ak4104->regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000195
196 /* set power-up and non-reset bits */
Daniel Mackb0ec7612013-03-06 22:22:15 +0100197 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
198 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN,
199 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000200 if (ret < 0)
201 return ret;
202
203 /* enable transmitter */
Daniel Mackb0ec7612013-03-06 22:22:15 +0100204 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_TX,
205 AK4104_TX_TXE, AK4104_TX_TXE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000206 if (ret < 0)
207 return ret;
208
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000209 return 0;
210}
211
212static int ak4104_remove(struct snd_soc_codec *codec)
213{
Daniel Mackb0ec7612013-03-06 22:22:15 +0100214 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
215
216 regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
217 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000218
Mark Brownafad95f2012-02-17 12:04:41 -0800219 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000220}
221
222static struct snd_soc_codec_driver soc_codec_device_ak4104 = {
223 .probe = ak4104_probe,
224 .remove = ak4104_remove,
Mark Brown2e619262013-08-07 19:01:40 +0100225
226 .dapm_widgets = ak4104_dapm_widgets,
227 .num_dapm_widgets = ARRAY_SIZE(ak4104_dapm_widgets),
228 .dapm_routes = ak4104_dapm_routes,
229 .num_dapm_routes = ARRAY_SIZE(ak4104_dapm_routes),
Mark Brown2901d6e2012-02-17 12:14:18 -0800230};
231
232static const struct regmap_config ak4104_regmap = {
233 .reg_bits = 8,
234 .val_bits = 8,
235
236 .max_register = AK4104_NUM_REGS - 1,
237 .read_flag_mask = AK4104_READ,
238 .write_flag_mask = AK4104_WRITE,
239
240 .cache_type = REGCACHE_RBTREE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000241};
Daniel Macka3819342009-03-09 02:13:17 +0100242
243static int ak4104_spi_probe(struct spi_device *spi)
244{
Daniel Mack385a4c22012-11-14 18:28:39 +0800245 struct device_node *np = spi->dev.of_node;
Daniel Macka3819342009-03-09 02:13:17 +0100246 struct ak4104_private *ak4104;
Mark Brown2901d6e2012-02-17 12:14:18 -0800247 unsigned int val;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000248 int ret;
Daniel Macka3819342009-03-09 02:13:17 +0100249
250 spi->bits_per_word = 8;
251 spi->mode = SPI_MODE_0;
252 ret = spi_setup(spi);
253 if (ret < 0)
254 return ret;
255
Axel Lin3922d512011-12-20 14:37:12 +0800256 ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private),
257 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000258 if (ak4104 == NULL)
Daniel Macka3819342009-03-09 02:13:17 +0100259 return -ENOMEM;
Daniel Macka3819342009-03-09 02:13:17 +0100260
Tushar Beheraa273cd12012-11-22 09:38:36 +0530261 ak4104->regmap = devm_regmap_init_spi(spi, &ak4104_regmap);
Mark Brown2901d6e2012-02-17 12:14:18 -0800262 if (IS_ERR(ak4104->regmap)) {
263 ret = PTR_ERR(ak4104->regmap);
264 return ret;
265 }
266
Daniel Mack385a4c22012-11-14 18:28:39 +0800267 if (np) {
268 enum of_gpio_flags flags;
269 int gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &flags);
270
271 if (gpio_is_valid(gpio)) {
272 ret = devm_gpio_request_one(&spi->dev, gpio,
273 flags & OF_GPIO_ACTIVE_LOW ?
274 GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
275 "ak4104 reset");
276 if (ret < 0)
277 return ret;
278 }
279 }
280
Mark Brown2901d6e2012-02-17 12:14:18 -0800281 /* read the 'reserved' register - according to the datasheet, it
282 * should contain 0x5b. Not a good way to verify the presence of
283 * the device, but there is no hardware ID register. */
284 ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val);
285 if (ret != 0)
Tushar Beheraa273cd12012-11-22 09:38:36 +0530286 return ret;
287 if (val != AK4104_RESERVED_VAL)
288 return -ENODEV;
Mark Brown2901d6e2012-02-17 12:14:18 -0800289
Daniel Macka3819342009-03-09 02:13:17 +0100290 spi_set_drvdata(spi, ak4104);
Daniel Macka3819342009-03-09 02:13:17 +0100291
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000292 ret = snd_soc_register_codec(&spi->dev,
293 &soc_codec_device_ak4104, &ak4104_dai, 1);
Daniel Macka3819342009-03-09 02:13:17 +0100294 return ret;
295}
296
Bill Pemberton7a79e942012-12-07 09:26:37 -0500297static int ak4104_spi_remove(struct spi_device *spi)
Daniel Macka3819342009-03-09 02:13:17 +0100298{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000299 snd_soc_unregister_codec(&spi->dev);
Daniel Macka3819342009-03-09 02:13:17 +0100300 return 0;
301}
302
Daniel Mackac5dbea2012-10-07 17:51:24 +0200303static const struct of_device_id ak4104_of_match[] = {
304 { .compatible = "asahi-kasei,ak4104", },
305 { }
306};
307MODULE_DEVICE_TABLE(of, ak4104_of_match);
308
Daniel Macka3819342009-03-09 02:13:17 +0100309static struct spi_driver ak4104_spi_driver = {
310 .driver = {
311 .name = DRV_NAME,
312 .owner = THIS_MODULE,
Daniel Mackac5dbea2012-10-07 17:51:24 +0200313 .of_match_table = ak4104_of_match,
Daniel Macka3819342009-03-09 02:13:17 +0100314 },
315 .probe = ak4104_spi_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500316 .remove = ak4104_spi_remove,
Daniel Macka3819342009-03-09 02:13:17 +0100317};
318
Mark Brown38d78ba2012-02-16 22:50:35 -0800319module_spi_driver(ak4104_spi_driver);
Daniel Macka3819342009-03-09 02:13:17 +0100320
321MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
322MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
323MODULE_LICENSE("GPL");
324