pmac.c 35 KB

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