fsi.c 48 KB

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