1b745c079STero Kristo /* 2b745c079STero Kristo * SCI Clock driver for keystone based devices 3b745c079STero Kristo * 4b745c079STero Kristo * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/ 5b745c079STero Kristo * Tero Kristo <t-kristo@ti.com> 6b745c079STero Kristo * 7b745c079STero Kristo * This program is free software; you can redistribute it and/or modify 8b745c079STero Kristo * it under the terms of the GNU General Public License version 2 as 9b745c079STero Kristo * published by the Free Software Foundation. 10b745c079STero Kristo * 11b745c079STero Kristo * This program is distributed "as is" WITHOUT ANY WARRANTY of any 12b745c079STero Kristo * kind, whether express or implied; without even the implied warranty 13b745c079STero Kristo * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14b745c079STero Kristo * GNU General Public License for more details. 15b745c079STero Kristo */ 16b745c079STero Kristo #include <linux/clk-provider.h> 17b745c079STero Kristo #include <linux/err.h> 18b745c079STero Kristo #include <linux/io.h> 19b745c079STero Kristo #include <linux/module.h> 20b745c079STero Kristo #include <linux/of_address.h> 21b745c079STero Kristo #include <linux/of_device.h> 22b745c079STero Kristo #include <linux/platform_device.h> 23b745c079STero Kristo #include <linux/slab.h> 24b745c079STero Kristo #include <linux/soc/ti/ti_sci_protocol.h> 25f54d2cd3STero Kristo #include <linux/bsearch.h> 26b745c079STero Kristo 27b745c079STero Kristo #define SCI_CLK_SSC_ENABLE BIT(0) 28b745c079STero Kristo #define SCI_CLK_ALLOW_FREQ_CHANGE BIT(1) 29b745c079STero Kristo #define SCI_CLK_INPUT_TERMINATION BIT(2) 30b745c079STero Kristo 31b745c079STero Kristo /** 32b745c079STero Kristo * struct sci_clk_provider - TI SCI clock provider representation 33b745c079STero Kristo * @sci: Handle to the System Control Interface protocol handler 34b745c079STero Kristo * @ops: Pointer to the SCI ops to be used by the clocks 35b745c079STero Kristo * @dev: Device pointer for the clock provider 36b745c079STero Kristo * @clocks: Clocks array for this device 37f54d2cd3STero Kristo * @num_clocks: Total number of clocks for this provider 38b745c079STero Kristo */ 39b745c079STero Kristo struct sci_clk_provider { 40b745c079STero Kristo const struct ti_sci_handle *sci; 41b745c079STero Kristo const struct ti_sci_clk_ops *ops; 42b745c079STero Kristo struct device *dev; 433c13933cSTero Kristo struct sci_clk **clocks; 44f54d2cd3STero Kristo int num_clocks; 45b745c079STero Kristo }; 46b745c079STero Kristo 47b745c079STero Kristo /** 48b745c079STero Kristo * struct sci_clk - TI SCI clock representation 49b745c079STero Kristo * @hw: Hardware clock cookie for common clock framework 50b745c079STero Kristo * @dev_id: Device index 51b745c079STero Kristo * @clk_id: Clock index 523c13933cSTero Kristo * @num_parents: Number of parents for this clock 53b745c079STero Kristo * @provider: Master clock provider 54b745c079STero Kristo * @flags: Flags for the clock 55b745c079STero Kristo */ 56b745c079STero Kristo struct sci_clk { 57b745c079STero Kristo struct clk_hw hw; 58b745c079STero Kristo u16 dev_id; 59b745c079STero Kristo u8 clk_id; 603c13933cSTero Kristo u8 num_parents; 61b745c079STero Kristo struct sci_clk_provider *provider; 62b745c079STero Kristo u8 flags; 63b745c079STero Kristo }; 64b745c079STero Kristo 65b745c079STero Kristo #define to_sci_clk(_hw) container_of(_hw, struct sci_clk, hw) 66b745c079STero Kristo 67b745c079STero Kristo /** 68b745c079STero Kristo * sci_clk_prepare - Prepare (enable) a TI SCI clock 69b745c079STero Kristo * @hw: clock to prepare 70b745c079STero Kristo * 71b745c079STero Kristo * Prepares a clock to be actively used. Returns the SCI protocol status. 72b745c079STero Kristo */ 73b745c079STero Kristo static int sci_clk_prepare(struct clk_hw *hw) 74b745c079STero Kristo { 75b745c079STero Kristo struct sci_clk *clk = to_sci_clk(hw); 76b745c079STero Kristo bool enable_ssc = clk->flags & SCI_CLK_SSC_ENABLE; 77b745c079STero Kristo bool allow_freq_change = clk->flags & SCI_CLK_ALLOW_FREQ_CHANGE; 78b745c079STero Kristo bool input_termination = clk->flags & SCI_CLK_INPUT_TERMINATION; 79b745c079STero Kristo 80b745c079STero Kristo return clk->provider->ops->get_clock(clk->provider->sci, clk->dev_id, 81b745c079STero Kristo clk->clk_id, enable_ssc, 82b745c079STero Kristo allow_freq_change, 83b745c079STero Kristo input_termination); 84b745c079STero Kristo } 85b745c079STero Kristo 86b745c079STero Kristo /** 87b745c079STero Kristo * sci_clk_unprepare - Un-prepares (disables) a TI SCI clock 88b745c079STero Kristo * @hw: clock to unprepare 89b745c079STero Kristo * 90b745c079STero Kristo * Un-prepares a clock from active state. 91b745c079STero Kristo */ 92b745c079STero Kristo static void sci_clk_unprepare(struct clk_hw *hw) 93b745c079STero Kristo { 94b745c079STero Kristo struct sci_clk *clk = to_sci_clk(hw); 95b745c079STero Kristo int ret; 96b745c079STero Kristo 97b745c079STero Kristo ret = clk->provider->ops->put_clock(clk->provider->sci, clk->dev_id, 98b745c079STero Kristo clk->clk_id); 99b745c079STero Kristo if (ret) 100b745c079STero Kristo dev_err(clk->provider->dev, 101b745c079STero Kristo "unprepare failed for dev=%d, clk=%d, ret=%d\n", 102b745c079STero Kristo clk->dev_id, clk->clk_id, ret); 103b745c079STero Kristo } 104b745c079STero Kristo 105b745c079STero Kristo /** 106b745c079STero Kristo * sci_clk_is_prepared - Check if a TI SCI clock is prepared or not 107b745c079STero Kristo * @hw: clock to check status for 108b745c079STero Kristo * 109b745c079STero Kristo * Checks if a clock is prepared (enabled) in hardware. Returns non-zero 110b745c079STero Kristo * value if clock is enabled, zero otherwise. 111b745c079STero Kristo */ 112b745c079STero Kristo static int sci_clk_is_prepared(struct clk_hw *hw) 113b745c079STero Kristo { 114b745c079STero Kristo struct sci_clk *clk = to_sci_clk(hw); 115b745c079STero Kristo bool req_state, current_state; 116b745c079STero Kristo int ret; 117b745c079STero Kristo 118b745c079STero Kristo ret = clk->provider->ops->is_on(clk->provider->sci, clk->dev_id, 119b745c079STero Kristo clk->clk_id, &req_state, 120b745c079STero Kristo ¤t_state); 121b745c079STero Kristo if (ret) { 122b745c079STero Kristo dev_err(clk->provider->dev, 123b745c079STero Kristo "is_prepared failed for dev=%d, clk=%d, ret=%d\n", 124b745c079STero Kristo clk->dev_id, clk->clk_id, ret); 125b745c079STero Kristo return 0; 126b745c079STero Kristo } 127b745c079STero Kristo 128b745c079STero Kristo return req_state; 129b745c079STero Kristo } 130b745c079STero Kristo 131b745c079STero Kristo /** 132b745c079STero Kristo * sci_clk_recalc_rate - Get clock rate for a TI SCI clock 133b745c079STero Kristo * @hw: clock to get rate for 134b745c079STero Kristo * @parent_rate: parent rate provided by common clock framework, not used 135b745c079STero Kristo * 136b745c079STero Kristo * Gets the current clock rate of a TI SCI clock. Returns the current 137b745c079STero Kristo * clock rate, or zero in failure. 138b745c079STero Kristo */ 139b745c079STero Kristo static unsigned long sci_clk_recalc_rate(struct clk_hw *hw, 140b745c079STero Kristo unsigned long parent_rate) 141b745c079STero Kristo { 142b745c079STero Kristo struct sci_clk *clk = to_sci_clk(hw); 143b745c079STero Kristo u64 freq; 144b745c079STero Kristo int ret; 145b745c079STero Kristo 146b745c079STero Kristo ret = clk->provider->ops->get_freq(clk->provider->sci, clk->dev_id, 147b745c079STero Kristo clk->clk_id, &freq); 148b745c079STero Kristo if (ret) { 149b745c079STero Kristo dev_err(clk->provider->dev, 150b745c079STero Kristo "recalc-rate failed for dev=%d, clk=%d, ret=%d\n", 151b745c079STero Kristo clk->dev_id, clk->clk_id, ret); 152b745c079STero Kristo return 0; 153b745c079STero Kristo } 154b745c079STero Kristo 155b745c079STero Kristo return freq; 156b745c079STero Kristo } 157b745c079STero Kristo 158b745c079STero Kristo /** 159b745c079STero Kristo * sci_clk_determine_rate - Determines a clock rate a clock can be set to 160b745c079STero Kristo * @hw: clock to change rate for 161b745c079STero Kristo * @req: requested rate configuration for the clock 162b745c079STero Kristo * 163b745c079STero Kristo * Determines a suitable clock rate and parent for a TI SCI clock. 164b745c079STero Kristo * The parent handling is un-used, as generally the parent clock rates 165b745c079STero Kristo * are not known by the kernel; instead these are internally handled 166b745c079STero Kristo * by the firmware. Returns 0 on success, negative error value on failure. 167b745c079STero Kristo */ 168b745c079STero Kristo static int sci_clk_determine_rate(struct clk_hw *hw, 169b745c079STero Kristo struct clk_rate_request *req) 170b745c079STero Kristo { 171b745c079STero Kristo struct sci_clk *clk = to_sci_clk(hw); 172b745c079STero Kristo int ret; 173b745c079STero Kristo u64 new_rate; 174b745c079STero Kristo 175b745c079STero Kristo ret = clk->provider->ops->get_best_match_freq(clk->provider->sci, 176b745c079STero Kristo clk->dev_id, 177b745c079STero Kristo clk->clk_id, 178b745c079STero Kristo req->min_rate, 179b745c079STero Kristo req->rate, 180b745c079STero Kristo req->max_rate, 181b745c079STero Kristo &new_rate); 182b745c079STero Kristo if (ret) { 183b745c079STero Kristo dev_err(clk->provider->dev, 184b745c079STero Kristo "determine-rate failed for dev=%d, clk=%d, ret=%d\n", 185b745c079STero Kristo clk->dev_id, clk->clk_id, ret); 186b745c079STero Kristo return ret; 187b745c079STero Kristo } 188b745c079STero Kristo 189b745c079STero Kristo req->rate = new_rate; 190b745c079STero Kristo 191b745c079STero Kristo return 0; 192b745c079STero Kristo } 193b745c079STero Kristo 194b745c079STero Kristo /** 195b745c079STero Kristo * sci_clk_set_rate - Set rate for a TI SCI clock 196b745c079STero Kristo * @hw: clock to change rate for 197b745c079STero Kristo * @rate: target rate for the clock 198b745c079STero Kristo * @parent_rate: rate of the clock parent, not used for TI SCI clocks 199b745c079STero Kristo * 200b745c079STero Kristo * Sets a clock frequency for a TI SCI clock. Returns the TI SCI 201b745c079STero Kristo * protocol status. 202b745c079STero Kristo */ 203b745c079STero Kristo static int sci_clk_set_rate(struct clk_hw *hw, unsigned long rate, 204b745c079STero Kristo unsigned long parent_rate) 205b745c079STero Kristo { 206b745c079STero Kristo struct sci_clk *clk = to_sci_clk(hw); 207b745c079STero Kristo 208b745c079STero Kristo return clk->provider->ops->set_freq(clk->provider->sci, clk->dev_id, 209b745c079STero Kristo clk->clk_id, rate, rate, rate); 210b745c079STero Kristo } 211b745c079STero Kristo 212b745c079STero Kristo /** 213b745c079STero Kristo * sci_clk_get_parent - Get the current parent of a TI SCI clock 214b745c079STero Kristo * @hw: clock to get parent for 215b745c079STero Kristo * 216b745c079STero Kristo * Returns the index of the currently selected parent for a TI SCI clock. 217b745c079STero Kristo */ 218b745c079STero Kristo static u8 sci_clk_get_parent(struct clk_hw *hw) 219b745c079STero Kristo { 220b745c079STero Kristo struct sci_clk *clk = to_sci_clk(hw); 221b745c079STero Kristo u8 parent_id; 222b745c079STero Kristo int ret; 223b745c079STero Kristo 224b745c079STero Kristo ret = clk->provider->ops->get_parent(clk->provider->sci, clk->dev_id, 225b745c079STero Kristo clk->clk_id, &parent_id); 226b745c079STero Kristo if (ret) { 227b745c079STero Kristo dev_err(clk->provider->dev, 228b745c079STero Kristo "get-parent failed for dev=%d, clk=%d, ret=%d\n", 229b745c079STero Kristo clk->dev_id, clk->clk_id, ret); 230b745c079STero Kristo return 0; 231b745c079STero Kristo } 232b745c079STero Kristo 233b745c079STero Kristo return parent_id - clk->clk_id - 1; 234b745c079STero Kristo } 235b745c079STero Kristo 236b745c079STero Kristo /** 237b745c079STero Kristo * sci_clk_set_parent - Set the parent of a TI SCI clock 238b745c079STero Kristo * @hw: clock to set parent for 239b745c079STero Kristo * @index: new parent index for the clock 240b745c079STero Kristo * 241b745c079STero Kristo * Sets the parent of a TI SCI clock. Return TI SCI protocol status. 242b745c079STero Kristo */ 243b745c079STero Kristo static int sci_clk_set_parent(struct clk_hw *hw, u8 index) 244b745c079STero Kristo { 245b745c079STero Kristo struct sci_clk *clk = to_sci_clk(hw); 246b745c079STero Kristo 247b745c079STero Kristo return clk->provider->ops->set_parent(clk->provider->sci, clk->dev_id, 248b745c079STero Kristo clk->clk_id, 249b745c079STero Kristo index + 1 + clk->clk_id); 250b745c079STero Kristo } 251b745c079STero Kristo 252b745c079STero Kristo static const struct clk_ops sci_clk_ops = { 253b745c079STero Kristo .prepare = sci_clk_prepare, 254b745c079STero Kristo .unprepare = sci_clk_unprepare, 255b745c079STero Kristo .is_prepared = sci_clk_is_prepared, 256b745c079STero Kristo .recalc_rate = sci_clk_recalc_rate, 257b745c079STero Kristo .determine_rate = sci_clk_determine_rate, 258b745c079STero Kristo .set_rate = sci_clk_set_rate, 259b745c079STero Kristo .get_parent = sci_clk_get_parent, 260b745c079STero Kristo .set_parent = sci_clk_set_parent, 261b745c079STero Kristo }; 262b745c079STero Kristo 263b745c079STero Kristo /** 264b745c079STero Kristo * _sci_clk_get - Gets a handle for an SCI clock 265b745c079STero Kristo * @provider: Handle to SCI clock provider 2663c13933cSTero Kristo * @sci_clk: Handle to the SCI clock to populate 267b745c079STero Kristo * 268b745c079STero Kristo * Gets a handle to an existing TI SCI hw clock, or builds a new clock 269b745c079STero Kristo * entry and registers it with the common clock framework. Called from 270b745c079STero Kristo * the common clock framework, when a corresponding of_clk_get call is 271b745c079STero Kristo * executed, or recursively from itself when parsing parent clocks. 2723c13933cSTero Kristo * Returns 0 on success, negative error code on failure. 273b745c079STero Kristo */ 2743c13933cSTero Kristo static int _sci_clk_build(struct sci_clk_provider *provider, 2753c13933cSTero Kristo struct sci_clk *sci_clk) 276b745c079STero Kristo { 277b745c079STero Kristo struct clk_init_data init = { NULL }; 278b745c079STero Kristo char *name = NULL; 279b745c079STero Kristo char **parent_names = NULL; 280b745c079STero Kristo int i; 2813c13933cSTero Kristo int ret = 0; 282b745c079STero Kristo 283b745c079STero Kristo name = kasprintf(GFP_KERNEL, "%s:%d:%d", dev_name(provider->dev), 284b745c079STero Kristo sci_clk->dev_id, sci_clk->clk_id); 285b745c079STero Kristo 286b745c079STero Kristo init.name = name; 287b745c079STero Kristo 288b745c079STero Kristo /* 289b745c079STero Kristo * From kernel point of view, we only care about a clocks parents, 290b745c079STero Kristo * if it has more than 1 possible parent. In this case, it is going 291b745c079STero Kristo * to have mux functionality. Otherwise it is going to act as a root 292b745c079STero Kristo * clock. 293b745c079STero Kristo */ 2943c13933cSTero Kristo if (sci_clk->num_parents < 2) 2953c13933cSTero Kristo sci_clk->num_parents = 0; 296b745c079STero Kristo 2973c13933cSTero Kristo if (sci_clk->num_parents) { 2983c13933cSTero Kristo parent_names = kcalloc(sci_clk->num_parents, sizeof(char *), 299b745c079STero Kristo GFP_KERNEL); 300b745c079STero Kristo 301b745c079STero Kristo if (!parent_names) { 302b745c079STero Kristo ret = -ENOMEM; 303b745c079STero Kristo goto err; 304b745c079STero Kristo } 305b745c079STero Kristo 3063c13933cSTero Kristo for (i = 0; i < sci_clk->num_parents; i++) { 307b745c079STero Kristo char *parent_name; 308b745c079STero Kristo 309b745c079STero Kristo parent_name = kasprintf(GFP_KERNEL, "%s:%d:%d", 310b745c079STero Kristo dev_name(provider->dev), 311b745c079STero Kristo sci_clk->dev_id, 312b745c079STero Kristo sci_clk->clk_id + 1 + i); 313b745c079STero Kristo if (!parent_name) { 314b745c079STero Kristo ret = -ENOMEM; 315b745c079STero Kristo goto err; 316b745c079STero Kristo } 317b745c079STero Kristo parent_names[i] = parent_name; 318b745c079STero Kristo } 319b745c079STero Kristo init.parent_names = (void *)parent_names; 320b745c079STero Kristo } 321b745c079STero Kristo 322b745c079STero Kristo init.ops = &sci_clk_ops; 3233c13933cSTero Kristo init.num_parents = sci_clk->num_parents; 324b745c079STero Kristo sci_clk->hw.init = &init; 325b745c079STero Kristo 326b745c079STero Kristo ret = devm_clk_hw_register(provider->dev, &sci_clk->hw); 327b745c079STero Kristo if (ret) 328b745c079STero Kristo dev_err(provider->dev, "failed clk register with %d\n", ret); 329b745c079STero Kristo 330b745c079STero Kristo err: 331b745c079STero Kristo if (parent_names) { 3323c13933cSTero Kristo for (i = 0; i < sci_clk->num_parents; i++) 333b745c079STero Kristo kfree(parent_names[i]); 334b745c079STero Kristo 335b745c079STero Kristo kfree(parent_names); 336b745c079STero Kristo } 337b745c079STero Kristo 338b745c079STero Kristo kfree(name); 339b745c079STero Kristo 3403c13933cSTero Kristo return ret; 341b745c079STero Kristo } 342b745c079STero Kristo 343f54d2cd3STero Kristo static int _cmp_sci_clk(const void *a, const void *b) 344f54d2cd3STero Kristo { 345f54d2cd3STero Kristo const struct sci_clk *ca = a; 346f54d2cd3STero Kristo const struct sci_clk *cb = *(struct sci_clk **)b; 347f54d2cd3STero Kristo 348f54d2cd3STero Kristo if (ca->dev_id == cb->dev_id && ca->clk_id == cb->clk_id) 349f54d2cd3STero Kristo return 0; 350f54d2cd3STero Kristo if (ca->dev_id > cb->dev_id || 351f54d2cd3STero Kristo (ca->dev_id == cb->dev_id && ca->clk_id > cb->clk_id)) 352f54d2cd3STero Kristo return 1; 353f54d2cd3STero Kristo return -1; 354f54d2cd3STero Kristo } 355f54d2cd3STero Kristo 356b745c079STero Kristo /** 357b745c079STero Kristo * sci_clk_get - Xlate function for getting clock handles 358b745c079STero Kristo * @clkspec: device tree clock specifier 359b745c079STero Kristo * @data: pointer to the clock provider 360b745c079STero Kristo * 361b745c079STero Kristo * Xlate function for retrieving clock TI SCI hw clock handles based on 362b745c079STero Kristo * device tree clock specifier. Called from the common clock framework, 363b745c079STero Kristo * when a corresponding of_clk_get call is executed. Returns a pointer 364b745c079STero Kristo * to the TI SCI hw clock struct, or ERR_PTR value in failure. 365b745c079STero Kristo */ 366b745c079STero Kristo static struct clk_hw *sci_clk_get(struct of_phandle_args *clkspec, void *data) 367b745c079STero Kristo { 368b745c079STero Kristo struct sci_clk_provider *provider = data; 369f54d2cd3STero Kristo struct sci_clk **clk; 370f54d2cd3STero Kristo struct sci_clk key; 371b745c079STero Kristo 372b745c079STero Kristo if (clkspec->args_count != 2) 373b745c079STero Kristo return ERR_PTR(-EINVAL); 374b745c079STero Kristo 375f54d2cd3STero Kristo key.dev_id = clkspec->args[0]; 376f54d2cd3STero Kristo key.clk_id = clkspec->args[1]; 377b745c079STero Kristo 378f54d2cd3STero Kristo clk = bsearch(&key, provider->clocks, provider->num_clocks, 379f54d2cd3STero Kristo sizeof(clk), _cmp_sci_clk); 380b745c079STero Kristo 381f54d2cd3STero Kristo if (!clk) 382b745c079STero Kristo return ERR_PTR(-ENODEV); 383f54d2cd3STero Kristo 384f54d2cd3STero Kristo return &(*clk)->hw; 385b745c079STero Kristo } 386b745c079STero Kristo 387b745c079STero Kristo static int ti_sci_init_clocks(struct sci_clk_provider *p) 388b745c079STero Kristo { 389b745c079STero Kristo int i; 3903c13933cSTero Kristo int ret; 391b745c079STero Kristo 3923c13933cSTero Kristo for (i = 0; i < p->num_clocks; i++) { 3933c13933cSTero Kristo ret = _sci_clk_build(p, p->clocks[i]); 3943c13933cSTero Kristo if (ret) 3953c13933cSTero Kristo return ret; 396b745c079STero Kristo } 397b745c079STero Kristo 398b745c079STero Kristo return 0; 399b745c079STero Kristo } 400b745c079STero Kristo 401b745c079STero Kristo static const struct of_device_id ti_sci_clk_of_match[] = { 4023c13933cSTero Kristo { .compatible = "ti,k2g-sci-clk" }, 403b745c079STero Kristo { /* Sentinel */ }, 404b745c079STero Kristo }; 405b745c079STero Kristo MODULE_DEVICE_TABLE(of, ti_sci_clk_of_match); 406b745c079STero Kristo 407b745c079STero Kristo /** 408b745c079STero Kristo * ti_sci_clk_probe - Probe function for the TI SCI clock driver 409b745c079STero Kristo * @pdev: platform device pointer to be probed 410b745c079STero Kristo * 411b745c079STero Kristo * Probes the TI SCI clock device. Allocates a new clock provider 412b745c079STero Kristo * and registers this to the common clock framework. Also applies 413b745c079STero Kristo * any required flags to the identified clocks via clock lists 414b745c079STero Kristo * supplied from DT. Returns 0 for success, negative error value 415b745c079STero Kristo * for failure. 416b745c079STero Kristo */ 417b745c079STero Kristo static int ti_sci_clk_probe(struct platform_device *pdev) 418b745c079STero Kristo { 419b745c079STero Kristo struct device *dev = &pdev->dev; 420b745c079STero Kristo struct device_node *np = dev->of_node; 421b745c079STero Kristo struct sci_clk_provider *provider; 422b745c079STero Kristo const struct ti_sci_handle *handle; 423b745c079STero Kristo int ret; 4243c13933cSTero Kristo int num_clks = 0; 4253c13933cSTero Kristo struct sci_clk **clks = NULL; 4263c13933cSTero Kristo struct sci_clk **tmp_clks; 4273c13933cSTero Kristo struct sci_clk *sci_clk; 4283c13933cSTero Kristo int max_clks = 0; 4293c13933cSTero Kristo int clk_id = 0; 4303c13933cSTero Kristo int dev_id = 0; 4313c13933cSTero Kristo u8 num_parents; 4323c13933cSTero Kristo int gap_size = 0; 433b745c079STero Kristo 434b745c079STero Kristo handle = devm_ti_sci_get_handle(dev); 435b745c079STero Kristo if (IS_ERR(handle)) 436b745c079STero Kristo return PTR_ERR(handle); 437b745c079STero Kristo 438b745c079STero Kristo provider = devm_kzalloc(dev, sizeof(*provider), GFP_KERNEL); 439b745c079STero Kristo if (!provider) 440b745c079STero Kristo return -ENOMEM; 441b745c079STero Kristo 442b745c079STero Kristo provider->sci = handle; 443b745c079STero Kristo provider->ops = &handle->ops.clk_ops; 444b745c079STero Kristo provider->dev = dev; 445b745c079STero Kristo 4463c13933cSTero Kristo while (1) { 4473c13933cSTero Kristo ret = provider->ops->get_num_parents(provider->sci, dev_id, 4483c13933cSTero Kristo clk_id, &num_parents); 4493c13933cSTero Kristo if (ret) { 4503c13933cSTero Kristo gap_size++; 4513c13933cSTero Kristo if (!clk_id) { 4523c13933cSTero Kristo if (gap_size >= 5) 4533c13933cSTero Kristo break; 4543c13933cSTero Kristo dev_id++; 4553c13933cSTero Kristo } else { 4563c13933cSTero Kristo if (gap_size >= 2) { 4573c13933cSTero Kristo dev_id++; 4583c13933cSTero Kristo clk_id = 0; 4593c13933cSTero Kristo gap_size = 0; 4603c13933cSTero Kristo } else { 4613c13933cSTero Kristo clk_id++; 4623c13933cSTero Kristo } 4633c13933cSTero Kristo } 4643c13933cSTero Kristo continue; 4653c13933cSTero Kristo } 4663c13933cSTero Kristo 4673c13933cSTero Kristo gap_size = 0; 4683c13933cSTero Kristo 4693c13933cSTero Kristo if (num_clks == max_clks) { 4703c13933cSTero Kristo tmp_clks = devm_kmalloc_array(dev, max_clks + 64, 4713c13933cSTero Kristo sizeof(sci_clk), 4723c13933cSTero Kristo GFP_KERNEL); 4733c13933cSTero Kristo memcpy(tmp_clks, clks, max_clks * sizeof(sci_clk)); 4743c13933cSTero Kristo if (max_clks) 4753c13933cSTero Kristo devm_kfree(dev, clks); 4763c13933cSTero Kristo max_clks += 64; 4773c13933cSTero Kristo clks = tmp_clks; 4783c13933cSTero Kristo } 4793c13933cSTero Kristo 4803c13933cSTero Kristo sci_clk = devm_kzalloc(dev, sizeof(*sci_clk), GFP_KERNEL); 4813c13933cSTero Kristo if (!sci_clk) 4823c13933cSTero Kristo return -ENOMEM; 4833c13933cSTero Kristo sci_clk->dev_id = dev_id; 4843c13933cSTero Kristo sci_clk->clk_id = clk_id; 4853c13933cSTero Kristo sci_clk->provider = provider; 4863c13933cSTero Kristo sci_clk->num_parents = num_parents; 4873c13933cSTero Kristo 4883c13933cSTero Kristo clks[num_clks] = sci_clk; 4893c13933cSTero Kristo 4903c13933cSTero Kristo clk_id++; 4913c13933cSTero Kristo num_clks++; 4923c13933cSTero Kristo } 4933c13933cSTero Kristo 4943c13933cSTero Kristo provider->clocks = devm_kmalloc_array(dev, num_clks, sizeof(sci_clk), 4953c13933cSTero Kristo GFP_KERNEL); 4963c13933cSTero Kristo if (!provider->clocks) 4973c13933cSTero Kristo return -ENOMEM; 4983c13933cSTero Kristo 4993c13933cSTero Kristo memcpy(provider->clocks, clks, num_clks * sizeof(sci_clk)); 5003c13933cSTero Kristo 5013c13933cSTero Kristo provider->num_clocks = num_clks; 5023c13933cSTero Kristo 5033c13933cSTero Kristo devm_kfree(dev, clks); 5043c13933cSTero Kristo 505b745c079STero Kristo ret = ti_sci_init_clocks(provider); 506b745c079STero Kristo if (ret) { 507b745c079STero Kristo pr_err("ti-sci-init-clocks failed.\n"); 508b745c079STero Kristo return ret; 509b745c079STero Kristo } 510b745c079STero Kristo 511b745c079STero Kristo return of_clk_add_hw_provider(np, sci_clk_get, provider); 512b745c079STero Kristo } 513b745c079STero Kristo 514b745c079STero Kristo /** 515b745c079STero Kristo * ti_sci_clk_remove - Remove TI SCI clock device 516b745c079STero Kristo * @pdev: platform device pointer for the device to be removed 517b745c079STero Kristo * 518b745c079STero Kristo * Removes the TI SCI device. Unregisters the clock provider registered 519b745c079STero Kristo * via common clock framework. Any memory allocated for the device will 520b745c079STero Kristo * be free'd silently via the devm framework. Returns 0 always. 521b745c079STero Kristo */ 522b745c079STero Kristo static int ti_sci_clk_remove(struct platform_device *pdev) 523b745c079STero Kristo { 524b745c079STero Kristo of_clk_del_provider(pdev->dev.of_node); 525b745c079STero Kristo 526b745c079STero Kristo return 0; 527b745c079STero Kristo } 528b745c079STero Kristo 529b745c079STero Kristo static struct platform_driver ti_sci_clk_driver = { 530b745c079STero Kristo .probe = ti_sci_clk_probe, 531b745c079STero Kristo .remove = ti_sci_clk_remove, 532b745c079STero Kristo .driver = { 533b745c079STero Kristo .name = "ti-sci-clk", 534b745c079STero Kristo .of_match_table = of_match_ptr(ti_sci_clk_of_match), 535b745c079STero Kristo }, 536b745c079STero Kristo }; 537b745c079STero Kristo module_platform_driver(ti_sci_clk_driver); 538b745c079STero Kristo 539b745c079STero Kristo MODULE_LICENSE("GPL v2"); 540b745c079STero Kristo MODULE_DESCRIPTION("TI System Control Interface(SCI) Clock driver"); 541b745c079STero Kristo MODULE_AUTHOR("Tero Kristo"); 542b745c079STero Kristo MODULE_ALIAS("platform:ti-sci-clk"); 543