pmac.c 36 KB

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