fsi.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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/init.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/delay.h>
  18. #include <linux/list.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/io.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/initval.h>
  24. #include <sound/soc.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/sh_fsi.h>
  27. #include <asm/atomic.h>
  28. #define DO_FMT 0x0000
  29. #define DOFF_CTL 0x0004
  30. #define DOFF_ST 0x0008
  31. #define DI_FMT 0x000C
  32. #define DIFF_CTL 0x0010
  33. #define DIFF_ST 0x0014
  34. #define CKG1 0x0018
  35. #define CKG2 0x001C
  36. #define DIDT 0x0020
  37. #define DODT 0x0024
  38. #define MUTE_ST 0x0028
  39. #define REG_END MUTE_ST
  40. #define INT_ST 0x0200
  41. #define IEMSK 0x0204
  42. #define IMSK 0x0208
  43. #define MUTE 0x020C
  44. #define CLK_RST 0x0210
  45. #define SOFT_RST 0x0214
  46. #define MREG_START INT_ST
  47. #define MREG_END SOFT_RST
  48. /* DO_FMT */
  49. /* DI_FMT */
  50. #define CR_FMT(param) ((param) << 4)
  51. # define CR_MONO 0x0
  52. # define CR_MONO_D 0x1
  53. # define CR_PCM 0x2
  54. # define CR_I2S 0x3
  55. # define CR_TDM 0x4
  56. # define CR_TDM_D 0x5
  57. /* DOFF_CTL */
  58. /* DIFF_CTL */
  59. #define IRQ_HALF 0x00100000
  60. #define FIFO_CLR 0x00000001
  61. /* DOFF_ST */
  62. #define ERR_OVER 0x00000010
  63. #define ERR_UNDER 0x00000001
  64. #define ST_ERR (ERR_OVER | ERR_UNDER)
  65. /* CLK_RST */
  66. #define B_CLK 0x00000010
  67. #define A_CLK 0x00000001
  68. /* INT_ST */
  69. #define INT_B_IN (1 << 12)
  70. #define INT_B_OUT (1 << 8)
  71. #define INT_A_IN (1 << 4)
  72. #define INT_A_OUT (1 << 0)
  73. #define FSI_RATES SNDRV_PCM_RATE_8000_96000
  74. #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  75. /************************************************************************
  76. struct
  77. ************************************************************************/
  78. struct fsi_priv {
  79. void __iomem *base;
  80. struct snd_pcm_substream *substream;
  81. struct fsi_master *master;
  82. int fifo_max;
  83. int chan;
  84. int byte_offset;
  85. int period_len;
  86. int buffer_len;
  87. int periods;
  88. };
  89. struct fsi_master {
  90. void __iomem *base;
  91. int irq;
  92. struct fsi_priv fsia;
  93. struct fsi_priv fsib;
  94. struct sh_fsi_platform_info *info;
  95. spinlock_t lock;
  96. };
  97. /************************************************************************
  98. basic read write function
  99. ************************************************************************/
  100. static void __fsi_reg_write(u32 reg, u32 data)
  101. {
  102. /* valid data area is 24bit */
  103. data &= 0x00ffffff;
  104. __raw_writel(data, reg);
  105. }
  106. static u32 __fsi_reg_read(u32 reg)
  107. {
  108. return __raw_readl(reg);
  109. }
  110. static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
  111. {
  112. u32 val = __fsi_reg_read(reg);
  113. val &= ~mask;
  114. val |= data & mask;
  115. __fsi_reg_write(reg, val);
  116. }
  117. static void fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data)
  118. {
  119. if (reg > REG_END)
  120. return;
  121. __fsi_reg_write((u32)(fsi->base + reg), data);
  122. }
  123. static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg)
  124. {
  125. if (reg > REG_END)
  126. return 0;
  127. return __fsi_reg_read((u32)(fsi->base + reg));
  128. }
  129. static void fsi_reg_mask_set(struct fsi_priv *fsi, u32 reg, u32 mask, u32 data)
  130. {
  131. if (reg > REG_END)
  132. return;
  133. __fsi_reg_mask_set((u32)(fsi->base + reg), mask, data);
  134. }
  135. static void fsi_master_write(struct fsi_master *master, u32 reg, u32 data)
  136. {
  137. unsigned long flags;
  138. if ((reg < MREG_START) ||
  139. (reg > MREG_END))
  140. return;
  141. spin_lock_irqsave(&master->lock, flags);
  142. __fsi_reg_write((u32)(master->base + reg), data);
  143. spin_unlock_irqrestore(&master->lock, flags);
  144. }
  145. static u32 fsi_master_read(struct fsi_master *master, u32 reg)
  146. {
  147. u32 ret;
  148. unsigned long flags;
  149. if ((reg < MREG_START) ||
  150. (reg > MREG_END))
  151. return 0;
  152. spin_lock_irqsave(&master->lock, flags);
  153. ret = __fsi_reg_read((u32)(master->base + reg));
  154. spin_unlock_irqrestore(&master->lock, flags);
  155. return ret;
  156. }
  157. static void fsi_master_mask_set(struct fsi_master *master,
  158. u32 reg, u32 mask, u32 data)
  159. {
  160. unsigned long flags;
  161. if ((reg < MREG_START) ||
  162. (reg > MREG_END))
  163. return;
  164. spin_lock_irqsave(&master->lock, flags);
  165. __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
  166. spin_unlock_irqrestore(&master->lock, flags);
  167. }
  168. /************************************************************************
  169. basic function
  170. ************************************************************************/
  171. static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
  172. {
  173. return fsi->master;
  174. }
  175. static int fsi_is_port_a(struct fsi_priv *fsi)
  176. {
  177. return fsi->master->base == fsi->base;
  178. }
  179. static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
  180. {
  181. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  182. struct snd_soc_dai_link *machine = rtd->dai;
  183. return machine->cpu_dai;
  184. }
  185. static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
  186. {
  187. struct snd_soc_dai *dai = fsi_get_dai(substream);
  188. return dai->private_data;
  189. }
  190. static u32 fsi_get_info_flags(struct fsi_priv *fsi)
  191. {
  192. int is_porta = fsi_is_port_a(fsi);
  193. struct fsi_master *master = fsi_get_master(fsi);
  194. return is_porta ? master->info->porta_flags :
  195. master->info->portb_flags;
  196. }
  197. static int fsi_is_master_mode(struct fsi_priv *fsi, int is_play)
  198. {
  199. u32 mode;
  200. u32 flags = fsi_get_info_flags(fsi);
  201. mode = is_play ? SH_FSI_OUT_SLAVE_MODE : SH_FSI_IN_SLAVE_MODE;
  202. /* return
  203. * 1 : master mode
  204. * 0 : slave mode
  205. */
  206. return (mode & flags) != mode;
  207. }
  208. static u32 fsi_port_ab_io_bit(struct fsi_priv *fsi, int is_play)
  209. {
  210. int is_porta = fsi_is_port_a(fsi);
  211. u32 data;
  212. if (is_porta)
  213. data = is_play ? (1 << 0) : (1 << 4);
  214. else
  215. data = is_play ? (1 << 8) : (1 << 12);
  216. return data;
  217. }
  218. static void fsi_stream_push(struct fsi_priv *fsi,
  219. struct snd_pcm_substream *substream,
  220. u32 buffer_len,
  221. u32 period_len)
  222. {
  223. fsi->substream = substream;
  224. fsi->buffer_len = buffer_len;
  225. fsi->period_len = period_len;
  226. fsi->byte_offset = 0;
  227. fsi->periods = 0;
  228. }
  229. static void fsi_stream_pop(struct fsi_priv *fsi)
  230. {
  231. fsi->substream = NULL;
  232. fsi->buffer_len = 0;
  233. fsi->period_len = 0;
  234. fsi->byte_offset = 0;
  235. fsi->periods = 0;
  236. }
  237. static int fsi_get_fifo_residue(struct fsi_priv *fsi, int is_play)
  238. {
  239. u32 status;
  240. u32 reg = is_play ? DOFF_ST : DIFF_ST;
  241. int residue;
  242. status = fsi_reg_read(fsi, reg);
  243. residue = 0x1ff & (status >> 8);
  244. residue *= fsi->chan;
  245. return residue;
  246. }
  247. /************************************************************************
  248. ctrl function
  249. ************************************************************************/
  250. static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
  251. {
  252. u32 data = fsi_port_ab_io_bit(fsi, is_play);
  253. struct fsi_master *master = fsi_get_master(fsi);
  254. fsi_master_mask_set(master, IMSK, data, data);
  255. fsi_master_mask_set(master, IEMSK, data, data);
  256. }
  257. static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
  258. {
  259. u32 data = fsi_port_ab_io_bit(fsi, is_play);
  260. struct fsi_master *master = fsi_get_master(fsi);
  261. fsi_master_mask_set(master, IMSK, data, 0);
  262. fsi_master_mask_set(master, IEMSK, data, 0);
  263. }
  264. static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
  265. {
  266. u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
  267. struct fsi_master *master = fsi_get_master(fsi);
  268. if (enable)
  269. fsi_master_mask_set(master, CLK_RST, val, val);
  270. else
  271. fsi_master_mask_set(master, CLK_RST, val, 0);
  272. }
  273. static void fsi_irq_init(struct fsi_priv *fsi, int is_play)
  274. {
  275. u32 data;
  276. u32 ctrl;
  277. data = fsi_port_ab_io_bit(fsi, is_play);
  278. ctrl = is_play ? DOFF_CTL : DIFF_CTL;
  279. /* set IMSK */
  280. fsi_irq_disable(fsi, is_play);
  281. /* set interrupt generation factor */
  282. fsi_reg_write(fsi, ctrl, IRQ_HALF);
  283. /* clear FIFO */
  284. fsi_reg_mask_set(fsi, ctrl, FIFO_CLR, FIFO_CLR);
  285. /* clear interrupt factor */
  286. fsi_master_mask_set(fsi_get_master(fsi), INT_ST, data, 0);
  287. }
  288. static void fsi_soft_all_reset(struct fsi_master *master)
  289. {
  290. u32 status = fsi_master_read(master, SOFT_RST);
  291. /* port AB reset */
  292. status &= 0x000000ff;
  293. fsi_master_write(master, SOFT_RST, status);
  294. mdelay(10);
  295. /* soft reset */
  296. status &= 0x000000f0;
  297. fsi_master_write(master, SOFT_RST, status);
  298. status |= 0x00000001;
  299. fsi_master_write(master, SOFT_RST, status);
  300. mdelay(10);
  301. }
  302. /* playback interrupt */
  303. static int fsi_data_push(struct fsi_priv *fsi, int startup)
  304. {
  305. struct snd_pcm_runtime *runtime;
  306. struct snd_pcm_substream *substream = NULL;
  307. u32 status;
  308. int send;
  309. int fifo_free;
  310. int width;
  311. u8 *start;
  312. int i, over_period;
  313. if (!fsi ||
  314. !fsi->substream ||
  315. !fsi->substream->runtime)
  316. return -EINVAL;
  317. over_period = 0;
  318. substream = fsi->substream;
  319. runtime = substream->runtime;
  320. /* FSI FIFO has limit.
  321. * So, this driver can not send periods data at a time
  322. */
  323. if (fsi->byte_offset >=
  324. fsi->period_len * (fsi->periods + 1)) {
  325. over_period = 1;
  326. fsi->periods = (fsi->periods + 1) % runtime->periods;
  327. if (0 == fsi->periods)
  328. fsi->byte_offset = 0;
  329. }
  330. /* get 1 channel data width */
  331. width = frames_to_bytes(runtime, 1) / fsi->chan;
  332. /* get send size for alsa */
  333. send = (fsi->buffer_len - fsi->byte_offset) / width;
  334. /* get FIFO free size */
  335. fifo_free = (fsi->fifo_max * fsi->chan) - fsi_get_fifo_residue(fsi, 1);
  336. /* size check */
  337. if (fifo_free < send)
  338. send = fifo_free;
  339. start = runtime->dma_area;
  340. start += fsi->byte_offset;
  341. switch (width) {
  342. case 2:
  343. for (i = 0; i < send; i++)
  344. fsi_reg_write(fsi, DODT,
  345. ((u32)*((u16 *)start + i) << 8));
  346. break;
  347. case 4:
  348. for (i = 0; i < send; i++)
  349. fsi_reg_write(fsi, DODT, *((u32 *)start + i));
  350. break;
  351. default:
  352. return -EINVAL;
  353. }
  354. fsi->byte_offset += send * width;
  355. status = fsi_reg_read(fsi, DOFF_ST);
  356. if (!startup) {
  357. struct snd_soc_dai *dai = fsi_get_dai(substream);
  358. if (status & ERR_OVER)
  359. dev_err(dai->dev, "over run\n");
  360. if (status & ERR_UNDER)
  361. dev_err(dai->dev, "under run\n");
  362. }
  363. fsi_reg_write(fsi, DOFF_ST, 0);
  364. fsi_irq_enable(fsi, 1);
  365. if (over_period)
  366. snd_pcm_period_elapsed(substream);
  367. return 0;
  368. }
  369. static int fsi_data_pop(struct fsi_priv *fsi, int startup)
  370. {
  371. struct snd_pcm_runtime *runtime;
  372. struct snd_pcm_substream *substream = NULL;
  373. u32 status;
  374. int free;
  375. int fifo_fill;
  376. int width;
  377. u8 *start;
  378. int i, over_period;
  379. if (!fsi ||
  380. !fsi->substream ||
  381. !fsi->substream->runtime)
  382. return -EINVAL;
  383. over_period = 0;
  384. substream = fsi->substream;
  385. runtime = substream->runtime;
  386. /* FSI FIFO has limit.
  387. * So, this driver can not send periods data at a time
  388. */
  389. if (fsi->byte_offset >=
  390. fsi->period_len * (fsi->periods + 1)) {
  391. over_period = 1;
  392. fsi->periods = (fsi->periods + 1) % runtime->periods;
  393. if (0 == fsi->periods)
  394. fsi->byte_offset = 0;
  395. }
  396. /* get 1 channel data width */
  397. width = frames_to_bytes(runtime, 1) / fsi->chan;
  398. /* get free space for alsa */
  399. free = (fsi->buffer_len - fsi->byte_offset) / width;
  400. /* get recv size */
  401. fifo_fill = fsi_get_fifo_residue(fsi, 0);
  402. if (free < fifo_fill)
  403. fifo_fill = free;
  404. start = runtime->dma_area;
  405. start += fsi->byte_offset;
  406. switch (width) {
  407. case 2:
  408. for (i = 0; i < fifo_fill; i++)
  409. *((u16 *)start + i) =
  410. (u16)(fsi_reg_read(fsi, DIDT) >> 8);
  411. break;
  412. case 4:
  413. for (i = 0; i < fifo_fill; i++)
  414. *((u32 *)start + i) = fsi_reg_read(fsi, DIDT);
  415. break;
  416. default:
  417. return -EINVAL;
  418. }
  419. fsi->byte_offset += fifo_fill * width;
  420. status = fsi_reg_read(fsi, DIFF_ST);
  421. if (!startup) {
  422. struct snd_soc_dai *dai = fsi_get_dai(substream);
  423. if (status & ERR_OVER)
  424. dev_err(dai->dev, "over run\n");
  425. if (status & ERR_UNDER)
  426. dev_err(dai->dev, "under run\n");
  427. }
  428. fsi_reg_write(fsi, DIFF_ST, 0);
  429. fsi_irq_enable(fsi, 0);
  430. if (over_period)
  431. snd_pcm_period_elapsed(substream);
  432. return 0;
  433. }
  434. static irqreturn_t fsi_interrupt(int irq, void *data)
  435. {
  436. struct fsi_master *master = data;
  437. u32 status = fsi_master_read(master, SOFT_RST) & ~0x00000010;
  438. u32 int_st = fsi_master_read(master, INT_ST);
  439. /* clear irq status */
  440. fsi_master_write(master, SOFT_RST, status);
  441. fsi_master_write(master, SOFT_RST, status | 0x00000010);
  442. if (int_st & INT_A_OUT)
  443. fsi_data_push(&master->fsia, 0);
  444. if (int_st & INT_B_OUT)
  445. fsi_data_push(&master->fsib, 0);
  446. if (int_st & INT_A_IN)
  447. fsi_data_pop(&master->fsia, 0);
  448. if (int_st & INT_B_IN)
  449. fsi_data_pop(&master->fsib, 0);
  450. fsi_master_write(master, INT_ST, 0x0000000);
  451. return IRQ_HANDLED;
  452. }
  453. /************************************************************************
  454. dai ops
  455. ************************************************************************/
  456. static int fsi_dai_startup(struct snd_pcm_substream *substream,
  457. struct snd_soc_dai *dai)
  458. {
  459. struct fsi_priv *fsi = fsi_get_priv(substream);
  460. const char *msg;
  461. u32 flags = fsi_get_info_flags(fsi);
  462. u32 fmt;
  463. u32 reg;
  464. u32 data;
  465. int is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  466. int is_master;
  467. int ret = 0;
  468. pm_runtime_get_sync(dai->dev);
  469. /* CKG1 */
  470. data = is_play ? (1 << 0) : (1 << 4);
  471. is_master = fsi_is_master_mode(fsi, is_play);
  472. if (is_master)
  473. fsi_reg_mask_set(fsi, CKG1, data, data);
  474. else
  475. fsi_reg_mask_set(fsi, CKG1, data, 0);
  476. /* clock inversion (CKG2) */
  477. data = 0;
  478. switch (SH_FSI_INVERSION_MASK & flags) {
  479. case SH_FSI_LRM_INV:
  480. data = 1 << 12;
  481. break;
  482. case SH_FSI_BRM_INV:
  483. data = 1 << 8;
  484. break;
  485. case SH_FSI_LRS_INV:
  486. data = 1 << 4;
  487. break;
  488. case SH_FSI_BRS_INV:
  489. data = 1 << 0;
  490. break;
  491. }
  492. fsi_reg_write(fsi, CKG2, data);
  493. /* do fmt, di fmt */
  494. data = 0;
  495. reg = is_play ? DO_FMT : DI_FMT;
  496. fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
  497. switch (fmt) {
  498. case SH_FSI_FMT_MONO:
  499. msg = "MONO";
  500. data = CR_FMT(CR_MONO);
  501. fsi->chan = 1;
  502. break;
  503. case SH_FSI_FMT_MONO_DELAY:
  504. msg = "MONO Delay";
  505. data = CR_FMT(CR_MONO_D);
  506. fsi->chan = 1;
  507. break;
  508. case SH_FSI_FMT_PCM:
  509. msg = "PCM";
  510. data = CR_FMT(CR_PCM);
  511. fsi->chan = 2;
  512. break;
  513. case SH_FSI_FMT_I2S:
  514. msg = "I2S";
  515. data = CR_FMT(CR_I2S);
  516. fsi->chan = 2;
  517. break;
  518. case SH_FSI_FMT_TDM:
  519. msg = "TDM";
  520. data = CR_FMT(CR_TDM) | (fsi->chan - 1);
  521. fsi->chan = is_play ?
  522. SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
  523. break;
  524. case SH_FSI_FMT_TDM_DELAY:
  525. msg = "TDM Delay";
  526. data = CR_FMT(CR_TDM_D) | (fsi->chan - 1);
  527. fsi->chan = is_play ?
  528. SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
  529. break;
  530. default:
  531. dev_err(dai->dev, "unknown format.\n");
  532. return -EINVAL;
  533. }
  534. switch (fsi->chan) {
  535. case 1:
  536. fsi->fifo_max = 256;
  537. break;
  538. case 2:
  539. fsi->fifo_max = 128;
  540. break;
  541. case 3:
  542. case 4:
  543. fsi->fifo_max = 64;
  544. break;
  545. case 5:
  546. case 6:
  547. case 7:
  548. case 8:
  549. fsi->fifo_max = 32;
  550. break;
  551. default:
  552. dev_err(dai->dev, "channel size error.\n");
  553. return -EINVAL;
  554. }
  555. fsi_reg_write(fsi, reg, data);
  556. /*
  557. * clear clk reset if master mode
  558. */
  559. if (is_master)
  560. fsi_clk_ctrl(fsi, 1);
  561. /* irq setting */
  562. fsi_irq_init(fsi, is_play);
  563. return ret;
  564. }
  565. static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
  566. struct snd_soc_dai *dai)
  567. {
  568. struct fsi_priv *fsi = fsi_get_priv(substream);
  569. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  570. fsi_irq_disable(fsi, is_play);
  571. fsi_clk_ctrl(fsi, 0);
  572. pm_runtime_put_sync(dai->dev);
  573. }
  574. static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  575. struct snd_soc_dai *dai)
  576. {
  577. struct fsi_priv *fsi = fsi_get_priv(substream);
  578. struct snd_pcm_runtime *runtime = substream->runtime;
  579. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  580. int ret = 0;
  581. switch (cmd) {
  582. case SNDRV_PCM_TRIGGER_START:
  583. fsi_stream_push(fsi, substream,
  584. frames_to_bytes(runtime, runtime->buffer_size),
  585. frames_to_bytes(runtime, runtime->period_size));
  586. ret = is_play ? fsi_data_push(fsi, 1) : fsi_data_pop(fsi, 1);
  587. break;
  588. case SNDRV_PCM_TRIGGER_STOP:
  589. fsi_irq_disable(fsi, is_play);
  590. fsi_stream_pop(fsi);
  591. break;
  592. }
  593. return ret;
  594. }
  595. static struct snd_soc_dai_ops fsi_dai_ops = {
  596. .startup = fsi_dai_startup,
  597. .shutdown = fsi_dai_shutdown,
  598. .trigger = fsi_dai_trigger,
  599. };
  600. /************************************************************************
  601. pcm ops
  602. ************************************************************************/
  603. static struct snd_pcm_hardware fsi_pcm_hardware = {
  604. .info = SNDRV_PCM_INFO_INTERLEAVED |
  605. SNDRV_PCM_INFO_MMAP |
  606. SNDRV_PCM_INFO_MMAP_VALID |
  607. SNDRV_PCM_INFO_PAUSE,
  608. .formats = FSI_FMTS,
  609. .rates = FSI_RATES,
  610. .rate_min = 8000,
  611. .rate_max = 192000,
  612. .channels_min = 1,
  613. .channels_max = 2,
  614. .buffer_bytes_max = 64 * 1024,
  615. .period_bytes_min = 32,
  616. .period_bytes_max = 8192,
  617. .periods_min = 1,
  618. .periods_max = 32,
  619. .fifo_size = 256,
  620. };
  621. static int fsi_pcm_open(struct snd_pcm_substream *substream)
  622. {
  623. struct snd_pcm_runtime *runtime = substream->runtime;
  624. int ret = 0;
  625. snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
  626. ret = snd_pcm_hw_constraint_integer(runtime,
  627. SNDRV_PCM_HW_PARAM_PERIODS);
  628. return ret;
  629. }
  630. static int fsi_hw_params(struct snd_pcm_substream *substream,
  631. struct snd_pcm_hw_params *hw_params)
  632. {
  633. return snd_pcm_lib_malloc_pages(substream,
  634. params_buffer_bytes(hw_params));
  635. }
  636. static int fsi_hw_free(struct snd_pcm_substream *substream)
  637. {
  638. return snd_pcm_lib_free_pages(substream);
  639. }
  640. static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
  641. {
  642. struct snd_pcm_runtime *runtime = substream->runtime;
  643. struct fsi_priv *fsi = fsi_get_priv(substream);
  644. long location;
  645. location = (fsi->byte_offset - 1);
  646. if (location < 0)
  647. location = 0;
  648. return bytes_to_frames(runtime, location);
  649. }
  650. static struct snd_pcm_ops fsi_pcm_ops = {
  651. .open = fsi_pcm_open,
  652. .ioctl = snd_pcm_lib_ioctl,
  653. .hw_params = fsi_hw_params,
  654. .hw_free = fsi_hw_free,
  655. .pointer = fsi_pointer,
  656. };
  657. /************************************************************************
  658. snd_soc_platform
  659. ************************************************************************/
  660. #define PREALLOC_BUFFER (32 * 1024)
  661. #define PREALLOC_BUFFER_MAX (32 * 1024)
  662. static void fsi_pcm_free(struct snd_pcm *pcm)
  663. {
  664. snd_pcm_lib_preallocate_free_for_all(pcm);
  665. }
  666. static int fsi_pcm_new(struct snd_card *card,
  667. struct snd_soc_dai *dai,
  668. struct snd_pcm *pcm)
  669. {
  670. /*
  671. * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
  672. * in MMAP mode (i.e. aplay -M)
  673. */
  674. return snd_pcm_lib_preallocate_pages_for_all(
  675. pcm,
  676. SNDRV_DMA_TYPE_CONTINUOUS,
  677. snd_dma_continuous_data(GFP_KERNEL),
  678. PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
  679. }
  680. /************************************************************************
  681. alsa struct
  682. ************************************************************************/
  683. struct snd_soc_dai fsi_soc_dai[] = {
  684. {
  685. .name = "FSIA",
  686. .id = 0,
  687. .playback = {
  688. .rates = FSI_RATES,
  689. .formats = FSI_FMTS,
  690. .channels_min = 1,
  691. .channels_max = 8,
  692. },
  693. .capture = {
  694. .rates = FSI_RATES,
  695. .formats = FSI_FMTS,
  696. .channels_min = 1,
  697. .channels_max = 8,
  698. },
  699. .ops = &fsi_dai_ops,
  700. },
  701. {
  702. .name = "FSIB",
  703. .id = 1,
  704. .playback = {
  705. .rates = FSI_RATES,
  706. .formats = FSI_FMTS,
  707. .channels_min = 1,
  708. .channels_max = 8,
  709. },
  710. .capture = {
  711. .rates = FSI_RATES,
  712. .formats = FSI_FMTS,
  713. .channels_min = 1,
  714. .channels_max = 8,
  715. },
  716. .ops = &fsi_dai_ops,
  717. },
  718. };
  719. EXPORT_SYMBOL_GPL(fsi_soc_dai);
  720. struct snd_soc_platform fsi_soc_platform = {
  721. .name = "fsi-pcm",
  722. .pcm_ops = &fsi_pcm_ops,
  723. .pcm_new = fsi_pcm_new,
  724. .pcm_free = fsi_pcm_free,
  725. };
  726. EXPORT_SYMBOL_GPL(fsi_soc_platform);
  727. /************************************************************************
  728. platform function
  729. ************************************************************************/
  730. static int fsi_probe(struct platform_device *pdev)
  731. {
  732. struct fsi_master *master;
  733. struct resource *res;
  734. unsigned int irq;
  735. int ret;
  736. if (0 != pdev->id) {
  737. dev_err(&pdev->dev, "current fsi support id 0 only now\n");
  738. return -ENODEV;
  739. }
  740. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  741. irq = platform_get_irq(pdev, 0);
  742. if (!res || (int)irq <= 0) {
  743. dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
  744. ret = -ENODEV;
  745. goto exit;
  746. }
  747. master = kzalloc(sizeof(*master), GFP_KERNEL);
  748. if (!master) {
  749. dev_err(&pdev->dev, "Could not allocate master\n");
  750. ret = -ENOMEM;
  751. goto exit;
  752. }
  753. master->base = ioremap_nocache(res->start, resource_size(res));
  754. if (!master->base) {
  755. ret = -ENXIO;
  756. dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
  757. goto exit_kfree;
  758. }
  759. master->irq = irq;
  760. master->info = pdev->dev.platform_data;
  761. master->fsia.base = master->base;
  762. master->fsia.master = master;
  763. master->fsib.base = master->base + 0x40;
  764. master->fsib.master = master;
  765. spin_lock_init(&master->lock);
  766. pm_runtime_enable(&pdev->dev);
  767. pm_runtime_resume(&pdev->dev);
  768. fsi_soc_dai[0].dev = &pdev->dev;
  769. fsi_soc_dai[0].private_data = &master->fsia;
  770. fsi_soc_dai[1].dev = &pdev->dev;
  771. fsi_soc_dai[1].private_data = &master->fsib;
  772. fsi_soft_all_reset(master);
  773. ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED, "fsi", master);
  774. if (ret) {
  775. dev_err(&pdev->dev, "irq request err\n");
  776. goto exit_iounmap;
  777. }
  778. ret = snd_soc_register_platform(&fsi_soc_platform);
  779. if (ret < 0) {
  780. dev_err(&pdev->dev, "cannot snd soc register\n");
  781. goto exit_free_irq;
  782. }
  783. return snd_soc_register_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
  784. exit_free_irq:
  785. free_irq(irq, master);
  786. exit_iounmap:
  787. iounmap(master->base);
  788. pm_runtime_disable(&pdev->dev);
  789. exit_kfree:
  790. kfree(master);
  791. master = NULL;
  792. exit:
  793. return ret;
  794. }
  795. static int fsi_remove(struct platform_device *pdev)
  796. {
  797. struct fsi_master *master;
  798. master = fsi_get_master(fsi_soc_dai[0].private_data);
  799. snd_soc_unregister_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
  800. snd_soc_unregister_platform(&fsi_soc_platform);
  801. pm_runtime_disable(&pdev->dev);
  802. free_irq(master->irq, master);
  803. iounmap(master->base);
  804. kfree(master);
  805. fsi_soc_dai[0].dev = NULL;
  806. fsi_soc_dai[0].private_data = NULL;
  807. fsi_soc_dai[1].dev = NULL;
  808. fsi_soc_dai[1].private_data = NULL;
  809. return 0;
  810. }
  811. static int fsi_runtime_nop(struct device *dev)
  812. {
  813. /* Runtime PM callback shared between ->runtime_suspend()
  814. * and ->runtime_resume(). Simply returns success.
  815. *
  816. * This driver re-initializes all registers after
  817. * pm_runtime_get_sync() anyway so there is no need
  818. * to save and restore registers here.
  819. */
  820. return 0;
  821. }
  822. static struct dev_pm_ops fsi_pm_ops = {
  823. .runtime_suspend = fsi_runtime_nop,
  824. .runtime_resume = fsi_runtime_nop,
  825. };
  826. static struct platform_driver fsi_driver = {
  827. .driver = {
  828. .name = "sh_fsi",
  829. .pm = &fsi_pm_ops,
  830. },
  831. .probe = fsi_probe,
  832. .remove = fsi_remove,
  833. };
  834. static int __init fsi_mobile_init(void)
  835. {
  836. return platform_driver_register(&fsi_driver);
  837. }
  838. static void __exit fsi_mobile_exit(void)
  839. {
  840. platform_driver_unregister(&fsi_driver);
  841. }
  842. module_init(fsi_mobile_init);
  843. module_exit(fsi_mobile_exit);
  844. MODULE_LICENSE("GPL");
  845. MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
  846. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");