pcm.c 27 KB

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