fsi.c 49 KB

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