fsi.c 48 KB

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