pmac.c 36 KB

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