diff options
Diffstat (limited to 'sound/soc/nuc900/nuc900-ac97.c')
-rw-r--r-- | sound/soc/nuc900/nuc900-ac97.c | 430 |
1 files changed, 430 insertions, 0 deletions
diff --git a/sound/soc/nuc900/nuc900-ac97.c b/sound/soc/nuc900/nuc900-ac97.c new file mode 100644 index 000000000000..caa7c901bc2e --- /dev/null +++ b/sound/soc/nuc900/nuc900-ac97.c | |||
@@ -0,0 +1,430 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2009-2010 Nuvoton technology corporation. | ||
3 | * | ||
4 | * Wan ZongShun <mcuos.com@gmail.com> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation;version 2 of the License. | ||
9 | * | ||
10 | */ | ||
11 | |||
12 | #include <linux/init.h> | ||
13 | #include <linux/module.h> | ||
14 | #include <linux/slab.h> | ||
15 | #include <linux/device.h> | ||
16 | #include <linux/delay.h> | ||
17 | #include <linux/mutex.h> | ||
18 | #include <linux/suspend.h> | ||
19 | #include <sound/core.h> | ||
20 | #include <sound/pcm.h> | ||
21 | #include <sound/initval.h> | ||
22 | #include <sound/soc.h> | ||
23 | #include <linux/device.h> | ||
24 | #include <linux/clk.h> | ||
25 | |||
26 | #include <mach/mfp.h> | ||
27 | |||
28 | #include "nuc900-audio.h" | ||
29 | |||
30 | static DEFINE_MUTEX(ac97_mutex); | ||
31 | struct nuc900_audio *nuc900_ac97_data; | ||
32 | |||
33 | static int nuc900_checkready(void) | ||
34 | { | ||
35 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; | ||
36 | |||
37 | if (!(AUDIO_READ(nuc900_audio->mmio + ACTL_ACIS0) & CODEC_READY)) | ||
38 | return -EPERM; | ||
39 | |||
40 | return 0; | ||
41 | } | ||
42 | |||
43 | /* AC97 controller reads codec register */ | ||
44 | static unsigned short nuc900_ac97_read(struct snd_ac97 *ac97, | ||
45 | unsigned short reg) | ||
46 | { | ||
47 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; | ||
48 | unsigned long timeout = 0x10000, val; | ||
49 | |||
50 | mutex_lock(&ac97_mutex); | ||
51 | |||
52 | val = nuc900_checkready(); | ||
53 | if (!!val) { | ||
54 | dev_err(nuc900_audio->dev, "AC97 codec is not ready\n"); | ||
55 | goto out; | ||
56 | } | ||
57 | |||
58 | /* set the R_WB bit and write register index */ | ||
59 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS1, R_WB | reg); | ||
60 | |||
61 | /* set the valid frame bit and valid slots */ | ||
62 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0); | ||
63 | val |= (VALID_FRAME | SLOT1_VALID); | ||
64 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, val); | ||
65 | |||
66 | udelay(100); | ||
67 | |||
68 | /* polling the AC_R_FINISH */ | ||
69 | while (!(AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON) & AC_R_FINISH) | ||
70 | && timeout--) | ||
71 | mdelay(1); | ||
72 | |||
73 | if (!timeout) { | ||
74 | dev_err(nuc900_audio->dev, "AC97 read register time out !\n"); | ||
75 | val = -EPERM; | ||
76 | goto out; | ||
77 | } | ||
78 | |||
79 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0) ; | ||
80 | val &= ~SLOT1_VALID; | ||
81 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, val); | ||
82 | |||
83 | if (AUDIO_READ(nuc900_audio->mmio + ACTL_ACIS1) >> 2 != reg) { | ||
84 | dev_err(nuc900_audio->dev, | ||
85 | "R_INDEX of REG_ACTL_ACIS1 not match!\n"); | ||
86 | } | ||
87 | |||
88 | udelay(100); | ||
89 | val = (AUDIO_READ(nuc900_audio->mmio + ACTL_ACIS2) & 0xFFFF); | ||
90 | |||
91 | out: | ||
92 | mutex_unlock(&ac97_mutex); | ||
93 | return val; | ||
94 | } | ||
95 | |||
96 | /* AC97 controller writes to codec register */ | ||
97 | static void nuc900_ac97_write(struct snd_ac97 *ac97, unsigned short reg, | ||
98 | unsigned short val) | ||
99 | { | ||
100 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; | ||
101 | unsigned long tmp, timeout = 0x10000; | ||
102 | |||
103 | mutex_lock(&ac97_mutex); | ||
104 | |||
105 | tmp = nuc900_checkready(); | ||
106 | if (!!tmp) | ||
107 | dev_err(nuc900_audio->dev, "AC97 codec is not ready\n"); | ||
108 | |||
109 | /* clear the R_WB bit and write register index */ | ||
110 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS1, reg); | ||
111 | |||
112 | /* write register value */ | ||
113 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS2, val); | ||
114 | |||
115 | /* set the valid frame bit and valid slots */ | ||
116 | tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0); | ||
117 | tmp |= SLOT1_VALID | SLOT2_VALID | VALID_FRAME; | ||
118 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, tmp); | ||
119 | |||
120 | udelay(100); | ||
121 | |||
122 | /* polling the AC_W_FINISH */ | ||
123 | while ((AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON) & AC_W_FINISH) | ||
124 | && timeout--) | ||
125 | mdelay(1); | ||
126 | |||
127 | if (!timeout) | ||
128 | dev_err(nuc900_audio->dev, "AC97 write register time out !\n"); | ||
129 | |||
130 | tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0); | ||
131 | tmp &= ~(SLOT1_VALID | SLOT2_VALID); | ||
132 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, tmp); | ||
133 | |||
134 | mutex_unlock(&ac97_mutex); | ||
135 | |||
136 | } | ||
137 | |||
138 | static void nuc900_ac97_warm_reset(struct snd_ac97 *ac97) | ||
139 | { | ||
140 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; | ||
141 | unsigned long val; | ||
142 | |||
143 | mutex_lock(&ac97_mutex); | ||
144 | |||
145 | /* warm reset AC 97 */ | ||
146 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON); | ||
147 | val |= AC_W_RES; | ||
148 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACCON, val); | ||
149 | |||
150 | udelay(100); | ||
151 | |||
152 | val = nuc900_checkready(); | ||
153 | if (!!val) | ||
154 | dev_err(nuc900_audio->dev, "AC97 codec is not ready\n"); | ||
155 | |||
156 | mutex_unlock(&ac97_mutex); | ||
157 | } | ||
158 | |||
159 | static void nuc900_ac97_cold_reset(struct snd_ac97 *ac97) | ||
160 | { | ||
161 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; | ||
162 | unsigned long val; | ||
163 | |||
164 | mutex_lock(&ac97_mutex); | ||
165 | |||
166 | /* reset Audio Controller */ | ||
167 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET); | ||
168 | val |= ACTL_RESET_BIT; | ||
169 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val); | ||
170 | |||
171 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET); | ||
172 | val &= (~ACTL_RESET_BIT); | ||
173 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val); | ||
174 | |||
175 | /* reset AC-link interface */ | ||
176 | |||
177 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET); | ||
178 | val |= AC_RESET; | ||
179 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val); | ||
180 | |||
181 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET); | ||
182 | val &= ~AC_RESET; | ||
183 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val); | ||
184 | |||
185 | /* cold reset AC 97 */ | ||
186 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON); | ||
187 | val |= AC_C_RES; | ||
188 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACCON, val); | ||
189 | |||
190 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON); | ||
191 | val &= (~AC_C_RES); | ||
192 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACCON, val); | ||
193 | |||
194 | udelay(100); | ||
195 | |||
196 | mutex_unlock(&ac97_mutex); | ||
197 | |||
198 | } | ||
199 | |||
200 | /* AC97 controller operations */ | ||
201 | struct snd_ac97_bus_ops soc_ac97_ops = { | ||
202 | .read = nuc900_ac97_read, | ||
203 | .write = nuc900_ac97_write, | ||
204 | .reset = nuc900_ac97_cold_reset, | ||
205 | .warm_reset = nuc900_ac97_warm_reset, | ||
206 | } | ||
207 | EXPORT_SYMBOL_GPL(soc_ac97_ops); | ||
208 | |||
209 | static int nuc900_ac97_trigger(struct snd_pcm_substream *substream, | ||
210 | int cmd, struct snd_soc_dai *dai) | ||
211 | { | ||
212 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; | ||
213 | int ret; | ||
214 | unsigned long val, tmp; | ||
215 | |||
216 | ret = 0; | ||
217 | |||
218 | switch (cmd) { | ||
219 | case SNDRV_PCM_TRIGGER_START: | ||
220 | case SNDRV_PCM_TRIGGER_RESUME: | ||
221 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET); | ||
222 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | ||
223 | tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0); | ||
224 | tmp |= (SLOT3_VALID | SLOT4_VALID | VALID_FRAME); | ||
225 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, tmp); | ||
226 | |||
227 | tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_PSR); | ||
228 | tmp |= (P_DMA_END_IRQ | P_DMA_MIDDLE_IRQ); | ||
229 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_PSR, tmp); | ||
230 | val |= AC_PLAY; | ||
231 | } else { | ||
232 | tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_RSR); | ||
233 | tmp |= (R_DMA_END_IRQ | R_DMA_MIDDLE_IRQ); | ||
234 | |||
235 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RSR, tmp); | ||
236 | val |= AC_RECORD; | ||
237 | } | ||
238 | |||
239 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val); | ||
240 | |||
241 | break; | ||
242 | case SNDRV_PCM_TRIGGER_STOP: | ||
243 | case SNDRV_PCM_TRIGGER_SUSPEND: | ||
244 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET); | ||
245 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | ||
246 | tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0); | ||
247 | tmp &= ~(SLOT3_VALID | SLOT4_VALID); | ||
248 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, tmp); | ||
249 | |||
250 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_PSR, RESET_PRSR); | ||
251 | val &= ~AC_PLAY; | ||
252 | } else { | ||
253 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RSR, RESET_PRSR); | ||
254 | val &= ~AC_RECORD; | ||
255 | } | ||
256 | |||
257 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val); | ||
258 | |||
259 | break; | ||
260 | default: | ||
261 | ret = -EINVAL; | ||
262 | } | ||
263 | |||
264 | return ret; | ||
265 | } | ||
266 | |||
267 | static int nuc900_ac97_probe(struct platform_device *pdev, | ||
268 | struct snd_soc_dai *dai) | ||
269 | { | ||
270 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; | ||
271 | unsigned long val; | ||
272 | |||
273 | mutex_lock(&ac97_mutex); | ||
274 | |||
275 | /* enable unit clock */ | ||
276 | clk_enable(nuc900_audio->clk); | ||
277 | |||
278 | /* enable audio controller and AC-link interface */ | ||
279 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_CON); | ||
280 | val |= (IIS_AC_PIN_SEL | ACLINK_EN); | ||
281 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_CON, val); | ||
282 | |||
283 | mutex_unlock(&ac97_mutex); | ||
284 | |||
285 | return 0; | ||
286 | } | ||
287 | |||
288 | static void nuc900_ac97_remove(struct platform_device *pdev, | ||
289 | struct snd_soc_dai *dai) | ||
290 | { | ||
291 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; | ||
292 | |||
293 | clk_disable(nuc900_audio->clk); | ||
294 | } | ||
295 | |||
296 | static struct snd_soc_dai_ops nuc900_ac97_dai_ops = { | ||
297 | .trigger = nuc900_ac97_trigger, | ||
298 | }; | ||
299 | |||
300 | struct snd_soc_dai nuc900_ac97_dai = { | ||
301 | .name = "nuc900-ac97", | ||
302 | .probe = nuc900_ac97_probe, | ||
303 | .remove = nuc900_ac97_remove, | ||
304 | .ac97_control = 1, | ||
305 | .playback = { | ||
306 | .rates = SNDRV_PCM_RATE_8000_48000, | ||
307 | .formats = SNDRV_PCM_FMTBIT_S16_LE, | ||
308 | .channels_min = 1, | ||
309 | .channels_max = 2, | ||
310 | }, | ||
311 | .capture = { | ||
312 | .rates = SNDRV_PCM_RATE_8000_48000, | ||
313 | .formats = SNDRV_PCM_FMTBIT_S16_LE, | ||
314 | .channels_min = 1, | ||
315 | .channels_max = 2, | ||
316 | }, | ||
317 | .ops = &nuc900_ac97_dai_ops, | ||
318 | } | ||
319 | EXPORT_SYMBOL_GPL(nuc900_ac97_dai); | ||
320 | |||
321 | static int __devinit nuc900_ac97_drvprobe(struct platform_device *pdev) | ||
322 | { | ||
323 | struct nuc900_audio *nuc900_audio; | ||
324 | int ret; | ||
325 | |||
326 | if (nuc900_ac97_data) | ||
327 | return -EBUSY; | ||
328 | |||
329 | nuc900_audio = kzalloc(sizeof(struct nuc900_audio), GFP_KERNEL); | ||
330 | if (!nuc900_audio) | ||
331 | return -ENOMEM; | ||
332 | |||
333 | spin_lock_init(&nuc900_audio->lock); | ||
334 | |||
335 | nuc900_audio->res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | ||
336 | if (!nuc900_audio->res) { | ||
337 | ret = -ENODEV; | ||
338 | goto out0; | ||
339 | } | ||
340 | |||
341 | if (!request_mem_region(nuc900_audio->res->start, | ||
342 | resource_size(nuc900_audio->res), pdev->name)) { | ||
343 | ret = -EBUSY; | ||
344 | goto out0; | ||
345 | } | ||
346 | |||
347 | nuc900_audio->mmio = ioremap(nuc900_audio->res->start, | ||
348 | resource_size(nuc900_audio->res)); | ||
349 | if (!nuc900_audio->mmio) { | ||
350 | ret = -ENOMEM; | ||
351 | goto out1; | ||
352 | } | ||
353 | |||
354 | nuc900_audio->clk = clk_get(&pdev->dev, NULL); | ||
355 | if (IS_ERR(nuc900_audio->clk)) { | ||
356 | ret = PTR_ERR(nuc900_audio->clk); | ||
357 | goto out2; | ||
358 | } | ||
359 | |||
360 | nuc900_audio->irq_num = platform_get_irq(pdev, 0); | ||
361 | if (!nuc900_audio->irq_num) { | ||
362 | ret = -EBUSY; | ||
363 | goto out2; | ||
364 | } | ||
365 | |||
366 | nuc900_ac97_data = nuc900_audio; | ||
367 | |||
368 | nuc900_audio->dev = nuc900_ac97_dai.dev = &pdev->dev; | ||
369 | |||
370 | ret = snd_soc_register_dai(&nuc900_ac97_dai); | ||
371 | if (ret) | ||
372 | goto out3; | ||
373 | |||
374 | mfp_set_groupg(nuc900_audio->dev); /* enbale ac97 multifunction pin*/ | ||
375 | |||
376 | return 0; | ||
377 | |||
378 | out3: | ||
379 | clk_put(nuc900_audio->clk); | ||
380 | out2: | ||
381 | iounmap(nuc900_audio->mmio); | ||
382 | out1: | ||
383 | release_mem_region(nuc900_audio->res->start, | ||
384 | resource_size(nuc900_audio->res)); | ||
385 | out0: | ||
386 | kfree(nuc900_audio); | ||
387 | return ret; | ||
388 | } | ||
389 | |||
390 | static int __devexit nuc900_ac97_drvremove(struct platform_device *pdev) | ||
391 | { | ||
392 | |||
393 | snd_soc_unregister_dai(&nuc900_ac97_dai); | ||
394 | |||
395 | clk_put(nuc900_ac97_data->clk); | ||
396 | iounmap(nuc900_ac97_data->mmio); | ||
397 | release_mem_region(nuc900_ac97_data->res->start, | ||
398 | resource_size(nuc900_ac97_data->res)); | ||
399 | |||
400 | nuc900_ac97_data = NULL; | ||
401 | |||
402 | return 0; | ||
403 | } | ||
404 | |||
405 | static struct platform_driver nuc900_ac97_driver = { | ||
406 | .driver = { | ||
407 | .name = "nuc900-audio", | ||
408 | .owner = THIS_MODULE, | ||
409 | }, | ||
410 | .probe = nuc900_ac97_drvprobe, | ||
411 | .remove = __devexit_p(nuc900_ac97_drvremove), | ||
412 | }; | ||
413 | |||
414 | static int __init nuc900_ac97_init(void) | ||
415 | { | ||
416 | return platform_driver_register(&nuc900_ac97_driver); | ||
417 | } | ||
418 | |||
419 | static void __exit nuc900_ac97_exit(void) | ||
420 | { | ||
421 | platform_driver_unregister(&nuc900_ac97_driver); | ||
422 | } | ||
423 | |||
424 | module_init(nuc900_ac97_init); | ||
425 | module_exit(nuc900_ac97_exit); | ||
426 | |||
427 | MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>"); | ||
428 | MODULE_DESCRIPTION("NUC900 AC97 SoC driver!"); | ||
429 | MODULE_LICENSE("GPL"); | ||
430 | MODULE_ALIAS("platform:nuc900-ac97"); | ||