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