fsi.c 48 KB

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