clk-scu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2018-2021 NXP
  4. * Dong Aisheng <[email protected]>
  5. */
  6. #include <dt-bindings/firmware/imx/rsrc.h>
  7. #include <linux/arm-smccc.h>
  8. #include <linux/bsearch.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/err.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_domain.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/slab.h>
  16. #include "clk-scu.h"
  17. #define IMX_SIP_CPUFREQ 0xC2000001
  18. #define IMX_SIP_SET_CPUFREQ 0x00
  19. static struct imx_sc_ipc *ccm_ipc_handle;
  20. static struct device_node *pd_np;
  21. static struct platform_driver imx_clk_scu_driver;
  22. static const struct imx_clk_scu_rsrc_table *rsrc_table;
  23. struct imx_scu_clk_node {
  24. const char *name;
  25. u32 rsrc;
  26. u8 clk_type;
  27. const char * const *parents;
  28. int num_parents;
  29. struct clk_hw *hw;
  30. struct list_head node;
  31. };
  32. struct list_head imx_scu_clks[IMX_SC_R_LAST];
  33. /*
  34. * struct clk_scu - Description of one SCU clock
  35. * @hw: the common clk_hw
  36. * @rsrc_id: resource ID of this SCU clock
  37. * @clk_type: type of this clock resource
  38. */
  39. struct clk_scu {
  40. struct clk_hw hw;
  41. u16 rsrc_id;
  42. u8 clk_type;
  43. /* for state save&restore */
  44. struct clk_hw *parent;
  45. u8 parent_index;
  46. bool is_enabled;
  47. u32 rate;
  48. };
  49. /*
  50. * struct clk_gpr_scu - Description of one SCU GPR clock
  51. * @hw: the common clk_hw
  52. * @rsrc_id: resource ID of this SCU clock
  53. * @gpr_id: GPR ID index to control the divider
  54. */
  55. struct clk_gpr_scu {
  56. struct clk_hw hw;
  57. u16 rsrc_id;
  58. u8 gpr_id;
  59. u8 flags;
  60. bool gate_invert;
  61. };
  62. #define to_clk_gpr_scu(_hw) container_of(_hw, struct clk_gpr_scu, hw)
  63. /*
  64. * struct imx_sc_msg_req_set_clock_rate - clock set rate protocol
  65. * @hdr: SCU protocol header
  66. * @rate: rate to set
  67. * @resource: clock resource to set rate
  68. * @clk: clk type of this resource
  69. *
  70. * This structure describes the SCU protocol of clock rate set
  71. */
  72. struct imx_sc_msg_req_set_clock_rate {
  73. struct imx_sc_rpc_msg hdr;
  74. __le32 rate;
  75. __le16 resource;
  76. u8 clk;
  77. } __packed __aligned(4);
  78. struct req_get_clock_rate {
  79. __le16 resource;
  80. u8 clk;
  81. } __packed __aligned(4);
  82. struct resp_get_clock_rate {
  83. __le32 rate;
  84. };
  85. /*
  86. * struct imx_sc_msg_get_clock_rate - clock get rate protocol
  87. * @hdr: SCU protocol header
  88. * @req: get rate request protocol
  89. * @resp: get rate response protocol
  90. *
  91. * This structure describes the SCU protocol of clock rate get
  92. */
  93. struct imx_sc_msg_get_clock_rate {
  94. struct imx_sc_rpc_msg hdr;
  95. union {
  96. struct req_get_clock_rate req;
  97. struct resp_get_clock_rate resp;
  98. } data;
  99. };
  100. /*
  101. * struct imx_sc_msg_get_clock_parent - clock get parent protocol
  102. * @hdr: SCU protocol header
  103. * @req: get parent request protocol
  104. * @resp: get parent response protocol
  105. *
  106. * This structure describes the SCU protocol of clock get parent
  107. */
  108. struct imx_sc_msg_get_clock_parent {
  109. struct imx_sc_rpc_msg hdr;
  110. union {
  111. struct req_get_clock_parent {
  112. __le16 resource;
  113. u8 clk;
  114. } __packed __aligned(4) req;
  115. struct resp_get_clock_parent {
  116. u8 parent;
  117. } resp;
  118. } data;
  119. };
  120. /*
  121. * struct imx_sc_msg_set_clock_parent - clock set parent protocol
  122. * @hdr: SCU protocol header
  123. * @req: set parent request protocol
  124. *
  125. * This structure describes the SCU protocol of clock set parent
  126. */
  127. struct imx_sc_msg_set_clock_parent {
  128. struct imx_sc_rpc_msg hdr;
  129. __le16 resource;
  130. u8 clk;
  131. u8 parent;
  132. } __packed;
  133. /*
  134. * struct imx_sc_msg_req_clock_enable - clock gate protocol
  135. * @hdr: SCU protocol header
  136. * @resource: clock resource to gate
  137. * @clk: clk type of this resource
  138. * @enable: whether gate off the clock
  139. * @autog: HW auto gate enable
  140. *
  141. * This structure describes the SCU protocol of clock gate
  142. */
  143. struct imx_sc_msg_req_clock_enable {
  144. struct imx_sc_rpc_msg hdr;
  145. __le16 resource;
  146. u8 clk;
  147. u8 enable;
  148. u8 autog;
  149. } __packed __aligned(4);
  150. static inline struct clk_scu *to_clk_scu(struct clk_hw *hw)
  151. {
  152. return container_of(hw, struct clk_scu, hw);
  153. }
  154. static inline int imx_scu_clk_search_cmp(const void *rsrc, const void *rsrc_p)
  155. {
  156. return *(u32 *)rsrc - *(u32 *)rsrc_p;
  157. }
  158. static bool imx_scu_clk_is_valid(u32 rsrc_id)
  159. {
  160. void *p;
  161. if (!rsrc_table)
  162. return true;
  163. p = bsearch(&rsrc_id, rsrc_table->rsrc, rsrc_table->num,
  164. sizeof(rsrc_table->rsrc[0]), imx_scu_clk_search_cmp);
  165. return p != NULL;
  166. }
  167. int imx_clk_scu_init(struct device_node *np,
  168. const struct imx_clk_scu_rsrc_table *data)
  169. {
  170. u32 clk_cells;
  171. int ret, i;
  172. ret = imx_scu_get_handle(&ccm_ipc_handle);
  173. if (ret)
  174. return ret;
  175. of_property_read_u32(np, "#clock-cells", &clk_cells);
  176. if (clk_cells == 2) {
  177. for (i = 0; i < IMX_SC_R_LAST; i++)
  178. INIT_LIST_HEAD(&imx_scu_clks[i]);
  179. /* pd_np will be used to attach power domains later */
  180. pd_np = of_find_compatible_node(NULL, NULL, "fsl,scu-pd");
  181. if (!pd_np)
  182. return -EINVAL;
  183. rsrc_table = data;
  184. }
  185. return platform_driver_register(&imx_clk_scu_driver);
  186. }
  187. /*
  188. * clk_scu_recalc_rate - Get clock rate for a SCU clock
  189. * @hw: clock to get rate for
  190. * @parent_rate: parent rate provided by common clock framework, not used
  191. *
  192. * Gets the current clock rate of a SCU clock. Returns the current
  193. * clock rate, or zero in failure.
  194. */
  195. static unsigned long clk_scu_recalc_rate(struct clk_hw *hw,
  196. unsigned long parent_rate)
  197. {
  198. struct clk_scu *clk = to_clk_scu(hw);
  199. struct imx_sc_msg_get_clock_rate msg;
  200. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  201. int ret;
  202. hdr->ver = IMX_SC_RPC_VERSION;
  203. hdr->svc = IMX_SC_RPC_SVC_PM;
  204. hdr->func = IMX_SC_PM_FUNC_GET_CLOCK_RATE;
  205. hdr->size = 2;
  206. msg.data.req.resource = cpu_to_le16(clk->rsrc_id);
  207. msg.data.req.clk = clk->clk_type;
  208. ret = imx_scu_call_rpc(ccm_ipc_handle, &msg, true);
  209. if (ret) {
  210. pr_err("%s: failed to get clock rate %d\n",
  211. clk_hw_get_name(hw), ret);
  212. return 0;
  213. }
  214. return le32_to_cpu(msg.data.resp.rate);
  215. }
  216. /*
  217. * clk_scu_round_rate - Round clock rate for a SCU clock
  218. * @hw: clock to round rate for
  219. * @rate: rate to round
  220. * @parent_rate: parent rate provided by common clock framework, not used
  221. *
  222. * Returns the current clock rate, or zero in failure.
  223. */
  224. static long clk_scu_round_rate(struct clk_hw *hw, unsigned long rate,
  225. unsigned long *parent_rate)
  226. {
  227. /*
  228. * Assume we support all the requested rate and let the SCU firmware
  229. * to handle the left work
  230. */
  231. return rate;
  232. }
  233. static int clk_scu_atf_set_cpu_rate(struct clk_hw *hw, unsigned long rate,
  234. unsigned long parent_rate)
  235. {
  236. struct clk_scu *clk = to_clk_scu(hw);
  237. struct arm_smccc_res res;
  238. unsigned long cluster_id;
  239. if (clk->rsrc_id == IMX_SC_R_A35 || clk->rsrc_id == IMX_SC_R_A53)
  240. cluster_id = 0;
  241. else if (clk->rsrc_id == IMX_SC_R_A72)
  242. cluster_id = 1;
  243. else
  244. return -EINVAL;
  245. /* CPU frequency scaling can ONLY be done by ARM-Trusted-Firmware */
  246. arm_smccc_smc(IMX_SIP_CPUFREQ, IMX_SIP_SET_CPUFREQ,
  247. cluster_id, rate, 0, 0, 0, 0, &res);
  248. return 0;
  249. }
  250. /*
  251. * clk_scu_set_rate - Set rate for a SCU clock
  252. * @hw: clock to change rate for
  253. * @rate: target rate for the clock
  254. * @parent_rate: rate of the clock parent, not used for SCU clocks
  255. *
  256. * Sets a clock frequency for a SCU clock. Returns the SCU
  257. * protocol status.
  258. */
  259. static int clk_scu_set_rate(struct clk_hw *hw, unsigned long rate,
  260. unsigned long parent_rate)
  261. {
  262. struct clk_scu *clk = to_clk_scu(hw);
  263. struct imx_sc_msg_req_set_clock_rate msg;
  264. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  265. hdr->ver = IMX_SC_RPC_VERSION;
  266. hdr->svc = IMX_SC_RPC_SVC_PM;
  267. hdr->func = IMX_SC_PM_FUNC_SET_CLOCK_RATE;
  268. hdr->size = 3;
  269. msg.rate = cpu_to_le32(rate);
  270. msg.resource = cpu_to_le16(clk->rsrc_id);
  271. msg.clk = clk->clk_type;
  272. return imx_scu_call_rpc(ccm_ipc_handle, &msg, true);
  273. }
  274. static u8 clk_scu_get_parent(struct clk_hw *hw)
  275. {
  276. struct clk_scu *clk = to_clk_scu(hw);
  277. struct imx_sc_msg_get_clock_parent msg;
  278. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  279. int ret;
  280. hdr->ver = IMX_SC_RPC_VERSION;
  281. hdr->svc = IMX_SC_RPC_SVC_PM;
  282. hdr->func = IMX_SC_PM_FUNC_GET_CLOCK_PARENT;
  283. hdr->size = 2;
  284. msg.data.req.resource = cpu_to_le16(clk->rsrc_id);
  285. msg.data.req.clk = clk->clk_type;
  286. ret = imx_scu_call_rpc(ccm_ipc_handle, &msg, true);
  287. if (ret) {
  288. pr_err("%s: failed to get clock parent %d\n",
  289. clk_hw_get_name(hw), ret);
  290. return 0;
  291. }
  292. clk->parent_index = msg.data.resp.parent;
  293. return msg.data.resp.parent;
  294. }
  295. static int clk_scu_set_parent(struct clk_hw *hw, u8 index)
  296. {
  297. struct clk_scu *clk = to_clk_scu(hw);
  298. struct imx_sc_msg_set_clock_parent msg;
  299. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  300. int ret;
  301. hdr->ver = IMX_SC_RPC_VERSION;
  302. hdr->svc = IMX_SC_RPC_SVC_PM;
  303. hdr->func = IMX_SC_PM_FUNC_SET_CLOCK_PARENT;
  304. hdr->size = 2;
  305. msg.resource = cpu_to_le16(clk->rsrc_id);
  306. msg.clk = clk->clk_type;
  307. msg.parent = index;
  308. ret = imx_scu_call_rpc(ccm_ipc_handle, &msg, true);
  309. if (ret) {
  310. pr_err("%s: failed to set clock parent %d\n",
  311. clk_hw_get_name(hw), ret);
  312. return ret;
  313. }
  314. clk->parent_index = index;
  315. return 0;
  316. }
  317. static int sc_pm_clock_enable(struct imx_sc_ipc *ipc, u16 resource,
  318. u8 clk, bool enable, bool autog)
  319. {
  320. struct imx_sc_msg_req_clock_enable msg;
  321. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  322. hdr->ver = IMX_SC_RPC_VERSION;
  323. hdr->svc = IMX_SC_RPC_SVC_PM;
  324. hdr->func = IMX_SC_PM_FUNC_CLOCK_ENABLE;
  325. hdr->size = 3;
  326. msg.resource = cpu_to_le16(resource);
  327. msg.clk = clk;
  328. msg.enable = enable;
  329. msg.autog = autog;
  330. return imx_scu_call_rpc(ccm_ipc_handle, &msg, true);
  331. }
  332. /*
  333. * clk_scu_prepare - Enable a SCU clock
  334. * @hw: clock to enable
  335. *
  336. * Enable the clock at the DSC slice level
  337. */
  338. static int clk_scu_prepare(struct clk_hw *hw)
  339. {
  340. struct clk_scu *clk = to_clk_scu(hw);
  341. return sc_pm_clock_enable(ccm_ipc_handle, clk->rsrc_id,
  342. clk->clk_type, true, false);
  343. }
  344. /*
  345. * clk_scu_unprepare - Disable a SCU clock
  346. * @hw: clock to enable
  347. *
  348. * Disable the clock at the DSC slice level
  349. */
  350. static void clk_scu_unprepare(struct clk_hw *hw)
  351. {
  352. struct clk_scu *clk = to_clk_scu(hw);
  353. int ret;
  354. ret = sc_pm_clock_enable(ccm_ipc_handle, clk->rsrc_id,
  355. clk->clk_type, false, false);
  356. if (ret)
  357. pr_warn("%s: clk unprepare failed %d\n", clk_hw_get_name(hw),
  358. ret);
  359. }
  360. static const struct clk_ops clk_scu_ops = {
  361. .recalc_rate = clk_scu_recalc_rate,
  362. .round_rate = clk_scu_round_rate,
  363. .set_rate = clk_scu_set_rate,
  364. .get_parent = clk_scu_get_parent,
  365. .set_parent = clk_scu_set_parent,
  366. .prepare = clk_scu_prepare,
  367. .unprepare = clk_scu_unprepare,
  368. };
  369. static const struct clk_ops clk_scu_cpu_ops = {
  370. .recalc_rate = clk_scu_recalc_rate,
  371. .round_rate = clk_scu_round_rate,
  372. .set_rate = clk_scu_atf_set_cpu_rate,
  373. .prepare = clk_scu_prepare,
  374. .unprepare = clk_scu_unprepare,
  375. };
  376. static const struct clk_ops clk_scu_pi_ops = {
  377. .recalc_rate = clk_scu_recalc_rate,
  378. .round_rate = clk_scu_round_rate,
  379. .set_rate = clk_scu_set_rate,
  380. };
  381. struct clk_hw *__imx_clk_scu(struct device *dev, const char *name,
  382. const char * const *parents, int num_parents,
  383. u32 rsrc_id, u8 clk_type)
  384. {
  385. struct clk_init_data init;
  386. struct clk_scu *clk;
  387. struct clk_hw *hw;
  388. int ret;
  389. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  390. if (!clk)
  391. return ERR_PTR(-ENOMEM);
  392. clk->rsrc_id = rsrc_id;
  393. clk->clk_type = clk_type;
  394. init.name = name;
  395. init.ops = &clk_scu_ops;
  396. if (rsrc_id == IMX_SC_R_A35 || rsrc_id == IMX_SC_R_A53 || rsrc_id == IMX_SC_R_A72)
  397. init.ops = &clk_scu_cpu_ops;
  398. else if (rsrc_id == IMX_SC_R_PI_0_PLL)
  399. init.ops = &clk_scu_pi_ops;
  400. else
  401. init.ops = &clk_scu_ops;
  402. init.parent_names = parents;
  403. init.num_parents = num_parents;
  404. /*
  405. * Note on MX8, the clocks are tightly coupled with power domain
  406. * that once the power domain is off, the clock status may be
  407. * lost. So we make it NOCACHE to let user to retrieve the real
  408. * clock status from HW instead of using the possible invalid
  409. * cached rate.
  410. */
  411. init.flags = CLK_GET_RATE_NOCACHE;
  412. clk->hw.init = &init;
  413. hw = &clk->hw;
  414. ret = clk_hw_register(dev, hw);
  415. if (ret) {
  416. kfree(clk);
  417. hw = ERR_PTR(ret);
  418. return hw;
  419. }
  420. if (dev)
  421. dev_set_drvdata(dev, clk);
  422. return hw;
  423. }
  424. struct clk_hw *imx_scu_of_clk_src_get(struct of_phandle_args *clkspec,
  425. void *data)
  426. {
  427. unsigned int rsrc = clkspec->args[0];
  428. unsigned int idx = clkspec->args[1];
  429. struct list_head *scu_clks = data;
  430. struct imx_scu_clk_node *clk;
  431. list_for_each_entry(clk, &scu_clks[rsrc], node) {
  432. if (clk->clk_type == idx)
  433. return clk->hw;
  434. }
  435. return ERR_PTR(-ENODEV);
  436. }
  437. static int imx_clk_scu_probe(struct platform_device *pdev)
  438. {
  439. struct device *dev = &pdev->dev;
  440. struct imx_scu_clk_node *clk = dev_get_platdata(dev);
  441. struct clk_hw *hw;
  442. int ret;
  443. if (!((clk->rsrc == IMX_SC_R_A35) || (clk->rsrc == IMX_SC_R_A53) ||
  444. (clk->rsrc == IMX_SC_R_A72))) {
  445. pm_runtime_set_suspended(dev);
  446. pm_runtime_set_autosuspend_delay(dev, 50);
  447. pm_runtime_use_autosuspend(&pdev->dev);
  448. pm_runtime_enable(dev);
  449. ret = pm_runtime_resume_and_get(dev);
  450. if (ret) {
  451. pm_genpd_remove_device(dev);
  452. pm_runtime_disable(dev);
  453. return ret;
  454. }
  455. }
  456. hw = __imx_clk_scu(dev, clk->name, clk->parents, clk->num_parents,
  457. clk->rsrc, clk->clk_type);
  458. if (IS_ERR(hw)) {
  459. pm_runtime_disable(dev);
  460. return PTR_ERR(hw);
  461. }
  462. clk->hw = hw;
  463. list_add_tail(&clk->node, &imx_scu_clks[clk->rsrc]);
  464. if (!((clk->rsrc == IMX_SC_R_A35) || (clk->rsrc == IMX_SC_R_A53) ||
  465. (clk->rsrc == IMX_SC_R_A72))) {
  466. pm_runtime_mark_last_busy(&pdev->dev);
  467. pm_runtime_put_autosuspend(&pdev->dev);
  468. }
  469. dev_dbg(dev, "register SCU clock rsrc:%d type:%d\n", clk->rsrc,
  470. clk->clk_type);
  471. return 0;
  472. }
  473. static int __maybe_unused imx_clk_scu_suspend(struct device *dev)
  474. {
  475. struct clk_scu *clk = dev_get_drvdata(dev);
  476. u32 rsrc_id = clk->rsrc_id;
  477. if ((rsrc_id == IMX_SC_R_A35) || (rsrc_id == IMX_SC_R_A53) ||
  478. (rsrc_id == IMX_SC_R_A72))
  479. return 0;
  480. clk->parent = clk_hw_get_parent(&clk->hw);
  481. /* DC SS needs to handle bypass clock using non-cached clock rate */
  482. if (clk->rsrc_id == IMX_SC_R_DC_0_VIDEO0 ||
  483. clk->rsrc_id == IMX_SC_R_DC_0_VIDEO1 ||
  484. clk->rsrc_id == IMX_SC_R_DC_1_VIDEO0 ||
  485. clk->rsrc_id == IMX_SC_R_DC_1_VIDEO1)
  486. clk->rate = clk_scu_recalc_rate(&clk->hw, 0);
  487. else
  488. clk->rate = clk_hw_get_rate(&clk->hw);
  489. clk->is_enabled = clk_hw_is_enabled(&clk->hw);
  490. if (clk->parent)
  491. dev_dbg(dev, "save parent %s idx %u\n", clk_hw_get_name(clk->parent),
  492. clk->parent_index);
  493. if (clk->rate)
  494. dev_dbg(dev, "save rate %d\n", clk->rate);
  495. if (clk->is_enabled)
  496. dev_dbg(dev, "save enabled state\n");
  497. return 0;
  498. }
  499. static int __maybe_unused imx_clk_scu_resume(struct device *dev)
  500. {
  501. struct clk_scu *clk = dev_get_drvdata(dev);
  502. u32 rsrc_id = clk->rsrc_id;
  503. int ret = 0;
  504. if ((rsrc_id == IMX_SC_R_A35) || (rsrc_id == IMX_SC_R_A53) ||
  505. (rsrc_id == IMX_SC_R_A72))
  506. return 0;
  507. if (clk->parent) {
  508. ret = clk_scu_set_parent(&clk->hw, clk->parent_index);
  509. dev_dbg(dev, "restore parent %s idx %u %s\n",
  510. clk_hw_get_name(clk->parent),
  511. clk->parent_index, !ret ? "success" : "failed");
  512. }
  513. if (clk->rate) {
  514. ret = clk_scu_set_rate(&clk->hw, clk->rate, 0);
  515. dev_dbg(dev, "restore rate %d %s\n", clk->rate,
  516. !ret ? "success" : "failed");
  517. }
  518. if (clk->is_enabled && rsrc_id != IMX_SC_R_PI_0_PLL) {
  519. ret = clk_scu_prepare(&clk->hw);
  520. dev_dbg(dev, "restore enabled state %s\n",
  521. !ret ? "success" : "failed");
  522. }
  523. return ret;
  524. }
  525. static const struct dev_pm_ops imx_clk_scu_pm_ops = {
  526. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_clk_scu_suspend,
  527. imx_clk_scu_resume)
  528. };
  529. static struct platform_driver imx_clk_scu_driver = {
  530. .driver = {
  531. .name = "imx-scu-clk",
  532. .suppress_bind_attrs = true,
  533. .pm = &imx_clk_scu_pm_ops,
  534. },
  535. .probe = imx_clk_scu_probe,
  536. };
  537. static int imx_clk_scu_attach_pd(struct device *dev, u32 rsrc_id)
  538. {
  539. struct of_phandle_args genpdspec = {
  540. .np = pd_np,
  541. .args_count = 1,
  542. .args[0] = rsrc_id,
  543. };
  544. if (rsrc_id == IMX_SC_R_A35 || rsrc_id == IMX_SC_R_A53 ||
  545. rsrc_id == IMX_SC_R_A72)
  546. return 0;
  547. return of_genpd_add_device(&genpdspec, dev);
  548. }
  549. struct clk_hw *imx_clk_scu_alloc_dev(const char *name,
  550. const char * const *parents,
  551. int num_parents, u32 rsrc_id, u8 clk_type)
  552. {
  553. struct imx_scu_clk_node clk = {
  554. .name = name,
  555. .rsrc = rsrc_id,
  556. .clk_type = clk_type,
  557. .parents = parents,
  558. .num_parents = num_parents,
  559. };
  560. struct platform_device *pdev;
  561. int ret;
  562. if (!imx_scu_clk_is_valid(rsrc_id))
  563. return ERR_PTR(-EINVAL);
  564. pdev = platform_device_alloc(name, PLATFORM_DEVID_NONE);
  565. if (!pdev) {
  566. pr_err("%s: failed to allocate scu clk dev rsrc %d type %d\n",
  567. name, rsrc_id, clk_type);
  568. return ERR_PTR(-ENOMEM);
  569. }
  570. ret = platform_device_add_data(pdev, &clk, sizeof(clk));
  571. if (ret) {
  572. platform_device_put(pdev);
  573. return ERR_PTR(ret);
  574. }
  575. ret = driver_set_override(&pdev->dev, &pdev->driver_override,
  576. "imx-scu-clk", strlen("imx-scu-clk"));
  577. if (ret) {
  578. platform_device_put(pdev);
  579. return ERR_PTR(ret);
  580. }
  581. ret = imx_clk_scu_attach_pd(&pdev->dev, rsrc_id);
  582. if (ret)
  583. pr_warn("%s: failed to attached the power domain %d\n",
  584. name, ret);
  585. ret = platform_device_add(pdev);
  586. if (ret) {
  587. platform_device_put(pdev);
  588. return ERR_PTR(ret);
  589. }
  590. /* For API backwards compatiblilty, simply return NULL for success */
  591. return NULL;
  592. }
  593. void imx_clk_scu_unregister(void)
  594. {
  595. struct imx_scu_clk_node *clk, *n;
  596. int i;
  597. for (i = 0; i < IMX_SC_R_LAST; i++) {
  598. list_for_each_entry_safe(clk, n, &imx_scu_clks[i], node) {
  599. clk_hw_unregister(clk->hw);
  600. kfree(clk);
  601. }
  602. }
  603. }
  604. static unsigned long clk_gpr_div_scu_recalc_rate(struct clk_hw *hw,
  605. unsigned long parent_rate)
  606. {
  607. struct clk_gpr_scu *clk = to_clk_gpr_scu(hw);
  608. unsigned long rate = 0;
  609. u32 val;
  610. int err;
  611. err = imx_sc_misc_get_control(ccm_ipc_handle, clk->rsrc_id,
  612. clk->gpr_id, &val);
  613. rate = val ? parent_rate / 2 : parent_rate;
  614. return err ? 0 : rate;
  615. }
  616. static long clk_gpr_div_scu_round_rate(struct clk_hw *hw, unsigned long rate,
  617. unsigned long *prate)
  618. {
  619. if (rate < *prate)
  620. rate = *prate / 2;
  621. else
  622. rate = *prate;
  623. return rate;
  624. }
  625. static int clk_gpr_div_scu_set_rate(struct clk_hw *hw, unsigned long rate,
  626. unsigned long parent_rate)
  627. {
  628. struct clk_gpr_scu *clk = to_clk_gpr_scu(hw);
  629. uint32_t val;
  630. int err;
  631. val = (rate < parent_rate) ? 1 : 0;
  632. err = imx_sc_misc_set_control(ccm_ipc_handle, clk->rsrc_id,
  633. clk->gpr_id, val);
  634. return err ? -EINVAL : 0;
  635. }
  636. static const struct clk_ops clk_gpr_div_scu_ops = {
  637. .recalc_rate = clk_gpr_div_scu_recalc_rate,
  638. .round_rate = clk_gpr_div_scu_round_rate,
  639. .set_rate = clk_gpr_div_scu_set_rate,
  640. };
  641. static u8 clk_gpr_mux_scu_get_parent(struct clk_hw *hw)
  642. {
  643. struct clk_gpr_scu *clk = to_clk_gpr_scu(hw);
  644. u32 val = 0;
  645. imx_sc_misc_get_control(ccm_ipc_handle, clk->rsrc_id,
  646. clk->gpr_id, &val);
  647. return (u8)val;
  648. }
  649. static int clk_gpr_mux_scu_set_parent(struct clk_hw *hw, u8 index)
  650. {
  651. struct clk_gpr_scu *clk = to_clk_gpr_scu(hw);
  652. return imx_sc_misc_set_control(ccm_ipc_handle, clk->rsrc_id,
  653. clk->gpr_id, index);
  654. }
  655. static const struct clk_ops clk_gpr_mux_scu_ops = {
  656. .get_parent = clk_gpr_mux_scu_get_parent,
  657. .set_parent = clk_gpr_mux_scu_set_parent,
  658. };
  659. static int clk_gpr_gate_scu_prepare(struct clk_hw *hw)
  660. {
  661. struct clk_gpr_scu *clk = to_clk_gpr_scu(hw);
  662. return imx_sc_misc_set_control(ccm_ipc_handle, clk->rsrc_id,
  663. clk->gpr_id, !clk->gate_invert);
  664. }
  665. static void clk_gpr_gate_scu_unprepare(struct clk_hw *hw)
  666. {
  667. struct clk_gpr_scu *clk = to_clk_gpr_scu(hw);
  668. int ret;
  669. ret = imx_sc_misc_set_control(ccm_ipc_handle, clk->rsrc_id,
  670. clk->gpr_id, clk->gate_invert);
  671. if (ret)
  672. pr_err("%s: clk unprepare failed %d\n", clk_hw_get_name(hw),
  673. ret);
  674. }
  675. static int clk_gpr_gate_scu_is_prepared(struct clk_hw *hw)
  676. {
  677. struct clk_gpr_scu *clk = to_clk_gpr_scu(hw);
  678. int ret;
  679. u32 val;
  680. ret = imx_sc_misc_get_control(ccm_ipc_handle, clk->rsrc_id,
  681. clk->gpr_id, &val);
  682. if (ret)
  683. return ret;
  684. return clk->gate_invert ? !val : val;
  685. }
  686. static const struct clk_ops clk_gpr_gate_scu_ops = {
  687. .prepare = clk_gpr_gate_scu_prepare,
  688. .unprepare = clk_gpr_gate_scu_unprepare,
  689. .is_prepared = clk_gpr_gate_scu_is_prepared,
  690. };
  691. struct clk_hw *__imx_clk_gpr_scu(const char *name, const char * const *parent_name,
  692. int num_parents, u32 rsrc_id, u8 gpr_id, u8 flags,
  693. bool invert)
  694. {
  695. struct imx_scu_clk_node *clk_node;
  696. struct clk_gpr_scu *clk;
  697. struct clk_hw *hw;
  698. struct clk_init_data init;
  699. int ret;
  700. if (rsrc_id >= IMX_SC_R_LAST || gpr_id >= IMX_SC_C_LAST)
  701. return ERR_PTR(-EINVAL);
  702. clk_node = kzalloc(sizeof(*clk_node), GFP_KERNEL);
  703. if (!clk_node)
  704. return ERR_PTR(-ENOMEM);
  705. if (!imx_scu_clk_is_valid(rsrc_id)) {
  706. kfree(clk_node);
  707. return ERR_PTR(-EINVAL);
  708. }
  709. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  710. if (!clk) {
  711. kfree(clk_node);
  712. return ERR_PTR(-ENOMEM);
  713. }
  714. clk->rsrc_id = rsrc_id;
  715. clk->gpr_id = gpr_id;
  716. clk->flags = flags;
  717. clk->gate_invert = invert;
  718. if (flags & IMX_SCU_GPR_CLK_GATE)
  719. init.ops = &clk_gpr_gate_scu_ops;
  720. if (flags & IMX_SCU_GPR_CLK_DIV)
  721. init.ops = &clk_gpr_div_scu_ops;
  722. if (flags & IMX_SCU_GPR_CLK_MUX)
  723. init.ops = &clk_gpr_mux_scu_ops;
  724. init.flags = 0;
  725. init.name = name;
  726. init.parent_names = parent_name;
  727. init.num_parents = num_parents;
  728. clk->hw.init = &init;
  729. hw = &clk->hw;
  730. ret = clk_hw_register(NULL, hw);
  731. if (ret) {
  732. kfree(clk);
  733. kfree(clk_node);
  734. hw = ERR_PTR(ret);
  735. } else {
  736. clk_node->hw = hw;
  737. clk_node->clk_type = gpr_id;
  738. list_add_tail(&clk_node->node, &imx_scu_clks[rsrc_id]);
  739. }
  740. return hw;
  741. }