pmac.c 36 KB

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