fsi.c 41 KB

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