fsi.c 31 KB

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