pmac.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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. inline static 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. inline static 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. inline static 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. inline static 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. ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
  660. }
  661. /*
  662. * release resources
  663. */
  664. static int snd_pmac_free(pmac_t *chip)
  665. {
  666. /* stop sounds */
  667. if (chip->initialized) {
  668. snd_pmac_dbdma_reset(chip);
  669. /* disable interrupts from awacs interface */
  670. out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
  671. }
  672. snd_pmac_sound_feature(chip, 0);
  673. #ifdef CONFIG_PM
  674. snd_pmac_unregister_sleep_notifier(chip);
  675. #endif
  676. /* clean up mixer if any */
  677. if (chip->mixer_free)
  678. chip->mixer_free(chip);
  679. snd_pmac_detach_beep(chip);
  680. /* release resources */
  681. if (chip->irq >= 0)
  682. free_irq(chip->irq, (void*)chip);
  683. if (chip->tx_irq >= 0)
  684. free_irq(chip->tx_irq, (void*)chip);
  685. if (chip->rx_irq >= 0)
  686. free_irq(chip->rx_irq, (void*)chip);
  687. snd_pmac_dbdma_free(chip, &chip->playback.cmd);
  688. snd_pmac_dbdma_free(chip, &chip->capture.cmd);
  689. snd_pmac_dbdma_free(chip, &chip->extra_dma);
  690. if (chip->macio_base)
  691. iounmap(chip->macio_base);
  692. if (chip->latch_base)
  693. iounmap(chip->latch_base);
  694. if (chip->awacs)
  695. iounmap(chip->awacs);
  696. if (chip->playback.dma)
  697. iounmap(chip->playback.dma);
  698. if (chip->capture.dma)
  699. iounmap(chip->capture.dma);
  700. #ifndef CONFIG_PPC64
  701. if (chip->node) {
  702. int i;
  703. for (i = 0; i < 3; i++) {
  704. if (chip->of_requested & (1 << i)) {
  705. if (chip->is_k2)
  706. release_OF_resource(chip->node->parent,
  707. i);
  708. else
  709. release_OF_resource(chip->node, i);
  710. }
  711. }
  712. }
  713. #endif /* CONFIG_PPC64 */
  714. if (chip->pdev)
  715. pci_dev_put(chip->pdev);
  716. kfree(chip);
  717. return 0;
  718. }
  719. /*
  720. * free the device
  721. */
  722. static int snd_pmac_dev_free(snd_device_t *device)
  723. {
  724. pmac_t *chip = device->device_data;
  725. return snd_pmac_free(chip);
  726. }
  727. /*
  728. * check the machine support byteswap (little-endian)
  729. */
  730. static void __init detect_byte_swap(pmac_t *chip)
  731. {
  732. struct device_node *mio;
  733. /* if seems that Keylargo can't byte-swap */
  734. for (mio = chip->node->parent; mio; mio = mio->parent) {
  735. if (strcmp(mio->name, "mac-io") == 0) {
  736. if (device_is_compatible(mio, "Keylargo"))
  737. chip->can_byte_swap = 0;
  738. break;
  739. }
  740. }
  741. /* it seems the Pismo & iBook can't byte-swap in hardware. */
  742. if (machine_is_compatible("PowerBook3,1") ||
  743. machine_is_compatible("PowerBook2,1"))
  744. chip->can_byte_swap = 0 ;
  745. if (machine_is_compatible("PowerBook2,1"))
  746. chip->can_duplex = 0;
  747. }
  748. /*
  749. * detect a sound chip
  750. */
  751. static int __init snd_pmac_detect(pmac_t *chip)
  752. {
  753. struct device_node *sound = NULL;
  754. unsigned int *prop, l;
  755. struct macio_chip* macio;
  756. u32 layout_id = 0;
  757. if (_machine != _MACH_Pmac)
  758. return -ENODEV;
  759. chip->subframe = 0;
  760. chip->revision = 0;
  761. chip->freqs_ok = 0xff; /* all ok */
  762. chip->model = PMAC_AWACS;
  763. chip->can_byte_swap = 1;
  764. chip->can_duplex = 1;
  765. chip->can_capture = 1;
  766. chip->num_freqs = ARRAY_SIZE(awacs_freqs);
  767. chip->freq_table = awacs_freqs;
  768. chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
  769. /* check machine type */
  770. if (machine_is_compatible("AAPL,3400/2400")
  771. || machine_is_compatible("AAPL,3500"))
  772. chip->is_pbook_3400 = 1;
  773. else if (machine_is_compatible("PowerBook1,1")
  774. || machine_is_compatible("AAPL,PowerBook1998"))
  775. chip->is_pbook_G3 = 1;
  776. chip->node = find_devices("awacs");
  777. if (chip->node)
  778. sound = chip->node;
  779. /*
  780. * powermac G3 models have a node called "davbus"
  781. * with a child called "sound".
  782. */
  783. if (!chip->node)
  784. chip->node = find_devices("davbus");
  785. /*
  786. * if we didn't find a davbus device, try 'i2s-a' since
  787. * this seems to be what iBooks have
  788. */
  789. if (! chip->node) {
  790. chip->node = find_devices("i2s-a");
  791. if (chip->node && chip->node->parent &&
  792. chip->node->parent->parent) {
  793. if (device_is_compatible(chip->node->parent->parent,
  794. "K2-Keylargo"))
  795. chip->is_k2 = 1;
  796. }
  797. }
  798. if (! chip->node)
  799. return -ENODEV;
  800. if (!sound) {
  801. sound = find_devices("sound");
  802. while (sound && sound->parent != chip->node)
  803. sound = sound->next;
  804. }
  805. if (! sound)
  806. return -ENODEV;
  807. prop = (unsigned int *) get_property(sound, "sub-frame", NULL);
  808. if (prop && *prop < 16)
  809. chip->subframe = *prop;
  810. prop = (unsigned int *) get_property(sound, "layout-id", NULL);
  811. if (prop)
  812. layout_id = *prop;
  813. /* This should be verified on older screamers */
  814. if (device_is_compatible(sound, "screamer")) {
  815. chip->model = PMAC_SCREAMER;
  816. // chip->can_byte_swap = 0; /* FIXME: check this */
  817. }
  818. if (device_is_compatible(sound, "burgundy")) {
  819. chip->model = PMAC_BURGUNDY;
  820. chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
  821. }
  822. if (device_is_compatible(sound, "daca")) {
  823. chip->model = PMAC_DACA;
  824. chip->can_capture = 0; /* no capture */
  825. chip->can_duplex = 0;
  826. // chip->can_byte_swap = 0; /* FIXME: check this */
  827. chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
  828. }
  829. if (device_is_compatible(sound, "tumbler")) {
  830. chip->model = PMAC_TUMBLER;
  831. chip->can_capture = 0; /* no capture */
  832. chip->can_duplex = 0;
  833. // chip->can_byte_swap = 0; /* FIXME: check this */
  834. chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
  835. chip->freq_table = tumbler_freqs;
  836. chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
  837. }
  838. if (device_is_compatible(sound, "snapper")) {
  839. chip->model = PMAC_SNAPPER;
  840. // chip->can_byte_swap = 0; /* FIXME: check this */
  841. chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
  842. chip->freq_table = tumbler_freqs;
  843. chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
  844. }
  845. if (device_is_compatible(sound, "AOAKeylargo") ||
  846. device_is_compatible(sound, "AOAbase") ||
  847. device_is_compatible(sound, "AOAK2")) {
  848. /* For now, only support very basic TAS3004 based machines with
  849. * single frequency until proper i2s control is implemented
  850. */
  851. switch(layout_id) {
  852. case 0x48:
  853. case 0x46:
  854. case 0x33:
  855. case 0x29:
  856. case 0x24:
  857. chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
  858. chip->model = PMAC_SNAPPER;
  859. chip->can_byte_swap = 0; /* FIXME: check this */
  860. chip->control_mask = MASK_IEPC | 0x11;/* disable IEE */
  861. break;
  862. case 0x3a:
  863. chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
  864. chip->model = PMAC_TOONIE;
  865. chip->can_byte_swap = 0; /* FIXME: check this */
  866. chip->control_mask = MASK_IEPC | 0x11;/* disable IEE */
  867. break;
  868. }
  869. }
  870. prop = (unsigned int *)get_property(sound, "device-id", NULL);
  871. if (prop)
  872. chip->device_id = *prop;
  873. chip->has_iic = (find_devices("perch") != NULL);
  874. /* We need the PCI device for DMA allocations, let's use a crude method
  875. * for now ...
  876. */
  877. macio = macio_find(chip->node, macio_unknown);
  878. if (macio == NULL)
  879. printk(KERN_WARNING "snd-powermac: can't locate macio !\n");
  880. else {
  881. struct pci_dev *pdev = NULL;
  882. for_each_pci_dev(pdev) {
  883. struct device_node *np = pci_device_to_OF_node(pdev);
  884. if (np && np == macio->of_node) {
  885. chip->pdev = pdev;
  886. break;
  887. }
  888. }
  889. }
  890. if (chip->pdev == NULL)
  891. printk(KERN_WARNING "snd-powermac: can't locate macio PCI"
  892. " device !\n");
  893. detect_byte_swap(chip);
  894. /* look for a property saying what sample rates
  895. are available */
  896. prop = (unsigned int *) get_property(sound, "sample-rates", &l);
  897. if (! prop)
  898. prop = (unsigned int *) get_property(sound,
  899. "output-frame-rates", &l);
  900. if (prop) {
  901. int i;
  902. chip->freqs_ok = 0;
  903. for (l /= sizeof(int); l > 0; --l) {
  904. unsigned int r = *prop++;
  905. /* Apple 'Fixed' format */
  906. if (r >= 0x10000)
  907. r >>= 16;
  908. for (i = 0; i < chip->num_freqs; ++i) {
  909. if (r == chip->freq_table[i]) {
  910. chip->freqs_ok |= (1 << i);
  911. break;
  912. }
  913. }
  914. }
  915. } else {
  916. /* assume only 44.1khz */
  917. chip->freqs_ok = 1;
  918. }
  919. return 0;
  920. }
  921. /*
  922. * exported - boolean info callbacks for ease of programming
  923. */
  924. int snd_pmac_boolean_stereo_info(snd_kcontrol_t *kcontrol,
  925. snd_ctl_elem_info_t *uinfo)
  926. {
  927. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  928. uinfo->count = 2;
  929. uinfo->value.integer.min = 0;
  930. uinfo->value.integer.max = 1;
  931. return 0;
  932. }
  933. int snd_pmac_boolean_mono_info(snd_kcontrol_t *kcontrol,
  934. snd_ctl_elem_info_t *uinfo)
  935. {
  936. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  937. uinfo->count = 1;
  938. uinfo->value.integer.min = 0;
  939. uinfo->value.integer.max = 1;
  940. return 0;
  941. }
  942. #ifdef PMAC_SUPPORT_AUTOMUTE
  943. /*
  944. * auto-mute
  945. */
  946. static int pmac_auto_mute_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  947. {
  948. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  949. ucontrol->value.integer.value[0] = chip->auto_mute;
  950. return 0;
  951. }
  952. static int pmac_auto_mute_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  953. {
  954. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  955. if (ucontrol->value.integer.value[0] != chip->auto_mute) {
  956. chip->auto_mute = ucontrol->value.integer.value[0];
  957. if (chip->update_automute)
  958. chip->update_automute(chip, 1);
  959. return 1;
  960. }
  961. return 0;
  962. }
  963. static int pmac_hp_detect_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  964. {
  965. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  966. if (chip->detect_headphone)
  967. ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
  968. else
  969. ucontrol->value.integer.value[0] = 0;
  970. return 0;
  971. }
  972. static snd_kcontrol_new_t auto_mute_controls[] __initdata = {
  973. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  974. .name = "Auto Mute Switch",
  975. .info = snd_pmac_boolean_mono_info,
  976. .get = pmac_auto_mute_get,
  977. .put = pmac_auto_mute_put,
  978. },
  979. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  980. .name = "Headphone Detection",
  981. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  982. .info = snd_pmac_boolean_mono_info,
  983. .get = pmac_hp_detect_get,
  984. },
  985. };
  986. int __init snd_pmac_add_automute(pmac_t *chip)
  987. {
  988. int err;
  989. chip->auto_mute = 1;
  990. err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
  991. if (err < 0) {
  992. printk(KERN_ERR "snd-powermac: Failed to add automute control\n");
  993. return err;
  994. }
  995. chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
  996. return snd_ctl_add(chip->card, chip->hp_detect_ctl);
  997. }
  998. #endif /* PMAC_SUPPORT_AUTOMUTE */
  999. /*
  1000. * create and detect a pmac chip record
  1001. */
  1002. int __init snd_pmac_new(snd_card_t *card, pmac_t **chip_return)
  1003. {
  1004. pmac_t *chip;
  1005. struct device_node *np;
  1006. int i, err;
  1007. unsigned long ctrl_addr, txdma_addr, rxdma_addr;
  1008. static snd_device_ops_t ops = {
  1009. .dev_free = snd_pmac_dev_free,
  1010. };
  1011. snd_runtime_check(chip_return, return -EINVAL);
  1012. *chip_return = NULL;
  1013. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  1014. if (chip == NULL)
  1015. return -ENOMEM;
  1016. chip->card = card;
  1017. spin_lock_init(&chip->reg_lock);
  1018. chip->irq = chip->tx_irq = chip->rx_irq = -1;
  1019. chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
  1020. chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
  1021. if ((err = snd_pmac_detect(chip)) < 0)
  1022. goto __error;
  1023. if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
  1024. snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
  1025. snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0) {
  1026. err = -ENOMEM;
  1027. goto __error;
  1028. }
  1029. np = chip->node;
  1030. if (chip->is_k2) {
  1031. if (np->parent->n_addrs < 2 || np->n_intrs < 3) {
  1032. err = -ENODEV;
  1033. goto __error;
  1034. }
  1035. for (i = 0; i < 2; i++) {
  1036. #ifndef CONFIG_PPC64
  1037. static char *name[2] = { "- Control", "- DMA" };
  1038. if (! request_OF_resource(np->parent, i, name[i])) {
  1039. snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
  1040. err = -ENODEV;
  1041. goto __error;
  1042. }
  1043. chip->of_requested |= (1 << i);
  1044. #endif /* CONFIG_PPC64 */
  1045. ctrl_addr = np->parent->addrs[0].address;
  1046. txdma_addr = np->parent->addrs[1].address;
  1047. rxdma_addr = txdma_addr + 0x100;
  1048. }
  1049. } else {
  1050. if (np->n_addrs < 3 || np->n_intrs < 3) {
  1051. err = -ENODEV;
  1052. goto __error;
  1053. }
  1054. for (i = 0; i < 3; i++) {
  1055. #ifndef CONFIG_PPC64
  1056. static char *name[3] = { "- Control", "- Tx DMA", "- Rx DMA" };
  1057. if (! request_OF_resource(np, i, name[i])) {
  1058. snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
  1059. err = -ENODEV;
  1060. goto __error;
  1061. }
  1062. chip->of_requested |= (1 << i);
  1063. #endif /* CONFIG_PPC64 */
  1064. ctrl_addr = np->addrs[0].address;
  1065. txdma_addr = np->addrs[1].address;
  1066. rxdma_addr = np->addrs[2].address;
  1067. }
  1068. }
  1069. chip->awacs = ioremap(ctrl_addr, 0x1000);
  1070. chip->playback.dma = ioremap(txdma_addr, 0x100);
  1071. chip->capture.dma = ioremap(rxdma_addr, 0x100);
  1072. if (chip->model <= PMAC_BURGUNDY) {
  1073. if (request_irq(np->intrs[0].line, snd_pmac_ctrl_intr, 0,
  1074. "PMac", (void*)chip)) {
  1075. snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[0].line);
  1076. err = -EBUSY;
  1077. goto __error;
  1078. }
  1079. chip->irq = np->intrs[0].line;
  1080. }
  1081. if (request_irq(np->intrs[1].line, snd_pmac_tx_intr, 0,
  1082. "PMac Output", (void*)chip)) {
  1083. snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[1].line);
  1084. err = -EBUSY;
  1085. goto __error;
  1086. }
  1087. chip->tx_irq = np->intrs[1].line;
  1088. if (request_irq(np->intrs[2].line, snd_pmac_rx_intr, 0,
  1089. "PMac Input", (void*)chip)) {
  1090. snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[2].line);
  1091. err = -EBUSY;
  1092. goto __error;
  1093. }
  1094. chip->rx_irq = np->intrs[2].line;
  1095. snd_pmac_sound_feature(chip, 1);
  1096. /* reset */
  1097. if (chip->model == PMAC_AWACS)
  1098. out_le32(&chip->awacs->control, 0x11);
  1099. /* Powerbooks have odd ways of enabling inputs such as
  1100. an expansion-bay CD or sound from an internal modem
  1101. or a PC-card modem. */
  1102. if (chip->is_pbook_3400) {
  1103. /* Enable CD and PC-card sound inputs. */
  1104. /* This is done by reading from address
  1105. * f301a000, + 0x10 to enable the expansion-bay
  1106. * CD sound input, + 0x80 to enable the PC-card
  1107. * sound input. The 0x100 enables the SCSI bus
  1108. * terminator power.
  1109. */
  1110. chip->latch_base = ioremap (0xf301a000, 0x1000);
  1111. in_8(chip->latch_base + 0x190);
  1112. } else if (chip->is_pbook_G3) {
  1113. struct device_node* mio;
  1114. for (mio = chip->node->parent; mio; mio = mio->parent) {
  1115. if (strcmp(mio->name, "mac-io") == 0
  1116. && mio->n_addrs > 0) {
  1117. chip->macio_base = ioremap(mio->addrs[0].address, 0x40);
  1118. break;
  1119. }
  1120. }
  1121. /* Enable CD sound input. */
  1122. /* The relevant bits for writing to this byte are 0x8f.
  1123. * I haven't found out what the 0x80 bit does.
  1124. * For the 0xf bits, writing 3 or 7 enables the CD
  1125. * input, any other value disables it. Values
  1126. * 1, 3, 5, 7 enable the microphone. Values 0, 2,
  1127. * 4, 6, 8 - f enable the input from the modem.
  1128. */
  1129. if (chip->macio_base)
  1130. out_8(chip->macio_base + 0x37, 3);
  1131. }
  1132. /* Reset dbdma channels */
  1133. snd_pmac_dbdma_reset(chip);
  1134. #ifdef CONFIG_PM
  1135. /* add sleep notifier */
  1136. if (! snd_pmac_register_sleep_notifier(chip))
  1137. snd_card_set_pm_callback(chip->card, snd_pmac_suspend, snd_pmac_resume, chip);
  1138. #endif
  1139. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
  1140. goto __error;
  1141. *chip_return = chip;
  1142. return 0;
  1143. __error:
  1144. if (chip->pdev)
  1145. pci_dev_put(chip->pdev);
  1146. snd_pmac_free(chip);
  1147. return err;
  1148. }
  1149. /*
  1150. * sleep notify for powerbook
  1151. */
  1152. #ifdef CONFIG_PM
  1153. /*
  1154. * Save state when going to sleep, restore it afterwards.
  1155. */
  1156. static int snd_pmac_suspend(snd_card_t *card, pm_message_t state)
  1157. {
  1158. pmac_t *chip = card->pm_private_data;
  1159. unsigned long flags;
  1160. if (chip->suspend)
  1161. chip->suspend(chip);
  1162. snd_pcm_suspend_all(chip->pcm);
  1163. spin_lock_irqsave(&chip->reg_lock, flags);
  1164. snd_pmac_beep_stop(chip);
  1165. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1166. if (chip->irq >= 0)
  1167. disable_irq(chip->irq);
  1168. if (chip->tx_irq >= 0)
  1169. disable_irq(chip->tx_irq);
  1170. if (chip->rx_irq >= 0)
  1171. disable_irq(chip->rx_irq);
  1172. snd_pmac_sound_feature(chip, 0);
  1173. return 0;
  1174. }
  1175. static int snd_pmac_resume(snd_card_t *card)
  1176. {
  1177. pmac_t *chip = card->pm_private_data;
  1178. snd_pmac_sound_feature(chip, 1);
  1179. if (chip->resume)
  1180. chip->resume(chip);
  1181. /* enable CD sound input */
  1182. if (chip->macio_base && chip->is_pbook_G3) {
  1183. out_8(chip->macio_base + 0x37, 3);
  1184. } else if (chip->is_pbook_3400) {
  1185. in_8(chip->latch_base + 0x190);
  1186. }
  1187. snd_pmac_pcm_set_format(chip);
  1188. if (chip->irq >= 0)
  1189. enable_irq(chip->irq);
  1190. if (chip->tx_irq >= 0)
  1191. enable_irq(chip->tx_irq);
  1192. if (chip->rx_irq >= 0)
  1193. enable_irq(chip->rx_irq);
  1194. return 0;
  1195. }
  1196. /* the chip is stored statically by snd_pmac_register_sleep_notifier
  1197. * because we can't have any private data for notify callback.
  1198. */
  1199. static pmac_t *sleeping_pmac = NULL;
  1200. static int snd_pmac_sleep_notify(struct pmu_sleep_notifier *self, int when)
  1201. {
  1202. pmac_t *chip;
  1203. chip = sleeping_pmac;
  1204. snd_runtime_check(chip, return 0);
  1205. switch (when) {
  1206. case PBOOK_SLEEP_NOW:
  1207. snd_pmac_suspend(chip->card, PMSG_SUSPEND);
  1208. break;
  1209. case PBOOK_WAKE:
  1210. snd_pmac_resume(chip->card);
  1211. break;
  1212. }
  1213. return PBOOK_SLEEP_OK;
  1214. }
  1215. static struct pmu_sleep_notifier snd_pmac_sleep_notifier = {
  1216. snd_pmac_sleep_notify, SLEEP_LEVEL_SOUND,
  1217. };
  1218. static int __init snd_pmac_register_sleep_notifier(pmac_t *chip)
  1219. {
  1220. /* should be protected here.. */
  1221. snd_assert(! sleeping_pmac, return -EBUSY);
  1222. sleeping_pmac = chip;
  1223. pmu_register_sleep_notifier(&snd_pmac_sleep_notifier);
  1224. return 0;
  1225. }
  1226. static int snd_pmac_unregister_sleep_notifier(pmac_t *chip)
  1227. {
  1228. /* should be protected here.. */
  1229. snd_assert(sleeping_pmac == chip, return -ENODEV);
  1230. pmu_unregister_sleep_notifier(&snd_pmac_sleep_notifier);
  1231. sleeping_pmac = NULL;
  1232. return 0;
  1233. }
  1234. #endif /* CONFIG_PM */