fsi.c 48 KB

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