blob: fe3285ceaf5bdc4b8903260aed762ab78b02a890 [file] [log] [blame]
Wan ZongShun1082e272010-05-18 13:41:46 +08001/*
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>
Wan ZongShun1082e272010-05-18 13:41:46 +080023#include <linux/clk.h>
24
25#include <mach/mfp.h>
26
Wan ZongShun0dc3b442010-06-02 14:05:14 +080027#include "nuc900-audio.h"
Wan ZongShun1082e272010-05-18 13:41:46 +080028
29static DEFINE_MUTEX(ac97_mutex);
30struct nuc900_audio *nuc900_ac97_data;
31
32static int nuc900_checkready(void)
33{
34 struct nuc900_audio *nuc900_audio = nuc900_ac97_data;
35
36 if (!(AUDIO_READ(nuc900_audio->mmio + ACTL_ACIS0) & CODEC_READY))
37 return -EPERM;
38
39 return 0;
40}
41
42/* AC97 controller reads codec register */
43static unsigned short nuc900_ac97_read(struct snd_ac97 *ac97,
44 unsigned short reg)
45{
46 struct nuc900_audio *nuc900_audio = nuc900_ac97_data;
47 unsigned long timeout = 0x10000, val;
48
49 mutex_lock(&ac97_mutex);
50
51 val = nuc900_checkready();
Axel Lin3f90e502010-11-29 17:43:39 +080052 if (val) {
Wan ZongShun1082e272010-05-18 13:41:46 +080053 dev_err(nuc900_audio->dev, "AC97 codec is not ready\n");
54 goto out;
55 }
56
57 /* set the R_WB bit and write register index */
58 AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS1, R_WB | reg);
59
60 /* set the valid frame bit and valid slots */
61 val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0);
62 val |= (VALID_FRAME | SLOT1_VALID);
63 AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, val);
64
65 udelay(100);
66
67 /* polling the AC_R_FINISH */
Wan ZongShun8dfb0c72010-06-02 14:02:33 +080068 while (!(AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON) & AC_R_FINISH)
69 && timeout--)
Wan ZongShun1082e272010-05-18 13:41:46 +080070 mdelay(1);
71
72 if (!timeout) {
73 dev_err(nuc900_audio->dev, "AC97 read register time out !\n");
74 val = -EPERM;
75 goto out;
76 }
77
78 val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0) ;
79 val &= ~SLOT1_VALID;
80 AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, val);
81
82 if (AUDIO_READ(nuc900_audio->mmio + ACTL_ACIS1) >> 2 != reg) {
83 dev_err(nuc900_audio->dev,
84 "R_INDEX of REG_ACTL_ACIS1 not match!\n");
85 }
86
87 udelay(100);
88 val = (AUDIO_READ(nuc900_audio->mmio + ACTL_ACIS2) & 0xFFFF);
89
90out:
91 mutex_unlock(&ac97_mutex);
92 return val;
93}
94
95/* AC97 controller writes to codec register */
96static void nuc900_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
97 unsigned short val)
98{
99 struct nuc900_audio *nuc900_audio = nuc900_ac97_data;
100 unsigned long tmp, timeout = 0x10000;
101
102 mutex_lock(&ac97_mutex);
103
104 tmp = nuc900_checkready();
Axel Lin3f90e502010-11-29 17:43:39 +0800105 if (tmp)
Wan ZongShun1082e272010-05-18 13:41:46 +0800106 dev_err(nuc900_audio->dev, "AC97 codec is not ready\n");
107
108 /* clear the R_WB bit and write register index */
109 AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS1, reg);
110
111 /* write register value */
112 AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS2, val);
113
114 /* set the valid frame bit and valid slots */
115 tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0);
116 tmp |= SLOT1_VALID | SLOT2_VALID | VALID_FRAME;
117 AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, tmp);
118
119 udelay(100);
120
121 /* polling the AC_W_FINISH */
Wan ZongShun8dfb0c72010-06-02 14:02:33 +0800122 while ((AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON) & AC_W_FINISH)
123 && timeout--)
Wan ZongShun1082e272010-05-18 13:41:46 +0800124 mdelay(1);
125
126 if (!timeout)
127 dev_err(nuc900_audio->dev, "AC97 write register time out !\n");
128
129 tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0);
130 tmp &= ~(SLOT1_VALID | SLOT2_VALID);
131 AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, tmp);
132
133 mutex_unlock(&ac97_mutex);
134
135}
136
137static void nuc900_ac97_warm_reset(struct snd_ac97 *ac97)
138{
139 struct nuc900_audio *nuc900_audio = nuc900_ac97_data;
140 unsigned long val;
141
142 mutex_lock(&ac97_mutex);
143
144 /* warm reset AC 97 */
145 val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON);
146 val |= AC_W_RES;
147 AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACCON, val);
148
Wan ZongShun08a0b712010-06-02 13:57:01 +0800149 udelay(100);
Wan ZongShun1082e272010-05-18 13:41:46 +0800150
151 val = nuc900_checkready();
Axel Lin3f90e502010-11-29 17:43:39 +0800152 if (val)
Wan ZongShun1082e272010-05-18 13:41:46 +0800153 dev_err(nuc900_audio->dev, "AC97 codec is not ready\n");
154
155 mutex_unlock(&ac97_mutex);
156}
157
158static void nuc900_ac97_cold_reset(struct snd_ac97 *ac97)
159{
160 struct nuc900_audio *nuc900_audio = nuc900_ac97_data;
161 unsigned long val;
162
163 mutex_lock(&ac97_mutex);
164
165 /* reset Audio Controller */
166 val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET);
167 val |= ACTL_RESET_BIT;
168 AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val);
169
Wan ZongShun1082e272010-05-18 13:41:46 +0800170 val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET);
171 val &= (~ACTL_RESET_BIT);
172 AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val);
173
Wan ZongShun1082e272010-05-18 13:41:46 +0800174 /* reset AC-link interface */
175
176 val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET);
177 val |= AC_RESET;
178 AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val);
179
Wan ZongShun1082e272010-05-18 13:41:46 +0800180 val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET);
181 val &= ~AC_RESET;
182 AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val);
183
Wan ZongShun1082e272010-05-18 13:41:46 +0800184 /* cold reset AC 97 */
185 val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON);
186 val |= AC_C_RES;
187 AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACCON, val);
188
Wan ZongShun1082e272010-05-18 13:41:46 +0800189 val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON);
190 val &= (~AC_C_RES);
191 AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACCON, val);
192
Wan ZongShun08a0b712010-06-02 13:57:01 +0800193 udelay(100);
Wan ZongShun1082e272010-05-18 13:41:46 +0800194
195 mutex_unlock(&ac97_mutex);
196
197}
198
199/* AC97 controller operations */
200struct snd_ac97_bus_ops soc_ac97_ops = {
201 .read = nuc900_ac97_read,
202 .write = nuc900_ac97_write,
203 .reset = nuc900_ac97_cold_reset,
204 .warm_reset = nuc900_ac97_warm_reset,
205}
206EXPORT_SYMBOL_GPL(soc_ac97_ops);
207
208static int nuc900_ac97_trigger(struct snd_pcm_substream *substream,
209 int cmd, struct snd_soc_dai *dai)
210{
211 struct nuc900_audio *nuc900_audio = nuc900_ac97_data;
Wan ZongShun018334c2010-06-02 13:54:25 +0800212 int ret;
Wan ZongShun1082e272010-05-18 13:41:46 +0800213 unsigned long val, tmp;
214
215 ret = 0;
216
217 switch (cmd) {
218 case SNDRV_PCM_TRIGGER_START:
219 case SNDRV_PCM_TRIGGER_RESUME:
220 val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET);
Wan ZongShun018334c2010-06-02 13:54:25 +0800221 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Wan ZongShun1082e272010-05-18 13:41:46 +0800222 tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0);
223 tmp |= (SLOT3_VALID | SLOT4_VALID | VALID_FRAME);
224 AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, tmp);
225
226 tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_PSR);
227 tmp |= (P_DMA_END_IRQ | P_DMA_MIDDLE_IRQ);
228 AUDIO_WRITE(nuc900_audio->mmio + ACTL_PSR, tmp);
229 val |= AC_PLAY;
230 } else {
231 tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_RSR);
232 tmp |= (R_DMA_END_IRQ | R_DMA_MIDDLE_IRQ);
233
234 AUDIO_WRITE(nuc900_audio->mmio + ACTL_RSR, tmp);
235 val |= AC_RECORD;
236 }
237
238 AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val);
239
240 break;
241 case SNDRV_PCM_TRIGGER_STOP:
242 case SNDRV_PCM_TRIGGER_SUSPEND:
243 val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET);
Wan ZongShun018334c2010-06-02 13:54:25 +0800244 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Wan ZongShun1082e272010-05-18 13:41:46 +0800245 tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0);
246 tmp &= ~(SLOT3_VALID | SLOT4_VALID);
247 AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, tmp);
248
249 AUDIO_WRITE(nuc900_audio->mmio + ACTL_PSR, RESET_PRSR);
250 val &= ~AC_PLAY;
251 } else {
252 AUDIO_WRITE(nuc900_audio->mmio + ACTL_RSR, RESET_PRSR);
253 val &= ~AC_RECORD;
254 }
255
256 AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val);
257
258 break;
259 default:
260 ret = -EINVAL;
261 }
262
263 return ret;
264}
265
Axel Line3edefb2010-11-29 17:40:05 +0800266static int nuc900_ac97_probe(struct snd_soc_dai *dai)
Wan ZongShun1082e272010-05-18 13:41:46 +0800267{
268 struct nuc900_audio *nuc900_audio = nuc900_ac97_data;
269 unsigned long val;
270
271 mutex_lock(&ac97_mutex);
272
273 /* enable unit clock */
274 clk_enable(nuc900_audio->clk);
275
276 /* enable audio controller and AC-link interface */
277 val = AUDIO_READ(nuc900_audio->mmio + ACTL_CON);
278 val |= (IIS_AC_PIN_SEL | ACLINK_EN);
279 AUDIO_WRITE(nuc900_audio->mmio + ACTL_CON, val);
280
281 mutex_unlock(&ac97_mutex);
282
283 return 0;
284}
285
Axel Line3edefb2010-11-29 17:40:05 +0800286static int nuc900_ac97_remove(struct snd_soc_dai *dai)
Wan ZongShun1082e272010-05-18 13:41:46 +0800287{
288 struct nuc900_audio *nuc900_audio = nuc900_ac97_data;
289
290 clk_disable(nuc900_audio->clk);
Axel Line3edefb2010-11-29 17:40:05 +0800291 return 0;
Wan ZongShun1082e272010-05-18 13:41:46 +0800292}
293
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100294static const struct snd_soc_dai_ops nuc900_ac97_dai_ops = {
Wan ZongShun1082e272010-05-18 13:41:46 +0800295 .trigger = nuc900_ac97_trigger,
296};
297
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000298static struct snd_soc_dai_driver nuc900_ac97_dai = {
Wan ZongShun1082e272010-05-18 13:41:46 +0800299 .probe = nuc900_ac97_probe,
300 .remove = nuc900_ac97_remove,
301 .ac97_control = 1,
302 .playback = {
303 .rates = SNDRV_PCM_RATE_8000_48000,
304 .formats = SNDRV_PCM_FMTBIT_S16_LE,
305 .channels_min = 1,
306 .channels_max = 2,
307 },
308 .capture = {
309 .rates = SNDRV_PCM_RATE_8000_48000,
310 .formats = SNDRV_PCM_FMTBIT_S16_LE,
311 .channels_min = 1,
312 .channels_max = 2,
313 },
314 .ops = &nuc900_ac97_dai_ops,
Axel Lin5a8f1d42010-11-29 17:39:10 +0800315};
Wan ZongShun1082e272010-05-18 13:41:46 +0800316
Kuninori Morimoto7fc34cc2013-03-21 03:33:13 -0700317static const struct snd_soc_component_driver nuc900_ac97_component = {
318 .name = "nuc900-ac97",
319};
320
Bill Pembertonce69ace2012-12-07 09:26:28 -0500321static int nuc900_ac97_drvprobe(struct platform_device *pdev)
Wan ZongShun1082e272010-05-18 13:41:46 +0800322{
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;
Julia Lawalla2dc56c2011-06-01 19:10:05 +0200363 goto out3;
Wan ZongShun1082e272010-05-18 13:41:46 +0800364 }
365
366 nuc900_ac97_data = nuc900_audio;
367
Kuninori Morimoto7fc34cc2013-03-21 03:33:13 -0700368 ret = snd_soc_register_component(&pdev->dev, &nuc900_ac97_component,
369 &nuc900_ac97_dai, 1);
Wan ZongShun1082e272010-05-18 13:41:46 +0800370 if (ret)
371 goto out3;
372
Axel Lin97371fa2011-11-25 00:23:28 +0100373 /* enbale ac97 multifunction pin */
Axel Line37051d2012-01-09 10:54:59 +0100374 mfp_set_groupg(nuc900_audio->dev, NULL);
Wan ZongShun1082e272010-05-18 13:41:46 +0800375
376 return 0;
377
378out3:
379 clk_put(nuc900_audio->clk);
380out2:
381 iounmap(nuc900_audio->mmio);
382out1:
383 release_mem_region(nuc900_audio->res->start,
384 resource_size(nuc900_audio->res));
385out0:
386 kfree(nuc900_audio);
387 return ret;
388}
389
Bill Pembertonce69ace2012-12-07 09:26:28 -0500390static int nuc900_ac97_drvremove(struct platform_device *pdev)
Wan ZongShun1082e272010-05-18 13:41:46 +0800391{
Kuninori Morimoto7fc34cc2013-03-21 03:33:13 -0700392 snd_soc_unregister_component(&pdev->dev);
Wan ZongShun1082e272010-05-18 13:41:46 +0800393
394 clk_put(nuc900_ac97_data->clk);
395 iounmap(nuc900_ac97_data->mmio);
396 release_mem_region(nuc900_ac97_data->res->start,
397 resource_size(nuc900_ac97_data->res));
398
Axel Lind6f443a2010-11-24 16:44:23 +0800399 kfree(nuc900_ac97_data);
Wan ZongShun1082e272010-05-18 13:41:46 +0800400 nuc900_ac97_data = NULL;
401
402 return 0;
403}
404
405static struct platform_driver nuc900_ac97_driver = {
406 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000407 .name = "nuc900-ac97",
Wan ZongShun1082e272010-05-18 13:41:46 +0800408 .owner = THIS_MODULE,
409 },
410 .probe = nuc900_ac97_drvprobe,
Bill Pembertonce69ace2012-12-07 09:26:28 -0500411 .remove = nuc900_ac97_drvremove,
Wan ZongShun1082e272010-05-18 13:41:46 +0800412};
413
Axel Lind0efa6a2011-11-24 10:45:32 +0800414module_platform_driver(nuc900_ac97_driver);
Wan ZongShun1082e272010-05-18 13:41:46 +0800415
416MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
417MODULE_DESCRIPTION("NUC900 AC97 SoC driver!");
418MODULE_LICENSE("GPL");
419MODULE_ALIAS("platform:nuc900-ac97");