meson-clk-measure.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2018 BayLibre, SAS
  4. * Author: Neil Armstrong <narmstrong@baylibre.com>
  5. */
  6. #include <linux/of_address.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/regmap.h>
  12. static DEFINE_MUTEX(measure_lock);
  13. #define MSR_CLK_DUTY 0x0
  14. #define MSR_CLK_REG0 0x4
  15. #define MSR_CLK_REG1 0x8
  16. #define MSR_CLK_REG2 0xc
  17. #define MSR_DURATION GENMASK(15, 0)
  18. #define MSR_ENABLE BIT(16)
  19. #define MSR_CONT BIT(17) /* continuous measurement */
  20. #define MSR_INTR BIT(18) /* interrupts */
  21. #define MSR_RUN BIT(19)
  22. #define MSR_CLK_SRC GENMASK(26, 20)
  23. #define MSR_BUSY BIT(31)
  24. #define MSR_VAL_MASK GENMASK(15, 0)
  25. #define DIV_MIN 32
  26. #define DIV_STEP 32
  27. #define DIV_MAX 640
  28. #define CLK_MSR_MAX 128
  29. struct meson_msr_id {
  30. struct meson_msr *priv;
  31. unsigned int id;
  32. const char *name;
  33. };
  34. struct meson_msr {
  35. struct regmap *regmap;
  36. struct meson_msr_id msr_table[CLK_MSR_MAX];
  37. };
  38. #define CLK_MSR_ID(__id, __name) \
  39. [__id] = {.id = __id, .name = __name,}
  40. static struct meson_msr_id clk_msr_m8[CLK_MSR_MAX] = {
  41. CLK_MSR_ID(0, "ring_osc_out_ee0"),
  42. CLK_MSR_ID(1, "ring_osc_out_ee1"),
  43. CLK_MSR_ID(2, "ring_osc_out_ee2"),
  44. CLK_MSR_ID(3, "a9_ring_osck"),
  45. CLK_MSR_ID(6, "vid_pll"),
  46. CLK_MSR_ID(7, "clk81"),
  47. CLK_MSR_ID(8, "encp"),
  48. CLK_MSR_ID(9, "encl"),
  49. CLK_MSR_ID(11, "eth_rmii"),
  50. CLK_MSR_ID(13, "amclk"),
  51. CLK_MSR_ID(14, "fec_clk_0"),
  52. CLK_MSR_ID(15, "fec_clk_1"),
  53. CLK_MSR_ID(16, "fec_clk_2"),
  54. CLK_MSR_ID(18, "a9_clk_div16"),
  55. CLK_MSR_ID(19, "hdmi_sys"),
  56. CLK_MSR_ID(20, "rtc_osc_clk_out"),
  57. CLK_MSR_ID(21, "i2s_clk_in_src0"),
  58. CLK_MSR_ID(22, "clk_rmii_from_pad"),
  59. CLK_MSR_ID(23, "hdmi_ch0_tmds"),
  60. CLK_MSR_ID(24, "lvds_fifo"),
  61. CLK_MSR_ID(26, "sc_clk_int"),
  62. CLK_MSR_ID(28, "sar_adc"),
  63. CLK_MSR_ID(30, "mpll_clk_test_out"),
  64. CLK_MSR_ID(31, "audac_clkpi"),
  65. CLK_MSR_ID(32, "vdac"),
  66. CLK_MSR_ID(33, "sdhc_rx"),
  67. CLK_MSR_ID(34, "sdhc_sd"),
  68. CLK_MSR_ID(35, "mali"),
  69. CLK_MSR_ID(36, "hdmi_tx_pixel"),
  70. CLK_MSR_ID(38, "vdin_meas"),
  71. CLK_MSR_ID(39, "pcm_sclk"),
  72. CLK_MSR_ID(40, "pcm_mclk"),
  73. CLK_MSR_ID(41, "eth_rx_tx"),
  74. CLK_MSR_ID(42, "pwm_d"),
  75. CLK_MSR_ID(43, "pwm_c"),
  76. CLK_MSR_ID(44, "pwm_b"),
  77. CLK_MSR_ID(45, "pwm_a"),
  78. CLK_MSR_ID(46, "pcm2_sclk"),
  79. CLK_MSR_ID(47, "ddr_dpll_pt"),
  80. CLK_MSR_ID(48, "pwm_f"),
  81. CLK_MSR_ID(49, "pwm_e"),
  82. CLK_MSR_ID(59, "hcodec"),
  83. CLK_MSR_ID(60, "usb_32k_alt"),
  84. CLK_MSR_ID(61, "gpio"),
  85. CLK_MSR_ID(62, "vid2_pll"),
  86. CLK_MSR_ID(63, "mipi_csi_cfg"),
  87. };
  88. static struct meson_msr_id clk_msr_gx[CLK_MSR_MAX] = {
  89. CLK_MSR_ID(0, "ring_osc_out_ee_0"),
  90. CLK_MSR_ID(1, "ring_osc_out_ee_1"),
  91. CLK_MSR_ID(2, "ring_osc_out_ee_2"),
  92. CLK_MSR_ID(3, "a53_ring_osc"),
  93. CLK_MSR_ID(4, "gp0_pll"),
  94. CLK_MSR_ID(6, "enci"),
  95. CLK_MSR_ID(7, "clk81"),
  96. CLK_MSR_ID(8, "encp"),
  97. CLK_MSR_ID(9, "encl"),
  98. CLK_MSR_ID(10, "vdac"),
  99. CLK_MSR_ID(11, "rgmii_tx"),
  100. CLK_MSR_ID(12, "pdm"),
  101. CLK_MSR_ID(13, "amclk"),
  102. CLK_MSR_ID(14, "fec_0"),
  103. CLK_MSR_ID(15, "fec_1"),
  104. CLK_MSR_ID(16, "fec_2"),
  105. CLK_MSR_ID(17, "sys_pll_div16"),
  106. CLK_MSR_ID(18, "sys_cpu_div16"),
  107. CLK_MSR_ID(19, "hdmitx_sys"),
  108. CLK_MSR_ID(20, "rtc_osc_out"),
  109. CLK_MSR_ID(21, "i2s_in_src0"),
  110. CLK_MSR_ID(22, "eth_phy_ref"),
  111. CLK_MSR_ID(23, "hdmi_todig"),
  112. CLK_MSR_ID(26, "sc_int"),
  113. CLK_MSR_ID(28, "sar_adc"),
  114. CLK_MSR_ID(31, "mpll_test_out"),
  115. CLK_MSR_ID(32, "vdec"),
  116. CLK_MSR_ID(35, "mali"),
  117. CLK_MSR_ID(36, "hdmi_tx_pixel"),
  118. CLK_MSR_ID(37, "i958"),
  119. CLK_MSR_ID(38, "vdin_meas"),
  120. CLK_MSR_ID(39, "pcm_sclk"),
  121. CLK_MSR_ID(40, "pcm_mclk"),
  122. CLK_MSR_ID(41, "eth_rx_or_rmii"),
  123. CLK_MSR_ID(42, "mp0_out"),
  124. CLK_MSR_ID(43, "fclk_div5"),
  125. CLK_MSR_ID(44, "pwm_b"),
  126. CLK_MSR_ID(45, "pwm_a"),
  127. CLK_MSR_ID(46, "vpu"),
  128. CLK_MSR_ID(47, "ddr_dpll_pt"),
  129. CLK_MSR_ID(48, "mp1_out"),
  130. CLK_MSR_ID(49, "mp2_out"),
  131. CLK_MSR_ID(50, "mp3_out"),
  132. CLK_MSR_ID(51, "nand_core"),
  133. CLK_MSR_ID(52, "sd_emmc_b"),
  134. CLK_MSR_ID(53, "sd_emmc_a"),
  135. CLK_MSR_ID(55, "vid_pll_div_out"),
  136. CLK_MSR_ID(56, "cci"),
  137. CLK_MSR_ID(57, "wave420l_c"),
  138. CLK_MSR_ID(58, "wave420l_b"),
  139. CLK_MSR_ID(59, "hcodec"),
  140. CLK_MSR_ID(60, "alt_32k"),
  141. CLK_MSR_ID(61, "gpio_msr"),
  142. CLK_MSR_ID(62, "hevc"),
  143. CLK_MSR_ID(66, "vid_lock"),
  144. CLK_MSR_ID(70, "pwm_f"),
  145. CLK_MSR_ID(71, "pwm_e"),
  146. CLK_MSR_ID(72, "pwm_d"),
  147. CLK_MSR_ID(73, "pwm_c"),
  148. CLK_MSR_ID(75, "aoclkx2_int"),
  149. CLK_MSR_ID(76, "aoclk_int"),
  150. CLK_MSR_ID(77, "rng_ring_osc_0"),
  151. CLK_MSR_ID(78, "rng_ring_osc_1"),
  152. CLK_MSR_ID(79, "rng_ring_osc_2"),
  153. CLK_MSR_ID(80, "rng_ring_osc_3"),
  154. CLK_MSR_ID(81, "vapb"),
  155. CLK_MSR_ID(82, "ge2d"),
  156. };
  157. static struct meson_msr_id clk_msr_axg[CLK_MSR_MAX] = {
  158. CLK_MSR_ID(0, "ring_osc_out_ee_0"),
  159. CLK_MSR_ID(1, "ring_osc_out_ee_1"),
  160. CLK_MSR_ID(2, "ring_osc_out_ee_2"),
  161. CLK_MSR_ID(3, "a53_ring_osc"),
  162. CLK_MSR_ID(4, "gp0_pll"),
  163. CLK_MSR_ID(5, "gp1_pll"),
  164. CLK_MSR_ID(7, "clk81"),
  165. CLK_MSR_ID(9, "encl"),
  166. CLK_MSR_ID(17, "sys_pll_div16"),
  167. CLK_MSR_ID(18, "sys_cpu_div16"),
  168. CLK_MSR_ID(20, "rtc_osc_out"),
  169. CLK_MSR_ID(23, "mmc_clk"),
  170. CLK_MSR_ID(28, "sar_adc"),
  171. CLK_MSR_ID(31, "mpll_test_out"),
  172. CLK_MSR_ID(40, "mod_eth_tx_clk"),
  173. CLK_MSR_ID(41, "mod_eth_rx_clk_rmii"),
  174. CLK_MSR_ID(42, "mp0_out"),
  175. CLK_MSR_ID(43, "fclk_div5"),
  176. CLK_MSR_ID(44, "pwm_b"),
  177. CLK_MSR_ID(45, "pwm_a"),
  178. CLK_MSR_ID(46, "vpu"),
  179. CLK_MSR_ID(47, "ddr_dpll_pt"),
  180. CLK_MSR_ID(48, "mp1_out"),
  181. CLK_MSR_ID(49, "mp2_out"),
  182. CLK_MSR_ID(50, "mp3_out"),
  183. CLK_MSR_ID(51, "sd_emmm_c"),
  184. CLK_MSR_ID(52, "sd_emmc_b"),
  185. CLK_MSR_ID(61, "gpio_msr"),
  186. CLK_MSR_ID(66, "audio_slv_lrclk_c"),
  187. CLK_MSR_ID(67, "audio_slv_lrclk_b"),
  188. CLK_MSR_ID(68, "audio_slv_lrclk_a"),
  189. CLK_MSR_ID(69, "audio_slv_sclk_c"),
  190. CLK_MSR_ID(70, "audio_slv_sclk_b"),
  191. CLK_MSR_ID(71, "audio_slv_sclk_a"),
  192. CLK_MSR_ID(72, "pwm_d"),
  193. CLK_MSR_ID(73, "pwm_c"),
  194. CLK_MSR_ID(74, "wifi_beacon"),
  195. CLK_MSR_ID(75, "tdmin_lb_lrcl"),
  196. CLK_MSR_ID(76, "tdmin_lb_sclk"),
  197. CLK_MSR_ID(77, "rng_ring_osc_0"),
  198. CLK_MSR_ID(78, "rng_ring_osc_1"),
  199. CLK_MSR_ID(79, "rng_ring_osc_2"),
  200. CLK_MSR_ID(80, "rng_ring_osc_3"),
  201. CLK_MSR_ID(81, "vapb"),
  202. CLK_MSR_ID(82, "ge2d"),
  203. CLK_MSR_ID(84, "audio_resample"),
  204. CLK_MSR_ID(85, "audio_pdm_sys"),
  205. CLK_MSR_ID(86, "audio_spdifout"),
  206. CLK_MSR_ID(87, "audio_spdifin"),
  207. CLK_MSR_ID(88, "audio_lrclk_f"),
  208. CLK_MSR_ID(89, "audio_lrclk_e"),
  209. CLK_MSR_ID(90, "audio_lrclk_d"),
  210. CLK_MSR_ID(91, "audio_lrclk_c"),
  211. CLK_MSR_ID(92, "audio_lrclk_b"),
  212. CLK_MSR_ID(93, "audio_lrclk_a"),
  213. CLK_MSR_ID(94, "audio_sclk_f"),
  214. CLK_MSR_ID(95, "audio_sclk_e"),
  215. CLK_MSR_ID(96, "audio_sclk_d"),
  216. CLK_MSR_ID(97, "audio_sclk_c"),
  217. CLK_MSR_ID(98, "audio_sclk_b"),
  218. CLK_MSR_ID(99, "audio_sclk_a"),
  219. CLK_MSR_ID(100, "audio_mclk_f"),
  220. CLK_MSR_ID(101, "audio_mclk_e"),
  221. CLK_MSR_ID(102, "audio_mclk_d"),
  222. CLK_MSR_ID(103, "audio_mclk_c"),
  223. CLK_MSR_ID(104, "audio_mclk_b"),
  224. CLK_MSR_ID(105, "audio_mclk_a"),
  225. CLK_MSR_ID(106, "pcie_refclk_n"),
  226. CLK_MSR_ID(107, "pcie_refclk_p"),
  227. CLK_MSR_ID(108, "audio_locker_out"),
  228. CLK_MSR_ID(109, "audio_locker_in"),
  229. };
  230. static struct meson_msr_id clk_msr_g12a[CLK_MSR_MAX] = {
  231. CLK_MSR_ID(0, "ring_osc_out_ee_0"),
  232. CLK_MSR_ID(1, "ring_osc_out_ee_1"),
  233. CLK_MSR_ID(2, "ring_osc_out_ee_2"),
  234. CLK_MSR_ID(3, "sys_cpu_ring_osc"),
  235. CLK_MSR_ID(4, "gp0_pll"),
  236. CLK_MSR_ID(6, "enci"),
  237. CLK_MSR_ID(7, "clk81"),
  238. CLK_MSR_ID(8, "encp"),
  239. CLK_MSR_ID(9, "encl"),
  240. CLK_MSR_ID(10, "vdac"),
  241. CLK_MSR_ID(11, "eth_tx"),
  242. CLK_MSR_ID(12, "hifi_pll"),
  243. CLK_MSR_ID(13, "mod_tcon"),
  244. CLK_MSR_ID(14, "fec_0"),
  245. CLK_MSR_ID(15, "fec_1"),
  246. CLK_MSR_ID(16, "fec_2"),
  247. CLK_MSR_ID(17, "sys_pll_div16"),
  248. CLK_MSR_ID(18, "sys_cpu_div16"),
  249. CLK_MSR_ID(19, "lcd_an_ph2"),
  250. CLK_MSR_ID(20, "rtc_osc_out"),
  251. CLK_MSR_ID(21, "lcd_an_ph3"),
  252. CLK_MSR_ID(22, "eth_phy_ref"),
  253. CLK_MSR_ID(23, "mpll_50m"),
  254. CLK_MSR_ID(24, "eth_125m"),
  255. CLK_MSR_ID(25, "eth_rmii"),
  256. CLK_MSR_ID(26, "sc_int"),
  257. CLK_MSR_ID(27, "in_mac"),
  258. CLK_MSR_ID(28, "sar_adc"),
  259. CLK_MSR_ID(29, "pcie_inp"),
  260. CLK_MSR_ID(30, "pcie_inn"),
  261. CLK_MSR_ID(31, "mpll_test_out"),
  262. CLK_MSR_ID(32, "vdec"),
  263. CLK_MSR_ID(33, "sys_cpu_ring_osc_1"),
  264. CLK_MSR_ID(34, "eth_mpll_50m"),
  265. CLK_MSR_ID(35, "mali"),
  266. CLK_MSR_ID(36, "hdmi_tx_pixel"),
  267. CLK_MSR_ID(37, "cdac"),
  268. CLK_MSR_ID(38, "vdin_meas"),
  269. CLK_MSR_ID(39, "bt656"),
  270. CLK_MSR_ID(41, "eth_rx_or_rmii"),
  271. CLK_MSR_ID(42, "mp0_out"),
  272. CLK_MSR_ID(43, "fclk_div5"),
  273. CLK_MSR_ID(44, "pwm_b"),
  274. CLK_MSR_ID(45, "pwm_a"),
  275. CLK_MSR_ID(46, "vpu"),
  276. CLK_MSR_ID(47, "ddr_dpll_pt"),
  277. CLK_MSR_ID(48, "mp1_out"),
  278. CLK_MSR_ID(49, "mp2_out"),
  279. CLK_MSR_ID(50, "mp3_out"),
  280. CLK_MSR_ID(51, "sd_emmc_c"),
  281. CLK_MSR_ID(52, "sd_emmc_b"),
  282. CLK_MSR_ID(53, "sd_emmc_a"),
  283. CLK_MSR_ID(54, "vpu_clkc"),
  284. CLK_MSR_ID(55, "vid_pll_div_out"),
  285. CLK_MSR_ID(56, "wave420l_a"),
  286. CLK_MSR_ID(57, "wave420l_c"),
  287. CLK_MSR_ID(58, "wave420l_b"),
  288. CLK_MSR_ID(59, "hcodec"),
  289. CLK_MSR_ID(61, "gpio_msr"),
  290. CLK_MSR_ID(62, "hevcb"),
  291. CLK_MSR_ID(63, "dsi_meas"),
  292. CLK_MSR_ID(64, "spicc_1"),
  293. CLK_MSR_ID(65, "spicc_0"),
  294. CLK_MSR_ID(66, "vid_lock"),
  295. CLK_MSR_ID(67, "dsi_phy"),
  296. CLK_MSR_ID(68, "hdcp22_esm"),
  297. CLK_MSR_ID(69, "hdcp22_skp"),
  298. CLK_MSR_ID(70, "pwm_f"),
  299. CLK_MSR_ID(71, "pwm_e"),
  300. CLK_MSR_ID(72, "pwm_d"),
  301. CLK_MSR_ID(73, "pwm_c"),
  302. CLK_MSR_ID(75, "hevcf"),
  303. CLK_MSR_ID(77, "rng_ring_osc_0"),
  304. CLK_MSR_ID(78, "rng_ring_osc_1"),
  305. CLK_MSR_ID(79, "rng_ring_osc_2"),
  306. CLK_MSR_ID(80, "rng_ring_osc_3"),
  307. CLK_MSR_ID(81, "vapb"),
  308. CLK_MSR_ID(82, "ge2d"),
  309. CLK_MSR_ID(83, "co_rx"),
  310. CLK_MSR_ID(84, "co_tx"),
  311. CLK_MSR_ID(89, "hdmi_todig"),
  312. CLK_MSR_ID(90, "hdmitx_sys"),
  313. CLK_MSR_ID(91, "sys_cpub_div16"),
  314. CLK_MSR_ID(92, "sys_pll_cpub_div16"),
  315. CLK_MSR_ID(94, "eth_phy_rx"),
  316. CLK_MSR_ID(95, "eth_phy_pll"),
  317. CLK_MSR_ID(96, "vpu_b"),
  318. CLK_MSR_ID(97, "cpu_b_tmp"),
  319. CLK_MSR_ID(98, "ts"),
  320. CLK_MSR_ID(99, "ring_osc_out_ee_3"),
  321. CLK_MSR_ID(100, "ring_osc_out_ee_4"),
  322. CLK_MSR_ID(101, "ring_osc_out_ee_5"),
  323. CLK_MSR_ID(102, "ring_osc_out_ee_6"),
  324. CLK_MSR_ID(103, "ring_osc_out_ee_7"),
  325. CLK_MSR_ID(104, "ring_osc_out_ee_8"),
  326. CLK_MSR_ID(105, "ring_osc_out_ee_9"),
  327. CLK_MSR_ID(106, "ephy_test"),
  328. CLK_MSR_ID(107, "au_dac_g128x"),
  329. CLK_MSR_ID(108, "audio_locker_out"),
  330. CLK_MSR_ID(109, "audio_locker_in"),
  331. CLK_MSR_ID(110, "audio_tdmout_c_sclk"),
  332. CLK_MSR_ID(111, "audio_tdmout_b_sclk"),
  333. CLK_MSR_ID(112, "audio_tdmout_a_sclk"),
  334. CLK_MSR_ID(113, "audio_tdmin_lb_sclk"),
  335. CLK_MSR_ID(114, "audio_tdmin_c_sclk"),
  336. CLK_MSR_ID(115, "audio_tdmin_b_sclk"),
  337. CLK_MSR_ID(116, "audio_tdmin_a_sclk"),
  338. CLK_MSR_ID(117, "audio_resample"),
  339. CLK_MSR_ID(118, "audio_pdm_sys"),
  340. CLK_MSR_ID(119, "audio_spdifout_b"),
  341. CLK_MSR_ID(120, "audio_spdifout"),
  342. CLK_MSR_ID(121, "audio_spdifin"),
  343. CLK_MSR_ID(122, "audio_pdm_dclk"),
  344. };
  345. static struct meson_msr_id clk_msr_sm1[CLK_MSR_MAX] = {
  346. CLK_MSR_ID(0, "ring_osc_out_ee_0"),
  347. CLK_MSR_ID(1, "ring_osc_out_ee_1"),
  348. CLK_MSR_ID(2, "ring_osc_out_ee_2"),
  349. CLK_MSR_ID(3, "ring_osc_out_ee_3"),
  350. CLK_MSR_ID(4, "gp0_pll"),
  351. CLK_MSR_ID(5, "gp1_pll"),
  352. CLK_MSR_ID(6, "enci"),
  353. CLK_MSR_ID(7, "clk81"),
  354. CLK_MSR_ID(8, "encp"),
  355. CLK_MSR_ID(9, "encl"),
  356. CLK_MSR_ID(10, "vdac"),
  357. CLK_MSR_ID(11, "eth_tx"),
  358. CLK_MSR_ID(12, "hifi_pll"),
  359. CLK_MSR_ID(13, "mod_tcon"),
  360. CLK_MSR_ID(14, "fec_0"),
  361. CLK_MSR_ID(15, "fec_1"),
  362. CLK_MSR_ID(16, "fec_2"),
  363. CLK_MSR_ID(17, "sys_pll_div16"),
  364. CLK_MSR_ID(18, "sys_cpu_div16"),
  365. CLK_MSR_ID(19, "lcd_an_ph2"),
  366. CLK_MSR_ID(20, "rtc_osc_out"),
  367. CLK_MSR_ID(21, "lcd_an_ph3"),
  368. CLK_MSR_ID(22, "eth_phy_ref"),
  369. CLK_MSR_ID(23, "mpll_50m"),
  370. CLK_MSR_ID(24, "eth_125m"),
  371. CLK_MSR_ID(25, "eth_rmii"),
  372. CLK_MSR_ID(26, "sc_int"),
  373. CLK_MSR_ID(27, "in_mac"),
  374. CLK_MSR_ID(28, "sar_adc"),
  375. CLK_MSR_ID(29, "pcie_inp"),
  376. CLK_MSR_ID(30, "pcie_inn"),
  377. CLK_MSR_ID(31, "mpll_test_out"),
  378. CLK_MSR_ID(32, "vdec"),
  379. CLK_MSR_ID(34, "eth_mpll_50m"),
  380. CLK_MSR_ID(35, "mali"),
  381. CLK_MSR_ID(36, "hdmi_tx_pixel"),
  382. CLK_MSR_ID(37, "cdac"),
  383. CLK_MSR_ID(38, "vdin_meas"),
  384. CLK_MSR_ID(39, "bt656"),
  385. CLK_MSR_ID(40, "arm_ring_osc_out_4"),
  386. CLK_MSR_ID(41, "eth_rx_or_rmii"),
  387. CLK_MSR_ID(42, "mp0_out"),
  388. CLK_MSR_ID(43, "fclk_div5"),
  389. CLK_MSR_ID(44, "pwm_b"),
  390. CLK_MSR_ID(45, "pwm_a"),
  391. CLK_MSR_ID(46, "vpu"),
  392. CLK_MSR_ID(47, "ddr_dpll_pt"),
  393. CLK_MSR_ID(48, "mp1_out"),
  394. CLK_MSR_ID(49, "mp2_out"),
  395. CLK_MSR_ID(50, "mp3_out"),
  396. CLK_MSR_ID(51, "sd_emmc_c"),
  397. CLK_MSR_ID(52, "sd_emmc_b"),
  398. CLK_MSR_ID(53, "sd_emmc_a"),
  399. CLK_MSR_ID(54, "vpu_clkc"),
  400. CLK_MSR_ID(55, "vid_pll_div_out"),
  401. CLK_MSR_ID(56, "wave420l_a"),
  402. CLK_MSR_ID(57, "wave420l_c"),
  403. CLK_MSR_ID(58, "wave420l_b"),
  404. CLK_MSR_ID(59, "hcodec"),
  405. CLK_MSR_ID(60, "arm_ring_osc_out_5"),
  406. CLK_MSR_ID(61, "gpio_msr"),
  407. CLK_MSR_ID(62, "hevcb"),
  408. CLK_MSR_ID(63, "dsi_meas"),
  409. CLK_MSR_ID(64, "spicc_1"),
  410. CLK_MSR_ID(65, "spicc_0"),
  411. CLK_MSR_ID(66, "vid_lock"),
  412. CLK_MSR_ID(67, "dsi_phy"),
  413. CLK_MSR_ID(68, "hdcp22_esm"),
  414. CLK_MSR_ID(69, "hdcp22_skp"),
  415. CLK_MSR_ID(70, "pwm_f"),
  416. CLK_MSR_ID(71, "pwm_e"),
  417. CLK_MSR_ID(72, "pwm_d"),
  418. CLK_MSR_ID(73, "pwm_c"),
  419. CLK_MSR_ID(74, "arm_ring_osc_out_6"),
  420. CLK_MSR_ID(75, "hevcf"),
  421. CLK_MSR_ID(76, "arm_ring_osc_out_7"),
  422. CLK_MSR_ID(77, "rng_ring_osc_0"),
  423. CLK_MSR_ID(78, "rng_ring_osc_1"),
  424. CLK_MSR_ID(79, "rng_ring_osc_2"),
  425. CLK_MSR_ID(80, "rng_ring_osc_3"),
  426. CLK_MSR_ID(81, "vapb"),
  427. CLK_MSR_ID(82, "ge2d"),
  428. CLK_MSR_ID(83, "co_rx"),
  429. CLK_MSR_ID(84, "co_tx"),
  430. CLK_MSR_ID(85, "arm_ring_osc_out_8"),
  431. CLK_MSR_ID(86, "arm_ring_osc_out_9"),
  432. CLK_MSR_ID(87, "mipi_dsi_phy"),
  433. CLK_MSR_ID(88, "cis2_adapt"),
  434. CLK_MSR_ID(89, "hdmi_todig"),
  435. CLK_MSR_ID(90, "hdmitx_sys"),
  436. CLK_MSR_ID(91, "nna_core"),
  437. CLK_MSR_ID(92, "nna_axi"),
  438. CLK_MSR_ID(93, "vad"),
  439. CLK_MSR_ID(94, "eth_phy_rx"),
  440. CLK_MSR_ID(95, "eth_phy_pll"),
  441. CLK_MSR_ID(96, "vpu_b"),
  442. CLK_MSR_ID(97, "cpu_b_tmp"),
  443. CLK_MSR_ID(98, "ts"),
  444. CLK_MSR_ID(99, "arm_ring_osc_out_10"),
  445. CLK_MSR_ID(100, "arm_ring_osc_out_11"),
  446. CLK_MSR_ID(101, "arm_ring_osc_out_12"),
  447. CLK_MSR_ID(102, "arm_ring_osc_out_13"),
  448. CLK_MSR_ID(103, "arm_ring_osc_out_14"),
  449. CLK_MSR_ID(104, "arm_ring_osc_out_15"),
  450. CLK_MSR_ID(105, "arm_ring_osc_out_16"),
  451. CLK_MSR_ID(106, "ephy_test"),
  452. CLK_MSR_ID(107, "au_dac_g128x"),
  453. CLK_MSR_ID(108, "audio_locker_out"),
  454. CLK_MSR_ID(109, "audio_locker_in"),
  455. CLK_MSR_ID(110, "audio_tdmout_c_sclk"),
  456. CLK_MSR_ID(111, "audio_tdmout_b_sclk"),
  457. CLK_MSR_ID(112, "audio_tdmout_a_sclk"),
  458. CLK_MSR_ID(113, "audio_tdmin_lb_sclk"),
  459. CLK_MSR_ID(114, "audio_tdmin_c_sclk"),
  460. CLK_MSR_ID(115, "audio_tdmin_b_sclk"),
  461. CLK_MSR_ID(116, "audio_tdmin_a_sclk"),
  462. CLK_MSR_ID(117, "audio_resample"),
  463. CLK_MSR_ID(118, "audio_pdm_sys"),
  464. CLK_MSR_ID(119, "audio_spdifout_b"),
  465. CLK_MSR_ID(120, "audio_spdifout"),
  466. CLK_MSR_ID(121, "audio_spdifin"),
  467. CLK_MSR_ID(122, "audio_pdm_dclk"),
  468. CLK_MSR_ID(123, "audio_resampled"),
  469. CLK_MSR_ID(124, "earcrx_pll"),
  470. CLK_MSR_ID(125, "earcrx_pll_test"),
  471. CLK_MSR_ID(126, "csi_phy0"),
  472. CLK_MSR_ID(127, "csi2_data"),
  473. };
  474. static int meson_measure_id(struct meson_msr_id *clk_msr_id,
  475. unsigned int duration)
  476. {
  477. struct meson_msr *priv = clk_msr_id->priv;
  478. unsigned int val;
  479. int ret;
  480. ret = mutex_lock_interruptible(&measure_lock);
  481. if (ret)
  482. return ret;
  483. regmap_write(priv->regmap, MSR_CLK_REG0, 0);
  484. /* Set measurement duration */
  485. regmap_update_bits(priv->regmap, MSR_CLK_REG0, MSR_DURATION,
  486. FIELD_PREP(MSR_DURATION, duration - 1));
  487. /* Set ID */
  488. regmap_update_bits(priv->regmap, MSR_CLK_REG0, MSR_CLK_SRC,
  489. FIELD_PREP(MSR_CLK_SRC, clk_msr_id->id));
  490. /* Enable & Start */
  491. regmap_update_bits(priv->regmap, MSR_CLK_REG0,
  492. MSR_RUN | MSR_ENABLE,
  493. MSR_RUN | MSR_ENABLE);
  494. ret = regmap_read_poll_timeout(priv->regmap, MSR_CLK_REG0,
  495. val, !(val & MSR_BUSY), 10, 10000);
  496. if (ret) {
  497. mutex_unlock(&measure_lock);
  498. return ret;
  499. }
  500. /* Disable */
  501. regmap_update_bits(priv->regmap, MSR_CLK_REG0, MSR_ENABLE, 0);
  502. /* Get the value in multiple of gate time counts */
  503. regmap_read(priv->regmap, MSR_CLK_REG2, &val);
  504. mutex_unlock(&measure_lock);
  505. if (val >= MSR_VAL_MASK)
  506. return -EINVAL;
  507. return DIV_ROUND_CLOSEST_ULL((val & MSR_VAL_MASK) * 1000000ULL,
  508. duration);
  509. }
  510. static int meson_measure_best_id(struct meson_msr_id *clk_msr_id,
  511. unsigned int *precision)
  512. {
  513. unsigned int duration = DIV_MAX;
  514. int ret;
  515. /* Start from max duration and down to min duration */
  516. do {
  517. ret = meson_measure_id(clk_msr_id, duration);
  518. if (ret >= 0)
  519. *precision = (2 * 1000000) / duration;
  520. else
  521. duration -= DIV_STEP;
  522. } while (duration >= DIV_MIN && ret == -EINVAL);
  523. return ret;
  524. }
  525. static int clk_msr_show(struct seq_file *s, void *data)
  526. {
  527. struct meson_msr_id *clk_msr_id = s->private;
  528. unsigned int precision = 0;
  529. int val;
  530. val = meson_measure_best_id(clk_msr_id, &precision);
  531. if (val < 0)
  532. return val;
  533. seq_printf(s, "%d\t+/-%dHz\n", val, precision);
  534. return 0;
  535. }
  536. DEFINE_SHOW_ATTRIBUTE(clk_msr);
  537. static int clk_msr_summary_show(struct seq_file *s, void *data)
  538. {
  539. struct meson_msr_id *msr_table = s->private;
  540. unsigned int precision = 0;
  541. int val, i;
  542. seq_puts(s, " clock rate precision\n");
  543. seq_puts(s, "---------------------------------------------\n");
  544. for (i = 0 ; i < CLK_MSR_MAX ; ++i) {
  545. if (!msr_table[i].name)
  546. continue;
  547. val = meson_measure_best_id(&msr_table[i], &precision);
  548. if (val < 0)
  549. return val;
  550. seq_printf(s, " %-20s %10d +/-%dHz\n",
  551. msr_table[i].name, val, precision);
  552. }
  553. return 0;
  554. }
  555. DEFINE_SHOW_ATTRIBUTE(clk_msr_summary);
  556. static const struct regmap_config meson_clk_msr_regmap_config = {
  557. .reg_bits = 32,
  558. .val_bits = 32,
  559. .reg_stride = 4,
  560. .max_register = MSR_CLK_REG2,
  561. };
  562. static int meson_msr_probe(struct platform_device *pdev)
  563. {
  564. const struct meson_msr_id *match_data;
  565. struct meson_msr *priv;
  566. struct resource *res;
  567. struct dentry *root, *clks;
  568. void __iomem *base;
  569. int i;
  570. priv = devm_kzalloc(&pdev->dev, sizeof(struct meson_msr),
  571. GFP_KERNEL);
  572. if (!priv)
  573. return -ENOMEM;
  574. match_data = device_get_match_data(&pdev->dev);
  575. if (!match_data) {
  576. dev_err(&pdev->dev, "failed to get match data\n");
  577. return -ENODEV;
  578. }
  579. memcpy(priv->msr_table, match_data, sizeof(priv->msr_table));
  580. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  581. base = devm_ioremap_resource(&pdev->dev, res);
  582. if (IS_ERR(base)) {
  583. dev_err(&pdev->dev, "io resource mapping failed\n");
  584. return PTR_ERR(base);
  585. }
  586. priv->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  587. &meson_clk_msr_regmap_config);
  588. if (IS_ERR(priv->regmap))
  589. return PTR_ERR(priv->regmap);
  590. root = debugfs_create_dir("meson-clk-msr", NULL);
  591. clks = debugfs_create_dir("clks", root);
  592. debugfs_create_file("measure_summary", 0444, root,
  593. priv->msr_table, &clk_msr_summary_fops);
  594. for (i = 0 ; i < CLK_MSR_MAX ; ++i) {
  595. if (!priv->msr_table[i].name)
  596. continue;
  597. priv->msr_table[i].priv = priv;
  598. debugfs_create_file(priv->msr_table[i].name, 0444, clks,
  599. &priv->msr_table[i], &clk_msr_fops);
  600. }
  601. return 0;
  602. }
  603. static const struct of_device_id meson_msr_match_table[] = {
  604. {
  605. .compatible = "amlogic,meson-gx-clk-measure",
  606. .data = (void *)clk_msr_gx,
  607. },
  608. {
  609. .compatible = "amlogic,meson8-clk-measure",
  610. .data = (void *)clk_msr_m8,
  611. },
  612. {
  613. .compatible = "amlogic,meson8b-clk-measure",
  614. .data = (void *)clk_msr_m8,
  615. },
  616. {
  617. .compatible = "amlogic,meson-axg-clk-measure",
  618. .data = (void *)clk_msr_axg,
  619. },
  620. {
  621. .compatible = "amlogic,meson-g12a-clk-measure",
  622. .data = (void *)clk_msr_g12a,
  623. },
  624. {
  625. .compatible = "amlogic,meson-sm1-clk-measure",
  626. .data = (void *)clk_msr_sm1,
  627. },
  628. { /* sentinel */ }
  629. };
  630. static struct platform_driver meson_msr_driver = {
  631. .probe = meson_msr_probe,
  632. .driver = {
  633. .name = "meson_msr",
  634. .of_match_table = meson_msr_match_table,
  635. },
  636. };
  637. builtin_platform_driver(meson_msr_driver);