pmac.c 37 KB

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