pcm.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. }