fsi.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  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_MONO (0x0 << 4)
  50. #define CR_MONO_D (0x1 << 4)
  51. #define CR_PCM (0x2 << 4)
  52. #define CR_I2S (0x3 << 4)
  53. #define CR_TDM (0x4 << 4)
  54. #define CR_TDM_D (0x5 << 4)
  55. #define CR_SPDIF 0x00100120
  56. /* DOFF_CTL */
  57. /* DIFF_CTL */
  58. #define IRQ_HALF 0x00100000
  59. #define FIFO_CLR 0x00000001
  60. /* DOFF_ST */
  61. #define ERR_OVER 0x00000010
  62. #define ERR_UNDER 0x00000001
  63. #define ST_ERR (ERR_OVER | ERR_UNDER)
  64. /* CKG1 */
  65. #define ACKMD_MASK 0x00007000
  66. #define BPFMD_MASK 0x00000700
  67. /* A/B MST_CTLR */
  68. #define BP (1 << 4) /* Fix the signal of Biphase output */
  69. #define SE (1 << 0) /* Fix the master clock */
  70. /* CLK_RST */
  71. #define B_CLK 0x00000010
  72. #define A_CLK 0x00000001
  73. /* IO SHIFT / MACRO */
  74. #define BI_SHIFT 12
  75. #define BO_SHIFT 8
  76. #define AI_SHIFT 4
  77. #define AO_SHIFT 0
  78. #define AB_IO(param, shift) (param << shift)
  79. /* SOFT_RST */
  80. #define PBSR (1 << 12) /* Port B Software Reset */
  81. #define PASR (1 << 8) /* Port A Software Reset */
  82. #define IR (1 << 4) /* Interrupt Reset */
  83. #define FSISR (1 << 0) /* Software Reset */
  84. /* FIFO_SZ */
  85. #define FIFO_SZ_MASK 0x7
  86. #define FSI_RATES SNDRV_PCM_RATE_8000_96000
  87. #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  88. /*
  89. * FSI driver use below type name for variable
  90. *
  91. * xxx_len : data length
  92. * xxx_width : data width
  93. * xxx_offset : data offset
  94. * xxx_num : number of data
  95. */
  96. /*
  97. * struct
  98. */
  99. struct fsi_stream {
  100. struct snd_pcm_substream *substream;
  101. int fifo_max_num;
  102. int chan_num;
  103. int buff_offset;
  104. int buff_len;
  105. int period_len;
  106. int period_num;
  107. };
  108. struct fsi_priv {
  109. void __iomem *base;
  110. struct fsi_master *master;
  111. struct fsi_stream playback;
  112. struct fsi_stream capture;
  113. long rate;
  114. u32 mst_ctrl;
  115. };
  116. struct fsi_core {
  117. int ver;
  118. u32 int_st;
  119. u32 iemsk;
  120. u32 imsk;
  121. };
  122. struct fsi_master {
  123. void __iomem *base;
  124. int irq;
  125. struct fsi_priv fsia;
  126. struct fsi_priv fsib;
  127. struct fsi_core *core;
  128. struct sh_fsi_platform_info *info;
  129. spinlock_t lock;
  130. };
  131. /*
  132. * basic read write function
  133. */
  134. static void __fsi_reg_write(u32 reg, u32 data)
  135. {
  136. /* valid data area is 24bit */
  137. data &= 0x00ffffff;
  138. __raw_writel(data, reg);
  139. }
  140. static u32 __fsi_reg_read(u32 reg)
  141. {
  142. return __raw_readl(reg);
  143. }
  144. static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
  145. {
  146. u32 val = __fsi_reg_read(reg);
  147. val &= ~mask;
  148. val |= data & mask;
  149. __fsi_reg_write(reg, val);
  150. }
  151. static void fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data)
  152. {
  153. if (reg > REG_END) {
  154. pr_err("fsi: register access err (%s)\n", __func__);
  155. return;
  156. }
  157. __fsi_reg_write((u32)(fsi->base + reg), data);
  158. }
  159. static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg)
  160. {
  161. if (reg > REG_END) {
  162. pr_err("fsi: register access err (%s)\n", __func__);
  163. return 0;
  164. }
  165. return __fsi_reg_read((u32)(fsi->base + reg));
  166. }
  167. static void fsi_reg_mask_set(struct fsi_priv *fsi, u32 reg, u32 mask, u32 data)
  168. {
  169. if (reg > REG_END) {
  170. pr_err("fsi: register access err (%s)\n", __func__);
  171. return;
  172. }
  173. __fsi_reg_mask_set((u32)(fsi->base + reg), mask, data);
  174. }
  175. static void fsi_master_write(struct fsi_master *master, u32 reg, u32 data)
  176. {
  177. unsigned long flags;
  178. if ((reg < MREG_START) ||
  179. (reg > MREG_END)) {
  180. pr_err("fsi: register access err (%s)\n", __func__);
  181. return;
  182. }
  183. spin_lock_irqsave(&master->lock, flags);
  184. __fsi_reg_write((u32)(master->base + reg), data);
  185. spin_unlock_irqrestore(&master->lock, flags);
  186. }
  187. static u32 fsi_master_read(struct fsi_master *master, u32 reg)
  188. {
  189. u32 ret;
  190. unsigned long flags;
  191. if ((reg < MREG_START) ||
  192. (reg > MREG_END)) {
  193. pr_err("fsi: register access err (%s)\n", __func__);
  194. return 0;
  195. }
  196. spin_lock_irqsave(&master->lock, flags);
  197. ret = __fsi_reg_read((u32)(master->base + reg));
  198. spin_unlock_irqrestore(&master->lock, flags);
  199. return ret;
  200. }
  201. static void fsi_master_mask_set(struct fsi_master *master,
  202. u32 reg, u32 mask, u32 data)
  203. {
  204. unsigned long flags;
  205. if ((reg < MREG_START) ||
  206. (reg > MREG_END)) {
  207. pr_err("fsi: register access err (%s)\n", __func__);
  208. return;
  209. }
  210. spin_lock_irqsave(&master->lock, flags);
  211. __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
  212. spin_unlock_irqrestore(&master->lock, flags);
  213. }
  214. /*
  215. * basic function
  216. */
  217. static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
  218. {
  219. return fsi->master;
  220. }
  221. static int fsi_is_port_a(struct fsi_priv *fsi)
  222. {
  223. return fsi->master->base == fsi->base;
  224. }
  225. static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
  226. {
  227. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  228. return rtd->cpu_dai;
  229. }
  230. static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
  231. {
  232. struct snd_soc_dai *dai = fsi_get_dai(substream);
  233. struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
  234. if (dai->id == 0)
  235. return &master->fsia;
  236. else
  237. return &master->fsib;
  238. }
  239. static u32 fsi_get_info_flags(struct fsi_priv *fsi)
  240. {
  241. int is_porta = fsi_is_port_a(fsi);
  242. struct fsi_master *master = fsi_get_master(fsi);
  243. return is_porta ? master->info->porta_flags :
  244. master->info->portb_flags;
  245. }
  246. static inline int fsi_stream_is_play(int stream)
  247. {
  248. return stream == SNDRV_PCM_STREAM_PLAYBACK;
  249. }
  250. static inline int fsi_is_play(struct snd_pcm_substream *substream)
  251. {
  252. return fsi_stream_is_play(substream->stream);
  253. }
  254. static inline struct fsi_stream *fsi_get_stream(struct fsi_priv *fsi,
  255. int is_play)
  256. {
  257. return is_play ? &fsi->playback : &fsi->capture;
  258. }
  259. static int fsi_is_master_mode(struct fsi_priv *fsi, int is_play)
  260. {
  261. u32 mode;
  262. u32 flags = fsi_get_info_flags(fsi);
  263. mode = is_play ? SH_FSI_OUT_SLAVE_MODE : SH_FSI_IN_SLAVE_MODE;
  264. /* return
  265. * 1 : master mode
  266. * 0 : slave mode
  267. */
  268. return (mode & flags) != mode;
  269. }
  270. static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
  271. {
  272. int is_porta = fsi_is_port_a(fsi);
  273. u32 shift;
  274. if (is_porta)
  275. shift = is_play ? AO_SHIFT : AI_SHIFT;
  276. else
  277. shift = is_play ? BO_SHIFT : BI_SHIFT;
  278. return shift;
  279. }
  280. static void fsi_stream_push(struct fsi_priv *fsi,
  281. int is_play,
  282. struct snd_pcm_substream *substream,
  283. u32 buffer_len,
  284. u32 period_len)
  285. {
  286. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  287. io->substream = substream;
  288. io->buff_len = buffer_len;
  289. io->buff_offset = 0;
  290. io->period_len = period_len;
  291. io->period_num = 0;
  292. }
  293. static void fsi_stream_pop(struct fsi_priv *fsi, int is_play)
  294. {
  295. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  296. io->substream = NULL;
  297. io->buff_len = 0;
  298. io->buff_offset = 0;
  299. io->period_len = 0;
  300. io->period_num = 0;
  301. }
  302. static int fsi_get_fifo_data_num(struct fsi_priv *fsi, int is_play)
  303. {
  304. u32 status;
  305. u32 reg = is_play ? DOFF_ST : DIFF_ST;
  306. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  307. int data_num;
  308. status = fsi_reg_read(fsi, reg);
  309. data_num = 0x1ff & (status >> 8);
  310. data_num *= io->chan_num;
  311. return data_num;
  312. }
  313. static int fsi_len2num(int len, int width)
  314. {
  315. return len / width;
  316. }
  317. #define fsi_num2offset(a, b) fsi_num2len(a, b)
  318. static int fsi_num2len(int num, int width)
  319. {
  320. return num * width;
  321. }
  322. static int fsi_get_frame_width(struct fsi_priv *fsi, int is_play)
  323. {
  324. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  325. struct snd_pcm_substream *substream = io->substream;
  326. struct snd_pcm_runtime *runtime = substream->runtime;
  327. return frames_to_bytes(runtime, 1) / io->chan_num;
  328. }
  329. /*
  330. * dma function
  331. */
  332. static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream)
  333. {
  334. int is_play = fsi_stream_is_play(stream);
  335. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  336. return io->substream->runtime->dma_area + io->buff_offset;
  337. }
  338. static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
  339. {
  340. u16 *start;
  341. int i;
  342. start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
  343. for (i = 0; i < num; i++)
  344. fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
  345. }
  346. static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
  347. {
  348. u16 *start;
  349. int i;
  350. start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
  351. for (i = 0; i < num; i++)
  352. *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
  353. }
  354. static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num)
  355. {
  356. u32 *start;
  357. int i;
  358. start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
  359. for (i = 0; i < num; i++)
  360. fsi_reg_write(fsi, DODT, *(start + i));
  361. }
  362. static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
  363. {
  364. u32 *start;
  365. int i;
  366. start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
  367. for (i = 0; i < num; i++)
  368. *(start + i) = fsi_reg_read(fsi, DIDT);
  369. }
  370. /*
  371. * irq function
  372. */
  373. static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
  374. {
  375. u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
  376. struct fsi_master *master = fsi_get_master(fsi);
  377. fsi_master_mask_set(master, master->core->imsk, data, data);
  378. fsi_master_mask_set(master, master->core->iemsk, data, data);
  379. }
  380. static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
  381. {
  382. u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
  383. struct fsi_master *master = fsi_get_master(fsi);
  384. fsi_master_mask_set(master, master->core->imsk, data, 0);
  385. fsi_master_mask_set(master, master->core->iemsk, data, 0);
  386. }
  387. static u32 fsi_irq_get_status(struct fsi_master *master)
  388. {
  389. return fsi_master_read(master, master->core->int_st);
  390. }
  391. static void fsi_irq_clear_all_status(struct fsi_master *master)
  392. {
  393. fsi_master_write(master, master->core->int_st, 0);
  394. }
  395. static void fsi_irq_clear_status(struct fsi_priv *fsi)
  396. {
  397. u32 data = 0;
  398. struct fsi_master *master = fsi_get_master(fsi);
  399. data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
  400. data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
  401. /* clear interrupt factor */
  402. fsi_master_mask_set(master, master->core->int_st, data, 0);
  403. }
  404. /*
  405. * SPDIF master clock function
  406. *
  407. * These functions are used later FSI2
  408. */
  409. static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
  410. {
  411. struct fsi_master *master = fsi_get_master(fsi);
  412. u32 val = BP | SE;
  413. if (master->core->ver < 2) {
  414. pr_err("fsi: register access err (%s)\n", __func__);
  415. return;
  416. }
  417. if (enable)
  418. fsi_master_mask_set(master, fsi->mst_ctrl, val, val);
  419. else
  420. fsi_master_mask_set(master, fsi->mst_ctrl, val, 0);
  421. }
  422. /*
  423. * ctrl function
  424. */
  425. static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
  426. {
  427. u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
  428. struct fsi_master *master = fsi_get_master(fsi);
  429. if (enable)
  430. fsi_master_mask_set(master, CLK_RST, val, val);
  431. else
  432. fsi_master_mask_set(master, CLK_RST, val, 0);
  433. }
  434. static void fsi_fifo_init(struct fsi_priv *fsi,
  435. int is_play,
  436. struct snd_soc_dai *dai)
  437. {
  438. struct fsi_master *master = fsi_get_master(fsi);
  439. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  440. u32 ctrl, shift, i;
  441. /* get on-chip RAM capacity */
  442. shift = fsi_master_read(master, FIFO_SZ);
  443. shift >>= fsi_get_port_shift(fsi, is_play);
  444. shift &= FIFO_SZ_MASK;
  445. io->fifo_max_num = 256 << shift;
  446. dev_dbg(dai->dev, "fifo = %d words\n", io->fifo_max_num);
  447. /*
  448. * The maximum number of sample data varies depending
  449. * on the number of channels selected for the format.
  450. *
  451. * FIFOs are used in 4-channel units in 3-channel mode
  452. * and in 8-channel units in 5- to 7-channel mode
  453. * meaning that more FIFOs than the required size of DPRAM
  454. * are used.
  455. *
  456. * ex) if 256 words of DP-RAM is connected
  457. * 1 channel: 256 (256 x 1 = 256)
  458. * 2 channels: 128 (128 x 2 = 256)
  459. * 3 channels: 64 ( 64 x 3 = 192)
  460. * 4 channels: 64 ( 64 x 4 = 256)
  461. * 5 channels: 32 ( 32 x 5 = 160)
  462. * 6 channels: 32 ( 32 x 6 = 192)
  463. * 7 channels: 32 ( 32 x 7 = 224)
  464. * 8 channels: 32 ( 32 x 8 = 256)
  465. */
  466. for (i = 1; i < io->chan_num; i <<= 1)
  467. io->fifo_max_num >>= 1;
  468. dev_dbg(dai->dev, "%d channel %d store\n",
  469. io->chan_num, io->fifo_max_num);
  470. ctrl = is_play ? DOFF_CTL : DIFF_CTL;
  471. /* set interrupt generation factor */
  472. fsi_reg_write(fsi, ctrl, IRQ_HALF);
  473. /* clear FIFO */
  474. fsi_reg_mask_set(fsi, ctrl, FIFO_CLR, FIFO_CLR);
  475. }
  476. static void fsi_soft_all_reset(struct fsi_master *master)
  477. {
  478. /* port AB reset */
  479. fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
  480. mdelay(10);
  481. /* soft reset */
  482. fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
  483. fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
  484. mdelay(10);
  485. }
  486. static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int startup, int stream)
  487. {
  488. struct snd_pcm_runtime *runtime;
  489. struct snd_pcm_substream *substream = NULL;
  490. int is_play = fsi_stream_is_play(stream);
  491. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  492. u32 status_reg = is_play ? DOFF_ST : DIFF_ST;
  493. int data_residue_num;
  494. int data_num;
  495. int data_num_max;
  496. int ch_width;
  497. int over_period;
  498. void (*fn)(struct fsi_priv *fsi, int size);
  499. if (!fsi ||
  500. !io->substream ||
  501. !io->substream->runtime)
  502. return -EINVAL;
  503. over_period = 0;
  504. substream = io->substream;
  505. runtime = substream->runtime;
  506. /* FSI FIFO has limit.
  507. * So, this driver can not send periods data at a time
  508. */
  509. if (io->buff_offset >=
  510. fsi_num2offset(io->period_num + 1, io->period_len)) {
  511. over_period = 1;
  512. io->period_num = (io->period_num + 1) % runtime->periods;
  513. if (0 == io->period_num)
  514. io->buff_offset = 0;
  515. }
  516. /* get 1 channel data width */
  517. ch_width = fsi_get_frame_width(fsi, is_play);
  518. /* get residue data number of alsa */
  519. data_residue_num = fsi_len2num(io->buff_len - io->buff_offset,
  520. ch_width);
  521. if (is_play) {
  522. /*
  523. * for play-back
  524. *
  525. * data_num_max : number of FSI fifo free space
  526. * data_num : number of ALSA residue data
  527. */
  528. data_num_max = io->fifo_max_num * io->chan_num;
  529. data_num_max -= fsi_get_fifo_data_num(fsi, is_play);
  530. data_num = data_residue_num;
  531. switch (ch_width) {
  532. case 2:
  533. fn = fsi_dma_soft_push16;
  534. break;
  535. case 4:
  536. fn = fsi_dma_soft_push32;
  537. break;
  538. default:
  539. return -EINVAL;
  540. }
  541. } else {
  542. /*
  543. * for capture
  544. *
  545. * data_num_max : number of ALSA free space
  546. * data_num : number of data in FSI fifo
  547. */
  548. data_num_max = data_residue_num;
  549. data_num = fsi_get_fifo_data_num(fsi, is_play);
  550. switch (ch_width) {
  551. case 2:
  552. fn = fsi_dma_soft_pop16;
  553. break;
  554. case 4:
  555. fn = fsi_dma_soft_pop32;
  556. break;
  557. default:
  558. return -EINVAL;
  559. }
  560. }
  561. data_num = min(data_num, data_num_max);
  562. fn(fsi, data_num);
  563. /* update buff_offset */
  564. io->buff_offset += fsi_num2offset(data_num, ch_width);
  565. /* check fifo status */
  566. if (!startup) {
  567. struct snd_soc_dai *dai = fsi_get_dai(substream);
  568. u32 status = fsi_reg_read(fsi, status_reg);
  569. if (status & ERR_OVER)
  570. dev_err(dai->dev, "over run\n");
  571. if (status & ERR_UNDER)
  572. dev_err(dai->dev, "under run\n");
  573. }
  574. fsi_reg_write(fsi, status_reg, 0);
  575. /* re-enable irq */
  576. fsi_irq_enable(fsi, is_play);
  577. if (over_period)
  578. snd_pcm_period_elapsed(substream);
  579. return 0;
  580. }
  581. static int fsi_data_pop(struct fsi_priv *fsi, int startup)
  582. {
  583. return fsi_fifo_data_ctrl(fsi, startup, SNDRV_PCM_STREAM_CAPTURE);
  584. }
  585. static int fsi_data_push(struct fsi_priv *fsi, int startup)
  586. {
  587. return fsi_fifo_data_ctrl(fsi, startup, SNDRV_PCM_STREAM_PLAYBACK);
  588. }
  589. static irqreturn_t fsi_interrupt(int irq, void *data)
  590. {
  591. struct fsi_master *master = data;
  592. u32 int_st = fsi_irq_get_status(master);
  593. /* clear irq status */
  594. fsi_master_mask_set(master, SOFT_RST, IR, 0);
  595. fsi_master_mask_set(master, SOFT_RST, IR, IR);
  596. if (int_st & AB_IO(1, AO_SHIFT))
  597. fsi_data_push(&master->fsia, 0);
  598. if (int_st & AB_IO(1, BO_SHIFT))
  599. fsi_data_push(&master->fsib, 0);
  600. if (int_st & AB_IO(1, AI_SHIFT))
  601. fsi_data_pop(&master->fsia, 0);
  602. if (int_st & AB_IO(1, BI_SHIFT))
  603. fsi_data_pop(&master->fsib, 0);
  604. fsi_irq_clear_all_status(master);
  605. return IRQ_HANDLED;
  606. }
  607. /*
  608. * dai ops
  609. */
  610. static int fsi_dai_startup(struct snd_pcm_substream *substream,
  611. struct snd_soc_dai *dai)
  612. {
  613. struct fsi_priv *fsi = fsi_get_priv(substream);
  614. struct fsi_master *master = fsi_get_master(fsi);
  615. struct fsi_stream *io;
  616. u32 flags = fsi_get_info_flags(fsi);
  617. u32 fmt;
  618. u32 reg;
  619. u32 data;
  620. int is_play = fsi_is_play(substream);
  621. int is_master;
  622. io = fsi_get_stream(fsi, is_play);
  623. pm_runtime_get_sync(dai->dev);
  624. /* CKG1 */
  625. data = is_play ? (1 << 0) : (1 << 4);
  626. is_master = fsi_is_master_mode(fsi, is_play);
  627. if (is_master)
  628. fsi_reg_mask_set(fsi, CKG1, data, data);
  629. else
  630. fsi_reg_mask_set(fsi, CKG1, data, 0);
  631. /* clock inversion (CKG2) */
  632. data = 0;
  633. if (SH_FSI_LRM_INV & flags)
  634. data |= 1 << 12;
  635. if (SH_FSI_BRM_INV & flags)
  636. data |= 1 << 8;
  637. if (SH_FSI_LRS_INV & flags)
  638. data |= 1 << 4;
  639. if (SH_FSI_BRS_INV & flags)
  640. data |= 1 << 0;
  641. fsi_reg_write(fsi, CKG2, data);
  642. /* do fmt, di fmt */
  643. data = 0;
  644. reg = is_play ? DO_FMT : DI_FMT;
  645. fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
  646. switch (fmt) {
  647. case SH_FSI_FMT_MONO:
  648. data = CR_MONO;
  649. io->chan_num = 1;
  650. break;
  651. case SH_FSI_FMT_MONO_DELAY:
  652. data = CR_MONO_D;
  653. io->chan_num = 1;
  654. break;
  655. case SH_FSI_FMT_PCM:
  656. data = CR_PCM;
  657. io->chan_num = 2;
  658. break;
  659. case SH_FSI_FMT_I2S:
  660. data = CR_I2S;
  661. io->chan_num = 2;
  662. break;
  663. case SH_FSI_FMT_TDM:
  664. io->chan_num = is_play ?
  665. SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
  666. data = CR_TDM | (io->chan_num - 1);
  667. break;
  668. case SH_FSI_FMT_TDM_DELAY:
  669. io->chan_num = is_play ?
  670. SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
  671. data = CR_TDM_D | (io->chan_num - 1);
  672. break;
  673. case SH_FSI_FMT_SPDIF:
  674. if (master->core->ver < 2) {
  675. dev_err(dai->dev, "This FSI can not use SPDIF\n");
  676. return -EINVAL;
  677. }
  678. data = CR_SPDIF;
  679. io->chan_num = 2;
  680. fsi_spdif_clk_ctrl(fsi, 1);
  681. fsi_reg_mask_set(fsi, OUT_SEL, 0x0010, 0x0010);
  682. break;
  683. default:
  684. dev_err(dai->dev, "unknown format.\n");
  685. return -EINVAL;
  686. }
  687. fsi_reg_write(fsi, reg, data);
  688. /* irq clear */
  689. fsi_irq_disable(fsi, is_play);
  690. fsi_irq_clear_status(fsi);
  691. /* fifo init */
  692. fsi_fifo_init(fsi, is_play, dai);
  693. return 0;
  694. }
  695. static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
  696. struct snd_soc_dai *dai)
  697. {
  698. struct fsi_priv *fsi = fsi_get_priv(substream);
  699. int is_play = fsi_is_play(substream);
  700. struct fsi_master *master = fsi_get_master(fsi);
  701. int (*set_rate)(struct device *dev, int is_porta, int rate, int enable);
  702. fsi_irq_disable(fsi, is_play);
  703. fsi_clk_ctrl(fsi, 0);
  704. set_rate = master->info->set_rate;
  705. if (set_rate && fsi->rate)
  706. set_rate(dai->dev, fsi_is_port_a(fsi), fsi->rate, 0);
  707. fsi->rate = 0;
  708. pm_runtime_put_sync(dai->dev);
  709. }
  710. static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  711. struct snd_soc_dai *dai)
  712. {
  713. struct fsi_priv *fsi = fsi_get_priv(substream);
  714. struct snd_pcm_runtime *runtime = substream->runtime;
  715. int is_play = fsi_is_play(substream);
  716. int ret = 0;
  717. switch (cmd) {
  718. case SNDRV_PCM_TRIGGER_START:
  719. fsi_stream_push(fsi, is_play, substream,
  720. frames_to_bytes(runtime, runtime->buffer_size),
  721. frames_to_bytes(runtime, runtime->period_size));
  722. ret = is_play ? fsi_data_push(fsi, 1) : fsi_data_pop(fsi, 1);
  723. break;
  724. case SNDRV_PCM_TRIGGER_STOP:
  725. fsi_irq_disable(fsi, is_play);
  726. fsi_stream_pop(fsi, is_play);
  727. break;
  728. }
  729. return ret;
  730. }
  731. static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
  732. struct snd_pcm_hw_params *params,
  733. struct snd_soc_dai *dai)
  734. {
  735. struct fsi_priv *fsi = fsi_get_priv(substream);
  736. struct fsi_master *master = fsi_get_master(fsi);
  737. int (*set_rate)(struct device *dev, int is_porta, int rate, int enable);
  738. int fsi_ver = master->core->ver;
  739. long rate = params_rate(params);
  740. int ret;
  741. set_rate = master->info->set_rate;
  742. if (!set_rate)
  743. return 0;
  744. ret = set_rate(dai->dev, fsi_is_port_a(fsi), rate, 1);
  745. if (ret < 0) /* error */
  746. return ret;
  747. fsi->rate = rate;
  748. if (ret > 0) {
  749. u32 data = 0;
  750. switch (ret & SH_FSI_ACKMD_MASK) {
  751. default:
  752. /* FALL THROUGH */
  753. case SH_FSI_ACKMD_512:
  754. data |= (0x0 << 12);
  755. break;
  756. case SH_FSI_ACKMD_256:
  757. data |= (0x1 << 12);
  758. break;
  759. case SH_FSI_ACKMD_128:
  760. data |= (0x2 << 12);
  761. break;
  762. case SH_FSI_ACKMD_64:
  763. data |= (0x3 << 12);
  764. break;
  765. case SH_FSI_ACKMD_32:
  766. if (fsi_ver < 2)
  767. dev_err(dai->dev, "unsupported ACKMD\n");
  768. else
  769. data |= (0x4 << 12);
  770. break;
  771. }
  772. switch (ret & SH_FSI_BPFMD_MASK) {
  773. default:
  774. /* FALL THROUGH */
  775. case SH_FSI_BPFMD_32:
  776. data |= (0x0 << 8);
  777. break;
  778. case SH_FSI_BPFMD_64:
  779. data |= (0x1 << 8);
  780. break;
  781. case SH_FSI_BPFMD_128:
  782. data |= (0x2 << 8);
  783. break;
  784. case SH_FSI_BPFMD_256:
  785. data |= (0x3 << 8);
  786. break;
  787. case SH_FSI_BPFMD_512:
  788. data |= (0x4 << 8);
  789. break;
  790. case SH_FSI_BPFMD_16:
  791. if (fsi_ver < 2)
  792. dev_err(dai->dev, "unsupported ACKMD\n");
  793. else
  794. data |= (0x7 << 8);
  795. break;
  796. }
  797. fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
  798. udelay(10);
  799. fsi_clk_ctrl(fsi, 1);
  800. ret = 0;
  801. }
  802. return ret;
  803. }
  804. static struct snd_soc_dai_ops fsi_dai_ops = {
  805. .startup = fsi_dai_startup,
  806. .shutdown = fsi_dai_shutdown,
  807. .trigger = fsi_dai_trigger,
  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. master->fsia.mst_ctrl = A_MST_CTLR;
  976. /* FSI B setting */
  977. master->fsib.base = master->base + 0x40;
  978. master->fsib.master = master;
  979. master->fsib.mst_ctrl = B_MST_CTLR;
  980. pm_runtime_enable(&pdev->dev);
  981. pm_runtime_resume(&pdev->dev);
  982. dev_set_drvdata(&pdev->dev, master);
  983. fsi_soft_all_reset(master);
  984. ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED,
  985. id_entry->name, master);
  986. if (ret) {
  987. dev_err(&pdev->dev, "irq request err\n");
  988. goto exit_iounmap;
  989. }
  990. ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
  991. if (ret < 0) {
  992. dev_err(&pdev->dev, "cannot snd soc register\n");
  993. goto exit_free_irq;
  994. }
  995. return snd_soc_register_dais(&pdev->dev, fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
  996. exit_free_irq:
  997. free_irq(irq, master);
  998. exit_iounmap:
  999. iounmap(master->base);
  1000. pm_runtime_disable(&pdev->dev);
  1001. exit_kfree:
  1002. kfree(master);
  1003. master = NULL;
  1004. exit:
  1005. return ret;
  1006. }
  1007. static int fsi_remove(struct platform_device *pdev)
  1008. {
  1009. struct fsi_master *master;
  1010. master = dev_get_drvdata(&pdev->dev);
  1011. snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
  1012. snd_soc_unregister_platform(&pdev->dev);
  1013. pm_runtime_disable(&pdev->dev);
  1014. free_irq(master->irq, master);
  1015. iounmap(master->base);
  1016. kfree(master);
  1017. return 0;
  1018. }
  1019. static int fsi_runtime_nop(struct device *dev)
  1020. {
  1021. /* Runtime PM callback shared between ->runtime_suspend()
  1022. * and ->runtime_resume(). Simply returns success.
  1023. *
  1024. * This driver re-initializes all registers after
  1025. * pm_runtime_get_sync() anyway so there is no need
  1026. * to save and restore registers here.
  1027. */
  1028. return 0;
  1029. }
  1030. static struct dev_pm_ops fsi_pm_ops = {
  1031. .runtime_suspend = fsi_runtime_nop,
  1032. .runtime_resume = fsi_runtime_nop,
  1033. };
  1034. static struct fsi_core fsi1_core = {
  1035. .ver = 1,
  1036. /* Interrupt */
  1037. .int_st = INT_ST,
  1038. .iemsk = IEMSK,
  1039. .imsk = IMSK,
  1040. };
  1041. static struct fsi_core fsi2_core = {
  1042. .ver = 2,
  1043. /* Interrupt */
  1044. .int_st = CPU_INT_ST,
  1045. .iemsk = CPU_IEMSK,
  1046. .imsk = CPU_IMSK,
  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>");