sde_rotator_dev.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "%s:%d: " fmt, __func__, __LINE__
  6. #include <linux/vmalloc.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <linux/delay.h>
  13. #include <linux/wait.h>
  14. #include <linux/of.h>
  15. #include <media/v4l2-ioctl.h>
  16. #include <media/v4l2-event.h>
  17. #include <media/videobuf2-v4l2.h>
  18. #include <media/v4l2-mem2mem.h>
  19. #include "sde_rotator_inline.h"
  20. #include "sde_rotator_base.h"
  21. #include "sde_rotator_core.h"
  22. #include "sde_rotator_dev.h"
  23. #include "sde_rotator_debug.h"
  24. #include "sde_rotator_trace.h"
  25. /* Start v4l2 device number (default allocation) */
  26. #define SDE_ROTATOR_BASE_DEVICE_NUMBER -1
  27. /* Default value for early_submit flag */
  28. #define SDE_ROTATOR_EARLY_SUBMIT 1
  29. /* Timeout (msec) waiting for stream to turn off. */
  30. #define SDE_ROTATOR_STREAM_OFF_TIMEOUT 500
  31. /* acquire fence time out, following other driver fence time out practice */
  32. #define SDE_ROTATOR_FENCE_TIMEOUT MSEC_PER_SEC
  33. /* Timeout (msec) waiting for ctx open */
  34. #define SDE_ROTATOR_CTX_OPEN_TIMEOUT 500
  35. /* Rotator default fps */
  36. #define SDE_ROTATOR_DEFAULT_FPS 60
  37. /* Rotator rotation angles */
  38. #define SDE_ROTATOR_DEGREE_270 270
  39. #define SDE_ROTATOR_DEGREE_180 180
  40. #define SDE_ROTATOR_DEGREE_90 90
  41. /* Inline rotator qos request */
  42. #define SDE_ROTATOR_ADD_REQUEST 1
  43. #define SDE_ROTATOR_REMOVE_REQUEST 0
  44. static void sde_rotator_submit_handler(struct kthread_work *work);
  45. static void sde_rotator_retire_handler(struct kthread_work *work);
  46. static void sde_rotator_pm_qos_request(struct sde_rotator_device *rot_dev,
  47. bool add_request);
  48. #ifdef CONFIG_COMPAT
  49. static long sde_rotator_compat_ioctl32(struct file *file,
  50. unsigned int cmd, unsigned long arg);
  51. #endif
  52. /*
  53. * sde_rotator_ctx_from_fh - Get rotator context from v4l2 fh.
  54. * @fh: Pointer to v4l2 fh.
  55. */
  56. static inline struct sde_rotator_ctx *sde_rotator_ctx_from_fh(
  57. struct v4l2_fh *fh)
  58. {
  59. return container_of(fh, struct sde_rotator_ctx, fh);
  60. }
  61. /*
  62. * sde_rotator_get_flags_from_ctx - Get low-level command flag
  63. * @ctx: Pointer to rotator context.
  64. */
  65. static uint32_t sde_rotator_get_flags_from_ctx(struct sde_rotator_ctx *ctx)
  66. {
  67. uint32_t ret_flags = 0;
  68. if (ctx->rotate == SDE_ROTATOR_DEGREE_270)
  69. ret_flags |= SDE_ROTATION_270;
  70. else if (ctx->rotate == SDE_ROTATOR_DEGREE_180)
  71. ret_flags |= SDE_ROTATION_180;
  72. else if (ctx->rotate == SDE_ROTATOR_DEGREE_90)
  73. ret_flags |= SDE_ROTATION_90;
  74. if (ctx->hflip)
  75. ret_flags ^= SDE_ROTATION_FLIP_LR;
  76. if (ctx->vflip)
  77. ret_flags ^= SDE_ROTATION_FLIP_UD;
  78. if (ctx->secure)
  79. ret_flags |= SDE_ROTATION_SECURE;
  80. if (ctx->secure_camera)
  81. ret_flags |= SDE_ROTATION_SECURE_CAMERA;
  82. if (ctx->format_out.fmt.pix.field == V4L2_FIELD_INTERLACED &&
  83. ctx->format_cap.fmt.pix.field == V4L2_FIELD_NONE)
  84. ret_flags |= SDE_ROTATION_DEINTERLACE;
  85. return ret_flags;
  86. }
  87. /*
  88. * sde_rotator_get_config_from_ctx - Fill rotator configure structure.
  89. * @ctx: Pointer to rotator ctx.
  90. * @config: Pointer to config structure.
  91. */
  92. static void sde_rotator_get_config_from_ctx(struct sde_rotator_ctx *ctx,
  93. struct sde_rotation_config *config)
  94. {
  95. memset(config, 0, sizeof(struct sde_rotation_config));
  96. config->flags = sde_rotator_get_flags_from_ctx(ctx);
  97. config->frame_rate = (ctx->timeperframe.numerator) ?
  98. ctx->timeperframe.denominator
  99. / ctx->timeperframe.numerator : 0;
  100. config->session_id = ctx->session_id;
  101. config->input.width = ctx->crop_out.width;
  102. config->input.height = ctx->crop_out.height;
  103. config->input.format = ctx->format_out.fmt.pix.pixelformat;
  104. config->input.comp_ratio.numer = 1;
  105. config->input.comp_ratio.denom = 1;
  106. config->output.width = ctx->crop_cap.width;
  107. config->output.height = ctx->crop_cap.height;
  108. config->output.format = ctx->format_cap.fmt.pix.pixelformat;
  109. config->output.comp_ratio.numer = 1;
  110. config->output.comp_ratio.denom = 1;
  111. /*
  112. * Use compression ratio of the first buffer to estimate
  113. * performance requirement of the session. If core layer does
  114. * not support dynamic per buffer compression ratio recalculation,
  115. * this configuration will determine the overall static compression
  116. * ratio of the session.
  117. */
  118. if (ctx->vbinfo_out)
  119. config->input.comp_ratio = ctx->vbinfo_out[0].comp_ratio;
  120. if (ctx->vbinfo_cap)
  121. config->output.comp_ratio = ctx->vbinfo_cap[0].comp_ratio;
  122. SDEDEV_DBG(ctx->rot_dev->dev, "config s:%d out_cr:%u/%u cap_cr:%u/%u\n",
  123. ctx->session_id,
  124. config->input.comp_ratio.numer,
  125. config->input.comp_ratio.denom,
  126. config->output.comp_ratio.numer,
  127. config->output.comp_ratio.denom);
  128. }
  129. /*
  130. * sde_rotator_get_item_from_ctx - Fill rotator item structure.
  131. * @ctx: Pointer to rotator ctx.
  132. * @item: Pointer to item structure.
  133. */
  134. static void sde_rotator_get_item_from_ctx(struct sde_rotator_ctx *ctx,
  135. struct sde_rotation_item *item)
  136. {
  137. memset(item, 0, sizeof(struct sde_rotation_item));
  138. item->flags = sde_rotator_get_flags_from_ctx(ctx);
  139. item->session_id = ctx->session_id;
  140. item->sequence_id = 0;
  141. /* assign high/low priority */
  142. item->wb_idx = (ctx->fh.prio >= V4L2_PRIORITY_DEFAULT) ? 0 : 1;
  143. item->src_rect.x = ctx->crop_out.left;
  144. item->src_rect.y = ctx->crop_out.top;
  145. item->src_rect.w = ctx->crop_out.width;
  146. item->src_rect.h = ctx->crop_out.height;
  147. item->input.width = ctx->format_out.fmt.pix.width;
  148. item->input.height = ctx->format_out.fmt.pix.height;
  149. item->input.format = ctx->format_out.fmt.pix.pixelformat;
  150. item->input.planes[0].fd = -1;
  151. item->input.planes[0].offset = 0;
  152. item->input.planes[0].stride = ctx->format_out.fmt.pix.bytesperline;
  153. item->input.plane_count = 1;
  154. item->input.fence = NULL;
  155. item->input.comp_ratio.numer = 1;
  156. item->input.comp_ratio.denom = 1;
  157. item->dst_rect.x = ctx->crop_cap.left;
  158. item->dst_rect.y = ctx->crop_cap.top;
  159. item->dst_rect.w = ctx->crop_cap.width;
  160. item->dst_rect.h = ctx->crop_cap.height;
  161. item->output.width = ctx->format_cap.fmt.pix.width;
  162. item->output.height = ctx->format_cap.fmt.pix.height;
  163. item->output.format = ctx->format_cap.fmt.pix.pixelformat;
  164. item->output.planes[0].fd = -1;
  165. item->output.planes[0].offset = 0;
  166. item->output.planes[0].stride = ctx->format_cap.fmt.pix.bytesperline;
  167. item->output.plane_count = 1;
  168. item->output.fence = NULL;
  169. item->output.comp_ratio.numer = 1;
  170. item->output.comp_ratio.denom = 1;
  171. }
  172. /*
  173. * sde_rotator_format_recalc - Recalculate format parameters.
  174. * @f: v4l2 format.
  175. */
  176. static void sde_rotator_format_recalc(struct v4l2_format *f)
  177. {
  178. int ret;
  179. struct sde_mdp_format_params *fmt;
  180. struct sde_mdp_plane_sizes ps;
  181. fmt = sde_get_format_params(f->fmt.pix.pixelformat);
  182. if (!fmt) {
  183. SDEROT_ERR("invalid format\n");
  184. goto error_fmt;
  185. }
  186. ret = sde_mdp_get_plane_sizes(fmt,
  187. f->fmt.pix.width, f->fmt.pix.height, &ps, 0, 0);
  188. if (ret) {
  189. SDEROT_ERR("invalid plane size\n");
  190. goto error_fmt;
  191. }
  192. f->fmt.pix.bytesperline = ps.ystride[0];
  193. f->fmt.pix.sizeimage = ps.total_size;
  194. return;
  195. error_fmt:
  196. f->fmt.pix.bytesperline = 0;
  197. f->fmt.pix.sizeimage = 0;
  198. }
  199. /*
  200. * sde_rotator_validate_item - Check if rotator item is valid for processing.
  201. * @ctx: Pointer to rotator ctx.
  202. * @item: Pointer to item structure
  203. */
  204. static int sde_rotator_validate_item(struct sde_rotator_ctx *ctx,
  205. struct sde_rotation_item *item)
  206. {
  207. int ret;
  208. struct sde_rot_entry_container *req;
  209. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  210. sde_rot_mgr_lock(rot_dev->mgr);
  211. req = sde_rotator_req_init(rot_dev->mgr, ctx->private, item, 1, 0);
  212. if (IS_ERR_OR_NULL(req)) {
  213. SDEDEV_ERR(rot_dev->dev, "fail allocate item\n");
  214. return -ENOMEM;
  215. }
  216. ret = sde_rotator_validate_request(rot_dev->mgr, ctx->private, req);
  217. sde_rot_mgr_unlock(rot_dev->mgr);
  218. devm_kfree(rot_dev->dev, req);
  219. return ret;
  220. }
  221. /*
  222. * sde_rotator_queue_setup - vb2_ops queue_setup callback.
  223. * @q: Pointer to vb2 queue struct.
  224. * @num_buffers: Pointer of number of buffers requested.
  225. * @num_planes: Pointer to number of planes requested.
  226. * @sizes: Array containing sizes of planes.
  227. * @alloc_ctxs: Array of allocated contexts for each plane.
  228. */
  229. static int sde_rotator_queue_setup(struct vb2_queue *q,
  230. unsigned int *num_buffers, unsigned int *num_planes,
  231. unsigned int sizes[], struct device *alloc_devs[])
  232. {
  233. struct sde_rotator_ctx *ctx = vb2_get_drv_priv(q);
  234. int i;
  235. if (!num_buffers)
  236. return -EINVAL;
  237. switch (q->type) {
  238. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  239. sizes[0] = ctx->format_out.fmt.pix.sizeimage;
  240. break;
  241. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  242. sizes[0] = ctx->format_cap.fmt.pix.sizeimage;
  243. break;
  244. default:
  245. return -EINVAL;
  246. }
  247. *num_planes = 1;
  248. alloc_devs[0] = (struct device *)ctx;
  249. switch (q->type) {
  250. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  251. ctx->nbuf_out = *num_buffers;
  252. kfree(ctx->vbinfo_out);
  253. ctx->vbinfo_out = kcalloc(ctx->nbuf_out,
  254. sizeof(struct sde_rotator_vbinfo), GFP_KERNEL);
  255. if (!ctx->vbinfo_out)
  256. return -ENOMEM;
  257. for (i = 0; i < ctx->nbuf_out; i++) {
  258. ctx->vbinfo_out[i].fd = -1;
  259. ctx->vbinfo_out[i].comp_ratio.numer = 1;
  260. ctx->vbinfo_out[i].comp_ratio.denom = 1;
  261. }
  262. break;
  263. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  264. ctx->nbuf_cap = *num_buffers;
  265. kfree(ctx->vbinfo_cap);
  266. ctx->vbinfo_cap = kcalloc(ctx->nbuf_cap,
  267. sizeof(struct sde_rotator_vbinfo), GFP_KERNEL);
  268. if (!ctx->vbinfo_cap)
  269. return -ENOMEM;
  270. for (i = 0; i < ctx->nbuf_cap; i++) {
  271. ctx->vbinfo_cap[i].fd = -1;
  272. ctx->vbinfo_cap[i].comp_ratio.numer = 1;
  273. ctx->vbinfo_cap[i].comp_ratio.denom = 1;
  274. }
  275. break;
  276. default:
  277. return -EINVAL;
  278. }
  279. return 0;
  280. }
  281. /*
  282. * sde_rotator_buf_queue - vb2_ops buf_queue callback.
  283. * @vb: Pointer to vb2 buffer struct.
  284. */
  285. static void sde_rotator_buf_queue(struct vb2_buffer *vb)
  286. {
  287. struct sde_rotator_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  288. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  289. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  290. }
  291. /*
  292. * sde_rotator_buf_finish - vb2_ops buf_finish to finalize buffer before going
  293. * back to user space
  294. * @vb: Pointer to vb2 buffer struct.
  295. */
  296. static void sde_rotator_buf_finish(struct vb2_buffer *vb)
  297. {
  298. struct sde_rotator_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  299. int i;
  300. SDEDEV_DBG(ctx->rot_dev->dev,
  301. "buf_finish t:%d i:%d s:%d m:%u np:%d up:%lu\n",
  302. vb->type, vb->index, vb->state,
  303. vb->vb2_queue->memory,
  304. vb->num_planes,
  305. vb->planes[0].m.userptr);
  306. if (vb->vb2_queue->memory != VB2_MEMORY_USERPTR)
  307. return;
  308. /*
  309. * We use userptr to tunnel fd, and fd can be the same across qbuf
  310. * even though the underlying buffer is different. Since vb2 layer
  311. * optimizes memory mapping for userptr by first checking if userptr
  312. * has changed, it will not trigger put_userptr if fd value does
  313. * not change. In order to force buffer release, we need to clear
  314. * userptr when the current buffer is done and ready to go back to
  315. * user mode. Since 0 is a valid fd, reset userptr to -1 instead.
  316. */
  317. for (i = 0; i < vb->num_planes; i++)
  318. vb->planes[i].m.userptr = ~0;
  319. }
  320. /*
  321. * sde_rotator_return_all_buffers - Return all buffers with the given status.
  322. * @q: Pointer to vb2 buffer queue struct.
  323. * @state: State of the buffer
  324. */
  325. static void sde_rotator_return_all_buffers(struct vb2_queue *q,
  326. enum vb2_buffer_state state)
  327. {
  328. struct sde_rotator_ctx *ctx = vb2_get_drv_priv(q);
  329. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  330. SDEDEV_DBG(rot_dev->dev,
  331. "return q t:%d c:%d dc:%d s:%d\n",
  332. q->type, q->queued_count,
  333. atomic_read(&q->owned_by_drv_count),
  334. state);
  335. /* return buffers according videobuffer2-core.h */
  336. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  337. struct vb2_v4l2_buffer *buf;
  338. while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx))) {
  339. SDEDEV_DBG(rot_dev->dev,
  340. "return vb t:%d i:%d\n",
  341. buf->vb2_buf.type,
  342. buf->vb2_buf.index);
  343. v4l2_m2m_buf_done(buf, state);
  344. }
  345. } else if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  346. struct vb2_v4l2_buffer *buf;
  347. while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx))) {
  348. SDEDEV_DBG(rot_dev->dev,
  349. "return vb t:%d i:%d\n",
  350. buf->vb2_buf.type,
  351. buf->vb2_buf.index);
  352. v4l2_m2m_buf_done(buf, state);
  353. }
  354. } else {
  355. SDEDEV_ERR(rot_dev->dev, "unsupported vb t:%d\n", q->type);
  356. }
  357. }
  358. /*
  359. * sde_rotator_start_streaming - vb2_ops start_streaming callback.
  360. * @q: Pointer to vb2 queue struct.
  361. * @count: Number of buffer queued before stream on call.
  362. */
  363. static int sde_rotator_start_streaming(struct vb2_queue *q, unsigned int count)
  364. {
  365. struct sde_rotator_ctx *ctx = vb2_get_drv_priv(q);
  366. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  367. SDEDEV_DBG(rot_dev->dev, "start streaming s:%d t:%d\n",
  368. ctx->session_id, q->type);
  369. if (!list_empty(&ctx->pending_list)) {
  370. SDEDEV_ERR(rot_dev->dev,
  371. "command pending error s:%d t:%d p:%d\n",
  372. ctx->session_id, q->type,
  373. !list_empty(&ctx->pending_list));
  374. return -EINVAL;
  375. }
  376. ctx->abort_pending = 0;
  377. return 0;
  378. }
  379. /*
  380. * sde_rotator_stop_streaming - vb2_ops stop_streaming callback.
  381. * @q: Pointer to vb2 queue struct.
  382. *
  383. * This function will block waiting for stream to stop. Unlock queue
  384. * lock to avoid deadlock.
  385. */
  386. static void sde_rotator_stop_streaming(struct vb2_queue *q)
  387. {
  388. struct sde_rotator_ctx *ctx = vb2_get_drv_priv(q);
  389. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  390. struct sde_rotator_request *request;
  391. struct list_head *curr, *next;
  392. int i;
  393. int ret;
  394. SDEDEV_DBG(rot_dev->dev, "stop streaming s:%d t:%d p:%d\n",
  395. ctx->session_id, q->type,
  396. !list_empty(&ctx->pending_list));
  397. ctx->abort_pending = 1;
  398. mutex_unlock(q->lock);
  399. ret = wait_event_timeout(ctx->wait_queue,
  400. list_empty(&ctx->pending_list),
  401. msecs_to_jiffies(rot_dev->streamoff_timeout));
  402. mutex_lock(q->lock);
  403. if (!ret) {
  404. SDEDEV_ERR(rot_dev->dev,
  405. "timeout to stream off s:%d t:%d p:%d\n",
  406. ctx->session_id, q->type,
  407. !list_empty(&ctx->pending_list));
  408. SDEROT_EVTLOG(ctx->session_id, q->type,
  409. !list_empty(&ctx->pending_list),
  410. SDE_ROT_EVTLOG_ERROR);
  411. sde_rot_mgr_lock(rot_dev->mgr);
  412. sde_rotator_cancel_all_requests(rot_dev->mgr, ctx->private);
  413. sde_rot_mgr_unlock(rot_dev->mgr);
  414. list_for_each_safe(curr, next, &ctx->pending_list) {
  415. request = container_of(curr, struct sde_rotator_request,
  416. list);
  417. SDEDEV_DBG(rot_dev->dev, "cancel request s:%d\n",
  418. ctx->session_id);
  419. mutex_unlock(q->lock);
  420. kthread_cancel_work_sync(&request->submit_work);
  421. kthread_cancel_work_sync(&request->retire_work);
  422. mutex_lock(q->lock);
  423. spin_lock(&ctx->list_lock);
  424. list_del_init(&request->list);
  425. list_add_tail(&request->list, &ctx->retired_list);
  426. spin_unlock(&ctx->list_lock);
  427. }
  428. }
  429. sde_rotator_return_all_buffers(q, VB2_BUF_STATE_ERROR);
  430. /* clear fence for buffer */
  431. sde_rotator_resync_timeline(ctx->work_queue.timeline);
  432. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  433. for (i = 0; i < ctx->nbuf_cap; i++) {
  434. struct sde_rotator_vbinfo *vbinfo =
  435. &ctx->vbinfo_cap[i];
  436. if (vbinfo->fence) {
  437. /* fence is not used */
  438. SDEDEV_DBG(rot_dev->dev,
  439. "put fence s:%d t:%d i:%d\n",
  440. ctx->session_id, q->type, i);
  441. sde_rotator_put_sync_fence(vbinfo->fence);
  442. }
  443. vbinfo->fence = NULL;
  444. vbinfo->fd = -1;
  445. }
  446. } else if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  447. for (i = 0; i < ctx->nbuf_out; i++) {
  448. struct sde_rotator_vbinfo *vbinfo =
  449. &ctx->vbinfo_out[i];
  450. if (vbinfo->fence) {
  451. SDEDEV_DBG(rot_dev->dev,
  452. "put fence s:%d t:%d i:%d\n",
  453. ctx->session_id, q->type, i);
  454. sde_rotator_put_sync_fence(vbinfo->fence);
  455. }
  456. vbinfo->fence = NULL;
  457. vbinfo->fd = -1;
  458. }
  459. }
  460. }
  461. /* Videobuf2 queue callbacks. */
  462. static const struct vb2_ops sde_rotator_vb2_q_ops = {
  463. .queue_setup = sde_rotator_queue_setup,
  464. .buf_queue = sde_rotator_buf_queue,
  465. .start_streaming = sde_rotator_start_streaming,
  466. .stop_streaming = sde_rotator_stop_streaming,
  467. .wait_prepare = vb2_ops_wait_prepare,
  468. .wait_finish = vb2_ops_wait_finish,
  469. .buf_finish = sde_rotator_buf_finish,
  470. };
  471. /*
  472. * sde_rotator_get_userptr - Map and get buffer handler for user pointer buffer.
  473. * @dev: device allocated in buf_setup.
  474. * @vaddr: Virtual addr passed from userpsace (in our case ion fd)
  475. * @size: Size of the buffer
  476. * @dma_dir: DMA data direction of the given buffer.
  477. */
  478. static void *sde_rotator_get_userptr(struct device *dev,
  479. unsigned long vaddr, unsigned long size,
  480. enum dma_data_direction dma_dir)
  481. {
  482. struct sde_rotator_ctx *ctx = (struct sde_rotator_ctx *)dev;
  483. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  484. struct sde_rotator_buf_handle *buf;
  485. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  486. if (!buf)
  487. return ERR_PTR(-ENOMEM);
  488. buf->fd = vaddr;
  489. buf->secure = ctx->secure || ctx->secure_camera;
  490. buf->ctx = ctx;
  491. buf->rot_dev = rot_dev;
  492. buf->size = size;
  493. buf->buffer = dma_buf_get(buf->fd);
  494. if (IS_ERR_OR_NULL(buf->buffer)) {
  495. SDEDEV_ERR(rot_dev->dev,
  496. "fail get dmabuf fd:%d r:%ld\n",
  497. buf->fd, PTR_ERR(buf->buffer));
  498. goto error_buf_get;
  499. }
  500. SDEDEV_DBG(rot_dev->dev,
  501. "get dmabuf s:%d fd:%d buf:%pad\n",
  502. buf->ctx->session_id,
  503. buf->fd, &buf->buffer);
  504. return buf;
  505. error_buf_get:
  506. kfree(buf);
  507. return ERR_PTR(-ENOMEM);
  508. }
  509. /*
  510. * sde_rotator_put_userptr - Unmap and free buffer handler.
  511. * @buf_priv: Buffer handler allocated get_userptr callback.
  512. */
  513. static void sde_rotator_put_userptr(void *buf_priv)
  514. {
  515. struct sde_rotator_buf_handle *buf = buf_priv;
  516. if (IS_ERR_OR_NULL(buf))
  517. return;
  518. if (!buf->rot_dev || !buf->ctx) {
  519. WARN_ON(!buf->rot_dev || !buf->ctx);
  520. SDEROT_ERR("null rotator device/context\n");
  521. return;
  522. }
  523. SDEDEV_DBG(buf->rot_dev->dev, "put dmabuf s:%d fd:%d buf:%pad\n",
  524. buf->ctx->session_id,
  525. buf->fd, &buf->buffer);
  526. if (buf->buffer) {
  527. dma_buf_put(buf->buffer);
  528. buf->buffer = NULL;
  529. }
  530. kfree(buf_priv);
  531. }
  532. /* Videobuf2 memory callbacks. */
  533. static struct vb2_mem_ops sde_rotator_vb2_mem_ops = {
  534. .get_userptr = sde_rotator_get_userptr,
  535. .put_userptr = sde_rotator_put_userptr,
  536. };
  537. /*
  538. * sde_rotator_s_ctx_ctrl - set context control variable to v4l2 control
  539. * @ctx: Pointer to rotator context.
  540. * @ctx_ctrl: Pointer to context control variable
  541. * @ctrl: Pointer to v4l2 control variable
  542. */
  543. static int sde_rotator_s_ctx_ctrl(struct sde_rotator_ctx *ctx,
  544. s32 *ctx_ctrl, struct v4l2_ctrl *ctrl)
  545. {
  546. *ctx_ctrl = ctrl->val;
  547. return 0;
  548. }
  549. /*
  550. * sde_rotator_s_ctrl - Set control.
  551. * @ctrl: Pointer to v4l2 control structure.
  552. */
  553. static int sde_rotator_s_ctrl(struct v4l2_ctrl *ctrl)
  554. {
  555. struct sde_rotator_ctx *ctx =
  556. container_of(ctrl->handler,
  557. struct sde_rotator_ctx, ctrl_handler);
  558. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  559. int ret;
  560. SDEDEV_DBG(rot_dev->dev, "set %s:%d s:%d\n", ctrl->name, ctrl->val,
  561. ctx->session_id);
  562. sde_rot_mgr_lock(rot_dev->mgr);
  563. switch (ctrl->id) {
  564. case V4L2_CID_HFLIP:
  565. ret = sde_rotator_s_ctx_ctrl(ctx, &ctx->hflip, ctrl);
  566. break;
  567. case V4L2_CID_VFLIP:
  568. ret = sde_rotator_s_ctx_ctrl(ctx, &ctx->vflip, ctrl);
  569. break;
  570. case V4L2_CID_ROTATE:
  571. ret = sde_rotator_s_ctx_ctrl(ctx, &ctx->rotate, ctrl);
  572. break;
  573. case V4L2_CID_SDE_ROTATOR_SECURE:
  574. ret = sde_rotator_s_ctx_ctrl(ctx, &ctx->secure, ctrl);
  575. break;
  576. case V4L2_CID_SDE_ROTATOR_SECURE_CAMERA:
  577. ret = sde_rotator_s_ctx_ctrl(ctx, &ctx->secure_camera, ctrl);
  578. break;
  579. default:
  580. v4l2_warn(&rot_dev->v4l2_dev, "invalid control %d\n", ctrl->id);
  581. ret = -EINVAL;
  582. }
  583. sde_rot_mgr_unlock(rot_dev->mgr);
  584. return ret;
  585. }
  586. /*
  587. * sde_rotator_ctrl_ops - Control operations.
  588. */
  589. static const struct v4l2_ctrl_ops sde_rotator_ctrl_ops = {
  590. .s_ctrl = sde_rotator_s_ctrl,
  591. };
  592. /*
  593. * sde_rotator_ctrl_secure - Non-secure/Secure.
  594. */
  595. static const struct v4l2_ctrl_config sde_rotator_ctrl_secure = {
  596. .ops = &sde_rotator_ctrl_ops,
  597. .id = V4L2_CID_SDE_ROTATOR_SECURE,
  598. .name = "Non-secure/Secure Domain",
  599. .type = V4L2_CTRL_TYPE_INTEGER,
  600. .def = 0,
  601. .min = 0,
  602. .max = 1,
  603. .step = 1,
  604. };
  605. static const struct v4l2_ctrl_config sde_rotator_ctrl_secure_camera = {
  606. .ops = &sde_rotator_ctrl_ops,
  607. .id = V4L2_CID_SDE_ROTATOR_SECURE_CAMERA,
  608. .name = "Secure Camera content",
  609. .type = V4L2_CTRL_TYPE_INTEGER,
  610. .def = 0,
  611. .min = 0,
  612. .max = 1,
  613. .step = 1,
  614. };
  615. /*
  616. * sde_rotator_ctx_show - show context state.
  617. */
  618. static ssize_t sde_rotator_ctx_show(struct kobject *kobj,
  619. struct kobj_attribute *attr, char *buf)
  620. {
  621. size_t len = PAGE_SIZE;
  622. int cnt = 0;
  623. struct sde_rotator_ctx *ctx =
  624. container_of(kobj, struct sde_rotator_ctx, kobj);
  625. if (!ctx)
  626. return cnt;
  627. #define SPRINT(fmt, ...) \
  628. (cnt += scnprintf(buf + cnt, len - cnt, fmt, ##__VA_ARGS__))
  629. SPRINT("rotate=%d\n", ctx->rotate);
  630. SPRINT("hflip=%d\n", ctx->hflip);
  631. SPRINT("vflip=%d\n", ctx->vflip);
  632. SPRINT("priority=%d\n", ctx->fh.prio);
  633. SPRINT("secure=%d\n", ctx->secure);
  634. SPRINT("timeperframe=%u %u\n", ctx->timeperframe.numerator,
  635. ctx->timeperframe.denominator);
  636. SPRINT("nbuf_out=%d\n", ctx->nbuf_out);
  637. SPRINT("nbuf_cap=%d\n", ctx->nbuf_cap);
  638. SPRINT("crop_out=%u %u %u %u\n",
  639. ctx->crop_out.left, ctx->crop_out.top,
  640. ctx->crop_out.width, ctx->crop_out.height);
  641. SPRINT("crop_cap=%u %u %u %u\n",
  642. ctx->crop_cap.left, ctx->crop_cap.top,
  643. ctx->crop_cap.width, ctx->crop_cap.height);
  644. SPRINT("fmt_out=%c%c%c%c %u %u %u %u\n",
  645. (ctx->format_out.fmt.pix.pixelformat>>0)&0xff,
  646. (ctx->format_out.fmt.pix.pixelformat>>8)&0xff,
  647. (ctx->format_out.fmt.pix.pixelformat>>16)&0xff,
  648. (ctx->format_out.fmt.pix.pixelformat>>24)&0xff,
  649. ctx->format_out.fmt.pix.width,
  650. ctx->format_out.fmt.pix.height,
  651. ctx->format_out.fmt.pix.bytesperline,
  652. ctx->format_out.fmt.pix.sizeimage);
  653. SPRINT("fmt_cap=%c%c%c%c %u %u %u %u\n",
  654. (ctx->format_cap.fmt.pix.pixelformat>>0)&0xff,
  655. (ctx->format_cap.fmt.pix.pixelformat>>8)&0xff,
  656. (ctx->format_cap.fmt.pix.pixelformat>>16)&0xff,
  657. (ctx->format_cap.fmt.pix.pixelformat>>24)&0xff,
  658. ctx->format_cap.fmt.pix.width,
  659. ctx->format_cap.fmt.pix.height,
  660. ctx->format_cap.fmt.pix.bytesperline,
  661. ctx->format_cap.fmt.pix.sizeimage);
  662. SPRINT("abort_pending=%d\n", ctx->abort_pending);
  663. SPRINT("command_pending=%d\n", !list_empty(&ctx->pending_list));
  664. SPRINT("sequence=%u\n",
  665. sde_rotator_get_timeline_commit_ts(ctx->work_queue.timeline));
  666. SPRINT("timestamp=%u\n",
  667. sde_rotator_get_timeline_retire_ts(ctx->work_queue.timeline));
  668. return cnt;
  669. }
  670. static struct kobj_attribute sde_rotator_ctx_attr =
  671. __ATTR(state, 0664, sde_rotator_ctx_show, NULL);
  672. static struct attribute *sde_rotator_fs_attrs[] = {
  673. &sde_rotator_ctx_attr.attr,
  674. NULL
  675. };
  676. static struct attribute_group sde_rotator_fs_attr_group = {
  677. .attrs = sde_rotator_fs_attrs
  678. };
  679. /*
  680. * sde_rotator_ctx_show - sysfs show callback.
  681. */
  682. static ssize_t sde_rotator_fs_show(struct kobject *kobj,
  683. struct attribute *attr, char *buf)
  684. {
  685. ssize_t ret = -EIO;
  686. struct kobj_attribute *kattr =
  687. container_of(attr, struct kobj_attribute, attr);
  688. if (kattr->show)
  689. ret = kattr->show(kobj, kattr, buf);
  690. return ret;
  691. }
  692. /*
  693. * sde_rotator_fs_store - sysfs store callback.
  694. */
  695. static ssize_t sde_rotator_fs_store(struct kobject *kobj,
  696. struct attribute *attr, const char *buf, size_t count)
  697. {
  698. ssize_t ret = -EIO;
  699. struct kobj_attribute *kattr =
  700. container_of(attr, struct kobj_attribute, attr);
  701. if (kattr->store)
  702. ret = kattr->store(kobj, kattr, buf, count);
  703. return ret;
  704. }
  705. static const struct sysfs_ops sde_rotator_fs_ops = {
  706. .show = sde_rotator_fs_show,
  707. .store = sde_rotator_fs_store,
  708. };
  709. static struct kobj_type sde_rotator_fs_ktype = {
  710. .sysfs_ops = &sde_rotator_fs_ops,
  711. };
  712. /*
  713. * sde_rotator_queue_init - m2m_ops queue_setup callback.
  714. * @priv: Pointer to rotator ctx.
  715. * @src_vq: vb2 source queue.
  716. * @dst_vq: vb2 destination queue.
  717. */
  718. static int sde_rotator_queue_init(void *priv, struct vb2_queue *src_vq,
  719. struct vb2_queue *dst_vq)
  720. {
  721. struct sde_rotator_ctx *ctx = priv;
  722. int ret;
  723. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  724. src_vq->io_modes = VB2_USERPTR;
  725. src_vq->drv_priv = ctx;
  726. src_vq->mem_ops = &sde_rotator_vb2_mem_ops;
  727. src_vq->ops = &sde_rotator_vb2_q_ops;
  728. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  729. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  730. src_vq->lock = &ctx->rot_dev->lock;
  731. src_vq->min_buffers_needed = 1;
  732. src_vq->dev = ctx->rot_dev->dev;
  733. ret = vb2_queue_init(src_vq);
  734. if (ret) {
  735. SDEDEV_ERR(ctx->rot_dev->dev,
  736. "fail init src queue r:%d\n", ret);
  737. return ret;
  738. }
  739. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  740. dst_vq->io_modes = VB2_USERPTR;
  741. dst_vq->drv_priv = ctx;
  742. dst_vq->mem_ops = &sde_rotator_vb2_mem_ops;
  743. dst_vq->ops = &sde_rotator_vb2_q_ops;
  744. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  745. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  746. dst_vq->lock = &ctx->rot_dev->lock;
  747. dst_vq->min_buffers_needed = 1;
  748. src_vq->dev = ctx->rot_dev->dev;
  749. ret = vb2_queue_init(dst_vq);
  750. if (ret) {
  751. SDEDEV_ERR(ctx->rot_dev->dev,
  752. "fail init dst queue r:%d\n", ret);
  753. return ret;
  754. }
  755. return 0;
  756. }
  757. /*
  758. * sde_rotator_ctx_open - Rotator device open method.
  759. * @rot_dev: Pointer to rotator device structure
  760. * @file: Pointer to file struct (optional)
  761. * return: Pointer rotator context if success; ptr error code, otherwise.
  762. */
  763. struct sde_rotator_ctx *sde_rotator_ctx_open(
  764. struct sde_rotator_device *rot_dev, struct file *file)
  765. {
  766. struct video_device *video = file ? video_devdata(file) : NULL;
  767. struct sde_rotator_ctx *ctx;
  768. struct v4l2_ctrl_handler *ctrl_handler;
  769. char name[32];
  770. int i, ret;
  771. if (atomic_read(&rot_dev->mgr->device_suspended))
  772. return ERR_PTR(-EPERM);
  773. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  774. if (!ctx)
  775. return ERR_PTR(-ENOMEM);
  776. if (mutex_lock_interruptible(&rot_dev->lock)) {
  777. ret = -ERESTARTSYS;
  778. goto error_lock;
  779. }
  780. /* wait until exclusive ctx, if exists, finishes or timeout */
  781. while (rot_dev->excl_ctx) {
  782. SDEROT_DBG("waiting to open %s session %d ...\n",
  783. file ? "v4l2" : "excl", rot_dev->session_id);
  784. mutex_unlock(&rot_dev->lock);
  785. ret = wait_event_interruptible_timeout(rot_dev->open_wq,
  786. !rot_dev->excl_ctx,
  787. msecs_to_jiffies(rot_dev->open_timeout));
  788. if (ret < 0) {
  789. goto error_lock;
  790. } else if (!ret) {
  791. SDEROT_WARN("timeout to open session %d\n",
  792. rot_dev->session_id);
  793. SDEROT_EVTLOG(rot_dev->session_id,
  794. SDE_ROT_EVTLOG_ERROR);
  795. ret = -EBUSY;
  796. goto error_lock;
  797. } else if (mutex_lock_interruptible(&rot_dev->lock)) {
  798. ret = -ERESTARTSYS;
  799. goto error_lock;
  800. }
  801. }
  802. ctx->rot_dev = rot_dev;
  803. ctx->file = file;
  804. /* Set context defaults */
  805. ctx->session_id = rot_dev->session_id++;
  806. SDEDEV_DBG(ctx->rot_dev->dev, "open %d\n", ctx->session_id);
  807. ctx->timeperframe.numerator = 1;
  808. ctx->timeperframe.denominator = SDE_ROTATOR_DEFAULT_FPS;
  809. ctx->hflip = 0;
  810. ctx->vflip = 0;
  811. ctx->rotate = 0;
  812. ctx->secure = 0;
  813. ctx->abort_pending = 0;
  814. ctx->kthread_id = -1;
  815. ctx->format_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  816. ctx->format_cap.fmt.pix.pixelformat = SDE_PIX_FMT_Y_CBCR_H2V2;
  817. ctx->format_cap.fmt.pix.width = 640;
  818. ctx->format_cap.fmt.pix.height = 480;
  819. ctx->crop_cap.width = 640;
  820. ctx->crop_cap.height = 480;
  821. ctx->format_out.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  822. ctx->format_out.fmt.pix.pixelformat = SDE_PIX_FMT_Y_CBCR_H2V2;
  823. ctx->format_out.fmt.pix.width = 640;
  824. ctx->format_out.fmt.pix.height = 480;
  825. ctx->crop_out.width = 640;
  826. ctx->crop_out.height = 480;
  827. init_waitqueue_head(&ctx->wait_queue);
  828. spin_lock_init(&ctx->list_lock);
  829. INIT_LIST_HEAD(&ctx->pending_list);
  830. INIT_LIST_HEAD(&ctx->retired_list);
  831. for (i = 0 ; i < ARRAY_SIZE(ctx->requests); i++) {
  832. struct sde_rotator_request *request = &ctx->requests[i];
  833. kthread_init_work(&request->submit_work,
  834. sde_rotator_submit_handler);
  835. kthread_init_work(&request->retire_work,
  836. sde_rotator_retire_handler);
  837. request->ctx = ctx;
  838. INIT_LIST_HEAD(&request->list);
  839. list_add_tail(&request->list, &ctx->retired_list);
  840. }
  841. if (ctx->file) {
  842. v4l2_fh_init(&ctx->fh, video);
  843. file->private_data = &ctx->fh;
  844. v4l2_fh_add(&ctx->fh);
  845. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(rot_dev->m2m_dev,
  846. ctx, sde_rotator_queue_init);
  847. if (IS_ERR_OR_NULL(ctx->fh.m2m_ctx)) {
  848. ret = PTR_ERR(ctx->fh.m2m_ctx);
  849. ctx->fh.m2m_ctx = NULL;
  850. goto error_m2m_init;
  851. }
  852. }
  853. ret = kobject_init_and_add(&ctx->kobj, &sde_rotator_fs_ktype,
  854. &rot_dev->dev->kobj, "session_%d", ctx->session_id);
  855. if (ret) {
  856. SDEDEV_ERR(ctx->rot_dev->dev,
  857. "fail initialize context kobject\n");
  858. goto error_kobj_init;
  859. }
  860. ret = sysfs_create_group(&ctx->kobj, &sde_rotator_fs_attr_group);
  861. if (ret) {
  862. SDEDEV_ERR(ctx->rot_dev->dev,
  863. "fail register rotator sysfs nodes\n");
  864. goto error_create_sysfs;
  865. }
  866. for (i = 0; i < MAX_ROT_OPEN_SESSION; i++) {
  867. if (rot_dev->kthread_free[i]) {
  868. rot_dev->kthread_free[i] = false;
  869. ctx->kthread_id = i;
  870. ctx->work_queue.rot_kw = &rot_dev->rot_kw[i];
  871. ctx->work_queue.rot_thread = rot_dev->rot_thread[i];
  872. break;
  873. }
  874. }
  875. if (ctx->kthread_id < 0) {
  876. SDEDEV_ERR(ctx->rot_dev->dev,
  877. "fail to acquire the kthread\n");
  878. ret = -EINVAL;
  879. goto error_alloc_kthread;
  880. }
  881. snprintf(name, sizeof(name), "%d_%d", rot_dev->dev->id,
  882. ctx->session_id);
  883. ctx->work_queue.timeline = sde_rotator_create_timeline(name);
  884. if (!ctx->work_queue.timeline)
  885. SDEDEV_DBG(ctx->rot_dev->dev, "timeline is not available\n");
  886. sde_rot_mgr_lock(rot_dev->mgr);
  887. sde_rotator_pm_qos_request(rot_dev,
  888. SDE_ROTATOR_ADD_REQUEST);
  889. ret = sde_rotator_session_open(rot_dev->mgr, &ctx->private,
  890. ctx->session_id, &ctx->work_queue);
  891. if (ret < 0) {
  892. SDEDEV_ERR(ctx->rot_dev->dev, "fail open session\n");
  893. goto error_open_session;
  894. }
  895. sde_rot_mgr_unlock(rot_dev->mgr);
  896. if (ctx->file) {
  897. /* Create control */
  898. ctrl_handler = &ctx->ctrl_handler;
  899. v4l2_ctrl_handler_init(ctrl_handler, 4);
  900. v4l2_ctrl_new_std(ctrl_handler,
  901. &sde_rotator_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  902. v4l2_ctrl_new_std(ctrl_handler,
  903. &sde_rotator_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  904. v4l2_ctrl_new_std(ctrl_handler,
  905. &sde_rotator_ctrl_ops, V4L2_CID_ROTATE, 0, 270, 90, 0);
  906. v4l2_ctrl_new_custom(ctrl_handler,
  907. &sde_rotator_ctrl_secure, NULL);
  908. v4l2_ctrl_new_custom(ctrl_handler,
  909. &sde_rotator_ctrl_secure_camera, NULL);
  910. if (ctrl_handler->error) {
  911. ret = ctrl_handler->error;
  912. v4l2_ctrl_handler_free(ctrl_handler);
  913. goto error_ctrl_handler;
  914. }
  915. ctx->fh.ctrl_handler = ctrl_handler;
  916. v4l2_ctrl_handler_setup(ctrl_handler);
  917. } else {
  918. /* acquire exclusive context */
  919. SDEDEV_DBG(rot_dev->dev, "acquire exclusive session id:%u\n",
  920. ctx->session_id);
  921. SDEROT_EVTLOG(ctx->session_id);
  922. rot_dev->excl_ctx = ctx;
  923. }
  924. mutex_unlock(&rot_dev->lock);
  925. SDEDEV_DBG(ctx->rot_dev->dev, "SDE v4l2 rotator open success\n");
  926. ATRACE_BEGIN(ctx->kobj.name);
  927. return ctx;
  928. error_ctrl_handler:
  929. error_open_session:
  930. sde_rot_mgr_unlock(rot_dev->mgr);
  931. sde_rotator_destroy_timeline(ctx->work_queue.timeline);
  932. rot_dev->kthread_free[ctx->kthread_id] = true;
  933. ctx->kthread_id = -1;
  934. error_alloc_kthread:
  935. sysfs_remove_group(&ctx->kobj, &sde_rotator_fs_attr_group);
  936. error_create_sysfs:
  937. kobject_put(&ctx->kobj);
  938. error_kobj_init:
  939. if (ctx->file) {
  940. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  941. ctx->fh.m2m_ctx = NULL;
  942. }
  943. error_m2m_init:
  944. if (ctx->file) {
  945. v4l2_fh_del(&ctx->fh);
  946. v4l2_fh_exit(&ctx->fh);
  947. }
  948. mutex_unlock(&rot_dev->lock);
  949. error_lock:
  950. kfree(ctx);
  951. ctx = NULL;
  952. return ERR_PTR(ret);
  953. }
  954. /*
  955. * sde_rotator_ctx_release - Rotator device release method.
  956. * @ctx: Pointer rotator context.
  957. * @file: Pointer to file struct (optional)
  958. * return: 0 if success; error code, otherwise
  959. */
  960. static int sde_rotator_ctx_release(struct sde_rotator_ctx *ctx,
  961. struct file *file)
  962. {
  963. struct sde_rotator_device *rot_dev;
  964. u32 session_id;
  965. struct list_head *curr, *next;
  966. if (!ctx) {
  967. SDEROT_DBG("ctx is NULL\n");
  968. return -EINVAL;
  969. }
  970. rot_dev = ctx->rot_dev;
  971. session_id = ctx->session_id;
  972. ATRACE_END(ctx->kobj.name);
  973. SDEDEV_DBG(rot_dev->dev, "release s:%d\n", session_id);
  974. mutex_lock(&rot_dev->lock);
  975. if (rot_dev->excl_ctx == ctx) {
  976. SDEDEV_DBG(rot_dev->dev, "release exclusive session id:%u\n",
  977. session_id);
  978. SDEROT_EVTLOG(session_id);
  979. rot_dev->excl_ctx = NULL;
  980. }
  981. if (ctx->file) {
  982. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  983. SDEDEV_DBG(rot_dev->dev, "release streams s:%d\n", session_id);
  984. if (ctx->fh.m2m_ctx) {
  985. v4l2_m2m_streamoff(file, ctx->fh.m2m_ctx,
  986. V4L2_BUF_TYPE_VIDEO_OUTPUT);
  987. v4l2_m2m_streamoff(file, ctx->fh.m2m_ctx,
  988. V4L2_BUF_TYPE_VIDEO_CAPTURE);
  989. }
  990. }
  991. mutex_unlock(&rot_dev->lock);
  992. SDEDEV_DBG(rot_dev->dev, "release submit work s:%d\n", session_id);
  993. list_for_each_safe(curr, next, &ctx->pending_list) {
  994. struct sde_rotator_request *request =
  995. container_of(curr, struct sde_rotator_request, list);
  996. SDEDEV_DBG(rot_dev->dev, "release submit work s:%d\n",
  997. session_id);
  998. kthread_cancel_work_sync(&request->submit_work);
  999. }
  1000. SDEDEV_DBG(rot_dev->dev, "release session s:%d\n", session_id);
  1001. sde_rot_mgr_lock(rot_dev->mgr);
  1002. sde_rotator_pm_qos_request(rot_dev,
  1003. SDE_ROTATOR_REMOVE_REQUEST);
  1004. sde_rotator_session_close(rot_dev->mgr, ctx->private, session_id);
  1005. sde_rot_mgr_unlock(rot_dev->mgr);
  1006. SDEDEV_DBG(rot_dev->dev, "release retire work s:%d\n", session_id);
  1007. list_for_each_safe(curr, next, &ctx->pending_list) {
  1008. struct sde_rotator_request *request =
  1009. container_of(curr, struct sde_rotator_request, list);
  1010. SDEDEV_DBG(rot_dev->dev, "release retire work s:%d\n",
  1011. session_id);
  1012. kthread_cancel_work_sync(&request->retire_work);
  1013. }
  1014. mutex_lock(&rot_dev->lock);
  1015. SDEDEV_DBG(rot_dev->dev, "release context s:%d\n", session_id);
  1016. sde_rotator_destroy_timeline(ctx->work_queue.timeline);
  1017. if (ctx->kthread_id >= 0 && ctx->work_queue.rot_kw) {
  1018. kthread_flush_worker(ctx->work_queue.rot_kw);
  1019. rot_dev->kthread_free[ctx->kthread_id] = true;
  1020. }
  1021. sysfs_remove_group(&ctx->kobj, &sde_rotator_fs_attr_group);
  1022. kobject_put(&ctx->kobj);
  1023. if (ctx->file) {
  1024. if (ctx->fh.m2m_ctx)
  1025. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1026. if (ctx->fh.vdev) {
  1027. v4l2_fh_del(&ctx->fh);
  1028. v4l2_fh_exit(&ctx->fh);
  1029. }
  1030. }
  1031. kfree(ctx->vbinfo_out);
  1032. kfree(ctx->vbinfo_cap);
  1033. kfree(ctx);
  1034. wake_up_interruptible(&rot_dev->open_wq);
  1035. mutex_unlock(&rot_dev->lock);
  1036. SDEDEV_DBG(rot_dev->dev, "release complete s:%d\n", session_id);
  1037. return 0;
  1038. }
  1039. /*
  1040. * sde_rotator_update_retire_sequence - update retired sequence of the context
  1041. * referenced in the request, and wake up any waiting for update event
  1042. * @request: Pointer to rotator request
  1043. */
  1044. static void sde_rotator_update_retire_sequence(
  1045. struct sde_rotator_request *request)
  1046. {
  1047. struct sde_rotator_ctx *ctx;
  1048. if (!request || !request->ctx) {
  1049. SDEROT_ERR("invalid parameters\n");
  1050. return;
  1051. }
  1052. ctx = request->ctx;
  1053. ctx->retired_sequence_id = request->sequence_id;
  1054. wake_up(&ctx->wait_queue);
  1055. SDEROT_DBG("update sequence s:%d.%d\n",
  1056. ctx->session_id, ctx->retired_sequence_id);
  1057. }
  1058. /*
  1059. * sde_rotator_retire_request - retire the given rotator request with
  1060. * device mutex locked
  1061. * @request: Pointer to rotator request
  1062. */
  1063. static void sde_rotator_retire_request(struct sde_rotator_request *request)
  1064. {
  1065. struct sde_rotator_ctx *ctx;
  1066. if (!request || !request->ctx) {
  1067. SDEROT_ERR("invalid parameters\n");
  1068. return;
  1069. }
  1070. ctx = request->ctx;
  1071. request->req = NULL;
  1072. request->sequence_id = 0;
  1073. request->committed = false;
  1074. spin_lock(&ctx->list_lock);
  1075. list_del_init(&request->list);
  1076. list_add_tail(&request->list, &ctx->retired_list);
  1077. spin_unlock(&ctx->list_lock);
  1078. wake_up(&ctx->wait_queue);
  1079. SDEROT_DBG("retire request s:%d.%d\n",
  1080. ctx->session_id, ctx->retired_sequence_id);
  1081. }
  1082. /*
  1083. * sde_rotator_is_request_retired - Return true if given request already expired
  1084. * @request: Pointer to rotator request
  1085. */
  1086. static bool sde_rotator_is_request_retired(struct sde_rotator_request *request)
  1087. {
  1088. struct sde_rotator_ctx *ctx;
  1089. u32 sequence_id;
  1090. s32 retire_delta;
  1091. if (!request || !request->ctx)
  1092. return true;
  1093. ctx = request->ctx;
  1094. sequence_id = request->sequence_id;
  1095. retire_delta = (s32) (ctx->retired_sequence_id - sequence_id);
  1096. SDEROT_DBG("sequence:%u/%u\n", sequence_id, ctx->retired_sequence_id);
  1097. return retire_delta >= 0;
  1098. }
  1099. static void sde_rotator_pm_qos_remove(struct sde_rot_data_type *rot_mdata)
  1100. {
  1101. struct pm_qos_request *req;
  1102. u32 cpu_mask;
  1103. if (!rot_mdata) {
  1104. SDEROT_DBG("invalid rot device or context\n");
  1105. return;
  1106. }
  1107. cpu_mask = rot_mdata->rot_pm_qos_cpu_mask;
  1108. if (!cpu_mask)
  1109. return;
  1110. req = &rot_mdata->pm_qos_rot_cpu_req;
  1111. pm_qos_remove_request(req);
  1112. }
  1113. void sde_rotator_pm_qos_add(struct sde_rot_data_type *rot_mdata)
  1114. {
  1115. struct pm_qos_request *req;
  1116. u32 cpu_mask;
  1117. int cpu;
  1118. if (!rot_mdata) {
  1119. SDEROT_DBG("invalid rot device or context\n");
  1120. return;
  1121. }
  1122. cpu_mask = rot_mdata->rot_pm_qos_cpu_mask;
  1123. if (!cpu_mask)
  1124. return;
  1125. req = &rot_mdata->pm_qos_rot_cpu_req;
  1126. req->type = PM_QOS_REQ_AFFINE_CORES;
  1127. cpumask_empty(&req->cpus_affine);
  1128. for_each_possible_cpu(cpu) {
  1129. if ((1 << cpu) & cpu_mask)
  1130. cpumask_set_cpu(cpu, &req->cpus_affine);
  1131. }
  1132. pm_qos_add_request(req, PM_QOS_CPU_DMA_LATENCY,
  1133. PM_QOS_DEFAULT_VALUE);
  1134. SDEROT_DBG("rotator pmqos add mask %x latency %x\n",
  1135. rot_mdata->rot_pm_qos_cpu_mask,
  1136. rot_mdata->rot_pm_qos_cpu_dma_latency);
  1137. }
  1138. static void sde_rotator_pm_qos_request(struct sde_rotator_device *rot_dev,
  1139. bool add_request)
  1140. {
  1141. u32 cpu_mask;
  1142. u32 cpu_dma_latency;
  1143. bool changed = false;
  1144. if (!rot_dev) {
  1145. SDEROT_DBG("invalid rot device or context\n");
  1146. return;
  1147. }
  1148. cpu_mask = rot_dev->mdata->rot_pm_qos_cpu_mask;
  1149. cpu_dma_latency = rot_dev->mdata->rot_pm_qos_cpu_dma_latency;
  1150. if (!cpu_mask)
  1151. return;
  1152. if (add_request) {
  1153. if (rot_dev->mdata->rot_pm_qos_cpu_count == 0)
  1154. changed = true;
  1155. rot_dev->mdata->rot_pm_qos_cpu_count++;
  1156. } else {
  1157. if (rot_dev->mdata->rot_pm_qos_cpu_count != 0) {
  1158. rot_dev->mdata->rot_pm_qos_cpu_count--;
  1159. if (rot_dev->mdata->rot_pm_qos_cpu_count == 0)
  1160. changed = true;
  1161. } else {
  1162. SDEROT_DBG("%s: ref_count is not balanced\n",
  1163. __func__);
  1164. }
  1165. }
  1166. if (!changed)
  1167. return;
  1168. SDEROT_EVTLOG(add_request, cpu_mask, cpu_dma_latency);
  1169. if (!add_request) {
  1170. pm_qos_update_request(&rot_dev->mdata->pm_qos_rot_cpu_req,
  1171. PM_QOS_DEFAULT_VALUE);
  1172. return;
  1173. }
  1174. pm_qos_update_request(&rot_dev->mdata->pm_qos_rot_cpu_req,
  1175. cpu_dma_latency);
  1176. }
  1177. /*
  1178. * sde_rotator_inline_open - open inline rotator session
  1179. * @pdev: Pointer to rotator platform device
  1180. * @video_mode: true if video mode is requested
  1181. * return: Pointer to new rotator session context
  1182. */
  1183. void *sde_rotator_inline_open(struct platform_device *pdev)
  1184. {
  1185. struct sde_rotator_device *rot_dev;
  1186. struct sde_rotator_ctx *ctx;
  1187. int rc;
  1188. if (!pdev) {
  1189. SDEROT_ERR("invalid platform device\n");
  1190. return ERR_PTR(-EINVAL);
  1191. }
  1192. rot_dev = (struct sde_rotator_device *) platform_get_drvdata(pdev);
  1193. if (!rot_dev) {
  1194. SDEROT_ERR("invalid rotator device\n");
  1195. return ERR_PTR(-EINVAL);
  1196. }
  1197. ctx = sde_rotator_ctx_open(rot_dev, NULL);
  1198. if (IS_ERR_OR_NULL(ctx)) {
  1199. rc = PTR_ERR(ctx);
  1200. SDEROT_ERR("failed to open rotator context %d\n", rc);
  1201. goto rotator_open_error;
  1202. }
  1203. ctx->slice = llcc_slice_getd(LLCC_ROTATOR);
  1204. if (IS_ERR(ctx->slice)) {
  1205. rc = PTR_ERR(ctx->slice);
  1206. SDEROT_ERR("failed to get system cache %d\n", rc);
  1207. goto slice_getd_error;
  1208. }
  1209. if (!rot_dev->disable_syscache) {
  1210. rc = llcc_slice_activate(ctx->slice);
  1211. if (rc) {
  1212. SDEROT_ERR("failed to activate slice %d\n", rc);
  1213. goto activate_error;
  1214. }
  1215. SDEROT_DBG("scid %d size %zukb\n",
  1216. llcc_get_slice_id(ctx->slice),
  1217. llcc_get_slice_size(ctx->slice));
  1218. } else {
  1219. SDEROT_DBG("syscache bypassed\n");
  1220. }
  1221. SDEROT_EVTLOG(ctx->session_id, llcc_get_slice_id(ctx->slice),
  1222. llcc_get_slice_size(ctx->slice),
  1223. rot_dev->disable_syscache);
  1224. return ctx;
  1225. activate_error:
  1226. llcc_slice_putd(ctx->slice);
  1227. ctx->slice = NULL;
  1228. slice_getd_error:
  1229. sde_rotator_ctx_release(ctx, NULL);
  1230. rotator_open_error:
  1231. return ERR_PTR(rc);
  1232. }
  1233. EXPORT_SYMBOL(sde_rotator_inline_open);
  1234. int sde_rotator_inline_release(void *handle)
  1235. {
  1236. struct sde_rotator_device *rot_dev;
  1237. struct sde_rotator_ctx *ctx;
  1238. if (!handle) {
  1239. SDEROT_ERR("invalid rotator ctx\n");
  1240. return -EINVAL;
  1241. }
  1242. ctx = handle;
  1243. rot_dev = ctx->rot_dev;
  1244. if (!rot_dev) {
  1245. SDEROT_ERR("invalid rotator device\n");
  1246. return -EINVAL;
  1247. }
  1248. if (ctx->slice) {
  1249. if (!rot_dev->disable_syscache)
  1250. llcc_slice_deactivate(ctx->slice);
  1251. llcc_slice_putd(ctx->slice);
  1252. ctx->slice = NULL;
  1253. }
  1254. SDEROT_EVTLOG(ctx->session_id);
  1255. return sde_rotator_ctx_release(ctx, NULL);
  1256. }
  1257. EXPORT_SYMBOL(sde_rotator_inline_release);
  1258. /*
  1259. * sde_rotator_inline_get_dst_pixfmt - determine output pixel format
  1260. * @pdev: Pointer to platform device
  1261. * @src_pixfmt: input pixel format
  1262. * @dst_pixfmt: Pointer to output pixel format (output)
  1263. * return: 0 if success; error code otherwise
  1264. */
  1265. int sde_rotator_inline_get_dst_pixfmt(struct platform_device *pdev,
  1266. u32 src_pixfmt, u32 *dst_pixfmt)
  1267. {
  1268. int rc;
  1269. if (!src_pixfmt || !dst_pixfmt)
  1270. return -EINVAL;
  1271. rc = sde_rot_get_base_tilea5x_pixfmt(src_pixfmt, dst_pixfmt);
  1272. if (rc)
  1273. return rc;
  1274. /*
  1275. * Currently, NV21 tile is not supported as output; hence,
  1276. * override with NV12 tile.
  1277. */
  1278. if (*dst_pixfmt == SDE_PIX_FMT_Y_CRCB_H2V2_TILE)
  1279. *dst_pixfmt = SDE_PIX_FMT_Y_CBCR_H2V2_TILE;
  1280. return 0;
  1281. }
  1282. EXPORT_SYMBOL(sde_rotator_inline_get_dst_pixfmt);
  1283. /*
  1284. * sde_rotator_inline_get_downscale_caps - get scaling capability
  1285. * @pdev: Pointer to platform device
  1286. * @caps: string buffer for capability
  1287. * @len: length of string buffer
  1288. * return: length of capability string
  1289. */
  1290. int sde_rotator_inline_get_downscale_caps(struct platform_device *pdev,
  1291. char *caps, int len)
  1292. {
  1293. struct sde_rotator_device *rot_dev;
  1294. int rc;
  1295. if (!pdev) {
  1296. SDEROT_ERR("invalid platform device\n");
  1297. return -EINVAL;
  1298. }
  1299. rot_dev = (struct sde_rotator_device *) platform_get_drvdata(pdev);
  1300. if (!rot_dev || !rot_dev->mgr) {
  1301. SDEROT_ERR("invalid rotator device\n");
  1302. return -EINVAL;
  1303. }
  1304. sde_rot_mgr_lock(rot_dev->mgr);
  1305. rc = sde_rotator_get_downscale_caps(rot_dev->mgr, caps, len);
  1306. sde_rot_mgr_unlock(rot_dev->mgr);
  1307. return rc;
  1308. }
  1309. EXPORT_SYMBOL(sde_rotator_inline_get_downscale_caps);
  1310. /*
  1311. * sde_rotator_inline_get_maxlinewidth - get maximum line width of rotator
  1312. * @pdev: Pointer to platform device
  1313. * return: maximum line width
  1314. */
  1315. int sde_rotator_inline_get_maxlinewidth(struct platform_device *pdev)
  1316. {
  1317. struct sde_rotator_device *rot_dev;
  1318. int maxlinewidth;
  1319. if (!pdev) {
  1320. SDEROT_ERR("invalid platform device\n");
  1321. return -EINVAL;
  1322. }
  1323. rot_dev = (struct sde_rotator_device *)platform_get_drvdata(pdev);
  1324. if (!rot_dev || !rot_dev->mgr) {
  1325. SDEROT_ERR("invalid rotator device\n");
  1326. return -EINVAL;
  1327. }
  1328. sde_rot_mgr_lock(rot_dev->mgr);
  1329. maxlinewidth = sde_rotator_get_maxlinewidth(rot_dev->mgr);
  1330. sde_rot_mgr_unlock(rot_dev->mgr);
  1331. return maxlinewidth;
  1332. }
  1333. EXPORT_SYMBOL(sde_rotator_inline_get_maxlinewidth);
  1334. /*
  1335. * sde_rotator_inline_get_pixfmt_caps - get pixel format capability
  1336. * @pdev: Pointer to platform device
  1337. * @pixfmt: array of pixel format buffer
  1338. * @len: length of pixel format buffer
  1339. * return: length of pixel format capability if success; error code otherwise
  1340. */
  1341. int sde_rotator_inline_get_pixfmt_caps(struct platform_device *pdev,
  1342. bool input, u32 *pixfmts, int len)
  1343. {
  1344. struct sde_rotator_device *rot_dev;
  1345. u32 i, pixfmt;
  1346. if (!pdev) {
  1347. SDEROT_ERR("invalid platform device\n");
  1348. return -EINVAL;
  1349. }
  1350. rot_dev = (struct sde_rotator_device *) platform_get_drvdata(pdev);
  1351. if (!rot_dev || !rot_dev->mgr) {
  1352. SDEROT_ERR("invalid rotator device\n");
  1353. return -EINVAL;
  1354. }
  1355. sde_rot_mgr_lock(rot_dev->mgr);
  1356. for (i = 0;; i++) {
  1357. pixfmt = sde_rotator_get_pixfmt(rot_dev->mgr, i, input,
  1358. SDE_ROTATOR_MODE_SBUF);
  1359. if (!pixfmt)
  1360. break;
  1361. if (pixfmts && i < len)
  1362. pixfmts[i] = pixfmt;
  1363. }
  1364. sde_rot_mgr_unlock(rot_dev->mgr);
  1365. return i;
  1366. }
  1367. EXPORT_SYMBOL(sde_rotator_inline_get_pixfmt_caps);
  1368. /*
  1369. * _sde_rotator_inline_cleanup - perform inline related request cleanup
  1370. * This function assumes rot_dev->mgr lock has been taken when called.
  1371. * @handle: Pointer to rotator context
  1372. * @request: Pointer to rotation request
  1373. * return: 0 if success; -EAGAIN if cleanup should be retried
  1374. */
  1375. static int _sde_rotator_inline_cleanup(void *handle,
  1376. struct sde_rotator_request *request)
  1377. {
  1378. struct sde_rotator_ctx *ctx;
  1379. struct sde_rotator_device *rot_dev;
  1380. int ret;
  1381. if (!handle || !request) {
  1382. SDEROT_ERR("invalid rotator handle/request\n");
  1383. return -EINVAL;
  1384. }
  1385. ctx = handle;
  1386. rot_dev = ctx->rot_dev;
  1387. if (!rot_dev || !rot_dev->mgr) {
  1388. SDEROT_ERR("invalid rotator device\n");
  1389. return -EINVAL;
  1390. }
  1391. if (request->committed) {
  1392. /* wait until request is finished */
  1393. sde_rot_mgr_unlock(rot_dev->mgr);
  1394. mutex_unlock(&rot_dev->lock);
  1395. ret = wait_event_timeout(ctx->wait_queue,
  1396. sde_rotator_is_request_retired(request),
  1397. msecs_to_jiffies(rot_dev->streamoff_timeout));
  1398. mutex_lock(&rot_dev->lock);
  1399. sde_rot_mgr_lock(rot_dev->mgr);
  1400. if (!ret) {
  1401. SDEROT_ERR("timeout w/o retire s:%d\n",
  1402. ctx->session_id);
  1403. SDEROT_EVTLOG(ctx->session_id, SDE_ROT_EVTLOG_ERROR);
  1404. sde_rotator_abort_inline_request(rot_dev->mgr,
  1405. ctx->private, request->req);
  1406. return -EAGAIN;
  1407. } else if (ret == 1) {
  1408. SDEROT_ERR("timeout w/ retire s:%d\n", ctx->session_id);
  1409. SDEROT_EVTLOG(ctx->session_id, SDE_ROT_EVTLOG_ERROR);
  1410. }
  1411. }
  1412. sde_rotator_req_finish(rot_dev->mgr, ctx->private, request->req);
  1413. sde_rotator_retire_request(request);
  1414. return 0;
  1415. }
  1416. /*
  1417. * sde_rotator_inline_commit - commit given rotator command
  1418. * @handle: Pointer to rotator context
  1419. * @cmd: Pointer to rotator command
  1420. * @cmd_type: command type - validate/prepare/commit/cleanup
  1421. * return: 0 if success; error code otherwise
  1422. */
  1423. int sde_rotator_inline_commit(void *handle, struct sde_rotator_inline_cmd *cmd,
  1424. enum sde_rotator_inline_cmd_type cmd_type)
  1425. {
  1426. struct sde_rotator_ctx *ctx;
  1427. struct sde_rotator_device *rot_dev;
  1428. struct sde_rotator_request *request = NULL;
  1429. struct sde_rot_entry_container *req = NULL;
  1430. struct sde_rotation_config rotcfg;
  1431. struct sde_rot_trace_entry rot_trace;
  1432. ktime_t *ts;
  1433. u32 flags = 0;
  1434. int i, ret = 0;
  1435. if (!handle || !cmd) {
  1436. SDEROT_ERR("invalid rotator handle/cmd\n");
  1437. return -EINVAL;
  1438. }
  1439. ctx = handle;
  1440. rot_dev = ctx->rot_dev;
  1441. if (!rot_dev || !rot_dev->mgr) {
  1442. SDEROT_ERR("invalid rotator device\n");
  1443. return -EINVAL;
  1444. }
  1445. SDEROT_DBG(
  1446. "s:%d.%u src:(%u,%u,%u,%u)/%ux%u/%c%c%c%c dst:(%u,%u,%u,%u)/%c%c%c%c r:%d f:%d/%d s:%d fps:%u clk:%llu bw:%llu prefill:%llu wb:%d vid:%d cmd:%d\n",
  1447. ctx->session_id, cmd->sequence_id,
  1448. cmd->src_rect_x, cmd->src_rect_y,
  1449. cmd->src_rect_w, cmd->src_rect_h,
  1450. cmd->src_width, cmd->src_height,
  1451. cmd->src_pixfmt >> 0, cmd->src_pixfmt >> 8,
  1452. cmd->src_pixfmt >> 16, cmd->src_pixfmt >> 24,
  1453. cmd->dst_rect_x, cmd->dst_rect_y,
  1454. cmd->dst_rect_w, cmd->dst_rect_h,
  1455. cmd->dst_pixfmt >> 0, cmd->dst_pixfmt >> 8,
  1456. cmd->dst_pixfmt >> 16, cmd->dst_pixfmt >> 24,
  1457. cmd->rot90, cmd->hflip, cmd->vflip, cmd->secure, cmd->fps,
  1458. cmd->clkrate, cmd->data_bw, cmd->prefill_bw,
  1459. cmd->dst_writeback, cmd->video_mode, cmd_type);
  1460. SDEROT_EVTLOG(ctx->session_id, cmd->sequence_id,
  1461. cmd->src_rect_x, cmd->src_rect_y,
  1462. cmd->src_rect_w, cmd->src_rect_h,
  1463. cmd->src_pixfmt,
  1464. cmd->dst_rect_w, cmd->dst_rect_h,
  1465. cmd->dst_pixfmt,
  1466. cmd->fps, cmd->clkrate, cmd->data_bw, cmd->prefill_bw,
  1467. (cmd->rot90 << 0) | (cmd->hflip << 1) | (cmd->vflip << 2) |
  1468. (cmd->secure << 3) | (cmd->dst_writeback << 4) |
  1469. (cmd->video_mode << 5) |
  1470. (cmd_type << 24));
  1471. mutex_lock(&rot_dev->lock);
  1472. sde_rot_mgr_lock(rot_dev->mgr);
  1473. if (cmd_type == SDE_ROTATOR_INLINE_CMD_VALIDATE ||
  1474. cmd_type == SDE_ROTATOR_INLINE_CMD_COMMIT) {
  1475. struct sde_rotation_item item;
  1476. struct sde_rotator_statistics *stats = &rot_dev->stats;
  1477. int scid = llcc_get_slice_id(ctx->slice);
  1478. /* allocate slot for timestamp */
  1479. ts = stats->ts[stats->count % SDE_ROTATOR_NUM_EVENTS];
  1480. if (cmd_type == SDE_ROTATOR_INLINE_CMD_COMMIT)
  1481. stats->count++;
  1482. if (cmd->rot90)
  1483. flags |= SDE_ROTATION_90;
  1484. if (cmd->hflip)
  1485. flags |= SDE_ROTATION_FLIP_LR;
  1486. if (cmd->vflip)
  1487. flags |= SDE_ROTATION_FLIP_UD;
  1488. if (cmd->secure)
  1489. flags |= SDE_ROTATION_SECURE;
  1490. flags |= SDE_ROTATION_EXT_PERF;
  1491. /* fill in item work structure */
  1492. memset(&item, 0, sizeof(struct sde_rotation_item));
  1493. item.flags = flags | SDE_ROTATION_EXT_IOVA;
  1494. item.trigger = cmd->video_mode ? SDE_ROTATOR_TRIGGER_VIDEO :
  1495. SDE_ROTATOR_TRIGGER_COMMAND;
  1496. item.prefill_bw = cmd->prefill_bw;
  1497. item.session_id = ctx->session_id;
  1498. item.sequence_id = cmd->sequence_id;
  1499. item.src_rect.x = cmd->src_rect_x;
  1500. item.src_rect.y = cmd->src_rect_y;
  1501. item.src_rect.w = cmd->src_rect_w;
  1502. item.src_rect.h = cmd->src_rect_h;
  1503. item.input.width = cmd->src_width;
  1504. item.input.height = cmd->src_height;
  1505. item.input.format = cmd->src_pixfmt;
  1506. for (i = 0; i < SDE_ROTATOR_INLINE_PLANE_MAX; i++) {
  1507. item.input.planes[i].addr = cmd->src_addr[i];
  1508. item.input.planes[i].len = cmd->src_len[i];
  1509. item.input.planes[i].fd = -1;
  1510. }
  1511. item.input.plane_count = cmd->src_planes;
  1512. item.input.comp_ratio.numer = 1;
  1513. item.input.comp_ratio.denom = 1;
  1514. item.output.width = cmd->dst_rect_x + cmd->dst_rect_w;
  1515. item.output.height = cmd->dst_rect_y + cmd->dst_rect_h;
  1516. item.dst_rect.x = cmd->dst_rect_x;
  1517. item.dst_rect.y = cmd->dst_rect_y;
  1518. item.dst_rect.w = cmd->dst_rect_w;
  1519. item.dst_rect.h = cmd->dst_rect_h;
  1520. item.output.sbuf = true;
  1521. item.output.scid = scid;
  1522. item.output.writeback = cmd->dst_writeback;
  1523. item.output.format = cmd->dst_pixfmt;
  1524. for (i = 0; i < SDE_ROTATOR_INLINE_PLANE_MAX; i++) {
  1525. item.output.planes[i].addr = cmd->dst_addr[i];
  1526. item.output.planes[i].len = cmd->dst_len[i];
  1527. item.output.planes[i].fd = -1;
  1528. }
  1529. item.output.plane_count = cmd->dst_planes;
  1530. item.output.comp_ratio.numer = 1;
  1531. item.output.comp_ratio.denom = 1;
  1532. item.sequence_id = ++(ctx->commit_sequence_id);
  1533. item.ts = ts;
  1534. req = sde_rotator_req_init(rot_dev->mgr, ctx->private,
  1535. &item, 1, 0);
  1536. if (IS_ERR_OR_NULL(req)) {
  1537. SDEROT_ERR("fail allocate request s:%d\n",
  1538. ctx->session_id);
  1539. ret = -ENOMEM;
  1540. goto error_init_request;
  1541. }
  1542. /* initialize session configuration */
  1543. memset(&rotcfg, 0, sizeof(struct sde_rotation_config));
  1544. rotcfg.flags = flags;
  1545. rotcfg.frame_rate = cmd->fps;
  1546. rotcfg.clk_rate = cmd->clkrate;
  1547. rotcfg.data_bw = cmd->data_bw;
  1548. rotcfg.session_id = ctx->session_id;
  1549. rotcfg.input.width = cmd->src_rect_w;
  1550. rotcfg.input.height = cmd->src_rect_h;
  1551. rotcfg.input.format = cmd->src_pixfmt;
  1552. rotcfg.input.comp_ratio.numer = 1;
  1553. rotcfg.input.comp_ratio.denom = 1;
  1554. rotcfg.output.width = cmd->dst_rect_w;
  1555. rotcfg.output.height = cmd->dst_rect_h;
  1556. rotcfg.output.format = cmd->dst_pixfmt;
  1557. rotcfg.output.comp_ratio.numer = 1;
  1558. rotcfg.output.comp_ratio.denom = 1;
  1559. rotcfg.output.sbuf = true;
  1560. }
  1561. if (cmd_type == SDE_ROTATOR_INLINE_CMD_VALIDATE) {
  1562. ret = sde_rotator_session_validate(rot_dev->mgr,
  1563. ctx->private, &rotcfg);
  1564. if (ret) {
  1565. SDEROT_WARN("fail session validation s:%d\n",
  1566. ctx->session_id);
  1567. goto error_session_validate;
  1568. }
  1569. devm_kfree(rot_dev->dev, req);
  1570. req = NULL;
  1571. } else if (cmd_type == SDE_ROTATOR_INLINE_CMD_COMMIT) {
  1572. if (memcmp(&rotcfg, &ctx->rotcfg, sizeof(rotcfg))) {
  1573. ret = sde_rotator_session_config(rot_dev->mgr,
  1574. ctx->private, &rotcfg);
  1575. if (ret) {
  1576. SDEROT_ERR("fail session config s:%d\n",
  1577. ctx->session_id);
  1578. goto error_session_config;
  1579. }
  1580. ctx->rotcfg = rotcfg;
  1581. }
  1582. request = list_first_entry_or_null(&ctx->retired_list,
  1583. struct sde_rotator_request, list);
  1584. if (!request) {
  1585. /* should not happen */
  1586. ret = -ENOMEM;
  1587. SDEROT_ERR("no free request s:%d\n", ctx->session_id);
  1588. goto error_retired_list;
  1589. }
  1590. request->req = req;
  1591. request->sequence_id = req->entries[0].item.sequence_id;
  1592. spin_lock(&ctx->list_lock);
  1593. list_del_init(&request->list);
  1594. list_add_tail(&request->list, &ctx->pending_list);
  1595. spin_unlock(&ctx->list_lock);
  1596. ts = req->entries[0].item.ts;
  1597. if (ts) {
  1598. ts[SDE_ROTATOR_TS_SRCQB] = ktime_get();
  1599. ts[SDE_ROTATOR_TS_DSTQB] = ktime_get();
  1600. ts[SDE_ROTATOR_TS_FENCE] = ktime_get();
  1601. } else {
  1602. SDEROT_ERR("invalid stats timestamp\n");
  1603. }
  1604. req->retire_kw = ctx->work_queue.rot_kw;
  1605. req->retire_work = &request->retire_work;
  1606. /* Set values to pass to trace */
  1607. rot_trace.wb_idx = req->entries[0].item.wb_idx;
  1608. rot_trace.flags = req->entries[0].item.flags;
  1609. rot_trace.input_format = req->entries[0].item.input.format;
  1610. rot_trace.input_width = req->entries[0].item.input.width;
  1611. rot_trace.input_height = req->entries[0].item.input.height;
  1612. rot_trace.src_x = req->entries[0].item.src_rect.x;
  1613. rot_trace.src_y = req->entries[0].item.src_rect.y;
  1614. rot_trace.src_w = req->entries[0].item.src_rect.w;
  1615. rot_trace.src_h = req->entries[0].item.src_rect.h;
  1616. rot_trace.output_format = req->entries[0].item.output.format;
  1617. rot_trace.output_width = req->entries[0].item.output.width;
  1618. rot_trace.output_height = req->entries[0].item.output.height;
  1619. rot_trace.dst_x = req->entries[0].item.dst_rect.x;
  1620. rot_trace.dst_y = req->entries[0].item.dst_rect.y;
  1621. rot_trace.dst_w = req->entries[0].item.dst_rect.w;
  1622. rot_trace.dst_h = req->entries[0].item.dst_rect.h;
  1623. trace_rot_entry_fence(
  1624. ctx->session_id, cmd->sequence_id, &rot_trace);
  1625. ret = sde_rotator_handle_request_common(
  1626. rot_dev->mgr, ctx->private, req);
  1627. if (ret) {
  1628. SDEROT_ERR("fail handle request s:%d\n",
  1629. ctx->session_id);
  1630. goto error_handle_request;
  1631. }
  1632. sde_rotator_req_reset_start(rot_dev->mgr, req);
  1633. sde_rotator_queue_request(rot_dev->mgr, ctx->private, req);
  1634. request->committed = true;
  1635. /* save request in private handle */
  1636. cmd->priv_handle = request;
  1637. } else if (cmd_type == SDE_ROTATOR_INLINE_CMD_START) {
  1638. if (!cmd->priv_handle) {
  1639. ret = -EINVAL;
  1640. SDEROT_ERR("invalid private handle\n");
  1641. goto error_invalid_handle;
  1642. }
  1643. request = cmd->priv_handle;
  1644. sde_rotator_req_set_start(rot_dev->mgr, request->req);
  1645. } else if (cmd_type == SDE_ROTATOR_INLINE_CMD_CLEANUP) {
  1646. if (!cmd->priv_handle) {
  1647. ret = -EINVAL;
  1648. SDEROT_ERR("invalid private handle\n");
  1649. goto error_invalid_handle;
  1650. }
  1651. request = cmd->priv_handle;
  1652. /* attempt single retry if first cleanup attempt failed */
  1653. if (_sde_rotator_inline_cleanup(handle, request) == -EAGAIN)
  1654. _sde_rotator_inline_cleanup(handle, request);
  1655. cmd->priv_handle = NULL;
  1656. } else if (cmd_type == SDE_ROTATOR_INLINE_CMD_ABORT) {
  1657. if (!cmd->priv_handle) {
  1658. ret = -EINVAL;
  1659. SDEROT_ERR("invalid private handle\n");
  1660. goto error_invalid_handle;
  1661. }
  1662. request = cmd->priv_handle;
  1663. if (!sde_rotator_is_request_retired(request))
  1664. sde_rotator_abort_inline_request(rot_dev->mgr,
  1665. ctx->private, request->req);
  1666. }
  1667. sde_rot_mgr_unlock(rot_dev->mgr);
  1668. mutex_unlock(&rot_dev->lock);
  1669. return 0;
  1670. error_handle_request:
  1671. sde_rotator_update_retire_sequence(request);
  1672. sde_rotator_retire_request(request);
  1673. error_retired_list:
  1674. error_session_validate:
  1675. error_session_config:
  1676. devm_kfree(rot_dev->dev, req);
  1677. error_invalid_handle:
  1678. error_init_request:
  1679. sde_rot_mgr_unlock(rot_dev->mgr);
  1680. mutex_unlock(&rot_dev->lock);
  1681. return ret;
  1682. }
  1683. EXPORT_SYMBOL(sde_rotator_inline_commit);
  1684. void sde_rotator_inline_reg_dump(struct platform_device *pdev)
  1685. {
  1686. struct sde_rotator_device *rot_dev;
  1687. if (!pdev) {
  1688. SDEROT_ERR("invalid platform device\n");
  1689. return;
  1690. }
  1691. rot_dev = (struct sde_rotator_device *) platform_get_drvdata(pdev);
  1692. if (!rot_dev || !rot_dev->mgr) {
  1693. SDEROT_ERR("invalid rotator device\n");
  1694. return;
  1695. }
  1696. sde_rot_mgr_lock(rot_dev->mgr);
  1697. sde_rotator_core_dump(rot_dev->mgr);
  1698. sde_rot_mgr_unlock(rot_dev->mgr);
  1699. }
  1700. EXPORT_SYMBOL(sde_rotator_inline_reg_dump);
  1701. /*
  1702. * sde_rotator_open - Rotator device open method.
  1703. * @file: Pointer to file struct.
  1704. */
  1705. static int sde_rotator_open(struct file *file)
  1706. {
  1707. struct sde_rotator_device *rot_dev = video_drvdata(file);
  1708. struct sde_rotator_ctx *ctx;
  1709. int ret = 0;
  1710. ctx = sde_rotator_ctx_open(rot_dev, file);
  1711. if (IS_ERR_OR_NULL(ctx)) {
  1712. SDEDEV_DBG(rot_dev->dev, "failed to open %d\n", ret);
  1713. ret = PTR_ERR(ctx);
  1714. }
  1715. return ret;
  1716. }
  1717. /*
  1718. * sde_rotator_release - Rotator device release method.
  1719. * @file: Pointer to file struct.
  1720. */
  1721. static int sde_rotator_release(struct file *file)
  1722. {
  1723. struct sde_rotator_ctx *ctx =
  1724. sde_rotator_ctx_from_fh(file->private_data);
  1725. return sde_rotator_ctx_release(ctx, file);
  1726. }
  1727. /*
  1728. * sde_rotator_poll - rotator device pool method.
  1729. * @file: Pointer to file struct.
  1730. * @wait: Pointer to poll table struct.
  1731. */
  1732. static unsigned int sde_rotator_poll(struct file *file,
  1733. struct poll_table_struct *wait)
  1734. {
  1735. struct sde_rotator_device *rot_dev = video_drvdata(file);
  1736. struct sde_rotator_ctx *ctx =
  1737. sde_rotator_ctx_from_fh(file->private_data);
  1738. int ret;
  1739. mutex_lock(&rot_dev->lock);
  1740. ret = v4l2_m2m_poll(file, ctx->fh.m2m_ctx, wait);
  1741. mutex_unlock(&rot_dev->lock);
  1742. return ret;
  1743. }
  1744. /* rotator device file operations callbacks */
  1745. static const struct v4l2_file_operations sde_rotator_fops = {
  1746. .owner = THIS_MODULE,
  1747. .open = sde_rotator_open,
  1748. .release = sde_rotator_release,
  1749. .poll = sde_rotator_poll,
  1750. .unlocked_ioctl = video_ioctl2,
  1751. #ifdef CONFIG_COMPAT
  1752. .compat_ioctl32 = sde_rotator_compat_ioctl32,
  1753. #endif
  1754. };
  1755. /*
  1756. * sde_rotator_querycap - V4l2 ioctl query capability handler.
  1757. * @file: Pointer to file struct.
  1758. * @fh: V4l2 File handle.
  1759. * @cap: Pointer to v4l2_capability struct need to be filled.
  1760. */
  1761. static int sde_rotator_querycap(struct file *file,
  1762. void *fh, struct v4l2_capability *cap)
  1763. {
  1764. cap->bus_info[0] = 0;
  1765. strlcpy(cap->driver, SDE_ROTATOR_DRV_NAME, sizeof(cap->driver));
  1766. strlcpy(cap->card, SDE_ROTATOR_DRV_NAME, sizeof(cap->card));
  1767. cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M |
  1768. V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_CAPTURE;
  1769. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1770. return 0;
  1771. }
  1772. /*
  1773. * sde_rotator_enum_fmt_vid_cap - V4l2 ioctl enumerate output format handler.
  1774. * @file: Pointer to file struct.
  1775. * @fh: V4l2 File handle.
  1776. * @f: Pointer to v4l2_fmtdesc struct need to be filled.
  1777. */
  1778. static int sde_rotator_enum_fmt_vid_cap(struct file *file,
  1779. void *fh, struct v4l2_fmtdesc *f)
  1780. {
  1781. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1782. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  1783. struct sde_mdp_format_params *fmt;
  1784. u32 i, index, pixfmt;
  1785. bool found = false;
  1786. for (i = 0, index = 0; index <= f->index; i++) {
  1787. pixfmt = sde_rotator_get_pixfmt(rot_dev->mgr, i, false,
  1788. SDE_ROTATOR_MODE_OFFLINE);
  1789. if (!pixfmt)
  1790. return -EINVAL;
  1791. fmt = sde_get_format_params(pixfmt);
  1792. if (!fmt)
  1793. return -EINVAL;
  1794. if (sde_mdp_is_private_format(fmt))
  1795. continue;
  1796. if (index == f->index) {
  1797. found = true;
  1798. break;
  1799. }
  1800. index++;
  1801. }
  1802. if (!found)
  1803. return -EINVAL;
  1804. f->pixelformat = pixfmt;
  1805. strlcpy(f->description, fmt->description, sizeof(f->description));
  1806. return 0;
  1807. }
  1808. /*
  1809. * sde_rotator_enum_fmt_vid_out - V4l2 ioctl enumerate capture format handler.
  1810. * @file: Pointer to file struct.
  1811. * @fh: V4l2 File handle.
  1812. * @f: Pointer to v4l2_fmtdesc struct need to be filled.
  1813. */
  1814. static int sde_rotator_enum_fmt_vid_out(struct file *file,
  1815. void *fh, struct v4l2_fmtdesc *f)
  1816. {
  1817. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1818. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  1819. struct sde_mdp_format_params *fmt;
  1820. u32 i, index, pixfmt;
  1821. bool found = false;
  1822. for (i = 0, index = 0; index <= f->index; i++) {
  1823. pixfmt = sde_rotator_get_pixfmt(rot_dev->mgr, i, true,
  1824. SDE_ROTATOR_MODE_OFFLINE);
  1825. if (!pixfmt)
  1826. return -EINVAL;
  1827. fmt = sde_get_format_params(pixfmt);
  1828. if (!fmt)
  1829. return -EINVAL;
  1830. if (sde_mdp_is_private_format(fmt))
  1831. continue;
  1832. if (index == f->index) {
  1833. found = true;
  1834. break;
  1835. }
  1836. index++;
  1837. }
  1838. if (!found)
  1839. return -EINVAL;
  1840. f->pixelformat = pixfmt;
  1841. strlcpy(f->description, fmt->description, sizeof(f->description));
  1842. return 0;
  1843. }
  1844. /*
  1845. * sde_rotator_g_fmt_cap - V4l2 ioctl get capture format handler.
  1846. * @file: Pointer to file struct.
  1847. * @fh: V4l2 File handle.
  1848. * @f: Pointer to v4l2_format struct need to be filled.
  1849. */
  1850. static int sde_rotator_g_fmt_cap(struct file *file, void *fh,
  1851. struct v4l2_format *f)
  1852. {
  1853. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1854. *f = ctx->format_cap;
  1855. return 0;
  1856. }
  1857. /*
  1858. * sde_rotator_g_fmt_out - V4l2 ioctl get output format handler.
  1859. * @file: Pointer to file struct.
  1860. * @fh: V4l2 File handle.
  1861. * @f: Pointer to v4l2_format struct need to be filled.
  1862. */
  1863. static int sde_rotator_g_fmt_out(struct file *file, void *fh,
  1864. struct v4l2_format *f)
  1865. {
  1866. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1867. *f = ctx->format_out;
  1868. return 0;
  1869. }
  1870. /*
  1871. * sde_rotator_try_fmt_vid_cap - V4l2 ioctl try capture format handler.
  1872. * @file: Pointer to file struct.
  1873. * @fh: V4l2 File handle.
  1874. * @f: Pointer to v4l2_format struct.
  1875. */
  1876. static int sde_rotator_try_fmt_vid_cap(struct file *file,
  1877. void *fh, struct v4l2_format *f)
  1878. {
  1879. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1880. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  1881. struct sde_rotation_config config;
  1882. int ret;
  1883. if ((f->fmt.pix.width == 0) || (f->fmt.pix.height == 0)) {
  1884. SDEDEV_WARN(ctx->rot_dev->dev,
  1885. "Not supporting 0 width/height: %dx%d\n",
  1886. f->fmt.pix.width, f->fmt.pix.height);
  1887. return -EINVAL;
  1888. }
  1889. sde_rot_mgr_lock(rot_dev->mgr);
  1890. sde_rotator_get_config_from_ctx(ctx, &config);
  1891. config.output.format = f->fmt.pix.pixelformat;
  1892. config.output.width = f->fmt.pix.width;
  1893. config.output.height = f->fmt.pix.height;
  1894. config.flags |= SDE_ROTATION_VERIFY_INPUT_ONLY;
  1895. ret = sde_rotator_verify_config_output(rot_dev->mgr, &config);
  1896. sde_rot_mgr_unlock(rot_dev->mgr);
  1897. if (ret) {
  1898. if ((config.output.width == f->fmt.pix.width) &&
  1899. (config.output.height == f->fmt.pix.height)) {
  1900. SDEDEV_WARN(ctx->rot_dev->dev,
  1901. "invalid capture format 0x%8.8x %dx%d\n",
  1902. f->fmt.pix.pixelformat,
  1903. f->fmt.pix.width,
  1904. f->fmt.pix.height);
  1905. return -EINVAL;
  1906. }
  1907. f->fmt.pix.width = config.output.width;
  1908. f->fmt.pix.height = config.output.height;
  1909. }
  1910. sde_rotator_format_recalc(f);
  1911. return ret;
  1912. }
  1913. /*
  1914. * sde_rotator_try_fmt_vid_out - V4l2 ioctl try output format handler.
  1915. * @file: Pointer to file struct.
  1916. * @fh: V4l2 File handle.
  1917. * @f: Pointer to v4l2_format struct.
  1918. */
  1919. static int sde_rotator_try_fmt_vid_out(struct file *file,
  1920. void *fh, struct v4l2_format *f)
  1921. {
  1922. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1923. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  1924. struct sde_rotation_config config;
  1925. int ret;
  1926. if ((f->fmt.pix.width == 0) || (f->fmt.pix.height == 0)) {
  1927. SDEDEV_WARN(ctx->rot_dev->dev,
  1928. "Not supporting 0 width/height: %dx%d\n",
  1929. f->fmt.pix.width, f->fmt.pix.height);
  1930. return -EINVAL;
  1931. }
  1932. sde_rot_mgr_lock(rot_dev->mgr);
  1933. sde_rotator_get_config_from_ctx(ctx, &config);
  1934. config.input.format = f->fmt.pix.pixelformat;
  1935. config.input.width = f->fmt.pix.width;
  1936. config.input.height = f->fmt.pix.height;
  1937. config.flags |= SDE_ROTATION_VERIFY_INPUT_ONLY;
  1938. ret = sde_rotator_verify_config_input(rot_dev->mgr, &config);
  1939. sde_rot_mgr_unlock(rot_dev->mgr);
  1940. if (ret) {
  1941. if ((config.input.width == f->fmt.pix.width) &&
  1942. (config.input.height == f->fmt.pix.height)) {
  1943. SDEDEV_WARN(ctx->rot_dev->dev,
  1944. "invalid output format 0x%8.8x %dx%d\n",
  1945. f->fmt.pix.pixelformat,
  1946. f->fmt.pix.width,
  1947. f->fmt.pix.height);
  1948. return -EINVAL;
  1949. }
  1950. f->fmt.pix.width = config.input.width;
  1951. f->fmt.pix.height = config.input.height;
  1952. }
  1953. sde_rotator_format_recalc(f);
  1954. return ret;
  1955. }
  1956. /*
  1957. * sde_rotator_s_fmt_vid_cap - V4l2 ioctl set capture format handler.
  1958. * @file: Pointer to file struct.
  1959. * @fh: V4l2 File handle.
  1960. * @f: Pointer to v4l2_format struct.
  1961. */
  1962. static int sde_rotator_s_fmt_vid_cap(struct file *file,
  1963. void *fh, struct v4l2_format *f)
  1964. {
  1965. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1966. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  1967. int ret;
  1968. ret = sde_rotator_try_fmt_vid_cap(file, fh, f);
  1969. if (ret)
  1970. return -EINVAL;
  1971. /* Initialize crop */
  1972. ctx->crop_cap.top = 0;
  1973. ctx->crop_cap.left = 0;
  1974. ctx->crop_cap.width = f->fmt.pix.width;
  1975. ctx->crop_cap.height = f->fmt.pix.height;
  1976. ctx->format_cap = *f;
  1977. SDEDEV_DBG(rot_dev->dev,
  1978. "s_fmt s:%d t:%d fmt:0x%8.8x field:%u (%u,%u)\n",
  1979. ctx->session_id, f->type,
  1980. f->fmt.pix.pixelformat,
  1981. f->fmt.pix.field,
  1982. f->fmt.pix.width, f->fmt.pix.height);
  1983. return 0;
  1984. }
  1985. /*
  1986. * sde_rotator_s_fmt_vid_out - V4l2 ioctl set output format handler.
  1987. * @file: Pointer to file struct.
  1988. * @fh: V4l2 File handle.
  1989. * @f: Pointer to v4l2_format struct.
  1990. */
  1991. static int sde_rotator_s_fmt_vid_out(struct file *file,
  1992. void *fh, struct v4l2_format *f)
  1993. {
  1994. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1995. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  1996. int ret;
  1997. ret = sde_rotator_try_fmt_vid_out(file, fh, f);
  1998. if (ret)
  1999. return -EINVAL;
  2000. /* Initialize crop */
  2001. ctx->crop_out.top = 0;
  2002. ctx->crop_out.left = 0;
  2003. ctx->crop_out.width = f->fmt.pix.width;
  2004. ctx->crop_out.height = f->fmt.pix.height;
  2005. ctx->format_out = *f;
  2006. SDEDEV_DBG(rot_dev->dev,
  2007. "s_fmt s:%d t:%d fmt:0x%8.8x field:%u (%u,%u)\n",
  2008. ctx->session_id, f->type,
  2009. f->fmt.pix.pixelformat,
  2010. f->fmt.pix.field,
  2011. f->fmt.pix.width, f->fmt.pix.height);
  2012. return 0;
  2013. }
  2014. /*
  2015. * sde_rotator_reqbufs - V4l2 ioctl request buffers handler.
  2016. * @file: Pointer to file struct.
  2017. * @fh: V4l2 File handle.
  2018. * @req: Pointer to v4l2_requestbuffer struct.
  2019. */
  2020. static int sde_rotator_reqbufs(struct file *file,
  2021. void *fh, struct v4l2_requestbuffers *req)
  2022. {
  2023. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2024. return v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, req);
  2025. }
  2026. /*
  2027. * sde_rotator_qbuf - V4l2 ioctl queue buffer handler.
  2028. * @file: Pointer to file struct.
  2029. * @fh: V4l2 File handle.
  2030. * @buf: Pointer to v4l2_buffer struct.
  2031. */
  2032. static int sde_rotator_qbuf(struct file *file, void *fh,
  2033. struct v4l2_buffer *buf)
  2034. {
  2035. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2036. int ret;
  2037. /* create fence for capture buffer */
  2038. if ((buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2039. && (buf->index < ctx->nbuf_cap)) {
  2040. int idx = buf->index;
  2041. ctx->vbinfo_cap[idx].fd = -1;
  2042. ctx->vbinfo_cap[idx].fence = sde_rotator_get_sync_fence(
  2043. ctx->work_queue.timeline, NULL,
  2044. &ctx->vbinfo_cap[idx].fence_ts);
  2045. ctx->vbinfo_cap[idx].qbuf_ts = ktime_get();
  2046. ctx->vbinfo_cap[idx].dqbuf_ts = NULL;
  2047. SDEDEV_DBG(ctx->rot_dev->dev,
  2048. "create buffer fence s:%d.%u i:%d f:%pK\n",
  2049. ctx->session_id,
  2050. ctx->vbinfo_cap[idx].fence_ts,
  2051. idx,
  2052. ctx->vbinfo_cap[idx].fence);
  2053. } else if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  2054. && (buf->index < ctx->nbuf_out)) {
  2055. int idx = buf->index;
  2056. ctx->vbinfo_out[idx].qbuf_ts = ktime_get();
  2057. ctx->vbinfo_out[idx].dqbuf_ts = NULL;
  2058. }
  2059. ret = v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
  2060. if (ret < 0)
  2061. SDEDEV_ERR(ctx->rot_dev->dev, "fail qbuf s:%d t:%d r:%d\n",
  2062. ctx->session_id, buf->type, ret);
  2063. SDEROT_EVTLOG(buf->type, buf->bytesused, buf->length, buf->m.fd, ret);
  2064. return ret;
  2065. }
  2066. /*
  2067. * sde_rotator_dqbuf - V4l2 ioctl dequeue buffer handler.
  2068. * @file: Pointer to file struct.
  2069. * @fh: V4l2 File handle.
  2070. * @buf: Pointer to v4l2_buffer struct.
  2071. */
  2072. static int sde_rotator_dqbuf(struct file *file,
  2073. void *fh, struct v4l2_buffer *buf)
  2074. {
  2075. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2076. int ret;
  2077. ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
  2078. if (ret) {
  2079. SDEDEV_ERR(ctx->rot_dev->dev,
  2080. "fail dqbuf s:%d t:%d i:%d r:%d\n",
  2081. ctx->session_id, buf->type, buf->index, ret);
  2082. return ret;
  2083. }
  2084. /* clear fence for buffer */
  2085. if ((buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2086. && (buf->index < ctx->nbuf_cap)) {
  2087. int idx = buf->index;
  2088. if (ctx->vbinfo_cap[idx].fence) {
  2089. /* fence is not used */
  2090. SDEDEV_DBG(ctx->rot_dev->dev, "put fence s:%d i:%d\n",
  2091. ctx->session_id, idx);
  2092. sde_rotator_put_sync_fence(ctx->vbinfo_cap[idx].fence);
  2093. }
  2094. ctx->vbinfo_cap[idx].fence = NULL;
  2095. ctx->vbinfo_cap[idx].fd = -1;
  2096. if (ctx->vbinfo_cap[idx].dqbuf_ts)
  2097. *(ctx->vbinfo_cap[idx].dqbuf_ts) = ktime_get();
  2098. } else if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  2099. && (buf->index < ctx->nbuf_out)) {
  2100. int idx = buf->index;
  2101. ctx->vbinfo_out[idx].fence = NULL;
  2102. ctx->vbinfo_out[idx].fd = -1;
  2103. if (ctx->vbinfo_out[idx].dqbuf_ts)
  2104. *(ctx->vbinfo_out[idx].dqbuf_ts) = ktime_get();
  2105. } else {
  2106. SDEDEV_WARN(ctx->rot_dev->dev, "invalid dq s:%d t:%d i:%d\n",
  2107. ctx->session_id, buf->type, buf->index);
  2108. }
  2109. return 0;
  2110. }
  2111. /*
  2112. * sde_rotator_querybuf - V4l2 ioctl query buffer handler.
  2113. * @file: Pointer to file struct.
  2114. * @fh: V4l2 File handle.
  2115. * @buf: Pointer to v4l2_buffer struct.
  2116. */
  2117. static int sde_rotator_querybuf(struct file *file,
  2118. void *fh, struct v4l2_buffer *buf)
  2119. {
  2120. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2121. return v4l2_m2m_querybuf(file, ctx->fh.m2m_ctx, buf);
  2122. }
  2123. /*
  2124. * sde_rotator_streamon - V4l2 ioctl stream on handler.
  2125. * @file: Pointer to file struct.
  2126. * @fh: V4l2 File handle.
  2127. * @buf_type: V4l2 buffer type.
  2128. */
  2129. static int sde_rotator_streamon(struct file *file,
  2130. void *fh, enum v4l2_buf_type buf_type)
  2131. {
  2132. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2133. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  2134. struct sde_rotation_config config;
  2135. struct vb2_queue *vq;
  2136. int ret;
  2137. SDEDEV_DBG(ctx->rot_dev->dev, "stream on s:%d t:%d\n",
  2138. ctx->session_id, buf_type);
  2139. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
  2140. buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT ?
  2141. V4L2_BUF_TYPE_VIDEO_CAPTURE :
  2142. V4L2_BUF_TYPE_VIDEO_OUTPUT);
  2143. if (!vq) {
  2144. SDEDEV_ERR(ctx->rot_dev->dev, "fail to get vq on s:%d t:%d\n",
  2145. ctx->session_id, buf_type);
  2146. return -EINVAL;
  2147. }
  2148. if (vb2_is_streaming(vq)) {
  2149. sde_rot_mgr_lock(rot_dev->mgr);
  2150. sde_rotator_get_config_from_ctx(ctx, &config);
  2151. config.flags &= ~SDE_ROTATION_VERIFY_INPUT_ONLY;
  2152. ret = sde_rotator_session_config(rot_dev->mgr, ctx->private,
  2153. &config);
  2154. sde_rot_mgr_unlock(rot_dev->mgr);
  2155. if (ret < 0) {
  2156. SDEDEV_ERR(rot_dev->dev,
  2157. "fail config in stream on s:%d t:%d r:%d\n",
  2158. ctx->session_id, buf_type, ret);
  2159. return ret;
  2160. }
  2161. ctx->rotcfg = config;
  2162. }
  2163. ret = v4l2_m2m_streamon(file, ctx->fh.m2m_ctx, buf_type);
  2164. if (ret < 0)
  2165. SDEDEV_ERR(ctx->rot_dev->dev, "fail stream on s:%d t:%d\n",
  2166. ctx->session_id, buf_type);
  2167. return ret;
  2168. }
  2169. /*
  2170. * sde_rotator_streamoff - V4l2 ioctl stream off handler.
  2171. * @file: Pointer to file struct.
  2172. * @fh: V4l2 File handle.
  2173. * @buf_type: V4l2 buffer type.
  2174. */
  2175. static int sde_rotator_streamoff(struct file *file,
  2176. void *fh, enum v4l2_buf_type buf_type)
  2177. {
  2178. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2179. int ret;
  2180. SDEDEV_DBG(ctx->rot_dev->dev, "stream off s:%d t:%d\n",
  2181. ctx->session_id, buf_type);
  2182. ret = v4l2_m2m_streamoff(file, ctx->fh.m2m_ctx, buf_type);
  2183. if (ret < 0)
  2184. SDEDEV_ERR(ctx->rot_dev->dev, "fail stream off s:%d t:%d\n",
  2185. ctx->session_id, buf_type);
  2186. return ret;
  2187. }
  2188. /*
  2189. * sde_rotator_cropcap - V4l2 ioctl crop capabilities.
  2190. * @file: Pointer to file struct.
  2191. * @fh: V4l2 File handle.
  2192. * @a: Pointer to v4l2_cropcap struct need to be set.
  2193. */
  2194. static int sde_rotator_cropcap(struct file *file, void *fh,
  2195. struct v4l2_cropcap *a)
  2196. {
  2197. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2198. struct v4l2_format *format;
  2199. struct v4l2_rect *crop;
  2200. switch (a->type) {
  2201. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2202. format = &ctx->format_out;
  2203. crop = &ctx->crop_out;
  2204. break;
  2205. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2206. format = &ctx->format_cap;
  2207. crop = &ctx->crop_cap;
  2208. break;
  2209. default:
  2210. return -EINVAL;
  2211. }
  2212. a->bounds.top = 0;
  2213. a->bounds.left = 0;
  2214. a->bounds.width = format->fmt.pix.width;
  2215. a->bounds.height = format->fmt.pix.height;
  2216. a->defrect = *crop;
  2217. a->pixelaspect.numerator = 1;
  2218. a->pixelaspect.denominator = 1;
  2219. SDEROT_EVTLOG(format->fmt.pix.width, format->fmt.pix.height, a->type);
  2220. return 0;
  2221. }
  2222. /*
  2223. * sde_rotator_g_crop - V4l2 ioctl get crop.
  2224. * @file: Pointer to file struct.
  2225. * @fh: V4l2 File handle.
  2226. * @crop: Pointer to v4l2_crop struct need to be set.
  2227. */
  2228. static int sde_rotator_g_crop(struct file *file, void *fh,
  2229. struct v4l2_crop *crop)
  2230. {
  2231. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2232. switch (crop->type) {
  2233. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2234. crop->c = ctx->crop_out;
  2235. break;
  2236. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2237. crop->c = ctx->crop_cap;
  2238. break;
  2239. default:
  2240. return -EINVAL;
  2241. }
  2242. return 0;
  2243. }
  2244. /*
  2245. * sde_rotator_s_crop - V4l2 ioctl set crop.
  2246. * @file: Pointer to file struct.
  2247. * @fh: V4l2 File handle.
  2248. * @crop: Pointer to v4l2_crop struct need to be set.
  2249. */
  2250. static int sde_rotator_s_crop(struct file *file, void *fh,
  2251. const struct v4l2_crop *crop)
  2252. {
  2253. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2254. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  2255. struct sde_rotation_item item;
  2256. struct v4l2_rect rect;
  2257. sde_rotator_get_item_from_ctx(ctx, &item);
  2258. rect.left = max_t(__u32, crop->c.left, 0);
  2259. rect.top = max_t(__u32, crop->c.top, 0);
  2260. rect.height = max_t(__u32, crop->c.height, 0);
  2261. rect.width = max_t(__u32, crop->c.width, 0);
  2262. if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  2263. rect.left = min_t(__u32, rect.left,
  2264. ctx->format_out.fmt.pix.width - 1);
  2265. rect.top = min_t(__u32, rect.top,
  2266. ctx->format_out.fmt.pix.height - 1);
  2267. rect.width = min_t(__u32, rect.width,
  2268. (ctx->format_out.fmt.pix.width - rect.left));
  2269. rect.height = min_t(__u32, rect.height,
  2270. (ctx->format_out.fmt.pix.height - rect.top));
  2271. item.src_rect.x = rect.left;
  2272. item.src_rect.y = rect.top;
  2273. item.src_rect.w = rect.width;
  2274. item.src_rect.h = rect.height;
  2275. sde_rotator_validate_item(ctx, &item);
  2276. SDEDEV_DBG(rot_dev->dev,
  2277. "s_crop s:%d t:%d (%u,%u,%u,%u)->(%u,%u,%u,%u)\n",
  2278. ctx->session_id, crop->type,
  2279. crop->c.left, crop->c.top,
  2280. crop->c.width, crop->c.height,
  2281. item.src_rect.x, item.src_rect.y,
  2282. item.src_rect.w, item.src_rect.h);
  2283. ctx->crop_out.left = item.src_rect.x;
  2284. ctx->crop_out.top = item.src_rect.y;
  2285. ctx->crop_out.width = item.src_rect.w;
  2286. ctx->crop_out.height = item.src_rect.h;
  2287. } else if (crop->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2288. rect.left = min_t(__u32, rect.left,
  2289. ctx->format_cap.fmt.pix.width - 1);
  2290. rect.top = min_t(__u32, rect.top,
  2291. ctx->format_cap.fmt.pix.height - 1);
  2292. rect.width = min_t(__u32, rect.width,
  2293. (ctx->format_cap.fmt.pix.width - rect.left));
  2294. rect.height = min_t(__u32, rect.height,
  2295. (ctx->format_cap.fmt.pix.height - rect.top));
  2296. item.dst_rect.x = rect.left;
  2297. item.dst_rect.y = rect.top;
  2298. item.dst_rect.w = rect.width;
  2299. item.dst_rect.h = rect.height;
  2300. sde_rotator_validate_item(ctx, &item);
  2301. SDEDEV_DBG(rot_dev->dev,
  2302. "s_crop s:%d t:%d (%u,%u,%u,%u)->(%u,%u,%u,%u)\n",
  2303. ctx->session_id, crop->type,
  2304. crop->c.left, crop->c.top,
  2305. crop->c.width, crop->c.height,
  2306. item.dst_rect.x, item.dst_rect.y,
  2307. item.dst_rect.w, item.dst_rect.h);
  2308. ctx->crop_cap.left = item.dst_rect.x;
  2309. ctx->crop_cap.top = item.dst_rect.y;
  2310. ctx->crop_cap.width = item.dst_rect.w;
  2311. ctx->crop_cap.height = item.dst_rect.h;
  2312. } else {
  2313. return -EINVAL;
  2314. }
  2315. return 0;
  2316. }
  2317. /*
  2318. * sde_rotator_g_parm - V4l2 ioctl get parm.
  2319. * @file: Pointer to file struct.
  2320. * @fh: V4l2 File handle.
  2321. * @a: Pointer to v4l2_streamparm struct need to be filled.
  2322. */
  2323. static int sde_rotator_g_parm(struct file *file, void *fh,
  2324. struct v4l2_streamparm *a)
  2325. {
  2326. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2327. /* Get param is supported only for input buffers */
  2328. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  2329. return -EINVAL;
  2330. a->parm.output.capability = 0;
  2331. a->parm.output.extendedmode = 0;
  2332. a->parm.output.outputmode = 0;
  2333. a->parm.output.writebuffers = 0;
  2334. a->parm.output.timeperframe = ctx->timeperframe;
  2335. return 0;
  2336. }
  2337. /*
  2338. * sde_rotator_s_parm - V4l2 ioctl set parm.
  2339. * @file: Pointer to file struct.
  2340. * @fh: V4l2 File handle.
  2341. * @a: Pointer to v4l2_streamparm struct need to be set.
  2342. */
  2343. static int sde_rotator_s_parm(struct file *file, void *fh,
  2344. struct v4l2_streamparm *a)
  2345. {
  2346. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2347. /* Set param is supported only for input buffers */
  2348. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  2349. return -EINVAL;
  2350. if (!a->parm.output.timeperframe.numerator ||
  2351. !a->parm.output.timeperframe.denominator)
  2352. return -EINVAL;
  2353. ctx->timeperframe = a->parm.output.timeperframe;
  2354. return 0;
  2355. }
  2356. /*
  2357. * sde_rotator_private_ioctl - V4l2 private ioctl handler.
  2358. * @file: Pointer to file struct.
  2359. * @fd: V4l2 device file handle.
  2360. * @valid_prio: Priority ioctl valid flag.
  2361. * @cmd: Ioctl command.
  2362. * @arg: Ioctl argument.
  2363. */
  2364. static long sde_rotator_private_ioctl(struct file *file, void *fh,
  2365. bool valid_prio, unsigned int cmd, void *arg)
  2366. {
  2367. struct sde_rotator_ctx *ctx =
  2368. sde_rotator_ctx_from_fh(file->private_data);
  2369. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  2370. struct msm_sde_rotator_fence *fence = arg;
  2371. struct msm_sde_rotator_comp_ratio *comp_ratio = arg;
  2372. struct sde_rotator_vbinfo *vbinfo;
  2373. int ret;
  2374. switch (cmd) {
  2375. case VIDIOC_S_SDE_ROTATOR_FENCE:
  2376. if (!fence)
  2377. return -EINVAL;
  2378. if (fence->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  2379. return -EINVAL;
  2380. if (fence->index >= ctx->nbuf_out)
  2381. return -EINVAL;
  2382. SDEDEV_DBG(rot_dev->dev,
  2383. "VIDIOC_S_SDE_ROTATOR_FENCE s:%d i:%d fd:%d\n",
  2384. ctx->session_id, fence->index,
  2385. fence->fd);
  2386. vbinfo = &ctx->vbinfo_out[fence->index];
  2387. if (vbinfo->fd >= 0) {
  2388. if (vbinfo->fence) {
  2389. SDEDEV_DBG(rot_dev->dev,
  2390. "put fence s:%d t:%d i:%d\n",
  2391. ctx->session_id,
  2392. fence->type, fence->index);
  2393. sde_rotator_put_sync_fence(vbinfo->fence);
  2394. }
  2395. vbinfo->fence = NULL;
  2396. vbinfo->fd = -1;
  2397. }
  2398. vbinfo->fd = fence->fd;
  2399. if (vbinfo->fd >= 0) {
  2400. vbinfo->fence =
  2401. sde_rotator_get_fd_sync_fence(vbinfo->fd);
  2402. if (!vbinfo->fence) {
  2403. SDEDEV_WARN(rot_dev->dev,
  2404. "invalid input fence fd s:%d fd:%d\n",
  2405. ctx->session_id, vbinfo->fd);
  2406. vbinfo->fd = -1;
  2407. return -EINVAL;
  2408. }
  2409. } else {
  2410. vbinfo->fence = NULL;
  2411. }
  2412. break;
  2413. case VIDIOC_G_SDE_ROTATOR_FENCE:
  2414. if (!fence)
  2415. return -EINVAL;
  2416. if (fence->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2417. return -EINVAL;
  2418. if (fence->index >= ctx->nbuf_cap)
  2419. return -EINVAL;
  2420. vbinfo = &ctx->vbinfo_cap[fence->index];
  2421. if (!vbinfo)
  2422. return -EINVAL;
  2423. if (vbinfo->fence) {
  2424. ret = sde_rotator_get_sync_fence_fd(vbinfo->fence);
  2425. if (ret < 0) {
  2426. SDEDEV_ERR(rot_dev->dev,
  2427. "fail get fence fd s:%d\n",
  2428. ctx->session_id);
  2429. return ret;
  2430. }
  2431. /**
  2432. * Cache fence descriptor in case user calls this
  2433. * ioctl multiple times. Cached value would be stale
  2434. * if user duplicated and closed old descriptor.
  2435. */
  2436. vbinfo->fd = ret;
  2437. } else if (!sde_rotator_get_fd_sync_fence(vbinfo->fd)) {
  2438. /**
  2439. * User has closed cached fence descriptor.
  2440. * Invalidate descriptor cache.
  2441. */
  2442. vbinfo->fd = -1;
  2443. }
  2444. fence->fd = vbinfo->fd;
  2445. SDEDEV_DBG(rot_dev->dev,
  2446. "VIDIOC_G_SDE_ROTATOR_FENCE s:%d i:%d fd:%d\n",
  2447. ctx->session_id, fence->index,
  2448. fence->fd);
  2449. break;
  2450. case VIDIOC_S_SDE_ROTATOR_COMP_RATIO:
  2451. if (!comp_ratio)
  2452. return -EINVAL;
  2453. else if (!comp_ratio->numer || !comp_ratio->denom)
  2454. return -EINVAL;
  2455. else if (comp_ratio->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2456. comp_ratio->index < ctx->nbuf_out)
  2457. vbinfo = &ctx->vbinfo_out[comp_ratio->index];
  2458. else if (comp_ratio->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2459. comp_ratio->index < ctx->nbuf_cap)
  2460. vbinfo = &ctx->vbinfo_cap[comp_ratio->index];
  2461. else
  2462. return -EINVAL;
  2463. vbinfo->comp_ratio.numer = comp_ratio->numer;
  2464. vbinfo->comp_ratio.denom = comp_ratio->denom;
  2465. SDEDEV_DBG(rot_dev->dev,
  2466. "VIDIOC_S_SDE_ROTATOR_COMP_RATIO s:%d i:%d t:%d cr:%u/%u\n",
  2467. ctx->session_id, comp_ratio->index,
  2468. comp_ratio->type,
  2469. vbinfo->comp_ratio.numer,
  2470. vbinfo->comp_ratio.denom);
  2471. break;
  2472. case VIDIOC_G_SDE_ROTATOR_COMP_RATIO:
  2473. if (!comp_ratio)
  2474. return -EINVAL;
  2475. else if (comp_ratio->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2476. comp_ratio->index < ctx->nbuf_out)
  2477. vbinfo = &ctx->vbinfo_out[comp_ratio->index];
  2478. else if (comp_ratio->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2479. comp_ratio->index < ctx->nbuf_cap)
  2480. vbinfo = &ctx->vbinfo_cap[comp_ratio->index];
  2481. else
  2482. return -EINVAL;
  2483. comp_ratio->numer = vbinfo->comp_ratio.numer;
  2484. comp_ratio->denom = vbinfo->comp_ratio.denom;
  2485. SDEDEV_DBG(rot_dev->dev,
  2486. "VIDIOC_G_SDE_ROTATOR_COMP_RATIO s:%d i:%d t:%d cr:%u/%u\n",
  2487. ctx->session_id, comp_ratio->index,
  2488. comp_ratio->type,
  2489. comp_ratio->numer,
  2490. comp_ratio->denom);
  2491. break;
  2492. default:
  2493. SDEDEV_WARN(rot_dev->dev, "invalid ioctl type %x\n", cmd);
  2494. return -ENOTTY;
  2495. }
  2496. return 0;
  2497. }
  2498. #ifdef CONFIG_COMPAT
  2499. /*
  2500. * sde_rotator_compat_ioctl32 - Compat ioctl handler function.
  2501. * @file: Pointer to file struct.
  2502. * @cmd: Ioctl command.
  2503. * @arg: Ioctl argument.
  2504. */
  2505. static long sde_rotator_compat_ioctl32(struct file *file,
  2506. unsigned int cmd, unsigned long arg)
  2507. {
  2508. struct video_device *vdev = video_devdata(file);
  2509. struct sde_rotator_ctx *ctx =
  2510. sde_rotator_ctx_from_fh(file->private_data);
  2511. long ret;
  2512. mutex_lock(vdev->lock);
  2513. switch (cmd) {
  2514. case VIDIOC_S_SDE_ROTATOR_FENCE:
  2515. case VIDIOC_G_SDE_ROTATOR_FENCE:
  2516. {
  2517. struct msm_sde_rotator_fence fence;
  2518. if (copy_from_user(&fence, (void __user *)arg,
  2519. sizeof(struct msm_sde_rotator_fence)))
  2520. goto ioctl32_error;
  2521. ret = sde_rotator_private_ioctl(file, file->private_data,
  2522. 0, cmd, (void *)&fence);
  2523. if (copy_to_user((void __user *)arg, &fence,
  2524. sizeof(struct msm_sde_rotator_fence)))
  2525. goto ioctl32_error;
  2526. break;
  2527. }
  2528. case VIDIOC_S_SDE_ROTATOR_COMP_RATIO:
  2529. case VIDIOC_G_SDE_ROTATOR_COMP_RATIO:
  2530. {
  2531. struct msm_sde_rotator_comp_ratio comp_ratio;
  2532. if (copy_from_user(&comp_ratio, (void __user *)arg,
  2533. sizeof(struct msm_sde_rotator_comp_ratio)))
  2534. goto ioctl32_error;
  2535. ret = sde_rotator_private_ioctl(file, file->private_data,
  2536. 0, cmd, (void *)&comp_ratio);
  2537. if (copy_to_user((void __user *)arg, &comp_ratio,
  2538. sizeof(struct msm_sde_rotator_comp_ratio)))
  2539. goto ioctl32_error;
  2540. break;
  2541. }
  2542. default:
  2543. SDEDEV_ERR(ctx->rot_dev->dev, "invalid ioctl32 type:%x\n", cmd);
  2544. ret = -ENOIOCTLCMD;
  2545. break;
  2546. }
  2547. mutex_unlock(vdev->lock);
  2548. return ret;
  2549. ioctl32_error:
  2550. mutex_unlock(vdev->lock);
  2551. SDEDEV_ERR(ctx->rot_dev->dev, "error handling ioctl32 cmd:%x\n", cmd);
  2552. return -EFAULT;
  2553. }
  2554. #endif
  2555. static int sde_rotator_ctrl_subscribe_event(struct v4l2_fh *fh,
  2556. const struct v4l2_event_subscription *sub)
  2557. {
  2558. return -EINVAL;
  2559. }
  2560. static int sde_rotator_event_unsubscribe(struct v4l2_fh *fh,
  2561. const struct v4l2_event_subscription *sub)
  2562. {
  2563. return -EINVAL;
  2564. }
  2565. /* V4l2 ioctl handlers */
  2566. static const struct v4l2_ioctl_ops sde_rotator_ioctl_ops = {
  2567. .vidioc_querycap = sde_rotator_querycap,
  2568. .vidioc_enum_fmt_vid_out = sde_rotator_enum_fmt_vid_out,
  2569. .vidioc_enum_fmt_vid_cap = sde_rotator_enum_fmt_vid_cap,
  2570. .vidioc_g_fmt_vid_out = sde_rotator_g_fmt_out,
  2571. .vidioc_g_fmt_vid_cap = sde_rotator_g_fmt_cap,
  2572. .vidioc_try_fmt_vid_out = sde_rotator_try_fmt_vid_out,
  2573. .vidioc_try_fmt_vid_cap = sde_rotator_try_fmt_vid_cap,
  2574. .vidioc_s_fmt_vid_out = sde_rotator_s_fmt_vid_out,
  2575. .vidioc_s_fmt_vid_cap = sde_rotator_s_fmt_vid_cap,
  2576. .vidioc_reqbufs = sde_rotator_reqbufs,
  2577. .vidioc_qbuf = sde_rotator_qbuf,
  2578. .vidioc_dqbuf = sde_rotator_dqbuf,
  2579. .vidioc_querybuf = sde_rotator_querybuf,
  2580. .vidioc_streamon = sde_rotator_streamon,
  2581. .vidioc_streamoff = sde_rotator_streamoff,
  2582. .vidioc_cropcap = sde_rotator_cropcap,
  2583. .vidioc_g_crop = sde_rotator_g_crop,
  2584. .vidioc_s_crop = sde_rotator_s_crop,
  2585. .vidioc_g_parm = sde_rotator_g_parm,
  2586. .vidioc_s_parm = sde_rotator_s_parm,
  2587. .vidioc_default = sde_rotator_private_ioctl,
  2588. .vidioc_log_status = v4l2_ctrl_log_status,
  2589. .vidioc_subscribe_event = sde_rotator_ctrl_subscribe_event,
  2590. .vidioc_unsubscribe_event = sde_rotator_event_unsubscribe,
  2591. };
  2592. /*
  2593. * sde_rotator_retire_handler - Invoked by hal when processing is done.
  2594. * @work: Pointer to work structure.
  2595. *
  2596. * This function is scheduled in work queue context.
  2597. */
  2598. static void sde_rotator_retire_handler(struct kthread_work *work)
  2599. {
  2600. struct vb2_v4l2_buffer *src_buf;
  2601. struct vb2_v4l2_buffer *dst_buf;
  2602. struct sde_rotator_ctx *ctx;
  2603. struct sde_rotator_device *rot_dev;
  2604. struct sde_rotator_request *request;
  2605. request = container_of(work, struct sde_rotator_request, retire_work);
  2606. ctx = request->ctx;
  2607. if (!ctx || !ctx->rot_dev) {
  2608. SDEROT_ERR("null context/device\n");
  2609. return;
  2610. }
  2611. rot_dev = ctx->rot_dev;
  2612. SDEDEV_DBG(rot_dev->dev, "retire handler s:%d\n", ctx->session_id);
  2613. mutex_lock(&rot_dev->lock);
  2614. if (ctx->abort_pending) {
  2615. SDEDEV_DBG(rot_dev->dev, "abort command in retire s:%d\n",
  2616. ctx->session_id);
  2617. sde_rotator_update_retire_sequence(request);
  2618. sde_rotator_retire_request(request);
  2619. mutex_unlock(&rot_dev->lock);
  2620. return;
  2621. }
  2622. if (!ctx->file) {
  2623. sde_rotator_update_retire_sequence(request);
  2624. } else if (rot_dev->early_submit) {
  2625. if (IS_ERR_OR_NULL(request->req)) {
  2626. /* fail pending request or something wrong */
  2627. SDEDEV_ERR(rot_dev->dev,
  2628. "pending request fail in retire s:%d\n",
  2629. ctx->session_id);
  2630. }
  2631. /* pending request. reschedule this context. */
  2632. v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
  2633. } else {
  2634. /* no pending request. acknowledge the usual way. */
  2635. src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  2636. dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  2637. if (!src_buf || !dst_buf) {
  2638. SDEDEV_ERR(rot_dev->dev,
  2639. "null buffer in retire s:%d sb:%pK db:%pK\n",
  2640. ctx->session_id,
  2641. src_buf, dst_buf);
  2642. }
  2643. sde_rotator_update_retire_sequence(request);
  2644. sde_rotator_retire_request(request);
  2645. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
  2646. v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
  2647. v4l2_m2m_job_finish(rot_dev->m2m_dev, ctx->fh.m2m_ctx);
  2648. }
  2649. mutex_unlock(&rot_dev->lock);
  2650. }
  2651. /*
  2652. * sde_rotator_process_buffers - Start rotator processing.
  2653. * @ctx: Pointer rotator context.
  2654. * @src_buf: Pointer to Vb2 source buffer.
  2655. * @dst_buf: Pointer to Vb2 destination buffer.
  2656. * @request: Pointer to rotator request
  2657. */
  2658. static int sde_rotator_process_buffers(struct sde_rotator_ctx *ctx,
  2659. struct vb2_buffer *src_buf, struct vb2_buffer *dst_buf,
  2660. struct sde_rotator_request *request)
  2661. {
  2662. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  2663. struct sde_rotation_item item;
  2664. struct sde_rot_entry_container *req = NULL;
  2665. struct sde_rotator_buf_handle *src_handle;
  2666. struct sde_rotator_buf_handle *dst_handle;
  2667. struct sde_rotator_statistics *stats = &rot_dev->stats;
  2668. struct sde_rotator_vbinfo *vbinfo_out;
  2669. struct sde_rotator_vbinfo *vbinfo_cap;
  2670. struct sde_rot_trace_entry rot_trace;
  2671. ktime_t *ts;
  2672. int ret;
  2673. if (!src_buf || !dst_buf) {
  2674. SDEDEV_ERR(rot_dev->dev, "null vb2 buffers\n");
  2675. ret = -EINVAL;
  2676. goto error_null_buffer;
  2677. }
  2678. src_handle = src_buf->planes[0].mem_priv;
  2679. dst_handle = dst_buf->planes[0].mem_priv;
  2680. if (!src_handle || !dst_handle) {
  2681. SDEDEV_ERR(rot_dev->dev, "null buffer handle\n");
  2682. ret = -EINVAL;
  2683. goto error_null_buffer;
  2684. }
  2685. vbinfo_out = &ctx->vbinfo_out[src_buf->index];
  2686. vbinfo_cap = &ctx->vbinfo_cap[dst_buf->index];
  2687. SDEDEV_DBG(rot_dev->dev,
  2688. "process buffer s:%d.%u src:(%u,%u,%u,%u) dst:(%u,%u,%u,%u) rot:%d flip:%d/%d sec:%d src_cr:%u/%u dst_cr:%u/%u\n",
  2689. ctx->session_id, vbinfo_cap->fence_ts,
  2690. ctx->crop_out.left, ctx->crop_out.top,
  2691. ctx->crop_out.width, ctx->crop_out.height,
  2692. ctx->crop_cap.left, ctx->crop_cap.top,
  2693. ctx->crop_cap.width, ctx->crop_cap.height,
  2694. ctx->rotate, ctx->hflip, ctx->vflip, ctx->secure,
  2695. vbinfo_out->comp_ratio.numer, vbinfo_out->comp_ratio.denom,
  2696. vbinfo_cap->comp_ratio.numer, vbinfo_cap->comp_ratio.denom);
  2697. /* allocate slot for timestamp */
  2698. ts = stats->ts[stats->count++ % SDE_ROTATOR_NUM_EVENTS];
  2699. ts[SDE_ROTATOR_TS_SRCQB] = vbinfo_out->qbuf_ts;
  2700. ts[SDE_ROTATOR_TS_DSTQB] = vbinfo_cap->qbuf_ts;
  2701. vbinfo_out->dqbuf_ts = &ts[SDE_ROTATOR_TS_SRCDQB];
  2702. vbinfo_cap->dqbuf_ts = &ts[SDE_ROTATOR_TS_DSTDQB];
  2703. ts[SDE_ROTATOR_TS_FENCE] = ktime_get();
  2704. /* Set values to pass to trace */
  2705. rot_trace.wb_idx = ctx->fh.prio;
  2706. rot_trace.flags = (ctx->rotate << 0) | (ctx->hflip << 8) |
  2707. (ctx->hflip << 9) | (ctx->secure << 10);
  2708. rot_trace.input_format = ctx->format_out.fmt.pix.pixelformat;
  2709. rot_trace.input_width = ctx->format_out.fmt.pix.width;
  2710. rot_trace.input_height = ctx->format_out.fmt.pix.height;
  2711. rot_trace.src_x = ctx->crop_out.left;
  2712. rot_trace.src_y = ctx->crop_out.top;
  2713. rot_trace.src_w = ctx->crop_out.width;
  2714. rot_trace.src_h = ctx->crop_out.height;
  2715. rot_trace.output_format = ctx->format_cap.fmt.pix.pixelformat;
  2716. rot_trace.output_width = ctx->format_cap.fmt.pix.width;
  2717. rot_trace.output_height = ctx->format_cap.fmt.pix.height;
  2718. rot_trace.dst_x = ctx->crop_cap.left;
  2719. rot_trace.dst_y = ctx->crop_cap.top;
  2720. rot_trace.dst_w = ctx->crop_cap.width;
  2721. rot_trace.dst_h = ctx->crop_cap.height;
  2722. trace_rot_entry_fence(
  2723. ctx->session_id, vbinfo_cap->fence_ts, &rot_trace);
  2724. if (vbinfo_out->fence) {
  2725. sde_rot_mgr_unlock(rot_dev->mgr);
  2726. mutex_unlock(&rot_dev->lock);
  2727. SDEDEV_DBG(rot_dev->dev, "fence enter s:%d.%d fd:%d\n",
  2728. ctx->session_id, vbinfo_cap->fence_ts, vbinfo_out->fd);
  2729. ret = sde_rotator_wait_sync_fence(vbinfo_out->fence,
  2730. rot_dev->fence_timeout);
  2731. mutex_lock(&rot_dev->lock);
  2732. sde_rot_mgr_lock(rot_dev->mgr);
  2733. sde_rotator_put_sync_fence(vbinfo_out->fence);
  2734. vbinfo_out->fence = NULL;
  2735. if (ret) {
  2736. SDEDEV_ERR(rot_dev->dev,
  2737. "error waiting for fence s:%d.%d fd:%d r:%d\n",
  2738. ctx->session_id,
  2739. vbinfo_cap->fence_ts, vbinfo_out->fd, ret);
  2740. SDEROT_EVTLOG(ctx->session_id, vbinfo_cap->fence_ts,
  2741. vbinfo_out->fd, ret,
  2742. SDE_ROT_EVTLOG_ERROR);
  2743. goto error_fence_wait;
  2744. } else {
  2745. SDEDEV_DBG(rot_dev->dev, "fence exit s:%d.%d fd:%d\n",
  2746. ctx->session_id,
  2747. vbinfo_cap->fence_ts, vbinfo_out->fd);
  2748. }
  2749. }
  2750. /* fill in item work structure */
  2751. sde_rotator_get_item_from_ctx(ctx, &item);
  2752. item.flags |= SDE_ROTATION_EXT_DMA_BUF;
  2753. item.input.planes[0].fd = src_handle->fd;
  2754. item.input.planes[0].buffer = src_handle->buffer;
  2755. item.input.planes[0].offset = src_handle->addr;
  2756. item.input.planes[0].stride = ctx->format_out.fmt.pix.bytesperline;
  2757. item.input.plane_count = 1;
  2758. item.input.fence = NULL;
  2759. item.input.comp_ratio = vbinfo_out->comp_ratio;
  2760. item.output.planes[0].fd = dst_handle->fd;
  2761. item.output.planes[0].buffer = dst_handle->buffer;
  2762. item.output.planes[0].offset = dst_handle->addr;
  2763. item.output.planes[0].stride = ctx->format_cap.fmt.pix.bytesperline;
  2764. item.output.plane_count = 1;
  2765. item.output.fence = NULL;
  2766. item.output.comp_ratio = vbinfo_cap->comp_ratio;
  2767. item.sequence_id = vbinfo_cap->fence_ts;
  2768. item.ts = ts;
  2769. req = sde_rotator_req_init(rot_dev->mgr, ctx->private, &item, 1, 0);
  2770. if (IS_ERR_OR_NULL(req)) {
  2771. SDEDEV_ERR(rot_dev->dev, "fail allocate rotation request\n");
  2772. ret = -ENOMEM;
  2773. goto error_init_request;
  2774. }
  2775. req->retire_kw = ctx->work_queue.rot_kw;
  2776. req->retire_work = &request->retire_work;
  2777. ret = sde_rotator_handle_request_common(
  2778. rot_dev->mgr, ctx->private, req);
  2779. if (ret) {
  2780. SDEDEV_ERR(rot_dev->dev, "fail handle request\n");
  2781. goto error_handle_request;
  2782. }
  2783. sde_rotator_queue_request(rot_dev->mgr, ctx->private, req);
  2784. request->req = req;
  2785. request->sequence_id = item.sequence_id;
  2786. request->committed = true;
  2787. return 0;
  2788. error_handle_request:
  2789. devm_kfree(rot_dev->dev, req);
  2790. error_init_request:
  2791. error_fence_wait:
  2792. error_null_buffer:
  2793. request->req = NULL;
  2794. request->sequence_id = 0;
  2795. request->committed = false;
  2796. return ret;
  2797. }
  2798. /*
  2799. * sde_rotator_submit_handler - Invoked by m2m to submit job.
  2800. * @work: Pointer to work structure.
  2801. *
  2802. * This function is scheduled in work queue context.
  2803. */
  2804. static void sde_rotator_submit_handler(struct kthread_work *work)
  2805. {
  2806. struct sde_rotator_ctx *ctx;
  2807. struct sde_rotator_device *rot_dev;
  2808. struct vb2_v4l2_buffer *src_buf;
  2809. struct vb2_v4l2_buffer *dst_buf;
  2810. struct sde_rotator_request *request;
  2811. int ret;
  2812. request = container_of(work, struct sde_rotator_request, submit_work);
  2813. ctx = request->ctx;
  2814. if (!ctx || !ctx->rot_dev) {
  2815. SDEROT_ERR("null device\n");
  2816. return;
  2817. }
  2818. rot_dev = ctx->rot_dev;
  2819. SDEDEV_DBG(rot_dev->dev, "submit handler s:%d\n", ctx->session_id);
  2820. mutex_lock(&rot_dev->lock);
  2821. if (ctx->abort_pending) {
  2822. SDEDEV_DBG(rot_dev->dev, "abort command in submit s:%d\n",
  2823. ctx->session_id);
  2824. sde_rotator_update_retire_sequence(request);
  2825. sde_rotator_retire_request(request);
  2826. mutex_unlock(&rot_dev->lock);
  2827. return;
  2828. }
  2829. /* submit new request */
  2830. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  2831. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  2832. sde_rot_mgr_lock(rot_dev->mgr);
  2833. ret = sde_rotator_process_buffers(ctx, &src_buf->vb2_buf,
  2834. &dst_buf->vb2_buf, request);
  2835. sde_rot_mgr_unlock(rot_dev->mgr);
  2836. if (ret) {
  2837. SDEDEV_ERR(rot_dev->dev,
  2838. "fail process buffer in submit s:%d\n",
  2839. ctx->session_id);
  2840. /* advance to device run to clean up buffers */
  2841. v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
  2842. }
  2843. mutex_unlock(&rot_dev->lock);
  2844. }
  2845. /*
  2846. * sde_rotator_device_run - rotator m2m device run callback
  2847. * @priv: Pointer rotator context.
  2848. */
  2849. static void sde_rotator_device_run(void *priv)
  2850. {
  2851. struct sde_rotator_ctx *ctx = priv;
  2852. struct sde_rotator_device *rot_dev;
  2853. struct vb2_v4l2_buffer *src_buf;
  2854. struct vb2_v4l2_buffer *dst_buf;
  2855. struct sde_rotator_request *request;
  2856. int ret;
  2857. if (!ctx || !ctx->rot_dev) {
  2858. SDEROT_ERR("null context/device\n");
  2859. return;
  2860. }
  2861. rot_dev = ctx->rot_dev;
  2862. SDEDEV_DBG(rot_dev->dev, "device run s:%d\n", ctx->session_id);
  2863. if (rot_dev->early_submit) {
  2864. request = list_first_entry_or_null(&ctx->pending_list,
  2865. struct sde_rotator_request, list);
  2866. /* pending request mode, check for completion */
  2867. if (!request || IS_ERR_OR_NULL(request->req)) {
  2868. /* pending request fails or something wrong. */
  2869. SDEDEV_ERR(rot_dev->dev,
  2870. "pending request fail in device run s:%d\n",
  2871. ctx->session_id);
  2872. rot_dev->stats.fail_count++;
  2873. ATRACE_INT("fail_count", rot_dev->stats.fail_count);
  2874. goto error_process_buffers;
  2875. } else if (!atomic_read(&request->req->pending_count)) {
  2876. /* pending request completed. signal done. */
  2877. int failed_count =
  2878. atomic_read(&request->req->failed_count);
  2879. SDEDEV_DBG(rot_dev->dev,
  2880. "pending request completed in device run s:%d\n",
  2881. ctx->session_id);
  2882. /* disconnect request (will be freed by core layer) */
  2883. sde_rot_mgr_lock(rot_dev->mgr);
  2884. sde_rotator_req_finish(rot_dev->mgr, ctx->private,
  2885. request->req);
  2886. sde_rot_mgr_unlock(rot_dev->mgr);
  2887. if (failed_count) {
  2888. SDEDEV_ERR(rot_dev->dev,
  2889. "pending request failed in device run s:%d f:%d\n",
  2890. ctx->session_id,
  2891. failed_count);
  2892. rot_dev->stats.fail_count++;
  2893. ATRACE_INT("fail_count",
  2894. rot_dev->stats.fail_count);
  2895. goto error_process_buffers;
  2896. }
  2897. src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  2898. dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  2899. if (!src_buf || !dst_buf) {
  2900. SDEDEV_ERR(rot_dev->dev,
  2901. "null buffer in device run s:%d sb:%pK db:%pK\n",
  2902. ctx->session_id,
  2903. src_buf, dst_buf);
  2904. goto error_process_buffers;
  2905. }
  2906. sde_rotator_update_retire_sequence(request);
  2907. sde_rotator_retire_request(request);
  2908. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
  2909. v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
  2910. v4l2_m2m_job_finish(rot_dev->m2m_dev, ctx->fh.m2m_ctx);
  2911. } else {
  2912. /* pending request not complete. something wrong. */
  2913. SDEDEV_ERR(rot_dev->dev,
  2914. "Incomplete pending request in device run s:%d\n",
  2915. ctx->session_id);
  2916. /* disconnect request (will be freed by core layer) */
  2917. sde_rot_mgr_lock(rot_dev->mgr);
  2918. sde_rotator_req_finish(rot_dev->mgr, ctx->private,
  2919. request->req);
  2920. sde_rot_mgr_unlock(rot_dev->mgr);
  2921. goto error_process_buffers;
  2922. }
  2923. } else {
  2924. request = list_first_entry_or_null(&ctx->retired_list,
  2925. struct sde_rotator_request, list);
  2926. if (!request) {
  2927. SDEDEV_ERR(rot_dev->dev,
  2928. "no free request in device run s:%d\n",
  2929. ctx->session_id);
  2930. goto error_retired_list;
  2931. }
  2932. spin_lock(&ctx->list_lock);
  2933. list_del_init(&request->list);
  2934. list_add_tail(&request->list, &ctx->pending_list);
  2935. spin_unlock(&ctx->list_lock);
  2936. /* no pending request. submit buffer the usual way. */
  2937. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  2938. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  2939. if (!src_buf || !dst_buf) {
  2940. SDEDEV_ERR(rot_dev->dev,
  2941. "null buffer in device run s:%d sb:%pK db:%pK\n",
  2942. ctx->session_id,
  2943. src_buf, dst_buf);
  2944. goto error_empty_buffer;
  2945. }
  2946. sde_rot_mgr_lock(rot_dev->mgr);
  2947. ret = sde_rotator_process_buffers(ctx, &src_buf->vb2_buf,
  2948. &dst_buf->vb2_buf, request);
  2949. sde_rot_mgr_unlock(rot_dev->mgr);
  2950. if (ret) {
  2951. SDEDEV_ERR(rot_dev->dev,
  2952. "fail process buffer in device run s:%d\n",
  2953. ctx->session_id);
  2954. rot_dev->stats.fail_count++;
  2955. ATRACE_INT("fail_count", rot_dev->stats.fail_count);
  2956. goto error_process_buffers;
  2957. }
  2958. }
  2959. return;
  2960. error_process_buffers:
  2961. error_empty_buffer:
  2962. error_retired_list:
  2963. sde_rotator_update_retire_sequence(request);
  2964. sde_rotator_retire_request(request);
  2965. src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  2966. dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  2967. if (src_buf)
  2968. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
  2969. if (dst_buf)
  2970. v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
  2971. sde_rotator_resync_timeline(ctx->work_queue.timeline);
  2972. v4l2_m2m_job_finish(rot_dev->m2m_dev, ctx->fh.m2m_ctx);
  2973. }
  2974. /*
  2975. * sde_rotator_job_abort - rotator m2m job abort callback
  2976. * @priv: Pointer rotator context.
  2977. */
  2978. static void sde_rotator_job_abort(void *priv)
  2979. {
  2980. struct sde_rotator_ctx *ctx = priv;
  2981. struct sde_rotator_device *rot_dev;
  2982. if (!ctx || !ctx->rot_dev) {
  2983. SDEROT_ERR("null context/device\n");
  2984. return;
  2985. }
  2986. rot_dev = ctx->rot_dev;
  2987. SDEDEV_DBG(rot_dev->dev, "job abort s:%d\n", ctx->session_id);
  2988. v4l2_m2m_job_finish(rot_dev->m2m_dev, ctx->fh.m2m_ctx);
  2989. }
  2990. /*
  2991. * sde_rotator_job_ready - rotator m2m job ready callback
  2992. * @priv: Pointer rotator context.
  2993. */
  2994. static int sde_rotator_job_ready(void *priv)
  2995. {
  2996. struct sde_rotator_ctx *ctx = priv;
  2997. struct sde_rotator_device *rot_dev;
  2998. struct sde_rotator_request *request;
  2999. int ret = 0;
  3000. if (!ctx || !ctx->rot_dev) {
  3001. SDEROT_ERR("null context/device\n");
  3002. return 0;
  3003. }
  3004. rot_dev = ctx->rot_dev;
  3005. SDEDEV_DBG(rot_dev->dev, "job ready s:%d\n", ctx->session_id);
  3006. request = list_first_entry_or_null(&ctx->pending_list,
  3007. struct sde_rotator_request, list);
  3008. if (!rot_dev->early_submit) {
  3009. /* always ready in normal mode. */
  3010. ret = 1;
  3011. } else if (request && IS_ERR_OR_NULL(request->req)) {
  3012. /* if pending request fails, forward to device run state. */
  3013. SDEDEV_DBG(rot_dev->dev,
  3014. "pending request fail in job ready s:%d\n",
  3015. ctx->session_id);
  3016. ret = 1;
  3017. } else if (list_empty(&ctx->pending_list)) {
  3018. /* if no pending request, submit a new request. */
  3019. SDEDEV_DBG(rot_dev->dev,
  3020. "submit job s:%d sc:%d dc:%d p:%d\n",
  3021. ctx->session_id,
  3022. v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx),
  3023. v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx),
  3024. !list_empty(&ctx->pending_list));
  3025. request = list_first_entry_or_null(&ctx->retired_list,
  3026. struct sde_rotator_request, list);
  3027. if (!request) {
  3028. /* should not happen */
  3029. SDEDEV_ERR(rot_dev->dev,
  3030. "no free request in job ready s:%d\n",
  3031. ctx->session_id);
  3032. } else {
  3033. spin_lock(&ctx->list_lock);
  3034. list_del_init(&request->list);
  3035. list_add_tail(&request->list, &ctx->pending_list);
  3036. spin_unlock(&ctx->list_lock);
  3037. kthread_queue_work(ctx->work_queue.rot_kw,
  3038. &request->submit_work);
  3039. }
  3040. } else if (request && !atomic_read(&request->req->pending_count)) {
  3041. /* if pending request completed, forward to device run state */
  3042. SDEDEV_DBG(rot_dev->dev,
  3043. "pending request completed in job ready s:%d\n",
  3044. ctx->session_id);
  3045. ret = 1;
  3046. }
  3047. return ret;
  3048. }
  3049. /* V4l2 mem2mem handlers */
  3050. static struct v4l2_m2m_ops sde_rotator_m2m_ops = {
  3051. .device_run = sde_rotator_device_run,
  3052. .job_abort = sde_rotator_job_abort,
  3053. .job_ready = sde_rotator_job_ready,
  3054. };
  3055. /* Device tree match struct */
  3056. static const struct of_device_id sde_rotator_dt_match[] = {
  3057. {
  3058. .compatible = "qcom,sde_rotator",
  3059. .data = NULL,
  3060. },
  3061. {}
  3062. };
  3063. /*
  3064. * sde_rotator_get_drv_data - rotator device driver data.
  3065. * @dev: Pointer to device.
  3066. */
  3067. static const void *sde_rotator_get_drv_data(struct device *dev)
  3068. {
  3069. const struct of_device_id *match;
  3070. match = of_match_node(sde_rotator_dt_match, dev->of_node);
  3071. if (match)
  3072. return match->data;
  3073. return NULL;
  3074. }
  3075. /*
  3076. * sde_rotator_probe - rotator device probe method.
  3077. * @pdev: Pointer to rotator platform device.
  3078. */
  3079. static int sde_rotator_probe(struct platform_device *pdev)
  3080. {
  3081. struct sde_rotator_device *rot_dev;
  3082. struct video_device *vdev;
  3083. int ret, i;
  3084. char name[32];
  3085. SDEDEV_DBG(&pdev->dev, "SDE v4l2 rotator probed\n");
  3086. /* sde rotator device struct */
  3087. rot_dev = kzalloc(sizeof(struct sde_rotator_device), GFP_KERNEL);
  3088. if (!rot_dev)
  3089. return -ENOMEM;
  3090. mutex_init(&rot_dev->lock);
  3091. rot_dev->early_submit = SDE_ROTATOR_EARLY_SUBMIT;
  3092. rot_dev->fence_timeout = SDE_ROTATOR_FENCE_TIMEOUT;
  3093. rot_dev->streamoff_timeout = SDE_ROTATOR_STREAM_OFF_TIMEOUT;
  3094. rot_dev->min_rot_clk = 0;
  3095. rot_dev->min_bw = 0;
  3096. rot_dev->min_overhead_us = 0;
  3097. rot_dev->drvdata = sde_rotator_get_drv_data(&pdev->dev);
  3098. rot_dev->open_timeout = SDE_ROTATOR_CTX_OPEN_TIMEOUT;
  3099. init_waitqueue_head(&rot_dev->open_wq);
  3100. rot_dev->pdev = pdev;
  3101. rot_dev->dev = &pdev->dev;
  3102. platform_set_drvdata(pdev, rot_dev);
  3103. ret = sde_rotator_base_init(&rot_dev->mdata, pdev, rot_dev->drvdata);
  3104. if (ret < 0) {
  3105. SDEDEV_ERR(&pdev->dev, "fail init base data %d\n", ret);
  3106. goto error_rotator_base_init;
  3107. }
  3108. ret = sde_rotator_core_init(&rot_dev->mgr, pdev);
  3109. if (ret < 0) {
  3110. if (ret == -EPROBE_DEFER)
  3111. SDEDEV_INFO(&pdev->dev, "probe defer for core init\n");
  3112. else
  3113. SDEDEV_ERR(&pdev->dev, "fail init core %d\n", ret);
  3114. goto error_rotator_core_init;
  3115. }
  3116. /* mem2mem device */
  3117. rot_dev->m2m_dev = v4l2_m2m_init(&sde_rotator_m2m_ops);
  3118. if (IS_ERR(rot_dev->m2m_dev)) {
  3119. ret = PTR_ERR(rot_dev->m2m_dev);
  3120. SDEDEV_ERR(&pdev->dev, "fail init mem2mem device %d\n", ret);
  3121. goto error_m2m_init;
  3122. }
  3123. /* v4l2 device */
  3124. ret = v4l2_device_register(&pdev->dev, &rot_dev->v4l2_dev);
  3125. if (ret < 0) {
  3126. SDEDEV_ERR(&pdev->dev, "fail register v4l2 device %d\n", ret);
  3127. goto error_v4l2_register;
  3128. }
  3129. vdev = video_device_alloc();
  3130. if (!vdev) {
  3131. SDEDEV_ERR(&pdev->dev, "fail allocate video device\n");
  3132. goto error_alloc_video_device;
  3133. }
  3134. vdev->fops = &sde_rotator_fops;
  3135. vdev->ioctl_ops = &sde_rotator_ioctl_ops;
  3136. vdev->lock = &rot_dev->lock;
  3137. vdev->minor = -1;
  3138. vdev->release = video_device_release;
  3139. vdev->v4l2_dev = &rot_dev->v4l2_dev;
  3140. vdev->vfl_dir = VFL_DIR_M2M;
  3141. vdev->vfl_type = VFL_TYPE_GRABBER;
  3142. strlcpy(vdev->name, SDE_ROTATOR_DRV_NAME, sizeof(vdev->name));
  3143. ret = video_register_device(vdev, VFL_TYPE_GRABBER,
  3144. SDE_ROTATOR_BASE_DEVICE_NUMBER);
  3145. if (ret < 0) {
  3146. SDEDEV_ERR(&pdev->dev, "fail register video device %d\n",
  3147. ret);
  3148. goto error_video_register;
  3149. }
  3150. rot_dev->vdev = vdev;
  3151. video_set_drvdata(rot_dev->vdev, rot_dev);
  3152. rot_dev->debugfs_root = sde_rotator_create_debugfs(rot_dev);
  3153. for (i = 0; i < MAX_ROT_OPEN_SESSION; i++) {
  3154. snprintf(name, sizeof(name), "rot_fenceq_%d_%d",
  3155. rot_dev->dev->id, i);
  3156. kthread_init_worker(&rot_dev->rot_kw[i]);
  3157. rot_dev->rot_thread[i] = kthread_run(kthread_worker_fn,
  3158. &rot_dev->rot_kw[i], name);
  3159. if (IS_ERR(rot_dev->rot_thread[i])) {
  3160. SDEDEV_ERR(rot_dev->dev,
  3161. "fail allocate kthread i:%d\n", i);
  3162. ret = -EPERM;
  3163. goto error_kthread_create;
  3164. }
  3165. rot_dev->kthread_free[i] = true;
  3166. }
  3167. SDEDEV_INFO(&pdev->dev, "SDE v4l2 rotator probe success\n");
  3168. return 0;
  3169. error_kthread_create:
  3170. for (i--; i >= 0; i--)
  3171. kthread_stop(rot_dev->rot_thread[i]);
  3172. sde_rotator_destroy_debugfs(rot_dev->debugfs_root);
  3173. video_unregister_device(rot_dev->vdev);
  3174. error_video_register:
  3175. video_device_release(vdev);
  3176. error_alloc_video_device:
  3177. v4l2_device_unregister(&rot_dev->v4l2_dev);
  3178. error_v4l2_register:
  3179. v4l2_m2m_release(rot_dev->m2m_dev);
  3180. error_m2m_init:
  3181. sde_rotator_core_destroy(rot_dev->mgr);
  3182. error_rotator_core_init:
  3183. sde_rotator_base_destroy(rot_dev->mdata);
  3184. error_rotator_base_init:
  3185. kfree(rot_dev);
  3186. return ret;
  3187. }
  3188. /*
  3189. * sde_rotator_remove - rotator device remove method.
  3190. * @pdev: Pointer rotator platform device.
  3191. */
  3192. static int sde_rotator_remove(struct platform_device *pdev)
  3193. {
  3194. struct sde_rotator_device *rot_dev;
  3195. int i;
  3196. rot_dev = platform_get_drvdata(pdev);
  3197. if (rot_dev == NULL) {
  3198. SDEDEV_ERR(&pdev->dev, "fail get rotator drvdata\n");
  3199. return 0;
  3200. }
  3201. sde_rotator_pm_qos_remove(rot_dev->mdata);
  3202. for (i = MAX_ROT_OPEN_SESSION - 1; i >= 0; i--)
  3203. kthread_stop(rot_dev->rot_thread[i]);
  3204. sde_rotator_destroy_debugfs(rot_dev->debugfs_root);
  3205. video_unregister_device(rot_dev->vdev);
  3206. video_device_release(rot_dev->vdev);
  3207. v4l2_device_unregister(&rot_dev->v4l2_dev);
  3208. v4l2_m2m_release(rot_dev->m2m_dev);
  3209. sde_rotator_core_destroy(rot_dev->mgr);
  3210. sde_rotator_base_destroy(rot_dev->mdata);
  3211. kfree(rot_dev);
  3212. return 0;
  3213. }
  3214. static const struct dev_pm_ops sde_rotator_pm_ops = {
  3215. SET_SYSTEM_SLEEP_PM_OPS(sde_rotator_pm_suspend, sde_rotator_pm_resume)
  3216. SET_RUNTIME_PM_OPS(sde_rotator_runtime_suspend,
  3217. sde_rotator_runtime_resume,
  3218. sde_rotator_runtime_idle)
  3219. };
  3220. /* SDE Rotator platform driver definition */
  3221. static struct platform_driver rotator_driver = {
  3222. .probe = sde_rotator_probe,
  3223. .remove = sde_rotator_remove,
  3224. .suspend = sde_rotator_suspend,
  3225. .resume = sde_rotator_resume,
  3226. .driver = {
  3227. .name = SDE_ROTATOR_DRV_NAME,
  3228. .of_match_table = sde_rotator_dt_match,
  3229. .pm = &sde_rotator_pm_ops,
  3230. },
  3231. };
  3232. static int __init sde_rotator_init_module(void)
  3233. {
  3234. return platform_driver_register(&rotator_driver);
  3235. }
  3236. static void __exit sde_rotator_exit_module(void)
  3237. {
  3238. platform_driver_unregister(&rotator_driver);
  3239. }
  3240. module_init(sde_rotator_init_module);
  3241. module_exit(sde_rotator_exit_module);
  3242. MODULE_DESCRIPTION("MSM SDE ROTATOR driver");