clkc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Zynq UltraScale+ MPSoC clock controller
  4. *
  5. * Copyright (C) 2016-2019 Xilinx
  6. *
  7. * Based on drivers/clk/zynq/clkc.c
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/clk.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/module.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/slab.h>
  15. #include <linux/string.h>
  16. #include "clk-zynqmp.h"
  17. #define MAX_PARENT 100
  18. #define MAX_NODES 6
  19. #define MAX_NAME_LEN 50
  20. /* Flags for parents */
  21. #define PARENT_CLK_SELF 0
  22. #define PARENT_CLK_NODE1 1
  23. #define PARENT_CLK_NODE2 2
  24. #define PARENT_CLK_NODE3 3
  25. #define PARENT_CLK_NODE4 4
  26. #define PARENT_CLK_EXTERNAL 5
  27. #define END_OF_CLK_NAME "END_OF_CLK"
  28. #define END_OF_TOPOLOGY_NODE 1
  29. #define END_OF_PARENTS 1
  30. #define RESERVED_CLK_NAME ""
  31. #define CLK_GET_NAME_RESP_LEN 16
  32. #define CLK_GET_TOPOLOGY_RESP_WORDS 3
  33. #define CLK_GET_PARENTS_RESP_WORDS 3
  34. #define CLK_GET_ATTR_RESP_WORDS 1
  35. enum clk_type {
  36. CLK_TYPE_OUTPUT,
  37. CLK_TYPE_EXTERNAL,
  38. };
  39. /**
  40. * struct clock_parent - Clock parent
  41. * @name: Parent name
  42. * @id: Parent clock ID
  43. * @flag: Parent flags
  44. */
  45. struct clock_parent {
  46. char name[MAX_NAME_LEN];
  47. int id;
  48. u32 flag;
  49. };
  50. /**
  51. * struct zynqmp_clock - Clock
  52. * @clk_name: Clock name
  53. * @valid: Validity flag of clock
  54. * @type: Clock type (Output/External)
  55. * @node: Clock topology nodes
  56. * @num_nodes: Number of nodes present in topology
  57. * @parent: Parent of clock
  58. * @num_parents: Number of parents of clock
  59. * @clk_id: Clock id
  60. */
  61. struct zynqmp_clock {
  62. char clk_name[MAX_NAME_LEN];
  63. u32 valid;
  64. enum clk_type type;
  65. struct clock_topology node[MAX_NODES];
  66. u32 num_nodes;
  67. struct clock_parent parent[MAX_PARENT];
  68. u32 num_parents;
  69. u32 clk_id;
  70. };
  71. struct name_resp {
  72. char name[CLK_GET_NAME_RESP_LEN];
  73. };
  74. struct topology_resp {
  75. #define CLK_TOPOLOGY_TYPE GENMASK(3, 0)
  76. #define CLK_TOPOLOGY_CUSTOM_TYPE_FLAGS GENMASK(7, 4)
  77. #define CLK_TOPOLOGY_FLAGS GENMASK(23, 8)
  78. #define CLK_TOPOLOGY_TYPE_FLAGS GENMASK(31, 24)
  79. u32 topology[CLK_GET_TOPOLOGY_RESP_WORDS];
  80. };
  81. struct parents_resp {
  82. #define NA_PARENT 0xFFFFFFFF
  83. #define DUMMY_PARENT 0xFFFFFFFE
  84. #define CLK_PARENTS_ID GENMASK(15, 0)
  85. #define CLK_PARENTS_FLAGS GENMASK(31, 16)
  86. u32 parents[CLK_GET_PARENTS_RESP_WORDS];
  87. };
  88. struct attr_resp {
  89. #define CLK_ATTR_VALID BIT(0)
  90. #define CLK_ATTR_TYPE BIT(2)
  91. #define CLK_ATTR_NODE_INDEX GENMASK(13, 0)
  92. #define CLK_ATTR_NODE_TYPE GENMASK(19, 14)
  93. #define CLK_ATTR_NODE_SUBCLASS GENMASK(25, 20)
  94. #define CLK_ATTR_NODE_CLASS GENMASK(31, 26)
  95. u32 attr[CLK_GET_ATTR_RESP_WORDS];
  96. };
  97. static const char clk_type_postfix[][10] = {
  98. [TYPE_INVALID] = "",
  99. [TYPE_MUX] = "_mux",
  100. [TYPE_GATE] = "",
  101. [TYPE_DIV1] = "_div1",
  102. [TYPE_DIV2] = "_div2",
  103. [TYPE_FIXEDFACTOR] = "_ff",
  104. [TYPE_PLL] = ""
  105. };
  106. static struct clk_hw *(* const clk_topology[]) (const char *name, u32 clk_id,
  107. const char * const *parents,
  108. u8 num_parents,
  109. const struct clock_topology *nodes)
  110. = {
  111. [TYPE_INVALID] = NULL,
  112. [TYPE_MUX] = zynqmp_clk_register_mux,
  113. [TYPE_PLL] = zynqmp_clk_register_pll,
  114. [TYPE_FIXEDFACTOR] = zynqmp_clk_register_fixed_factor,
  115. [TYPE_DIV1] = zynqmp_clk_register_divider,
  116. [TYPE_DIV2] = zynqmp_clk_register_divider,
  117. [TYPE_GATE] = zynqmp_clk_register_gate
  118. };
  119. static struct zynqmp_clock *clock;
  120. static struct clk_hw_onecell_data *zynqmp_data;
  121. static unsigned int clock_max_idx;
  122. /**
  123. * zynqmp_is_valid_clock() - Check whether clock is valid or not
  124. * @clk_id: Clock index
  125. *
  126. * Return: 1 if clock is valid, 0 if clock is invalid else error code
  127. */
  128. static inline int zynqmp_is_valid_clock(u32 clk_id)
  129. {
  130. if (clk_id >= clock_max_idx)
  131. return -ENODEV;
  132. return clock[clk_id].valid;
  133. }
  134. /**
  135. * zynqmp_get_clock_name() - Get name of clock from Clock index
  136. * @clk_id: Clock index
  137. * @clk_name: Name of clock
  138. *
  139. * Return: 0 on success else error code
  140. */
  141. static int zynqmp_get_clock_name(u32 clk_id, char *clk_name)
  142. {
  143. int ret;
  144. ret = zynqmp_is_valid_clock(clk_id);
  145. if (ret == 1) {
  146. strscpy(clk_name, clock[clk_id].clk_name, MAX_NAME_LEN);
  147. return 0;
  148. }
  149. return ret == 0 ? -EINVAL : ret;
  150. }
  151. /**
  152. * zynqmp_get_clock_type() - Get type of clock
  153. * @clk_id: Clock index
  154. * @type: Clock type: CLK_TYPE_OUTPUT or CLK_TYPE_EXTERNAL
  155. *
  156. * Return: 0 on success else error code
  157. */
  158. static int zynqmp_get_clock_type(u32 clk_id, u32 *type)
  159. {
  160. int ret;
  161. ret = zynqmp_is_valid_clock(clk_id);
  162. if (ret == 1) {
  163. *type = clock[clk_id].type;
  164. return 0;
  165. }
  166. return ret == 0 ? -EINVAL : ret;
  167. }
  168. /**
  169. * zynqmp_pm_clock_get_num_clocks() - Get number of clocks in system
  170. * @nclocks: Number of clocks in system/board.
  171. *
  172. * Call firmware API to get number of clocks.
  173. *
  174. * Return: 0 on success else error code.
  175. */
  176. static int zynqmp_pm_clock_get_num_clocks(u32 *nclocks)
  177. {
  178. struct zynqmp_pm_query_data qdata = {0};
  179. u32 ret_payload[PAYLOAD_ARG_CNT];
  180. int ret;
  181. qdata.qid = PM_QID_CLOCK_GET_NUM_CLOCKS;
  182. ret = zynqmp_pm_query_data(qdata, ret_payload);
  183. *nclocks = ret_payload[1];
  184. return ret;
  185. }
  186. /**
  187. * zynqmp_pm_clock_get_name() - Get the name of clock for given id
  188. * @clock_id: ID of the clock to be queried
  189. * @response: Name of the clock with the given id
  190. *
  191. * This function is used to get name of clock specified by given
  192. * clock ID.
  193. *
  194. * Return: 0 on success else error+reason
  195. */
  196. static int zynqmp_pm_clock_get_name(u32 clock_id,
  197. struct name_resp *response)
  198. {
  199. struct zynqmp_pm_query_data qdata = {0};
  200. u32 ret_payload[PAYLOAD_ARG_CNT];
  201. int ret;
  202. qdata.qid = PM_QID_CLOCK_GET_NAME;
  203. qdata.arg1 = clock_id;
  204. ret = zynqmp_pm_query_data(qdata, ret_payload);
  205. if (ret)
  206. return ret;
  207. memcpy(response, ret_payload, sizeof(*response));
  208. return 0;
  209. }
  210. /**
  211. * zynqmp_pm_clock_get_topology() - Get the topology of clock for given id
  212. * @clock_id: ID of the clock to be queried
  213. * @index: Node index of clock topology
  214. * @response: Buffer used for the topology response
  215. *
  216. * This function is used to get topology information for the clock
  217. * specified by given clock ID.
  218. *
  219. * This API will return 3 node of topology with a single response. To get
  220. * other nodes, master should call same API in loop with new
  221. * index till error is returned. E.g First call should have
  222. * index 0 which will return nodes 0,1 and 2. Next call, index
  223. * should be 3 which will return nodes 3,4 and 5 and so on.
  224. *
  225. * Return: 0 on success else error+reason
  226. */
  227. static int zynqmp_pm_clock_get_topology(u32 clock_id, u32 index,
  228. struct topology_resp *response)
  229. {
  230. struct zynqmp_pm_query_data qdata = {0};
  231. u32 ret_payload[PAYLOAD_ARG_CNT];
  232. int ret;
  233. qdata.qid = PM_QID_CLOCK_GET_TOPOLOGY;
  234. qdata.arg1 = clock_id;
  235. qdata.arg2 = index;
  236. ret = zynqmp_pm_query_data(qdata, ret_payload);
  237. memcpy(response, &ret_payload[1], sizeof(*response));
  238. return ret;
  239. }
  240. unsigned long zynqmp_clk_map_common_ccf_flags(const u32 zynqmp_flag)
  241. {
  242. unsigned long ccf_flag = 0;
  243. if (zynqmp_flag & ZYNQMP_CLK_SET_RATE_GATE)
  244. ccf_flag |= CLK_SET_RATE_GATE;
  245. if (zynqmp_flag & ZYNQMP_CLK_SET_PARENT_GATE)
  246. ccf_flag |= CLK_SET_PARENT_GATE;
  247. if (zynqmp_flag & ZYNQMP_CLK_SET_RATE_PARENT)
  248. ccf_flag |= CLK_SET_RATE_PARENT;
  249. if (zynqmp_flag & ZYNQMP_CLK_IGNORE_UNUSED)
  250. ccf_flag |= CLK_IGNORE_UNUSED;
  251. if (zynqmp_flag & ZYNQMP_CLK_SET_RATE_NO_REPARENT)
  252. ccf_flag |= CLK_SET_RATE_NO_REPARENT;
  253. if (zynqmp_flag & ZYNQMP_CLK_IS_CRITICAL)
  254. ccf_flag |= CLK_IS_CRITICAL;
  255. return ccf_flag;
  256. }
  257. /**
  258. * zynqmp_clk_register_fixed_factor() - Register fixed factor with the
  259. * clock framework
  260. * @name: Name of this clock
  261. * @clk_id: Clock ID
  262. * @parents: Name of this clock's parents
  263. * @num_parents: Number of parents
  264. * @nodes: Clock topology node
  265. *
  266. * Return: clock hardware to the registered clock
  267. */
  268. struct clk_hw *zynqmp_clk_register_fixed_factor(const char *name, u32 clk_id,
  269. const char * const *parents,
  270. u8 num_parents,
  271. const struct clock_topology *nodes)
  272. {
  273. u32 mult, div;
  274. struct clk_hw *hw;
  275. struct zynqmp_pm_query_data qdata = {0};
  276. u32 ret_payload[PAYLOAD_ARG_CNT];
  277. int ret;
  278. unsigned long flag;
  279. qdata.qid = PM_QID_CLOCK_GET_FIXEDFACTOR_PARAMS;
  280. qdata.arg1 = clk_id;
  281. ret = zynqmp_pm_query_data(qdata, ret_payload);
  282. if (ret)
  283. return ERR_PTR(ret);
  284. mult = ret_payload[1];
  285. div = ret_payload[2];
  286. flag = zynqmp_clk_map_common_ccf_flags(nodes->flag);
  287. hw = clk_hw_register_fixed_factor(NULL, name,
  288. parents[0],
  289. flag, mult,
  290. div);
  291. return hw;
  292. }
  293. /**
  294. * zynqmp_pm_clock_get_parents() - Get the first 3 parents of clock for given id
  295. * @clock_id: Clock ID
  296. * @index: Parent index
  297. * @response: Parents of the given clock
  298. *
  299. * This function is used to get 3 parents for the clock specified by
  300. * given clock ID.
  301. *
  302. * This API will return 3 parents with a single response. To get
  303. * other parents, master should call same API in loop with new
  304. * parent index till error is returned. E.g First call should have
  305. * index 0 which will return parents 0,1 and 2. Next call, index
  306. * should be 3 which will return parent 3,4 and 5 and so on.
  307. *
  308. * Return: 0 on success else error+reason
  309. */
  310. static int zynqmp_pm_clock_get_parents(u32 clock_id, u32 index,
  311. struct parents_resp *response)
  312. {
  313. struct zynqmp_pm_query_data qdata = {0};
  314. u32 ret_payload[PAYLOAD_ARG_CNT];
  315. int ret;
  316. qdata.qid = PM_QID_CLOCK_GET_PARENTS;
  317. qdata.arg1 = clock_id;
  318. qdata.arg2 = index;
  319. ret = zynqmp_pm_query_data(qdata, ret_payload);
  320. memcpy(response, &ret_payload[1], sizeof(*response));
  321. return ret;
  322. }
  323. /**
  324. * zynqmp_pm_clock_get_attributes() - Get the attributes of clock for given id
  325. * @clock_id: Clock ID
  326. * @response: Clock attributes response
  327. *
  328. * This function is used to get clock's attributes(e.g. valid, clock type, etc).
  329. *
  330. * Return: 0 on success else error+reason
  331. */
  332. static int zynqmp_pm_clock_get_attributes(u32 clock_id,
  333. struct attr_resp *response)
  334. {
  335. struct zynqmp_pm_query_data qdata = {0};
  336. u32 ret_payload[PAYLOAD_ARG_CNT];
  337. int ret;
  338. qdata.qid = PM_QID_CLOCK_GET_ATTRIBUTES;
  339. qdata.arg1 = clock_id;
  340. ret = zynqmp_pm_query_data(qdata, ret_payload);
  341. memcpy(response, &ret_payload[1], sizeof(*response));
  342. return ret;
  343. }
  344. /**
  345. * __zynqmp_clock_get_topology() - Get topology data of clock from firmware
  346. * response data
  347. * @topology: Clock topology
  348. * @response: Clock topology data received from firmware
  349. * @nnodes: Number of nodes
  350. *
  351. * Return: 0 on success else error+reason
  352. */
  353. static int __zynqmp_clock_get_topology(struct clock_topology *topology,
  354. struct topology_resp *response,
  355. u32 *nnodes)
  356. {
  357. int i;
  358. u32 type;
  359. for (i = 0; i < ARRAY_SIZE(response->topology); i++) {
  360. type = FIELD_GET(CLK_TOPOLOGY_TYPE, response->topology[i]);
  361. if (type == TYPE_INVALID)
  362. return END_OF_TOPOLOGY_NODE;
  363. topology[*nnodes].type = type;
  364. topology[*nnodes].flag = FIELD_GET(CLK_TOPOLOGY_FLAGS,
  365. response->topology[i]);
  366. topology[*nnodes].type_flag =
  367. FIELD_GET(CLK_TOPOLOGY_TYPE_FLAGS,
  368. response->topology[i]);
  369. topology[*nnodes].custom_type_flag =
  370. FIELD_GET(CLK_TOPOLOGY_CUSTOM_TYPE_FLAGS,
  371. response->topology[i]);
  372. (*nnodes)++;
  373. }
  374. return 0;
  375. }
  376. /**
  377. * zynqmp_clock_get_topology() - Get topology of clock from firmware using
  378. * PM_API
  379. * @clk_id: Clock index
  380. * @topology: Clock topology
  381. * @num_nodes: Number of nodes
  382. *
  383. * Return: 0 on success else error+reason
  384. */
  385. static int zynqmp_clock_get_topology(u32 clk_id,
  386. struct clock_topology *topology,
  387. u32 *num_nodes)
  388. {
  389. int j, ret;
  390. struct topology_resp response = { };
  391. *num_nodes = 0;
  392. for (j = 0; j <= MAX_NODES; j += ARRAY_SIZE(response.topology)) {
  393. ret = zynqmp_pm_clock_get_topology(clock[clk_id].clk_id, j,
  394. &response);
  395. if (ret)
  396. return ret;
  397. ret = __zynqmp_clock_get_topology(topology, &response,
  398. num_nodes);
  399. if (ret == END_OF_TOPOLOGY_NODE)
  400. return 0;
  401. }
  402. return 0;
  403. }
  404. /**
  405. * __zynqmp_clock_get_parents() - Get parents info of clock from firmware
  406. * response data
  407. * @parents: Clock parents
  408. * @response: Clock parents data received from firmware
  409. * @nparent: Number of parent
  410. *
  411. * Return: 0 on success else error+reason
  412. */
  413. static int __zynqmp_clock_get_parents(struct clock_parent *parents,
  414. struct parents_resp *response,
  415. u32 *nparent)
  416. {
  417. int i;
  418. struct clock_parent *parent;
  419. for (i = 0; i < ARRAY_SIZE(response->parents); i++) {
  420. if (response->parents[i] == NA_PARENT)
  421. return END_OF_PARENTS;
  422. parent = &parents[i];
  423. parent->id = FIELD_GET(CLK_PARENTS_ID, response->parents[i]);
  424. if (response->parents[i] == DUMMY_PARENT) {
  425. strcpy(parent->name, "dummy_name");
  426. parent->flag = 0;
  427. } else {
  428. parent->flag = FIELD_GET(CLK_PARENTS_FLAGS,
  429. response->parents[i]);
  430. if (zynqmp_get_clock_name(parent->id, parent->name))
  431. continue;
  432. }
  433. *nparent += 1;
  434. }
  435. return 0;
  436. }
  437. /**
  438. * zynqmp_clock_get_parents() - Get parents info from firmware using PM_API
  439. * @clk_id: Clock index
  440. * @parents: Clock parents
  441. * @num_parents: Total number of parents
  442. *
  443. * Return: 0 on success else error+reason
  444. */
  445. static int zynqmp_clock_get_parents(u32 clk_id, struct clock_parent *parents,
  446. u32 *num_parents)
  447. {
  448. int j = 0, ret;
  449. struct parents_resp response = { };
  450. *num_parents = 0;
  451. do {
  452. /* Get parents from firmware */
  453. ret = zynqmp_pm_clock_get_parents(clock[clk_id].clk_id, j,
  454. &response);
  455. if (ret)
  456. return ret;
  457. ret = __zynqmp_clock_get_parents(&parents[j], &response,
  458. num_parents);
  459. if (ret == END_OF_PARENTS)
  460. return 0;
  461. j += ARRAY_SIZE(response.parents);
  462. } while (*num_parents <= MAX_PARENT);
  463. return 0;
  464. }
  465. /**
  466. * zynqmp_get_parent_list() - Create list of parents name
  467. * @np: Device node
  468. * @clk_id: Clock index
  469. * @parent_list: List of parent's name
  470. * @num_parents: Total number of parents
  471. *
  472. * Return: 0 on success else error+reason
  473. */
  474. static int zynqmp_get_parent_list(struct device_node *np, u32 clk_id,
  475. const char **parent_list, u32 *num_parents)
  476. {
  477. int i = 0, ret;
  478. u32 total_parents = clock[clk_id].num_parents;
  479. struct clock_topology *clk_nodes;
  480. struct clock_parent *parents;
  481. clk_nodes = clock[clk_id].node;
  482. parents = clock[clk_id].parent;
  483. for (i = 0; i < total_parents; i++) {
  484. if (!parents[i].flag) {
  485. parent_list[i] = parents[i].name;
  486. } else if (parents[i].flag == PARENT_CLK_EXTERNAL) {
  487. ret = of_property_match_string(np, "clock-names",
  488. parents[i].name);
  489. if (ret < 0)
  490. strcpy(parents[i].name, "dummy_name");
  491. parent_list[i] = parents[i].name;
  492. } else {
  493. strcat(parents[i].name,
  494. clk_type_postfix[clk_nodes[parents[i].flag - 1].
  495. type]);
  496. parent_list[i] = parents[i].name;
  497. }
  498. }
  499. *num_parents = total_parents;
  500. return 0;
  501. }
  502. /**
  503. * zynqmp_register_clk_topology() - Register clock topology
  504. * @clk_id: Clock index
  505. * @clk_name: Clock Name
  506. * @num_parents: Total number of parents
  507. * @parent_names: List of parents name
  508. *
  509. * Return: Returns either clock hardware or error+reason
  510. */
  511. static struct clk_hw *zynqmp_register_clk_topology(int clk_id, char *clk_name,
  512. int num_parents,
  513. const char **parent_names)
  514. {
  515. int j;
  516. u32 num_nodes, clk_dev_id;
  517. char *clk_out[MAX_NODES];
  518. struct clock_topology *nodes;
  519. struct clk_hw *hw = NULL;
  520. nodes = clock[clk_id].node;
  521. num_nodes = clock[clk_id].num_nodes;
  522. clk_dev_id = clock[clk_id].clk_id;
  523. for (j = 0; j < num_nodes; j++) {
  524. /*
  525. * Clock name received from firmware is output clock name.
  526. * Intermediate clock names are postfixed with type of clock.
  527. */
  528. if (j != (num_nodes - 1)) {
  529. clk_out[j] = kasprintf(GFP_KERNEL, "%s%s", clk_name,
  530. clk_type_postfix[nodes[j].type]);
  531. } else {
  532. clk_out[j] = kasprintf(GFP_KERNEL, "%s", clk_name);
  533. }
  534. if (!clk_topology[nodes[j].type])
  535. continue;
  536. hw = (*clk_topology[nodes[j].type])(clk_out[j], clk_dev_id,
  537. parent_names,
  538. num_parents,
  539. &nodes[j]);
  540. if (IS_ERR(hw))
  541. pr_warn_once("%s() 0x%x: %s register fail with %ld\n",
  542. __func__, clk_dev_id, clk_name,
  543. PTR_ERR(hw));
  544. parent_names[0] = clk_out[j];
  545. }
  546. for (j = 0; j < num_nodes; j++)
  547. kfree(clk_out[j]);
  548. return hw;
  549. }
  550. /**
  551. * zynqmp_register_clocks() - Register clocks
  552. * @np: Device node
  553. *
  554. * Return: 0 on success else error code
  555. */
  556. static int zynqmp_register_clocks(struct device_node *np)
  557. {
  558. int ret;
  559. u32 i, total_parents = 0, type = 0;
  560. const char *parent_names[MAX_PARENT];
  561. for (i = 0; i < clock_max_idx; i++) {
  562. char clk_name[MAX_NAME_LEN];
  563. /* get clock name, continue to next clock if name not found */
  564. if (zynqmp_get_clock_name(i, clk_name))
  565. continue;
  566. /* Check if clock is valid and output clock.
  567. * Do not register invalid or external clock.
  568. */
  569. ret = zynqmp_get_clock_type(i, &type);
  570. if (ret || type != CLK_TYPE_OUTPUT)
  571. continue;
  572. /* Get parents of clock*/
  573. if (zynqmp_get_parent_list(np, i, parent_names,
  574. &total_parents)) {
  575. WARN_ONCE(1, "No parents found for %s\n",
  576. clock[i].clk_name);
  577. continue;
  578. }
  579. zynqmp_data->hws[i] =
  580. zynqmp_register_clk_topology(i, clk_name,
  581. total_parents,
  582. parent_names);
  583. }
  584. for (i = 0; i < clock_max_idx; i++) {
  585. if (IS_ERR(zynqmp_data->hws[i])) {
  586. pr_err("Zynq Ultrascale+ MPSoC clk %s: register failed with %ld\n",
  587. clock[i].clk_name, PTR_ERR(zynqmp_data->hws[i]));
  588. WARN_ON(1);
  589. }
  590. }
  591. return 0;
  592. }
  593. /**
  594. * zynqmp_get_clock_info() - Get clock information from firmware using PM_API
  595. */
  596. static void zynqmp_get_clock_info(void)
  597. {
  598. int i, ret;
  599. u32 type = 0;
  600. u32 nodetype, subclass, class;
  601. struct attr_resp attr;
  602. struct name_resp name;
  603. for (i = 0; i < clock_max_idx; i++) {
  604. ret = zynqmp_pm_clock_get_attributes(i, &attr);
  605. if (ret)
  606. continue;
  607. clock[i].valid = FIELD_GET(CLK_ATTR_VALID, attr.attr[0]);
  608. /* skip query for Invalid clock */
  609. ret = zynqmp_is_valid_clock(i);
  610. if (ret != CLK_ATTR_VALID)
  611. continue;
  612. clock[i].type = FIELD_GET(CLK_ATTR_TYPE, attr.attr[0]) ?
  613. CLK_TYPE_EXTERNAL : CLK_TYPE_OUTPUT;
  614. nodetype = FIELD_GET(CLK_ATTR_NODE_TYPE, attr.attr[0]);
  615. subclass = FIELD_GET(CLK_ATTR_NODE_SUBCLASS, attr.attr[0]);
  616. class = FIELD_GET(CLK_ATTR_NODE_CLASS, attr.attr[0]);
  617. clock[i].clk_id = FIELD_PREP(CLK_ATTR_NODE_CLASS, class) |
  618. FIELD_PREP(CLK_ATTR_NODE_SUBCLASS, subclass) |
  619. FIELD_PREP(CLK_ATTR_NODE_TYPE, nodetype) |
  620. FIELD_PREP(CLK_ATTR_NODE_INDEX, i);
  621. zynqmp_pm_clock_get_name(clock[i].clk_id, &name);
  622. /*
  623. * Terminate with NULL character in case name provided by firmware
  624. * is longer and truncated due to size limit.
  625. */
  626. name.name[sizeof(name.name) - 1] = '\0';
  627. if (!strcmp(name.name, RESERVED_CLK_NAME))
  628. continue;
  629. strscpy(clock[i].clk_name, name.name, MAX_NAME_LEN);
  630. }
  631. /* Get topology of all clock */
  632. for (i = 0; i < clock_max_idx; i++) {
  633. ret = zynqmp_get_clock_type(i, &type);
  634. if (ret || type != CLK_TYPE_OUTPUT)
  635. continue;
  636. ret = zynqmp_clock_get_topology(i, clock[i].node,
  637. &clock[i].num_nodes);
  638. if (ret)
  639. continue;
  640. ret = zynqmp_clock_get_parents(i, clock[i].parent,
  641. &clock[i].num_parents);
  642. if (ret)
  643. continue;
  644. }
  645. }
  646. /**
  647. * zynqmp_clk_setup() - Setup the clock framework and register clocks
  648. * @np: Device node
  649. *
  650. * Return: 0 on success else error code
  651. */
  652. static int zynqmp_clk_setup(struct device_node *np)
  653. {
  654. int ret;
  655. ret = zynqmp_pm_clock_get_num_clocks(&clock_max_idx);
  656. if (ret)
  657. return ret;
  658. zynqmp_data = kzalloc(struct_size(zynqmp_data, hws, clock_max_idx),
  659. GFP_KERNEL);
  660. if (!zynqmp_data)
  661. return -ENOMEM;
  662. clock = kcalloc(clock_max_idx, sizeof(*clock), GFP_KERNEL);
  663. if (!clock) {
  664. kfree(zynqmp_data);
  665. return -ENOMEM;
  666. }
  667. zynqmp_get_clock_info();
  668. zynqmp_register_clocks(np);
  669. zynqmp_data->num = clock_max_idx;
  670. return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, zynqmp_data);
  671. }
  672. static int zynqmp_clock_probe(struct platform_device *pdev)
  673. {
  674. int ret;
  675. struct device *dev = &pdev->dev;
  676. ret = zynqmp_clk_setup(dev->of_node);
  677. return ret;
  678. }
  679. static const struct of_device_id zynqmp_clock_of_match[] = {
  680. {.compatible = "xlnx,zynqmp-clk"},
  681. {.compatible = "xlnx,versal-clk"},
  682. {},
  683. };
  684. MODULE_DEVICE_TABLE(of, zynqmp_clock_of_match);
  685. static struct platform_driver zynqmp_clock_driver = {
  686. .driver = {
  687. .name = "zynqmp_clock",
  688. .of_match_table = zynqmp_clock_of_match,
  689. },
  690. .probe = zynqmp_clock_probe,
  691. };
  692. module_platform_driver(zynqmp_clock_driver);