pmac.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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;
  433. /* look up frequency table and fill bit mask */
  434. runtime->hw.rates = 0;
  435. for (i = 0; i < chip->num_freqs; i++)
  436. if (chip->freqs_ok & (1 << i))
  437. runtime->hw.rates |=
  438. snd_pcm_rate_to_rate_bit(chip->freq_table[i]);
  439. /* check for minimum and maximum rates */
  440. for (i = 0; i < chip->num_freqs; i++) {
  441. if (chip->freqs_ok & (1 << i)) {
  442. runtime->hw.rate_max = chip->freq_table[i];
  443. break;
  444. }
  445. }
  446. for (i = chip->num_freqs - 1; i >= 0; i--) {
  447. if (chip->freqs_ok & (1 << i)) {
  448. runtime->hw.rate_min = chip->freq_table[i];
  449. break;
  450. }
  451. }
  452. runtime->hw.formats = chip->formats_ok;
  453. if (chip->can_capture) {
  454. if (! chip->can_duplex)
  455. runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
  456. runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  457. }
  458. runtime->private_data = rec;
  459. rec->substream = subs;
  460. #if 0 /* FIXME: still under development.. */
  461. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  462. snd_pmac_hw_rule_rate, chip, rec->stream, -1);
  463. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  464. snd_pmac_hw_rule_format, chip, rec->stream, -1);
  465. #endif
  466. runtime->hw.periods_max = rec->cmd.size - 1;
  467. /* constraints to fix choppy sound */
  468. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  469. return 0;
  470. }
  471. static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec,
  472. struct snd_pcm_substream *subs)
  473. {
  474. struct pmac_stream *astr;
  475. snd_pmac_dma_stop(rec);
  476. astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
  477. if (! astr)
  478. return -EINVAL;
  479. /* reset constraints */
  480. astr->cur_freqs = chip->freqs_ok;
  481. astr->cur_formats = chip->formats_ok;
  482. return 0;
  483. }
  484. static int snd_pmac_playback_open(struct snd_pcm_substream *subs)
  485. {
  486. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  487. subs->runtime->hw = snd_pmac_playback;
  488. return snd_pmac_pcm_open(chip, &chip->playback, subs);
  489. }
  490. static int snd_pmac_capture_open(struct snd_pcm_substream *subs)
  491. {
  492. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  493. subs->runtime->hw = snd_pmac_capture;
  494. return snd_pmac_pcm_open(chip, &chip->capture, subs);
  495. }
  496. static int snd_pmac_playback_close(struct snd_pcm_substream *subs)
  497. {
  498. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  499. return snd_pmac_pcm_close(chip, &chip->playback, subs);
  500. }
  501. static int snd_pmac_capture_close(struct snd_pcm_substream *subs)
  502. {
  503. struct snd_pmac *chip = snd_pcm_substream_chip(subs);
  504. return snd_pmac_pcm_close(chip, &chip->capture, subs);
  505. }
  506. /*
  507. */
  508. static struct snd_pcm_ops snd_pmac_playback_ops = {
  509. .open = snd_pmac_playback_open,
  510. .close = snd_pmac_playback_close,
  511. .ioctl = snd_pcm_lib_ioctl,
  512. .hw_params = snd_pmac_pcm_hw_params,
  513. .hw_free = snd_pmac_pcm_hw_free,
  514. .prepare = snd_pmac_playback_prepare,
  515. .trigger = snd_pmac_playback_trigger,
  516. .pointer = snd_pmac_playback_pointer,
  517. };
  518. static struct snd_pcm_ops snd_pmac_capture_ops = {
  519. .open = snd_pmac_capture_open,
  520. .close = snd_pmac_capture_close,
  521. .ioctl = snd_pcm_lib_ioctl,
  522. .hw_params = snd_pmac_pcm_hw_params,
  523. .hw_free = snd_pmac_pcm_hw_free,
  524. .prepare = snd_pmac_capture_prepare,
  525. .trigger = snd_pmac_capture_trigger,
  526. .pointer = snd_pmac_capture_pointer,
  527. };
  528. int __init snd_pmac_pcm_new(struct snd_pmac *chip)
  529. {
  530. struct snd_pcm *pcm;
  531. int err;
  532. int num_captures = 1;
  533. if (! chip->can_capture)
  534. num_captures = 0;
  535. err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
  536. if (err < 0)
  537. return err;
  538. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
  539. if (chip->can_capture)
  540. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
  541. pcm->private_data = chip;
  542. pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  543. strcpy(pcm->name, chip->card->shortname);
  544. chip->pcm = pcm;
  545. chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
  546. if (chip->can_byte_swap)
  547. chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
  548. chip->playback.cur_formats = chip->formats_ok;
  549. chip->capture.cur_formats = chip->formats_ok;
  550. chip->playback.cur_freqs = chip->freqs_ok;
  551. chip->capture.cur_freqs = chip->freqs_ok;
  552. /* preallocate 64k buffer */
  553. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  554. &chip->pdev->dev,
  555. 64 * 1024, 64 * 1024);
  556. return 0;
  557. }
  558. static void snd_pmac_dbdma_reset(struct snd_pmac *chip)
  559. {
  560. out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
  561. snd_pmac_wait_ack(&chip->playback);
  562. out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
  563. snd_pmac_wait_ack(&chip->capture);
  564. }
  565. /*
  566. * handling beep
  567. */
  568. void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed)
  569. {
  570. struct pmac_stream *rec = &chip->playback;
  571. snd_pmac_dma_stop(rec);
  572. st_le16(&chip->extra_dma.cmds->req_count, bytes);
  573. st_le16(&chip->extra_dma.cmds->xfer_status, 0);
  574. st_le32(&chip->extra_dma.cmds->cmd_dep, chip->extra_dma.addr);
  575. st_le32(&chip->extra_dma.cmds->phy_addr, addr);
  576. st_le16(&chip->extra_dma.cmds->command, OUTPUT_MORE + BR_ALWAYS);
  577. out_le32(&chip->awacs->control,
  578. (in_le32(&chip->awacs->control) & ~0x1f00)
  579. | (speed << 8));
  580. out_le32(&chip->awacs->byteswap, 0);
  581. snd_pmac_dma_set_command(rec, &chip->extra_dma);
  582. snd_pmac_dma_run(rec, RUN);
  583. }
  584. void snd_pmac_beep_dma_stop(struct snd_pmac *chip)
  585. {
  586. snd_pmac_dma_stop(&chip->playback);
  587. st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
  588. snd_pmac_pcm_set_format(chip); /* reset format */
  589. }
  590. /*
  591. * interrupt handlers
  592. */
  593. static irqreturn_t
  594. snd_pmac_tx_intr(int irq, void *devid)
  595. {
  596. struct snd_pmac *chip = devid;
  597. snd_pmac_pcm_update(chip, &chip->playback);
  598. return IRQ_HANDLED;
  599. }
  600. static irqreturn_t
  601. snd_pmac_rx_intr(int irq, void *devid)
  602. {
  603. struct snd_pmac *chip = devid;
  604. snd_pmac_pcm_update(chip, &chip->capture);
  605. return IRQ_HANDLED;
  606. }
  607. static irqreturn_t
  608. snd_pmac_ctrl_intr(int irq, void *devid)
  609. {
  610. struct snd_pmac *chip = devid;
  611. int ctrl = in_le32(&chip->awacs->control);
  612. /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
  613. if (ctrl & MASK_PORTCHG) {
  614. /* do something when headphone is plugged/unplugged? */
  615. if (chip->update_automute)
  616. chip->update_automute(chip, 1);
  617. }
  618. if (ctrl & MASK_CNTLERR) {
  619. int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
  620. if (err && chip->model <= PMAC_SCREAMER)
  621. snd_printk(KERN_DEBUG "error %x\n", err);
  622. }
  623. /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
  624. out_le32(&chip->awacs->control, ctrl);
  625. return IRQ_HANDLED;
  626. }
  627. /*
  628. * a wrapper to feature call for compatibility
  629. */
  630. static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable)
  631. {
  632. if (ppc_md.feature_call)
  633. ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
  634. }
  635. /*
  636. * release resources
  637. */
  638. static int snd_pmac_free(struct snd_pmac *chip)
  639. {
  640. /* stop sounds */
  641. if (chip->initialized) {
  642. snd_pmac_dbdma_reset(chip);
  643. /* disable interrupts from awacs interface */
  644. out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
  645. }
  646. if (chip->node)
  647. snd_pmac_sound_feature(chip, 0);
  648. /* clean up mixer if any */
  649. if (chip->mixer_free)
  650. chip->mixer_free(chip);
  651. snd_pmac_detach_beep(chip);
  652. /* release resources */
  653. if (chip->irq >= 0)
  654. free_irq(chip->irq, (void*)chip);
  655. if (chip->tx_irq >= 0)
  656. free_irq(chip->tx_irq, (void*)chip);
  657. if (chip->rx_irq >= 0)
  658. free_irq(chip->rx_irq, (void*)chip);
  659. snd_pmac_dbdma_free(chip, &chip->playback.cmd);
  660. snd_pmac_dbdma_free(chip, &chip->capture.cmd);
  661. snd_pmac_dbdma_free(chip, &chip->extra_dma);
  662. if (chip->macio_base)
  663. iounmap(chip->macio_base);
  664. if (chip->latch_base)
  665. iounmap(chip->latch_base);
  666. if (chip->awacs)
  667. iounmap(chip->awacs);
  668. if (chip->playback.dma)
  669. iounmap(chip->playback.dma);
  670. if (chip->capture.dma)
  671. iounmap(chip->capture.dma);
  672. if (chip->node) {
  673. int i;
  674. for (i = 0; i < 3; i++) {
  675. if (chip->requested & (1 << i))
  676. release_mem_region(chip->rsrc[i].start,
  677. chip->rsrc[i].end -
  678. chip->rsrc[i].start + 1);
  679. }
  680. }
  681. if (chip->pdev)
  682. pci_dev_put(chip->pdev);
  683. of_node_put(chip->node);
  684. kfree(chip);
  685. return 0;
  686. }
  687. /*
  688. * free the device
  689. */
  690. static int snd_pmac_dev_free(struct snd_device *device)
  691. {
  692. struct snd_pmac *chip = device->device_data;
  693. return snd_pmac_free(chip);
  694. }
  695. /*
  696. * check the machine support byteswap (little-endian)
  697. */
  698. static void __init detect_byte_swap(struct snd_pmac *chip)
  699. {
  700. struct device_node *mio;
  701. /* if seems that Keylargo can't byte-swap */
  702. for (mio = chip->node->parent; mio; mio = mio->parent) {
  703. if (strcmp(mio->name, "mac-io") == 0) {
  704. if (of_device_is_compatible(mio, "Keylargo"))
  705. chip->can_byte_swap = 0;
  706. break;
  707. }
  708. }
  709. /* it seems the Pismo & iBook can't byte-swap in hardware. */
  710. if (machine_is_compatible("PowerBook3,1") ||
  711. machine_is_compatible("PowerBook2,1"))
  712. chip->can_byte_swap = 0 ;
  713. if (machine_is_compatible("PowerBook2,1"))
  714. chip->can_duplex = 0;
  715. }
  716. /*
  717. * detect a sound chip
  718. */
  719. static int __init snd_pmac_detect(struct snd_pmac *chip)
  720. {
  721. struct device_node *sound;
  722. struct device_node *dn;
  723. const unsigned int *prop;
  724. unsigned int l;
  725. struct macio_chip* macio;
  726. if (!machine_is(powermac))
  727. return -ENODEV;
  728. chip->subframe = 0;
  729. chip->revision = 0;
  730. chip->freqs_ok = 0xff; /* all ok */
  731. chip->model = PMAC_AWACS;
  732. chip->can_byte_swap = 1;
  733. chip->can_duplex = 1;
  734. chip->can_capture = 1;
  735. chip->num_freqs = ARRAY_SIZE(awacs_freqs);
  736. chip->freq_table = awacs_freqs;
  737. chip->pdev = NULL;
  738. chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
  739. /* check machine type */
  740. if (machine_is_compatible("AAPL,3400/2400")
  741. || machine_is_compatible("AAPL,3500"))
  742. chip->is_pbook_3400 = 1;
  743. else if (machine_is_compatible("PowerBook1,1")
  744. || machine_is_compatible("AAPL,PowerBook1998"))
  745. chip->is_pbook_G3 = 1;
  746. chip->node = of_find_node_by_name(NULL, "awacs");
  747. sound = of_node_get(chip->node);
  748. /*
  749. * powermac G3 models have a node called "davbus"
  750. * with a child called "sound".
  751. */
  752. if (!chip->node)
  753. chip->node = of_find_node_by_name(NULL, "davbus");
  754. /*
  755. * if we didn't find a davbus device, try 'i2s-a' since
  756. * this seems to be what iBooks have
  757. */
  758. if (! chip->node) {
  759. chip->node = of_find_node_by_name(NULL, "i2s-a");
  760. if (chip->node && chip->node->parent &&
  761. chip->node->parent->parent) {
  762. if (of_device_is_compatible(chip->node->parent->parent,
  763. "K2-Keylargo"))
  764. chip->is_k2 = 1;
  765. }
  766. }
  767. if (! chip->node)
  768. return -ENODEV;
  769. if (!sound) {
  770. sound = of_find_node_by_name(NULL, "sound");
  771. while (sound && sound->parent != chip->node)
  772. sound = of_find_node_by_name(sound, "sound");
  773. }
  774. if (! sound) {
  775. of_node_put(chip->node);
  776. chip->node = NULL;
  777. return -ENODEV;
  778. }
  779. prop = of_get_property(sound, "sub-frame", NULL);
  780. if (prop && *prop < 16)
  781. chip->subframe = *prop;
  782. prop = of_get_property(sound, "layout-id", NULL);
  783. if (prop) {
  784. /* partly deprecate snd-powermac, for those machines
  785. * that have a layout-id property for now */
  786. printk(KERN_INFO "snd-powermac no longer handles any "
  787. "machines with a layout-id property "
  788. "in the device-tree, use snd-aoa.\n");
  789. of_node_put(sound);
  790. of_node_put(chip->node);
  791. chip->node = NULL;
  792. return -ENODEV;
  793. }
  794. /* This should be verified on older screamers */
  795. if (of_device_is_compatible(sound, "screamer")) {
  796. chip->model = PMAC_SCREAMER;
  797. // chip->can_byte_swap = 0; /* FIXME: check this */
  798. }
  799. if (of_device_is_compatible(sound, "burgundy")) {
  800. chip->model = PMAC_BURGUNDY;
  801. chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
  802. }
  803. if (of_device_is_compatible(sound, "daca")) {
  804. chip->model = PMAC_DACA;
  805. chip->can_capture = 0; /* no capture */
  806. chip->can_duplex = 0;
  807. // chip->can_byte_swap = 0; /* FIXME: check this */
  808. chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
  809. }
  810. if (of_device_is_compatible(sound, "tumbler")) {
  811. chip->model = PMAC_TUMBLER;
  812. chip->can_capture = 0; /* no capture */
  813. chip->can_duplex = 0;
  814. // chip->can_byte_swap = 0; /* FIXME: check this */
  815. chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
  816. chip->freq_table = tumbler_freqs;
  817. chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
  818. }
  819. if (of_device_is_compatible(sound, "snapper")) {
  820. chip->model = PMAC_SNAPPER;
  821. // chip->can_byte_swap = 0; /* FIXME: check this */
  822. chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
  823. chip->freq_table = tumbler_freqs;
  824. chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
  825. }
  826. prop = of_get_property(sound, "device-id", NULL);
  827. if (prop)
  828. chip->device_id = *prop;
  829. dn = of_find_node_by_name(NULL, "perch");
  830. chip->has_iic = (dn != NULL);
  831. of_node_put(dn);
  832. /* We need the PCI device for DMA allocations, let's use a crude method
  833. * for now ...
  834. */
  835. macio = macio_find(chip->node, macio_unknown);
  836. if (macio == NULL)
  837. printk(KERN_WARNING "snd-powermac: can't locate macio !\n");
  838. else {
  839. struct pci_dev *pdev = NULL;
  840. for_each_pci_dev(pdev) {
  841. struct device_node *np = pci_device_to_OF_node(pdev);
  842. if (np && np == macio->of_node) {
  843. chip->pdev = pdev;
  844. break;
  845. }
  846. }
  847. }
  848. if (chip->pdev == NULL)
  849. printk(KERN_WARNING "snd-powermac: can't locate macio PCI"
  850. " device !\n");
  851. detect_byte_swap(chip);
  852. /* look for a property saying what sample rates
  853. are available */
  854. prop = of_get_property(sound, "sample-rates", &l);
  855. if (! prop)
  856. prop = of_get_property(sound, "output-frame-rates", &l);
  857. if (prop) {
  858. int i;
  859. chip->freqs_ok = 0;
  860. for (l /= sizeof(int); l > 0; --l) {
  861. unsigned int r = *prop++;
  862. /* Apple 'Fixed' format */
  863. if (r >= 0x10000)
  864. r >>= 16;
  865. for (i = 0; i < chip->num_freqs; ++i) {
  866. if (r == chip->freq_table[i]) {
  867. chip->freqs_ok |= (1 << i);
  868. break;
  869. }
  870. }
  871. }
  872. } else {
  873. /* assume only 44.1khz */
  874. chip->freqs_ok = 1;
  875. }
  876. of_node_put(sound);
  877. return 0;
  878. }
  879. #ifdef PMAC_SUPPORT_AUTOMUTE
  880. /*
  881. * auto-mute
  882. */
  883. static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol,
  884. struct snd_ctl_elem_value *ucontrol)
  885. {
  886. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  887. ucontrol->value.integer.value[0] = chip->auto_mute;
  888. return 0;
  889. }
  890. static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol,
  891. struct snd_ctl_elem_value *ucontrol)
  892. {
  893. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  894. if (ucontrol->value.integer.value[0] != chip->auto_mute) {
  895. chip->auto_mute = ucontrol->value.integer.value[0];
  896. if (chip->update_automute)
  897. chip->update_automute(chip, 1);
  898. return 1;
  899. }
  900. return 0;
  901. }
  902. static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol,
  903. struct snd_ctl_elem_value *ucontrol)
  904. {
  905. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  906. if (chip->detect_headphone)
  907. ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
  908. else
  909. ucontrol->value.integer.value[0] = 0;
  910. return 0;
  911. }
  912. static struct snd_kcontrol_new auto_mute_controls[] __initdata = {
  913. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  914. .name = "Auto Mute Switch",
  915. .info = snd_pmac_boolean_mono_info,
  916. .get = pmac_auto_mute_get,
  917. .put = pmac_auto_mute_put,
  918. },
  919. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  920. .name = "Headphone Detection",
  921. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  922. .info = snd_pmac_boolean_mono_info,
  923. .get = pmac_hp_detect_get,
  924. },
  925. };
  926. int __init snd_pmac_add_automute(struct snd_pmac *chip)
  927. {
  928. int err;
  929. chip->auto_mute = 1;
  930. err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
  931. if (err < 0) {
  932. printk(KERN_ERR "snd-powermac: Failed to add automute control\n");
  933. return err;
  934. }
  935. chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
  936. return snd_ctl_add(chip->card, chip->hp_detect_ctl);
  937. }
  938. #endif /* PMAC_SUPPORT_AUTOMUTE */
  939. /*
  940. * create and detect a pmac chip record
  941. */
  942. int __init snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return)
  943. {
  944. struct snd_pmac *chip;
  945. struct device_node *np;
  946. int i, err;
  947. unsigned int irq;
  948. unsigned long ctrl_addr, txdma_addr, rxdma_addr;
  949. static struct snd_device_ops ops = {
  950. .dev_free = snd_pmac_dev_free,
  951. };
  952. *chip_return = NULL;
  953. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  954. if (chip == NULL)
  955. return -ENOMEM;
  956. chip->card = card;
  957. spin_lock_init(&chip->reg_lock);
  958. chip->irq = chip->tx_irq = chip->rx_irq = -1;
  959. chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
  960. chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
  961. if ((err = snd_pmac_detect(chip)) < 0)
  962. goto __error;
  963. if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
  964. snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
  965. snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0) {
  966. err = -ENOMEM;
  967. goto __error;
  968. }
  969. np = chip->node;
  970. chip->requested = 0;
  971. if (chip->is_k2) {
  972. static char *rnames[] = {
  973. "Sound Control", "Sound DMA" };
  974. for (i = 0; i < 2; i ++) {
  975. if (of_address_to_resource(np->parent, i,
  976. &chip->rsrc[i])) {
  977. printk(KERN_ERR "snd: can't translate rsrc "
  978. " %d (%s)\n", i, rnames[i]);
  979. err = -ENODEV;
  980. goto __error;
  981. }
  982. if (request_mem_region(chip->rsrc[i].start,
  983. chip->rsrc[i].end -
  984. chip->rsrc[i].start + 1,
  985. rnames[i]) == NULL) {
  986. printk(KERN_ERR "snd: can't request rsrc "
  987. " %d (%s: 0x%016llx:%016llx)\n",
  988. i, rnames[i],
  989. (unsigned long long)chip->rsrc[i].start,
  990. (unsigned long long)chip->rsrc[i].end);
  991. err = -ENODEV;
  992. goto __error;
  993. }
  994. chip->requested |= (1 << i);
  995. }
  996. ctrl_addr = chip->rsrc[0].start;
  997. txdma_addr = chip->rsrc[1].start;
  998. rxdma_addr = txdma_addr + 0x100;
  999. } else {
  1000. static char *rnames[] = {
  1001. "Sound Control", "Sound Tx DMA", "Sound Rx DMA" };
  1002. for (i = 0; i < 3; i ++) {
  1003. if (of_address_to_resource(np, i,
  1004. &chip->rsrc[i])) {
  1005. printk(KERN_ERR "snd: can't translate rsrc "
  1006. " %d (%s)\n", i, rnames[i]);
  1007. err = -ENODEV;
  1008. goto __error;
  1009. }
  1010. if (request_mem_region(chip->rsrc[i].start,
  1011. chip->rsrc[i].end -
  1012. chip->rsrc[i].start + 1,
  1013. rnames[i]) == NULL) {
  1014. printk(KERN_ERR "snd: can't request rsrc "
  1015. " %d (%s: 0x%016llx:%016llx)\n",
  1016. i, rnames[i],
  1017. (unsigned long long)chip->rsrc[i].start,
  1018. (unsigned long long)chip->rsrc[i].end);
  1019. err = -ENODEV;
  1020. goto __error;
  1021. }
  1022. chip->requested |= (1 << i);
  1023. }
  1024. ctrl_addr = chip->rsrc[0].start;
  1025. txdma_addr = chip->rsrc[1].start;
  1026. rxdma_addr = chip->rsrc[2].start;
  1027. }
  1028. chip->awacs = ioremap(ctrl_addr, 0x1000);
  1029. chip->playback.dma = ioremap(txdma_addr, 0x100);
  1030. chip->capture.dma = ioremap(rxdma_addr, 0x100);
  1031. if (chip->model <= PMAC_BURGUNDY) {
  1032. irq = irq_of_parse_and_map(np, 0);
  1033. if (request_irq(irq, snd_pmac_ctrl_intr, 0,
  1034. "PMac", (void*)chip)) {
  1035. snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n",
  1036. irq);
  1037. err = -EBUSY;
  1038. goto __error;
  1039. }
  1040. chip->irq = irq;
  1041. }
  1042. irq = irq_of_parse_and_map(np, 1);
  1043. if (request_irq(irq, snd_pmac_tx_intr, 0, "PMac Output", (void*)chip)){
  1044. snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
  1045. err = -EBUSY;
  1046. goto __error;
  1047. }
  1048. chip->tx_irq = irq;
  1049. irq = irq_of_parse_and_map(np, 2);
  1050. if (request_irq(irq, snd_pmac_rx_intr, 0, "PMac Input", (void*)chip)) {
  1051. snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
  1052. err = -EBUSY;
  1053. goto __error;
  1054. }
  1055. chip->rx_irq = irq;
  1056. snd_pmac_sound_feature(chip, 1);
  1057. /* reset */
  1058. if (chip->model == PMAC_AWACS)
  1059. out_le32(&chip->awacs->control, 0x11);
  1060. /* Powerbooks have odd ways of enabling inputs such as
  1061. an expansion-bay CD or sound from an internal modem
  1062. or a PC-card modem. */
  1063. if (chip->is_pbook_3400) {
  1064. /* Enable CD and PC-card sound inputs. */
  1065. /* This is done by reading from address
  1066. * f301a000, + 0x10 to enable the expansion-bay
  1067. * CD sound input, + 0x80 to enable the PC-card
  1068. * sound input. The 0x100 enables the SCSI bus
  1069. * terminator power.
  1070. */
  1071. chip->latch_base = ioremap (0xf301a000, 0x1000);
  1072. in_8(chip->latch_base + 0x190);
  1073. } else if (chip->is_pbook_G3) {
  1074. struct device_node* mio;
  1075. for (mio = chip->node->parent; mio; mio = mio->parent) {
  1076. if (strcmp(mio->name, "mac-io") == 0) {
  1077. struct resource r;
  1078. if (of_address_to_resource(mio, 0, &r) == 0)
  1079. chip->macio_base =
  1080. ioremap(r.start, 0x40);
  1081. break;
  1082. }
  1083. }
  1084. /* Enable CD sound input. */
  1085. /* The relevant bits for writing to this byte are 0x8f.
  1086. * I haven't found out what the 0x80 bit does.
  1087. * For the 0xf bits, writing 3 or 7 enables the CD
  1088. * input, any other value disables it. Values
  1089. * 1, 3, 5, 7 enable the microphone. Values 0, 2,
  1090. * 4, 6, 8 - f enable the input from the modem.
  1091. */
  1092. if (chip->macio_base)
  1093. out_8(chip->macio_base + 0x37, 3);
  1094. }
  1095. /* Reset dbdma channels */
  1096. snd_pmac_dbdma_reset(chip);
  1097. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
  1098. goto __error;
  1099. *chip_return = chip;
  1100. return 0;
  1101. __error:
  1102. snd_pmac_free(chip);
  1103. return err;
  1104. }
  1105. /*
  1106. * sleep notify for powerbook
  1107. */
  1108. #ifdef CONFIG_PM
  1109. /*
  1110. * Save state when going to sleep, restore it afterwards.
  1111. */
  1112. void snd_pmac_suspend(struct snd_pmac *chip)
  1113. {
  1114. unsigned long flags;
  1115. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
  1116. if (chip->suspend)
  1117. chip->suspend(chip);
  1118. snd_pcm_suspend_all(chip->pcm);
  1119. spin_lock_irqsave(&chip->reg_lock, flags);
  1120. snd_pmac_beep_stop(chip);
  1121. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1122. if (chip->irq >= 0)
  1123. disable_irq(chip->irq);
  1124. if (chip->tx_irq >= 0)
  1125. disable_irq(chip->tx_irq);
  1126. if (chip->rx_irq >= 0)
  1127. disable_irq(chip->rx_irq);
  1128. snd_pmac_sound_feature(chip, 0);
  1129. }
  1130. void snd_pmac_resume(struct snd_pmac *chip)
  1131. {
  1132. snd_pmac_sound_feature(chip, 1);
  1133. if (chip->resume)
  1134. chip->resume(chip);
  1135. /* enable CD sound input */
  1136. if (chip->macio_base && chip->is_pbook_G3)
  1137. out_8(chip->macio_base + 0x37, 3);
  1138. else if (chip->is_pbook_3400)
  1139. in_8(chip->latch_base + 0x190);
  1140. snd_pmac_pcm_set_format(chip);
  1141. if (chip->irq >= 0)
  1142. enable_irq(chip->irq);
  1143. if (chip->tx_irq >= 0)
  1144. enable_irq(chip->tx_irq);
  1145. if (chip->rx_irq >= 0)
  1146. enable_irq(chip->rx_irq);
  1147. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
  1148. }
  1149. #endif /* CONFIG_PM */