fsi.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  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, int is_play)
  300. {
  301. int is_porta = fsi_is_port_a(fsi);
  302. u32 shift;
  303. if (is_porta)
  304. shift = is_play ? AO_SHIFT : AI_SHIFT;
  305. else
  306. shift = is_play ? BO_SHIFT : BI_SHIFT;
  307. return shift;
  308. }
  309. static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
  310. {
  311. return frames * fsi->chan_num;
  312. }
  313. static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
  314. {
  315. return samples / fsi->chan_num;
  316. }
  317. static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
  318. struct fsi_stream *io)
  319. {
  320. int is_play = fsi_stream_is_play(fsi, io);
  321. u32 status;
  322. int frames;
  323. status = is_play ?
  324. fsi_reg_read(fsi, DOFF_ST) :
  325. fsi_reg_read(fsi, DIFF_ST);
  326. frames = 0x1ff & (status >> 8);
  327. return fsi_frame2sample(fsi, frames);
  328. }
  329. static void fsi_count_fifo_err(struct fsi_priv *fsi)
  330. {
  331. u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
  332. u32 istatus = fsi_reg_read(fsi, DIFF_ST);
  333. if (ostatus & ERR_OVER)
  334. fsi->playback.oerr_num++;
  335. if (ostatus & ERR_UNDER)
  336. fsi->playback.uerr_num++;
  337. if (istatus & ERR_OVER)
  338. fsi->capture.oerr_num++;
  339. if (istatus & ERR_UNDER)
  340. fsi->capture.uerr_num++;
  341. fsi_reg_write(fsi, DOFF_ST, 0);
  342. fsi_reg_write(fsi, DIFF_ST, 0);
  343. }
  344. /*
  345. * fsi_stream_xx() function
  346. */
  347. static inline int fsi_stream_is_play(struct fsi_priv *fsi,
  348. struct fsi_stream *io)
  349. {
  350. return &fsi->playback == io;
  351. }
  352. static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
  353. int is_play)
  354. {
  355. return is_play ? &fsi->playback : &fsi->capture;
  356. }
  357. static int fsi_stream_is_working(struct fsi_priv *fsi,
  358. int is_play)
  359. {
  360. struct fsi_stream *io = fsi_stream_get(fsi, is_play);
  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. int is_play,
  375. struct snd_pcm_substream *substream)
  376. {
  377. struct fsi_stream *io = fsi_stream_get(fsi, is_play);
  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. spin_unlock_irqrestore(&master->lock, flags);
  391. }
  392. static void fsi_stream_quit(struct fsi_priv *fsi, int is_play)
  393. {
  394. struct fsi_stream *io = fsi_stream_get(fsi, is_play);
  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. io->substream = NULL;
  404. io->buff_sample_capa = 0;
  405. io->buff_sample_pos = 0;
  406. io->period_samples = 0;
  407. io->period_pos = 0;
  408. io->sample_width = 0;
  409. io->oerr_num = 0;
  410. io->uerr_num = 0;
  411. spin_unlock_irqrestore(&master->lock, flags);
  412. }
  413. static int fsi_stream_transfer(struct fsi_stream *io)
  414. {
  415. struct fsi_priv *fsi = fsi_stream_to_priv(io);
  416. if (!fsi)
  417. return -EIO;
  418. return fsi_stream_handler_call(io, transfer, fsi, io);
  419. }
  420. static int fsi_stream_probe(struct fsi_priv *fsi)
  421. {
  422. struct fsi_stream *io;
  423. int ret1, ret2;
  424. io = &fsi->playback;
  425. ret1 = fsi_stream_handler_call(io, probe, fsi, io);
  426. io = &fsi->capture;
  427. ret2 = fsi_stream_handler_call(io, probe, fsi, io);
  428. if (ret1 < 0)
  429. return ret1;
  430. if (ret2 < 0)
  431. return ret2;
  432. return 0;
  433. }
  434. static int fsi_stream_remove(struct fsi_priv *fsi)
  435. {
  436. struct fsi_stream *io;
  437. int ret1, ret2;
  438. io = &fsi->playback;
  439. ret1 = fsi_stream_handler_call(io, remove, fsi, io);
  440. io = &fsi->capture;
  441. ret2 = fsi_stream_handler_call(io, remove, fsi, io);
  442. if (ret1 < 0)
  443. return ret1;
  444. if (ret2 < 0)
  445. return ret2;
  446. return 0;
  447. }
  448. /*
  449. * pio function
  450. */
  451. static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
  452. {
  453. struct snd_pcm_runtime *runtime = io->substream->runtime;
  454. return runtime->dma_area +
  455. samples_to_bytes(runtime, io->buff_sample_pos);
  456. }
  457. static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int num)
  458. {
  459. u16 *start = (u16 *)_buf;
  460. int i;
  461. for (i = 0; i < num; i++)
  462. fsi_reg_write(fsi, DODT, ((u32)*(start + i) << 8));
  463. }
  464. static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int num)
  465. {
  466. u16 *start = (u16 *)_buf;
  467. int i;
  468. for (i = 0; i < num; i++)
  469. *(start + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
  470. }
  471. static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int num)
  472. {
  473. u32 *start = (u32 *)_buf;
  474. int i;
  475. for (i = 0; i < num; i++)
  476. fsi_reg_write(fsi, DODT, *(start + i));
  477. }
  478. static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int num)
  479. {
  480. u32 *start = (u32 *)_buf;
  481. int i;
  482. for (i = 0; i < num; i++)
  483. *(start + i) = fsi_reg_read(fsi, DIDT);
  484. }
  485. /*
  486. * irq function
  487. */
  488. static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
  489. {
  490. u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
  491. struct fsi_master *master = fsi_get_master(fsi);
  492. fsi_core_mask_set(master, imsk, data, data);
  493. fsi_core_mask_set(master, iemsk, data, data);
  494. }
  495. static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
  496. {
  497. u32 data = AB_IO(1, fsi_get_port_shift(fsi, is_play));
  498. struct fsi_master *master = fsi_get_master(fsi);
  499. fsi_core_mask_set(master, imsk, data, 0);
  500. fsi_core_mask_set(master, iemsk, data, 0);
  501. }
  502. static u32 fsi_irq_get_status(struct fsi_master *master)
  503. {
  504. return fsi_core_read(master, int_st);
  505. }
  506. static void fsi_irq_clear_status(struct fsi_priv *fsi)
  507. {
  508. u32 data = 0;
  509. struct fsi_master *master = fsi_get_master(fsi);
  510. data |= AB_IO(1, fsi_get_port_shift(fsi, 0));
  511. data |= AB_IO(1, fsi_get_port_shift(fsi, 1));
  512. /* clear interrupt factor */
  513. fsi_core_mask_set(master, int_st, data, 0);
  514. }
  515. /*
  516. * SPDIF master clock function
  517. *
  518. * These functions are used later FSI2
  519. */
  520. static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
  521. {
  522. struct fsi_master *master = fsi_get_master(fsi);
  523. u32 mask, val;
  524. if (master->core->ver < 2) {
  525. pr_err("fsi: register access err (%s)\n", __func__);
  526. return;
  527. }
  528. mask = BP | SE;
  529. val = enable ? mask : 0;
  530. fsi_is_port_a(fsi) ?
  531. fsi_core_mask_set(master, a_mclk, mask, val) :
  532. fsi_core_mask_set(master, b_mclk, mask, val);
  533. }
  534. /*
  535. * clock function
  536. */
  537. static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
  538. long rate, int enable)
  539. {
  540. struct fsi_master *master = fsi_get_master(fsi);
  541. set_rate_func set_rate = fsi_get_info_set_rate(master);
  542. int fsi_ver = master->core->ver;
  543. int ret;
  544. ret = set_rate(dev, fsi_is_port_a(fsi), rate, enable);
  545. if (ret < 0) /* error */
  546. return ret;
  547. if (!enable)
  548. return 0;
  549. if (ret > 0) {
  550. u32 data = 0;
  551. switch (ret & SH_FSI_ACKMD_MASK) {
  552. default:
  553. /* FALL THROUGH */
  554. case SH_FSI_ACKMD_512:
  555. data |= (0x0 << 12);
  556. break;
  557. case SH_FSI_ACKMD_256:
  558. data |= (0x1 << 12);
  559. break;
  560. case SH_FSI_ACKMD_128:
  561. data |= (0x2 << 12);
  562. break;
  563. case SH_FSI_ACKMD_64:
  564. data |= (0x3 << 12);
  565. break;
  566. case SH_FSI_ACKMD_32:
  567. if (fsi_ver < 2)
  568. dev_err(dev, "unsupported ACKMD\n");
  569. else
  570. data |= (0x4 << 12);
  571. break;
  572. }
  573. switch (ret & SH_FSI_BPFMD_MASK) {
  574. default:
  575. /* FALL THROUGH */
  576. case SH_FSI_BPFMD_32:
  577. data |= (0x0 << 8);
  578. break;
  579. case SH_FSI_BPFMD_64:
  580. data |= (0x1 << 8);
  581. break;
  582. case SH_FSI_BPFMD_128:
  583. data |= (0x2 << 8);
  584. break;
  585. case SH_FSI_BPFMD_256:
  586. data |= (0x3 << 8);
  587. break;
  588. case SH_FSI_BPFMD_512:
  589. data |= (0x4 << 8);
  590. break;
  591. case SH_FSI_BPFMD_16:
  592. if (fsi_ver < 2)
  593. dev_err(dev, "unsupported ACKMD\n");
  594. else
  595. data |= (0x7 << 8);
  596. break;
  597. }
  598. fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
  599. udelay(10);
  600. ret = 0;
  601. }
  602. return ret;
  603. }
  604. #define fsi_port_start(f, i) __fsi_port_clk_ctrl(f, i, 1)
  605. #define fsi_port_stop(f, i) __fsi_port_clk_ctrl(f, i, 0)
  606. static void __fsi_port_clk_ctrl(struct fsi_priv *fsi, int is_play, int enable)
  607. {
  608. struct fsi_master *master = fsi_get_master(fsi);
  609. u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
  610. if (enable)
  611. fsi_irq_enable(fsi, is_play);
  612. else
  613. fsi_irq_disable(fsi, is_play);
  614. if (fsi_is_clk_master(fsi))
  615. fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
  616. }
  617. /*
  618. * ctrl function
  619. */
  620. static int fsi_fifo_data_ctrl(struct fsi_priv *fsi, struct fsi_stream *io,
  621. void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
  622. void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
  623. int samples)
  624. {
  625. struct snd_pcm_runtime *runtime;
  626. struct snd_pcm_substream *substream;
  627. u8 *buf;
  628. int over_period;
  629. if (!fsi ||
  630. !io->substream ||
  631. !io->substream->runtime)
  632. return -EINVAL;
  633. over_period = 0;
  634. substream = io->substream;
  635. runtime = substream->runtime;
  636. /* FSI FIFO has limit.
  637. * So, this driver can not send periods data at a time
  638. */
  639. if (io->buff_sample_pos >=
  640. io->period_samples * (io->period_pos + 1)) {
  641. over_period = 1;
  642. io->period_pos = (io->period_pos + 1) % runtime->periods;
  643. if (0 == io->period_pos)
  644. io->buff_sample_pos = 0;
  645. }
  646. buf = fsi_pio_get_area(fsi, io);
  647. switch (io->sample_width) {
  648. case 2:
  649. run16(fsi, buf, samples);
  650. break;
  651. case 4:
  652. run32(fsi, buf, samples);
  653. break;
  654. default:
  655. return -EINVAL;
  656. }
  657. /* update buff_sample_pos */
  658. io->buff_sample_pos += samples;
  659. if (over_period)
  660. snd_pcm_period_elapsed(substream);
  661. return 0;
  662. }
  663. static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
  664. {
  665. int sample_residues; /* samples in FSI fifo */
  666. int sample_space; /* ALSA free samples space */
  667. int samples;
  668. sample_residues = fsi_get_current_fifo_samples(fsi, io);
  669. sample_space = io->buff_sample_capa - io->buff_sample_pos;
  670. samples = min(sample_residues, sample_space);
  671. return fsi_fifo_data_ctrl(fsi, io,
  672. fsi_pio_pop16,
  673. fsi_pio_pop32,
  674. samples);
  675. }
  676. static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
  677. {
  678. int sample_residues; /* ALSA residue samples */
  679. int sample_space; /* FSI fifo free samples space */
  680. int samples;
  681. sample_residues = io->buff_sample_capa - io->buff_sample_pos;
  682. sample_space = io->fifo_sample_capa -
  683. fsi_get_current_fifo_samples(fsi, io);
  684. samples = min(sample_residues, sample_space);
  685. return fsi_fifo_data_ctrl(fsi, io,
  686. fsi_pio_push16,
  687. fsi_pio_push32,
  688. samples);
  689. }
  690. static struct fsi_stream_handler fsi_pio_push_handler = {
  691. .transfer = fsi_pio_push,
  692. };
  693. static struct fsi_stream_handler fsi_pio_pop_handler = {
  694. .transfer = fsi_pio_pop,
  695. };
  696. static irqreturn_t fsi_interrupt(int irq, void *data)
  697. {
  698. struct fsi_master *master = data;
  699. u32 int_st = fsi_irq_get_status(master);
  700. /* clear irq status */
  701. fsi_master_mask_set(master, SOFT_RST, IR, 0);
  702. fsi_master_mask_set(master, SOFT_RST, IR, IR);
  703. if (int_st & AB_IO(1, AO_SHIFT))
  704. fsi_stream_transfer(&master->fsia.playback);
  705. if (int_st & AB_IO(1, BO_SHIFT))
  706. fsi_stream_transfer(&master->fsib.playback);
  707. if (int_st & AB_IO(1, AI_SHIFT))
  708. fsi_stream_transfer(&master->fsia.capture);
  709. if (int_st & AB_IO(1, BI_SHIFT))
  710. fsi_stream_transfer(&master->fsib.capture);
  711. fsi_count_fifo_err(&master->fsia);
  712. fsi_count_fifo_err(&master->fsib);
  713. fsi_irq_clear_status(&master->fsia);
  714. fsi_irq_clear_status(&master->fsib);
  715. return IRQ_HANDLED;
  716. }
  717. /*
  718. * dai ops
  719. */
  720. static void fsi_fifo_init(struct fsi_priv *fsi,
  721. int is_play,
  722. struct device *dev)
  723. {
  724. struct fsi_master *master = fsi_get_master(fsi);
  725. struct fsi_stream *io = fsi_stream_get(fsi, is_play);
  726. u32 shift, i;
  727. int frame_capa;
  728. /* get on-chip RAM capacity */
  729. shift = fsi_master_read(master, FIFO_SZ);
  730. shift >>= fsi_get_port_shift(fsi, is_play);
  731. shift &= FIFO_SZ_MASK;
  732. frame_capa = 256 << shift;
  733. dev_dbg(dev, "fifo = %d words\n", frame_capa);
  734. /*
  735. * The maximum number of sample data varies depending
  736. * on the number of channels selected for the format.
  737. *
  738. * FIFOs are used in 4-channel units in 3-channel mode
  739. * and in 8-channel units in 5- to 7-channel mode
  740. * meaning that more FIFOs than the required size of DPRAM
  741. * are used.
  742. *
  743. * ex) if 256 words of DP-RAM is connected
  744. * 1 channel: 256 (256 x 1 = 256)
  745. * 2 channels: 128 (128 x 2 = 256)
  746. * 3 channels: 64 ( 64 x 3 = 192)
  747. * 4 channels: 64 ( 64 x 4 = 256)
  748. * 5 channels: 32 ( 32 x 5 = 160)
  749. * 6 channels: 32 ( 32 x 6 = 192)
  750. * 7 channels: 32 ( 32 x 7 = 224)
  751. * 8 channels: 32 ( 32 x 8 = 256)
  752. */
  753. for (i = 1; i < fsi->chan_num; i <<= 1)
  754. frame_capa >>= 1;
  755. dev_dbg(dev, "%d channel %d store\n",
  756. fsi->chan_num, frame_capa);
  757. io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
  758. /*
  759. * set interrupt generation factor
  760. * clear FIFO
  761. */
  762. if (is_play) {
  763. fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
  764. fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
  765. } else {
  766. fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
  767. fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
  768. }
  769. }
  770. static int fsi_hw_startup(struct fsi_priv *fsi,
  771. int is_play,
  772. struct device *dev)
  773. {
  774. struct fsi_master *master = fsi_get_master(fsi);
  775. int fsi_ver = master->core->ver;
  776. u32 flags = fsi_get_info_flags(fsi);
  777. u32 data = 0;
  778. /* clock setting */
  779. if (fsi_is_clk_master(fsi))
  780. data = DIMD | DOMD;
  781. fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
  782. /* clock inversion (CKG2) */
  783. data = 0;
  784. if (SH_FSI_LRM_INV & flags)
  785. data |= 1 << 12;
  786. if (SH_FSI_BRM_INV & flags)
  787. data |= 1 << 8;
  788. if (SH_FSI_LRS_INV & flags)
  789. data |= 1 << 4;
  790. if (SH_FSI_BRS_INV & flags)
  791. data |= 1 << 0;
  792. fsi_reg_write(fsi, CKG2, data);
  793. /* set format */
  794. fsi_reg_write(fsi, DO_FMT, fsi->do_fmt);
  795. fsi_reg_write(fsi, DI_FMT, fsi->di_fmt);
  796. /* spdif ? */
  797. if (fsi_is_spdif(fsi)) {
  798. fsi_spdif_clk_ctrl(fsi, 1);
  799. fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
  800. }
  801. /*
  802. * FIXME
  803. *
  804. * FSI driver assumed that data package is in-back.
  805. * FSI2 chip can select it.
  806. */
  807. if (fsi_ver >= 2) {
  808. fsi_reg_write(fsi, OUT_DMAC, (1 << 4));
  809. fsi_reg_write(fsi, IN_DMAC, (1 << 4));
  810. }
  811. /* irq clear */
  812. fsi_irq_disable(fsi, is_play);
  813. fsi_irq_clear_status(fsi);
  814. /* fifo init */
  815. fsi_fifo_init(fsi, is_play, dev);
  816. return 0;
  817. }
  818. static void fsi_hw_shutdown(struct fsi_priv *fsi,
  819. struct device *dev)
  820. {
  821. if (fsi_is_clk_master(fsi))
  822. fsi_set_master_clk(dev, fsi, fsi->rate, 0);
  823. }
  824. static int fsi_dai_startup(struct snd_pcm_substream *substream,
  825. struct snd_soc_dai *dai)
  826. {
  827. struct fsi_priv *fsi = fsi_get_priv(substream);
  828. int is_play = fsi_is_play(substream);
  829. return fsi_hw_startup(fsi, is_play, dai->dev);
  830. }
  831. static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
  832. struct snd_soc_dai *dai)
  833. {
  834. struct fsi_priv *fsi = fsi_get_priv(substream);
  835. fsi_hw_shutdown(fsi, dai->dev);
  836. fsi->rate = 0;
  837. }
  838. static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  839. struct snd_soc_dai *dai)
  840. {
  841. struct fsi_priv *fsi = fsi_get_priv(substream);
  842. struct fsi_stream *io = fsi_stream_get(fsi, fsi_is_play(substream));
  843. int is_play = fsi_is_play(substream);
  844. int ret = 0;
  845. switch (cmd) {
  846. case SNDRV_PCM_TRIGGER_START:
  847. fsi_stream_init(fsi, is_play, substream);
  848. ret = fsi_stream_transfer(io);
  849. if (0 == ret)
  850. fsi_port_start(fsi, is_play);
  851. break;
  852. case SNDRV_PCM_TRIGGER_STOP:
  853. fsi_port_stop(fsi, is_play);
  854. fsi_stream_quit(fsi, is_play);
  855. break;
  856. }
  857. return ret;
  858. }
  859. static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
  860. {
  861. u32 data = 0;
  862. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  863. case SND_SOC_DAIFMT_I2S:
  864. data = CR_I2S;
  865. fsi->chan_num = 2;
  866. break;
  867. case SND_SOC_DAIFMT_LEFT_J:
  868. data = CR_PCM;
  869. fsi->chan_num = 2;
  870. break;
  871. default:
  872. return -EINVAL;
  873. }
  874. fsi->do_fmt = data;
  875. fsi->di_fmt = data;
  876. return 0;
  877. }
  878. static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
  879. {
  880. struct fsi_master *master = fsi_get_master(fsi);
  881. u32 data = 0;
  882. if (master->core->ver < 2)
  883. return -EINVAL;
  884. data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
  885. fsi->chan_num = 2;
  886. fsi->spdif = 1;
  887. fsi->do_fmt = data;
  888. fsi->di_fmt = data;
  889. return 0;
  890. }
  891. static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  892. {
  893. struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
  894. struct fsi_master *master = fsi_get_master(fsi);
  895. set_rate_func set_rate = fsi_get_info_set_rate(master);
  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, fsi_is_play(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 resource *res;
  1082. unsigned int irq;
  1083. int ret;
  1084. id_entry = pdev->id_entry;
  1085. if (!id_entry) {
  1086. dev_err(&pdev->dev, "unknown fsi device\n");
  1087. return -ENODEV;
  1088. }
  1089. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1090. irq = platform_get_irq(pdev, 0);
  1091. if (!res || (int)irq <= 0) {
  1092. dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
  1093. ret = -ENODEV;
  1094. goto exit;
  1095. }
  1096. master = kzalloc(sizeof(*master), GFP_KERNEL);
  1097. if (!master) {
  1098. dev_err(&pdev->dev, "Could not allocate master\n");
  1099. ret = -ENOMEM;
  1100. goto exit;
  1101. }
  1102. master->base = ioremap_nocache(res->start, resource_size(res));
  1103. if (!master->base) {
  1104. ret = -ENXIO;
  1105. dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
  1106. goto exit_kfree;
  1107. }
  1108. /* master setting */
  1109. master->irq = irq;
  1110. master->info = pdev->dev.platform_data;
  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. fsi_handler_init(&master->fsia);
  1117. ret = fsi_stream_probe(&master->fsia);
  1118. if (ret < 0) {
  1119. dev_err(&pdev->dev, "FSIA stream probe failed\n");
  1120. goto exit_iounmap;
  1121. }
  1122. /* FSI B setting */
  1123. master->fsib.base = master->base + 0x40;
  1124. master->fsib.master = master;
  1125. fsi_handler_init(&master->fsib);
  1126. ret = fsi_stream_probe(&master->fsib);
  1127. if (ret < 0) {
  1128. dev_err(&pdev->dev, "FSIB stream probe failed\n");
  1129. goto exit_fsia;
  1130. }
  1131. pm_runtime_enable(&pdev->dev);
  1132. dev_set_drvdata(&pdev->dev, master);
  1133. ret = request_irq(irq, &fsi_interrupt, 0,
  1134. id_entry->name, master);
  1135. if (ret) {
  1136. dev_err(&pdev->dev, "irq request err\n");
  1137. goto exit_fsib;
  1138. }
  1139. ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
  1140. if (ret < 0) {
  1141. dev_err(&pdev->dev, "cannot snd soc register\n");
  1142. goto exit_free_irq;
  1143. }
  1144. ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
  1145. ARRAY_SIZE(fsi_soc_dai));
  1146. if (ret < 0) {
  1147. dev_err(&pdev->dev, "cannot snd dai register\n");
  1148. goto exit_snd_soc;
  1149. }
  1150. return ret;
  1151. exit_snd_soc:
  1152. snd_soc_unregister_platform(&pdev->dev);
  1153. exit_free_irq:
  1154. free_irq(irq, master);
  1155. exit_fsib:
  1156. fsi_stream_remove(&master->fsib);
  1157. exit_fsia:
  1158. fsi_stream_remove(&master->fsia);
  1159. exit_iounmap:
  1160. iounmap(master->base);
  1161. pm_runtime_disable(&pdev->dev);
  1162. exit_kfree:
  1163. kfree(master);
  1164. master = NULL;
  1165. exit:
  1166. return ret;
  1167. }
  1168. static int fsi_remove(struct platform_device *pdev)
  1169. {
  1170. struct fsi_master *master;
  1171. master = dev_get_drvdata(&pdev->dev);
  1172. free_irq(master->irq, master);
  1173. pm_runtime_disable(&pdev->dev);
  1174. snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
  1175. snd_soc_unregister_platform(&pdev->dev);
  1176. fsi_stream_remove(&master->fsia);
  1177. fsi_stream_remove(&master->fsib);
  1178. iounmap(master->base);
  1179. kfree(master);
  1180. return 0;
  1181. }
  1182. static void __fsi_suspend(struct fsi_priv *fsi,
  1183. int is_play,
  1184. struct device *dev)
  1185. {
  1186. if (!fsi_stream_is_working(fsi, is_play))
  1187. return;
  1188. fsi_port_stop(fsi, is_play);
  1189. fsi_hw_shutdown(fsi, dev);
  1190. }
  1191. static void __fsi_resume(struct fsi_priv *fsi,
  1192. int is_play,
  1193. struct device *dev)
  1194. {
  1195. if (!fsi_stream_is_working(fsi, is_play))
  1196. return;
  1197. fsi_hw_startup(fsi, is_play, dev);
  1198. if (fsi_is_clk_master(fsi) && fsi->rate)
  1199. fsi_set_master_clk(dev, fsi, fsi->rate, 1);
  1200. fsi_port_start(fsi, is_play);
  1201. }
  1202. static int fsi_suspend(struct device *dev)
  1203. {
  1204. struct fsi_master *master = dev_get_drvdata(dev);
  1205. struct fsi_priv *fsia = &master->fsia;
  1206. struct fsi_priv *fsib = &master->fsib;
  1207. __fsi_suspend(fsia, 1, dev);
  1208. __fsi_suspend(fsia, 0, dev);
  1209. __fsi_suspend(fsib, 1, dev);
  1210. __fsi_suspend(fsib, 0, dev);
  1211. return 0;
  1212. }
  1213. static int fsi_resume(struct device *dev)
  1214. {
  1215. struct fsi_master *master = dev_get_drvdata(dev);
  1216. struct fsi_priv *fsia = &master->fsia;
  1217. struct fsi_priv *fsib = &master->fsib;
  1218. __fsi_resume(fsia, 1, dev);
  1219. __fsi_resume(fsia, 0, dev);
  1220. __fsi_resume(fsib, 1, dev);
  1221. __fsi_resume(fsib, 0, dev);
  1222. return 0;
  1223. }
  1224. static struct dev_pm_ops fsi_pm_ops = {
  1225. .suspend = fsi_suspend,
  1226. .resume = fsi_resume,
  1227. };
  1228. static struct fsi_core fsi1_core = {
  1229. .ver = 1,
  1230. /* Interrupt */
  1231. .int_st = INT_ST,
  1232. .iemsk = IEMSK,
  1233. .imsk = IMSK,
  1234. };
  1235. static struct fsi_core fsi2_core = {
  1236. .ver = 2,
  1237. /* Interrupt */
  1238. .int_st = CPU_INT_ST,
  1239. .iemsk = CPU_IEMSK,
  1240. .imsk = CPU_IMSK,
  1241. .a_mclk = A_MST_CTLR,
  1242. .b_mclk = B_MST_CTLR,
  1243. };
  1244. static struct platform_device_id fsi_id_table[] = {
  1245. { "sh_fsi", (kernel_ulong_t)&fsi1_core },
  1246. { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
  1247. {},
  1248. };
  1249. MODULE_DEVICE_TABLE(platform, fsi_id_table);
  1250. static struct platform_driver fsi_driver = {
  1251. .driver = {
  1252. .name = "fsi-pcm-audio",
  1253. .pm = &fsi_pm_ops,
  1254. },
  1255. .probe = fsi_probe,
  1256. .remove = fsi_remove,
  1257. .id_table = fsi_id_table,
  1258. };
  1259. module_platform_driver(fsi_driver);
  1260. MODULE_LICENSE("GPL");
  1261. MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
  1262. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
  1263. MODULE_ALIAS("platform:fsi-pcm-audio");