fsi.c 22 KB

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