fsi.c 34 KB

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