PageRenderTime 1000ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/sound/aoa/soundbus/i2sbus/pcm.c

https://github.com/Mengqi/linux-2.6
C | 1063 lines | 814 code | 135 blank | 114 comment | 165 complexity | d8ee39d613559a55c2a6483f80a2670b MD5 | raw file
  1. /*
  2. * i2sbus driver -- pcm routines
  3. *
  4. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * GPL v2, can be found in COPYING.
  7. */
  8. #include <asm/io.h>
  9. #include <linux/delay.h>
  10. #include <linux/slab.h>
  11. #include <sound/core.h>
  12. #include <asm/macio.h>
  13. #include <linux/pci.h>
  14. #include "../soundbus.h"
  15. #include "i2sbus.h"
  16. static inline void get_pcm_info(struct i2sbus_dev *i2sdev, int in,
  17. struct pcm_info **pi, struct pcm_info **other)
  18. {
  19. if (in) {
  20. if (pi)
  21. *pi = &i2sdev->in;
  22. if (other)
  23. *other = &i2sdev->out;
  24. } else {
  25. if (pi)
  26. *pi = &i2sdev->out;
  27. if (other)
  28. *other = &i2sdev->in;
  29. }
  30. }
  31. static int clock_and_divisors(int mclk, int sclk, int rate, int *out)
  32. {
  33. /* sclk must be derived from mclk! */
  34. if (mclk % sclk)
  35. return -1;
  36. /* derive sclk register value */
  37. if (i2s_sf_sclkdiv(mclk / sclk, out))
  38. return -1;
  39. if (I2S_CLOCK_SPEED_18MHz % (rate * mclk) == 0) {
  40. if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_18MHz / (rate * mclk), out)) {
  41. *out |= I2S_SF_CLOCK_SOURCE_18MHz;
  42. return 0;
  43. }
  44. }
  45. if (I2S_CLOCK_SPEED_45MHz % (rate * mclk) == 0) {
  46. if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_45MHz / (rate * mclk), out)) {
  47. *out |= I2S_SF_CLOCK_SOURCE_45MHz;
  48. return 0;
  49. }
  50. }
  51. if (I2S_CLOCK_SPEED_49MHz % (rate * mclk) == 0) {
  52. if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_49MHz / (rate * mclk), out)) {
  53. *out |= I2S_SF_CLOCK_SOURCE_49MHz;
  54. return 0;
  55. }
  56. }
  57. return -1;
  58. }
  59. #define CHECK_RATE(rate) \
  60. do { if (rates & SNDRV_PCM_RATE_ ##rate) { \
  61. int dummy; \
  62. if (clock_and_divisors(sysclock_factor, \
  63. bus_factor, rate, &dummy)) \
  64. rates &= ~SNDRV_PCM_RATE_ ##rate; \
  65. } } while (0)
  66. static int i2sbus_pcm_open(struct i2sbus_dev *i2sdev, int in)
  67. {
  68. struct pcm_info *pi, *other;
  69. struct soundbus_dev *sdev;
  70. int masks_inited = 0, err;
  71. struct codec_info_item *cii, *rev;
  72. struct snd_pcm_hardware *hw;
  73. u64 formats = 0;
  74. unsigned int rates = 0;
  75. struct transfer_info v;
  76. int result = 0;
  77. int bus_factor = 0, sysclock_factor = 0;
  78. int found_this;
  79. mutex_lock(&i2sdev->lock);
  80. get_pcm_info(i2sdev, in, &pi, &other);
  81. hw = &pi->substream->runtime->hw;
  82. sdev = &i2sdev->sound;
  83. if (pi->active) {
  84. /* alsa messed up */
  85. result = -EBUSY;
  86. goto out_unlock;
  87. }
  88. /* we now need to assign the hw */
  89. list_for_each_entry(cii, &sdev->codec_list, list) {
  90. struct transfer_info *ti = cii->codec->transfers;
  91. bus_factor = cii->codec->bus_factor;
  92. sysclock_factor = cii->codec->sysclock_factor;
  93. while (ti->formats && ti->rates) {
  94. v = *ti;
  95. if (ti->transfer_in == in
  96. && cii->codec->usable(cii, ti, &v)) {
  97. if (masks_inited) {
  98. formats &= v.formats;
  99. rates &= v.rates;
  100. } else {
  101. formats = v.formats;
  102. rates = v.rates;
  103. masks_inited = 1;
  104. }
  105. }
  106. ti++;
  107. }
  108. }
  109. if (!masks_inited || !bus_factor || !sysclock_factor) {
  110. result = -ENODEV;
  111. goto out_unlock;
  112. }
  113. /* bus dependent stuff */
  114. hw->info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  115. SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_RESUME |
  116. SNDRV_PCM_INFO_JOINT_DUPLEX;
  117. CHECK_RATE(5512);
  118. CHECK_RATE(8000);
  119. CHECK_RATE(11025);
  120. CHECK_RATE(16000);
  121. CHECK_RATE(22050);
  122. CHECK_RATE(32000);
  123. CHECK_RATE(44100);
  124. CHECK_RATE(48000);
  125. CHECK_RATE(64000);
  126. CHECK_RATE(88200);
  127. CHECK_RATE(96000);
  128. CHECK_RATE(176400);
  129. CHECK_RATE(192000);
  130. hw->rates = rates;
  131. /* well. the codec might want 24 bits only, and we'll
  132. * ever only transfer 24 bits, but they are top-aligned!
  133. * So for alsa, we claim that we're doing full 32 bit
  134. * while in reality we'll ignore the lower 8 bits of
  135. * that when doing playback (they're transferred as 0
  136. * as far as I know, no codecs we have are 32-bit capable
  137. * so I can't really test) and when doing recording we'll
  138. * always have those lower 8 bits recorded as 0 */
  139. if (formats & SNDRV_PCM_FMTBIT_S24_BE)
  140. formats |= SNDRV_PCM_FMTBIT_S32_BE;
  141. if (formats & SNDRV_PCM_FMTBIT_U24_BE)
  142. formats |= SNDRV_PCM_FMTBIT_U32_BE;
  143. /* now mask off what we can support. I suppose we could
  144. * also support S24_3LE and some similar formats, but I
  145. * doubt there's a codec that would be able to use that,
  146. * so we don't support it here. */
  147. hw->formats = formats & (SNDRV_PCM_FMTBIT_S16_BE |
  148. SNDRV_PCM_FMTBIT_U16_BE |
  149. SNDRV_PCM_FMTBIT_S32_BE |
  150. SNDRV_PCM_FMTBIT_U32_BE);
  151. /* we need to set the highest and lowest rate possible.
  152. * These are the highest and lowest rates alsa can
  153. * support properly in its bitfield.
  154. * Below, we'll use that to restrict to the rate
  155. * currently in use (if any). */
  156. hw->rate_min = 5512;
  157. hw->rate_max = 192000;
  158. /* if the other stream is active, then we can only
  159. * support what it is currently using.
  160. * FIXME: I lied. This comment is wrong. We can support
  161. * anything that works with the same serial format, ie.
  162. * when recording 24 bit sound we can well play 16 bit
  163. * sound at the same time iff using the same transfer mode.
  164. */
  165. if (other->active) {
  166. /* FIXME: is this guaranteed by the alsa api? */
  167. hw->formats &= (1ULL << i2sdev->format);
  168. /* see above, restrict rates to the one we already have */
  169. hw->rate_min = i2sdev->rate;
  170. hw->rate_max = i2sdev->rate;
  171. }
  172. hw->channels_min = 2;
  173. hw->channels_max = 2;
  174. /* these are somewhat arbitrary */
  175. hw->buffer_bytes_max = 131072;
  176. hw->period_bytes_min = 256;
  177. hw->period_bytes_max = 16384;
  178. hw->periods_min = 3;
  179. hw->periods_max = MAX_DBDMA_COMMANDS;
  180. err = snd_pcm_hw_constraint_integer(pi->substream->runtime,
  181. SNDRV_PCM_HW_PARAM_PERIODS);
  182. if (err < 0) {
  183. result = err;
  184. goto out_unlock;
  185. }
  186. list_for_each_entry(cii, &sdev->codec_list, list) {
  187. if (cii->codec->open) {
  188. err = cii->codec->open(cii, pi->substream);
  189. if (err) {
  190. result = err;
  191. /* unwind */
  192. found_this = 0;
  193. list_for_each_entry_reverse(rev,
  194. &sdev->codec_list, list) {
  195. if (found_this && rev->codec->close) {
  196. rev->codec->close(rev,
  197. pi->substream);
  198. }
  199. if (rev == cii)
  200. found_this = 1;
  201. }
  202. goto out_unlock;
  203. }
  204. }
  205. }
  206. out_unlock:
  207. mutex_unlock(&i2sdev->lock);
  208. return result;
  209. }
  210. #undef CHECK_RATE
  211. static int i2sbus_pcm_close(struct i2sbus_dev *i2sdev, int in)
  212. {
  213. struct codec_info_item *cii;
  214. struct pcm_info *pi;
  215. int err = 0, tmp;
  216. mutex_lock(&i2sdev->lock);
  217. get_pcm_info(i2sdev, in, &pi, NULL);
  218. list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
  219. if (cii->codec->close) {
  220. tmp = cii->codec->close(cii, pi->substream);
  221. if (tmp)
  222. err = tmp;
  223. }
  224. }
  225. pi->substream = NULL;
  226. pi->active = 0;
  227. mutex_unlock(&i2sdev->lock);
  228. return err;
  229. }
  230. static void i2sbus_wait_for_stop(struct i2sbus_dev *i2sdev,
  231. struct pcm_info *pi)
  232. {
  233. unsigned long flags;
  234. struct completion done;
  235. long timeout;
  236. spin_lock_irqsave(&i2sdev->low_lock, flags);
  237. if (pi->dbdma_ring.stopping) {
  238. init_completion(&done);
  239. pi->stop_completion = &done;
  240. spin_unlock_irqrestore(&i2sdev->low_lock, flags);
  241. timeout = wait_for_completion_timeout(&done, HZ);
  242. spin_lock_irqsave(&i2sdev->low_lock, flags);
  243. pi->stop_completion = NULL;
  244. if (timeout == 0) {
  245. /* timeout expired, stop dbdma forcefully */
  246. printk(KERN_ERR "i2sbus_wait_for_stop: timed out\n");
  247. /* make sure RUN, PAUSE and S0 bits are cleared */
  248. out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
  249. pi->dbdma_ring.stopping = 0;
  250. timeout = 10;
  251. while (in_le32(&pi->dbdma->status) & ACTIVE) {
  252. if (--timeout <= 0)
  253. break;
  254. udelay(1);
  255. }
  256. }
  257. }
  258. spin_unlock_irqrestore(&i2sdev->low_lock, flags);
  259. }
  260. #ifdef CONFIG_PM
  261. void i2sbus_wait_for_stop_both(struct i2sbus_dev *i2sdev)
  262. {
  263. struct pcm_info *pi;
  264. get_pcm_info(i2sdev, 0, &pi, NULL);
  265. i2sbus_wait_for_stop(i2sdev, pi);
  266. get_pcm_info(i2sdev, 1, &pi, NULL);
  267. i2sbus_wait_for_stop(i2sdev, pi);
  268. }
  269. #endif
  270. static int i2sbus_hw_params(struct snd_pcm_substream *substream,
  271. struct snd_pcm_hw_params *params)
  272. {
  273. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  274. }
  275. static inline int i2sbus_hw_free(struct snd_pcm_substream *substream, int in)
  276. {
  277. struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
  278. struct pcm_info *pi;
  279. get_pcm_info(i2sdev, in, &pi, NULL);
  280. if (pi->dbdma_ring.stopping)
  281. i2sbus_wait_for_stop(i2sdev, pi);
  282. snd_pcm_lib_free_pages(substream);
  283. return 0;
  284. }
  285. static int i2sbus_playback_hw_free(struct snd_pcm_substream *substream)
  286. {
  287. return i2sbus_hw_free(substream, 0);
  288. }
  289. static int i2sbus_record_hw_free(struct snd_pcm_substream *substream)
  290. {
  291. return i2sbus_hw_free(substream, 1);
  292. }
  293. static int i2sbus_pcm_prepare(struct i2sbus_dev *i2sdev, int in)
  294. {
  295. /* whee. Hard work now. The user has selected a bitrate
  296. * and bit format, so now we have to program our
  297. * I2S controller appropriately. */
  298. struct snd_pcm_runtime *runtime;
  299. struct dbdma_cmd *command;
  300. int i, periodsize, nperiods;
  301. dma_addr_t offset;
  302. struct bus_info bi;
  303. struct codec_info_item *cii;
  304. int sfr = 0; /* serial format register */
  305. int dws = 0; /* data word sizes reg */
  306. int input_16bit;
  307. struct pcm_info *pi, *other;
  308. int cnt;
  309. int result = 0;
  310. unsigned int cmd, stopaddr;
  311. mutex_lock(&i2sdev->lock);
  312. get_pcm_info(i2sdev, in, &pi, &other);
  313. if (pi->dbdma_ring.running) {
  314. result = -EBUSY;
  315. goto out_unlock;
  316. }
  317. if (pi->dbdma_ring.stopping)
  318. i2sbus_wait_for_stop(i2sdev, pi);
  319. if (!pi->substream || !pi->substream->runtime) {
  320. result = -EINVAL;
  321. goto out_unlock;
  322. }
  323. runtime = pi->substream->runtime;
  324. pi->active = 1;
  325. if (other->active &&
  326. ((i2sdev->format != runtime->format)
  327. || (i2sdev->rate != runtime->rate))) {
  328. result = -EINVAL;
  329. goto out_unlock;
  330. }
  331. i2sdev->format = runtime->format;
  332. i2sdev->rate = runtime->rate;
  333. periodsize = snd_pcm_lib_period_bytes(pi->substream);
  334. nperiods = pi->substream->runtime->periods;
  335. pi->current_period = 0;
  336. /* generate dbdma command ring first */
  337. command = pi->dbdma_ring.cmds;
  338. memset(command, 0, (nperiods + 2) * sizeof(struct dbdma_cmd));
  339. /* commands to DMA to/from the ring */
  340. /*
  341. * For input, we need to do a graceful stop; if we abort
  342. * the DMA, we end up with leftover bytes that corrupt
  343. * the next recording. To do this we set the S0 status
  344. * bit and wait for the DMA controller to stop. Each
  345. * command has a branch condition to
  346. * make it branch to a stop command if S0 is set.
  347. * On input we also need to wait for the S7 bit to be
  348. * set before turning off the DMA controller.
  349. * In fact we do the graceful stop for output as well.
  350. */
  351. offset = runtime->dma_addr;
  352. cmd = (in? INPUT_MORE: OUTPUT_MORE) | BR_IFSET | INTR_ALWAYS;
  353. stopaddr = pi->dbdma_ring.bus_cmd_start +
  354. (nperiods + 1) * sizeof(struct dbdma_cmd);
  355. for (i = 0; i < nperiods; i++, command++, offset += periodsize) {
  356. command->command = cpu_to_le16(cmd);
  357. command->cmd_dep = cpu_to_le32(stopaddr);
  358. command->phy_addr = cpu_to_le32(offset);
  359. command->req_count = cpu_to_le16(periodsize);
  360. }
  361. /* branch back to beginning of ring */
  362. command->command = cpu_to_le16(DBDMA_NOP | BR_ALWAYS);
  363. command->cmd_dep = cpu_to_le32(pi->dbdma_ring.bus_cmd_start);
  364. command++;
  365. /* set stop command */
  366. command->command = cpu_to_le16(DBDMA_STOP);
  367. /* ok, let's set the serial format and stuff */
  368. switch (runtime->format) {
  369. /* 16 bit formats */
  370. case SNDRV_PCM_FORMAT_S16_BE:
  371. case SNDRV_PCM_FORMAT_U16_BE:
  372. /* FIXME: if we add different bus factors we need to
  373. * do more here!! */
  374. bi.bus_factor = 0;
  375. list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
  376. bi.bus_factor = cii->codec->bus_factor;
  377. break;
  378. }
  379. if (!bi.bus_factor) {
  380. result = -ENODEV;
  381. goto out_unlock;
  382. }
  383. input_16bit = 1;
  384. break;
  385. case SNDRV_PCM_FORMAT_S32_BE:
  386. case SNDRV_PCM_FORMAT_U32_BE:
  387. /* force 64x bus speed, otherwise the data cannot be
  388. * transferred quickly enough! */
  389. bi.bus_factor = 64;
  390. input_16bit = 0;
  391. break;
  392. default:
  393. result = -EINVAL;
  394. goto out_unlock;
  395. }
  396. /* we assume all sysclocks are the same! */
  397. list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
  398. bi.sysclock_factor = cii->codec->sysclock_factor;
  399. break;
  400. }
  401. if (clock_and_divisors(bi.sysclock_factor,
  402. bi.bus_factor,
  403. runtime->rate,
  404. &sfr) < 0) {
  405. result = -EINVAL;
  406. goto out_unlock;
  407. }
  408. switch (bi.bus_factor) {
  409. case 32:
  410. sfr |= I2S_SF_SERIAL_FORMAT_I2S_32X;
  411. break;
  412. case 64:
  413. sfr |= I2S_SF_SERIAL_FORMAT_I2S_64X;
  414. break;
  415. }
  416. /* FIXME: THIS ASSUMES MASTER ALL THE TIME */
  417. sfr |= I2S_SF_SCLK_MASTER;
  418. list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
  419. int err = 0;
  420. if (cii->codec->prepare)
  421. err = cii->codec->prepare(cii, &bi, pi->substream);
  422. if (err) {
  423. result = err;
  424. goto out_unlock;
  425. }
  426. }
  427. /* codecs are fine with it, so set our clocks */
  428. if (input_16bit)
  429. dws = (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
  430. (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
  431. I2S_DWS_DATA_IN_16BIT | I2S_DWS_DATA_OUT_16BIT;
  432. else
  433. dws = (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
  434. (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
  435. I2S_DWS_DATA_IN_24BIT | I2S_DWS_DATA_OUT_24BIT;
  436. /* early exit if already programmed correctly */
  437. /* not locking these is fine since we touch them only in this function */
  438. if (in_le32(&i2sdev->intfregs->serial_format) == sfr
  439. && in_le32(&i2sdev->intfregs->data_word_sizes) == dws)
  440. goto out_unlock;
  441. /* let's notify the codecs about clocks going away.
  442. * For now we only do mastering on the i2s cell... */
  443. list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
  444. if (cii->codec->switch_clock)
  445. cii->codec->switch_clock(cii, CLOCK_SWITCH_PREPARE_SLAVE);
  446. i2sbus_control_enable(i2sdev->control, i2sdev);
  447. i2sbus_control_cell(i2sdev->control, i2sdev, 1);
  448. out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
  449. i2sbus_control_clock(i2sdev->control, i2sdev, 0);
  450. msleep(1);
  451. /* wait for clock stopped. This can apparently take a while... */
  452. cnt = 100;
  453. while (cnt-- &&
  454. !(in_le32(&i2sdev->intfregs->intr_ctl) & I2S_PENDING_CLOCKS_STOPPED)) {
  455. msleep(5);
  456. }
  457. out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
  458. /* not locking these is fine since we touch them only in this function */
  459. out_le32(&i2sdev->intfregs->serial_format, sfr);
  460. out_le32(&i2sdev->intfregs->data_word_sizes, dws);
  461. i2sbus_control_enable(i2sdev->control, i2sdev);
  462. i2sbus_control_cell(i2sdev->control, i2sdev, 1);
  463. i2sbus_control_clock(i2sdev->control, i2sdev, 1);
  464. msleep(1);
  465. list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
  466. if (cii->codec->switch_clock)
  467. cii->codec->switch_clock(cii, CLOCK_SWITCH_SLAVE);
  468. out_unlock:
  469. mutex_unlock(&i2sdev->lock);
  470. return result;
  471. }
  472. #ifdef CONFIG_PM
  473. void i2sbus_pcm_prepare_both(struct i2sbus_dev *i2sdev)
  474. {
  475. i2sbus_pcm_prepare(i2sdev, 0);
  476. i2sbus_pcm_prepare(i2sdev, 1);
  477. }
  478. #endif
  479. static int i2sbus_pcm_trigger(struct i2sbus_dev *i2sdev, int in, int cmd)
  480. {
  481. struct codec_info_item *cii;
  482. struct pcm_info *pi;
  483. int result = 0;
  484. unsigned long flags;
  485. spin_lock_irqsave(&i2sdev->low_lock, flags);
  486. get_pcm_info(i2sdev, in, &pi, NULL);
  487. switch (cmd) {
  488. case SNDRV_PCM_TRIGGER_START:
  489. case SNDRV_PCM_TRIGGER_RESUME:
  490. if (pi->dbdma_ring.running) {
  491. result = -EALREADY;
  492. goto out_unlock;
  493. }
  494. list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
  495. if (cii->codec->start)
  496. cii->codec->start(cii, pi->substream);
  497. pi->dbdma_ring.running = 1;
  498. if (pi->dbdma_ring.stopping) {
  499. /* Clear the S0 bit, then see if we stopped yet */
  500. out_le32(&pi->dbdma->control, 1 << 16);
  501. if (in_le32(&pi->dbdma->status) & ACTIVE) {
  502. /* possible race here? */
  503. udelay(10);
  504. if (in_le32(&pi->dbdma->status) & ACTIVE) {
  505. pi->dbdma_ring.stopping = 0;
  506. goto out_unlock; /* keep running */
  507. }
  508. }
  509. }
  510. /* make sure RUN, PAUSE and S0 bits are cleared */
  511. out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
  512. /* set branch condition select register */
  513. out_le32(&pi->dbdma->br_sel, (1 << 16) | 1);
  514. /* write dma command buffer address to the dbdma chip */
  515. out_le32(&pi->dbdma->cmdptr, pi->dbdma_ring.bus_cmd_start);
  516. /* initialize the frame count and current period */
  517. pi->current_period = 0;
  518. pi->frame_count = in_le32(&i2sdev->intfregs->frame_count);
  519. /* set the DMA controller running */
  520. out_le32(&pi->dbdma->control, (RUN << 16) | RUN);
  521. /* off you go! */
  522. break;
  523. case SNDRV_PCM_TRIGGER_STOP:
  524. case SNDRV_PCM_TRIGGER_SUSPEND:
  525. if (!pi->dbdma_ring.running) {
  526. result = -EALREADY;
  527. goto out_unlock;
  528. }
  529. pi->dbdma_ring.running = 0;
  530. /* Set the S0 bit to make the DMA branch to the stop cmd */
  531. out_le32(&pi->dbdma->control, (1 << 16) | 1);
  532. pi->dbdma_ring.stopping = 1;
  533. list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
  534. if (cii->codec->stop)
  535. cii->codec->stop(cii, pi->substream);
  536. break;
  537. default:
  538. result = -EINVAL;
  539. goto out_unlock;
  540. }
  541. out_unlock:
  542. spin_unlock_irqrestore(&i2sdev->low_lock, flags);
  543. return result;
  544. }
  545. static snd_pcm_uframes_t i2sbus_pcm_pointer(struct i2sbus_dev *i2sdev, int in)
  546. {
  547. struct pcm_info *pi;
  548. u32 fc;
  549. get_pcm_info(i2sdev, in, &pi, NULL);
  550. fc = in_le32(&i2sdev->intfregs->frame_count);
  551. fc = fc - pi->frame_count;
  552. if (fc >= pi->substream->runtime->buffer_size)
  553. fc %= pi->substream->runtime->buffer_size;
  554. return fc;
  555. }
  556. static inline void handle_interrupt(struct i2sbus_dev *i2sdev, int in)
  557. {
  558. struct pcm_info *pi;
  559. u32 fc, nframes;
  560. u32 status;
  561. int timeout, i;
  562. int dma_stopped = 0;
  563. struct snd_pcm_runtime *runtime;
  564. spin_lock(&i2sdev->low_lock);
  565. get_pcm_info(i2sdev, in, &pi, NULL);
  566. if (!pi->dbdma_ring.running && !pi->dbdma_ring.stopping)
  567. goto out_unlock;
  568. i = pi->current_period;
  569. runtime = pi->substream->runtime;
  570. while (pi->dbdma_ring.cmds[i].xfer_status) {
  571. if (le16_to_cpu(pi->dbdma_ring.cmds[i].xfer_status) & BT)
  572. /*
  573. * BT is the branch taken bit. If it took a branch
  574. * it is because we set the S0 bit to make it
  575. * branch to the stop command.
  576. */
  577. dma_stopped = 1;
  578. pi->dbdma_ring.cmds[i].xfer_status = 0;
  579. if (++i >= runtime->periods) {
  580. i = 0;
  581. pi->frame_count += runtime->buffer_size;
  582. }
  583. pi->current_period = i;
  584. /*
  585. * Check the frame count. The DMA tends to get a bit
  586. * ahead of the frame counter, which confuses the core.
  587. */
  588. fc = in_le32(&i2sdev->intfregs->frame_count);
  589. nframes = i * runtime->period_size;
  590. if (fc < pi->frame_count + nframes)
  591. pi->frame_count = fc - nframes;
  592. }
  593. if (dma_stopped) {
  594. timeout = 1000;
  595. for (;;) {
  596. status = in_le32(&pi->dbdma->status);
  597. if (!(status & ACTIVE) && (!in || (status & 0x80)))
  598. break;
  599. if (--timeout <= 0) {
  600. printk(KERN_ERR "i2sbus: timed out "
  601. "waiting for DMA to stop!\n");
  602. break;
  603. }
  604. udelay(1);
  605. }
  606. /* Turn off DMA controller, clear S0 bit */
  607. out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
  608. pi->dbdma_ring.stopping = 0;
  609. if (pi->stop_completion)
  610. complete(pi->stop_completion);
  611. }
  612. if (!pi->dbdma_ring.running)
  613. goto out_unlock;
  614. spin_unlock(&i2sdev->low_lock);
  615. /* may call _trigger again, hence needs to be unlocked */
  616. snd_pcm_period_elapsed(pi->substream);
  617. return;
  618. out_unlock:
  619. spin_unlock(&i2sdev->low_lock);
  620. }
  621. irqreturn_t i2sbus_tx_intr(int irq, void *devid)
  622. {
  623. handle_interrupt((struct i2sbus_dev *)devid, 0);
  624. return IRQ_HANDLED;
  625. }
  626. irqreturn_t i2sbus_rx_intr(int irq, void *devid)
  627. {
  628. handle_interrupt((struct i2sbus_dev *)devid, 1);
  629. return IRQ_HANDLED;
  630. }
  631. static int i2sbus_playback_open(struct snd_pcm_substream *substream)
  632. {
  633. struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
  634. if (!i2sdev)
  635. return -EINVAL;
  636. i2sdev->out.substream = substream;
  637. return i2sbus_pcm_open(i2sdev, 0);
  638. }
  639. static int i2sbus_playback_close(struct snd_pcm_substream *substream)
  640. {
  641. struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
  642. int err;
  643. if (!i2sdev)
  644. return -EINVAL;
  645. if (i2sdev->out.substream != substream)
  646. return -EINVAL;
  647. err = i2sbus_pcm_close(i2sdev, 0);
  648. if (!err)
  649. i2sdev->out.substream = NULL;
  650. return err;
  651. }
  652. static int i2sbus_playback_prepare(struct snd_pcm_substream *substream)
  653. {
  654. struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
  655. if (!i2sdev)
  656. return -EINVAL;
  657. if (i2sdev->out.substream != substream)
  658. return -EINVAL;
  659. return i2sbus_pcm_prepare(i2sdev, 0);
  660. }
  661. static int i2sbus_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  662. {
  663. struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
  664. if (!i2sdev)
  665. return -EINVAL;
  666. if (i2sdev->out.substream != substream)
  667. return -EINVAL;
  668. return i2sbus_pcm_trigger(i2sdev, 0, cmd);
  669. }
  670. static snd_pcm_uframes_t i2sbus_playback_pointer(struct snd_pcm_substream
  671. *substream)
  672. {
  673. struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
  674. if (!i2sdev)
  675. return -EINVAL;
  676. if (i2sdev->out.substream != substream)
  677. return 0;
  678. return i2sbus_pcm_pointer(i2sdev, 0);
  679. }
  680. static struct snd_pcm_ops i2sbus_playback_ops = {
  681. .open = i2sbus_playback_open,
  682. .close = i2sbus_playback_close,
  683. .ioctl = snd_pcm_lib_ioctl,
  684. .hw_params = i2sbus_hw_params,
  685. .hw_free = i2sbus_playback_hw_free,
  686. .prepare = i2sbus_playback_prepare,
  687. .trigger = i2sbus_playback_trigger,
  688. .pointer = i2sbus_playback_pointer,
  689. };
  690. static int i2sbus_record_open(struct snd_pcm_substream *substream)
  691. {
  692. struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
  693. if (!i2sdev)
  694. return -EINVAL;
  695. i2sdev->in.substream = substream;
  696. return i2sbus_pcm_open(i2sdev, 1);
  697. }
  698. static int i2sbus_record_close(struct snd_pcm_substream *substream)
  699. {
  700. struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
  701. int err;
  702. if (!i2sdev)
  703. return -EINVAL;
  704. if (i2sdev->in.substream != substream)
  705. return -EINVAL;
  706. err = i2sbus_pcm_close(i2sdev, 1);
  707. if (!err)
  708. i2sdev->in.substream = NULL;
  709. return err;
  710. }
  711. static int i2sbus_record_prepare(struct snd_pcm_substream *substream)
  712. {
  713. struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
  714. if (!i2sdev)
  715. return -EINVAL;
  716. if (i2sdev->in.substream != substream)
  717. return -EINVAL;
  718. return i2sbus_pcm_prepare(i2sdev, 1);
  719. }
  720. static int i2sbus_record_trigger(struct snd_pcm_substream *substream, int cmd)
  721. {
  722. struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
  723. if (!i2sdev)
  724. return -EINVAL;
  725. if (i2sdev->in.substream != substream)
  726. return -EINVAL;
  727. return i2sbus_pcm_trigger(i2sdev, 1, cmd);
  728. }
  729. static snd_pcm_uframes_t i2sbus_record_pointer(struct snd_pcm_substream
  730. *substream)
  731. {
  732. struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
  733. if (!i2sdev)
  734. return -EINVAL;
  735. if (i2sdev->in.substream != substream)
  736. return 0;
  737. return i2sbus_pcm_pointer(i2sdev, 1);
  738. }
  739. static struct snd_pcm_ops i2sbus_record_ops = {
  740. .open = i2sbus_record_open,
  741. .close = i2sbus_record_close,
  742. .ioctl = snd_pcm_lib_ioctl,
  743. .hw_params = i2sbus_hw_params,
  744. .hw_free = i2sbus_record_hw_free,
  745. .prepare = i2sbus_record_prepare,
  746. .trigger = i2sbus_record_trigger,
  747. .pointer = i2sbus_record_pointer,
  748. };
  749. static void i2sbus_private_free(struct snd_pcm *pcm)
  750. {
  751. struct i2sbus_dev *i2sdev = snd_pcm_chip(pcm);
  752. struct codec_info_item *p, *tmp;
  753. i2sdev->sound.pcm = NULL;
  754. i2sdev->out.created = 0;
  755. i2sdev->in.created = 0;
  756. list_for_each_entry_safe(p, tmp, &i2sdev->sound.codec_list, list) {
  757. printk(KERN_ERR "i2sbus: a codec didn't unregister!\n");
  758. list_del(&p->list);
  759. module_put(p->codec->owner);
  760. kfree(p);
  761. }
  762. soundbus_dev_put(&i2sdev->sound);
  763. module_put(THIS_MODULE);
  764. }
  765. int
  766. i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card,
  767. struct codec_info *ci, void *data)
  768. {
  769. int err, in = 0, out = 0;
  770. struct transfer_info *tmp;
  771. struct i2sbus_dev *i2sdev = soundbus_dev_to_i2sbus_dev(dev);
  772. struct codec_info_item *cii;
  773. if (!dev->pcmname || dev->pcmid == -1) {
  774. printk(KERN_ERR "i2sbus: pcm name and id must be set!\n");
  775. return -EINVAL;
  776. }
  777. list_for_each_entry(cii, &dev->codec_list, list) {
  778. if (cii->codec_data == data)
  779. return -EALREADY;
  780. }
  781. if (!ci->transfers || !ci->transfers->formats
  782. || !ci->transfers->rates || !ci->usable)
  783. return -EINVAL;
  784. /* we currently code the i2s transfer on the clock, and support only
  785. * 32 and 64 */
  786. if (ci->bus_factor != 32 && ci->bus_factor != 64)
  787. return -EINVAL;
  788. /* If you want to fix this, you need to keep track of what transport infos
  789. * are to be used, which codecs they belong to, and then fix all the
  790. * sysclock/busclock stuff above to depend on which is usable */
  791. list_for_each_entry(cii, &dev->codec_list, list) {
  792. if (cii->codec->sysclock_factor != ci->sysclock_factor) {
  793. printk(KERN_DEBUG
  794. "cannot yet handle multiple different sysclocks!\n");
  795. return -EINVAL;
  796. }
  797. if (cii->codec->bus_factor != ci->bus_factor) {
  798. printk(KERN_DEBUG
  799. "cannot yet handle multiple different bus clocks!\n");
  800. return -EINVAL;
  801. }
  802. }
  803. tmp = ci->transfers;
  804. while (tmp->formats && tmp->rates) {
  805. if (tmp->transfer_in)
  806. in = 1;
  807. else
  808. out = 1;
  809. tmp++;
  810. }
  811. cii = kzalloc(sizeof(struct codec_info_item), GFP_KERNEL);
  812. if (!cii) {
  813. printk(KERN_DEBUG "i2sbus: failed to allocate cii\n");
  814. return -ENOMEM;
  815. }
  816. /* use the private data to point to the codec info */
  817. cii->sdev = soundbus_dev_get(dev);
  818. cii->codec = ci;
  819. cii->codec_data = data;
  820. if (!cii->sdev) {
  821. printk(KERN_DEBUG
  822. "i2sbus: failed to get soundbus dev reference\n");
  823. err = -ENODEV;
  824. goto out_free_cii;
  825. }
  826. if (!try_module_get(THIS_MODULE)) {
  827. printk(KERN_DEBUG "i2sbus: failed to get module reference!\n");
  828. err = -EBUSY;
  829. goto out_put_sdev;
  830. }
  831. if (!try_module_get(ci->owner)) {
  832. printk(KERN_DEBUG
  833. "i2sbus: failed to get module reference to codec owner!\n");
  834. err = -EBUSY;
  835. goto out_put_this_module;
  836. }
  837. if (!dev->pcm) {
  838. err = snd_pcm_new(card, dev->pcmname, dev->pcmid, 0, 0,
  839. &dev->pcm);
  840. if (err) {
  841. printk(KERN_DEBUG "i2sbus: failed to create pcm\n");
  842. goto out_put_ci_module;
  843. }
  844. dev->pcm->dev = &dev->ofdev.dev;
  845. }
  846. /* ALSA yet again sucks.
  847. * If it is ever fixed, remove this line. See below. */
  848. out = in = 1;
  849. if (!i2sdev->out.created && out) {
  850. if (dev->pcm->card != card) {
  851. /* eh? */
  852. printk(KERN_ERR
  853. "Can't attach same bus to different cards!\n");
  854. err = -EINVAL;
  855. goto out_put_ci_module;
  856. }
  857. err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, 1);
  858. if (err)
  859. goto out_put_ci_module;
  860. snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
  861. &i2sbus_playback_ops);
  862. i2sdev->out.created = 1;
  863. }
  864. if (!i2sdev->in.created && in) {
  865. if (dev->pcm->card != card) {
  866. printk(KERN_ERR
  867. "Can't attach same bus to different cards!\n");
  868. err = -EINVAL;
  869. goto out_put_ci_module;
  870. }
  871. err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, 1);
  872. if (err)
  873. goto out_put_ci_module;
  874. snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
  875. &i2sbus_record_ops);
  876. i2sdev->in.created = 1;
  877. }
  878. /* so we have to register the pcm after adding any substream
  879. * to it because alsa doesn't create the devices for the
  880. * substreams when we add them later.
  881. * Therefore, force in and out on both busses (above) and
  882. * register the pcm now instead of just after creating it.
  883. */
  884. err = snd_device_register(card, dev->pcm);
  885. if (err) {
  886. printk(KERN_ERR "i2sbus: error registering new pcm\n");
  887. goto out_put_ci_module;
  888. }
  889. /* no errors any more, so let's add this to our list */
  890. list_add(&cii->list, &dev->codec_list);
  891. dev->pcm->private_data = i2sdev;
  892. dev->pcm->private_free = i2sbus_private_free;
  893. /* well, we really should support scatter/gather DMA */
  894. snd_pcm_lib_preallocate_pages_for_all(
  895. dev->pcm, SNDRV_DMA_TYPE_DEV,
  896. snd_dma_pci_data(macio_get_pci_dev(i2sdev->macio)),
  897. 64 * 1024, 64 * 1024);
  898. return 0;
  899. out_put_ci_module:
  900. module_put(ci->owner);
  901. out_put_this_module:
  902. module_put(THIS_MODULE);
  903. out_put_sdev:
  904. soundbus_dev_put(dev);
  905. out_free_cii:
  906. kfree(cii);
  907. return err;
  908. }
  909. void i2sbus_detach_codec(struct soundbus_dev *dev, void *data)
  910. {
  911. struct codec_info_item *cii = NULL, *i;
  912. list_for_each_entry(i, &dev->codec_list, list) {
  913. if (i->codec_data == data) {
  914. cii = i;
  915. break;
  916. }
  917. }
  918. if (cii) {
  919. list_del(&cii->list);
  920. module_put(cii->codec->owner);
  921. kfree(cii);
  922. }
  923. /* no more codecs, but still a pcm? */
  924. if (list_empty(&dev->codec_list) && dev->pcm) {
  925. /* the actual cleanup is done by the callback above! */
  926. snd_device_free(dev->pcm->card, dev->pcm);
  927. }
  928. }