pmac.c 37 KB

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