fsi.c 46 KB

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