adg.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * Helper routines for R-Car sound ADG.
  3. *
  4. * Copyright (C) 2013 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/clk-provider.h>
  11. #include "rsnd.h"
  12. #define CLKA 0
  13. #define CLKB 1
  14. #define CLKC 2
  15. #define CLKI 3
  16. #define CLKMAX 4
  17. #define CLKOUT 0
  18. #define CLKOUT1 1
  19. #define CLKOUT2 2
  20. #define CLKOUT3 3
  21. #define CLKOUTMAX 4
  22. #define BRRx_MASK(x) (0x3FF & x)
  23. static struct rsnd_mod_ops adg_ops = {
  24. .name = "adg",
  25. };
  26. struct rsnd_adg {
  27. struct clk *clk[CLKMAX];
  28. struct clk *clkout[CLKOUTMAX];
  29. struct clk_onecell_data onecell;
  30. struct rsnd_mod mod;
  31. u32 flags;
  32. int rbga_rate_for_441khz; /* RBGA */
  33. int rbgb_rate_for_48khz; /* RBGB */
  34. };
  35. #define LRCLK_ASYNC (1 << 0)
  36. #define adg_mode_flags(adg) (adg->flags)
  37. #define for_each_rsnd_clk(pos, adg, i) \
  38. for (i = 0; \
  39. (i < CLKMAX) && \
  40. ((pos) = adg->clk[i]); \
  41. i++)
  42. #define for_each_rsnd_clkout(pos, adg, i) \
  43. for (i = 0; \
  44. (i < CLKOUTMAX) && \
  45. ((pos) = adg->clkout[i]); \
  46. i++)
  47. #define rsnd_priv_to_adg(priv) ((struct rsnd_adg *)(priv)->adg)
  48. static u32 rsnd_adg_calculate_rbgx(unsigned long div)
  49. {
  50. int i, ratio;
  51. if (!div)
  52. return 0;
  53. for (i = 3; i >= 0; i--) {
  54. ratio = 2 << (i * 2);
  55. if (0 == (div % ratio))
  56. return (u32)((i << 8) | ((div / ratio) - 1));
  57. }
  58. return ~0;
  59. }
  60. static u32 rsnd_adg_ssi_ws_timing_gen2(struct rsnd_dai_stream *io)
  61. {
  62. struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
  63. int id = rsnd_mod_id(ssi_mod);
  64. int ws = id;
  65. if (rsnd_ssi_is_pin_sharing(io)) {
  66. switch (id) {
  67. case 1:
  68. case 2:
  69. ws = 0;
  70. break;
  71. case 4:
  72. ws = 3;
  73. break;
  74. case 8:
  75. ws = 7;
  76. break;
  77. }
  78. }
  79. return (0x6 + ws) << 8;
  80. }
  81. static void __rsnd_adg_get_timesel_ratio(struct rsnd_priv *priv,
  82. struct rsnd_dai_stream *io,
  83. unsigned int target_rate,
  84. unsigned int *target_val,
  85. unsigned int *target_en)
  86. {
  87. struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
  88. struct device *dev = rsnd_priv_to_dev(priv);
  89. int idx, sel, div, step;
  90. unsigned int val, en;
  91. unsigned int min, diff;
  92. unsigned int sel_rate[] = {
  93. clk_get_rate(adg->clk[CLKA]), /* 0000: CLKA */
  94. clk_get_rate(adg->clk[CLKB]), /* 0001: CLKB */
  95. clk_get_rate(adg->clk[CLKC]), /* 0010: CLKC */
  96. adg->rbga_rate_for_441khz, /* 0011: RBGA */
  97. adg->rbgb_rate_for_48khz, /* 0100: RBGB */
  98. };
  99. min = ~0;
  100. val = 0;
  101. en = 0;
  102. for (sel = 0; sel < ARRAY_SIZE(sel_rate); sel++) {
  103. idx = 0;
  104. step = 2;
  105. if (!sel_rate[sel])
  106. continue;
  107. for (div = 2; div <= 98304; div += step) {
  108. diff = abs(target_rate - sel_rate[sel] / div);
  109. if (min > diff) {
  110. val = (sel << 8) | idx;
  111. min = diff;
  112. en = 1 << (sel + 1); /* fixme */
  113. }
  114. /*
  115. * step of 0_0000 / 0_0001 / 0_1101
  116. * are out of order
  117. */
  118. if ((idx > 2) && (idx % 2))
  119. step *= 2;
  120. if (idx == 0x1c) {
  121. div += step;
  122. step *= 2;
  123. }
  124. idx++;
  125. }
  126. }
  127. if (min == ~0) {
  128. dev_err(dev, "no Input clock\n");
  129. return;
  130. }
  131. *target_val = val;
  132. if (target_en)
  133. *target_en = en;
  134. }
  135. static void rsnd_adg_get_timesel_ratio(struct rsnd_priv *priv,
  136. struct rsnd_dai_stream *io,
  137. unsigned int in_rate,
  138. unsigned int out_rate,
  139. u32 *in, u32 *out, u32 *en)
  140. {
  141. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  142. unsigned int target_rate;
  143. u32 *target_val;
  144. u32 _in;
  145. u32 _out;
  146. u32 _en;
  147. /* default = SSI WS */
  148. _in =
  149. _out = rsnd_adg_ssi_ws_timing_gen2(io);
  150. target_rate = 0;
  151. target_val = NULL;
  152. _en = 0;
  153. if (runtime->rate != in_rate) {
  154. target_rate = out_rate;
  155. target_val = &_out;
  156. } else if (runtime->rate != out_rate) {
  157. target_rate = in_rate;
  158. target_val = &_in;
  159. }
  160. if (target_rate)
  161. __rsnd_adg_get_timesel_ratio(priv, io,
  162. target_rate,
  163. target_val, &_en);
  164. if (in)
  165. *in = _in;
  166. if (out)
  167. *out = _out;
  168. if (en)
  169. *en = _en;
  170. }
  171. int rsnd_adg_set_cmd_timsel_gen2(struct rsnd_mod *cmd_mod,
  172. struct rsnd_dai_stream *io)
  173. {
  174. struct rsnd_priv *priv = rsnd_mod_to_priv(cmd_mod);
  175. struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
  176. struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
  177. int id = rsnd_mod_id(cmd_mod);
  178. int shift = (id % 2) ? 16 : 0;
  179. u32 mask, val;
  180. rsnd_adg_get_timesel_ratio(priv, io,
  181. rsnd_src_get_in_rate(priv, io),
  182. rsnd_src_get_out_rate(priv, io),
  183. NULL, &val, NULL);
  184. val = val << shift;
  185. mask = 0xffff << shift;
  186. rsnd_mod_bset(adg_mod, CMDOUT_TIMSEL, mask, val);
  187. return 0;
  188. }
  189. int rsnd_adg_set_src_timesel_gen2(struct rsnd_mod *src_mod,
  190. struct rsnd_dai_stream *io,
  191. unsigned int in_rate,
  192. unsigned int out_rate)
  193. {
  194. struct rsnd_priv *priv = rsnd_mod_to_priv(src_mod);
  195. struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
  196. struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
  197. u32 in, out;
  198. u32 mask, en;
  199. int id = rsnd_mod_id(src_mod);
  200. int shift = (id % 2) ? 16 : 0;
  201. rsnd_mod_confirm_src(src_mod);
  202. rsnd_adg_get_timesel_ratio(priv, io,
  203. in_rate, out_rate,
  204. &in, &out, &en);
  205. in = in << shift;
  206. out = out << shift;
  207. mask = 0xffff << shift;
  208. switch (id / 2) {
  209. case 0:
  210. rsnd_mod_bset(adg_mod, SRCIN_TIMSEL0, mask, in);
  211. rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL0, mask, out);
  212. break;
  213. case 1:
  214. rsnd_mod_bset(adg_mod, SRCIN_TIMSEL1, mask, in);
  215. rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL1, mask, out);
  216. break;
  217. case 2:
  218. rsnd_mod_bset(adg_mod, SRCIN_TIMSEL2, mask, in);
  219. rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL2, mask, out);
  220. break;
  221. case 3:
  222. rsnd_mod_bset(adg_mod, SRCIN_TIMSEL3, mask, in);
  223. rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL3, mask, out);
  224. break;
  225. case 4:
  226. rsnd_mod_bset(adg_mod, SRCIN_TIMSEL4, mask, in);
  227. rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL4, mask, out);
  228. break;
  229. }
  230. if (en)
  231. rsnd_mod_bset(adg_mod, DIV_EN, en, en);
  232. return 0;
  233. }
  234. static void rsnd_adg_set_ssi_clk(struct rsnd_mod *ssi_mod, u32 val)
  235. {
  236. struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
  237. struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
  238. struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
  239. int id = rsnd_mod_id(ssi_mod);
  240. int shift = (id % 4) * 8;
  241. u32 mask = 0xFF << shift;
  242. rsnd_mod_confirm_ssi(ssi_mod);
  243. val = val << shift;
  244. /*
  245. * SSI 8 is not connected to ADG.
  246. * it works with SSI 7
  247. */
  248. if (id == 8)
  249. return;
  250. switch (id / 4) {
  251. case 0:
  252. rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL0, mask, val);
  253. break;
  254. case 1:
  255. rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL1, mask, val);
  256. break;
  257. case 2:
  258. rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL2, mask, val);
  259. break;
  260. }
  261. }
  262. int rsnd_adg_ssi_clk_stop(struct rsnd_mod *ssi_mod)
  263. {
  264. rsnd_adg_set_ssi_clk(ssi_mod, 0);
  265. return 0;
  266. }
  267. int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *ssi_mod, unsigned int rate)
  268. {
  269. struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
  270. struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
  271. struct device *dev = rsnd_priv_to_dev(priv);
  272. struct clk *clk;
  273. int i;
  274. u32 data;
  275. int sel_table[] = {
  276. [CLKA] = 0x1,
  277. [CLKB] = 0x2,
  278. [CLKC] = 0x3,
  279. [CLKI] = 0x0,
  280. };
  281. dev_dbg(dev, "request clock = %d\n", rate);
  282. /*
  283. * find suitable clock from
  284. * AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC/AUDIO_CLKI.
  285. */
  286. data = 0;
  287. for_each_rsnd_clk(clk, adg, i) {
  288. if (rate == clk_get_rate(clk)) {
  289. data = sel_table[i];
  290. goto found_clock;
  291. }
  292. }
  293. /*
  294. * find divided clock from BRGA/BRGB
  295. */
  296. if (rate == adg->rbga_rate_for_441khz) {
  297. data = 0x10;
  298. goto found_clock;
  299. }
  300. if (rate == adg->rbgb_rate_for_48khz) {
  301. data = 0x20;
  302. goto found_clock;
  303. }
  304. return -EIO;
  305. found_clock:
  306. rsnd_adg_set_ssi_clk(ssi_mod, data);
  307. if (!(adg_mode_flags(adg) & LRCLK_ASYNC)) {
  308. struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
  309. u32 ckr = 0;
  310. if (0 == (rate % 8000))
  311. ckr = 0x80000000;
  312. rsnd_mod_bset(adg_mod, SSICKR, 0x80000000, ckr);
  313. }
  314. dev_dbg(dev, "ADG: %s[%d] selects 0x%x for %d\n",
  315. rsnd_mod_name(ssi_mod), rsnd_mod_id(ssi_mod),
  316. data, rate);
  317. return 0;
  318. }
  319. static void rsnd_adg_get_clkin(struct rsnd_priv *priv,
  320. struct rsnd_adg *adg)
  321. {
  322. struct device *dev = rsnd_priv_to_dev(priv);
  323. struct clk *clk;
  324. static const char * const clk_name[] = {
  325. [CLKA] = "clk_a",
  326. [CLKB] = "clk_b",
  327. [CLKC] = "clk_c",
  328. [CLKI] = "clk_i",
  329. };
  330. int i, ret;
  331. for (i = 0; i < CLKMAX; i++) {
  332. clk = devm_clk_get(dev, clk_name[i]);
  333. adg->clk[i] = IS_ERR(clk) ? NULL : clk;
  334. }
  335. for_each_rsnd_clk(clk, adg, i) {
  336. ret = clk_prepare_enable(clk);
  337. if (ret < 0)
  338. dev_warn(dev, "can't use clk %d\n", i);
  339. dev_dbg(dev, "clk %d : %p : %ld\n", i, clk, clk_get_rate(clk));
  340. }
  341. }
  342. static void rsnd_adg_get_clkout(struct rsnd_priv *priv,
  343. struct rsnd_adg *adg)
  344. {
  345. struct clk *clk;
  346. struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
  347. struct device *dev = rsnd_priv_to_dev(priv);
  348. struct device_node *np = dev->of_node;
  349. u32 ckr, rbgx, rbga, rbgb;
  350. u32 rate, req_rate = 0, div;
  351. uint32_t count = 0;
  352. unsigned long req_48kHz_rate, req_441kHz_rate;
  353. int i;
  354. const char *parent_clk_name = NULL;
  355. static const char * const clkout_name[] = {
  356. [CLKOUT] = "audio_clkout",
  357. [CLKOUT1] = "audio_clkout1",
  358. [CLKOUT2] = "audio_clkout2",
  359. [CLKOUT3] = "audio_clkout3",
  360. };
  361. int brg_table[] = {
  362. [CLKA] = 0x0,
  363. [CLKB] = 0x1,
  364. [CLKC] = 0x4,
  365. [CLKI] = 0x2,
  366. };
  367. of_property_read_u32(np, "#clock-cells", &count);
  368. /*
  369. * ADG supports BRRA/BRRB output only
  370. * this means all clkout0/1/2/3 will be same rate
  371. */
  372. of_property_read_u32(np, "clock-frequency", &req_rate);
  373. req_48kHz_rate = 0;
  374. req_441kHz_rate = 0;
  375. if (0 == (req_rate % 44100))
  376. req_441kHz_rate = req_rate;
  377. if (0 == (req_rate % 48000))
  378. req_48kHz_rate = req_rate;
  379. /*
  380. * This driver is assuming that AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC
  381. * have 44.1kHz or 48kHz base clocks for now.
  382. *
  383. * SSI itself can divide parent clock by 1/1 - 1/16
  384. * see
  385. * rsnd_adg_ssi_clk_try_start()
  386. * rsnd_ssi_master_clk_start()
  387. */
  388. ckr = 0;
  389. rbga = 2; /* default 1/6 */
  390. rbgb = 2; /* default 1/6 */
  391. adg->rbga_rate_for_441khz = 0;
  392. adg->rbgb_rate_for_48khz = 0;
  393. for_each_rsnd_clk(clk, adg, i) {
  394. rate = clk_get_rate(clk);
  395. if (0 == rate) /* not used */
  396. continue;
  397. /* RBGA */
  398. if (!adg->rbga_rate_for_441khz && (0 == rate % 44100)) {
  399. div = 6;
  400. if (req_441kHz_rate)
  401. div = rate / req_441kHz_rate;
  402. rbgx = rsnd_adg_calculate_rbgx(div);
  403. if (BRRx_MASK(rbgx) == rbgx) {
  404. rbga = rbgx;
  405. adg->rbga_rate_for_441khz = rate / div;
  406. ckr |= brg_table[i] << 20;
  407. if (req_441kHz_rate)
  408. parent_clk_name = __clk_get_name(clk);
  409. }
  410. }
  411. /* RBGB */
  412. if (!adg->rbgb_rate_for_48khz && (0 == rate % 48000)) {
  413. div = 6;
  414. if (req_48kHz_rate)
  415. div = rate / req_48kHz_rate;
  416. rbgx = rsnd_adg_calculate_rbgx(div);
  417. if (BRRx_MASK(rbgx) == rbgx) {
  418. rbgb = rbgx;
  419. adg->rbgb_rate_for_48khz = rate / div;
  420. ckr |= brg_table[i] << 16;
  421. if (req_48kHz_rate) {
  422. parent_clk_name = __clk_get_name(clk);
  423. ckr |= 0x80000000;
  424. }
  425. }
  426. }
  427. }
  428. /*
  429. * ADG supports BRRA/BRRB output only.
  430. * this means all clkout0/1/2/3 will be * same rate
  431. */
  432. /*
  433. * for clkout
  434. */
  435. if (!count) {
  436. clk = clk_register_fixed_rate(dev, clkout_name[CLKOUT],
  437. parent_clk_name, 0, req_rate);
  438. if (!IS_ERR(clk)) {
  439. adg->clkout[CLKOUT] = clk;
  440. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  441. }
  442. }
  443. /*
  444. * for clkout0/1/2/3
  445. */
  446. else {
  447. for (i = 0; i < CLKOUTMAX; i++) {
  448. clk = clk_register_fixed_rate(dev, clkout_name[i],
  449. parent_clk_name, 0,
  450. req_rate);
  451. if (!IS_ERR(clk)) {
  452. adg->onecell.clks = adg->clkout;
  453. adg->onecell.clk_num = CLKOUTMAX;
  454. adg->clkout[i] = clk;
  455. of_clk_add_provider(np, of_clk_src_onecell_get,
  456. &adg->onecell);
  457. }
  458. }
  459. }
  460. rsnd_mod_bset(adg_mod, SSICKR, 0x80FF0000, ckr);
  461. rsnd_mod_write(adg_mod, BRRA, rbga);
  462. rsnd_mod_write(adg_mod, BRRB, rbgb);
  463. for_each_rsnd_clkout(clk, adg, i)
  464. dev_dbg(dev, "clkout %d : %p : %ld\n", i, clk, clk_get_rate(clk));
  465. dev_dbg(dev, "SSICKR = 0x%08x, BRRA/BRRB = 0x%x/0x%x\n",
  466. ckr, rbga, rbgb);
  467. }
  468. int rsnd_adg_probe(struct rsnd_priv *priv)
  469. {
  470. struct rsnd_adg *adg;
  471. struct device *dev = rsnd_priv_to_dev(priv);
  472. struct device_node *np = dev->of_node;
  473. adg = devm_kzalloc(dev, sizeof(*adg), GFP_KERNEL);
  474. if (!adg) {
  475. dev_err(dev, "ADG allocate failed\n");
  476. return -ENOMEM;
  477. }
  478. rsnd_mod_init(priv, &adg->mod, &adg_ops,
  479. NULL, NULL, 0, 0);
  480. rsnd_adg_get_clkin(priv, adg);
  481. rsnd_adg_get_clkout(priv, adg);
  482. if (of_get_property(np, "clkout-lr-asynchronous", NULL))
  483. adg->flags = LRCLK_ASYNC;
  484. priv->adg = adg;
  485. return 0;
  486. }
  487. void rsnd_adg_remove(struct rsnd_priv *priv)
  488. {
  489. struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
  490. struct clk *clk;
  491. int i;
  492. for_each_rsnd_clk(clk, adg, i) {
  493. clk_disable_unprepare(clk);
  494. }
  495. }