fsi.c 28 KB

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