xref: /openbmc/linux/sound/soc/codecs/ak4104.c (revision b0ec761b)
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>
13 #include <linux/slab.h>
14 #include <sound/core.h>
15 #include <sound/soc.h>
16 #include <sound/initval.h>
17 #include <linux/spi/spi.h>
18 #include <linux/of_device.h>
19 #include <linux/of_gpio.h>
20 #include <sound/asoundef.h>
21 
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 
48 #define DRV_NAME "ak4104-codec"
49 
50 struct ak4104_private {
51 	struct regmap *regmap;
52 };
53 
54 static 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;
58 	struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
59 	int val = 0;
60 	int ret;
61 
62 	/* 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 
81 	ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
82 				 AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1,
83 				 val);
84 	if (ret < 0)
85 		return ret;
86 
87 	return 0;
88 }
89 
90 static int ak4104_hw_params(struct snd_pcm_substream *substream,
91 			    struct snd_pcm_hw_params *params,
92 			    struct snd_soc_dai *dai)
93 {
94 	struct snd_soc_codec *codec = dai->codec;
95 	struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
96 	int val = 0;
97 
98 	/* set the IEC958 bits: consumer mode, no copyright bit */
99 	val |= IEC958_AES0_CON_NOT_COPYRIGHT;
100 	regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(0), val);
101 
102 	val = 0;
103 
104 	switch (params_rate(params)) {
105 	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;
114 	case 44100:
115 		val |= IEC958_AES3_CON_FS_44100;
116 		break;
117 	case 48000:
118 		val |= IEC958_AES3_CON_FS_48000;
119 		break;
120 	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;
131 		break;
132 	default:
133 		dev_err(codec->dev, "unsupported sampling rate\n");
134 		return -EINVAL;
135 	}
136 
137 	return regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(3), val);
138 }
139 
140 static const struct snd_soc_dai_ops ak4101_dai_ops = {
141 	.hw_params = ak4104_hw_params,
142 	.set_fmt = ak4104_set_dai_fmt,
143 };
144 
145 static struct snd_soc_dai_driver ak4104_dai = {
146 	.name = "ak4104-hifi",
147 	.playback = {
148 		.stream_name = "Playback",
149 		.channels_min = 2,
150 		.channels_max = 2,
151 		.rates = SNDRV_PCM_RATE_8000_192000,
152 		.formats = SNDRV_PCM_FMTBIT_S16_LE  |
153 			   SNDRV_PCM_FMTBIT_S24_3LE |
154 			   SNDRV_PCM_FMTBIT_S24_LE
155 	},
156 	.ops = &ak4101_dai_ops,
157 };
158 
159 static int ak4104_probe(struct snd_soc_codec *codec)
160 {
161 	struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
162 	int ret;
163 
164 	codec->control_data = ak4104->regmap;
165 
166 	/* set power-up and non-reset bits */
167 	ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
168 				 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN,
169 				 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
170 	if (ret < 0)
171 		return ret;
172 
173 	/* enable transmitter */
174 	ret = regmap_update_bits(ak4104->regmap, AK4104_REG_TX,
175 				 AK4104_TX_TXE, AK4104_TX_TXE);
176 	if (ret < 0)
177 		return ret;
178 
179 	return 0;
180 }
181 
182 static int ak4104_remove(struct snd_soc_codec *codec)
183 {
184 	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);
188 
189 	return 0;
190 }
191 
192 static struct snd_soc_codec_driver soc_codec_device_ak4104 = {
193 	.probe =	ak4104_probe,
194 	.remove =	ak4104_remove,
195 };
196 
197 static 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,
206 };
207 
208 static int ak4104_spi_probe(struct spi_device *spi)
209 {
210 	struct device_node *np = spi->dev.of_node;
211 	struct ak4104_private *ak4104;
212 	unsigned int val;
213 	int ret;
214 
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 
221 	ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private),
222 			      GFP_KERNEL);
223 	if (ak4104 == NULL)
224 		return -ENOMEM;
225 
226 	ak4104->regmap = devm_regmap_init_spi(spi, &ak4104_regmap);
227 	if (IS_ERR(ak4104->regmap)) {
228 		ret = PTR_ERR(ak4104->regmap);
229 		return ret;
230 	}
231 
232 	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 
246 	/* 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)
251 		return ret;
252 	if (val != AK4104_RESERVED_VAL)
253 		return -ENODEV;
254 
255 	spi_set_drvdata(spi, ak4104);
256 
257 	ret = snd_soc_register_codec(&spi->dev,
258 			&soc_codec_device_ak4104, &ak4104_dai, 1);
259 	return ret;
260 }
261 
262 static int ak4104_spi_remove(struct spi_device *spi)
263 {
264 	snd_soc_unregister_codec(&spi->dev);
265 	return 0;
266 }
267 
268 static const struct of_device_id ak4104_of_match[] = {
269 	{ .compatible = "asahi-kasei,ak4104", },
270 	{ }
271 };
272 MODULE_DEVICE_TABLE(of, ak4104_of_match);
273 
274 static struct spi_driver ak4104_spi_driver = {
275 	.driver  = {
276 		.name   = DRV_NAME,
277 		.owner  = THIS_MODULE,
278 		.of_match_table = ak4104_of_match,
279 	},
280 	.probe  = ak4104_spi_probe,
281 	.remove = ak4104_spi_remove,
282 };
283 
284 module_spi_driver(ak4104_spi_driver);
285 
286 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
287 MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
288 MODULE_LICENSE("GPL");
289 
290