blob: 58f390d3ea7ae711ffabc8b8f20fb2d00a166d53 [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
Daniel Macka3819342009-03-09 02:13:17 +010054static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai,
55 unsigned int format)
56{
57 struct snd_soc_codec *codec = codec_dai->codec;
Daniel Mackb0ec7612013-03-06 22:22:15 +010058 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
Daniel Macka3819342009-03-09 02:13:17 +010059 int val = 0;
Mark Brown2901d6e2012-02-17 12:14:18 -080060 int ret;
Daniel Macka3819342009-03-09 02:13:17 +010061
Daniel Macka3819342009-03-09 02:13:17 +010062 /* set DAI format */
63 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
64 case SND_SOC_DAIFMT_RIGHT_J:
65 break;
66 case SND_SOC_DAIFMT_LEFT_J:
67 val |= AK4104_CONTROL1_DIF0;
68 break;
69 case SND_SOC_DAIFMT_I2S:
70 val |= AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1;
71 break;
72 default:
73 dev_err(codec->dev, "invalid dai format\n");
74 return -EINVAL;
75 }
76
77 /* This device can only be slave */
78 if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
79 return -EINVAL;
80
Daniel Mackb0ec7612013-03-06 22:22:15 +010081 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
82 AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1,
83 val);
Mark Brownafad95f2012-02-17 12:04:41 -080084 if (ret < 0)
85 return ret;
86
87 return 0;
Daniel Macka3819342009-03-09 02:13:17 +010088}
89
90static int ak4104_hw_params(struct snd_pcm_substream *substream,
91 struct snd_pcm_hw_params *params,
92 struct snd_soc_dai *dai)
93{
Mark Browne6968a12012-04-04 15:58:16 +010094 struct snd_soc_codec *codec = dai->codec;
Daniel Mackb0ec7612013-03-06 22:22:15 +010095 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
Daniel Macka3819342009-03-09 02:13:17 +010096 int val = 0;
97
98 /* set the IEC958 bits: consumer mode, no copyright bit */
99 val |= IEC958_AES0_CON_NOT_COPYRIGHT;
Daniel Mackb0ec7612013-03-06 22:22:15 +0100100 regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(0), val);
Daniel Macka3819342009-03-09 02:13:17 +0100101
102 val = 0;
103
104 switch (params_rate(params)) {
Daniel Mack08201de2012-10-07 17:51:23 +0200105 case 22050:
106 val |= IEC958_AES3_CON_FS_22050;
107 break;
108 case 24000:
109 val |= IEC958_AES3_CON_FS_24000;
110 break;
111 case 32000:
112 val |= IEC958_AES3_CON_FS_32000;
113 break;
Daniel Macka3819342009-03-09 02:13:17 +0100114 case 44100:
115 val |= IEC958_AES3_CON_FS_44100;
116 break;
117 case 48000:
118 val |= IEC958_AES3_CON_FS_48000;
119 break;
Daniel Mack08201de2012-10-07 17:51:23 +0200120 case 88200:
121 val |= IEC958_AES3_CON_FS_88200;
122 break;
123 case 96000:
124 val |= IEC958_AES3_CON_FS_96000;
125 break;
126 case 176400:
127 val |= IEC958_AES3_CON_FS_176400;
128 break;
129 case 192000:
130 val |= IEC958_AES3_CON_FS_192000;
Daniel Macka3819342009-03-09 02:13:17 +0100131 break;
132 default:
133 dev_err(codec->dev, "unsupported sampling rate\n");
134 return -EINVAL;
135 }
136
Daniel Mackb0ec7612013-03-06 22:22:15 +0100137 return regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(3), val);
Daniel Macka3819342009-03-09 02:13:17 +0100138}
139
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100140static const struct snd_soc_dai_ops ak4101_dai_ops = {
Mark Brown65ec1cd2009-03-11 16:51:31 +0000141 .hw_params = ak4104_hw_params,
142 .set_fmt = ak4104_set_dai_fmt,
143};
144
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145static struct snd_soc_dai_driver ak4104_dai = {
146 .name = "ak4104-hifi",
Daniel Macka3819342009-03-09 02:13:17 +0100147 .playback = {
148 .stream_name = "Playback",
149 .channels_min = 2,
150 .channels_max = 2,
Daniel Mack617b14c2010-01-13 11:25:05 +0100151 .rates = SNDRV_PCM_RATE_8000_192000,
Daniel Macka3819342009-03-09 02:13:17 +0100152 .formats = SNDRV_PCM_FMTBIT_S16_LE |
153 SNDRV_PCM_FMTBIT_S24_3LE |
154 SNDRV_PCM_FMTBIT_S24_LE
155 },
Mark Brown65ec1cd2009-03-11 16:51:31 +0000156 .ops = &ak4101_dai_ops,
Daniel Macka3819342009-03-09 02:13:17 +0100157};
158
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000159static int ak4104_probe(struct snd_soc_codec *codec)
160{
161 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
Mark Brown2901d6e2012-02-17 12:14:18 -0800162 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000163
Mark Brown2901d6e2012-02-17 12:14:18 -0800164 codec->control_data = ak4104->regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000165
166 /* set power-up and non-reset bits */
Daniel Mackb0ec7612013-03-06 22:22:15 +0100167 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
168 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN,
169 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000170 if (ret < 0)
171 return ret;
172
173 /* enable transmitter */
Daniel Mackb0ec7612013-03-06 22:22:15 +0100174 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_TX,
175 AK4104_TX_TXE, AK4104_TX_TXE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000176 if (ret < 0)
177 return ret;
178
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000179 return 0;
180}
181
182static int ak4104_remove(struct snd_soc_codec *codec)
183{
Daniel Mackb0ec7612013-03-06 22:22:15 +0100184 struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
185
186 regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
187 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000188
Mark Brownafad95f2012-02-17 12:04:41 -0800189 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000190}
191
192static struct snd_soc_codec_driver soc_codec_device_ak4104 = {
193 .probe = ak4104_probe,
194 .remove = ak4104_remove,
Mark Brown2901d6e2012-02-17 12:14:18 -0800195};
196
197static const struct regmap_config ak4104_regmap = {
198 .reg_bits = 8,
199 .val_bits = 8,
200
201 .max_register = AK4104_NUM_REGS - 1,
202 .read_flag_mask = AK4104_READ,
203 .write_flag_mask = AK4104_WRITE,
204
205 .cache_type = REGCACHE_RBTREE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000206};
Daniel Macka3819342009-03-09 02:13:17 +0100207
208static int ak4104_spi_probe(struct spi_device *spi)
209{
Daniel Mack385a4c22012-11-14 18:28:39 +0800210 struct device_node *np = spi->dev.of_node;
Daniel Macka3819342009-03-09 02:13:17 +0100211 struct ak4104_private *ak4104;
Mark Brown2901d6e2012-02-17 12:14:18 -0800212 unsigned int val;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000213 int ret;
Daniel Macka3819342009-03-09 02:13:17 +0100214
215 spi->bits_per_word = 8;
216 spi->mode = SPI_MODE_0;
217 ret = spi_setup(spi);
218 if (ret < 0)
219 return ret;
220
Axel Lin3922d512011-12-20 14:37:12 +0800221 ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private),
222 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000223 if (ak4104 == NULL)
Daniel Macka3819342009-03-09 02:13:17 +0100224 return -ENOMEM;
Daniel Macka3819342009-03-09 02:13:17 +0100225
Tushar Beheraa273cd12012-11-22 09:38:36 +0530226 ak4104->regmap = devm_regmap_init_spi(spi, &ak4104_regmap);
Mark Brown2901d6e2012-02-17 12:14:18 -0800227 if (IS_ERR(ak4104->regmap)) {
228 ret = PTR_ERR(ak4104->regmap);
229 return ret;
230 }
231
Daniel Mack385a4c22012-11-14 18:28:39 +0800232 if (np) {
233 enum of_gpio_flags flags;
234 int gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &flags);
235
236 if (gpio_is_valid(gpio)) {
237 ret = devm_gpio_request_one(&spi->dev, gpio,
238 flags & OF_GPIO_ACTIVE_LOW ?
239 GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
240 "ak4104 reset");
241 if (ret < 0)
242 return ret;
243 }
244 }
245
Mark Brown2901d6e2012-02-17 12:14:18 -0800246 /* read the 'reserved' register - according to the datasheet, it
247 * should contain 0x5b. Not a good way to verify the presence of
248 * the device, but there is no hardware ID register. */
249 ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val);
250 if (ret != 0)
Tushar Beheraa273cd12012-11-22 09:38:36 +0530251 return ret;
252 if (val != AK4104_RESERVED_VAL)
253 return -ENODEV;
Mark Brown2901d6e2012-02-17 12:14:18 -0800254
Daniel Macka3819342009-03-09 02:13:17 +0100255 spi_set_drvdata(spi, ak4104);
Daniel Macka3819342009-03-09 02:13:17 +0100256
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000257 ret = snd_soc_register_codec(&spi->dev,
258 &soc_codec_device_ak4104, &ak4104_dai, 1);
Daniel Macka3819342009-03-09 02:13:17 +0100259 return ret;
260}
261
Bill Pemberton7a79e942012-12-07 09:26:37 -0500262static int ak4104_spi_remove(struct spi_device *spi)
Daniel Macka3819342009-03-09 02:13:17 +0100263{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000264 snd_soc_unregister_codec(&spi->dev);
Daniel Macka3819342009-03-09 02:13:17 +0100265 return 0;
266}
267
Daniel Mackac5dbea2012-10-07 17:51:24 +0200268static const struct of_device_id ak4104_of_match[] = {
269 { .compatible = "asahi-kasei,ak4104", },
270 { }
271};
272MODULE_DEVICE_TABLE(of, ak4104_of_match);
273
Daniel Macka3819342009-03-09 02:13:17 +0100274static struct spi_driver ak4104_spi_driver = {
275 .driver = {
276 .name = DRV_NAME,
277 .owner = THIS_MODULE,
Daniel Mackac5dbea2012-10-07 17:51:24 +0200278 .of_match_table = ak4104_of_match,
Daniel Macka3819342009-03-09 02:13:17 +0100279 },
280 .probe = ak4104_spi_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500281 .remove = ak4104_spi_remove,
Daniel Macka3819342009-03-09 02:13:17 +0100282};
283
Mark Brown38d78ba2012-02-16 22:50:35 -0800284module_spi_driver(ak4104_spi_driver);
Daniel Macka3819342009-03-09 02:13:17 +0100285
286MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
287MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
288MODULE_LICENSE("GPL");
289