blob: 04b95ae5c3aa0ddf864e2cd17da4729220f5aa06 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
40static int awacs_freqs[8] = {
41 44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
42};
43/* fixed frequency table for tumbler */
44static int tumbler_freqs[1] = {
45 44100
46};
47
48/*
49 * allocate DBDMA command arrays
50 */
Takashi Iwai65b29f52005-11-17 15:09:46 +010051static int snd_pmac_dbdma_alloc(struct snd_pmac *chip, struct pmac_dbdma *rec, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070053 unsigned int rsize = sizeof(struct dbdma_cmd) * (size + 1);
54
55 rec->space = dma_alloc_coherent(&chip->pdev->dev, rsize,
56 &rec->dma_base, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (rec->space == NULL)
58 return -ENOMEM;
59 rec->size = size;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070060 memset(rec->space, 0, rsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 rec->cmds = (void __iomem *)DBDMA_ALIGN(rec->space);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070062 rec->addr = rec->dma_base + (unsigned long)((char *)rec->cmds - (char *)rec->space);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return 0;
65}
66
Takashi Iwai65b29f52005-11-17 15:09:46 +010067static void snd_pmac_dbdma_free(struct snd_pmac *chip, struct pmac_dbdma *rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Benjamin Herrenschmidt367636e2006-02-08 15:04:18 +110069 if (rec->space) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -070070 unsigned int rsize = sizeof(struct dbdma_cmd) * (rec->size + 1);
71
72 dma_free_coherent(&chip->pdev->dev, rsize, rec->space, rec->dma_base);
73 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76
77/*
78 * pcm stuff
79 */
80
81/*
82 * look up frequency table
83 */
84
Takashi Iwai65b29f52005-11-17 15:09:46 +010085unsigned int snd_pmac_rate_index(struct snd_pmac *chip, struct pmac_stream *rec, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 int i, ok, found;
88
89 ok = rec->cur_freqs;
90 if (rate > chip->freq_table[0])
91 return 0;
92 found = 0;
93 for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
94 if (! (ok & 1)) continue;
95 found = i;
96 if (rate >= chip->freq_table[i])
97 break;
98 }
99 return found;
100}
101
102/*
103 * check whether another stream is active
104 */
105static inline int another_stream(int stream)
106{
107 return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
108 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
109}
110
111/*
112 * allocate buffers
113 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100114static int snd_pmac_pcm_hw_params(struct snd_pcm_substream *subs,
115 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
118}
119
120/*
121 * release buffers
122 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100123static int snd_pmac_pcm_hw_free(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 snd_pcm_lib_free_pages(subs);
126 return 0;
127}
128
129/*
130 * get a stream of the opposite direction
131 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100132static struct pmac_stream *snd_pmac_get_stream(struct snd_pmac *chip, int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 switch (stream) {
135 case SNDRV_PCM_STREAM_PLAYBACK:
136 return &chip->playback;
137 case SNDRV_PCM_STREAM_CAPTURE:
138 return &chip->capture;
139 default:
140 snd_BUG();
141 return NULL;
142 }
143}
144
145/*
146 * wait while run status is on
147 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700148static inline void
Takashi Iwai65b29f52005-11-17 15:09:46 +0100149snd_pmac_wait_ack(struct pmac_stream *rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 int timeout = 50000;
152 while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
153 udelay(1);
154}
155
156/*
157 * set the format and rate to the chip.
158 * call the lowlevel function if defined (e.g. for AWACS).
159 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100160static void snd_pmac_pcm_set_format(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 /* set up frequency and format */
163 out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
164 out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
165 if (chip->set_format)
166 chip->set_format(chip);
167}
168
169/*
170 * stop the DMA transfer
171 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100172static inline void snd_pmac_dma_stop(struct pmac_stream *rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
175 snd_pmac_wait_ack(rec);
176}
177
178/*
179 * set the command pointer address
180 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100181static inline void snd_pmac_dma_set_command(struct pmac_stream *rec, struct pmac_dbdma *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 out_le32(&rec->dma->cmdptr, cmd->addr);
184}
185
186/*
187 * start the DMA
188 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100189static inline void snd_pmac_dma_run(struct pmac_stream *rec, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 out_le32(&rec->dma->control, status | (status << 16));
192}
193
194
195/*
196 * prepare playback/capture stream
197 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100198static 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 -0700199{
200 int i;
201 volatile struct dbdma_cmd __iomem *cp;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100202 struct snd_pcm_runtime *runtime = subs->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int rate_index;
204 long offset;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100205 struct pmac_stream *astr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
208 rec->period_size = snd_pcm_lib_period_bytes(subs);
209 rec->nperiods = rec->dma_size / rec->period_size;
210 rec->cur_period = 0;
211 rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
212
213 /* set up constraints */
214 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200215 if (! astr)
216 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 astr->cur_freqs = 1 << rate_index;
218 astr->cur_formats = 1 << runtime->format;
219 chip->rate_index = rate_index;
220 chip->format = runtime->format;
221
222 /* We really want to execute a DMA stop command, after the AWACS
223 * is initialized.
224 * For reasons I don't understand, it stops the hissing noise
225 * common to many PowerBook G3 systems and random noise otherwise
226 * captured on iBook2's about every third time. -ReneR
227 */
228 spin_lock_irq(&chip->reg_lock);
229 snd_pmac_dma_stop(rec);
230 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
231 snd_pmac_dma_set_command(rec, &chip->extra_dma);
232 snd_pmac_dma_run(rec, RUN);
233 spin_unlock_irq(&chip->reg_lock);
234 mdelay(5);
235 spin_lock_irq(&chip->reg_lock);
236 /* continuous DMA memory type doesn't provide the physical address,
237 * so we need to resolve the address here...
238 */
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700239 offset = runtime->dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) {
241 st_le32(&cp->phy_addr, offset);
242 st_le16(&cp->req_count, rec->period_size);
243 /*st_le16(&cp->res_count, 0);*/
244 st_le16(&cp->xfer_status, 0);
245 offset += rec->period_size;
246 }
247 /* make loop */
248 st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
249 st_le32(&cp->cmd_dep, rec->cmd.addr);
250
251 snd_pmac_dma_stop(rec);
252 snd_pmac_dma_set_command(rec, &rec->cmd);
253 spin_unlock_irq(&chip->reg_lock);
254
255 return 0;
256}
257
258
259/*
260 * PCM trigger/stop
261 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100262static int snd_pmac_pcm_trigger(struct snd_pmac *chip, struct pmac_stream *rec,
263 struct snd_pcm_substream *subs, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 volatile struct dbdma_cmd __iomem *cp;
266 int i, command;
267
268 switch (cmd) {
269 case SNDRV_PCM_TRIGGER_START:
270 case SNDRV_PCM_TRIGGER_RESUME:
271 if (rec->running)
272 return -EBUSY;
273 command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
274 OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
275 spin_lock(&chip->reg_lock);
276 snd_pmac_beep_stop(chip);
277 snd_pmac_pcm_set_format(chip);
278 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
279 out_le16(&cp->command, command);
280 snd_pmac_dma_set_command(rec, &rec->cmd);
281 (void)in_le32(&rec->dma->status);
282 snd_pmac_dma_run(rec, RUN|WAKE);
283 rec->running = 1;
284 spin_unlock(&chip->reg_lock);
285 break;
286
287 case SNDRV_PCM_TRIGGER_STOP:
288 case SNDRV_PCM_TRIGGER_SUSPEND:
289 spin_lock(&chip->reg_lock);
290 rec->running = 0;
291 /*printk("stopped!!\n");*/
292 snd_pmac_dma_stop(rec);
293 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
294 out_le16(&cp->command, DBDMA_STOP);
295 spin_unlock(&chip->reg_lock);
296 break;
297
298 default:
299 return -EINVAL;
300 }
301
302 return 0;
303}
304
305/*
306 * return the current pointer
307 */
308inline
Takashi Iwai65b29f52005-11-17 15:09:46 +0100309static snd_pcm_uframes_t snd_pmac_pcm_pointer(struct snd_pmac *chip,
310 struct pmac_stream *rec,
311 struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 int count = 0;
314
315#if 1 /* hmm.. how can we get the current dma pointer?? */
316 int stat;
317 volatile struct dbdma_cmd __iomem *cp = &rec->cmd.cmds[rec->cur_period];
318 stat = ld_le16(&cp->xfer_status);
319 if (stat & (ACTIVE|DEAD)) {
320 count = in_le16(&cp->res_count);
321 if (count)
322 count = rec->period_size - count;
323 }
324#endif
325 count += rec->cur_period * rec->period_size;
326 /*printk("pointer=%d\n", count);*/
327 return bytes_to_frames(subs->runtime, count);
328}
329
330/*
331 * playback
332 */
333
Takashi Iwai65b29f52005-11-17 15:09:46 +0100334static int snd_pmac_playback_prepare(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100336 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
338}
339
Takashi Iwai65b29f52005-11-17 15:09:46 +0100340static int snd_pmac_playback_trigger(struct snd_pcm_substream *subs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int cmd)
342{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100343 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
345}
346
Takashi Iwai65b29f52005-11-17 15:09:46 +0100347static snd_pcm_uframes_t snd_pmac_playback_pointer(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100349 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
351}
352
353
354/*
355 * capture
356 */
357
Takashi Iwai65b29f52005-11-17 15:09:46 +0100358static int snd_pmac_capture_prepare(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100360 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
362}
363
Takashi Iwai65b29f52005-11-17 15:09:46 +0100364static int snd_pmac_capture_trigger(struct snd_pcm_substream *subs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int cmd)
366{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100367 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
369}
370
Takashi Iwai65b29f52005-11-17 15:09:46 +0100371static snd_pcm_uframes_t snd_pmac_capture_pointer(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100373 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
375}
376
377
378/*
379 * update playback/capture pointer from interrupts
380 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100381static void snd_pmac_pcm_update(struct snd_pmac *chip, struct pmac_stream *rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 volatile struct dbdma_cmd __iomem *cp;
384 int c;
385 int stat;
386
387 spin_lock(&chip->reg_lock);
388 if (rec->running) {
389 cp = &rec->cmd.cmds[rec->cur_period];
390 for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */
391 stat = ld_le16(&cp->xfer_status);
392 if (! (stat & ACTIVE))
393 break;
394 /*printk("update frag %d\n", rec->cur_period);*/
395 st_le16(&cp->xfer_status, 0);
396 st_le16(&cp->req_count, rec->period_size);
397 /*st_le16(&cp->res_count, 0);*/
398 rec->cur_period++;
399 if (rec->cur_period >= rec->nperiods) {
400 rec->cur_period = 0;
401 cp = rec->cmd.cmds;
402 } else
403 cp++;
404 spin_unlock(&chip->reg_lock);
405 snd_pcm_period_elapsed(rec->substream);
406 spin_lock(&chip->reg_lock);
407 }
408 }
409 spin_unlock(&chip->reg_lock);
410}
411
412
413/*
414 * hw info
415 */
416
Takashi Iwai65b29f52005-11-17 15:09:46 +0100417static struct snd_pcm_hardware snd_pmac_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 .info = (SNDRV_PCM_INFO_INTERLEAVED |
420 SNDRV_PCM_INFO_MMAP |
421 SNDRV_PCM_INFO_MMAP_VALID |
422 SNDRV_PCM_INFO_RESUME),
423 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
424 .rates = SNDRV_PCM_RATE_8000_44100,
425 .rate_min = 7350,
426 .rate_max = 44100,
427 .channels_min = 2,
428 .channels_max = 2,
429 .buffer_bytes_max = 131072,
430 .period_bytes_min = 256,
431 .period_bytes_max = 16384,
432 .periods_min = 3,
433 .periods_max = PMAC_MAX_FRAGS,
434};
435
Takashi Iwai65b29f52005-11-17 15:09:46 +0100436static struct snd_pcm_hardware snd_pmac_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 .info = (SNDRV_PCM_INFO_INTERLEAVED |
439 SNDRV_PCM_INFO_MMAP |
440 SNDRV_PCM_INFO_MMAP_VALID |
441 SNDRV_PCM_INFO_RESUME),
442 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
443 .rates = SNDRV_PCM_RATE_8000_44100,
444 .rate_min = 7350,
445 .rate_max = 44100,
446 .channels_min = 2,
447 .channels_max = 2,
448 .buffer_bytes_max = 131072,
449 .period_bytes_min = 256,
450 .period_bytes_max = 16384,
451 .periods_min = 3,
452 .periods_max = PMAC_MAX_FRAGS,
453};
454
455
456#if 0 // NYI
Takashi Iwai65b29f52005-11-17 15:09:46 +0100457static int snd_pmac_hw_rule_rate(struct snd_pcm_hw_params *params,
458 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100460 struct snd_pmac *chip = rule->private;
461 struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int i, freq_table[8], num_freqs;
463
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200464 if (! rec)
465 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 num_freqs = 0;
467 for (i = chip->num_freqs - 1; i >= 0; i--) {
468 if (rec->cur_freqs & (1 << i))
469 freq_table[num_freqs++] = chip->freq_table[i];
470 }
471
472 return snd_interval_list(hw_param_interval(params, rule->var),
473 num_freqs, freq_table, 0);
474}
475
Takashi Iwai65b29f52005-11-17 15:09:46 +0100476static int snd_pmac_hw_rule_format(struct snd_pcm_hw_params *params,
477 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100479 struct snd_pmac *chip = rule->private;
480 struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200482 if (! rec)
483 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
485 rec->cur_formats);
486}
487#endif // NYI
488
Takashi Iwai65b29f52005-11-17 15:09:46 +0100489static int snd_pmac_pcm_open(struct snd_pmac *chip, struct pmac_stream *rec,
490 struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100492 struct snd_pcm_runtime *runtime = subs->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 int i, j, fflags;
494 static int typical_freqs[] = {
495 44100,
496 22050,
497 11025,
498 0,
499 };
500 static int typical_freq_flags[] = {
501 SNDRV_PCM_RATE_44100,
502 SNDRV_PCM_RATE_22050,
503 SNDRV_PCM_RATE_11025,
504 0,
505 };
506
507 /* look up frequency table and fill bit mask */
508 runtime->hw.rates = 0;
509 fflags = chip->freqs_ok;
510 for (i = 0; typical_freqs[i]; i++) {
511 for (j = 0; j < chip->num_freqs; j++) {
512 if ((chip->freqs_ok & (1 << j)) &&
513 chip->freq_table[j] == typical_freqs[i]) {
514 runtime->hw.rates |= typical_freq_flags[i];
515 fflags &= ~(1 << j);
516 break;
517 }
518 }
519 }
520 if (fflags) /* rest */
521 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
522
523 /* check for minimum and maximum rates */
524 for (i = 0; i < chip->num_freqs; i++) {
525 if (chip->freqs_ok & (1 << i)) {
526 runtime->hw.rate_max = chip->freq_table[i];
527 break;
528 }
529 }
530 for (i = chip->num_freqs - 1; i >= 0; i--) {
531 if (chip->freqs_ok & (1 << i)) {
532 runtime->hw.rate_min = chip->freq_table[i];
533 break;
534 }
535 }
536 runtime->hw.formats = chip->formats_ok;
537 if (chip->can_capture) {
538 if (! chip->can_duplex)
539 runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
540 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
541 }
542 runtime->private_data = rec;
543 rec->substream = subs;
544
545#if 0 /* FIXME: still under development.. */
546 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
547 snd_pmac_hw_rule_rate, chip, rec->stream, -1);
548 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
549 snd_pmac_hw_rule_format, chip, rec->stream, -1);
550#endif
551
552 runtime->hw.periods_max = rec->cmd.size - 1;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* constraints to fix choppy sound */
555 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
556 return 0;
557}
558
Takashi Iwai65b29f52005-11-17 15:09:46 +0100559static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec,
560 struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100562 struct pmac_stream *astr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 snd_pmac_dma_stop(rec);
565
566 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200567 if (! astr)
568 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* reset constraints */
571 astr->cur_freqs = chip->freqs_ok;
572 astr->cur_formats = chip->formats_ok;
573
574 return 0;
575}
576
Takashi Iwai65b29f52005-11-17 15:09:46 +0100577static int snd_pmac_playback_open(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100579 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 subs->runtime->hw = snd_pmac_playback;
582 return snd_pmac_pcm_open(chip, &chip->playback, subs);
583}
584
Takashi Iwai65b29f52005-11-17 15:09:46 +0100585static int snd_pmac_capture_open(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100587 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 subs->runtime->hw = snd_pmac_capture;
590 return snd_pmac_pcm_open(chip, &chip->capture, subs);
591}
592
Takashi Iwai65b29f52005-11-17 15:09:46 +0100593static int snd_pmac_playback_close(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100595 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 return snd_pmac_pcm_close(chip, &chip->playback, subs);
598}
599
Takashi Iwai65b29f52005-11-17 15:09:46 +0100600static int snd_pmac_capture_close(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100602 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 return snd_pmac_pcm_close(chip, &chip->capture, subs);
605}
606
607/*
608 */
609
Takashi Iwai65b29f52005-11-17 15:09:46 +0100610static struct snd_pcm_ops snd_pmac_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 .open = snd_pmac_playback_open,
612 .close = snd_pmac_playback_close,
613 .ioctl = snd_pcm_lib_ioctl,
614 .hw_params = snd_pmac_pcm_hw_params,
615 .hw_free = snd_pmac_pcm_hw_free,
616 .prepare = snd_pmac_playback_prepare,
617 .trigger = snd_pmac_playback_trigger,
618 .pointer = snd_pmac_playback_pointer,
619};
620
Takashi Iwai65b29f52005-11-17 15:09:46 +0100621static struct snd_pcm_ops snd_pmac_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 .open = snd_pmac_capture_open,
623 .close = snd_pmac_capture_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_capture_prepare,
628 .trigger = snd_pmac_capture_trigger,
629 .pointer = snd_pmac_capture_pointer,
630};
631
Takashi Iwai65b29f52005-11-17 15:09:46 +0100632int __init snd_pmac_pcm_new(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100634 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int err;
636 int num_captures = 1;
637
638 if (! chip->can_capture)
639 num_captures = 0;
640 err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
641 if (err < 0)
642 return err;
643
644 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
645 if (chip->can_capture)
646 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
647
648 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
650 strcpy(pcm->name, chip->card->shortname);
651 chip->pcm = pcm;
652
653 chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
654 if (chip->can_byte_swap)
655 chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
656
657 chip->playback.cur_formats = chip->formats_ok;
658 chip->capture.cur_formats = chip->formats_ok;
659 chip->playback.cur_freqs = chip->freqs_ok;
660 chip->capture.cur_freqs = chip->freqs_ok;
661
662 /* preallocate 64k buffer */
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700663 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
664 &chip->pdev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 64 * 1024, 64 * 1024);
666
667 return 0;
668}
669
670
Takashi Iwai65b29f52005-11-17 15:09:46 +0100671static void snd_pmac_dbdma_reset(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
674 snd_pmac_wait_ack(&chip->playback);
675 out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
676 snd_pmac_wait_ack(&chip->capture);
677}
678
679
680/*
681 * handling beep
682 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100683void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100685 struct pmac_stream *rec = &chip->playback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 snd_pmac_dma_stop(rec);
688 st_le16(&chip->extra_dma.cmds->req_count, bytes);
689 st_le16(&chip->extra_dma.cmds->xfer_status, 0);
690 st_le32(&chip->extra_dma.cmds->cmd_dep, chip->extra_dma.addr);
691 st_le32(&chip->extra_dma.cmds->phy_addr, addr);
692 st_le16(&chip->extra_dma.cmds->command, OUTPUT_MORE + BR_ALWAYS);
693 out_le32(&chip->awacs->control,
694 (in_le32(&chip->awacs->control) & ~0x1f00)
695 | (speed << 8));
696 out_le32(&chip->awacs->byteswap, 0);
697 snd_pmac_dma_set_command(rec, &chip->extra_dma);
698 snd_pmac_dma_run(rec, RUN);
699}
700
Takashi Iwai65b29f52005-11-17 15:09:46 +0100701void snd_pmac_beep_dma_stop(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 snd_pmac_dma_stop(&chip->playback);
704 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
705 snd_pmac_pcm_set_format(chip); /* reset format */
706}
707
708
709/*
710 * interrupt handlers
711 */
712static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100713snd_pmac_tx_intr(int irq, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100715 struct snd_pmac *chip = devid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 snd_pmac_pcm_update(chip, &chip->playback);
717 return IRQ_HANDLED;
718}
719
720
721static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100722snd_pmac_rx_intr(int irq, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100724 struct snd_pmac *chip = devid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 snd_pmac_pcm_update(chip, &chip->capture);
726 return IRQ_HANDLED;
727}
728
729
730static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100731snd_pmac_ctrl_intr(int irq, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100733 struct snd_pmac *chip = devid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int ctrl = in_le32(&chip->awacs->control);
735
736 /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
737 if (ctrl & MASK_PORTCHG) {
738 /* do something when headphone is plugged/unplugged? */
739 if (chip->update_automute)
740 chip->update_automute(chip, 1);
741 }
742 if (ctrl & MASK_CNTLERR) {
743 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
744 if (err && chip->model <= PMAC_SCREAMER)
745 snd_printk(KERN_DEBUG "error %x\n", err);
746 }
747 /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
748 out_le32(&chip->awacs->control, ctrl);
749 return IRQ_HANDLED;
750}
751
752
753/*
754 * a wrapper to feature call for compatibility
755 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100756static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
David Woodhouse4e6a06e2005-08-17 11:36:35 +0100758 if (ppc_md.feature_call)
759 ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762/*
763 * release resources
764 */
765
Takashi Iwai65b29f52005-11-17 15:09:46 +0100766static int snd_pmac_free(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* stop sounds */
769 if (chip->initialized) {
770 snd_pmac_dbdma_reset(chip);
771 /* disable interrupts from awacs interface */
772 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
773 }
774
Benjamin Herrenschmidt41e904d2007-06-19 14:37:39 +1000775 if (chip->node)
776 snd_pmac_sound_feature(chip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 /* clean up mixer if any */
779 if (chip->mixer_free)
780 chip->mixer_free(chip);
781
782 snd_pmac_detach_beep(chip);
783
784 /* release resources */
785 if (chip->irq >= 0)
786 free_irq(chip->irq, (void*)chip);
787 if (chip->tx_irq >= 0)
788 free_irq(chip->tx_irq, (void*)chip);
789 if (chip->rx_irq >= 0)
790 free_irq(chip->rx_irq, (void*)chip);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700791 snd_pmac_dbdma_free(chip, &chip->playback.cmd);
792 snd_pmac_dbdma_free(chip, &chip->capture.cmd);
793 snd_pmac_dbdma_free(chip, &chip->extra_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (chip->macio_base)
795 iounmap(chip->macio_base);
796 if (chip->latch_base)
797 iounmap(chip->latch_base);
798 if (chip->awacs)
799 iounmap(chip->awacs);
800 if (chip->playback.dma)
801 iounmap(chip->playback.dma);
802 if (chip->capture.dma)
803 iounmap(chip->capture.dma);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (chip->node) {
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700806 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 for (i = 0; i < 3; i++) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100808 if (chip->requested & (1 << i))
809 release_mem_region(chip->rsrc[i].start,
810 chip->rsrc[i].end -
811 chip->rsrc[i].start + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813 }
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100814
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700815 if (chip->pdev)
816 pci_dev_put(chip->pdev);
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000817 of_node_put(chip->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 kfree(chip);
819 return 0;
820}
821
822
823/*
824 * free the device
825 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100826static int snd_pmac_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100828 struct snd_pmac *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return snd_pmac_free(chip);
830}
831
832
833/*
834 * check the machine support byteswap (little-endian)
835 */
836
Takashi Iwai65b29f52005-11-17 15:09:46 +0100837static void __init detect_byte_swap(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 struct device_node *mio;
840
841 /* if seems that Keylargo can't byte-swap */
842 for (mio = chip->node->parent; mio; mio = mio->parent) {
843 if (strcmp(mio->name, "mac-io") == 0) {
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000844 if (of_device_is_compatible(mio, "Keylargo"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 chip->can_byte_swap = 0;
846 break;
847 }
848 }
849
850 /* it seems the Pismo & iBook can't byte-swap in hardware. */
851 if (machine_is_compatible("PowerBook3,1") ||
852 machine_is_compatible("PowerBook2,1"))
853 chip->can_byte_swap = 0 ;
854
855 if (machine_is_compatible("PowerBook2,1"))
856 chip->can_duplex = 0;
857}
858
859
860/*
861 * detect a sound chip
862 */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100863static int __init snd_pmac_detect(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000865 struct device_node *sound;
866 struct device_node *dn;
Stephen Rothwellc4f55b32007-04-03 22:39:14 +1000867 const unsigned int *prop;
868 unsigned int l;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700869 struct macio_chip* macio;
870
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100871 if (!machine_is(powermac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return -ENODEV;
873
874 chip->subframe = 0;
875 chip->revision = 0;
876 chip->freqs_ok = 0xff; /* all ok */
877 chip->model = PMAC_AWACS;
878 chip->can_byte_swap = 1;
879 chip->can_duplex = 1;
880 chip->can_capture = 1;
881 chip->num_freqs = ARRAY_SIZE(awacs_freqs);
882 chip->freq_table = awacs_freqs;
Benjamin Herrenschmidt367636e2006-02-08 15:04:18 +1100883 chip->pdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
886
887 /* check machine type */
888 if (machine_is_compatible("AAPL,3400/2400")
889 || machine_is_compatible("AAPL,3500"))
890 chip->is_pbook_3400 = 1;
891 else if (machine_is_compatible("PowerBook1,1")
892 || machine_is_compatible("AAPL,PowerBook1998"))
893 chip->is_pbook_G3 = 1;
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000894 chip->node = of_find_node_by_name(NULL, "awacs");
895 sound = of_node_get(chip->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /*
898 * powermac G3 models have a node called "davbus"
899 * with a child called "sound".
900 */
Benjamin Herrenschmidt52180642005-05-18 16:31:51 +0200901 if (!chip->node)
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000902 chip->node = of_find_node_by_name(NULL, "davbus");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /*
904 * if we didn't find a davbus device, try 'i2s-a' since
905 * this seems to be what iBooks have
906 */
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700907 if (! chip->node) {
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000908 chip->node = of_find_node_by_name(NULL, "i2s-a");
Benjamin Herrenschmidt52180642005-05-18 16:31:51 +0200909 if (chip->node && chip->node->parent &&
910 chip->node->parent->parent) {
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000911 if (of_device_is_compatible(chip->node->parent->parent,
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700912 "K2-Keylargo"))
913 chip->is_k2 = 1;
914 }
915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (! chip->node)
917 return -ENODEV;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700918
Benjamin Herrenschmidt52180642005-05-18 16:31:51 +0200919 if (!sound) {
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000920 sound = of_find_node_by_name(NULL, "sound");
Benjamin Herrenschmidt52180642005-05-18 16:31:51 +0200921 while (sound && sound->parent != chip->node)
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000922 sound = of_find_node_by_name(sound, "sound");
Benjamin Herrenschmidt52180642005-05-18 16:31:51 +0200923 }
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000924 if (! sound) {
925 of_node_put(chip->node);
Benjamin Herrenschmidt41e904d2007-06-19 14:37:39 +1000926 chip->node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return -ENODEV;
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000928 }
Stephen Rothwellc4f55b32007-04-03 22:39:14 +1000929 prop = of_get_property(sound, "sub-frame", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (prop && *prop < 16)
931 chip->subframe = *prop;
Stephen Rothwellc4f55b32007-04-03 22:39:14 +1000932 prop = of_get_property(sound, "layout-id", NULL);
Johannes Berg55c385a2006-06-21 15:43:44 +0200933 if (prop) {
934 /* partly deprecate snd-powermac, for those machines
935 * that have a layout-id property for now */
936 printk(KERN_INFO "snd-powermac no longer handles any "
937 "machines with a layout-id property "
938 "in the device-tree, use snd-aoa.\n");
Benjamin Herrenschmidt41e904d2007-06-19 14:37:39 +1000939 of_node_put(sound);
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000940 of_node_put(chip->node);
Benjamin Herrenschmidt41e904d2007-06-19 14:37:39 +1000941 chip->node = NULL;
Johannes Berg55c385a2006-06-21 15:43:44 +0200942 return -ENODEV;
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /* This should be verified on older screamers */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000945 if (of_device_is_compatible(sound, "screamer")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 chip->model = PMAC_SCREAMER;
947 // chip->can_byte_swap = 0; /* FIXME: check this */
948 }
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000949 if (of_device_is_compatible(sound, "burgundy")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 chip->model = PMAC_BURGUNDY;
951 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
952 }
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000953 if (of_device_is_compatible(sound, "daca")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 chip->model = PMAC_DACA;
955 chip->can_capture = 0; /* no capture */
956 chip->can_duplex = 0;
957 // chip->can_byte_swap = 0; /* FIXME: check this */
958 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
959 }
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000960 if (of_device_is_compatible(sound, "tumbler")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 chip->model = PMAC_TUMBLER;
962 chip->can_capture = 0; /* no capture */
963 chip->can_duplex = 0;
964 // chip->can_byte_swap = 0; /* FIXME: check this */
965 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
966 chip->freq_table = tumbler_freqs;
967 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
968 }
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000969 if (of_device_is_compatible(sound, "snapper")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 chip->model = PMAC_SNAPPER;
971 // chip->can_byte_swap = 0; /* FIXME: check this */
972 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
973 chip->freq_table = tumbler_freqs;
974 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
975 }
Stephen Rothwellc4f55b32007-04-03 22:39:14 +1000976 prop = of_get_property(sound, "device-id", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (prop)
978 chip->device_id = *prop;
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000979 dn = of_find_node_by_name(NULL, "perch");
980 chip->has_iic = (dn != NULL);
981 of_node_put(dn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -0700983 /* We need the PCI device for DMA allocations, let's use a crude method
984 * for now ...
985 */
986 macio = macio_find(chip->node, macio_unknown);
987 if (macio == NULL)
988 printk(KERN_WARNING "snd-powermac: can't locate macio !\n");
989 else {
990 struct pci_dev *pdev = NULL;
991
992 for_each_pci_dev(pdev) {
993 struct device_node *np = pci_device_to_OF_node(pdev);
994 if (np && np == macio->of_node) {
995 chip->pdev = pdev;
996 break;
997 }
998 }
999 }
1000 if (chip->pdev == NULL)
Benjamin Herrenschmidt52180642005-05-18 16:31:51 +02001001 printk(KERN_WARNING "snd-powermac: can't locate macio PCI"
1002 " device !\n");
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 detect_byte_swap(chip);
1005
1006 /* look for a property saying what sample rates
1007 are available */
Stephen Rothwellc4f55b32007-04-03 22:39:14 +10001008 prop = of_get_property(sound, "sample-rates", &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (! prop)
Stephen Rothwellc4f55b32007-04-03 22:39:14 +10001010 prop = of_get_property(sound, "output-frame-rates", &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (prop) {
1012 int i;
1013 chip->freqs_ok = 0;
1014 for (l /= sizeof(int); l > 0; --l) {
1015 unsigned int r = *prop++;
1016 /* Apple 'Fixed' format */
1017 if (r >= 0x10000)
1018 r >>= 16;
1019 for (i = 0; i < chip->num_freqs; ++i) {
1020 if (r == chip->freq_table[i]) {
1021 chip->freqs_ok |= (1 << i);
1022 break;
1023 }
1024 }
1025 }
1026 } else {
1027 /* assume only 44.1khz */
1028 chip->freqs_ok = 1;
1029 }
1030
Stephen Rothwell30686ba2007-04-24 13:53:04 +10001031 of_node_put(sound);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return 0;
1033}
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035#ifdef PMAC_SUPPORT_AUTOMUTE
1036/*
1037 * auto-mute
1038 */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001039static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol,
1040 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001042 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 ucontrol->value.integer.value[0] = chip->auto_mute;
1044 return 0;
1045}
1046
Takashi Iwai65b29f52005-11-17 15:09:46 +01001047static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol,
1048 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001050 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1052 chip->auto_mute = ucontrol->value.integer.value[0];
1053 if (chip->update_automute)
1054 chip->update_automute(chip, 1);
1055 return 1;
1056 }
1057 return 0;
1058}
1059
Takashi Iwai65b29f52005-11-17 15:09:46 +01001060static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol,
1061 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001063 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (chip->detect_headphone)
1065 ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1066 else
1067 ucontrol->value.integer.value[0] = 0;
1068 return 0;
1069}
1070
Takashi Iwai65b29f52005-11-17 15:09:46 +01001071static struct snd_kcontrol_new auto_mute_controls[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1073 .name = "Auto Mute Switch",
1074 .info = snd_pmac_boolean_mono_info,
1075 .get = pmac_auto_mute_get,
1076 .put = pmac_auto_mute_put,
1077 },
1078 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1079 .name = "Headphone Detection",
1080 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1081 .info = snd_pmac_boolean_mono_info,
1082 .get = pmac_hp_detect_get,
1083 },
1084};
1085
Takashi Iwai65b29f52005-11-17 15:09:46 +01001086int __init snd_pmac_add_automute(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 int err;
1089 chip->auto_mute = 1;
1090 err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001091 if (err < 0) {
1092 printk(KERN_ERR "snd-powermac: Failed to add automute control\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return err;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1096 return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1097}
1098#endif /* PMAC_SUPPORT_AUTOMUTE */
1099
1100/*
1101 * create and detect a pmac chip record
1102 */
Takashi Iwai65b29f52005-11-17 15:09:46 +01001103int __init snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Takashi Iwai65b29f52005-11-17 15:09:46 +01001105 struct snd_pmac *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct device_node *np;
1107 int i, err;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001108 unsigned int irq;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001109 unsigned long ctrl_addr, txdma_addr, rxdma_addr;
Takashi Iwai65b29f52005-11-17 15:09:46 +01001110 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 .dev_free = snd_pmac_dev_free,
1112 };
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 *chip_return = NULL;
1115
Takashi Iwai561b2202005-09-09 14:22:34 +02001116 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (chip == NULL)
1118 return -ENOMEM;
1119 chip->card = card;
1120
1121 spin_lock_init(&chip->reg_lock);
1122 chip->irq = chip->tx_irq = chip->rx_irq = -1;
1123
1124 chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1125 chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1126
1127 if ((err = snd_pmac_detect(chip)) < 0)
1128 goto __error;
1129
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001130 if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1131 snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1132 snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 err = -ENOMEM;
1134 goto __error;
1135 }
1136
1137 np = chip->node;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001138 chip->requested = 0;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001139 if (chip->is_k2) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001140 static char *rnames[] = {
1141 "Sound Control", "Sound DMA" };
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001142 for (i = 0; i < 2; i ++) {
1143 if (of_address_to_resource(np->parent, i,
1144 &chip->rsrc[i])) {
1145 printk(KERN_ERR "snd: can't translate rsrc "
1146 " %d (%s)\n", i, rnames[i]);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001147 err = -ENODEV;
1148 goto __error;
1149 }
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001150 if (request_mem_region(chip->rsrc[i].start,
1151 chip->rsrc[i].end -
1152 chip->rsrc[i].start + 1,
1153 rnames[i]) == NULL) {
1154 printk(KERN_ERR "snd: can't request rsrc "
Benjamin Herrenschmidtd63fb6c2006-07-02 11:55:03 +10001155 " %d (%s: 0x%016llx:%016llx)\n",
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07001156 i, rnames[i],
1157 (unsigned long long)chip->rsrc[i].start,
1158 (unsigned long long)chip->rsrc[i].end);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001159 err = -ENODEV;
1160 goto __error;
1161 }
1162 chip->requested |= (1 << i);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001163 }
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001164 ctrl_addr = chip->rsrc[0].start;
1165 txdma_addr = chip->rsrc[1].start;
1166 rxdma_addr = txdma_addr + 0x100;
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001167 } else {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001168 static char *rnames[] = {
1169 "Sound Control", "Sound Tx DMA", "Sound Rx DMA" };
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001170 for (i = 0; i < 3; i ++) {
Benjamin Herrenschmidt88356e92006-02-01 03:04:43 -08001171 if (of_address_to_resource(np, i,
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001172 &chip->rsrc[i])) {
1173 printk(KERN_ERR "snd: can't translate rsrc "
1174 " %d (%s)\n", i, rnames[i]);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001175 err = -ENODEV;
1176 goto __error;
1177 }
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001178 if (request_mem_region(chip->rsrc[i].start,
1179 chip->rsrc[i].end -
1180 chip->rsrc[i].start + 1,
1181 rnames[i]) == NULL) {
1182 printk(KERN_ERR "snd: can't request rsrc "
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07001183 " %d (%s: 0x%016llx:%016llx)\n",
1184 i, rnames[i],
1185 (unsigned long long)chip->rsrc[i].start,
1186 (unsigned long long)chip->rsrc[i].end);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001187 err = -ENODEV;
1188 goto __error;
1189 }
1190 chip->requested |= (1 << i);
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001191 }
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001192 ctrl_addr = chip->rsrc[0].start;
1193 txdma_addr = chip->rsrc[1].start;
1194 rxdma_addr = chip->rsrc[2].start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001197 chip->awacs = ioremap(ctrl_addr, 0x1000);
1198 chip->playback.dma = ioremap(txdma_addr, 0x100);
1199 chip->capture.dma = ioremap(rxdma_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (chip->model <= PMAC_BURGUNDY) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001201 irq = irq_of_parse_and_map(np, 0);
1202 if (request_irq(irq, snd_pmac_ctrl_intr, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 "PMac", (void*)chip)) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001204 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n",
1205 irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 err = -EBUSY;
1207 goto __error;
1208 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001209 chip->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001211 irq = irq_of_parse_and_map(np, 1);
1212 if (request_irq(irq, snd_pmac_tx_intr, 0, "PMac Output", (void*)chip)){
1213 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 err = -EBUSY;
1215 goto __error;
1216 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001217 chip->tx_irq = irq;
1218 irq = irq_of_parse_and_map(np, 2);
1219 if (request_irq(irq, snd_pmac_rx_intr, 0, "PMac Input", (void*)chip)) {
1220 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 err = -EBUSY;
1222 goto __error;
1223 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001224 chip->rx_irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 snd_pmac_sound_feature(chip, 1);
1227
1228 /* reset */
Benjamin Herrenschmidt7bbd8272005-04-16 15:24:32 -07001229 if (chip->model == PMAC_AWACS)
1230 out_le32(&chip->awacs->control, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /* Powerbooks have odd ways of enabling inputs such as
1233 an expansion-bay CD or sound from an internal modem
1234 or a PC-card modem. */
1235 if (chip->is_pbook_3400) {
1236 /* Enable CD and PC-card sound inputs. */
1237 /* This is done by reading from address
1238 * f301a000, + 0x10 to enable the expansion-bay
1239 * CD sound input, + 0x80 to enable the PC-card
1240 * sound input. The 0x100 enables the SCSI bus
1241 * terminator power.
1242 */
1243 chip->latch_base = ioremap (0xf301a000, 0x1000);
1244 in_8(chip->latch_base + 0x190);
1245 } else if (chip->is_pbook_G3) {
1246 struct device_node* mio;
1247 for (mio = chip->node->parent; mio; mio = mio->parent) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001248 if (strcmp(mio->name, "mac-io") == 0) {
1249 struct resource r;
1250 if (of_address_to_resource(mio, 0, &r) == 0)
1251 chip->macio_base =
1252 ioremap(r.start, 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
1254 }
1255 }
1256 /* Enable CD sound input. */
1257 /* The relevant bits for writing to this byte are 0x8f.
1258 * I haven't found out what the 0x80 bit does.
1259 * For the 0xf bits, writing 3 or 7 enables the CD
1260 * input, any other value disables it. Values
1261 * 1, 3, 5, 7 enable the microphone. Values 0, 2,
1262 * 4, 6, 8 - f enable the input from the modem.
1263 */
1264 if (chip->macio_base)
1265 out_8(chip->macio_base + 0x37, 3);
1266 }
1267
1268 /* Reset dbdma channels */
1269 snd_pmac_dbdma_reset(chip);
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1272 goto __error;
1273
1274 *chip_return = chip;
1275 return 0;
1276
1277 __error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 snd_pmac_free(chip);
1279 return err;
1280}
1281
1282
1283/*
1284 * sleep notify for powerbook
1285 */
1286
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001287#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289/*
1290 * Save state when going to sleep, restore it afterwards.
1291 */
1292
Takashi Iwai5e12bea2005-11-17 17:17:08 +01001293void snd_pmac_suspend(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 unsigned long flags;
1296
Takashi Iwai5e12bea2005-11-17 17:17:08 +01001297 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (chip->suspend)
1299 chip->suspend(chip);
1300 snd_pcm_suspend_all(chip->pcm);
1301 spin_lock_irqsave(&chip->reg_lock, flags);
1302 snd_pmac_beep_stop(chip);
1303 spin_unlock_irqrestore(&chip->reg_lock, flags);
1304 if (chip->irq >= 0)
1305 disable_irq(chip->irq);
1306 if (chip->tx_irq >= 0)
1307 disable_irq(chip->tx_irq);
1308 if (chip->rx_irq >= 0)
1309 disable_irq(chip->rx_irq);
1310 snd_pmac_sound_feature(chip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
Takashi Iwai5e12bea2005-11-17 17:17:08 +01001313void snd_pmac_resume(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 snd_pmac_sound_feature(chip, 1);
1316 if (chip->resume)
1317 chip->resume(chip);
1318 /* enable CD sound input */
Takashi Iwai5e12bea2005-11-17 17:17:08 +01001319 if (chip->macio_base && chip->is_pbook_G3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 out_8(chip->macio_base + 0x37, 3);
Takashi Iwai5e12bea2005-11-17 17:17:08 +01001321 else if (chip->is_pbook_3400)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 in_8(chip->latch_base + 0x190);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 snd_pmac_pcm_set_format(chip);
1325
1326 if (chip->irq >= 0)
1327 enable_irq(chip->irq);
1328 if (chip->tx_irq >= 0)
1329 enable_irq(chip->tx_irq);
1330 if (chip->rx_irq >= 0)
1331 enable_irq(chip->rx_irq);
1332
Takashi Iwai5e12bea2005-11-17 17:17:08 +01001333 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001336#endif /* CONFIG_PM */
1337