clk-scmi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Power Interface (SCMI) Protocol based clock driver
  4. *
  5. * Copyright (C) 2018-2024 ARM Ltd.
  6. */
  7. #include <linux/bits.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/of.h>
  12. #include <linux/module.h>
  13. #include <linux/scmi_protocol.h>
  14. #include <asm/div64.h>
  15. #define NOT_ATOMIC false
  16. #define ATOMIC true
  17. enum scmi_clk_feats {
  18. SCMI_CLK_ATOMIC_SUPPORTED,
  19. SCMI_CLK_STATE_CTRL_SUPPORTED,
  20. SCMI_CLK_RATE_CTRL_SUPPORTED,
  21. SCMI_CLK_PARENT_CTRL_SUPPORTED,
  22. SCMI_CLK_DUTY_CYCLE_SUPPORTED,
  23. SCMI_CLK_FEATS_COUNT
  24. };
  25. #define SCMI_MAX_CLK_OPS BIT(SCMI_CLK_FEATS_COUNT)
  26. static const struct scmi_clk_proto_ops *scmi_proto_clk_ops;
  27. struct scmi_clk {
  28. u32 id;
  29. struct device *dev;
  30. struct clk_hw hw;
  31. const struct scmi_clock_info *info;
  32. const struct scmi_protocol_handle *ph;
  33. struct clk_parent_data *parent_data;
  34. };
  35. #define to_scmi_clk(clk) container_of(clk, struct scmi_clk, hw)
  36. static unsigned long scmi_clk_recalc_rate(struct clk_hw *hw,
  37. unsigned long parent_rate)
  38. {
  39. int ret;
  40. u64 rate;
  41. struct scmi_clk *clk = to_scmi_clk(hw);
  42. ret = scmi_proto_clk_ops->rate_get(clk->ph, clk->id, &rate);
  43. if (ret)
  44. return 0;
  45. return rate;
  46. }
  47. static int scmi_clk_determine_rate(struct clk_hw *hw,
  48. struct clk_rate_request *req)
  49. {
  50. u64 fmin, fmax, ftmp;
  51. struct scmi_clk *clk = to_scmi_clk(hw);
  52. /*
  53. * We can't figure out what rate it will be, so just return the
  54. * rate back to the caller. scmi_clk_recalc_rate() will be called
  55. * after the rate is set and we'll know what rate the clock is
  56. * running at then.
  57. */
  58. if (clk->info->rate_discrete)
  59. return 0;
  60. fmin = clk->info->range.min_rate;
  61. fmax = clk->info->range.max_rate;
  62. if (req->rate <= fmin) {
  63. req->rate = fmin;
  64. return 0;
  65. } else if (req->rate >= fmax) {
  66. req->rate = fmax;
  67. return 0;
  68. }
  69. ftmp = req->rate - fmin;
  70. ftmp += clk->info->range.step_size - 1; /* to round up */
  71. do_div(ftmp, clk->info->range.step_size);
  72. req->rate = ftmp * clk->info->range.step_size + fmin;
  73. return 0;
  74. }
  75. static int scmi_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  76. unsigned long parent_rate)
  77. {
  78. struct scmi_clk *clk = to_scmi_clk(hw);
  79. return scmi_proto_clk_ops->rate_set(clk->ph, clk->id, rate);
  80. }
  81. static int scmi_clk_set_parent(struct clk_hw *hw, u8 parent_index)
  82. {
  83. struct scmi_clk *clk = to_scmi_clk(hw);
  84. return scmi_proto_clk_ops->parent_set(clk->ph, clk->id, parent_index);
  85. }
  86. static u8 scmi_clk_get_parent(struct clk_hw *hw)
  87. {
  88. struct scmi_clk *clk = to_scmi_clk(hw);
  89. u32 parent_id, p_idx;
  90. int ret;
  91. ret = scmi_proto_clk_ops->parent_get(clk->ph, clk->id, &parent_id);
  92. if (ret)
  93. return 0;
  94. for (p_idx = 0; p_idx < clk->info->num_parents; p_idx++) {
  95. if (clk->parent_data[p_idx].index == parent_id)
  96. break;
  97. }
  98. if (p_idx == clk->info->num_parents)
  99. return 0;
  100. return p_idx;
  101. }
  102. static int scmi_clk_enable(struct clk_hw *hw)
  103. {
  104. struct scmi_clk *clk = to_scmi_clk(hw);
  105. return scmi_proto_clk_ops->enable(clk->ph, clk->id, NOT_ATOMIC);
  106. }
  107. static void scmi_clk_disable(struct clk_hw *hw)
  108. {
  109. struct scmi_clk *clk = to_scmi_clk(hw);
  110. scmi_proto_clk_ops->disable(clk->ph, clk->id, NOT_ATOMIC);
  111. }
  112. static int scmi_clk_atomic_enable(struct clk_hw *hw)
  113. {
  114. struct scmi_clk *clk = to_scmi_clk(hw);
  115. return scmi_proto_clk_ops->enable(clk->ph, clk->id, ATOMIC);
  116. }
  117. static void scmi_clk_atomic_disable(struct clk_hw *hw)
  118. {
  119. struct scmi_clk *clk = to_scmi_clk(hw);
  120. scmi_proto_clk_ops->disable(clk->ph, clk->id, ATOMIC);
  121. }
  122. static int __scmi_clk_is_enabled(struct clk_hw *hw, bool atomic)
  123. {
  124. int ret;
  125. bool enabled = false;
  126. struct scmi_clk *clk = to_scmi_clk(hw);
  127. ret = scmi_proto_clk_ops->state_get(clk->ph, clk->id, &enabled, atomic);
  128. if (ret)
  129. dev_warn(clk->dev,
  130. "Failed to get state for clock ID %d\n", clk->id);
  131. return !!enabled;
  132. }
  133. static int scmi_clk_atomic_is_enabled(struct clk_hw *hw)
  134. {
  135. return __scmi_clk_is_enabled(hw, ATOMIC);
  136. }
  137. static int scmi_clk_is_enabled(struct clk_hw *hw)
  138. {
  139. return __scmi_clk_is_enabled(hw, NOT_ATOMIC);
  140. }
  141. static int scmi_clk_get_duty_cycle(struct clk_hw *hw, struct clk_duty *duty)
  142. {
  143. int ret;
  144. u32 val;
  145. struct scmi_clk *clk = to_scmi_clk(hw);
  146. ret = scmi_proto_clk_ops->config_oem_get(clk->ph, clk->id,
  147. SCMI_CLOCK_CFG_DUTY_CYCLE,
  148. &val, NULL, false);
  149. if (!ret) {
  150. duty->num = val;
  151. duty->den = 100;
  152. } else {
  153. dev_warn(clk->dev,
  154. "Failed to get duty cycle for clock ID %d\n", clk->id);
  155. }
  156. return ret;
  157. }
  158. static int scmi_clk_set_duty_cycle(struct clk_hw *hw, struct clk_duty *duty)
  159. {
  160. int ret;
  161. u32 val;
  162. struct scmi_clk *clk = to_scmi_clk(hw);
  163. /* SCMI OEM Duty Cycle is expressed as a percentage */
  164. val = (duty->num * 100) / duty->den;
  165. ret = scmi_proto_clk_ops->config_oem_set(clk->ph, clk->id,
  166. SCMI_CLOCK_CFG_DUTY_CYCLE,
  167. val, false);
  168. if (ret)
  169. dev_warn(clk->dev,
  170. "Failed to set duty cycle(%u/%u) for clock ID %d\n",
  171. duty->num, duty->den, clk->id);
  172. return ret;
  173. }
  174. static int scmi_clk_ops_init(struct device *dev, struct scmi_clk *sclk,
  175. const struct clk_ops *scmi_ops)
  176. {
  177. int ret;
  178. unsigned long min_rate, max_rate;
  179. struct clk_init_data init = {
  180. .flags = CLK_GET_RATE_NOCACHE,
  181. .num_parents = sclk->info->num_parents,
  182. .ops = scmi_ops,
  183. .name = sclk->info->name,
  184. .parent_data = sclk->parent_data,
  185. };
  186. sclk->hw.init = &init;
  187. ret = devm_clk_hw_register(dev, &sclk->hw);
  188. if (ret)
  189. return ret;
  190. if (sclk->info->rate_discrete) {
  191. int num_rates = sclk->info->list.num_rates;
  192. if (num_rates <= 0)
  193. return -EINVAL;
  194. min_rate = sclk->info->list.rates[0];
  195. max_rate = sclk->info->list.rates[num_rates - 1];
  196. } else {
  197. min_rate = sclk->info->range.min_rate;
  198. max_rate = sclk->info->range.max_rate;
  199. }
  200. clk_hw_set_rate_range(&sclk->hw, min_rate, max_rate);
  201. return ret;
  202. }
  203. /**
  204. * scmi_clk_ops_alloc() - Alloc and configure clock operations
  205. * @dev: A device reference for devres
  206. * @feats_key: A bitmap representing the desired clk_ops capabilities
  207. *
  208. * Allocate and configure a proper set of clock operations depending on the
  209. * specifically required SCMI clock features.
  210. *
  211. * Return: A pointer to the allocated and configured clk_ops on success,
  212. * or NULL on allocation failure.
  213. */
  214. static const struct clk_ops *
  215. scmi_clk_ops_alloc(struct device *dev, unsigned long feats_key)
  216. {
  217. struct clk_ops *ops;
  218. ops = devm_kzalloc(dev, sizeof(*ops), GFP_KERNEL);
  219. if (!ops)
  220. return NULL;
  221. /*
  222. * We can provide enable/disable/is_enabled atomic callbacks only if the
  223. * underlying SCMI transport for an SCMI instance is configured to
  224. * handle SCMI commands in an atomic manner.
  225. *
  226. * When no SCMI atomic transport support is available we instead provide
  227. * only the prepare/unprepare API, as allowed by the clock framework
  228. * when atomic calls are not available.
  229. */
  230. if (feats_key & BIT(SCMI_CLK_STATE_CTRL_SUPPORTED)) {
  231. if (feats_key & BIT(SCMI_CLK_ATOMIC_SUPPORTED)) {
  232. ops->enable = scmi_clk_atomic_enable;
  233. ops->disable = scmi_clk_atomic_disable;
  234. } else {
  235. ops->prepare = scmi_clk_enable;
  236. ops->unprepare = scmi_clk_disable;
  237. }
  238. }
  239. if (feats_key & BIT(SCMI_CLK_ATOMIC_SUPPORTED))
  240. ops->is_enabled = scmi_clk_atomic_is_enabled;
  241. else
  242. ops->is_prepared = scmi_clk_is_enabled;
  243. /* Rate ops */
  244. ops->recalc_rate = scmi_clk_recalc_rate;
  245. ops->determine_rate = scmi_clk_determine_rate;
  246. if (feats_key & BIT(SCMI_CLK_RATE_CTRL_SUPPORTED))
  247. ops->set_rate = scmi_clk_set_rate;
  248. /* Parent ops */
  249. ops->get_parent = scmi_clk_get_parent;
  250. if (feats_key & BIT(SCMI_CLK_PARENT_CTRL_SUPPORTED))
  251. ops->set_parent = scmi_clk_set_parent;
  252. /* Duty cycle */
  253. if (feats_key & BIT(SCMI_CLK_DUTY_CYCLE_SUPPORTED)) {
  254. ops->get_duty_cycle = scmi_clk_get_duty_cycle;
  255. ops->set_duty_cycle = scmi_clk_set_duty_cycle;
  256. }
  257. return ops;
  258. }
  259. /**
  260. * scmi_clk_ops_select() - Select a proper set of clock operations
  261. * @sclk: A reference to an SCMI clock descriptor
  262. * @atomic_capable: A flag to indicate if atomic mode is supported by the
  263. * transport
  264. * @atomic_threshold_us: Platform atomic threshold value in microseconds:
  265. * clk_ops are atomic when clock enable latency is less
  266. * than this threshold
  267. * @clk_ops_db: A reference to the array used as a database to store all the
  268. * created clock operations combinations.
  269. * @db_size: Maximum number of entries held by @clk_ops_db
  270. *
  271. * After having built a bitmap descriptor to represent the set of features
  272. * needed by this SCMI clock, at first use it to lookup into the set of
  273. * previously allocated clk_ops to check if a suitable combination of clock
  274. * operations was already created; when no match is found allocate a brand new
  275. * set of clk_ops satisfying the required combination of features and save it
  276. * for future references.
  277. *
  278. * In this way only one set of clk_ops is ever created for each different
  279. * combination that is effectively needed by a driver instance.
  280. *
  281. * Return: A pointer to the allocated and configured clk_ops on success, or
  282. * NULL otherwise.
  283. */
  284. static const struct clk_ops *
  285. scmi_clk_ops_select(struct scmi_clk *sclk, bool atomic_capable,
  286. unsigned int atomic_threshold_us,
  287. const struct clk_ops **clk_ops_db, size_t db_size)
  288. {
  289. int ret;
  290. u32 val;
  291. const struct scmi_clock_info *ci = sclk->info;
  292. unsigned int feats_key = 0;
  293. const struct clk_ops *ops;
  294. /*
  295. * Note that when transport is atomic but SCMI protocol did not
  296. * specify (or support) an enable_latency associated with a
  297. * clock, we default to use atomic operations mode.
  298. */
  299. if (atomic_capable && ci->enable_latency <= atomic_threshold_us)
  300. feats_key |= BIT(SCMI_CLK_ATOMIC_SUPPORTED);
  301. if (!ci->state_ctrl_forbidden)
  302. feats_key |= BIT(SCMI_CLK_STATE_CTRL_SUPPORTED);
  303. if (!ci->rate_ctrl_forbidden)
  304. feats_key |= BIT(SCMI_CLK_RATE_CTRL_SUPPORTED);
  305. if (!ci->parent_ctrl_forbidden)
  306. feats_key |= BIT(SCMI_CLK_PARENT_CTRL_SUPPORTED);
  307. if (ci->extended_config) {
  308. ret = scmi_proto_clk_ops->config_oem_get(sclk->ph, sclk->id,
  309. SCMI_CLOCK_CFG_DUTY_CYCLE,
  310. &val, NULL, false);
  311. if (!ret)
  312. feats_key |= BIT(SCMI_CLK_DUTY_CYCLE_SUPPORTED);
  313. }
  314. if (WARN_ON(feats_key >= db_size))
  315. return NULL;
  316. /* Lookup previously allocated ops */
  317. ops = clk_ops_db[feats_key];
  318. if (ops)
  319. return ops;
  320. /* Did not find a pre-allocated clock_ops */
  321. ops = scmi_clk_ops_alloc(sclk->dev, feats_key);
  322. if (!ops)
  323. return NULL;
  324. /* Store new ops combinations */
  325. clk_ops_db[feats_key] = ops;
  326. return ops;
  327. }
  328. static int scmi_clocks_probe(struct scmi_device *sdev)
  329. {
  330. int idx, count, err;
  331. unsigned int atomic_threshold_us;
  332. bool transport_is_atomic;
  333. struct clk_hw **hws;
  334. struct clk_hw_onecell_data *clk_data;
  335. struct device *dev = &sdev->dev;
  336. struct device_node *np = dev->of_node;
  337. const struct scmi_handle *handle = sdev->handle;
  338. struct scmi_protocol_handle *ph;
  339. const struct clk_ops *scmi_clk_ops_db[SCMI_MAX_CLK_OPS] = {};
  340. struct scmi_clk *sclks;
  341. if (!handle)
  342. return -ENODEV;
  343. scmi_proto_clk_ops =
  344. handle->devm_protocol_get(sdev, SCMI_PROTOCOL_CLOCK, &ph);
  345. if (IS_ERR(scmi_proto_clk_ops))
  346. return PTR_ERR(scmi_proto_clk_ops);
  347. count = scmi_proto_clk_ops->count_get(ph);
  348. if (count < 0) {
  349. dev_err(dev, "%pOFn: invalid clock output count\n", np);
  350. return -EINVAL;
  351. }
  352. clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, count),
  353. GFP_KERNEL);
  354. if (!clk_data)
  355. return -ENOMEM;
  356. clk_data->num = count;
  357. hws = clk_data->hws;
  358. transport_is_atomic = handle->is_transport_atomic(handle,
  359. &atomic_threshold_us);
  360. sclks = devm_kcalloc(dev, count, sizeof(*sclks), GFP_KERNEL);
  361. if (!sclks)
  362. return -ENOMEM;
  363. for (idx = 0; idx < count; idx++)
  364. hws[idx] = &sclks[idx].hw;
  365. for (idx = 0; idx < count; idx++) {
  366. struct scmi_clk *sclk = &sclks[idx];
  367. const struct clk_ops *scmi_ops;
  368. sclk->info = scmi_proto_clk_ops->info_get(ph, idx);
  369. if (!sclk->info) {
  370. dev_dbg(dev, "invalid clock info for idx %d\n", idx);
  371. hws[idx] = NULL;
  372. continue;
  373. }
  374. sclk->id = idx;
  375. sclk->ph = ph;
  376. sclk->dev = dev;
  377. /*
  378. * Note that the scmi_clk_ops_db is on the stack, not global,
  379. * because it cannot be shared between multiple probe-sequences
  380. * to avoid sharing the devm_ allocated clk_ops between multiple
  381. * SCMI clk driver instances.
  382. */
  383. scmi_ops = scmi_clk_ops_select(sclk, transport_is_atomic,
  384. atomic_threshold_us,
  385. scmi_clk_ops_db,
  386. ARRAY_SIZE(scmi_clk_ops_db));
  387. if (!scmi_ops)
  388. return -ENOMEM;
  389. /* Initialize clock parent data. */
  390. if (sclk->info->num_parents > 0) {
  391. sclk->parent_data = devm_kcalloc(dev, sclk->info->num_parents,
  392. sizeof(*sclk->parent_data), GFP_KERNEL);
  393. if (!sclk->parent_data)
  394. return -ENOMEM;
  395. for (int i = 0; i < sclk->info->num_parents; i++) {
  396. sclk->parent_data[i].index = sclk->info->parents[i];
  397. sclk->parent_data[i].hw = hws[sclk->info->parents[i]];
  398. }
  399. }
  400. err = scmi_clk_ops_init(dev, sclk, scmi_ops);
  401. if (err) {
  402. dev_err(dev, "failed to register clock %d\n", idx);
  403. devm_kfree(dev, sclk->parent_data);
  404. hws[idx] = NULL;
  405. } else {
  406. dev_dbg(dev, "Registered clock:%s%s\n",
  407. sclk->info->name,
  408. scmi_ops->enable ? " (atomic ops)" : "");
  409. }
  410. }
  411. return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
  412. clk_data);
  413. }
  414. static const struct scmi_device_id scmi_id_table[] = {
  415. { SCMI_PROTOCOL_CLOCK, "clocks" },
  416. { },
  417. };
  418. MODULE_DEVICE_TABLE(scmi, scmi_id_table);
  419. static struct scmi_driver scmi_clocks_driver = {
  420. .name = "scmi-clocks",
  421. .probe = scmi_clocks_probe,
  422. .id_table = scmi_id_table,
  423. };
  424. module_scmi_driver(scmi_clocks_driver);
  425. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  426. MODULE_DESCRIPTION("ARM SCMI clock driver");
  427. MODULE_LICENSE("GPL v2");