pmac.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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. chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
  859. chip->model = PMAC_SNAPPER;
  860. chip->can_byte_swap = 0; /* FIXME: check this */
  861. chip->control_mask = MASK_IEPC | 0x11;/* disable IEE */
  862. break;
  863. case 0x3a:
  864. chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
  865. chip->model = PMAC_TOONIE;
  866. chip->can_byte_swap = 0; /* FIXME: check this */
  867. chip->control_mask = MASK_IEPC | 0x11;/* disable IEE */
  868. break;
  869. }
  870. }
  871. prop = (unsigned int *)get_property(sound, "device-id", NULL);
  872. if (prop)
  873. chip->device_id = *prop;
  874. chip->has_iic = (find_devices("perch") != NULL);
  875. /* We need the PCI device for DMA allocations, let's use a crude method
  876. * for now ...
  877. */
  878. macio = macio_find(chip->node, macio_unknown);
  879. if (macio == NULL)
  880. printk(KERN_WARNING "snd-powermac: can't locate macio !\n");
  881. else {
  882. struct pci_dev *pdev = NULL;
  883. for_each_pci_dev(pdev) {
  884. struct device_node *np = pci_device_to_OF_node(pdev);
  885. if (np && np == macio->of_node) {
  886. chip->pdev = pdev;
  887. break;
  888. }
  889. }
  890. }
  891. if (chip->pdev == NULL)
  892. printk(KERN_WARNING "snd-powermac: can't locate macio PCI"
  893. " device !\n");
  894. detect_byte_swap(chip);
  895. /* look for a property saying what sample rates
  896. are available */
  897. prop = (unsigned int *) get_property(sound, "sample-rates", &l);
  898. if (! prop)
  899. prop = (unsigned int *) get_property(sound,
  900. "output-frame-rates", &l);
  901. if (prop) {
  902. int i;
  903. chip->freqs_ok = 0;
  904. for (l /= sizeof(int); l > 0; --l) {
  905. unsigned int r = *prop++;
  906. /* Apple 'Fixed' format */
  907. if (r >= 0x10000)
  908. r >>= 16;
  909. for (i = 0; i < chip->num_freqs; ++i) {
  910. if (r == chip->freq_table[i]) {
  911. chip->freqs_ok |= (1 << i);
  912. break;
  913. }
  914. }
  915. }
  916. } else {
  917. /* assume only 44.1khz */
  918. chip->freqs_ok = 1;
  919. }
  920. return 0;
  921. }
  922. /*
  923. * exported - boolean info callbacks for ease of programming
  924. */
  925. int snd_pmac_boolean_stereo_info(snd_kcontrol_t *kcontrol,
  926. snd_ctl_elem_info_t *uinfo)
  927. {
  928. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  929. uinfo->count = 2;
  930. uinfo->value.integer.min = 0;
  931. uinfo->value.integer.max = 1;
  932. return 0;
  933. }
  934. int snd_pmac_boolean_mono_info(snd_kcontrol_t *kcontrol,
  935. snd_ctl_elem_info_t *uinfo)
  936. {
  937. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  938. uinfo->count = 1;
  939. uinfo->value.integer.min = 0;
  940. uinfo->value.integer.max = 1;
  941. return 0;
  942. }
  943. #ifdef PMAC_SUPPORT_AUTOMUTE
  944. /*
  945. * auto-mute
  946. */
  947. static int pmac_auto_mute_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  948. {
  949. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  950. ucontrol->value.integer.value[0] = chip->auto_mute;
  951. return 0;
  952. }
  953. static int pmac_auto_mute_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  954. {
  955. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  956. if (ucontrol->value.integer.value[0] != chip->auto_mute) {
  957. chip->auto_mute = ucontrol->value.integer.value[0];
  958. if (chip->update_automute)
  959. chip->update_automute(chip, 1);
  960. return 1;
  961. }
  962. return 0;
  963. }
  964. static int pmac_hp_detect_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  965. {
  966. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  967. if (chip->detect_headphone)
  968. ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
  969. else
  970. ucontrol->value.integer.value[0] = 0;
  971. return 0;
  972. }
  973. static snd_kcontrol_new_t auto_mute_controls[] __initdata = {
  974. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  975. .name = "Auto Mute Switch",
  976. .info = snd_pmac_boolean_mono_info,
  977. .get = pmac_auto_mute_get,
  978. .put = pmac_auto_mute_put,
  979. },
  980. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  981. .name = "Headphone Detection",
  982. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  983. .info = snd_pmac_boolean_mono_info,
  984. .get = pmac_hp_detect_get,
  985. },
  986. };
  987. int __init snd_pmac_add_automute(pmac_t *chip)
  988. {
  989. int err;
  990. chip->auto_mute = 1;
  991. err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
  992. if (err < 0) {
  993. printk(KERN_ERR "snd-powermac: Failed to add automute control\n");
  994. return err;
  995. }
  996. chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
  997. return snd_ctl_add(chip->card, chip->hp_detect_ctl);
  998. }
  999. #endif /* PMAC_SUPPORT_AUTOMUTE */
  1000. /*
  1001. * create and detect a pmac chip record
  1002. */
  1003. int __init snd_pmac_new(snd_card_t *card, pmac_t **chip_return)
  1004. {
  1005. pmac_t *chip;
  1006. struct device_node *np;
  1007. int i, err;
  1008. unsigned long ctrl_addr, txdma_addr, rxdma_addr;
  1009. static snd_device_ops_t ops = {
  1010. .dev_free = snd_pmac_dev_free,
  1011. };
  1012. snd_runtime_check(chip_return, return -EINVAL);
  1013. *chip_return = NULL;
  1014. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  1015. if (chip == NULL)
  1016. return -ENOMEM;
  1017. chip->card = card;
  1018. spin_lock_init(&chip->reg_lock);
  1019. chip->irq = chip->tx_irq = chip->rx_irq = -1;
  1020. chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
  1021. chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
  1022. if ((err = snd_pmac_detect(chip)) < 0)
  1023. goto __error;
  1024. if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
  1025. snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
  1026. snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0) {
  1027. err = -ENOMEM;
  1028. goto __error;
  1029. }
  1030. np = chip->node;
  1031. if (chip->is_k2) {
  1032. if (np->parent->n_addrs < 2 || np->n_intrs < 3) {
  1033. err = -ENODEV;
  1034. goto __error;
  1035. }
  1036. for (i = 0; i < 2; i++) {
  1037. #ifndef CONFIG_PPC64
  1038. static char *name[2] = { "- Control", "- DMA" };
  1039. if (! request_OF_resource(np->parent, i, name[i])) {
  1040. snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
  1041. err = -ENODEV;
  1042. goto __error;
  1043. }
  1044. chip->of_requested |= (1 << i);
  1045. #endif /* CONFIG_PPC64 */
  1046. ctrl_addr = np->parent->addrs[0].address;
  1047. txdma_addr = np->parent->addrs[1].address;
  1048. rxdma_addr = txdma_addr + 0x100;
  1049. }
  1050. } else {
  1051. if (np->n_addrs < 3 || np->n_intrs < 3) {
  1052. err = -ENODEV;
  1053. goto __error;
  1054. }
  1055. for (i = 0; i < 3; i++) {
  1056. #ifndef CONFIG_PPC64
  1057. static char *name[3] = { "- Control", "- Tx DMA", "- Rx DMA" };
  1058. if (! request_OF_resource(np, i, name[i])) {
  1059. snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
  1060. err = -ENODEV;
  1061. goto __error;
  1062. }
  1063. chip->of_requested |= (1 << i);
  1064. #endif /* CONFIG_PPC64 */
  1065. ctrl_addr = np->addrs[0].address;
  1066. txdma_addr = np->addrs[1].address;
  1067. rxdma_addr = np->addrs[2].address;
  1068. }
  1069. }
  1070. chip->awacs = ioremap(ctrl_addr, 0x1000);
  1071. chip->playback.dma = ioremap(txdma_addr, 0x100);
  1072. chip->capture.dma = ioremap(rxdma_addr, 0x100);
  1073. if (chip->model <= PMAC_BURGUNDY) {
  1074. if (request_irq(np->intrs[0].line, snd_pmac_ctrl_intr, 0,
  1075. "PMac", (void*)chip)) {
  1076. snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[0].line);
  1077. err = -EBUSY;
  1078. goto __error;
  1079. }
  1080. chip->irq = np->intrs[0].line;
  1081. }
  1082. if (request_irq(np->intrs[1].line, snd_pmac_tx_intr, 0,
  1083. "PMac Output", (void*)chip)) {
  1084. snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[1].line);
  1085. err = -EBUSY;
  1086. goto __error;
  1087. }
  1088. chip->tx_irq = np->intrs[1].line;
  1089. if (request_irq(np->intrs[2].line, snd_pmac_rx_intr, 0,
  1090. "PMac Input", (void*)chip)) {
  1091. snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[2].line);
  1092. err = -EBUSY;
  1093. goto __error;
  1094. }
  1095. chip->rx_irq = np->intrs[2].line;
  1096. snd_pmac_sound_feature(chip, 1);
  1097. /* reset */
  1098. if (chip->model == PMAC_AWACS)
  1099. out_le32(&chip->awacs->control, 0x11);
  1100. /* Powerbooks have odd ways of enabling inputs such as
  1101. an expansion-bay CD or sound from an internal modem
  1102. or a PC-card modem. */
  1103. if (chip->is_pbook_3400) {
  1104. /* Enable CD and PC-card sound inputs. */
  1105. /* This is done by reading from address
  1106. * f301a000, + 0x10 to enable the expansion-bay
  1107. * CD sound input, + 0x80 to enable the PC-card
  1108. * sound input. The 0x100 enables the SCSI bus
  1109. * terminator power.
  1110. */
  1111. chip->latch_base = ioremap (0xf301a000, 0x1000);
  1112. in_8(chip->latch_base + 0x190);
  1113. } else if (chip->is_pbook_G3) {
  1114. struct device_node* mio;
  1115. for (mio = chip->node->parent; mio; mio = mio->parent) {
  1116. if (strcmp(mio->name, "mac-io") == 0
  1117. && mio->n_addrs > 0) {
  1118. chip->macio_base = ioremap(mio->addrs[0].address, 0x40);
  1119. break;
  1120. }
  1121. }
  1122. /* Enable CD sound input. */
  1123. /* The relevant bits for writing to this byte are 0x8f.
  1124. * I haven't found out what the 0x80 bit does.
  1125. * For the 0xf bits, writing 3 or 7 enables the CD
  1126. * input, any other value disables it. Values
  1127. * 1, 3, 5, 7 enable the microphone. Values 0, 2,
  1128. * 4, 6, 8 - f enable the input from the modem.
  1129. */
  1130. if (chip->macio_base)
  1131. out_8(chip->macio_base + 0x37, 3);
  1132. }
  1133. /* Reset dbdma channels */
  1134. snd_pmac_dbdma_reset(chip);
  1135. #ifdef CONFIG_PM
  1136. /* add sleep notifier */
  1137. if (! snd_pmac_register_sleep_notifier(chip))
  1138. snd_card_set_pm_callback(chip->card, snd_pmac_suspend, snd_pmac_resume, chip);
  1139. #endif
  1140. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
  1141. goto __error;
  1142. *chip_return = chip;
  1143. return 0;
  1144. __error:
  1145. if (chip->pdev)
  1146. pci_dev_put(chip->pdev);
  1147. snd_pmac_free(chip);
  1148. return err;
  1149. }
  1150. /*
  1151. * sleep notify for powerbook
  1152. */
  1153. #ifdef CONFIG_PM
  1154. /*
  1155. * Save state when going to sleep, restore it afterwards.
  1156. */
  1157. static int snd_pmac_suspend(snd_card_t *card, pm_message_t state)
  1158. {
  1159. pmac_t *chip = card->pm_private_data;
  1160. unsigned long flags;
  1161. if (chip->suspend)
  1162. chip->suspend(chip);
  1163. snd_pcm_suspend_all(chip->pcm);
  1164. spin_lock_irqsave(&chip->reg_lock, flags);
  1165. snd_pmac_beep_stop(chip);
  1166. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1167. if (chip->irq >= 0)
  1168. disable_irq(chip->irq);
  1169. if (chip->tx_irq >= 0)
  1170. disable_irq(chip->tx_irq);
  1171. if (chip->rx_irq >= 0)
  1172. disable_irq(chip->rx_irq);
  1173. snd_pmac_sound_feature(chip, 0);
  1174. return 0;
  1175. }
  1176. static int snd_pmac_resume(snd_card_t *card)
  1177. {
  1178. pmac_t *chip = card->pm_private_data;
  1179. snd_pmac_sound_feature(chip, 1);
  1180. if (chip->resume)
  1181. chip->resume(chip);
  1182. /* enable CD sound input */
  1183. if (chip->macio_base && chip->is_pbook_G3) {
  1184. out_8(chip->macio_base + 0x37, 3);
  1185. } else if (chip->is_pbook_3400) {
  1186. in_8(chip->latch_base + 0x190);
  1187. }
  1188. snd_pmac_pcm_set_format(chip);
  1189. if (chip->irq >= 0)
  1190. enable_irq(chip->irq);
  1191. if (chip->tx_irq >= 0)
  1192. enable_irq(chip->tx_irq);
  1193. if (chip->rx_irq >= 0)
  1194. enable_irq(chip->rx_irq);
  1195. return 0;
  1196. }
  1197. /* the chip is stored statically by snd_pmac_register_sleep_notifier
  1198. * because we can't have any private data for notify callback.
  1199. */
  1200. static pmac_t *sleeping_pmac = NULL;
  1201. static int snd_pmac_sleep_notify(struct pmu_sleep_notifier *self, int when)
  1202. {
  1203. pmac_t *chip;
  1204. chip = sleeping_pmac;
  1205. snd_runtime_check(chip, return 0);
  1206. switch (when) {
  1207. case PBOOK_SLEEP_NOW:
  1208. snd_pmac_suspend(chip->card, PMSG_SUSPEND);
  1209. break;
  1210. case PBOOK_WAKE:
  1211. snd_pmac_resume(chip->card);
  1212. break;
  1213. }
  1214. return PBOOK_SLEEP_OK;
  1215. }
  1216. static struct pmu_sleep_notifier snd_pmac_sleep_notifier = {
  1217. snd_pmac_sleep_notify, SLEEP_LEVEL_SOUND,
  1218. };
  1219. static int __init snd_pmac_register_sleep_notifier(pmac_t *chip)
  1220. {
  1221. /* should be protected here.. */
  1222. snd_assert(! sleeping_pmac, return -EBUSY);
  1223. sleeping_pmac = chip;
  1224. pmu_register_sleep_notifier(&snd_pmac_sleep_notifier);
  1225. return 0;
  1226. }
  1227. static int snd_pmac_unregister_sleep_notifier(pmac_t *chip)
  1228. {
  1229. /* should be protected here.. */
  1230. snd_assert(sleeping_pmac == chip, return -ENODEV);
  1231. pmu_unregister_sleep_notifier(&snd_pmac_sleep_notifier);
  1232. sleeping_pmac = NULL;
  1233. return 0;
  1234. }
  1235. #endif /* CONFIG_PM */