blob: 222765fd0dc56412031696519d862ed695a5dadb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PMac DBDMA lowlevel functions
3 *
4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
5 * code based on dmasound.c.
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 as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22
23#include <sound/driver.h>
24#include <asm/io.h>
25#include <asm/irq.h>
26#include <linux/init.h>
27#include <linux/delay.h>
28#include <linux/slab.h>
29#include <linux/interrupt.h>
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070030#include <linux/pci.h>
31#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/core.h>
33#include "pmac.h"
34#include <sound/pcm_params.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/pmac_feature.h>
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070036#include <asm/pci-bridge.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -070039#ifdef CONFIG_PM
Takashi Iwai65b29f52005-11-17 15:09:46 +010040static int snd_pmac_register_sleep_notifier(struct snd_pmac *chip);
41static int snd_pmac_unregister_sleep_notifier(struct snd_pmac *chip);
42static int snd_pmac_suspend(struct snd_card *card, pm_message_t state);
43static int snd_pmac_resume(struct snd_card *card);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#endif
45
46
47/* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
48static int awacs_freqs[8] = {
49 44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
50};
51/* fixed frequency table for tumbler */
52static int tumbler_freqs[1] = {
53 44100
54};
55
56/*
57 * allocate DBDMA command arrays
58 */
Takashi Iwai65b29f52005-11-17 15:09:46 +010059static int snd_pmac_dbdma_alloc(struct snd_pmac *chip, struct pmac_dbdma *rec, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070061 unsigned int rsize = sizeof(struct dbdma_cmd) * (size + 1);
62
63 rec->space = dma_alloc_coherent(&chip->pdev->dev, rsize,
64 &rec->dma_base, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (rec->space == NULL)
66 return -ENOMEM;
67 rec->size = size;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070068 memset(rec->space, 0, rsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 rec->cmds = (void __iomem *)DBDMA_ALIGN(rec->space);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070070 rec->addr = rec->dma_base + (unsigned long)((char *)rec->cmds - (char *)rec->space);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return 0;
73}
74
Takashi Iwai65b29f52005-11-17 15:09:46 +010075static void snd_pmac_dbdma_free(struct snd_pmac *chip, struct pmac_dbdma *rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070077 if (rec) {
78 unsigned int rsize = sizeof(struct dbdma_cmd) * (rec->size + 1);
79
80 dma_free_coherent(&chip->pdev->dev, rsize, rec->space, rec->dma_base);
81 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84
85/*
86 * pcm stuff
87 */
88
89/*
90 * look up frequency table
91 */
92
Takashi Iwai65b29f52005-11-17 15:09:46 +010093unsigned int snd_pmac_rate_index(struct snd_pmac *chip, struct pmac_stream *rec, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 int i, ok, found;
96
97 ok = rec->cur_freqs;
98 if (rate > chip->freq_table[0])
99 return 0;
100 found = 0;
101 for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
102 if (! (ok & 1)) continue;
103 found = i;
104 if (rate >= chip->freq_table[i])
105 break;
106 }
107 return found;
108}
109
110/*
111 * check whether another stream is active
112 */
113static inline int another_stream(int stream)
114{
115 return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
116 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
117}
118
119/*
120 * allocate buffers
121 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100122static int snd_pmac_pcm_hw_params(struct snd_pcm_substream *subs,
123 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
126}
127
128/*
129 * release buffers
130 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100131static int snd_pmac_pcm_hw_free(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 snd_pcm_lib_free_pages(subs);
134 return 0;
135}
136
137/*
138 * get a stream of the opposite direction
139 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100140static struct pmac_stream *snd_pmac_get_stream(struct snd_pmac *chip, int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 switch (stream) {
143 case SNDRV_PCM_STREAM_PLAYBACK:
144 return &chip->playback;
145 case SNDRV_PCM_STREAM_CAPTURE:
146 return &chip->capture;
147 default:
148 snd_BUG();
149 return NULL;
150 }
151}
152
153/*
154 * wait while run status is on
155 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700156static inline void
Takashi Iwai65b29f52005-11-17 15:09:46 +0100157snd_pmac_wait_ack(struct pmac_stream *rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 int timeout = 50000;
160 while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
161 udelay(1);
162}
163
164/*
165 * set the format and rate to the chip.
166 * call the lowlevel function if defined (e.g. for AWACS).
167 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100168static void snd_pmac_pcm_set_format(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 /* set up frequency and format */
171 out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
172 out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
173 if (chip->set_format)
174 chip->set_format(chip);
175}
176
177/*
178 * stop the DMA transfer
179 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100180static inline void snd_pmac_dma_stop(struct pmac_stream *rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
183 snd_pmac_wait_ack(rec);
184}
185
186/*
187 * set the command pointer address
188 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100189static inline void snd_pmac_dma_set_command(struct pmac_stream *rec, struct pmac_dbdma *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 out_le32(&rec->dma->cmdptr, cmd->addr);
192}
193
194/*
195 * start the DMA
196 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100197static inline void snd_pmac_dma_run(struct pmac_stream *rec, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 out_le32(&rec->dma->control, status | (status << 16));
200}
201
202
203/*
204 * prepare playback/capture stream
205 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100206static int snd_pmac_pcm_prepare(struct snd_pmac *chip, struct pmac_stream *rec, struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 int i;
209 volatile struct dbdma_cmd __iomem *cp;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100210 struct snd_pcm_runtime *runtime = subs->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 int rate_index;
212 long offset;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100213 struct pmac_stream *astr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
216 rec->period_size = snd_pcm_lib_period_bytes(subs);
217 rec->nperiods = rec->dma_size / rec->period_size;
218 rec->cur_period = 0;
219 rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
220
221 /* set up constraints */
222 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200223 if (! astr)
224 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 astr->cur_freqs = 1 << rate_index;
226 astr->cur_formats = 1 << runtime->format;
227 chip->rate_index = rate_index;
228 chip->format = runtime->format;
229
230 /* We really want to execute a DMA stop command, after the AWACS
231 * is initialized.
232 * For reasons I don't understand, it stops the hissing noise
233 * common to many PowerBook G3 systems and random noise otherwise
234 * captured on iBook2's about every third time. -ReneR
235 */
236 spin_lock_irq(&chip->reg_lock);
237 snd_pmac_dma_stop(rec);
238 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
239 snd_pmac_dma_set_command(rec, &chip->extra_dma);
240 snd_pmac_dma_run(rec, RUN);
241 spin_unlock_irq(&chip->reg_lock);
242 mdelay(5);
243 spin_lock_irq(&chip->reg_lock);
244 /* continuous DMA memory type doesn't provide the physical address,
245 * so we need to resolve the address here...
246 */
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700247 offset = runtime->dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) {
249 st_le32(&cp->phy_addr, offset);
250 st_le16(&cp->req_count, rec->period_size);
251 /*st_le16(&cp->res_count, 0);*/
252 st_le16(&cp->xfer_status, 0);
253 offset += rec->period_size;
254 }
255 /* make loop */
256 st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
257 st_le32(&cp->cmd_dep, rec->cmd.addr);
258
259 snd_pmac_dma_stop(rec);
260 snd_pmac_dma_set_command(rec, &rec->cmd);
261 spin_unlock_irq(&chip->reg_lock);
262
263 return 0;
264}
265
266
267/*
268 * PCM trigger/stop
269 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100270static int snd_pmac_pcm_trigger(struct snd_pmac *chip, struct pmac_stream *rec,
271 struct snd_pcm_substream *subs, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 volatile struct dbdma_cmd __iomem *cp;
274 int i, command;
275
276 switch (cmd) {
277 case SNDRV_PCM_TRIGGER_START:
278 case SNDRV_PCM_TRIGGER_RESUME:
279 if (rec->running)
280 return -EBUSY;
281 command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
282 OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
283 spin_lock(&chip->reg_lock);
284 snd_pmac_beep_stop(chip);
285 snd_pmac_pcm_set_format(chip);
286 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
287 out_le16(&cp->command, command);
288 snd_pmac_dma_set_command(rec, &rec->cmd);
289 (void)in_le32(&rec->dma->status);
290 snd_pmac_dma_run(rec, RUN|WAKE);
291 rec->running = 1;
292 spin_unlock(&chip->reg_lock);
293 break;
294
295 case SNDRV_PCM_TRIGGER_STOP:
296 case SNDRV_PCM_TRIGGER_SUSPEND:
297 spin_lock(&chip->reg_lock);
298 rec->running = 0;
299 /*printk("stopped!!\n");*/
300 snd_pmac_dma_stop(rec);
301 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
302 out_le16(&cp->command, DBDMA_STOP);
303 spin_unlock(&chip->reg_lock);
304 break;
305
306 default:
307 return -EINVAL;
308 }
309
310 return 0;
311}
312
313/*
314 * return the current pointer
315 */
316inline
Takashi Iwai65b29f52005-11-17 15:09:46 +0100317static snd_pcm_uframes_t snd_pmac_pcm_pointer(struct snd_pmac *chip,
318 struct pmac_stream *rec,
319 struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 int count = 0;
322
323#if 1 /* hmm.. how can we get the current dma pointer?? */
324 int stat;
325 volatile struct dbdma_cmd __iomem *cp = &rec->cmd.cmds[rec->cur_period];
326 stat = ld_le16(&cp->xfer_status);
327 if (stat & (ACTIVE|DEAD)) {
328 count = in_le16(&cp->res_count);
329 if (count)
330 count = rec->period_size - count;
331 }
332#endif
333 count += rec->cur_period * rec->period_size;
334 /*printk("pointer=%d\n", count);*/
335 return bytes_to_frames(subs->runtime, count);
336}
337
338/*
339 * playback
340 */
341
Takashi Iwai65b29f52005-11-17 15:09:46 +0100342static int snd_pmac_playback_prepare(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100344 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
346}
347
Takashi Iwai65b29f52005-11-17 15:09:46 +0100348static int snd_pmac_playback_trigger(struct snd_pcm_substream *subs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int cmd)
350{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100351 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
353}
354
Takashi Iwai65b29f52005-11-17 15:09:46 +0100355static snd_pcm_uframes_t snd_pmac_playback_pointer(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100357 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
359}
360
361
362/*
363 * capture
364 */
365
Takashi Iwai65b29f52005-11-17 15:09:46 +0100366static int snd_pmac_capture_prepare(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100368 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
370}
371
Takashi Iwai65b29f52005-11-17 15:09:46 +0100372static int snd_pmac_capture_trigger(struct snd_pcm_substream *subs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int cmd)
374{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100375 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
377}
378
Takashi Iwai65b29f52005-11-17 15:09:46 +0100379static snd_pcm_uframes_t snd_pmac_capture_pointer(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100381 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
383}
384
385
386/*
387 * update playback/capture pointer from interrupts
388 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100389static void snd_pmac_pcm_update(struct snd_pmac *chip, struct pmac_stream *rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 volatile struct dbdma_cmd __iomem *cp;
392 int c;
393 int stat;
394
395 spin_lock(&chip->reg_lock);
396 if (rec->running) {
397 cp = &rec->cmd.cmds[rec->cur_period];
398 for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */
399 stat = ld_le16(&cp->xfer_status);
400 if (! (stat & ACTIVE))
401 break;
402 /*printk("update frag %d\n", rec->cur_period);*/
403 st_le16(&cp->xfer_status, 0);
404 st_le16(&cp->req_count, rec->period_size);
405 /*st_le16(&cp->res_count, 0);*/
406 rec->cur_period++;
407 if (rec->cur_period >= rec->nperiods) {
408 rec->cur_period = 0;
409 cp = rec->cmd.cmds;
410 } else
411 cp++;
412 spin_unlock(&chip->reg_lock);
413 snd_pcm_period_elapsed(rec->substream);
414 spin_lock(&chip->reg_lock);
415 }
416 }
417 spin_unlock(&chip->reg_lock);
418}
419
420
421/*
422 * hw info
423 */
424
Takashi Iwai65b29f52005-11-17 15:09:46 +0100425static struct snd_pcm_hardware snd_pmac_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 .info = (SNDRV_PCM_INFO_INTERLEAVED |
428 SNDRV_PCM_INFO_MMAP |
429 SNDRV_PCM_INFO_MMAP_VALID |
430 SNDRV_PCM_INFO_RESUME),
431 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
432 .rates = SNDRV_PCM_RATE_8000_44100,
433 .rate_min = 7350,
434 .rate_max = 44100,
435 .channels_min = 2,
436 .channels_max = 2,
437 .buffer_bytes_max = 131072,
438 .period_bytes_min = 256,
439 .period_bytes_max = 16384,
440 .periods_min = 3,
441 .periods_max = PMAC_MAX_FRAGS,
442};
443
Takashi Iwai65b29f52005-11-17 15:09:46 +0100444static struct snd_pcm_hardware snd_pmac_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 .info = (SNDRV_PCM_INFO_INTERLEAVED |
447 SNDRV_PCM_INFO_MMAP |
448 SNDRV_PCM_INFO_MMAP_VALID |
449 SNDRV_PCM_INFO_RESUME),
450 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
451 .rates = SNDRV_PCM_RATE_8000_44100,
452 .rate_min = 7350,
453 .rate_max = 44100,
454 .channels_min = 2,
455 .channels_max = 2,
456 .buffer_bytes_max = 131072,
457 .period_bytes_min = 256,
458 .period_bytes_max = 16384,
459 .periods_min = 3,
460 .periods_max = PMAC_MAX_FRAGS,
461};
462
463
464#if 0 // NYI
Takashi Iwai65b29f52005-11-17 15:09:46 +0100465static int snd_pmac_hw_rule_rate(struct snd_pcm_hw_params *params,
466 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100468 struct snd_pmac *chip = rule->private;
469 struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int i, freq_table[8], num_freqs;
471
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200472 if (! rec)
473 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 num_freqs = 0;
475 for (i = chip->num_freqs - 1; i >= 0; i--) {
476 if (rec->cur_freqs & (1 << i))
477 freq_table[num_freqs++] = chip->freq_table[i];
478 }
479
480 return snd_interval_list(hw_param_interval(params, rule->var),
481 num_freqs, freq_table, 0);
482}
483
Takashi Iwai65b29f52005-11-17 15:09:46 +0100484static int snd_pmac_hw_rule_format(struct snd_pcm_hw_params *params,
485 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100487 struct snd_pmac *chip = rule->private;
488 struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200490 if (! rec)
491 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
493 rec->cur_formats);
494}
495#endif // NYI
496
Takashi Iwai65b29f52005-11-17 15:09:46 +0100497static int snd_pmac_pcm_open(struct snd_pmac *chip, struct pmac_stream *rec,
498 struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100500 struct snd_pcm_runtime *runtime = subs->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 int i, j, fflags;
502 static int typical_freqs[] = {
503 44100,
504 22050,
505 11025,
506 0,
507 };
508 static int typical_freq_flags[] = {
509 SNDRV_PCM_RATE_44100,
510 SNDRV_PCM_RATE_22050,
511 SNDRV_PCM_RATE_11025,
512 0,
513 };
514
515 /* look up frequency table and fill bit mask */
516 runtime->hw.rates = 0;
517 fflags = chip->freqs_ok;
518 for (i = 0; typical_freqs[i]; i++) {
519 for (j = 0; j < chip->num_freqs; j++) {
520 if ((chip->freqs_ok & (1 << j)) &&
521 chip->freq_table[j] == typical_freqs[i]) {
522 runtime->hw.rates |= typical_freq_flags[i];
523 fflags &= ~(1 << j);
524 break;
525 }
526 }
527 }
528 if (fflags) /* rest */
529 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
530
531 /* check for minimum and maximum rates */
532 for (i = 0; i < chip->num_freqs; i++) {
533 if (chip->freqs_ok & (1 << i)) {
534 runtime->hw.rate_max = chip->freq_table[i];
535 break;
536 }
537 }
538 for (i = chip->num_freqs - 1; i >= 0; i--) {
539 if (chip->freqs_ok & (1 << i)) {
540 runtime->hw.rate_min = chip->freq_table[i];
541 break;
542 }
543 }
544 runtime->hw.formats = chip->formats_ok;
545 if (chip->can_capture) {
546 if (! chip->can_duplex)
547 runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
548 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
549 }
550 runtime->private_data = rec;
551 rec->substream = subs;
552
553#if 0 /* FIXME: still under development.. */
554 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
555 snd_pmac_hw_rule_rate, chip, rec->stream, -1);
556 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
557 snd_pmac_hw_rule_format, chip, rec->stream, -1);
558#endif
559
560 runtime->hw.periods_max = rec->cmd.size - 1;
561
562 if (chip->can_duplex)
563 snd_pcm_set_sync(subs);
564
565 /* constraints to fix choppy sound */
566 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
567 return 0;
568}
569
Takashi Iwai65b29f52005-11-17 15:09:46 +0100570static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec,
571 struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100573 struct pmac_stream *astr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 snd_pmac_dma_stop(rec);
576
577 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200578 if (! astr)
579 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /* reset constraints */
582 astr->cur_freqs = chip->freqs_ok;
583 astr->cur_formats = chip->formats_ok;
584
585 return 0;
586}
587
Takashi Iwai65b29f52005-11-17 15:09:46 +0100588static int snd_pmac_playback_open(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100590 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 subs->runtime->hw = snd_pmac_playback;
593 return snd_pmac_pcm_open(chip, &chip->playback, subs);
594}
595
Takashi Iwai65b29f52005-11-17 15:09:46 +0100596static int snd_pmac_capture_open(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100598 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 subs->runtime->hw = snd_pmac_capture;
601 return snd_pmac_pcm_open(chip, &chip->capture, subs);
602}
603
Takashi Iwai65b29f52005-11-17 15:09:46 +0100604static int snd_pmac_playback_close(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100606 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 return snd_pmac_pcm_close(chip, &chip->playback, subs);
609}
610
Takashi Iwai65b29f52005-11-17 15:09:46 +0100611static int snd_pmac_capture_close(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100613 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 return snd_pmac_pcm_close(chip, &chip->capture, subs);
616}
617
618/*
619 */
620
Takashi Iwai65b29f52005-11-17 15:09:46 +0100621static struct snd_pcm_ops snd_pmac_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 .open = snd_pmac_playback_open,
623 .close = snd_pmac_playback_close,
624 .ioctl = snd_pcm_lib_ioctl,
625 .hw_params = snd_pmac_pcm_hw_params,
626 .hw_free = snd_pmac_pcm_hw_free,
627 .prepare = snd_pmac_playback_prepare,
628 .trigger = snd_pmac_playback_trigger,
629 .pointer = snd_pmac_playback_pointer,
630};
631
Takashi Iwai65b29f52005-11-17 15:09:46 +0100632static struct snd_pcm_ops snd_pmac_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 .open = snd_pmac_capture_open,
634 .close = snd_pmac_capture_close,
635 .ioctl = snd_pcm_lib_ioctl,
636 .hw_params = snd_pmac_pcm_hw_params,
637 .hw_free = snd_pmac_pcm_hw_free,
638 .prepare = snd_pmac_capture_prepare,
639 .trigger = snd_pmac_capture_trigger,
640 .pointer = snd_pmac_capture_pointer,
641};
642
Takashi Iwai65b29f52005-11-17 15:09:46 +0100643int __init snd_pmac_pcm_new(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100645 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int err;
647 int num_captures = 1;
648
649 if (! chip->can_capture)
650 num_captures = 0;
651 err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
652 if (err < 0)
653 return err;
654
655 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
656 if (chip->can_capture)
657 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
658
659 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
661 strcpy(pcm->name, chip->card->shortname);
662 chip->pcm = pcm;
663
664 chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
665 if (chip->can_byte_swap)
666 chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
667
668 chip->playback.cur_formats = chip->formats_ok;
669 chip->capture.cur_formats = chip->formats_ok;
670 chip->playback.cur_freqs = chip->freqs_ok;
671 chip->capture.cur_freqs = chip->freqs_ok;
672
673 /* preallocate 64k buffer */
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700674 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
675 &chip->pdev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 64 * 1024, 64 * 1024);
677
678 return 0;
679}
680
681
Takashi Iwai65b29f52005-11-17 15:09:46 +0100682static void snd_pmac_dbdma_reset(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
685 snd_pmac_wait_ack(&chip->playback);
686 out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
687 snd_pmac_wait_ack(&chip->capture);
688}
689
690
691/*
692 * handling beep
693 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100694void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100696 struct pmac_stream *rec = &chip->playback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 snd_pmac_dma_stop(rec);
699 st_le16(&chip->extra_dma.cmds->req_count, bytes);
700 st_le16(&chip->extra_dma.cmds->xfer_status, 0);
701 st_le32(&chip->extra_dma.cmds->cmd_dep, chip->extra_dma.addr);
702 st_le32(&chip->extra_dma.cmds->phy_addr, addr);
703 st_le16(&chip->extra_dma.cmds->command, OUTPUT_MORE + BR_ALWAYS);
704 out_le32(&chip->awacs->control,
705 (in_le32(&chip->awacs->control) & ~0x1f00)
706 | (speed << 8));
707 out_le32(&chip->awacs->byteswap, 0);
708 snd_pmac_dma_set_command(rec, &chip->extra_dma);
709 snd_pmac_dma_run(rec, RUN);
710}
711
Takashi Iwai65b29f52005-11-17 15:09:46 +0100712void snd_pmac_beep_dma_stop(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 snd_pmac_dma_stop(&chip->playback);
715 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
716 snd_pmac_pcm_set_format(chip); /* reset format */
717}
718
719
720/*
721 * interrupt handlers
722 */
723static irqreturn_t
724snd_pmac_tx_intr(int irq, void *devid, struct pt_regs *regs)
725{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100726 struct snd_pmac *chip = devid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 snd_pmac_pcm_update(chip, &chip->playback);
728 return IRQ_HANDLED;
729}
730
731
732static irqreturn_t
733snd_pmac_rx_intr(int irq, void *devid, struct pt_regs *regs)
734{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100735 struct snd_pmac *chip = devid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 snd_pmac_pcm_update(chip, &chip->capture);
737 return IRQ_HANDLED;
738}
739
740
741static irqreturn_t
742snd_pmac_ctrl_intr(int irq, void *devid, struct pt_regs *regs)
743{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100744 struct snd_pmac *chip = devid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int ctrl = in_le32(&chip->awacs->control);
746
747 /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
748 if (ctrl & MASK_PORTCHG) {
749 /* do something when headphone is plugged/unplugged? */
750 if (chip->update_automute)
751 chip->update_automute(chip, 1);
752 }
753 if (ctrl & MASK_CNTLERR) {
754 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
755 if (err && chip->model <= PMAC_SCREAMER)
756 snd_printk(KERN_DEBUG "error %x\n", err);
757 }
758 /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
759 out_le32(&chip->awacs->control, ctrl);
760 return IRQ_HANDLED;
761}
762
763
764/*
765 * a wrapper to feature call for compatibility
766 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100767static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
David Woodhouse4e6a06e2005-08-17 11:36:35 +0100769 if (ppc_md.feature_call)
770 ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773/*
774 * release resources
775 */
776
Takashi Iwai65b29f52005-11-17 15:09:46 +0100777static int snd_pmac_free(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* stop sounds */
780 if (chip->initialized) {
781 snd_pmac_dbdma_reset(chip);
782 /* disable interrupts from awacs interface */
783 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
784 }
785
786 snd_pmac_sound_feature(chip, 0);
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700787#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 snd_pmac_unregister_sleep_notifier(chip);
789#endif
790
791 /* clean up mixer if any */
792 if (chip->mixer_free)
793 chip->mixer_free(chip);
794
795 snd_pmac_detach_beep(chip);
796
797 /* release resources */
798 if (chip->irq >= 0)
799 free_irq(chip->irq, (void*)chip);
800 if (chip->tx_irq >= 0)
801 free_irq(chip->tx_irq, (void*)chip);
802 if (chip->rx_irq >= 0)
803 free_irq(chip->rx_irq, (void*)chip);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700804 snd_pmac_dbdma_free(chip, &chip->playback.cmd);
805 snd_pmac_dbdma_free(chip, &chip->capture.cmd);
806 snd_pmac_dbdma_free(chip, &chip->extra_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (chip->macio_base)
808 iounmap(chip->macio_base);
809 if (chip->latch_base)
810 iounmap(chip->latch_base);
811 if (chip->awacs)
812 iounmap(chip->awacs);
813 if (chip->playback.dma)
814 iounmap(chip->playback.dma);
815 if (chip->capture.dma)
816 iounmap(chip->capture.dma);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700817#ifndef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (chip->node) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700819 int i;
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 for (i = 0; i < 3; i++) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700822 if (chip->of_requested & (1 << i)) {
823 if (chip->is_k2)
824 release_OF_resource(chip->node->parent,
825 i);
826 else
827 release_OF_resource(chip->node, i);
828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 }
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700831#endif /* CONFIG_PPC64 */
832 if (chip->pdev)
833 pci_dev_put(chip->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 kfree(chip);
835 return 0;
836}
837
838
839/*
840 * free the device
841 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100842static int snd_pmac_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100844 struct snd_pmac *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return snd_pmac_free(chip);
846}
847
848
849/*
850 * check the machine support byteswap (little-endian)
851 */
852
Takashi Iwai65b29f52005-11-17 15:09:46 +0100853static void __init detect_byte_swap(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct device_node *mio;
856
857 /* if seems that Keylargo can't byte-swap */
858 for (mio = chip->node->parent; mio; mio = mio->parent) {
859 if (strcmp(mio->name, "mac-io") == 0) {
860 if (device_is_compatible(mio, "Keylargo"))
861 chip->can_byte_swap = 0;
862 break;
863 }
864 }
865
866 /* it seems the Pismo & iBook can't byte-swap in hardware. */
867 if (machine_is_compatible("PowerBook3,1") ||
868 machine_is_compatible("PowerBook2,1"))
869 chip->can_byte_swap = 0 ;
870
871 if (machine_is_compatible("PowerBook2,1"))
872 chip->can_duplex = 0;
873}
874
875
876/*
877 * detect a sound chip
878 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100879static int __init snd_pmac_detect(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Benjamin Herrenschmidt52180642005-05-18 16:31:51 +0200881 struct device_node *sound = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 unsigned int *prop, l;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700883 struct macio_chip* macio;
884
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -0700885 u32 layout_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 if (_machine != _MACH_Pmac)
888 return -ENODEV;
889
890 chip->subframe = 0;
891 chip->revision = 0;
892 chip->freqs_ok = 0xff; /* all ok */
893 chip->model = PMAC_AWACS;
894 chip->can_byte_swap = 1;
895 chip->can_duplex = 1;
896 chip->can_capture = 1;
897 chip->num_freqs = ARRAY_SIZE(awacs_freqs);
898 chip->freq_table = awacs_freqs;
899
900 chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
901
902 /* check machine type */
903 if (machine_is_compatible("AAPL,3400/2400")
904 || machine_is_compatible("AAPL,3500"))
905 chip->is_pbook_3400 = 1;
906 else if (machine_is_compatible("PowerBook1,1")
907 || machine_is_compatible("AAPL,PowerBook1998"))
908 chip->is_pbook_G3 = 1;
909 chip->node = find_devices("awacs");
910 if (chip->node)
Benjamin Herrenschmidt52180642005-05-18 16:31:51 +0200911 sound = chip->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 /*
914 * powermac G3 models have a node called "davbus"
915 * with a child called "sound".
916 */
Benjamin Herrenschmidt52180642005-05-18 16:31:51 +0200917 if (!chip->node)
918 chip->node = find_devices("davbus");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 /*
920 * if we didn't find a davbus device, try 'i2s-a' since
921 * this seems to be what iBooks have
922 */
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700923 if (! chip->node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 chip->node = find_devices("i2s-a");
Benjamin Herrenschmidt52180642005-05-18 16:31:51 +0200925 if (chip->node && chip->node->parent &&
926 chip->node->parent->parent) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700927 if (device_is_compatible(chip->node->parent->parent,
928 "K2-Keylargo"))
929 chip->is_k2 = 1;
930 }
931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (! chip->node)
933 return -ENODEV;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700934
Benjamin Herrenschmidt52180642005-05-18 16:31:51 +0200935 if (!sound) {
936 sound = find_devices("sound");
937 while (sound && sound->parent != chip->node)
938 sound = sound->next;
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (! sound)
941 return -ENODEV;
942 prop = (unsigned int *) get_property(sound, "sub-frame", NULL);
943 if (prop && *prop < 16)
944 chip->subframe = *prop;
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -0700945 prop = (unsigned int *) get_property(sound, "layout-id", NULL);
946 if (prop)
947 layout_id = *prop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /* This should be verified on older screamers */
949 if (device_is_compatible(sound, "screamer")) {
950 chip->model = PMAC_SCREAMER;
951 // chip->can_byte_swap = 0; /* FIXME: check this */
952 }
953 if (device_is_compatible(sound, "burgundy")) {
954 chip->model = PMAC_BURGUNDY;
955 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
956 }
957 if (device_is_compatible(sound, "daca")) {
958 chip->model = PMAC_DACA;
959 chip->can_capture = 0; /* no capture */
960 chip->can_duplex = 0;
961 // chip->can_byte_swap = 0; /* FIXME: check this */
962 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
963 }
964 if (device_is_compatible(sound, "tumbler")) {
965 chip->model = PMAC_TUMBLER;
966 chip->can_capture = 0; /* no capture */
967 chip->can_duplex = 0;
968 // chip->can_byte_swap = 0; /* FIXME: check this */
969 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
970 chip->freq_table = tumbler_freqs;
971 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
972 }
973 if (device_is_compatible(sound, "snapper")) {
974 chip->model = PMAC_SNAPPER;
975 // chip->can_byte_swap = 0; /* FIXME: check this */
976 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
977 chip->freq_table = tumbler_freqs;
978 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
979 }
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -0700980 if (device_is_compatible(sound, "AOAKeylargo") ||
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700981 device_is_compatible(sound, "AOAbase") ||
982 device_is_compatible(sound, "AOAK2")) {
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -0700983 /* For now, only support very basic TAS3004 based machines with
984 * single frequency until proper i2s control is implemented
985 */
986 switch(layout_id) {
987 case 0x48:
988 case 0x46:
989 case 0x33:
990 case 0x29:
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700991 case 0x24:
Takashi Iwai1e8bdca2005-09-19 15:21:17 +0200992 case 0x50:
Vincent Pelletier821690c2005-09-07 13:28:14 +0200993 case 0x5c:
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -0700994 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
995 chip->model = PMAC_SNAPPER;
996 chip->can_byte_swap = 0; /* FIXME: check this */
Benjamin Herrenschmidt1f7b49d2005-05-01 08:58:43 -0700997 chip->control_mask = MASK_IEPC | 0x11;/* disable IEE */
998 break;
999 case 0x3a:
1000 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
1001 chip->model = PMAC_TOONIE;
1002 chip->can_byte_swap = 0; /* FIXME: check this */
1003 chip->control_mask = MASK_IEPC | 0x11;/* disable IEE */
Benjamin Herrenschmidtb75550e2005-04-16 15:24:31 -07001004 break;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007 prop = (unsigned int *)get_property(sound, "device-id", NULL);
1008 if (prop)
1009 chip->device_id = *prop;
1010 chip->has_iic = (find_devices("perch") != NULL);
1011
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001012 /* We need the PCI device for DMA allocations, let's use a crude method
1013 * for now ...
1014 */
1015 macio = macio_find(chip->node, macio_unknown);
1016 if (macio == NULL)
1017 printk(KERN_WARNING "snd-powermac: can't locate macio !\n");
1018 else {
1019 struct pci_dev *pdev = NULL;
1020
1021 for_each_pci_dev(pdev) {
1022 struct device_node *np = pci_device_to_OF_node(pdev);
1023 if (np && np == macio->of_node) {
1024 chip->pdev = pdev;
1025 break;
1026 }
1027 }
1028 }
1029 if (chip->pdev == NULL)
Benjamin Herrenschmidt52180642005-05-18 16:31:51 +02001030 printk(KERN_WARNING "snd-powermac: can't locate macio PCI"
1031 " device !\n");
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 detect_byte_swap(chip);
1034
1035 /* look for a property saying what sample rates
1036 are available */
1037 prop = (unsigned int *) get_property(sound, "sample-rates", &l);
1038 if (! prop)
Benjamin Herrenschmidt52180642005-05-18 16:31:51 +02001039 prop = (unsigned int *) get_property(sound,
1040 "output-frame-rates", &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (prop) {
1042 int i;
1043 chip->freqs_ok = 0;
1044 for (l /= sizeof(int); l > 0; --l) {
1045 unsigned int r = *prop++;
1046 /* Apple 'Fixed' format */
1047 if (r >= 0x10000)
1048 r >>= 16;
1049 for (i = 0; i < chip->num_freqs; ++i) {
1050 if (r == chip->freq_table[i]) {
1051 chip->freqs_ok |= (1 << i);
1052 break;
1053 }
1054 }
1055 }
1056 } else {
1057 /* assume only 44.1khz */
1058 chip->freqs_ok = 1;
1059 }
1060
1061 return 0;
1062}
1063
1064/*
1065 * exported - boolean info callbacks for ease of programming
1066 */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001067int snd_pmac_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1068 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1071 uinfo->count = 2;
1072 uinfo->value.integer.min = 0;
1073 uinfo->value.integer.max = 1;
1074 return 0;
1075}
1076
Takashi Iwai65b29f52005-11-17 15:09:46 +01001077int snd_pmac_boolean_mono_info(struct snd_kcontrol *kcontrol,
1078 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1081 uinfo->count = 1;
1082 uinfo->value.integer.min = 0;
1083 uinfo->value.integer.max = 1;
1084 return 0;
1085}
1086
1087#ifdef PMAC_SUPPORT_AUTOMUTE
1088/*
1089 * auto-mute
1090 */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001091static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol,
1092 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001094 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 ucontrol->value.integer.value[0] = chip->auto_mute;
1096 return 0;
1097}
1098
Takashi Iwai65b29f52005-11-17 15:09:46 +01001099static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol,
1100 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001102 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1104 chip->auto_mute = ucontrol->value.integer.value[0];
1105 if (chip->update_automute)
1106 chip->update_automute(chip, 1);
1107 return 1;
1108 }
1109 return 0;
1110}
1111
Takashi Iwai65b29f52005-11-17 15:09:46 +01001112static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol,
1113 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001115 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (chip->detect_headphone)
1117 ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1118 else
1119 ucontrol->value.integer.value[0] = 0;
1120 return 0;
1121}
1122
Takashi Iwai65b29f52005-11-17 15:09:46 +01001123static struct snd_kcontrol_new auto_mute_controls[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1125 .name = "Auto Mute Switch",
1126 .info = snd_pmac_boolean_mono_info,
1127 .get = pmac_auto_mute_get,
1128 .put = pmac_auto_mute_put,
1129 },
1130 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1131 .name = "Headphone Detection",
1132 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1133 .info = snd_pmac_boolean_mono_info,
1134 .get = pmac_hp_detect_get,
1135 },
1136};
1137
Takashi Iwai65b29f52005-11-17 15:09:46 +01001138int __init snd_pmac_add_automute(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 int err;
1141 chip->auto_mute = 1;
1142 err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001143 if (err < 0) {
1144 printk(KERN_ERR "snd-powermac: Failed to add automute control\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return err;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1148 return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1149}
1150#endif /* PMAC_SUPPORT_AUTOMUTE */
1151
1152/*
1153 * create and detect a pmac chip record
1154 */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001155int __init snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001157 struct snd_pmac *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct device_node *np;
1159 int i, err;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001160 unsigned long ctrl_addr, txdma_addr, rxdma_addr;
Takashi Iwai65b29f52005-11-17 15:09:46 +01001161 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 .dev_free = snd_pmac_dev_free,
1163 };
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 *chip_return = NULL;
1166
Takashi Iwai561b2202005-09-09 14:22:34 +02001167 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (chip == NULL)
1169 return -ENOMEM;
1170 chip->card = card;
1171
1172 spin_lock_init(&chip->reg_lock);
1173 chip->irq = chip->tx_irq = chip->rx_irq = -1;
1174
1175 chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1176 chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1177
1178 if ((err = snd_pmac_detect(chip)) < 0)
1179 goto __error;
1180
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001181 if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1182 snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1183 snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 err = -ENOMEM;
1185 goto __error;
1186 }
1187
1188 np = chip->node;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001189 if (chip->is_k2) {
1190 if (np->parent->n_addrs < 2 || np->n_intrs < 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 err = -ENODEV;
1192 goto __error;
1193 }
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001194 for (i = 0; i < 2; i++) {
1195#ifndef CONFIG_PPC64
1196 static char *name[2] = { "- Control", "- DMA" };
1197 if (! request_OF_resource(np->parent, i, name[i])) {
1198 snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
1199 err = -ENODEV;
1200 goto __error;
1201 }
1202 chip->of_requested |= (1 << i);
1203#endif /* CONFIG_PPC64 */
1204 ctrl_addr = np->parent->addrs[0].address;
1205 txdma_addr = np->parent->addrs[1].address;
1206 rxdma_addr = txdma_addr + 0x100;
1207 }
1208
1209 } else {
1210 if (np->n_addrs < 3 || np->n_intrs < 3) {
1211 err = -ENODEV;
1212 goto __error;
1213 }
1214
1215 for (i = 0; i < 3; i++) {
1216#ifndef CONFIG_PPC64
1217 static char *name[3] = { "- Control", "- Tx DMA", "- Rx DMA" };
1218 if (! request_OF_resource(np, i, name[i])) {
1219 snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
1220 err = -ENODEV;
1221 goto __error;
1222 }
1223 chip->of_requested |= (1 << i);
1224#endif /* CONFIG_PPC64 */
1225 ctrl_addr = np->addrs[0].address;
1226 txdma_addr = np->addrs[1].address;
1227 rxdma_addr = np->addrs[2].address;
1228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001231 chip->awacs = ioremap(ctrl_addr, 0x1000);
1232 chip->playback.dma = ioremap(txdma_addr, 0x100);
1233 chip->capture.dma = ioremap(rxdma_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (chip->model <= PMAC_BURGUNDY) {
1235 if (request_irq(np->intrs[0].line, snd_pmac_ctrl_intr, 0,
1236 "PMac", (void*)chip)) {
1237 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[0].line);
1238 err = -EBUSY;
1239 goto __error;
1240 }
1241 chip->irq = np->intrs[0].line;
1242 }
1243 if (request_irq(np->intrs[1].line, snd_pmac_tx_intr, 0,
1244 "PMac Output", (void*)chip)) {
1245 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[1].line);
1246 err = -EBUSY;
1247 goto __error;
1248 }
1249 chip->tx_irq = np->intrs[1].line;
1250 if (request_irq(np->intrs[2].line, snd_pmac_rx_intr, 0,
1251 "PMac Input", (void*)chip)) {
1252 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[2].line);
1253 err = -EBUSY;
1254 goto __error;
1255 }
1256 chip->rx_irq = np->intrs[2].line;
1257
1258 snd_pmac_sound_feature(chip, 1);
1259
1260 /* reset */
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001261 if (chip->model == PMAC_AWACS)
1262 out_le32(&chip->awacs->control, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /* Powerbooks have odd ways of enabling inputs such as
1265 an expansion-bay CD or sound from an internal modem
1266 or a PC-card modem. */
1267 if (chip->is_pbook_3400) {
1268 /* Enable CD and PC-card sound inputs. */
1269 /* This is done by reading from address
1270 * f301a000, + 0x10 to enable the expansion-bay
1271 * CD sound input, + 0x80 to enable the PC-card
1272 * sound input. The 0x100 enables the SCSI bus
1273 * terminator power.
1274 */
1275 chip->latch_base = ioremap (0xf301a000, 0x1000);
1276 in_8(chip->latch_base + 0x190);
1277 } else if (chip->is_pbook_G3) {
1278 struct device_node* mio;
1279 for (mio = chip->node->parent; mio; mio = mio->parent) {
1280 if (strcmp(mio->name, "mac-io") == 0
1281 && mio->n_addrs > 0) {
1282 chip->macio_base = ioremap(mio->addrs[0].address, 0x40);
1283 break;
1284 }
1285 }
1286 /* Enable CD sound input. */
1287 /* The relevant bits for writing to this byte are 0x8f.
1288 * I haven't found out what the 0x80 bit does.
1289 * For the 0xf bits, writing 3 or 7 enables the CD
1290 * input, any other value disables it. Values
1291 * 1, 3, 5, 7 enable the microphone. Values 0, 2,
1292 * 4, 6, 8 - f enable the input from the modem.
1293 */
1294 if (chip->macio_base)
1295 out_8(chip->macio_base + 0x37, 3);
1296 }
1297
1298 /* Reset dbdma channels */
1299 snd_pmac_dbdma_reset(chip);
1300
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001301#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /* add sleep notifier */
1303 if (! snd_pmac_register_sleep_notifier(chip))
1304 snd_card_set_pm_callback(chip->card, snd_pmac_suspend, snd_pmac_resume, chip);
1305#endif
1306
1307 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1308 goto __error;
1309
1310 *chip_return = chip;
1311 return 0;
1312
1313 __error:
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001314 if (chip->pdev)
1315 pci_dev_put(chip->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 snd_pmac_free(chip);
1317 return err;
1318}
1319
1320
1321/*
1322 * sleep notify for powerbook
1323 */
1324
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001325#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327/*
1328 * Save state when going to sleep, restore it afterwards.
1329 */
1330
Takashi Iwai65b29f52005-11-17 15:09:46 +01001331static int snd_pmac_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001333 struct snd_pmac *chip = card->pm_private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 unsigned long flags;
1335
1336 if (chip->suspend)
1337 chip->suspend(chip);
1338 snd_pcm_suspend_all(chip->pcm);
1339 spin_lock_irqsave(&chip->reg_lock, flags);
1340 snd_pmac_beep_stop(chip);
1341 spin_unlock_irqrestore(&chip->reg_lock, flags);
1342 if (chip->irq >= 0)
1343 disable_irq(chip->irq);
1344 if (chip->tx_irq >= 0)
1345 disable_irq(chip->tx_irq);
1346 if (chip->rx_irq >= 0)
1347 disable_irq(chip->rx_irq);
1348 snd_pmac_sound_feature(chip, 0);
1349 return 0;
1350}
1351
Takashi Iwai65b29f52005-11-17 15:09:46 +01001352static int snd_pmac_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001354 struct snd_pmac *chip = card->pm_private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 snd_pmac_sound_feature(chip, 1);
1357 if (chip->resume)
1358 chip->resume(chip);
1359 /* enable CD sound input */
1360 if (chip->macio_base && chip->is_pbook_G3) {
1361 out_8(chip->macio_base + 0x37, 3);
1362 } else if (chip->is_pbook_3400) {
1363 in_8(chip->latch_base + 0x190);
1364 }
1365
1366 snd_pmac_pcm_set_format(chip);
1367
1368 if (chip->irq >= 0)
1369 enable_irq(chip->irq);
1370 if (chip->tx_irq >= 0)
1371 enable_irq(chip->tx_irq);
1372 if (chip->rx_irq >= 0)
1373 enable_irq(chip->rx_irq);
1374
1375 return 0;
1376}
1377
1378/* the chip is stored statically by snd_pmac_register_sleep_notifier
1379 * because we can't have any private data for notify callback.
1380 */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001381static struct snd_pmac *sleeping_pmac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383static int snd_pmac_sleep_notify(struct pmu_sleep_notifier *self, int when)
1384{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001385 struct snd_pmac *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 chip = sleeping_pmac;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02001388 if (! chip)
1389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 switch (when) {
1392 case PBOOK_SLEEP_NOW:
1393 snd_pmac_suspend(chip->card, PMSG_SUSPEND);
1394 break;
1395 case PBOOK_WAKE:
1396 snd_pmac_resume(chip->card);
1397 break;
1398 }
1399 return PBOOK_SLEEP_OK;
1400}
1401
1402static struct pmu_sleep_notifier snd_pmac_sleep_notifier = {
1403 snd_pmac_sleep_notify, SLEEP_LEVEL_SOUND,
1404};
1405
Takashi Iwai65b29f52005-11-17 15:09:46 +01001406static int __init snd_pmac_register_sleep_notifier(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 /* should be protected here.. */
1409 snd_assert(! sleeping_pmac, return -EBUSY);
1410 sleeping_pmac = chip;
1411 pmu_register_sleep_notifier(&snd_pmac_sleep_notifier);
1412 return 0;
1413}
1414
Takashi Iwai65b29f52005-11-17 15:09:46 +01001415static int snd_pmac_unregister_sleep_notifier(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 /* should be protected here.. */
1418 snd_assert(sleeping_pmac == chip, return -ENODEV);
1419 pmu_unregister_sleep_notifier(&snd_pmac_sleep_notifier);
1420 sleeping_pmac = NULL;
1421 return 0;
1422}
1423
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001424#endif /* CONFIG_PM */
1425