fsi.c 31 KB

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