ssi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /*
  2. * Renesas R-Car SSIU/SSI support
  3. *
  4. * Copyright (C) 2013 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * Based on fsi.c
  8. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  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 "rsnd.h"
  16. #define RSND_SSI_NAME_SIZE 16
  17. /*
  18. * SSICR
  19. */
  20. #define FORCE (1 << 31) /* Fixed */
  21. #define DMEN (1 << 28) /* DMA Enable */
  22. #define UIEN (1 << 27) /* Underflow Interrupt Enable */
  23. #define OIEN (1 << 26) /* Overflow Interrupt Enable */
  24. #define IIEN (1 << 25) /* Idle Mode Interrupt Enable */
  25. #define DIEN (1 << 24) /* Data Interrupt Enable */
  26. #define CHNL_4 (1 << 22) /* Channels */
  27. #define CHNL_6 (2 << 22) /* Channels */
  28. #define CHNL_8 (3 << 22) /* Channels */
  29. #define DWL_8 (0 << 19) /* Data Word Length */
  30. #define DWL_16 (1 << 19) /* Data Word Length */
  31. #define DWL_18 (2 << 19) /* Data Word Length */
  32. #define DWL_20 (3 << 19) /* Data Word Length */
  33. #define DWL_22 (4 << 19) /* Data Word Length */
  34. #define DWL_24 (5 << 19) /* Data Word Length */
  35. #define DWL_32 (6 << 19) /* Data Word Length */
  36. #define SWL_32 (3 << 16) /* R/W System Word Length */
  37. #define SCKD (1 << 15) /* Serial Bit Clock Direction */
  38. #define SWSD (1 << 14) /* Serial WS Direction */
  39. #define SCKP (1 << 13) /* Serial Bit Clock Polarity */
  40. #define SWSP (1 << 12) /* Serial WS Polarity */
  41. #define SDTA (1 << 10) /* Serial Data Alignment */
  42. #define PDTA (1 << 9) /* Parallel Data Alignment */
  43. #define DEL (1 << 8) /* Serial Data Delay */
  44. #define CKDV(v) (v << 4) /* Serial Clock Division Ratio */
  45. #define TRMD (1 << 1) /* Transmit/Receive Mode Select */
  46. #define EN (1 << 0) /* SSI Module Enable */
  47. /*
  48. * SSISR
  49. */
  50. #define UIRQ (1 << 27) /* Underflow Error Interrupt Status */
  51. #define OIRQ (1 << 26) /* Overflow Error Interrupt Status */
  52. #define IIRQ (1 << 25) /* Idle Mode Interrupt Status */
  53. #define DIRQ (1 << 24) /* Data Interrupt Status Flag */
  54. /*
  55. * SSIWSR
  56. */
  57. #define CONT (1 << 8) /* WS Continue Function */
  58. #define WS_MODE (1 << 0) /* WS Mode */
  59. #define SSI_NAME "ssi"
  60. struct rsnd_ssi {
  61. struct rsnd_mod mod;
  62. struct rsnd_mod *dma;
  63. u32 flags;
  64. u32 cr_own;
  65. u32 cr_clk;
  66. u32 cr_mode;
  67. u32 wsr;
  68. int chan;
  69. int rate;
  70. int irq;
  71. unsigned int usrcnt;
  72. };
  73. /* flags */
  74. #define RSND_SSI_CLK_PIN_SHARE (1 << 0)
  75. #define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */
  76. #define for_each_rsnd_ssi(pos, priv, i) \
  77. for (i = 0; \
  78. (i < rsnd_ssi_nr(priv)) && \
  79. ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
  80. i++)
  81. #define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
  82. #define rsnd_ssi_to_dma(mod) ((ssi)->dma)
  83. #define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
  84. #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
  85. #define rsnd_ssi_mode_flags(p) ((p)->flags)
  86. #define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
  87. #define rsnd_ssi_is_multi_slave(mod, io) \
  88. (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod)))
  89. #define rsnd_ssi_is_run_mods(mod, io) \
  90. (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
  91. int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
  92. {
  93. struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
  94. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  95. int use_busif = 0;
  96. if (!rsnd_ssi_is_dma_mode(mod))
  97. return 0;
  98. if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
  99. use_busif = 1;
  100. if (rsnd_io_to_mod_src(io))
  101. use_busif = 1;
  102. return use_busif;
  103. }
  104. static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
  105. {
  106. rsnd_mod_write(mod, SSISR, 0);
  107. }
  108. static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
  109. {
  110. return rsnd_mod_read(mod, SSISR);
  111. }
  112. static void rsnd_ssi_status_check(struct rsnd_mod *mod,
  113. u32 bit)
  114. {
  115. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  116. struct device *dev = rsnd_priv_to_dev(priv);
  117. u32 status;
  118. int i;
  119. for (i = 0; i < 1024; i++) {
  120. status = rsnd_ssi_status_get(mod);
  121. if (status & bit)
  122. return;
  123. udelay(50);
  124. }
  125. dev_warn(dev, "%s[%d] status check failed\n",
  126. rsnd_mod_name(mod), rsnd_mod_id(mod));
  127. }
  128. static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
  129. {
  130. struct rsnd_mod *mod;
  131. enum rsnd_mod_type types[] = {
  132. RSND_MOD_SSIM1,
  133. RSND_MOD_SSIM2,
  134. RSND_MOD_SSIM3,
  135. };
  136. int i, mask;
  137. mask = 0;
  138. for (i = 0; i < ARRAY_SIZE(types); i++) {
  139. mod = rsnd_io_to_mod(io, types[i]);
  140. if (!mod)
  141. continue;
  142. mask |= 1 << rsnd_mod_id(mod);
  143. }
  144. return mask;
  145. }
  146. static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
  147. {
  148. struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
  149. struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
  150. return rsnd_ssi_multi_slaves_runtime(io) |
  151. 1 << rsnd_mod_id(ssi_mod) |
  152. 1 << rsnd_mod_id(ssi_parent_mod);
  153. }
  154. u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
  155. {
  156. if (rsnd_runtime_is_ssi_multi(io))
  157. return rsnd_ssi_multi_slaves(io);
  158. return 0;
  159. }
  160. static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
  161. struct rsnd_dai_stream *io)
  162. {
  163. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  164. struct device *dev = rsnd_priv_to_dev(priv);
  165. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  166. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  167. struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
  168. int chan = rsnd_runtime_channel_for_ssi(io);
  169. int j, ret;
  170. int ssi_clk_mul_table[] = {
  171. 1, 2, 4, 8, 16, 6, 12,
  172. };
  173. unsigned int main_rate;
  174. unsigned int rate = rsnd_io_is_play(io) ?
  175. rsnd_src_get_out_rate(priv, io) :
  176. rsnd_src_get_in_rate(priv, io);
  177. if (!rsnd_rdai_is_clk_master(rdai))
  178. return 0;
  179. if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
  180. return 0;
  181. if (rsnd_ssi_is_multi_slave(mod, io))
  182. return 0;
  183. if (ssi->usrcnt > 1) {
  184. if (ssi->rate != rate) {
  185. dev_err(dev, "SSI parent/child should use same rate\n");
  186. return -EINVAL;
  187. }
  188. return 0;
  189. }
  190. /*
  191. * Find best clock, and try to start ADG
  192. */
  193. for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
  194. /*
  195. * this driver is assuming that
  196. * system word is 32bit x chan
  197. * see rsnd_ssi_init()
  198. */
  199. main_rate = rate * 32 * chan * ssi_clk_mul_table[j];
  200. ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
  201. if (0 == ret) {
  202. ssi->cr_clk = FORCE | SWL_32 |
  203. SCKD | SWSD | CKDV(j);
  204. ssi->wsr = CONT;
  205. ssi->rate = rate;
  206. dev_dbg(dev, "%s[%d] outputs %u Hz\n",
  207. rsnd_mod_name(mod),
  208. rsnd_mod_id(mod), rate);
  209. return 0;
  210. }
  211. }
  212. dev_err(dev, "unsupported clock rate\n");
  213. return -EIO;
  214. }
  215. static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
  216. struct rsnd_dai_stream *io)
  217. {
  218. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  219. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  220. struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
  221. if (!rsnd_rdai_is_clk_master(rdai))
  222. return;
  223. if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
  224. return;
  225. if (ssi->usrcnt > 1)
  226. return;
  227. ssi->cr_clk = 0;
  228. ssi->rate = 0;
  229. rsnd_adg_ssi_clk_stop(mod);
  230. }
  231. static void rsnd_ssi_config_init(struct rsnd_mod *mod,
  232. struct rsnd_dai_stream *io)
  233. {
  234. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  235. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  236. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  237. u32 cr_own;
  238. u32 cr_mode;
  239. u32 wsr;
  240. int is_tdm;
  241. is_tdm = rsnd_runtime_is_ssi_tdm(io);
  242. /*
  243. * always use 32bit system word.
  244. * see also rsnd_ssi_master_clk_enable()
  245. */
  246. cr_own = FORCE | SWL_32 | PDTA;
  247. if (rdai->bit_clk_inv)
  248. cr_own |= SCKP;
  249. if (rdai->frm_clk_inv ^ is_tdm)
  250. cr_own |= SWSP;
  251. if (rdai->data_alignment)
  252. cr_own |= SDTA;
  253. if (rdai->sys_delay)
  254. cr_own |= DEL;
  255. if (rsnd_io_is_play(io))
  256. cr_own |= TRMD;
  257. switch (runtime->sample_bits) {
  258. case 16:
  259. cr_own |= DWL_16;
  260. break;
  261. case 32:
  262. cr_own |= DWL_24;
  263. break;
  264. }
  265. if (rsnd_ssi_is_dma_mode(mod)) {
  266. cr_mode = UIEN | OIEN | /* over/under run */
  267. DMEN; /* DMA : enable DMA */
  268. } else {
  269. cr_mode = DIEN; /* PIO : enable Data interrupt */
  270. }
  271. /*
  272. * TDM Extend Mode
  273. * see
  274. * rsnd_ssiu_init_gen2()
  275. */
  276. wsr = ssi->wsr;
  277. if (is_tdm) {
  278. wsr |= WS_MODE;
  279. cr_own |= CHNL_8;
  280. }
  281. ssi->cr_own = cr_own;
  282. ssi->cr_mode = cr_mode;
  283. ssi->wsr = wsr;
  284. }
  285. static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
  286. {
  287. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  288. rsnd_mod_write(mod, SSIWSR, ssi->wsr);
  289. rsnd_mod_write(mod, SSICR, ssi->cr_own |
  290. ssi->cr_clk |
  291. ssi->cr_mode); /* without EN */
  292. }
  293. /*
  294. * SSI mod common functions
  295. */
  296. static int rsnd_ssi_init(struct rsnd_mod *mod,
  297. struct rsnd_dai_stream *io,
  298. struct rsnd_priv *priv)
  299. {
  300. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  301. int ret;
  302. if (!rsnd_ssi_is_run_mods(mod, io))
  303. return 0;
  304. ssi->usrcnt++;
  305. rsnd_mod_power_on(mod);
  306. ret = rsnd_ssi_master_clk_start(mod, io);
  307. if (ret < 0)
  308. return ret;
  309. if (!rsnd_ssi_is_parent(mod, io))
  310. rsnd_ssi_config_init(mod, io);
  311. rsnd_ssi_register_setup(mod);
  312. /* clear error status */
  313. rsnd_ssi_status_clear(mod);
  314. return 0;
  315. }
  316. static int rsnd_ssi_quit(struct rsnd_mod *mod,
  317. struct rsnd_dai_stream *io,
  318. struct rsnd_priv *priv)
  319. {
  320. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  321. struct device *dev = rsnd_priv_to_dev(priv);
  322. if (!rsnd_ssi_is_run_mods(mod, io))
  323. return 0;
  324. if (!ssi->usrcnt) {
  325. dev_err(dev, "%s[%d] usrcnt error\n",
  326. rsnd_mod_name(mod), rsnd_mod_id(mod));
  327. return -EIO;
  328. }
  329. if (!rsnd_ssi_is_parent(mod, io))
  330. ssi->cr_own = 0;
  331. rsnd_ssi_master_clk_stop(mod, io);
  332. rsnd_mod_power_off(mod);
  333. ssi->usrcnt--;
  334. return 0;
  335. }
  336. static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
  337. struct rsnd_dai_stream *io,
  338. struct snd_pcm_substream *substream,
  339. struct snd_pcm_hw_params *params)
  340. {
  341. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  342. int chan = params_channels(params);
  343. /*
  344. * snd_pcm_ops::hw_params will be called *before*
  345. * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
  346. * in 1st call.
  347. */
  348. if (ssi->usrcnt) {
  349. /*
  350. * Already working.
  351. * It will happen if SSI has parent/child connection.
  352. * it is error if child <-> parent SSI uses
  353. * different channels.
  354. */
  355. if (ssi->chan != chan)
  356. return -EIO;
  357. }
  358. ssi->chan = chan;
  359. return 0;
  360. }
  361. static int rsnd_ssi_start(struct rsnd_mod *mod,
  362. struct rsnd_dai_stream *io,
  363. struct rsnd_priv *priv)
  364. {
  365. if (!rsnd_ssi_is_run_mods(mod, io))
  366. return 0;
  367. /*
  368. * EN will be set via SSIU :: SSI_CONTROL
  369. * if Multi channel mode
  370. */
  371. if (rsnd_ssi_multi_slaves_runtime(io))
  372. return 0;
  373. rsnd_mod_bset(mod, SSICR, EN, EN);
  374. return 0;
  375. }
  376. static int rsnd_ssi_stop(struct rsnd_mod *mod,
  377. struct rsnd_dai_stream *io,
  378. struct rsnd_priv *priv)
  379. {
  380. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  381. u32 cr;
  382. if (!rsnd_ssi_is_run_mods(mod, io))
  383. return 0;
  384. /*
  385. * don't stop if not last user
  386. * see also
  387. * rsnd_ssi_start
  388. * rsnd_ssi_interrupt
  389. */
  390. if (ssi->usrcnt > 1)
  391. return 0;
  392. /*
  393. * disable all IRQ,
  394. * and, wait all data was sent
  395. */
  396. cr = ssi->cr_own |
  397. ssi->cr_clk;
  398. rsnd_mod_write(mod, SSICR, cr | EN);
  399. rsnd_ssi_status_check(mod, DIRQ);
  400. /*
  401. * disable SSI,
  402. * and, wait idle state
  403. */
  404. rsnd_mod_write(mod, SSICR, cr); /* disabled all */
  405. rsnd_ssi_status_check(mod, IIRQ);
  406. return 0;
  407. }
  408. static int rsnd_ssi_irq(struct rsnd_mod *mod,
  409. struct rsnd_dai_stream *io,
  410. struct rsnd_priv *priv,
  411. int enable)
  412. {
  413. u32 val = 0;
  414. if (rsnd_is_gen1(priv))
  415. return 0;
  416. if (rsnd_ssi_is_parent(mod, io))
  417. return 0;
  418. if (!rsnd_ssi_is_run_mods(mod, io))
  419. return 0;
  420. if (enable)
  421. val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
  422. rsnd_mod_write(mod, SSI_INT_ENABLE, val);
  423. return 0;
  424. }
  425. static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
  426. struct rsnd_dai_stream *io)
  427. {
  428. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  429. int is_dma = rsnd_ssi_is_dma_mode(mod);
  430. u32 status;
  431. bool elapsed = false;
  432. bool stop = false;
  433. spin_lock(&priv->lock);
  434. /* ignore all cases if not working */
  435. if (!rsnd_io_is_working(io))
  436. goto rsnd_ssi_interrupt_out;
  437. status = rsnd_ssi_status_get(mod);
  438. /* PIO only */
  439. if (!is_dma && (status & DIRQ)) {
  440. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  441. u32 *buf = (u32 *)(runtime->dma_area +
  442. rsnd_dai_pointer_offset(io, 0));
  443. /*
  444. * 8/16/32 data can be assesse to TDR/RDR register
  445. * directly as 32bit data
  446. * see rsnd_ssi_init()
  447. */
  448. if (rsnd_io_is_play(io))
  449. rsnd_mod_write(mod, SSITDR, *buf);
  450. else
  451. *buf = rsnd_mod_read(mod, SSIRDR);
  452. elapsed = rsnd_dai_pointer_update(io, sizeof(*buf));
  453. }
  454. /* DMA only */
  455. if (is_dma && (status & (UIRQ | OIRQ)))
  456. stop = true;
  457. rsnd_ssi_status_clear(mod);
  458. rsnd_ssi_interrupt_out:
  459. spin_unlock(&priv->lock);
  460. if (elapsed)
  461. rsnd_dai_period_elapsed(io);
  462. if (stop)
  463. snd_pcm_stop_xrun(io->substream);
  464. }
  465. static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
  466. {
  467. struct rsnd_mod *mod = data;
  468. rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
  469. return IRQ_HANDLED;
  470. }
  471. /*
  472. * SSI PIO
  473. */
  474. static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
  475. struct rsnd_dai_stream *io)
  476. {
  477. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  478. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  479. if (!__rsnd_ssi_is_pin_sharing(mod))
  480. return;
  481. if (!rsnd_rdai_is_clk_master(rdai))
  482. return;
  483. switch (rsnd_mod_id(mod)) {
  484. case 1:
  485. case 2:
  486. rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
  487. break;
  488. case 4:
  489. rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
  490. break;
  491. case 8:
  492. rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
  493. break;
  494. }
  495. }
  496. static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
  497. struct rsnd_dai_stream *io,
  498. struct snd_soc_pcm_runtime *rtd)
  499. {
  500. /*
  501. * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
  502. * and, pcm_new will be called after it.
  503. * This function reuse pcm_new at this point.
  504. */
  505. rsnd_ssi_parent_attach(mod, io);
  506. return 0;
  507. }
  508. static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
  509. struct rsnd_dai_stream *io,
  510. struct rsnd_priv *priv)
  511. {
  512. struct device *dev = rsnd_priv_to_dev(priv);
  513. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  514. int ret;
  515. /*
  516. * SSIP/SSIU/IRQ are not needed on
  517. * SSI Multi slaves
  518. */
  519. if (rsnd_ssi_is_multi_slave(mod, io))
  520. return 0;
  521. /*
  522. * It can't judge ssi parent at this point
  523. * see rsnd_ssi_pcm_new()
  524. */
  525. ret = rsnd_ssiu_attach(io, mod);
  526. if (ret < 0)
  527. return ret;
  528. /*
  529. * SSI might be called again as PIO fallback
  530. * It is easy to manual handling for IRQ request/free
  531. */
  532. ret = request_irq(ssi->irq,
  533. rsnd_ssi_interrupt,
  534. IRQF_SHARED,
  535. dev_name(dev), mod);
  536. return ret;
  537. }
  538. static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
  539. .name = SSI_NAME,
  540. .probe = rsnd_ssi_common_probe,
  541. .init = rsnd_ssi_init,
  542. .quit = rsnd_ssi_quit,
  543. .start = rsnd_ssi_start,
  544. .stop = rsnd_ssi_stop,
  545. .irq = rsnd_ssi_irq,
  546. .pcm_new = rsnd_ssi_pcm_new,
  547. .hw_params = rsnd_ssi_hw_params,
  548. };
  549. static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
  550. struct rsnd_dai_stream *io,
  551. struct rsnd_priv *priv)
  552. {
  553. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  554. int ret;
  555. /*
  556. * SSIP/SSIU/IRQ/DMA are not needed on
  557. * SSI Multi slaves
  558. */
  559. if (rsnd_ssi_is_multi_slave(mod, io))
  560. return 0;
  561. ret = rsnd_ssi_common_probe(mod, io, priv);
  562. if (ret)
  563. return ret;
  564. /* SSI probe might be called many times in MUX multi path */
  565. ret = rsnd_dma_attach(io, mod, &ssi->dma);
  566. return ret;
  567. }
  568. static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
  569. struct rsnd_dai_stream *io,
  570. struct rsnd_priv *priv)
  571. {
  572. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  573. /* PIO will request IRQ again */
  574. free_irq(ssi->irq, mod);
  575. return 0;
  576. }
  577. static int rsnd_ssi_fallback(struct rsnd_mod *mod,
  578. struct rsnd_dai_stream *io,
  579. struct rsnd_priv *priv)
  580. {
  581. struct device *dev = rsnd_priv_to_dev(priv);
  582. /*
  583. * fallback to PIO
  584. *
  585. * SSI .probe might be called again.
  586. * see
  587. * rsnd_rdai_continuance_probe()
  588. */
  589. mod->ops = &rsnd_ssi_pio_ops;
  590. dev_info(dev, "%s[%d] fallback to PIO mode\n",
  591. rsnd_mod_name(mod), rsnd_mod_id(mod));
  592. return 0;
  593. }
  594. static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
  595. struct rsnd_mod *mod)
  596. {
  597. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  598. int is_play = rsnd_io_is_play(io);
  599. char *name;
  600. if (rsnd_ssi_use_busif(io))
  601. name = is_play ? "rxu" : "txu";
  602. else
  603. name = is_play ? "rx" : "tx";
  604. return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
  605. mod, name);
  606. }
  607. static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
  608. .name = SSI_NAME,
  609. .dma_req = rsnd_ssi_dma_req,
  610. .probe = rsnd_ssi_dma_probe,
  611. .remove = rsnd_ssi_dma_remove,
  612. .init = rsnd_ssi_init,
  613. .quit = rsnd_ssi_quit,
  614. .start = rsnd_ssi_start,
  615. .stop = rsnd_ssi_stop,
  616. .irq = rsnd_ssi_irq,
  617. .pcm_new = rsnd_ssi_pcm_new,
  618. .fallback = rsnd_ssi_fallback,
  619. .hw_params = rsnd_ssi_hw_params,
  620. };
  621. int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
  622. {
  623. return mod->ops == &rsnd_ssi_dma_ops;
  624. }
  625. /*
  626. * Non SSI
  627. */
  628. static struct rsnd_mod_ops rsnd_ssi_non_ops = {
  629. .name = SSI_NAME,
  630. };
  631. /*
  632. * ssi mod function
  633. */
  634. static void rsnd_ssi_connect(struct rsnd_mod *mod,
  635. struct rsnd_dai_stream *io)
  636. {
  637. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  638. enum rsnd_mod_type types[] = {
  639. RSND_MOD_SSI,
  640. RSND_MOD_SSIM1,
  641. RSND_MOD_SSIM2,
  642. RSND_MOD_SSIM3,
  643. };
  644. enum rsnd_mod_type type;
  645. int i;
  646. /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
  647. for (i = 0; i < ARRAY_SIZE(types); i++) {
  648. type = types[i];
  649. if (!rsnd_io_to_mod(io, type)) {
  650. rsnd_dai_connect(mod, io, type);
  651. rsnd_set_slot(rdai, 2 * (i + 1), (i + 1));
  652. return;
  653. }
  654. }
  655. }
  656. void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
  657. struct device_node *playback,
  658. struct device_node *capture)
  659. {
  660. struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
  661. struct device_node *node;
  662. struct device_node *np;
  663. struct rsnd_mod *mod;
  664. int i;
  665. node = rsnd_ssi_of_node(priv);
  666. if (!node)
  667. return;
  668. i = 0;
  669. for_each_child_of_node(node, np) {
  670. mod = rsnd_ssi_mod_get(priv, i);
  671. if (np == playback)
  672. rsnd_ssi_connect(mod, &rdai->playback);
  673. if (np == capture)
  674. rsnd_ssi_connect(mod, &rdai->capture);
  675. i++;
  676. }
  677. of_node_put(node);
  678. }
  679. struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
  680. {
  681. if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
  682. id = 0;
  683. return rsnd_mod_get(rsnd_ssi_get(priv, id));
  684. }
  685. int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
  686. {
  687. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  688. return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
  689. }
  690. static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
  691. struct rsnd_mod *mod,
  692. enum rsnd_mod_type type)
  693. {
  694. /*
  695. * SSIP (= SSI parent) needs to be special, otherwise,
  696. * 2nd SSI might doesn't start. see also rsnd_mod_call()
  697. *
  698. * We can't include parent SSI status on SSI, because we don't know
  699. * how many SSI requests parent SSI. Thus, it is localed on "io" now.
  700. * ex) trouble case
  701. * Playback: SSI0
  702. * Capture : SSI1 (needs SSI0)
  703. *
  704. * 1) start Capture -> SSI0/SSI1 are started.
  705. * 2) start Playback -> SSI0 doesn't work, because it is already
  706. * marked as "started" on 1)
  707. *
  708. * OTOH, using each mod's status is good for MUX case.
  709. * It doesn't need to start in 2nd start
  710. * ex)
  711. * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
  712. * |
  713. * IO-1: SRC1 -> CTU2 -+
  714. *
  715. * 1) start IO-0 -> start SSI0
  716. * 2) start IO-1 -> SSI0 doesn't need to start, because it is
  717. * already started on 1)
  718. */
  719. if (type == RSND_MOD_SSIP)
  720. return &io->parent_ssi_status;
  721. return rsnd_mod_get_status(io, mod, type);
  722. }
  723. int rsnd_ssi_probe(struct rsnd_priv *priv)
  724. {
  725. struct device_node *node;
  726. struct device_node *np;
  727. struct device *dev = rsnd_priv_to_dev(priv);
  728. struct rsnd_mod_ops *ops;
  729. struct clk *clk;
  730. struct rsnd_ssi *ssi;
  731. char name[RSND_SSI_NAME_SIZE];
  732. int i, nr, ret;
  733. node = rsnd_ssi_of_node(priv);
  734. if (!node)
  735. return -EINVAL;
  736. nr = of_get_child_count(node);
  737. if (!nr) {
  738. ret = -EINVAL;
  739. goto rsnd_ssi_probe_done;
  740. }
  741. ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
  742. if (!ssi) {
  743. ret = -ENOMEM;
  744. goto rsnd_ssi_probe_done;
  745. }
  746. priv->ssi = ssi;
  747. priv->ssi_nr = nr;
  748. i = 0;
  749. for_each_child_of_node(node, np) {
  750. ssi = rsnd_ssi_get(priv, i);
  751. snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
  752. SSI_NAME, i);
  753. clk = devm_clk_get(dev, name);
  754. if (IS_ERR(clk)) {
  755. ret = PTR_ERR(clk);
  756. goto rsnd_ssi_probe_done;
  757. }
  758. if (of_get_property(np, "shared-pin", NULL))
  759. ssi->flags |= RSND_SSI_CLK_PIN_SHARE;
  760. if (of_get_property(np, "no-busif", NULL))
  761. ssi->flags |= RSND_SSI_NO_BUSIF;
  762. ssi->irq = irq_of_parse_and_map(np, 0);
  763. if (!ssi->irq) {
  764. ret = -EINVAL;
  765. goto rsnd_ssi_probe_done;
  766. }
  767. ops = &rsnd_ssi_non_ops;
  768. if (of_property_read_bool(np, "pio-transfer"))
  769. ops = &rsnd_ssi_pio_ops;
  770. else
  771. ops = &rsnd_ssi_dma_ops;
  772. ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
  773. rsnd_ssi_get_status, RSND_MOD_SSI, i);
  774. if (ret)
  775. goto rsnd_ssi_probe_done;
  776. i++;
  777. }
  778. ret = 0;
  779. rsnd_ssi_probe_done:
  780. of_node_put(node);
  781. return ret;
  782. }
  783. void rsnd_ssi_remove(struct rsnd_priv *priv)
  784. {
  785. struct rsnd_ssi *ssi;
  786. int i;
  787. for_each_rsnd_ssi(ssi, priv, i) {
  788. rsnd_mod_quit(rsnd_mod_get(ssi));
  789. }
  790. }