Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Zynq UltraScale+ MPSoC clock controller
0004  *
0005  *  Copyright (C) 2016-2019 Xilinx
0006  *
0007  * Based on drivers/clk/zynq/clkc.c
0008  */
0009 
0010 #include <linux/bitfield.h>
0011 #include <linux/clk.h>
0012 #include <linux/clk-provider.h>
0013 #include <linux/module.h>
0014 #include <linux/of_platform.h>
0015 #include <linux/slab.h>
0016 #include <linux/string.h>
0017 
0018 #include "clk-zynqmp.h"
0019 
0020 #define MAX_PARENT          100
0021 #define MAX_NODES           6
0022 #define MAX_NAME_LEN            50
0023 
0024 /* Flags for parents */
0025 #define PARENT_CLK_SELF         0
0026 #define PARENT_CLK_NODE1        1
0027 #define PARENT_CLK_NODE2        2
0028 #define PARENT_CLK_NODE3        3
0029 #define PARENT_CLK_NODE4        4
0030 #define PARENT_CLK_EXTERNAL     5
0031 
0032 #define END_OF_CLK_NAME         "END_OF_CLK"
0033 #define END_OF_TOPOLOGY_NODE        1
0034 #define END_OF_PARENTS          1
0035 #define RESERVED_CLK_NAME       ""
0036 
0037 #define CLK_GET_NAME_RESP_LEN       16
0038 #define CLK_GET_TOPOLOGY_RESP_WORDS 3
0039 #define CLK_GET_PARENTS_RESP_WORDS  3
0040 #define CLK_GET_ATTR_RESP_WORDS     1
0041 
0042 enum clk_type {
0043     CLK_TYPE_OUTPUT,
0044     CLK_TYPE_EXTERNAL,
0045 };
0046 
0047 /**
0048  * struct clock_parent - Clock parent
0049  * @name:   Parent name
0050  * @id:     Parent clock ID
0051  * @flag:   Parent flags
0052  */
0053 struct clock_parent {
0054     char name[MAX_NAME_LEN];
0055     int id;
0056     u32 flag;
0057 };
0058 
0059 /**
0060  * struct zynqmp_clock - Clock
0061  * @clk_name:       Clock name
0062  * @valid:      Validity flag of clock
0063  * @type:       Clock type (Output/External)
0064  * @node:       Clock topology nodes
0065  * @num_nodes:      Number of nodes present in topology
0066  * @parent:     Parent of clock
0067  * @num_parents:    Number of parents of clock
0068  * @clk_id:     Clock id
0069  */
0070 struct zynqmp_clock {
0071     char clk_name[MAX_NAME_LEN];
0072     u32 valid;
0073     enum clk_type type;
0074     struct clock_topology node[MAX_NODES];
0075     u32 num_nodes;
0076     struct clock_parent parent[MAX_PARENT];
0077     u32 num_parents;
0078     u32 clk_id;
0079 };
0080 
0081 struct name_resp {
0082     char name[CLK_GET_NAME_RESP_LEN];
0083 };
0084 
0085 struct topology_resp {
0086 #define CLK_TOPOLOGY_TYPE       GENMASK(3, 0)
0087 #define CLK_TOPOLOGY_CUSTOM_TYPE_FLAGS  GENMASK(7, 4)
0088 #define CLK_TOPOLOGY_FLAGS      GENMASK(23, 8)
0089 #define CLK_TOPOLOGY_TYPE_FLAGS     GENMASK(31, 24)
0090     u32 topology[CLK_GET_TOPOLOGY_RESP_WORDS];
0091 };
0092 
0093 struct parents_resp {
0094 #define NA_PARENT           0xFFFFFFFF
0095 #define DUMMY_PARENT            0xFFFFFFFE
0096 #define CLK_PARENTS_ID          GENMASK(15, 0)
0097 #define CLK_PARENTS_FLAGS       GENMASK(31, 16)
0098     u32 parents[CLK_GET_PARENTS_RESP_WORDS];
0099 };
0100 
0101 struct attr_resp {
0102 #define CLK_ATTR_VALID          BIT(0)
0103 #define CLK_ATTR_TYPE           BIT(2)
0104 #define CLK_ATTR_NODE_INDEX     GENMASK(13, 0)
0105 #define CLK_ATTR_NODE_TYPE      GENMASK(19, 14)
0106 #define CLK_ATTR_NODE_SUBCLASS      GENMASK(25, 20)
0107 #define CLK_ATTR_NODE_CLASS     GENMASK(31, 26)
0108     u32 attr[CLK_GET_ATTR_RESP_WORDS];
0109 };
0110 
0111 static const char clk_type_postfix[][10] = {
0112     [TYPE_INVALID] = "",
0113     [TYPE_MUX] = "_mux",
0114     [TYPE_GATE] = "",
0115     [TYPE_DIV1] = "_div1",
0116     [TYPE_DIV2] = "_div2",
0117     [TYPE_FIXEDFACTOR] = "_ff",
0118     [TYPE_PLL] = ""
0119 };
0120 
0121 static struct clk_hw *(* const clk_topology[]) (const char *name, u32 clk_id,
0122                     const char * const *parents,
0123                     u8 num_parents,
0124                     const struct clock_topology *nodes)
0125                     = {
0126     [TYPE_INVALID] = NULL,
0127     [TYPE_MUX] = zynqmp_clk_register_mux,
0128     [TYPE_PLL] = zynqmp_clk_register_pll,
0129     [TYPE_FIXEDFACTOR] = zynqmp_clk_register_fixed_factor,
0130     [TYPE_DIV1] = zynqmp_clk_register_divider,
0131     [TYPE_DIV2] = zynqmp_clk_register_divider,
0132     [TYPE_GATE] = zynqmp_clk_register_gate
0133 };
0134 
0135 static struct zynqmp_clock *clock;
0136 static struct clk_hw_onecell_data *zynqmp_data;
0137 static unsigned int clock_max_idx;
0138 
0139 /**
0140  * zynqmp_is_valid_clock() - Check whether clock is valid or not
0141  * @clk_id: Clock index
0142  *
0143  * Return: 1 if clock is valid, 0 if clock is invalid else error code
0144  */
0145 static inline int zynqmp_is_valid_clock(u32 clk_id)
0146 {
0147     if (clk_id >= clock_max_idx)
0148         return -ENODEV;
0149 
0150     return clock[clk_id].valid;
0151 }
0152 
0153 /**
0154  * zynqmp_get_clock_name() - Get name of clock from Clock index
0155  * @clk_id: Clock index
0156  * @clk_name:   Name of clock
0157  *
0158  * Return: 0 on success else error code
0159  */
0160 static int zynqmp_get_clock_name(u32 clk_id, char *clk_name)
0161 {
0162     int ret;
0163 
0164     ret = zynqmp_is_valid_clock(clk_id);
0165     if (ret == 1) {
0166         strncpy(clk_name, clock[clk_id].clk_name, MAX_NAME_LEN);
0167         return 0;
0168     }
0169 
0170     return ret == 0 ? -EINVAL : ret;
0171 }
0172 
0173 /**
0174  * zynqmp_get_clock_type() - Get type of clock
0175  * @clk_id: Clock index
0176  * @type:   Clock type: CLK_TYPE_OUTPUT or CLK_TYPE_EXTERNAL
0177  *
0178  * Return: 0 on success else error code
0179  */
0180 static int zynqmp_get_clock_type(u32 clk_id, u32 *type)
0181 {
0182     int ret;
0183 
0184     ret = zynqmp_is_valid_clock(clk_id);
0185     if (ret == 1) {
0186         *type = clock[clk_id].type;
0187         return 0;
0188     }
0189 
0190     return ret == 0 ? -EINVAL : ret;
0191 }
0192 
0193 /**
0194  * zynqmp_pm_clock_get_num_clocks() - Get number of clocks in system
0195  * @nclocks:    Number of clocks in system/board.
0196  *
0197  * Call firmware API to get number of clocks.
0198  *
0199  * Return: 0 on success else error code.
0200  */
0201 static int zynqmp_pm_clock_get_num_clocks(u32 *nclocks)
0202 {
0203     struct zynqmp_pm_query_data qdata = {0};
0204     u32 ret_payload[PAYLOAD_ARG_CNT];
0205     int ret;
0206 
0207     qdata.qid = PM_QID_CLOCK_GET_NUM_CLOCKS;
0208 
0209     ret = zynqmp_pm_query_data(qdata, ret_payload);
0210     *nclocks = ret_payload[1];
0211 
0212     return ret;
0213 }
0214 
0215 /**
0216  * zynqmp_pm_clock_get_name() - Get the name of clock for given id
0217  * @clock_id:   ID of the clock to be queried
0218  * @response:   Name of the clock with the given id
0219  *
0220  * This function is used to get name of clock specified by given
0221  * clock ID.
0222  *
0223  * Return: Returns 0
0224  */
0225 static int zynqmp_pm_clock_get_name(u32 clock_id,
0226                     struct name_resp *response)
0227 {
0228     struct zynqmp_pm_query_data qdata = {0};
0229     u32 ret_payload[PAYLOAD_ARG_CNT];
0230 
0231     qdata.qid = PM_QID_CLOCK_GET_NAME;
0232     qdata.arg1 = clock_id;
0233 
0234     zynqmp_pm_query_data(qdata, ret_payload);
0235     memcpy(response, ret_payload, sizeof(*response));
0236 
0237     return 0;
0238 }
0239 
0240 /**
0241  * zynqmp_pm_clock_get_topology() - Get the topology of clock for given id
0242  * @clock_id:   ID of the clock to be queried
0243  * @index:  Node index of clock topology
0244  * @response:   Buffer used for the topology response
0245  *
0246  * This function is used to get topology information for the clock
0247  * specified by given clock ID.
0248  *
0249  * This API will return 3 node of topology with a single response. To get
0250  * other nodes, master should call same API in loop with new
0251  * index till error is returned. E.g First call should have
0252  * index 0 which will return nodes 0,1 and 2. Next call, index
0253  * should be 3 which will return nodes 3,4 and 5 and so on.
0254  *
0255  * Return: 0 on success else error+reason
0256  */
0257 static int zynqmp_pm_clock_get_topology(u32 clock_id, u32 index,
0258                     struct topology_resp *response)
0259 {
0260     struct zynqmp_pm_query_data qdata = {0};
0261     u32 ret_payload[PAYLOAD_ARG_CNT];
0262     int ret;
0263 
0264     qdata.qid = PM_QID_CLOCK_GET_TOPOLOGY;
0265     qdata.arg1 = clock_id;
0266     qdata.arg2 = index;
0267 
0268     ret = zynqmp_pm_query_data(qdata, ret_payload);
0269     memcpy(response, &ret_payload[1], sizeof(*response));
0270 
0271     return ret;
0272 }
0273 
0274 unsigned long zynqmp_clk_map_common_ccf_flags(const u32 zynqmp_flag)
0275 {
0276     unsigned long ccf_flag = 0;
0277 
0278     if (zynqmp_flag & ZYNQMP_CLK_SET_RATE_GATE)
0279         ccf_flag |= CLK_SET_RATE_GATE;
0280     if (zynqmp_flag & ZYNQMP_CLK_SET_PARENT_GATE)
0281         ccf_flag |= CLK_SET_PARENT_GATE;
0282     if (zynqmp_flag & ZYNQMP_CLK_SET_RATE_PARENT)
0283         ccf_flag |= CLK_SET_RATE_PARENT;
0284     if (zynqmp_flag & ZYNQMP_CLK_IGNORE_UNUSED)
0285         ccf_flag |= CLK_IGNORE_UNUSED;
0286     if (zynqmp_flag & ZYNQMP_CLK_SET_RATE_NO_REPARENT)
0287         ccf_flag |= CLK_SET_RATE_NO_REPARENT;
0288     if (zynqmp_flag & ZYNQMP_CLK_IS_CRITICAL)
0289         ccf_flag |= CLK_IS_CRITICAL;
0290 
0291     return ccf_flag;
0292 }
0293 
0294 /**
0295  * zynqmp_clk_register_fixed_factor() - Register fixed factor with the
0296  *                  clock framework
0297  * @name:       Name of this clock
0298  * @clk_id:     Clock ID
0299  * @parents:        Name of this clock's parents
0300  * @num_parents:    Number of parents
0301  * @nodes:      Clock topology node
0302  *
0303  * Return: clock hardware to the registered clock
0304  */
0305 struct clk_hw *zynqmp_clk_register_fixed_factor(const char *name, u32 clk_id,
0306                     const char * const *parents,
0307                     u8 num_parents,
0308                     const struct clock_topology *nodes)
0309 {
0310     u32 mult, div;
0311     struct clk_hw *hw;
0312     struct zynqmp_pm_query_data qdata = {0};
0313     u32 ret_payload[PAYLOAD_ARG_CNT];
0314     int ret;
0315     unsigned long flag;
0316 
0317     qdata.qid = PM_QID_CLOCK_GET_FIXEDFACTOR_PARAMS;
0318     qdata.arg1 = clk_id;
0319 
0320     ret = zynqmp_pm_query_data(qdata, ret_payload);
0321     if (ret)
0322         return ERR_PTR(ret);
0323 
0324     mult = ret_payload[1];
0325     div = ret_payload[2];
0326 
0327     flag = zynqmp_clk_map_common_ccf_flags(nodes->flag);
0328 
0329     hw = clk_hw_register_fixed_factor(NULL, name,
0330                       parents[0],
0331                       flag, mult,
0332                       div);
0333 
0334     return hw;
0335 }
0336 
0337 /**
0338  * zynqmp_pm_clock_get_parents() - Get the first 3 parents of clock for given id
0339  * @clock_id:   Clock ID
0340  * @index:  Parent index
0341  * @response:   Parents of the given clock
0342  *
0343  * This function is used to get 3 parents for the clock specified by
0344  * given clock ID.
0345  *
0346  * This API will return 3 parents with a single response. To get
0347  * other parents, master should call same API in loop with new
0348  * parent index till error is returned. E.g First call should have
0349  * index 0 which will return parents 0,1 and 2. Next call, index
0350  * should be 3 which will return parent 3,4 and 5 and so on.
0351  *
0352  * Return: 0 on success else error+reason
0353  */
0354 static int zynqmp_pm_clock_get_parents(u32 clock_id, u32 index,
0355                        struct parents_resp *response)
0356 {
0357     struct zynqmp_pm_query_data qdata = {0};
0358     u32 ret_payload[PAYLOAD_ARG_CNT];
0359     int ret;
0360 
0361     qdata.qid = PM_QID_CLOCK_GET_PARENTS;
0362     qdata.arg1 = clock_id;
0363     qdata.arg2 = index;
0364 
0365     ret = zynqmp_pm_query_data(qdata, ret_payload);
0366     memcpy(response, &ret_payload[1], sizeof(*response));
0367 
0368     return ret;
0369 }
0370 
0371 /**
0372  * zynqmp_pm_clock_get_attributes() - Get the attributes of clock for given id
0373  * @clock_id:   Clock ID
0374  * @response:   Clock attributes response
0375  *
0376  * This function is used to get clock's attributes(e.g. valid, clock type, etc).
0377  *
0378  * Return: 0 on success else error+reason
0379  */
0380 static int zynqmp_pm_clock_get_attributes(u32 clock_id,
0381                       struct attr_resp *response)
0382 {
0383     struct zynqmp_pm_query_data qdata = {0};
0384     u32 ret_payload[PAYLOAD_ARG_CNT];
0385     int ret;
0386 
0387     qdata.qid = PM_QID_CLOCK_GET_ATTRIBUTES;
0388     qdata.arg1 = clock_id;
0389 
0390     ret = zynqmp_pm_query_data(qdata, ret_payload);
0391     memcpy(response, &ret_payload[1], sizeof(*response));
0392 
0393     return ret;
0394 }
0395 
0396 /**
0397  * __zynqmp_clock_get_topology() - Get topology data of clock from firmware
0398  *                 response data
0399  * @topology:       Clock topology
0400  * @response:       Clock topology data received from firmware
0401  * @nnodes:     Number of nodes
0402  *
0403  * Return: 0 on success else error+reason
0404  */
0405 static int __zynqmp_clock_get_topology(struct clock_topology *topology,
0406                        struct topology_resp *response,
0407                        u32 *nnodes)
0408 {
0409     int i;
0410     u32 type;
0411 
0412     for (i = 0; i < ARRAY_SIZE(response->topology); i++) {
0413         type = FIELD_GET(CLK_TOPOLOGY_TYPE, response->topology[i]);
0414         if (type == TYPE_INVALID)
0415             return END_OF_TOPOLOGY_NODE;
0416         topology[*nnodes].type = type;
0417         topology[*nnodes].flag = FIELD_GET(CLK_TOPOLOGY_FLAGS,
0418                            response->topology[i]);
0419         topology[*nnodes].type_flag =
0420                 FIELD_GET(CLK_TOPOLOGY_TYPE_FLAGS,
0421                       response->topology[i]);
0422         topology[*nnodes].custom_type_flag =
0423             FIELD_GET(CLK_TOPOLOGY_CUSTOM_TYPE_FLAGS,
0424                   response->topology[i]);
0425         (*nnodes)++;
0426     }
0427 
0428     return 0;
0429 }
0430 
0431 /**
0432  * zynqmp_clock_get_topology() - Get topology of clock from firmware using
0433  *               PM_API
0434  * @clk_id:     Clock index
0435  * @topology:       Clock topology
0436  * @num_nodes:      Number of nodes
0437  *
0438  * Return: 0 on success else error+reason
0439  */
0440 static int zynqmp_clock_get_topology(u32 clk_id,
0441                      struct clock_topology *topology,
0442                      u32 *num_nodes)
0443 {
0444     int j, ret;
0445     struct topology_resp response = { };
0446 
0447     *num_nodes = 0;
0448     for (j = 0; j <= MAX_NODES; j += ARRAY_SIZE(response.topology)) {
0449         ret = zynqmp_pm_clock_get_topology(clock[clk_id].clk_id, j,
0450                            &response);
0451         if (ret)
0452             return ret;
0453         ret = __zynqmp_clock_get_topology(topology, &response,
0454                           num_nodes);
0455         if (ret == END_OF_TOPOLOGY_NODE)
0456             return 0;
0457     }
0458 
0459     return 0;
0460 }
0461 
0462 /**
0463  * __zynqmp_clock_get_parents() - Get parents info of clock from firmware
0464  *                 response data
0465  * @parents:        Clock parents
0466  * @response:       Clock parents data received from firmware
0467  * @nparent:        Number of parent
0468  *
0469  * Return: 0 on success else error+reason
0470  */
0471 static int __zynqmp_clock_get_parents(struct clock_parent *parents,
0472                       struct parents_resp *response,
0473                       u32 *nparent)
0474 {
0475     int i;
0476     struct clock_parent *parent;
0477 
0478     for (i = 0; i < ARRAY_SIZE(response->parents); i++) {
0479         if (response->parents[i] == NA_PARENT)
0480             return END_OF_PARENTS;
0481 
0482         parent = &parents[i];
0483         parent->id = FIELD_GET(CLK_PARENTS_ID, response->parents[i]);
0484         if (response->parents[i] == DUMMY_PARENT) {
0485             strcpy(parent->name, "dummy_name");
0486             parent->flag = 0;
0487         } else {
0488             parent->flag = FIELD_GET(CLK_PARENTS_FLAGS,
0489                          response->parents[i]);
0490             if (zynqmp_get_clock_name(parent->id, parent->name))
0491                 continue;
0492         }
0493         *nparent += 1;
0494     }
0495 
0496     return 0;
0497 }
0498 
0499 /**
0500  * zynqmp_clock_get_parents() - Get parents info from firmware using PM_API
0501  * @clk_id:     Clock index
0502  * @parents:        Clock parents
0503  * @num_parents:    Total number of parents
0504  *
0505  * Return: 0 on success else error+reason
0506  */
0507 static int zynqmp_clock_get_parents(u32 clk_id, struct clock_parent *parents,
0508                     u32 *num_parents)
0509 {
0510     int j = 0, ret;
0511     struct parents_resp response = { };
0512 
0513     *num_parents = 0;
0514     do {
0515         /* Get parents from firmware */
0516         ret = zynqmp_pm_clock_get_parents(clock[clk_id].clk_id, j,
0517                           &response);
0518         if (ret)
0519             return ret;
0520 
0521         ret = __zynqmp_clock_get_parents(&parents[j], &response,
0522                          num_parents);
0523         if (ret == END_OF_PARENTS)
0524             return 0;
0525         j += ARRAY_SIZE(response.parents);
0526     } while (*num_parents <= MAX_PARENT);
0527 
0528     return 0;
0529 }
0530 
0531 /**
0532  * zynqmp_get_parent_list() - Create list of parents name
0533  * @np:         Device node
0534  * @clk_id:     Clock index
0535  * @parent_list:    List of parent's name
0536  * @num_parents:    Total number of parents
0537  *
0538  * Return: 0 on success else error+reason
0539  */
0540 static int zynqmp_get_parent_list(struct device_node *np, u32 clk_id,
0541                   const char **parent_list, u32 *num_parents)
0542 {
0543     int i = 0, ret;
0544     u32 total_parents = clock[clk_id].num_parents;
0545     struct clock_topology *clk_nodes;
0546     struct clock_parent *parents;
0547 
0548     clk_nodes = clock[clk_id].node;
0549     parents = clock[clk_id].parent;
0550 
0551     for (i = 0; i < total_parents; i++) {
0552         if (!parents[i].flag) {
0553             parent_list[i] = parents[i].name;
0554         } else if (parents[i].flag == PARENT_CLK_EXTERNAL) {
0555             ret = of_property_match_string(np, "clock-names",
0556                                parents[i].name);
0557             if (ret < 0)
0558                 strcpy(parents[i].name, "dummy_name");
0559             parent_list[i] = parents[i].name;
0560         } else {
0561             strcat(parents[i].name,
0562                    clk_type_postfix[clk_nodes[parents[i].flag - 1].
0563                    type]);
0564             parent_list[i] = parents[i].name;
0565         }
0566     }
0567 
0568     *num_parents = total_parents;
0569     return 0;
0570 }
0571 
0572 /**
0573  * zynqmp_register_clk_topology() - Register clock topology
0574  * @clk_id:     Clock index
0575  * @clk_name:       Clock Name
0576  * @num_parents:    Total number of parents
0577  * @parent_names:   List of parents name
0578  *
0579  * Return: Returns either clock hardware or error+reason
0580  */
0581 static struct clk_hw *zynqmp_register_clk_topology(int clk_id, char *clk_name,
0582                            int num_parents,
0583                            const char **parent_names)
0584 {
0585     int j;
0586     u32 num_nodes, clk_dev_id;
0587     char *clk_out[MAX_NODES];
0588     struct clock_topology *nodes;
0589     struct clk_hw *hw = NULL;
0590 
0591     nodes = clock[clk_id].node;
0592     num_nodes = clock[clk_id].num_nodes;
0593     clk_dev_id = clock[clk_id].clk_id;
0594 
0595     for (j = 0; j < num_nodes; j++) {
0596         /*
0597          * Clock name received from firmware is output clock name.
0598          * Intermediate clock names are postfixed with type of clock.
0599          */
0600         if (j != (num_nodes - 1)) {
0601             clk_out[j] = kasprintf(GFP_KERNEL, "%s%s", clk_name,
0602                         clk_type_postfix[nodes[j].type]);
0603         } else {
0604             clk_out[j] = kasprintf(GFP_KERNEL, "%s", clk_name);
0605         }
0606 
0607         if (!clk_topology[nodes[j].type])
0608             continue;
0609 
0610         hw = (*clk_topology[nodes[j].type])(clk_out[j], clk_dev_id,
0611                             parent_names,
0612                             num_parents,
0613                             &nodes[j]);
0614         if (IS_ERR(hw))
0615             pr_warn_once("%s() 0x%x: %s register fail with %ld\n",
0616                      __func__,  clk_dev_id, clk_name,
0617                      PTR_ERR(hw));
0618 
0619         parent_names[0] = clk_out[j];
0620     }
0621 
0622     for (j = 0; j < num_nodes; j++)
0623         kfree(clk_out[j]);
0624 
0625     return hw;
0626 }
0627 
0628 /**
0629  * zynqmp_register_clocks() - Register clocks
0630  * @np:     Device node
0631  *
0632  * Return: 0 on success else error code
0633  */
0634 static int zynqmp_register_clocks(struct device_node *np)
0635 {
0636     int ret;
0637     u32 i, total_parents = 0, type = 0;
0638     const char *parent_names[MAX_PARENT];
0639 
0640     for (i = 0; i < clock_max_idx; i++) {
0641         char clk_name[MAX_NAME_LEN];
0642 
0643         /* get clock name, continue to next clock if name not found */
0644         if (zynqmp_get_clock_name(i, clk_name))
0645             continue;
0646 
0647         /* Check if clock is valid and output clock.
0648          * Do not register invalid or external clock.
0649          */
0650         ret = zynqmp_get_clock_type(i, &type);
0651         if (ret || type != CLK_TYPE_OUTPUT)
0652             continue;
0653 
0654         /* Get parents of clock*/
0655         if (zynqmp_get_parent_list(np, i, parent_names,
0656                        &total_parents)) {
0657             WARN_ONCE(1, "No parents found for %s\n",
0658                   clock[i].clk_name);
0659             continue;
0660         }
0661 
0662         zynqmp_data->hws[i] =
0663             zynqmp_register_clk_topology(i, clk_name,
0664                              total_parents,
0665                              parent_names);
0666     }
0667 
0668     for (i = 0; i < clock_max_idx; i++) {
0669         if (IS_ERR(zynqmp_data->hws[i])) {
0670             pr_err("Zynq Ultrascale+ MPSoC clk %s: register failed with %ld\n",
0671                    clock[i].clk_name, PTR_ERR(zynqmp_data->hws[i]));
0672             WARN_ON(1);
0673         }
0674     }
0675     return 0;
0676 }
0677 
0678 /**
0679  * zynqmp_get_clock_info() - Get clock information from firmware using PM_API
0680  */
0681 static void zynqmp_get_clock_info(void)
0682 {
0683     int i, ret;
0684     u32 type = 0;
0685     u32 nodetype, subclass, class;
0686     struct attr_resp attr;
0687     struct name_resp name;
0688 
0689     for (i = 0; i < clock_max_idx; i++) {
0690         ret = zynqmp_pm_clock_get_attributes(i, &attr);
0691         if (ret)
0692             continue;
0693 
0694         clock[i].valid = FIELD_GET(CLK_ATTR_VALID, attr.attr[0]);
0695         /* skip query for Invalid clock */
0696         ret = zynqmp_is_valid_clock(i);
0697         if (ret != CLK_ATTR_VALID)
0698             continue;
0699 
0700         clock[i].type = FIELD_GET(CLK_ATTR_TYPE, attr.attr[0]) ?
0701             CLK_TYPE_EXTERNAL : CLK_TYPE_OUTPUT;
0702 
0703         nodetype = FIELD_GET(CLK_ATTR_NODE_TYPE, attr.attr[0]);
0704         subclass = FIELD_GET(CLK_ATTR_NODE_SUBCLASS, attr.attr[0]);
0705         class = FIELD_GET(CLK_ATTR_NODE_CLASS, attr.attr[0]);
0706 
0707         clock[i].clk_id = FIELD_PREP(CLK_ATTR_NODE_CLASS, class) |
0708                   FIELD_PREP(CLK_ATTR_NODE_SUBCLASS, subclass) |
0709                   FIELD_PREP(CLK_ATTR_NODE_TYPE, nodetype) |
0710                   FIELD_PREP(CLK_ATTR_NODE_INDEX, i);
0711 
0712         zynqmp_pm_clock_get_name(clock[i].clk_id, &name);
0713         if (!strcmp(name.name, RESERVED_CLK_NAME))
0714             continue;
0715         strncpy(clock[i].clk_name, name.name, MAX_NAME_LEN);
0716     }
0717 
0718     /* Get topology of all clock */
0719     for (i = 0; i < clock_max_idx; i++) {
0720         ret = zynqmp_get_clock_type(i, &type);
0721         if (ret || type != CLK_TYPE_OUTPUT)
0722             continue;
0723 
0724         ret = zynqmp_clock_get_topology(i, clock[i].node,
0725                         &clock[i].num_nodes);
0726         if (ret)
0727             continue;
0728 
0729         ret = zynqmp_clock_get_parents(i, clock[i].parent,
0730                            &clock[i].num_parents);
0731         if (ret)
0732             continue;
0733     }
0734 }
0735 
0736 /**
0737  * zynqmp_clk_setup() - Setup the clock framework and register clocks
0738  * @np:     Device node
0739  *
0740  * Return: 0 on success else error code
0741  */
0742 static int zynqmp_clk_setup(struct device_node *np)
0743 {
0744     int ret;
0745 
0746     ret = zynqmp_pm_clock_get_num_clocks(&clock_max_idx);
0747     if (ret)
0748         return ret;
0749 
0750     zynqmp_data = kzalloc(struct_size(zynqmp_data, hws, clock_max_idx),
0751                   GFP_KERNEL);
0752     if (!zynqmp_data)
0753         return -ENOMEM;
0754 
0755     clock = kcalloc(clock_max_idx, sizeof(*clock), GFP_KERNEL);
0756     if (!clock) {
0757         kfree(zynqmp_data);
0758         return -ENOMEM;
0759     }
0760 
0761     zynqmp_get_clock_info();
0762     zynqmp_register_clocks(np);
0763 
0764     zynqmp_data->num = clock_max_idx;
0765     return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, zynqmp_data);
0766 }
0767 
0768 static int zynqmp_clock_probe(struct platform_device *pdev)
0769 {
0770     int ret;
0771     struct device *dev = &pdev->dev;
0772 
0773     ret = zynqmp_clk_setup(dev->of_node);
0774 
0775     return ret;
0776 }
0777 
0778 static const struct of_device_id zynqmp_clock_of_match[] = {
0779     {.compatible = "xlnx,zynqmp-clk"},
0780     {.compatible = "xlnx,versal-clk"},
0781     {},
0782 };
0783 MODULE_DEVICE_TABLE(of, zynqmp_clock_of_match);
0784 
0785 static struct platform_driver zynqmp_clock_driver = {
0786     .driver = {
0787         .name = "zynqmp_clock",
0788         .of_match_table = zynqmp_clock_of_match,
0789     },
0790     .probe = zynqmp_clock_probe,
0791 };
0792 module_platform_driver(zynqmp_clock_driver);