pcm.c 28 KB

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