fsi.c 34 KB

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