fsi.c 27 KB

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