fsi.c 46 KB

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