fsi.c 27 KB

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