fsi.c 27 KB

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