fsi.c 23 KB

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