fsi.c 49 KB

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