adv7842.c 82 KB

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