fsi.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. * Fifo-attached Serial Interface (FSI) support for SH7724
  3. *
  4. * Copyright (C) 2009 Renesas Solutions Corp.
  5. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  6. *
  7. * Based on ssi.c
  8. * Copyright (c) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/delay.h>
  18. #include <linux/list.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/initval.h>
  24. #include <sound/soc.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/sh_fsi.h>
  27. #include <asm/atomic.h>
  28. #include <asm/dma.h>
  29. #include <asm/dma-sh.h>
  30. #define DO_FMT 0x0000
  31. #define DOFF_CTL 0x0004
  32. #define DOFF_ST 0x0008
  33. #define DI_FMT 0x000C
  34. #define DIFF_CTL 0x0010
  35. #define DIFF_ST 0x0014
  36. #define CKG1 0x0018
  37. #define CKG2 0x001C
  38. #define DIDT 0x0020
  39. #define DODT 0x0024
  40. #define MUTE_ST 0x0028
  41. #define REG_END MUTE_ST
  42. #define INT_ST 0x0200
  43. #define IEMSK 0x0204
  44. #define IMSK 0x0208
  45. #define MUTE 0x020C
  46. #define CLK_RST 0x0210
  47. #define SOFT_RST 0x0214
  48. #define MREG_START INT_ST
  49. #define MREG_END SOFT_RST
  50. /* DO_FMT */
  51. /* DI_FMT */
  52. #define CR_FMT(param) ((param) << 4)
  53. # define CR_MONO 0x0
  54. # define CR_MONO_D 0x1
  55. # define CR_PCM 0x2
  56. # define CR_I2S 0x3
  57. # define CR_TDM 0x4
  58. # define CR_TDM_D 0x5
  59. /* DOFF_CTL */
  60. /* DIFF_CTL */
  61. #define IRQ_HALF 0x00100000
  62. #define FIFO_CLR 0x00000001
  63. /* DOFF_ST */
  64. #define ERR_OVER 0x00000010
  65. #define ERR_UNDER 0x00000001
  66. /* CLK_RST */
  67. #define B_CLK 0x00000010
  68. #define A_CLK 0x00000001
  69. /* INT_ST */
  70. #define INT_B_IN (1 << 12)
  71. #define INT_B_OUT (1 << 8)
  72. #define INT_A_IN (1 << 4)
  73. #define INT_A_OUT (1 << 0)
  74. #define FSI_RATES SNDRV_PCM_RATE_8000_96000
  75. #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  76. /************************************************************************
  77. struct
  78. ************************************************************************/
  79. struct fsi_priv {
  80. void __iomem *base;
  81. struct snd_pcm_substream *substream;
  82. int fifo_max;
  83. int chan;
  84. int dma_chan;
  85. int byte_offset;
  86. int period_len;
  87. int buffer_len;
  88. int periods;
  89. };
  90. struct fsi_master {
  91. void __iomem *base;
  92. int irq;
  93. struct clk *clk;
  94. struct fsi_priv fsia;
  95. struct fsi_priv fsib;
  96. struct sh_fsi_platform_info *info;
  97. };
  98. static struct fsi_master *master;
  99. /************************************************************************
  100. basic read write function
  101. ************************************************************************/
  102. static int __fsi_reg_write(u32 reg, u32 data)
  103. {
  104. /* valid data area is 24bit */
  105. data &= 0x00ffffff;
  106. return ctrl_outl(data, reg);
  107. }
  108. static u32 __fsi_reg_read(u32 reg)
  109. {
  110. return ctrl_inl(reg);
  111. }
  112. static int __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
  113. {
  114. u32 val = __fsi_reg_read(reg);
  115. val &= ~mask;
  116. val |= data & mask;
  117. return __fsi_reg_write(reg, val);
  118. }
  119. static int fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data)
  120. {
  121. if (reg > REG_END)
  122. return -1;
  123. return __fsi_reg_write((u32)(fsi->base + reg), data);
  124. }
  125. static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg)
  126. {
  127. if (reg > REG_END)
  128. return 0;
  129. return __fsi_reg_read((u32)(fsi->base + reg));
  130. }
  131. static int fsi_reg_mask_set(struct fsi_priv *fsi, u32 reg, u32 mask, u32 data)
  132. {
  133. if (reg > REG_END)
  134. return -1;
  135. return __fsi_reg_mask_set((u32)(fsi->base + reg), mask, data);
  136. }
  137. static int fsi_master_write(u32 reg, u32 data)
  138. {
  139. if ((reg < MREG_START) ||
  140. (reg > MREG_END))
  141. return -1;
  142. return __fsi_reg_write((u32)(master->base + reg), data);
  143. }
  144. static u32 fsi_master_read(u32 reg)
  145. {
  146. if ((reg < MREG_START) ||
  147. (reg > MREG_END))
  148. return 0;
  149. return __fsi_reg_read((u32)(master->base + reg));
  150. }
  151. static int fsi_master_mask_set(u32 reg, u32 mask, u32 data)
  152. {
  153. if ((reg < MREG_START) ||
  154. (reg > MREG_END))
  155. return -1;
  156. return __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
  157. }
  158. /************************************************************************
  159. basic function
  160. ************************************************************************/
  161. static struct fsi_priv *fsi_get(struct snd_pcm_substream *substream)
  162. {
  163. struct snd_soc_pcm_runtime *rtd;
  164. struct fsi_priv *fsi = NULL;
  165. if (!substream || !master)
  166. return NULL;
  167. rtd = substream->private_data;
  168. switch (rtd->dai->cpu_dai->id) {
  169. case 0:
  170. fsi = &master->fsia;
  171. break;
  172. case 1:
  173. fsi = &master->fsib;
  174. break;
  175. }
  176. return fsi;
  177. }
  178. static int fsi_is_port_a(struct fsi_priv *fsi)
  179. {
  180. /* return
  181. * 1 : port a
  182. * 0 : port b
  183. */
  184. if (fsi == &master->fsia)
  185. return 1;
  186. return 0;
  187. }
  188. static u32 fsi_get_info_flags(struct fsi_priv *fsi)
  189. {
  190. int is_porta = fsi_is_port_a(fsi);
  191. return is_porta ? master->info->porta_flags :
  192. master->info->portb_flags;
  193. }
  194. static int fsi_is_master_mode(struct fsi_priv *fsi, int is_play)
  195. {
  196. u32 mode;
  197. u32 flags = fsi_get_info_flags(fsi);
  198. mode = is_play ? SH_FSI_OUT_SLAVE_MODE : SH_FSI_IN_SLAVE_MODE;
  199. /* return
  200. * 1 : master mode
  201. * 0 : slave mode
  202. */
  203. return (mode & flags) != mode;
  204. }
  205. static u32 fsi_port_ab_io_bit(struct fsi_priv *fsi, int is_play)
  206. {
  207. int is_porta = fsi_is_port_a(fsi);
  208. u32 data;
  209. if (is_porta)
  210. data = is_play ? (1 << 0) : (1 << 4);
  211. else
  212. data = is_play ? (1 << 8) : (1 << 12);
  213. return data;
  214. }
  215. static void fsi_stream_push(struct fsi_priv *fsi,
  216. struct snd_pcm_substream *substream,
  217. u32 buffer_len,
  218. u32 period_len)
  219. {
  220. fsi->substream = substream;
  221. fsi->buffer_len = buffer_len;
  222. fsi->period_len = period_len;
  223. fsi->byte_offset = 0;
  224. fsi->periods = 0;
  225. }
  226. static void fsi_stream_pop(struct fsi_priv *fsi)
  227. {
  228. fsi->substream = NULL;
  229. fsi->buffer_len = 0;
  230. fsi->period_len = 0;
  231. fsi->byte_offset = 0;
  232. fsi->periods = 0;
  233. }
  234. static int fsi_get_fifo_residue(struct fsi_priv *fsi, int is_play)
  235. {
  236. u32 status;
  237. u32 reg = is_play ? DOFF_ST : DIFF_ST;
  238. int residue;
  239. status = fsi_reg_read(fsi, reg);
  240. residue = 0x1ff & (status >> 8);
  241. residue *= fsi->chan;
  242. return residue;
  243. }
  244. static int fsi_get_residue(struct fsi_priv *fsi, int is_play)
  245. {
  246. int residue;
  247. int width;
  248. struct snd_pcm_runtime *runtime;
  249. runtime = fsi->substream->runtime;
  250. /* get 1 channel data width */
  251. width = frames_to_bytes(runtime, 1) / fsi->chan;
  252. if (2 == width)
  253. residue = fsi_get_fifo_residue(fsi, is_play);
  254. else
  255. residue = get_dma_residue(fsi->dma_chan);
  256. return residue;
  257. }
  258. /************************************************************************
  259. basic dma function
  260. ************************************************************************/
  261. #define PORTA_DMA 0
  262. #define PORTB_DMA 1
  263. static int fsi_get_dma_chan(void)
  264. {
  265. if (0 != request_dma(PORTA_DMA, "fsia"))
  266. return -EIO;
  267. if (0 != request_dma(PORTB_DMA, "fsib")) {
  268. free_dma(PORTA_DMA);
  269. return -EIO;
  270. }
  271. master->fsia.dma_chan = PORTA_DMA;
  272. master->fsib.dma_chan = PORTB_DMA;
  273. return 0;
  274. }
  275. static void fsi_free_dma_chan(void)
  276. {
  277. dma_wait_for_completion(PORTA_DMA);
  278. dma_wait_for_completion(PORTB_DMA);
  279. free_dma(PORTA_DMA);
  280. free_dma(PORTB_DMA);
  281. master->fsia.dma_chan = -1;
  282. master->fsib.dma_chan = -1;
  283. }
  284. /************************************************************************
  285. ctrl function
  286. ************************************************************************/
  287. static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
  288. {
  289. u32 data = fsi_port_ab_io_bit(fsi, is_play);
  290. fsi_master_mask_set(IMSK, data, data);
  291. fsi_master_mask_set(IEMSK, data, data);
  292. }
  293. static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
  294. {
  295. u32 data = fsi_port_ab_io_bit(fsi, is_play);
  296. fsi_master_mask_set(IMSK, data, 0);
  297. fsi_master_mask_set(IEMSK, data, 0);
  298. }
  299. static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
  300. {
  301. u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
  302. if (enable)
  303. fsi_master_mask_set(CLK_RST, val, val);
  304. else
  305. fsi_master_mask_set(CLK_RST, val, 0);
  306. }
  307. static void fsi_irq_init(struct fsi_priv *fsi, int is_play)
  308. {
  309. u32 data;
  310. u32 ctrl;
  311. data = fsi_port_ab_io_bit(fsi, is_play);
  312. ctrl = is_play ? DOFF_CTL : DIFF_CTL;
  313. /* set IMSK */
  314. fsi_irq_disable(fsi, is_play);
  315. /* set interrupt generation factor */
  316. fsi_reg_write(fsi, ctrl, IRQ_HALF);
  317. /* clear FIFO */
  318. fsi_reg_mask_set(fsi, ctrl, FIFO_CLR, FIFO_CLR);
  319. /* clear interrupt factor */
  320. fsi_master_mask_set(INT_ST, data, 0);
  321. }
  322. static void fsi_soft_all_reset(void)
  323. {
  324. u32 status = fsi_master_read(SOFT_RST);
  325. /* port AB reset */
  326. status &= 0x000000ff;
  327. fsi_master_write(SOFT_RST, status);
  328. mdelay(10);
  329. /* soft reset */
  330. status &= 0x000000f0;
  331. fsi_master_write(SOFT_RST, status);
  332. status |= 0x00000001;
  333. fsi_master_write(SOFT_RST, status);
  334. mdelay(10);
  335. }
  336. static void fsi_16data_push(struct fsi_priv *fsi,
  337. struct snd_pcm_runtime *runtime,
  338. int send)
  339. {
  340. u16 *dma_start;
  341. u32 snd;
  342. int i;
  343. /* get dma start position for FSI */
  344. dma_start = (u16 *)runtime->dma_area;
  345. dma_start += fsi->byte_offset / 2;
  346. /*
  347. * soft dma
  348. * FSI can not use DMA when 16bpp
  349. */
  350. for (i = 0; i < send; i++) {
  351. snd = (u32)dma_start[i];
  352. fsi_reg_write(fsi, DODT, snd << 8);
  353. }
  354. }
  355. static void fsi_32data_push(struct fsi_priv *fsi,
  356. struct snd_pcm_runtime *runtime,
  357. int send)
  358. {
  359. u32 *dma_start;
  360. /* get dma start position for FSI */
  361. dma_start = (u32 *)runtime->dma_area;
  362. dma_start += fsi->byte_offset / 4;
  363. dma_wait_for_completion(fsi->dma_chan);
  364. dma_configure_channel(fsi->dma_chan, (SM_INC|0x400|TS_32|TM_BUR));
  365. dma_write(fsi->dma_chan, (u32)dma_start,
  366. (u32)(fsi->base + DODT), send * 4);
  367. }
  368. /* playback interrupt */
  369. static int fsi_data_push(struct fsi_priv *fsi)
  370. {
  371. struct snd_pcm_runtime *runtime;
  372. struct snd_pcm_substream *substream = NULL;
  373. int send;
  374. int fifo_free;
  375. int width;
  376. if (!fsi ||
  377. !fsi->substream ||
  378. !fsi->substream->runtime)
  379. return -EINVAL;
  380. runtime = fsi->substream->runtime;
  381. /* FSI FIFO has limit.
  382. * So, this driver can not send periods data at a time
  383. */
  384. if (fsi->byte_offset >=
  385. fsi->period_len * (fsi->periods + 1)) {
  386. substream = fsi->substream;
  387. fsi->periods = (fsi->periods + 1) % runtime->periods;
  388. if (0 == fsi->periods)
  389. fsi->byte_offset = 0;
  390. }
  391. /* get 1 channel data width */
  392. width = frames_to_bytes(runtime, 1) / fsi->chan;
  393. /* get send size for alsa */
  394. send = (fsi->buffer_len - fsi->byte_offset) / width;
  395. /* get FIFO free size */
  396. fifo_free = (fsi->fifo_max * fsi->chan) - fsi_get_fifo_residue(fsi, 1);
  397. /* size check */
  398. if (fifo_free < send)
  399. send = fifo_free;
  400. if (2 == width)
  401. fsi_16data_push(fsi, runtime, send);
  402. else if (4 == width)
  403. fsi_32data_push(fsi, runtime, send);
  404. else
  405. return -EINVAL;
  406. fsi->byte_offset += send * width;
  407. fsi_irq_enable(fsi, 1);
  408. if (substream)
  409. snd_pcm_period_elapsed(substream);
  410. return 0;
  411. }
  412. static irqreturn_t fsi_interrupt(int irq, void *data)
  413. {
  414. u32 status = fsi_master_read(SOFT_RST) & ~0x00000010;
  415. u32 int_st = fsi_master_read(INT_ST);
  416. /* clear irq status */
  417. fsi_master_write(SOFT_RST, status);
  418. fsi_master_write(SOFT_RST, status | 0x00000010);
  419. if (int_st & INT_A_OUT)
  420. fsi_data_push(&master->fsia);
  421. if (int_st & INT_B_OUT)
  422. fsi_data_push(&master->fsib);
  423. fsi_master_write(INT_ST, 0x0000000);
  424. return IRQ_HANDLED;
  425. }
  426. /************************************************************************
  427. dai ops
  428. ************************************************************************/
  429. static int fsi_dai_startup(struct snd_pcm_substream *substream,
  430. struct snd_soc_dai *dai)
  431. {
  432. struct fsi_priv *fsi = fsi_get(substream);
  433. const char *msg;
  434. u32 flags = fsi_get_info_flags(fsi);
  435. u32 fmt;
  436. u32 reg;
  437. u32 data;
  438. int is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  439. int is_master;
  440. int ret = 0;
  441. clk_enable(master->clk);
  442. /* CKG1 */
  443. data = is_play ? (1 << 0) : (1 << 4);
  444. is_master = fsi_is_master_mode(fsi, is_play);
  445. if (is_master)
  446. fsi_reg_mask_set(fsi, CKG1, data, data);
  447. else
  448. fsi_reg_mask_set(fsi, CKG1, data, 0);
  449. /* clock inversion (CKG2) */
  450. data = 0;
  451. switch (SH_FSI_INVERSION_MASK & flags) {
  452. case SH_FSI_LRM_INV:
  453. data = 1 << 12;
  454. break;
  455. case SH_FSI_BRM_INV:
  456. data = 1 << 8;
  457. break;
  458. case SH_FSI_LRS_INV:
  459. data = 1 << 4;
  460. break;
  461. case SH_FSI_BRS_INV:
  462. data = 1 << 0;
  463. break;
  464. }
  465. fsi_reg_write(fsi, CKG2, data);
  466. /* do fmt, di fmt */
  467. data = 0;
  468. reg = is_play ? DO_FMT : DI_FMT;
  469. fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
  470. switch (fmt) {
  471. case SH_FSI_FMT_MONO:
  472. msg = "MONO";
  473. data = CR_FMT(CR_MONO);
  474. fsi->chan = 1;
  475. break;
  476. case SH_FSI_FMT_MONO_DELAY:
  477. msg = "MONO Delay";
  478. data = CR_FMT(CR_MONO_D);
  479. fsi->chan = 1;
  480. break;
  481. case SH_FSI_FMT_PCM:
  482. msg = "PCM";
  483. data = CR_FMT(CR_PCM);
  484. fsi->chan = 2;
  485. break;
  486. case SH_FSI_FMT_I2S:
  487. msg = "I2S";
  488. data = CR_FMT(CR_I2S);
  489. fsi->chan = 2;
  490. break;
  491. case SH_FSI_FMT_TDM:
  492. msg = "TDM";
  493. data = CR_FMT(CR_TDM) | (fsi->chan - 1);
  494. fsi->chan = is_play ?
  495. SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
  496. break;
  497. case SH_FSI_FMT_TDM_DELAY:
  498. msg = "TDM Delay";
  499. data = CR_FMT(CR_TDM_D) | (fsi->chan - 1);
  500. fsi->chan = is_play ?
  501. SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
  502. break;
  503. default:
  504. dev_err(dai->dev, "unknown format.\n");
  505. return -EINVAL;
  506. }
  507. switch (fsi->chan) {
  508. case 1:
  509. fsi->fifo_max = 256;
  510. break;
  511. case 2:
  512. fsi->fifo_max = 128;
  513. break;
  514. case 3:
  515. case 4:
  516. fsi->fifo_max = 64;
  517. break;
  518. case 5:
  519. case 6:
  520. case 7:
  521. case 8:
  522. fsi->fifo_max = 32;
  523. break;
  524. default:
  525. dev_err(dai->dev, "channel size error.\n");
  526. return -EINVAL;
  527. }
  528. fsi_reg_write(fsi, reg, data);
  529. dev_dbg(dai->dev, "use %s format (%d channel) use %d DMAC\n",
  530. msg, fsi->chan, fsi->dma_chan);
  531. /*
  532. * clear clk reset if master mode
  533. */
  534. if (is_master)
  535. fsi_clk_ctrl(fsi, 1);
  536. /* irq setting */
  537. fsi_irq_init(fsi, is_play);
  538. return ret;
  539. }
  540. static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
  541. struct snd_soc_dai *dai)
  542. {
  543. struct fsi_priv *fsi = fsi_get(substream);
  544. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  545. fsi_irq_disable(fsi, is_play);
  546. fsi_clk_ctrl(fsi, 0);
  547. clk_disable(master->clk);
  548. }
  549. static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  550. struct snd_soc_dai *dai)
  551. {
  552. struct fsi_priv *fsi = fsi_get(substream);
  553. struct snd_pcm_runtime *runtime = substream->runtime;
  554. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  555. int ret = 0;
  556. /* capture not supported */
  557. if (!is_play)
  558. return -ENODEV;
  559. switch (cmd) {
  560. case SNDRV_PCM_TRIGGER_START:
  561. fsi_stream_push(fsi, substream,
  562. frames_to_bytes(runtime, runtime->buffer_size),
  563. frames_to_bytes(runtime, runtime->period_size));
  564. ret = fsi_data_push(fsi);
  565. break;
  566. case SNDRV_PCM_TRIGGER_STOP:
  567. fsi_irq_disable(fsi, is_play);
  568. fsi_stream_pop(fsi);
  569. break;
  570. }
  571. return ret;
  572. }
  573. static struct snd_soc_dai_ops fsi_dai_ops = {
  574. .startup = fsi_dai_startup,
  575. .shutdown = fsi_dai_shutdown,
  576. .trigger = fsi_dai_trigger,
  577. };
  578. /************************************************************************
  579. pcm ops
  580. ************************************************************************/
  581. static struct snd_pcm_hardware fsi_pcm_hardware = {
  582. .info = SNDRV_PCM_INFO_INTERLEAVED |
  583. SNDRV_PCM_INFO_MMAP |
  584. SNDRV_PCM_INFO_MMAP_VALID |
  585. SNDRV_PCM_INFO_PAUSE,
  586. .formats = FSI_FMTS,
  587. .rates = FSI_RATES,
  588. .rate_min = 8000,
  589. .rate_max = 192000,
  590. .channels_min = 1,
  591. .channels_max = 2,
  592. .buffer_bytes_max = 64 * 1024,
  593. .period_bytes_min = 32,
  594. .period_bytes_max = 8192,
  595. .periods_min = 1,
  596. .periods_max = 32,
  597. .fifo_size = 256,
  598. };
  599. static int fsi_pcm_open(struct snd_pcm_substream *substream)
  600. {
  601. struct snd_pcm_runtime *runtime = substream->runtime;
  602. int ret = 0;
  603. snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
  604. ret = snd_pcm_hw_constraint_integer(runtime,
  605. SNDRV_PCM_HW_PARAM_PERIODS);
  606. return ret;
  607. }
  608. static int fsi_hw_params(struct snd_pcm_substream *substream,
  609. struct snd_pcm_hw_params *hw_params)
  610. {
  611. return snd_pcm_lib_malloc_pages(substream,
  612. params_buffer_bytes(hw_params));
  613. }
  614. static int fsi_hw_free(struct snd_pcm_substream *substream)
  615. {
  616. return snd_pcm_lib_free_pages(substream);
  617. }
  618. static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
  619. {
  620. struct snd_pcm_runtime *runtime = substream->runtime;
  621. struct fsi_priv *fsi = fsi_get(substream);
  622. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  623. long location;
  624. location = (fsi->byte_offset - 1) - fsi_get_residue(fsi, is_play);
  625. if (location < 0)
  626. location = 0;
  627. return bytes_to_frames(runtime, location);
  628. }
  629. static struct snd_pcm_ops fsi_pcm_ops = {
  630. .open = fsi_pcm_open,
  631. .ioctl = snd_pcm_lib_ioctl,
  632. .hw_params = fsi_hw_params,
  633. .hw_free = fsi_hw_free,
  634. .pointer = fsi_pointer,
  635. };
  636. /************************************************************************
  637. snd_soc_platform
  638. ************************************************************************/
  639. #define PREALLOC_BUFFER (32 * 1024)
  640. #define PREALLOC_BUFFER_MAX (32 * 1024)
  641. static void fsi_pcm_free(struct snd_pcm *pcm)
  642. {
  643. snd_pcm_lib_preallocate_free_for_all(pcm);
  644. }
  645. static int fsi_pcm_new(struct snd_card *card,
  646. struct snd_soc_dai *dai,
  647. struct snd_pcm *pcm)
  648. {
  649. /*
  650. * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
  651. * in MMAP mode (i.e. aplay -M)
  652. */
  653. return snd_pcm_lib_preallocate_pages_for_all(
  654. pcm,
  655. SNDRV_DMA_TYPE_CONTINUOUS,
  656. snd_dma_continuous_data(GFP_KERNEL),
  657. PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
  658. }
  659. /************************************************************************
  660. alsa struct
  661. ************************************************************************/
  662. struct snd_soc_dai fsi_soc_dai[] = {
  663. {
  664. .name = "FSIA",
  665. .id = 0,
  666. .playback = {
  667. .rates = FSI_RATES,
  668. .formats = FSI_FMTS,
  669. .channels_min = 1,
  670. .channels_max = 8,
  671. },
  672. /* capture not supported */
  673. .ops = &fsi_dai_ops,
  674. },
  675. {
  676. .name = "FSIB",
  677. .id = 1,
  678. .playback = {
  679. .rates = FSI_RATES,
  680. .formats = FSI_FMTS,
  681. .channels_min = 1,
  682. .channels_max = 8,
  683. },
  684. /* capture not supported */
  685. .ops = &fsi_dai_ops,
  686. },
  687. };
  688. EXPORT_SYMBOL_GPL(fsi_soc_dai);
  689. struct snd_soc_platform fsi_soc_platform = {
  690. .name = "fsi-pcm",
  691. .pcm_ops = &fsi_pcm_ops,
  692. .pcm_new = fsi_pcm_new,
  693. .pcm_free = fsi_pcm_free,
  694. };
  695. EXPORT_SYMBOL_GPL(fsi_soc_platform);
  696. /************************************************************************
  697. platform function
  698. ************************************************************************/
  699. static int fsi_probe(struct platform_device *pdev)
  700. {
  701. struct resource *res;
  702. char clk_name[8];
  703. unsigned int irq;
  704. int ret;
  705. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  706. irq = platform_get_irq(pdev, 0);
  707. if (!res || !irq) {
  708. dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
  709. ret = -ENODEV;
  710. goto exit;
  711. }
  712. master = kzalloc(sizeof(*master), GFP_KERNEL);
  713. if (!master) {
  714. dev_err(&pdev->dev, "Could not allocate master\n");
  715. ret = -ENOMEM;
  716. goto exit;
  717. }
  718. master->base = ioremap_nocache(res->start, resource_size(res));
  719. if (!master->base) {
  720. ret = -ENXIO;
  721. dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
  722. goto exit_kfree;
  723. }
  724. master->irq = irq;
  725. master->info = pdev->dev.platform_data;
  726. master->fsia.base = master->base;
  727. master->fsib.base = master->base + 0x40;
  728. master->fsia.dma_chan = -1;
  729. master->fsib.dma_chan = -1;
  730. ret = fsi_get_dma_chan();
  731. if (ret < 0) {
  732. dev_err(&pdev->dev, "cannot get dma api\n");
  733. goto exit_iounmap;
  734. }
  735. /* FSI is based on SPU mstp */
  736. snprintf(clk_name, sizeof(clk_name), "spu%d", pdev->id);
  737. master->clk = clk_get(NULL, clk_name);
  738. if (IS_ERR(master->clk)) {
  739. dev_err(&pdev->dev, "cannot get %s mstp\n", clk_name);
  740. ret = -EIO;
  741. goto exit_free_dma;
  742. }
  743. fsi_soc_dai[0].dev = &pdev->dev;
  744. fsi_soc_dai[1].dev = &pdev->dev;
  745. fsi_soft_all_reset();
  746. ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED, "fsi", master);
  747. if (ret) {
  748. dev_err(&pdev->dev, "irq request err\n");
  749. goto exit_free_dma;
  750. }
  751. ret = snd_soc_register_platform(&fsi_soc_platform);
  752. if (ret < 0) {
  753. dev_err(&pdev->dev, "cannot snd soc register\n");
  754. goto exit_free_irq;
  755. }
  756. return snd_soc_register_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
  757. exit_free_irq:
  758. free_irq(irq, master);
  759. exit_free_dma:
  760. fsi_free_dma_chan();
  761. exit_iounmap:
  762. iounmap(master->base);
  763. exit_kfree:
  764. kfree(master);
  765. master = NULL;
  766. exit:
  767. return ret;
  768. }
  769. static int fsi_remove(struct platform_device *pdev)
  770. {
  771. snd_soc_unregister_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
  772. snd_soc_unregister_platform(&fsi_soc_platform);
  773. clk_put(master->clk);
  774. fsi_free_dma_chan();
  775. free_irq(master->irq, master);
  776. iounmap(master->base);
  777. kfree(master);
  778. master = NULL;
  779. return 0;
  780. }
  781. static struct platform_driver fsi_driver = {
  782. .driver = {
  783. .name = "sh_fsi",
  784. },
  785. .probe = fsi_probe,
  786. .remove = fsi_remove,
  787. };
  788. static int __init fsi_mobile_init(void)
  789. {
  790. return platform_driver_register(&fsi_driver);
  791. }
  792. static void __exit fsi_mobile_exit(void)
  793. {
  794. platform_driver_unregister(&fsi_driver);
  795. }
  796. module_init(fsi_mobile_init);
  797. module_exit(fsi_mobile_exit);
  798. MODULE_LICENSE("GPL");
  799. MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
  800. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");