fsi.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  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/dma-mapping.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/io.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/sh_dma.h>
  20. #include <linux/slab.h>
  21. #include <linux/module.h>
  22. #include <sound/soc.h>
  23. #include <sound/sh_fsi.h>
  24. /* PortA/PortB register */
  25. #define REG_DO_FMT 0x0000
  26. #define REG_DOFF_CTL 0x0004
  27. #define REG_DOFF_ST 0x0008
  28. #define REG_DI_FMT 0x000C
  29. #define REG_DIFF_CTL 0x0010
  30. #define REG_DIFF_ST 0x0014
  31. #define REG_CKG1 0x0018
  32. #define REG_CKG2 0x001C
  33. #define REG_DIDT 0x0020
  34. #define REG_DODT 0x0024
  35. #define REG_MUTE_ST 0x0028
  36. #define REG_OUT_DMAC 0x002C
  37. #define REG_OUT_SEL 0x0030
  38. #define REG_IN_DMAC 0x0038
  39. /* master register */
  40. #define MST_CLK_RST 0x0210
  41. #define MST_SOFT_RST 0x0214
  42. #define MST_FIFO_SZ 0x0218
  43. /* core register (depend on FSI version) */
  44. #define A_MST_CTLR 0x0180
  45. #define B_MST_CTLR 0x01A0
  46. #define CPU_INT_ST 0x01F4
  47. #define CPU_IEMSK 0x01F8
  48. #define CPU_IMSK 0x01FC
  49. #define INT_ST 0x0200
  50. #define IEMSK 0x0204
  51. #define IMSK 0x0208
  52. /* DO_FMT */
  53. /* DI_FMT */
  54. #define CR_BWS_MASK (0x3 << 20) /* FSI2 */
  55. #define CR_BWS_24 (0x0 << 20) /* FSI2 */
  56. #define CR_BWS_16 (0x1 << 20) /* FSI2 */
  57. #define CR_BWS_20 (0x2 << 20) /* FSI2 */
  58. #define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
  59. #define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
  60. #define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
  61. #define CR_MONO (0x0 << 4)
  62. #define CR_MONO_D (0x1 << 4)
  63. #define CR_PCM (0x2 << 4)
  64. #define CR_I2S (0x3 << 4)
  65. #define CR_TDM (0x4 << 4)
  66. #define CR_TDM_D (0x5 << 4)
  67. /* OUT_DMAC */
  68. /* IN_DMAC */
  69. #define VDMD_MASK (0x3 << 4)
  70. #define VDMD_FRONT (0x0 << 4) /* Package in front */
  71. #define VDMD_BACK (0x1 << 4) /* Package in back */
  72. #define VDMD_STREAM (0x2 << 4) /* Stream mode(16bit * 2) */
  73. #define DMA_ON (0x1 << 0)
  74. /* DOFF_CTL */
  75. /* DIFF_CTL */
  76. #define IRQ_HALF 0x00100000
  77. #define FIFO_CLR 0x00000001
  78. /* DOFF_ST */
  79. #define ERR_OVER 0x00000010
  80. #define ERR_UNDER 0x00000001
  81. #define ST_ERR (ERR_OVER | ERR_UNDER)
  82. /* CKG1 */
  83. #define ACKMD_MASK 0x00007000
  84. #define BPFMD_MASK 0x00000700
  85. #define DIMD (1 << 4)
  86. #define DOMD (1 << 0)
  87. /* A/B MST_CTLR */
  88. #define BP (1 << 4) /* Fix the signal of Biphase output */
  89. #define SE (1 << 0) /* Fix the master clock */
  90. /* CLK_RST */
  91. #define CRB (1 << 4)
  92. #define CRA (1 << 0)
  93. /* IO SHIFT / MACRO */
  94. #define BI_SHIFT 12
  95. #define BO_SHIFT 8
  96. #define AI_SHIFT 4
  97. #define AO_SHIFT 0
  98. #define AB_IO(param, shift) (param << shift)
  99. /* SOFT_RST */
  100. #define PBSR (1 << 12) /* Port B Software Reset */
  101. #define PASR (1 << 8) /* Port A Software Reset */
  102. #define IR (1 << 4) /* Interrupt Reset */
  103. #define FSISR (1 << 0) /* Software Reset */
  104. /* OUT_SEL (FSI2) */
  105. #define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
  106. /* 1: Biphase and serial */
  107. /* FIFO_SZ */
  108. #define FIFO_SZ_MASK 0x7
  109. #define FSI_RATES SNDRV_PCM_RATE_8000_96000
  110. #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  111. typedef int (*set_rate_func)(struct device *dev, int rate, int enable);
  112. /*
  113. * bus options
  114. *
  115. * 0x000000BA
  116. *
  117. * A : sample widtht 16bit setting
  118. * B : sample widtht 24bit setting
  119. */
  120. #define SHIFT_16DATA 0
  121. #define SHIFT_24DATA 4
  122. #define PACKAGE_24BITBUS_BACK 0
  123. #define PACKAGE_24BITBUS_FRONT 1
  124. #define PACKAGE_16BITBUS_STREAM 2
  125. #define BUSOP_SET(s, a) ((a) << SHIFT_ ## s ## DATA)
  126. #define BUSOP_GET(s, a) (((a) >> SHIFT_ ## s ## DATA) & 0xF)
  127. /*
  128. * FSI driver use below type name for variable
  129. *
  130. * xxx_num : number of data
  131. * xxx_pos : position of data
  132. * xxx_capa : capacity of data
  133. */
  134. /*
  135. * period/frame/sample image
  136. *
  137. * ex) PCM (2ch)
  138. *
  139. * period pos period pos
  140. * [n] [n + 1]
  141. * |<-------------------- period--------------------->|
  142. * ==|============================================ ... =|==
  143. * | |
  144. * ||<----- frame ----->|<------ frame ----->| ... |
  145. * |+--------------------+--------------------+- ... |
  146. * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
  147. * |+--------------------+--------------------+- ... |
  148. * ==|============================================ ... =|==
  149. */
  150. /*
  151. * FSI FIFO image
  152. *
  153. * | |
  154. * | |
  155. * | [ sample ] |
  156. * | [ sample ] |
  157. * | [ sample ] |
  158. * | [ sample ] |
  159. * --> go to codecs
  160. */
  161. /*
  162. * struct
  163. */
  164. struct fsi_stream_handler;
  165. struct fsi_stream {
  166. /*
  167. * these are initialized by fsi_stream_init()
  168. */
  169. struct snd_pcm_substream *substream;
  170. int fifo_sample_capa; /* sample capacity of FSI FIFO */
  171. int buff_sample_capa; /* sample capacity of ALSA buffer */
  172. int buff_sample_pos; /* sample position of ALSA buffer */
  173. int period_samples; /* sample number / 1 period */
  174. int period_pos; /* current period position */
  175. int sample_width; /* sample width */
  176. int uerr_num;
  177. int oerr_num;
  178. /*
  179. * bus options
  180. */
  181. u32 bus_option;
  182. /*
  183. * thse are initialized by fsi_handler_init()
  184. */
  185. struct fsi_stream_handler *handler;
  186. struct fsi_priv *priv;
  187. /*
  188. * these are for DMAEngine
  189. */
  190. struct dma_chan *chan;
  191. struct sh_dmae_slave slave; /* see fsi_handler_init() */
  192. struct tasklet_struct tasklet;
  193. dma_addr_t dma;
  194. };
  195. struct fsi_priv {
  196. void __iomem *base;
  197. struct fsi_master *master;
  198. struct sh_fsi_port_info *info;
  199. struct fsi_stream playback;
  200. struct fsi_stream capture;
  201. u32 fmt;
  202. int chan_num:16;
  203. int clk_master:1;
  204. int spdif:1;
  205. long rate;
  206. };
  207. struct fsi_stream_handler {
  208. int (*init)(struct fsi_priv *fsi, struct fsi_stream *io);
  209. int (*quit)(struct fsi_priv *fsi, struct fsi_stream *io);
  210. int (*probe)(struct fsi_priv *fsi, struct fsi_stream *io);
  211. int (*transfer)(struct fsi_priv *fsi, struct fsi_stream *io);
  212. int (*remove)(struct fsi_priv *fsi, struct fsi_stream *io);
  213. void (*start_stop)(struct fsi_priv *fsi, struct fsi_stream *io,
  214. int enable);
  215. };
  216. #define fsi_stream_handler_call(io, func, args...) \
  217. (!(io) ? -ENODEV : \
  218. !((io)->handler->func) ? 0 : \
  219. (io)->handler->func(args))
  220. struct fsi_core {
  221. int ver;
  222. u32 int_st;
  223. u32 iemsk;
  224. u32 imsk;
  225. u32 a_mclk;
  226. u32 b_mclk;
  227. };
  228. struct fsi_master {
  229. void __iomem *base;
  230. int irq;
  231. struct fsi_priv fsia;
  232. struct fsi_priv fsib;
  233. struct fsi_core *core;
  234. spinlock_t lock;
  235. };
  236. static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
  237. /*
  238. * basic read write function
  239. */
  240. static void __fsi_reg_write(u32 __iomem *reg, u32 data)
  241. {
  242. /* valid data area is 24bit */
  243. data &= 0x00ffffff;
  244. __raw_writel(data, reg);
  245. }
  246. static u32 __fsi_reg_read(u32 __iomem *reg)
  247. {
  248. return __raw_readl(reg);
  249. }
  250. static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
  251. {
  252. u32 val = __fsi_reg_read(reg);
  253. val &= ~mask;
  254. val |= data & mask;
  255. __fsi_reg_write(reg, val);
  256. }
  257. #define fsi_reg_write(p, r, d)\
  258. __fsi_reg_write((p->base + REG_##r), d)
  259. #define fsi_reg_read(p, r)\
  260. __fsi_reg_read((p->base + REG_##r))
  261. #define fsi_reg_mask_set(p, r, m, d)\
  262. __fsi_reg_mask_set((p->base + REG_##r), m, d)
  263. #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
  264. #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
  265. static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
  266. {
  267. u32 ret;
  268. unsigned long flags;
  269. spin_lock_irqsave(&master->lock, flags);
  270. ret = __fsi_reg_read(master->base + reg);
  271. spin_unlock_irqrestore(&master->lock, flags);
  272. return ret;
  273. }
  274. #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
  275. #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
  276. static void _fsi_master_mask_set(struct fsi_master *master,
  277. u32 reg, u32 mask, u32 data)
  278. {
  279. unsigned long flags;
  280. spin_lock_irqsave(&master->lock, flags);
  281. __fsi_reg_mask_set(master->base + reg, mask, data);
  282. spin_unlock_irqrestore(&master->lock, flags);
  283. }
  284. /*
  285. * basic function
  286. */
  287. static int fsi_version(struct fsi_master *master)
  288. {
  289. return master->core->ver;
  290. }
  291. static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
  292. {
  293. return fsi->master;
  294. }
  295. static int fsi_is_clk_master(struct fsi_priv *fsi)
  296. {
  297. return fsi->clk_master;
  298. }
  299. static int fsi_is_port_a(struct fsi_priv *fsi)
  300. {
  301. return fsi->master->base == fsi->base;
  302. }
  303. static int fsi_is_spdif(struct fsi_priv *fsi)
  304. {
  305. return fsi->spdif;
  306. }
  307. static int fsi_is_play(struct snd_pcm_substream *substream)
  308. {
  309. return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  310. }
  311. static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
  312. {
  313. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  314. return rtd->cpu_dai;
  315. }
  316. static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
  317. {
  318. struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
  319. if (dai->id == 0)
  320. return &master->fsia;
  321. else
  322. return &master->fsib;
  323. }
  324. static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
  325. {
  326. return fsi_get_priv_frm_dai(fsi_get_dai(substream));
  327. }
  328. static set_rate_func fsi_get_info_set_rate(struct fsi_priv *fsi)
  329. {
  330. if (!fsi->info)
  331. return NULL;
  332. return fsi->info->set_rate;
  333. }
  334. static u32 fsi_get_info_flags(struct fsi_priv *fsi)
  335. {
  336. if (!fsi->info)
  337. return 0;
  338. return fsi->info->flags;
  339. }
  340. static u32 fsi_get_port_shift(struct fsi_priv *fsi, struct fsi_stream *io)
  341. {
  342. int is_play = fsi_stream_is_play(fsi, io);
  343. int is_porta = fsi_is_port_a(fsi);
  344. u32 shift;
  345. if (is_porta)
  346. shift = is_play ? AO_SHIFT : AI_SHIFT;
  347. else
  348. shift = is_play ? BO_SHIFT : BI_SHIFT;
  349. return shift;
  350. }
  351. static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
  352. {
  353. return frames * fsi->chan_num;
  354. }
  355. static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
  356. {
  357. return samples / fsi->chan_num;
  358. }
  359. static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
  360. struct fsi_stream *io)
  361. {
  362. int is_play = fsi_stream_is_play(fsi, io);
  363. u32 status;
  364. int frames;
  365. status = is_play ?
  366. fsi_reg_read(fsi, DOFF_ST) :
  367. fsi_reg_read(fsi, DIFF_ST);
  368. frames = 0x1ff & (status >> 8);
  369. return fsi_frame2sample(fsi, frames);
  370. }
  371. static void fsi_count_fifo_err(struct fsi_priv *fsi)
  372. {
  373. u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
  374. u32 istatus = fsi_reg_read(fsi, DIFF_ST);
  375. if (ostatus & ERR_OVER)
  376. fsi->playback.oerr_num++;
  377. if (ostatus & ERR_UNDER)
  378. fsi->playback.uerr_num++;
  379. if (istatus & ERR_OVER)
  380. fsi->capture.oerr_num++;
  381. if (istatus & ERR_UNDER)
  382. fsi->capture.uerr_num++;
  383. fsi_reg_write(fsi, DOFF_ST, 0);
  384. fsi_reg_write(fsi, DIFF_ST, 0);
  385. }
  386. /*
  387. * fsi_stream_xx() function
  388. */
  389. static inline int fsi_stream_is_play(struct fsi_priv *fsi,
  390. struct fsi_stream *io)
  391. {
  392. return &fsi->playback == io;
  393. }
  394. static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
  395. struct snd_pcm_substream *substream)
  396. {
  397. return fsi_is_play(substream) ? &fsi->playback : &fsi->capture;
  398. }
  399. static int fsi_stream_is_working(struct fsi_priv *fsi,
  400. struct fsi_stream *io)
  401. {
  402. struct fsi_master *master = fsi_get_master(fsi);
  403. unsigned long flags;
  404. int ret;
  405. spin_lock_irqsave(&master->lock, flags);
  406. ret = !!(io->substream && io->substream->runtime);
  407. spin_unlock_irqrestore(&master->lock, flags);
  408. return ret;
  409. }
  410. static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
  411. {
  412. return io->priv;
  413. }
  414. static void fsi_stream_init(struct fsi_priv *fsi,
  415. struct fsi_stream *io,
  416. struct snd_pcm_substream *substream)
  417. {
  418. struct snd_pcm_runtime *runtime = substream->runtime;
  419. struct fsi_master *master = fsi_get_master(fsi);
  420. unsigned long flags;
  421. spin_lock_irqsave(&master->lock, flags);
  422. io->substream = substream;
  423. io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
  424. io->buff_sample_pos = 0;
  425. io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
  426. io->period_pos = 0;
  427. io->sample_width = samples_to_bytes(runtime, 1);
  428. io->bus_option = 0;
  429. io->oerr_num = -1; /* ignore 1st err */
  430. io->uerr_num = -1; /* ignore 1st err */
  431. fsi_stream_handler_call(io, init, fsi, io);
  432. spin_unlock_irqrestore(&master->lock, flags);
  433. }
  434. static void fsi_stream_quit(struct fsi_priv *fsi, struct fsi_stream *io)
  435. {
  436. struct snd_soc_dai *dai = fsi_get_dai(io->substream);
  437. struct fsi_master *master = fsi_get_master(fsi);
  438. unsigned long flags;
  439. spin_lock_irqsave(&master->lock, flags);
  440. if (io->oerr_num > 0)
  441. dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
  442. if (io->uerr_num > 0)
  443. dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
  444. fsi_stream_handler_call(io, quit, fsi, io);
  445. io->substream = NULL;
  446. io->buff_sample_capa = 0;
  447. io->buff_sample_pos = 0;
  448. io->period_samples = 0;
  449. io->period_pos = 0;
  450. io->sample_width = 0;
  451. io->bus_option = 0;
  452. io->oerr_num = 0;
  453. io->uerr_num = 0;
  454. spin_unlock_irqrestore(&master->lock, flags);
  455. }
  456. static int fsi_stream_transfer(struct fsi_stream *io)
  457. {
  458. struct fsi_priv *fsi = fsi_stream_to_priv(io);
  459. if (!fsi)
  460. return -EIO;
  461. return fsi_stream_handler_call(io, transfer, fsi, io);
  462. }
  463. #define fsi_stream_start(fsi, io)\
  464. fsi_stream_handler_call(io, start_stop, fsi, io, 1)
  465. #define fsi_stream_stop(fsi, io)\
  466. fsi_stream_handler_call(io, start_stop, fsi, io, 0)
  467. static int fsi_stream_probe(struct fsi_priv *fsi)
  468. {
  469. struct fsi_stream *io;
  470. int ret1, ret2;
  471. io = &fsi->playback;
  472. ret1 = fsi_stream_handler_call(io, probe, fsi, io);
  473. io = &fsi->capture;
  474. ret2 = fsi_stream_handler_call(io, probe, fsi, io);
  475. if (ret1 < 0)
  476. return ret1;
  477. if (ret2 < 0)
  478. return ret2;
  479. return 0;
  480. }
  481. static int fsi_stream_remove(struct fsi_priv *fsi)
  482. {
  483. struct fsi_stream *io;
  484. int ret1, ret2;
  485. io = &fsi->playback;
  486. ret1 = fsi_stream_handler_call(io, remove, fsi, io);
  487. io = &fsi->capture;
  488. ret2 = fsi_stream_handler_call(io, remove, fsi, io);
  489. if (ret1 < 0)
  490. return ret1;
  491. if (ret2 < 0)
  492. return ret2;
  493. return 0;
  494. }
  495. /*
  496. * format/bus/dma setting
  497. */
  498. static void fsi_format_bus_setup(struct fsi_priv *fsi, struct fsi_stream *io,
  499. u32 bus, struct device *dev)
  500. {
  501. struct fsi_master *master = fsi_get_master(fsi);
  502. int is_play = fsi_stream_is_play(fsi, io);
  503. u32 fmt = fsi->fmt;
  504. if (fsi_version(master) >= 2) {
  505. u32 dma = 0;
  506. /*
  507. * FSI2 needs DMA/Bus setting
  508. */
  509. switch (bus) {
  510. case PACKAGE_24BITBUS_FRONT:
  511. fmt |= CR_BWS_24;
  512. dma |= VDMD_FRONT;
  513. dev_dbg(dev, "24bit bus / package in front\n");
  514. break;
  515. case PACKAGE_16BITBUS_STREAM:
  516. fmt |= CR_BWS_16;
  517. dma |= VDMD_STREAM;
  518. dev_dbg(dev, "16bit bus / stream mode\n");
  519. break;
  520. case PACKAGE_24BITBUS_BACK:
  521. default:
  522. fmt |= CR_BWS_24;
  523. dma |= VDMD_BACK;
  524. dev_dbg(dev, "24bit bus / package in back\n");
  525. break;
  526. }
  527. if (is_play)
  528. fsi_reg_write(fsi, OUT_DMAC, dma);
  529. else
  530. fsi_reg_write(fsi, IN_DMAC, dma);
  531. }
  532. if (is_play)
  533. fsi_reg_write(fsi, DO_FMT, fmt);
  534. else
  535. fsi_reg_write(fsi, DI_FMT, fmt);
  536. }
  537. /*
  538. * irq function
  539. */
  540. static void fsi_irq_enable(struct fsi_priv *fsi, struct fsi_stream *io)
  541. {
  542. u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
  543. struct fsi_master *master = fsi_get_master(fsi);
  544. fsi_core_mask_set(master, imsk, data, data);
  545. fsi_core_mask_set(master, iemsk, data, data);
  546. }
  547. static void fsi_irq_disable(struct fsi_priv *fsi, struct fsi_stream *io)
  548. {
  549. u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
  550. struct fsi_master *master = fsi_get_master(fsi);
  551. fsi_core_mask_set(master, imsk, data, 0);
  552. fsi_core_mask_set(master, iemsk, data, 0);
  553. }
  554. static u32 fsi_irq_get_status(struct fsi_master *master)
  555. {
  556. return fsi_core_read(master, int_st);
  557. }
  558. static void fsi_irq_clear_status(struct fsi_priv *fsi)
  559. {
  560. u32 data = 0;
  561. struct fsi_master *master = fsi_get_master(fsi);
  562. data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->playback));
  563. data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->capture));
  564. /* clear interrupt factor */
  565. fsi_core_mask_set(master, int_st, data, 0);
  566. }
  567. /*
  568. * SPDIF master clock function
  569. *
  570. * These functions are used later FSI2
  571. */
  572. static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
  573. {
  574. struct fsi_master *master = fsi_get_master(fsi);
  575. u32 mask, val;
  576. mask = BP | SE;
  577. val = enable ? mask : 0;
  578. fsi_is_port_a(fsi) ?
  579. fsi_core_mask_set(master, a_mclk, mask, val) :
  580. fsi_core_mask_set(master, b_mclk, mask, val);
  581. }
  582. /*
  583. * clock function
  584. */
  585. static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
  586. long rate, int enable)
  587. {
  588. set_rate_func set_rate = fsi_get_info_set_rate(fsi);
  589. int ret;
  590. if (!set_rate)
  591. return 0;
  592. ret = set_rate(dev, rate, enable);
  593. if (ret < 0) /* error */
  594. return ret;
  595. if (!enable)
  596. return 0;
  597. if (ret > 0) {
  598. u32 data = 0;
  599. switch (ret & SH_FSI_ACKMD_MASK) {
  600. default:
  601. /* FALL THROUGH */
  602. case SH_FSI_ACKMD_512:
  603. data |= (0x0 << 12);
  604. break;
  605. case SH_FSI_ACKMD_256:
  606. data |= (0x1 << 12);
  607. break;
  608. case SH_FSI_ACKMD_128:
  609. data |= (0x2 << 12);
  610. break;
  611. case SH_FSI_ACKMD_64:
  612. data |= (0x3 << 12);
  613. break;
  614. case SH_FSI_ACKMD_32:
  615. data |= (0x4 << 12);
  616. break;
  617. }
  618. switch (ret & SH_FSI_BPFMD_MASK) {
  619. default:
  620. /* FALL THROUGH */
  621. case SH_FSI_BPFMD_32:
  622. data |= (0x0 << 8);
  623. break;
  624. case SH_FSI_BPFMD_64:
  625. data |= (0x1 << 8);
  626. break;
  627. case SH_FSI_BPFMD_128:
  628. data |= (0x2 << 8);
  629. break;
  630. case SH_FSI_BPFMD_256:
  631. data |= (0x3 << 8);
  632. break;
  633. case SH_FSI_BPFMD_512:
  634. data |= (0x4 << 8);
  635. break;
  636. case SH_FSI_BPFMD_16:
  637. data |= (0x7 << 8);
  638. break;
  639. }
  640. fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
  641. udelay(10);
  642. ret = 0;
  643. }
  644. return ret;
  645. }
  646. /*
  647. * pio data transfer handler
  648. */
  649. static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
  650. {
  651. u32 enable_stream = fsi_get_info_flags(fsi) & SH_FSI_ENABLE_STREAM_MODE;
  652. int i;
  653. if (enable_stream) {
  654. /*
  655. * stream mode
  656. * see
  657. * fsi_pio_push_init()
  658. */
  659. u32 *buf = (u32 *)_buf;
  660. for (i = 0; i < samples / 2; i++)
  661. fsi_reg_write(fsi, DODT, buf[i]);
  662. } else {
  663. /* normal mode */
  664. u16 *buf = (u16 *)_buf;
  665. for (i = 0; i < samples; i++)
  666. fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
  667. }
  668. }
  669. static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
  670. {
  671. u16 *buf = (u16 *)_buf;
  672. int i;
  673. for (i = 0; i < samples; i++)
  674. *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
  675. }
  676. static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
  677. {
  678. u32 *buf = (u32 *)_buf;
  679. int i;
  680. for (i = 0; i < samples; i++)
  681. fsi_reg_write(fsi, DODT, *(buf + i));
  682. }
  683. static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
  684. {
  685. u32 *buf = (u32 *)_buf;
  686. int i;
  687. for (i = 0; i < samples; i++)
  688. *(buf + i) = fsi_reg_read(fsi, DIDT);
  689. }
  690. static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
  691. {
  692. struct snd_pcm_runtime *runtime = io->substream->runtime;
  693. return runtime->dma_area +
  694. samples_to_bytes(runtime, io->buff_sample_pos);
  695. }
  696. static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
  697. void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
  698. void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
  699. int samples)
  700. {
  701. struct snd_pcm_runtime *runtime;
  702. struct snd_pcm_substream *substream;
  703. u8 *buf;
  704. int over_period;
  705. if (!fsi_stream_is_working(fsi, io))
  706. return -EINVAL;
  707. over_period = 0;
  708. substream = io->substream;
  709. runtime = substream->runtime;
  710. /* FSI FIFO has limit.
  711. * So, this driver can not send periods data at a time
  712. */
  713. if (io->buff_sample_pos >=
  714. io->period_samples * (io->period_pos + 1)) {
  715. over_period = 1;
  716. io->period_pos = (io->period_pos + 1) % runtime->periods;
  717. if (0 == io->period_pos)
  718. io->buff_sample_pos = 0;
  719. }
  720. buf = fsi_pio_get_area(fsi, io);
  721. switch (io->sample_width) {
  722. case 2:
  723. run16(fsi, buf, samples);
  724. break;
  725. case 4:
  726. run32(fsi, buf, samples);
  727. break;
  728. default:
  729. return -EINVAL;
  730. }
  731. /* update buff_sample_pos */
  732. io->buff_sample_pos += samples;
  733. if (over_period)
  734. snd_pcm_period_elapsed(substream);
  735. return 0;
  736. }
  737. static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
  738. {
  739. int sample_residues; /* samples in FSI fifo */
  740. int sample_space; /* ALSA free samples space */
  741. int samples;
  742. sample_residues = fsi_get_current_fifo_samples(fsi, io);
  743. sample_space = io->buff_sample_capa - io->buff_sample_pos;
  744. samples = min(sample_residues, sample_space);
  745. return fsi_pio_transfer(fsi, io,
  746. fsi_pio_pop16,
  747. fsi_pio_pop32,
  748. samples);
  749. }
  750. static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
  751. {
  752. int sample_residues; /* ALSA residue samples */
  753. int sample_space; /* FSI fifo free samples space */
  754. int samples;
  755. sample_residues = io->buff_sample_capa - io->buff_sample_pos;
  756. sample_space = io->fifo_sample_capa -
  757. fsi_get_current_fifo_samples(fsi, io);
  758. samples = min(sample_residues, sample_space);
  759. return fsi_pio_transfer(fsi, io,
  760. fsi_pio_push16,
  761. fsi_pio_push32,
  762. samples);
  763. }
  764. static void fsi_pio_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
  765. int enable)
  766. {
  767. struct fsi_master *master = fsi_get_master(fsi);
  768. u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
  769. if (enable)
  770. fsi_irq_enable(fsi, io);
  771. else
  772. fsi_irq_disable(fsi, io);
  773. if (fsi_is_clk_master(fsi))
  774. fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
  775. }
  776. static int fsi_pio_push_init(struct fsi_priv *fsi, struct fsi_stream *io)
  777. {
  778. u32 enable_stream = fsi_get_info_flags(fsi) & SH_FSI_ENABLE_STREAM_MODE;
  779. /*
  780. * we can use 16bit stream mode
  781. * when "playback" and "16bit data"
  782. * and platform allows "stream mode"
  783. * see
  784. * fsi_pio_push16()
  785. */
  786. if (enable_stream)
  787. io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
  788. BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
  789. else
  790. io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
  791. BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
  792. return 0;
  793. }
  794. static int fsi_pio_pop_init(struct fsi_priv *fsi, struct fsi_stream *io)
  795. {
  796. /*
  797. * always 24bit bus, package back when "capture"
  798. */
  799. io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
  800. BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
  801. return 0;
  802. }
  803. static struct fsi_stream_handler fsi_pio_push_handler = {
  804. .init = fsi_pio_push_init,
  805. .transfer = fsi_pio_push,
  806. .start_stop = fsi_pio_start_stop,
  807. };
  808. static struct fsi_stream_handler fsi_pio_pop_handler = {
  809. .init = fsi_pio_pop_init,
  810. .transfer = fsi_pio_pop,
  811. .start_stop = fsi_pio_start_stop,
  812. };
  813. static irqreturn_t fsi_interrupt(int irq, void *data)
  814. {
  815. struct fsi_master *master = data;
  816. u32 int_st = fsi_irq_get_status(master);
  817. /* clear irq status */
  818. fsi_master_mask_set(master, SOFT_RST, IR, 0);
  819. fsi_master_mask_set(master, SOFT_RST, IR, IR);
  820. if (int_st & AB_IO(1, AO_SHIFT))
  821. fsi_stream_transfer(&master->fsia.playback);
  822. if (int_st & AB_IO(1, BO_SHIFT))
  823. fsi_stream_transfer(&master->fsib.playback);
  824. if (int_st & AB_IO(1, AI_SHIFT))
  825. fsi_stream_transfer(&master->fsia.capture);
  826. if (int_st & AB_IO(1, BI_SHIFT))
  827. fsi_stream_transfer(&master->fsib.capture);
  828. fsi_count_fifo_err(&master->fsia);
  829. fsi_count_fifo_err(&master->fsib);
  830. fsi_irq_clear_status(&master->fsia);
  831. fsi_irq_clear_status(&master->fsib);
  832. return IRQ_HANDLED;
  833. }
  834. /*
  835. * dma data transfer handler
  836. */
  837. static int fsi_dma_init(struct fsi_priv *fsi, struct fsi_stream *io)
  838. {
  839. struct snd_pcm_runtime *runtime = io->substream->runtime;
  840. struct snd_soc_dai *dai = fsi_get_dai(io->substream);
  841. enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
  842. DMA_TO_DEVICE : DMA_FROM_DEVICE;
  843. /*
  844. * 24bit data : 24bit bus / package in back
  845. * 16bit data : 16bit bus / stream mode
  846. */
  847. io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
  848. BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
  849. io->dma = dma_map_single(dai->dev, runtime->dma_area,
  850. snd_pcm_lib_buffer_bytes(io->substream), dir);
  851. return 0;
  852. }
  853. static int fsi_dma_quit(struct fsi_priv *fsi, struct fsi_stream *io)
  854. {
  855. struct snd_soc_dai *dai = fsi_get_dai(io->substream);
  856. enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
  857. DMA_TO_DEVICE : DMA_FROM_DEVICE;
  858. dma_unmap_single(dai->dev, io->dma,
  859. snd_pcm_lib_buffer_bytes(io->substream), dir);
  860. return 0;
  861. }
  862. static dma_addr_t fsi_dma_get_area(struct fsi_stream *io)
  863. {
  864. struct snd_pcm_runtime *runtime = io->substream->runtime;
  865. return io->dma + samples_to_bytes(runtime, io->buff_sample_pos);
  866. }
  867. static void fsi_dma_complete(void *data)
  868. {
  869. struct fsi_stream *io = (struct fsi_stream *)data;
  870. struct fsi_priv *fsi = fsi_stream_to_priv(io);
  871. struct snd_pcm_runtime *runtime = io->substream->runtime;
  872. struct snd_soc_dai *dai = fsi_get_dai(io->substream);
  873. enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
  874. DMA_TO_DEVICE : DMA_FROM_DEVICE;
  875. dma_sync_single_for_cpu(dai->dev, fsi_dma_get_area(io),
  876. samples_to_bytes(runtime, io->period_samples), dir);
  877. io->buff_sample_pos += io->period_samples;
  878. io->period_pos++;
  879. if (io->period_pos >= runtime->periods) {
  880. io->period_pos = 0;
  881. io->buff_sample_pos = 0;
  882. }
  883. fsi_count_fifo_err(fsi);
  884. fsi_stream_transfer(io);
  885. snd_pcm_period_elapsed(io->substream);
  886. }
  887. static void fsi_dma_do_tasklet(unsigned long data)
  888. {
  889. struct fsi_stream *io = (struct fsi_stream *)data;
  890. struct fsi_priv *fsi = fsi_stream_to_priv(io);
  891. struct dma_chan *chan;
  892. struct snd_soc_dai *dai;
  893. struct dma_async_tx_descriptor *desc;
  894. struct scatterlist sg;
  895. struct snd_pcm_runtime *runtime;
  896. enum dma_data_direction dir;
  897. dma_cookie_t cookie;
  898. int is_play = fsi_stream_is_play(fsi, io);
  899. int len;
  900. dma_addr_t buf;
  901. if (!fsi_stream_is_working(fsi, io))
  902. return;
  903. dai = fsi_get_dai(io->substream);
  904. chan = io->chan;
  905. runtime = io->substream->runtime;
  906. dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  907. len = samples_to_bytes(runtime, io->period_samples);
  908. buf = fsi_dma_get_area(io);
  909. dma_sync_single_for_device(dai->dev, buf, len, dir);
  910. sg_init_table(&sg, 1);
  911. sg_set_page(&sg, pfn_to_page(PFN_DOWN(buf)),
  912. len , offset_in_page(buf));
  913. sg_dma_address(&sg) = buf;
  914. sg_dma_len(&sg) = len;
  915. desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir,
  916. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  917. if (!desc) {
  918. dev_err(dai->dev, "dmaengine_prep_slave_sg() fail\n");
  919. return;
  920. }
  921. desc->callback = fsi_dma_complete;
  922. desc->callback_param = io;
  923. cookie = desc->tx_submit(desc);
  924. if (cookie < 0) {
  925. dev_err(dai->dev, "tx_submit() fail\n");
  926. return;
  927. }
  928. dma_async_issue_pending(chan);
  929. /*
  930. * FIXME
  931. *
  932. * In DMAEngine case, codec and FSI cannot be started simultaneously
  933. * since FSI is using tasklet.
  934. * Therefore, in capture case, probably FSI FIFO will have got
  935. * overflow error in this point.
  936. * in that case, DMA cannot start transfer until error was cleared.
  937. */
  938. if (!is_play) {
  939. if (ERR_OVER & fsi_reg_read(fsi, DIFF_ST)) {
  940. fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
  941. fsi_reg_write(fsi, DIFF_ST, 0);
  942. }
  943. }
  944. }
  945. static bool fsi_dma_filter(struct dma_chan *chan, void *param)
  946. {
  947. struct sh_dmae_slave *slave = param;
  948. chan->private = slave;
  949. return true;
  950. }
  951. static int fsi_dma_transfer(struct fsi_priv *fsi, struct fsi_stream *io)
  952. {
  953. tasklet_schedule(&io->tasklet);
  954. return 0;
  955. }
  956. static void fsi_dma_push_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
  957. int start)
  958. {
  959. struct fsi_master *master = fsi_get_master(fsi);
  960. u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
  961. u32 enable = start ? DMA_ON : 0;
  962. fsi_reg_mask_set(fsi, OUT_DMAC, DMA_ON, enable);
  963. dmaengine_terminate_all(io->chan);
  964. if (fsi_is_clk_master(fsi))
  965. fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
  966. }
  967. static int fsi_dma_probe(struct fsi_priv *fsi, struct fsi_stream *io)
  968. {
  969. dma_cap_mask_t mask;
  970. dma_cap_zero(mask);
  971. dma_cap_set(DMA_SLAVE, mask);
  972. io->chan = dma_request_channel(mask, fsi_dma_filter, &io->slave);
  973. if (!io->chan)
  974. return -EIO;
  975. tasklet_init(&io->tasklet, fsi_dma_do_tasklet, (unsigned long)io);
  976. return 0;
  977. }
  978. static int fsi_dma_remove(struct fsi_priv *fsi, struct fsi_stream *io)
  979. {
  980. tasklet_kill(&io->tasklet);
  981. fsi_stream_stop(fsi, io);
  982. if (io->chan)
  983. dma_release_channel(io->chan);
  984. io->chan = NULL;
  985. return 0;
  986. }
  987. static struct fsi_stream_handler fsi_dma_push_handler = {
  988. .init = fsi_dma_init,
  989. .quit = fsi_dma_quit,
  990. .probe = fsi_dma_probe,
  991. .transfer = fsi_dma_transfer,
  992. .remove = fsi_dma_remove,
  993. .start_stop = fsi_dma_push_start_stop,
  994. };
  995. /*
  996. * dai ops
  997. */
  998. static void fsi_fifo_init(struct fsi_priv *fsi,
  999. struct fsi_stream *io,
  1000. struct device *dev)
  1001. {
  1002. struct fsi_master *master = fsi_get_master(fsi);
  1003. int is_play = fsi_stream_is_play(fsi, io);
  1004. u32 shift, i;
  1005. int frame_capa;
  1006. /* get on-chip RAM capacity */
  1007. shift = fsi_master_read(master, FIFO_SZ);
  1008. shift >>= fsi_get_port_shift(fsi, io);
  1009. shift &= FIFO_SZ_MASK;
  1010. frame_capa = 256 << shift;
  1011. dev_dbg(dev, "fifo = %d words\n", frame_capa);
  1012. /*
  1013. * The maximum number of sample data varies depending
  1014. * on the number of channels selected for the format.
  1015. *
  1016. * FIFOs are used in 4-channel units in 3-channel mode
  1017. * and in 8-channel units in 5- to 7-channel mode
  1018. * meaning that more FIFOs than the required size of DPRAM
  1019. * are used.
  1020. *
  1021. * ex) if 256 words of DP-RAM is connected
  1022. * 1 channel: 256 (256 x 1 = 256)
  1023. * 2 channels: 128 (128 x 2 = 256)
  1024. * 3 channels: 64 ( 64 x 3 = 192)
  1025. * 4 channels: 64 ( 64 x 4 = 256)
  1026. * 5 channels: 32 ( 32 x 5 = 160)
  1027. * 6 channels: 32 ( 32 x 6 = 192)
  1028. * 7 channels: 32 ( 32 x 7 = 224)
  1029. * 8 channels: 32 ( 32 x 8 = 256)
  1030. */
  1031. for (i = 1; i < fsi->chan_num; i <<= 1)
  1032. frame_capa >>= 1;
  1033. dev_dbg(dev, "%d channel %d store\n",
  1034. fsi->chan_num, frame_capa);
  1035. io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
  1036. /*
  1037. * set interrupt generation factor
  1038. * clear FIFO
  1039. */
  1040. if (is_play) {
  1041. fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
  1042. fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
  1043. } else {
  1044. fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
  1045. fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
  1046. }
  1047. }
  1048. static int fsi_hw_startup(struct fsi_priv *fsi,
  1049. struct fsi_stream *io,
  1050. struct device *dev)
  1051. {
  1052. u32 flags = fsi_get_info_flags(fsi);
  1053. u32 data = 0;
  1054. /* clock setting */
  1055. if (fsi_is_clk_master(fsi))
  1056. data = DIMD | DOMD;
  1057. fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
  1058. /* clock inversion (CKG2) */
  1059. data = 0;
  1060. if (SH_FSI_LRM_INV & flags)
  1061. data |= 1 << 12;
  1062. if (SH_FSI_BRM_INV & flags)
  1063. data |= 1 << 8;
  1064. if (SH_FSI_LRS_INV & flags)
  1065. data |= 1 << 4;
  1066. if (SH_FSI_BRS_INV & flags)
  1067. data |= 1 << 0;
  1068. fsi_reg_write(fsi, CKG2, data);
  1069. /* spdif ? */
  1070. if (fsi_is_spdif(fsi)) {
  1071. fsi_spdif_clk_ctrl(fsi, 1);
  1072. fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
  1073. }
  1074. /*
  1075. * get bus settings
  1076. */
  1077. data = 0;
  1078. switch (io->sample_width) {
  1079. case 2:
  1080. data = BUSOP_GET(16, io->bus_option);
  1081. break;
  1082. case 4:
  1083. data = BUSOP_GET(24, io->bus_option);
  1084. break;
  1085. }
  1086. fsi_format_bus_setup(fsi, io, data, dev);
  1087. /* irq clear */
  1088. fsi_irq_disable(fsi, io);
  1089. fsi_irq_clear_status(fsi);
  1090. /* fifo init */
  1091. fsi_fifo_init(fsi, io, dev);
  1092. return 0;
  1093. }
  1094. static void fsi_hw_shutdown(struct fsi_priv *fsi,
  1095. struct device *dev)
  1096. {
  1097. if (fsi_is_clk_master(fsi))
  1098. fsi_set_master_clk(dev, fsi, fsi->rate, 0);
  1099. }
  1100. static int fsi_dai_startup(struct snd_pcm_substream *substream,
  1101. struct snd_soc_dai *dai)
  1102. {
  1103. struct fsi_priv *fsi = fsi_get_priv(substream);
  1104. fsi->rate = 0;
  1105. return 0;
  1106. }
  1107. static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
  1108. struct snd_soc_dai *dai)
  1109. {
  1110. struct fsi_priv *fsi = fsi_get_priv(substream);
  1111. fsi->rate = 0;
  1112. }
  1113. static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  1114. struct snd_soc_dai *dai)
  1115. {
  1116. struct fsi_priv *fsi = fsi_get_priv(substream);
  1117. struct fsi_stream *io = fsi_stream_get(fsi, substream);
  1118. int ret = 0;
  1119. switch (cmd) {
  1120. case SNDRV_PCM_TRIGGER_START:
  1121. fsi_stream_init(fsi, io, substream);
  1122. fsi_hw_startup(fsi, io, dai->dev);
  1123. ret = fsi_stream_transfer(io);
  1124. if (0 == ret)
  1125. fsi_stream_start(fsi, io);
  1126. break;
  1127. case SNDRV_PCM_TRIGGER_STOP:
  1128. fsi_hw_shutdown(fsi, dai->dev);
  1129. fsi_stream_stop(fsi, io);
  1130. fsi_stream_quit(fsi, io);
  1131. break;
  1132. }
  1133. return ret;
  1134. }
  1135. static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
  1136. {
  1137. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1138. case SND_SOC_DAIFMT_I2S:
  1139. fsi->fmt = CR_I2S;
  1140. fsi->chan_num = 2;
  1141. break;
  1142. case SND_SOC_DAIFMT_LEFT_J:
  1143. fsi->fmt = CR_PCM;
  1144. fsi->chan_num = 2;
  1145. break;
  1146. default:
  1147. return -EINVAL;
  1148. }
  1149. return 0;
  1150. }
  1151. static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
  1152. {
  1153. struct fsi_master *master = fsi_get_master(fsi);
  1154. if (fsi_version(master) < 2)
  1155. return -EINVAL;
  1156. fsi->fmt = CR_DTMD_SPDIF_PCM | CR_PCM;
  1157. fsi->chan_num = 2;
  1158. fsi->spdif = 1;
  1159. return 0;
  1160. }
  1161. static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1162. {
  1163. struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
  1164. set_rate_func set_rate = fsi_get_info_set_rate(fsi);
  1165. u32 flags = fsi_get_info_flags(fsi);
  1166. int ret;
  1167. /* set master/slave audio interface */
  1168. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1169. case SND_SOC_DAIFMT_CBM_CFM:
  1170. fsi->clk_master = 1;
  1171. break;
  1172. case SND_SOC_DAIFMT_CBS_CFS:
  1173. break;
  1174. default:
  1175. return -EINVAL;
  1176. }
  1177. if (fsi_is_clk_master(fsi) && !set_rate) {
  1178. dev_err(dai->dev, "platform doesn't have set_rate\n");
  1179. return -EINVAL;
  1180. }
  1181. /* set format */
  1182. switch (flags & SH_FSI_FMT_MASK) {
  1183. case SH_FSI_FMT_DAI:
  1184. ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
  1185. break;
  1186. case SH_FSI_FMT_SPDIF:
  1187. ret = fsi_set_fmt_spdif(fsi);
  1188. break;
  1189. default:
  1190. ret = -EINVAL;
  1191. }
  1192. return ret;
  1193. }
  1194. static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
  1195. struct snd_pcm_hw_params *params,
  1196. struct snd_soc_dai *dai)
  1197. {
  1198. struct fsi_priv *fsi = fsi_get_priv(substream);
  1199. long rate = params_rate(params);
  1200. int ret;
  1201. if (!fsi_is_clk_master(fsi))
  1202. return 0;
  1203. ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
  1204. if (ret < 0)
  1205. return ret;
  1206. fsi->rate = rate;
  1207. return ret;
  1208. }
  1209. static const struct snd_soc_dai_ops fsi_dai_ops = {
  1210. .startup = fsi_dai_startup,
  1211. .shutdown = fsi_dai_shutdown,
  1212. .trigger = fsi_dai_trigger,
  1213. .set_fmt = fsi_dai_set_fmt,
  1214. .hw_params = fsi_dai_hw_params,
  1215. };
  1216. /*
  1217. * pcm ops
  1218. */
  1219. static struct snd_pcm_hardware fsi_pcm_hardware = {
  1220. .info = SNDRV_PCM_INFO_INTERLEAVED |
  1221. SNDRV_PCM_INFO_MMAP |
  1222. SNDRV_PCM_INFO_MMAP_VALID |
  1223. SNDRV_PCM_INFO_PAUSE,
  1224. .formats = FSI_FMTS,
  1225. .rates = FSI_RATES,
  1226. .rate_min = 8000,
  1227. .rate_max = 192000,
  1228. .channels_min = 1,
  1229. .channels_max = 2,
  1230. .buffer_bytes_max = 64 * 1024,
  1231. .period_bytes_min = 32,
  1232. .period_bytes_max = 8192,
  1233. .periods_min = 1,
  1234. .periods_max = 32,
  1235. .fifo_size = 256,
  1236. };
  1237. static int fsi_pcm_open(struct snd_pcm_substream *substream)
  1238. {
  1239. struct snd_pcm_runtime *runtime = substream->runtime;
  1240. int ret = 0;
  1241. snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
  1242. ret = snd_pcm_hw_constraint_integer(runtime,
  1243. SNDRV_PCM_HW_PARAM_PERIODS);
  1244. return ret;
  1245. }
  1246. static int fsi_hw_params(struct snd_pcm_substream *substream,
  1247. struct snd_pcm_hw_params *hw_params)
  1248. {
  1249. return snd_pcm_lib_malloc_pages(substream,
  1250. params_buffer_bytes(hw_params));
  1251. }
  1252. static int fsi_hw_free(struct snd_pcm_substream *substream)
  1253. {
  1254. return snd_pcm_lib_free_pages(substream);
  1255. }
  1256. static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
  1257. {
  1258. struct fsi_priv *fsi = fsi_get_priv(substream);
  1259. struct fsi_stream *io = fsi_stream_get(fsi, substream);
  1260. return fsi_sample2frame(fsi, io->buff_sample_pos);
  1261. }
  1262. static struct snd_pcm_ops fsi_pcm_ops = {
  1263. .open = fsi_pcm_open,
  1264. .ioctl = snd_pcm_lib_ioctl,
  1265. .hw_params = fsi_hw_params,
  1266. .hw_free = fsi_hw_free,
  1267. .pointer = fsi_pointer,
  1268. };
  1269. /*
  1270. * snd_soc_platform
  1271. */
  1272. #define PREALLOC_BUFFER (32 * 1024)
  1273. #define PREALLOC_BUFFER_MAX (32 * 1024)
  1274. static void fsi_pcm_free(struct snd_pcm *pcm)
  1275. {
  1276. snd_pcm_lib_preallocate_free_for_all(pcm);
  1277. }
  1278. static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1279. {
  1280. struct snd_pcm *pcm = rtd->pcm;
  1281. /*
  1282. * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
  1283. * in MMAP mode (i.e. aplay -M)
  1284. */
  1285. return snd_pcm_lib_preallocate_pages_for_all(
  1286. pcm,
  1287. SNDRV_DMA_TYPE_CONTINUOUS,
  1288. snd_dma_continuous_data(GFP_KERNEL),
  1289. PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
  1290. }
  1291. /*
  1292. * alsa struct
  1293. */
  1294. static struct snd_soc_dai_driver fsi_soc_dai[] = {
  1295. {
  1296. .name = "fsia-dai",
  1297. .playback = {
  1298. .rates = FSI_RATES,
  1299. .formats = FSI_FMTS,
  1300. .channels_min = 1,
  1301. .channels_max = 8,
  1302. },
  1303. .capture = {
  1304. .rates = FSI_RATES,
  1305. .formats = FSI_FMTS,
  1306. .channels_min = 1,
  1307. .channels_max = 8,
  1308. },
  1309. .ops = &fsi_dai_ops,
  1310. },
  1311. {
  1312. .name = "fsib-dai",
  1313. .playback = {
  1314. .rates = FSI_RATES,
  1315. .formats = FSI_FMTS,
  1316. .channels_min = 1,
  1317. .channels_max = 8,
  1318. },
  1319. .capture = {
  1320. .rates = FSI_RATES,
  1321. .formats = FSI_FMTS,
  1322. .channels_min = 1,
  1323. .channels_max = 8,
  1324. },
  1325. .ops = &fsi_dai_ops,
  1326. },
  1327. };
  1328. static struct snd_soc_platform_driver fsi_soc_platform = {
  1329. .ops = &fsi_pcm_ops,
  1330. .pcm_new = fsi_pcm_new,
  1331. .pcm_free = fsi_pcm_free,
  1332. };
  1333. /*
  1334. * platform function
  1335. */
  1336. static void fsi_handler_init(struct fsi_priv *fsi)
  1337. {
  1338. fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
  1339. fsi->playback.priv = fsi;
  1340. fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
  1341. fsi->capture.priv = fsi;
  1342. if (fsi->info->tx_id) {
  1343. fsi->playback.slave.slave_id = fsi->info->tx_id;
  1344. fsi->playback.handler = &fsi_dma_push_handler;
  1345. }
  1346. }
  1347. static int fsi_probe(struct platform_device *pdev)
  1348. {
  1349. struct fsi_master *master;
  1350. const struct platform_device_id *id_entry;
  1351. struct sh_fsi_platform_info *info = pdev->dev.platform_data;
  1352. struct resource *res;
  1353. unsigned int irq;
  1354. int ret;
  1355. id_entry = pdev->id_entry;
  1356. if (!id_entry) {
  1357. dev_err(&pdev->dev, "unknown fsi device\n");
  1358. return -ENODEV;
  1359. }
  1360. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1361. irq = platform_get_irq(pdev, 0);
  1362. if (!res || (int)irq <= 0) {
  1363. dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
  1364. ret = -ENODEV;
  1365. goto exit;
  1366. }
  1367. master = kzalloc(sizeof(*master), GFP_KERNEL);
  1368. if (!master) {
  1369. dev_err(&pdev->dev, "Could not allocate master\n");
  1370. ret = -ENOMEM;
  1371. goto exit;
  1372. }
  1373. master->base = ioremap_nocache(res->start, resource_size(res));
  1374. if (!master->base) {
  1375. ret = -ENXIO;
  1376. dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
  1377. goto exit_kfree;
  1378. }
  1379. /* master setting */
  1380. master->irq = irq;
  1381. master->core = (struct fsi_core *)id_entry->driver_data;
  1382. spin_lock_init(&master->lock);
  1383. /* FSI A setting */
  1384. master->fsia.base = master->base;
  1385. master->fsia.master = master;
  1386. master->fsia.info = &info->port_a;
  1387. fsi_handler_init(&master->fsia);
  1388. ret = fsi_stream_probe(&master->fsia);
  1389. if (ret < 0) {
  1390. dev_err(&pdev->dev, "FSIA stream probe failed\n");
  1391. goto exit_iounmap;
  1392. }
  1393. /* FSI B setting */
  1394. master->fsib.base = master->base + 0x40;
  1395. master->fsib.master = master;
  1396. master->fsib.info = &info->port_b;
  1397. fsi_handler_init(&master->fsib);
  1398. ret = fsi_stream_probe(&master->fsib);
  1399. if (ret < 0) {
  1400. dev_err(&pdev->dev, "FSIB stream probe failed\n");
  1401. goto exit_fsia;
  1402. }
  1403. pm_runtime_enable(&pdev->dev);
  1404. dev_set_drvdata(&pdev->dev, master);
  1405. ret = request_irq(irq, &fsi_interrupt, 0,
  1406. id_entry->name, master);
  1407. if (ret) {
  1408. dev_err(&pdev->dev, "irq request err\n");
  1409. goto exit_fsib;
  1410. }
  1411. ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
  1412. if (ret < 0) {
  1413. dev_err(&pdev->dev, "cannot snd soc register\n");
  1414. goto exit_free_irq;
  1415. }
  1416. ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
  1417. ARRAY_SIZE(fsi_soc_dai));
  1418. if (ret < 0) {
  1419. dev_err(&pdev->dev, "cannot snd dai register\n");
  1420. goto exit_snd_soc;
  1421. }
  1422. return ret;
  1423. exit_snd_soc:
  1424. snd_soc_unregister_platform(&pdev->dev);
  1425. exit_free_irq:
  1426. free_irq(irq, master);
  1427. exit_fsib:
  1428. fsi_stream_remove(&master->fsib);
  1429. exit_fsia:
  1430. fsi_stream_remove(&master->fsia);
  1431. exit_iounmap:
  1432. iounmap(master->base);
  1433. pm_runtime_disable(&pdev->dev);
  1434. exit_kfree:
  1435. kfree(master);
  1436. master = NULL;
  1437. exit:
  1438. return ret;
  1439. }
  1440. static int fsi_remove(struct platform_device *pdev)
  1441. {
  1442. struct fsi_master *master;
  1443. master = dev_get_drvdata(&pdev->dev);
  1444. free_irq(master->irq, master);
  1445. pm_runtime_disable(&pdev->dev);
  1446. snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
  1447. snd_soc_unregister_platform(&pdev->dev);
  1448. fsi_stream_remove(&master->fsia);
  1449. fsi_stream_remove(&master->fsib);
  1450. iounmap(master->base);
  1451. kfree(master);
  1452. return 0;
  1453. }
  1454. static void __fsi_suspend(struct fsi_priv *fsi,
  1455. struct fsi_stream *io,
  1456. struct device *dev)
  1457. {
  1458. if (!fsi_stream_is_working(fsi, io))
  1459. return;
  1460. fsi_stream_stop(fsi, io);
  1461. fsi_hw_shutdown(fsi, dev);
  1462. }
  1463. static void __fsi_resume(struct fsi_priv *fsi,
  1464. struct fsi_stream *io,
  1465. struct device *dev)
  1466. {
  1467. if (!fsi_stream_is_working(fsi, io))
  1468. return;
  1469. fsi_hw_startup(fsi, io, dev);
  1470. if (fsi_is_clk_master(fsi) && fsi->rate)
  1471. fsi_set_master_clk(dev, fsi, fsi->rate, 1);
  1472. fsi_stream_start(fsi, io);
  1473. }
  1474. static int fsi_suspend(struct device *dev)
  1475. {
  1476. struct fsi_master *master = dev_get_drvdata(dev);
  1477. struct fsi_priv *fsia = &master->fsia;
  1478. struct fsi_priv *fsib = &master->fsib;
  1479. __fsi_suspend(fsia, &fsia->playback, dev);
  1480. __fsi_suspend(fsia, &fsia->capture, dev);
  1481. __fsi_suspend(fsib, &fsib->playback, dev);
  1482. __fsi_suspend(fsib, &fsib->capture, dev);
  1483. return 0;
  1484. }
  1485. static int fsi_resume(struct device *dev)
  1486. {
  1487. struct fsi_master *master = dev_get_drvdata(dev);
  1488. struct fsi_priv *fsia = &master->fsia;
  1489. struct fsi_priv *fsib = &master->fsib;
  1490. __fsi_resume(fsia, &fsia->playback, dev);
  1491. __fsi_resume(fsia, &fsia->capture, dev);
  1492. __fsi_resume(fsib, &fsib->playback, dev);
  1493. __fsi_resume(fsib, &fsib->capture, dev);
  1494. return 0;
  1495. }
  1496. static struct dev_pm_ops fsi_pm_ops = {
  1497. .suspend = fsi_suspend,
  1498. .resume = fsi_resume,
  1499. };
  1500. static struct fsi_core fsi1_core = {
  1501. .ver = 1,
  1502. /* Interrupt */
  1503. .int_st = INT_ST,
  1504. .iemsk = IEMSK,
  1505. .imsk = IMSK,
  1506. };
  1507. static struct fsi_core fsi2_core = {
  1508. .ver = 2,
  1509. /* Interrupt */
  1510. .int_st = CPU_INT_ST,
  1511. .iemsk = CPU_IEMSK,
  1512. .imsk = CPU_IMSK,
  1513. .a_mclk = A_MST_CTLR,
  1514. .b_mclk = B_MST_CTLR,
  1515. };
  1516. static struct platform_device_id fsi_id_table[] = {
  1517. { "sh_fsi", (kernel_ulong_t)&fsi1_core },
  1518. { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
  1519. {},
  1520. };
  1521. MODULE_DEVICE_TABLE(platform, fsi_id_table);
  1522. static struct platform_driver fsi_driver = {
  1523. .driver = {
  1524. .name = "fsi-pcm-audio",
  1525. .pm = &fsi_pm_ops,
  1526. },
  1527. .probe = fsi_probe,
  1528. .remove = fsi_remove,
  1529. .id_table = fsi_id_table,
  1530. };
  1531. module_platform_driver(fsi_driver);
  1532. MODULE_LICENSE("GPL");
  1533. MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
  1534. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
  1535. MODULE_ALIAS("platform:fsi-pcm-audio");