fsi.c 32 KB

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