fsi.c 48 KB

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