xref: /openbmc/linux/sound/soc/codecs/rt5514-spi.c (revision 8730046c)
1 /*
2  * rt5514-spi.c  --  RT5514 SPI driver
3  *
4  * Copyright 2015 Realtek Semiconductor Corp.
5  * Author: Oder Chiou <oder_chiou@realtek.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/input.h>
14 #include <linux/spi/spi.h>
15 #include <linux/device.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/slab.h>
21 #include <linux/gpio.h>
22 #include <linux/sched.h>
23 #include <linux/uaccess.h>
24 #include <linux/miscdevice.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/pm_qos.h>
27 #include <linux/sysfs.h>
28 #include <linux/clk.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/soc.h>
33 #include <sound/soc-dapm.h>
34 #include <sound/initval.h>
35 #include <sound/tlv.h>
36 
37 #include "rt5514-spi.h"
38 
39 static struct spi_device *rt5514_spi;
40 
41 struct rt5514_dsp {
42 	struct device *dev;
43 	struct delayed_work copy_work;
44 	struct mutex dma_lock;
45 	struct snd_pcm_substream *substream;
46 	unsigned int buf_base, buf_limit, buf_rp;
47 	size_t buf_size;
48 	size_t dma_offset;
49 	size_t dsp_offset;
50 };
51 
52 static const struct snd_pcm_hardware rt5514_spi_pcm_hardware = {
53 	.info			= SNDRV_PCM_INFO_MMAP |
54 				  SNDRV_PCM_INFO_MMAP_VALID |
55 				  SNDRV_PCM_INFO_INTERLEAVED,
56 	.formats		= SNDRV_PCM_FMTBIT_S16_LE,
57 	.period_bytes_min	= PAGE_SIZE,
58 	.period_bytes_max	= 0x20000 / 8,
59 	.periods_min		= 8,
60 	.periods_max		= 8,
61 	.channels_min		= 1,
62 	.channels_max		= 1,
63 	.buffer_bytes_max	= 0x20000,
64 };
65 
66 static struct snd_soc_dai_driver rt5514_spi_dai = {
67 	.name = "rt5514-dsp-cpu-dai",
68 	.id = 0,
69 	.capture = {
70 		.stream_name = "DSP Capture",
71 		.channels_min = 1,
72 		.channels_max = 1,
73 		.rates = SNDRV_PCM_RATE_16000,
74 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
75 	},
76 };
77 
78 static void rt5514_spi_copy_work(struct work_struct *work)
79 {
80 	struct rt5514_dsp *rt5514_dsp =
81 		container_of(work, struct rt5514_dsp, copy_work.work);
82 	struct snd_pcm_runtime *runtime;
83 	size_t period_bytes, truncated_bytes = 0;
84 
85 	mutex_lock(&rt5514_dsp->dma_lock);
86 	if (!rt5514_dsp->substream) {
87 		dev_err(rt5514_dsp->dev, "No pcm substream\n");
88 		goto done;
89 	}
90 
91 	runtime = rt5514_dsp->substream->runtime;
92 	period_bytes = snd_pcm_lib_period_bytes(rt5514_dsp->substream);
93 
94 	if (rt5514_dsp->buf_size - rt5514_dsp->dsp_offset <  period_bytes)
95 		period_bytes = rt5514_dsp->buf_size - rt5514_dsp->dsp_offset;
96 
97 	if (rt5514_dsp->buf_rp + period_bytes <= rt5514_dsp->buf_limit) {
98 		rt5514_spi_burst_read(rt5514_dsp->buf_rp,
99 			runtime->dma_area + rt5514_dsp->dma_offset,
100 			period_bytes);
101 
102 		if (rt5514_dsp->buf_rp + period_bytes == rt5514_dsp->buf_limit)
103 			rt5514_dsp->buf_rp = rt5514_dsp->buf_base;
104 		else
105 			rt5514_dsp->buf_rp += period_bytes;
106 	} else {
107 		truncated_bytes = rt5514_dsp->buf_limit - rt5514_dsp->buf_rp;
108 		rt5514_spi_burst_read(rt5514_dsp->buf_rp,
109 			runtime->dma_area + rt5514_dsp->dma_offset,
110 			truncated_bytes);
111 
112 		rt5514_spi_burst_read(rt5514_dsp->buf_base,
113 			runtime->dma_area + rt5514_dsp->dma_offset +
114 			truncated_bytes, period_bytes - truncated_bytes);
115 
116 			rt5514_dsp->buf_rp = rt5514_dsp->buf_base +
117 				period_bytes - truncated_bytes;
118 	}
119 
120 	rt5514_dsp->dma_offset += period_bytes;
121 	if (rt5514_dsp->dma_offset >= runtime->dma_bytes)
122 		rt5514_dsp->dma_offset = 0;
123 
124 	rt5514_dsp->dsp_offset += period_bytes;
125 
126 	snd_pcm_period_elapsed(rt5514_dsp->substream);
127 
128 	if (rt5514_dsp->dsp_offset < rt5514_dsp->buf_size)
129 		schedule_delayed_work(&rt5514_dsp->copy_work, 5);
130 done:
131 	mutex_unlock(&rt5514_dsp->dma_lock);
132 }
133 
134 /* PCM for streaming audio from the DSP buffer */
135 static int rt5514_spi_pcm_open(struct snd_pcm_substream *substream)
136 {
137 	snd_soc_set_runtime_hwparams(substream, &rt5514_spi_pcm_hardware);
138 
139 	return 0;
140 }
141 
142 static int rt5514_spi_hw_params(struct snd_pcm_substream *substream,
143 			       struct snd_pcm_hw_params *hw_params)
144 {
145 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
146 	struct rt5514_dsp *rt5514_dsp =
147 			snd_soc_platform_get_drvdata(rtd->platform);
148 	int ret;
149 
150 	mutex_lock(&rt5514_dsp->dma_lock);
151 	ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
152 			params_buffer_bytes(hw_params));
153 	rt5514_dsp->substream = substream;
154 	mutex_unlock(&rt5514_dsp->dma_lock);
155 
156 	return ret;
157 }
158 
159 static int rt5514_spi_hw_free(struct snd_pcm_substream *substream)
160 {
161 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
162 	struct rt5514_dsp *rt5514_dsp =
163 			snd_soc_platform_get_drvdata(rtd->platform);
164 
165 	mutex_lock(&rt5514_dsp->dma_lock);
166 	rt5514_dsp->substream = NULL;
167 	mutex_unlock(&rt5514_dsp->dma_lock);
168 
169 	cancel_delayed_work_sync(&rt5514_dsp->copy_work);
170 
171 	return snd_pcm_lib_free_vmalloc_buffer(substream);
172 }
173 
174 static int rt5514_spi_prepare(struct snd_pcm_substream *substream)
175 {
176 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
177 	struct rt5514_dsp *rt5514_dsp =
178 			snd_soc_platform_get_drvdata(rtd->platform);
179 	u8 buf[8];
180 
181 	rt5514_dsp->dma_offset = 0;
182 	rt5514_dsp->dsp_offset = 0;
183 
184 	/**
185 	 * The address area x1800XXXX is the register address, and it cannot
186 	 * support spi burst read perfectly. So we use the spi burst read
187 	 * individually to make sure the data correctly.
188 	*/
189 	rt5514_spi_burst_read(RT5514_BUFFER_VOICE_BASE, (u8 *)&buf,
190 		sizeof(buf));
191 	rt5514_dsp->buf_base = buf[0] | buf[1] << 8 | buf[2] << 16 |
192 				buf[3] << 24;
193 
194 	rt5514_spi_burst_read(RT5514_BUFFER_VOICE_LIMIT, (u8 *)&buf,
195 		sizeof(buf));
196 	rt5514_dsp->buf_limit = buf[0] | buf[1] << 8 | buf[2] << 16 |
197 				buf[3] << 24;
198 
199 	rt5514_spi_burst_read(RT5514_BUFFER_VOICE_RP, (u8 *)&buf,
200 		sizeof(buf));
201 	rt5514_dsp->buf_rp = buf[0] | buf[1] << 8 | buf[2] << 16 |
202 				buf[3] << 24;
203 
204 	rt5514_spi_burst_read(RT5514_BUFFER_VOICE_SIZE, (u8 *)&buf,
205 		sizeof(buf));
206 	rt5514_dsp->buf_size = buf[0] | buf[1] << 8 | buf[2] << 16 |
207 				buf[3] << 24;
208 
209 	return 0;
210 }
211 
212 static int rt5514_spi_trigger(struct snd_pcm_substream *substream, int cmd)
213 {
214 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
215 	struct rt5514_dsp *rt5514_dsp =
216 			snd_soc_platform_get_drvdata(rtd->platform);
217 
218 	if (cmd == SNDRV_PCM_TRIGGER_START) {
219 		if (rt5514_dsp->buf_base && rt5514_dsp->buf_limit &&
220 			rt5514_dsp->buf_rp && rt5514_dsp->buf_size)
221 			schedule_delayed_work(&rt5514_dsp->copy_work, 0);
222 	}
223 
224 	return 0;
225 }
226 
227 static snd_pcm_uframes_t rt5514_spi_pcm_pointer(
228 		struct snd_pcm_substream *substream)
229 {
230 	struct snd_pcm_runtime *runtime = substream->runtime;
231 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
232 	struct rt5514_dsp *rt5514_dsp =
233 		snd_soc_platform_get_drvdata(rtd->platform);
234 
235 	return bytes_to_frames(runtime, rt5514_dsp->dma_offset);
236 }
237 
238 static const struct snd_pcm_ops rt5514_spi_pcm_ops = {
239 	.open		= rt5514_spi_pcm_open,
240 	.hw_params	= rt5514_spi_hw_params,
241 	.hw_free	= rt5514_spi_hw_free,
242 	.trigger	= rt5514_spi_trigger,
243 	.prepare	= rt5514_spi_prepare,
244 	.pointer	= rt5514_spi_pcm_pointer,
245 	.mmap		= snd_pcm_lib_mmap_vmalloc,
246 	.page		= snd_pcm_lib_get_vmalloc_page,
247 };
248 
249 static int rt5514_spi_pcm_probe(struct snd_soc_platform *platform)
250 {
251 	struct rt5514_dsp *rt5514_dsp;
252 
253 	rt5514_dsp = devm_kzalloc(platform->dev, sizeof(*rt5514_dsp),
254 			GFP_KERNEL);
255 
256 	rt5514_dsp->dev = &rt5514_spi->dev;
257 	mutex_init(&rt5514_dsp->dma_lock);
258 	INIT_DELAYED_WORK(&rt5514_dsp->copy_work, rt5514_spi_copy_work);
259 	snd_soc_platform_set_drvdata(platform, rt5514_dsp);
260 
261 	return 0;
262 }
263 
264 static struct snd_soc_platform_driver rt5514_spi_platform = {
265 	.probe = rt5514_spi_pcm_probe,
266 	.ops = &rt5514_spi_pcm_ops,
267 };
268 
269 static const struct snd_soc_component_driver rt5514_spi_dai_component = {
270 	.name		= "rt5514-spi-dai",
271 };
272 
273 /**
274  * rt5514_spi_burst_read - Read data from SPI by rt5514 address.
275  * @addr: Start address.
276  * @rxbuf: Data Buffer for reading.
277  * @len: Data length, it must be a multiple of 8.
278  *
279  *
280  * Returns true for success.
281  */
282 int rt5514_spi_burst_read(unsigned int addr, u8 *rxbuf, size_t len)
283 {
284 	u8 spi_cmd = RT5514_SPI_CMD_BURST_READ;
285 	int status;
286 	u8 write_buf[8];
287 	unsigned int i, end, offset = 0;
288 
289 	struct spi_message message;
290 	struct spi_transfer x[3];
291 
292 	while (offset < len) {
293 		if (offset + RT5514_SPI_BUF_LEN <= len)
294 			end = RT5514_SPI_BUF_LEN;
295 		else
296 			end = len % RT5514_SPI_BUF_LEN;
297 
298 		write_buf[0] = spi_cmd;
299 		write_buf[1] = ((addr + offset) & 0xff000000) >> 24;
300 		write_buf[2] = ((addr + offset) & 0x00ff0000) >> 16;
301 		write_buf[3] = ((addr + offset) & 0x0000ff00) >> 8;
302 		write_buf[4] = ((addr + offset) & 0x000000ff) >> 0;
303 
304 		spi_message_init(&message);
305 		memset(x, 0, sizeof(x));
306 
307 		x[0].len = 5;
308 		x[0].tx_buf = write_buf;
309 		spi_message_add_tail(&x[0], &message);
310 
311 		x[1].len = 4;
312 		x[1].tx_buf = write_buf;
313 		spi_message_add_tail(&x[1], &message);
314 
315 		x[2].len = end;
316 		x[2].rx_buf = rxbuf + offset;
317 		spi_message_add_tail(&x[2], &message);
318 
319 		status = spi_sync(rt5514_spi, &message);
320 
321 		if (status)
322 			return false;
323 
324 		offset += RT5514_SPI_BUF_LEN;
325 	}
326 
327 	for (i = 0; i < len; i += 8) {
328 		write_buf[0] = rxbuf[i + 0];
329 		write_buf[1] = rxbuf[i + 1];
330 		write_buf[2] = rxbuf[i + 2];
331 		write_buf[3] = rxbuf[i + 3];
332 		write_buf[4] = rxbuf[i + 4];
333 		write_buf[5] = rxbuf[i + 5];
334 		write_buf[6] = rxbuf[i + 6];
335 		write_buf[7] = rxbuf[i + 7];
336 
337 		rxbuf[i + 0] = write_buf[7];
338 		rxbuf[i + 1] = write_buf[6];
339 		rxbuf[i + 2] = write_buf[5];
340 		rxbuf[i + 3] = write_buf[4];
341 		rxbuf[i + 4] = write_buf[3];
342 		rxbuf[i + 5] = write_buf[2];
343 		rxbuf[i + 6] = write_buf[1];
344 		rxbuf[i + 7] = write_buf[0];
345 	}
346 
347 	return true;
348 }
349 
350 /**
351  * rt5514_spi_burst_write - Write data to SPI by rt5514 address.
352  * @addr: Start address.
353  * @txbuf: Data Buffer for writng.
354  * @len: Data length, it must be a multiple of 8.
355  *
356  *
357  * Returns true for success.
358  */
359 int rt5514_spi_burst_write(u32 addr, const u8 *txbuf, size_t len)
360 {
361 	u8 spi_cmd = RT5514_SPI_CMD_BURST_WRITE;
362 	u8 *write_buf;
363 	unsigned int i, end, offset = 0;
364 
365 	write_buf = kmalloc(RT5514_SPI_BUF_LEN + 6, GFP_KERNEL);
366 
367 	if (write_buf == NULL)
368 		return -ENOMEM;
369 
370 	while (offset < len) {
371 		if (offset + RT5514_SPI_BUF_LEN <= len)
372 			end = RT5514_SPI_BUF_LEN;
373 		else
374 			end = len % RT5514_SPI_BUF_LEN;
375 
376 		write_buf[0] = spi_cmd;
377 		write_buf[1] = ((addr + offset) & 0xff000000) >> 24;
378 		write_buf[2] = ((addr + offset) & 0x00ff0000) >> 16;
379 		write_buf[3] = ((addr + offset) & 0x0000ff00) >> 8;
380 		write_buf[4] = ((addr + offset) & 0x000000ff) >> 0;
381 
382 		for (i = 0; i < end; i += 8) {
383 			write_buf[i + 12] = txbuf[offset + i + 0];
384 			write_buf[i + 11] = txbuf[offset + i + 1];
385 			write_buf[i + 10] = txbuf[offset + i + 2];
386 			write_buf[i +  9] = txbuf[offset + i + 3];
387 			write_buf[i +  8] = txbuf[offset + i + 4];
388 			write_buf[i +  7] = txbuf[offset + i + 5];
389 			write_buf[i +  6] = txbuf[offset + i + 6];
390 			write_buf[i +  5] = txbuf[offset + i + 7];
391 		}
392 
393 		write_buf[end + 5] = spi_cmd;
394 
395 		spi_write(rt5514_spi, write_buf, end + 6);
396 
397 		offset += RT5514_SPI_BUF_LEN;
398 	}
399 
400 	kfree(write_buf);
401 
402 	return 0;
403 }
404 EXPORT_SYMBOL_GPL(rt5514_spi_burst_write);
405 
406 static int rt5514_spi_probe(struct spi_device *spi)
407 {
408 	int ret;
409 
410 	rt5514_spi = spi;
411 
412 	ret = devm_snd_soc_register_platform(&spi->dev, &rt5514_spi_platform);
413 	if (ret < 0) {
414 		dev_err(&spi->dev, "Failed to register platform.\n");
415 		return ret;
416 	}
417 
418 	ret = devm_snd_soc_register_component(&spi->dev,
419 					      &rt5514_spi_dai_component,
420 					      &rt5514_spi_dai, 1);
421 	if (ret < 0) {
422 		dev_err(&spi->dev, "Failed to register component.\n");
423 		return ret;
424 	}
425 
426 	return 0;
427 }
428 
429 static const struct of_device_id rt5514_of_match[] = {
430 	{ .compatible = "realtek,rt5514", },
431 	{},
432 };
433 MODULE_DEVICE_TABLE(of, rt5514_of_match);
434 
435 static struct spi_driver rt5514_spi_driver = {
436 	.driver = {
437 		.name = "rt5514",
438 		.of_match_table = of_match_ptr(rt5514_of_match),
439 	},
440 	.probe = rt5514_spi_probe,
441 };
442 module_spi_driver(rt5514_spi_driver);
443 
444 MODULE_DESCRIPTION("RT5514 SPI driver");
445 MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
446 MODULE_LICENSE("GPL v2");
447