pmac.c 35 KB

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