adv7842.c 88 KB

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