pmac.c 37 KB

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