clk.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-26 GuEe-GUI first version
  9. * 2025-01-24 wumingzi add doxygen comment
  10. * 2024-05-01 GuEe-GUI make cell for hareware clock
  11. */
  12. #include <rtthread.h>
  13. #include <rtservice.h>
  14. #include <rtdevice.h>
  15. /**
  16. * @addtogroup group_driver_clock
  17. * @{
  18. */
  19. #define DBG_TAG "rtdm.clk"
  20. #define DBG_LVL DBG_INFO
  21. #include <rtdbg.h>
  22. static rt_bool_t clk_ignore_unused = RT_FALSE;
  23. static struct rt_mutex _clk_lock;
  24. static rt_list_t _clk_node_nodes = RT_LIST_OBJECT_INIT(_clk_node_nodes);
  25. static rt_list_t _clk_notifier_nodes = RT_LIST_OBJECT_INIT(_clk_notifier_nodes);
  26. static int clk_init(void)
  27. {
  28. #ifdef RT_USING_OFW
  29. clk_ignore_unused = !!rt_ofw_bootargs_select("clk_ignore_unused", 0);
  30. #endif
  31. rt_mutex_init(&_clk_lock, "CLK", RT_IPC_FLAG_PRIO);
  32. return 0;
  33. }
  34. INIT_CORE_EXPORT(clk_init);
  35. /**
  36. * @brief Acquire global clock framework lock.
  37. *
  38. */
  39. static void clk_lock(void)
  40. {
  41. if (rt_thread_self())
  42. {
  43. rt_mutex_take(&_clk_lock, RT_WAITING_FOREVER);
  44. }
  45. }
  46. /**
  47. * @brief Release global clock framework lock.
  48. *
  49. */
  50. static void clk_unlock(void)
  51. {
  52. if (rt_thread_self())
  53. {
  54. rt_mutex_release(&_clk_lock);
  55. }
  56. }
  57. /**
  58. * @brief Allocate memory space for struct clock and return it
  59. *
  60. * @param cell point to clock cell
  61. * @param dev_id device identifier for the clock
  62. * @param con_id connection identifier for the clock
  63. *
  64. * @return struct rt_clk* point to clock
  65. */
  66. static struct rt_clk *clk_alloc(struct rt_clk_cell *cell, const char *dev_id, const char *con_id)
  67. {
  68. struct rt_clk *clk = rt_calloc(1, sizeof(*clk));
  69. if (!clk)
  70. {
  71. LOG_E("%s not memory to create CLK for dev_id = %s con_id = %s",
  72. cell->name, dev_id, con_id);
  73. return RT_NULL;
  74. }
  75. clk->cell = cell;
  76. clk->dev_id = dev_id;
  77. clk->con_id = con_id;
  78. if (cell->clk)
  79. {
  80. clk->min_rate = cell->clk->min_rate;
  81. clk->max_rate = cell->clk->max_rate;
  82. }
  83. else
  84. {
  85. clk->min_rate = 0;
  86. clk->max_rate = ~0UL;
  87. }
  88. return clk;
  89. }
  90. /**
  91. * @brief Update clk cell hardware information
  92. *
  93. * @param cell point to clock cell
  94. * @param clk bind clk
  95. *
  96. */
  97. static void clk_cell_bind(struct rt_clk_cell *cell, struct rt_clk *clk)
  98. {
  99. if (!cell->clk)
  100. {
  101. cell->clk = clk;
  102. }
  103. else
  104. {
  105. if (!cell->clk->dev_id)
  106. {
  107. cell->clk->dev_id = clk->dev_id;
  108. }
  109. if (!cell->clk->con_id)
  110. {
  111. cell->clk->con_id = clk->con_id;
  112. }
  113. }
  114. }
  115. /**
  116. * @brief Register a clock node into the global clock framework.
  117. *
  118. * This function initializes an @ref rt_clk_node structure and inserts it
  119. * into the global clock node list. Each node may contain multiple
  120. * @ref rt_clk_cell instances, which represent the individual output clocks
  121. * provided by the node.
  122. *
  123. * If the node is associated with a device (clk_np->dev is not NULL),
  124. * the framework will automatically try to obtain its parent clocks by calling
  125. * rt_clk_get_array(clk_np->dev). Otherwise, the node will be treated as a
  126. * root-level provider (e.g., fixed clock).
  127. *
  128. * The caller must ensure that all @ref rt_clk_cell entries are allocated
  129. * and linked to the node before calling this function.
  130. *
  131. * @param clk_np Pointer to the clock node to be registered.
  132. *
  133. * @retval RT_EOK Successfully registered.
  134. * @retval -RT_EINVAL Invalid argument or missing cell list.
  135. * @retval -RT_ENOMEM Failed to allocate parent clock array.
  136. */
  137. rt_err_t rt_clk_register(struct rt_clk_node *clk_np)
  138. {
  139. rt_err_t err = RT_EOK;
  140. struct rt_clk_cell *cell;
  141. if (!clk_np || !clk_np->cells_nr || !clk_np->cells)
  142. {
  143. return -RT_EINVAL;
  144. }
  145. if (clk_np->dev && !rt_is_err(clk_np->parents_clk))
  146. {
  147. clk_np->parents_clk = rt_clk_get_array(clk_np->dev);
  148. if (rt_is_err(clk_np->parents_clk))
  149. {
  150. return rt_ptr_err(clk_np->parents_clk);
  151. }
  152. }
  153. #if RT_NAME_MAX > 0
  154. rt_strncpy(clk_np->parent.name, RT_CLK_NODE_OBJ_NAME, RT_NAME_MAX);
  155. #else
  156. clk_np->parent.name = RT_CLK_NODE_OBJ_NAME;
  157. #endif
  158. rt_list_init(&clk_np->parent.list);
  159. for (int i = 0; i < clk_np->cells_nr; ++i)
  160. {
  161. cell = clk_np->cells[i];
  162. if (!cell)
  163. {
  164. continue;
  165. }
  166. cell->clk_np = clk_np;
  167. cell->rate = 0;
  168. cell->prepare_count = 0;
  169. cell->enable_count = 0;
  170. }
  171. clk_lock();
  172. rt_list_insert_after(&_clk_node_nodes, &clk_np->parent.list);
  173. clk_unlock();
  174. #ifdef RT_USING_OFW
  175. if (clk_np->dev && clk_np->dev->ofw_node)
  176. {
  177. rt_bool_t set_ofw_data = RT_FALSE;
  178. struct rt_ofw_node *np = clk_np->dev->ofw_node;
  179. if (!rt_ofw_data(np))
  180. {
  181. set_ofw_data = RT_TRUE;
  182. rt_ofw_data(np) = &clk_np->parent;
  183. }
  184. if ((err = rt_ofw_clk_set_defaults(np)))
  185. {
  186. if (set_ofw_data)
  187. {
  188. rt_ofw_data(np) = RT_NULL;
  189. }
  190. }
  191. }
  192. #endif /* RT_USING_OFW */
  193. if (err)
  194. {
  195. if (!rt_is_err(clk_np->parents_clk))
  196. {
  197. rt_clk_array_put(clk_np->parents_clk);
  198. }
  199. }
  200. return err;
  201. }
  202. /**
  203. * @brief Unregister a clock node from global clock list
  204. *
  205. * This API is intended for rollback use only, typically called
  206. * when a clock provider fails after registration but before any
  207. * consumer starts using its clocks.
  208. *
  209. * It removes the specified clock node from the global list and
  210. * releases its parent clock array if present. The function does
  211. * not free memory of @ref rt_clk_cell instances or the node itself.
  212. *
  213. * @param clk_np Pointer to the clock node to be unregistered.
  214. *
  215. * @retval RT_EOK Successfully unregistered.
  216. * @retval -RT_EINVAL Invalid parameter.
  217. * @retval -RT_EBUSY One or more cells are active and cannot be removed.
  218. */
  219. rt_err_t rt_clk_unregister(struct rt_clk_node *clk_np)
  220. {
  221. struct rt_clk_cell *cell;
  222. if (!clk_np)
  223. {
  224. return -RT_EINVAL;
  225. }
  226. if (clk_np->cells && clk_np->cells_nr)
  227. {
  228. for (int i = 0; i < clk_np->cells_nr; ++i)
  229. {
  230. cell = clk_np->cells[i];
  231. if (cell && cell->enable_count > 0)
  232. {
  233. return -RT_EBUSY;
  234. }
  235. }
  236. }
  237. clk_lock();
  238. rt_list_remove(&clk_np->parent.list);
  239. clk_unlock();
  240. if (!rt_is_err(clk_np->parents_clk))
  241. {
  242. rt_clk_array_put(clk_np->parents_clk);
  243. }
  244. return RT_EOK;
  245. }
  246. /**
  247. * @brief Register clock notifier into notifier list
  248. *
  249. * @param clk point to clock
  250. * @param notifier point to notifier for register
  251. *
  252. * @retval RT_EOK
  253. * @retval -RT_EINVAL
  254. */
  255. rt_err_t rt_clk_notifier_register(struct rt_clk *clk, struct rt_clk_notifier *notifier)
  256. {
  257. if (!clk || !notifier)
  258. {
  259. return -RT_EINVAL;
  260. }
  261. notifier->clk = clk;
  262. rt_list_init(&notifier->list);
  263. clk_lock();
  264. rt_list_insert_after(&_clk_notifier_nodes, &notifier->list);
  265. clk_unlock();
  266. return RT_EOK;
  267. }
  268. /**
  269. * @brief Unregister clock notifier into notifier list
  270. *
  271. * @param clk point to clock
  272. * @param notifier point to notifier for unregister
  273. *
  274. * @retval RT_EOK
  275. * @retval -RT_EINVAL
  276. */
  277. rt_err_t rt_clk_notifier_unregister(struct rt_clk *clk, struct rt_clk_notifier *notifier)
  278. {
  279. struct rt_clk_notifier *notifier_find;
  280. if (!clk || !notifier)
  281. {
  282. return -RT_EINVAL;
  283. }
  284. clk_lock();
  285. rt_list_for_each_entry(notifier_find, &_clk_notifier_nodes, list)
  286. {
  287. if (notifier_find == notifier)
  288. {
  289. rt_list_remove(&notifier->list);
  290. break;
  291. }
  292. }
  293. clk_unlock();
  294. return RT_EOK;
  295. }
  296. /**
  297. * @brief Notify corresponding clock from all
  298. *
  299. * @param clk_np point to clock node
  300. * @param msg message identifier for the event
  301. * @param old_rate old rate of the clock before the event
  302. * @param new_rate new rate of the clock after the event
  303. *
  304. * @return rt_err_t RT_EOK on notify clock sucessfully, and other value is failed.
  305. */
  306. static rt_err_t clk_notify(struct rt_clk_node *clk_np, rt_ubase_t msg,
  307. rt_ubase_t old_rate, rt_ubase_t new_rate)
  308. {
  309. rt_err_t err = RT_EOK;
  310. struct rt_clk_notifier *notifier;
  311. rt_list_for_each_entry(notifier, &_clk_notifier_nodes, list)
  312. {
  313. if (notifier->clk->cell->clk_np == clk_np)
  314. {
  315. err = notifier->callback(notifier, msg, old_rate, new_rate);
  316. /* Only check hareware's error */
  317. if (err == -RT_EIO)
  318. {
  319. break;
  320. }
  321. }
  322. }
  323. return err;
  324. }
  325. static void clk_unprepare(struct rt_clk *clk);
  326. static void clk_disable(struct rt_clk *clk);
  327. static rt_ubase_t clk_round_rate(struct rt_clk *clk, rt_ubase_t rate);
  328. static rt_err_t clk_set_rate(struct rt_clk *clk, rt_ubase_t rate);
  329. static rt_err_t clk_set_parent(struct rt_clk *clk, struct rt_clk *parent);
  330. static struct rt_clk *clk_get_parent(struct rt_clk *clk);
  331. static rt_ubase_t clk_get_rate(struct rt_clk *clk);
  332. static struct rt_clk *clk_cell_get_clk(struct rt_clk_cell *cell);
  333. /**
  334. * @brief Recursively prepare clock
  335. *
  336. * @param clk Ponit to clock that will be prepared
  337. *
  338. * @return rt_err_t RT_EOK on prepare clock sucessfully, and other value is failed.
  339. */
  340. static rt_err_t clk_prepare(struct rt_clk *clk)
  341. {
  342. rt_err_t err;
  343. struct rt_clk *parent;
  344. struct rt_clk_cell *cell;
  345. cell = clk->cell;
  346. /* Already prepared */
  347. if (cell->prepare_count++ > 0)
  348. {
  349. return RT_EOK;
  350. }
  351. parent = clk_get_parent(clk);
  352. if (parent)
  353. {
  354. if ((err = clk_prepare(parent)))
  355. {
  356. goto _fail;
  357. }
  358. }
  359. if (cell->ops->prepare)
  360. {
  361. if ((err = cell->ops->prepare(cell)))
  362. {
  363. if (parent)
  364. {
  365. clk_unprepare(parent);
  366. }
  367. goto _fail;
  368. }
  369. }
  370. return RT_EOK;
  371. _fail:
  372. --cell->prepare_count;
  373. return err;
  374. }
  375. /**
  376. * @brief Prepare clock
  377. *
  378. * @param clk
  379. *
  380. * @return rt_err_t RT_EOK on prepare clock sucessfully, and other value is failed.
  381. */
  382. rt_err_t rt_clk_prepare(struct rt_clk *clk)
  383. {
  384. rt_err_t err;
  385. RT_DEBUG_NOT_IN_INTERRUPT;
  386. if (clk)
  387. {
  388. clk_lock();
  389. err = clk_prepare(clk);
  390. clk_unlock();
  391. }
  392. else
  393. {
  394. err = RT_EOK;
  395. }
  396. return err;
  397. }
  398. /**
  399. * @brief Recursively unprepare clock
  400. *
  401. * @param clk Ponit to clock that will be unprepared
  402. *
  403. */
  404. static void clk_unprepare(struct rt_clk *clk)
  405. {
  406. struct rt_clk *parent;
  407. struct rt_clk_cell *cell;
  408. cell = clk->cell;
  409. /* Don't unprepare readly */
  410. if (cell->prepare_count-- > 1)
  411. {
  412. return;
  413. }
  414. if (cell->ops->unprepare)
  415. {
  416. cell->ops->unprepare(cell);
  417. }
  418. parent = clk_get_parent(clk);
  419. if (parent)
  420. {
  421. clk_unprepare(parent);
  422. }
  423. }
  424. /**
  425. * @brief Unprepare clock
  426. *
  427. * @param clk Ponit to clock that will be unprepared
  428. *
  429. */
  430. void rt_clk_unprepare(struct rt_clk *clk)
  431. {
  432. RT_DEBUG_NOT_IN_INTERRUPT;
  433. if (clk)
  434. {
  435. clk_lock();
  436. clk_unprepare(clk);
  437. clk_unlock();
  438. }
  439. }
  440. /**
  441. * @brief Enable clock
  442. *
  443. * @param clk point to clock
  444. *
  445. * @return rt_err_t RT_EOK on enable clock FOREVER.
  446. */
  447. static rt_err_t clk_enable(struct rt_clk *clk)
  448. {
  449. rt_err_t err;
  450. struct rt_clk *parent;
  451. struct rt_clk_cell *cell;
  452. cell = clk->cell;
  453. /* Already enabled */
  454. if (cell->enable_count++ > 0)
  455. {
  456. return RT_EOK;
  457. }
  458. parent = clk_get_parent(clk);
  459. if (parent)
  460. {
  461. if ((err = clk_enable(parent)))
  462. {
  463. goto _fail;
  464. }
  465. }
  466. if (cell->ops->enable)
  467. {
  468. if ((err = cell->ops->enable(cell)))
  469. {
  470. if (parent)
  471. {
  472. clk_disable(parent);
  473. }
  474. goto _fail;
  475. }
  476. }
  477. return RT_EOK;
  478. _fail:
  479. --cell->enable_count;
  480. return err;
  481. }
  482. /**
  483. * @brief Enable clock
  484. *
  485. * @param clk point to clock
  486. *
  487. * @return rt_err_t RT_EOK on enable clock sucessfully, and other value is failed.
  488. */
  489. rt_err_t rt_clk_enable(struct rt_clk *clk)
  490. {
  491. rt_err_t err;
  492. if (clk)
  493. {
  494. clk_lock();
  495. err = clk_enable(clk);
  496. clk_unlock();
  497. }
  498. else
  499. {
  500. err = RT_EOK;
  501. }
  502. return err;
  503. }
  504. /**
  505. * @brief Recursively disable clock
  506. *
  507. * @param clk Ponit to clock that will be disabled
  508. *
  509. */
  510. static void clk_disable(struct rt_clk *clk)
  511. {
  512. struct rt_clk *parent;
  513. struct rt_clk_cell *cell;
  514. cell = clk->cell;
  515. if (cell->enable_count == 0)
  516. {
  517. LOG_W("%s: Disable called with count = 0", cell->name);
  518. return;
  519. }
  520. if (cell->enable_count-- > 1)
  521. {
  522. return;
  523. }
  524. if (cell->flags & RT_CLK_F_IS_CRITICAL)
  525. {
  526. return;
  527. }
  528. if (clk_ignore_unused && cell->flags & RT_CLK_F_IGNORE_UNUSED)
  529. {
  530. return;
  531. }
  532. if (cell->ops->disable)
  533. {
  534. cell->ops->disable(cell);
  535. }
  536. parent = clk_get_parent(clk);
  537. if (parent)
  538. {
  539. clk_disable(parent);
  540. }
  541. }
  542. /**
  543. * @brief Disable clock
  544. *
  545. * @param clk point to clock
  546. *
  547. */
  548. void rt_clk_disable(struct rt_clk *clk)
  549. {
  550. if (clk)
  551. {
  552. clk_lock();
  553. clk_disable(clk);
  554. clk_unlock();
  555. }
  556. }
  557. /**
  558. * @brief Prepare and enable clock
  559. *
  560. * @param clk point to clock
  561. *
  562. * @return rt_err_t RT_EOK on prepare and enable clock sucessfully, and other value is failed.
  563. */
  564. rt_err_t rt_clk_prepare_enable(struct rt_clk *clk)
  565. {
  566. rt_err_t err;
  567. RT_DEBUG_NOT_IN_INTERRUPT;
  568. if (clk)
  569. {
  570. clk_lock();
  571. if (!(err = clk_prepare(clk)))
  572. {
  573. if ((err = clk_enable(clk)))
  574. {
  575. clk_unprepare(clk);
  576. }
  577. }
  578. clk_unlock();
  579. }
  580. else
  581. {
  582. err = RT_EOK;
  583. }
  584. return err;
  585. }
  586. /**
  587. * @brief Disable and unprepare clock
  588. *
  589. * @param clk point to clock
  590. *
  591. */
  592. void rt_clk_disable_unprepare(struct rt_clk *clk)
  593. {
  594. RT_DEBUG_NOT_IN_INTERRUPT;
  595. if (clk)
  596. {
  597. clk_lock();
  598. clk_disable(clk);
  599. clk_unprepare(clk);
  600. clk_unlock();
  601. }
  602. }
  603. /**
  604. * @brief Prepare clock array for mutipule out clock
  605. *
  606. * @param clk_arr point to clock array
  607. *
  608. * @return rt_err_t RT_EOK on prepare clock array sucessfully, and other value is failed.
  609. */
  610. rt_err_t rt_clk_array_prepare(struct rt_clk_array *clk_arr)
  611. {
  612. rt_err_t err;
  613. if (clk_arr)
  614. {
  615. for (int i = 0; i < clk_arr->count; ++i)
  616. {
  617. if ((err = rt_clk_prepare(clk_arr->clks[i])))
  618. {
  619. LOG_E("CLK Array[%d] %s failed error = %s", i,
  620. "prepare", rt_strerror(err));
  621. while (i --> 0)
  622. {
  623. rt_clk_unprepare(clk_arr->clks[i]);
  624. }
  625. break;
  626. }
  627. }
  628. }
  629. else
  630. {
  631. err = RT_EOK;
  632. }
  633. return err;
  634. }
  635. /**
  636. * @brief Unprepare clock array for mutipule out clock
  637. *
  638. * @param clk_arr point to clock array
  639. *
  640. */
  641. void rt_clk_array_unprepare(struct rt_clk_array *clk_arr)
  642. {
  643. if (clk_arr)
  644. {
  645. for (int i = 0; i < clk_arr->count; ++i)
  646. {
  647. rt_clk_unprepare(clk_arr->clks[i]);
  648. }
  649. }
  650. }
  651. /**
  652. * @brief Enable clock array for mutipule out clock
  653. *
  654. * @param clk_arr point to clock array
  655. *
  656. * @return rt_err_t RT_EOK on Enable clock array sucessfully, and other value is failed.
  657. */
  658. rt_err_t rt_clk_array_enable(struct rt_clk_array *clk_arr)
  659. {
  660. rt_err_t err;
  661. if (clk_arr)
  662. {
  663. for (int i = 0; i < clk_arr->count; ++i)
  664. {
  665. if ((err = rt_clk_enable(clk_arr->clks[i])))
  666. {
  667. LOG_E("CLK Array[%d] %s failed error = %s", i,
  668. "enable", rt_strerror(err));
  669. while (i --> 0)
  670. {
  671. rt_clk_disable(clk_arr->clks[i]);
  672. }
  673. break;
  674. }
  675. }
  676. }
  677. else
  678. {
  679. err = RT_EOK;
  680. }
  681. return err;
  682. }
  683. /**
  684. * @brief Enable clock array for mutipule out clock
  685. *
  686. * @param clk_arr point to clock array
  687. *
  688. */
  689. void rt_clk_array_disable(struct rt_clk_array *clk_arr)
  690. {
  691. if (clk_arr)
  692. {
  693. for (int i = 0; i < clk_arr->count; ++i)
  694. {
  695. rt_clk_disable(clk_arr->clks[i]);
  696. }
  697. }
  698. }
  699. /**
  700. * @brief Prepare and enable clock array
  701. *
  702. * @param clk_arr point to clock array
  703. *
  704. * @return rt_err_t RT_EOK on prepare and enable clock array sucessfully, and other
  705. value is failed.
  706. */
  707. rt_err_t rt_clk_array_prepare_enable(struct rt_clk_array *clk_arr)
  708. {
  709. rt_err_t err;
  710. if ((err = rt_clk_array_prepare(clk_arr)))
  711. {
  712. return err;
  713. }
  714. if ((err = rt_clk_array_enable(clk_arr)))
  715. {
  716. rt_clk_array_unprepare(clk_arr);
  717. }
  718. return err;
  719. }
  720. /**
  721. * @brief Disable and unprepare clock array
  722. *
  723. * @param clk_arr point to clock array
  724. *
  725. */
  726. void rt_clk_array_disable_unprepare(struct rt_clk_array *clk_arr)
  727. {
  728. rt_clk_array_disable(clk_arr);
  729. rt_clk_array_unprepare(clk_arr);
  730. }
  731. /**
  732. * @brief Set and clamp clock rate within specified range.
  733. *
  734. * @details This function updates the minimum and maximum allowed rate
  735. * of a clock, clamps its current rate to the new range, and
  736. * immediately applies the change via set_rate().
  737. *
  738. * @param clk Pointer to clock handle.
  739. * @param min Minimum allowed rate (Hz).
  740. * @param max Maximum allowed rate (Hz).
  741. *
  742. * @retval RT_EOK Successfully updated.
  743. * @retval -RT_EINVAL Invalid parameter or range.
  744. * @retval -RT_ENOSYS Clock driver does not support set_rate().
  745. * @retval other Hardware-specific error returned by set_rate().
  746. */
  747. static rt_err_t clk_set_rate_range(struct rt_clk *clk, rt_ubase_t min, rt_ubase_t max)
  748. {
  749. rt_err_t err;
  750. rt_ubase_t rate, old_min, old_max;
  751. struct rt_clk_cell *cell;
  752. if (min > max)
  753. {
  754. return -RT_EINVAL;
  755. }
  756. cell = clk->cell;
  757. old_min = clk->min_rate;
  758. old_max = clk->max_rate;
  759. clk->min_rate = min;
  760. clk->max_rate = max;
  761. if (cell->flags & RT_CLK_F_GET_RATE_NOCACHE)
  762. {
  763. rate = clk_get_rate(clk);
  764. }
  765. else
  766. {
  767. rate = cell->rate;
  768. }
  769. if ((err = clk_set_rate(clk, rt_clamp(rate, min, max))))
  770. {
  771. goto _fail;
  772. }
  773. return RT_EOK;
  774. _fail:
  775. clk->min_rate = old_min;
  776. clk->max_rate = old_max;
  777. return err;
  778. }
  779. /**
  780. * @brief Set clock rate range
  781. *
  782. * @param clk point to clock
  783. * @param min minimum clock rate
  784. * @param max minimum clock rate
  785. *
  786. * @return rt_err_t RT_EOK on set clock rate range sucessfully, and other value is failed.
  787. */
  788. rt_err_t rt_clk_set_rate_range(struct rt_clk *clk, rt_ubase_t min, rt_ubase_t max)
  789. {
  790. rt_err_t err;
  791. if (!clk)
  792. {
  793. return RT_EOK;
  794. }
  795. clk_lock();
  796. err = clk_set_rate_range(clk, min, max);
  797. clk_unlock();
  798. return err;
  799. }
  800. /**
  801. * @brief Set minimum clock rate
  802. *
  803. * @param clk point to clock
  804. * @param rate miminum clock rate
  805. *
  806. * @return rt_err_t RT_EOK on set minimum clock rate sucessfully, and other value is failed.
  807. */
  808. rt_err_t rt_clk_set_min_rate(struct rt_clk *clk, rt_ubase_t rate)
  809. {
  810. if (clk)
  811. {
  812. return rt_clk_set_rate_range(clk, rate, clk->max_rate);
  813. }
  814. return RT_EOK;
  815. }
  816. /**
  817. * @brief Set maximum clock rate
  818. *
  819. * @param clk point to clock
  820. * @param rate maximum clock rate
  821. *
  822. * @return rt_err_t RT_EOK on set maximum clock rate sucessfully, and other value is failed.
  823. */
  824. rt_err_t rt_clk_set_max_rate(struct rt_clk *clk, rt_ubase_t rate)
  825. {
  826. if (clk)
  827. {
  828. return rt_clk_set_rate_range(clk, clk->min_rate, rate);
  829. }
  830. return RT_EOK;
  831. }
  832. /**
  833. * @brief Set clock rate.
  834. *
  835. * @details This function directly sets the frequency of the given clock.
  836. * If the hardware driver supports set_rate(), the new rate will
  837. * be applied immediately and the cached rate will be updated.
  838. *
  839. * @param clk Pointer to clock handle.
  840. * @param rate Target frequency (Hz).
  841. *
  842. * @retval RT_EOK Successfully updated.
  843. * @retval -RT_EINVAL Invalid parameter.
  844. * @retval -RT_ENOSYS Clock driver does not support set_rate().
  845. * @retval other Hardware-specific error returned by set_rate().
  846. */
  847. static rt_err_t clk_set_rate(struct rt_clk *clk, rt_ubase_t rate)
  848. {
  849. rt_err_t err;
  850. rt_ubase_t old_rate, prate;
  851. rt_bool_t was_enabled = RT_FALSE;
  852. rt_bool_t was_disabled = RT_FALSE;
  853. struct rt_clk *parent = RT_NULL;
  854. struct rt_clk_node *clk_np;
  855. struct rt_clk_cell *cell;
  856. cell = clk->cell;
  857. if (!cell->ops->set_rate)
  858. {
  859. return -RT_ENOSYS;
  860. }
  861. clk_np = cell->clk_np;
  862. if (cell->flags & RT_CLK_F_GET_RATE_NOCACHE)
  863. {
  864. old_rate = clk_get_rate(clk);
  865. }
  866. else
  867. {
  868. old_rate = cell->rate;
  869. }
  870. rate = rt_clamp(rate, clk->min_rate, clk->max_rate);
  871. parent = clk_get_parent(clk);
  872. if (cell->parents_nr > 1)
  873. {
  874. rt_uint8_t best_idx = RT_UINT8_MAX;
  875. rt_ubase_t best_rounded = 0, best_diff = ~0UL;
  876. struct rt_clk_cell *parent_cell, *best_parent_cell = RT_NULL;
  877. for (rt_uint8_t idx = 0; idx < cell->parents_nr; ++idx)
  878. {
  879. rt_ubase_t rounded, diff;
  880. if (!(parent_cell = rt_clk_cell_get_parent_by_index(cell, idx)))
  881. {
  882. continue;
  883. }
  884. if (!parent_cell->clk && !(parent_cell->clk = clk_cell_get_clk(parent_cell)))
  885. {
  886. return RT_NULL;
  887. }
  888. prate = clk_get_rate(parent_cell->clk);
  889. rounded = clk_round_rate(parent_cell->clk, rate);
  890. rounded = (rounded > 0) ? rounded : rate;
  891. diff = rt_abs(rounded - rate);
  892. if (diff < best_diff)
  893. {
  894. best_idx = idx;
  895. best_diff = diff;
  896. best_rounded = rounded;
  897. best_parent_cell = parent_cell;
  898. }
  899. }
  900. if (best_idx != RT_UINT8_MAX && parent->cell != best_parent_cell)
  901. {
  902. parent = best_parent_cell->clk;
  903. if ((err = clk_set_parent(clk, parent)))
  904. {
  905. return err;
  906. }
  907. rate = best_rounded;
  908. }
  909. }
  910. if (parent)
  911. {
  912. if (cell->flags & RT_CLK_F_SET_RATE_PARENT)
  913. {
  914. if ((err = clk_set_rate(parent, rate)))
  915. {
  916. return err;
  917. }
  918. }
  919. prate = clk_get_rate(parent);
  920. }
  921. else
  922. {
  923. prate = 0;
  924. }
  925. if ((cell->flags & RT_CLK_F_SET_RATE_GATE) && cell->enable_count > 0)
  926. {
  927. was_enabled = RT_TRUE;
  928. clk_disable(clk);
  929. }
  930. else if ((cell->flags & RT_CLK_F_SET_RATE_UNGATE) && cell->enable_count == 0)
  931. {
  932. was_disabled = RT_TRUE;
  933. clk_enable(clk);
  934. }
  935. clk_notify(clk_np, RT_CLK_MSG_PRE_RATE_CHANGE, old_rate, rate);
  936. if ((err = cell->ops->set_rate(cell, rate, prate)))
  937. {
  938. clk_notify(clk_np, RT_CLK_MSG_ABORT_RATE_CHANGE, old_rate, rate);
  939. goto _end;
  940. }
  941. /* Update cached rate */
  942. cell->rate = rate;
  943. clk_notify(clk_np, RT_CLK_MSG_POST_RATE_CHANGE, old_rate, rate);
  944. _end:
  945. if (was_enabled)
  946. {
  947. clk_enable(clk);
  948. }
  949. else if (was_disabled)
  950. {
  951. clk_disable(clk);
  952. }
  953. return err;
  954. }
  955. /**
  956. * @brief Set clock rate
  957. *
  958. * @param clk point to clock
  959. * @param rate target rate
  960. *
  961. * @return rt_err_t RT_EOK on set clock rate sucessfully, and other value is failed.
  962. */
  963. rt_err_t rt_clk_set_rate(struct rt_clk *clk, rt_ubase_t rate)
  964. {
  965. rt_err_t err;
  966. if (clk)
  967. {
  968. clk_lock();
  969. err = clk_set_rate(clk, rate);
  970. clk_unlock();
  971. }
  972. else
  973. {
  974. err = RT_EOK;
  975. }
  976. return err;
  977. }
  978. /**
  979. * @brief Internal helper to get clock rate (no locking, no validation).
  980. *
  981. * @param clk Pointer to clock handle.
  982. *
  983. * @return Clock frequency in Hz, or 0 if invalid.
  984. */
  985. static rt_ubase_t clk_get_rate(struct rt_clk *clk)
  986. {
  987. rt_ubase_t prate;
  988. struct rt_clk *parent;
  989. struct rt_clk_cell *cell;
  990. cell = clk->cell;
  991. parent = clk_get_parent(clk);
  992. prate = parent ? clk_get_rate(parent) : 0;
  993. if (cell->ops->recalc_rate)
  994. {
  995. cell->rate = cell->ops->recalc_rate(cell, prate);
  996. }
  997. else
  998. {
  999. cell->rate = prate;
  1000. }
  1001. return cell->rate;
  1002. }
  1003. /**
  1004. * @brief Get clock rate
  1005. *
  1006. * @param clk point to clock
  1007. *
  1008. * @return rt_ubase_t clock rate or error code
  1009. */
  1010. rt_ubase_t rt_clk_get_rate(struct rt_clk *clk)
  1011. {
  1012. rt_ubase_t rate;
  1013. if (clk)
  1014. {
  1015. clk_lock();
  1016. rate = clk_get_rate(clk);
  1017. clk_unlock();
  1018. }
  1019. else
  1020. {
  1021. rate = 0;
  1022. }
  1023. return rate;
  1024. }
  1025. /**
  1026. * @brief Internal helper to round clock rate (no locking).
  1027. *
  1028. * @param clk Pointer to clock handle.
  1029. * @param rate Desired frequency in Hz.
  1030. *
  1031. * @return Rounded frequency in Hz (may differ from requested value).
  1032. */
  1033. static rt_ubase_t clk_round_rate(struct rt_clk *clk, rt_ubase_t rate)
  1034. {
  1035. rt_ubase_t prate, rounded = rate;
  1036. struct rt_clk *parent;
  1037. struct rt_clk_cell *cell;
  1038. cell = clk->cell;
  1039. parent = clk_get_parent(clk);
  1040. prate = parent ? clk_get_rate(parent) : 0;
  1041. /* If driver provides round_rate() callback, use it */
  1042. if (cell->ops->round_rate)
  1043. {
  1044. rt_base_t res = cell->ops->round_rate(cell, rate, &prate);
  1045. if (res > 0)
  1046. {
  1047. rounded = res;
  1048. }
  1049. }
  1050. else if ((cell->flags & RT_CLK_F_SET_RATE_PARENT) && parent)
  1051. {
  1052. /* Delegate rounding to parent clock if supported */
  1053. rounded = clk_round_rate(parent, rate);
  1054. }
  1055. /* Clamp to valid range */
  1056. return rt_clamp(rounded, clk->min_rate, clk->max_rate);
  1057. }
  1058. /**
  1059. * @brief Check if clock rate is in the minimum to maximun and get it
  1060. *
  1061. * @param clk point to clock
  1062. * @param rate rate will be checked
  1063. *
  1064. * @return rt_base_t get the correct rate
  1065. * @note if parameter rate less than the minimum or more than maximum, the
  1066. retrun rate will be set to minimum ormaximum value
  1067. */
  1068. rt_base_t rt_clk_round_rate(struct rt_clk *clk, rt_ubase_t rate)
  1069. {
  1070. rt_ubase_t rounded = 0;
  1071. if (clk)
  1072. {
  1073. clk_lock();
  1074. rounded = clk_round_rate(clk, rate);
  1075. clk_unlock();
  1076. }
  1077. return rounded;
  1078. }
  1079. /**
  1080. * @brief Set parent clock
  1081. *
  1082. * @param clk_np point to clock node
  1083. * @param parent_np point to parent rt_clk
  1084. *
  1085. * @return rt_err_t RT_EOK on set clock parent sucessfully, and other value is failed.
  1086. */
  1087. static rt_err_t clk_set_parent(struct rt_clk *clk, struct rt_clk *parent)
  1088. {
  1089. rt_err_t err;
  1090. rt_uint8_t idx = RT_UINT8_MAX;
  1091. rt_bool_t was_enabled = RT_FALSE;
  1092. struct rt_clk_cell *cell;
  1093. cell = clk->cell;
  1094. /* Already same parent? */
  1095. if (parent)
  1096. {
  1097. if (cell->parent == parent->cell->clk)
  1098. {
  1099. return RT_EOK;
  1100. }
  1101. }
  1102. else if (!cell->parent)
  1103. {
  1104. return RT_EOK;
  1105. }
  1106. /* No multi-parent */
  1107. if (cell->parents_nr <= 1)
  1108. {
  1109. return -RT_EINVAL;
  1110. }
  1111. /* Multi-parent but driver lacks support */
  1112. if (!cell->ops->set_parent)
  1113. {
  1114. return -RT_EINVAL;
  1115. }
  1116. /* Find new parent index if provided */
  1117. if (parent)
  1118. {
  1119. const char *pname = parent->cell->name;
  1120. /* Temporarily gate if required */
  1121. if ((cell->flags & RT_CLK_F_SET_PARENT_GATE) && cell->enable_count > 0)
  1122. {
  1123. was_enabled = RT_TRUE;
  1124. clk_disable(clk);
  1125. }
  1126. for (int i = 0; i < cell->parents_nr; ++i)
  1127. {
  1128. if (!rt_strcmp(cell->parent_names[i], pname))
  1129. {
  1130. idx = i;
  1131. break;
  1132. }
  1133. }
  1134. if (idx == RT_UINT8_MAX)
  1135. {
  1136. LOG_W("%s: Invalid parent %s", cell->name, pname);
  1137. err = -RT_EINVAL;
  1138. goto _end;
  1139. }
  1140. if (cell->ops->set_parent)
  1141. {
  1142. if (!(err = cell->ops->set_parent(cell, idx)))
  1143. {
  1144. cell->parent = parent->cell->clk;
  1145. }
  1146. }
  1147. else
  1148. {
  1149. err = -RT_ENOSYS;
  1150. }
  1151. _end:
  1152. if (was_enabled)
  1153. {
  1154. clk_enable(clk);
  1155. }
  1156. }
  1157. else
  1158. {
  1159. err = RT_EOK;
  1160. }
  1161. return err;
  1162. }
  1163. /**
  1164. * @brief Set clock parent object
  1165. *
  1166. * @param clk point to clock
  1167. * @param clk_parent point to parent clock
  1168. *
  1169. * @return rt_err_t RT_EOK on set clock parent sucessfully, and other value is failed.
  1170. */
  1171. rt_err_t rt_clk_set_parent(struct rt_clk *clk, struct rt_clk *clk_parent)
  1172. {
  1173. rt_err_t err;
  1174. if (clk)
  1175. {
  1176. clk_lock();
  1177. err = clk_set_parent(clk, clk_parent);
  1178. clk_unlock();
  1179. }
  1180. else
  1181. {
  1182. err = RT_EOK;
  1183. }
  1184. return err;
  1185. }
  1186. /**
  1187. * @brief Resolve and return the parent clock of a given clock handle.
  1188. *
  1189. * @details This function determines the parent clock for the provided
  1190. * clock handle (`clk`). It first checks for an existing cached
  1191. * parent, and if none exists:
  1192. * 1. Calls the driver's `get_parent()` callback to retrieve
  1193. * the parent index.
  1194. * 2. Looks up the corresponding parent clock by name from:
  1195. * - The controller's `parents_clk` array, or
  1196. * - The controller's own `cells` array.
  1197. * 3. If a matching cell exists but no rt_clk handle yet, a new
  1198. * handle is allocated via `clk_alloc()`.
  1199. * 4. The resolved parent is cached in `cell->parent`.
  1200. *
  1201. * @param clk Pointer to the clock handle.
  1202. *
  1203. * @return Pointer to the parent clock handle, or NULL on failure.
  1204. */
  1205. static struct rt_clk *clk_get_parent(struct rt_clk *clk)
  1206. {
  1207. rt_uint8_t idx;
  1208. struct rt_clk *parent;
  1209. struct rt_clk_cell *cell, *parent_cell;
  1210. cell = clk->cell;
  1211. if (!cell->parent_names)
  1212. {
  1213. return RT_NULL;
  1214. }
  1215. if (cell->parent)
  1216. {
  1217. return cell->parent;
  1218. }
  1219. if (cell->parents_nr > 1)
  1220. {
  1221. if (!cell->ops->get_parent)
  1222. {
  1223. LOG_E("%s: Missing get_parent() while having parent_names", cell->name);
  1224. return RT_NULL;
  1225. }
  1226. idx = cell->ops->get_parent(cell);
  1227. if (idx >= cell->parents_nr)
  1228. {
  1229. LOG_E("%s: Get parent fail", cell->name);
  1230. return RT_NULL;
  1231. }
  1232. }
  1233. else
  1234. {
  1235. idx = 0;
  1236. }
  1237. parent_cell = rt_clk_cell_get_parent_by_index(cell, idx);
  1238. if (!parent_cell)
  1239. {
  1240. return RT_NULL;
  1241. }
  1242. if (!parent_cell->clk && !(parent_cell->clk = clk_cell_get_clk(parent_cell)))
  1243. {
  1244. return RT_NULL;
  1245. }
  1246. parent = parent_cell->clk;
  1247. cell->parent = parent;
  1248. return parent;
  1249. }
  1250. /**
  1251. * @brief Get parent clock pointer
  1252. *
  1253. * @param clk child clock
  1254. *
  1255. * @return struct rt_clk* parent clock object pointer will be return, unless child
  1256. clock node havn't parent node instead return RT_NULL
  1257. */
  1258. struct rt_clk *rt_clk_get_parent(struct rt_clk *clk)
  1259. {
  1260. struct rt_clk *parent;
  1261. if (clk)
  1262. {
  1263. clk_lock();
  1264. parent = clk_get_parent(clk);
  1265. clk_unlock();
  1266. }
  1267. else
  1268. {
  1269. parent = RT_NULL;
  1270. }
  1271. return parent;
  1272. }
  1273. /**
  1274. * @brief Set clock phase
  1275. *
  1276. * @param clk point to clock
  1277. * @param degrees target phase and the unit of phase is degree
  1278. *
  1279. * @return rt_err_t RT_EOK on set clock phase sucessfully, and other value is failed.
  1280. */
  1281. rt_err_t rt_clk_set_phase(struct rt_clk *clk, int degrees)
  1282. {
  1283. rt_err_t err;
  1284. if (clk)
  1285. {
  1286. struct rt_clk_cell *cell = clk->cell;
  1287. /* Sanity check degrees */
  1288. degrees %= 360;
  1289. if (degrees < 0)
  1290. {
  1291. degrees += 360;
  1292. }
  1293. if (cell->ops->set_phase)
  1294. {
  1295. err = cell->ops->set_phase(cell, degrees);
  1296. }
  1297. else
  1298. {
  1299. err = -RT_ENOSYS;
  1300. }
  1301. }
  1302. else
  1303. {
  1304. err = RT_EOK;
  1305. }
  1306. return err;
  1307. }
  1308. /**
  1309. * @brief Get clock phase
  1310. *
  1311. * @param clk point to clock
  1312. *
  1313. * @return rt_base_t clock phase or error code
  1314. */
  1315. rt_base_t rt_clk_get_phase(struct rt_clk *clk)
  1316. {
  1317. rt_base_t res;
  1318. if (clk)
  1319. {
  1320. struct rt_clk_cell *cell = clk->cell;
  1321. if (cell->ops->get_phase)
  1322. {
  1323. res = cell->ops->get_phase(cell);
  1324. }
  1325. else
  1326. {
  1327. res = 0;
  1328. }
  1329. }
  1330. else
  1331. {
  1332. res = 0;
  1333. }
  1334. return res;
  1335. }
  1336. /**
  1337. * @brief Check if the clock cell is prepared
  1338. *
  1339. * @param cell Pointer to clock cell
  1340. *
  1341. * @return RT_TRUE if prepared, otherwise RT_FALSE
  1342. */
  1343. rt_bool_t rt_clk_cell_is_prepared(const struct rt_clk_cell *cell)
  1344. {
  1345. RT_ASSERT(cell != RT_NULL);
  1346. if (cell->ops->is_prepared)
  1347. {
  1348. return cell->ops->is_prepared((struct rt_clk_cell *)cell);
  1349. }
  1350. return RT_TRUE;
  1351. }
  1352. /**
  1353. * @brief Get or create clock handle for a clock cell
  1354. *
  1355. * @param cell Pointer to clock cell
  1356. *
  1357. * @return Pointer to clock handle, or RT_NULL on failure
  1358. *
  1359. * @note If the clock handle does not exist, it will be created automatically.
  1360. */
  1361. static struct rt_clk *clk_cell_get_clk(struct rt_clk_cell *cell)
  1362. {
  1363. if (cell->clk)
  1364. {
  1365. return cell->clk;
  1366. }
  1367. cell->clk = clk_alloc(cell, RT_NULL, RT_NULL);
  1368. return cell->clk;
  1369. }
  1370. /**
  1371. * @brief Get or create clock handle for a clock cell
  1372. *
  1373. * @param cell Pointer to clock cell
  1374. * @param con_id Connection identifier for the clock cell
  1375. *
  1376. * @return Pointer to clock handle, or RT_NULL on failure
  1377. *
  1378. * @note If the clock handle does not exist, it will be created automatically.
  1379. */
  1380. struct rt_clk *rt_clk_cell_get_clk(const struct rt_clk_cell *cell, const char *con_id)
  1381. {
  1382. struct rt_clk *clk;
  1383. RT_ASSERT(cell != RT_NULL);
  1384. if ((clk = clk_cell_get_clk((struct rt_clk_cell *)cell)))
  1385. {
  1386. if (!clk->con_id)
  1387. {
  1388. clk->con_id = con_id;
  1389. }
  1390. }
  1391. return clk;
  1392. }
  1393. /**
  1394. * @brief Check if the clock cell is enabled
  1395. *
  1396. * @param cell Pointer to clock cell
  1397. *
  1398. * @return RT_TRUE if enabled, otherwise RT_FALSE
  1399. */
  1400. rt_bool_t rt_clk_cell_is_enabled(const struct rt_clk_cell *cell)
  1401. {
  1402. RT_ASSERT(cell != RT_NULL);
  1403. if (cell->ops->is_enabled)
  1404. {
  1405. return cell->ops->is_enabled((struct rt_clk_cell *)cell);
  1406. }
  1407. return RT_TRUE;
  1408. }
  1409. /**
  1410. * @brief Get current rate of the clock cell
  1411. *
  1412. * @param cell Pointer to clock cell
  1413. *
  1414. * @return Current rate in Hz
  1415. */
  1416. rt_ubase_t rt_clk_cell_get_rate(const struct rt_clk_cell *cell)
  1417. {
  1418. struct rt_clk *clk;
  1419. RT_ASSERT(cell != RT_NULL);
  1420. clk = clk_cell_get_clk((struct rt_clk_cell *)cell);
  1421. return clk_get_rate(clk);
  1422. }
  1423. /**
  1424. * @brief Round a desired rate to the nearest supported rate
  1425. *
  1426. * @param cell Pointer to clock cell
  1427. * @param rate Desired frequency in Hz
  1428. *
  1429. * @return Closest supported frequency in Hz
  1430. */
  1431. rt_ubase_t rt_clk_cell_round_rate(struct rt_clk_cell *cell, rt_ubase_t rate)
  1432. {
  1433. struct rt_clk *clk;
  1434. RT_ASSERT(cell != RT_NULL);
  1435. clk = clk_cell_get_clk((struct rt_clk_cell *)cell);
  1436. return clk_round_rate(clk, rate);
  1437. }
  1438. /**
  1439. * @brief Get parent clock cell
  1440. *
  1441. * @param cell Pointer to clock cell
  1442. *
  1443. * @return Pointer to parent clock cell, or RT_NULL if none
  1444. */
  1445. struct rt_clk_cell *rt_clk_cell_get_parent(const struct rt_clk_cell *cell)
  1446. {
  1447. struct rt_clk *clk, *parent_clk;
  1448. RT_ASSERT(cell != RT_NULL);
  1449. clk = clk_cell_get_clk((struct rt_clk_cell *)cell);
  1450. if ((parent_clk = clk_get_parent(clk)))
  1451. {
  1452. return parent_clk->cell;
  1453. }
  1454. return RT_NULL;
  1455. }
  1456. /**
  1457. * @brief Get parent clock cell by index
  1458. *
  1459. * @param cell Pointer to clock cell
  1460. * @param idx Parent index
  1461. *
  1462. * @return Pointer to parent clock cell, or RT_NULL if not found
  1463. */
  1464. struct rt_clk_cell *rt_clk_cell_get_parent_by_index(const struct rt_clk_cell *cell, rt_uint8_t idx)
  1465. {
  1466. const char *pname;
  1467. struct rt_clk_cell *parent_cell;
  1468. struct rt_clk_node *clk_np, *clk_np_raw;
  1469. RT_ASSERT(cell != RT_NULL);
  1470. RT_ASSERT(idx != RT_UINT8_MAX);
  1471. clk_np = cell->clk_np;
  1472. if (cell->parents_nr > 1)
  1473. {
  1474. pname = cell->parent_names[idx];
  1475. }
  1476. else if (idx == 0)
  1477. {
  1478. pname = cell->parent_name;
  1479. }
  1480. else
  1481. {
  1482. pname = RT_NULL;
  1483. goto _end;
  1484. }
  1485. clk_np_raw = RT_NULL;
  1486. _retry:
  1487. if (!rt_is_err_or_null(clk_np->parents_clk))
  1488. {
  1489. struct rt_clk_array *parents_clk = clk_np->parents_clk;
  1490. for (rt_uint8_t i = 0; i < parents_clk->count; ++i)
  1491. {
  1492. if (!rt_strcmp(pname, parents_clk->clks[i]->cell->name))
  1493. {
  1494. return parents_clk->clks[i]->cell;
  1495. }
  1496. }
  1497. }
  1498. for (int i = 0; i < clk_np->cells_nr; ++i)
  1499. {
  1500. parent_cell = clk_np->cells[i];
  1501. if (!parent_cell)
  1502. {
  1503. continue;
  1504. }
  1505. if (!rt_strcmp(parent_cell->name, pname))
  1506. {
  1507. return (struct rt_clk_cell *)parent_cell;
  1508. }
  1509. }
  1510. /* Find on the global list */
  1511. if (clk_np_raw)
  1512. {
  1513. do {
  1514. clk_np = rt_list_entry(clk_np->parent.list.next, rt_typeof(*clk_np), parent.list);
  1515. } while (&clk_np->parent.list != &_clk_node_nodes && clk_np == clk_np_raw);
  1516. }
  1517. else
  1518. {
  1519. clk_np_raw = clk_np;
  1520. clk_np = rt_list_entry(_clk_node_nodes.next, rt_typeof(*clk_np), parent.list);
  1521. }
  1522. if (&clk_np->parent.list != &_clk_node_nodes)
  1523. {
  1524. goto _retry;
  1525. }
  1526. _end:
  1527. LOG_E("%s: Parent[%d] '%s' not found", cell->name, idx, pname);
  1528. return RT_NULL;
  1529. }
  1530. /**
  1531. * @brief Get current parent index
  1532. *
  1533. * @param cell Pointer to clock cell
  1534. *
  1535. * @return Parent index on success, negative error code on failure
  1536. */
  1537. rt_uint8_t rt_clk_cell_get_parent_index(struct rt_clk_cell *cell)
  1538. {
  1539. RT_ASSERT(cell != RT_NULL);
  1540. if (cell->ops->get_parent)
  1541. {
  1542. return cell->ops->get_parent(cell);
  1543. }
  1544. return RT_UINT8_MAX;
  1545. }
  1546. /**
  1547. * @brief Set new parent clock cell
  1548. *
  1549. * @param cell Pointer to clock cell
  1550. * @param parent Pointer to new parent clock cell
  1551. *
  1552. * @return RT_EOK on success, or error code on failure
  1553. */
  1554. rt_err_t rt_clk_cell_set_parent(struct rt_clk_cell *cell, struct rt_clk_cell *parent)
  1555. {
  1556. rt_err_t err;
  1557. struct rt_clk *clk, *parent_clk = RT_NULL;
  1558. RT_ASSERT(cell != RT_NULL);
  1559. clk = clk_cell_get_clk((struct rt_clk_cell *)cell);
  1560. if (parent)
  1561. {
  1562. parent_clk = clk_cell_get_clk((struct rt_clk_cell *)parent);
  1563. }
  1564. if ((err = clk_set_parent(clk, parent_clk)))
  1565. {
  1566. return err;
  1567. }
  1568. return RT_EOK;
  1569. }
  1570. /**
  1571. * @brief Get clock array pointer from ofw device node
  1572. *
  1573. * @param dev point to dev
  1574. *
  1575. * @return struct rt_clk_array* if use ofw and under normal circumstance, it will return
  1576. clock array pointer and other value is RT_NULL
  1577. */
  1578. struct rt_clk_array *rt_clk_get_array(struct rt_device *dev)
  1579. {
  1580. struct rt_clk_array *clk_arr = RT_NULL;
  1581. #ifdef RT_USING_OFW
  1582. clk_arr = rt_ofw_get_clk_array(dev->ofw_node);
  1583. #endif
  1584. return clk_arr;
  1585. }
  1586. /**
  1587. * @brief Get clock pointer from ofw device node by index
  1588. *
  1589. * @param dev point to dev
  1590. * @param index index of clock object
  1591. *
  1592. * @return struct rt_clk* if use ofw and under normal circumstance, it will return clock
  1593. pointer and other value is RT_NULL
  1594. */
  1595. struct rt_clk *rt_clk_get_by_index(struct rt_device *dev, int index)
  1596. {
  1597. struct rt_clk *clk = RT_NULL;
  1598. #ifdef RT_USING_OFW
  1599. clk = rt_ofw_get_clk(dev->ofw_node, index);
  1600. #endif
  1601. return clk;
  1602. }
  1603. /**
  1604. * @brief Get clock pointer from ofw device node by name
  1605. *
  1606. * @param dev point to dev
  1607. * @param name name of clock object
  1608. *
  1609. * @return struct rt_clk* if use ofw and under normal circumstance, it will return clock
  1610. pointer and other value is RT_NULL
  1611. */
  1612. struct rt_clk *rt_clk_get_by_name(struct rt_device *dev, const char *name)
  1613. {
  1614. struct rt_clk *clk = RT_NULL;
  1615. #ifdef RT_USING_OFW
  1616. clk = rt_ofw_get_clk_by_name(dev->ofw_node, name);
  1617. #endif
  1618. if (!clk && name)
  1619. {
  1620. struct rt_clk_node *clk_np;
  1621. struct rt_clk_cell *cell = RT_NULL;
  1622. clk_lock();
  1623. rt_list_for_each_entry(clk_np, &_clk_node_nodes, parent.list)
  1624. {
  1625. for (int i = 0; i < clk_np->cells_nr; ++i)
  1626. {
  1627. cell = clk_np->cells[i];
  1628. if (!cell)
  1629. {
  1630. continue;
  1631. }
  1632. if (!rt_strcmp(cell->name, name))
  1633. {
  1634. clk = clk_alloc(cell, rt_dm_dev_get_name(dev), RT_NULL);
  1635. if (clk)
  1636. {
  1637. clk_cell_bind(cell, clk);
  1638. }
  1639. goto _out_lock;
  1640. }
  1641. }
  1642. }
  1643. _out_lock:
  1644. clk_unlock();
  1645. }
  1646. return clk;
  1647. }
  1648. /**
  1649. * @brief Put reference count of all colock in the clock array
  1650. *
  1651. * @param clk_arr point to clock array
  1652. *
  1653. */
  1654. void rt_clk_array_put(struct rt_clk_array *clk_arr)
  1655. {
  1656. if (clk_arr)
  1657. {
  1658. for (int i = 0; i < clk_arr->count; ++i)
  1659. {
  1660. if (!rt_is_err_or_null(clk_arr->clks[i]))
  1661. {
  1662. rt_clk_put(clk_arr->clks[i]);
  1663. }
  1664. else
  1665. {
  1666. break;
  1667. }
  1668. }
  1669. rt_free(clk_arr);
  1670. }
  1671. }
  1672. /**
  1673. * @brief Put reference count of clock
  1674. *
  1675. * @param clk point to clock
  1676. *
  1677. */
  1678. void rt_clk_put(struct rt_clk *clk)
  1679. {
  1680. if (clk && clk->cell->clk != clk)
  1681. {
  1682. rt_free(clk);
  1683. }
  1684. }
  1685. #ifdef RT_USING_OFW
  1686. static struct rt_clk_array *ofw_get_clk_array(struct rt_ofw_node *np,
  1687. const char *basename, const char *propname);
  1688. static struct rt_clk *ofw_get_clk(struct rt_ofw_node *np,
  1689. const char *basename, int index, const char *name);
  1690. /**
  1691. * @brief Retrieve a clock cell from a clock node using OFW (device tree) arguments.
  1692. *
  1693. * @details
  1694. * This helper function translates parsed device tree clock specifiers
  1695. * (from `clocks = <&phandle args...>;`) into an actual `rt_clk_cell` pointer
  1696. * belonging to the specified `clk_np` (clock node).
  1697. *
  1698. * Behavior:
  1699. * - If the clock node provides a custom parser (`clk_np->ofw_parse`),
  1700. * this function delegates the lookup to that callback.
  1701. * → This allows complex clock providers (e.g. multiplexers, dividers)
  1702. * to interpret multiple arguments or encoded indices.
  1703. * - Otherwise, it assumes the first argument (`args->args[0]`)
  1704. * is the cell index and directly returns `clk_np->cells[args->args[0]]`.
  1705. *
  1706. * This abstraction allows different clock providers to implement flexible
  1707. * device-tree bindings without changing the core clock framework.
  1708. *
  1709. * @param clk_np Pointer to the clock node containing clock cells.
  1710. * @param args Pointer to parsed OFW clock arguments (from device tree).
  1711. *
  1712. * @return Pointer to the resolved `rt_clk_cell` if found, or `RT_NULL` on failure.
  1713. *
  1714. * @note
  1715. * - The default indexing behavior assumes that the clock node’s `#clock-cells`
  1716. * property equals 1 (only one integer index).
  1717. * - Complex clock providers should implement their own `.ofw_parse()` callback
  1718. * to handle multiple argument cases.
  1719. * - This function is typically used during `rt_ofw_clk_get()` to
  1720. * resolve device clock references.
  1721. */
  1722. static struct rt_clk_cell *ofw_get_cell(struct rt_clk_node *clk_np, struct rt_ofw_cell_args *args)
  1723. {
  1724. if (clk_np->ofw_parse)
  1725. {
  1726. return clk_np->ofw_parse(clk_np, args);
  1727. }
  1728. return clk_np->cells[args->args[0]];
  1729. }
  1730. /**
  1731. * @brief Get clock array from ofw by name
  1732. *
  1733. * @param np point to ofw node
  1734. * @param basename name of clocks base name
  1735. * @param propname name of clocks prop name
  1736. *
  1737. * @return struct rt_clk_array* point to the newly created clock array, or an error pointer
  1738. */
  1739. static struct rt_clk_array *ofw_get_clk_array(struct rt_ofw_node *np,
  1740. const char *basename, const char *propname)
  1741. {
  1742. int count;
  1743. rt_bool_t has_name;
  1744. struct rt_clk_array *clk_arr;
  1745. if ((count = rt_ofw_count_phandle_cells(np, basename, "#clock-cells")) <= 0)
  1746. {
  1747. if (count)
  1748. {
  1749. return rt_err_ptr(count);
  1750. }
  1751. return RT_NULL;
  1752. }
  1753. clk_arr = rt_calloc(1, sizeof(*clk_arr) + sizeof(clk_arr->clks[0]) * count);
  1754. if (!clk_arr)
  1755. {
  1756. return rt_err_ptr(-RT_ENOMEM);
  1757. }
  1758. clk_arr->count = count;
  1759. has_name = rt_ofw_prop_read_bool(np, propname);
  1760. clk_lock();
  1761. for (int i = 0; i < count; ++i)
  1762. {
  1763. const char *name = RT_NULL;
  1764. if (has_name)
  1765. {
  1766. rt_ofw_prop_read_string_index(np, "clock-names", i, &name);
  1767. }
  1768. clk_arr->clks[i] = ofw_get_clk(np, basename, i, name);
  1769. if (rt_is_err(clk_arr->clks[i]))
  1770. {
  1771. rt_err_t err = rt_ptr_err(clk_arr->clks[i]);
  1772. clk_unlock();
  1773. rt_clk_array_put(clk_arr);
  1774. return rt_err_ptr(err);
  1775. }
  1776. }
  1777. clk_unlock();
  1778. return clk_arr;
  1779. }
  1780. /**
  1781. * @brief Get clock array from ofw
  1782. *
  1783. * @param np point to ofw node
  1784. *
  1785. * @return struct rt_clk_array* point to the newly created clock array, or an error pointer
  1786. */
  1787. struct rt_clk_array *rt_ofw_get_clk_array(struct rt_ofw_node *np)
  1788. {
  1789. if (!np)
  1790. {
  1791. return rt_err_ptr(-RT_EINVAL);
  1792. }
  1793. return ofw_get_clk_array(np, "clocks", "clock-names");
  1794. }
  1795. /**
  1796. * @brief Get clock from ofw
  1797. *
  1798. * @param np point to ofw node
  1799. * @param basename name of clocks base name
  1800. * @param index index of clock in ofw
  1801. * @param name connection identifier for the clock
  1802. *
  1803. * @return struct rt_clk* point to the newly created clock object, or an error pointer
  1804. */
  1805. static struct rt_clk *ofw_get_clk(struct rt_ofw_node *np,
  1806. const char *basename, int index, const char *name)
  1807. {
  1808. struct rt_object *obj;
  1809. struct rt_clk *clk;
  1810. struct rt_clk_cell *cell;
  1811. struct rt_clk_node *clk_np = RT_NULL;
  1812. struct rt_ofw_node *clk_ofw_np;
  1813. struct rt_ofw_cell_args clk_args;
  1814. if (rt_ofw_parse_phandle_cells(np, basename, "#clock-cells", index, &clk_args))
  1815. {
  1816. return RT_NULL;
  1817. }
  1818. clk_ofw_np = clk_args.data;
  1819. if (!rt_ofw_data(clk_ofw_np))
  1820. {
  1821. if (clk_ofw_np == np)
  1822. {
  1823. LOG_D("%s: No registration to the system yet", rt_ofw_node_full_name(clk_ofw_np));
  1824. return RT_NULL;
  1825. }
  1826. rt_platform_ofw_request(clk_ofw_np);
  1827. }
  1828. if (rt_ofw_data(clk_ofw_np) && (obj = rt_ofw_parse_object(clk_ofw_np,
  1829. RT_CLK_NODE_OBJ_NAME, "#clock-cells")))
  1830. {
  1831. clk_np = rt_container_of(obj, struct rt_clk_node, parent);
  1832. }
  1833. if (!clk_np)
  1834. {
  1835. clk = rt_err_ptr(-RT_EINVAL);
  1836. goto _end;
  1837. }
  1838. if (!clk_args.args_count)
  1839. {
  1840. clk_args.args[0] = 0;
  1841. }
  1842. index = clk_args.args[0];
  1843. if (rt_ofw_prop_read_bool(clk_ofw_np, "clock-indices"))
  1844. {
  1845. const fdt32_t *val_raw;
  1846. rt_uint32_t val, indice = 0;
  1847. struct rt_ofw_prop *prop;
  1848. rt_ofw_foreach_prop_u32(clk_ofw_np, "clock-indices", prop, val_raw, val)
  1849. {
  1850. if (index == val)
  1851. {
  1852. index = indice;
  1853. goto _goon;
  1854. }
  1855. ++indice;
  1856. }
  1857. clk = rt_err_ptr(-RT_EINVAL);
  1858. goto _end;
  1859. }
  1860. _goon:
  1861. rt_ofw_prop_read_string_index(clk_ofw_np, "clock-output-names", index, &name);
  1862. if (!(cell = ofw_get_cell(clk_np, &clk_args)))
  1863. {
  1864. LOG_D("%s: CLK index = %d (%s) is not implemented",
  1865. rt_ofw_node_full_name(np), index, name);
  1866. return RT_NULL;
  1867. }
  1868. clk = clk_alloc(cell, rt_ofw_node_full_name(np), name);
  1869. if (clk)
  1870. {
  1871. clk_cell_bind(cell, clk);
  1872. }
  1873. else
  1874. {
  1875. clk = rt_err_ptr(-RT_ENOMEM);
  1876. }
  1877. _end:
  1878. rt_ofw_node_put(clk_ofw_np);
  1879. return clk;
  1880. }
  1881. /**
  1882. * @brief Get clock from ofw with acquiring a spin lock by index and node pointer
  1883. *
  1884. * @param np point to ofw node
  1885. * @param index index of clock in ofw
  1886. *
  1887. * @return struct rt_clk* point to the newly created clock object, or an error pointer
  1888. */
  1889. struct rt_clk *rt_ofw_get_clk(struct rt_ofw_node *np, int index)
  1890. {
  1891. struct rt_clk *clk = RT_NULL;
  1892. if (np && index >= 0)
  1893. {
  1894. clk = ofw_get_clk(np, "clocks", index, RT_NULL);
  1895. }
  1896. return clk;
  1897. }
  1898. /**
  1899. * @brief Get clock from ofw with acquiring a spin lock by name
  1900. *
  1901. * @param np point to ofw node
  1902. * @param name name of clock will be returned
  1903. *
  1904. * @return struct rt_clk* point to the newly created clock object, or an error pointer
  1905. */
  1906. struct rt_clk *rt_ofw_get_clk_by_name(struct rt_ofw_node *np, const char *name)
  1907. {
  1908. struct rt_clk *clk = RT_NULL;
  1909. if (np && name)
  1910. {
  1911. int index = rt_ofw_prop_index_of_string(np, "clock-names", name);
  1912. if (index >= 0)
  1913. {
  1914. clk = ofw_get_clk(np, "clocks", index, name);
  1915. }
  1916. }
  1917. return clk;
  1918. }
  1919. /**
  1920. * @brief Count number of clocks in ofw
  1921. *
  1922. * @param clk_ofw_np point to ofw node
  1923. *
  1924. * @return rt_ssize_t number of clocks
  1925. */
  1926. rt_ssize_t rt_ofw_count_of_clk(struct rt_ofw_node *clk_ofw_np)
  1927. {
  1928. if (clk_ofw_np)
  1929. {
  1930. struct rt_clk_node *clk_np = rt_ofw_data(clk_ofw_np);
  1931. if (clk_np && clk_np->multi_clk)
  1932. {
  1933. return clk_np->multi_clk;
  1934. }
  1935. else
  1936. {
  1937. const fdt32_t *cell;
  1938. rt_uint32_t count = 0;
  1939. struct rt_ofw_prop *prop;
  1940. prop = rt_ofw_get_prop(clk_ofw_np, "clock-indices", RT_NULL);
  1941. if (prop)
  1942. {
  1943. rt_uint32_t max_idx = 0, idx;
  1944. for (cell = rt_ofw_prop_next_u32(prop, RT_NULL, &idx);
  1945. cell;
  1946. cell = rt_ofw_prop_next_u32(prop, cell, &idx))
  1947. {
  1948. if (idx > max_idx)
  1949. {
  1950. max_idx = idx;
  1951. }
  1952. }
  1953. count = max_idx + 1;
  1954. }
  1955. else
  1956. {
  1957. rt_ssize_t len;
  1958. if ((prop = rt_ofw_get_prop(clk_ofw_np, "clock-output-names", &len)))
  1959. {
  1960. char *value = prop->value;
  1961. for (int i = 0; i < len; ++i, ++value)
  1962. {
  1963. if (*value == '\0')
  1964. {
  1965. ++count;
  1966. }
  1967. }
  1968. if (!count)
  1969. {
  1970. count = 1;
  1971. }
  1972. }
  1973. else
  1974. {
  1975. count = 1;
  1976. }
  1977. }
  1978. if (clk_np)
  1979. {
  1980. clk_np->multi_clk = count;
  1981. }
  1982. return count;
  1983. }
  1984. }
  1985. return -RT_EINVAL;
  1986. }
  1987. /**
  1988. * @brief Get parent clock name from device tree
  1989. *
  1990. * @param np Pointer to device tree node
  1991. * @param index Index within "clocks" property
  1992. *
  1993. * @return const char* Name of the parent clock, or NULL if not found
  1994. */
  1995. const char *rt_ofw_clk_get_parent_name(struct rt_ofw_node *np, int index)
  1996. {
  1997. const char *pname = RT_NULL;
  1998. struct rt_ofw_node *clk_ofw_np;
  1999. struct rt_ofw_cell_args clk_args;
  2000. if (rt_ofw_parse_phandle_cells(np, "clocks", "#clock-cells", index, &clk_args))
  2001. {
  2002. return RT_NULL;
  2003. }
  2004. clk_ofw_np = clk_args.data;
  2005. index = clk_args.args_count ? clk_args.args[0] : 0;
  2006. if (rt_ofw_prop_read_bool(clk_ofw_np, "clock-indices"))
  2007. {
  2008. const fdt32_t *val_raw;
  2009. rt_uint32_t val, indice = 0;
  2010. struct rt_ofw_prop *prop;
  2011. rt_ofw_foreach_prop_u32(clk_ofw_np, "clock-indices", prop, val_raw, val)
  2012. {
  2013. if (index == val)
  2014. {
  2015. index = indice;
  2016. goto _goon;
  2017. }
  2018. ++indice;
  2019. }
  2020. goto _end;
  2021. }
  2022. _goon:
  2023. if (rt_ofw_prop_read_string_index(clk_ofw_np, "clock-output-names", index, &pname))
  2024. {
  2025. struct rt_clk *provider_clk = rt_ofw_get_clk(np, index);
  2026. if (rt_is_err_or_null(provider_clk))
  2027. {
  2028. pname = provider_clk->cell->name;
  2029. rt_clk_put(provider_clk);
  2030. }
  2031. }
  2032. _end:
  2033. rt_ofw_node_put(clk_ofw_np);
  2034. return pname;
  2035. }
  2036. /**
  2037. * @brief Initialize clock from device tree (OFW) defaults.
  2038. *
  2039. * @details
  2040. * This function applies the device tree–specified clock default.
  2041. * It processes the following standard DT bindings in order:
  2042. *
  2043. * - **assigned-clocks**: list of clock phandles that must be configured
  2044. * before the device is probed.
  2045. * - **assigned-clock-parents**: optional list of corresponding parent
  2046. * clock phandles for each entry in *assigned-clocks*.
  2047. * - **assigned-clock-rates**: optional list of target rates (in Hz)
  2048. * to set for each clock in *assigned-clocks*.
  2049. *
  2050. * For each assigned clock, the function will:
  2051. * 1. Retrieve the referenced clock handle.
  2052. * 2. Set its parent if a corresponding entry in
  2053. * *assigned-clock-parents* exists.
  2054. * 3. Set its rate if a corresponding entry in
  2055. * *assigned-clock-rates* exists.
  2056. *
  2057. * This ensures that all clocks required by a device are configured
  2058. * according to the hardware design before the device driver runs.
  2059. *
  2060. * @param np Point to ofw node
  2061. *
  2062. * @return
  2063. * - RT_EOK : Successfully applied assigned-clocks settings.
  2064. * - -RT_EINVAL : Invalid or inconsistent device tree entries.
  2065. * - -RT_ENOSYS : Clock driver does not support required operation.
  2066. * - Other negative values : Underlying driver or hardware error.
  2067. *
  2068. * @note
  2069. * - This function should be called **after all clocks in the system
  2070. * have been registered**, ensuring that referenced parents exist.
  2071. * - Clocks not listed in *assigned-clocks* are left unchanged.
  2072. * - The function is typically invoked at the end of
  2073. * `rt_clk_node_register()`.
  2074. */
  2075. rt_err_t rt_ofw_clk_set_defaults(struct rt_ofw_node *np)
  2076. {
  2077. struct rt_clk *clk;
  2078. struct rt_clk_array *clk_arr;
  2079. if (!np)
  2080. {
  2081. return RT_EOK;
  2082. }
  2083. clk_arr = ofw_get_clk_array(np, "assigned-clocks", RT_NULL);
  2084. if (rt_is_err(clk_arr))
  2085. {
  2086. return rt_ptr_err(clk_arr);
  2087. }
  2088. if (clk_arr)
  2089. {
  2090. rt_uint32_t rate;
  2091. struct rt_clk_array *clk_parent_arr;
  2092. clk_parent_arr = ofw_get_clk_array(np, "assigned-clock-parents", RT_NULL);
  2093. if (rt_is_err(clk_parent_arr))
  2094. {
  2095. rt_clk_array_put(clk_arr);
  2096. return rt_ptr_err(clk_parent_arr);
  2097. }
  2098. for (int i = 0; i < clk_arr->count; ++i)
  2099. {
  2100. clk = clk_arr->clks[i];
  2101. if (clk_parent_arr && i < clk_parent_arr->count)
  2102. {
  2103. rt_clk_set_parent(clk, clk_parent_arr->clks[i]);
  2104. }
  2105. if (!rt_ofw_prop_read_u32_index(np, "assigned-clock-rates", i, &rate))
  2106. {
  2107. rt_clk_set_rate(clk, rate);
  2108. }
  2109. }
  2110. rt_clk_array_put(clk_parent_arr);
  2111. rt_clk_array_put(clk_arr);
  2112. }
  2113. return RT_EOK;
  2114. }
  2115. #endif /* RT_USING_OFW */
  2116. #if defined(RT_USING_CONSOLE) && defined(RT_USING_MSH)
  2117. static int list_clk(int argc, char**argv)
  2118. {
  2119. struct rt_clk_node *clk_np;
  2120. struct rt_clk_cell *cell, *parent;
  2121. rt_kprintf("%-*.s %-*.s %-*.s %-*.s %-*.s %-*.s Parent\n",
  2122. 32, "Name",
  2123. 12, "Enable Count",
  2124. 13, "Prepare Count",
  2125. 11, "Rate",
  2126. 32, "Device ID",
  2127. 32, "Connection ID");
  2128. clk_lock();
  2129. rt_list_for_each_entry(clk_np, &_clk_node_nodes, parent.list)
  2130. {
  2131. for (int i = 0; i < clk_np->cells_nr; ++i)
  2132. {
  2133. rt_ubase_t rate;
  2134. const char *dev_id = "deviceless", *con_id = "no_connection_id";
  2135. cell = clk_np->cells[i];
  2136. if (!cell)
  2137. {
  2138. continue;
  2139. }
  2140. rate = cell->rate ? : rt_clk_cell_get_rate(cell);
  2141. if (cell->clk)
  2142. {
  2143. if (cell->clk->dev_id)
  2144. {
  2145. dev_id = cell->clk->dev_id;
  2146. }
  2147. if (cell->clk->con_id)
  2148. {
  2149. con_id = cell->clk->con_id;
  2150. }
  2151. }
  2152. parent = rt_clk_cell_get_parent(cell);
  2153. rt_kprintf("%-*.s %-12d %-13d %-11lu %-*.s %-*.s %s\n",
  2154. 32, cell->name,
  2155. cell->enable_count,
  2156. cell->prepare_count,
  2157. rate,
  2158. 32, dev_id,
  2159. 32, con_id,
  2160. parent ? parent->name : RT_NULL);
  2161. }
  2162. }
  2163. clk_unlock();
  2164. return 0;
  2165. }
  2166. MSH_CMD_EXPORT(list_clk, dump all of clk information);
  2167. #endif /* RT_USING_CONSOLE && RT_USING_MSH */
  2168. /**@}*/