PageRenderTime 66ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/sound/soc/sh/rcar/adg.c

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