i2sbus-pcm.c 26 KB

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