fsi.c 20 KB

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