fsi.c 28 KB

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