fsi.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  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 <linux/module.h>
  19. #include <sound/soc.h>
  20. #include <sound/sh_fsi.h>
  21. /* PortA/PortB register */
  22. #define REG_DO_FMT 0x0000
  23. #define REG_DOFF_CTL 0x0004
  24. #define REG_DOFF_ST 0x0008
  25. #define REG_DI_FMT 0x000C
  26. #define REG_DIFF_CTL 0x0010
  27. #define REG_DIFF_ST 0x0014
  28. #define REG_CKG1 0x0018
  29. #define REG_CKG2 0x001C
  30. #define REG_DIDT 0x0020
  31. #define REG_DODT 0x0024
  32. #define REG_MUTE_ST 0x0028
  33. #define REG_OUT_DMAC 0x002C
  34. #define REG_OUT_SEL 0x0030
  35. #define REG_IN_DMAC 0x0038
  36. /* master register */
  37. #define MST_CLK_RST 0x0210
  38. #define MST_SOFT_RST 0x0214
  39. #define MST_FIFO_SZ 0x0218
  40. /* core register (depend on FSI version) */
  41. #define A_MST_CTLR 0x0180
  42. #define B_MST_CTLR 0x01A0
  43. #define CPU_INT_ST 0x01F4
  44. #define CPU_IEMSK 0x01F8
  45. #define CPU_IMSK 0x01FC
  46. #define INT_ST 0x0200
  47. #define IEMSK 0x0204
  48. #define IMSK 0x0208
  49. /* DO_FMT */
  50. /* DI_FMT */
  51. #define CR_BWS_24 (0x0 << 20) /* FSI2 */
  52. #define CR_BWS_16 (0x1 << 20) /* FSI2 */
  53. #define CR_BWS_20 (0x2 << 20) /* FSI2 */
  54. #define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
  55. #define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
  56. #define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
  57. #define CR_MONO (0x0 << 4)
  58. #define CR_MONO_D (0x1 << 4)
  59. #define CR_PCM (0x2 << 4)
  60. #define CR_I2S (0x3 << 4)
  61. #define CR_TDM (0x4 << 4)
  62. #define CR_TDM_D (0x5 << 4)
  63. /* DOFF_CTL */
  64. /* DIFF_CTL */
  65. #define IRQ_HALF 0x00100000
  66. #define FIFO_CLR 0x00000001
  67. /* DOFF_ST */
  68. #define ERR_OVER 0x00000010
  69. #define ERR_UNDER 0x00000001
  70. #define ST_ERR (ERR_OVER | ERR_UNDER)
  71. /* CKG1 */
  72. #define ACKMD_MASK 0x00007000
  73. #define BPFMD_MASK 0x00000700
  74. #define DIMD (1 << 4)
  75. #define DOMD (1 << 0)
  76. /* A/B MST_CTLR */
  77. #define BP (1 << 4) /* Fix the signal of Biphase output */
  78. #define SE (1 << 0) /* Fix the master clock */
  79. /* CLK_RST */
  80. #define CRB (1 << 4)
  81. #define CRA (1 << 0)
  82. /* IO SHIFT / MACRO */
  83. #define BI_SHIFT 12
  84. #define BO_SHIFT 8
  85. #define AI_SHIFT 4
  86. #define AO_SHIFT 0
  87. #define AB_IO(param, shift) (param << shift)
  88. /* SOFT_RST */
  89. #define PBSR (1 << 12) /* Port B Software Reset */
  90. #define PASR (1 << 8) /* Port A Software Reset */
  91. #define IR (1 << 4) /* Interrupt Reset */
  92. #define FSISR (1 << 0) /* Software Reset */
  93. /* OUT_SEL (FSI2) */
  94. #define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
  95. /* 1: Biphase and serial */
  96. /* FIFO_SZ */
  97. #define FIFO_SZ_MASK 0x7
  98. #define FSI_RATES SNDRV_PCM_RATE_8000_96000
  99. #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  100. typedef int (*set_rate_func)(struct device *dev, int is_porta, int rate, int enable);
  101. /*
  102. * FSI driver use below type name for variable
  103. *
  104. * xxx_num : number of data
  105. * xxx_pos : position of data
  106. * xxx_capa : capacity of data
  107. */
  108. /*
  109. * period/frame/sample image
  110. *
  111. * ex) PCM (2ch)
  112. *
  113. * period pos period pos
  114. * [n] [n + 1]
  115. * |<-------------------- period--------------------->|
  116. * ==|============================================ ... =|==
  117. * | |
  118. * ||<----- frame ----->|<------ frame ----->| ... |
  119. * |+--------------------+--------------------+- ... |
  120. * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
  121. * |+--------------------+--------------------+- ... |
  122. * ==|============================================ ... =|==
  123. */
  124. /*
  125. * FSI FIFO image
  126. *
  127. * | |
  128. * | |
  129. * | [ sample ] |
  130. * | [ sample ] |
  131. * | [ sample ] |
  132. * | [ sample ] |
  133. * --> go to codecs
  134. */
  135. /*
  136. * struct
  137. */
  138. struct fsi_stream {
  139. struct snd_pcm_substream *substream;
  140. int fifo_sample_capa; /* sample capacity of FSI FIFO */
  141. int buff_sample_capa; /* sample capacity of ALSA buffer */
  142. int buff_sample_pos; /* sample position of ALSA buffer */
  143. int period_samples; /* sample number / 1 period */
  144. int period_pos; /* current period position */
  145. int uerr_num;
  146. int oerr_num;
  147. };
  148. struct fsi_priv {
  149. void __iomem *base;
  150. struct fsi_master *master;
  151. struct fsi_stream playback;
  152. struct fsi_stream capture;
  153. u32 do_fmt;
  154. u32 di_fmt;
  155. int chan_num:16;
  156. int clk_master:1;
  157. int spdif:1;
  158. long rate;
  159. };
  160. struct fsi_core {
  161. int ver;
  162. u32 int_st;
  163. u32 iemsk;
  164. u32 imsk;
  165. u32 a_mclk;
  166. u32 b_mclk;
  167. };
  168. struct fsi_master {
  169. void __iomem *base;
  170. int irq;
  171. struct fsi_priv fsia;
  172. struct fsi_priv fsib;
  173. struct fsi_core *core;
  174. struct sh_fsi_platform_info *info;
  175. spinlock_t lock;
  176. };
  177. /*
  178. * basic read write function
  179. */
  180. static void __fsi_reg_write(u32 __iomem *reg, u32 data)
  181. {
  182. /* valid data area is 24bit */
  183. data &= 0x00ffffff;
  184. __raw_writel(data, reg);
  185. }
  186. static u32 __fsi_reg_read(u32 __iomem *reg)
  187. {
  188. return __raw_readl(reg);
  189. }
  190. static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
  191. {
  192. u32 val = __fsi_reg_read(reg);
  193. val &= ~mask;
  194. val |= data & mask;
  195. __fsi_reg_write(reg, val);
  196. }
  197. #define fsi_reg_write(p, r, d)\
  198. __fsi_reg_write((p->base + REG_##r), d)
  199. #define fsi_reg_read(p, r)\
  200. __fsi_reg_read((p->base + REG_##r))
  201. #define fsi_reg_mask_set(p, r, m, d)\
  202. __fsi_reg_mask_set((p->base + REG_##r), m, d)
  203. #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
  204. #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
  205. static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
  206. {
  207. u32 ret;
  208. unsigned long flags;
  209. spin_lock_irqsave(&master->lock, flags);
  210. ret = __fsi_reg_read(master->base + reg);
  211. spin_unlock_irqrestore(&master->lock, flags);
  212. return ret;
  213. }
  214. #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
  215. #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
  216. static void _fsi_master_mask_set(struct fsi_master *master,
  217. u32 reg, u32 mask, u32 data)
  218. {
  219. unsigned long flags;
  220. spin_lock_irqsave(&master->lock, flags);
  221. __fsi_reg_mask_set(master->base + reg, mask, data);
  222. spin_unlock_irqrestore(&master->lock, flags);
  223. }
  224. /*
  225. * basic function
  226. */
  227. static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
  228. {
  229. return fsi->master;
  230. }
  231. static int fsi_is_clk_master(struct fsi_priv *fsi)
  232. {
  233. return fsi->clk_master;
  234. }
  235. static int fsi_is_port_a(struct fsi_priv *fsi)
  236. {
  237. return fsi->master->base == fsi->base;
  238. }
  239. static int fsi_is_spdif(struct fsi_priv *fsi)
  240. {
  241. return fsi->spdif;
  242. }
  243. static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
  244. {
  245. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  246. return rtd->cpu_dai;
  247. }
  248. static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
  249. {
  250. struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
  251. if (dai->id == 0)
  252. return &master->fsia;
  253. else
  254. return &master->fsib;
  255. }
  256. static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
  257. {
  258. return fsi_get_priv_frm_dai(fsi_get_dai(substream));
  259. }
  260. static set_rate_func fsi_get_info_set_rate(struct fsi_master *master)
  261. {
  262. if (!master->info)
  263. return NULL;
  264. return master->info->set_rate;
  265. }
  266. static u32 fsi_get_info_flags(struct fsi_priv *fsi)
  267. {
  268. int is_porta = fsi_is_port_a(fsi);
  269. struct fsi_master *master = fsi_get_master(fsi);
  270. if (!master->info)
  271. return 0;
  272. return is_porta ? master->info->porta_flags :
  273. master->info->portb_flags;
  274. }
  275. static inline int fsi_stream_is_play(int stream)
  276. {
  277. return stream == SNDRV_PCM_STREAM_PLAYBACK;
  278. }
  279. static inline int fsi_is_play(struct snd_pcm_substream *substream)
  280. {
  281. return fsi_stream_is_play(substream->stream);
  282. }
  283. static inline struct fsi_stream *fsi_get_stream(struct fsi_priv *fsi,
  284. int is_play)
  285. {
  286. return is_play ? &fsi->playback : &fsi->capture;
  287. }
  288. static u32 fsi_get_port_shift(struct fsi_priv *fsi, int is_play)
  289. {
  290. int is_porta = fsi_is_port_a(fsi);
  291. u32 shift;
  292. if (is_porta)
  293. shift = is_play ? AO_SHIFT : AI_SHIFT;
  294. else
  295. shift = is_play ? BO_SHIFT : BI_SHIFT;
  296. return shift;
  297. }
  298. static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
  299. {
  300. return frames * fsi->chan_num;
  301. }
  302. static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
  303. {
  304. return samples / fsi->chan_num;
  305. }
  306. static int fsi_stream_is_working(struct fsi_priv *fsi,
  307. int is_play)
  308. {
  309. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  310. struct fsi_master *master = fsi_get_master(fsi);
  311. unsigned long flags;
  312. int ret;
  313. spin_lock_irqsave(&master->lock, flags);
  314. ret = !!io->substream;
  315. spin_unlock_irqrestore(&master->lock, flags);
  316. return ret;
  317. }
  318. static void fsi_stream_push(struct fsi_priv *fsi,
  319. int is_play,
  320. struct snd_pcm_substream *substream)
  321. {
  322. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  323. struct snd_pcm_runtime *runtime = substream->runtime;
  324. struct fsi_master *master = fsi_get_master(fsi);
  325. unsigned long flags;
  326. spin_lock_irqsave(&master->lock, flags);
  327. io->substream = substream;
  328. io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
  329. io->buff_sample_pos = 0;
  330. io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
  331. io->period_pos = 0;
  332. io->oerr_num = -1; /* ignore 1st err */
  333. io->uerr_num = -1; /* ignore 1st err */
  334. spin_unlock_irqrestore(&master->lock, flags);
  335. }
  336. static void fsi_stream_pop(struct fsi_priv *fsi, int is_play)
  337. {
  338. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  339. struct snd_soc_dai *dai = fsi_get_dai(io->substream);
  340. struct fsi_master *master = fsi_get_master(fsi);
  341. unsigned long flags;
  342. spin_lock_irqsave(&master->lock, flags);
  343. if (io->oerr_num > 0)
  344. dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
  345. if (io->uerr_num > 0)
  346. dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
  347. io->substream = NULL;
  348. io->buff_sample_capa = 0;
  349. io->buff_sample_pos = 0;
  350. io->period_samples = 0;
  351. io->period_pos = 0;
  352. io->oerr_num = 0;
  353. io->uerr_num = 0;
  354. spin_unlock_irqrestore(&master->lock, flags);
  355. }
  356. static int fsi_get_current_fifo_samples(struct fsi_priv *fsi, int is_play)
  357. {
  358. u32 status;
  359. int frames;
  360. status = is_play ?
  361. fsi_reg_read(fsi, DOFF_ST) :
  362. fsi_reg_read(fsi, DIFF_ST);
  363. frames = 0x1ff & (status >> 8);
  364. return fsi_frame2sample(fsi, frames);
  365. }
  366. static void fsi_count_fifo_err(struct fsi_priv *fsi)
  367. {
  368. u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
  369. u32 istatus = fsi_reg_read(fsi, DIFF_ST);
  370. if (ostatus & ERR_OVER)
  371. fsi->playback.oerr_num++;
  372. if (ostatus & ERR_UNDER)
  373. fsi->playback.uerr_num++;
  374. if (istatus & ERR_OVER)
  375. fsi->capture.oerr_num++;
  376. if (istatus & ERR_UNDER)
  377. fsi->capture.uerr_num++;
  378. fsi_reg_write(fsi, DOFF_ST, 0);
  379. fsi_reg_write(fsi, DIFF_ST, 0);
  380. }
  381. /*
  382. * dma function
  383. */
  384. static u8 *fsi_dma_get_area(struct fsi_priv *fsi, int stream)
  385. {
  386. int is_play = fsi_stream_is_play(stream);
  387. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  388. struct snd_pcm_runtime *runtime = io->substream->runtime;
  389. return runtime->dma_area +
  390. samples_to_bytes(runtime, io->buff_sample_pos);
  391. }
  392. static void fsi_dma_soft_push16(struct fsi_priv *fsi, int num)
  393. {
  394. u16 *start;
  395. int i;
  396. start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
  397. for (i = 0; i < num; i++)
  398. fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
  399. }
  400. static void fsi_dma_soft_pop16(struct fsi_priv *fsi, int num)
  401. {
  402. u16 *start;
  403. int i;
  404. start = (u16 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
  405. for (i = 0; i < num; i++)
  406. *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
  407. }
  408. static void fsi_dma_soft_push32(struct fsi_priv *fsi, int num)
  409. {
  410. u32 *start;
  411. int i;
  412. start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_PLAYBACK);
  413. for (i = 0; i < num; i++)
  414. fsi_reg_write(fsi, DODT, *(start + i));
  415. }
  416. static void fsi_dma_soft_pop32(struct fsi_priv *fsi, int num)
  417. {
  418. u32 *start;
  419. int i;
  420. start = (u32 *)fsi_dma_get_area(fsi, SNDRV_PCM_STREAM_CAPTURE);
  421. for (i = 0; i < num; i++)
  422. *(start + i) = fsi_reg_read(fsi, DIDT);
  423. }
  424. /*
  425. * irq function
  426. */
  427. static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
  428. {
  429. u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
  430. struct fsi_master *master = fsi_get_master(fsi);
  431. fsi_core_mask_set(master, imsk, data, data);
  432. fsi_core_mask_set(master, iemsk, data, data);
  433. }
  434. static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
  435. {
  436. u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
  437. struct fsi_master *master = fsi_get_master(fsi);
  438. fsi_core_mask_set(master, imsk, data, 0);
  439. fsi_core_mask_set(master, iemsk, data, 0);
  440. }
  441. static u32 fsi_irq_get_status(struct fsi_master *master)
  442. {
  443. return fsi_core_read(master, int_st);
  444. }
  445. static void fsi_irq_clear_status(struct fsi_priv *fsi)
  446. {
  447. u32 data = 0;
  448. struct fsi_master *master = fsi_get_master(fsi);
  449. data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
  450. data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
  451. /* clear interrupt factor */
  452. fsi_core_mask_set(master, int_st, data, 0);
  453. }
  454. /*
  455. * SPDIF master clock function
  456. *
  457. * These functions are used later FSI2
  458. */
  459. static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
  460. {
  461. struct fsi_master *master = fsi_get_master(fsi);
  462. u32 mask, val;
  463. if (master->core->ver < 2) {
  464. pr_err("fsi: register access err (%s)\n", __func__);
  465. return;
  466. }
  467. mask = BP | SE;
  468. val = enable ? mask : 0;
  469. fsi_is_port_a(fsi) ?
  470. fsi_core_mask_set(master, a_mclk, mask, val) :
  471. fsi_core_mask_set(master, b_mclk, mask, val);
  472. }
  473. /*
  474. * clock function
  475. */
  476. static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
  477. long rate, int enable)
  478. {
  479. struct fsi_master *master = fsi_get_master(fsi);
  480. set_rate_func set_rate = fsi_get_info_set_rate(master);
  481. int fsi_ver = master->core->ver;
  482. int ret;
  483. ret = set_rate(dev, fsi_is_port_a(fsi), rate, enable);
  484. if (ret < 0) /* error */
  485. return ret;
  486. if (!enable)
  487. return 0;
  488. if (ret > 0) {
  489. u32 data = 0;
  490. switch (ret & SH_FSI_ACKMD_MASK) {
  491. default:
  492. /* FALL THROUGH */
  493. case SH_FSI_ACKMD_512:
  494. data |= (0x0 << 12);
  495. break;
  496. case SH_FSI_ACKMD_256:
  497. data |= (0x1 << 12);
  498. break;
  499. case SH_FSI_ACKMD_128:
  500. data |= (0x2 << 12);
  501. break;
  502. case SH_FSI_ACKMD_64:
  503. data |= (0x3 << 12);
  504. break;
  505. case SH_FSI_ACKMD_32:
  506. if (fsi_ver < 2)
  507. dev_err(dev, "unsupported ACKMD\n");
  508. else
  509. data |= (0x4 << 12);
  510. break;
  511. }
  512. switch (ret & SH_FSI_BPFMD_MASK) {
  513. default:
  514. /* FALL THROUGH */
  515. case SH_FSI_BPFMD_32:
  516. data |= (0x0 << 8);
  517. break;
  518. case SH_FSI_BPFMD_64:
  519. data |= (0x1 << 8);
  520. break;
  521. case SH_FSI_BPFMD_128:
  522. data |= (0x2 << 8);
  523. break;
  524. case SH_FSI_BPFMD_256:
  525. data |= (0x3 << 8);
  526. break;
  527. case SH_FSI_BPFMD_512:
  528. data |= (0x4 << 8);
  529. break;
  530. case SH_FSI_BPFMD_16:
  531. if (fsi_ver < 2)
  532. dev_err(dev, "unsupported ACKMD\n");
  533. else
  534. data |= (0x7 << 8);
  535. break;
  536. }
  537. fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
  538. udelay(10);
  539. ret = 0;
  540. }
  541. return ret;
  542. }
  543. #define fsi_port_start(f, i) __fsi_port_clk_ctrl(f, i, 1)
  544. #define fsi_port_stop(f, i) __fsi_port_clk_ctrl(f, i, 0)
  545. static void __fsi_port_clk_ctrl(struct fsi_priv *fsi, int is_play, int enable)
  546. {
  547. struct fsi_master *master = fsi_get_master(fsi);
  548. u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
  549. if (enable)
  550. fsi_irq_enable(fsi, is_play);
  551. else
  552. fsi_irq_disable(fsi, is_play);
  553. if (fsi_is_clk_master(fsi))
  554. fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
  555. }
  556. /*
  557. * ctrl function
  558. */
  559. static void fsi_fifo_init(struct fsi_priv *fsi,
  560. int is_play,
  561. struct device *dev)
  562. {
  563. struct fsi_master *master = fsi_get_master(fsi);
  564. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  565. u32 shift, i;
  566. int frame_capa;
  567. /* get on-chip RAM capacity */
  568. shift = fsi_master_read(master, FIFO_SZ);
  569. shift >>= fsi_get_port_shift(fsi, is_play);
  570. shift &= FIFO_SZ_MASK;
  571. frame_capa = 256 << shift;
  572. dev_dbg(dev, "fifo = %d words\n", frame_capa);
  573. /*
  574. * The maximum number of sample data varies depending
  575. * on the number of channels selected for the format.
  576. *
  577. * FIFOs are used in 4-channel units in 3-channel mode
  578. * and in 8-channel units in 5- to 7-channel mode
  579. * meaning that more FIFOs than the required size of DPRAM
  580. * are used.
  581. *
  582. * ex) if 256 words of DP-RAM is connected
  583. * 1 channel: 256 (256 x 1 = 256)
  584. * 2 channels: 128 (128 x 2 = 256)
  585. * 3 channels: 64 ( 64 x 3 = 192)
  586. * 4 channels: 64 ( 64 x 4 = 256)
  587. * 5 channels: 32 ( 32 x 5 = 160)
  588. * 6 channels: 32 ( 32 x 6 = 192)
  589. * 7 channels: 32 ( 32 x 7 = 224)
  590. * 8 channels: 32 ( 32 x 8 = 256)
  591. */
  592. for (i = 1; i < fsi->chan_num; i <<= 1)
  593. frame_capa >>= 1;
  594. dev_dbg(dev, "%d channel %d store\n",
  595. fsi->chan_num, frame_capa);
  596. io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
  597. /*
  598. * set interrupt generation factor
  599. * clear FIFO
  600. */
  601. if (is_play) {
  602. fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
  603. fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
  604. } else {
  605. fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
  606. fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
  607. }
  608. }
  609. static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, int stream)
  610. {
  611. struct snd_pcm_runtime *runtime;
  612. struct snd_pcm_substream *substream = NULL;
  613. int is_play = fsi_stream_is_play(stream);
  614. struct fsi_stream *io = fsi_get_stream(fsi, is_play);
  615. int sample_residues;
  616. int sample_width;
  617. int samples;
  618. int samples_max;
  619. int over_period;
  620. void (*fn)(struct fsi_priv *fsi, int size);
  621. if (!fsi ||
  622. !io->substream ||
  623. !io->substream->runtime)
  624. return -EINVAL;
  625. over_period = 0;
  626. substream = io->substream;
  627. runtime = substream->runtime;
  628. /* FSI FIFO has limit.
  629. * So, this driver can not send periods data at a time
  630. */
  631. if (io->buff_sample_pos >=
  632. io->period_samples * (io->period_pos + 1)) {
  633. over_period = 1;
  634. io->period_pos = (io->period_pos + 1) % runtime->periods;
  635. if (0 == io->period_pos)
  636. io->buff_sample_pos = 0;
  637. }
  638. /* get 1 sample data width */
  639. sample_width = samples_to_bytes(runtime, 1);
  640. /* get number of residue samples */
  641. sample_residues = io->buff_sample_capa - io->buff_sample_pos;
  642. if (is_play) {
  643. /*
  644. * for play-back
  645. *
  646. * samples_max : number of FSI fifo free samples space
  647. * samples : number of ALSA residue samples
  648. */
  649. samples_max = io->fifo_sample_capa;
  650. samples_max -= fsi_get_current_fifo_samples(fsi, is_play);
  651. samples = sample_residues;
  652. switch (sample_width) {
  653. case 2:
  654. fn = fsi_dma_soft_push16;
  655. break;
  656. case 4:
  657. fn = fsi_dma_soft_push32;
  658. break;
  659. default:
  660. return -EINVAL;
  661. }
  662. } else {
  663. /*
  664. * for capture
  665. *
  666. * samples_max : number of ALSA free samples space
  667. * samples : number of samples in FSI fifo
  668. */
  669. samples_max = sample_residues;
  670. samples = fsi_get_current_fifo_samples(fsi, is_play);
  671. switch (sample_width) {
  672. case 2:
  673. fn = fsi_dma_soft_pop16;
  674. break;
  675. case 4:
  676. fn = fsi_dma_soft_pop32;
  677. break;
  678. default:
  679. return -EINVAL;
  680. }
  681. }
  682. samples = min(samples, samples_max);
  683. fn(fsi, samples);
  684. /* update buff_sample_pos */
  685. io->buff_sample_pos += samples;
  686. if (over_period)
  687. snd_pcm_period_elapsed(substream);
  688. return 0;
  689. }
  690. static int fsi_data_pop(struct fsi_priv *fsi)
  691. {
  692. return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_CAPTURE);
  693. }
  694. static int fsi_data_push(struct fsi_priv *fsi)
  695. {
  696. return fsi_fifo_data_ctrl(fsi, SNDRV_PCM_STREAM_PLAYBACK);
  697. }
  698. static irqreturn_t fsi_interrupt(int irq, void *data)
  699. {
  700. struct fsi_master *master = data;
  701. u32 int_st = fsi_irq_get_status(master);
  702. /* clear irq status */
  703. fsi_master_mask_set(master, SOFT_RST, IR, 0);
  704. fsi_master_mask_set(master, SOFT_RST, IR, IR);
  705. if (int_st & AB_IO(1, AO_SHIFT))
  706. fsi_data_push(&master->fsia);
  707. if (int_st & AB_IO(1, BO_SHIFT))
  708. fsi_data_push(&master->fsib);
  709. if (int_st & AB_IO(1, AI_SHIFT))
  710. fsi_data_pop(&master->fsia);
  711. if (int_st & AB_IO(1, BI_SHIFT))
  712. fsi_data_pop(&master->fsib);
  713. fsi_count_fifo_err(&master->fsia);
  714. fsi_count_fifo_err(&master->fsib);
  715. fsi_irq_clear_status(&master->fsia);
  716. fsi_irq_clear_status(&master->fsib);
  717. return IRQ_HANDLED;
  718. }
  719. /*
  720. * dai ops
  721. */
  722. static int fsi_hw_startup(struct fsi_priv *fsi,
  723. int is_play,
  724. struct device *dev)
  725. {
  726. struct fsi_master *master = fsi_get_master(fsi);
  727. int fsi_ver = master->core->ver;
  728. u32 flags = fsi_get_info_flags(fsi);
  729. u32 data = 0;
  730. /* clock setting */
  731. if (fsi_is_clk_master(fsi))
  732. data = DIMD | DOMD;
  733. fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
  734. /* clock inversion (CKG2) */
  735. data = 0;
  736. if (SH_FSI_LRM_INV & flags)
  737. data |= 1 << 12;
  738. if (SH_FSI_BRM_INV & flags)
  739. data |= 1 << 8;
  740. if (SH_FSI_LRS_INV & flags)
  741. data |= 1 << 4;
  742. if (SH_FSI_BRS_INV & flags)
  743. data |= 1 << 0;
  744. fsi_reg_write(fsi, CKG2, data);
  745. /* set format */
  746. fsi_reg_write(fsi, DO_FMT, fsi->do_fmt);
  747. fsi_reg_write(fsi, DI_FMT, fsi->di_fmt);
  748. /* spdif ? */
  749. if (fsi_is_spdif(fsi)) {
  750. fsi_spdif_clk_ctrl(fsi, 1);
  751. fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
  752. }
  753. /*
  754. * FIXME
  755. *
  756. * FSI driver assumed that data package is in-back.
  757. * FSI2 chip can select it.
  758. */
  759. if (fsi_ver >= 2) {
  760. fsi_reg_write(fsi, OUT_DMAC, (1 << 4));
  761. fsi_reg_write(fsi, IN_DMAC, (1 << 4));
  762. }
  763. /* irq clear */
  764. fsi_irq_disable(fsi, is_play);
  765. fsi_irq_clear_status(fsi);
  766. /* fifo init */
  767. fsi_fifo_init(fsi, is_play, dev);
  768. return 0;
  769. }
  770. static void fsi_hw_shutdown(struct fsi_priv *fsi,
  771. int is_play,
  772. struct device *dev)
  773. {
  774. if (fsi_is_clk_master(fsi))
  775. fsi_set_master_clk(dev, fsi, fsi->rate, 0);
  776. }
  777. static int fsi_dai_startup(struct snd_pcm_substream *substream,
  778. struct snd_soc_dai *dai)
  779. {
  780. struct fsi_priv *fsi = fsi_get_priv(substream);
  781. int is_play = fsi_is_play(substream);
  782. return fsi_hw_startup(fsi, is_play, dai->dev);
  783. }
  784. static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
  785. struct snd_soc_dai *dai)
  786. {
  787. struct fsi_priv *fsi = fsi_get_priv(substream);
  788. int is_play = fsi_is_play(substream);
  789. fsi_hw_shutdown(fsi, is_play, dai->dev);
  790. fsi->rate = 0;
  791. }
  792. static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  793. struct snd_soc_dai *dai)
  794. {
  795. struct fsi_priv *fsi = fsi_get_priv(substream);
  796. int is_play = fsi_is_play(substream);
  797. int ret = 0;
  798. switch (cmd) {
  799. case SNDRV_PCM_TRIGGER_START:
  800. fsi_stream_push(fsi, is_play, substream);
  801. ret = is_play ? fsi_data_push(fsi) : fsi_data_pop(fsi);
  802. fsi_port_start(fsi, is_play);
  803. break;
  804. case SNDRV_PCM_TRIGGER_STOP:
  805. fsi_port_stop(fsi, is_play);
  806. fsi_stream_pop(fsi, is_play);
  807. break;
  808. }
  809. return ret;
  810. }
  811. static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
  812. {
  813. u32 data = 0;
  814. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  815. case SND_SOC_DAIFMT_I2S:
  816. data = CR_I2S;
  817. fsi->chan_num = 2;
  818. break;
  819. case SND_SOC_DAIFMT_LEFT_J:
  820. data = CR_PCM;
  821. fsi->chan_num = 2;
  822. break;
  823. default:
  824. return -EINVAL;
  825. }
  826. fsi->do_fmt = data;
  827. fsi->di_fmt = data;
  828. return 0;
  829. }
  830. static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
  831. {
  832. struct fsi_master *master = fsi_get_master(fsi);
  833. u32 data = 0;
  834. if (master->core->ver < 2)
  835. return -EINVAL;
  836. data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
  837. fsi->chan_num = 2;
  838. fsi->spdif = 1;
  839. fsi->do_fmt = data;
  840. fsi->di_fmt = data;
  841. return 0;
  842. }
  843. static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  844. {
  845. struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
  846. struct fsi_master *master = fsi_get_master(fsi);
  847. set_rate_func set_rate = fsi_get_info_set_rate(master);
  848. u32 flags = fsi_get_info_flags(fsi);
  849. int ret;
  850. /* set master/slave audio interface */
  851. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  852. case SND_SOC_DAIFMT_CBM_CFM:
  853. fsi->clk_master = 1;
  854. break;
  855. case SND_SOC_DAIFMT_CBS_CFS:
  856. break;
  857. default:
  858. return -EINVAL;
  859. }
  860. if (fsi_is_clk_master(fsi) && !set_rate) {
  861. dev_err(dai->dev, "platform doesn't have set_rate\n");
  862. return -EINVAL;
  863. }
  864. /* set format */
  865. switch (flags & SH_FSI_FMT_MASK) {
  866. case SH_FSI_FMT_DAI:
  867. ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
  868. break;
  869. case SH_FSI_FMT_SPDIF:
  870. ret = fsi_set_fmt_spdif(fsi);
  871. break;
  872. default:
  873. ret = -EINVAL;
  874. }
  875. return ret;
  876. }
  877. static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
  878. struct snd_pcm_hw_params *params,
  879. struct snd_soc_dai *dai)
  880. {
  881. struct fsi_priv *fsi = fsi_get_priv(substream);
  882. long rate = params_rate(params);
  883. int ret;
  884. if (!fsi_is_clk_master(fsi))
  885. return 0;
  886. ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
  887. if (ret < 0)
  888. return ret;
  889. fsi->rate = rate;
  890. return ret;
  891. }
  892. static const struct snd_soc_dai_ops fsi_dai_ops = {
  893. .startup = fsi_dai_startup,
  894. .shutdown = fsi_dai_shutdown,
  895. .trigger = fsi_dai_trigger,
  896. .set_fmt = fsi_dai_set_fmt,
  897. .hw_params = fsi_dai_hw_params,
  898. };
  899. /*
  900. * pcm ops
  901. */
  902. static struct snd_pcm_hardware fsi_pcm_hardware = {
  903. .info = SNDRV_PCM_INFO_INTERLEAVED |
  904. SNDRV_PCM_INFO_MMAP |
  905. SNDRV_PCM_INFO_MMAP_VALID |
  906. SNDRV_PCM_INFO_PAUSE,
  907. .formats = FSI_FMTS,
  908. .rates = FSI_RATES,
  909. .rate_min = 8000,
  910. .rate_max = 192000,
  911. .channels_min = 1,
  912. .channels_max = 2,
  913. .buffer_bytes_max = 64 * 1024,
  914. .period_bytes_min = 32,
  915. .period_bytes_max = 8192,
  916. .periods_min = 1,
  917. .periods_max = 32,
  918. .fifo_size = 256,
  919. };
  920. static int fsi_pcm_open(struct snd_pcm_substream *substream)
  921. {
  922. struct snd_pcm_runtime *runtime = substream->runtime;
  923. int ret = 0;
  924. snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
  925. ret = snd_pcm_hw_constraint_integer(runtime,
  926. SNDRV_PCM_HW_PARAM_PERIODS);
  927. return ret;
  928. }
  929. static int fsi_hw_params(struct snd_pcm_substream *substream,
  930. struct snd_pcm_hw_params *hw_params)
  931. {
  932. return snd_pcm_lib_malloc_pages(substream,
  933. params_buffer_bytes(hw_params));
  934. }
  935. static int fsi_hw_free(struct snd_pcm_substream *substream)
  936. {
  937. return snd_pcm_lib_free_pages(substream);
  938. }
  939. static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
  940. {
  941. struct fsi_priv *fsi = fsi_get_priv(substream);
  942. struct fsi_stream *io = fsi_get_stream(fsi, fsi_is_play(substream));
  943. int samples_pos = io->buff_sample_pos - 1;
  944. if (samples_pos < 0)
  945. samples_pos = 0;
  946. return fsi_sample2frame(fsi, samples_pos);
  947. }
  948. static struct snd_pcm_ops fsi_pcm_ops = {
  949. .open = fsi_pcm_open,
  950. .ioctl = snd_pcm_lib_ioctl,
  951. .hw_params = fsi_hw_params,
  952. .hw_free = fsi_hw_free,
  953. .pointer = fsi_pointer,
  954. };
  955. /*
  956. * snd_soc_platform
  957. */
  958. #define PREALLOC_BUFFER (32 * 1024)
  959. #define PREALLOC_BUFFER_MAX (32 * 1024)
  960. static void fsi_pcm_free(struct snd_pcm *pcm)
  961. {
  962. snd_pcm_lib_preallocate_free_for_all(pcm);
  963. }
  964. static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
  965. {
  966. struct snd_pcm *pcm = rtd->pcm;
  967. /*
  968. * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
  969. * in MMAP mode (i.e. aplay -M)
  970. */
  971. return snd_pcm_lib_preallocate_pages_for_all(
  972. pcm,
  973. SNDRV_DMA_TYPE_CONTINUOUS,
  974. snd_dma_continuous_data(GFP_KERNEL),
  975. PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
  976. }
  977. /*
  978. * alsa struct
  979. */
  980. static struct snd_soc_dai_driver fsi_soc_dai[] = {
  981. {
  982. .name = "fsia-dai",
  983. .playback = {
  984. .rates = FSI_RATES,
  985. .formats = FSI_FMTS,
  986. .channels_min = 1,
  987. .channels_max = 8,
  988. },
  989. .capture = {
  990. .rates = FSI_RATES,
  991. .formats = FSI_FMTS,
  992. .channels_min = 1,
  993. .channels_max = 8,
  994. },
  995. .ops = &fsi_dai_ops,
  996. },
  997. {
  998. .name = "fsib-dai",
  999. .playback = {
  1000. .rates = FSI_RATES,
  1001. .formats = FSI_FMTS,
  1002. .channels_min = 1,
  1003. .channels_max = 8,
  1004. },
  1005. .capture = {
  1006. .rates = FSI_RATES,
  1007. .formats = FSI_FMTS,
  1008. .channels_min = 1,
  1009. .channels_max = 8,
  1010. },
  1011. .ops = &fsi_dai_ops,
  1012. },
  1013. };
  1014. static struct snd_soc_platform_driver fsi_soc_platform = {
  1015. .ops = &fsi_pcm_ops,
  1016. .pcm_new = fsi_pcm_new,
  1017. .pcm_free = fsi_pcm_free,
  1018. };
  1019. /*
  1020. * platform function
  1021. */
  1022. static int fsi_probe(struct platform_device *pdev)
  1023. {
  1024. struct fsi_master *master;
  1025. const struct platform_device_id *id_entry;
  1026. struct resource *res;
  1027. unsigned int irq;
  1028. int ret;
  1029. id_entry = pdev->id_entry;
  1030. if (!id_entry) {
  1031. dev_err(&pdev->dev, "unknown fsi device\n");
  1032. return -ENODEV;
  1033. }
  1034. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1035. irq = platform_get_irq(pdev, 0);
  1036. if (!res || (int)irq <= 0) {
  1037. dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
  1038. ret = -ENODEV;
  1039. goto exit;
  1040. }
  1041. master = kzalloc(sizeof(*master), GFP_KERNEL);
  1042. if (!master) {
  1043. dev_err(&pdev->dev, "Could not allocate master\n");
  1044. ret = -ENOMEM;
  1045. goto exit;
  1046. }
  1047. master->base = ioremap_nocache(res->start, resource_size(res));
  1048. if (!master->base) {
  1049. ret = -ENXIO;
  1050. dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
  1051. goto exit_kfree;
  1052. }
  1053. /* master setting */
  1054. master->irq = irq;
  1055. master->info = pdev->dev.platform_data;
  1056. master->core = (struct fsi_core *)id_entry->driver_data;
  1057. spin_lock_init(&master->lock);
  1058. /* FSI A setting */
  1059. master->fsia.base = master->base;
  1060. master->fsia.master = master;
  1061. /* FSI B setting */
  1062. master->fsib.base = master->base + 0x40;
  1063. master->fsib.master = master;
  1064. pm_runtime_enable(&pdev->dev);
  1065. dev_set_drvdata(&pdev->dev, master);
  1066. ret = request_irq(irq, &fsi_interrupt, 0,
  1067. id_entry->name, master);
  1068. if (ret) {
  1069. dev_err(&pdev->dev, "irq request err\n");
  1070. goto exit_iounmap;
  1071. }
  1072. ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
  1073. if (ret < 0) {
  1074. dev_err(&pdev->dev, "cannot snd soc register\n");
  1075. goto exit_free_irq;
  1076. }
  1077. ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
  1078. ARRAY_SIZE(fsi_soc_dai));
  1079. if (ret < 0) {
  1080. dev_err(&pdev->dev, "cannot snd dai register\n");
  1081. goto exit_snd_soc;
  1082. }
  1083. return ret;
  1084. exit_snd_soc:
  1085. snd_soc_unregister_platform(&pdev->dev);
  1086. exit_free_irq:
  1087. free_irq(irq, master);
  1088. exit_iounmap:
  1089. iounmap(master->base);
  1090. pm_runtime_disable(&pdev->dev);
  1091. exit_kfree:
  1092. kfree(master);
  1093. master = NULL;
  1094. exit:
  1095. return ret;
  1096. }
  1097. static int fsi_remove(struct platform_device *pdev)
  1098. {
  1099. struct fsi_master *master;
  1100. master = dev_get_drvdata(&pdev->dev);
  1101. free_irq(master->irq, master);
  1102. pm_runtime_disable(&pdev->dev);
  1103. snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
  1104. snd_soc_unregister_platform(&pdev->dev);
  1105. iounmap(master->base);
  1106. kfree(master);
  1107. return 0;
  1108. }
  1109. static void __fsi_suspend(struct fsi_priv *fsi,
  1110. int is_play,
  1111. struct device *dev)
  1112. {
  1113. if (!fsi_stream_is_working(fsi, is_play))
  1114. return;
  1115. fsi_port_stop(fsi, is_play);
  1116. fsi_hw_shutdown(fsi, is_play, dev);
  1117. }
  1118. static void __fsi_resume(struct fsi_priv *fsi,
  1119. int is_play,
  1120. struct device *dev)
  1121. {
  1122. if (!fsi_stream_is_working(fsi, is_play))
  1123. return;
  1124. fsi_hw_startup(fsi, is_play, dev);
  1125. if (fsi_is_clk_master(fsi) && fsi->rate)
  1126. fsi_set_master_clk(dev, fsi, fsi->rate, 1);
  1127. fsi_port_start(fsi, is_play);
  1128. }
  1129. static int fsi_suspend(struct device *dev)
  1130. {
  1131. struct fsi_master *master = dev_get_drvdata(dev);
  1132. struct fsi_priv *fsia = &master->fsia;
  1133. struct fsi_priv *fsib = &master->fsib;
  1134. __fsi_suspend(fsia, 1, dev);
  1135. __fsi_suspend(fsia, 0, dev);
  1136. __fsi_suspend(fsib, 1, dev);
  1137. __fsi_suspend(fsib, 0, dev);
  1138. return 0;
  1139. }
  1140. static int fsi_resume(struct device *dev)
  1141. {
  1142. struct fsi_master *master = dev_get_drvdata(dev);
  1143. struct fsi_priv *fsia = &master->fsia;
  1144. struct fsi_priv *fsib = &master->fsib;
  1145. __fsi_resume(fsia, 1, dev);
  1146. __fsi_resume(fsia, 0, dev);
  1147. __fsi_resume(fsib, 1, dev);
  1148. __fsi_resume(fsib, 0, dev);
  1149. return 0;
  1150. }
  1151. static int fsi_runtime_nop(struct device *dev)
  1152. {
  1153. /* Runtime PM callback shared between ->runtime_suspend()
  1154. * and ->runtime_resume(). Simply returns success.
  1155. *
  1156. * This driver re-initializes all registers after
  1157. * pm_runtime_get_sync() anyway so there is no need
  1158. * to save and restore registers here.
  1159. */
  1160. return 0;
  1161. }
  1162. static struct dev_pm_ops fsi_pm_ops = {
  1163. .suspend = fsi_suspend,
  1164. .resume = fsi_resume,
  1165. .runtime_suspend = fsi_runtime_nop,
  1166. .runtime_resume = fsi_runtime_nop,
  1167. };
  1168. static struct fsi_core fsi1_core = {
  1169. .ver = 1,
  1170. /* Interrupt */
  1171. .int_st = INT_ST,
  1172. .iemsk = IEMSK,
  1173. .imsk = IMSK,
  1174. };
  1175. static struct fsi_core fsi2_core = {
  1176. .ver = 2,
  1177. /* Interrupt */
  1178. .int_st = CPU_INT_ST,
  1179. .iemsk = CPU_IEMSK,
  1180. .imsk = CPU_IMSK,
  1181. .a_mclk = A_MST_CTLR,
  1182. .b_mclk = B_MST_CTLR,
  1183. };
  1184. static struct platform_device_id fsi_id_table[] = {
  1185. { "sh_fsi", (kernel_ulong_t)&fsi1_core },
  1186. { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
  1187. {},
  1188. };
  1189. MODULE_DEVICE_TABLE(platform, fsi_id_table);
  1190. static struct platform_driver fsi_driver = {
  1191. .driver = {
  1192. .name = "fsi-pcm-audio",
  1193. .pm = &fsi_pm_ops,
  1194. },
  1195. .probe = fsi_probe,
  1196. .remove = fsi_remove,
  1197. .id_table = fsi_id_table,
  1198. };
  1199. module_platform_driver(fsi_driver);
  1200. MODULE_LICENSE("GPL");
  1201. MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
  1202. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
  1203. MODULE_ALIAS("platform:fsi-pcm-audio");