fsi.c 27 KB

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