i2sbus-pcm.c 27 KB

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