fsi.c 48 KB

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