adv7842.c 82 KB

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