fsi.c 26 KB

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