fsi.c 33 KB

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