fsi.c 28 KB

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