adv7842.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * adv7842 - Analog Devices ADV7842 video decoder driver
  4. *
  5. * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  6. */
  7. /*
  8. * References (c = chapter, p = page):
  9. * REF_01 - Analog devices, ADV7842,
  10. * Register Settings Recommendations, Rev. 1.9, April 2011
  11. * REF_02 - Analog devices, Software User Guide, UG-206,
  12. * ADV7842 I2C Register Maps, Rev. 0, November 2010
  13. * REF_03 - Analog devices, Hardware User Guide, UG-214,
  14. * ADV7842 Fast Switching 2:1 HDMI 1.4 Receiver with 3D-Comb
  15. * Decoder and Digitizer , Rev. 0, January 2011
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/i2c.h>
  21. #include <linux/delay.h>
  22. #include <linux/videodev2.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/v4l2-dv-timings.h>
  25. #include <linux/hdmi.h>
  26. #include <media/cec.h>
  27. #include <media/v4l2-device.h>
  28. #include <media/v4l2-event.h>
  29. #include <media/v4l2-ctrls.h>
  30. #include <media/v4l2-dv-timings.h>
  31. #include <media/i2c/adv7842.h>
  32. static int debug;
  33. module_param(debug, int, 0644);
  34. MODULE_PARM_DESC(debug, "debug level (0-2)");
  35. MODULE_DESCRIPTION("Analog Devices ADV7842 video decoder driver");
  36. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
  37. MODULE_AUTHOR("Martin Bugge <marbugge@cisco.com>");
  38. MODULE_LICENSE("GPL");
  39. /* ADV7842 system clock frequency */
  40. #define ADV7842_fsc (28636360)
  41. #define ADV7842_RGB_OUT (1 << 1)
  42. #define ADV7842_OP_FORMAT_SEL_8BIT (0 << 0)
  43. #define ADV7842_OP_FORMAT_SEL_10BIT (1 << 0)
  44. #define ADV7842_OP_FORMAT_SEL_12BIT (2 << 0)
  45. #define ADV7842_OP_MODE_SEL_SDR_422 (0 << 5)
  46. #define ADV7842_OP_MODE_SEL_DDR_422 (1 << 5)
  47. #define ADV7842_OP_MODE_SEL_SDR_444 (2 << 5)
  48. #define ADV7842_OP_MODE_SEL_DDR_444 (3 << 5)
  49. #define ADV7842_OP_MODE_SEL_SDR_422_2X (4 << 5)
  50. #define ADV7842_OP_MODE_SEL_ADI_CM (5 << 5)
  51. #define ADV7842_OP_CH_SEL_GBR (0 << 5)
  52. #define ADV7842_OP_CH_SEL_GRB (1 << 5)
  53. #define ADV7842_OP_CH_SEL_BGR (2 << 5)
  54. #define ADV7842_OP_CH_SEL_RGB (3 << 5)
  55. #define ADV7842_OP_CH_SEL_BRG (4 << 5)
  56. #define ADV7842_OP_CH_SEL_RBG (5 << 5)
  57. #define ADV7842_OP_SWAP_CB_CR (1 << 0)
  58. #define ADV7842_MAX_ADDRS (3)
  59. /*
  60. **********************************************************************
  61. *
  62. * Arrays with configuration parameters for the ADV7842
  63. *
  64. **********************************************************************
  65. */
  66. struct adv7842_format_info {
  67. u32 code;
  68. u8 op_ch_sel;
  69. bool rgb_out;
  70. bool swap_cb_cr;
  71. u8 op_format_sel;
  72. };
  73. struct adv7842_state {
  74. struct adv7842_platform_data pdata;
  75. struct v4l2_subdev sd;
  76. struct media_pad pad;
  77. struct v4l2_ctrl_handler hdl;
  78. enum adv7842_mode mode;
  79. struct v4l2_dv_timings timings;
  80. enum adv7842_vid_std_select vid_std_select;
  81. const struct adv7842_format_info *format;
  82. v4l2_std_id norm;
  83. struct {
  84. u8 edid[256];
  85. u32 present;
  86. } hdmi_edid;
  87. struct {
  88. u8 edid[256];
  89. u32 present;
  90. } vga_edid;
  91. struct v4l2_fract aspect_ratio;
  92. u32 rgb_quantization_range;
  93. bool is_cea_format;
  94. struct delayed_work delayed_work_enable_hotplug;
  95. bool restart_stdi_once;
  96. bool hdmi_port_a;
  97. /* i2c clients */
  98. struct i2c_client *i2c_sdp_io;
  99. struct i2c_client *i2c_sdp;
  100. struct i2c_client *i2c_cp;
  101. struct i2c_client *i2c_vdp;
  102. struct i2c_client *i2c_afe;
  103. struct i2c_client *i2c_hdmi;
  104. struct i2c_client *i2c_repeater;
  105. struct i2c_client *i2c_edid;
  106. struct i2c_client *i2c_infoframe;
  107. struct i2c_client *i2c_cec;
  108. struct i2c_client *i2c_avlink;
  109. /* controls */
  110. struct v4l2_ctrl *detect_tx_5v_ctrl;
  111. struct v4l2_ctrl *analog_sampling_phase_ctrl;
  112. struct v4l2_ctrl *free_run_color_ctrl_manual;
  113. struct v4l2_ctrl *free_run_color_ctrl;
  114. struct v4l2_ctrl *rgb_quantization_range_ctrl;
  115. struct cec_adapter *cec_adap;
  116. u8 cec_addr[ADV7842_MAX_ADDRS];
  117. u8 cec_valid_addrs;
  118. bool cec_enabled_adap;
  119. };
  120. /* Unsupported timings. This device cannot support 720p30. */
  121. static const struct v4l2_dv_timings adv7842_timings_exceptions[] = {
  122. V4L2_DV_BT_CEA_1280X720P30,
  123. { }
  124. };
  125. static bool adv7842_check_dv_timings(const struct v4l2_dv_timings *t, void *hdl)
  126. {
  127. int i;
  128. for (i = 0; adv7842_timings_exceptions[i].bt.width; i++)
  129. if (v4l2_match_dv_timings(t, adv7842_timings_exceptions + i, 0, false))
  130. return false;
  131. return true;
  132. }
  133. struct adv7842_video_standards {
  134. struct v4l2_dv_timings timings;
  135. u8 vid_std;
  136. u8 v_freq;
  137. };
  138. /* sorted by number of lines */
  139. static const struct adv7842_video_standards adv7842_prim_mode_comp[] = {
  140. /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
  141. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  142. { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
  143. { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
  144. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  145. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  146. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  147. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  148. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  149. /* TODO add 1920x1080P60_RB (CVT timing) */
  150. { },
  151. };
  152. /* sorted by number of lines */
  153. static const struct adv7842_video_standards adv7842_prim_mode_gr[] = {
  154. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  155. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  156. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  157. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  158. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  159. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  160. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  161. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  162. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  163. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  164. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  165. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  166. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  167. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  168. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  169. { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
  170. { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
  171. { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
  172. { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
  173. { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
  174. /* TODO add 1600X1200P60_RB (not a DMT timing) */
  175. { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
  176. { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
  177. { },
  178. };
  179. /* sorted by number of lines */
  180. static const struct adv7842_video_standards adv7842_prim_mode_hdmi_comp[] = {
  181. { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
  182. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  183. { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
  184. { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
  185. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  186. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  187. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  188. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  189. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  190. { },
  191. };
  192. /* sorted by number of lines */
  193. static const struct adv7842_video_standards adv7842_prim_mode_hdmi_gr[] = {
  194. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  195. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  196. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  197. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  198. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  199. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  200. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  201. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  202. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  203. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  204. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  205. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  206. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  207. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  208. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  209. { },
  210. };
  211. static const struct v4l2_event adv7842_ev_fmt = {
  212. .type = V4L2_EVENT_SOURCE_CHANGE,
  213. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  214. };
  215. /* ----------------------------------------------------------------------- */
  216. static inline struct adv7842_state *to_state(struct v4l2_subdev *sd)
  217. {
  218. return container_of(sd, struct adv7842_state, sd);
  219. }
  220. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  221. {
  222. return &container_of(ctrl->handler, struct adv7842_state, hdl)->sd;
  223. }
  224. static inline unsigned hblanking(const struct v4l2_bt_timings *t)
  225. {
  226. return V4L2_DV_BT_BLANKING_WIDTH(t);
  227. }
  228. static inline unsigned htotal(const struct v4l2_bt_timings *t)
  229. {
  230. return V4L2_DV_BT_FRAME_WIDTH(t);
  231. }
  232. static inline unsigned vblanking(const struct v4l2_bt_timings *t)
  233. {
  234. return V4L2_DV_BT_BLANKING_HEIGHT(t);
  235. }
  236. static inline unsigned vtotal(const struct v4l2_bt_timings *t)
  237. {
  238. return V4L2_DV_BT_FRAME_HEIGHT(t);
  239. }
  240. /* ----------------------------------------------------------------------- */
  241. static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
  242. u8 command, bool check)
  243. {
  244. union i2c_smbus_data data;
  245. if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  246. I2C_SMBUS_READ, command,
  247. I2C_SMBUS_BYTE_DATA, &data))
  248. return data.byte;
  249. if (check)
  250. v4l_err(client, "error reading %02x, %02x\n",
  251. client->addr, command);
  252. return -EIO;
  253. }
  254. static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
  255. {
  256. int i;
  257. for (i = 0; i < 3; i++) {
  258. int ret = adv_smbus_read_byte_data_check(client, command, true);
  259. if (ret >= 0) {
  260. if (i)
  261. v4l_err(client, "read ok after %d retries\n", i);
  262. return ret;
  263. }
  264. }
  265. v4l_err(client, "read failed\n");
  266. return -EIO;
  267. }
  268. static s32 adv_smbus_write_byte_data(struct i2c_client *client,
  269. u8 command, u8 value)
  270. {
  271. union i2c_smbus_data data;
  272. int err;
  273. int i;
  274. data.byte = value;
  275. for (i = 0; i < 3; i++) {
  276. err = i2c_smbus_xfer(client->adapter, client->addr,
  277. client->flags,
  278. I2C_SMBUS_WRITE, command,
  279. I2C_SMBUS_BYTE_DATA, &data);
  280. if (!err)
  281. break;
  282. }
  283. if (err < 0)
  284. v4l_err(client, "error writing %02x, %02x, %02x\n",
  285. client->addr, command, value);
  286. return err;
  287. }
  288. static void adv_smbus_write_byte_no_check(struct i2c_client *client,
  289. u8 command, u8 value)
  290. {
  291. union i2c_smbus_data data;
  292. data.byte = value;
  293. i2c_smbus_xfer(client->adapter, client->addr,
  294. client->flags,
  295. I2C_SMBUS_WRITE, command,
  296. I2C_SMBUS_BYTE_DATA, &data);
  297. }
  298. static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
  299. u8 command, unsigned length, const u8 *values)
  300. {
  301. union i2c_smbus_data data;
  302. if (length > I2C_SMBUS_BLOCK_MAX)
  303. length = I2C_SMBUS_BLOCK_MAX;
  304. data.block[0] = length;
  305. memcpy(data.block + 1, values, length);
  306. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  307. I2C_SMBUS_WRITE, command,
  308. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  309. }
  310. /* ----------------------------------------------------------------------- */
  311. static inline int io_read(struct v4l2_subdev *sd, u8 reg)
  312. {
  313. struct i2c_client *client = v4l2_get_subdevdata(sd);
  314. return adv_smbus_read_byte_data(client, reg);
  315. }
  316. static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  317. {
  318. struct i2c_client *client = v4l2_get_subdevdata(sd);
  319. return adv_smbus_write_byte_data(client, reg, val);
  320. }
  321. static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  322. {
  323. return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
  324. }
  325. static inline int io_write_clr_set(struct v4l2_subdev *sd,
  326. u8 reg, u8 mask, u8 val)
  327. {
  328. return io_write(sd, reg, (io_read(sd, reg) & ~mask) | val);
  329. }
  330. static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
  331. {
  332. struct adv7842_state *state = to_state(sd);
  333. return adv_smbus_read_byte_data(state->i2c_avlink, reg);
  334. }
  335. static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  336. {
  337. struct adv7842_state *state = to_state(sd);
  338. return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
  339. }
  340. static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
  341. {
  342. struct adv7842_state *state = to_state(sd);
  343. return adv_smbus_read_byte_data(state->i2c_cec, reg);
  344. }
  345. static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  346. {
  347. struct adv7842_state *state = to_state(sd);
  348. return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
  349. }
  350. static inline int cec_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  351. {
  352. return cec_write(sd, reg, (cec_read(sd, reg) & ~mask) | val);
  353. }
  354. static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
  355. {
  356. struct adv7842_state *state = to_state(sd);
  357. return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
  358. }
  359. static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  360. {
  361. struct adv7842_state *state = to_state(sd);
  362. return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
  363. }
  364. static inline int sdp_io_read(struct v4l2_subdev *sd, u8 reg)
  365. {
  366. struct adv7842_state *state = to_state(sd);
  367. return adv_smbus_read_byte_data(state->i2c_sdp_io, reg);
  368. }
  369. static inline int sdp_io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  370. {
  371. struct adv7842_state *state = to_state(sd);
  372. return adv_smbus_write_byte_data(state->i2c_sdp_io, reg, val);
  373. }
  374. static inline int sdp_io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  375. {
  376. return sdp_io_write(sd, reg, (sdp_io_read(sd, reg) & mask) | val);
  377. }
  378. static inline int sdp_read(struct v4l2_subdev *sd, u8 reg)
  379. {
  380. struct adv7842_state *state = to_state(sd);
  381. return adv_smbus_read_byte_data(state->i2c_sdp, reg);
  382. }
  383. static inline int sdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  384. {
  385. struct adv7842_state *state = to_state(sd);
  386. return adv_smbus_write_byte_data(state->i2c_sdp, reg, val);
  387. }
  388. static inline int sdp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  389. {
  390. return sdp_write(sd, reg, (sdp_read(sd, reg) & mask) | val);
  391. }
  392. static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
  393. {
  394. struct adv7842_state *state = to_state(sd);
  395. return adv_smbus_read_byte_data(state->i2c_afe, reg);
  396. }
  397. static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  398. {
  399. struct adv7842_state *state = to_state(sd);
  400. return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
  401. }
  402. static inline int afe_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  403. {
  404. return afe_write(sd, reg, (afe_read(sd, reg) & mask) | val);
  405. }
  406. static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
  407. {
  408. struct adv7842_state *state = to_state(sd);
  409. return adv_smbus_read_byte_data(state->i2c_repeater, reg);
  410. }
  411. static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  412. {
  413. struct adv7842_state *state = to_state(sd);
  414. return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
  415. }
  416. static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  417. {
  418. return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
  419. }
  420. static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
  421. {
  422. struct adv7842_state *state = to_state(sd);
  423. return adv_smbus_read_byte_data(state->i2c_edid, reg);
  424. }
  425. static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  426. {
  427. struct adv7842_state *state = to_state(sd);
  428. return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
  429. }
  430. static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
  431. {
  432. struct adv7842_state *state = to_state(sd);
  433. return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
  434. }
  435. static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  436. {
  437. struct adv7842_state *state = to_state(sd);
  438. return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
  439. }
  440. static inline int hdmi_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  441. {
  442. return hdmi_write(sd, reg, (hdmi_read(sd, reg) & mask) | val);
  443. }
  444. static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
  445. {
  446. struct adv7842_state *state = to_state(sd);
  447. return adv_smbus_read_byte_data(state->i2c_cp, reg);
  448. }
  449. static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  450. {
  451. struct adv7842_state *state = to_state(sd);
  452. return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
  453. }
  454. static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  455. {
  456. return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
  457. }
  458. static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
  459. {
  460. struct adv7842_state *state = to_state(sd);
  461. return adv_smbus_read_byte_data(state->i2c_vdp, reg);
  462. }
  463. static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  464. {
  465. struct adv7842_state *state = to_state(sd);
  466. return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
  467. }
  468. static void main_reset(struct v4l2_subdev *sd)
  469. {
  470. struct i2c_client *client = v4l2_get_subdevdata(sd);
  471. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  472. adv_smbus_write_byte_no_check(client, 0xff, 0x80);
  473. mdelay(5);
  474. }
  475. /* -----------------------------------------------------------------------------
  476. * Format helpers
  477. */
  478. static const struct adv7842_format_info adv7842_formats[] = {
  479. { MEDIA_BUS_FMT_RGB888_1X24, ADV7842_OP_CH_SEL_RGB, true, false,
  480. ADV7842_OP_MODE_SEL_SDR_444 | ADV7842_OP_FORMAT_SEL_8BIT },
  481. { MEDIA_BUS_FMT_YUYV8_2X8, ADV7842_OP_CH_SEL_RGB, false, false,
  482. ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_8BIT },
  483. { MEDIA_BUS_FMT_YVYU8_2X8, ADV7842_OP_CH_SEL_RGB, false, true,
  484. ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_8BIT },
  485. { MEDIA_BUS_FMT_YUYV10_2X10, ADV7842_OP_CH_SEL_RGB, false, false,
  486. ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_10BIT },
  487. { MEDIA_BUS_FMT_YVYU10_2X10, ADV7842_OP_CH_SEL_RGB, false, true,
  488. ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_10BIT },
  489. { MEDIA_BUS_FMT_YUYV12_2X12, ADV7842_OP_CH_SEL_RGB, false, false,
  490. ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_12BIT },
  491. { MEDIA_BUS_FMT_YVYU12_2X12, ADV7842_OP_CH_SEL_RGB, false, true,
  492. ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_12BIT },
  493. { MEDIA_BUS_FMT_UYVY8_1X16, ADV7842_OP_CH_SEL_RBG, false, false,
  494. ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_8BIT },
  495. { MEDIA_BUS_FMT_VYUY8_1X16, ADV7842_OP_CH_SEL_RBG, false, true,
  496. ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_8BIT },
  497. { MEDIA_BUS_FMT_YUYV8_1X16, ADV7842_OP_CH_SEL_RGB, false, false,
  498. ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_8BIT },
  499. { MEDIA_BUS_FMT_YVYU8_1X16, ADV7842_OP_CH_SEL_RGB, false, true,
  500. ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_8BIT },
  501. { MEDIA_BUS_FMT_UYVY10_1X20, ADV7842_OP_CH_SEL_RBG, false, false,
  502. ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_10BIT },
  503. { MEDIA_BUS_FMT_VYUY10_1X20, ADV7842_OP_CH_SEL_RBG, false, true,
  504. ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_10BIT },
  505. { MEDIA_BUS_FMT_YUYV10_1X20, ADV7842_OP_CH_SEL_RGB, false, false,
  506. ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_10BIT },
  507. { MEDIA_BUS_FMT_YVYU10_1X20, ADV7842_OP_CH_SEL_RGB, false, true,
  508. ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_10BIT },
  509. { MEDIA_BUS_FMT_UYVY12_1X24, ADV7842_OP_CH_SEL_RBG, false, false,
  510. ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_12BIT },
  511. { MEDIA_BUS_FMT_VYUY12_1X24, ADV7842_OP_CH_SEL_RBG, false, true,
  512. ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_12BIT },
  513. { MEDIA_BUS_FMT_YUYV12_1X24, ADV7842_OP_CH_SEL_RGB, false, false,
  514. ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_12BIT },
  515. { MEDIA_BUS_FMT_YVYU12_1X24, ADV7842_OP_CH_SEL_RGB, false, true,
  516. ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_12BIT },
  517. };
  518. static const struct adv7842_format_info *
  519. adv7842_format_info(struct adv7842_state *state, u32 code)
  520. {
  521. unsigned int i;
  522. for (i = 0; i < ARRAY_SIZE(adv7842_formats); ++i) {
  523. if (adv7842_formats[i].code == code)
  524. return &adv7842_formats[i];
  525. }
  526. return NULL;
  527. }
  528. /* ----------------------------------------------------------------------- */
  529. static inline bool is_analog_input(struct v4l2_subdev *sd)
  530. {
  531. struct adv7842_state *state = to_state(sd);
  532. return ((state->mode == ADV7842_MODE_RGB) ||
  533. (state->mode == ADV7842_MODE_COMP));
  534. }
  535. static inline bool is_digital_input(struct v4l2_subdev *sd)
  536. {
  537. struct adv7842_state *state = to_state(sd);
  538. return state->mode == ADV7842_MODE_HDMI;
  539. }
  540. static const struct v4l2_dv_timings_cap adv7842_timings_cap_analog = {
  541. .type = V4L2_DV_BT_656_1120,
  542. /* keep this initialization for compatibility with GCC < 4.4.6 */
  543. .reserved = { 0 },
  544. V4L2_INIT_BT_TIMINGS(640, 1920, 350, 1200, 25000000, 170000000,
  545. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  546. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
  547. V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
  548. V4L2_DV_BT_CAP_CUSTOM)
  549. };
  550. static const struct v4l2_dv_timings_cap adv7842_timings_cap_digital = {
  551. .type = V4L2_DV_BT_656_1120,
  552. /* keep this initialization for compatibility with GCC < 4.4.6 */
  553. .reserved = { 0 },
  554. V4L2_INIT_BT_TIMINGS(640, 1920, 350, 1200, 25000000, 225000000,
  555. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  556. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
  557. V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
  558. V4L2_DV_BT_CAP_CUSTOM)
  559. };
  560. static inline const struct v4l2_dv_timings_cap *
  561. adv7842_get_dv_timings_cap(struct v4l2_subdev *sd)
  562. {
  563. return is_digital_input(sd) ? &adv7842_timings_cap_digital :
  564. &adv7842_timings_cap_analog;
  565. }
  566. /* ----------------------------------------------------------------------- */
  567. static u16 adv7842_read_cable_det(struct v4l2_subdev *sd)
  568. {
  569. u8 reg = io_read(sd, 0x6f);
  570. u16 val = 0;
  571. if (reg & 0x02)
  572. val |= 1; /* port A */
  573. if (reg & 0x01)
  574. val |= 2; /* port B */
  575. return val;
  576. }
  577. static void adv7842_delayed_work_enable_hotplug(struct work_struct *work)
  578. {
  579. struct delayed_work *dwork = to_delayed_work(work);
  580. struct adv7842_state *state = container_of(dwork,
  581. struct adv7842_state, delayed_work_enable_hotplug);
  582. struct v4l2_subdev *sd = &state->sd;
  583. int present = state->hdmi_edid.present;
  584. u8 mask = 0;
  585. v4l2_dbg(2, debug, sd, "%s: enable hotplug on ports: 0x%x\n",
  586. __func__, present);
  587. if (present & (0x04 << ADV7842_EDID_PORT_A))
  588. mask |= 0x20;
  589. if (present & (0x04 << ADV7842_EDID_PORT_B))
  590. mask |= 0x10;
  591. io_write_and_or(sd, 0x20, 0xcf, mask);
  592. }
  593. static int edid_write_vga_segment(struct v4l2_subdev *sd)
  594. {
  595. struct i2c_client *client = v4l2_get_subdevdata(sd);
  596. struct adv7842_state *state = to_state(sd);
  597. const u8 *val = state->vga_edid.edid;
  598. int err = 0;
  599. int i;
  600. v4l2_dbg(2, debug, sd, "%s: write EDID on VGA port\n", __func__);
  601. /* HPA disable on port A and B */
  602. io_write_and_or(sd, 0x20, 0xcf, 0x00);
  603. /* Disable I2C access to internal EDID ram from VGA DDC port */
  604. rep_write_and_or(sd, 0x7f, 0x7f, 0x00);
  605. /* edid segment pointer '1' for VGA port */
  606. rep_write_and_or(sd, 0x77, 0xef, 0x10);
  607. for (i = 0; !err && i < 256; i += I2C_SMBUS_BLOCK_MAX)
  608. err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
  609. I2C_SMBUS_BLOCK_MAX, val + i);
  610. if (err)
  611. return err;
  612. /* Calculates the checksums and enables I2C access
  613. * to internal EDID ram from VGA DDC port.
  614. */
  615. rep_write_and_or(sd, 0x7f, 0x7f, 0x80);
  616. for (i = 0; i < 1000; i++) {
  617. if (rep_read(sd, 0x79) & 0x20)
  618. break;
  619. mdelay(1);
  620. }
  621. if (i == 1000) {
  622. v4l_err(client, "error enabling edid on VGA port\n");
  623. return -EIO;
  624. }
  625. /* enable hotplug after 200 ms */
  626. schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 5);
  627. return 0;
  628. }
  629. static int edid_write_hdmi_segment(struct v4l2_subdev *sd, u8 port)
  630. {
  631. struct i2c_client *client = v4l2_get_subdevdata(sd);
  632. struct adv7842_state *state = to_state(sd);
  633. const u8 *edid = state->hdmi_edid.edid;
  634. int spa_loc;
  635. u16 pa;
  636. int err = 0;
  637. int i;
  638. v4l2_dbg(2, debug, sd, "%s: write EDID on port %c\n",
  639. __func__, (port == ADV7842_EDID_PORT_A) ? 'A' : 'B');
  640. /* HPA disable on port A and B */
  641. io_write_and_or(sd, 0x20, 0xcf, 0x00);
  642. /* Disable I2C access to internal EDID ram from HDMI DDC ports */
  643. rep_write_and_or(sd, 0x77, 0xf3, 0x00);
  644. if (!state->hdmi_edid.present) {
  645. cec_phys_addr_invalidate(state->cec_adap);
  646. return 0;
  647. }
  648. pa = v4l2_get_edid_phys_addr(edid, 256, &spa_loc);
  649. err = v4l2_phys_addr_validate(pa, &pa, NULL);
  650. if (err)
  651. return err;
  652. /*
  653. * Return an error if no location of the source physical address
  654. * was found.
  655. */
  656. if (spa_loc == 0)
  657. return -EINVAL;
  658. /* edid segment pointer '0' for HDMI ports */
  659. rep_write_and_or(sd, 0x77, 0xef, 0x00);
  660. for (i = 0; !err && i < 256; i += I2C_SMBUS_BLOCK_MAX)
  661. err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
  662. I2C_SMBUS_BLOCK_MAX, edid + i);
  663. if (err)
  664. return err;
  665. if (port == ADV7842_EDID_PORT_A) {
  666. rep_write(sd, 0x72, edid[spa_loc]);
  667. rep_write(sd, 0x73, edid[spa_loc + 1]);
  668. } else {
  669. rep_write(sd, 0x74, edid[spa_loc]);
  670. rep_write(sd, 0x75, edid[spa_loc + 1]);
  671. }
  672. rep_write(sd, 0x76, spa_loc & 0xff);
  673. rep_write_and_or(sd, 0x77, 0xbf, (spa_loc >> 2) & 0x40);
  674. /* Calculates the checksums and enables I2C access to internal
  675. * EDID ram from HDMI DDC ports
  676. */
  677. rep_write_and_or(sd, 0x77, 0xf3, state->hdmi_edid.present);
  678. for (i = 0; i < 1000; i++) {
  679. if (rep_read(sd, 0x7d) & state->hdmi_edid.present)
  680. break;
  681. mdelay(1);
  682. }
  683. if (i == 1000) {
  684. v4l_err(client, "error enabling edid on port %c\n",
  685. (port == ADV7842_EDID_PORT_A) ? 'A' : 'B');
  686. return -EIO;
  687. }
  688. cec_s_phys_addr(state->cec_adap, pa, false);
  689. /* enable hotplug after 200 ms */
  690. schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 5);
  691. return 0;
  692. }
  693. /* ----------------------------------------------------------------------- */
  694. #ifdef CONFIG_VIDEO_ADV_DEBUG
  695. static void adv7842_inv_register(struct v4l2_subdev *sd)
  696. {
  697. v4l2_info(sd, "0x000-0x0ff: IO Map\n");
  698. v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
  699. v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
  700. v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
  701. v4l2_info(sd, "0x400-0x4ff: SDP_IO Map\n");
  702. v4l2_info(sd, "0x500-0x5ff: SDP Map\n");
  703. v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
  704. v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
  705. v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
  706. v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
  707. v4l2_info(sd, "0xa00-0xaff: CP Map\n");
  708. v4l2_info(sd, "0xb00-0xbff: VDP Map\n");
  709. }
  710. static int adv7842_g_register(struct v4l2_subdev *sd,
  711. struct v4l2_dbg_register *reg)
  712. {
  713. reg->size = 1;
  714. switch (reg->reg >> 8) {
  715. case 0:
  716. reg->val = io_read(sd, reg->reg & 0xff);
  717. break;
  718. case 1:
  719. reg->val = avlink_read(sd, reg->reg & 0xff);
  720. break;
  721. case 2:
  722. reg->val = cec_read(sd, reg->reg & 0xff);
  723. break;
  724. case 3:
  725. reg->val = infoframe_read(sd, reg->reg & 0xff);
  726. break;
  727. case 4:
  728. reg->val = sdp_io_read(sd, reg->reg & 0xff);
  729. break;
  730. case 5:
  731. reg->val = sdp_read(sd, reg->reg & 0xff);
  732. break;
  733. case 6:
  734. reg->val = afe_read(sd, reg->reg & 0xff);
  735. break;
  736. case 7:
  737. reg->val = rep_read(sd, reg->reg & 0xff);
  738. break;
  739. case 8:
  740. reg->val = edid_read(sd, reg->reg & 0xff);
  741. break;
  742. case 9:
  743. reg->val = hdmi_read(sd, reg->reg & 0xff);
  744. break;
  745. case 0xa:
  746. reg->val = cp_read(sd, reg->reg & 0xff);
  747. break;
  748. case 0xb:
  749. reg->val = vdp_read(sd, reg->reg & 0xff);
  750. break;
  751. default:
  752. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  753. adv7842_inv_register(sd);
  754. break;
  755. }
  756. return 0;
  757. }
  758. static int adv7842_s_register(struct v4l2_subdev *sd,
  759. const struct v4l2_dbg_register *reg)
  760. {
  761. u8 val = reg->val & 0xff;
  762. switch (reg->reg >> 8) {
  763. case 0:
  764. io_write(sd, reg->reg & 0xff, val);
  765. break;
  766. case 1:
  767. avlink_write(sd, reg->reg & 0xff, val);
  768. break;
  769. case 2:
  770. cec_write(sd, reg->reg & 0xff, val);
  771. break;
  772. case 3:
  773. infoframe_write(sd, reg->reg & 0xff, val);
  774. break;
  775. case 4:
  776. sdp_io_write(sd, reg->reg & 0xff, val);
  777. break;
  778. case 5:
  779. sdp_write(sd, reg->reg & 0xff, val);
  780. break;
  781. case 6:
  782. afe_write(sd, reg->reg & 0xff, val);
  783. break;
  784. case 7:
  785. rep_write(sd, reg->reg & 0xff, val);
  786. break;
  787. case 8:
  788. edid_write(sd, reg->reg & 0xff, val);
  789. break;
  790. case 9:
  791. hdmi_write(sd, reg->reg & 0xff, val);
  792. break;
  793. case 0xa:
  794. cp_write(sd, reg->reg & 0xff, val);
  795. break;
  796. case 0xb:
  797. vdp_write(sd, reg->reg & 0xff, val);
  798. break;
  799. default:
  800. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  801. adv7842_inv_register(sd);
  802. break;
  803. }
  804. return 0;
  805. }
  806. #endif
  807. static int adv7842_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
  808. {
  809. struct adv7842_state *state = to_state(sd);
  810. u16 cable_det = adv7842_read_cable_det(sd);
  811. v4l2_dbg(1, debug, sd, "%s: 0x%x\n", __func__, cable_det);
  812. return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl, cable_det);
  813. }
  814. static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
  815. u8 prim_mode,
  816. const struct adv7842_video_standards *predef_vid_timings,
  817. const struct v4l2_dv_timings *timings)
  818. {
  819. int i;
  820. for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
  821. if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
  822. is_digital_input(sd) ? 250000 : 1000000, false))
  823. continue;
  824. /* video std */
  825. io_write(sd, 0x00, predef_vid_timings[i].vid_std);
  826. /* v_freq and prim mode */
  827. io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) + prim_mode);
  828. return 0;
  829. }
  830. return -1;
  831. }
  832. static int configure_predefined_video_timings(struct v4l2_subdev *sd,
  833. struct v4l2_dv_timings *timings)
  834. {
  835. struct adv7842_state *state = to_state(sd);
  836. int err;
  837. v4l2_dbg(1, debug, sd, "%s\n", __func__);
  838. /* reset to default values */
  839. io_write(sd, 0x16, 0x43);
  840. io_write(sd, 0x17, 0x5a);
  841. /* disable embedded syncs for auto graphics mode */
  842. cp_write_and_or(sd, 0x81, 0xef, 0x00);
  843. cp_write(sd, 0x26, 0x00);
  844. cp_write(sd, 0x27, 0x00);
  845. cp_write(sd, 0x28, 0x00);
  846. cp_write(sd, 0x29, 0x00);
  847. cp_write(sd, 0x8f, 0x40);
  848. cp_write(sd, 0x90, 0x00);
  849. cp_write(sd, 0xa5, 0x00);
  850. cp_write(sd, 0xa6, 0x00);
  851. cp_write(sd, 0xa7, 0x00);
  852. cp_write(sd, 0xab, 0x00);
  853. cp_write(sd, 0xac, 0x00);
  854. switch (state->mode) {
  855. case ADV7842_MODE_COMP:
  856. case ADV7842_MODE_RGB:
  857. err = find_and_set_predefined_video_timings(sd,
  858. 0x01, adv7842_prim_mode_comp, timings);
  859. if (err)
  860. err = find_and_set_predefined_video_timings(sd,
  861. 0x02, adv7842_prim_mode_gr, timings);
  862. break;
  863. case ADV7842_MODE_HDMI:
  864. err = find_and_set_predefined_video_timings(sd,
  865. 0x05, adv7842_prim_mode_hdmi_comp, timings);
  866. if (err)
  867. err = find_and_set_predefined_video_timings(sd,
  868. 0x06, adv7842_prim_mode_hdmi_gr, timings);
  869. break;
  870. default:
  871. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  872. __func__, state->mode);
  873. err = -1;
  874. break;
  875. }
  876. return err;
  877. }
  878. static void configure_custom_video_timings(struct v4l2_subdev *sd,
  879. const struct v4l2_bt_timings *bt)
  880. {
  881. struct adv7842_state *state = to_state(sd);
  882. struct i2c_client *client = v4l2_get_subdevdata(sd);
  883. u32 width = htotal(bt);
  884. u32 height = vtotal(bt);
  885. u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
  886. u16 cp_start_eav = width - bt->hfrontporch;
  887. u16 cp_start_vbi = height - bt->vfrontporch + 1;
  888. u16 cp_end_vbi = bt->vsync + bt->vbackporch + 1;
  889. u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
  890. ((width * (ADV7842_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
  891. const u8 pll[2] = {
  892. 0xc0 | ((width >> 8) & 0x1f),
  893. width & 0xff
  894. };
  895. v4l2_dbg(2, debug, sd, "%s\n", __func__);
  896. switch (state->mode) {
  897. case ADV7842_MODE_COMP:
  898. case ADV7842_MODE_RGB:
  899. /* auto graphics */
  900. io_write(sd, 0x00, 0x07); /* video std */
  901. io_write(sd, 0x01, 0x02); /* prim mode */
  902. /* enable embedded syncs for auto graphics mode */
  903. cp_write_and_or(sd, 0x81, 0xef, 0x10);
  904. /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
  905. /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
  906. /* IO-map reg. 0x16 and 0x17 should be written in sequence */
  907. if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll)) {
  908. v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
  909. break;
  910. }
  911. /* active video - horizontal timing */
  912. cp_write(sd, 0x26, (cp_start_sav >> 8) & 0xf);
  913. cp_write(sd, 0x27, (cp_start_sav & 0xff));
  914. cp_write(sd, 0x28, (cp_start_eav >> 8) & 0xf);
  915. cp_write(sd, 0x29, (cp_start_eav & 0xff));
  916. /* active video - vertical timing */
  917. cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
  918. cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
  919. ((cp_end_vbi >> 8) & 0xf));
  920. cp_write(sd, 0xa7, cp_end_vbi & 0xff);
  921. break;
  922. case ADV7842_MODE_HDMI:
  923. /* set default prim_mode/vid_std for HDMI
  924. according to [REF_03, c. 4.2] */
  925. io_write(sd, 0x00, 0x02); /* video std */
  926. io_write(sd, 0x01, 0x06); /* prim mode */
  927. break;
  928. default:
  929. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  930. __func__, state->mode);
  931. break;
  932. }
  933. cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
  934. cp_write(sd, 0x90, ch1_fr_ll & 0xff);
  935. cp_write(sd, 0xab, (height >> 4) & 0xff);
  936. cp_write(sd, 0xac, (height & 0x0f) << 4);
  937. }
  938. static void adv7842_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
  939. {
  940. struct adv7842_state *state = to_state(sd);
  941. u8 offset_buf[4];
  942. if (auto_offset) {
  943. offset_a = 0x3ff;
  944. offset_b = 0x3ff;
  945. offset_c = 0x3ff;
  946. }
  947. v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
  948. __func__, auto_offset ? "Auto" : "Manual",
  949. offset_a, offset_b, offset_c);
  950. offset_buf[0]= (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
  951. offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
  952. offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
  953. offset_buf[3] = offset_c & 0x0ff;
  954. /* Registers must be written in this order with no i2c access in between */
  955. if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x77, 4, offset_buf))
  956. v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
  957. }
  958. static void adv7842_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
  959. {
  960. struct adv7842_state *state = to_state(sd);
  961. u8 gain_buf[4];
  962. u8 gain_man = 1;
  963. u8 agc_mode_man = 1;
  964. if (auto_gain) {
  965. gain_man = 0;
  966. agc_mode_man = 0;
  967. gain_a = 0x100;
  968. gain_b = 0x100;
  969. gain_c = 0x100;
  970. }
  971. v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
  972. __func__, auto_gain ? "Auto" : "Manual",
  973. gain_a, gain_b, gain_c);
  974. gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
  975. gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
  976. gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
  977. gain_buf[3] = ((gain_c & 0x0ff));
  978. /* Registers must be written in this order with no i2c access in between */
  979. if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x73, 4, gain_buf))
  980. v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
  981. }
  982. static void set_rgb_quantization_range(struct v4l2_subdev *sd)
  983. {
  984. struct adv7842_state *state = to_state(sd);
  985. bool rgb_output = io_read(sd, 0x02) & 0x02;
  986. bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
  987. u8 y = HDMI_COLORSPACE_RGB;
  988. if (hdmi_signal && (io_read(sd, 0x60) & 1))
  989. y = infoframe_read(sd, 0x01) >> 5;
  990. v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
  991. __func__, state->rgb_quantization_range,
  992. rgb_output, hdmi_signal);
  993. adv7842_set_gain(sd, true, 0x0, 0x0, 0x0);
  994. adv7842_set_offset(sd, true, 0x0, 0x0, 0x0);
  995. io_write_clr_set(sd, 0x02, 0x04, rgb_output ? 0 : 4);
  996. switch (state->rgb_quantization_range) {
  997. case V4L2_DV_RGB_RANGE_AUTO:
  998. if (state->mode == ADV7842_MODE_RGB) {
  999. /* Receiving analog RGB signal
  1000. * Set RGB full range (0-255) */
  1001. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  1002. break;
  1003. }
  1004. if (state->mode == ADV7842_MODE_COMP) {
  1005. /* Receiving analog YPbPr signal
  1006. * Set automode */
  1007. io_write_and_or(sd, 0x02, 0x0f, 0xf0);
  1008. break;
  1009. }
  1010. if (hdmi_signal) {
  1011. /* Receiving HDMI signal
  1012. * Set automode */
  1013. io_write_and_or(sd, 0x02, 0x0f, 0xf0);
  1014. break;
  1015. }
  1016. /* Receiving DVI-D signal
  1017. * ADV7842 selects RGB limited range regardless of
  1018. * input format (CE/IT) in automatic mode */
  1019. if (state->timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) {
  1020. /* RGB limited range (16-235) */
  1021. io_write_and_or(sd, 0x02, 0x0f, 0x00);
  1022. } else {
  1023. /* RGB full range (0-255) */
  1024. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  1025. if (is_digital_input(sd) && rgb_output) {
  1026. adv7842_set_offset(sd, false, 0x40, 0x40, 0x40);
  1027. } else {
  1028. adv7842_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
  1029. adv7842_set_offset(sd, false, 0x70, 0x70, 0x70);
  1030. }
  1031. }
  1032. break;
  1033. case V4L2_DV_RGB_RANGE_LIMITED:
  1034. if (state->mode == ADV7842_MODE_COMP) {
  1035. /* YCrCb limited range (16-235) */
  1036. io_write_and_or(sd, 0x02, 0x0f, 0x20);
  1037. break;
  1038. }
  1039. if (y != HDMI_COLORSPACE_RGB)
  1040. break;
  1041. /* RGB limited range (16-235) */
  1042. io_write_and_or(sd, 0x02, 0x0f, 0x00);
  1043. break;
  1044. case V4L2_DV_RGB_RANGE_FULL:
  1045. if (state->mode == ADV7842_MODE_COMP) {
  1046. /* YCrCb full range (0-255) */
  1047. io_write_and_or(sd, 0x02, 0x0f, 0x60);
  1048. break;
  1049. }
  1050. if (y != HDMI_COLORSPACE_RGB)
  1051. break;
  1052. /* RGB full range (0-255) */
  1053. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  1054. if (is_analog_input(sd) || hdmi_signal)
  1055. break;
  1056. /* Adjust gain/offset for DVI-D signals only */
  1057. if (rgb_output) {
  1058. adv7842_set_offset(sd, false, 0x40, 0x40, 0x40);
  1059. } else {
  1060. adv7842_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
  1061. adv7842_set_offset(sd, false, 0x70, 0x70, 0x70);
  1062. }
  1063. break;
  1064. }
  1065. }
  1066. static int adv7842_s_ctrl(struct v4l2_ctrl *ctrl)
  1067. {
  1068. struct v4l2_subdev *sd = to_sd(ctrl);
  1069. struct adv7842_state *state = to_state(sd);
  1070. /* TODO SDP ctrls
  1071. contrast/brightness/hue/free run is acting a bit strange,
  1072. not sure if sdp csc is correct.
  1073. */
  1074. switch (ctrl->id) {
  1075. /* standard ctrls */
  1076. case V4L2_CID_BRIGHTNESS:
  1077. cp_write(sd, 0x3c, ctrl->val);
  1078. sdp_write(sd, 0x14, ctrl->val);
  1079. /* ignore lsb sdp 0x17[3:2] */
  1080. return 0;
  1081. case V4L2_CID_CONTRAST:
  1082. cp_write(sd, 0x3a, ctrl->val);
  1083. sdp_write(sd, 0x13, ctrl->val);
  1084. /* ignore lsb sdp 0x17[1:0] */
  1085. return 0;
  1086. case V4L2_CID_SATURATION:
  1087. cp_write(sd, 0x3b, ctrl->val);
  1088. sdp_write(sd, 0x15, ctrl->val);
  1089. /* ignore lsb sdp 0x17[5:4] */
  1090. return 0;
  1091. case V4L2_CID_HUE:
  1092. cp_write(sd, 0x3d, ctrl->val);
  1093. sdp_write(sd, 0x16, ctrl->val);
  1094. /* ignore lsb sdp 0x17[7:6] */
  1095. return 0;
  1096. /* custom ctrls */
  1097. case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
  1098. afe_write(sd, 0xc8, ctrl->val);
  1099. return 0;
  1100. case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
  1101. cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
  1102. sdp_write_and_or(sd, 0xdd, ~0x04, (ctrl->val << 2));
  1103. return 0;
  1104. case V4L2_CID_ADV_RX_FREE_RUN_COLOR: {
  1105. u8 R = (ctrl->val & 0xff0000) >> 16;
  1106. u8 G = (ctrl->val & 0x00ff00) >> 8;
  1107. u8 B = (ctrl->val & 0x0000ff);
  1108. /* RGB -> YUV, numerical approximation */
  1109. int Y = 66 * R + 129 * G + 25 * B;
  1110. int U = -38 * R - 74 * G + 112 * B;
  1111. int V = 112 * R - 94 * G - 18 * B;
  1112. /* Scale down to 8 bits with rounding */
  1113. Y = (Y + 128) >> 8;
  1114. U = (U + 128) >> 8;
  1115. V = (V + 128) >> 8;
  1116. /* make U,V positive */
  1117. Y += 16;
  1118. U += 128;
  1119. V += 128;
  1120. v4l2_dbg(1, debug, sd, "R %x, G %x, B %x\n", R, G, B);
  1121. v4l2_dbg(1, debug, sd, "Y %x, U %x, V %x\n", Y, U, V);
  1122. /* CP */
  1123. cp_write(sd, 0xc1, R);
  1124. cp_write(sd, 0xc0, G);
  1125. cp_write(sd, 0xc2, B);
  1126. /* SDP */
  1127. sdp_write(sd, 0xde, Y);
  1128. sdp_write(sd, 0xdf, (V & 0xf0) | ((U >> 4) & 0x0f));
  1129. return 0;
  1130. }
  1131. case V4L2_CID_DV_RX_RGB_RANGE:
  1132. state->rgb_quantization_range = ctrl->val;
  1133. set_rgb_quantization_range(sd);
  1134. return 0;
  1135. }
  1136. return -EINVAL;
  1137. }
  1138. static int adv7842_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  1139. {
  1140. struct v4l2_subdev *sd = to_sd(ctrl);
  1141. if (ctrl->id == V4L2_CID_DV_RX_IT_CONTENT_TYPE) {
  1142. ctrl->val = V4L2_DV_IT_CONTENT_TYPE_NO_ITC;
  1143. if ((io_read(sd, 0x60) & 1) && (infoframe_read(sd, 0x03) & 0x80))
  1144. ctrl->val = (infoframe_read(sd, 0x05) >> 4) & 3;
  1145. return 0;
  1146. }
  1147. return -EINVAL;
  1148. }
  1149. static inline bool no_power(struct v4l2_subdev *sd)
  1150. {
  1151. return io_read(sd, 0x0c) & 0x24;
  1152. }
  1153. static inline bool no_cp_signal(struct v4l2_subdev *sd)
  1154. {
  1155. return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0) || !(cp_read(sd, 0xb1) & 0x80);
  1156. }
  1157. static inline bool is_hdmi(struct v4l2_subdev *sd)
  1158. {
  1159. return hdmi_read(sd, 0x05) & 0x80;
  1160. }
  1161. static int adv7842_g_input_status(struct v4l2_subdev *sd, u32 *status)
  1162. {
  1163. struct adv7842_state *state = to_state(sd);
  1164. *status = 0;
  1165. if (io_read(sd, 0x0c) & 0x24)
  1166. *status |= V4L2_IN_ST_NO_POWER;
  1167. if (state->mode == ADV7842_MODE_SDP) {
  1168. /* status from SDP block */
  1169. if (!(sdp_read(sd, 0x5A) & 0x01))
  1170. *status |= V4L2_IN_ST_NO_SIGNAL;
  1171. v4l2_dbg(1, debug, sd, "%s: SDP status = 0x%x\n",
  1172. __func__, *status);
  1173. return 0;
  1174. }
  1175. /* status from CP block */
  1176. if ((cp_read(sd, 0xb5) & 0xd0) != 0xd0 ||
  1177. !(cp_read(sd, 0xb1) & 0x80))
  1178. /* TODO channel 2 */
  1179. *status |= V4L2_IN_ST_NO_SIGNAL;
  1180. if (is_digital_input(sd) && ((io_read(sd, 0x74) & 0x03) != 0x03))
  1181. *status |= V4L2_IN_ST_NO_SIGNAL;
  1182. v4l2_dbg(1, debug, sd, "%s: CP status = 0x%x\n",
  1183. __func__, *status);
  1184. return 0;
  1185. }
  1186. struct stdi_readback {
  1187. u16 bl, lcf, lcvs;
  1188. u8 hs_pol, vs_pol;
  1189. bool interlaced;
  1190. };
  1191. static int stdi2dv_timings(struct v4l2_subdev *sd,
  1192. struct stdi_readback *stdi,
  1193. struct v4l2_dv_timings *timings)
  1194. {
  1195. struct adv7842_state *state = to_state(sd);
  1196. u32 hfreq = (ADV7842_fsc * 8) / stdi->bl;
  1197. u32 pix_clk;
  1198. int i;
  1199. for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
  1200. const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
  1201. if (!v4l2_valid_dv_timings(&v4l2_dv_timings_presets[i],
  1202. adv7842_get_dv_timings_cap(sd),
  1203. adv7842_check_dv_timings, NULL))
  1204. continue;
  1205. if (vtotal(bt) != stdi->lcf + 1)
  1206. continue;
  1207. if (bt->vsync != stdi->lcvs)
  1208. continue;
  1209. pix_clk = hfreq * htotal(bt);
  1210. if ((pix_clk < bt->pixelclock + 1000000) &&
  1211. (pix_clk > bt->pixelclock - 1000000)) {
  1212. *timings = v4l2_dv_timings_presets[i];
  1213. return 0;
  1214. }
  1215. }
  1216. if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs, 0,
  1217. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  1218. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  1219. false, timings))
  1220. return 0;
  1221. if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
  1222. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  1223. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  1224. false, state->aspect_ratio, timings))
  1225. return 0;
  1226. v4l2_dbg(2, debug, sd,
  1227. "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
  1228. __func__, stdi->lcvs, stdi->lcf, stdi->bl,
  1229. stdi->hs_pol, stdi->vs_pol);
  1230. return -1;
  1231. }
  1232. static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
  1233. {
  1234. u32 status;
  1235. adv7842_g_input_status(sd, &status);
  1236. if (status & V4L2_IN_ST_NO_SIGNAL) {
  1237. v4l2_dbg(2, debug, sd, "%s: no signal\n", __func__);
  1238. return -ENOLINK;
  1239. }
  1240. stdi->bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
  1241. stdi->lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
  1242. stdi->lcvs = cp_read(sd, 0xb3) >> 3;
  1243. if ((cp_read(sd, 0xb5) & 0x80) && ((cp_read(sd, 0xb5) & 0x03) == 0x01)) {
  1244. stdi->hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
  1245. ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
  1246. stdi->vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
  1247. ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
  1248. } else {
  1249. stdi->hs_pol = 'x';
  1250. stdi->vs_pol = 'x';
  1251. }
  1252. stdi->interlaced = (cp_read(sd, 0xb1) & 0x40) ? true : false;
  1253. if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
  1254. v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
  1255. return -ENOLINK;
  1256. }
  1257. v4l2_dbg(2, debug, sd,
  1258. "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
  1259. __func__, stdi->lcf, stdi->bl, stdi->lcvs,
  1260. stdi->hs_pol, stdi->vs_pol,
  1261. stdi->interlaced ? "interlaced" : "progressive");
  1262. return 0;
  1263. }
  1264. static int adv7842_enum_dv_timings(struct v4l2_subdev *sd,
  1265. struct v4l2_enum_dv_timings *timings)
  1266. {
  1267. if (timings->pad != 0)
  1268. return -EINVAL;
  1269. return v4l2_enum_dv_timings_cap(timings,
  1270. adv7842_get_dv_timings_cap(sd), adv7842_check_dv_timings, NULL);
  1271. }
  1272. static int adv7842_dv_timings_cap(struct v4l2_subdev *sd,
  1273. struct v4l2_dv_timings_cap *cap)
  1274. {
  1275. if (cap->pad != 0)
  1276. return -EINVAL;
  1277. *cap = *adv7842_get_dv_timings_cap(sd);
  1278. return 0;
  1279. }
  1280. /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
  1281. if the format is listed in adv7842_timings[] */
  1282. static void adv7842_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
  1283. struct v4l2_dv_timings *timings)
  1284. {
  1285. v4l2_find_dv_timings_cap(timings, adv7842_get_dv_timings_cap(sd),
  1286. is_digital_input(sd) ? 250000 : 1000000,
  1287. adv7842_check_dv_timings, NULL);
  1288. }
  1289. static int adv7842_query_dv_timings(struct v4l2_subdev *sd,
  1290. struct v4l2_dv_timings *timings)
  1291. {
  1292. struct adv7842_state *state = to_state(sd);
  1293. struct v4l2_bt_timings *bt = &timings->bt;
  1294. struct stdi_readback stdi = { 0 };
  1295. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  1296. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1297. /* SDP block */
  1298. if (state->mode == ADV7842_MODE_SDP)
  1299. return -ENODATA;
  1300. /* read STDI */
  1301. if (read_stdi(sd, &stdi)) {
  1302. state->restart_stdi_once = true;
  1303. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  1304. return -ENOLINK;
  1305. }
  1306. bt->interlaced = stdi.interlaced ?
  1307. V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
  1308. bt->standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  1309. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
  1310. if (is_digital_input(sd)) {
  1311. u32 freq;
  1312. timings->type = V4L2_DV_BT_656_1120;
  1313. bt->width = (hdmi_read(sd, 0x07) & 0x0f) * 256 + hdmi_read(sd, 0x08);
  1314. bt->height = (hdmi_read(sd, 0x09) & 0x0f) * 256 + hdmi_read(sd, 0x0a);
  1315. freq = ((hdmi_read(sd, 0x51) << 1) + (hdmi_read(sd, 0x52) >> 7)) * 1000000;
  1316. freq += ((hdmi_read(sd, 0x52) & 0x7f) * 7813);
  1317. if (is_hdmi(sd)) {
  1318. /* adjust for deep color mode */
  1319. freq = freq * 8 / (((hdmi_read(sd, 0x0b) & 0xc0) >> 6) * 2 + 8);
  1320. }
  1321. bt->pixelclock = freq;
  1322. bt->hfrontporch = (hdmi_read(sd, 0x20) & 0x03) * 256 +
  1323. hdmi_read(sd, 0x21);
  1324. bt->hsync = (hdmi_read(sd, 0x22) & 0x03) * 256 +
  1325. hdmi_read(sd, 0x23);
  1326. bt->hbackporch = (hdmi_read(sd, 0x24) & 0x03) * 256 +
  1327. hdmi_read(sd, 0x25);
  1328. bt->vfrontporch = ((hdmi_read(sd, 0x2a) & 0x1f) * 256 +
  1329. hdmi_read(sd, 0x2b)) / 2;
  1330. bt->vsync = ((hdmi_read(sd, 0x2e) & 0x1f) * 256 +
  1331. hdmi_read(sd, 0x2f)) / 2;
  1332. bt->vbackporch = ((hdmi_read(sd, 0x32) & 0x1f) * 256 +
  1333. hdmi_read(sd, 0x33)) / 2;
  1334. bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
  1335. ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
  1336. if (bt->interlaced == V4L2_DV_INTERLACED) {
  1337. bt->height += (hdmi_read(sd, 0x0b) & 0x0f) * 256 +
  1338. hdmi_read(sd, 0x0c);
  1339. bt->il_vfrontporch = ((hdmi_read(sd, 0x2c) & 0x1f) * 256 +
  1340. hdmi_read(sd, 0x2d)) / 2;
  1341. bt->il_vsync = ((hdmi_read(sd, 0x30) & 0x1f) * 256 +
  1342. hdmi_read(sd, 0x31)) / 2;
  1343. bt->il_vbackporch = ((hdmi_read(sd, 0x34) & 0x1f) * 256 +
  1344. hdmi_read(sd, 0x35)) / 2;
  1345. } else {
  1346. bt->il_vfrontporch = 0;
  1347. bt->il_vsync = 0;
  1348. bt->il_vbackporch = 0;
  1349. }
  1350. adv7842_fill_optional_dv_timings_fields(sd, timings);
  1351. } else {
  1352. /* find format
  1353. * Since LCVS values are inaccurate [REF_03, p. 339-340],
  1354. * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
  1355. */
  1356. if (!stdi2dv_timings(sd, &stdi, timings))
  1357. goto found;
  1358. stdi.lcvs += 1;
  1359. v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
  1360. if (!stdi2dv_timings(sd, &stdi, timings))
  1361. goto found;
  1362. stdi.lcvs -= 2;
  1363. v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
  1364. if (stdi2dv_timings(sd, &stdi, timings)) {
  1365. /*
  1366. * The STDI block may measure wrong values, especially
  1367. * for lcvs and lcf. If the driver can not find any
  1368. * valid timing, the STDI block is restarted to measure
  1369. * the video timings again. The function will return an
  1370. * error, but the restart of STDI will generate a new
  1371. * STDI interrupt and the format detection process will
  1372. * restart.
  1373. */
  1374. if (state->restart_stdi_once) {
  1375. v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
  1376. /* TODO restart STDI for Sync Channel 2 */
  1377. /* enter one-shot mode */
  1378. cp_write_and_or(sd, 0x86, 0xf9, 0x00);
  1379. /* trigger STDI restart */
  1380. cp_write_and_or(sd, 0x86, 0xf9, 0x04);
  1381. /* reset to continuous mode */
  1382. cp_write_and_or(sd, 0x86, 0xf9, 0x02);
  1383. state->restart_stdi_once = false;
  1384. return -ENOLINK;
  1385. }
  1386. v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
  1387. return -ERANGE;
  1388. }
  1389. state->restart_stdi_once = true;
  1390. }
  1391. found:
  1392. if (debug > 1)
  1393. v4l2_print_dv_timings(sd->name, "adv7842_query_dv_timings:",
  1394. timings, true);
  1395. return 0;
  1396. }
  1397. static int adv7842_s_dv_timings(struct v4l2_subdev *sd,
  1398. struct v4l2_dv_timings *timings)
  1399. {
  1400. struct adv7842_state *state = to_state(sd);
  1401. struct v4l2_bt_timings *bt;
  1402. int err;
  1403. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  1404. if (state->mode == ADV7842_MODE_SDP)
  1405. return -ENODATA;
  1406. if (v4l2_match_dv_timings(&state->timings, timings, 0, false)) {
  1407. v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
  1408. return 0;
  1409. }
  1410. bt = &timings->bt;
  1411. if (!v4l2_valid_dv_timings(timings, adv7842_get_dv_timings_cap(sd),
  1412. adv7842_check_dv_timings, NULL))
  1413. return -ERANGE;
  1414. adv7842_fill_optional_dv_timings_fields(sd, timings);
  1415. state->timings = *timings;
  1416. cp_write(sd, 0x91, bt->interlaced ? 0x40 : 0x00);
  1417. /* Use prim_mode and vid_std when available */
  1418. err = configure_predefined_video_timings(sd, timings);
  1419. if (err) {
  1420. /* custom settings when the video format
  1421. does not have prim_mode/vid_std */
  1422. configure_custom_video_timings(sd, bt);
  1423. }
  1424. set_rgb_quantization_range(sd);
  1425. if (debug > 1)
  1426. v4l2_print_dv_timings(sd->name, "adv7842_s_dv_timings: ",
  1427. timings, true);
  1428. return 0;
  1429. }
  1430. static int adv7842_g_dv_timings(struct v4l2_subdev *sd,
  1431. struct v4l2_dv_timings *timings)
  1432. {
  1433. struct adv7842_state *state = to_state(sd);
  1434. if (state->mode == ADV7842_MODE_SDP)
  1435. return -ENODATA;
  1436. *timings = state->timings;
  1437. return 0;
  1438. }
  1439. static void enable_input(struct v4l2_subdev *sd)
  1440. {
  1441. struct adv7842_state *state = to_state(sd);
  1442. set_rgb_quantization_range(sd);
  1443. switch (state->mode) {
  1444. case ADV7842_MODE_SDP:
  1445. case ADV7842_MODE_COMP:
  1446. case ADV7842_MODE_RGB:
  1447. io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
  1448. break;
  1449. case ADV7842_MODE_HDMI:
  1450. hdmi_write(sd, 0x01, 0x00); /* Enable HDMI clock terminators */
  1451. io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
  1452. hdmi_write_and_or(sd, 0x1a, 0xef, 0x00); /* Unmute audio */
  1453. break;
  1454. default:
  1455. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  1456. __func__, state->mode);
  1457. break;
  1458. }
  1459. }
  1460. static void disable_input(struct v4l2_subdev *sd)
  1461. {
  1462. hdmi_write_and_or(sd, 0x1a, 0xef, 0x10); /* Mute audio [REF_01, c. 2.2.2] */
  1463. msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 8.29] */
  1464. io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
  1465. hdmi_write(sd, 0x01, 0x78); /* Disable HDMI clock terminators */
  1466. }
  1467. static void sdp_csc_coeff(struct v4l2_subdev *sd,
  1468. const struct adv7842_sdp_csc_coeff *c)
  1469. {
  1470. /* csc auto/manual */
  1471. sdp_io_write_and_or(sd, 0xe0, 0xbf, c->manual ? 0x00 : 0x40);
  1472. if (!c->manual)
  1473. return;
  1474. /* csc scaling */
  1475. sdp_io_write_and_or(sd, 0xe0, 0x7f, c->scaling == 2 ? 0x80 : 0x00);
  1476. /* A coeff */
  1477. sdp_io_write_and_or(sd, 0xe0, 0xe0, c->A1 >> 8);
  1478. sdp_io_write(sd, 0xe1, c->A1);
  1479. sdp_io_write_and_or(sd, 0xe2, 0xe0, c->A2 >> 8);
  1480. sdp_io_write(sd, 0xe3, c->A2);
  1481. sdp_io_write_and_or(sd, 0xe4, 0xe0, c->A3 >> 8);
  1482. sdp_io_write(sd, 0xe5, c->A3);
  1483. /* A scale */
  1484. sdp_io_write_and_or(sd, 0xe6, 0x80, c->A4 >> 8);
  1485. sdp_io_write(sd, 0xe7, c->A4);
  1486. /* B coeff */
  1487. sdp_io_write_and_or(sd, 0xe8, 0xe0, c->B1 >> 8);
  1488. sdp_io_write(sd, 0xe9, c->B1);
  1489. sdp_io_write_and_or(sd, 0xea, 0xe0, c->B2 >> 8);
  1490. sdp_io_write(sd, 0xeb, c->B2);
  1491. sdp_io_write_and_or(sd, 0xec, 0xe0, c->B3 >> 8);
  1492. sdp_io_write(sd, 0xed, c->B3);
  1493. /* B scale */
  1494. sdp_io_write_and_or(sd, 0xee, 0x80, c->B4 >> 8);
  1495. sdp_io_write(sd, 0xef, c->B4);
  1496. /* C coeff */
  1497. sdp_io_write_and_or(sd, 0xf0, 0xe0, c->C1 >> 8);
  1498. sdp_io_write(sd, 0xf1, c->C1);
  1499. sdp_io_write_and_or(sd, 0xf2, 0xe0, c->C2 >> 8);
  1500. sdp_io_write(sd, 0xf3, c->C2);
  1501. sdp_io_write_and_or(sd, 0xf4, 0xe0, c->C3 >> 8);
  1502. sdp_io_write(sd, 0xf5, c->C3);
  1503. /* C scale */
  1504. sdp_io_write_and_or(sd, 0xf6, 0x80, c->C4 >> 8);
  1505. sdp_io_write(sd, 0xf7, c->C4);
  1506. }
  1507. static void select_input(struct v4l2_subdev *sd,
  1508. enum adv7842_vid_std_select vid_std_select)
  1509. {
  1510. struct adv7842_state *state = to_state(sd);
  1511. switch (state->mode) {
  1512. case ADV7842_MODE_SDP:
  1513. io_write(sd, 0x00, vid_std_select); /* video std: CVBS or YC mode */
  1514. io_write(sd, 0x01, 0); /* prim mode */
  1515. /* enable embedded syncs for auto graphics mode */
  1516. cp_write_and_or(sd, 0x81, 0xef, 0x10);
  1517. afe_write(sd, 0x00, 0x00); /* power up ADC */
  1518. afe_write(sd, 0xc8, 0x00); /* phase control */
  1519. io_write(sd, 0xdd, 0x90); /* Manual 2x output clock */
  1520. /* script says register 0xde, which don't exist in manual */
  1521. /* Manual analog input muxing mode, CVBS (6.4)*/
  1522. afe_write_and_or(sd, 0x02, 0x7f, 0x80);
  1523. if (vid_std_select == ADV7842_SDP_VID_STD_CVBS_SD_4x1) {
  1524. afe_write(sd, 0x03, 0xa0); /* ADC0 to AIN10 (CVBS), ADC1 N/C*/
  1525. afe_write(sd, 0x04, 0x00); /* ADC2 N/C,ADC3 N/C*/
  1526. } else {
  1527. afe_write(sd, 0x03, 0xa0); /* ADC0 to AIN10 (CVBS), ADC1 N/C*/
  1528. afe_write(sd, 0x04, 0xc0); /* ADC2 to AIN12, ADC3 N/C*/
  1529. }
  1530. afe_write(sd, 0x0c, 0x1f); /* ADI recommend write */
  1531. afe_write(sd, 0x12, 0x63); /* ADI recommend write */
  1532. sdp_io_write(sd, 0xb2, 0x60); /* Disable AV codes */
  1533. sdp_io_write(sd, 0xc8, 0xe3); /* Disable Ancillary data */
  1534. /* SDP recommended settings */
  1535. sdp_write(sd, 0x00, 0x3F); /* Autodetect PAL NTSC (not SECAM) */
  1536. sdp_write(sd, 0x01, 0x00); /* Pedestal Off */
  1537. sdp_write(sd, 0x03, 0xE4); /* Manual VCR Gain Luma 0x40B */
  1538. sdp_write(sd, 0x04, 0x0B); /* Manual Luma setting */
  1539. sdp_write(sd, 0x05, 0xC3); /* Manual Chroma setting 0x3FE */
  1540. sdp_write(sd, 0x06, 0xFE); /* Manual Chroma setting */
  1541. sdp_write(sd, 0x12, 0x0D); /* Frame TBC,I_P, 3D comb enabled */
  1542. sdp_write(sd, 0xA7, 0x00); /* ADI Recommended Write */
  1543. sdp_io_write(sd, 0xB0, 0x00); /* Disable H and v blanking */
  1544. /* deinterlacer enabled and 3D comb */
  1545. sdp_write_and_or(sd, 0x12, 0xf6, 0x09);
  1546. break;
  1547. case ADV7842_MODE_COMP:
  1548. case ADV7842_MODE_RGB:
  1549. /* Automatic analog input muxing mode */
  1550. afe_write_and_or(sd, 0x02, 0x7f, 0x00);
  1551. /* set mode and select free run resolution */
  1552. io_write(sd, 0x00, vid_std_select); /* video std */
  1553. io_write(sd, 0x01, 0x02); /* prim mode */
  1554. cp_write_and_or(sd, 0x81, 0xef, 0x10); /* enable embedded syncs
  1555. for auto graphics mode */
  1556. afe_write(sd, 0x00, 0x00); /* power up ADC */
  1557. afe_write(sd, 0xc8, 0x00); /* phase control */
  1558. if (state->mode == ADV7842_MODE_COMP) {
  1559. /* force to YCrCb */
  1560. io_write_and_or(sd, 0x02, 0x0f, 0x60);
  1561. } else {
  1562. /* force to RGB */
  1563. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  1564. }
  1565. /* set ADI recommended settings for digitizer */
  1566. /* "ADV7842 Register Settings Recommendations
  1567. * (rev. 1.8, November 2010)" p. 9. */
  1568. afe_write(sd, 0x0c, 0x1f); /* ADC Range improvement */
  1569. afe_write(sd, 0x12, 0x63); /* ADC Range improvement */
  1570. /* set to default gain for RGB */
  1571. cp_write(sd, 0x73, 0x10);
  1572. cp_write(sd, 0x74, 0x04);
  1573. cp_write(sd, 0x75, 0x01);
  1574. cp_write(sd, 0x76, 0x00);
  1575. cp_write(sd, 0x3e, 0x04); /* CP core pre-gain control */
  1576. cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
  1577. cp_write(sd, 0x40, 0x5c); /* CP core pre-gain control. Graphics mode */
  1578. break;
  1579. case ADV7842_MODE_HDMI:
  1580. /* Automatic analog input muxing mode */
  1581. afe_write_and_or(sd, 0x02, 0x7f, 0x00);
  1582. /* set mode and select free run resolution */
  1583. if (state->hdmi_port_a)
  1584. hdmi_write(sd, 0x00, 0x02); /* select port A */
  1585. else
  1586. hdmi_write(sd, 0x00, 0x03); /* select port B */
  1587. io_write(sd, 0x00, vid_std_select); /* video std */
  1588. io_write(sd, 0x01, 5); /* prim mode */
  1589. cp_write_and_or(sd, 0x81, 0xef, 0x00); /* disable embedded syncs
  1590. for auto graphics mode */
  1591. /* set ADI recommended settings for HDMI: */
  1592. /* "ADV7842 Register Settings Recommendations
  1593. * (rev. 1.8, November 2010)" p. 3. */
  1594. hdmi_write(sd, 0xc0, 0x00);
  1595. hdmi_write(sd, 0x0d, 0x34); /* ADI recommended write */
  1596. hdmi_write(sd, 0x3d, 0x10); /* ADI recommended write */
  1597. hdmi_write(sd, 0x44, 0x85); /* TMDS PLL optimization */
  1598. hdmi_write(sd, 0x46, 0x1f); /* ADI recommended write */
  1599. hdmi_write(sd, 0x57, 0xb6); /* TMDS PLL optimization */
  1600. hdmi_write(sd, 0x58, 0x03); /* TMDS PLL optimization */
  1601. hdmi_write(sd, 0x60, 0x88); /* TMDS PLL optimization */
  1602. hdmi_write(sd, 0x61, 0x88); /* TMDS PLL optimization */
  1603. hdmi_write(sd, 0x6c, 0x18); /* Disable ISRC clearing bit,
  1604. Improve robustness */
  1605. hdmi_write(sd, 0x75, 0x10); /* DDC drive strength */
  1606. hdmi_write(sd, 0x85, 0x1f); /* equaliser */
  1607. hdmi_write(sd, 0x87, 0x70); /* ADI recommended write */
  1608. hdmi_write(sd, 0x89, 0x04); /* equaliser */
  1609. hdmi_write(sd, 0x8a, 0x1e); /* equaliser */
  1610. hdmi_write(sd, 0x93, 0x04); /* equaliser */
  1611. hdmi_write(sd, 0x94, 0x1e); /* equaliser */
  1612. hdmi_write(sd, 0x99, 0xa1); /* ADI recommended write */
  1613. hdmi_write(sd, 0x9b, 0x09); /* ADI recommended write */
  1614. hdmi_write(sd, 0x9d, 0x02); /* equaliser */
  1615. afe_write(sd, 0x00, 0xff); /* power down ADC */
  1616. afe_write(sd, 0xc8, 0x40); /* phase control */
  1617. /* set to default gain for HDMI */
  1618. cp_write(sd, 0x73, 0x10);
  1619. cp_write(sd, 0x74, 0x04);
  1620. cp_write(sd, 0x75, 0x01);
  1621. cp_write(sd, 0x76, 0x00);
  1622. /* reset ADI recommended settings for digitizer */
  1623. /* "ADV7842 Register Settings Recommendations
  1624. * (rev. 2.5, June 2010)" p. 17. */
  1625. afe_write(sd, 0x12, 0xfb); /* ADC noise shaping filter controls */
  1626. afe_write(sd, 0x0c, 0x0d); /* CP core gain controls */
  1627. cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
  1628. /* CP coast control */
  1629. cp_write(sd, 0xc3, 0x33); /* Component mode */
  1630. /* color space conversion, autodetect color space */
  1631. io_write_and_or(sd, 0x02, 0x0f, 0xf0);
  1632. break;
  1633. default:
  1634. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  1635. __func__, state->mode);
  1636. break;
  1637. }
  1638. }
  1639. static int adv7842_s_routing(struct v4l2_subdev *sd,
  1640. u32 input, u32 output, u32 config)
  1641. {
  1642. struct adv7842_state *state = to_state(sd);
  1643. v4l2_dbg(2, debug, sd, "%s: input %d\n", __func__, input);
  1644. switch (input) {
  1645. case ADV7842_SELECT_HDMI_PORT_A:
  1646. state->mode = ADV7842_MODE_HDMI;
  1647. state->vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P;
  1648. state->hdmi_port_a = true;
  1649. break;
  1650. case ADV7842_SELECT_HDMI_PORT_B:
  1651. state->mode = ADV7842_MODE_HDMI;
  1652. state->vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P;
  1653. state->hdmi_port_a = false;
  1654. break;
  1655. case ADV7842_SELECT_VGA_COMP:
  1656. state->mode = ADV7842_MODE_COMP;
  1657. state->vid_std_select = ADV7842_RGB_VID_STD_AUTO_GRAPH_MODE;
  1658. break;
  1659. case ADV7842_SELECT_VGA_RGB:
  1660. state->mode = ADV7842_MODE_RGB;
  1661. state->vid_std_select = ADV7842_RGB_VID_STD_AUTO_GRAPH_MODE;
  1662. break;
  1663. case ADV7842_SELECT_SDP_CVBS:
  1664. state->mode = ADV7842_MODE_SDP;
  1665. state->vid_std_select = ADV7842_SDP_VID_STD_CVBS_SD_4x1;
  1666. break;
  1667. case ADV7842_SELECT_SDP_YC:
  1668. state->mode = ADV7842_MODE_SDP;
  1669. state->vid_std_select = ADV7842_SDP_VID_STD_YC_SD4_x1;
  1670. break;
  1671. default:
  1672. return -EINVAL;
  1673. }
  1674. disable_input(sd);
  1675. select_input(sd, state->vid_std_select);
  1676. enable_input(sd);
  1677. v4l2_subdev_notify_event(sd, &adv7842_ev_fmt);
  1678. return 0;
  1679. }
  1680. static int adv7842_enum_mbus_code(struct v4l2_subdev *sd,
  1681. struct v4l2_subdev_pad_config *cfg,
  1682. struct v4l2_subdev_mbus_code_enum *code)
  1683. {
  1684. if (code->index >= ARRAY_SIZE(adv7842_formats))
  1685. return -EINVAL;
  1686. code->code = adv7842_formats[code->index].code;
  1687. return 0;
  1688. }
  1689. static void adv7842_fill_format(struct adv7842_state *state,
  1690. struct v4l2_mbus_framefmt *format)
  1691. {
  1692. memset(format, 0, sizeof(*format));
  1693. format->width = state->timings.bt.width;
  1694. format->height = state->timings.bt.height;
  1695. format->field = V4L2_FIELD_NONE;
  1696. format->colorspace = V4L2_COLORSPACE_SRGB;
  1697. if (state->timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO)
  1698. format->colorspace = (state->timings.bt.height <= 576) ?
  1699. V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
  1700. }
  1701. /*
  1702. * Compute the op_ch_sel value required to obtain on the bus the component order
  1703. * corresponding to the selected format taking into account bus reordering
  1704. * applied by the board at the output of the device.
  1705. *
  1706. * The following table gives the op_ch_value from the format component order
  1707. * (expressed as op_ch_sel value in column) and the bus reordering (expressed as
  1708. * adv7842_bus_order value in row).
  1709. *
  1710. * | GBR(0) GRB(1) BGR(2) RGB(3) BRG(4) RBG(5)
  1711. * ----------+-------------------------------------------------
  1712. * RGB (NOP) | GBR GRB BGR RGB BRG RBG
  1713. * GRB (1-2) | BGR RGB GBR GRB RBG BRG
  1714. * RBG (2-3) | GRB GBR BRG RBG BGR RGB
  1715. * BGR (1-3) | RBG BRG RGB BGR GRB GBR
  1716. * BRG (ROR) | BRG RBG GRB GBR RGB BGR
  1717. * GBR (ROL) | RGB BGR RBG BRG GBR GRB
  1718. */
  1719. static unsigned int adv7842_op_ch_sel(struct adv7842_state *state)
  1720. {
  1721. #define _SEL(a, b, c, d, e, f) { \
  1722. ADV7842_OP_CH_SEL_##a, ADV7842_OP_CH_SEL_##b, ADV7842_OP_CH_SEL_##c, \
  1723. ADV7842_OP_CH_SEL_##d, ADV7842_OP_CH_SEL_##e, ADV7842_OP_CH_SEL_##f }
  1724. #define _BUS(x) [ADV7842_BUS_ORDER_##x]
  1725. static const unsigned int op_ch_sel[6][6] = {
  1726. _BUS(RGB) /* NOP */ = _SEL(GBR, GRB, BGR, RGB, BRG, RBG),
  1727. _BUS(GRB) /* 1-2 */ = _SEL(BGR, RGB, GBR, GRB, RBG, BRG),
  1728. _BUS(RBG) /* 2-3 */ = _SEL(GRB, GBR, BRG, RBG, BGR, RGB),
  1729. _BUS(BGR) /* 1-3 */ = _SEL(RBG, BRG, RGB, BGR, GRB, GBR),
  1730. _BUS(BRG) /* ROR */ = _SEL(BRG, RBG, GRB, GBR, RGB, BGR),
  1731. _BUS(GBR) /* ROL */ = _SEL(RGB, BGR, RBG, BRG, GBR, GRB),
  1732. };
  1733. return op_ch_sel[state->pdata.bus_order][state->format->op_ch_sel >> 5];
  1734. }
  1735. static void adv7842_setup_format(struct adv7842_state *state)
  1736. {
  1737. struct v4l2_subdev *sd = &state->sd;
  1738. io_write_clr_set(sd, 0x02, 0x02,
  1739. state->format->rgb_out ? ADV7842_RGB_OUT : 0);
  1740. io_write(sd, 0x03, state->format->op_format_sel |
  1741. state->pdata.op_format_mode_sel);
  1742. io_write_clr_set(sd, 0x04, 0xe0, adv7842_op_ch_sel(state));
  1743. io_write_clr_set(sd, 0x05, 0x01,
  1744. state->format->swap_cb_cr ? ADV7842_OP_SWAP_CB_CR : 0);
  1745. set_rgb_quantization_range(sd);
  1746. }
  1747. static int adv7842_get_format(struct v4l2_subdev *sd,
  1748. struct v4l2_subdev_pad_config *cfg,
  1749. struct v4l2_subdev_format *format)
  1750. {
  1751. struct adv7842_state *state = to_state(sd);
  1752. if (format->pad != ADV7842_PAD_SOURCE)
  1753. return -EINVAL;
  1754. if (state->mode == ADV7842_MODE_SDP) {
  1755. /* SPD block */
  1756. if (!(sdp_read(sd, 0x5a) & 0x01))
  1757. return -EINVAL;
  1758. format->format.code = MEDIA_BUS_FMT_YUYV8_2X8;
  1759. format->format.width = 720;
  1760. /* valid signal */
  1761. if (state->norm & V4L2_STD_525_60)
  1762. format->format.height = 480;
  1763. else
  1764. format->format.height = 576;
  1765. format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1766. return 0;
  1767. }
  1768. adv7842_fill_format(state, &format->format);
  1769. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1770. struct v4l2_mbus_framefmt *fmt;
  1771. fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
  1772. format->format.code = fmt->code;
  1773. } else {
  1774. format->format.code = state->format->code;
  1775. }
  1776. return 0;
  1777. }
  1778. static int adv7842_set_format(struct v4l2_subdev *sd,
  1779. struct v4l2_subdev_pad_config *cfg,
  1780. struct v4l2_subdev_format *format)
  1781. {
  1782. struct adv7842_state *state = to_state(sd);
  1783. const struct adv7842_format_info *info;
  1784. if (format->pad != ADV7842_PAD_SOURCE)
  1785. return -EINVAL;
  1786. if (state->mode == ADV7842_MODE_SDP)
  1787. return adv7842_get_format(sd, cfg, format);
  1788. info = adv7842_format_info(state, format->format.code);
  1789. if (info == NULL)
  1790. info = adv7842_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
  1791. adv7842_fill_format(state, &format->format);
  1792. format->format.code = info->code;
  1793. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1794. struct v4l2_mbus_framefmt *fmt;
  1795. fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
  1796. fmt->code = format->format.code;
  1797. } else {
  1798. state->format = info;
  1799. adv7842_setup_format(state);
  1800. }
  1801. return 0;
  1802. }
  1803. static void adv7842_irq_enable(struct v4l2_subdev *sd, bool enable)
  1804. {
  1805. if (enable) {
  1806. /* Enable SSPD, STDI and CP locked/unlocked interrupts */
  1807. io_write(sd, 0x46, 0x9c);
  1808. /* ESDP_50HZ_DET interrupt */
  1809. io_write(sd, 0x5a, 0x10);
  1810. /* Enable CABLE_DET_A/B_ST (+5v) interrupt */
  1811. io_write(sd, 0x73, 0x03);
  1812. /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
  1813. io_write(sd, 0x78, 0x03);
  1814. /* Enable SDP Standard Detection Change and SDP Video Detected */
  1815. io_write(sd, 0xa0, 0x09);
  1816. /* Enable HDMI_MODE interrupt */
  1817. io_write(sd, 0x69, 0x08);
  1818. } else {
  1819. io_write(sd, 0x46, 0x0);
  1820. io_write(sd, 0x5a, 0x0);
  1821. io_write(sd, 0x73, 0x0);
  1822. io_write(sd, 0x78, 0x0);
  1823. io_write(sd, 0xa0, 0x0);
  1824. io_write(sd, 0x69, 0x0);
  1825. }
  1826. }
  1827. #if IS_ENABLED(CONFIG_VIDEO_ADV7842_CEC)
  1828. static void adv7842_cec_tx_raw_status(struct v4l2_subdev *sd, u8 tx_raw_status)
  1829. {
  1830. struct adv7842_state *state = to_state(sd);
  1831. if ((cec_read(sd, 0x11) & 0x01) == 0) {
  1832. v4l2_dbg(1, debug, sd, "%s: tx raw: tx disabled\n", __func__);
  1833. return;
  1834. }
  1835. if (tx_raw_status & 0x02) {
  1836. v4l2_dbg(1, debug, sd, "%s: tx raw: arbitration lost\n",
  1837. __func__);
  1838. cec_transmit_done(state->cec_adap, CEC_TX_STATUS_ARB_LOST,
  1839. 1, 0, 0, 0);
  1840. return;
  1841. }
  1842. if (tx_raw_status & 0x04) {
  1843. u8 status;
  1844. u8 nack_cnt;
  1845. u8 low_drive_cnt;
  1846. v4l2_dbg(1, debug, sd, "%s: tx raw: retry failed\n", __func__);
  1847. /*
  1848. * We set this status bit since this hardware performs
  1849. * retransmissions.
  1850. */
  1851. status = CEC_TX_STATUS_MAX_RETRIES;
  1852. nack_cnt = cec_read(sd, 0x14) & 0xf;
  1853. if (nack_cnt)
  1854. status |= CEC_TX_STATUS_NACK;
  1855. low_drive_cnt = cec_read(sd, 0x14) >> 4;
  1856. if (low_drive_cnt)
  1857. status |= CEC_TX_STATUS_LOW_DRIVE;
  1858. cec_transmit_done(state->cec_adap, status,
  1859. 0, nack_cnt, low_drive_cnt, 0);
  1860. return;
  1861. }
  1862. if (tx_raw_status & 0x01) {
  1863. v4l2_dbg(1, debug, sd, "%s: tx raw: ready ok\n", __func__);
  1864. cec_transmit_done(state->cec_adap, CEC_TX_STATUS_OK, 0, 0, 0, 0);
  1865. return;
  1866. }
  1867. }
  1868. static void adv7842_cec_isr(struct v4l2_subdev *sd, bool *handled)
  1869. {
  1870. u8 cec_irq;
  1871. /* cec controller */
  1872. cec_irq = io_read(sd, 0x93) & 0x0f;
  1873. if (!cec_irq)
  1874. return;
  1875. v4l2_dbg(1, debug, sd, "%s: cec: irq 0x%x\n", __func__, cec_irq);
  1876. adv7842_cec_tx_raw_status(sd, cec_irq);
  1877. if (cec_irq & 0x08) {
  1878. struct adv7842_state *state = to_state(sd);
  1879. struct cec_msg msg;
  1880. msg.len = cec_read(sd, 0x25) & 0x1f;
  1881. if (msg.len > 16)
  1882. msg.len = 16;
  1883. if (msg.len) {
  1884. u8 i;
  1885. for (i = 0; i < msg.len; i++)
  1886. msg.msg[i] = cec_read(sd, i + 0x15);
  1887. cec_write(sd, 0x26, 0x01); /* re-enable rx */
  1888. cec_received_msg(state->cec_adap, &msg);
  1889. }
  1890. }
  1891. io_write(sd, 0x94, cec_irq);
  1892. if (handled)
  1893. *handled = true;
  1894. }
  1895. static int adv7842_cec_adap_enable(struct cec_adapter *adap, bool enable)
  1896. {
  1897. struct adv7842_state *state = cec_get_drvdata(adap);
  1898. struct v4l2_subdev *sd = &state->sd;
  1899. if (!state->cec_enabled_adap && enable) {
  1900. cec_write_clr_set(sd, 0x2a, 0x01, 0x01); /* power up cec */
  1901. cec_write(sd, 0x2c, 0x01); /* cec soft reset */
  1902. cec_write_clr_set(sd, 0x11, 0x01, 0); /* initially disable tx */
  1903. /* enabled irqs: */
  1904. /* tx: ready */
  1905. /* tx: arbitration lost */
  1906. /* tx: retry timeout */
  1907. /* rx: ready */
  1908. io_write_clr_set(sd, 0x96, 0x0f, 0x0f);
  1909. cec_write(sd, 0x26, 0x01); /* enable rx */
  1910. } else if (state->cec_enabled_adap && !enable) {
  1911. /* disable cec interrupts */
  1912. io_write_clr_set(sd, 0x96, 0x0f, 0x00);
  1913. /* disable address mask 1-3 */
  1914. cec_write_clr_set(sd, 0x27, 0x70, 0x00);
  1915. /* power down cec section */
  1916. cec_write_clr_set(sd, 0x2a, 0x01, 0x00);
  1917. state->cec_valid_addrs = 0;
  1918. }
  1919. state->cec_enabled_adap = enable;
  1920. return 0;
  1921. }
  1922. static int adv7842_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
  1923. {
  1924. struct adv7842_state *state = cec_get_drvdata(adap);
  1925. struct v4l2_subdev *sd = &state->sd;
  1926. unsigned int i, free_idx = ADV7842_MAX_ADDRS;
  1927. if (!state->cec_enabled_adap)
  1928. return addr == CEC_LOG_ADDR_INVALID ? 0 : -EIO;
  1929. if (addr == CEC_LOG_ADDR_INVALID) {
  1930. cec_write_clr_set(sd, 0x27, 0x70, 0);
  1931. state->cec_valid_addrs = 0;
  1932. return 0;
  1933. }
  1934. for (i = 0; i < ADV7842_MAX_ADDRS; i++) {
  1935. bool is_valid = state->cec_valid_addrs & (1 << i);
  1936. if (free_idx == ADV7842_MAX_ADDRS && !is_valid)
  1937. free_idx = i;
  1938. if (is_valid && state->cec_addr[i] == addr)
  1939. return 0;
  1940. }
  1941. if (i == ADV7842_MAX_ADDRS) {
  1942. i = free_idx;
  1943. if (i == ADV7842_MAX_ADDRS)
  1944. return -ENXIO;
  1945. }
  1946. state->cec_addr[i] = addr;
  1947. state->cec_valid_addrs |= 1 << i;
  1948. switch (i) {
  1949. case 0:
  1950. /* enable address mask 0 */
  1951. cec_write_clr_set(sd, 0x27, 0x10, 0x10);
  1952. /* set address for mask 0 */
  1953. cec_write_clr_set(sd, 0x28, 0x0f, addr);
  1954. break;
  1955. case 1:
  1956. /* enable address mask 1 */
  1957. cec_write_clr_set(sd, 0x27, 0x20, 0x20);
  1958. /* set address for mask 1 */
  1959. cec_write_clr_set(sd, 0x28, 0xf0, addr << 4);
  1960. break;
  1961. case 2:
  1962. /* enable address mask 2 */
  1963. cec_write_clr_set(sd, 0x27, 0x40, 0x40);
  1964. /* set address for mask 1 */
  1965. cec_write_clr_set(sd, 0x29, 0x0f, addr);
  1966. break;
  1967. }
  1968. return 0;
  1969. }
  1970. static int adv7842_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  1971. u32 signal_free_time, struct cec_msg *msg)
  1972. {
  1973. struct adv7842_state *state = cec_get_drvdata(adap);
  1974. struct v4l2_subdev *sd = &state->sd;
  1975. u8 len = msg->len;
  1976. unsigned int i;
  1977. /*
  1978. * The number of retries is the number of attempts - 1, but retry
  1979. * at least once. It's not clear if a value of 0 is allowed, so
  1980. * let's do at least one retry.
  1981. */
  1982. cec_write_clr_set(sd, 0x12, 0x70, max(1, attempts - 1) << 4);
  1983. if (len > 16) {
  1984. v4l2_err(sd, "%s: len exceeded 16 (%d)\n", __func__, len);
  1985. return -EINVAL;
  1986. }
  1987. /* write data */
  1988. for (i = 0; i < len; i++)
  1989. cec_write(sd, i, msg->msg[i]);
  1990. /* set length (data + header) */
  1991. cec_write(sd, 0x10, len);
  1992. /* start transmit, enable tx */
  1993. cec_write(sd, 0x11, 0x01);
  1994. return 0;
  1995. }
  1996. static const struct cec_adap_ops adv7842_cec_adap_ops = {
  1997. .adap_enable = adv7842_cec_adap_enable,
  1998. .adap_log_addr = adv7842_cec_adap_log_addr,
  1999. .adap_transmit = adv7842_cec_adap_transmit,
  2000. };
  2001. #endif
  2002. static int adv7842_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  2003. {
  2004. struct adv7842_state *state = to_state(sd);
  2005. u8 fmt_change_cp, fmt_change_digital, fmt_change_sdp;
  2006. u8 irq_status[6];
  2007. adv7842_irq_enable(sd, false);
  2008. /* read status */
  2009. irq_status[0] = io_read(sd, 0x43);
  2010. irq_status[1] = io_read(sd, 0x57);
  2011. irq_status[2] = io_read(sd, 0x70);
  2012. irq_status[3] = io_read(sd, 0x75);
  2013. irq_status[4] = io_read(sd, 0x9d);
  2014. irq_status[5] = io_read(sd, 0x66);
  2015. /* and clear */
  2016. if (irq_status[0])
  2017. io_write(sd, 0x44, irq_status[0]);
  2018. if (irq_status[1])
  2019. io_write(sd, 0x58, irq_status[1]);
  2020. if (irq_status[2])
  2021. io_write(sd, 0x71, irq_status[2]);
  2022. if (irq_status[3])
  2023. io_write(sd, 0x76, irq_status[3]);
  2024. if (irq_status[4])
  2025. io_write(sd, 0x9e, irq_status[4]);
  2026. if (irq_status[5])
  2027. io_write(sd, 0x67, irq_status[5]);
  2028. adv7842_irq_enable(sd, true);
  2029. v4l2_dbg(1, debug, sd, "%s: irq %x, %x, %x, %x, %x, %x\n", __func__,
  2030. irq_status[0], irq_status[1], irq_status[2],
  2031. irq_status[3], irq_status[4], irq_status[5]);
  2032. /* format change CP */
  2033. fmt_change_cp = irq_status[0] & 0x9c;
  2034. /* format change SDP */
  2035. if (state->mode == ADV7842_MODE_SDP)
  2036. fmt_change_sdp = (irq_status[1] & 0x30) | (irq_status[4] & 0x09);
  2037. else
  2038. fmt_change_sdp = 0;
  2039. /* digital format CP */
  2040. if (is_digital_input(sd))
  2041. fmt_change_digital = irq_status[3] & 0x03;
  2042. else
  2043. fmt_change_digital = 0;
  2044. /* format change */
  2045. if (fmt_change_cp || fmt_change_digital || fmt_change_sdp) {
  2046. v4l2_dbg(1, debug, sd,
  2047. "%s: fmt_change_cp = 0x%x, fmt_change_digital = 0x%x, fmt_change_sdp = 0x%x\n",
  2048. __func__, fmt_change_cp, fmt_change_digital,
  2049. fmt_change_sdp);
  2050. v4l2_subdev_notify_event(sd, &adv7842_ev_fmt);
  2051. if (handled)
  2052. *handled = true;
  2053. }
  2054. /* HDMI/DVI mode */
  2055. if (irq_status[5] & 0x08) {
  2056. v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
  2057. (io_read(sd, 0x65) & 0x08) ? "HDMI" : "DVI");
  2058. set_rgb_quantization_range(sd);
  2059. if (handled)
  2060. *handled = true;
  2061. }
  2062. #if IS_ENABLED(CONFIG_VIDEO_ADV7842_CEC)
  2063. /* cec */
  2064. adv7842_cec_isr(sd, handled);
  2065. #endif
  2066. /* tx 5v detect */
  2067. if (irq_status[2] & 0x3) {
  2068. v4l2_dbg(1, debug, sd, "%s: irq tx_5v\n", __func__);
  2069. adv7842_s_detect_tx_5v_ctrl(sd);
  2070. if (handled)
  2071. *handled = true;
  2072. }
  2073. return 0;
  2074. }
  2075. static int adv7842_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  2076. {
  2077. struct adv7842_state *state = to_state(sd);
  2078. u8 *data = NULL;
  2079. memset(edid->reserved, 0, sizeof(edid->reserved));
  2080. switch (edid->pad) {
  2081. case ADV7842_EDID_PORT_A:
  2082. case ADV7842_EDID_PORT_B:
  2083. if (state->hdmi_edid.present & (0x04 << edid->pad))
  2084. data = state->hdmi_edid.edid;
  2085. break;
  2086. case ADV7842_EDID_PORT_VGA:
  2087. if (state->vga_edid.present)
  2088. data = state->vga_edid.edid;
  2089. break;
  2090. default:
  2091. return -EINVAL;
  2092. }
  2093. if (edid->start_block == 0 && edid->blocks == 0) {
  2094. edid->blocks = data ? 2 : 0;
  2095. return 0;
  2096. }
  2097. if (!data)
  2098. return -ENODATA;
  2099. if (edid->start_block >= 2)
  2100. return -EINVAL;
  2101. if (edid->start_block + edid->blocks > 2)
  2102. edid->blocks = 2 - edid->start_block;
  2103. memcpy(edid->edid, data + edid->start_block * 128, edid->blocks * 128);
  2104. return 0;
  2105. }
  2106. static int adv7842_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *e)
  2107. {
  2108. struct adv7842_state *state = to_state(sd);
  2109. int err = 0;
  2110. memset(e->reserved, 0, sizeof(e->reserved));
  2111. if (e->pad > ADV7842_EDID_PORT_VGA)
  2112. return -EINVAL;
  2113. if (e->start_block != 0)
  2114. return -EINVAL;
  2115. if (e->blocks > 2) {
  2116. e->blocks = 2;
  2117. return -E2BIG;
  2118. }
  2119. /* todo, per edid */
  2120. state->aspect_ratio = v4l2_calc_aspect_ratio(e->edid[0x15],
  2121. e->edid[0x16]);
  2122. switch (e->pad) {
  2123. case ADV7842_EDID_PORT_VGA:
  2124. memset(&state->vga_edid.edid, 0, 256);
  2125. state->vga_edid.present = e->blocks ? 0x1 : 0x0;
  2126. memcpy(&state->vga_edid.edid, e->edid, 128 * e->blocks);
  2127. err = edid_write_vga_segment(sd);
  2128. break;
  2129. case ADV7842_EDID_PORT_A:
  2130. case ADV7842_EDID_PORT_B:
  2131. memset(&state->hdmi_edid.edid, 0, 256);
  2132. if (e->blocks) {
  2133. state->hdmi_edid.present |= 0x04 << e->pad;
  2134. } else {
  2135. state->hdmi_edid.present &= ~(0x04 << e->pad);
  2136. adv7842_s_detect_tx_5v_ctrl(sd);
  2137. }
  2138. memcpy(&state->hdmi_edid.edid, e->edid, 128 * e->blocks);
  2139. err = edid_write_hdmi_segment(sd, e->pad);
  2140. break;
  2141. default:
  2142. return -EINVAL;
  2143. }
  2144. if (err < 0)
  2145. v4l2_err(sd, "error %d writing edid on port %d\n", err, e->pad);
  2146. return err;
  2147. }
  2148. struct adv7842_cfg_read_infoframe {
  2149. const char *desc;
  2150. u8 present_mask;
  2151. u8 head_addr;
  2152. u8 payload_addr;
  2153. };
  2154. static void log_infoframe(struct v4l2_subdev *sd, struct adv7842_cfg_read_infoframe *cri)
  2155. {
  2156. int i;
  2157. u8 buffer[32];
  2158. union hdmi_infoframe frame;
  2159. u8 len;
  2160. struct i2c_client *client = v4l2_get_subdevdata(sd);
  2161. struct device *dev = &client->dev;
  2162. if (!(io_read(sd, 0x60) & cri->present_mask)) {
  2163. v4l2_info(sd, "%s infoframe not received\n", cri->desc);
  2164. return;
  2165. }
  2166. for (i = 0; i < 3; i++)
  2167. buffer[i] = infoframe_read(sd, cri->head_addr + i);
  2168. len = buffer[2] + 1;
  2169. if (len + 3 > sizeof(buffer)) {
  2170. v4l2_err(sd, "%s: invalid %s infoframe length %d\n", __func__, cri->desc, len);
  2171. return;
  2172. }
  2173. for (i = 0; i < len; i++)
  2174. buffer[i + 3] = infoframe_read(sd, cri->payload_addr + i);
  2175. if (hdmi_infoframe_unpack(&frame, buffer) < 0) {
  2176. v4l2_err(sd, "%s: unpack of %s infoframe failed\n", __func__, cri->desc);
  2177. return;
  2178. }
  2179. hdmi_infoframe_log(KERN_INFO, dev, &frame);
  2180. }
  2181. static void adv7842_log_infoframes(struct v4l2_subdev *sd)
  2182. {
  2183. int i;
  2184. struct adv7842_cfg_read_infoframe cri[] = {
  2185. { "AVI", 0x01, 0xe0, 0x00 },
  2186. { "Audio", 0x02, 0xe3, 0x1c },
  2187. { "SDP", 0x04, 0xe6, 0x2a },
  2188. { "Vendor", 0x10, 0xec, 0x54 }
  2189. };
  2190. if (!(hdmi_read(sd, 0x05) & 0x80)) {
  2191. v4l2_info(sd, "receive DVI-D signal, no infoframes\n");
  2192. return;
  2193. }
  2194. for (i = 0; i < ARRAY_SIZE(cri); i++)
  2195. log_infoframe(sd, &cri[i]);
  2196. }
  2197. #if 0
  2198. /* Let's keep it here for now, as it could be useful for debug */
  2199. static const char * const prim_mode_txt[] = {
  2200. "SDP",
  2201. "Component",
  2202. "Graphics",
  2203. "Reserved",
  2204. "CVBS & HDMI AUDIO",
  2205. "HDMI-Comp",
  2206. "HDMI-GR",
  2207. "Reserved",
  2208. "Reserved",
  2209. "Reserved",
  2210. "Reserved",
  2211. "Reserved",
  2212. "Reserved",
  2213. "Reserved",
  2214. "Reserved",
  2215. "Reserved",
  2216. };
  2217. #endif
  2218. static int adv7842_sdp_log_status(struct v4l2_subdev *sd)
  2219. {
  2220. /* SDP (Standard definition processor) block */
  2221. u8 sdp_signal_detected = sdp_read(sd, 0x5A) & 0x01;
  2222. v4l2_info(sd, "Chip powered %s\n", no_power(sd) ? "off" : "on");
  2223. v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x\n",
  2224. io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f);
  2225. v4l2_info(sd, "SDP: free run: %s\n",
  2226. (sdp_read(sd, 0x56) & 0x01) ? "on" : "off");
  2227. v4l2_info(sd, "SDP: %s\n", sdp_signal_detected ?
  2228. "valid SD/PR signal detected" : "invalid/no signal");
  2229. if (sdp_signal_detected) {
  2230. static const char * const sdp_std_txt[] = {
  2231. "NTSC-M/J",
  2232. "1?",
  2233. "NTSC-443",
  2234. "60HzSECAM",
  2235. "PAL-M",
  2236. "5?",
  2237. "PAL-60",
  2238. "7?", "8?", "9?", "a?", "b?",
  2239. "PAL-CombN",
  2240. "d?",
  2241. "PAL-BGHID",
  2242. "SECAM"
  2243. };
  2244. v4l2_info(sd, "SDP: standard %s\n",
  2245. sdp_std_txt[sdp_read(sd, 0x52) & 0x0f]);
  2246. v4l2_info(sd, "SDP: %s\n",
  2247. (sdp_read(sd, 0x59) & 0x08) ? "50Hz" : "60Hz");
  2248. v4l2_info(sd, "SDP: %s\n",
  2249. (sdp_read(sd, 0x57) & 0x08) ? "Interlaced" : "Progressive");
  2250. v4l2_info(sd, "SDP: deinterlacer %s\n",
  2251. (sdp_read(sd, 0x12) & 0x08) ? "enabled" : "disabled");
  2252. v4l2_info(sd, "SDP: csc %s mode\n",
  2253. (sdp_io_read(sd, 0xe0) & 0x40) ? "auto" : "manual");
  2254. }
  2255. return 0;
  2256. }
  2257. static int adv7842_cp_log_status(struct v4l2_subdev *sd)
  2258. {
  2259. /* CP block */
  2260. struct adv7842_state *state = to_state(sd);
  2261. struct v4l2_dv_timings timings;
  2262. u8 reg_io_0x02 = io_read(sd, 0x02);
  2263. u8 reg_io_0x21 = io_read(sd, 0x21);
  2264. u8 reg_rep_0x77 = rep_read(sd, 0x77);
  2265. u8 reg_rep_0x7d = rep_read(sd, 0x7d);
  2266. bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
  2267. bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
  2268. bool audio_mute = io_read(sd, 0x65) & 0x40;
  2269. static const char * const csc_coeff_sel_rb[16] = {
  2270. "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
  2271. "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
  2272. "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
  2273. "reserved", "reserved", "reserved", "reserved", "manual"
  2274. };
  2275. static const char * const input_color_space_txt[16] = {
  2276. "RGB limited range (16-235)", "RGB full range (0-255)",
  2277. "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
  2278. "xvYCC Bt.601", "xvYCC Bt.709",
  2279. "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
  2280. "invalid", "invalid", "invalid", "invalid", "invalid",
  2281. "invalid", "invalid", "automatic"
  2282. };
  2283. static const char * const rgb_quantization_range_txt[] = {
  2284. "Automatic",
  2285. "RGB limited range (16-235)",
  2286. "RGB full range (0-255)",
  2287. };
  2288. static const char * const deep_color_mode_txt[4] = {
  2289. "8-bits per channel",
  2290. "10-bits per channel",
  2291. "12-bits per channel",
  2292. "16-bits per channel (not supported)"
  2293. };
  2294. v4l2_info(sd, "-----Chip status-----\n");
  2295. v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
  2296. v4l2_info(sd, "HDMI/DVI-D port selected: %s\n",
  2297. state->hdmi_port_a ? "A" : "B");
  2298. v4l2_info(sd, "EDID A %s, B %s\n",
  2299. ((reg_rep_0x7d & 0x04) && (reg_rep_0x77 & 0x04)) ?
  2300. "enabled" : "disabled",
  2301. ((reg_rep_0x7d & 0x08) && (reg_rep_0x77 & 0x08)) ?
  2302. "enabled" : "disabled");
  2303. v4l2_info(sd, "HPD A %s, B %s\n",
  2304. reg_io_0x21 & 0x02 ? "enabled" : "disabled",
  2305. reg_io_0x21 & 0x01 ? "enabled" : "disabled");
  2306. v4l2_info(sd, "CEC: %s\n", state->cec_enabled_adap ?
  2307. "enabled" : "disabled");
  2308. if (state->cec_enabled_adap) {
  2309. int i;
  2310. for (i = 0; i < ADV7842_MAX_ADDRS; i++) {
  2311. bool is_valid = state->cec_valid_addrs & (1 << i);
  2312. if (is_valid)
  2313. v4l2_info(sd, "CEC Logical Address: 0x%x\n",
  2314. state->cec_addr[i]);
  2315. }
  2316. }
  2317. v4l2_info(sd, "-----Signal status-----\n");
  2318. if (state->hdmi_port_a) {
  2319. v4l2_info(sd, "Cable detected (+5V power): %s\n",
  2320. io_read(sd, 0x6f) & 0x02 ? "true" : "false");
  2321. v4l2_info(sd, "TMDS signal detected: %s\n",
  2322. (io_read(sd, 0x6a) & 0x02) ? "true" : "false");
  2323. v4l2_info(sd, "TMDS signal locked: %s\n",
  2324. (io_read(sd, 0x6a) & 0x20) ? "true" : "false");
  2325. } else {
  2326. v4l2_info(sd, "Cable detected (+5V power):%s\n",
  2327. io_read(sd, 0x6f) & 0x01 ? "true" : "false");
  2328. v4l2_info(sd, "TMDS signal detected: %s\n",
  2329. (io_read(sd, 0x6a) & 0x01) ? "true" : "false");
  2330. v4l2_info(sd, "TMDS signal locked: %s\n",
  2331. (io_read(sd, 0x6a) & 0x10) ? "true" : "false");
  2332. }
  2333. v4l2_info(sd, "CP free run: %s\n",
  2334. (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
  2335. v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
  2336. io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
  2337. (io_read(sd, 0x01) & 0x70) >> 4);
  2338. v4l2_info(sd, "-----Video Timings-----\n");
  2339. if (no_cp_signal(sd)) {
  2340. v4l2_info(sd, "STDI: not locked\n");
  2341. } else {
  2342. u32 bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
  2343. u32 lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
  2344. u32 lcvs = cp_read(sd, 0xb3) >> 3;
  2345. u32 fcl = ((cp_read(sd, 0xb8) & 0x1f) << 8) | cp_read(sd, 0xb9);
  2346. char hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
  2347. ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
  2348. char vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
  2349. ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
  2350. v4l2_info(sd,
  2351. "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, fcl = %d, %s, %chsync, %cvsync\n",
  2352. lcf, bl, lcvs, fcl,
  2353. (cp_read(sd, 0xb1) & 0x40) ?
  2354. "interlaced" : "progressive",
  2355. hs_pol, vs_pol);
  2356. }
  2357. if (adv7842_query_dv_timings(sd, &timings))
  2358. v4l2_info(sd, "No video detected\n");
  2359. else
  2360. v4l2_print_dv_timings(sd->name, "Detected format: ",
  2361. &timings, true);
  2362. v4l2_print_dv_timings(sd->name, "Configured format: ",
  2363. &state->timings, true);
  2364. if (no_cp_signal(sd))
  2365. return 0;
  2366. v4l2_info(sd, "-----Color space-----\n");
  2367. v4l2_info(sd, "RGB quantization range ctrl: %s\n",
  2368. rgb_quantization_range_txt[state->rgb_quantization_range]);
  2369. v4l2_info(sd, "Input color space: %s\n",
  2370. input_color_space_txt[reg_io_0x02 >> 4]);
  2371. v4l2_info(sd, "Output color space: %s %s, alt-gamma %s\n",
  2372. (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
  2373. (((reg_io_0x02 >> 2) & 0x01) ^ (reg_io_0x02 & 0x01)) ?
  2374. "(16-235)" : "(0-255)",
  2375. (reg_io_0x02 & 0x08) ? "enabled" : "disabled");
  2376. v4l2_info(sd, "Color space conversion: %s\n",
  2377. csc_coeff_sel_rb[cp_read(sd, 0xf4) >> 4]);
  2378. if (!is_digital_input(sd))
  2379. return 0;
  2380. v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
  2381. v4l2_info(sd, "HDCP encrypted content: %s\n",
  2382. (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
  2383. v4l2_info(sd, "HDCP keys read: %s%s\n",
  2384. (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
  2385. (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
  2386. if (!is_hdmi(sd))
  2387. return 0;
  2388. v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
  2389. audio_pll_locked ? "locked" : "not locked",
  2390. audio_sample_packet_detect ? "detected" : "not detected",
  2391. audio_mute ? "muted" : "enabled");
  2392. if (audio_pll_locked && audio_sample_packet_detect) {
  2393. v4l2_info(sd, "Audio format: %s\n",
  2394. (hdmi_read(sd, 0x07) & 0x40) ? "multi-channel" : "stereo");
  2395. }
  2396. v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
  2397. (hdmi_read(sd, 0x5c) << 8) +
  2398. (hdmi_read(sd, 0x5d) & 0xf0));
  2399. v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
  2400. (hdmi_read(sd, 0x5e) << 8) +
  2401. hdmi_read(sd, 0x5f));
  2402. v4l2_info(sd, "AV Mute: %s\n",
  2403. (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
  2404. v4l2_info(sd, "Deep color mode: %s\n",
  2405. deep_color_mode_txt[hdmi_read(sd, 0x0b) >> 6]);
  2406. adv7842_log_infoframes(sd);
  2407. return 0;
  2408. }
  2409. static int adv7842_log_status(struct v4l2_subdev *sd)
  2410. {
  2411. struct adv7842_state *state = to_state(sd);
  2412. if (state->mode == ADV7842_MODE_SDP)
  2413. return adv7842_sdp_log_status(sd);
  2414. return adv7842_cp_log_status(sd);
  2415. }
  2416. static int adv7842_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  2417. {
  2418. struct adv7842_state *state = to_state(sd);
  2419. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  2420. if (state->mode != ADV7842_MODE_SDP)
  2421. return -ENODATA;
  2422. if (!(sdp_read(sd, 0x5A) & 0x01)) {
  2423. *std = 0;
  2424. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  2425. return 0;
  2426. }
  2427. switch (sdp_read(sd, 0x52) & 0x0f) {
  2428. case 0:
  2429. /* NTSC-M/J */
  2430. *std &= V4L2_STD_NTSC;
  2431. break;
  2432. case 2:
  2433. /* NTSC-443 */
  2434. *std &= V4L2_STD_NTSC_443;
  2435. break;
  2436. case 3:
  2437. /* 60HzSECAM */
  2438. *std &= V4L2_STD_SECAM;
  2439. break;
  2440. case 4:
  2441. /* PAL-M */
  2442. *std &= V4L2_STD_PAL_M;
  2443. break;
  2444. case 6:
  2445. /* PAL-60 */
  2446. *std &= V4L2_STD_PAL_60;
  2447. break;
  2448. case 0xc:
  2449. /* PAL-CombN */
  2450. *std &= V4L2_STD_PAL_Nc;
  2451. break;
  2452. case 0xe:
  2453. /* PAL-BGHID */
  2454. *std &= V4L2_STD_PAL;
  2455. break;
  2456. case 0xf:
  2457. /* SECAM */
  2458. *std &= V4L2_STD_SECAM;
  2459. break;
  2460. default:
  2461. *std &= V4L2_STD_ALL;
  2462. break;
  2463. }
  2464. return 0;
  2465. }
  2466. static void adv7842_s_sdp_io(struct v4l2_subdev *sd, struct adv7842_sdp_io_sync_adjustment *s)
  2467. {
  2468. if (s && s->adjust) {
  2469. sdp_io_write(sd, 0x94, (s->hs_beg >> 8) & 0xf);
  2470. sdp_io_write(sd, 0x95, s->hs_beg & 0xff);
  2471. sdp_io_write(sd, 0x96, (s->hs_width >> 8) & 0xf);
  2472. sdp_io_write(sd, 0x97, s->hs_width & 0xff);
  2473. sdp_io_write(sd, 0x98, (s->de_beg >> 8) & 0xf);
  2474. sdp_io_write(sd, 0x99, s->de_beg & 0xff);
  2475. sdp_io_write(sd, 0x9a, (s->de_end >> 8) & 0xf);
  2476. sdp_io_write(sd, 0x9b, s->de_end & 0xff);
  2477. sdp_io_write(sd, 0xa8, s->vs_beg_o);
  2478. sdp_io_write(sd, 0xa9, s->vs_beg_e);
  2479. sdp_io_write(sd, 0xaa, s->vs_end_o);
  2480. sdp_io_write(sd, 0xab, s->vs_end_e);
  2481. sdp_io_write(sd, 0xac, s->de_v_beg_o);
  2482. sdp_io_write(sd, 0xad, s->de_v_beg_e);
  2483. sdp_io_write(sd, 0xae, s->de_v_end_o);
  2484. sdp_io_write(sd, 0xaf, s->de_v_end_e);
  2485. } else {
  2486. /* set to default */
  2487. sdp_io_write(sd, 0x94, 0x00);
  2488. sdp_io_write(sd, 0x95, 0x00);
  2489. sdp_io_write(sd, 0x96, 0x00);
  2490. sdp_io_write(sd, 0x97, 0x20);
  2491. sdp_io_write(sd, 0x98, 0x00);
  2492. sdp_io_write(sd, 0x99, 0x00);
  2493. sdp_io_write(sd, 0x9a, 0x00);
  2494. sdp_io_write(sd, 0x9b, 0x00);
  2495. sdp_io_write(sd, 0xa8, 0x04);
  2496. sdp_io_write(sd, 0xa9, 0x04);
  2497. sdp_io_write(sd, 0xaa, 0x04);
  2498. sdp_io_write(sd, 0xab, 0x04);
  2499. sdp_io_write(sd, 0xac, 0x04);
  2500. sdp_io_write(sd, 0xad, 0x04);
  2501. sdp_io_write(sd, 0xae, 0x04);
  2502. sdp_io_write(sd, 0xaf, 0x04);
  2503. }
  2504. }
  2505. static int adv7842_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  2506. {
  2507. struct adv7842_state *state = to_state(sd);
  2508. struct adv7842_platform_data *pdata = &state->pdata;
  2509. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  2510. if (state->mode != ADV7842_MODE_SDP)
  2511. return -ENODATA;
  2512. if (norm & V4L2_STD_625_50)
  2513. adv7842_s_sdp_io(sd, &pdata->sdp_io_sync_625);
  2514. else if (norm & V4L2_STD_525_60)
  2515. adv7842_s_sdp_io(sd, &pdata->sdp_io_sync_525);
  2516. else
  2517. adv7842_s_sdp_io(sd, NULL);
  2518. if (norm & V4L2_STD_ALL) {
  2519. state->norm = norm;
  2520. return 0;
  2521. }
  2522. return -EINVAL;
  2523. }
  2524. static int adv7842_g_std(struct v4l2_subdev *sd, v4l2_std_id *norm)
  2525. {
  2526. struct adv7842_state *state = to_state(sd);
  2527. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  2528. if (state->mode != ADV7842_MODE_SDP)
  2529. return -ENODATA;
  2530. *norm = state->norm;
  2531. return 0;
  2532. }
  2533. /* ----------------------------------------------------------------------- */
  2534. static int adv7842_core_init(struct v4l2_subdev *sd)
  2535. {
  2536. struct adv7842_state *state = to_state(sd);
  2537. struct adv7842_platform_data *pdata = &state->pdata;
  2538. hdmi_write(sd, 0x48,
  2539. (pdata->disable_pwrdnb ? 0x80 : 0) |
  2540. (pdata->disable_cable_det_rst ? 0x40 : 0));
  2541. disable_input(sd);
  2542. /*
  2543. * Disable I2C access to internal EDID ram from HDMI DDC ports
  2544. * Disable auto edid enable when leaving powerdown mode
  2545. */
  2546. rep_write_and_or(sd, 0x77, 0xd3, 0x20);
  2547. /* power */
  2548. io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
  2549. io_write(sd, 0x15, 0x80); /* Power up pads */
  2550. /* video format */
  2551. io_write(sd, 0x02, 0xf0 | pdata->alt_gamma << 3);
  2552. io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
  2553. pdata->insert_av_codes << 2 |
  2554. pdata->replicate_av_codes << 1);
  2555. adv7842_setup_format(state);
  2556. /* HDMI audio */
  2557. hdmi_write_and_or(sd, 0x1a, 0xf1, 0x08); /* Wait 1 s before unmute */
  2558. /* Drive strength */
  2559. io_write_and_or(sd, 0x14, 0xc0,
  2560. pdata->dr_str_data << 4 |
  2561. pdata->dr_str_clk << 2 |
  2562. pdata->dr_str_sync);
  2563. /* HDMI free run */
  2564. cp_write_and_or(sd, 0xba, 0xfc, pdata->hdmi_free_run_enable |
  2565. (pdata->hdmi_free_run_mode << 1));
  2566. /* SPD free run */
  2567. sdp_write_and_or(sd, 0xdd, 0xf0, pdata->sdp_free_run_force |
  2568. (pdata->sdp_free_run_cbar_en << 1) |
  2569. (pdata->sdp_free_run_man_col_en << 2) |
  2570. (pdata->sdp_free_run_auto << 3));
  2571. /* TODO from platform data */
  2572. cp_write(sd, 0x69, 0x14); /* Enable CP CSC */
  2573. io_write(sd, 0x06, 0xa6); /* positive VS and HS and DE */
  2574. cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
  2575. afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
  2576. afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
  2577. io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
  2578. sdp_csc_coeff(sd, &pdata->sdp_csc_coeff);
  2579. /* todo, improve settings for sdram */
  2580. if (pdata->sd_ram_size >= 128) {
  2581. sdp_write(sd, 0x12, 0x0d); /* Frame TBC,3D comb enabled */
  2582. if (pdata->sd_ram_ddr) {
  2583. /* SDP setup for the AD eval board */
  2584. sdp_io_write(sd, 0x6f, 0x00); /* DDR mode */
  2585. sdp_io_write(sd, 0x75, 0x0a); /* 128 MB memory size */
  2586. sdp_io_write(sd, 0x7a, 0xa5); /* Timing Adjustment */
  2587. sdp_io_write(sd, 0x7b, 0x8f); /* Timing Adjustment */
  2588. sdp_io_write(sd, 0x60, 0x01); /* SDRAM reset */
  2589. } else {
  2590. sdp_io_write(sd, 0x75, 0x0a); /* 64 MB memory size ?*/
  2591. sdp_io_write(sd, 0x74, 0x00); /* must be zero for sdr sdram */
  2592. sdp_io_write(sd, 0x79, 0x33); /* CAS latency to 3,
  2593. depends on memory */
  2594. sdp_io_write(sd, 0x6f, 0x01); /* SDR mode */
  2595. sdp_io_write(sd, 0x7a, 0xa5); /* Timing Adjustment */
  2596. sdp_io_write(sd, 0x7b, 0x8f); /* Timing Adjustment */
  2597. sdp_io_write(sd, 0x60, 0x01); /* SDRAM reset */
  2598. }
  2599. } else {
  2600. /*
  2601. * Manual UG-214, rev 0 is bit confusing on this bit
  2602. * but a '1' disables any signal if the Ram is active.
  2603. */
  2604. sdp_io_write(sd, 0x29, 0x10); /* Tristate memory interface */
  2605. }
  2606. select_input(sd, pdata->vid_std_select);
  2607. enable_input(sd);
  2608. if (pdata->hpa_auto) {
  2609. /* HPA auto, HPA 0.5s after Edid set and Cable detect */
  2610. hdmi_write(sd, 0x69, 0x5c);
  2611. } else {
  2612. /* HPA manual */
  2613. hdmi_write(sd, 0x69, 0xa3);
  2614. /* HPA disable on port A and B */
  2615. io_write_and_or(sd, 0x20, 0xcf, 0x00);
  2616. }
  2617. /* LLC */
  2618. io_write(sd, 0x19, 0x80 | pdata->llc_dll_phase);
  2619. io_write(sd, 0x33, 0x40);
  2620. /* interrupts */
  2621. io_write(sd, 0x40, 0xf2); /* Configure INT1 */
  2622. adv7842_irq_enable(sd, true);
  2623. return v4l2_ctrl_handler_setup(sd->ctrl_handler);
  2624. }
  2625. /* ----------------------------------------------------------------------- */
  2626. static int adv7842_ddr_ram_test(struct v4l2_subdev *sd)
  2627. {
  2628. /*
  2629. * From ADV784x external Memory test.pdf
  2630. *
  2631. * Reset must just been performed before running test.
  2632. * Recommended to reset after test.
  2633. */
  2634. int i;
  2635. int pass = 0;
  2636. int fail = 0;
  2637. int complete = 0;
  2638. io_write(sd, 0x00, 0x01); /* Program SDP 4x1 */
  2639. io_write(sd, 0x01, 0x00); /* Program SDP mode */
  2640. afe_write(sd, 0x80, 0x92); /* SDP Recommeneded Write */
  2641. afe_write(sd, 0x9B, 0x01); /* SDP Recommeneded Write ADV7844ES1 */
  2642. afe_write(sd, 0x9C, 0x60); /* SDP Recommeneded Write ADV7844ES1 */
  2643. afe_write(sd, 0x9E, 0x02); /* SDP Recommeneded Write ADV7844ES1 */
  2644. afe_write(sd, 0xA0, 0x0B); /* SDP Recommeneded Write ADV7844ES1 */
  2645. afe_write(sd, 0xC3, 0x02); /* Memory BIST Initialisation */
  2646. io_write(sd, 0x0C, 0x40); /* Power up ADV7844 */
  2647. io_write(sd, 0x15, 0xBA); /* Enable outputs */
  2648. sdp_write(sd, 0x12, 0x00); /* Disable 3D comb, Frame TBC & 3DNR */
  2649. io_write(sd, 0xFF, 0x04); /* Reset memory controller */
  2650. usleep_range(5000, 6000);
  2651. sdp_write(sd, 0x12, 0x00); /* Disable 3D Comb, Frame TBC & 3DNR */
  2652. sdp_io_write(sd, 0x2A, 0x01); /* Memory BIST Initialisation */
  2653. sdp_io_write(sd, 0x7c, 0x19); /* Memory BIST Initialisation */
  2654. sdp_io_write(sd, 0x80, 0x87); /* Memory BIST Initialisation */
  2655. sdp_io_write(sd, 0x81, 0x4a); /* Memory BIST Initialisation */
  2656. sdp_io_write(sd, 0x82, 0x2c); /* Memory BIST Initialisation */
  2657. sdp_io_write(sd, 0x83, 0x0e); /* Memory BIST Initialisation */
  2658. sdp_io_write(sd, 0x84, 0x94); /* Memory BIST Initialisation */
  2659. sdp_io_write(sd, 0x85, 0x62); /* Memory BIST Initialisation */
  2660. sdp_io_write(sd, 0x7d, 0x00); /* Memory BIST Initialisation */
  2661. sdp_io_write(sd, 0x7e, 0x1a); /* Memory BIST Initialisation */
  2662. usleep_range(5000, 6000);
  2663. sdp_io_write(sd, 0xd9, 0xd5); /* Enable BIST Test */
  2664. sdp_write(sd, 0x12, 0x05); /* Enable FRAME TBC & 3D COMB */
  2665. msleep(20);
  2666. for (i = 0; i < 10; i++) {
  2667. u8 result = sdp_io_read(sd, 0xdb);
  2668. if (result & 0x10) {
  2669. complete++;
  2670. if (result & 0x20)
  2671. fail++;
  2672. else
  2673. pass++;
  2674. }
  2675. msleep(20);
  2676. }
  2677. v4l2_dbg(1, debug, sd,
  2678. "Ram Test: completed %d of %d: pass %d, fail %d\n",
  2679. complete, i, pass, fail);
  2680. if (!complete || fail)
  2681. return -EIO;
  2682. return 0;
  2683. }
  2684. static void adv7842_rewrite_i2c_addresses(struct v4l2_subdev *sd,
  2685. struct adv7842_platform_data *pdata)
  2686. {
  2687. io_write(sd, 0xf1, pdata->i2c_sdp << 1);
  2688. io_write(sd, 0xf2, pdata->i2c_sdp_io << 1);
  2689. io_write(sd, 0xf3, pdata->i2c_avlink << 1);
  2690. io_write(sd, 0xf4, pdata->i2c_cec << 1);
  2691. io_write(sd, 0xf5, pdata->i2c_infoframe << 1);
  2692. io_write(sd, 0xf8, pdata->i2c_afe << 1);
  2693. io_write(sd, 0xf9, pdata->i2c_repeater << 1);
  2694. io_write(sd, 0xfa, pdata->i2c_edid << 1);
  2695. io_write(sd, 0xfb, pdata->i2c_hdmi << 1);
  2696. io_write(sd, 0xfd, pdata->i2c_cp << 1);
  2697. io_write(sd, 0xfe, pdata->i2c_vdp << 1);
  2698. }
  2699. static int adv7842_command_ram_test(struct v4l2_subdev *sd)
  2700. {
  2701. struct i2c_client *client = v4l2_get_subdevdata(sd);
  2702. struct adv7842_state *state = to_state(sd);
  2703. struct adv7842_platform_data *pdata = client->dev.platform_data;
  2704. struct v4l2_dv_timings timings;
  2705. int ret = 0;
  2706. if (!pdata)
  2707. return -ENODEV;
  2708. if (!pdata->sd_ram_size || !pdata->sd_ram_ddr) {
  2709. v4l2_info(sd, "no sdram or no ddr sdram\n");
  2710. return -EINVAL;
  2711. }
  2712. main_reset(sd);
  2713. adv7842_rewrite_i2c_addresses(sd, pdata);
  2714. /* run ram test */
  2715. ret = adv7842_ddr_ram_test(sd);
  2716. main_reset(sd);
  2717. adv7842_rewrite_i2c_addresses(sd, pdata);
  2718. /* and re-init chip and state */
  2719. adv7842_core_init(sd);
  2720. disable_input(sd);
  2721. select_input(sd, state->vid_std_select);
  2722. enable_input(sd);
  2723. edid_write_vga_segment(sd);
  2724. edid_write_hdmi_segment(sd, ADV7842_EDID_PORT_A);
  2725. edid_write_hdmi_segment(sd, ADV7842_EDID_PORT_B);
  2726. timings = state->timings;
  2727. memset(&state->timings, 0, sizeof(struct v4l2_dv_timings));
  2728. adv7842_s_dv_timings(sd, &timings);
  2729. return ret;
  2730. }
  2731. static long adv7842_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  2732. {
  2733. switch (cmd) {
  2734. case ADV7842_CMD_RAM_TEST:
  2735. return adv7842_command_ram_test(sd);
  2736. }
  2737. return -ENOTTY;
  2738. }
  2739. static int adv7842_subscribe_event(struct v4l2_subdev *sd,
  2740. struct v4l2_fh *fh,
  2741. struct v4l2_event_subscription *sub)
  2742. {
  2743. switch (sub->type) {
  2744. case V4L2_EVENT_SOURCE_CHANGE:
  2745. return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
  2746. case V4L2_EVENT_CTRL:
  2747. return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
  2748. default:
  2749. return -EINVAL;
  2750. }
  2751. }
  2752. static int adv7842_registered(struct v4l2_subdev *sd)
  2753. {
  2754. struct adv7842_state *state = to_state(sd);
  2755. struct i2c_client *client = v4l2_get_subdevdata(sd);
  2756. int err;
  2757. err = cec_register_adapter(state->cec_adap, &client->dev);
  2758. if (err)
  2759. cec_delete_adapter(state->cec_adap);
  2760. return err;
  2761. }
  2762. static void adv7842_unregistered(struct v4l2_subdev *sd)
  2763. {
  2764. struct adv7842_state *state = to_state(sd);
  2765. cec_unregister_adapter(state->cec_adap);
  2766. }
  2767. /* ----------------------------------------------------------------------- */
  2768. static const struct v4l2_ctrl_ops adv7842_ctrl_ops = {
  2769. .s_ctrl = adv7842_s_ctrl,
  2770. .g_volatile_ctrl = adv7842_g_volatile_ctrl,
  2771. };
  2772. static const struct v4l2_subdev_core_ops adv7842_core_ops = {
  2773. .log_status = adv7842_log_status,
  2774. .ioctl = adv7842_ioctl,
  2775. .interrupt_service_routine = adv7842_isr,
  2776. .subscribe_event = adv7842_subscribe_event,
  2777. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  2778. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2779. .g_register = adv7842_g_register,
  2780. .s_register = adv7842_s_register,
  2781. #endif
  2782. };
  2783. static const struct v4l2_subdev_video_ops adv7842_video_ops = {
  2784. .g_std = adv7842_g_std,
  2785. .s_std = adv7842_s_std,
  2786. .s_routing = adv7842_s_routing,
  2787. .querystd = adv7842_querystd,
  2788. .g_input_status = adv7842_g_input_status,
  2789. .s_dv_timings = adv7842_s_dv_timings,
  2790. .g_dv_timings = adv7842_g_dv_timings,
  2791. .query_dv_timings = adv7842_query_dv_timings,
  2792. };
  2793. static const struct v4l2_subdev_pad_ops adv7842_pad_ops = {
  2794. .enum_mbus_code = adv7842_enum_mbus_code,
  2795. .get_fmt = adv7842_get_format,
  2796. .set_fmt = adv7842_set_format,
  2797. .get_edid = adv7842_get_edid,
  2798. .set_edid = adv7842_set_edid,
  2799. .enum_dv_timings = adv7842_enum_dv_timings,
  2800. .dv_timings_cap = adv7842_dv_timings_cap,
  2801. };
  2802. static const struct v4l2_subdev_ops adv7842_ops = {
  2803. .core = &adv7842_core_ops,
  2804. .video = &adv7842_video_ops,
  2805. .pad = &adv7842_pad_ops,
  2806. };
  2807. static const struct v4l2_subdev_internal_ops adv7842_int_ops = {
  2808. .registered = adv7842_registered,
  2809. .unregistered = adv7842_unregistered,
  2810. };
  2811. /* -------------------------- custom ctrls ---------------------------------- */
  2812. static const struct v4l2_ctrl_config adv7842_ctrl_analog_sampling_phase = {
  2813. .ops = &adv7842_ctrl_ops,
  2814. .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
  2815. .name = "Analog Sampling Phase",
  2816. .type = V4L2_CTRL_TYPE_INTEGER,
  2817. .min = 0,
  2818. .max = 0x1f,
  2819. .step = 1,
  2820. .def = 0,
  2821. };
  2822. static const struct v4l2_ctrl_config adv7842_ctrl_free_run_color_manual = {
  2823. .ops = &adv7842_ctrl_ops,
  2824. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
  2825. .name = "Free Running Color, Manual",
  2826. .type = V4L2_CTRL_TYPE_BOOLEAN,
  2827. .max = 1,
  2828. .step = 1,
  2829. .def = 1,
  2830. };
  2831. static const struct v4l2_ctrl_config adv7842_ctrl_free_run_color = {
  2832. .ops = &adv7842_ctrl_ops,
  2833. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
  2834. .name = "Free Running Color",
  2835. .type = V4L2_CTRL_TYPE_INTEGER,
  2836. .max = 0xffffff,
  2837. .step = 0x1,
  2838. };
  2839. static void adv7842_unregister_clients(struct v4l2_subdev *sd)
  2840. {
  2841. struct adv7842_state *state = to_state(sd);
  2842. if (state->i2c_avlink)
  2843. i2c_unregister_device(state->i2c_avlink);
  2844. if (state->i2c_cec)
  2845. i2c_unregister_device(state->i2c_cec);
  2846. if (state->i2c_infoframe)
  2847. i2c_unregister_device(state->i2c_infoframe);
  2848. if (state->i2c_sdp_io)
  2849. i2c_unregister_device(state->i2c_sdp_io);
  2850. if (state->i2c_sdp)
  2851. i2c_unregister_device(state->i2c_sdp);
  2852. if (state->i2c_afe)
  2853. i2c_unregister_device(state->i2c_afe);
  2854. if (state->i2c_repeater)
  2855. i2c_unregister_device(state->i2c_repeater);
  2856. if (state->i2c_edid)
  2857. i2c_unregister_device(state->i2c_edid);
  2858. if (state->i2c_hdmi)
  2859. i2c_unregister_device(state->i2c_hdmi);
  2860. if (state->i2c_cp)
  2861. i2c_unregister_device(state->i2c_cp);
  2862. if (state->i2c_vdp)
  2863. i2c_unregister_device(state->i2c_vdp);
  2864. state->i2c_avlink = NULL;
  2865. state->i2c_cec = NULL;
  2866. state->i2c_infoframe = NULL;
  2867. state->i2c_sdp_io = NULL;
  2868. state->i2c_sdp = NULL;
  2869. state->i2c_afe = NULL;
  2870. state->i2c_repeater = NULL;
  2871. state->i2c_edid = NULL;
  2872. state->i2c_hdmi = NULL;
  2873. state->i2c_cp = NULL;
  2874. state->i2c_vdp = NULL;
  2875. }
  2876. static struct i2c_client *adv7842_dummy_client(struct v4l2_subdev *sd, const char *desc,
  2877. u8 addr, u8 io_reg)
  2878. {
  2879. struct i2c_client *client = v4l2_get_subdevdata(sd);
  2880. struct i2c_client *cp;
  2881. io_write(sd, io_reg, addr << 1);
  2882. if (addr == 0) {
  2883. v4l2_err(sd, "no %s i2c addr configured\n", desc);
  2884. return NULL;
  2885. }
  2886. cp = i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
  2887. if (!cp)
  2888. v4l2_err(sd, "register %s on i2c addr 0x%x failed\n", desc, addr);
  2889. return cp;
  2890. }
  2891. static int adv7842_register_clients(struct v4l2_subdev *sd)
  2892. {
  2893. struct adv7842_state *state = to_state(sd);
  2894. struct adv7842_platform_data *pdata = &state->pdata;
  2895. state->i2c_avlink = adv7842_dummy_client(sd, "avlink", pdata->i2c_avlink, 0xf3);
  2896. state->i2c_cec = adv7842_dummy_client(sd, "cec", pdata->i2c_cec, 0xf4);
  2897. state->i2c_infoframe = adv7842_dummy_client(sd, "infoframe", pdata->i2c_infoframe, 0xf5);
  2898. state->i2c_sdp_io = adv7842_dummy_client(sd, "sdp_io", pdata->i2c_sdp_io, 0xf2);
  2899. state->i2c_sdp = adv7842_dummy_client(sd, "sdp", pdata->i2c_sdp, 0xf1);
  2900. state->i2c_afe = adv7842_dummy_client(sd, "afe", pdata->i2c_afe, 0xf8);
  2901. state->i2c_repeater = adv7842_dummy_client(sd, "repeater", pdata->i2c_repeater, 0xf9);
  2902. state->i2c_edid = adv7842_dummy_client(sd, "edid", pdata->i2c_edid, 0xfa);
  2903. state->i2c_hdmi = adv7842_dummy_client(sd, "hdmi", pdata->i2c_hdmi, 0xfb);
  2904. state->i2c_cp = adv7842_dummy_client(sd, "cp", pdata->i2c_cp, 0xfd);
  2905. state->i2c_vdp = adv7842_dummy_client(sd, "vdp", pdata->i2c_vdp, 0xfe);
  2906. if (!state->i2c_avlink ||
  2907. !state->i2c_cec ||
  2908. !state->i2c_infoframe ||
  2909. !state->i2c_sdp_io ||
  2910. !state->i2c_sdp ||
  2911. !state->i2c_afe ||
  2912. !state->i2c_repeater ||
  2913. !state->i2c_edid ||
  2914. !state->i2c_hdmi ||
  2915. !state->i2c_cp ||
  2916. !state->i2c_vdp)
  2917. return -1;
  2918. return 0;
  2919. }
  2920. static int adv7842_probe(struct i2c_client *client,
  2921. const struct i2c_device_id *id)
  2922. {
  2923. struct adv7842_state *state;
  2924. static const struct v4l2_dv_timings cea640x480 =
  2925. V4L2_DV_BT_CEA_640X480P59_94;
  2926. struct adv7842_platform_data *pdata = client->dev.platform_data;
  2927. struct v4l2_ctrl_handler *hdl;
  2928. struct v4l2_ctrl *ctrl;
  2929. struct v4l2_subdev *sd;
  2930. u16 rev;
  2931. int err;
  2932. /* Check if the adapter supports the needed features */
  2933. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  2934. return -EIO;
  2935. v4l_dbg(1, debug, client, "detecting adv7842 client on address 0x%x\n",
  2936. client->addr << 1);
  2937. if (!pdata) {
  2938. v4l_err(client, "No platform data!\n");
  2939. return -ENODEV;
  2940. }
  2941. state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
  2942. if (!state)
  2943. return -ENOMEM;
  2944. /* platform data */
  2945. state->pdata = *pdata;
  2946. state->timings = cea640x480;
  2947. state->format = adv7842_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
  2948. sd = &state->sd;
  2949. v4l2_i2c_subdev_init(sd, client, &adv7842_ops);
  2950. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  2951. sd->internal_ops = &adv7842_int_ops;
  2952. state->mode = pdata->mode;
  2953. state->hdmi_port_a = pdata->input == ADV7842_SELECT_HDMI_PORT_A;
  2954. state->restart_stdi_once = true;
  2955. /* i2c access to adv7842? */
  2956. rev = adv_smbus_read_byte_data_check(client, 0xea, false) << 8 |
  2957. adv_smbus_read_byte_data_check(client, 0xeb, false);
  2958. if (rev != 0x2012) {
  2959. v4l2_info(sd, "got rev=0x%04x on first read attempt\n", rev);
  2960. rev = adv_smbus_read_byte_data_check(client, 0xea, false) << 8 |
  2961. adv_smbus_read_byte_data_check(client, 0xeb, false);
  2962. }
  2963. if (rev != 0x2012) {
  2964. v4l2_info(sd, "not an adv7842 on address 0x%x (rev=0x%04x)\n",
  2965. client->addr << 1, rev);
  2966. return -ENODEV;
  2967. }
  2968. if (pdata->chip_reset)
  2969. main_reset(sd);
  2970. /* control handlers */
  2971. hdl = &state->hdl;
  2972. v4l2_ctrl_handler_init(hdl, 6);
  2973. /* add in ascending ID order */
  2974. v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
  2975. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  2976. v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
  2977. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  2978. v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
  2979. V4L2_CID_SATURATION, 0, 255, 1, 128);
  2980. v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
  2981. V4L2_CID_HUE, 0, 128, 1, 0);
  2982. ctrl = v4l2_ctrl_new_std_menu(hdl, &adv7842_ctrl_ops,
  2983. V4L2_CID_DV_RX_IT_CONTENT_TYPE, V4L2_DV_IT_CONTENT_TYPE_NO_ITC,
  2984. 0, V4L2_DV_IT_CONTENT_TYPE_NO_ITC);
  2985. if (ctrl)
  2986. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  2987. /* custom controls */
  2988. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  2989. V4L2_CID_DV_RX_POWER_PRESENT, 0, 3, 0, 0);
  2990. state->analog_sampling_phase_ctrl = v4l2_ctrl_new_custom(hdl,
  2991. &adv7842_ctrl_analog_sampling_phase, NULL);
  2992. state->free_run_color_ctrl_manual = v4l2_ctrl_new_custom(hdl,
  2993. &adv7842_ctrl_free_run_color_manual, NULL);
  2994. state->free_run_color_ctrl = v4l2_ctrl_new_custom(hdl,
  2995. &adv7842_ctrl_free_run_color, NULL);
  2996. state->rgb_quantization_range_ctrl =
  2997. v4l2_ctrl_new_std_menu(hdl, &adv7842_ctrl_ops,
  2998. V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
  2999. 0, V4L2_DV_RGB_RANGE_AUTO);
  3000. sd->ctrl_handler = hdl;
  3001. if (hdl->error) {
  3002. err = hdl->error;
  3003. goto err_hdl;
  3004. }
  3005. if (adv7842_s_detect_tx_5v_ctrl(sd)) {
  3006. err = -ENODEV;
  3007. goto err_hdl;
  3008. }
  3009. if (adv7842_register_clients(sd) < 0) {
  3010. err = -ENOMEM;
  3011. v4l2_err(sd, "failed to create all i2c clients\n");
  3012. goto err_i2c;
  3013. }
  3014. INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
  3015. adv7842_delayed_work_enable_hotplug);
  3016. sd->entity.function = MEDIA_ENT_F_DV_DECODER;
  3017. state->pad.flags = MEDIA_PAD_FL_SOURCE;
  3018. err = media_entity_pads_init(&sd->entity, 1, &state->pad);
  3019. if (err)
  3020. goto err_work_queues;
  3021. err = adv7842_core_init(sd);
  3022. if (err)
  3023. goto err_entity;
  3024. #if IS_ENABLED(CONFIG_VIDEO_ADV7842_CEC)
  3025. state->cec_adap = cec_allocate_adapter(&adv7842_cec_adap_ops,
  3026. state, dev_name(&client->dev),
  3027. CEC_CAP_DEFAULTS, ADV7842_MAX_ADDRS);
  3028. err = PTR_ERR_OR_ZERO(state->cec_adap);
  3029. if (err)
  3030. goto err_entity;
  3031. #endif
  3032. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  3033. client->addr << 1, client->adapter->name);
  3034. return 0;
  3035. err_entity:
  3036. media_entity_cleanup(&sd->entity);
  3037. err_work_queues:
  3038. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  3039. err_i2c:
  3040. adv7842_unregister_clients(sd);
  3041. err_hdl:
  3042. v4l2_ctrl_handler_free(hdl);
  3043. return err;
  3044. }
  3045. /* ----------------------------------------------------------------------- */
  3046. static int adv7842_remove(struct i2c_client *client)
  3047. {
  3048. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  3049. struct adv7842_state *state = to_state(sd);
  3050. adv7842_irq_enable(sd, false);
  3051. cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
  3052. v4l2_device_unregister_subdev(sd);
  3053. media_entity_cleanup(&sd->entity);
  3054. adv7842_unregister_clients(sd);
  3055. v4l2_ctrl_handler_free(sd->ctrl_handler);
  3056. return 0;
  3057. }
  3058. /* ----------------------------------------------------------------------- */
  3059. static const struct i2c_device_id adv7842_id[] = {
  3060. { "adv7842", 0 },
  3061. { }
  3062. };
  3063. MODULE_DEVICE_TABLE(i2c, adv7842_id);
  3064. /* ----------------------------------------------------------------------- */
  3065. static struct i2c_driver adv7842_driver = {
  3066. .driver = {
  3067. .name = "adv7842",
  3068. },
  3069. .probe = adv7842_probe,
  3070. .remove = adv7842_remove,
  3071. .id_table = adv7842_id,
  3072. };
  3073. module_i2c_driver(adv7842_driver);