fsi.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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/delay.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/io.h>
  17. #include <linux/slab.h>
  18. #include <sound/soc.h>
  19. #include <sound/sh_fsi.h>
  20. #define DO_FMT 0x0000
  21. #define DOFF_CTL 0x0004
  22. #define DOFF_ST 0x0008
  23. #define DI_FMT 0x000C
  24. #define DIFF_CTL 0x0010
  25. #define DIFF_ST 0x0014
  26. #define CKG1 0x0018
  27. #define CKG2 0x001C
  28. #define DIDT 0x0020
  29. #define DODT 0x0024
  30. #define MUTE_ST 0x0028
  31. #define OUT_SEL 0x0030
  32. #define REG_END OUT_SEL
  33. #define A_MST_CTLR 0x0180
  34. #define B_MST_CTLR 0x01A0
  35. #define CPU_INT_ST 0x01F4
  36. #define CPU_IEMSK 0x01F8
  37. #define CPU_IMSK 0x01FC
  38. #define INT_ST 0x0200
  39. #define IEMSK 0x0204
  40. #define IMSK 0x0208
  41. #define MUTE 0x020C
  42. #define CLK_RST 0x0210
  43. #define SOFT_RST 0x0214
  44. #define FIFO_SZ 0x0218
  45. #define MREG_START A_MST_CTLR
  46. #define MREG_END FIFO_SZ
  47. /* DO_FMT */
  48. /* DI_FMT */
  49. #define CR_BWS_24 (0x0 << 20) /* FSI2 */
  50. #define CR_BWS_16 (0x1 << 20) /* FSI2 */
  51. #define CR_BWS_20 (0x2 << 20) /* FSI2 */
  52. #define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
  53. #define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
  54. #define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
  55. #define CR_MONO (0x0 << 4)
  56. #define CR_MONO_D (0x1 << 4)
  57. #define CR_PCM (0x2 << 4)
  58. #define CR_I2S (0x3 << 4)
  59. #define CR_TDM (0x4 << 4)
  60. #define CR_TDM_D (0x5 << 4)
  61. /* DOFF_CTL */
  62. /* DIFF_CTL */
  63. #define IRQ_HALF 0x00100000
  64. #define FIFO_CLR 0x00000001
  65. /* DOFF_ST */
  66. #define ERR_OVER 0x00000010
  67. #define ERR_UNDER 0x00000001
  68. #define ST_ERR (ERR_OVER | ERR_UNDER)
  69. /* CKG1 */
  70. #define ACKMD_MASK 0x00007000
  71. #define BPFMD_MASK 0x00000700
  72. /* A/B MST_CTLR */
  73. #define BP (1 << 4) /* Fix the signal of Biphase output */
  74. #define SE (1 << 0) /* Fix the master clock */
  75. /* CLK_RST */
  76. #define B_CLK 0x00000010
  77. #define A_CLK 0x00000001
  78. /* IO SHIFT / MACRO */
  79. #define BI_SHIFT 12
  80. #define BO_SHIFT 8
  81. #define AI_SHIFT 4
  82. #define AO_SHIFT 0
  83. #define AB_IO(param, shift) (param << shift)
  84. /* SOFT_RST */
  85. #define PBSR (1 << 12) /* Port B Software Reset */
  86. #define PASR (1 << 8) /* Port A Software Reset */
  87. #define IR (1 << 4) /* Interrupt Reset */
  88. #define FSISR (1 << 0) /* Software Reset */
  89. /* OUT_SEL (FSI2) */
  90. #define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
  91. /* 1: Biphase and serial */
  92. /* FIFO_SZ */
  93. #define FIFO_SZ_MASK 0x7
  94. #define FSI_RATES SNDRV_PCM_RATE_8000_96000
  95. #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  96. /*
  97. * FSI driver use below type name for variable
  98. *
  99. * xxx_len : data length
  100. * xxx_width : data width
  101. * xxx_offset : data offset
  102. * xxx_num : number of data
  103. */
  104. /*
  105. * struct
  106. */
  107. struct fsi_stream {
  108. struct snd_pcm_substream *substream;
  109. int fifo_max_num;
  110. int chan_num;
  111. int buff_offset;
  112. int buff_len;
  113. int period_len;
  114. int period_num;
  115. };
  116. struct fsi_priv {
  117. void __iomem *base;
  118. struct fsi_master *master;
  119. struct fsi_stream playback;
  120. struct fsi_stream capture;
  121. u32 mst_ctrl;
  122. };
  123. struct fsi_core {
  124. int ver;
  125. u32 int_st;
  126. u32 iemsk;
  127. u32 imsk;
  128. };
  129. struct fsi_master {
  130. void __iomem *base;
  131. int irq;
  132. struct fsi_priv fsia;
  133. struct fsi_priv fsib;
  134. struct fsi_core *core;
  135. struct sh_fsi_platform_info *info;
  136. spinlock_t lock;
  137. };
  138. /*
  139. * basic read write function
  140. */
  141. static void __fsi_reg_write(u32 reg, u32 data)
  142. {
  143. /* valid data area is 24bit */
  144. data &= 0x00ffffff;
  145. __raw_writel(data, reg);
  146. }
  147. static u32 __fsi_reg_read(u32 reg)
  148. {
  149. return __raw_readl(reg);
  150. }
  151. static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
  152. {
  153. u32 val = __fsi_reg_read(reg);
  154. val &= ~mask;
  155. val |= data & mask;
  156. __fsi_reg_write(reg, val);
  157. }
  158. static void fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data)
  159. {
  160. if (reg > REG_END) {
  161. pr_err("fsi: register access err (%s)\n", __func__);
  162. return;
  163. }
  164. __fsi_reg_write((u32)(fsi->base + reg), data);
  165. }
  166. static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg)
  167. {
  168. if (reg > REG_END) {
  169. pr_err("fsi: register access err (%s)\n", __func__);
  170. return 0;
  171. }
  172. return __fsi_reg_read((u32)(fsi->base + reg));
  173. }
  174. static void fsi_reg_mask_set(struct fsi_priv *fsi, u32 reg, u32 mask, u32 data)
  175. {
  176. if (reg > REG_END) {
  177. pr_err("fsi: register access err (%s)\n", __func__);
  178. return;
  179. }
  180. __fsi_reg_mask_set((u32)(fsi->base + reg), mask, data);
  181. }
  182. static u32 fsi_master_read(struct fsi_master *master, u32 reg)
  183. {
  184. u32 ret;
  185. unsigned long flags;
  186. if ((reg < MREG_START) ||
  187. (reg > MREG_END)) {
  188. pr_err("fsi: register access err (%s)\n", __func__);
  189. return 0;
  190. }
  191. spin_lock_irqsave(&master->lock, flags);
  192. ret = __fsi_reg_read((u32)(master->base + reg));
  193. spin_unlock_irqrestore(&master->lock, flags);
  194. return ret;
  195. }
  196. static void fsi_master_mask_set(struct fsi_master *master,
  197. u32 reg, u32 mask, u32 data)
  198. {
  199. unsigned long flags;
  200. if ((reg < MREG_START) ||
  201. (reg > MREG_END)) {
  202. pr_err("fsi: register access err (%s)\n", __func__);
  203. return;
  204. }
  205. spin_lock_irqsave(&master->lock, flags);
  206. __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
  207. spin_unlock_irqrestore(&master->lock, flags);
  208. }
  209. /*
  210. * basic function
  211. */
  212. static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
  213. {
  214. return fsi->master;
  215. }
  216. static int fsi_is_port_a(struct fsi_priv *fsi)
  217. {
  218. return fsi->master->base == fsi->base;
  219. }
  220. static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
  221. {
  222. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  223. return rtd->cpu_dai;
  224. }
  225. static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
  226. {
  227. struct snd_soc_dai *dai = fsi_get_dai(substream);
  228. struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
  229. if (dai->id == 0)
  230. return &master->fsia;
  231. else
  232. return &master->fsib;
  233. }
  234. static u32 fsi_get_info_flags(struct fsi_priv *fsi)
  235. {
  236. int is_porta = fsi_is_port_a(fsi);
  237. struct fsi_master *master = fsi_get_master(fsi);
  238. return is_porta ? master->info->porta_flags :
  239. master->info->portb_flags;
  240. }
  241. static inline int fsi_stream_is_play(int stream)
  242. {
  243. return stream == SNDRV_PCM_STREAM_PLAYBACK;
  244. }
  245. static inline int fsi_is_play(struct snd_pcm_substream *substream)
  246. {
  247. return fsi_stream_is_play(substream->stream);
  248. }
  249. static inline struct fsi_stream *fsi_get_stream(struct fsi_priv *fsi,
  250. int is_play)
  251. {
  252. return is_play ? &fsi->playback : &fsi->capture;
  253. }
  254. static int fsi_is_master_mode(struct fsi_priv *fsi, int is_play)
  255. {
  256. u32 mode;
  257. u32 flags = fsi_get_info_flags(fsi);
  258. mode = is_play ? SH_FSI_OUT_SLAVE_MODE : SH_FSI_IN_SLAVE_MODE;
  259. /* return
  260. * 1 : master mode
  261. * 0 : slave mode
  262. */
  263. return (mode & flags) != mode;
  264. }
  265. static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
  266. {
  267. int is_porta = fsi_is_port_a(fsi);
  268. u32 shift;
  269. if (is_porta)
  270. shift = is_play ? AO_SHIFT : AI_SHIFT;
  271. else
  272. shift = is_play ? BO_SHIFT : BI_SHIFT;
  273. return shift;
  274. }
  275. static void fsi_stream_push(struct fsi_priv *fsi,
  276. int is_play,
  277. struct snd_pcm_substream *substream,
  278. u32 buffer_len,
  279. u32 period_len)
  280. {
  281. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  282. io->substream = substream;
  283. io->buff_len = buffer_len;
  284. io->buff_offset = 0;
  285. io->period_len = period_len;
  286. io->period_num = 0;
  287. }
  288. static void fsi_stream_pop(struct fsi_priv *fsi, int is_play)
  289. {
  290. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  291. io->substream = NULL;
  292. io->buff_len = 0;
  293. io->buff_offset = 0;
  294. io->period_len = 0;
  295. io->period_num = 0;
  296. }
  297. static int fsi_get_fifo_data_num(struct fsi_priv *fsi, int is_play)
  298. {
  299. u32 status;
  300. u32 reg = is_play ? DOFF_ST : DIFF_ST;
  301. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  302. int data_num;
  303. status = fsi_reg_read(fsi, reg);
  304. data_num = 0x1ff & (status >> 8);
  305. data_num *= io->chan_num;
  306. return data_num;
  307. }
  308. static int fsi_len2num(int len, int width)
  309. {
  310. return len / width;
  311. }
  312. #define fsi_num2offset(a, b) fsi_num2len(a, b)
  313. static int fsi_num2len(int num, int width)
  314. {
  315. return num * width;
  316. }
  317. static int fsi_get_frame_width(struct fsi_priv *fsi, int is_play)
  318. {
  319. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  320. struct snd_pcm_substream *substream = io->substream;
  321. struct snd_pcm_runtime *runtime = substream->runtime;
  322. return frames_to_bytes(runtime, 1) / io->chan_num;
  323. }
  324. /*
  325. * dma function
  326. */
  327. static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream)
  328. {
  329. int is_play = fsi_stream_is_play(stream);
  330. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  331. return io->substream->runtime->dma_area + io->buff_offset;
  332. }
  333. static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
  334. {
  335. u16 *start;
  336. int i;
  337. start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
  338. for (i = 0; i < num; i++)
  339. fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
  340. }
  341. static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
  342. {
  343. u16 *start;
  344. int i;
  345. start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
  346. for (i = 0; i < num; i++)
  347. *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
  348. }
  349. static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num)
  350. {
  351. u32 *start;
  352. int i;
  353. start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
  354. for (i = 0; i < num; i++)
  355. fsi_reg_write(fsi, DODT, *(start + i));
  356. }
  357. static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
  358. {
  359. u32 *start;
  360. int i;
  361. start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
  362. for (i = 0; i < num; i++)
  363. *(start + i) = fsi_reg_read(fsi, DIDT);
  364. }
  365. /*
  366. * irq function
  367. */
  368. static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
  369. {
  370. u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
  371. struct fsi_master *master = fsi_get_master(fsi);
  372. fsi_master_mask_set(master, master->core->imsk, data, data);
  373. fsi_master_mask_set(master, master->core->iemsk, data, data);
  374. }
  375. static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
  376. {
  377. u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
  378. struct fsi_master *master = fsi_get_master(fsi);
  379. fsi_master_mask_set(master, master->core->imsk, data, 0);
  380. fsi_master_mask_set(master, master->core->iemsk, data, 0);
  381. }
  382. static u32 fsi_irq_get_status(struct fsi_master *master)
  383. {
  384. return fsi_master_read(master, master->core->int_st);
  385. }
  386. static void fsi_irq_clear_status(struct fsi_priv *fsi)
  387. {
  388. u32 data = 0;
  389. struct fsi_master *master = fsi_get_master(fsi);
  390. data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
  391. data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
  392. /* clear interrupt factor */
  393. fsi_master_mask_set(master, master->core->int_st, data, 0);
  394. }
  395. /*
  396. * SPDIF master clock function
  397. *
  398. * These functions are used later FSI2
  399. */
  400. static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
  401. {
  402. struct fsi_master *master = fsi_get_master(fsi);
  403. u32 val = BP | SE;
  404. if (master->core->ver < 2) {
  405. pr_err("fsi: register access err (%s)\n", __func__);
  406. return;
  407. }
  408. if (enable)
  409. fsi_master_mask_set(master, fsi->mst_ctrl, val, val);
  410. else
  411. fsi_master_mask_set(master, fsi->mst_ctrl, val, 0);
  412. }
  413. /*
  414. * ctrl function
  415. */
  416. static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
  417. {
  418. u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
  419. struct fsi_master *master = fsi_get_master(fsi);
  420. if (enable)
  421. fsi_master_mask_set(master, CLK_RST, val, val);
  422. else
  423. fsi_master_mask_set(master, CLK_RST, val, 0);
  424. }
  425. static void fsi_fifo_init(struct fsi_priv *fsi,
  426. int is_play,
  427. struct snd_soc_dai *dai)
  428. {
  429. struct fsi_master *master = fsi_get_master(fsi);
  430. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  431. u32 ctrl, shift, i;
  432. /* get on-chip RAM capacity */
  433. shift = fsi_master_read(master, FIFO_SZ);
  434. shift >>= fsi_get_port_shift(fsi, is_play);
  435. shift &= FIFO_SZ_MASK;
  436. io->fifo_max_num = 256 << shift;
  437. dev_dbg(dai->dev, "fifo = %d words\n", io->fifo_max_num);
  438. /*
  439. * The maximum number of sample data varies depending
  440. * on the number of channels selected for the format.
  441. *
  442. * FIFOs are used in 4-channel units in 3-channel mode
  443. * and in 8-channel units in 5- to 7-channel mode
  444. * meaning that more FIFOs than the required size of DPRAM
  445. * are used.
  446. *
  447. * ex) if 256 words of DP-RAM is connected
  448. * 1 channel: 256 (256 x 1 = 256)
  449. * 2 channels: 128 (128 x 2 = 256)
  450. * 3 channels: 64 ( 64 x 3 = 192)
  451. * 4 channels: 64 ( 64 x 4 = 256)
  452. * 5 channels: 32 ( 32 x 5 = 160)
  453. * 6 channels: 32 ( 32 x 6 = 192)
  454. * 7 channels: 32 ( 32 x 7 = 224)
  455. * 8 channels: 32 ( 32 x 8 = 256)
  456. */
  457. for (i = 1; i < io->chan_num; i <<= 1)
  458. io->fifo_max_num >>= 1;
  459. dev_dbg(dai->dev, "%d channel %d store\n",
  460. io->chan_num, io->fifo_max_num);
  461. ctrl = is_play ? DOFF_CTL : DIFF_CTL;
  462. /* set interrupt generation factor */
  463. fsi_reg_write(fsi, ctrl, IRQ_HALF);
  464. /* clear FIFO */
  465. fsi_reg_mask_set(fsi, ctrl, FIFO_CLR, FIFO_CLR);
  466. }
  467. static void fsi_soft_all_reset(struct fsi_master *master)
  468. {
  469. /* port AB reset */
  470. fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
  471. mdelay(10);
  472. /* soft reset */
  473. fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
  474. fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
  475. mdelay(10);
  476. }
  477. static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int startup, int stream)
  478. {
  479. struct snd_pcm_runtime *runtime;
  480. struct snd_pcm_substream *substream = NULL;
  481. int is_play = fsi_stream_is_play(stream);
  482. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  483. u32 status_reg = is_play ? DOFF_ST : DIFF_ST;
  484. int data_residue_num;
  485. int data_num;
  486. int data_num_max;
  487. int ch_width;
  488. int over_period;
  489. void (*fn)(struct fsi_priv *fsi, int size);
  490. if (!fsi ||
  491. !io->substream ||
  492. !io->substream->runtime)
  493. return -EINVAL;
  494. over_period = 0;
  495. substream = io->substream;
  496. runtime = substream->runtime;
  497. /* FSI FIFO has limit.
  498. * So, this driver can not send periods data at a time
  499. */
  500. if (io->buff_offset >=
  501. fsi_num2offset(io->period_num + 1, io->period_len)) {
  502. over_period = 1;
  503. io->period_num = (io->period_num + 1) % runtime->periods;
  504. if (0 == io->period_num)
  505. io->buff_offset = 0;
  506. }
  507. /* get 1 channel data width */
  508. ch_width = fsi_get_frame_width(fsi, is_play);
  509. /* get residue data number of alsa */
  510. data_residue_num = fsi_len2num(io->buff_len - io->buff_offset,
  511. ch_width);
  512. if (is_play) {
  513. /*
  514. * for play-back
  515. *
  516. * data_num_max : number of FSI fifo free space
  517. * data_num : number of ALSA residue data
  518. */
  519. data_num_max = io->fifo_max_num * io->chan_num;
  520. data_num_max -= fsi_get_fifo_data_num(fsi, is_play);
  521. data_num = data_residue_num;
  522. switch (ch_width) {
  523. case 2:
  524. fn = fsi_dma_soft_push16;
  525. break;
  526. case 4:
  527. fn = fsi_dma_soft_push32;
  528. break;
  529. default:
  530. return -EINVAL;
  531. }
  532. } else {
  533. /*
  534. * for capture
  535. *
  536. * data_num_max : number of ALSA free space
  537. * data_num : number of data in FSI fifo
  538. */
  539. data_num_max = data_residue_num;
  540. data_num = fsi_get_fifo_data_num(fsi, is_play);
  541. switch (ch_width) {
  542. case 2:
  543. fn = fsi_dma_soft_pop16;
  544. break;
  545. case 4:
  546. fn = fsi_dma_soft_pop32;
  547. break;
  548. default:
  549. return -EINVAL;
  550. }
  551. }
  552. data_num = min(data_num, data_num_max);
  553. fn(fsi, data_num);
  554. /* update buff_offset */
  555. io->buff_offset += fsi_num2offset(data_num, ch_width);
  556. /* check fifo status */
  557. if (!startup) {
  558. struct snd_soc_dai *dai = fsi_get_dai(substream);
  559. u32 status = fsi_reg_read(fsi, status_reg);
  560. if (status & ERR_OVER)
  561. dev_err(dai->dev, "over run\n");
  562. if (status & ERR_UNDER)
  563. dev_err(dai->dev, "under run\n");
  564. }
  565. fsi_reg_write(fsi, status_reg, 0);
  566. /* re-enable irq */
  567. fsi_irq_enable(fsi, is_play);
  568. if (over_period)
  569. snd_pcm_period_elapsed(substream);
  570. return 0;
  571. }
  572. static int fsi_data_pop(struct fsi_priv *fsi, int startup)
  573. {
  574. return fsi_fifo_data_ctrl(fsi, startup, SNDRV_PCM_STREAM_CAPTURE);
  575. }
  576. static int fsi_data_push(struct fsi_priv *fsi, int startup)
  577. {
  578. return fsi_fifo_data_ctrl(fsi, startup, SNDRV_PCM_STREAM_PLAYBACK);
  579. }
  580. static irqreturn_t fsi_interrupt(int irq, void *data)
  581. {
  582. struct fsi_master *master = data;
  583. u32 int_st = fsi_irq_get_status(master);
  584. /* clear irq status */
  585. fsi_master_mask_set(master, SOFT_RST, IR, 0);
  586. fsi_master_mask_set(master, SOFT_RST, IR, IR);
  587. if (int_st & AB_IO(1, AO_SHIFT))
  588. fsi_data_push(&master->fsia, 0);
  589. if (int_st & AB_IO(1, BO_SHIFT))
  590. fsi_data_push(&master->fsib, 0);
  591. if (int_st & AB_IO(1, AI_SHIFT))
  592. fsi_data_pop(&master->fsia, 0);
  593. if (int_st & AB_IO(1, BI_SHIFT))
  594. fsi_data_pop(&master->fsib, 0);
  595. fsi_irq_clear_status(&master->fsia);
  596. fsi_irq_clear_status(&master->fsib);
  597. return IRQ_HANDLED;
  598. }
  599. /*
  600. * dai ops
  601. */
  602. static int fsi_dai_startup(struct snd_pcm_substream *substream,
  603. struct snd_soc_dai *dai)
  604. {
  605. struct fsi_priv *fsi = fsi_get_priv(substream);
  606. struct fsi_master *master = fsi_get_master(fsi);
  607. struct fsi_stream *io;
  608. u32 flags = fsi_get_info_flags(fsi);
  609. u32 fmt;
  610. u32 reg;
  611. u32 data;
  612. int is_play = fsi_is_play(substream);
  613. int is_master;
  614. io = fsi_get_stream(fsi, is_play);
  615. pm_runtime_get_sync(dai->dev);
  616. /* CKG1 */
  617. data = is_play ? (1 << 0) : (1 << 4);
  618. is_master = fsi_is_master_mode(fsi, is_play);
  619. if (is_master)
  620. fsi_reg_mask_set(fsi, CKG1, data, data);
  621. else
  622. fsi_reg_mask_set(fsi, CKG1, data, 0);
  623. /* clock inversion (CKG2) */
  624. data = 0;
  625. if (SH_FSI_LRM_INV & flags)
  626. data |= 1 << 12;
  627. if (SH_FSI_BRM_INV & flags)
  628. data |= 1 << 8;
  629. if (SH_FSI_LRS_INV & flags)
  630. data |= 1 << 4;
  631. if (SH_FSI_BRS_INV & flags)
  632. data |= 1 << 0;
  633. fsi_reg_write(fsi, CKG2, data);
  634. /* do fmt, di fmt */
  635. data = 0;
  636. reg = is_play ? DO_FMT : DI_FMT;
  637. fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
  638. switch (fmt) {
  639. case SH_FSI_FMT_MONO:
  640. data = CR_MONO;
  641. io->chan_num = 1;
  642. break;
  643. case SH_FSI_FMT_MONO_DELAY:
  644. data = CR_MONO_D;
  645. io->chan_num = 1;
  646. break;
  647. case SH_FSI_FMT_PCM:
  648. data = CR_PCM;
  649. io->chan_num = 2;
  650. break;
  651. case SH_FSI_FMT_I2S:
  652. data = CR_I2S;
  653. io->chan_num = 2;
  654. break;
  655. case SH_FSI_FMT_TDM:
  656. io->chan_num = is_play ?
  657. SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
  658. data = CR_TDM | (io->chan_num - 1);
  659. break;
  660. case SH_FSI_FMT_TDM_DELAY:
  661. io->chan_num = is_play ?
  662. SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
  663. data = CR_TDM_D | (io->chan_num - 1);
  664. break;
  665. case SH_FSI_FMT_SPDIF:
  666. if (master->core->ver < 2) {
  667. dev_err(dai->dev, "This FSI can not use SPDIF\n");
  668. return -EINVAL;
  669. }
  670. data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
  671. io->chan_num = 2;
  672. fsi_spdif_clk_ctrl(fsi, 1);
  673. fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
  674. break;
  675. default:
  676. dev_err(dai->dev, "unknown format.\n");
  677. return -EINVAL;
  678. }
  679. fsi_reg_write(fsi, reg, data);
  680. /* irq clear */
  681. fsi_irq_disable(fsi, is_play);
  682. fsi_irq_clear_status(fsi);
  683. /* fifo init */
  684. fsi_fifo_init(fsi, is_play, dai);
  685. return 0;
  686. }
  687. static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
  688. struct snd_soc_dai *dai)
  689. {
  690. struct fsi_priv *fsi = fsi_get_priv(substream);
  691. int is_play = fsi_is_play(substream);
  692. fsi_irq_disable(fsi, is_play);
  693. fsi_clk_ctrl(fsi, 0);
  694. pm_runtime_put_sync(dai->dev);
  695. }
  696. static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  697. struct snd_soc_dai *dai)
  698. {
  699. struct fsi_priv *fsi = fsi_get_priv(substream);
  700. struct snd_pcm_runtime *runtime = substream->runtime;
  701. int is_play = fsi_is_play(substream);
  702. int ret = 0;
  703. switch (cmd) {
  704. case SNDRV_PCM_TRIGGER_START:
  705. fsi_stream_push(fsi, is_play, substream,
  706. frames_to_bytes(runtime, runtime->buffer_size),
  707. frames_to_bytes(runtime, runtime->period_size));
  708. ret = is_play ? fsi_data_push(fsi, 1) : fsi_data_pop(fsi, 1);
  709. break;
  710. case SNDRV_PCM_TRIGGER_STOP:
  711. fsi_irq_disable(fsi, is_play);
  712. fsi_stream_pop(fsi, is_play);
  713. break;
  714. }
  715. return ret;
  716. }
  717. static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
  718. struct snd_pcm_hw_params *params,
  719. struct snd_soc_dai *dai)
  720. {
  721. struct fsi_priv *fsi = fsi_get_priv(substream);
  722. struct fsi_master *master = fsi_get_master(fsi);
  723. int (*set_rate)(int is_porta, int rate) = master->info->set_rate;
  724. int fsi_ver = master->core->ver;
  725. int is_play = fsi_is_play(substream);
  726. int ret;
  727. /* if slave mode, set_rate is not needed */
  728. if (!fsi_is_master_mode(fsi, is_play))
  729. return 0;
  730. /* it is error if no set_rate */
  731. if (!set_rate)
  732. return -EIO;
  733. ret = set_rate(fsi_is_port_a(fsi), params_rate(params));
  734. if (ret > 0) {
  735. u32 data = 0;
  736. switch (ret & SH_FSI_ACKMD_MASK) {
  737. default:
  738. /* FALL THROUGH */
  739. case SH_FSI_ACKMD_512:
  740. data |= (0x0 << 12);
  741. break;
  742. case SH_FSI_ACKMD_256:
  743. data |= (0x1 << 12);
  744. break;
  745. case SH_FSI_ACKMD_128:
  746. data |= (0x2 << 12);
  747. break;
  748. case SH_FSI_ACKMD_64:
  749. data |= (0x3 << 12);
  750. break;
  751. case SH_FSI_ACKMD_32:
  752. if (fsi_ver < 2)
  753. dev_err(dai->dev, "unsupported ACKMD\n");
  754. else
  755. data |= (0x4 << 12);
  756. break;
  757. }
  758. switch (ret & SH_FSI_BPFMD_MASK) {
  759. default:
  760. /* FALL THROUGH */
  761. case SH_FSI_BPFMD_32:
  762. data |= (0x0 << 8);
  763. break;
  764. case SH_FSI_BPFMD_64:
  765. data |= (0x1 << 8);
  766. break;
  767. case SH_FSI_BPFMD_128:
  768. data |= (0x2 << 8);
  769. break;
  770. case SH_FSI_BPFMD_256:
  771. data |= (0x3 << 8);
  772. break;
  773. case SH_FSI_BPFMD_512:
  774. data |= (0x4 << 8);
  775. break;
  776. case SH_FSI_BPFMD_16:
  777. if (fsi_ver < 2)
  778. dev_err(dai->dev, "unsupported ACKMD\n");
  779. else
  780. data |= (0x7 << 8);
  781. break;
  782. }
  783. fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
  784. udelay(10);
  785. fsi_clk_ctrl(fsi, 1);
  786. ret = 0;
  787. }
  788. return ret;
  789. }
  790. static struct snd_soc_dai_ops fsi_dai_ops = {
  791. .startup = fsi_dai_startup,
  792. .shutdown = fsi_dai_shutdown,
  793. .trigger = fsi_dai_trigger,
  794. .hw_params = fsi_dai_hw_params,
  795. };
  796. /*
  797. * pcm ops
  798. */
  799. static struct snd_pcm_hardware fsi_pcm_hardware = {
  800. .info = SNDRV_PCM_INFO_INTERLEAVED |
  801. SNDRV_PCM_INFO_MMAP |
  802. SNDRV_PCM_INFO_MMAP_VALID |
  803. SNDRV_PCM_INFO_PAUSE,
  804. .formats = FSI_FMTS,
  805. .rates = FSI_RATES,
  806. .rate_min = 8000,
  807. .rate_max = 192000,
  808. .channels_min = 1,
  809. .channels_max = 2,
  810. .buffer_bytes_max = 64 * 1024,
  811. .period_bytes_min = 32,
  812. .period_bytes_max = 8192,
  813. .periods_min = 1,
  814. .periods_max = 32,
  815. .fifo_size = 256,
  816. };
  817. static int fsi_pcm_open(struct snd_pcm_substream *substream)
  818. {
  819. struct snd_pcm_runtime *runtime = substream->runtime;
  820. int ret = 0;
  821. snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
  822. ret = snd_pcm_hw_constraint_integer(runtime,
  823. SNDRV_PCM_HW_PARAM_PERIODS);
  824. return ret;
  825. }
  826. static int fsi_hw_params(struct snd_pcm_substream *substream,
  827. struct snd_pcm_hw_params *hw_params)
  828. {
  829. return snd_pcm_lib_malloc_pages(substream,
  830. params_buffer_bytes(hw_params));
  831. }
  832. static int fsi_hw_free(struct snd_pcm_substream *substream)
  833. {
  834. return snd_pcm_lib_free_pages(substream);
  835. }
  836. static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
  837. {
  838. struct snd_pcm_runtime *runtime = substream->runtime;
  839. struct fsi_priv *fsi = fsi_get_priv(substream);
  840. struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream));
  841. long location;
  842. location = (io->buff_offset - 1);
  843. if (location < 0)
  844. location = 0;
  845. return bytes_to_frames(runtime, location);
  846. }
  847. static struct snd_pcm_ops fsi_pcm_ops = {
  848. .open = fsi_pcm_open,
  849. .ioctl = snd_pcm_lib_ioctl,
  850. .hw_params = fsi_hw_params,
  851. .hw_free = fsi_hw_free,
  852. .pointer = fsi_pointer,
  853. };
  854. /*
  855. * snd_soc_platform
  856. */
  857. #define PREALLOC_BUFFER (32 * 1024)
  858. #define PREALLOC_BUFFER_MAX (32 * 1024)
  859. static void fsi_pcm_free(struct snd_pcm *pcm)
  860. {
  861. snd_pcm_lib_preallocate_free_for_all(pcm);
  862. }
  863. static int fsi_pcm_new(struct snd_card *card,
  864. struct snd_soc_dai *dai,
  865. struct snd_pcm *pcm)
  866. {
  867. /*
  868. * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
  869. * in MMAP mode (i.e. aplay -M)
  870. */
  871. return snd_pcm_lib_preallocate_pages_for_all(
  872. pcm,
  873. SNDRV_DMA_TYPE_CONTINUOUS,
  874. snd_dma_continuous_data(GFP_KERNEL),
  875. PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
  876. }
  877. /*
  878. * alsa struct
  879. */
  880. static struct snd_soc_dai_driver fsi_soc_dai[] = {
  881. {
  882. .name = "fsia-dai",
  883. .playback = {
  884. .rates = FSI_RATES,
  885. .formats = FSI_FMTS,
  886. .channels_min = 1,
  887. .channels_max = 8,
  888. },
  889. .capture = {
  890. .rates = FSI_RATES,
  891. .formats = FSI_FMTS,
  892. .channels_min = 1,
  893. .channels_max = 8,
  894. },
  895. .ops = &fsi_dai_ops,
  896. },
  897. {
  898. .name = "fsib-dai",
  899. .playback = {
  900. .rates = FSI_RATES,
  901. .formats = FSI_FMTS,
  902. .channels_min = 1,
  903. .channels_max = 8,
  904. },
  905. .capture = {
  906. .rates = FSI_RATES,
  907. .formats = FSI_FMTS,
  908. .channels_min = 1,
  909. .channels_max = 8,
  910. },
  911. .ops = &fsi_dai_ops,
  912. },
  913. };
  914. static struct snd_soc_platform_driver fsi_soc_platform = {
  915. .ops = &fsi_pcm_ops,
  916. .pcm_new = fsi_pcm_new,
  917. .pcm_free = fsi_pcm_free,
  918. };
  919. /*
  920. * platform function
  921. */
  922. static int fsi_probe(struct platform_device *pdev)
  923. {
  924. struct fsi_master *master;
  925. const struct platform_device_id *id_entry;
  926. struct resource *res;
  927. unsigned int irq;
  928. int ret;
  929. id_entry = pdev->id_entry;
  930. if (!id_entry) {
  931. dev_err(&pdev->dev, "unknown fsi device\n");
  932. return -ENODEV;
  933. }
  934. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  935. irq = platform_get_irq(pdev, 0);
  936. if (!res || (int)irq <= 0) {
  937. dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
  938. ret = -ENODEV;
  939. goto exit;
  940. }
  941. master = kzalloc(sizeof(*master), GFP_KERNEL);
  942. if (!master) {
  943. dev_err(&pdev->dev, "Could not allocate master\n");
  944. ret = -ENOMEM;
  945. goto exit;
  946. }
  947. master->base = ioremap_nocache(res->start, resource_size(res));
  948. if (!master->base) {
  949. ret = -ENXIO;
  950. dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
  951. goto exit_kfree;
  952. }
  953. /* master setting */
  954. master->irq = irq;
  955. master->info = pdev->dev.platform_data;
  956. master->core = (struct fsi_core *)id_entry->driver_data;
  957. spin_lock_init(&master->lock);
  958. /* FSI A setting */
  959. master->fsia.base = master->base;
  960. master->fsia.master = master;
  961. master->fsia.mst_ctrl = A_MST_CTLR;
  962. /* FSI B setting */
  963. master->fsib.base = master->base + 0x40;
  964. master->fsib.master = master;
  965. master->fsib.mst_ctrl = B_MST_CTLR;
  966. pm_runtime_enable(&pdev->dev);
  967. pm_runtime_resume(&pdev->dev);
  968. dev_set_drvdata(&pdev->dev, master);
  969. fsi_soft_all_reset(master);
  970. ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED,
  971. id_entry->name, master);
  972. if (ret) {
  973. dev_err(&pdev->dev, "irq request err\n");
  974. goto exit_iounmap;
  975. }
  976. ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
  977. if (ret < 0) {
  978. dev_err(&pdev->dev, "cannot snd soc register\n");
  979. goto exit_free_irq;
  980. }
  981. return snd_soc_register_dais(&pdev->dev, fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
  982. exit_free_irq:
  983. free_irq(irq, master);
  984. exit_iounmap:
  985. iounmap(master->base);
  986. pm_runtime_disable(&pdev->dev);
  987. exit_kfree:
  988. kfree(master);
  989. master = NULL;
  990. exit:
  991. return ret;
  992. }
  993. static int fsi_remove(struct platform_device *pdev)
  994. {
  995. struct fsi_master *master;
  996. master = dev_get_drvdata(&pdev->dev);
  997. snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
  998. snd_soc_unregister_platform(&pdev->dev);
  999. pm_runtime_disable(&pdev->dev);
  1000. free_irq(master->irq, master);
  1001. iounmap(master->base);
  1002. kfree(master);
  1003. return 0;
  1004. }
  1005. static int fsi_runtime_nop(struct device *dev)
  1006. {
  1007. /* Runtime PM callback shared between ->runtime_suspend()
  1008. * and ->runtime_resume(). Simply returns success.
  1009. *
  1010. * This driver re-initializes all registers after
  1011. * pm_runtime_get_sync() anyway so there is no need
  1012. * to save and restore registers here.
  1013. */
  1014. return 0;
  1015. }
  1016. static struct dev_pm_ops fsi_pm_ops = {
  1017. .runtime_suspend = fsi_runtime_nop,
  1018. .runtime_resume = fsi_runtime_nop,
  1019. };
  1020. static struct fsi_core fsi1_core = {
  1021. .ver = 1,
  1022. /* Interrupt */
  1023. .int_st = INT_ST,
  1024. .iemsk = IEMSK,
  1025. .imsk = IMSK,
  1026. };
  1027. static struct fsi_core fsi2_core = {
  1028. .ver = 2,
  1029. /* Interrupt */
  1030. .int_st = CPU_INT_ST,
  1031. .iemsk = CPU_IEMSK,
  1032. .imsk = CPU_IMSK,
  1033. };
  1034. static struct platform_device_id fsi_id_table[] = {
  1035. { "sh_fsi", (kernel_ulong_t)&fsi1_core },
  1036. { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
  1037. {},
  1038. };
  1039. MODULE_DEVICE_TABLE(platform, fsi_id_table);
  1040. static struct platform_driver fsi_driver = {
  1041. .driver = {
  1042. .name = "fsi-pcm-audio",
  1043. .pm = &fsi_pm_ops,
  1044. },
  1045. .probe = fsi_probe,
  1046. .remove = fsi_remove,
  1047. .id_table = fsi_id_table,
  1048. };
  1049. static int __init fsi_mobile_init(void)
  1050. {
  1051. return platform_driver_register(&fsi_driver);
  1052. }
  1053. static void __exit fsi_mobile_exit(void)
  1054. {
  1055. platform_driver_unregister(&fsi_driver);
  1056. }
  1057. module_init(fsi_mobile_init);
  1058. module_exit(fsi_mobile_exit);
  1059. MODULE_LICENSE("GPL");
  1060. MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
  1061. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");