fsi.c 47 KB

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