blob: 3be2be60576df8bc336f7a8130cdce27404d92a6 [file] [log] [blame]
Cliff Caif2028622008-09-05 18:21:37 +08001/*
2 * bf5xx-ac97.c -- AC97 support for the ADI blackfin chip.
3 *
4 * Author: Roy Huang
5 * Created: 11th. June 2007
6 * Copyright: Analog Device Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/interrupt.h>
17#include <linux/wait.h>
18#include <linux/delay.h>
19
20#include <sound/core.h>
21#include <sound/pcm.h>
22#include <sound/ac97_codec.h>
23#include <sound/initval.h>
24#include <sound/soc.h>
25
26#include <asm/irq.h>
27#include <asm/portmux.h>
28#include <linux/mutex.h>
29#include <linux/gpio.h>
30
31#include "bf5xx-sport.h"
32#include "bf5xx-ac97.h"
33
34#if defined(CONFIG_BF54x)
35#define PIN_REQ_SPORT_0 {P_SPORT0_TFS, P_SPORT0_DTPRI, P_SPORT0_TSCLK, \
36 P_SPORT0_RFS, P_SPORT0_DRPRI, P_SPORT0_RSCLK, 0}
37
38#define PIN_REQ_SPORT_1 {P_SPORT1_TFS, P_SPORT1_DTPRI, P_SPORT1_TSCLK, \
39 P_SPORT1_RFS, P_SPORT1_DRPRI, P_SPORT1_RSCLK, 0}
40
41#define PIN_REQ_SPORT_2 {P_SPORT2_TFS, P_SPORT2_DTPRI, P_SPORT2_TSCLK, \
42 P_SPORT2_RFS, P_SPORT2_DRPRI, P_SPORT2_RSCLK, 0}
43
44#define PIN_REQ_SPORT_3 {P_SPORT3_TFS, P_SPORT3_DTPRI, P_SPORT3_TSCLK, \
45 P_SPORT3_RFS, P_SPORT3_DRPRI, P_SPORT3_RSCLK, 0}
46#else
47#define PIN_REQ_SPORT_0 {P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS, \
48 P_SPORT0_DRPRI, P_SPORT0_RSCLK, 0}
49
50#define PIN_REQ_SPORT_1 {P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS, \
51 P_SPORT1_DRPRI, P_SPORT1_RSCLK, 0}
52#endif
53
54static int *cmd_count;
55static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM;
56
Cliff Cai67f854b2008-11-18 16:18:17 +080057static u16 sport_req[][7] = {
58 PIN_REQ_SPORT_0,
59#ifdef PIN_REQ_SPORT_1
60 PIN_REQ_SPORT_1,
61#endif
62#ifdef PIN_REQ_SPORT_2
63 PIN_REQ_SPORT_2,
64#endif
65#ifdef PIN_REQ_SPORT_3
66 PIN_REQ_SPORT_3,
67#endif
68 };
69
Cliff Caif2028622008-09-05 18:21:37 +080070static struct sport_param sport_params[4] = {
71 {
72 .dma_rx_chan = CH_SPORT0_RX,
73 .dma_tx_chan = CH_SPORT0_TX,
Cliff Caif2028622008-09-05 18:21:37 +080074 .err_irq = IRQ_SPORT0_ERROR,
75 .regs = (struct sport_register *)SPORT0_TCR1,
76 },
Cliff Cai67f854b2008-11-18 16:18:17 +080077#ifdef PIN_REQ_SPORT_1
Cliff Caif2028622008-09-05 18:21:37 +080078 {
79 .dma_rx_chan = CH_SPORT1_RX,
80 .dma_tx_chan = CH_SPORT1_TX,
81 .err_irq = IRQ_SPORT1_ERROR,
82 .regs = (struct sport_register *)SPORT1_TCR1,
Cliff Cai67f854b2008-11-18 16:18:17 +080083 },
Cliff Caif2028622008-09-05 18:21:37 +080084#endif
Cliff Cai67f854b2008-11-18 16:18:17 +080085#ifdef PIN_REQ_SPORT_2
86 {
87 .dma_rx_chan = CH_SPORT2_RX,
88 .dma_tx_chan = CH_SPORT2_TX,
89 .err_irq = IRQ_SPORT2_ERROR,
90 .regs = (struct sport_register *)SPORT2_TCR1,
91 },
92#endif
93#ifdef PIN_REQ_SPORT_3
94 {
95 .dma_rx_chan = CH_SPORT3_RX,
96 .dma_tx_chan = CH_SPORT3_TX,
97 .err_irq = IRQ_SPORT3_ERROR,
98 .regs = (struct sport_register *)SPORT3_TCR1,
99 }
100#endif
101};
Cliff Caif2028622008-09-05 18:21:37 +0800102
Cliff Cai67f854b2008-11-18 16:18:17 +0800103void bf5xx_pcm_to_ac97(struct ac97_frame *dst, const __u16 *src,
104 size_t count, unsigned int chan_mask)
Cliff Caif2028622008-09-05 18:21:37 +0800105{
106 while (count--) {
Cliff Cai67f854b2008-11-18 16:18:17 +0800107 dst->ac97_tag = TAG_VALID;
108 if (chan_mask & SP_FL) {
109 dst->ac97_pcm_r = *src++;
110 dst->ac97_tag |= TAG_PCM_RIGHT;
111 }
112 if (chan_mask & SP_FR) {
113 dst->ac97_pcm_l = *src++;
114 dst->ac97_tag |= TAG_PCM_LEFT;
115
116 }
117#if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
118 if (chan_mask & SP_SR) {
119 dst->ac97_sl = *src++;
120 dst->ac97_tag |= TAG_PCM_SL;
121 }
122 if (chan_mask & SP_SL) {
123 dst->ac97_sr = *src++;
124 dst->ac97_tag |= TAG_PCM_SR;
125 }
126 if (chan_mask & SP_LFE) {
127 dst->ac97_lfe = *src++;
128 dst->ac97_tag |= TAG_PCM_LFE;
129 }
130 if (chan_mask & SP_FC) {
131 dst->ac97_center = *src++;
132 dst->ac97_tag |= TAG_PCM_CENTER;
133 }
134#endif
135 dst++;
Cliff Caif2028622008-09-05 18:21:37 +0800136 }
137}
138EXPORT_SYMBOL(bf5xx_pcm_to_ac97);
139
Cliff Cai67f854b2008-11-18 16:18:17 +0800140void bf5xx_ac97_to_pcm(const struct ac97_frame *src, __u16 *dst,
Cliff Caif2028622008-09-05 18:21:37 +0800141 size_t count)
142{
Cliff Cai67f854b2008-11-18 16:18:17 +0800143 while (count--) {
144 *(dst++) = src->ac97_pcm_l;
145 *(dst++) = src->ac97_pcm_r;
146 src++;
147 }
Cliff Caif2028622008-09-05 18:21:37 +0800148}
149EXPORT_SYMBOL(bf5xx_ac97_to_pcm);
150
151static unsigned int sport_tx_curr_frag(struct sport_device *sport)
152{
Cliff Cai67f854b2008-11-18 16:18:17 +0800153 return sport->tx_curr_frag = sport_curr_offset_tx(sport) /
Cliff Caif2028622008-09-05 18:21:37 +0800154 sport->tx_fragsize;
155}
156
157static void enqueue_cmd(struct snd_ac97 *ac97, __u16 addr, __u16 data)
158{
159 struct sport_device *sport = sport_handle;
160 int nextfrag = sport_tx_curr_frag(sport);
161 struct ac97_frame *nextwrite;
162
163 sport_incfrag(sport, &nextfrag, 1);
Cliff Caif2028622008-09-05 18:21:37 +0800164
Cliff Cai67f854b2008-11-18 16:18:17 +0800165 nextwrite = (struct ac97_frame *)(sport->tx_buf +
Cliff Caif2028622008-09-05 18:21:37 +0800166 nextfrag * sport->tx_fragsize);
167 pr_debug("sport->tx_buf:%p, nextfrag:0x%x nextwrite:%p, cmd_count:%d\n",
168 sport->tx_buf, nextfrag, nextwrite, cmd_count[nextfrag]);
169 nextwrite[cmd_count[nextfrag]].ac97_tag |= TAG_CMD;
170 nextwrite[cmd_count[nextfrag]].ac97_addr = addr;
171 nextwrite[cmd_count[nextfrag]].ac97_data = data;
172 ++cmd_count[nextfrag];
173 pr_debug("ac97_sport: Inserting %02x/%04x into fragment %d\n",
174 addr >> 8, data, nextfrag);
175}
176
177static unsigned short bf5xx_ac97_read(struct snd_ac97 *ac97,
178 unsigned short reg)
179{
180 struct ac97_frame out_frame[2], in_frame[2];
181
182 pr_debug("%s enter 0x%x\n", __func__, reg);
183
184 /* When dma descriptor is enabled, the register should not be read */
185 if (sport_handle->tx_run || sport_handle->rx_run) {
186 pr_err("Could you send a mail to cliff.cai@analog.com "
187 "to report this?\n");
188 return -EFAULT;
189 }
190
191 memset(&out_frame, 0, 2 * sizeof(struct ac97_frame));
192 memset(&in_frame, 0, 2 * sizeof(struct ac97_frame));
193 out_frame[0].ac97_tag = TAG_VALID | TAG_CMD;
194 out_frame[0].ac97_addr = ((reg << 8) | 0x8000);
195 sport_send_and_recv(sport_handle, (unsigned char *)&out_frame,
196 (unsigned char *)&in_frame,
197 2 * sizeof(struct ac97_frame));
198 return in_frame[1].ac97_data;
199}
200
201void bf5xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
202 unsigned short val)
203{
204 pr_debug("%s enter 0x%x:0x%04x\n", __func__, reg, val);
205
206 if (sport_handle->tx_run) {
207 enqueue_cmd(ac97, (reg << 8), val); /* write */
208 enqueue_cmd(ac97, (reg << 8) | 0x8000, 0); /* read back */
209 } else {
210 struct ac97_frame frame;
211 memset(&frame, 0, sizeof(struct ac97_frame));
212 frame.ac97_tag = TAG_VALID | TAG_CMD;
213 frame.ac97_addr = (reg << 8);
214 frame.ac97_data = val;
215 sport_send_and_recv(sport_handle, (unsigned char *)&frame, \
216 NULL, sizeof(struct ac97_frame));
217 }
218}
219
220static void bf5xx_ac97_warm_reset(struct snd_ac97 *ac97)
221{
222#if defined(CONFIG_BF54x) || defined(CONFIG_BF561) || \
223 (defined(BF537_FAMILY) && (CONFIG_SND_BF5XX_SPORT_NUM == 1))
224
225#define CONCAT(a, b, c) a ## b ## c
226#define BFIN_SPORT_RFS(x) CONCAT(P_SPORT, x, _RFS)
227
228 u16 per = BFIN_SPORT_RFS(CONFIG_SND_BF5XX_SPORT_NUM);
229 u16 gpio = P_IDENT(BFIN_SPORT_RFS(CONFIG_SND_BF5XX_SPORT_NUM));
230
231 pr_debug("%s enter\n", __func__);
232
233 peripheral_free(per);
234 gpio_request(gpio, "bf5xx-ac97");
235 gpio_direction_output(gpio, 1);
236 udelay(2);
237 gpio_set_value(gpio, 0);
238 udelay(1);
239 gpio_free(gpio);
240 peripheral_request(per, "soc-audio");
241#else
242 pr_info("%s: Not implemented\n", __func__);
243#endif
244}
245
246static void bf5xx_ac97_cold_reset(struct snd_ac97 *ac97)
247{
248#ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
249 pr_debug("%s enter\n", __func__);
250
251 /* It is specified for bf548-ezkit */
252 gpio_set_value(CONFIG_SND_BF5XX_RESET_GPIO_NUM, 0);
253 /* Keep reset pin low for 1 ms */
254 mdelay(1);
255 gpio_set_value(CONFIG_SND_BF5XX_RESET_GPIO_NUM, 1);
256 /* Wait for bit clock recover */
257 mdelay(1);
258#else
259 pr_info("%s: Not implemented\n", __func__);
260#endif
261}
262
263struct snd_ac97_bus_ops soc_ac97_ops = {
264 .read = bf5xx_ac97_read,
265 .write = bf5xx_ac97_write,
266 .warm_reset = bf5xx_ac97_warm_reset,
267 .reset = bf5xx_ac97_cold_reset,
268};
269EXPORT_SYMBOL_GPL(soc_ac97_ops);
270
271#ifdef CONFIG_PM
Mark Browndc7d7b82008-12-03 18:21:52 +0000272static int bf5xx_ac97_suspend(struct snd_soc_dai *dai)
Cliff Caif2028622008-09-05 18:21:37 +0800273{
274 struct sport_device *sport =
275 (struct sport_device *)dai->private_data;
276
277 pr_debug("%s : sport %d\n", __func__, dai->id);
278 if (!dai->active)
279 return 0;
280 if (dai->capture.active)
281 sport_rx_stop(sport);
282 if (dai->playback.active)
283 sport_tx_stop(sport);
284 return 0;
285}
286
Mark Browndc7d7b82008-12-03 18:21:52 +0000287static int bf5xx_ac97_resume(struct snd_soc_dai *dai)
Cliff Caif2028622008-09-05 18:21:37 +0800288{
289 int ret;
290 struct sport_device *sport =
291 (struct sport_device *)dai->private_data;
292
293 pr_debug("%s : sport %d\n", __func__, dai->id);
294 if (!dai->active)
295 return 0;
296
297 ret = sport_set_multichannel(sport_handle, 16, 0x1F, 1);
298 if (ret) {
299 pr_err("SPORT is busy!\n");
300 return -EBUSY;
301 }
302
303 ret = sport_config_rx(sport_handle, IRFS, 0xF, 0, (16*16-1));
304 if (ret) {
305 pr_err("SPORT is busy!\n");
306 return -EBUSY;
307 }
308
309 ret = sport_config_tx(sport_handle, ITFS, 0xF, 0, (16*16-1));
310 if (ret) {
311 pr_err("SPORT is busy!\n");
312 return -EBUSY;
313 }
314
315 if (dai->capture.active)
316 sport_rx_start(sport);
317 if (dai->playback.active)
318 sport_tx_start(sport);
319 return 0;
320}
321
322#else
323#define bf5xx_ac97_suspend NULL
324#define bf5xx_ac97_resume NULL
325#endif
326
327static int bf5xx_ac97_probe(struct platform_device *pdev,
328 struct snd_soc_dai *dai)
329{
Cliff Cai67f854b2008-11-18 16:18:17 +0800330 int ret = 0;
Cliff Caif2028622008-09-05 18:21:37 +0800331 cmd_count = (int *)get_zeroed_page(GFP_KERNEL);
332 if (cmd_count == NULL)
333 return -ENOMEM;
334
335 if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) {
336 pr_err("Requesting Peripherals failed\n");
Cliff Cai67f854b2008-11-18 16:18:17 +0800337 ret = -EFAULT;
338 goto peripheral_err;
Cliff Caif2028622008-09-05 18:21:37 +0800339 }
340
341#ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
342 /* Request PB3 as reset pin */
343 if (gpio_request(CONFIG_SND_BF5XX_RESET_GPIO_NUM, "SND_AD198x RESET")) {
344 pr_err("Failed to request GPIO_%d for reset\n",
345 CONFIG_SND_BF5XX_RESET_GPIO_NUM);
Cliff Cai67f854b2008-11-18 16:18:17 +0800346 ret = -1;
347 goto gpio_err;
Cliff Caif2028622008-09-05 18:21:37 +0800348 }
349 gpio_direction_output(CONFIG_SND_BF5XX_RESET_GPIO_NUM, 1);
350#endif
351 sport_handle = sport_init(&sport_params[sport_num], 2, \
352 sizeof(struct ac97_frame), NULL);
353 if (!sport_handle) {
Cliff Cai67f854b2008-11-18 16:18:17 +0800354 ret = -ENODEV;
355 goto sport_err;
Cliff Caif2028622008-09-05 18:21:37 +0800356 }
357 /*SPORT works in TDM mode to simulate AC97 transfers*/
358 ret = sport_set_multichannel(sport_handle, 16, 0x1F, 1);
359 if (ret) {
360 pr_err("SPORT is busy!\n");
Cliff Cai67f854b2008-11-18 16:18:17 +0800361 ret = -EBUSY;
362 goto sport_config_err;
Cliff Caif2028622008-09-05 18:21:37 +0800363 }
364
365 ret = sport_config_rx(sport_handle, IRFS, 0xF, 0, (16*16-1));
366 if (ret) {
367 pr_err("SPORT is busy!\n");
Cliff Cai67f854b2008-11-18 16:18:17 +0800368 ret = -EBUSY;
369 goto sport_config_err;
Cliff Caif2028622008-09-05 18:21:37 +0800370 }
371
372 ret = sport_config_tx(sport_handle, ITFS, 0xF, 0, (16*16-1));
373 if (ret) {
374 pr_err("SPORT is busy!\n");
Cliff Cai67f854b2008-11-18 16:18:17 +0800375 ret = -EBUSY;
376 goto sport_config_err;
Cliff Caif2028622008-09-05 18:21:37 +0800377 }
Cliff Cai67f854b2008-11-18 16:18:17 +0800378
Michael Hennerich0cade262008-11-18 16:18:19 +0800379 return 0;
380
Cliff Cai67f854b2008-11-18 16:18:17 +0800381sport_config_err:
382 kfree(sport_handle);
383sport_err:
384#ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
385 gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM);
386#endif
387gpio_err:
388 peripheral_free_list(&sport_req[sport_num][0]);
389peripheral_err:
390 free_page((unsigned long)cmd_count);
391 cmd_count = NULL;
392
393 return ret;
Cliff Caif2028622008-09-05 18:21:37 +0800394}
395
396static void bf5xx_ac97_remove(struct platform_device *pdev,
397 struct snd_soc_dai *dai)
398{
399 free_page((unsigned long)cmd_count);
400 cmd_count = NULL;
Cliff Cai67f854b2008-11-18 16:18:17 +0800401 peripheral_free_list(&sport_req[sport_num][0]);
Cliff Caif2028622008-09-05 18:21:37 +0800402#ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
403 gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM);
404#endif
405}
406
407struct snd_soc_dai bfin_ac97_dai = {
408 .name = "bf5xx-ac97",
409 .id = 0,
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000410 .ac97_control = 1,
Cliff Caif2028622008-09-05 18:21:37 +0800411 .probe = bf5xx_ac97_probe,
412 .remove = bf5xx_ac97_remove,
413 .suspend = bf5xx_ac97_suspend,
414 .resume = bf5xx_ac97_resume,
415 .playback = {
416 .stream_name = "AC97 Playback",
417 .channels_min = 2,
Cliff Cai67f854b2008-11-18 16:18:17 +0800418#if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
419 .channels_max = 6,
420#else
Cliff Caif2028622008-09-05 18:21:37 +0800421 .channels_max = 2,
Cliff Cai67f854b2008-11-18 16:18:17 +0800422#endif
Cliff Caif2028622008-09-05 18:21:37 +0800423 .rates = SNDRV_PCM_RATE_48000,
424 .formats = SNDRV_PCM_FMTBIT_S16_LE, },
425 .capture = {
426 .stream_name = "AC97 Capture",
427 .channels_min = 2,
428 .channels_max = 2,
429 .rates = SNDRV_PCM_RATE_48000,
430 .formats = SNDRV_PCM_FMTBIT_S16_LE, },
431};
432EXPORT_SYMBOL_GPL(bfin_ac97_dai);
433
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100434static int __init bfin_ac97_init(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000435{
436 return snd_soc_register_dai(&bfin_ac97_dai);
437}
438module_init(bfin_ac97_init);
439
440static void __exit bfin_ac97_exit(void)
441{
442 snd_soc_unregister_dai(&bfin_ac97_dai);
443}
444module_exit(bfin_ac97_exit);
445
Cliff Caif2028622008-09-05 18:21:37 +0800446MODULE_AUTHOR("Roy Huang");
447MODULE_DESCRIPTION("AC97 driver for ADI Blackfin");
448MODULE_LICENSE("GPL");