i2sbus-pcm.c 27 KB

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