Wan ZongShun | 1082e27 | 2010-05-18 13:41:46 +0800 | [diff] [blame] | 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-auido.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 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON); |
| 70 | val &= AC_R_FINISH; |
| 71 | while (!val && timeout--) |
| 72 | mdelay(1); |
| 73 | |
| 74 | if (!timeout) { |
| 75 | dev_err(nuc900_audio->dev, "AC97 read register time out !\n"); |
| 76 | val = -EPERM; |
| 77 | goto out; |
| 78 | } |
| 79 | |
| 80 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0) ; |
| 81 | val &= ~SLOT1_VALID; |
| 82 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, val); |
| 83 | |
| 84 | if (AUDIO_READ(nuc900_audio->mmio + ACTL_ACIS1) >> 2 != reg) { |
| 85 | dev_err(nuc900_audio->dev, |
| 86 | "R_INDEX of REG_ACTL_ACIS1 not match!\n"); |
| 87 | } |
| 88 | |
| 89 | udelay(100); |
| 90 | val = (AUDIO_READ(nuc900_audio->mmio + ACTL_ACIS2) & 0xFFFF); |
| 91 | |
| 92 | out: |
| 93 | mutex_unlock(&ac97_mutex); |
| 94 | return val; |
| 95 | } |
| 96 | |
| 97 | /* AC97 controller writes to codec register */ |
| 98 | static void nuc900_ac97_write(struct snd_ac97 *ac97, unsigned short reg, |
| 99 | unsigned short val) |
| 100 | { |
| 101 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; |
| 102 | unsigned long tmp, timeout = 0x10000; |
| 103 | |
| 104 | mutex_lock(&ac97_mutex); |
| 105 | |
| 106 | tmp = nuc900_checkready(); |
| 107 | if (!!tmp) |
| 108 | dev_err(nuc900_audio->dev, "AC97 codec is not ready\n"); |
| 109 | |
| 110 | /* clear the R_WB bit and write register index */ |
| 111 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS1, reg); |
| 112 | |
| 113 | /* write register value */ |
| 114 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS2, val); |
| 115 | |
| 116 | /* set the valid frame bit and valid slots */ |
| 117 | tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0); |
| 118 | tmp |= SLOT1_VALID | SLOT2_VALID | VALID_FRAME; |
| 119 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, tmp); |
| 120 | |
| 121 | udelay(100); |
| 122 | |
| 123 | /* polling the AC_W_FINISH */ |
| 124 | tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON); |
| 125 | tmp &= AC_W_FINISH; |
| 126 | while (tmp && timeout--) |
| 127 | mdelay(1); |
| 128 | |
| 129 | if (!timeout) |
| 130 | dev_err(nuc900_audio->dev, "AC97 write register time out !\n"); |
| 131 | |
| 132 | tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0); |
| 133 | tmp &= ~(SLOT1_VALID | SLOT2_VALID); |
| 134 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, tmp); |
| 135 | |
| 136 | mutex_unlock(&ac97_mutex); |
| 137 | |
| 138 | } |
| 139 | |
| 140 | static void nuc900_ac97_warm_reset(struct snd_ac97 *ac97) |
| 141 | { |
| 142 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; |
| 143 | unsigned long val; |
| 144 | |
| 145 | mutex_lock(&ac97_mutex); |
| 146 | |
| 147 | /* warm reset AC 97 */ |
| 148 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON); |
| 149 | val |= AC_W_RES; |
| 150 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACCON, val); |
| 151 | |
| 152 | udelay(1000); |
| 153 | |
| 154 | val = nuc900_checkready(); |
| 155 | if (!!val) |
| 156 | dev_err(nuc900_audio->dev, "AC97 codec is not ready\n"); |
| 157 | |
| 158 | mutex_unlock(&ac97_mutex); |
| 159 | } |
| 160 | |
| 161 | static void nuc900_ac97_cold_reset(struct snd_ac97 *ac97) |
| 162 | { |
| 163 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; |
| 164 | unsigned long val; |
| 165 | |
| 166 | mutex_lock(&ac97_mutex); |
| 167 | |
| 168 | /* reset Audio Controller */ |
| 169 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET); |
| 170 | val |= ACTL_RESET_BIT; |
| 171 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val); |
| 172 | |
| 173 | udelay(1000); |
| 174 | |
| 175 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET); |
| 176 | val &= (~ACTL_RESET_BIT); |
| 177 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val); |
| 178 | |
| 179 | udelay(1000); |
| 180 | |
| 181 | /* reset AC-link interface */ |
| 182 | |
| 183 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET); |
| 184 | val |= AC_RESET; |
| 185 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val); |
| 186 | |
| 187 | udelay(1000); |
| 188 | |
| 189 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET); |
| 190 | val &= ~AC_RESET; |
| 191 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val); |
| 192 | |
| 193 | udelay(1000); |
| 194 | |
| 195 | /* cold reset AC 97 */ |
| 196 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON); |
| 197 | val |= AC_C_RES; |
| 198 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACCON, val); |
| 199 | |
| 200 | udelay(1000); |
| 201 | |
| 202 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_ACCON); |
| 203 | val &= (~AC_C_RES); |
| 204 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACCON, val); |
| 205 | |
| 206 | udelay(1000); |
| 207 | |
| 208 | mutex_unlock(&ac97_mutex); |
| 209 | |
| 210 | } |
| 211 | |
| 212 | /* AC97 controller operations */ |
| 213 | struct snd_ac97_bus_ops soc_ac97_ops = { |
| 214 | .read = nuc900_ac97_read, |
| 215 | .write = nuc900_ac97_write, |
| 216 | .reset = nuc900_ac97_cold_reset, |
| 217 | .warm_reset = nuc900_ac97_warm_reset, |
| 218 | } |
| 219 | EXPORT_SYMBOL_GPL(soc_ac97_ops); |
| 220 | |
| 221 | static int nuc900_ac97_trigger(struct snd_pcm_substream *substream, |
| 222 | int cmd, struct snd_soc_dai *dai) |
| 223 | { |
| 224 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; |
| 225 | int ret, stype = SUBSTREAM_TYPE(substream); |
| 226 | unsigned long val, tmp; |
| 227 | |
| 228 | ret = 0; |
| 229 | |
| 230 | switch (cmd) { |
| 231 | case SNDRV_PCM_TRIGGER_START: |
| 232 | case SNDRV_PCM_TRIGGER_RESUME: |
| 233 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET); |
| 234 | if (PCM_TX == stype) { |
| 235 | tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0); |
| 236 | tmp |= (SLOT3_VALID | SLOT4_VALID | VALID_FRAME); |
| 237 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, tmp); |
| 238 | |
| 239 | tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_PSR); |
| 240 | tmp |= (P_DMA_END_IRQ | P_DMA_MIDDLE_IRQ); |
| 241 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_PSR, tmp); |
| 242 | val |= AC_PLAY; |
| 243 | } else { |
| 244 | tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_RSR); |
| 245 | tmp |= (R_DMA_END_IRQ | R_DMA_MIDDLE_IRQ); |
| 246 | |
| 247 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RSR, tmp); |
| 248 | val |= AC_RECORD; |
| 249 | } |
| 250 | |
| 251 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val); |
| 252 | |
| 253 | break; |
| 254 | case SNDRV_PCM_TRIGGER_STOP: |
| 255 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 256 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_RESET); |
| 257 | if (PCM_TX == stype) { |
| 258 | tmp = AUDIO_READ(nuc900_audio->mmio + ACTL_ACOS0); |
| 259 | tmp &= ~(SLOT3_VALID | SLOT4_VALID); |
| 260 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_ACOS0, tmp); |
| 261 | |
| 262 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_PSR, RESET_PRSR); |
| 263 | val &= ~AC_PLAY; |
| 264 | } else { |
| 265 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RSR, RESET_PRSR); |
| 266 | val &= ~AC_RECORD; |
| 267 | } |
| 268 | |
| 269 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_RESET, val); |
| 270 | |
| 271 | break; |
| 272 | default: |
| 273 | ret = -EINVAL; |
| 274 | } |
| 275 | |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | static int nuc900_ac97_probe(struct platform_device *pdev, |
| 280 | struct snd_soc_dai *dai) |
| 281 | { |
| 282 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; |
| 283 | unsigned long val; |
| 284 | |
| 285 | mutex_lock(&ac97_mutex); |
| 286 | |
| 287 | /* enable unit clock */ |
| 288 | clk_enable(nuc900_audio->clk); |
| 289 | |
| 290 | /* enable audio controller and AC-link interface */ |
| 291 | val = AUDIO_READ(nuc900_audio->mmio + ACTL_CON); |
| 292 | val |= (IIS_AC_PIN_SEL | ACLINK_EN); |
| 293 | AUDIO_WRITE(nuc900_audio->mmio + ACTL_CON, val); |
| 294 | |
| 295 | mutex_unlock(&ac97_mutex); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static void nuc900_ac97_remove(struct platform_device *pdev, |
| 301 | struct snd_soc_dai *dai) |
| 302 | { |
| 303 | struct nuc900_audio *nuc900_audio = nuc900_ac97_data; |
| 304 | |
| 305 | clk_disable(nuc900_audio->clk); |
| 306 | } |
| 307 | |
| 308 | static struct snd_soc_dai_ops nuc900_ac97_dai_ops = { |
| 309 | .trigger = nuc900_ac97_trigger, |
| 310 | }; |
| 311 | |
| 312 | struct snd_soc_dai nuc900_ac97_dai = { |
| 313 | .name = "nuc900-ac97", |
| 314 | .probe = nuc900_ac97_probe, |
| 315 | .remove = nuc900_ac97_remove, |
| 316 | .ac97_control = 1, |
| 317 | .playback = { |
| 318 | .rates = SNDRV_PCM_RATE_8000_48000, |
| 319 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 320 | .channels_min = 1, |
| 321 | .channels_max = 2, |
| 322 | }, |
| 323 | .capture = { |
| 324 | .rates = SNDRV_PCM_RATE_8000_48000, |
| 325 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 326 | .channels_min = 1, |
| 327 | .channels_max = 2, |
| 328 | }, |
| 329 | .ops = &nuc900_ac97_dai_ops, |
| 330 | } |
| 331 | EXPORT_SYMBOL_GPL(nuc900_ac97_dai); |
| 332 | |
| 333 | static int __devinit nuc900_ac97_drvprobe(struct platform_device *pdev) |
| 334 | { |
| 335 | struct nuc900_audio *nuc900_audio; |
| 336 | int ret; |
| 337 | |
| 338 | if (nuc900_ac97_data) |
| 339 | return -EBUSY; |
| 340 | |
| 341 | nuc900_audio = kzalloc(sizeof(struct nuc900_audio), GFP_KERNEL); |
| 342 | if (!nuc900_audio) |
| 343 | return -ENOMEM; |
| 344 | |
| 345 | spin_lock_init(&nuc900_audio->lock); |
| 346 | |
| 347 | nuc900_audio->res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 348 | if (!nuc900_audio->res) { |
| 349 | ret = -ENODEV; |
| 350 | goto out0; |
| 351 | } |
| 352 | |
| 353 | if (!request_mem_region(nuc900_audio->res->start, |
| 354 | resource_size(nuc900_audio->res), pdev->name)) { |
| 355 | ret = -EBUSY; |
| 356 | goto out0; |
| 357 | } |
| 358 | |
| 359 | nuc900_audio->mmio = ioremap(nuc900_audio->res->start, |
| 360 | resource_size(nuc900_audio->res)); |
| 361 | if (!nuc900_audio->mmio) { |
| 362 | ret = -ENOMEM; |
| 363 | goto out1; |
| 364 | } |
| 365 | |
| 366 | nuc900_audio->clk = clk_get(&pdev->dev, NULL); |
| 367 | if (IS_ERR(nuc900_audio->clk)) { |
| 368 | ret = PTR_ERR(nuc900_audio->clk); |
| 369 | goto out2; |
| 370 | } |
| 371 | |
| 372 | nuc900_audio->irq_num = platform_get_irq(pdev, 0); |
| 373 | if (!nuc900_audio->irq_num) { |
| 374 | ret = -EBUSY; |
| 375 | goto out2; |
| 376 | } |
| 377 | |
| 378 | nuc900_ac97_data = nuc900_audio; |
| 379 | |
| 380 | nuc900_audio->dev = nuc900_ac97_dai.dev = &pdev->dev; |
| 381 | |
| 382 | ret = snd_soc_register_dai(&nuc900_ac97_dai); |
| 383 | if (ret) |
| 384 | goto out3; |
| 385 | |
| 386 | mfp_set_groupg(nuc900_audio->dev); /* enbale ac97 multifunction pin*/ |
| 387 | |
| 388 | return 0; |
| 389 | |
| 390 | out3: |
| 391 | clk_put(nuc900_audio->clk); |
| 392 | out2: |
| 393 | iounmap(nuc900_audio->mmio); |
| 394 | out1: |
| 395 | release_mem_region(nuc900_audio->res->start, |
| 396 | resource_size(nuc900_audio->res)); |
| 397 | out0: |
| 398 | kfree(nuc900_audio); |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | static int __devexit nuc900_ac97_drvremove(struct platform_device *pdev) |
| 403 | { |
| 404 | |
| 405 | snd_soc_unregister_dai(&nuc900_ac97_dai); |
| 406 | |
| 407 | clk_put(nuc900_ac97_data->clk); |
| 408 | iounmap(nuc900_ac97_data->mmio); |
| 409 | release_mem_region(nuc900_ac97_data->res->start, |
| 410 | resource_size(nuc900_ac97_data->res)); |
| 411 | |
| 412 | nuc900_ac97_data = NULL; |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static struct platform_driver nuc900_ac97_driver = { |
| 418 | .driver = { |
| 419 | .name = "nuc900-audio", |
| 420 | .owner = THIS_MODULE, |
| 421 | }, |
| 422 | .probe = nuc900_ac97_drvprobe, |
| 423 | .remove = __devexit_p(nuc900_ac97_drvremove), |
| 424 | }; |
| 425 | |
| 426 | static int __init nuc900_ac97_init(void) |
| 427 | { |
| 428 | return platform_driver_register(&nuc900_ac97_driver); |
| 429 | } |
| 430 | |
| 431 | static void __exit nuc900_ac97_exit(void) |
| 432 | { |
| 433 | platform_driver_unregister(&nuc900_ac97_driver); |
| 434 | } |
| 435 | |
| 436 | module_init(nuc900_ac97_init); |
| 437 | module_exit(nuc900_ac97_exit); |
| 438 | |
| 439 | MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>"); |
| 440 | MODULE_DESCRIPTION("NUC900 AC97 SoC driver!"); |
| 441 | MODULE_LICENSE("GPL"); |
| 442 | MODULE_ALIAS("platform:nuc900-ac97"); |