pcm.c 27 KB

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