clk_test.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Kunit tests for clk framework
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/clk-provider.h>
  7. #include <linux/clk/clk-conf.h>
  8. #include <linux/of.h>
  9. #include <linux/platform_device.h>
  10. /* Needed for clk_hw_get_clk() */
  11. #include "clk.h"
  12. #include <kunit/clk.h>
  13. #include <kunit/of.h>
  14. #include <kunit/platform_device.h>
  15. #include <kunit/test.h>
  16. #include "kunit_clk_assigned_rates.h"
  17. #include "clk_parent_data_test.h"
  18. static const struct clk_ops empty_clk_ops = { };
  19. #define DUMMY_CLOCK_INIT_RATE (42 * 1000 * 1000)
  20. #define DUMMY_CLOCK_RATE_1 (142 * 1000 * 1000)
  21. #define DUMMY_CLOCK_RATE_2 (242 * 1000 * 1000)
  22. struct clk_dummy_context {
  23. struct clk_hw hw;
  24. unsigned long rate;
  25. };
  26. static unsigned long clk_dummy_recalc_rate(struct clk_hw *hw,
  27. unsigned long parent_rate)
  28. {
  29. struct clk_dummy_context *ctx =
  30. container_of(hw, struct clk_dummy_context, hw);
  31. return ctx->rate;
  32. }
  33. static int clk_dummy_determine_rate(struct clk_hw *hw,
  34. struct clk_rate_request *req)
  35. {
  36. /* Just return the same rate without modifying it */
  37. return 0;
  38. }
  39. static int clk_dummy_maximize_rate(struct clk_hw *hw,
  40. struct clk_rate_request *req)
  41. {
  42. /*
  43. * If there's a maximum set, always run the clock at the maximum
  44. * allowed.
  45. */
  46. if (req->max_rate < ULONG_MAX)
  47. req->rate = req->max_rate;
  48. return 0;
  49. }
  50. static int clk_dummy_minimize_rate(struct clk_hw *hw,
  51. struct clk_rate_request *req)
  52. {
  53. /*
  54. * If there's a minimum set, always run the clock at the minimum
  55. * allowed.
  56. */
  57. if (req->min_rate > 0)
  58. req->rate = req->min_rate;
  59. return 0;
  60. }
  61. static int clk_dummy_set_rate(struct clk_hw *hw,
  62. unsigned long rate,
  63. unsigned long parent_rate)
  64. {
  65. struct clk_dummy_context *ctx =
  66. container_of(hw, struct clk_dummy_context, hw);
  67. ctx->rate = rate;
  68. return 0;
  69. }
  70. static int clk_dummy_single_set_parent(struct clk_hw *hw, u8 index)
  71. {
  72. if (index >= clk_hw_get_num_parents(hw))
  73. return -EINVAL;
  74. return 0;
  75. }
  76. static u8 clk_dummy_single_get_parent(struct clk_hw *hw)
  77. {
  78. return 0;
  79. }
  80. static const struct clk_ops clk_dummy_rate_ops = {
  81. .recalc_rate = clk_dummy_recalc_rate,
  82. .determine_rate = clk_dummy_determine_rate,
  83. .set_rate = clk_dummy_set_rate,
  84. };
  85. static const struct clk_ops clk_dummy_maximize_rate_ops = {
  86. .recalc_rate = clk_dummy_recalc_rate,
  87. .determine_rate = clk_dummy_maximize_rate,
  88. .set_rate = clk_dummy_set_rate,
  89. };
  90. static const struct clk_ops clk_dummy_minimize_rate_ops = {
  91. .recalc_rate = clk_dummy_recalc_rate,
  92. .determine_rate = clk_dummy_minimize_rate,
  93. .set_rate = clk_dummy_set_rate,
  94. };
  95. static const struct clk_ops clk_dummy_single_parent_ops = {
  96. /*
  97. * FIXME: Even though we should probably be able to use
  98. * __clk_mux_determine_rate() here, if we use it and call
  99. * clk_round_rate() or clk_set_rate() with a rate lower than
  100. * what all the parents can provide, it will return -EINVAL.
  101. *
  102. * This is due to the fact that it has the undocumented
  103. * behaviour to always pick up the closest rate higher than the
  104. * requested rate. If we get something lower, it thus considers
  105. * that it's not acceptable and will return an error.
  106. *
  107. * It's somewhat inconsistent and creates a weird threshold
  108. * between rates above the parent rate which would be rounded to
  109. * what the parent can provide, but rates below will simply
  110. * return an error.
  111. */
  112. .determine_rate = __clk_mux_determine_rate_closest,
  113. .set_parent = clk_dummy_single_set_parent,
  114. .get_parent = clk_dummy_single_get_parent,
  115. };
  116. struct clk_multiple_parent_ctx {
  117. struct clk_dummy_context parents_ctx[2];
  118. struct clk_hw hw;
  119. u8 current_parent;
  120. };
  121. static int clk_multiple_parents_mux_set_parent(struct clk_hw *hw, u8 index)
  122. {
  123. struct clk_multiple_parent_ctx *ctx =
  124. container_of(hw, struct clk_multiple_parent_ctx, hw);
  125. if (index >= clk_hw_get_num_parents(hw))
  126. return -EINVAL;
  127. ctx->current_parent = index;
  128. return 0;
  129. }
  130. static u8 clk_multiple_parents_mux_get_parent(struct clk_hw *hw)
  131. {
  132. struct clk_multiple_parent_ctx *ctx =
  133. container_of(hw, struct clk_multiple_parent_ctx, hw);
  134. return ctx->current_parent;
  135. }
  136. static const struct clk_ops clk_multiple_parents_mux_ops = {
  137. .get_parent = clk_multiple_parents_mux_get_parent,
  138. .set_parent = clk_multiple_parents_mux_set_parent,
  139. .determine_rate = __clk_mux_determine_rate_closest,
  140. };
  141. static const struct clk_ops clk_multiple_parents_no_reparent_mux_ops = {
  142. .determine_rate = clk_hw_determine_rate_no_reparent,
  143. .get_parent = clk_multiple_parents_mux_get_parent,
  144. .set_parent = clk_multiple_parents_mux_set_parent,
  145. };
  146. static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
  147. {
  148. struct clk_dummy_context *ctx;
  149. struct clk_init_data init = { };
  150. int ret;
  151. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  152. if (!ctx)
  153. return -ENOMEM;
  154. ctx->rate = DUMMY_CLOCK_INIT_RATE;
  155. test->priv = ctx;
  156. init.name = "test_dummy_rate";
  157. init.ops = ops;
  158. ctx->hw.init = &init;
  159. ret = clk_hw_register(NULL, &ctx->hw);
  160. if (ret)
  161. return ret;
  162. return 0;
  163. }
  164. static int clk_test_init(struct kunit *test)
  165. {
  166. return clk_test_init_with_ops(test, &clk_dummy_rate_ops);
  167. }
  168. static int clk_maximize_test_init(struct kunit *test)
  169. {
  170. return clk_test_init_with_ops(test, &clk_dummy_maximize_rate_ops);
  171. }
  172. static int clk_minimize_test_init(struct kunit *test)
  173. {
  174. return clk_test_init_with_ops(test, &clk_dummy_minimize_rate_ops);
  175. }
  176. static void clk_test_exit(struct kunit *test)
  177. {
  178. struct clk_dummy_context *ctx = test->priv;
  179. clk_hw_unregister(&ctx->hw);
  180. }
  181. /*
  182. * Test that the actual rate matches what is returned by clk_get_rate()
  183. */
  184. static void clk_test_get_rate(struct kunit *test)
  185. {
  186. struct clk_dummy_context *ctx = test->priv;
  187. struct clk_hw *hw = &ctx->hw;
  188. struct clk *clk = clk_hw_get_clk(hw, NULL);
  189. unsigned long rate;
  190. rate = clk_get_rate(clk);
  191. KUNIT_ASSERT_GT(test, rate, 0);
  192. KUNIT_EXPECT_EQ(test, rate, ctx->rate);
  193. clk_put(clk);
  194. }
  195. /*
  196. * Test that, after a call to clk_set_rate(), the rate returned by
  197. * clk_get_rate() matches.
  198. *
  199. * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
  200. * modify the requested rate, which is our case in clk_dummy_rate_ops.
  201. */
  202. static void clk_test_set_get_rate(struct kunit *test)
  203. {
  204. struct clk_dummy_context *ctx = test->priv;
  205. struct clk_hw *hw = &ctx->hw;
  206. struct clk *clk = clk_hw_get_clk(hw, NULL);
  207. unsigned long rate;
  208. KUNIT_ASSERT_EQ(test,
  209. clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
  210. 0);
  211. rate = clk_get_rate(clk);
  212. KUNIT_ASSERT_GT(test, rate, 0);
  213. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
  214. clk_put(clk);
  215. }
  216. /*
  217. * Test that, after several calls to clk_set_rate(), the rate returned
  218. * by clk_get_rate() matches the last one.
  219. *
  220. * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
  221. * modify the requested rate, which is our case in clk_dummy_rate_ops.
  222. */
  223. static void clk_test_set_set_get_rate(struct kunit *test)
  224. {
  225. struct clk_dummy_context *ctx = test->priv;
  226. struct clk_hw *hw = &ctx->hw;
  227. struct clk *clk = clk_hw_get_clk(hw, NULL);
  228. unsigned long rate;
  229. KUNIT_ASSERT_EQ(test,
  230. clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
  231. 0);
  232. KUNIT_ASSERT_EQ(test,
  233. clk_set_rate(clk, DUMMY_CLOCK_RATE_2),
  234. 0);
  235. rate = clk_get_rate(clk);
  236. KUNIT_ASSERT_GT(test, rate, 0);
  237. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
  238. clk_put(clk);
  239. }
  240. /*
  241. * Test that clk_round_rate and clk_set_rate are consistent and will
  242. * return the same frequency.
  243. */
  244. static void clk_test_round_set_get_rate(struct kunit *test)
  245. {
  246. struct clk_dummy_context *ctx = test->priv;
  247. struct clk_hw *hw = &ctx->hw;
  248. struct clk *clk = clk_hw_get_clk(hw, NULL);
  249. unsigned long set_rate;
  250. long rounded_rate;
  251. rounded_rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1);
  252. KUNIT_ASSERT_GT(test, rounded_rate, 0);
  253. KUNIT_EXPECT_EQ(test, rounded_rate, DUMMY_CLOCK_RATE_1);
  254. KUNIT_ASSERT_EQ(test,
  255. clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
  256. 0);
  257. set_rate = clk_get_rate(clk);
  258. KUNIT_ASSERT_GT(test, set_rate, 0);
  259. KUNIT_EXPECT_EQ(test, rounded_rate, set_rate);
  260. clk_put(clk);
  261. }
  262. static struct kunit_case clk_test_cases[] = {
  263. KUNIT_CASE(clk_test_get_rate),
  264. KUNIT_CASE(clk_test_set_get_rate),
  265. KUNIT_CASE(clk_test_set_set_get_rate),
  266. KUNIT_CASE(clk_test_round_set_get_rate),
  267. {}
  268. };
  269. /*
  270. * Test suite for a basic rate clock, without any parent.
  271. *
  272. * These tests exercise the rate API with simple scenarios
  273. */
  274. static struct kunit_suite clk_test_suite = {
  275. .name = "clk-test",
  276. .init = clk_test_init,
  277. .exit = clk_test_exit,
  278. .test_cases = clk_test_cases,
  279. };
  280. static int clk_uncached_test_init(struct kunit *test)
  281. {
  282. struct clk_dummy_context *ctx;
  283. int ret;
  284. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  285. if (!ctx)
  286. return -ENOMEM;
  287. test->priv = ctx;
  288. ctx->rate = DUMMY_CLOCK_INIT_RATE;
  289. ctx->hw.init = CLK_HW_INIT_NO_PARENT("test-clk",
  290. &clk_dummy_rate_ops,
  291. CLK_GET_RATE_NOCACHE);
  292. ret = clk_hw_register(NULL, &ctx->hw);
  293. if (ret)
  294. return ret;
  295. return 0;
  296. }
  297. /*
  298. * Test that for an uncached clock, the clock framework doesn't cache
  299. * the rate and clk_get_rate() will return the underlying clock rate
  300. * even if it changed.
  301. */
  302. static void clk_test_uncached_get_rate(struct kunit *test)
  303. {
  304. struct clk_dummy_context *ctx = test->priv;
  305. struct clk_hw *hw = &ctx->hw;
  306. struct clk *clk = clk_hw_get_clk(hw, NULL);
  307. unsigned long rate;
  308. rate = clk_get_rate(clk);
  309. KUNIT_ASSERT_GT(test, rate, 0);
  310. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
  311. /* We change the rate behind the clock framework's back */
  312. ctx->rate = DUMMY_CLOCK_RATE_1;
  313. rate = clk_get_rate(clk);
  314. KUNIT_ASSERT_GT(test, rate, 0);
  315. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
  316. clk_put(clk);
  317. }
  318. /*
  319. * Test that for an uncached clock, clk_set_rate_range() will work
  320. * properly if the rate hasn't changed.
  321. */
  322. static void clk_test_uncached_set_range(struct kunit *test)
  323. {
  324. struct clk_dummy_context *ctx = test->priv;
  325. struct clk_hw *hw = &ctx->hw;
  326. struct clk *clk = clk_hw_get_clk(hw, NULL);
  327. unsigned long rate;
  328. KUNIT_ASSERT_EQ(test,
  329. clk_set_rate_range(clk,
  330. DUMMY_CLOCK_RATE_1,
  331. DUMMY_CLOCK_RATE_2),
  332. 0);
  333. rate = clk_get_rate(clk);
  334. KUNIT_ASSERT_GT(test, rate, 0);
  335. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
  336. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
  337. clk_put(clk);
  338. }
  339. /*
  340. * Test that for an uncached clock, clk_set_rate_range() will work
  341. * properly if the rate has changed in hardware.
  342. *
  343. * In this case, it means that if the rate wasn't initially in the range
  344. * we're trying to set, but got changed at some point into the range
  345. * without the kernel knowing about it, its rate shouldn't be affected.
  346. */
  347. static void clk_test_uncached_updated_rate_set_range(struct kunit *test)
  348. {
  349. struct clk_dummy_context *ctx = test->priv;
  350. struct clk_hw *hw = &ctx->hw;
  351. struct clk *clk = clk_hw_get_clk(hw, NULL);
  352. unsigned long rate;
  353. /* We change the rate behind the clock framework's back */
  354. ctx->rate = DUMMY_CLOCK_RATE_1 + 1000;
  355. KUNIT_ASSERT_EQ(test,
  356. clk_set_rate_range(clk,
  357. DUMMY_CLOCK_RATE_1,
  358. DUMMY_CLOCK_RATE_2),
  359. 0);
  360. rate = clk_get_rate(clk);
  361. KUNIT_ASSERT_GT(test, rate, 0);
  362. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
  363. clk_put(clk);
  364. }
  365. static struct kunit_case clk_uncached_test_cases[] = {
  366. KUNIT_CASE(clk_test_uncached_get_rate),
  367. KUNIT_CASE(clk_test_uncached_set_range),
  368. KUNIT_CASE(clk_test_uncached_updated_rate_set_range),
  369. {}
  370. };
  371. /*
  372. * Test suite for a basic, uncached, rate clock, without any parent.
  373. *
  374. * These tests exercise the rate API with simple scenarios
  375. */
  376. static struct kunit_suite clk_uncached_test_suite = {
  377. .name = "clk-uncached-test",
  378. .init = clk_uncached_test_init,
  379. .exit = clk_test_exit,
  380. .test_cases = clk_uncached_test_cases,
  381. };
  382. static int
  383. clk_multiple_parents_mux_test_init(struct kunit *test)
  384. {
  385. struct clk_multiple_parent_ctx *ctx;
  386. const char *parents[2] = { "parent-0", "parent-1"};
  387. int ret;
  388. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  389. if (!ctx)
  390. return -ENOMEM;
  391. test->priv = ctx;
  392. ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
  393. &clk_dummy_rate_ops,
  394. 0);
  395. ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
  396. ret = clk_hw_register_kunit(test, NULL, &ctx->parents_ctx[0].hw);
  397. if (ret)
  398. return ret;
  399. ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
  400. &clk_dummy_rate_ops,
  401. 0);
  402. ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
  403. ret = clk_hw_register_kunit(test, NULL, &ctx->parents_ctx[1].hw);
  404. if (ret)
  405. return ret;
  406. ctx->current_parent = 0;
  407. ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
  408. &clk_multiple_parents_mux_ops,
  409. CLK_SET_RATE_PARENT);
  410. ret = clk_hw_register_kunit(test, NULL, &ctx->hw);
  411. if (ret)
  412. return ret;
  413. return 0;
  414. }
  415. /*
  416. * Test that for a clock with multiple parents, clk_get_parent()
  417. * actually returns the current one.
  418. */
  419. static void
  420. clk_test_multiple_parents_mux_get_parent(struct kunit *test)
  421. {
  422. struct clk_multiple_parent_ctx *ctx = test->priv;
  423. struct clk_hw *hw = &ctx->hw;
  424. struct clk *clk = clk_hw_get_clk(hw, NULL);
  425. struct clk *parent = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
  426. KUNIT_EXPECT_TRUE(test, clk_is_match(clk_get_parent(clk), parent));
  427. clk_put(parent);
  428. clk_put(clk);
  429. }
  430. /*
  431. * Test that for a clock with a multiple parents, clk_has_parent()
  432. * actually reports all of them as parents.
  433. */
  434. static void
  435. clk_test_multiple_parents_mux_has_parent(struct kunit *test)
  436. {
  437. struct clk_multiple_parent_ctx *ctx = test->priv;
  438. struct clk_hw *hw = &ctx->hw;
  439. struct clk *clk = clk_hw_get_clk(hw, NULL);
  440. struct clk *parent;
  441. parent = clk_hw_get_clk(&ctx->parents_ctx[0].hw, NULL);
  442. KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
  443. clk_put(parent);
  444. parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
  445. KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
  446. clk_put(parent);
  447. clk_put(clk);
  448. }
  449. /*
  450. * Test that for a clock with a multiple parents, if we set a range on
  451. * that clock and the parent is changed, its rate after the reparenting
  452. * is still within the range we asked for.
  453. *
  454. * FIXME: clk_set_parent() only does the reparenting but doesn't
  455. * reevaluate whether the new clock rate is within its boundaries or
  456. * not.
  457. */
  458. static void
  459. clk_test_multiple_parents_mux_set_range_set_parent_get_rate(struct kunit *test)
  460. {
  461. struct clk_multiple_parent_ctx *ctx = test->priv;
  462. struct clk_hw *hw = &ctx->hw;
  463. struct clk *clk = clk_hw_get_clk_kunit(test, hw, NULL);
  464. struct clk *parent1, *parent2;
  465. unsigned long rate;
  466. int ret;
  467. kunit_skip(test, "This needs to be fixed in the core.");
  468. parent1 = clk_hw_get_clk_kunit(test, &ctx->parents_ctx[0].hw, NULL);
  469. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent1);
  470. KUNIT_ASSERT_TRUE(test, clk_is_match(clk_get_parent(clk), parent1));
  471. parent2 = clk_hw_get_clk_kunit(test, &ctx->parents_ctx[1].hw, NULL);
  472. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent2);
  473. ret = clk_set_rate(parent1, DUMMY_CLOCK_RATE_1);
  474. KUNIT_ASSERT_EQ(test, ret, 0);
  475. ret = clk_set_rate(parent2, DUMMY_CLOCK_RATE_2);
  476. KUNIT_ASSERT_EQ(test, ret, 0);
  477. ret = clk_set_rate_range(clk,
  478. DUMMY_CLOCK_RATE_1 - 1000,
  479. DUMMY_CLOCK_RATE_1 + 1000);
  480. KUNIT_ASSERT_EQ(test, ret, 0);
  481. ret = clk_set_parent(clk, parent2);
  482. KUNIT_ASSERT_EQ(test, ret, 0);
  483. rate = clk_get_rate(clk);
  484. KUNIT_ASSERT_GT(test, rate, 0);
  485. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 - 1000);
  486. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
  487. }
  488. static struct kunit_case clk_multiple_parents_mux_test_cases[] = {
  489. KUNIT_CASE(clk_test_multiple_parents_mux_get_parent),
  490. KUNIT_CASE(clk_test_multiple_parents_mux_has_parent),
  491. KUNIT_CASE(clk_test_multiple_parents_mux_set_range_set_parent_get_rate),
  492. {}
  493. };
  494. /*
  495. * Test suite for a basic mux clock with two parents, with
  496. * CLK_SET_RATE_PARENT on the child.
  497. *
  498. * These tests exercise the consumer API and check that the state of the
  499. * child and parents are sane and consistent.
  500. */
  501. static struct kunit_suite
  502. clk_multiple_parents_mux_test_suite = {
  503. .name = "clk-multiple-parents-mux-test",
  504. .init = clk_multiple_parents_mux_test_init,
  505. .test_cases = clk_multiple_parents_mux_test_cases,
  506. };
  507. static int
  508. clk_orphan_transparent_multiple_parent_mux_test_init(struct kunit *test)
  509. {
  510. struct clk_multiple_parent_ctx *ctx;
  511. const char *parents[2] = { "missing-parent", "proper-parent"};
  512. int ret;
  513. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  514. if (!ctx)
  515. return -ENOMEM;
  516. test->priv = ctx;
  517. ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("proper-parent",
  518. &clk_dummy_rate_ops,
  519. 0);
  520. ctx->parents_ctx[1].rate = DUMMY_CLOCK_INIT_RATE;
  521. ret = clk_hw_register_kunit(test, NULL, &ctx->parents_ctx[1].hw);
  522. if (ret)
  523. return ret;
  524. ctx->hw.init = CLK_HW_INIT_PARENTS("test-orphan-mux", parents,
  525. &clk_multiple_parents_mux_ops,
  526. CLK_SET_RATE_PARENT);
  527. ret = clk_hw_register_kunit(test, NULL, &ctx->hw);
  528. if (ret)
  529. return ret;
  530. return 0;
  531. }
  532. /*
  533. * Test that, for a mux whose current parent hasn't been registered yet and is
  534. * thus orphan, clk_get_parent() will return NULL.
  535. */
  536. static void
  537. clk_test_orphan_transparent_multiple_parent_mux_get_parent(struct kunit *test)
  538. {
  539. struct clk_multiple_parent_ctx *ctx = test->priv;
  540. struct clk_hw *hw = &ctx->hw;
  541. struct clk *clk = clk_hw_get_clk(hw, NULL);
  542. KUNIT_EXPECT_PTR_EQ(test, clk_get_parent(clk), NULL);
  543. clk_put(clk);
  544. }
  545. /*
  546. * Test that, for a mux whose current parent hasn't been registered yet,
  547. * calling clk_set_parent() to a valid parent will properly update the
  548. * mux parent and its orphan status.
  549. */
  550. static void
  551. clk_test_orphan_transparent_multiple_parent_mux_set_parent(struct kunit *test)
  552. {
  553. struct clk_multiple_parent_ctx *ctx = test->priv;
  554. struct clk_hw *hw = &ctx->hw;
  555. struct clk *clk = clk_hw_get_clk(hw, NULL);
  556. struct clk *parent, *new_parent;
  557. int ret;
  558. parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
  559. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
  560. ret = clk_set_parent(clk, parent);
  561. KUNIT_ASSERT_EQ(test, ret, 0);
  562. new_parent = clk_get_parent(clk);
  563. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
  564. KUNIT_EXPECT_TRUE(test, clk_is_match(parent, new_parent));
  565. clk_put(parent);
  566. clk_put(clk);
  567. }
  568. /*
  569. * Test that, for a mux that started orphan but got switched to a valid
  570. * parent, calling clk_drop_range() on the mux won't affect the parent
  571. * rate.
  572. */
  573. static void
  574. clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range(struct kunit *test)
  575. {
  576. struct clk_multiple_parent_ctx *ctx = test->priv;
  577. struct clk_hw *hw = &ctx->hw;
  578. struct clk *clk = clk_hw_get_clk(hw, NULL);
  579. struct clk *parent;
  580. unsigned long parent_rate, new_parent_rate;
  581. int ret;
  582. parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
  583. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
  584. parent_rate = clk_get_rate(parent);
  585. KUNIT_ASSERT_GT(test, parent_rate, 0);
  586. ret = clk_set_parent(clk, parent);
  587. KUNIT_ASSERT_EQ(test, ret, 0);
  588. ret = clk_drop_range(clk);
  589. KUNIT_ASSERT_EQ(test, ret, 0);
  590. new_parent_rate = clk_get_rate(clk);
  591. KUNIT_ASSERT_GT(test, new_parent_rate, 0);
  592. KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
  593. clk_put(parent);
  594. clk_put(clk);
  595. }
  596. /*
  597. * Test that, for a mux that started orphan but got switched to a valid
  598. * parent, the rate of the mux and its new parent are consistent.
  599. */
  600. static void
  601. clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate(struct kunit *test)
  602. {
  603. struct clk_multiple_parent_ctx *ctx = test->priv;
  604. struct clk_hw *hw = &ctx->hw;
  605. struct clk *clk = clk_hw_get_clk(hw, NULL);
  606. struct clk *parent;
  607. unsigned long parent_rate, rate;
  608. int ret;
  609. parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
  610. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
  611. parent_rate = clk_get_rate(parent);
  612. KUNIT_ASSERT_GT(test, parent_rate, 0);
  613. ret = clk_set_parent(clk, parent);
  614. KUNIT_ASSERT_EQ(test, ret, 0);
  615. rate = clk_get_rate(clk);
  616. KUNIT_ASSERT_GT(test, rate, 0);
  617. KUNIT_EXPECT_EQ(test, parent_rate, rate);
  618. clk_put(parent);
  619. clk_put(clk);
  620. }
  621. /*
  622. * Test that, for a mux that started orphan but got switched to a valid
  623. * parent, calling clk_put() on the mux won't affect the parent rate.
  624. */
  625. static void
  626. clk_test_orphan_transparent_multiple_parent_mux_set_parent_put(struct kunit *test)
  627. {
  628. struct clk_multiple_parent_ctx *ctx = test->priv;
  629. struct clk *clk, *parent;
  630. unsigned long parent_rate, new_parent_rate;
  631. int ret;
  632. parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
  633. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
  634. clk = clk_hw_get_clk(&ctx->hw, NULL);
  635. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
  636. parent_rate = clk_get_rate(parent);
  637. KUNIT_ASSERT_GT(test, parent_rate, 0);
  638. ret = clk_set_parent(clk, parent);
  639. KUNIT_ASSERT_EQ(test, ret, 0);
  640. clk_put(clk);
  641. new_parent_rate = clk_get_rate(parent);
  642. KUNIT_ASSERT_GT(test, new_parent_rate, 0);
  643. KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
  644. clk_put(parent);
  645. }
  646. /*
  647. * Test that, for a mux that started orphan but got switched to a valid
  648. * parent, calling clk_set_rate_range() will affect the parent state if
  649. * its rate is out of range.
  650. */
  651. static void
  652. clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified(struct kunit *test)
  653. {
  654. struct clk_multiple_parent_ctx *ctx = test->priv;
  655. struct clk_hw *hw = &ctx->hw;
  656. struct clk *clk = clk_hw_get_clk(hw, NULL);
  657. struct clk *parent;
  658. unsigned long rate;
  659. int ret;
  660. parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
  661. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
  662. ret = clk_set_parent(clk, parent);
  663. KUNIT_ASSERT_EQ(test, ret, 0);
  664. ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
  665. KUNIT_ASSERT_EQ(test, ret, 0);
  666. rate = clk_get_rate(clk);
  667. KUNIT_ASSERT_GT(test, rate, 0);
  668. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
  669. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
  670. clk_put(parent);
  671. clk_put(clk);
  672. }
  673. /*
  674. * Test that, for a mux that started orphan but got switched to a valid
  675. * parent, calling clk_set_rate_range() won't affect the parent state if
  676. * its rate is within range.
  677. */
  678. static void
  679. clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched(struct kunit *test)
  680. {
  681. struct clk_multiple_parent_ctx *ctx = test->priv;
  682. struct clk_hw *hw = &ctx->hw;
  683. struct clk *clk = clk_hw_get_clk(hw, NULL);
  684. struct clk *parent;
  685. unsigned long parent_rate, new_parent_rate;
  686. int ret;
  687. parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
  688. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
  689. parent_rate = clk_get_rate(parent);
  690. KUNIT_ASSERT_GT(test, parent_rate, 0);
  691. ret = clk_set_parent(clk, parent);
  692. KUNIT_ASSERT_EQ(test, ret, 0);
  693. ret = clk_set_rate_range(clk,
  694. DUMMY_CLOCK_INIT_RATE - 1000,
  695. DUMMY_CLOCK_INIT_RATE + 1000);
  696. KUNIT_ASSERT_EQ(test, ret, 0);
  697. new_parent_rate = clk_get_rate(parent);
  698. KUNIT_ASSERT_GT(test, new_parent_rate, 0);
  699. KUNIT_EXPECT_EQ(test, parent_rate, new_parent_rate);
  700. clk_put(parent);
  701. clk_put(clk);
  702. }
  703. /*
  704. * Test that, for a mux whose current parent hasn't been registered yet,
  705. * calling clk_set_rate_range() will succeed, and will be taken into
  706. * account when rounding a rate.
  707. */
  708. static void
  709. clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate(struct kunit *test)
  710. {
  711. struct clk_multiple_parent_ctx *ctx = test->priv;
  712. struct clk_hw *hw = &ctx->hw;
  713. struct clk *clk = clk_hw_get_clk(hw, NULL);
  714. long rate;
  715. int ret;
  716. ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
  717. KUNIT_ASSERT_EQ(test, ret, 0);
  718. rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
  719. KUNIT_ASSERT_GT(test, rate, 0);
  720. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
  721. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
  722. clk_put(clk);
  723. }
  724. /*
  725. * Test that, for a mux that started orphan, was assigned and rate and
  726. * then got switched to a valid parent, its rate is eventually within
  727. * range.
  728. *
  729. * FIXME: Even though we update the rate as part of clk_set_parent(), we
  730. * don't evaluate whether that new rate is within range and needs to be
  731. * adjusted.
  732. */
  733. static void
  734. clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate(struct kunit *test)
  735. {
  736. struct clk_multiple_parent_ctx *ctx = test->priv;
  737. struct clk_hw *hw = &ctx->hw;
  738. struct clk *clk = clk_hw_get_clk_kunit(test, hw, NULL);
  739. struct clk *parent;
  740. unsigned long rate;
  741. int ret;
  742. kunit_skip(test, "This needs to be fixed in the core.");
  743. clk_hw_set_rate_range(hw, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
  744. parent = clk_hw_get_clk_kunit(test, &ctx->parents_ctx[1].hw, NULL);
  745. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
  746. ret = clk_set_parent(clk, parent);
  747. KUNIT_ASSERT_EQ(test, ret, 0);
  748. rate = clk_get_rate(clk);
  749. KUNIT_ASSERT_GT(test, rate, 0);
  750. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
  751. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
  752. }
  753. static struct kunit_case clk_orphan_transparent_multiple_parent_mux_test_cases[] = {
  754. KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_get_parent),
  755. KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent),
  756. KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range),
  757. KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate),
  758. KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_put),
  759. KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified),
  760. KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched),
  761. KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate),
  762. KUNIT_CASE(clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate),
  763. {}
  764. };
  765. /*
  766. * Test suite for a basic mux clock with two parents. The default parent
  767. * isn't registered, only the second parent is. By default, the clock
  768. * will thus be orphan.
  769. *
  770. * These tests exercise the behaviour of the consumer API when dealing
  771. * with an orphan clock, and how we deal with the transition to a valid
  772. * parent.
  773. */
  774. static struct kunit_suite clk_orphan_transparent_multiple_parent_mux_test_suite = {
  775. .name = "clk-orphan-transparent-multiple-parent-mux-test",
  776. .init = clk_orphan_transparent_multiple_parent_mux_test_init,
  777. .test_cases = clk_orphan_transparent_multiple_parent_mux_test_cases,
  778. };
  779. struct clk_single_parent_ctx {
  780. struct clk_dummy_context parent_ctx;
  781. struct clk_hw hw;
  782. };
  783. static int clk_single_parent_mux_test_init(struct kunit *test)
  784. {
  785. struct clk_single_parent_ctx *ctx;
  786. int ret;
  787. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  788. if (!ctx)
  789. return -ENOMEM;
  790. test->priv = ctx;
  791. ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
  792. ctx->parent_ctx.hw.init =
  793. CLK_HW_INIT_NO_PARENT("parent-clk",
  794. &clk_dummy_rate_ops,
  795. 0);
  796. ret = clk_hw_register_kunit(test, NULL, &ctx->parent_ctx.hw);
  797. if (ret)
  798. return ret;
  799. ctx->hw.init = CLK_HW_INIT("test-clk", "parent-clk",
  800. &clk_dummy_single_parent_ops,
  801. CLK_SET_RATE_PARENT);
  802. ret = clk_hw_register_kunit(test, NULL, &ctx->hw);
  803. if (ret)
  804. return ret;
  805. return 0;
  806. }
  807. static void
  808. clk_single_parent_mux_test_exit(struct kunit *test)
  809. {
  810. struct clk_single_parent_ctx *ctx = test->priv;
  811. clk_hw_unregister(&ctx->hw);
  812. clk_hw_unregister(&ctx->parent_ctx.hw);
  813. }
  814. /*
  815. * Test that for a clock with a single parent, clk_get_parent() actually
  816. * returns the parent.
  817. */
  818. static void
  819. clk_test_single_parent_mux_get_parent(struct kunit *test)
  820. {
  821. struct clk_single_parent_ctx *ctx = test->priv;
  822. struct clk_hw *hw = &ctx->hw;
  823. struct clk *clk = clk_hw_get_clk(hw, NULL);
  824. struct clk *parent = clk_hw_get_clk(&ctx->parent_ctx.hw, NULL);
  825. KUNIT_EXPECT_TRUE(test, clk_is_match(clk_get_parent(clk), parent));
  826. clk_put(parent);
  827. clk_put(clk);
  828. }
  829. /*
  830. * Test that for a clock with a single parent, clk_has_parent() actually
  831. * reports it as a parent.
  832. */
  833. static void
  834. clk_test_single_parent_mux_has_parent(struct kunit *test)
  835. {
  836. struct clk_single_parent_ctx *ctx = test->priv;
  837. struct clk_hw *hw = &ctx->hw;
  838. struct clk *clk = clk_hw_get_clk(hw, NULL);
  839. struct clk *parent = clk_hw_get_clk(&ctx->parent_ctx.hw, NULL);
  840. KUNIT_EXPECT_TRUE(test, clk_has_parent(clk, parent));
  841. clk_put(parent);
  842. clk_put(clk);
  843. }
  844. /*
  845. * Test that for a clock that can't modify its rate and with a single
  846. * parent, if we set disjoints range on the parent and then the child,
  847. * the second will return an error.
  848. *
  849. * FIXME: clk_set_rate_range() only considers the current clock when
  850. * evaluating whether ranges are disjoints and not the upstream clocks
  851. * ranges.
  852. */
  853. static void
  854. clk_test_single_parent_mux_set_range_disjoint_child_last(struct kunit *test)
  855. {
  856. struct clk_single_parent_ctx *ctx = test->priv;
  857. struct clk_hw *hw = &ctx->hw;
  858. struct clk *clk = clk_hw_get_clk_kunit(test, hw, NULL);
  859. struct clk *parent;
  860. int ret;
  861. kunit_skip(test, "This needs to be fixed in the core.");
  862. parent = clk_get_parent(clk);
  863. KUNIT_ASSERT_PTR_NE(test, parent, NULL);
  864. ret = clk_set_rate_range(parent, 1000, 2000);
  865. KUNIT_ASSERT_EQ(test, ret, 0);
  866. ret = clk_set_rate_range(clk, 3000, 4000);
  867. KUNIT_EXPECT_LT(test, ret, 0);
  868. }
  869. /*
  870. * Test that for a clock that can't modify its rate and with a single
  871. * parent, if we set disjoints range on the child and then the parent,
  872. * the second will return an error.
  873. *
  874. * FIXME: clk_set_rate_range() only considers the current clock when
  875. * evaluating whether ranges are disjoints and not the downstream clocks
  876. * ranges.
  877. */
  878. static void
  879. clk_test_single_parent_mux_set_range_disjoint_parent_last(struct kunit *test)
  880. {
  881. struct clk_single_parent_ctx *ctx = test->priv;
  882. struct clk_hw *hw = &ctx->hw;
  883. struct clk *clk = clk_hw_get_clk_kunit(test, hw, NULL);
  884. struct clk *parent;
  885. int ret;
  886. kunit_skip(test, "This needs to be fixed in the core.");
  887. parent = clk_get_parent(clk);
  888. KUNIT_ASSERT_PTR_NE(test, parent, NULL);
  889. ret = clk_set_rate_range(clk, 1000, 2000);
  890. KUNIT_ASSERT_EQ(test, ret, 0);
  891. ret = clk_set_rate_range(parent, 3000, 4000);
  892. KUNIT_EXPECT_LT(test, ret, 0);
  893. }
  894. /*
  895. * Test that for a clock that can't modify its rate and with a single
  896. * parent, if we set a range on the parent and then call
  897. * clk_round_rate(), the boundaries of the parent are taken into
  898. * account.
  899. */
  900. static void
  901. clk_test_single_parent_mux_set_range_round_rate_parent_only(struct kunit *test)
  902. {
  903. struct clk_single_parent_ctx *ctx = test->priv;
  904. struct clk_hw *hw = &ctx->hw;
  905. struct clk *clk = clk_hw_get_clk(hw, NULL);
  906. struct clk *parent;
  907. long rate;
  908. int ret;
  909. parent = clk_get_parent(clk);
  910. KUNIT_ASSERT_PTR_NE(test, parent, NULL);
  911. ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
  912. KUNIT_ASSERT_EQ(test, ret, 0);
  913. rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
  914. KUNIT_ASSERT_GT(test, rate, 0);
  915. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
  916. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
  917. clk_put(clk);
  918. }
  919. /*
  920. * Test that for a clock that can't modify its rate and with a single
  921. * parent, if we set a range on the parent and a more restrictive one on
  922. * the child, and then call clk_round_rate(), the boundaries of the
  923. * two clocks are taken into account.
  924. */
  925. static void
  926. clk_test_single_parent_mux_set_range_round_rate_child_smaller(struct kunit *test)
  927. {
  928. struct clk_single_parent_ctx *ctx = test->priv;
  929. struct clk_hw *hw = &ctx->hw;
  930. struct clk *clk = clk_hw_get_clk(hw, NULL);
  931. struct clk *parent;
  932. long rate;
  933. int ret;
  934. parent = clk_get_parent(clk);
  935. KUNIT_ASSERT_PTR_NE(test, parent, NULL);
  936. ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
  937. KUNIT_ASSERT_EQ(test, ret, 0);
  938. ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
  939. KUNIT_ASSERT_EQ(test, ret, 0);
  940. rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
  941. KUNIT_ASSERT_GT(test, rate, 0);
  942. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
  943. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
  944. rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
  945. KUNIT_ASSERT_GT(test, rate, 0);
  946. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
  947. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
  948. clk_put(clk);
  949. }
  950. /*
  951. * Test that for a clock that can't modify its rate and with a single
  952. * parent, if we set a range on the child and a more restrictive one on
  953. * the parent, and then call clk_round_rate(), the boundaries of the
  954. * two clocks are taken into account.
  955. */
  956. static void
  957. clk_test_single_parent_mux_set_range_round_rate_parent_smaller(struct kunit *test)
  958. {
  959. struct clk_single_parent_ctx *ctx = test->priv;
  960. struct clk_hw *hw = &ctx->hw;
  961. struct clk *clk = clk_hw_get_clk(hw, NULL);
  962. struct clk *parent;
  963. long rate;
  964. int ret;
  965. parent = clk_get_parent(clk);
  966. KUNIT_ASSERT_PTR_NE(test, parent, NULL);
  967. ret = clk_set_rate_range(parent, DUMMY_CLOCK_RATE_1 + 1000, DUMMY_CLOCK_RATE_2 - 1000);
  968. KUNIT_ASSERT_EQ(test, ret, 0);
  969. ret = clk_set_rate_range(clk, DUMMY_CLOCK_RATE_1, DUMMY_CLOCK_RATE_2);
  970. KUNIT_ASSERT_EQ(test, ret, 0);
  971. rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
  972. KUNIT_ASSERT_GT(test, rate, 0);
  973. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
  974. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
  975. rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
  976. KUNIT_ASSERT_GT(test, rate, 0);
  977. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
  978. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
  979. clk_put(clk);
  980. }
  981. static struct kunit_case clk_single_parent_mux_test_cases[] = {
  982. KUNIT_CASE(clk_test_single_parent_mux_get_parent),
  983. KUNIT_CASE(clk_test_single_parent_mux_has_parent),
  984. KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_child_last),
  985. KUNIT_CASE(clk_test_single_parent_mux_set_range_disjoint_parent_last),
  986. KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_child_smaller),
  987. KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_only),
  988. KUNIT_CASE(clk_test_single_parent_mux_set_range_round_rate_parent_smaller),
  989. {}
  990. };
  991. /*
  992. * Test suite for a basic mux clock with one parent, with
  993. * CLK_SET_RATE_PARENT on the child.
  994. *
  995. * These tests exercise the consumer API and check that the state of the
  996. * child and parent are sane and consistent.
  997. */
  998. static struct kunit_suite
  999. clk_single_parent_mux_test_suite = {
  1000. .name = "clk-single-parent-mux-test",
  1001. .init = clk_single_parent_mux_test_init,
  1002. .test_cases = clk_single_parent_mux_test_cases,
  1003. };
  1004. static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test)
  1005. {
  1006. struct clk_single_parent_ctx *ctx;
  1007. struct clk_init_data init = { };
  1008. const char * const parents[] = { "orphan_parent" };
  1009. int ret;
  1010. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  1011. if (!ctx)
  1012. return -ENOMEM;
  1013. test->priv = ctx;
  1014. init.name = "test_orphan_dummy_parent";
  1015. init.ops = &clk_dummy_single_parent_ops;
  1016. init.parent_names = parents;
  1017. init.num_parents = ARRAY_SIZE(parents);
  1018. init.flags = CLK_SET_RATE_PARENT;
  1019. ctx->hw.init = &init;
  1020. ret = clk_hw_register(NULL, &ctx->hw);
  1021. if (ret)
  1022. return ret;
  1023. memset(&init, 0, sizeof(init));
  1024. init.name = "orphan_parent";
  1025. init.ops = &clk_dummy_rate_ops;
  1026. ctx->parent_ctx.hw.init = &init;
  1027. ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
  1028. ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
  1029. if (ret)
  1030. return ret;
  1031. return 0;
  1032. }
  1033. /*
  1034. * Test that a mux-only clock, with an initial rate within a range,
  1035. * will still have the same rate after the range has been enforced.
  1036. *
  1037. * See:
  1038. * https://lore.kernel.org/linux-clk/7720158d-10a7-a17b-73a4-a8615c9c6d5c@collabora.com/
  1039. */
  1040. static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit *test)
  1041. {
  1042. struct clk_single_parent_ctx *ctx = test->priv;
  1043. struct clk_hw *hw = &ctx->hw;
  1044. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1045. unsigned long rate, new_rate;
  1046. rate = clk_get_rate(clk);
  1047. KUNIT_ASSERT_GT(test, rate, 0);
  1048. KUNIT_ASSERT_EQ(test,
  1049. clk_set_rate_range(clk,
  1050. ctx->parent_ctx.rate - 1000,
  1051. ctx->parent_ctx.rate + 1000),
  1052. 0);
  1053. new_rate = clk_get_rate(clk);
  1054. KUNIT_ASSERT_GT(test, new_rate, 0);
  1055. KUNIT_EXPECT_EQ(test, rate, new_rate);
  1056. clk_put(clk);
  1057. }
  1058. static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] = {
  1059. KUNIT_CASE(clk_test_orphan_transparent_parent_mux_set_range),
  1060. {}
  1061. };
  1062. /*
  1063. * Test suite for a basic mux clock with one parent. The parent is
  1064. * registered after its child. The clock will thus be an orphan when
  1065. * registered, but will no longer be when the tests run.
  1066. *
  1067. * These tests make sure a clock that used to be orphan has a sane,
  1068. * consistent, behaviour.
  1069. */
  1070. static struct kunit_suite clk_orphan_transparent_single_parent_test_suite = {
  1071. .name = "clk-orphan-transparent-single-parent-test",
  1072. .init = clk_orphan_transparent_single_parent_mux_test_init,
  1073. .exit = clk_single_parent_mux_test_exit,
  1074. .test_cases = clk_orphan_transparent_single_parent_mux_test_cases,
  1075. };
  1076. struct clk_single_parent_two_lvl_ctx {
  1077. struct clk_dummy_context parent_parent_ctx;
  1078. struct clk_dummy_context parent_ctx;
  1079. struct clk_hw hw;
  1080. };
  1081. static int
  1082. clk_orphan_two_level_root_last_test_init(struct kunit *test)
  1083. {
  1084. struct clk_single_parent_two_lvl_ctx *ctx;
  1085. int ret;
  1086. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  1087. if (!ctx)
  1088. return -ENOMEM;
  1089. test->priv = ctx;
  1090. ctx->parent_ctx.hw.init =
  1091. CLK_HW_INIT("intermediate-parent",
  1092. "root-parent",
  1093. &clk_dummy_single_parent_ops,
  1094. CLK_SET_RATE_PARENT);
  1095. ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
  1096. if (ret)
  1097. return ret;
  1098. ctx->hw.init =
  1099. CLK_HW_INIT("test-clk", "intermediate-parent",
  1100. &clk_dummy_single_parent_ops,
  1101. CLK_SET_RATE_PARENT);
  1102. ret = clk_hw_register(NULL, &ctx->hw);
  1103. if (ret)
  1104. return ret;
  1105. ctx->parent_parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
  1106. ctx->parent_parent_ctx.hw.init =
  1107. CLK_HW_INIT_NO_PARENT("root-parent",
  1108. &clk_dummy_rate_ops,
  1109. 0);
  1110. ret = clk_hw_register(NULL, &ctx->parent_parent_ctx.hw);
  1111. if (ret)
  1112. return ret;
  1113. return 0;
  1114. }
  1115. static void
  1116. clk_orphan_two_level_root_last_test_exit(struct kunit *test)
  1117. {
  1118. struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
  1119. clk_hw_unregister(&ctx->hw);
  1120. clk_hw_unregister(&ctx->parent_ctx.hw);
  1121. clk_hw_unregister(&ctx->parent_parent_ctx.hw);
  1122. }
  1123. /*
  1124. * Test that, for a clock whose parent used to be orphan, clk_get_rate()
  1125. * will return the proper rate.
  1126. */
  1127. static void
  1128. clk_orphan_two_level_root_last_test_get_rate(struct kunit *test)
  1129. {
  1130. struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
  1131. struct clk_hw *hw = &ctx->hw;
  1132. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1133. unsigned long rate;
  1134. rate = clk_get_rate(clk);
  1135. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
  1136. clk_put(clk);
  1137. }
  1138. /*
  1139. * Test that, for a clock whose parent used to be orphan,
  1140. * clk_set_rate_range() won't affect its rate if it is already within
  1141. * range.
  1142. *
  1143. * See (for Exynos 4210):
  1144. * https://lore.kernel.org/linux-clk/366a0232-bb4a-c357-6aa8-636e398e05eb@samsung.com/
  1145. */
  1146. static void
  1147. clk_orphan_two_level_root_last_test_set_range(struct kunit *test)
  1148. {
  1149. struct clk_single_parent_two_lvl_ctx *ctx = test->priv;
  1150. struct clk_hw *hw = &ctx->hw;
  1151. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1152. unsigned long rate;
  1153. int ret;
  1154. ret = clk_set_rate_range(clk,
  1155. DUMMY_CLOCK_INIT_RATE - 1000,
  1156. DUMMY_CLOCK_INIT_RATE + 1000);
  1157. KUNIT_ASSERT_EQ(test, ret, 0);
  1158. rate = clk_get_rate(clk);
  1159. KUNIT_ASSERT_GT(test, rate, 0);
  1160. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_INIT_RATE);
  1161. clk_put(clk);
  1162. }
  1163. static struct kunit_case
  1164. clk_orphan_two_level_root_last_test_cases[] = {
  1165. KUNIT_CASE(clk_orphan_two_level_root_last_test_get_rate),
  1166. KUNIT_CASE(clk_orphan_two_level_root_last_test_set_range),
  1167. {}
  1168. };
  1169. /*
  1170. * Test suite for a basic, transparent, clock with a parent that is also
  1171. * such a clock. The parent's parent is registered last, while the
  1172. * parent and its child are registered in that order. The intermediate
  1173. * and leaf clocks will thus be orphan when registered, but the leaf
  1174. * clock itself will always have its parent and will never be
  1175. * reparented. Indeed, it's only orphan because its parent is.
  1176. *
  1177. * These tests exercise the behaviour of the consumer API when dealing
  1178. * with an orphan clock, and how we deal with the transition to a valid
  1179. * parent.
  1180. */
  1181. static struct kunit_suite
  1182. clk_orphan_two_level_root_last_test_suite = {
  1183. .name = "clk-orphan-two-level-root-last-test",
  1184. .init = clk_orphan_two_level_root_last_test_init,
  1185. .exit = clk_orphan_two_level_root_last_test_exit,
  1186. .test_cases = clk_orphan_two_level_root_last_test_cases,
  1187. };
  1188. /*
  1189. * Test that clk_set_rate_range won't return an error for a valid range
  1190. * and that it will make sure the rate of the clock is within the
  1191. * boundaries.
  1192. */
  1193. static void clk_range_test_set_range(struct kunit *test)
  1194. {
  1195. struct clk_dummy_context *ctx = test->priv;
  1196. struct clk_hw *hw = &ctx->hw;
  1197. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1198. unsigned long rate;
  1199. KUNIT_ASSERT_EQ(test,
  1200. clk_set_rate_range(clk,
  1201. DUMMY_CLOCK_RATE_1,
  1202. DUMMY_CLOCK_RATE_2),
  1203. 0);
  1204. rate = clk_get_rate(clk);
  1205. KUNIT_ASSERT_GT(test, rate, 0);
  1206. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
  1207. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
  1208. clk_put(clk);
  1209. }
  1210. /*
  1211. * Test that calling clk_set_rate_range with a minimum rate higher than
  1212. * the maximum rate returns an error.
  1213. */
  1214. static void clk_range_test_set_range_invalid(struct kunit *test)
  1215. {
  1216. struct clk_dummy_context *ctx = test->priv;
  1217. struct clk_hw *hw = &ctx->hw;
  1218. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1219. KUNIT_EXPECT_LT(test,
  1220. clk_set_rate_range(clk,
  1221. DUMMY_CLOCK_RATE_1 + 1000,
  1222. DUMMY_CLOCK_RATE_1),
  1223. 0);
  1224. clk_put(clk);
  1225. }
  1226. /*
  1227. * Test that users can't set multiple, disjoints, range that would be
  1228. * impossible to meet.
  1229. */
  1230. static void clk_range_test_multiple_disjoints_range(struct kunit *test)
  1231. {
  1232. struct clk_dummy_context *ctx = test->priv;
  1233. struct clk_hw *hw = &ctx->hw;
  1234. struct clk *user1, *user2;
  1235. user1 = clk_hw_get_clk(hw, NULL);
  1236. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
  1237. user2 = clk_hw_get_clk(hw, NULL);
  1238. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
  1239. KUNIT_ASSERT_EQ(test,
  1240. clk_set_rate_range(user1, 1000, 2000),
  1241. 0);
  1242. KUNIT_EXPECT_LT(test,
  1243. clk_set_rate_range(user2, 3000, 4000),
  1244. 0);
  1245. clk_put(user2);
  1246. clk_put(user1);
  1247. }
  1248. /*
  1249. * Test that if our clock has some boundaries and we try to round a rate
  1250. * lower than the minimum, the returned rate will be within range.
  1251. */
  1252. static void clk_range_test_set_range_round_rate_lower(struct kunit *test)
  1253. {
  1254. struct clk_dummy_context *ctx = test->priv;
  1255. struct clk_hw *hw = &ctx->hw;
  1256. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1257. long rate;
  1258. KUNIT_ASSERT_EQ(test,
  1259. clk_set_rate_range(clk,
  1260. DUMMY_CLOCK_RATE_1,
  1261. DUMMY_CLOCK_RATE_2),
  1262. 0);
  1263. rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
  1264. KUNIT_ASSERT_GT(test, rate, 0);
  1265. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
  1266. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
  1267. clk_put(clk);
  1268. }
  1269. /*
  1270. * Test that if our clock has some boundaries and we try to set a rate
  1271. * higher than the maximum, the new rate will be within range.
  1272. */
  1273. static void clk_range_test_set_range_set_rate_lower(struct kunit *test)
  1274. {
  1275. struct clk_dummy_context *ctx = test->priv;
  1276. struct clk_hw *hw = &ctx->hw;
  1277. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1278. unsigned long rate;
  1279. KUNIT_ASSERT_EQ(test,
  1280. clk_set_rate_range(clk,
  1281. DUMMY_CLOCK_RATE_1,
  1282. DUMMY_CLOCK_RATE_2),
  1283. 0);
  1284. KUNIT_ASSERT_EQ(test,
  1285. clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
  1286. 0);
  1287. rate = clk_get_rate(clk);
  1288. KUNIT_ASSERT_GT(test, rate, 0);
  1289. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
  1290. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
  1291. clk_put(clk);
  1292. }
  1293. /*
  1294. * Test that if our clock has some boundaries and we try to round and
  1295. * set a rate lower than the minimum, the rate returned by
  1296. * clk_round_rate() will be consistent with the new rate set by
  1297. * clk_set_rate().
  1298. */
  1299. static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit *test)
  1300. {
  1301. struct clk_dummy_context *ctx = test->priv;
  1302. struct clk_hw *hw = &ctx->hw;
  1303. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1304. long rounded;
  1305. KUNIT_ASSERT_EQ(test,
  1306. clk_set_rate_range(clk,
  1307. DUMMY_CLOCK_RATE_1,
  1308. DUMMY_CLOCK_RATE_2),
  1309. 0);
  1310. rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
  1311. KUNIT_ASSERT_GT(test, rounded, 0);
  1312. KUNIT_ASSERT_EQ(test,
  1313. clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
  1314. 0);
  1315. KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
  1316. clk_put(clk);
  1317. }
  1318. /*
  1319. * Test that if our clock has some boundaries and we try to round a rate
  1320. * higher than the maximum, the returned rate will be within range.
  1321. */
  1322. static void clk_range_test_set_range_round_rate_higher(struct kunit *test)
  1323. {
  1324. struct clk_dummy_context *ctx = test->priv;
  1325. struct clk_hw *hw = &ctx->hw;
  1326. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1327. long rate;
  1328. KUNIT_ASSERT_EQ(test,
  1329. clk_set_rate_range(clk,
  1330. DUMMY_CLOCK_RATE_1,
  1331. DUMMY_CLOCK_RATE_2),
  1332. 0);
  1333. rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
  1334. KUNIT_ASSERT_GT(test, rate, 0);
  1335. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
  1336. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
  1337. clk_put(clk);
  1338. }
  1339. /*
  1340. * Test that if our clock has some boundaries and we try to set a rate
  1341. * higher than the maximum, the new rate will be within range.
  1342. */
  1343. static void clk_range_test_set_range_set_rate_higher(struct kunit *test)
  1344. {
  1345. struct clk_dummy_context *ctx = test->priv;
  1346. struct clk_hw *hw = &ctx->hw;
  1347. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1348. unsigned long rate;
  1349. KUNIT_ASSERT_EQ(test,
  1350. clk_set_rate_range(clk,
  1351. DUMMY_CLOCK_RATE_1,
  1352. DUMMY_CLOCK_RATE_2),
  1353. 0);
  1354. KUNIT_ASSERT_EQ(test,
  1355. clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
  1356. 0);
  1357. rate = clk_get_rate(clk);
  1358. KUNIT_ASSERT_GT(test, rate, 0);
  1359. KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
  1360. KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
  1361. clk_put(clk);
  1362. }
  1363. /*
  1364. * Test that if our clock has some boundaries and we try to round and
  1365. * set a rate higher than the maximum, the rate returned by
  1366. * clk_round_rate() will be consistent with the new rate set by
  1367. * clk_set_rate().
  1368. */
  1369. static void clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit *test)
  1370. {
  1371. struct clk_dummy_context *ctx = test->priv;
  1372. struct clk_hw *hw = &ctx->hw;
  1373. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1374. long rounded;
  1375. KUNIT_ASSERT_EQ(test,
  1376. clk_set_rate_range(clk,
  1377. DUMMY_CLOCK_RATE_1,
  1378. DUMMY_CLOCK_RATE_2),
  1379. 0);
  1380. rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
  1381. KUNIT_ASSERT_GT(test, rounded, 0);
  1382. KUNIT_ASSERT_EQ(test,
  1383. clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
  1384. 0);
  1385. KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
  1386. clk_put(clk);
  1387. }
  1388. /*
  1389. * Test that if our clock has a rate lower than the minimum set by a
  1390. * call to clk_set_rate_range(), the rate will be raised to match the
  1391. * new minimum.
  1392. *
  1393. * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
  1394. * modify the requested rate, which is our case in clk_dummy_rate_ops.
  1395. */
  1396. static void clk_range_test_set_range_get_rate_raised(struct kunit *test)
  1397. {
  1398. struct clk_dummy_context *ctx = test->priv;
  1399. struct clk_hw *hw = &ctx->hw;
  1400. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1401. unsigned long rate;
  1402. KUNIT_ASSERT_EQ(test,
  1403. clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
  1404. 0);
  1405. KUNIT_ASSERT_EQ(test,
  1406. clk_set_rate_range(clk,
  1407. DUMMY_CLOCK_RATE_1,
  1408. DUMMY_CLOCK_RATE_2),
  1409. 0);
  1410. rate = clk_get_rate(clk);
  1411. KUNIT_ASSERT_GT(test, rate, 0);
  1412. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
  1413. clk_put(clk);
  1414. }
  1415. /*
  1416. * Test that if our clock has a rate higher than the maximum set by a
  1417. * call to clk_set_rate_range(), the rate will be lowered to match the
  1418. * new maximum.
  1419. *
  1420. * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
  1421. * modify the requested rate, which is our case in clk_dummy_rate_ops.
  1422. */
  1423. static void clk_range_test_set_range_get_rate_lowered(struct kunit *test)
  1424. {
  1425. struct clk_dummy_context *ctx = test->priv;
  1426. struct clk_hw *hw = &ctx->hw;
  1427. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1428. unsigned long rate;
  1429. KUNIT_ASSERT_EQ(test,
  1430. clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
  1431. 0);
  1432. KUNIT_ASSERT_EQ(test,
  1433. clk_set_rate_range(clk,
  1434. DUMMY_CLOCK_RATE_1,
  1435. DUMMY_CLOCK_RATE_2),
  1436. 0);
  1437. rate = clk_get_rate(clk);
  1438. KUNIT_ASSERT_GT(test, rate, 0);
  1439. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
  1440. clk_put(clk);
  1441. }
  1442. static struct kunit_case clk_range_test_cases[] = {
  1443. KUNIT_CASE(clk_range_test_set_range),
  1444. KUNIT_CASE(clk_range_test_set_range_invalid),
  1445. KUNIT_CASE(clk_range_test_multiple_disjoints_range),
  1446. KUNIT_CASE(clk_range_test_set_range_round_rate_lower),
  1447. KUNIT_CASE(clk_range_test_set_range_set_rate_lower),
  1448. KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_lower),
  1449. KUNIT_CASE(clk_range_test_set_range_round_rate_higher),
  1450. KUNIT_CASE(clk_range_test_set_range_set_rate_higher),
  1451. KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_higher),
  1452. KUNIT_CASE(clk_range_test_set_range_get_rate_raised),
  1453. KUNIT_CASE(clk_range_test_set_range_get_rate_lowered),
  1454. {}
  1455. };
  1456. /*
  1457. * Test suite for a basic rate clock, without any parent.
  1458. *
  1459. * These tests exercise the rate range API: clk_set_rate_range(),
  1460. * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range().
  1461. */
  1462. static struct kunit_suite clk_range_test_suite = {
  1463. .name = "clk-range-test",
  1464. .init = clk_test_init,
  1465. .exit = clk_test_exit,
  1466. .test_cases = clk_range_test_cases,
  1467. };
  1468. /*
  1469. * Test that if we have several subsequent calls to
  1470. * clk_set_rate_range(), the core will reevaluate whether a new rate is
  1471. * needed each and every time.
  1472. *
  1473. * With clk_dummy_maximize_rate_ops, this means that the rate will
  1474. * trail along the maximum as it evolves.
  1475. */
  1476. static void clk_range_test_set_range_rate_maximized(struct kunit *test)
  1477. {
  1478. struct clk_dummy_context *ctx = test->priv;
  1479. struct clk_hw *hw = &ctx->hw;
  1480. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1481. unsigned long rate;
  1482. KUNIT_ASSERT_EQ(test,
  1483. clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
  1484. 0);
  1485. KUNIT_ASSERT_EQ(test,
  1486. clk_set_rate_range(clk,
  1487. DUMMY_CLOCK_RATE_1,
  1488. DUMMY_CLOCK_RATE_2),
  1489. 0);
  1490. rate = clk_get_rate(clk);
  1491. KUNIT_ASSERT_GT(test, rate, 0);
  1492. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
  1493. KUNIT_ASSERT_EQ(test,
  1494. clk_set_rate_range(clk,
  1495. DUMMY_CLOCK_RATE_1,
  1496. DUMMY_CLOCK_RATE_2 - 1000),
  1497. 0);
  1498. rate = clk_get_rate(clk);
  1499. KUNIT_ASSERT_GT(test, rate, 0);
  1500. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
  1501. KUNIT_ASSERT_EQ(test,
  1502. clk_set_rate_range(clk,
  1503. DUMMY_CLOCK_RATE_1,
  1504. DUMMY_CLOCK_RATE_2),
  1505. 0);
  1506. rate = clk_get_rate(clk);
  1507. KUNIT_ASSERT_GT(test, rate, 0);
  1508. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
  1509. clk_put(clk);
  1510. }
  1511. /*
  1512. * Test that if we have several subsequent calls to
  1513. * clk_set_rate_range(), across multiple users, the core will reevaluate
  1514. * whether a new rate is needed each and every time.
  1515. *
  1516. * With clk_dummy_maximize_rate_ops, this means that the rate will
  1517. * trail along the maximum as it evolves.
  1518. */
  1519. static void clk_range_test_multiple_set_range_rate_maximized(struct kunit *test)
  1520. {
  1521. struct clk_dummy_context *ctx = test->priv;
  1522. struct clk_hw *hw = &ctx->hw;
  1523. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1524. struct clk *user1, *user2;
  1525. unsigned long rate;
  1526. user1 = clk_hw_get_clk(hw, NULL);
  1527. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
  1528. user2 = clk_hw_get_clk(hw, NULL);
  1529. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
  1530. KUNIT_ASSERT_EQ(test,
  1531. clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
  1532. 0);
  1533. KUNIT_ASSERT_EQ(test,
  1534. clk_set_rate_range(user1,
  1535. 0,
  1536. DUMMY_CLOCK_RATE_2),
  1537. 0);
  1538. rate = clk_get_rate(clk);
  1539. KUNIT_ASSERT_GT(test, rate, 0);
  1540. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
  1541. KUNIT_ASSERT_EQ(test,
  1542. clk_set_rate_range(user2,
  1543. 0,
  1544. DUMMY_CLOCK_RATE_1),
  1545. 0);
  1546. rate = clk_get_rate(clk);
  1547. KUNIT_ASSERT_GT(test, rate, 0);
  1548. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
  1549. KUNIT_ASSERT_EQ(test,
  1550. clk_drop_range(user2),
  1551. 0);
  1552. rate = clk_get_rate(clk);
  1553. KUNIT_ASSERT_GT(test, rate, 0);
  1554. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
  1555. clk_put(user2);
  1556. clk_put(user1);
  1557. clk_put(clk);
  1558. }
  1559. /*
  1560. * Test that if we have several subsequent calls to
  1561. * clk_set_rate_range(), across multiple users, the core will reevaluate
  1562. * whether a new rate is needed, including when a user drop its clock.
  1563. *
  1564. * With clk_dummy_maximize_rate_ops, this means that the rate will
  1565. * trail along the maximum as it evolves.
  1566. */
  1567. static void clk_range_test_multiple_set_range_rate_put_maximized(struct kunit *test)
  1568. {
  1569. struct clk_dummy_context *ctx = test->priv;
  1570. struct clk_hw *hw = &ctx->hw;
  1571. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1572. struct clk *user1, *user2;
  1573. unsigned long rate;
  1574. user1 = clk_hw_get_clk(hw, NULL);
  1575. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
  1576. user2 = clk_hw_get_clk(hw, NULL);
  1577. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
  1578. KUNIT_ASSERT_EQ(test,
  1579. clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
  1580. 0);
  1581. KUNIT_ASSERT_EQ(test,
  1582. clk_set_rate_range(user1,
  1583. 0,
  1584. DUMMY_CLOCK_RATE_2),
  1585. 0);
  1586. rate = clk_get_rate(clk);
  1587. KUNIT_ASSERT_GT(test, rate, 0);
  1588. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
  1589. KUNIT_ASSERT_EQ(test,
  1590. clk_set_rate_range(user2,
  1591. 0,
  1592. DUMMY_CLOCK_RATE_1),
  1593. 0);
  1594. rate = clk_get_rate(clk);
  1595. KUNIT_ASSERT_GT(test, rate, 0);
  1596. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
  1597. clk_put(user2);
  1598. rate = clk_get_rate(clk);
  1599. KUNIT_ASSERT_GT(test, rate, 0);
  1600. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
  1601. clk_put(user1);
  1602. clk_put(clk);
  1603. }
  1604. static struct kunit_case clk_range_maximize_test_cases[] = {
  1605. KUNIT_CASE(clk_range_test_set_range_rate_maximized),
  1606. KUNIT_CASE(clk_range_test_multiple_set_range_rate_maximized),
  1607. KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_maximized),
  1608. {}
  1609. };
  1610. /*
  1611. * Test suite for a basic rate clock, without any parent.
  1612. *
  1613. * These tests exercise the rate range API: clk_set_rate_range(),
  1614. * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a
  1615. * driver that will always try to run at the highest possible rate.
  1616. */
  1617. static struct kunit_suite clk_range_maximize_test_suite = {
  1618. .name = "clk-range-maximize-test",
  1619. .init = clk_maximize_test_init,
  1620. .exit = clk_test_exit,
  1621. .test_cases = clk_range_maximize_test_cases,
  1622. };
  1623. /*
  1624. * Test that if we have several subsequent calls to
  1625. * clk_set_rate_range(), the core will reevaluate whether a new rate is
  1626. * needed each and every time.
  1627. *
  1628. * With clk_dummy_minimize_rate_ops, this means that the rate will
  1629. * trail along the minimum as it evolves.
  1630. */
  1631. static void clk_range_test_set_range_rate_minimized(struct kunit *test)
  1632. {
  1633. struct clk_dummy_context *ctx = test->priv;
  1634. struct clk_hw *hw = &ctx->hw;
  1635. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1636. unsigned long rate;
  1637. KUNIT_ASSERT_EQ(test,
  1638. clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
  1639. 0);
  1640. KUNIT_ASSERT_EQ(test,
  1641. clk_set_rate_range(clk,
  1642. DUMMY_CLOCK_RATE_1,
  1643. DUMMY_CLOCK_RATE_2),
  1644. 0);
  1645. rate = clk_get_rate(clk);
  1646. KUNIT_ASSERT_GT(test, rate, 0);
  1647. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
  1648. KUNIT_ASSERT_EQ(test,
  1649. clk_set_rate_range(clk,
  1650. DUMMY_CLOCK_RATE_1 + 1000,
  1651. DUMMY_CLOCK_RATE_2),
  1652. 0);
  1653. rate = clk_get_rate(clk);
  1654. KUNIT_ASSERT_GT(test, rate, 0);
  1655. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
  1656. KUNIT_ASSERT_EQ(test,
  1657. clk_set_rate_range(clk,
  1658. DUMMY_CLOCK_RATE_1,
  1659. DUMMY_CLOCK_RATE_2),
  1660. 0);
  1661. rate = clk_get_rate(clk);
  1662. KUNIT_ASSERT_GT(test, rate, 0);
  1663. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
  1664. clk_put(clk);
  1665. }
  1666. /*
  1667. * Test that if we have several subsequent calls to
  1668. * clk_set_rate_range(), across multiple users, the core will reevaluate
  1669. * whether a new rate is needed each and every time.
  1670. *
  1671. * With clk_dummy_minimize_rate_ops, this means that the rate will
  1672. * trail along the minimum as it evolves.
  1673. */
  1674. static void clk_range_test_multiple_set_range_rate_minimized(struct kunit *test)
  1675. {
  1676. struct clk_dummy_context *ctx = test->priv;
  1677. struct clk_hw *hw = &ctx->hw;
  1678. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1679. struct clk *user1, *user2;
  1680. unsigned long rate;
  1681. user1 = clk_hw_get_clk(hw, NULL);
  1682. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
  1683. user2 = clk_hw_get_clk(hw, NULL);
  1684. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
  1685. KUNIT_ASSERT_EQ(test,
  1686. clk_set_rate_range(user1,
  1687. DUMMY_CLOCK_RATE_1,
  1688. ULONG_MAX),
  1689. 0);
  1690. rate = clk_get_rate(clk);
  1691. KUNIT_ASSERT_GT(test, rate, 0);
  1692. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
  1693. KUNIT_ASSERT_EQ(test,
  1694. clk_set_rate_range(user2,
  1695. DUMMY_CLOCK_RATE_2,
  1696. ULONG_MAX),
  1697. 0);
  1698. rate = clk_get_rate(clk);
  1699. KUNIT_ASSERT_GT(test, rate, 0);
  1700. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
  1701. KUNIT_ASSERT_EQ(test,
  1702. clk_drop_range(user2),
  1703. 0);
  1704. rate = clk_get_rate(clk);
  1705. KUNIT_ASSERT_GT(test, rate, 0);
  1706. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
  1707. clk_put(user2);
  1708. clk_put(user1);
  1709. clk_put(clk);
  1710. }
  1711. /*
  1712. * Test that if we have several subsequent calls to
  1713. * clk_set_rate_range(), across multiple users, the core will reevaluate
  1714. * whether a new rate is needed, including when a user drop its clock.
  1715. *
  1716. * With clk_dummy_minimize_rate_ops, this means that the rate will
  1717. * trail along the minimum as it evolves.
  1718. */
  1719. static void clk_range_test_multiple_set_range_rate_put_minimized(struct kunit *test)
  1720. {
  1721. struct clk_dummy_context *ctx = test->priv;
  1722. struct clk_hw *hw = &ctx->hw;
  1723. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1724. struct clk *user1, *user2;
  1725. unsigned long rate;
  1726. user1 = clk_hw_get_clk(hw, NULL);
  1727. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
  1728. user2 = clk_hw_get_clk(hw, NULL);
  1729. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
  1730. KUNIT_ASSERT_EQ(test,
  1731. clk_set_rate_range(user1,
  1732. DUMMY_CLOCK_RATE_1,
  1733. ULONG_MAX),
  1734. 0);
  1735. rate = clk_get_rate(clk);
  1736. KUNIT_ASSERT_GT(test, rate, 0);
  1737. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
  1738. KUNIT_ASSERT_EQ(test,
  1739. clk_set_rate_range(user2,
  1740. DUMMY_CLOCK_RATE_2,
  1741. ULONG_MAX),
  1742. 0);
  1743. rate = clk_get_rate(clk);
  1744. KUNIT_ASSERT_GT(test, rate, 0);
  1745. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
  1746. clk_put(user2);
  1747. rate = clk_get_rate(clk);
  1748. KUNIT_ASSERT_GT(test, rate, 0);
  1749. KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
  1750. clk_put(user1);
  1751. clk_put(clk);
  1752. }
  1753. static struct kunit_case clk_range_minimize_test_cases[] = {
  1754. KUNIT_CASE(clk_range_test_set_range_rate_minimized),
  1755. KUNIT_CASE(clk_range_test_multiple_set_range_rate_minimized),
  1756. KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_minimized),
  1757. {}
  1758. };
  1759. /*
  1760. * Test suite for a basic rate clock, without any parent.
  1761. *
  1762. * These tests exercise the rate range API: clk_set_rate_range(),
  1763. * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a
  1764. * driver that will always try to run at the lowest possible rate.
  1765. */
  1766. static struct kunit_suite clk_range_minimize_test_suite = {
  1767. .name = "clk-range-minimize-test",
  1768. .init = clk_minimize_test_init,
  1769. .exit = clk_test_exit,
  1770. .test_cases = clk_range_minimize_test_cases,
  1771. };
  1772. struct clk_leaf_mux_ctx {
  1773. struct clk_multiple_parent_ctx mux_ctx;
  1774. struct clk_hw hw;
  1775. struct clk_hw parent;
  1776. struct clk_rate_request *req;
  1777. int (*determine_rate_func)(struct clk_hw *hw, struct clk_rate_request *req);
  1778. };
  1779. static int clk_leaf_mux_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
  1780. {
  1781. struct clk_leaf_mux_ctx *ctx = container_of(hw, struct clk_leaf_mux_ctx, hw);
  1782. int ret;
  1783. struct clk_rate_request *parent_req = ctx->req;
  1784. clk_hw_forward_rate_request(hw, req, req->best_parent_hw, parent_req, req->rate);
  1785. ret = ctx->determine_rate_func(req->best_parent_hw, parent_req);
  1786. if (ret)
  1787. return ret;
  1788. req->rate = parent_req->rate;
  1789. return 0;
  1790. }
  1791. static const struct clk_ops clk_leaf_mux_set_rate_parent_ops = {
  1792. .determine_rate = clk_leaf_mux_determine_rate,
  1793. .set_parent = clk_dummy_single_set_parent,
  1794. .get_parent = clk_dummy_single_get_parent,
  1795. };
  1796. static int
  1797. clk_leaf_mux_set_rate_parent_test_init(struct kunit *test)
  1798. {
  1799. struct clk_leaf_mux_ctx *ctx;
  1800. const char *top_parents[2] = { "parent-0", "parent-1" };
  1801. int ret;
  1802. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  1803. if (!ctx)
  1804. return -ENOMEM;
  1805. test->priv = ctx;
  1806. ctx->mux_ctx.parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
  1807. &clk_dummy_rate_ops,
  1808. 0);
  1809. ctx->mux_ctx.parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
  1810. ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[0].hw);
  1811. if (ret)
  1812. return ret;
  1813. ctx->mux_ctx.parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
  1814. &clk_dummy_rate_ops,
  1815. 0);
  1816. ctx->mux_ctx.parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
  1817. ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[1].hw);
  1818. if (ret)
  1819. return ret;
  1820. ctx->mux_ctx.current_parent = 0;
  1821. ctx->mux_ctx.hw.init = CLK_HW_INIT_PARENTS("test-mux", top_parents,
  1822. &clk_multiple_parents_mux_ops,
  1823. 0);
  1824. ret = clk_hw_register(NULL, &ctx->mux_ctx.hw);
  1825. if (ret)
  1826. return ret;
  1827. ctx->parent.init = CLK_HW_INIT_HW("test-parent", &ctx->mux_ctx.hw,
  1828. &empty_clk_ops, CLK_SET_RATE_PARENT);
  1829. ret = clk_hw_register(NULL, &ctx->parent);
  1830. if (ret)
  1831. return ret;
  1832. ctx->hw.init = CLK_HW_INIT_HW("test-clock", &ctx->parent,
  1833. &clk_leaf_mux_set_rate_parent_ops,
  1834. CLK_SET_RATE_PARENT);
  1835. ret = clk_hw_register(NULL, &ctx->hw);
  1836. if (ret)
  1837. return ret;
  1838. return 0;
  1839. }
  1840. static void clk_leaf_mux_set_rate_parent_test_exit(struct kunit *test)
  1841. {
  1842. struct clk_leaf_mux_ctx *ctx = test->priv;
  1843. clk_hw_unregister(&ctx->hw);
  1844. clk_hw_unregister(&ctx->parent);
  1845. clk_hw_unregister(&ctx->mux_ctx.hw);
  1846. clk_hw_unregister(&ctx->mux_ctx.parents_ctx[0].hw);
  1847. clk_hw_unregister(&ctx->mux_ctx.parents_ctx[1].hw);
  1848. }
  1849. struct clk_leaf_mux_set_rate_parent_determine_rate_test_case {
  1850. const char *desc;
  1851. int (*determine_rate_func)(struct clk_hw *hw, struct clk_rate_request *req);
  1852. };
  1853. static void
  1854. clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc(
  1855. const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case *t, char *desc)
  1856. {
  1857. strcpy(desc, t->desc);
  1858. }
  1859. static const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case
  1860. clk_leaf_mux_set_rate_parent_determine_rate_test_cases[] = {
  1861. {
  1862. /*
  1863. * Test that __clk_determine_rate() on the parent that can't
  1864. * change rate doesn't return a clk_rate_request structure with
  1865. * the best_parent_hw pointer pointing to the parent.
  1866. */
  1867. .desc = "clk_leaf_mux_set_rate_parent__clk_determine_rate_proper_parent",
  1868. .determine_rate_func = __clk_determine_rate,
  1869. },
  1870. {
  1871. /*
  1872. * Test that __clk_mux_determine_rate() on the parent that
  1873. * can't change rate doesn't return a clk_rate_request
  1874. * structure with the best_parent_hw pointer pointing to
  1875. * the parent.
  1876. */
  1877. .desc = "clk_leaf_mux_set_rate_parent__clk_mux_determine_rate_proper_parent",
  1878. .determine_rate_func = __clk_mux_determine_rate,
  1879. },
  1880. {
  1881. /*
  1882. * Test that __clk_mux_determine_rate_closest() on the parent
  1883. * that can't change rate doesn't return a clk_rate_request
  1884. * structure with the best_parent_hw pointer pointing to
  1885. * the parent.
  1886. */
  1887. .desc = "clk_leaf_mux_set_rate_parent__clk_mux_determine_rate_closest_proper_parent",
  1888. .determine_rate_func = __clk_mux_determine_rate_closest,
  1889. },
  1890. {
  1891. /*
  1892. * Test that clk_hw_determine_rate_no_reparent() on the parent
  1893. * that can't change rate doesn't return a clk_rate_request
  1894. * structure with the best_parent_hw pointer pointing to
  1895. * the parent.
  1896. */
  1897. .desc = "clk_leaf_mux_set_rate_parent_clk_hw_determine_rate_no_reparent_proper_parent",
  1898. .determine_rate_func = clk_hw_determine_rate_no_reparent,
  1899. },
  1900. };
  1901. KUNIT_ARRAY_PARAM(clk_leaf_mux_set_rate_parent_determine_rate_test,
  1902. clk_leaf_mux_set_rate_parent_determine_rate_test_cases,
  1903. clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc)
  1904. /*
  1905. * Test that when a clk that can't change rate itself calls a function like
  1906. * __clk_determine_rate() on its parent it doesn't get back a clk_rate_request
  1907. * structure that has the best_parent_hw pointer point to the clk_hw passed
  1908. * into the determine rate function. See commit 262ca38f4b6e ("clk: Stop
  1909. * forwarding clk_rate_requests to the parent") for more background.
  1910. */
  1911. static void clk_leaf_mux_set_rate_parent_determine_rate_test(struct kunit *test)
  1912. {
  1913. struct clk_leaf_mux_ctx *ctx = test->priv;
  1914. struct clk_hw *hw = &ctx->hw;
  1915. struct clk *clk = clk_hw_get_clk(hw, NULL);
  1916. struct clk_rate_request req;
  1917. unsigned long rate;
  1918. const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case *test_param;
  1919. test_param = test->param_value;
  1920. ctx->determine_rate_func = test_param->determine_rate_func;
  1921. ctx->req = &req;
  1922. rate = clk_get_rate(clk);
  1923. KUNIT_ASSERT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
  1924. KUNIT_ASSERT_EQ(test, DUMMY_CLOCK_RATE_2, clk_round_rate(clk, DUMMY_CLOCK_RATE_2));
  1925. KUNIT_EXPECT_EQ(test, req.rate, DUMMY_CLOCK_RATE_2);
  1926. KUNIT_EXPECT_EQ(test, req.best_parent_rate, DUMMY_CLOCK_RATE_2);
  1927. KUNIT_EXPECT_PTR_EQ(test, req.best_parent_hw, &ctx->mux_ctx.hw);
  1928. clk_put(clk);
  1929. }
  1930. static struct kunit_case clk_leaf_mux_set_rate_parent_test_cases[] = {
  1931. KUNIT_CASE_PARAM(clk_leaf_mux_set_rate_parent_determine_rate_test,
  1932. clk_leaf_mux_set_rate_parent_determine_rate_test_gen_params),
  1933. {}
  1934. };
  1935. /*
  1936. * Test suite for a clock whose parent is a pass-through clk whose parent is a
  1937. * mux with multiple parents. The leaf and pass-through clocks have the
  1938. * CLK_SET_RATE_PARENT flag, and will forward rate requests to the mux, which
  1939. * will then select which parent is the best fit for a given rate.
  1940. *
  1941. * These tests exercise the behaviour of muxes, and the proper selection
  1942. * of parents.
  1943. */
  1944. static struct kunit_suite clk_leaf_mux_set_rate_parent_test_suite = {
  1945. .name = "clk-leaf-mux-set-rate-parent",
  1946. .init = clk_leaf_mux_set_rate_parent_test_init,
  1947. .exit = clk_leaf_mux_set_rate_parent_test_exit,
  1948. .test_cases = clk_leaf_mux_set_rate_parent_test_cases,
  1949. };
  1950. struct clk_mux_notifier_rate_change {
  1951. bool done;
  1952. unsigned long old_rate;
  1953. unsigned long new_rate;
  1954. wait_queue_head_t wq;
  1955. };
  1956. struct clk_mux_notifier_ctx {
  1957. struct clk_multiple_parent_ctx mux_ctx;
  1958. struct clk *clk;
  1959. struct notifier_block clk_nb;
  1960. struct clk_mux_notifier_rate_change pre_rate_change;
  1961. struct clk_mux_notifier_rate_change post_rate_change;
  1962. };
  1963. #define NOTIFIER_TIMEOUT_MS 100
  1964. static int clk_mux_notifier_callback(struct notifier_block *nb,
  1965. unsigned long action, void *data)
  1966. {
  1967. struct clk_notifier_data *clk_data = data;
  1968. struct clk_mux_notifier_ctx *ctx = container_of(nb,
  1969. struct clk_mux_notifier_ctx,
  1970. clk_nb);
  1971. if (action & PRE_RATE_CHANGE) {
  1972. ctx->pre_rate_change.old_rate = clk_data->old_rate;
  1973. ctx->pre_rate_change.new_rate = clk_data->new_rate;
  1974. ctx->pre_rate_change.done = true;
  1975. wake_up_interruptible(&ctx->pre_rate_change.wq);
  1976. }
  1977. if (action & POST_RATE_CHANGE) {
  1978. ctx->post_rate_change.old_rate = clk_data->old_rate;
  1979. ctx->post_rate_change.new_rate = clk_data->new_rate;
  1980. ctx->post_rate_change.done = true;
  1981. wake_up_interruptible(&ctx->post_rate_change.wq);
  1982. }
  1983. return 0;
  1984. }
  1985. static int clk_mux_notifier_test_init(struct kunit *test)
  1986. {
  1987. struct clk_mux_notifier_ctx *ctx;
  1988. const char *top_parents[2] = { "parent-0", "parent-1" };
  1989. int ret;
  1990. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  1991. if (!ctx)
  1992. return -ENOMEM;
  1993. test->priv = ctx;
  1994. ctx->clk_nb.notifier_call = clk_mux_notifier_callback;
  1995. init_waitqueue_head(&ctx->pre_rate_change.wq);
  1996. init_waitqueue_head(&ctx->post_rate_change.wq);
  1997. ctx->mux_ctx.parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
  1998. &clk_dummy_rate_ops,
  1999. 0);
  2000. ctx->mux_ctx.parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
  2001. ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[0].hw);
  2002. if (ret)
  2003. return ret;
  2004. ctx->mux_ctx.parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
  2005. &clk_dummy_rate_ops,
  2006. 0);
  2007. ctx->mux_ctx.parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
  2008. ret = clk_hw_register(NULL, &ctx->mux_ctx.parents_ctx[1].hw);
  2009. if (ret)
  2010. return ret;
  2011. ctx->mux_ctx.current_parent = 0;
  2012. ctx->mux_ctx.hw.init = CLK_HW_INIT_PARENTS("test-mux", top_parents,
  2013. &clk_multiple_parents_mux_ops,
  2014. 0);
  2015. ret = clk_hw_register(NULL, &ctx->mux_ctx.hw);
  2016. if (ret)
  2017. return ret;
  2018. ctx->clk = clk_hw_get_clk(&ctx->mux_ctx.hw, NULL);
  2019. ret = clk_notifier_register(ctx->clk, &ctx->clk_nb);
  2020. if (ret)
  2021. return ret;
  2022. return 0;
  2023. }
  2024. static void clk_mux_notifier_test_exit(struct kunit *test)
  2025. {
  2026. struct clk_mux_notifier_ctx *ctx = test->priv;
  2027. struct clk *clk = ctx->clk;
  2028. clk_notifier_unregister(clk, &ctx->clk_nb);
  2029. clk_put(clk);
  2030. clk_hw_unregister(&ctx->mux_ctx.hw);
  2031. clk_hw_unregister(&ctx->mux_ctx.parents_ctx[0].hw);
  2032. clk_hw_unregister(&ctx->mux_ctx.parents_ctx[1].hw);
  2033. }
  2034. /*
  2035. * Test that if the we have a notifier registered on a mux, the core
  2036. * will notify us when we switch to another parent, and with the proper
  2037. * old and new rates.
  2038. */
  2039. static void clk_mux_notifier_set_parent_test(struct kunit *test)
  2040. {
  2041. struct clk_mux_notifier_ctx *ctx = test->priv;
  2042. struct clk_hw *hw = &ctx->mux_ctx.hw;
  2043. struct clk *clk = clk_hw_get_clk(hw, NULL);
  2044. struct clk *new_parent = clk_hw_get_clk(&ctx->mux_ctx.parents_ctx[1].hw, NULL);
  2045. int ret;
  2046. ret = clk_set_parent(clk, new_parent);
  2047. KUNIT_ASSERT_EQ(test, ret, 0);
  2048. ret = wait_event_interruptible_timeout(ctx->pre_rate_change.wq,
  2049. ctx->pre_rate_change.done,
  2050. msecs_to_jiffies(NOTIFIER_TIMEOUT_MS));
  2051. KUNIT_ASSERT_GT(test, ret, 0);
  2052. KUNIT_EXPECT_EQ(test, ctx->pre_rate_change.old_rate, DUMMY_CLOCK_RATE_1);
  2053. KUNIT_EXPECT_EQ(test, ctx->pre_rate_change.new_rate, DUMMY_CLOCK_RATE_2);
  2054. ret = wait_event_interruptible_timeout(ctx->post_rate_change.wq,
  2055. ctx->post_rate_change.done,
  2056. msecs_to_jiffies(NOTIFIER_TIMEOUT_MS));
  2057. KUNIT_ASSERT_GT(test, ret, 0);
  2058. KUNIT_EXPECT_EQ(test, ctx->post_rate_change.old_rate, DUMMY_CLOCK_RATE_1);
  2059. KUNIT_EXPECT_EQ(test, ctx->post_rate_change.new_rate, DUMMY_CLOCK_RATE_2);
  2060. clk_put(new_parent);
  2061. clk_put(clk);
  2062. }
  2063. static struct kunit_case clk_mux_notifier_test_cases[] = {
  2064. KUNIT_CASE(clk_mux_notifier_set_parent_test),
  2065. {}
  2066. };
  2067. /*
  2068. * Test suite for a mux with multiple parents, and a notifier registered
  2069. * on the mux.
  2070. *
  2071. * These tests exercise the behaviour of notifiers.
  2072. */
  2073. static struct kunit_suite clk_mux_notifier_test_suite = {
  2074. .name = "clk-mux-notifier",
  2075. .init = clk_mux_notifier_test_init,
  2076. .exit = clk_mux_notifier_test_exit,
  2077. .test_cases = clk_mux_notifier_test_cases,
  2078. };
  2079. static int
  2080. clk_mux_no_reparent_test_init(struct kunit *test)
  2081. {
  2082. struct clk_multiple_parent_ctx *ctx;
  2083. const char *parents[2] = { "parent-0", "parent-1"};
  2084. int ret;
  2085. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  2086. if (!ctx)
  2087. return -ENOMEM;
  2088. test->priv = ctx;
  2089. ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
  2090. &clk_dummy_rate_ops,
  2091. 0);
  2092. ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
  2093. ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
  2094. if (ret)
  2095. return ret;
  2096. ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
  2097. &clk_dummy_rate_ops,
  2098. 0);
  2099. ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
  2100. ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
  2101. if (ret)
  2102. return ret;
  2103. ctx->current_parent = 0;
  2104. ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
  2105. &clk_multiple_parents_no_reparent_mux_ops,
  2106. 0);
  2107. ret = clk_hw_register(NULL, &ctx->hw);
  2108. if (ret)
  2109. return ret;
  2110. return 0;
  2111. }
  2112. static void
  2113. clk_mux_no_reparent_test_exit(struct kunit *test)
  2114. {
  2115. struct clk_multiple_parent_ctx *ctx = test->priv;
  2116. clk_hw_unregister(&ctx->hw);
  2117. clk_hw_unregister(&ctx->parents_ctx[0].hw);
  2118. clk_hw_unregister(&ctx->parents_ctx[1].hw);
  2119. }
  2120. /*
  2121. * Test that if the we have a mux that cannot change parent and we call
  2122. * clk_round_rate() on it with a rate that should cause it to change
  2123. * parent, it won't.
  2124. */
  2125. static void clk_mux_no_reparent_round_rate(struct kunit *test)
  2126. {
  2127. struct clk_multiple_parent_ctx *ctx = test->priv;
  2128. struct clk_hw *hw = &ctx->hw;
  2129. struct clk *clk = clk_hw_get_clk(hw, NULL);
  2130. struct clk *other_parent, *parent;
  2131. unsigned long other_parent_rate;
  2132. unsigned long parent_rate;
  2133. long rounded_rate;
  2134. parent = clk_get_parent(clk);
  2135. KUNIT_ASSERT_PTR_NE(test, parent, NULL);
  2136. parent_rate = clk_get_rate(parent);
  2137. KUNIT_ASSERT_GT(test, parent_rate, 0);
  2138. other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
  2139. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
  2140. KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
  2141. other_parent_rate = clk_get_rate(other_parent);
  2142. KUNIT_ASSERT_GT(test, other_parent_rate, 0);
  2143. clk_put(other_parent);
  2144. rounded_rate = clk_round_rate(clk, other_parent_rate);
  2145. KUNIT_ASSERT_GT(test, rounded_rate, 0);
  2146. KUNIT_EXPECT_EQ(test, rounded_rate, parent_rate);
  2147. clk_put(clk);
  2148. }
  2149. /*
  2150. * Test that if the we have a mux that cannot change parent and we call
  2151. * clk_set_rate() on it with a rate that should cause it to change
  2152. * parent, it won't.
  2153. */
  2154. static void clk_mux_no_reparent_set_rate(struct kunit *test)
  2155. {
  2156. struct clk_multiple_parent_ctx *ctx = test->priv;
  2157. struct clk_hw *hw = &ctx->hw;
  2158. struct clk *clk = clk_hw_get_clk(hw, NULL);
  2159. struct clk *other_parent, *parent;
  2160. unsigned long other_parent_rate;
  2161. unsigned long parent_rate;
  2162. unsigned long rate;
  2163. int ret;
  2164. parent = clk_get_parent(clk);
  2165. KUNIT_ASSERT_PTR_NE(test, parent, NULL);
  2166. parent_rate = clk_get_rate(parent);
  2167. KUNIT_ASSERT_GT(test, parent_rate, 0);
  2168. other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
  2169. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
  2170. KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
  2171. other_parent_rate = clk_get_rate(other_parent);
  2172. KUNIT_ASSERT_GT(test, other_parent_rate, 0);
  2173. clk_put(other_parent);
  2174. ret = clk_set_rate(clk, other_parent_rate);
  2175. KUNIT_ASSERT_EQ(test, ret, 0);
  2176. rate = clk_get_rate(clk);
  2177. KUNIT_ASSERT_GT(test, rate, 0);
  2178. KUNIT_EXPECT_EQ(test, rate, parent_rate);
  2179. clk_put(clk);
  2180. }
  2181. static struct kunit_case clk_mux_no_reparent_test_cases[] = {
  2182. KUNIT_CASE(clk_mux_no_reparent_round_rate),
  2183. KUNIT_CASE(clk_mux_no_reparent_set_rate),
  2184. {}
  2185. };
  2186. /*
  2187. * Test suite for a clock mux that isn't allowed to change parent, using
  2188. * the clk_hw_determine_rate_no_reparent() helper.
  2189. *
  2190. * These tests exercise that helper, and the proper selection of
  2191. * rates and parents.
  2192. */
  2193. static struct kunit_suite clk_mux_no_reparent_test_suite = {
  2194. .name = "clk-mux-no-reparent",
  2195. .init = clk_mux_no_reparent_test_init,
  2196. .exit = clk_mux_no_reparent_test_exit,
  2197. .test_cases = clk_mux_no_reparent_test_cases,
  2198. };
  2199. struct clk_register_clk_parent_data_test_case {
  2200. const char *desc;
  2201. struct clk_parent_data pdata;
  2202. };
  2203. static void
  2204. clk_register_clk_parent_data_test_case_to_desc(
  2205. const struct clk_register_clk_parent_data_test_case *t, char *desc)
  2206. {
  2207. strcpy(desc, t->desc);
  2208. }
  2209. static const struct clk_register_clk_parent_data_test_case
  2210. clk_register_clk_parent_data_of_cases[] = {
  2211. {
  2212. /*
  2213. * Test that a clk registered with a struct device_node can
  2214. * find a parent based on struct clk_parent_data::index.
  2215. */
  2216. .desc = "clk_parent_data_of_index_test",
  2217. .pdata.index = 0,
  2218. },
  2219. {
  2220. /*
  2221. * Test that a clk registered with a struct device_node can
  2222. * find a parent based on struct clk_parent_data::fwname.
  2223. */
  2224. .desc = "clk_parent_data_of_fwname_test",
  2225. .pdata.fw_name = CLK_PARENT_DATA_PARENT1,
  2226. },
  2227. {
  2228. /*
  2229. * Test that a clk registered with a struct device_node can
  2230. * find a parent based on struct clk_parent_data::name.
  2231. */
  2232. .desc = "clk_parent_data_of_name_test",
  2233. /* The index must be negative to indicate firmware not used */
  2234. .pdata.index = -1,
  2235. .pdata.name = CLK_PARENT_DATA_1MHZ_NAME,
  2236. },
  2237. {
  2238. /*
  2239. * Test that a clk registered with a struct device_node can
  2240. * find a parent based on struct
  2241. * clk_parent_data::{fw_name,name}.
  2242. */
  2243. .desc = "clk_parent_data_of_fwname_name_test",
  2244. .pdata.fw_name = CLK_PARENT_DATA_PARENT1,
  2245. .pdata.name = "not_matching",
  2246. },
  2247. {
  2248. /*
  2249. * Test that a clk registered with a struct device_node can
  2250. * find a parent based on struct clk_parent_data::{index,name}.
  2251. * Index takes priority.
  2252. */
  2253. .desc = "clk_parent_data_of_index_name_priority_test",
  2254. .pdata.index = 0,
  2255. .pdata.name = "not_matching",
  2256. },
  2257. {
  2258. /*
  2259. * Test that a clk registered with a struct device_node can
  2260. * find a parent based on struct
  2261. * clk_parent_data::{index,fwname,name}. The fw_name takes
  2262. * priority over index and name.
  2263. */
  2264. .desc = "clk_parent_data_of_index_fwname_name_priority_test",
  2265. .pdata.index = 1,
  2266. .pdata.fw_name = CLK_PARENT_DATA_PARENT1,
  2267. .pdata.name = "not_matching",
  2268. },
  2269. };
  2270. KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_of_test, clk_register_clk_parent_data_of_cases,
  2271. clk_register_clk_parent_data_test_case_to_desc)
  2272. /**
  2273. * struct clk_register_clk_parent_data_of_ctx - Context for clk_parent_data OF tests
  2274. * @np: device node of clk under test
  2275. * @hw: clk_hw for clk under test
  2276. */
  2277. struct clk_register_clk_parent_data_of_ctx {
  2278. struct device_node *np;
  2279. struct clk_hw hw;
  2280. };
  2281. static int clk_register_clk_parent_data_of_test_init(struct kunit *test)
  2282. {
  2283. struct clk_register_clk_parent_data_of_ctx *ctx;
  2284. KUNIT_ASSERT_EQ(test, 0,
  2285. of_overlay_apply_kunit(test, kunit_clk_parent_data_test));
  2286. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  2287. if (!ctx)
  2288. return -ENOMEM;
  2289. test->priv = ctx;
  2290. ctx->np = of_find_compatible_node(NULL, NULL, "test,clk-parent-data");
  2291. if (!ctx->np)
  2292. return -ENODEV;
  2293. of_node_put_kunit(test, ctx->np);
  2294. return 0;
  2295. }
  2296. /*
  2297. * Test that a clk registered with a struct device_node can find a parent based on
  2298. * struct clk_parent_data when the hw member isn't set.
  2299. */
  2300. static void clk_register_clk_parent_data_of_test(struct kunit *test)
  2301. {
  2302. struct clk_register_clk_parent_data_of_ctx *ctx = test->priv;
  2303. struct clk_hw *parent_hw;
  2304. const struct clk_register_clk_parent_data_test_case *test_param;
  2305. struct clk_init_data init = { };
  2306. struct clk *expected_parent, *actual_parent;
  2307. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->np);
  2308. expected_parent = of_clk_get_kunit(test, ctx->np, 0);
  2309. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_parent);
  2310. test_param = test->param_value;
  2311. init.parent_data = &test_param->pdata;
  2312. init.num_parents = 1;
  2313. init.name = "parent_data_of_test_clk";
  2314. init.ops = &clk_dummy_single_parent_ops;
  2315. ctx->hw.init = &init;
  2316. KUNIT_ASSERT_EQ(test, 0, of_clk_hw_register_kunit(test, ctx->np, &ctx->hw));
  2317. parent_hw = clk_hw_get_parent(&ctx->hw);
  2318. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
  2319. actual_parent = clk_hw_get_clk_kunit(test, parent_hw, __func__);
  2320. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, actual_parent);
  2321. KUNIT_EXPECT_TRUE(test, clk_is_match(expected_parent, actual_parent));
  2322. }
  2323. static struct kunit_case clk_register_clk_parent_data_of_test_cases[] = {
  2324. KUNIT_CASE_PARAM(clk_register_clk_parent_data_of_test,
  2325. clk_register_clk_parent_data_of_test_gen_params),
  2326. {}
  2327. };
  2328. /*
  2329. * Test suite for registering clks with struct clk_parent_data and a struct
  2330. * device_node.
  2331. */
  2332. static struct kunit_suite clk_register_clk_parent_data_of_suite = {
  2333. .name = "clk_register_clk_parent_data_of",
  2334. .init = clk_register_clk_parent_data_of_test_init,
  2335. .test_cases = clk_register_clk_parent_data_of_test_cases,
  2336. };
  2337. /**
  2338. * struct platform_driver_dev_ctx - Context to stash platform device
  2339. * @dev: device under test
  2340. * @pdrv: driver to attach to find @dev
  2341. */
  2342. struct platform_driver_dev_ctx {
  2343. struct device *dev;
  2344. struct platform_driver pdrv;
  2345. };
  2346. static inline struct platform_driver_dev_ctx *
  2347. pdev_to_platform_driver_dev_ctx(struct platform_device *pdev)
  2348. {
  2349. return container_of(to_platform_driver(pdev->dev.driver),
  2350. struct platform_driver_dev_ctx, pdrv);
  2351. }
  2352. static int kunit_platform_driver_dev_probe(struct platform_device *pdev)
  2353. {
  2354. struct platform_driver_dev_ctx *ctx;
  2355. ctx = pdev_to_platform_driver_dev_ctx(pdev);
  2356. ctx->dev = &pdev->dev;
  2357. return 0;
  2358. }
  2359. static struct device *
  2360. kunit_of_platform_driver_dev(struct kunit *test, const struct of_device_id *match_table)
  2361. {
  2362. struct platform_driver_dev_ctx *ctx;
  2363. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  2364. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
  2365. ctx->pdrv.probe = kunit_platform_driver_dev_probe;
  2366. ctx->pdrv.driver.of_match_table = match_table;
  2367. ctx->pdrv.driver.name = __func__;
  2368. ctx->pdrv.driver.owner = THIS_MODULE;
  2369. KUNIT_ASSERT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv));
  2370. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->dev);
  2371. return ctx->dev;
  2372. }
  2373. static const struct clk_register_clk_parent_data_test_case
  2374. clk_register_clk_parent_data_device_cases[] = {
  2375. {
  2376. /*
  2377. * Test that a clk registered with a struct device can find a
  2378. * parent based on struct clk_parent_data::index.
  2379. */
  2380. .desc = "clk_parent_data_device_index_test",
  2381. .pdata.index = 1,
  2382. },
  2383. {
  2384. /*
  2385. * Test that a clk registered with a struct device can find a
  2386. * parent based on struct clk_parent_data::fwname.
  2387. */
  2388. .desc = "clk_parent_data_device_fwname_test",
  2389. .pdata.fw_name = CLK_PARENT_DATA_PARENT2,
  2390. },
  2391. {
  2392. /*
  2393. * Test that a clk registered with a struct device can find a
  2394. * parent based on struct clk_parent_data::name.
  2395. */
  2396. .desc = "clk_parent_data_device_name_test",
  2397. /* The index must be negative to indicate firmware not used */
  2398. .pdata.index = -1,
  2399. .pdata.name = CLK_PARENT_DATA_50MHZ_NAME,
  2400. },
  2401. {
  2402. /*
  2403. * Test that a clk registered with a struct device can find a
  2404. * parent based on struct clk_parent_data::{fw_name,name}.
  2405. */
  2406. .desc = "clk_parent_data_device_fwname_name_test",
  2407. .pdata.fw_name = CLK_PARENT_DATA_PARENT2,
  2408. .pdata.name = "not_matching",
  2409. },
  2410. {
  2411. /*
  2412. * Test that a clk registered with a struct device can find a
  2413. * parent based on struct clk_parent_data::{index,name}. Index
  2414. * takes priority.
  2415. */
  2416. .desc = "clk_parent_data_device_index_name_priority_test",
  2417. .pdata.index = 1,
  2418. .pdata.name = "not_matching",
  2419. },
  2420. {
  2421. /*
  2422. * Test that a clk registered with a struct device can find a
  2423. * parent based on struct clk_parent_data::{index,fwname,name}.
  2424. * The fw_name takes priority over index and name.
  2425. */
  2426. .desc = "clk_parent_data_device_index_fwname_name_priority_test",
  2427. .pdata.index = 0,
  2428. .pdata.fw_name = CLK_PARENT_DATA_PARENT2,
  2429. .pdata.name = "not_matching",
  2430. },
  2431. };
  2432. KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_device_test,
  2433. clk_register_clk_parent_data_device_cases,
  2434. clk_register_clk_parent_data_test_case_to_desc)
  2435. /*
  2436. * Test that a clk registered with a struct device can find a parent based on
  2437. * struct clk_parent_data when the hw member isn't set.
  2438. */
  2439. static void clk_register_clk_parent_data_device_test(struct kunit *test)
  2440. {
  2441. struct device *dev;
  2442. struct clk_hw *hw;
  2443. const struct clk_register_clk_parent_data_test_case *test_param;
  2444. struct clk_hw *parent_hw;
  2445. struct clk_init_data init = { };
  2446. struct clk *expected_parent, *actual_parent;
  2447. static const struct of_device_id match_table[] = {
  2448. { .compatible = "test,clk-parent-data" },
  2449. { }
  2450. };
  2451. dev = kunit_of_platform_driver_dev(test, match_table);
  2452. expected_parent = clk_get_kunit(test, dev, "50");
  2453. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_parent);
  2454. hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
  2455. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
  2456. test_param = test->param_value;
  2457. init.parent_data = &test_param->pdata;
  2458. init.num_parents = 1;
  2459. init.name = "parent_data_device_test_clk";
  2460. init.ops = &clk_dummy_single_parent_ops;
  2461. hw->init = &init;
  2462. KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, dev, hw));
  2463. parent_hw = clk_hw_get_parent(hw);
  2464. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
  2465. actual_parent = clk_hw_get_clk_kunit(test, parent_hw, __func__);
  2466. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, actual_parent);
  2467. KUNIT_EXPECT_TRUE(test, clk_is_match(expected_parent, actual_parent));
  2468. }
  2469. static const struct clk_register_clk_parent_data_test_case
  2470. clk_register_clk_parent_data_device_hw_cases[] = {
  2471. {
  2472. /*
  2473. * Test that a clk registered with a struct device can find a
  2474. * parent based on struct clk_parent_data::hw.
  2475. */
  2476. .desc = "clk_parent_data_device_hw_index_test",
  2477. /* The index must be negative to indicate firmware not used */
  2478. .pdata.index = -1,
  2479. },
  2480. {
  2481. /*
  2482. * Test that a clk registered with a struct device can find a
  2483. * parent based on struct clk_parent_data::hw when
  2484. * struct clk_parent_data::fw_name is set.
  2485. */
  2486. .desc = "clk_parent_data_device_hw_fwname_test",
  2487. .pdata.fw_name = CLK_PARENT_DATA_PARENT2,
  2488. },
  2489. {
  2490. /*
  2491. * Test that a clk registered with a struct device can find a
  2492. * parent based on struct clk_parent_data::hw when struct
  2493. * clk_parent_data::name is set.
  2494. */
  2495. .desc = "clk_parent_data_device_hw_name_test",
  2496. /* The index must be negative to indicate firmware not used */
  2497. .pdata.index = -1,
  2498. .pdata.name = CLK_PARENT_DATA_50MHZ_NAME,
  2499. },
  2500. {
  2501. /*
  2502. * Test that a clk registered with a struct device can find a
  2503. * parent based on struct clk_parent_data::hw when struct
  2504. * clk_parent_data::{fw_name,name} are set.
  2505. */
  2506. .desc = "clk_parent_data_device_hw_fwname_name_test",
  2507. .pdata.fw_name = CLK_PARENT_DATA_PARENT2,
  2508. .pdata.name = "not_matching",
  2509. },
  2510. {
  2511. /*
  2512. * Test that a clk registered with a struct device can find a
  2513. * parent based on struct clk_parent_data::hw when struct
  2514. * clk_parent_data::index is set. The hw pointer takes
  2515. * priority.
  2516. */
  2517. .desc = "clk_parent_data_device_hw_index_priority_test",
  2518. .pdata.index = 0,
  2519. },
  2520. {
  2521. /*
  2522. * Test that a clk registered with a struct device can find a
  2523. * parent based on struct clk_parent_data::hw when
  2524. * struct clk_parent_data::{index,fwname,name} are set.
  2525. * The hw pointer takes priority over everything else.
  2526. */
  2527. .desc = "clk_parent_data_device_hw_index_fwname_name_priority_test",
  2528. .pdata.index = 0,
  2529. .pdata.fw_name = CLK_PARENT_DATA_PARENT2,
  2530. .pdata.name = "not_matching",
  2531. },
  2532. };
  2533. KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_device_hw_test,
  2534. clk_register_clk_parent_data_device_hw_cases,
  2535. clk_register_clk_parent_data_test_case_to_desc)
  2536. /*
  2537. * Test that a clk registered with a struct device can find a
  2538. * parent based on struct clk_parent_data::hw.
  2539. */
  2540. static void clk_register_clk_parent_data_device_hw_test(struct kunit *test)
  2541. {
  2542. struct device *dev;
  2543. struct clk_hw *hw;
  2544. const struct clk_register_clk_parent_data_test_case *test_param;
  2545. struct clk_dummy_context *parent;
  2546. struct clk_hw *parent_hw;
  2547. struct clk_parent_data pdata = { };
  2548. struct clk_init_data init = { };
  2549. static const struct of_device_id match_table[] = {
  2550. { .compatible = "test,clk-parent-data" },
  2551. { }
  2552. };
  2553. dev = kunit_of_platform_driver_dev(test, match_table);
  2554. parent = kunit_kzalloc(test, sizeof(*parent), GFP_KERNEL);
  2555. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
  2556. parent_hw = &parent->hw;
  2557. parent_hw->init = CLK_HW_INIT_NO_PARENT("parent-clk",
  2558. &clk_dummy_rate_ops, 0);
  2559. KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, dev, parent_hw));
  2560. hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
  2561. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
  2562. test_param = test->param_value;
  2563. memcpy(&pdata, &test_param->pdata, sizeof(pdata));
  2564. pdata.hw = parent_hw;
  2565. init.parent_data = &pdata;
  2566. init.num_parents = 1;
  2567. init.ops = &clk_dummy_single_parent_ops;
  2568. init.name = "parent_data_device_hw_test_clk";
  2569. hw->init = &init;
  2570. KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, dev, hw));
  2571. KUNIT_EXPECT_PTR_EQ(test, parent_hw, clk_hw_get_parent(hw));
  2572. }
  2573. static struct kunit_case clk_register_clk_parent_data_device_test_cases[] = {
  2574. KUNIT_CASE_PARAM(clk_register_clk_parent_data_device_test,
  2575. clk_register_clk_parent_data_device_test_gen_params),
  2576. KUNIT_CASE_PARAM(clk_register_clk_parent_data_device_hw_test,
  2577. clk_register_clk_parent_data_device_hw_test_gen_params),
  2578. {}
  2579. };
  2580. static int clk_register_clk_parent_data_device_init(struct kunit *test)
  2581. {
  2582. KUNIT_ASSERT_EQ(test, 0,
  2583. of_overlay_apply_kunit(test, kunit_clk_parent_data_test));
  2584. return 0;
  2585. }
  2586. /*
  2587. * Test suite for registering clks with struct clk_parent_data and a struct
  2588. * device.
  2589. */
  2590. static struct kunit_suite clk_register_clk_parent_data_device_suite = {
  2591. .name = "clk_register_clk_parent_data_device",
  2592. .init = clk_register_clk_parent_data_device_init,
  2593. .test_cases = clk_register_clk_parent_data_device_test_cases,
  2594. };
  2595. struct clk_assigned_rates_context {
  2596. struct clk_dummy_context clk0;
  2597. struct clk_dummy_context clk1;
  2598. };
  2599. /*
  2600. * struct clk_assigned_rates_test_param - Test parameters for clk_assigned_rates test
  2601. * @desc: Test description
  2602. * @overlay_begin: Pointer to start of DT overlay to apply for test
  2603. * @overlay_end: Pointer to end of DT overlay to apply for test
  2604. * @rate0: Initial rate of first clk
  2605. * @rate1: Initial rate of second clk
  2606. * @consumer_test: true if a consumer is being tested
  2607. */
  2608. struct clk_assigned_rates_test_param {
  2609. const char *desc;
  2610. u8 *overlay_begin;
  2611. u8 *overlay_end;
  2612. unsigned long rate0;
  2613. unsigned long rate1;
  2614. bool consumer_test;
  2615. };
  2616. #define TEST_PARAM_OVERLAY(overlay_name) \
  2617. .overlay_begin = of_overlay_begin(overlay_name), \
  2618. .overlay_end = of_overlay_end(overlay_name)
  2619. static void
  2620. clk_assigned_rates_register_clk(struct kunit *test,
  2621. struct clk_dummy_context *ctx,
  2622. struct device_node *np, const char *name,
  2623. unsigned long rate)
  2624. {
  2625. struct clk_init_data init = { };
  2626. init.name = name;
  2627. init.ops = &clk_dummy_rate_ops;
  2628. ctx->hw.init = &init;
  2629. ctx->rate = rate;
  2630. KUNIT_ASSERT_EQ(test, 0, of_clk_hw_register_kunit(test, np, &ctx->hw));
  2631. KUNIT_ASSERT_EQ(test, ctx->rate, rate);
  2632. }
  2633. /*
  2634. * Does most of the work of the test:
  2635. *
  2636. * 1. Apply the overlay to test
  2637. * 2. Register the clk or clks to test
  2638. * 3. Register the clk provider
  2639. * 4. Apply clk defaults to the consumer device if this is a consumer test
  2640. *
  2641. * The tests will set different test_param values to test different scenarios
  2642. * and validate that in their test functions.
  2643. */
  2644. static int clk_assigned_rates_test_init(struct kunit *test)
  2645. {
  2646. struct device_node *np, *consumer;
  2647. struct clk_hw_onecell_data *data;
  2648. struct clk_assigned_rates_context *ctx;
  2649. u32 clk_cells;
  2650. const struct clk_assigned_rates_test_param *test_param;
  2651. test_param = test->param_value;
  2652. KUNIT_ASSERT_EQ(test, 0, __of_overlay_apply_kunit(test,
  2653. test_param->overlay_begin,
  2654. test_param->overlay_end));
  2655. KUNIT_ASSERT_NOT_ERR_OR_NULL(test,
  2656. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL));
  2657. test->priv = ctx;
  2658. KUNIT_ASSERT_NOT_ERR_OR_NULL(test,
  2659. np = of_find_compatible_node(NULL, NULL, "test,clk-assigned-rates"));
  2660. of_node_put_kunit(test, np);
  2661. KUNIT_ASSERT_EQ(test, 0, of_property_read_u32(np, "#clock-cells", &clk_cells));
  2662. /* Only support #clock-cells = <0> or <1> */
  2663. KUNIT_ASSERT_LT(test, clk_cells, 2);
  2664. clk_assigned_rates_register_clk(test, &ctx->clk0, np,
  2665. "test_assigned_rate0", test_param->rate0);
  2666. if (clk_cells == 0) {
  2667. KUNIT_ASSERT_EQ(test, 0,
  2668. of_clk_add_hw_provider_kunit(test, np, of_clk_hw_simple_get,
  2669. &ctx->clk0.hw));
  2670. } else if (clk_cells == 1) {
  2671. clk_assigned_rates_register_clk(test, &ctx->clk1, np,
  2672. "test_assigned_rate1", test_param->rate1);
  2673. KUNIT_ASSERT_NOT_ERR_OR_NULL(test,
  2674. data = kunit_kzalloc(test, struct_size(data, hws, 2), GFP_KERNEL));
  2675. data->num = 2;
  2676. data->hws[0] = &ctx->clk0.hw;
  2677. data->hws[1] = &ctx->clk1.hw;
  2678. KUNIT_ASSERT_EQ(test, 0,
  2679. of_clk_add_hw_provider_kunit(test, np, of_clk_hw_onecell_get, data));
  2680. }
  2681. /* Consumers are optional */
  2682. if (test_param->consumer_test) {
  2683. KUNIT_ASSERT_NOT_ERR_OR_NULL(test,
  2684. consumer = of_find_compatible_node(NULL, NULL, "test,clk-consumer"));
  2685. of_node_put_kunit(test, consumer);
  2686. KUNIT_ASSERT_EQ(test, 0, of_clk_set_defaults(consumer, false));
  2687. }
  2688. return 0;
  2689. }
  2690. static void clk_assigned_rates_assigns_one(struct kunit *test)
  2691. {
  2692. struct clk_assigned_rates_context *ctx = test->priv;
  2693. KUNIT_EXPECT_EQ(test, ctx->clk0.rate, ASSIGNED_RATES_0_RATE);
  2694. }
  2695. static void clk_assigned_rates_assigns_multiple(struct kunit *test)
  2696. {
  2697. struct clk_assigned_rates_context *ctx = test->priv;
  2698. KUNIT_EXPECT_EQ(test, ctx->clk0.rate, ASSIGNED_RATES_0_RATE);
  2699. KUNIT_EXPECT_EQ(test, ctx->clk1.rate, ASSIGNED_RATES_1_RATE);
  2700. }
  2701. static void clk_assigned_rates_skips(struct kunit *test)
  2702. {
  2703. struct clk_assigned_rates_context *ctx = test->priv;
  2704. const struct clk_assigned_rates_test_param *test_param = test->param_value;
  2705. KUNIT_EXPECT_NE(test, ctx->clk0.rate, ASSIGNED_RATES_0_RATE);
  2706. KUNIT_EXPECT_EQ(test, ctx->clk0.rate, test_param->rate0);
  2707. }
  2708. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_one);
  2709. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_one_consumer);
  2710. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_u64_one);
  2711. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_u64_one_consumer);
  2712. /* Test cases that assign one rate */
  2713. static const struct clk_assigned_rates_test_param clk_assigned_rates_assigns_one_test_params[] = {
  2714. {
  2715. /*
  2716. * Test that a single cell assigned-clock-rates property
  2717. * assigns the rate when the property is in the provider.
  2718. */
  2719. .desc = "provider assigns",
  2720. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_one),
  2721. },
  2722. {
  2723. /*
  2724. * Test that a single cell assigned-clock-rates property
  2725. * assigns the rate when the property is in the consumer.
  2726. */
  2727. .desc = "consumer assigns",
  2728. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_one_consumer),
  2729. .consumer_test = true,
  2730. },
  2731. {
  2732. /*
  2733. * Test that a single cell assigned-clock-rates-u64 property
  2734. * assigns the rate when the property is in the provider.
  2735. */
  2736. .desc = "provider assigns u64",
  2737. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_u64_one),
  2738. },
  2739. {
  2740. /*
  2741. * Test that a single cell assigned-clock-rates-u64 property
  2742. * assigns the rate when the property is in the consumer.
  2743. */
  2744. .desc = "consumer assigns u64",
  2745. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_u64_one_consumer),
  2746. .consumer_test = true,
  2747. },
  2748. };
  2749. KUNIT_ARRAY_PARAM_DESC(clk_assigned_rates_assigns_one,
  2750. clk_assigned_rates_assigns_one_test_params, desc)
  2751. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_multiple);
  2752. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_multiple_consumer);
  2753. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_u64_multiple);
  2754. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_u64_multiple_consumer);
  2755. /* Test cases that assign multiple rates */
  2756. static const struct clk_assigned_rates_test_param clk_assigned_rates_assigns_multiple_test_params[] = {
  2757. {
  2758. /*
  2759. * Test that a multiple cell assigned-clock-rates property
  2760. * assigns the rates when the property is in the provider.
  2761. */
  2762. .desc = "provider assigns",
  2763. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_multiple),
  2764. },
  2765. {
  2766. /*
  2767. * Test that a multiple cell assigned-clock-rates property
  2768. * assigns the rates when the property is in the consumer.
  2769. */
  2770. .desc = "consumer assigns",
  2771. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_multiple_consumer),
  2772. .consumer_test = true,
  2773. },
  2774. {
  2775. /*
  2776. * Test that a single cell assigned-clock-rates-u64 property
  2777. * assigns the rate when the property is in the provider.
  2778. */
  2779. .desc = "provider assigns u64",
  2780. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_u64_multiple),
  2781. },
  2782. {
  2783. /*
  2784. * Test that a multiple cell assigned-clock-rates-u64 property
  2785. * assigns the rates when the property is in the consumer.
  2786. */
  2787. .desc = "consumer assigns u64",
  2788. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_u64_multiple_consumer),
  2789. .consumer_test = true,
  2790. },
  2791. };
  2792. KUNIT_ARRAY_PARAM_DESC(clk_assigned_rates_assigns_multiple,
  2793. clk_assigned_rates_assigns_multiple_test_params,
  2794. desc)
  2795. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_without);
  2796. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_without_consumer);
  2797. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_zero);
  2798. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_zero_consumer);
  2799. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_null);
  2800. OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_null_consumer);
  2801. /* Test cases that skip changing the rate due to malformed DT */
  2802. static const struct clk_assigned_rates_test_param clk_assigned_rates_skips_test_params[] = {
  2803. {
  2804. /*
  2805. * Test that an assigned-clock-rates property without an assigned-clocks
  2806. * property fails when the property is in the provider.
  2807. */
  2808. .desc = "provider missing assigned-clocks",
  2809. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_without),
  2810. .rate0 = 3000,
  2811. },
  2812. {
  2813. /*
  2814. * Test that an assigned-clock-rates property without an assigned-clocks
  2815. * property fails when the property is in the consumer.
  2816. */
  2817. .desc = "consumer missing assigned-clocks",
  2818. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_without_consumer),
  2819. .rate0 = 3000,
  2820. .consumer_test = true,
  2821. },
  2822. {
  2823. /*
  2824. * Test that an assigned-clock-rates property of zero doesn't
  2825. * set a rate when the property is in the provider.
  2826. */
  2827. .desc = "provider assigned-clock-rates of zero",
  2828. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_zero),
  2829. .rate0 = 3000,
  2830. },
  2831. {
  2832. /*
  2833. * Test that an assigned-clock-rates property of zero doesn't
  2834. * set a rate when the property is in the consumer.
  2835. */
  2836. .desc = "consumer assigned-clock-rates of zero",
  2837. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_zero_consumer),
  2838. .rate0 = 3000,
  2839. .consumer_test = true,
  2840. },
  2841. {
  2842. /*
  2843. * Test that an assigned-clocks property with a null phandle
  2844. * doesn't set a rate when the property is in the provider.
  2845. */
  2846. .desc = "provider assigned-clocks null phandle",
  2847. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_null),
  2848. .rate0 = 3000,
  2849. },
  2850. {
  2851. /*
  2852. * Test that an assigned-clocks property with a null phandle
  2853. * doesn't set a rate when the property is in the consumer.
  2854. */
  2855. .desc = "provider assigned-clocks null phandle",
  2856. TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_null_consumer),
  2857. .rate0 = 3000,
  2858. .consumer_test = true,
  2859. },
  2860. };
  2861. KUNIT_ARRAY_PARAM_DESC(clk_assigned_rates_skips,
  2862. clk_assigned_rates_skips_test_params,
  2863. desc)
  2864. static struct kunit_case clk_assigned_rates_test_cases[] = {
  2865. KUNIT_CASE_PARAM(clk_assigned_rates_assigns_one,
  2866. clk_assigned_rates_assigns_one_gen_params),
  2867. KUNIT_CASE_PARAM(clk_assigned_rates_assigns_multiple,
  2868. clk_assigned_rates_assigns_multiple_gen_params),
  2869. KUNIT_CASE_PARAM(clk_assigned_rates_skips,
  2870. clk_assigned_rates_skips_gen_params),
  2871. {}
  2872. };
  2873. /*
  2874. * Test suite for assigned-clock-rates{-u64} DT property.
  2875. */
  2876. static struct kunit_suite clk_assigned_rates_suite = {
  2877. .name = "clk_assigned_rates",
  2878. .test_cases = clk_assigned_rates_test_cases,
  2879. .init = clk_assigned_rates_test_init,
  2880. };
  2881. static const struct clk_init_data clk_hw_get_dev_of_node_init_data = {
  2882. .name = "clk_hw_get_dev_of_node",
  2883. .ops = &empty_clk_ops,
  2884. };
  2885. /*
  2886. * Test that a clk registered with a struct device returns the device from
  2887. * clk_hw_get_dev() and the node from clk_hw_get_of_node()
  2888. */
  2889. static void clk_hw_register_dev_get_dev_returns_dev(struct kunit *test)
  2890. {
  2891. struct device *dev;
  2892. struct clk_hw *hw;
  2893. static const struct of_device_id match_table[] = {
  2894. { .compatible = "test,clk-hw-get-dev-of-node" },
  2895. { }
  2896. };
  2897. KUNIT_ASSERT_EQ(test, 0, of_overlay_apply_kunit(test, kunit_clk_hw_get_dev_of_node));
  2898. dev = kunit_of_platform_driver_dev(test, match_table);
  2899. hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
  2900. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
  2901. hw->init = &clk_hw_get_dev_of_node_init_data;
  2902. KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, dev, hw));
  2903. KUNIT_EXPECT_PTR_EQ(test, dev, clk_hw_get_dev(hw));
  2904. KUNIT_EXPECT_PTR_EQ(test, dev_of_node(dev), clk_hw_get_of_node(hw));
  2905. }
  2906. /*
  2907. * Test that a clk registered with a struct device that's not associated with
  2908. * an OF node returns the device from clk_hw_get_dev() and NULL from
  2909. * clk_hw_get_of_node()
  2910. */
  2911. static void clk_hw_register_dev_no_node_get_dev_returns_dev(struct kunit *test)
  2912. {
  2913. struct platform_device *pdev;
  2914. struct device *dev;
  2915. struct clk_hw *hw;
  2916. pdev = kunit_platform_device_alloc(test, "clk_hw_register_dev_no_node", -1);
  2917. KUNIT_ASSERT_NOT_NULL(test, pdev);
  2918. KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev));
  2919. dev = &pdev->dev;
  2920. hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
  2921. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
  2922. hw->init = &clk_hw_get_dev_of_node_init_data;
  2923. KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, dev, hw));
  2924. KUNIT_EXPECT_PTR_EQ(test, dev, clk_hw_get_dev(hw));
  2925. KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_get_of_node(hw));
  2926. }
  2927. /*
  2928. * Test that a clk registered without a struct device returns NULL from
  2929. * clk_hw_get_dev()
  2930. */
  2931. static void clk_hw_register_NULL_get_dev_of_node_returns_NULL(struct kunit *test)
  2932. {
  2933. struct clk_hw *hw;
  2934. hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
  2935. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
  2936. hw->init = &clk_hw_get_dev_of_node_init_data;
  2937. KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, NULL, hw));
  2938. KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_get_dev(hw));
  2939. KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_get_of_node(hw));
  2940. }
  2941. /*
  2942. * Test that a clk registered with an of_node returns the node from
  2943. * clk_hw_get_of_node() and NULL from clk_hw_get_dev()
  2944. */
  2945. static void of_clk_hw_register_node_get_of_node_returns_node(struct kunit *test)
  2946. {
  2947. struct device_node *np;
  2948. struct clk_hw *hw;
  2949. hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
  2950. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
  2951. KUNIT_ASSERT_EQ(test, 0, of_overlay_apply_kunit(test, kunit_clk_hw_get_dev_of_node));
  2952. np = of_find_compatible_node(NULL, NULL, "test,clk-hw-get-dev-of-node");
  2953. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, np);
  2954. of_node_put_kunit(test, np);
  2955. hw->init = &clk_hw_get_dev_of_node_init_data;
  2956. KUNIT_ASSERT_EQ(test, 0, of_clk_hw_register_kunit(test, np, hw));
  2957. KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_get_dev(hw));
  2958. KUNIT_EXPECT_PTR_EQ(test, np, clk_hw_get_of_node(hw));
  2959. }
  2960. /*
  2961. * Test that a clk registered without an of_node returns the node from
  2962. * clk_hw_get_of_node() and clk_hw_get_dev()
  2963. */
  2964. static void of_clk_hw_register_NULL_get_of_node_returns_NULL(struct kunit *test)
  2965. {
  2966. struct clk_hw *hw;
  2967. hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
  2968. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
  2969. hw->init = &clk_hw_get_dev_of_node_init_data;
  2970. KUNIT_ASSERT_EQ(test, 0, of_clk_hw_register_kunit(test, NULL, hw));
  2971. KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_get_dev(hw));
  2972. KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_get_of_node(hw));
  2973. }
  2974. static struct kunit_case clk_hw_get_dev_of_node_test_cases[] = {
  2975. KUNIT_CASE(clk_hw_register_dev_get_dev_returns_dev),
  2976. KUNIT_CASE(clk_hw_register_dev_no_node_get_dev_returns_dev),
  2977. KUNIT_CASE(clk_hw_register_NULL_get_dev_of_node_returns_NULL),
  2978. KUNIT_CASE(of_clk_hw_register_node_get_of_node_returns_node),
  2979. KUNIT_CASE(of_clk_hw_register_NULL_get_of_node_returns_NULL),
  2980. {}
  2981. };
  2982. /*
  2983. * Test suite to verify clk_hw_get_dev() and clk_hw_get_of_node() when clk
  2984. * registered with clk_hw_register() and of_clk_hw_register()
  2985. */
  2986. static struct kunit_suite clk_hw_get_dev_of_node_test_suite = {
  2987. .name = "clk_hw_get_dev_of_node_test_suite",
  2988. .test_cases = clk_hw_get_dev_of_node_test_cases,
  2989. };
  2990. kunit_test_suites(
  2991. &clk_assigned_rates_suite,
  2992. &clk_hw_get_dev_of_node_test_suite,
  2993. &clk_leaf_mux_set_rate_parent_test_suite,
  2994. &clk_test_suite,
  2995. &clk_multiple_parents_mux_test_suite,
  2996. &clk_mux_no_reparent_test_suite,
  2997. &clk_mux_notifier_test_suite,
  2998. &clk_orphan_transparent_multiple_parent_mux_test_suite,
  2999. &clk_orphan_transparent_single_parent_test_suite,
  3000. &clk_orphan_two_level_root_last_test_suite,
  3001. &clk_range_test_suite,
  3002. &clk_range_maximize_test_suite,
  3003. &clk_range_minimize_test_suite,
  3004. &clk_register_clk_parent_data_of_suite,
  3005. &clk_register_clk_parent_data_device_suite,
  3006. &clk_single_parent_mux_test_suite,
  3007. &clk_uncached_test_suite,
  3008. );
  3009. MODULE_DESCRIPTION("Kunit tests for clk framework");
  3010. MODULE_LICENSE("GPL v2");