ssi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Renesas R-Car SSIU/SSI support
  4. //
  5. // Copyright (C) 2013 Renesas Solutions Corp.
  6. // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. //
  8. // Based on fsi.c
  9. // Kuninori Morimoto <morimoto.kuninori@renesas.com>
  10. /*
  11. * you can enable below define if you don't need
  12. * SSI interrupt status debug message when debugging
  13. * see rsnd_dbg_irq_status()
  14. *
  15. * #define RSND_DEBUG_NO_IRQ_STATUS 1
  16. */
  17. #include <sound/simple_card_utils.h>
  18. #include <linux/delay.h>
  19. #include "rsnd.h"
  20. #define RSND_SSI_NAME_SIZE 16
  21. /*
  22. * SSICR
  23. */
  24. #define FORCE (1 << 31) /* Fixed */
  25. #define DMEN (1 << 28) /* DMA Enable */
  26. #define UIEN (1 << 27) /* Underflow Interrupt Enable */
  27. #define OIEN (1 << 26) /* Overflow Interrupt Enable */
  28. #define IIEN (1 << 25) /* Idle Mode Interrupt Enable */
  29. #define DIEN (1 << 24) /* Data Interrupt Enable */
  30. #define CHNL_4 (1 << 22) /* Channels */
  31. #define CHNL_6 (2 << 22) /* Channels */
  32. #define CHNL_8 (3 << 22) /* Channels */
  33. #define DWL_MASK (7 << 19) /* Data Word Length mask */
  34. #define DWL_8 (0 << 19) /* Data Word Length */
  35. #define DWL_16 (1 << 19) /* Data Word Length */
  36. #define DWL_18 (2 << 19) /* Data Word Length */
  37. #define DWL_20 (3 << 19) /* Data Word Length */
  38. #define DWL_22 (4 << 19) /* Data Word Length */
  39. #define DWL_24 (5 << 19) /* Data Word Length */
  40. #define DWL_32 (6 << 19) /* Data Word Length */
  41. #define SWL_32 (3 << 16) /* R/W System Word Length */
  42. #define SCKD (1 << 15) /* Serial Bit Clock Direction */
  43. #define SWSD (1 << 14) /* Serial WS Direction */
  44. #define SCKP (1 << 13) /* Serial Bit Clock Polarity */
  45. #define SWSP (1 << 12) /* Serial WS Polarity */
  46. #define SDTA (1 << 10) /* Serial Data Alignment */
  47. #define PDTA (1 << 9) /* Parallel Data Alignment */
  48. #define DEL (1 << 8) /* Serial Data Delay */
  49. #define CKDV(v) (v << 4) /* Serial Clock Division Ratio */
  50. #define TRMD (1 << 1) /* Transmit/Receive Mode Select */
  51. #define EN (1 << 0) /* SSI Module Enable */
  52. /*
  53. * SSISR
  54. */
  55. #define UIRQ (1 << 27) /* Underflow Error Interrupt Status */
  56. #define OIRQ (1 << 26) /* Overflow Error Interrupt Status */
  57. #define IIRQ (1 << 25) /* Idle Mode Interrupt Status */
  58. #define DIRQ (1 << 24) /* Data Interrupt Status Flag */
  59. /*
  60. * SSIWSR
  61. */
  62. #define CONT (1 << 8) /* WS Continue Function */
  63. #define WS_MODE (1 << 0) /* WS Mode */
  64. #define SSI_NAME "ssi"
  65. struct rsnd_ssi {
  66. struct rsnd_mod mod;
  67. u32 flags;
  68. u32 cr_own;
  69. u32 cr_clk;
  70. u32 cr_mode;
  71. u32 cr_en;
  72. u32 wsr;
  73. int chan;
  74. int rate;
  75. int irq;
  76. unsigned int usrcnt;
  77. /* for PIO */
  78. int byte_pos;
  79. int byte_per_period;
  80. int next_period_byte;
  81. };
  82. /* flags */
  83. #define RSND_SSI_CLK_PIN_SHARE (1 << 0)
  84. #define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */
  85. #define RSND_SSI_HDMI0 (1 << 2) /* for HDMI0 */
  86. #define RSND_SSI_HDMI1 (1 << 3) /* for HDMI1 */
  87. #define RSND_SSI_PROBED (1 << 4)
  88. #define for_each_rsnd_ssi(pos, priv, i) \
  89. for (i = 0; \
  90. (i < rsnd_ssi_nr(priv)) && \
  91. ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
  92. i++)
  93. #define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
  94. #define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
  95. #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
  96. #define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
  97. #define rsnd_ssi_is_multi_slave(mod, io) \
  98. (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod)))
  99. #define rsnd_ssi_is_run_mods(mod, io) \
  100. (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
  101. #define rsnd_ssi_can_output_clk(mod) (!__rsnd_ssi_is_pin_sharing(mod))
  102. int rsnd_ssi_hdmi_port(struct rsnd_dai_stream *io)
  103. {
  104. struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
  105. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  106. if (rsnd_flags_has(ssi, RSND_SSI_HDMI0))
  107. return RSND_SSI_HDMI_PORT0;
  108. if (rsnd_flags_has(ssi, RSND_SSI_HDMI1))
  109. return RSND_SSI_HDMI_PORT1;
  110. return 0;
  111. }
  112. int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
  113. {
  114. struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
  115. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  116. int use_busif = 0;
  117. if (!rsnd_ssi_is_dma_mode(mod))
  118. return 0;
  119. if (!(rsnd_flags_has(ssi, RSND_SSI_NO_BUSIF)))
  120. use_busif = 1;
  121. if (rsnd_io_to_mod_src(io))
  122. use_busif = 1;
  123. return use_busif;
  124. }
  125. static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
  126. {
  127. rsnd_mod_write(mod, SSISR, 0);
  128. }
  129. static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
  130. {
  131. return rsnd_mod_read(mod, SSISR);
  132. }
  133. static void rsnd_ssi_status_check(struct rsnd_mod *mod,
  134. u32 bit)
  135. {
  136. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  137. struct device *dev = rsnd_priv_to_dev(priv);
  138. u32 status;
  139. int i;
  140. for (i = 0; i < 1024; i++) {
  141. status = rsnd_ssi_status_get(mod);
  142. if (status & bit)
  143. return;
  144. udelay(5);
  145. }
  146. dev_warn(dev, "%s[%d] status check failed\n",
  147. rsnd_mod_name(mod), rsnd_mod_id(mod));
  148. }
  149. static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
  150. {
  151. struct rsnd_mod *mod;
  152. enum rsnd_mod_type types[] = {
  153. RSND_MOD_SSIM1,
  154. RSND_MOD_SSIM2,
  155. RSND_MOD_SSIM3,
  156. };
  157. int i, mask;
  158. mask = 0;
  159. for (i = 0; i < ARRAY_SIZE(types); i++) {
  160. mod = rsnd_io_to_mod(io, types[i]);
  161. if (!mod)
  162. continue;
  163. mask |= 1 << rsnd_mod_id(mod);
  164. }
  165. return mask;
  166. }
  167. static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
  168. {
  169. struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
  170. struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
  171. u32 mods;
  172. mods = rsnd_ssi_multi_slaves_runtime(io) |
  173. 1 << rsnd_mod_id(ssi_mod);
  174. if (ssi_parent_mod)
  175. mods |= 1 << rsnd_mod_id(ssi_parent_mod);
  176. return mods;
  177. }
  178. u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
  179. {
  180. if (rsnd_runtime_is_ssi_multi(io))
  181. return rsnd_ssi_multi_slaves(io);
  182. return 0;
  183. }
  184. unsigned int rsnd_ssi_clk_query(struct rsnd_priv *priv,
  185. int param1, int param2, int *idx)
  186. {
  187. int ssi_clk_mul_table[] = {
  188. 1, 2, 4, 8, 16, 6, 12,
  189. };
  190. int j, ret;
  191. unsigned int main_rate;
  192. for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
  193. /*
  194. * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
  195. * with it is not allowed. (SSIWSR.WS_MODE with
  196. * SSICR.CKDV = 000 is not allowed either).
  197. * Skip it. See SSICR.CKDV
  198. */
  199. if (j == 0)
  200. continue;
  201. /*
  202. * this driver is assuming that
  203. * system word is 32bit x chan
  204. * see rsnd_ssi_init()
  205. */
  206. main_rate = 32 * param1 * param2 * ssi_clk_mul_table[j];
  207. ret = rsnd_adg_clk_query(priv, main_rate);
  208. if (ret < 0)
  209. continue;
  210. if (idx)
  211. *idx = j;
  212. return main_rate;
  213. }
  214. return 0;
  215. }
  216. static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
  217. struct rsnd_dai_stream *io)
  218. {
  219. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  220. struct device *dev = rsnd_priv_to_dev(priv);
  221. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  222. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  223. int chan = rsnd_runtime_channel_for_ssi(io);
  224. int idx, ret;
  225. unsigned int main_rate;
  226. unsigned int rate = rsnd_io_is_play(io) ?
  227. rsnd_src_get_out_rate(priv, io) :
  228. rsnd_src_get_in_rate(priv, io);
  229. if (!rsnd_rdai_is_clk_master(rdai))
  230. return 0;
  231. if (!rsnd_ssi_can_output_clk(mod))
  232. return 0;
  233. if (rsnd_ssi_is_multi_slave(mod, io))
  234. return 0;
  235. if (ssi->usrcnt > 0) {
  236. if (ssi->rate != rate) {
  237. dev_err(dev, "SSI parent/child should use same rate\n");
  238. return -EINVAL;
  239. }
  240. return 0;
  241. }
  242. main_rate = rsnd_ssi_clk_query(priv, rate, chan, &idx);
  243. if (!main_rate) {
  244. dev_err(dev, "unsupported clock rate\n");
  245. return -EIO;
  246. }
  247. ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
  248. if (ret < 0)
  249. return ret;
  250. /*
  251. * SSI clock will be output contiguously
  252. * by below settings.
  253. * This means, rsnd_ssi_master_clk_start()
  254. * and rsnd_ssi_register_setup() are necessary
  255. * for SSI parent
  256. *
  257. * SSICR : FORCE, SCKD, SWSD
  258. * SSIWSR : CONT
  259. */
  260. ssi->cr_clk = FORCE | SWL_32 | SCKD | SWSD | CKDV(idx);
  261. ssi->wsr = CONT;
  262. ssi->rate = rate;
  263. dev_dbg(dev, "%s[%d] outputs %u Hz\n",
  264. rsnd_mod_name(mod),
  265. rsnd_mod_id(mod), rate);
  266. return 0;
  267. }
  268. static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
  269. struct rsnd_dai_stream *io)
  270. {
  271. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  272. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  273. if (!rsnd_rdai_is_clk_master(rdai))
  274. return;
  275. if (!rsnd_ssi_can_output_clk(mod))
  276. return;
  277. if (ssi->usrcnt > 1)
  278. return;
  279. ssi->cr_clk = 0;
  280. ssi->rate = 0;
  281. rsnd_adg_ssi_clk_stop(mod);
  282. }
  283. static void rsnd_ssi_config_init(struct rsnd_mod *mod,
  284. struct rsnd_dai_stream *io)
  285. {
  286. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  287. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  288. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  289. u32 cr_own = ssi->cr_own;
  290. u32 cr_mode = ssi->cr_mode;
  291. u32 wsr = ssi->wsr;
  292. int is_tdm;
  293. is_tdm = rsnd_runtime_is_ssi_tdm(io);
  294. /*
  295. * always use 32bit system word.
  296. * see also rsnd_ssi_master_clk_enable()
  297. */
  298. cr_own |= FORCE | SWL_32;
  299. if (rdai->bit_clk_inv)
  300. cr_own |= SCKP;
  301. if (rdai->frm_clk_inv ^ is_tdm)
  302. cr_own |= SWSP;
  303. if (rdai->data_alignment)
  304. cr_own |= SDTA;
  305. if (rdai->sys_delay)
  306. cr_own |= DEL;
  307. /*
  308. * We shouldn't exchange SWSP after running.
  309. * This means, parent needs to care it.
  310. */
  311. if (rsnd_ssi_is_parent(mod, io))
  312. goto init_end;
  313. if (rsnd_io_is_play(io))
  314. cr_own |= TRMD;
  315. cr_own &= ~DWL_MASK;
  316. switch (snd_pcm_format_width(runtime->format)) {
  317. case 16:
  318. cr_own |= DWL_16;
  319. break;
  320. case 24:
  321. cr_own |= DWL_24;
  322. break;
  323. }
  324. if (rsnd_ssi_is_dma_mode(mod)) {
  325. cr_mode = UIEN | OIEN | /* over/under run */
  326. DMEN; /* DMA : enable DMA */
  327. } else {
  328. cr_mode = DIEN; /* PIO : enable Data interrupt */
  329. }
  330. /*
  331. * TDM Extend Mode
  332. * see
  333. * rsnd_ssiu_init_gen2()
  334. */
  335. wsr = ssi->wsr;
  336. if (is_tdm) {
  337. wsr |= WS_MODE;
  338. cr_own |= CHNL_8;
  339. }
  340. init_end:
  341. ssi->cr_own = cr_own;
  342. ssi->cr_mode = cr_mode;
  343. ssi->wsr = wsr;
  344. }
  345. static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
  346. {
  347. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  348. rsnd_mod_write(mod, SSIWSR, ssi->wsr);
  349. rsnd_mod_write(mod, SSICR, ssi->cr_own |
  350. ssi->cr_clk |
  351. ssi->cr_mode |
  352. ssi->cr_en);
  353. }
  354. /*
  355. * SSI mod common functions
  356. */
  357. static int rsnd_ssi_init(struct rsnd_mod *mod,
  358. struct rsnd_dai_stream *io,
  359. struct rsnd_priv *priv)
  360. {
  361. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  362. if (!rsnd_ssi_is_run_mods(mod, io))
  363. return 0;
  364. ssi->usrcnt++;
  365. rsnd_mod_power_on(mod);
  366. rsnd_ssi_config_init(mod, io);
  367. rsnd_ssi_register_setup(mod);
  368. /* clear error status */
  369. rsnd_ssi_status_clear(mod);
  370. return 0;
  371. }
  372. static int rsnd_ssi_quit(struct rsnd_mod *mod,
  373. struct rsnd_dai_stream *io,
  374. struct rsnd_priv *priv)
  375. {
  376. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  377. struct device *dev = rsnd_priv_to_dev(priv);
  378. if (!rsnd_ssi_is_run_mods(mod, io))
  379. return 0;
  380. if (!ssi->usrcnt) {
  381. dev_err(dev, "%s[%d] usrcnt error\n",
  382. rsnd_mod_name(mod), rsnd_mod_id(mod));
  383. return -EIO;
  384. }
  385. rsnd_ssi_master_clk_stop(mod, io);
  386. rsnd_mod_power_off(mod);
  387. ssi->usrcnt--;
  388. if (!ssi->usrcnt) {
  389. ssi->cr_own = 0;
  390. ssi->cr_mode = 0;
  391. ssi->wsr = 0;
  392. }
  393. return 0;
  394. }
  395. static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
  396. struct rsnd_dai_stream *io,
  397. struct snd_pcm_substream *substream,
  398. struct snd_pcm_hw_params *params)
  399. {
  400. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  401. int chan = params_channels(params);
  402. /*
  403. * snd_pcm_ops::hw_params will be called *before*
  404. * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
  405. * in 1st call.
  406. */
  407. if (ssi->usrcnt) {
  408. /*
  409. * Already working.
  410. * It will happen if SSI has parent/child connection.
  411. * it is error if child <-> parent SSI uses
  412. * different channels.
  413. */
  414. if (ssi->chan != chan)
  415. return -EIO;
  416. }
  417. ssi->chan = chan;
  418. return 0;
  419. }
  420. static int rsnd_ssi_start(struct rsnd_mod *mod,
  421. struct rsnd_dai_stream *io,
  422. struct rsnd_priv *priv)
  423. {
  424. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  425. if (!rsnd_ssi_is_run_mods(mod, io))
  426. return 0;
  427. /*
  428. * EN will be set via SSIU :: SSI_CONTROL
  429. * if Multi channel mode
  430. */
  431. if (rsnd_ssi_multi_slaves_runtime(io))
  432. return 0;
  433. /*
  434. * EN is for data output.
  435. * SSI parent EN is not needed.
  436. */
  437. if (rsnd_ssi_is_parent(mod, io))
  438. return 0;
  439. ssi->cr_en = EN;
  440. rsnd_mod_write(mod, SSICR, ssi->cr_own |
  441. ssi->cr_clk |
  442. ssi->cr_mode |
  443. ssi->cr_en);
  444. return 0;
  445. }
  446. static int rsnd_ssi_stop(struct rsnd_mod *mod,
  447. struct rsnd_dai_stream *io,
  448. struct rsnd_priv *priv)
  449. {
  450. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  451. u32 cr;
  452. if (!rsnd_ssi_is_run_mods(mod, io))
  453. return 0;
  454. if (rsnd_ssi_is_parent(mod, io))
  455. return 0;
  456. cr = ssi->cr_own |
  457. ssi->cr_clk;
  458. /*
  459. * disable all IRQ,
  460. * Playback: Wait all data was sent
  461. * Capture: It might not receave data. Do nothing
  462. */
  463. if (rsnd_io_is_play(io)) {
  464. rsnd_mod_write(mod, SSICR, cr | ssi->cr_en);
  465. rsnd_ssi_status_check(mod, DIRQ);
  466. }
  467. /* In multi-SSI mode, stop is performed by setting ssi0129 in
  468. * SSI_CONTROL to 0 (in rsnd_ssio_stop_gen2). Do nothing here.
  469. */
  470. if (rsnd_ssi_multi_slaves_runtime(io))
  471. return 0;
  472. /*
  473. * disable SSI,
  474. * and, wait idle state
  475. */
  476. rsnd_mod_write(mod, SSICR, cr); /* disabled all */
  477. rsnd_ssi_status_check(mod, IIRQ);
  478. ssi->cr_en = 0;
  479. return 0;
  480. }
  481. static int rsnd_ssi_irq(struct rsnd_mod *mod,
  482. struct rsnd_dai_stream *io,
  483. struct rsnd_priv *priv,
  484. int enable)
  485. {
  486. u32 val = 0;
  487. if (rsnd_is_gen1(priv))
  488. return 0;
  489. if (rsnd_ssi_is_parent(mod, io))
  490. return 0;
  491. if (!rsnd_ssi_is_run_mods(mod, io))
  492. return 0;
  493. if (enable)
  494. val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
  495. rsnd_mod_write(mod, SSI_INT_ENABLE, val);
  496. return 0;
  497. }
  498. static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
  499. struct rsnd_dai_stream *io);
  500. static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
  501. struct rsnd_dai_stream *io)
  502. {
  503. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  504. struct device *dev = rsnd_priv_to_dev(priv);
  505. int is_dma = rsnd_ssi_is_dma_mode(mod);
  506. u32 status;
  507. bool elapsed = false;
  508. bool stop = false;
  509. spin_lock(&priv->lock);
  510. /* ignore all cases if not working */
  511. if (!rsnd_io_is_working(io))
  512. goto rsnd_ssi_interrupt_out;
  513. status = rsnd_ssi_status_get(mod);
  514. /* PIO only */
  515. if (!is_dma && (status & DIRQ))
  516. elapsed = rsnd_ssi_pio_interrupt(mod, io);
  517. /* DMA only */
  518. if (is_dma && (status & (UIRQ | OIRQ))) {
  519. rsnd_dbg_irq_status(dev, "%s[%d] err status : 0x%08x\n",
  520. rsnd_mod_name(mod), rsnd_mod_id(mod), status);
  521. stop = true;
  522. }
  523. rsnd_ssi_status_clear(mod);
  524. rsnd_ssi_interrupt_out:
  525. spin_unlock(&priv->lock);
  526. if (elapsed)
  527. rsnd_dai_period_elapsed(io);
  528. if (stop)
  529. snd_pcm_stop_xrun(io->substream);
  530. }
  531. static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
  532. {
  533. struct rsnd_mod *mod = data;
  534. rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
  535. return IRQ_HANDLED;
  536. }
  537. /*
  538. * SSI PIO
  539. */
  540. static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
  541. struct rsnd_dai_stream *io)
  542. {
  543. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  544. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  545. if (!__rsnd_ssi_is_pin_sharing(mod))
  546. return;
  547. if (!rsnd_rdai_is_clk_master(rdai))
  548. return;
  549. if (rsnd_ssi_is_multi_slave(mod, io))
  550. return;
  551. switch (rsnd_mod_id(mod)) {
  552. case 1:
  553. case 2:
  554. rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
  555. break;
  556. case 4:
  557. rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
  558. break;
  559. case 8:
  560. rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
  561. break;
  562. }
  563. }
  564. static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
  565. struct rsnd_dai_stream *io,
  566. struct snd_soc_pcm_runtime *rtd)
  567. {
  568. /*
  569. * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
  570. * and, pcm_new will be called after it.
  571. * This function reuse pcm_new at this point.
  572. */
  573. rsnd_ssi_parent_attach(mod, io);
  574. return 0;
  575. }
  576. static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
  577. struct rsnd_dai_stream *io,
  578. struct rsnd_priv *priv)
  579. {
  580. struct device *dev = rsnd_priv_to_dev(priv);
  581. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  582. int ret;
  583. /*
  584. * SSIP/SSIU/IRQ are not needed on
  585. * SSI Multi slaves
  586. */
  587. if (rsnd_ssi_is_multi_slave(mod, io))
  588. return 0;
  589. /*
  590. * It can't judge ssi parent at this point
  591. * see rsnd_ssi_pcm_new()
  592. */
  593. ret = rsnd_ssiu_attach(io, mod);
  594. if (ret < 0)
  595. return ret;
  596. /*
  597. * SSI might be called again as PIO fallback
  598. * It is easy to manual handling for IRQ request/free
  599. *
  600. * OTOH, this function might be called many times if platform is
  601. * using MIX. It needs xxx_attach() many times on xxx_probe().
  602. * Because of it, we can't control .probe/.remove calling count by
  603. * mod->status.
  604. * But it don't need to call request_irq() many times.
  605. * Let's control it by RSND_SSI_PROBED flag.
  606. */
  607. if (!rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
  608. ret = request_irq(ssi->irq,
  609. rsnd_ssi_interrupt,
  610. IRQF_SHARED,
  611. dev_name(dev), mod);
  612. rsnd_flags_set(ssi, RSND_SSI_PROBED);
  613. }
  614. return ret;
  615. }
  616. static int rsnd_ssi_common_remove(struct rsnd_mod *mod,
  617. struct rsnd_dai_stream *io,
  618. struct rsnd_priv *priv)
  619. {
  620. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  621. struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io);
  622. /* Do nothing if non SSI (= SSI parent, multi SSI) mod */
  623. if (pure_ssi_mod != mod)
  624. return 0;
  625. /* PIO will request IRQ again */
  626. if (rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
  627. free_irq(ssi->irq, mod);
  628. rsnd_flags_del(ssi, RSND_SSI_PROBED);
  629. }
  630. return 0;
  631. }
  632. /*
  633. * SSI PIO functions
  634. */
  635. static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
  636. struct rsnd_dai_stream *io)
  637. {
  638. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  639. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  640. u32 *buf = (u32 *)(runtime->dma_area + ssi->byte_pos);
  641. int shift = 0;
  642. int byte_pos;
  643. bool elapsed = false;
  644. if (snd_pcm_format_width(runtime->format) == 24)
  645. shift = 8;
  646. /*
  647. * 8/16/32 data can be assesse to TDR/RDR register
  648. * directly as 32bit data
  649. * see rsnd_ssi_init()
  650. */
  651. if (rsnd_io_is_play(io))
  652. rsnd_mod_write(mod, SSITDR, (*buf) << shift);
  653. else
  654. *buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
  655. byte_pos = ssi->byte_pos + sizeof(*buf);
  656. if (byte_pos >= ssi->next_period_byte) {
  657. int period_pos = byte_pos / ssi->byte_per_period;
  658. if (period_pos >= runtime->periods) {
  659. byte_pos = 0;
  660. period_pos = 0;
  661. }
  662. ssi->next_period_byte = (period_pos + 1) * ssi->byte_per_period;
  663. elapsed = true;
  664. }
  665. WRITE_ONCE(ssi->byte_pos, byte_pos);
  666. return elapsed;
  667. }
  668. static int rsnd_ssi_pio_init(struct rsnd_mod *mod,
  669. struct rsnd_dai_stream *io,
  670. struct rsnd_priv *priv)
  671. {
  672. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  673. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  674. if (!rsnd_ssi_is_parent(mod, io)) {
  675. ssi->byte_pos = 0;
  676. ssi->byte_per_period = runtime->period_size *
  677. runtime->channels *
  678. samples_to_bytes(runtime, 1);
  679. ssi->next_period_byte = ssi->byte_per_period;
  680. }
  681. return rsnd_ssi_init(mod, io, priv);
  682. }
  683. static int rsnd_ssi_pio_pointer(struct rsnd_mod *mod,
  684. struct rsnd_dai_stream *io,
  685. snd_pcm_uframes_t *pointer)
  686. {
  687. struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
  688. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  689. *pointer = bytes_to_frames(runtime, READ_ONCE(ssi->byte_pos));
  690. return 0;
  691. }
  692. static int rsnd_ssi_prepare(struct rsnd_mod *mod,
  693. struct rsnd_dai_stream *io,
  694. struct rsnd_priv *priv)
  695. {
  696. return rsnd_ssi_master_clk_start(mod, io);
  697. }
  698. static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
  699. .name = SSI_NAME,
  700. .probe = rsnd_ssi_common_probe,
  701. .remove = rsnd_ssi_common_remove,
  702. .init = rsnd_ssi_pio_init,
  703. .quit = rsnd_ssi_quit,
  704. .start = rsnd_ssi_start,
  705. .stop = rsnd_ssi_stop,
  706. .irq = rsnd_ssi_irq,
  707. .pointer = rsnd_ssi_pio_pointer,
  708. .pcm_new = rsnd_ssi_pcm_new,
  709. .hw_params = rsnd_ssi_hw_params,
  710. .prepare = rsnd_ssi_prepare,
  711. };
  712. static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
  713. struct rsnd_dai_stream *io,
  714. struct rsnd_priv *priv)
  715. {
  716. int ret;
  717. /*
  718. * SSIP/SSIU/IRQ/DMA are not needed on
  719. * SSI Multi slaves
  720. */
  721. if (rsnd_ssi_is_multi_slave(mod, io))
  722. return 0;
  723. ret = rsnd_ssi_common_probe(mod, io, priv);
  724. if (ret)
  725. return ret;
  726. /* SSI probe might be called many times in MUX multi path */
  727. ret = rsnd_dma_attach(io, mod, &io->dma);
  728. return ret;
  729. }
  730. static int rsnd_ssi_fallback(struct rsnd_mod *mod,
  731. struct rsnd_dai_stream *io,
  732. struct rsnd_priv *priv)
  733. {
  734. struct device *dev = rsnd_priv_to_dev(priv);
  735. /*
  736. * fallback to PIO
  737. *
  738. * SSI .probe might be called again.
  739. * see
  740. * rsnd_rdai_continuance_probe()
  741. */
  742. mod->ops = &rsnd_ssi_pio_ops;
  743. dev_info(dev, "%s[%d] fallback to PIO mode\n",
  744. rsnd_mod_name(mod), rsnd_mod_id(mod));
  745. return 0;
  746. }
  747. static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
  748. struct rsnd_mod *mod)
  749. {
  750. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  751. int is_play = rsnd_io_is_play(io);
  752. char *name;
  753. if (rsnd_ssi_use_busif(io))
  754. name = is_play ? "rxu" : "txu";
  755. else
  756. name = is_play ? "rx" : "tx";
  757. return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
  758. mod, name);
  759. }
  760. static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
  761. .name = SSI_NAME,
  762. .dma_req = rsnd_ssi_dma_req,
  763. .probe = rsnd_ssi_dma_probe,
  764. .remove = rsnd_ssi_common_remove,
  765. .init = rsnd_ssi_init,
  766. .quit = rsnd_ssi_quit,
  767. .start = rsnd_ssi_start,
  768. .stop = rsnd_ssi_stop,
  769. .irq = rsnd_ssi_irq,
  770. .pcm_new = rsnd_ssi_pcm_new,
  771. .fallback = rsnd_ssi_fallback,
  772. .hw_params = rsnd_ssi_hw_params,
  773. .prepare = rsnd_ssi_prepare,
  774. };
  775. int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
  776. {
  777. return mod->ops == &rsnd_ssi_dma_ops;
  778. }
  779. /*
  780. * ssi mod function
  781. */
  782. static void rsnd_ssi_connect(struct rsnd_mod *mod,
  783. struct rsnd_dai_stream *io)
  784. {
  785. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  786. enum rsnd_mod_type types[] = {
  787. RSND_MOD_SSI,
  788. RSND_MOD_SSIM1,
  789. RSND_MOD_SSIM2,
  790. RSND_MOD_SSIM3,
  791. };
  792. enum rsnd_mod_type type;
  793. int i;
  794. /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
  795. for (i = 0; i < ARRAY_SIZE(types); i++) {
  796. type = types[i];
  797. if (!rsnd_io_to_mod(io, type)) {
  798. rsnd_dai_connect(mod, io, type);
  799. rsnd_rdai_channels_set(rdai, (i + 1) * 2);
  800. rsnd_rdai_ssi_lane_set(rdai, (i + 1));
  801. return;
  802. }
  803. }
  804. }
  805. void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
  806. struct device_node *playback,
  807. struct device_node *capture)
  808. {
  809. struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
  810. struct device_node *node;
  811. struct device_node *np;
  812. struct rsnd_mod *mod;
  813. int i;
  814. node = rsnd_ssi_of_node(priv);
  815. if (!node)
  816. return;
  817. i = 0;
  818. for_each_child_of_node(node, np) {
  819. mod = rsnd_ssi_mod_get(priv, i);
  820. if (np == playback)
  821. rsnd_ssi_connect(mod, &rdai->playback);
  822. if (np == capture)
  823. rsnd_ssi_connect(mod, &rdai->capture);
  824. i++;
  825. }
  826. of_node_put(node);
  827. }
  828. static void __rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
  829. struct rsnd_dai_stream *io,
  830. struct device_node *remote_ep)
  831. {
  832. struct device *dev = rsnd_priv_to_dev(priv);
  833. struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
  834. struct rsnd_ssi *ssi;
  835. struct device_node *remote_node = of_graph_get_port_parent(remote_ep);
  836. /* support Gen3 only */
  837. if (!rsnd_is_gen3(priv))
  838. return;
  839. if (!mod)
  840. return;
  841. ssi = rsnd_mod_to_ssi(mod);
  842. /* HDMI0 */
  843. if (strstr(remote_node->full_name, "hdmi@fead0000")) {
  844. rsnd_flags_set(ssi, RSND_SSI_HDMI0);
  845. dev_dbg(dev, "%s[%d] connected to HDMI0\n",
  846. rsnd_mod_name(mod), rsnd_mod_id(mod));
  847. }
  848. /* HDMI1 */
  849. if (strstr(remote_node->full_name, "hdmi@feae0000")) {
  850. rsnd_flags_set(ssi, RSND_SSI_HDMI1);
  851. dev_dbg(dev, "%s[%d] connected to HDMI1\n",
  852. rsnd_mod_name(mod), rsnd_mod_id(mod));
  853. }
  854. }
  855. void rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
  856. struct device_node *endpoint,
  857. int dai_i)
  858. {
  859. struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i);
  860. struct device_node *remote_ep;
  861. remote_ep = of_graph_get_remote_endpoint(endpoint);
  862. if (!remote_ep)
  863. return;
  864. __rsnd_ssi_parse_hdmi_connection(priv, &rdai->playback, remote_ep);
  865. __rsnd_ssi_parse_hdmi_connection(priv, &rdai->capture, remote_ep);
  866. }
  867. struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
  868. {
  869. if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
  870. id = 0;
  871. return rsnd_mod_get(rsnd_ssi_get(priv, id));
  872. }
  873. int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
  874. {
  875. if (!mod)
  876. return 0;
  877. return !!(rsnd_flags_has(rsnd_mod_to_ssi(mod), RSND_SSI_CLK_PIN_SHARE));
  878. }
  879. static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
  880. struct rsnd_mod *mod,
  881. enum rsnd_mod_type type)
  882. {
  883. /*
  884. * SSIP (= SSI parent) needs to be special, otherwise,
  885. * 2nd SSI might doesn't start. see also rsnd_mod_call()
  886. *
  887. * We can't include parent SSI status on SSI, because we don't know
  888. * how many SSI requests parent SSI. Thus, it is localed on "io" now.
  889. * ex) trouble case
  890. * Playback: SSI0
  891. * Capture : SSI1 (needs SSI0)
  892. *
  893. * 1) start Capture -> SSI0/SSI1 are started.
  894. * 2) start Playback -> SSI0 doesn't work, because it is already
  895. * marked as "started" on 1)
  896. *
  897. * OTOH, using each mod's status is good for MUX case.
  898. * It doesn't need to start in 2nd start
  899. * ex)
  900. * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
  901. * |
  902. * IO-1: SRC1 -> CTU2 -+
  903. *
  904. * 1) start IO-0 -> start SSI0
  905. * 2) start IO-1 -> SSI0 doesn't need to start, because it is
  906. * already started on 1)
  907. */
  908. if (type == RSND_MOD_SSIP)
  909. return &io->parent_ssi_status;
  910. return rsnd_mod_get_status(io, mod, type);
  911. }
  912. int rsnd_ssi_probe(struct rsnd_priv *priv)
  913. {
  914. struct device_node *node;
  915. struct device_node *np;
  916. struct device *dev = rsnd_priv_to_dev(priv);
  917. struct rsnd_mod_ops *ops;
  918. struct clk *clk;
  919. struct rsnd_ssi *ssi;
  920. char name[RSND_SSI_NAME_SIZE];
  921. int i, nr, ret;
  922. node = rsnd_ssi_of_node(priv);
  923. if (!node)
  924. return -EINVAL;
  925. nr = of_get_child_count(node);
  926. if (!nr) {
  927. ret = -EINVAL;
  928. goto rsnd_ssi_probe_done;
  929. }
  930. ssi = devm_kcalloc(dev, nr, sizeof(*ssi), GFP_KERNEL);
  931. if (!ssi) {
  932. ret = -ENOMEM;
  933. goto rsnd_ssi_probe_done;
  934. }
  935. priv->ssi = ssi;
  936. priv->ssi_nr = nr;
  937. i = 0;
  938. for_each_child_of_node(node, np) {
  939. if (!of_device_is_available(np))
  940. goto skip;
  941. ssi = rsnd_ssi_get(priv, i);
  942. snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
  943. SSI_NAME, i);
  944. clk = devm_clk_get(dev, name);
  945. if (IS_ERR(clk)) {
  946. ret = PTR_ERR(clk);
  947. of_node_put(np);
  948. goto rsnd_ssi_probe_done;
  949. }
  950. if (of_get_property(np, "shared-pin", NULL))
  951. rsnd_flags_set(ssi, RSND_SSI_CLK_PIN_SHARE);
  952. if (of_get_property(np, "no-busif", NULL))
  953. rsnd_flags_set(ssi, RSND_SSI_NO_BUSIF);
  954. ssi->irq = irq_of_parse_and_map(np, 0);
  955. if (!ssi->irq) {
  956. ret = -EINVAL;
  957. of_node_put(np);
  958. goto rsnd_ssi_probe_done;
  959. }
  960. if (of_property_read_bool(np, "pio-transfer"))
  961. ops = &rsnd_ssi_pio_ops;
  962. else
  963. ops = &rsnd_ssi_dma_ops;
  964. ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
  965. rsnd_ssi_get_status, RSND_MOD_SSI, i);
  966. if (ret) {
  967. of_node_put(np);
  968. goto rsnd_ssi_probe_done;
  969. }
  970. skip:
  971. i++;
  972. }
  973. ret = 0;
  974. rsnd_ssi_probe_done:
  975. of_node_put(node);
  976. return ret;
  977. }
  978. void rsnd_ssi_remove(struct rsnd_priv *priv)
  979. {
  980. struct rsnd_ssi *ssi;
  981. int i;
  982. for_each_rsnd_ssi(ssi, priv, i) {
  983. rsnd_mod_quit(rsnd_mod_get(ssi));
  984. }
  985. }