pmac.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PMac DBDMA lowlevel functions
  4. *
  5. * Copyright (c) by Takashi Iwai <[email protected]>
  6. * code based on dmasound.c.
  7. */
  8. #include <linux/io.h>
  9. #include <asm/irq.h>
  10. #include <linux/init.h>
  11. #include <linux/delay.h>
  12. #include <linux/slab.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/pci.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <sound/core.h>
  19. #include "pmac.h"
  20. #include <sound/pcm_params.h>
  21. #include <asm/pmac_feature.h>
  22. /* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
  23. static const int awacs_freqs[8] = {
  24. 44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
  25. };
  26. /* fixed frequency table for tumbler */
  27. static const int tumbler_freqs[1] = {
  28. 44100
  29. };
  30. /*
  31. * we will allocate a single 'emergency' dbdma cmd block to use if the
  32. * tx status comes up "DEAD". This happens on some PowerComputing Pmac
  33. * clones, either owing to a bug in dbdma or some interaction between
  34. * IDE and sound. However, this measure would deal with DEAD status if
  35. * it appeared elsewhere.
  36. */
  37. static struct pmac_dbdma emergency_dbdma;
  38. static int emergency_in_use;
  39. /*
  40. * allocate DBDMA command arrays
  41. */
  42. static int snd_pmac_dbdma_alloc(struct snd_pmac *chip, struct pmac_dbdma *rec, int size)
  43. {
  44. unsigned int rsize = sizeof(struct dbdma_cmd) * (size + 1);
  45. rec->space = dma_alloc_coherent(&chip->pdev->dev, rsize,
  46. &rec->dma_base, GFP_KERNEL);
  47. if (rec->space == NULL)
  48. return -ENOMEM;
  49. rec->size = size;
  50. memset(rec->space, 0, rsize);
  51. rec->cmds = (void __iomem *)DBDMA_ALIGN(rec->space);
  52. rec->addr = rec->dma_base + (unsigned long)((char *)rec->cmds - (char *)rec->space);
  53. return 0;
  54. }
  55. static void snd_pmac_dbdma_free(struct snd_pmac *chip, struct pmac_dbdma *rec)
  56. {
  57. if (rec->space) {
  58. unsigned int rsize = sizeof(struct dbdma_cmd) * (rec->size + 1);
  59. dma_free_coherent(&chip->pdev->dev, rsize, rec->space, rec->dma_base);
  60. }
  61. }
  62. /*
  63. * pcm stuff
  64. */
  65. /*
  66. * look up frequency table
  67. */
  68. unsigned int snd_pmac_rate_index(struct snd_pmac *chip, struct pmac_stream *rec, unsigned int rate)
  69. {
  70. int i, ok, found;
  71. ok = rec->cur_freqs;
  72. if (rate > chip->freq_table[0])
  73. return 0;
  74. found = 0;
  75. for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
  76. if (! (ok & 1)) continue;
  77. found = i;
  78. if (rate >= chip->freq_table[i])
  79. break;
  80. }
  81. return found;
  82. }
  83. /*
  84. * check whether another stream is active
  85. */
  86. static inline int another_stream(int stream)
  87. {
  88. return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  89. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  90. }
  91. /*
  92. * get a stream of the opposite direction
  93. */
  94. static struct pmac_stream *snd_pmac_get_stream(struct snd_pmac *chip, int stream)
  95. {
  96. switch (stream) {
  97. case SNDRV_PCM_STREAM_PLAYBACK:
  98. return &chip->playback;
  99. case SNDRV_PCM_STREAM_CAPTURE:
  100. return &chip->capture;
  101. default:
  102. snd_BUG();
  103. return NULL;
  104. }
  105. }
  106. /*
  107. * wait while run status is on
  108. */
  109. static inline void
  110. snd_pmac_wait_ack(struct pmac_stream *rec)
  111. {
  112. int timeout = 50000;
  113. while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
  114. udelay(1);
  115. }
  116. /*
  117. * set the format and rate to the chip.
  118. * call the lowlevel function if defined (e.g. for AWACS).
  119. */
  120. static void snd_pmac_pcm_set_format(struct snd_pmac *chip)
  121. {
  122. /* set up frequency and format */
  123. out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
  124. out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
  125. if (chip->set_format)
  126. chip->set_format(chip);
  127. }
  128. /*
  129. * stop the DMA transfer
  130. */
  131. static inline void snd_pmac_dma_stop(struct pmac_stream *rec)
  132. {
  133. out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
  134. snd_pmac_wait_ack(rec);
  135. }
  136. /*
  137. * set the command pointer address
  138. */
  139. static inline void snd_pmac_dma_set_command(struct pmac_stream *rec, struct pmac_dbdma *cmd)
  140. {
  141. out_le32(&rec->dma->cmdptr, cmd->addr);
  142. }
  143. /*
  144. * start the DMA
  145. */
  146. static inline void snd_pmac_dma_run(struct pmac_stream *rec, int status)
  147. {
  148. out_le32(&rec->dma->control, status | (status << 16));
  149. }
  150. /*
  151. * prepare playback/capture stream
  152. */
  153. static int snd_pmac_pcm_prepare(struct snd_pmac *chip, struct pmac_stream *rec, struct snd_pcm_substream *subs)
  154. {
  155. int i;
  156. volatile struct dbdma_cmd __iomem *cp;
  157. struct snd_pcm_runtime *runtime = subs->runtime;
  158. int rate_index;
  159. long offset;
  160. struct pmac_stream *astr;
  161. rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
  162. rec->period_size = snd_pcm_lib_period_bytes(subs);
  163. rec->nperiods = rec->dma_size / rec->period_size;
  164. rec->cur_period = 0;
  165. rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
  166. /* set up constraints */
  167. astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
  168. if (! astr)
  169. return -EINVAL;
  170. astr->cur_freqs = 1 << rate_index;
  171. astr->cur_formats = 1 << runtime->format;
  172. chip->rate_index = rate_index;
  173. chip->format = runtime->format;
  174. /* We really want to execute a DMA stop command, after the AWACS
  175. * is initialized.
  176. * For reasons I don't understand, it stops the hissing noise
  177. * common to many PowerBook G3 systems and random noise otherwise
  178. * captured on iBook2's about every third time. -ReneR
  179. */
  180. spin_lock_irq(&chip->reg_lock);
  181. snd_pmac_dma_stop(rec);
  182. chip->extra_dma.cmds->command = cpu_to_le16(DBDMA_STOP);
  183. snd_pmac_dma_set_command(rec, &chip->extra_dma);
  184. snd_pmac_dma_run(rec, RUN);
  185. spin_unlock_irq(&chip->reg_lock);
  186. mdelay(5);
  187. spin_lock_irq(&chip->reg_lock);
  188. /* continuous DMA memory type doesn't provide the physical address,
  189. * so we need to resolve the address here...
  190. */
  191. offset = runtime->dma_addr;
  192. for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) {
  193. cp->phy_addr = cpu_to_le32(offset);
  194. cp->req_count = cpu_to_le16(rec->period_size);
  195. /*cp->res_count = cpu_to_le16(0);*/
  196. cp->xfer_status = cpu_to_le16(0);
  197. offset += rec->period_size;
  198. }
  199. /* make loop */
  200. cp->command = cpu_to_le16(DBDMA_NOP | BR_ALWAYS);
  201. cp->cmd_dep = cpu_to_le32(rec->cmd.addr);
  202. snd_pmac_dma_stop(rec);
  203. snd_pmac_dma_set_command(rec, &rec->cmd);
  204. spin_unlock_irq(&chip->reg_lock);
  205. return 0;
  206. }
  207. /*
  208. * PCM trigger/stop
  209. */
  210. static int snd_pmac_pcm_trigger(struct snd_pmac *chip, struct pmac_stream *rec,
  211. struct snd_pcm_substream *subs, int cmd)
  212. {
  213. volatile struct dbdma_cmd __iomem *cp;
  214. int i, command;
  215. switch (cmd) {
  216. case SNDRV_PCM_TRIGGER_START:
  217. case SNDRV_PCM_TRIGGER_RESUME:
  218. if (rec->running)
  219. return -EBUSY;
  220. command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  221. OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
  222. spin_lock(&chip->reg_lock);
  223. snd_pmac_beep_stop(chip);
  224. snd_pmac_pcm_set_format(chip);
  225. for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
  226. out_le16(&cp->command, command);
  227. snd_pmac_dma_set_command(rec, &rec->cmd);
  228. (void)in_le32(&rec->dma->status);
  229. snd_pmac_dma_run(rec, RUN|WAKE);
  230. rec->running = 1;
  231. spin_unlock(&chip->reg_lock);
  232. break;
  233. case SNDRV_PCM_TRIGGER_STOP:
  234. case SNDRV_PCM_TRIGGER_SUSPEND:
  235. spin_lock(&chip->reg_lock);
  236. rec->running = 0;
  237. /*printk(KERN_DEBUG "stopped!!\n");*/
  238. snd_pmac_dma_stop(rec);
  239. for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
  240. out_le16(&cp->command, DBDMA_STOP);
  241. spin_unlock(&chip->reg_lock);
  242. break;
  243. default:
  244. return -EINVAL;
  245. }
  246. return 0;
  247. }
  248. /*
  249. * return the current pointer
  250. */
  251. inline
  252. static snd_pcm_uframes_t snd_pmac_pcm_pointer(struct snd_pmac *chip,
  253. struct pmac_stream *rec,
  254. struct snd_pcm_substream *subs)
  255. {
  256. int count = 0;
  257. #if 1 /* hmm.. how can we get the current dma pointer?? */
  258. int stat;
  259. volatile struct dbdma_cmd __iomem *cp = &rec->cmd.cmds[rec->cur_period];
  260. stat = le16_to_cpu(cp->xfer_status);
  261. if (stat & (ACTIVE|DEAD)) {
  262. count = in_le16(&cp->res_count);
  263. if (count)
  264. count = rec->period_size - count;
  265. }
  266. #endif
  267. count += rec->cur_period * rec->period_size;
  268. /*printk(KERN_DEBUG "pointer=%d\n", count);*/
  269. return bytes_to_frames(subs->runtime, count);
  270. }
  271. /*
  272. * playback
  273. */
  274. static int snd_pmac_playback_prepare(struct snd_pcm_substream *subs)
  275. {
  276. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  277. return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
  278. }
  279. static int snd_pmac_playback_trigger(struct snd_pcm_substream *subs,
  280. int cmd)
  281. {
  282. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  283. return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
  284. }
  285. static snd_pcm_uframes_t snd_pmac_playback_pointer(struct snd_pcm_substream *subs)
  286. {
  287. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  288. return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
  289. }
  290. /*
  291. * capture
  292. */
  293. static int snd_pmac_capture_prepare(struct snd_pcm_substream *subs)
  294. {
  295. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  296. return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
  297. }
  298. static int snd_pmac_capture_trigger(struct snd_pcm_substream *subs,
  299. int cmd)
  300. {
  301. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  302. return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
  303. }
  304. static snd_pcm_uframes_t snd_pmac_capture_pointer(struct snd_pcm_substream *subs)
  305. {
  306. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  307. return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
  308. }
  309. /*
  310. * Handle DEAD DMA transfers:
  311. * if the TX status comes up "DEAD" - reported on some Power Computing machines
  312. * we need to re-start the dbdma - but from a different physical start address
  313. * and with a different transfer length. It would get very messy to do this
  314. * with the normal dbdma_cmd blocks - we would have to re-write the buffer start
  315. * addresses each time. So, we will keep a single dbdma_cmd block which can be
  316. * fiddled with.
  317. * When DEAD status is first reported the content of the faulted dbdma block is
  318. * copied into the emergency buffer and we note that the buffer is in use.
  319. * we then bump the start physical address by the amount that was successfully
  320. * output before it died.
  321. * On any subsequent DEAD result we just do the bump-ups (we know that we are
  322. * already using the emergency dbdma_cmd).
  323. * CHECK: this just tries to "do it". It is possible that we should abandon
  324. * xfers when the number of residual bytes gets below a certain value - I can
  325. * see that this might cause a loop-forever if a too small transfer causes
  326. * DEAD status. However this is a TODO for now - we'll see what gets reported.
  327. * When we get a successful transfer result with the emergency buffer we just
  328. * pretend that it completed using the original dmdma_cmd and carry on. The
  329. * 'next_cmd' field will already point back to the original loop of blocks.
  330. */
  331. static inline void snd_pmac_pcm_dead_xfer(struct pmac_stream *rec,
  332. volatile struct dbdma_cmd __iomem *cp)
  333. {
  334. unsigned short req, res ;
  335. unsigned int phy ;
  336. /* printk(KERN_WARNING "snd-powermac: DMA died - patching it up!\n"); */
  337. /* to clear DEAD status we must first clear RUN
  338. set it to quiescent to be on the safe side */
  339. (void)in_le32(&rec->dma->status);
  340. out_le32(&rec->dma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
  341. if (!emergency_in_use) { /* new problem */
  342. memcpy((void *)emergency_dbdma.cmds, (void *)cp,
  343. sizeof(struct dbdma_cmd));
  344. emergency_in_use = 1;
  345. cp->xfer_status = cpu_to_le16(0);
  346. cp->req_count = cpu_to_le16(rec->period_size);
  347. cp = emergency_dbdma.cmds;
  348. }
  349. /* now bump the values to reflect the amount
  350. we haven't yet shifted */
  351. req = le16_to_cpu(cp->req_count);
  352. res = le16_to_cpu(cp->res_count);
  353. phy = le32_to_cpu(cp->phy_addr);
  354. phy += (req - res);
  355. cp->req_count = cpu_to_le16(res);
  356. cp->res_count = cpu_to_le16(0);
  357. cp->xfer_status = cpu_to_le16(0);
  358. cp->phy_addr = cpu_to_le32(phy);
  359. cp->cmd_dep = cpu_to_le32(rec->cmd.addr
  360. + sizeof(struct dbdma_cmd)*((rec->cur_period+1)%rec->nperiods));
  361. cp->command = cpu_to_le16(OUTPUT_MORE | BR_ALWAYS | INTR_ALWAYS);
  362. /* point at our patched up command block */
  363. out_le32(&rec->dma->cmdptr, emergency_dbdma.addr);
  364. /* we must re-start the controller */
  365. (void)in_le32(&rec->dma->status);
  366. /* should complete clearing the DEAD status */
  367. out_le32(&rec->dma->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
  368. }
  369. /*
  370. * update playback/capture pointer from interrupts
  371. */
  372. static void snd_pmac_pcm_update(struct snd_pmac *chip, struct pmac_stream *rec)
  373. {
  374. volatile struct dbdma_cmd __iomem *cp;
  375. int c;
  376. int stat;
  377. spin_lock(&chip->reg_lock);
  378. if (rec->running) {
  379. for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */
  380. if (emergency_in_use) /* already using DEAD xfer? */
  381. cp = emergency_dbdma.cmds;
  382. else
  383. cp = &rec->cmd.cmds[rec->cur_period];
  384. stat = le16_to_cpu(cp->xfer_status);
  385. if (stat & DEAD) {
  386. snd_pmac_pcm_dead_xfer(rec, cp);
  387. break; /* this block is still going */
  388. }
  389. if (emergency_in_use)
  390. emergency_in_use = 0 ; /* done that */
  391. if (! (stat & ACTIVE))
  392. break;
  393. /*printk(KERN_DEBUG "update frag %d\n", rec->cur_period);*/
  394. cp->xfer_status = cpu_to_le16(0);
  395. cp->req_count = cpu_to_le16(rec->period_size);
  396. /*cp->res_count = cpu_to_le16(0);*/
  397. rec->cur_period++;
  398. if (rec->cur_period >= rec->nperiods) {
  399. rec->cur_period = 0;
  400. }
  401. spin_unlock(&chip->reg_lock);
  402. snd_pcm_period_elapsed(rec->substream);
  403. spin_lock(&chip->reg_lock);
  404. }
  405. }
  406. spin_unlock(&chip->reg_lock);
  407. }
  408. /*
  409. * hw info
  410. */
  411. static const struct snd_pcm_hardware snd_pmac_playback =
  412. {
  413. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  414. SNDRV_PCM_INFO_MMAP |
  415. SNDRV_PCM_INFO_MMAP_VALID |
  416. SNDRV_PCM_INFO_RESUME),
  417. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
  418. .rates = SNDRV_PCM_RATE_8000_44100,
  419. .rate_min = 7350,
  420. .rate_max = 44100,
  421. .channels_min = 2,
  422. .channels_max = 2,
  423. .buffer_bytes_max = 131072,
  424. .period_bytes_min = 256,
  425. .period_bytes_max = 16384,
  426. .periods_min = 3,
  427. .periods_max = PMAC_MAX_FRAGS,
  428. };
  429. static const struct snd_pcm_hardware snd_pmac_capture =
  430. {
  431. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  432. SNDRV_PCM_INFO_MMAP |
  433. SNDRV_PCM_INFO_MMAP_VALID |
  434. SNDRV_PCM_INFO_RESUME),
  435. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
  436. .rates = SNDRV_PCM_RATE_8000_44100,
  437. .rate_min = 7350,
  438. .rate_max = 44100,
  439. .channels_min = 2,
  440. .channels_max = 2,
  441. .buffer_bytes_max = 131072,
  442. .period_bytes_min = 256,
  443. .period_bytes_max = 16384,
  444. .periods_min = 3,
  445. .periods_max = PMAC_MAX_FRAGS,
  446. };
  447. #if 0 // NYI
  448. static int snd_pmac_hw_rule_rate(struct snd_pcm_hw_params *params,
  449. struct snd_pcm_hw_rule *rule)
  450. {
  451. struct snd_pmac *chip = rule->private;
  452. struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
  453. int i, freq_table[8], num_freqs;
  454. if (! rec)
  455. return -EINVAL;
  456. num_freqs = 0;
  457. for (i = chip->num_freqs - 1; i >= 0; i--) {
  458. if (rec->cur_freqs & (1 << i))
  459. freq_table[num_freqs++] = chip->freq_table[i];
  460. }
  461. return snd_interval_list(hw_param_interval(params, rule->var),
  462. num_freqs, freq_table, 0);
  463. }
  464. static int snd_pmac_hw_rule_format(struct snd_pcm_hw_params *params,
  465. struct snd_pcm_hw_rule *rule)
  466. {
  467. struct snd_pmac *chip = rule->private;
  468. struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
  469. if (! rec)
  470. return -EINVAL;
  471. return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
  472. rec->cur_formats);
  473. }
  474. #endif // NYI
  475. static int snd_pmac_pcm_open(struct snd_pmac *chip, struct pmac_stream *rec,
  476. struct snd_pcm_substream *subs)
  477. {
  478. struct snd_pcm_runtime *runtime = subs->runtime;
  479. int i;
  480. /* look up frequency table and fill bit mask */
  481. runtime->hw.rates = 0;
  482. for (i = 0; i < chip->num_freqs; i++)
  483. if (chip->freqs_ok & (1 << i))
  484. runtime->hw.rates |=
  485. snd_pcm_rate_to_rate_bit(chip->freq_table[i]);
  486. /* check for minimum and maximum rates */
  487. for (i = 0; i < chip->num_freqs; i++) {
  488. if (chip->freqs_ok & (1 << i)) {
  489. runtime->hw.rate_max = chip->freq_table[i];
  490. break;
  491. }
  492. }
  493. for (i = chip->num_freqs - 1; i >= 0; i--) {
  494. if (chip->freqs_ok & (1 << i)) {
  495. runtime->hw.rate_min = chip->freq_table[i];
  496. break;
  497. }
  498. }
  499. runtime->hw.formats = chip->formats_ok;
  500. if (chip->can_capture) {
  501. if (! chip->can_duplex)
  502. runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
  503. runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  504. }
  505. runtime->private_data = rec;
  506. rec->substream = subs;
  507. #if 0 /* FIXME: still under development.. */
  508. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  509. snd_pmac_hw_rule_rate, chip, rec->stream, -1);
  510. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  511. snd_pmac_hw_rule_format, chip, rec->stream, -1);
  512. #endif
  513. runtime->hw.periods_max = rec->cmd.size - 1;
  514. /* constraints to fix choppy sound */
  515. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  516. return 0;
  517. }
  518. static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec,
  519. struct snd_pcm_substream *subs)
  520. {
  521. struct pmac_stream *astr;
  522. snd_pmac_dma_stop(rec);
  523. astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
  524. if (! astr)
  525. return -EINVAL;
  526. /* reset constraints */
  527. astr->cur_freqs = chip->freqs_ok;
  528. astr->cur_formats = chip->formats_ok;
  529. return 0;
  530. }
  531. static int snd_pmac_playback_open(struct snd_pcm_substream *subs)
  532. {
  533. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  534. subs->runtime->hw = snd_pmac_playback;
  535. return snd_pmac_pcm_open(chip, &chip->playback, subs);
  536. }
  537. static int snd_pmac_capture_open(struct snd_pcm_substream *subs)
  538. {
  539. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  540. subs->runtime->hw = snd_pmac_capture;
  541. return snd_pmac_pcm_open(chip, &chip->capture, subs);
  542. }
  543. static int snd_pmac_playback_close(struct snd_pcm_substream *subs)
  544. {
  545. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  546. return snd_pmac_pcm_close(chip, &chip->playback, subs);
  547. }
  548. static int snd_pmac_capture_close(struct snd_pcm_substream *subs)
  549. {
  550. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  551. return snd_pmac_pcm_close(chip, &chip->capture, subs);
  552. }
  553. /*
  554. */
  555. static const struct snd_pcm_ops snd_pmac_playback_ops = {
  556. .open = snd_pmac_playback_open,
  557. .close = snd_pmac_playback_close,
  558. .prepare = snd_pmac_playback_prepare,
  559. .trigger = snd_pmac_playback_trigger,
  560. .pointer = snd_pmac_playback_pointer,
  561. };
  562. static const struct snd_pcm_ops snd_pmac_capture_ops = {
  563. .open = snd_pmac_capture_open,
  564. .close = snd_pmac_capture_close,
  565. .prepare = snd_pmac_capture_prepare,
  566. .trigger = snd_pmac_capture_trigger,
  567. .pointer = snd_pmac_capture_pointer,
  568. };
  569. int snd_pmac_pcm_new(struct snd_pmac *chip)
  570. {
  571. struct snd_pcm *pcm;
  572. int err;
  573. int num_captures = 1;
  574. if (! chip->can_capture)
  575. num_captures = 0;
  576. err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
  577. if (err < 0)
  578. return err;
  579. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
  580. if (chip->can_capture)
  581. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
  582. pcm->private_data = chip;
  583. pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  584. strcpy(pcm->name, chip->card->shortname);
  585. chip->pcm = pcm;
  586. chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
  587. if (chip->can_byte_swap)
  588. chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
  589. chip->playback.cur_formats = chip->formats_ok;
  590. chip->capture.cur_formats = chip->formats_ok;
  591. chip->playback.cur_freqs = chip->freqs_ok;
  592. chip->capture.cur_freqs = chip->freqs_ok;
  593. /* preallocate 64k buffer */
  594. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
  595. &chip->pdev->dev,
  596. 64 * 1024, 64 * 1024);
  597. return 0;
  598. }
  599. static void snd_pmac_dbdma_reset(struct snd_pmac *chip)
  600. {
  601. out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
  602. snd_pmac_wait_ack(&chip->playback);
  603. out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
  604. snd_pmac_wait_ack(&chip->capture);
  605. }
  606. /*
  607. * handling beep
  608. */
  609. void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed)
  610. {
  611. struct pmac_stream *rec = &chip->playback;
  612. snd_pmac_dma_stop(rec);
  613. chip->extra_dma.cmds->req_count = cpu_to_le16(bytes);
  614. chip->extra_dma.cmds->xfer_status = cpu_to_le16(0);
  615. chip->extra_dma.cmds->cmd_dep = cpu_to_le32(chip->extra_dma.addr);
  616. chip->extra_dma.cmds->phy_addr = cpu_to_le32(addr);
  617. chip->extra_dma.cmds->command = cpu_to_le16(OUTPUT_MORE | BR_ALWAYS);
  618. out_le32(&chip->awacs->control,
  619. (in_le32(&chip->awacs->control) & ~0x1f00)
  620. | (speed << 8));
  621. out_le32(&chip->awacs->byteswap, 0);
  622. snd_pmac_dma_set_command(rec, &chip->extra_dma);
  623. snd_pmac_dma_run(rec, RUN);
  624. }
  625. void snd_pmac_beep_dma_stop(struct snd_pmac *chip)
  626. {
  627. snd_pmac_dma_stop(&chip->playback);
  628. chip->extra_dma.cmds->command = cpu_to_le16(DBDMA_STOP);
  629. snd_pmac_pcm_set_format(chip); /* reset format */
  630. }
  631. /*
  632. * interrupt handlers
  633. */
  634. static irqreturn_t
  635. snd_pmac_tx_intr(int irq, void *devid)
  636. {
  637. struct snd_pmac *chip = devid;
  638. snd_pmac_pcm_update(chip, &chip->playback);
  639. return IRQ_HANDLED;
  640. }
  641. static irqreturn_t
  642. snd_pmac_rx_intr(int irq, void *devid)
  643. {
  644. struct snd_pmac *chip = devid;
  645. snd_pmac_pcm_update(chip, &chip->capture);
  646. return IRQ_HANDLED;
  647. }
  648. static irqreturn_t
  649. snd_pmac_ctrl_intr(int irq, void *devid)
  650. {
  651. struct snd_pmac *chip = devid;
  652. int ctrl = in_le32(&chip->awacs->control);
  653. /*printk(KERN_DEBUG "pmac: control interrupt.. 0x%x\n", ctrl);*/
  654. if (ctrl & MASK_PORTCHG) {
  655. /* do something when headphone is plugged/unplugged? */
  656. if (chip->update_automute)
  657. chip->update_automute(chip, 1);
  658. }
  659. if (ctrl & MASK_CNTLERR) {
  660. int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
  661. if (err && chip->model <= PMAC_SCREAMER)
  662. snd_printk(KERN_DEBUG "error %x\n", err);
  663. }
  664. /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
  665. out_le32(&chip->awacs->control, ctrl);
  666. return IRQ_HANDLED;
  667. }
  668. /*
  669. * a wrapper to feature call for compatibility
  670. */
  671. static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable)
  672. {
  673. if (ppc_md.feature_call)
  674. ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
  675. }
  676. /*
  677. * release resources
  678. */
  679. static int snd_pmac_free(struct snd_pmac *chip)
  680. {
  681. /* stop sounds */
  682. if (chip->initialized) {
  683. snd_pmac_dbdma_reset(chip);
  684. /* disable interrupts from awacs interface */
  685. out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
  686. }
  687. if (chip->node)
  688. snd_pmac_sound_feature(chip, 0);
  689. /* clean up mixer if any */
  690. if (chip->mixer_free)
  691. chip->mixer_free(chip);
  692. snd_pmac_detach_beep(chip);
  693. /* release resources */
  694. if (chip->irq >= 0)
  695. free_irq(chip->irq, (void*)chip);
  696. if (chip->tx_irq >= 0)
  697. free_irq(chip->tx_irq, (void*)chip);
  698. if (chip->rx_irq >= 0)
  699. free_irq(chip->rx_irq, (void*)chip);
  700. snd_pmac_dbdma_free(chip, &chip->playback.cmd);
  701. snd_pmac_dbdma_free(chip, &chip->capture.cmd);
  702. snd_pmac_dbdma_free(chip, &chip->extra_dma);
  703. snd_pmac_dbdma_free(chip, &emergency_dbdma);
  704. iounmap(chip->macio_base);
  705. iounmap(chip->latch_base);
  706. iounmap(chip->awacs);
  707. iounmap(chip->playback.dma);
  708. iounmap(chip->capture.dma);
  709. if (chip->node) {
  710. int i;
  711. for (i = 0; i < 3; i++) {
  712. if (chip->requested & (1 << i))
  713. release_mem_region(chip->rsrc[i].start,
  714. resource_size(&chip->rsrc[i]));
  715. }
  716. }
  717. pci_dev_put(chip->pdev);
  718. of_node_put(chip->node);
  719. kfree(chip);
  720. return 0;
  721. }
  722. /*
  723. * free the device
  724. */
  725. static int snd_pmac_dev_free(struct snd_device *device)
  726. {
  727. struct snd_pmac *chip = device->device_data;
  728. return snd_pmac_free(chip);
  729. }
  730. /*
  731. * check the machine support byteswap (little-endian)
  732. */
  733. static void detect_byte_swap(struct snd_pmac *chip)
  734. {
  735. struct device_node *mio;
  736. /* if seems that Keylargo can't byte-swap */
  737. for (mio = chip->node->parent; mio; mio = mio->parent) {
  738. if (of_node_name_eq(mio, "mac-io")) {
  739. if (of_device_is_compatible(mio, "Keylargo"))
  740. chip->can_byte_swap = 0;
  741. break;
  742. }
  743. }
  744. /* it seems the Pismo & iBook can't byte-swap in hardware. */
  745. if (of_machine_is_compatible("PowerBook3,1") ||
  746. of_machine_is_compatible("PowerBook2,1"))
  747. chip->can_byte_swap = 0 ;
  748. if (of_machine_is_compatible("PowerBook2,1"))
  749. chip->can_duplex = 0;
  750. }
  751. /*
  752. * detect a sound chip
  753. */
  754. static int snd_pmac_detect(struct snd_pmac *chip)
  755. {
  756. struct device_node *sound;
  757. struct device_node *dn;
  758. const unsigned int *prop;
  759. unsigned int l;
  760. struct macio_chip* macio;
  761. if (!machine_is(powermac))
  762. return -ENODEV;
  763. chip->subframe = 0;
  764. chip->revision = 0;
  765. chip->freqs_ok = 0xff; /* all ok */
  766. chip->model = PMAC_AWACS;
  767. chip->can_byte_swap = 1;
  768. chip->can_duplex = 1;
  769. chip->can_capture = 1;
  770. chip->num_freqs = ARRAY_SIZE(awacs_freqs);
  771. chip->freq_table = awacs_freqs;
  772. chip->pdev = NULL;
  773. chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
  774. /* check machine type */
  775. if (of_machine_is_compatible("AAPL,3400/2400")
  776. || of_machine_is_compatible("AAPL,3500"))
  777. chip->is_pbook_3400 = 1;
  778. else if (of_machine_is_compatible("PowerBook1,1")
  779. || of_machine_is_compatible("AAPL,PowerBook1998"))
  780. chip->is_pbook_G3 = 1;
  781. chip->node = of_find_node_by_name(NULL, "awacs");
  782. sound = of_node_get(chip->node);
  783. /*
  784. * powermac G3 models have a node called "davbus"
  785. * with a child called "sound".
  786. */
  787. if (!chip->node)
  788. chip->node = of_find_node_by_name(NULL, "davbus");
  789. /*
  790. * if we didn't find a davbus device, try 'i2s-a' since
  791. * this seems to be what iBooks have
  792. */
  793. if (! chip->node) {
  794. chip->node = of_find_node_by_name(NULL, "i2s-a");
  795. if (chip->node && chip->node->parent &&
  796. chip->node->parent->parent) {
  797. if (of_device_is_compatible(chip->node->parent->parent,
  798. "K2-Keylargo"))
  799. chip->is_k2 = 1;
  800. }
  801. }
  802. if (! chip->node)
  803. return -ENODEV;
  804. if (!sound) {
  805. for_each_node_by_name(sound, "sound")
  806. if (sound->parent == chip->node)
  807. break;
  808. }
  809. if (! sound) {
  810. of_node_put(chip->node);
  811. chip->node = NULL;
  812. return -ENODEV;
  813. }
  814. prop = of_get_property(sound, "sub-frame", NULL);
  815. if (prop && *prop < 16)
  816. chip->subframe = *prop;
  817. prop = of_get_property(sound, "layout-id", NULL);
  818. if (prop) {
  819. /* partly deprecate snd-powermac, for those machines
  820. * that have a layout-id property for now */
  821. printk(KERN_INFO "snd-powermac no longer handles any "
  822. "machines with a layout-id property "
  823. "in the device-tree, use snd-aoa.\n");
  824. of_node_put(sound);
  825. of_node_put(chip->node);
  826. chip->node = NULL;
  827. return -ENODEV;
  828. }
  829. /* This should be verified on older screamers */
  830. if (of_device_is_compatible(sound, "screamer")) {
  831. chip->model = PMAC_SCREAMER;
  832. // chip->can_byte_swap = 0; /* FIXME: check this */
  833. }
  834. if (of_device_is_compatible(sound, "burgundy")) {
  835. chip->model = PMAC_BURGUNDY;
  836. chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
  837. }
  838. if (of_device_is_compatible(sound, "daca")) {
  839. chip->model = PMAC_DACA;
  840. chip->can_capture = 0; /* no capture */
  841. chip->can_duplex = 0;
  842. // chip->can_byte_swap = 0; /* FIXME: check this */
  843. chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
  844. }
  845. if (of_device_is_compatible(sound, "tumbler")) {
  846. chip->model = PMAC_TUMBLER;
  847. chip->can_capture = of_machine_is_compatible("PowerMac4,2")
  848. || of_machine_is_compatible("PowerBook3,2")
  849. || of_machine_is_compatible("PowerBook3,3")
  850. || of_machine_is_compatible("PowerBook4,1")
  851. || of_machine_is_compatible("PowerBook4,2")
  852. || of_machine_is_compatible("PowerBook4,3");
  853. chip->can_duplex = 0;
  854. // chip->can_byte_swap = 0; /* FIXME: check this */
  855. chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
  856. chip->freq_table = tumbler_freqs;
  857. chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
  858. }
  859. if (of_device_is_compatible(sound, "snapper")) {
  860. chip->model = PMAC_SNAPPER;
  861. // chip->can_byte_swap = 0; /* FIXME: check this */
  862. chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
  863. chip->freq_table = tumbler_freqs;
  864. chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
  865. }
  866. prop = of_get_property(sound, "device-id", NULL);
  867. if (prop)
  868. chip->device_id = *prop;
  869. dn = of_find_node_by_name(NULL, "perch");
  870. chip->has_iic = (dn != NULL);
  871. of_node_put(dn);
  872. /* We need the PCI device for DMA allocations, let's use a crude method
  873. * for now ...
  874. */
  875. macio = macio_find(chip->node, macio_unknown);
  876. if (macio == NULL)
  877. printk(KERN_WARNING "snd-powermac: can't locate macio !\n");
  878. else {
  879. struct pci_dev *pdev = NULL;
  880. for_each_pci_dev(pdev) {
  881. struct device_node *np = pci_device_to_OF_node(pdev);
  882. if (np && np == macio->of_node) {
  883. chip->pdev = pdev;
  884. break;
  885. }
  886. }
  887. }
  888. if (chip->pdev == NULL)
  889. printk(KERN_WARNING "snd-powermac: can't locate macio PCI"
  890. " device !\n");
  891. detect_byte_swap(chip);
  892. /* look for a property saying what sample rates
  893. are available */
  894. prop = of_get_property(sound, "sample-rates", &l);
  895. if (! prop)
  896. prop = of_get_property(sound, "output-frame-rates", &l);
  897. if (prop) {
  898. int i;
  899. chip->freqs_ok = 0;
  900. for (l /= sizeof(int); l > 0; --l) {
  901. unsigned int r = *prop++;
  902. /* Apple 'Fixed' format */
  903. if (r >= 0x10000)
  904. r >>= 16;
  905. for (i = 0; i < chip->num_freqs; ++i) {
  906. if (r == chip->freq_table[i]) {
  907. chip->freqs_ok |= (1 << i);
  908. break;
  909. }
  910. }
  911. }
  912. } else {
  913. /* assume only 44.1khz */
  914. chip->freqs_ok = 1;
  915. }
  916. of_node_put(sound);
  917. return 0;
  918. }
  919. #ifdef PMAC_SUPPORT_AUTOMUTE
  920. /*
  921. * auto-mute
  922. */
  923. static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol,
  924. struct snd_ctl_elem_value *ucontrol)
  925. {
  926. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  927. ucontrol->value.integer.value[0] = chip->auto_mute;
  928. return 0;
  929. }
  930. static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol,
  931. struct snd_ctl_elem_value *ucontrol)
  932. {
  933. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  934. if (ucontrol->value.integer.value[0] != chip->auto_mute) {
  935. chip->auto_mute = !!ucontrol->value.integer.value[0];
  936. if (chip->update_automute)
  937. chip->update_automute(chip, 1);
  938. return 1;
  939. }
  940. return 0;
  941. }
  942. static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol,
  943. struct snd_ctl_elem_value *ucontrol)
  944. {
  945. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  946. if (chip->detect_headphone)
  947. ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
  948. else
  949. ucontrol->value.integer.value[0] = 0;
  950. return 0;
  951. }
  952. static const struct snd_kcontrol_new auto_mute_controls[] = {
  953. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  954. .name = "Auto Mute Switch",
  955. .info = snd_pmac_boolean_mono_info,
  956. .get = pmac_auto_mute_get,
  957. .put = pmac_auto_mute_put,
  958. },
  959. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  960. .name = "Headphone Detection",
  961. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  962. .info = snd_pmac_boolean_mono_info,
  963. .get = pmac_hp_detect_get,
  964. },
  965. };
  966. int snd_pmac_add_automute(struct snd_pmac *chip)
  967. {
  968. int err;
  969. chip->auto_mute = 1;
  970. err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
  971. if (err < 0) {
  972. printk(KERN_ERR "snd-powermac: Failed to add automute control\n");
  973. return err;
  974. }
  975. chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
  976. return snd_ctl_add(chip->card, chip->hp_detect_ctl);
  977. }
  978. #endif /* PMAC_SUPPORT_AUTOMUTE */
  979. /*
  980. * create and detect a pmac chip record
  981. */
  982. int snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return)
  983. {
  984. struct snd_pmac *chip;
  985. struct device_node *np;
  986. int i, err;
  987. unsigned int irq;
  988. unsigned long ctrl_addr, txdma_addr, rxdma_addr;
  989. static const struct snd_device_ops ops = {
  990. .dev_free = snd_pmac_dev_free,
  991. };
  992. *chip_return = NULL;
  993. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  994. if (chip == NULL)
  995. return -ENOMEM;
  996. chip->card = card;
  997. spin_lock_init(&chip->reg_lock);
  998. chip->irq = chip->tx_irq = chip->rx_irq = -1;
  999. chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
  1000. chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
  1001. err = snd_pmac_detect(chip);
  1002. if (err < 0)
  1003. goto __error;
  1004. if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
  1005. snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
  1006. snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0 ||
  1007. snd_pmac_dbdma_alloc(chip, &emergency_dbdma, 2) < 0) {
  1008. err = -ENOMEM;
  1009. goto __error;
  1010. }
  1011. np = chip->node;
  1012. chip->requested = 0;
  1013. if (chip->is_k2) {
  1014. static const char * const rnames[] = {
  1015. "Sound Control", "Sound DMA" };
  1016. for (i = 0; i < 2; i ++) {
  1017. if (of_address_to_resource(np->parent, i,
  1018. &chip->rsrc[i])) {
  1019. printk(KERN_ERR "snd: can't translate rsrc "
  1020. " %d (%s)\n", i, rnames[i]);
  1021. err = -ENODEV;
  1022. goto __error;
  1023. }
  1024. if (request_mem_region(chip->rsrc[i].start,
  1025. resource_size(&chip->rsrc[i]),
  1026. rnames[i]) == NULL) {
  1027. printk(KERN_ERR "snd: can't request rsrc "
  1028. " %d (%s: %pR)\n",
  1029. i, rnames[i], &chip->rsrc[i]);
  1030. err = -ENODEV;
  1031. goto __error;
  1032. }
  1033. chip->requested |= (1 << i);
  1034. }
  1035. ctrl_addr = chip->rsrc[0].start;
  1036. txdma_addr = chip->rsrc[1].start;
  1037. rxdma_addr = txdma_addr + 0x100;
  1038. } else {
  1039. static const char * const rnames[] = {
  1040. "Sound Control", "Sound Tx DMA", "Sound Rx DMA" };
  1041. for (i = 0; i < 3; i ++) {
  1042. if (of_address_to_resource(np, i,
  1043. &chip->rsrc[i])) {
  1044. printk(KERN_ERR "snd: can't translate rsrc "
  1045. " %d (%s)\n", i, rnames[i]);
  1046. err = -ENODEV;
  1047. goto __error;
  1048. }
  1049. if (request_mem_region(chip->rsrc[i].start,
  1050. resource_size(&chip->rsrc[i]),
  1051. rnames[i]) == NULL) {
  1052. printk(KERN_ERR "snd: can't request rsrc "
  1053. " %d (%s: %pR)\n",
  1054. i, rnames[i], &chip->rsrc[i]);
  1055. err = -ENODEV;
  1056. goto __error;
  1057. }
  1058. chip->requested |= (1 << i);
  1059. }
  1060. ctrl_addr = chip->rsrc[0].start;
  1061. txdma_addr = chip->rsrc[1].start;
  1062. rxdma_addr = chip->rsrc[2].start;
  1063. }
  1064. chip->awacs = ioremap(ctrl_addr, 0x1000);
  1065. chip->playback.dma = ioremap(txdma_addr, 0x100);
  1066. chip->capture.dma = ioremap(rxdma_addr, 0x100);
  1067. if (chip->model <= PMAC_BURGUNDY) {
  1068. irq = irq_of_parse_and_map(np, 0);
  1069. if (request_irq(irq, snd_pmac_ctrl_intr, 0,
  1070. "PMac", (void*)chip)) {
  1071. snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n",
  1072. irq);
  1073. err = -EBUSY;
  1074. goto __error;
  1075. }
  1076. chip->irq = irq;
  1077. }
  1078. irq = irq_of_parse_and_map(np, 1);
  1079. if (request_irq(irq, snd_pmac_tx_intr, 0, "PMac Output", (void*)chip)){
  1080. snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
  1081. err = -EBUSY;
  1082. goto __error;
  1083. }
  1084. chip->tx_irq = irq;
  1085. irq = irq_of_parse_and_map(np, 2);
  1086. if (request_irq(irq, snd_pmac_rx_intr, 0, "PMac Input", (void*)chip)) {
  1087. snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
  1088. err = -EBUSY;
  1089. goto __error;
  1090. }
  1091. chip->rx_irq = irq;
  1092. snd_pmac_sound_feature(chip, 1);
  1093. /* reset & enable interrupts */
  1094. if (chip->model <= PMAC_BURGUNDY)
  1095. out_le32(&chip->awacs->control, chip->control_mask);
  1096. /* Powerbooks have odd ways of enabling inputs such as
  1097. an expansion-bay CD or sound from an internal modem
  1098. or a PC-card modem. */
  1099. if (chip->is_pbook_3400) {
  1100. /* Enable CD and PC-card sound inputs. */
  1101. /* This is done by reading from address
  1102. * f301a000, + 0x10 to enable the expansion-bay
  1103. * CD sound input, + 0x80 to enable the PC-card
  1104. * sound input. The 0x100 enables the SCSI bus
  1105. * terminator power.
  1106. */
  1107. chip->latch_base = ioremap (0xf301a000, 0x1000);
  1108. in_8(chip->latch_base + 0x190);
  1109. } else if (chip->is_pbook_G3) {
  1110. struct device_node* mio;
  1111. for (mio = chip->node->parent; mio; mio = mio->parent) {
  1112. if (of_node_name_eq(mio, "mac-io")) {
  1113. struct resource r;
  1114. if (of_address_to_resource(mio, 0, &r) == 0)
  1115. chip->macio_base =
  1116. ioremap(r.start, 0x40);
  1117. break;
  1118. }
  1119. }
  1120. /* Enable CD sound input. */
  1121. /* The relevant bits for writing to this byte are 0x8f.
  1122. * I haven't found out what the 0x80 bit does.
  1123. * For the 0xf bits, writing 3 or 7 enables the CD
  1124. * input, any other value disables it. Values
  1125. * 1, 3, 5, 7 enable the microphone. Values 0, 2,
  1126. * 4, 6, 8 - f enable the input from the modem.
  1127. */
  1128. if (chip->macio_base)
  1129. out_8(chip->macio_base + 0x37, 3);
  1130. }
  1131. /* Reset dbdma channels */
  1132. snd_pmac_dbdma_reset(chip);
  1133. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  1134. if (err < 0)
  1135. goto __error;
  1136. *chip_return = chip;
  1137. return 0;
  1138. __error:
  1139. snd_pmac_free(chip);
  1140. return err;
  1141. }
  1142. /*
  1143. * sleep notify for powerbook
  1144. */
  1145. #ifdef CONFIG_PM
  1146. /*
  1147. * Save state when going to sleep, restore it afterwards.
  1148. */
  1149. void snd_pmac_suspend(struct snd_pmac *chip)
  1150. {
  1151. unsigned long flags;
  1152. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
  1153. if (chip->suspend)
  1154. chip->suspend(chip);
  1155. spin_lock_irqsave(&chip->reg_lock, flags);
  1156. snd_pmac_beep_stop(chip);
  1157. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1158. if (chip->irq >= 0)
  1159. disable_irq(chip->irq);
  1160. if (chip->tx_irq >= 0)
  1161. disable_irq(chip->tx_irq);
  1162. if (chip->rx_irq >= 0)
  1163. disable_irq(chip->rx_irq);
  1164. snd_pmac_sound_feature(chip, 0);
  1165. }
  1166. void snd_pmac_resume(struct snd_pmac *chip)
  1167. {
  1168. snd_pmac_sound_feature(chip, 1);
  1169. if (chip->resume)
  1170. chip->resume(chip);
  1171. /* enable CD sound input */
  1172. if (chip->macio_base && chip->is_pbook_G3)
  1173. out_8(chip->macio_base + 0x37, 3);
  1174. else if (chip->is_pbook_3400)
  1175. in_8(chip->latch_base + 0x190);
  1176. snd_pmac_pcm_set_format(chip);
  1177. if (chip->irq >= 0)
  1178. enable_irq(chip->irq);
  1179. if (chip->tx_irq >= 0)
  1180. enable_irq(chip->tx_irq);
  1181. if (chip->rx_irq >= 0)
  1182. enable_irq(chip->rx_irq);
  1183. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
  1184. }
  1185. #endif /* CONFIG_PM */