fsi.c 28 KB

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