fsi.c 31 KB

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