blob: b4819dcd4f4dc73f781383681d0ed4198ac2f220 [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
Daniel Macka3819342009-03-09 02:13:17 +010048struct ak4104_private {
Mark Brown2901d6e2012-02-17 12:14:18 -080049 struct regmap *regmap;
Daniel Macka3819342009-03-09 02:13:17 +010050};
51
Mark Brown2e619262013-08-07 19:01:40 +010052static const struct snd_soc_dapm_widget ak4104_dapm_widgets[] = {
Mark Browna5db4d52013-08-07 19:05:47 +010053SND_SOC_DAPM_PGA("TXE", AK4104_REG_TX, AK4104_TX_TXE, 0, NULL, 0),
54
Mark Brown2e619262013-08-07 19:01:40 +010055SND_SOC_DAPM_OUTPUT("TX"),
56};
57
58static const struct snd_soc_dapm_route ak4104_dapm_routes[] = {
Mark Browna5db4d52013-08-07 19:05:47 +010059 { "TXE", NULL, "Playback" },
60 { "TX", NULL, "TXE" },
Mark Brown2e619262013-08-07 19:01:40 +010061};
62
Daniel Macka3819342009-03-09 02:13:17 +010063static 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;
Daniel Mackb0ec7612013-03-06 22:22:15 +010067 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
Daniel Macka3819342009-03-09 02:13:17 +010068 int val = 0;
Mark Brown2901d6e2012-02-17 12:14:18 -080069 int ret;
Daniel Macka3819342009-03-09 02:13:17 +010070
Daniel Macka3819342009-03-09 02:13:17 +010071 /* 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
Daniel Mackb0ec7612013-03-06 22:22:15 +010090 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
91 AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1,
92 val);
Mark Brownafad95f2012-02-17 12:04:41 -080093 if (ret < 0)
94 return ret;
95
96 return 0;
Daniel Macka3819342009-03-09 02:13:17 +010097}
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{
Mark Browne6968a12012-04-04 15:58:16 +0100103 struct snd_soc_codec *codec = dai->codec;
Daniel Mackb0ec7612013-03-06 22:22:15 +0100104 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
Daniel Mackb692a432013-03-06 22:22:16 +0100105 int ret, val = 0;
Daniel Macka3819342009-03-09 02:13:17 +0100106
107 /* set the IEC958 bits: consumer mode, no copyright bit */
108 val |= IEC958_AES0_CON_NOT_COPYRIGHT;
Daniel Mackb0ec7612013-03-06 22:22:15 +0100109 regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(0), val);
Daniel Macka3819342009-03-09 02:13:17 +0100110
111 val = 0;
112
113 switch (params_rate(params)) {
Daniel Mack08201de2012-10-07 17:51:23 +0200114 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;
Daniel Macka3819342009-03-09 02:13:17 +0100123 case 44100:
124 val |= IEC958_AES3_CON_FS_44100;
125 break;
126 case 48000:
127 val |= IEC958_AES3_CON_FS_48000;
128 break;
Daniel Mack08201de2012-10-07 17:51:23 +0200129 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;
Daniel Macka3819342009-03-09 02:13:17 +0100140 break;
141 default:
142 dev_err(codec->dev, "unsupported sampling rate\n");
143 return -EINVAL;
144 }
145
Daniel Mackb692a432013-03-06 22:22:16 +0100146 ret = regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(3), val);
147 if (ret < 0)
148 return ret;
149
Daniel Mackb692a432013-03-06 22:22:16 +0100150 return 0;
151}
152
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100153static const struct snd_soc_dai_ops ak4101_dai_ops = {
Mark Brown65ec1cd2009-03-11 16:51:31 +0000154 .hw_params = ak4104_hw_params,
155 .set_fmt = ak4104_set_dai_fmt,
156};
157
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000158static struct snd_soc_dai_driver ak4104_dai = {
159 .name = "ak4104-hifi",
Daniel Macka3819342009-03-09 02:13:17 +0100160 .playback = {
161 .stream_name = "Playback",
162 .channels_min = 2,
163 .channels_max = 2,
Daniel Mack617b14c2010-01-13 11:25:05 +0100164 .rates = SNDRV_PCM_RATE_8000_192000,
Daniel Macka3819342009-03-09 02:13:17 +0100165 .formats = SNDRV_PCM_FMTBIT_S16_LE |
166 SNDRV_PCM_FMTBIT_S24_3LE |
167 SNDRV_PCM_FMTBIT_S24_LE
168 },
Mark Brown65ec1cd2009-03-11 16:51:31 +0000169 .ops = &ak4101_dai_ops,
Daniel Macka3819342009-03-09 02:13:17 +0100170};
171
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000172static int ak4104_probe(struct snd_soc_codec *codec)
173{
174 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
Mark Brown2901d6e2012-02-17 12:14:18 -0800175 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000176
Mark Brown2901d6e2012-02-17 12:14:18 -0800177 codec->control_data = ak4104->regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000178
179 /* set power-up and non-reset bits */
Daniel Mackb0ec7612013-03-06 22:22:15 +0100180 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
181 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN,
182 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000183 if (ret < 0)
184 return ret;
185
186 /* enable transmitter */
Daniel Mackb0ec7612013-03-06 22:22:15 +0100187 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_TX,
188 AK4104_TX_TXE, AK4104_TX_TXE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000189 if (ret < 0)
190 return ret;
191
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000192 return 0;
193}
194
195static int ak4104_remove(struct snd_soc_codec *codec)
196{
Daniel Mackb0ec7612013-03-06 22:22:15 +0100197 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
198
199 regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
200 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000201
Mark Brownafad95f2012-02-17 12:04:41 -0800202 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000203}
204
205static struct snd_soc_codec_driver soc_codec_device_ak4104 = {
206 .probe = ak4104_probe,
207 .remove = ak4104_remove,
Mark Brown2e619262013-08-07 19:01:40 +0100208
209 .dapm_widgets = ak4104_dapm_widgets,
210 .num_dapm_widgets = ARRAY_SIZE(ak4104_dapm_widgets),
211 .dapm_routes = ak4104_dapm_routes,
212 .num_dapm_routes = ARRAY_SIZE(ak4104_dapm_routes),
Mark Brown2901d6e2012-02-17 12:14:18 -0800213};
214
215static const struct regmap_config ak4104_regmap = {
216 .reg_bits = 8,
217 .val_bits = 8,
218
219 .max_register = AK4104_NUM_REGS - 1,
220 .read_flag_mask = AK4104_READ,
221 .write_flag_mask = AK4104_WRITE,
222
223 .cache_type = REGCACHE_RBTREE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000224};
Daniel Macka3819342009-03-09 02:13:17 +0100225
226static int ak4104_spi_probe(struct spi_device *spi)
227{
Daniel Mack385a4c22012-11-14 18:28:39 +0800228 struct device_node *np = spi->dev.of_node;
Daniel Macka3819342009-03-09 02:13:17 +0100229 struct ak4104_private *ak4104;
Mark Brown2901d6e2012-02-17 12:14:18 -0800230 unsigned int val;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000231 int ret;
Daniel Macka3819342009-03-09 02:13:17 +0100232
233 spi->bits_per_word = 8;
234 spi->mode = SPI_MODE_0;
235 ret = spi_setup(spi);
236 if (ret < 0)
237 return ret;
238
Axel Lin3922d512011-12-20 14:37:12 +0800239 ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private),
240 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000241 if (ak4104 == NULL)
Daniel Macka3819342009-03-09 02:13:17 +0100242 return -ENOMEM;
Daniel Macka3819342009-03-09 02:13:17 +0100243
Tushar Beheraa273cd12012-11-22 09:38:36 +0530244 ak4104->regmap = devm_regmap_init_spi(spi, &ak4104_regmap);
Mark Brown2901d6e2012-02-17 12:14:18 -0800245 if (IS_ERR(ak4104->regmap)) {
246 ret = PTR_ERR(ak4104->regmap);
247 return ret;
248 }
249
Daniel Mack385a4c22012-11-14 18:28:39 +0800250 if (np) {
251 enum of_gpio_flags flags;
252 int gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &flags);
253
254 if (gpio_is_valid(gpio)) {
255 ret = devm_gpio_request_one(&spi->dev, gpio,
256 flags & OF_GPIO_ACTIVE_LOW ?
257 GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
258 "ak4104 reset");
259 if (ret < 0)
260 return ret;
261 }
262 }
263
Mark Brown2901d6e2012-02-17 12:14:18 -0800264 /* read the 'reserved' register - according to the datasheet, it
265 * should contain 0x5b. Not a good way to verify the presence of
266 * the device, but there is no hardware ID register. */
267 ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val);
268 if (ret != 0)
Tushar Beheraa273cd12012-11-22 09:38:36 +0530269 return ret;
270 if (val != AK4104_RESERVED_VAL)
271 return -ENODEV;
Mark Brown2901d6e2012-02-17 12:14:18 -0800272
Daniel Macka3819342009-03-09 02:13:17 +0100273 spi_set_drvdata(spi, ak4104);
Daniel Macka3819342009-03-09 02:13:17 +0100274
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000275 ret = snd_soc_register_codec(&spi->dev,
276 &soc_codec_device_ak4104, &ak4104_dai, 1);
Daniel Macka3819342009-03-09 02:13:17 +0100277 return ret;
278}
279
Bill Pemberton7a79e942012-12-07 09:26:37 -0500280static int ak4104_spi_remove(struct spi_device *spi)
Daniel Macka3819342009-03-09 02:13:17 +0100281{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000282 snd_soc_unregister_codec(&spi->dev);
Daniel Macka3819342009-03-09 02:13:17 +0100283 return 0;
284}
285
Daniel Mackac5dbea2012-10-07 17:51:24 +0200286static const struct of_device_id ak4104_of_match[] = {
287 { .compatible = "asahi-kasei,ak4104", },
288 { }
289};
290MODULE_DEVICE_TABLE(of, ak4104_of_match);
291
Daniel Mack193b2f62013-09-16 18:14:20 +0200292static const struct spi_device_id ak4104_id_table[] = {
293 { "ak4104", 0 },
294 { }
295};
296MODULE_DEVICE_TABLE(spi, ak4104_id_table);
297
Daniel Macka3819342009-03-09 02:13:17 +0100298static struct spi_driver ak4104_spi_driver = {
299 .driver = {
Daniel Mack193b2f62013-09-16 18:14:20 +0200300 .name = "ak4104",
Daniel Macka3819342009-03-09 02:13:17 +0100301 .owner = THIS_MODULE,
Daniel Mackac5dbea2012-10-07 17:51:24 +0200302 .of_match_table = ak4104_of_match,
Daniel Macka3819342009-03-09 02:13:17 +0100303 },
Daniel Mack193b2f62013-09-16 18:14:20 +0200304 .id_table = ak4104_id_table,
Daniel Macka3819342009-03-09 02:13:17 +0100305 .probe = ak4104_spi_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500306 .remove = ak4104_spi_remove,
Daniel Macka3819342009-03-09 02:13:17 +0100307};
308
Mark Brown38d78ba2012-02-16 22:50:35 -0800309module_spi_driver(ak4104_spi_driver);
Daniel Macka3819342009-03-09 02:13:17 +0100310
311MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
312MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
313MODULE_LICENSE("GPL");
314