sci-clk.c 14 KB

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