fsi.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  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/sh_fsi.h>
  25. /* PortA/PortB register */
  26. #define REG_DO_FMT 0x0000
  27. #define REG_DOFF_CTL 0x0004
  28. #define REG_DOFF_ST 0x0008
  29. #define REG_DI_FMT 0x000C
  30. #define REG_DIFF_CTL 0x0010
  31. #define REG_DIFF_ST 0x0014
  32. #define REG_CKG1 0x0018
  33. #define REG_CKG2 0x001C
  34. #define REG_DIDT 0x0020
  35. #define REG_DODT 0x0024
  36. #define REG_MUTE_ST 0x0028
  37. #define REG_OUT_DMAC 0x002C
  38. #define REG_OUT_SEL 0x0030
  39. #define REG_IN_DMAC 0x0038
  40. /* master register */
  41. #define MST_CLK_RST 0x0210
  42. #define MST_SOFT_RST 0x0214
  43. #define MST_FIFO_SZ 0x0218
  44. /* core register (depend on FSI version) */
  45. #define A_MST_CTLR 0x0180
  46. #define B_MST_CTLR 0x01A0
  47. #define CPU_INT_ST 0x01F4
  48. #define CPU_IEMSK 0x01F8
  49. #define CPU_IMSK 0x01FC
  50. #define INT_ST 0x0200
  51. #define IEMSK 0x0204
  52. #define IMSK 0x0208
  53. /* DO_FMT */
  54. /* DI_FMT */
  55. #define CR_BWS_MASK (0x3 << 20) /* FSI2 */
  56. #define CR_BWS_24 (0x0 << 20) /* FSI2 */
  57. #define CR_BWS_16 (0x1 << 20) /* FSI2 */
  58. #define CR_BWS_20 (0x2 << 20) /* FSI2 */
  59. #define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
  60. #define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
  61. #define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
  62. #define CR_MONO (0x0 << 4)
  63. #define CR_MONO_D (0x1 << 4)
  64. #define CR_PCM (0x2 << 4)
  65. #define CR_I2S (0x3 << 4)
  66. #define CR_TDM (0x4 << 4)
  67. #define CR_TDM_D (0x5 << 4)
  68. /* OUT_DMAC */
  69. /* IN_DMAC */
  70. #define VDMD_MASK (0x3 << 4)
  71. #define VDMD_FRONT (0x0 << 4) /* Package in front */
  72. #define VDMD_BACK (0x1 << 4) /* Package in back */
  73. #define VDMD_STREAM (0x2 << 4) /* Stream mode(16bit * 2) */
  74. #define DMA_ON (0x1 << 0)
  75. /* DOFF_CTL */
  76. /* DIFF_CTL */
  77. #define IRQ_HALF 0x00100000
  78. #define FIFO_CLR 0x00000001
  79. /* DOFF_ST */
  80. #define ERR_OVER 0x00000010
  81. #define ERR_UNDER 0x00000001
  82. #define ST_ERR (ERR_OVER | ERR_UNDER)
  83. /* CKG1 */
  84. #define ACKMD_MASK 0x00007000
  85. #define BPFMD_MASK 0x00000700
  86. #define DIMD (1 << 4)
  87. #define DOMD (1 << 0)
  88. /* A/B MST_CTLR */
  89. #define BP (1 << 4) /* Fix the signal of Biphase output */
  90. #define SE (1 << 0) /* Fix the master clock */
  91. /* CLK_RST */
  92. #define CRB (1 << 4)
  93. #define CRA (1 << 0)
  94. /* IO SHIFT / MACRO */
  95. #define BI_SHIFT 12
  96. #define BO_SHIFT 8
  97. #define AI_SHIFT 4
  98. #define AO_SHIFT 0
  99. #define AB_IO(param, shift) (param << shift)
  100. /* SOFT_RST */
  101. #define PBSR (1 << 12) /* Port B Software Reset */
  102. #define PASR (1 << 8) /* Port A Software Reset */
  103. #define IR (1 << 4) /* Interrupt Reset */
  104. #define FSISR (1 << 0) /* Software Reset */
  105. /* OUT_SEL (FSI2) */
  106. #define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
  107. /* 1: Biphase and serial */
  108. /* FIFO_SZ */
  109. #define FIFO_SZ_MASK 0x7
  110. #define FSI_RATES SNDRV_PCM_RATE_8000_96000
  111. #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  112. typedef int (*set_rate_func)(struct device *dev, int rate, int enable);
  113. /*
  114. * FSI driver use below type name for variable
  115. *
  116. * xxx_num : number of data
  117. * xxx_pos : position of data
  118. * xxx_capa : capacity of data
  119. */
  120. /*
  121. * period/frame/sample image
  122. *
  123. * ex) PCM (2ch)
  124. *
  125. * period pos period pos
  126. * [n] [n + 1]
  127. * |<-------------------- period--------------------->|
  128. * ==|============================================ ... =|==
  129. * | |
  130. * ||<----- frame ----->|<------ frame ----->| ... |
  131. * |+--------------------+--------------------+- ... |
  132. * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
  133. * |+--------------------+--------------------+- ... |
  134. * ==|============================================ ... =|==
  135. */
  136. /*
  137. * FSI FIFO image
  138. *
  139. * | |
  140. * | |
  141. * | [ sample ] |
  142. * | [ sample ] |
  143. * | [ sample ] |
  144. * | [ sample ] |
  145. * --> go to codecs
  146. */
  147. /*
  148. * struct
  149. */
  150. struct fsi_stream_handler;
  151. struct fsi_stream {
  152. /*
  153. * these are initialized by fsi_stream_init()
  154. */
  155. struct snd_pcm_substream *substream;
  156. int fifo_sample_capa; /* sample capacity of FSI FIFO */
  157. int buff_sample_capa; /* sample capacity of ALSA buffer */
  158. int buff_sample_pos; /* sample position of ALSA buffer */
  159. int period_samples; /* sample number / 1 period */
  160. int period_pos; /* current period position */
  161. int sample_width; /* sample width */
  162. int uerr_num;
  163. int oerr_num;
  164. /*
  165. * thse are initialized by fsi_handler_init()
  166. */
  167. struct fsi_stream_handler *handler;
  168. struct fsi_priv *priv;
  169. /*
  170. * these are for DMAEngine
  171. */
  172. struct dma_chan *chan;
  173. struct sh_dmae_slave slave; /* see fsi_handler_init() */
  174. struct work_struct work;
  175. dma_addr_t dma;
  176. };
  177. struct fsi_priv {
  178. void __iomem *base;
  179. struct fsi_master *master;
  180. struct sh_fsi_port_info *info;
  181. struct fsi_stream playback;
  182. struct fsi_stream capture;
  183. u32 do_fmt;
  184. u32 di_fmt;
  185. int chan_num:16;
  186. int clk_master:1;
  187. int spdif:1;
  188. long rate;
  189. };
  190. struct fsi_stream_handler {
  191. int (*init)(struct fsi_priv *fsi, struct fsi_stream *io);
  192. int (*quit)(struct fsi_priv *fsi, struct fsi_stream *io);
  193. int (*probe)(struct fsi_priv *fsi, struct fsi_stream *io);
  194. int (*transfer)(struct fsi_priv *fsi, struct fsi_stream *io);
  195. int (*remove)(struct fsi_priv *fsi, struct fsi_stream *io);
  196. void (*start_stop)(struct fsi_priv *fsi, struct fsi_stream *io,
  197. int enable);
  198. };
  199. #define fsi_stream_handler_call(io, func, args...) \
  200. (!(io) ? -ENODEV : \
  201. !((io)->handler->func) ? 0 : \
  202. (io)->handler->func(args))
  203. struct fsi_core {
  204. int ver;
  205. u32 int_st;
  206. u32 iemsk;
  207. u32 imsk;
  208. u32 a_mclk;
  209. u32 b_mclk;
  210. };
  211. struct fsi_master {
  212. void __iomem *base;
  213. int irq;
  214. struct fsi_priv fsia;
  215. struct fsi_priv fsib;
  216. struct fsi_core *core;
  217. spinlock_t lock;
  218. };
  219. static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
  220. /*
  221. * basic read write function
  222. */
  223. static void __fsi_reg_write(u32 __iomem *reg, u32 data)
  224. {
  225. /* valid data area is 24bit */
  226. data &= 0x00ffffff;
  227. __raw_writel(data, reg);
  228. }
  229. static u32 __fsi_reg_read(u32 __iomem *reg)
  230. {
  231. return __raw_readl(reg);
  232. }
  233. static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
  234. {
  235. u32 val = __fsi_reg_read(reg);
  236. val &= ~mask;
  237. val |= data & mask;
  238. __fsi_reg_write(reg, val);
  239. }
  240. #define fsi_reg_write(p, r, d)\
  241. __fsi_reg_write((p->base + REG_##r), d)
  242. #define fsi_reg_read(p, r)\
  243. __fsi_reg_read((p->base + REG_##r))
  244. #define fsi_reg_mask_set(p, r, m, d)\
  245. __fsi_reg_mask_set((p->base + REG_##r), m, d)
  246. #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
  247. #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
  248. static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
  249. {
  250. u32 ret;
  251. unsigned long flags;
  252. spin_lock_irqsave(&master->lock, flags);
  253. ret = __fsi_reg_read(master->base + reg);
  254. spin_unlock_irqrestore(&master->lock, flags);
  255. return ret;
  256. }
  257. #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
  258. #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
  259. static void _fsi_master_mask_set(struct fsi_master *master,
  260. u32 reg, u32 mask, u32 data)
  261. {
  262. unsigned long flags;
  263. spin_lock_irqsave(&master->lock, flags);
  264. __fsi_reg_mask_set(master->base + reg, mask, data);
  265. spin_unlock_irqrestore(&master->lock, flags);
  266. }
  267. /*
  268. * basic function
  269. */
  270. static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
  271. {
  272. return fsi->master;
  273. }
  274. static int fsi_is_clk_master(struct fsi_priv *fsi)
  275. {
  276. return fsi->clk_master;
  277. }
  278. static int fsi_is_port_a(struct fsi_priv *fsi)
  279. {
  280. return fsi->master->base == fsi->base;
  281. }
  282. static int fsi_is_spdif(struct fsi_priv *fsi)
  283. {
  284. return fsi->spdif;
  285. }
  286. static int fsi_is_play(struct snd_pcm_substream *substream)
  287. {
  288. return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  289. }
  290. static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
  291. {
  292. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  293. return rtd->cpu_dai;
  294. }
  295. static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
  296. {
  297. struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
  298. if (dai->id == 0)
  299. return &master->fsia;
  300. else
  301. return &master->fsib;
  302. }
  303. static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
  304. {
  305. return fsi_get_priv_frm_dai(fsi_get_dai(substream));
  306. }
  307. static set_rate_func fsi_get_info_set_rate(struct fsi_priv *fsi)
  308. {
  309. if (!fsi->info)
  310. return NULL;
  311. return fsi->info->set_rate;
  312. }
  313. static u32 fsi_get_info_flags(struct fsi_priv *fsi)
  314. {
  315. if (!fsi->info)
  316. return 0;
  317. return fsi->info->flags;
  318. }
  319. static u32 fsi_get_port_shift(struct fsi_priv *fsi, struct fsi_stream *io)
  320. {
  321. int is_play = fsi_stream_is_play(fsi, io);
  322. int is_porta = fsi_is_port_a(fsi);
  323. u32 shift;
  324. if (is_porta)
  325. shift = is_play ? AO_SHIFT : AI_SHIFT;
  326. else
  327. shift = is_play ? BO_SHIFT : BI_SHIFT;
  328. return shift;
  329. }
  330. static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
  331. {
  332. return frames * fsi->chan_num;
  333. }
  334. static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
  335. {
  336. return samples / fsi->chan_num;
  337. }
  338. static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
  339. struct fsi_stream *io)
  340. {
  341. int is_play = fsi_stream_is_play(fsi, io);
  342. u32 status;
  343. int frames;
  344. status = is_play ?
  345. fsi_reg_read(fsi, DOFF_ST) :
  346. fsi_reg_read(fsi, DIFF_ST);
  347. frames = 0x1ff & (status >> 8);
  348. return fsi_frame2sample(fsi, frames);
  349. }
  350. static void fsi_count_fifo_err(struct fsi_priv *fsi)
  351. {
  352. u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
  353. u32 istatus = fsi_reg_read(fsi, DIFF_ST);
  354. if (ostatus & ERR_OVER)
  355. fsi->playback.oerr_num++;
  356. if (ostatus & ERR_UNDER)
  357. fsi->playback.uerr_num++;
  358. if (istatus & ERR_OVER)
  359. fsi->capture.oerr_num++;
  360. if (istatus & ERR_UNDER)
  361. fsi->capture.uerr_num++;
  362. fsi_reg_write(fsi, DOFF_ST, 0);
  363. fsi_reg_write(fsi, DIFF_ST, 0);
  364. }
  365. /*
  366. * fsi_stream_xx() function
  367. */
  368. static inline int fsi_stream_is_play(struct fsi_priv *fsi,
  369. struct fsi_stream *io)
  370. {
  371. return &fsi->playback == io;
  372. }
  373. static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
  374. struct snd_pcm_substream *substream)
  375. {
  376. return fsi_is_play(substream) ? &fsi->playback : &fsi->capture;
  377. }
  378. static int fsi_stream_is_working(struct fsi_priv *fsi,
  379. struct fsi_stream *io)
  380. {
  381. struct fsi_master *master = fsi_get_master(fsi);
  382. unsigned long flags;
  383. int ret;
  384. spin_lock_irqsave(&master->lock, flags);
  385. ret = !!(io->substream && io->substream->runtime);
  386. spin_unlock_irqrestore(&master->lock, flags);
  387. return ret;
  388. }
  389. static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
  390. {
  391. return io->priv;
  392. }
  393. static void fsi_stream_init(struct fsi_priv *fsi,
  394. struct fsi_stream *io,
  395. struct snd_pcm_substream *substream)
  396. {
  397. struct snd_pcm_runtime *runtime = substream->runtime;
  398. struct fsi_master *master = fsi_get_master(fsi);
  399. unsigned long flags;
  400. spin_lock_irqsave(&master->lock, flags);
  401. io->substream = substream;
  402. io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
  403. io->buff_sample_pos = 0;
  404. io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
  405. io->period_pos = 0;
  406. io->sample_width = samples_to_bytes(runtime, 1);
  407. io->oerr_num = -1; /* ignore 1st err */
  408. io->uerr_num = -1; /* ignore 1st err */
  409. fsi_stream_handler_call(io, init, fsi, io);
  410. spin_unlock_irqrestore(&master->lock, flags);
  411. }
  412. static void fsi_stream_quit(struct fsi_priv *fsi, struct fsi_stream *io)
  413. {
  414. struct snd_soc_dai *dai = fsi_get_dai(io->substream);
  415. struct fsi_master *master = fsi_get_master(fsi);
  416. unsigned long flags;
  417. spin_lock_irqsave(&master->lock, flags);
  418. if (io->oerr_num > 0)
  419. dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
  420. if (io->uerr_num > 0)
  421. dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
  422. fsi_stream_handler_call(io, quit, fsi, io);
  423. io->substream = NULL;
  424. io->buff_sample_capa = 0;
  425. io->buff_sample_pos = 0;
  426. io->period_samples = 0;
  427. io->period_pos = 0;
  428. io->sample_width = 0;
  429. io->oerr_num = 0;
  430. io->uerr_num = 0;
  431. spin_unlock_irqrestore(&master->lock, flags);
  432. }
  433. static int fsi_stream_transfer(struct fsi_stream *io)
  434. {
  435. struct fsi_priv *fsi = fsi_stream_to_priv(io);
  436. if (!fsi)
  437. return -EIO;
  438. return fsi_stream_handler_call(io, transfer, fsi, io);
  439. }
  440. #define fsi_stream_start(fsi, io)\
  441. fsi_stream_handler_call(io, start_stop, fsi, io, 1)
  442. #define fsi_stream_stop(fsi, io)\
  443. fsi_stream_handler_call(io, start_stop, fsi, io, 0)
  444. static int fsi_stream_probe(struct fsi_priv *fsi)
  445. {
  446. struct fsi_stream *io;
  447. int ret1, ret2;
  448. io = &fsi->playback;
  449. ret1 = fsi_stream_handler_call(io, probe, fsi, io);
  450. io = &fsi->capture;
  451. ret2 = fsi_stream_handler_call(io, probe, fsi, io);
  452. if (ret1 < 0)
  453. return ret1;
  454. if (ret2 < 0)
  455. return ret2;
  456. return 0;
  457. }
  458. static int fsi_stream_remove(struct fsi_priv *fsi)
  459. {
  460. struct fsi_stream *io;
  461. int ret1, ret2;
  462. io = &fsi->playback;
  463. ret1 = fsi_stream_handler_call(io, remove, fsi, io);
  464. io = &fsi->capture;
  465. ret2 = fsi_stream_handler_call(io, remove, fsi, io);
  466. if (ret1 < 0)
  467. return ret1;
  468. if (ret2 < 0)
  469. return ret2;
  470. return 0;
  471. }
  472. /*
  473. * irq function
  474. */
  475. static void fsi_irq_enable(struct fsi_priv *fsi, struct fsi_stream *io)
  476. {
  477. u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
  478. struct fsi_master *master = fsi_get_master(fsi);
  479. fsi_core_mask_set(master, imsk, data, data);
  480. fsi_core_mask_set(master, iemsk, data, data);
  481. }
  482. static void fsi_irq_disable(struct fsi_priv *fsi, struct fsi_stream *io)
  483. {
  484. u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
  485. struct fsi_master *master = fsi_get_master(fsi);
  486. fsi_core_mask_set(master, imsk, data, 0);
  487. fsi_core_mask_set(master, iemsk, data, 0);
  488. }
  489. static u32 fsi_irq_get_status(struct fsi_master *master)
  490. {
  491. return fsi_core_read(master, int_st);
  492. }
  493. static void fsi_irq_clear_status(struct fsi_priv *fsi)
  494. {
  495. u32 data = 0;
  496. struct fsi_master *master = fsi_get_master(fsi);
  497. data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->playback));
  498. data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->capture));
  499. /* clear interrupt factor */
  500. fsi_core_mask_set(master, int_st, data, 0);
  501. }
  502. /*
  503. * SPDIF master clock function
  504. *
  505. * These functions are used later FSI2
  506. */
  507. static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
  508. {
  509. struct fsi_master *master = fsi_get_master(fsi);
  510. u32 mask, val;
  511. if (master->core->ver < 2) {
  512. pr_err("fsi: register access err (%s)\n", __func__);
  513. return;
  514. }
  515. mask = BP | SE;
  516. val = enable ? mask : 0;
  517. fsi_is_port_a(fsi) ?
  518. fsi_core_mask_set(master, a_mclk, mask, val) :
  519. fsi_core_mask_set(master, b_mclk, mask, val);
  520. }
  521. /*
  522. * clock function
  523. */
  524. static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
  525. long rate, int enable)
  526. {
  527. struct fsi_master *master = fsi_get_master(fsi);
  528. set_rate_func set_rate = fsi_get_info_set_rate(fsi);
  529. int fsi_ver = master->core->ver;
  530. int ret;
  531. if (!set_rate)
  532. return 0;
  533. ret = set_rate(dev, rate, enable);
  534. if (ret < 0) /* error */
  535. return ret;
  536. if (!enable)
  537. return 0;
  538. if (ret > 0) {
  539. u32 data = 0;
  540. switch (ret & SH_FSI_ACKMD_MASK) {
  541. default:
  542. /* FALL THROUGH */
  543. case SH_FSI_ACKMD_512:
  544. data |= (0x0 << 12);
  545. break;
  546. case SH_FSI_ACKMD_256:
  547. data |= (0x1 << 12);
  548. break;
  549. case SH_FSI_ACKMD_128:
  550. data |= (0x2 << 12);
  551. break;
  552. case SH_FSI_ACKMD_64:
  553. data |= (0x3 << 12);
  554. break;
  555. case SH_FSI_ACKMD_32:
  556. if (fsi_ver < 2)
  557. dev_err(dev, "unsupported ACKMD\n");
  558. else
  559. data |= (0x4 << 12);
  560. break;
  561. }
  562. switch (ret & SH_FSI_BPFMD_MASK) {
  563. default:
  564. /* FALL THROUGH */
  565. case SH_FSI_BPFMD_32:
  566. data |= (0x0 << 8);
  567. break;
  568. case SH_FSI_BPFMD_64:
  569. data |= (0x1 << 8);
  570. break;
  571. case SH_FSI_BPFMD_128:
  572. data |= (0x2 << 8);
  573. break;
  574. case SH_FSI_BPFMD_256:
  575. data |= (0x3 << 8);
  576. break;
  577. case SH_FSI_BPFMD_512:
  578. data |= (0x4 << 8);
  579. break;
  580. case SH_FSI_BPFMD_16:
  581. if (fsi_ver < 2)
  582. dev_err(dev, "unsupported ACKMD\n");
  583. else
  584. data |= (0x7 << 8);
  585. break;
  586. }
  587. fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
  588. udelay(10);
  589. ret = 0;
  590. }
  591. return ret;
  592. }
  593. /*
  594. * pio data transfer handler
  595. */
  596. static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
  597. {
  598. u16 *buf = (u16 *)_buf;
  599. int i;
  600. for (i = 0; i < samples; i++)
  601. fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
  602. }
  603. static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
  604. {
  605. u16 *buf = (u16 *)_buf;
  606. int i;
  607. for (i = 0; i < samples; i++)
  608. *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
  609. }
  610. static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
  611. {
  612. u32 *buf = (u32 *)_buf;
  613. int i;
  614. for (i = 0; i < samples; i++)
  615. fsi_reg_write(fsi, DODT, *(buf + i));
  616. }
  617. static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
  618. {
  619. u32 *buf = (u32 *)_buf;
  620. int i;
  621. for (i = 0; i < samples; i++)
  622. *(buf + i) = fsi_reg_read(fsi, DIDT);
  623. }
  624. static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
  625. {
  626. struct snd_pcm_runtime *runtime = io->substream->runtime;
  627. return runtime->dma_area +
  628. samples_to_bytes(runtime, io->buff_sample_pos);
  629. }
  630. static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
  631. void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
  632. void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
  633. int samples)
  634. {
  635. struct snd_pcm_runtime *runtime;
  636. struct snd_pcm_substream *substream;
  637. u8 *buf;
  638. int over_period;
  639. if (!fsi_stream_is_working(fsi, io))
  640. return -EINVAL;
  641. over_period = 0;
  642. substream = io->substream;
  643. runtime = substream->runtime;
  644. /* FSI FIFO has limit.
  645. * So, this driver can not send periods data at a time
  646. */
  647. if (io->buff_sample_pos >=
  648. io->period_samples * (io->period_pos + 1)) {
  649. over_period = 1;
  650. io->period_pos = (io->period_pos + 1) % runtime->periods;
  651. if (0 == io->period_pos)
  652. io->buff_sample_pos = 0;
  653. }
  654. buf = fsi_pio_get_area(fsi, io);
  655. switch (io->sample_width) {
  656. case 2:
  657. run16(fsi, buf, samples);
  658. break;
  659. case 4:
  660. run32(fsi, buf, samples);
  661. break;
  662. default:
  663. return -EINVAL;
  664. }
  665. /* update buff_sample_pos */
  666. io->buff_sample_pos += samples;
  667. if (over_period)
  668. snd_pcm_period_elapsed(substream);
  669. return 0;
  670. }
  671. static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
  672. {
  673. int sample_residues; /* samples in FSI fifo */
  674. int sample_space; /* ALSA free samples space */
  675. int samples;
  676. sample_residues = fsi_get_current_fifo_samples(fsi, io);
  677. sample_space = io->buff_sample_capa - io->buff_sample_pos;
  678. samples = min(sample_residues, sample_space);
  679. return fsi_pio_transfer(fsi, io,
  680. fsi_pio_pop16,
  681. fsi_pio_pop32,
  682. samples);
  683. }
  684. static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
  685. {
  686. int sample_residues; /* ALSA residue samples */
  687. int sample_space; /* FSI fifo free samples space */
  688. int samples;
  689. sample_residues = io->buff_sample_capa - io->buff_sample_pos;
  690. sample_space = io->fifo_sample_capa -
  691. fsi_get_current_fifo_samples(fsi, io);
  692. samples = min(sample_residues, sample_space);
  693. return fsi_pio_transfer(fsi, io,
  694. fsi_pio_push16,
  695. fsi_pio_push32,
  696. samples);
  697. }
  698. static void fsi_pio_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
  699. int enable)
  700. {
  701. struct fsi_master *master = fsi_get_master(fsi);
  702. u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
  703. if (enable)
  704. fsi_irq_enable(fsi, io);
  705. else
  706. fsi_irq_disable(fsi, io);
  707. if (fsi_is_clk_master(fsi))
  708. fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
  709. }
  710. static struct fsi_stream_handler fsi_pio_push_handler = {
  711. .transfer = fsi_pio_push,
  712. .start_stop = fsi_pio_start_stop,
  713. };
  714. static struct fsi_stream_handler fsi_pio_pop_handler = {
  715. .transfer = fsi_pio_pop,
  716. .start_stop = fsi_pio_start_stop,
  717. };
  718. static irqreturn_t fsi_interrupt(int irq, void *data)
  719. {
  720. struct fsi_master *master = data;
  721. u32 int_st = fsi_irq_get_status(master);
  722. /* clear irq status */
  723. fsi_master_mask_set(master, SOFT_RST, IR, 0);
  724. fsi_master_mask_set(master, SOFT_RST, IR, IR);
  725. if (int_st & AB_IO(1, AO_SHIFT))
  726. fsi_stream_transfer(&master->fsia.playback);
  727. if (int_st & AB_IO(1, BO_SHIFT))
  728. fsi_stream_transfer(&master->fsib.playback);
  729. if (int_st & AB_IO(1, AI_SHIFT))
  730. fsi_stream_transfer(&master->fsia.capture);
  731. if (int_st & AB_IO(1, BI_SHIFT))
  732. fsi_stream_transfer(&master->fsib.capture);
  733. fsi_count_fifo_err(&master->fsia);
  734. fsi_count_fifo_err(&master->fsib);
  735. fsi_irq_clear_status(&master->fsia);
  736. fsi_irq_clear_status(&master->fsib);
  737. return IRQ_HANDLED;
  738. }
  739. /*
  740. * dma data transfer handler
  741. */
  742. static int fsi_dma_init(struct fsi_priv *fsi, struct fsi_stream *io)
  743. {
  744. struct snd_pcm_runtime *runtime = io->substream->runtime;
  745. struct snd_soc_dai *dai = fsi_get_dai(io->substream);
  746. enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
  747. DMA_TO_DEVICE : DMA_FROM_DEVICE;
  748. io->dma = dma_map_single(dai->dev, runtime->dma_area,
  749. snd_pcm_lib_buffer_bytes(io->substream), dir);
  750. return 0;
  751. }
  752. static int fsi_dma_quit(struct fsi_priv *fsi, struct fsi_stream *io)
  753. {
  754. struct snd_soc_dai *dai = fsi_get_dai(io->substream);
  755. enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
  756. DMA_TO_DEVICE : DMA_FROM_DEVICE;
  757. dma_unmap_single(dai->dev, io->dma,
  758. snd_pcm_lib_buffer_bytes(io->substream), dir);
  759. return 0;
  760. }
  761. static void fsi_dma_complete(void *data)
  762. {
  763. struct fsi_stream *io = (struct fsi_stream *)data;
  764. struct fsi_priv *fsi = fsi_stream_to_priv(io);
  765. struct snd_pcm_runtime *runtime = io->substream->runtime;
  766. struct snd_soc_dai *dai = fsi_get_dai(io->substream);
  767. enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
  768. DMA_TO_DEVICE : DMA_FROM_DEVICE;
  769. dma_sync_single_for_cpu(dai->dev, io->dma,
  770. samples_to_bytes(runtime, io->period_samples), dir);
  771. io->buff_sample_pos += io->period_samples;
  772. io->period_pos++;
  773. if (io->period_pos >= runtime->periods) {
  774. io->period_pos = 0;
  775. io->buff_sample_pos = 0;
  776. }
  777. fsi_count_fifo_err(fsi);
  778. fsi_stream_transfer(io);
  779. snd_pcm_period_elapsed(io->substream);
  780. }
  781. static dma_addr_t fsi_dma_get_area(struct fsi_stream *io)
  782. {
  783. struct snd_pcm_runtime *runtime = io->substream->runtime;
  784. return io->dma + samples_to_bytes(runtime, io->buff_sample_pos);
  785. }
  786. static void fsi_dma_do_work(struct work_struct *work)
  787. {
  788. struct fsi_stream *io = container_of(work, struct fsi_stream, work);
  789. struct fsi_priv *fsi = fsi_stream_to_priv(io);
  790. struct dma_chan *chan;
  791. struct snd_soc_dai *dai;
  792. struct dma_async_tx_descriptor *desc;
  793. struct scatterlist sg;
  794. struct snd_pcm_runtime *runtime;
  795. enum dma_data_direction dir;
  796. dma_cookie_t cookie;
  797. int is_play = fsi_stream_is_play(fsi, io);
  798. int len;
  799. dma_addr_t buf;
  800. if (!fsi_stream_is_working(fsi, io))
  801. return;
  802. dai = fsi_get_dai(io->substream);
  803. chan = io->chan;
  804. runtime = io->substream->runtime;
  805. dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  806. len = samples_to_bytes(runtime, io->period_samples);
  807. buf = fsi_dma_get_area(io);
  808. dma_sync_single_for_device(dai->dev, io->dma, len, dir);
  809. sg_init_table(&sg, 1);
  810. sg_set_page(&sg, pfn_to_page(PFN_DOWN(buf)),
  811. len , offset_in_page(buf));
  812. sg_dma_address(&sg) = buf;
  813. sg_dma_len(&sg) = len;
  814. desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir,
  815. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  816. if (!desc) {
  817. dev_err(dai->dev, "dmaengine_prep_slave_sg() fail\n");
  818. return;
  819. }
  820. desc->callback = fsi_dma_complete;
  821. desc->callback_param = io;
  822. cookie = desc->tx_submit(desc);
  823. if (cookie < 0) {
  824. dev_err(dai->dev, "tx_submit() fail\n");
  825. return;
  826. }
  827. dma_async_issue_pending(chan);
  828. /*
  829. * FIXME
  830. *
  831. * In DMAEngine case, codec and FSI cannot be started simultaneously
  832. * since FSI is using the scheduler work queue.
  833. * Therefore, in capture case, probably FSI FIFO will have got
  834. * overflow error in this point.
  835. * in that case, DMA cannot start transfer until error was cleared.
  836. */
  837. if (!is_play) {
  838. if (ERR_OVER & fsi_reg_read(fsi, DIFF_ST)) {
  839. fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
  840. fsi_reg_write(fsi, DIFF_ST, 0);
  841. }
  842. }
  843. }
  844. static bool fsi_dma_filter(struct dma_chan *chan, void *param)
  845. {
  846. struct sh_dmae_slave *slave = param;
  847. chan->private = slave;
  848. return true;
  849. }
  850. static int fsi_dma_transfer(struct fsi_priv *fsi, struct fsi_stream *io)
  851. {
  852. schedule_work(&io->work);
  853. return 0;
  854. }
  855. static void fsi_dma_push_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
  856. int start)
  857. {
  858. u32 bws;
  859. u32 dma;
  860. switch (io->sample_width * start) {
  861. case 2:
  862. bws = CR_BWS_16;
  863. dma = VDMD_STREAM | DMA_ON;
  864. break;
  865. case 4:
  866. bws = CR_BWS_24;
  867. dma = VDMD_BACK | DMA_ON;
  868. break;
  869. default:
  870. bws = 0;
  871. dma = 0;
  872. }
  873. fsi_reg_mask_set(fsi, DO_FMT, CR_BWS_MASK, bws);
  874. fsi_reg_write(fsi, OUT_DMAC, dma);
  875. }
  876. static int fsi_dma_probe(struct fsi_priv *fsi, struct fsi_stream *io)
  877. {
  878. dma_cap_mask_t mask;
  879. dma_cap_zero(mask);
  880. dma_cap_set(DMA_SLAVE, mask);
  881. io->chan = dma_request_channel(mask, fsi_dma_filter, &io->slave);
  882. if (!io->chan)
  883. return -EIO;
  884. INIT_WORK(&io->work, fsi_dma_do_work);
  885. return 0;
  886. }
  887. static int fsi_dma_remove(struct fsi_priv *fsi, struct fsi_stream *io)
  888. {
  889. cancel_work_sync(&io->work);
  890. fsi_stream_stop(fsi, io);
  891. if (io->chan)
  892. dma_release_channel(io->chan);
  893. io->chan = NULL;
  894. return 0;
  895. }
  896. static struct fsi_stream_handler fsi_dma_push_handler = {
  897. .init = fsi_dma_init,
  898. .quit = fsi_dma_quit,
  899. .probe = fsi_dma_probe,
  900. .transfer = fsi_dma_transfer,
  901. .remove = fsi_dma_remove,
  902. .start_stop = fsi_dma_push_start_stop,
  903. };
  904. /*
  905. * dai ops
  906. */
  907. static void fsi_fifo_init(struct fsi_priv *fsi,
  908. struct fsi_stream *io,
  909. struct device *dev)
  910. {
  911. struct fsi_master *master = fsi_get_master(fsi);
  912. int is_play = fsi_stream_is_play(fsi, io);
  913. u32 shift, i;
  914. int frame_capa;
  915. /* get on-chip RAM capacity */
  916. shift = fsi_master_read(master, FIFO_SZ);
  917. shift >>= fsi_get_port_shift(fsi, io);
  918. shift &= FIFO_SZ_MASK;
  919. frame_capa = 256 << shift;
  920. dev_dbg(dev, "fifo = %d words\n", frame_capa);
  921. /*
  922. * The maximum number of sample data varies depending
  923. * on the number of channels selected for the format.
  924. *
  925. * FIFOs are used in 4-channel units in 3-channel mode
  926. * and in 8-channel units in 5- to 7-channel mode
  927. * meaning that more FIFOs than the required size of DPRAM
  928. * are used.
  929. *
  930. * ex) if 256 words of DP-RAM is connected
  931. * 1 channel: 256 (256 x 1 = 256)
  932. * 2 channels: 128 (128 x 2 = 256)
  933. * 3 channels: 64 ( 64 x 3 = 192)
  934. * 4 channels: 64 ( 64 x 4 = 256)
  935. * 5 channels: 32 ( 32 x 5 = 160)
  936. * 6 channels: 32 ( 32 x 6 = 192)
  937. * 7 channels: 32 ( 32 x 7 = 224)
  938. * 8 channels: 32 ( 32 x 8 = 256)
  939. */
  940. for (i = 1; i < fsi->chan_num; i <<= 1)
  941. frame_capa >>= 1;
  942. dev_dbg(dev, "%d channel %d store\n",
  943. fsi->chan_num, frame_capa);
  944. io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
  945. /*
  946. * set interrupt generation factor
  947. * clear FIFO
  948. */
  949. if (is_play) {
  950. fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
  951. fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
  952. } else {
  953. fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
  954. fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
  955. }
  956. }
  957. static int fsi_hw_startup(struct fsi_priv *fsi,
  958. struct fsi_stream *io,
  959. struct device *dev)
  960. {
  961. struct fsi_master *master = fsi_get_master(fsi);
  962. int fsi_ver = master->core->ver;
  963. u32 flags = fsi_get_info_flags(fsi);
  964. u32 data = 0;
  965. /* clock setting */
  966. if (fsi_is_clk_master(fsi))
  967. data = DIMD | DOMD;
  968. fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
  969. /* clock inversion (CKG2) */
  970. data = 0;
  971. if (SH_FSI_LRM_INV & flags)
  972. data |= 1 << 12;
  973. if (SH_FSI_BRM_INV & flags)
  974. data |= 1 << 8;
  975. if (SH_FSI_LRS_INV & flags)
  976. data |= 1 << 4;
  977. if (SH_FSI_BRS_INV & flags)
  978. data |= 1 << 0;
  979. fsi_reg_write(fsi, CKG2, data);
  980. /* set format */
  981. fsi_reg_write(fsi, DO_FMT, fsi->do_fmt);
  982. fsi_reg_write(fsi, DI_FMT, fsi->di_fmt);
  983. /* spdif ? */
  984. if (fsi_is_spdif(fsi)) {
  985. fsi_spdif_clk_ctrl(fsi, 1);
  986. fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
  987. }
  988. /*
  989. * FIXME
  990. *
  991. * FSI driver assumed that data package is in-back.
  992. * FSI2 chip can select it.
  993. */
  994. if (fsi_ver >= 2) {
  995. fsi_reg_write(fsi, OUT_DMAC, (1 << 4));
  996. fsi_reg_write(fsi, IN_DMAC, (1 << 4));
  997. }
  998. /* irq clear */
  999. fsi_irq_disable(fsi, io);
  1000. fsi_irq_clear_status(fsi);
  1001. /* fifo init */
  1002. fsi_fifo_init(fsi, io, dev);
  1003. return 0;
  1004. }
  1005. static void fsi_hw_shutdown(struct fsi_priv *fsi,
  1006. struct device *dev)
  1007. {
  1008. if (fsi_is_clk_master(fsi))
  1009. fsi_set_master_clk(dev, fsi, fsi->rate, 0);
  1010. }
  1011. static int fsi_dai_startup(struct snd_pcm_substream *substream,
  1012. struct snd_soc_dai *dai)
  1013. {
  1014. struct fsi_priv *fsi = fsi_get_priv(substream);
  1015. return fsi_hw_startup(fsi, fsi_stream_get(fsi, substream), dai->dev);
  1016. }
  1017. static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
  1018. struct snd_soc_dai *dai)
  1019. {
  1020. struct fsi_priv *fsi = fsi_get_priv(substream);
  1021. fsi_hw_shutdown(fsi, dai->dev);
  1022. fsi->rate = 0;
  1023. }
  1024. static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  1025. struct snd_soc_dai *dai)
  1026. {
  1027. struct fsi_priv *fsi = fsi_get_priv(substream);
  1028. struct fsi_stream *io = fsi_stream_get(fsi, substream);
  1029. int ret = 0;
  1030. switch (cmd) {
  1031. case SNDRV_PCM_TRIGGER_START:
  1032. fsi_stream_init(fsi, io, substream);
  1033. ret = fsi_stream_transfer(io);
  1034. if (0 == ret)
  1035. fsi_stream_start(fsi, io);
  1036. break;
  1037. case SNDRV_PCM_TRIGGER_STOP:
  1038. fsi_stream_stop(fsi, io);
  1039. fsi_stream_quit(fsi, io);
  1040. break;
  1041. }
  1042. return ret;
  1043. }
  1044. static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
  1045. {
  1046. u32 data = 0;
  1047. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1048. case SND_SOC_DAIFMT_I2S:
  1049. data = CR_I2S;
  1050. fsi->chan_num = 2;
  1051. break;
  1052. case SND_SOC_DAIFMT_LEFT_J:
  1053. data = CR_PCM;
  1054. fsi->chan_num = 2;
  1055. break;
  1056. default:
  1057. return -EINVAL;
  1058. }
  1059. fsi->do_fmt = data;
  1060. fsi->di_fmt = data;
  1061. return 0;
  1062. }
  1063. static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
  1064. {
  1065. struct fsi_master *master = fsi_get_master(fsi);
  1066. u32 data = 0;
  1067. if (master->core->ver < 2)
  1068. return -EINVAL;
  1069. data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
  1070. fsi->chan_num = 2;
  1071. fsi->spdif = 1;
  1072. fsi->do_fmt = data;
  1073. fsi->di_fmt = data;
  1074. return 0;
  1075. }
  1076. static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1077. {
  1078. struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
  1079. set_rate_func set_rate = fsi_get_info_set_rate(fsi);
  1080. u32 flags = fsi_get_info_flags(fsi);
  1081. int ret;
  1082. /* set master/slave audio interface */
  1083. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1084. case SND_SOC_DAIFMT_CBM_CFM:
  1085. fsi->clk_master = 1;
  1086. break;
  1087. case SND_SOC_DAIFMT_CBS_CFS:
  1088. break;
  1089. default:
  1090. return -EINVAL;
  1091. }
  1092. if (fsi_is_clk_master(fsi) && !set_rate) {
  1093. dev_err(dai->dev, "platform doesn't have set_rate\n");
  1094. return -EINVAL;
  1095. }
  1096. /* set format */
  1097. switch (flags & SH_FSI_FMT_MASK) {
  1098. case SH_FSI_FMT_DAI:
  1099. ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
  1100. break;
  1101. case SH_FSI_FMT_SPDIF:
  1102. ret = fsi_set_fmt_spdif(fsi);
  1103. break;
  1104. default:
  1105. ret = -EINVAL;
  1106. }
  1107. return ret;
  1108. }
  1109. static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
  1110. struct snd_pcm_hw_params *params,
  1111. struct snd_soc_dai *dai)
  1112. {
  1113. struct fsi_priv *fsi = fsi_get_priv(substream);
  1114. long rate = params_rate(params);
  1115. int ret;
  1116. if (!fsi_is_clk_master(fsi))
  1117. return 0;
  1118. ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
  1119. if (ret < 0)
  1120. return ret;
  1121. fsi->rate = rate;
  1122. return ret;
  1123. }
  1124. static const struct snd_soc_dai_ops fsi_dai_ops = {
  1125. .startup = fsi_dai_startup,
  1126. .shutdown = fsi_dai_shutdown,
  1127. .trigger = fsi_dai_trigger,
  1128. .set_fmt = fsi_dai_set_fmt,
  1129. .hw_params = fsi_dai_hw_params,
  1130. };
  1131. /*
  1132. * pcm ops
  1133. */
  1134. static struct snd_pcm_hardware fsi_pcm_hardware = {
  1135. .info = SNDRV_PCM_INFO_INTERLEAVED |
  1136. SNDRV_PCM_INFO_MMAP |
  1137. SNDRV_PCM_INFO_MMAP_VALID,
  1138. .formats = FSI_FMTS,
  1139. .rates = FSI_RATES,
  1140. .rate_min = 8000,
  1141. .rate_max = 192000,
  1142. .channels_min = 1,
  1143. .channels_max = 2,
  1144. .buffer_bytes_max = 64 * 1024,
  1145. .period_bytes_min = 32,
  1146. .period_bytes_max = 8192,
  1147. .periods_min = 1,
  1148. .periods_max = 32,
  1149. .fifo_size = 256,
  1150. };
  1151. static int fsi_pcm_open(struct snd_pcm_substream *substream)
  1152. {
  1153. struct snd_pcm_runtime *runtime = substream->runtime;
  1154. int ret = 0;
  1155. snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
  1156. ret = snd_pcm_hw_constraint_integer(runtime,
  1157. SNDRV_PCM_HW_PARAM_PERIODS);
  1158. return ret;
  1159. }
  1160. static int fsi_hw_params(struct snd_pcm_substream *substream,
  1161. struct snd_pcm_hw_params *hw_params)
  1162. {
  1163. return snd_pcm_lib_malloc_pages(substream,
  1164. params_buffer_bytes(hw_params));
  1165. }
  1166. static int fsi_hw_free(struct snd_pcm_substream *substream)
  1167. {
  1168. return snd_pcm_lib_free_pages(substream);
  1169. }
  1170. static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
  1171. {
  1172. struct fsi_priv *fsi = fsi_get_priv(substream);
  1173. struct fsi_stream *io = fsi_stream_get(fsi, substream);
  1174. return fsi_sample2frame(fsi, io->buff_sample_pos);
  1175. }
  1176. static struct snd_pcm_ops fsi_pcm_ops = {
  1177. .open = fsi_pcm_open,
  1178. .ioctl = snd_pcm_lib_ioctl,
  1179. .hw_params = fsi_hw_params,
  1180. .hw_free = fsi_hw_free,
  1181. .pointer = fsi_pointer,
  1182. };
  1183. /*
  1184. * snd_soc_platform
  1185. */
  1186. #define PREALLOC_BUFFER (32 * 1024)
  1187. #define PREALLOC_BUFFER_MAX (32 * 1024)
  1188. static void fsi_pcm_free(struct snd_pcm *pcm)
  1189. {
  1190. snd_pcm_lib_preallocate_free_for_all(pcm);
  1191. }
  1192. static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1193. {
  1194. struct snd_pcm *pcm = rtd->pcm;
  1195. /*
  1196. * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
  1197. * in MMAP mode (i.e. aplay -M)
  1198. */
  1199. return snd_pcm_lib_preallocate_pages_for_all(
  1200. pcm,
  1201. SNDRV_DMA_TYPE_CONTINUOUS,
  1202. snd_dma_continuous_data(GFP_KERNEL),
  1203. PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
  1204. }
  1205. /*
  1206. * alsa struct
  1207. */
  1208. static struct snd_soc_dai_driver fsi_soc_dai[] = {
  1209. {
  1210. .name = "fsia-dai",
  1211. .playback = {
  1212. .rates = FSI_RATES,
  1213. .formats = FSI_FMTS,
  1214. .channels_min = 1,
  1215. .channels_max = 8,
  1216. },
  1217. .capture = {
  1218. .rates = FSI_RATES,
  1219. .formats = FSI_FMTS,
  1220. .channels_min = 1,
  1221. .channels_max = 8,
  1222. },
  1223. .ops = &fsi_dai_ops,
  1224. },
  1225. {
  1226. .name = "fsib-dai",
  1227. .playback = {
  1228. .rates = FSI_RATES,
  1229. .formats = FSI_FMTS,
  1230. .channels_min = 1,
  1231. .channels_max = 8,
  1232. },
  1233. .capture = {
  1234. .rates = FSI_RATES,
  1235. .formats = FSI_FMTS,
  1236. .channels_min = 1,
  1237. .channels_max = 8,
  1238. },
  1239. .ops = &fsi_dai_ops,
  1240. },
  1241. };
  1242. static struct snd_soc_platform_driver fsi_soc_platform = {
  1243. .ops = &fsi_pcm_ops,
  1244. .pcm_new = fsi_pcm_new,
  1245. .pcm_free = fsi_pcm_free,
  1246. };
  1247. /*
  1248. * platform function
  1249. */
  1250. static void fsi_handler_init(struct fsi_priv *fsi)
  1251. {
  1252. fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
  1253. fsi->playback.priv = fsi;
  1254. fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
  1255. fsi->capture.priv = fsi;
  1256. if (fsi->info->tx_id) {
  1257. fsi->playback.slave.slave_id = fsi->info->tx_id;
  1258. fsi->playback.handler = &fsi_dma_push_handler;
  1259. }
  1260. }
  1261. static int fsi_probe(struct platform_device *pdev)
  1262. {
  1263. struct fsi_master *master;
  1264. const struct platform_device_id *id_entry;
  1265. struct sh_fsi_platform_info *info = pdev->dev.platform_data;
  1266. struct resource *res;
  1267. unsigned int irq;
  1268. int ret;
  1269. id_entry = pdev->id_entry;
  1270. if (!id_entry) {
  1271. dev_err(&pdev->dev, "unknown fsi device\n");
  1272. return -ENODEV;
  1273. }
  1274. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1275. irq = platform_get_irq(pdev, 0);
  1276. if (!res || (int)irq <= 0) {
  1277. dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
  1278. ret = -ENODEV;
  1279. goto exit;
  1280. }
  1281. master = kzalloc(sizeof(*master), GFP_KERNEL);
  1282. if (!master) {
  1283. dev_err(&pdev->dev, "Could not allocate master\n");
  1284. ret = -ENOMEM;
  1285. goto exit;
  1286. }
  1287. master->base = ioremap_nocache(res->start, resource_size(res));
  1288. if (!master->base) {
  1289. ret = -ENXIO;
  1290. dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
  1291. goto exit_kfree;
  1292. }
  1293. /* master setting */
  1294. master->irq = irq;
  1295. master->core = (struct fsi_core *)id_entry->driver_data;
  1296. spin_lock_init(&master->lock);
  1297. /* FSI A setting */
  1298. master->fsia.base = master->base;
  1299. master->fsia.master = master;
  1300. master->fsia.info = &info->port_a;
  1301. fsi_handler_init(&master->fsia);
  1302. ret = fsi_stream_probe(&master->fsia);
  1303. if (ret < 0) {
  1304. dev_err(&pdev->dev, "FSIA stream probe failed\n");
  1305. goto exit_iounmap;
  1306. }
  1307. /* FSI B setting */
  1308. master->fsib.base = master->base + 0x40;
  1309. master->fsib.master = master;
  1310. master->fsib.info = &info->port_b;
  1311. fsi_handler_init(&master->fsib);
  1312. ret = fsi_stream_probe(&master->fsib);
  1313. if (ret < 0) {
  1314. dev_err(&pdev->dev, "FSIB stream probe failed\n");
  1315. goto exit_fsia;
  1316. }
  1317. pm_runtime_enable(&pdev->dev);
  1318. dev_set_drvdata(&pdev->dev, master);
  1319. ret = request_irq(irq, &fsi_interrupt, 0,
  1320. id_entry->name, master);
  1321. if (ret) {
  1322. dev_err(&pdev->dev, "irq request err\n");
  1323. goto exit_fsib;
  1324. }
  1325. ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
  1326. if (ret < 0) {
  1327. dev_err(&pdev->dev, "cannot snd soc register\n");
  1328. goto exit_free_irq;
  1329. }
  1330. ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
  1331. ARRAY_SIZE(fsi_soc_dai));
  1332. if (ret < 0) {
  1333. dev_err(&pdev->dev, "cannot snd dai register\n");
  1334. goto exit_snd_soc;
  1335. }
  1336. return ret;
  1337. exit_snd_soc:
  1338. snd_soc_unregister_platform(&pdev->dev);
  1339. exit_free_irq:
  1340. free_irq(irq, master);
  1341. exit_fsib:
  1342. fsi_stream_remove(&master->fsib);
  1343. exit_fsia:
  1344. fsi_stream_remove(&master->fsia);
  1345. exit_iounmap:
  1346. iounmap(master->base);
  1347. pm_runtime_disable(&pdev->dev);
  1348. exit_kfree:
  1349. kfree(master);
  1350. master = NULL;
  1351. exit:
  1352. return ret;
  1353. }
  1354. static int fsi_remove(struct platform_device *pdev)
  1355. {
  1356. struct fsi_master *master;
  1357. master = dev_get_drvdata(&pdev->dev);
  1358. free_irq(master->irq, master);
  1359. pm_runtime_disable(&pdev->dev);
  1360. snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
  1361. snd_soc_unregister_platform(&pdev->dev);
  1362. fsi_stream_remove(&master->fsia);
  1363. fsi_stream_remove(&master->fsib);
  1364. iounmap(master->base);
  1365. kfree(master);
  1366. return 0;
  1367. }
  1368. static void __fsi_suspend(struct fsi_priv *fsi,
  1369. struct fsi_stream *io,
  1370. struct device *dev)
  1371. {
  1372. if (!fsi_stream_is_working(fsi, io))
  1373. return;
  1374. fsi_stream_stop(fsi, io);
  1375. fsi_hw_shutdown(fsi, dev);
  1376. }
  1377. static void __fsi_resume(struct fsi_priv *fsi,
  1378. struct fsi_stream *io,
  1379. struct device *dev)
  1380. {
  1381. if (!fsi_stream_is_working(fsi, io))
  1382. return;
  1383. fsi_hw_startup(fsi, io, dev);
  1384. if (fsi_is_clk_master(fsi) && fsi->rate)
  1385. fsi_set_master_clk(dev, fsi, fsi->rate, 1);
  1386. fsi_stream_start(fsi, io);
  1387. }
  1388. static int fsi_suspend(struct device *dev)
  1389. {
  1390. struct fsi_master *master = dev_get_drvdata(dev);
  1391. struct fsi_priv *fsia = &master->fsia;
  1392. struct fsi_priv *fsib = &master->fsib;
  1393. __fsi_suspend(fsia, &fsia->playback, dev);
  1394. __fsi_suspend(fsia, &fsia->capture, dev);
  1395. __fsi_suspend(fsib, &fsib->playback, dev);
  1396. __fsi_suspend(fsib, &fsib->capture, dev);
  1397. return 0;
  1398. }
  1399. static int fsi_resume(struct device *dev)
  1400. {
  1401. struct fsi_master *master = dev_get_drvdata(dev);
  1402. struct fsi_priv *fsia = &master->fsia;
  1403. struct fsi_priv *fsib = &master->fsib;
  1404. __fsi_resume(fsia, &fsia->playback, dev);
  1405. __fsi_resume(fsia, &fsia->capture, dev);
  1406. __fsi_resume(fsib, &fsib->playback, dev);
  1407. __fsi_resume(fsib, &fsib->capture, dev);
  1408. return 0;
  1409. }
  1410. static struct dev_pm_ops fsi_pm_ops = {
  1411. .suspend = fsi_suspend,
  1412. .resume = fsi_resume,
  1413. };
  1414. static struct fsi_core fsi1_core = {
  1415. .ver = 1,
  1416. /* Interrupt */
  1417. .int_st = INT_ST,
  1418. .iemsk = IEMSK,
  1419. .imsk = IMSK,
  1420. };
  1421. static struct fsi_core fsi2_core = {
  1422. .ver = 2,
  1423. /* Interrupt */
  1424. .int_st = CPU_INT_ST,
  1425. .iemsk = CPU_IEMSK,
  1426. .imsk = CPU_IMSK,
  1427. .a_mclk = A_MST_CTLR,
  1428. .b_mclk = B_MST_CTLR,
  1429. };
  1430. static struct platform_device_id fsi_id_table[] = {
  1431. { "sh_fsi", (kernel_ulong_t)&fsi1_core },
  1432. { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
  1433. {},
  1434. };
  1435. MODULE_DEVICE_TABLE(platform, fsi_id_table);
  1436. static struct platform_driver fsi_driver = {
  1437. .driver = {
  1438. .name = "fsi-pcm-audio",
  1439. .pm = &fsi_pm_ops,
  1440. },
  1441. .probe = fsi_probe,
  1442. .remove = fsi_remove,
  1443. .id_table = fsi_id_table,
  1444. };
  1445. module_platform_driver(fsi_driver);
  1446. MODULE_LICENSE("GPL");
  1447. MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
  1448. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
  1449. MODULE_ALIAS("platform:fsi-pcm-audio");