fsi.c 32 KB

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