fsi.c 39 KB

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