adv7604.c 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561
  1. /*
  2. * adv7604 - Analog Devices ADV7604 video decoder driver
  3. *
  4. * Copyright 2012 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, ADV7604, 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. * REF_03 - Analog devices, ADV7604, Hardware Manual, Rev. F, August 2010
  27. */
  28. #include <linux/delay.h>
  29. #include <linux/gpio/consumer.h>
  30. #include <linux/hdmi.h>
  31. #include <linux/i2c.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #include <linux/v4l2-dv-timings.h>
  36. #include <linux/videodev2.h>
  37. #include <linux/workqueue.h>
  38. #include <linux/regmap.h>
  39. #include <media/i2c/adv7604.h>
  40. #include <media/cec.h>
  41. #include <media/v4l2-ctrls.h>
  42. #include <media/v4l2-device.h>
  43. #include <media/v4l2-event.h>
  44. #include <media/v4l2-dv-timings.h>
  45. #include <media/v4l2-of.h>
  46. static int debug;
  47. module_param(debug, int, 0644);
  48. MODULE_PARM_DESC(debug, "debug level (0-2)");
  49. MODULE_DESCRIPTION("Analog Devices ADV7604 video decoder driver");
  50. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
  51. MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
  52. MODULE_LICENSE("GPL");
  53. /* ADV7604 system clock frequency */
  54. #define ADV76XX_FSC (28636360)
  55. #define ADV76XX_RGB_OUT (1 << 1)
  56. #define ADV76XX_OP_FORMAT_SEL_8BIT (0 << 0)
  57. #define ADV7604_OP_FORMAT_SEL_10BIT (1 << 0)
  58. #define ADV76XX_OP_FORMAT_SEL_12BIT (2 << 0)
  59. #define ADV76XX_OP_MODE_SEL_SDR_422 (0 << 5)
  60. #define ADV7604_OP_MODE_SEL_DDR_422 (1 << 5)
  61. #define ADV76XX_OP_MODE_SEL_SDR_444 (2 << 5)
  62. #define ADV7604_OP_MODE_SEL_DDR_444 (3 << 5)
  63. #define ADV76XX_OP_MODE_SEL_SDR_422_2X (4 << 5)
  64. #define ADV7604_OP_MODE_SEL_ADI_CM (5 << 5)
  65. #define ADV76XX_OP_CH_SEL_GBR (0 << 5)
  66. #define ADV76XX_OP_CH_SEL_GRB (1 << 5)
  67. #define ADV76XX_OP_CH_SEL_BGR (2 << 5)
  68. #define ADV76XX_OP_CH_SEL_RGB (3 << 5)
  69. #define ADV76XX_OP_CH_SEL_BRG (4 << 5)
  70. #define ADV76XX_OP_CH_SEL_RBG (5 << 5)
  71. #define ADV76XX_OP_SWAP_CB_CR (1 << 0)
  72. #define ADV76XX_MAX_ADDRS (3)
  73. enum adv76xx_type {
  74. ADV7604,
  75. ADV7611,
  76. ADV7612,
  77. };
  78. struct adv76xx_reg_seq {
  79. unsigned int reg;
  80. u8 val;
  81. };
  82. struct adv76xx_format_info {
  83. u32 code;
  84. u8 op_ch_sel;
  85. bool rgb_out;
  86. bool swap_cb_cr;
  87. u8 op_format_sel;
  88. };
  89. struct adv76xx_cfg_read_infoframe {
  90. const char *desc;
  91. u8 present_mask;
  92. u8 head_addr;
  93. u8 payload_addr;
  94. };
  95. struct adv76xx_chip_info {
  96. enum adv76xx_type type;
  97. bool has_afe;
  98. unsigned int max_port;
  99. unsigned int num_dv_ports;
  100. unsigned int edid_enable_reg;
  101. unsigned int edid_status_reg;
  102. unsigned int lcf_reg;
  103. unsigned int cable_det_mask;
  104. unsigned int tdms_lock_mask;
  105. unsigned int fmt_change_digital_mask;
  106. unsigned int cp_csc;
  107. const struct adv76xx_format_info *formats;
  108. unsigned int nformats;
  109. void (*set_termination)(struct v4l2_subdev *sd, bool enable);
  110. void (*setup_irqs)(struct v4l2_subdev *sd);
  111. unsigned int (*read_hdmi_pixelclock)(struct v4l2_subdev *sd);
  112. unsigned int (*read_cable_det)(struct v4l2_subdev *sd);
  113. /* 0 = AFE, 1 = HDMI */
  114. const struct adv76xx_reg_seq *recommended_settings[2];
  115. unsigned int num_recommended_settings[2];
  116. unsigned long page_mask;
  117. /* Masks for timings */
  118. unsigned int linewidth_mask;
  119. unsigned int field0_height_mask;
  120. unsigned int field1_height_mask;
  121. unsigned int hfrontporch_mask;
  122. unsigned int hsync_mask;
  123. unsigned int hbackporch_mask;
  124. unsigned int field0_vfrontporch_mask;
  125. unsigned int field1_vfrontporch_mask;
  126. unsigned int field0_vsync_mask;
  127. unsigned int field1_vsync_mask;
  128. unsigned int field0_vbackporch_mask;
  129. unsigned int field1_vbackporch_mask;
  130. };
  131. /*
  132. **********************************************************************
  133. *
  134. * Arrays with configuration parameters for the ADV7604
  135. *
  136. **********************************************************************
  137. */
  138. struct adv76xx_state {
  139. const struct adv76xx_chip_info *info;
  140. struct adv76xx_platform_data pdata;
  141. struct gpio_desc *hpd_gpio[4];
  142. struct gpio_desc *reset_gpio;
  143. struct v4l2_subdev sd;
  144. struct media_pad pads[ADV76XX_PAD_MAX];
  145. unsigned int source_pad;
  146. struct v4l2_ctrl_handler hdl;
  147. enum adv76xx_pad selected_input;
  148. struct v4l2_dv_timings timings;
  149. const struct adv76xx_format_info *format;
  150. struct {
  151. u8 edid[256];
  152. u32 present;
  153. unsigned blocks;
  154. } edid;
  155. u16 spa_port_a[2];
  156. struct v4l2_fract aspect_ratio;
  157. u32 rgb_quantization_range;
  158. struct delayed_work delayed_work_enable_hotplug;
  159. bool restart_stdi_once;
  160. /* CEC */
  161. struct cec_adapter *cec_adap;
  162. u8 cec_addr[ADV76XX_MAX_ADDRS];
  163. u8 cec_valid_addrs;
  164. bool cec_enabled_adap;
  165. /* i2c clients */
  166. struct i2c_client *i2c_clients[ADV76XX_PAGE_MAX];
  167. /* Regmaps */
  168. struct regmap *regmap[ADV76XX_PAGE_MAX];
  169. /* controls */
  170. struct v4l2_ctrl *detect_tx_5v_ctrl;
  171. struct v4l2_ctrl *analog_sampling_phase_ctrl;
  172. struct v4l2_ctrl *free_run_color_manual_ctrl;
  173. struct v4l2_ctrl *free_run_color_ctrl;
  174. struct v4l2_ctrl *rgb_quantization_range_ctrl;
  175. };
  176. static bool adv76xx_has_afe(struct adv76xx_state *state)
  177. {
  178. return state->info->has_afe;
  179. }
  180. /* Unsupported timings. This device cannot support 720p30. */
  181. static const struct v4l2_dv_timings adv76xx_timings_exceptions[] = {
  182. V4L2_DV_BT_CEA_1280X720P30,
  183. { }
  184. };
  185. static bool adv76xx_check_dv_timings(const struct v4l2_dv_timings *t, void *hdl)
  186. {
  187. int i;
  188. for (i = 0; adv76xx_timings_exceptions[i].bt.width; i++)
  189. if (v4l2_match_dv_timings(t, adv76xx_timings_exceptions + i, 0, false))
  190. return false;
  191. return true;
  192. }
  193. struct adv76xx_video_standards {
  194. struct v4l2_dv_timings timings;
  195. u8 vid_std;
  196. u8 v_freq;
  197. };
  198. /* sorted by number of lines */
  199. static const struct adv76xx_video_standards adv7604_prim_mode_comp[] = {
  200. /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
  201. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  202. { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
  203. { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
  204. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  205. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  206. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  207. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  208. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  209. /* TODO add 1920x1080P60_RB (CVT timing) */
  210. { },
  211. };
  212. /* sorted by number of lines */
  213. static const struct adv76xx_video_standards adv7604_prim_mode_gr[] = {
  214. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  215. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  216. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  217. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  218. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  219. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  220. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  221. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  222. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  223. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  224. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  225. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  226. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  227. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  228. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  229. { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
  230. { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
  231. { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
  232. { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
  233. { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
  234. /* TODO add 1600X1200P60_RB (not a DMT timing) */
  235. { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
  236. { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
  237. { },
  238. };
  239. /* sorted by number of lines */
  240. static const struct adv76xx_video_standards adv76xx_prim_mode_hdmi_comp[] = {
  241. { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
  242. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  243. { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
  244. { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
  245. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  246. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  247. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  248. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  249. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  250. { },
  251. };
  252. /* sorted by number of lines */
  253. static const struct adv76xx_video_standards adv76xx_prim_mode_hdmi_gr[] = {
  254. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  255. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  256. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  257. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  258. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  259. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  260. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  261. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  262. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  263. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  264. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  265. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  266. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  267. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  268. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  269. { },
  270. };
  271. static const struct v4l2_event adv76xx_ev_fmt = {
  272. .type = V4L2_EVENT_SOURCE_CHANGE,
  273. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  274. };
  275. /* ----------------------------------------------------------------------- */
  276. static inline struct adv76xx_state *to_state(struct v4l2_subdev *sd)
  277. {
  278. return container_of(sd, struct adv76xx_state, sd);
  279. }
  280. static inline unsigned htotal(const struct v4l2_bt_timings *t)
  281. {
  282. return V4L2_DV_BT_FRAME_WIDTH(t);
  283. }
  284. static inline unsigned vtotal(const struct v4l2_bt_timings *t)
  285. {
  286. return V4L2_DV_BT_FRAME_HEIGHT(t);
  287. }
  288. /* ----------------------------------------------------------------------- */
  289. static int adv76xx_read_check(struct adv76xx_state *state,
  290. int client_page, u8 reg)
  291. {
  292. struct i2c_client *client = state->i2c_clients[client_page];
  293. int err;
  294. unsigned int val;
  295. err = regmap_read(state->regmap[client_page], reg, &val);
  296. if (err) {
  297. v4l_err(client, "error reading %02x, %02x\n",
  298. client->addr, reg);
  299. return err;
  300. }
  301. return val;
  302. }
  303. /* adv76xx_write_block(): Write raw data with a maximum of I2C_SMBUS_BLOCK_MAX
  304. * size to one or more registers.
  305. *
  306. * A value of zero will be returned on success, a negative errno will
  307. * be returned in error cases.
  308. */
  309. static int adv76xx_write_block(struct adv76xx_state *state, int client_page,
  310. unsigned int init_reg, const void *val,
  311. size_t val_len)
  312. {
  313. struct regmap *regmap = state->regmap[client_page];
  314. if (val_len > I2C_SMBUS_BLOCK_MAX)
  315. val_len = I2C_SMBUS_BLOCK_MAX;
  316. return regmap_raw_write(regmap, init_reg, val, val_len);
  317. }
  318. /* ----------------------------------------------------------------------- */
  319. static inline int io_read(struct v4l2_subdev *sd, u8 reg)
  320. {
  321. struct adv76xx_state *state = to_state(sd);
  322. return adv76xx_read_check(state, ADV76XX_PAGE_IO, reg);
  323. }
  324. static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  325. {
  326. struct adv76xx_state *state = to_state(sd);
  327. return regmap_write(state->regmap[ADV76XX_PAGE_IO], reg, val);
  328. }
  329. static inline int io_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask,
  330. u8 val)
  331. {
  332. return io_write(sd, reg, (io_read(sd, reg) & ~mask) | val);
  333. }
  334. static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
  335. {
  336. struct adv76xx_state *state = to_state(sd);
  337. return adv76xx_read_check(state, ADV7604_PAGE_AVLINK, reg);
  338. }
  339. static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  340. {
  341. struct adv76xx_state *state = to_state(sd);
  342. return regmap_write(state->regmap[ADV7604_PAGE_AVLINK], reg, val);
  343. }
  344. static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
  345. {
  346. struct adv76xx_state *state = to_state(sd);
  347. return adv76xx_read_check(state, ADV76XX_PAGE_CEC, reg);
  348. }
  349. static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  350. {
  351. struct adv76xx_state *state = to_state(sd);
  352. return regmap_write(state->regmap[ADV76XX_PAGE_CEC], reg, val);
  353. }
  354. static inline int cec_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask,
  355. u8 val)
  356. {
  357. return cec_write(sd, reg, (cec_read(sd, reg) & ~mask) | val);
  358. }
  359. static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
  360. {
  361. struct adv76xx_state *state = to_state(sd);
  362. return adv76xx_read_check(state, ADV76XX_PAGE_INFOFRAME, reg);
  363. }
  364. static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  365. {
  366. struct adv76xx_state *state = to_state(sd);
  367. return regmap_write(state->regmap[ADV76XX_PAGE_INFOFRAME], reg, val);
  368. }
  369. static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
  370. {
  371. struct adv76xx_state *state = to_state(sd);
  372. return adv76xx_read_check(state, ADV76XX_PAGE_AFE, reg);
  373. }
  374. static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  375. {
  376. struct adv76xx_state *state = to_state(sd);
  377. return regmap_write(state->regmap[ADV76XX_PAGE_AFE], reg, val);
  378. }
  379. static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
  380. {
  381. struct adv76xx_state *state = to_state(sd);
  382. return adv76xx_read_check(state, ADV76XX_PAGE_REP, reg);
  383. }
  384. static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  385. {
  386. struct adv76xx_state *state = to_state(sd);
  387. return regmap_write(state->regmap[ADV76XX_PAGE_REP], reg, val);
  388. }
  389. static inline int rep_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  390. {
  391. return rep_write(sd, reg, (rep_read(sd, reg) & ~mask) | val);
  392. }
  393. static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
  394. {
  395. struct adv76xx_state *state = to_state(sd);
  396. return adv76xx_read_check(state, ADV76XX_PAGE_EDID, reg);
  397. }
  398. static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  399. {
  400. struct adv76xx_state *state = to_state(sd);
  401. return regmap_write(state->regmap[ADV76XX_PAGE_EDID], reg, val);
  402. }
  403. static inline int edid_write_block(struct v4l2_subdev *sd,
  404. unsigned int total_len, const u8 *val)
  405. {
  406. struct adv76xx_state *state = to_state(sd);
  407. int err = 0;
  408. int i = 0;
  409. int len = 0;
  410. v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n",
  411. __func__, total_len);
  412. while (!err && i < total_len) {
  413. len = (total_len - i) > I2C_SMBUS_BLOCK_MAX ?
  414. I2C_SMBUS_BLOCK_MAX :
  415. (total_len - i);
  416. err = adv76xx_write_block(state, ADV76XX_PAGE_EDID,
  417. i, val + i, len);
  418. i += len;
  419. }
  420. return err;
  421. }
  422. static void adv76xx_set_hpd(struct adv76xx_state *state, unsigned int hpd)
  423. {
  424. unsigned int i;
  425. for (i = 0; i < state->info->num_dv_ports; ++i)
  426. gpiod_set_value_cansleep(state->hpd_gpio[i], hpd & BIT(i));
  427. v4l2_subdev_notify(&state->sd, ADV76XX_HOTPLUG, &hpd);
  428. }
  429. static void adv76xx_delayed_work_enable_hotplug(struct work_struct *work)
  430. {
  431. struct delayed_work *dwork = to_delayed_work(work);
  432. struct adv76xx_state *state = container_of(dwork, struct adv76xx_state,
  433. delayed_work_enable_hotplug);
  434. struct v4l2_subdev *sd = &state->sd;
  435. v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
  436. adv76xx_set_hpd(state, state->edid.present);
  437. }
  438. static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
  439. {
  440. struct adv76xx_state *state = to_state(sd);
  441. return adv76xx_read_check(state, ADV76XX_PAGE_HDMI, reg);
  442. }
  443. static u16 hdmi_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
  444. {
  445. return ((hdmi_read(sd, reg) << 8) | hdmi_read(sd, reg + 1)) & mask;
  446. }
  447. static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  448. {
  449. struct adv76xx_state *state = to_state(sd);
  450. return regmap_write(state->regmap[ADV76XX_PAGE_HDMI], reg, val);
  451. }
  452. static inline int hdmi_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  453. {
  454. return hdmi_write(sd, reg, (hdmi_read(sd, reg) & ~mask) | val);
  455. }
  456. static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  457. {
  458. struct adv76xx_state *state = to_state(sd);
  459. return regmap_write(state->regmap[ADV76XX_PAGE_TEST], reg, val);
  460. }
  461. static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
  462. {
  463. struct adv76xx_state *state = to_state(sd);
  464. return adv76xx_read_check(state, ADV76XX_PAGE_CP, reg);
  465. }
  466. static u16 cp_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
  467. {
  468. return ((cp_read(sd, reg) << 8) | cp_read(sd, reg + 1)) & mask;
  469. }
  470. static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  471. {
  472. struct adv76xx_state *state = to_state(sd);
  473. return regmap_write(state->regmap[ADV76XX_PAGE_CP], reg, val);
  474. }
  475. static inline int cp_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  476. {
  477. return cp_write(sd, reg, (cp_read(sd, reg) & ~mask) | val);
  478. }
  479. static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
  480. {
  481. struct adv76xx_state *state = to_state(sd);
  482. return adv76xx_read_check(state, ADV7604_PAGE_VDP, reg);
  483. }
  484. static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  485. {
  486. struct adv76xx_state *state = to_state(sd);
  487. return regmap_write(state->regmap[ADV7604_PAGE_VDP], reg, val);
  488. }
  489. #define ADV76XX_REG(page, offset) (((page) << 8) | (offset))
  490. #define ADV76XX_REG_SEQ_TERM 0xffff
  491. #ifdef CONFIG_VIDEO_ADV_DEBUG
  492. static int adv76xx_read_reg(struct v4l2_subdev *sd, unsigned int reg)
  493. {
  494. struct adv76xx_state *state = to_state(sd);
  495. unsigned int page = reg >> 8;
  496. unsigned int val;
  497. int err;
  498. if (!(BIT(page) & state->info->page_mask))
  499. return -EINVAL;
  500. reg &= 0xff;
  501. err = regmap_read(state->regmap[page], reg, &val);
  502. return err ? err : val;
  503. }
  504. #endif
  505. static int adv76xx_write_reg(struct v4l2_subdev *sd, unsigned int reg, u8 val)
  506. {
  507. struct adv76xx_state *state = to_state(sd);
  508. unsigned int page = reg >> 8;
  509. if (!(BIT(page) & state->info->page_mask))
  510. return -EINVAL;
  511. reg &= 0xff;
  512. return regmap_write(state->regmap[page], reg, val);
  513. }
  514. static void adv76xx_write_reg_seq(struct v4l2_subdev *sd,
  515. const struct adv76xx_reg_seq *reg_seq)
  516. {
  517. unsigned int i;
  518. for (i = 0; reg_seq[i].reg != ADV76XX_REG_SEQ_TERM; i++)
  519. adv76xx_write_reg(sd, reg_seq[i].reg, reg_seq[i].val);
  520. }
  521. /* -----------------------------------------------------------------------------
  522. * Format helpers
  523. */
  524. static const struct adv76xx_format_info adv7604_formats[] = {
  525. { MEDIA_BUS_FMT_RGB888_1X24, ADV76XX_OP_CH_SEL_RGB, true, false,
  526. ADV76XX_OP_MODE_SEL_SDR_444 | ADV76XX_OP_FORMAT_SEL_8BIT },
  527. { MEDIA_BUS_FMT_YUYV8_2X8, ADV76XX_OP_CH_SEL_RGB, false, false,
  528. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_8BIT },
  529. { MEDIA_BUS_FMT_YVYU8_2X8, ADV76XX_OP_CH_SEL_RGB, false, true,
  530. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_8BIT },
  531. { MEDIA_BUS_FMT_YUYV10_2X10, ADV76XX_OP_CH_SEL_RGB, false, false,
  532. ADV76XX_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
  533. { MEDIA_BUS_FMT_YVYU10_2X10, ADV76XX_OP_CH_SEL_RGB, false, true,
  534. ADV76XX_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
  535. { MEDIA_BUS_FMT_YUYV12_2X12, ADV76XX_OP_CH_SEL_RGB, false, false,
  536. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_12BIT },
  537. { MEDIA_BUS_FMT_YVYU12_2X12, ADV76XX_OP_CH_SEL_RGB, false, true,
  538. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_12BIT },
  539. { MEDIA_BUS_FMT_UYVY8_1X16, ADV76XX_OP_CH_SEL_RBG, false, false,
  540. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  541. { MEDIA_BUS_FMT_VYUY8_1X16, ADV76XX_OP_CH_SEL_RBG, false, true,
  542. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  543. { MEDIA_BUS_FMT_YUYV8_1X16, ADV76XX_OP_CH_SEL_RGB, false, false,
  544. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  545. { MEDIA_BUS_FMT_YVYU8_1X16, ADV76XX_OP_CH_SEL_RGB, false, true,
  546. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  547. { MEDIA_BUS_FMT_UYVY10_1X20, ADV76XX_OP_CH_SEL_RBG, false, false,
  548. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
  549. { MEDIA_BUS_FMT_VYUY10_1X20, ADV76XX_OP_CH_SEL_RBG, false, true,
  550. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
  551. { MEDIA_BUS_FMT_YUYV10_1X20, ADV76XX_OP_CH_SEL_RGB, false, false,
  552. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
  553. { MEDIA_BUS_FMT_YVYU10_1X20, ADV76XX_OP_CH_SEL_RGB, false, true,
  554. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
  555. { MEDIA_BUS_FMT_UYVY12_1X24, ADV76XX_OP_CH_SEL_RBG, false, false,
  556. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  557. { MEDIA_BUS_FMT_VYUY12_1X24, ADV76XX_OP_CH_SEL_RBG, false, true,
  558. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  559. { MEDIA_BUS_FMT_YUYV12_1X24, ADV76XX_OP_CH_SEL_RGB, false, false,
  560. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  561. { MEDIA_BUS_FMT_YVYU12_1X24, ADV76XX_OP_CH_SEL_RGB, false, true,
  562. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  563. };
  564. static const struct adv76xx_format_info adv7611_formats[] = {
  565. { MEDIA_BUS_FMT_RGB888_1X24, ADV76XX_OP_CH_SEL_RGB, true, false,
  566. ADV76XX_OP_MODE_SEL_SDR_444 | ADV76XX_OP_FORMAT_SEL_8BIT },
  567. { MEDIA_BUS_FMT_YUYV8_2X8, ADV76XX_OP_CH_SEL_RGB, false, false,
  568. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_8BIT },
  569. { MEDIA_BUS_FMT_YVYU8_2X8, ADV76XX_OP_CH_SEL_RGB, false, true,
  570. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_8BIT },
  571. { MEDIA_BUS_FMT_YUYV12_2X12, ADV76XX_OP_CH_SEL_RGB, false, false,
  572. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_12BIT },
  573. { MEDIA_BUS_FMT_YVYU12_2X12, ADV76XX_OP_CH_SEL_RGB, false, true,
  574. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_12BIT },
  575. { MEDIA_BUS_FMT_UYVY8_1X16, ADV76XX_OP_CH_SEL_RBG, false, false,
  576. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  577. { MEDIA_BUS_FMT_VYUY8_1X16, ADV76XX_OP_CH_SEL_RBG, false, true,
  578. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  579. { MEDIA_BUS_FMT_YUYV8_1X16, ADV76XX_OP_CH_SEL_RGB, false, false,
  580. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  581. { MEDIA_BUS_FMT_YVYU8_1X16, ADV76XX_OP_CH_SEL_RGB, false, true,
  582. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  583. { MEDIA_BUS_FMT_UYVY12_1X24, ADV76XX_OP_CH_SEL_RBG, false, false,
  584. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  585. { MEDIA_BUS_FMT_VYUY12_1X24, ADV76XX_OP_CH_SEL_RBG, false, true,
  586. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  587. { MEDIA_BUS_FMT_YUYV12_1X24, ADV76XX_OP_CH_SEL_RGB, false, false,
  588. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  589. { MEDIA_BUS_FMT_YVYU12_1X24, ADV76XX_OP_CH_SEL_RGB, false, true,
  590. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  591. };
  592. static const struct adv76xx_format_info adv7612_formats[] = {
  593. { MEDIA_BUS_FMT_RGB888_1X24, ADV76XX_OP_CH_SEL_RGB, true, false,
  594. ADV76XX_OP_MODE_SEL_SDR_444 | ADV76XX_OP_FORMAT_SEL_8BIT },
  595. { MEDIA_BUS_FMT_YUYV8_2X8, ADV76XX_OP_CH_SEL_RGB, false, false,
  596. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_8BIT },
  597. { MEDIA_BUS_FMT_YVYU8_2X8, ADV76XX_OP_CH_SEL_RGB, false, true,
  598. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_8BIT },
  599. { MEDIA_BUS_FMT_UYVY8_1X16, ADV76XX_OP_CH_SEL_RBG, false, false,
  600. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  601. { MEDIA_BUS_FMT_VYUY8_1X16, ADV76XX_OP_CH_SEL_RBG, false, true,
  602. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  603. { MEDIA_BUS_FMT_YUYV8_1X16, ADV76XX_OP_CH_SEL_RGB, false, false,
  604. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  605. { MEDIA_BUS_FMT_YVYU8_1X16, ADV76XX_OP_CH_SEL_RGB, false, true,
  606. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  607. };
  608. static const struct adv76xx_format_info *
  609. adv76xx_format_info(struct adv76xx_state *state, u32 code)
  610. {
  611. unsigned int i;
  612. for (i = 0; i < state->info->nformats; ++i) {
  613. if (state->info->formats[i].code == code)
  614. return &state->info->formats[i];
  615. }
  616. return NULL;
  617. }
  618. /* ----------------------------------------------------------------------- */
  619. static inline bool is_analog_input(struct v4l2_subdev *sd)
  620. {
  621. struct adv76xx_state *state = to_state(sd);
  622. return state->selected_input == ADV7604_PAD_VGA_RGB ||
  623. state->selected_input == ADV7604_PAD_VGA_COMP;
  624. }
  625. static inline bool is_digital_input(struct v4l2_subdev *sd)
  626. {
  627. struct adv76xx_state *state = to_state(sd);
  628. return state->selected_input == ADV76XX_PAD_HDMI_PORT_A ||
  629. state->selected_input == ADV7604_PAD_HDMI_PORT_B ||
  630. state->selected_input == ADV7604_PAD_HDMI_PORT_C ||
  631. state->selected_input == ADV7604_PAD_HDMI_PORT_D;
  632. }
  633. static const struct v4l2_dv_timings_cap adv7604_timings_cap_analog = {
  634. .type = V4L2_DV_BT_656_1120,
  635. /* keep this initialization for compatibility with GCC < 4.4.6 */
  636. .reserved = { 0 },
  637. V4L2_INIT_BT_TIMINGS(0, 1920, 0, 1200, 25000000, 170000000,
  638. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  639. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
  640. V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
  641. V4L2_DV_BT_CAP_CUSTOM)
  642. };
  643. static const struct v4l2_dv_timings_cap adv76xx_timings_cap_digital = {
  644. .type = V4L2_DV_BT_656_1120,
  645. /* keep this initialization for compatibility with GCC < 4.4.6 */
  646. .reserved = { 0 },
  647. V4L2_INIT_BT_TIMINGS(0, 1920, 0, 1200, 25000000, 225000000,
  648. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  649. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
  650. V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
  651. V4L2_DV_BT_CAP_CUSTOM)
  652. };
  653. /*
  654. * Return the DV timings capabilities for the requested sink pad. As a special
  655. * case, pad value -1 returns the capabilities for the currently selected input.
  656. */
  657. static const struct v4l2_dv_timings_cap *
  658. adv76xx_get_dv_timings_cap(struct v4l2_subdev *sd, int pad)
  659. {
  660. if (pad == -1) {
  661. struct adv76xx_state *state = to_state(sd);
  662. pad = state->selected_input;
  663. }
  664. switch (pad) {
  665. case ADV76XX_PAD_HDMI_PORT_A:
  666. case ADV7604_PAD_HDMI_PORT_B:
  667. case ADV7604_PAD_HDMI_PORT_C:
  668. case ADV7604_PAD_HDMI_PORT_D:
  669. return &adv76xx_timings_cap_digital;
  670. case ADV7604_PAD_VGA_RGB:
  671. case ADV7604_PAD_VGA_COMP:
  672. default:
  673. return &adv7604_timings_cap_analog;
  674. }
  675. }
  676. /* ----------------------------------------------------------------------- */
  677. #ifdef CONFIG_VIDEO_ADV_DEBUG
  678. static void adv76xx_inv_register(struct v4l2_subdev *sd)
  679. {
  680. v4l2_info(sd, "0x000-0x0ff: IO Map\n");
  681. v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
  682. v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
  683. v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
  684. v4l2_info(sd, "0x400-0x4ff: ESDP Map\n");
  685. v4l2_info(sd, "0x500-0x5ff: DPP Map\n");
  686. v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
  687. v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
  688. v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
  689. v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
  690. v4l2_info(sd, "0xa00-0xaff: Test Map\n");
  691. v4l2_info(sd, "0xb00-0xbff: CP Map\n");
  692. v4l2_info(sd, "0xc00-0xcff: VDP Map\n");
  693. }
  694. static int adv76xx_g_register(struct v4l2_subdev *sd,
  695. struct v4l2_dbg_register *reg)
  696. {
  697. int ret;
  698. ret = adv76xx_read_reg(sd, reg->reg);
  699. if (ret < 0) {
  700. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  701. adv76xx_inv_register(sd);
  702. return ret;
  703. }
  704. reg->size = 1;
  705. reg->val = ret;
  706. return 0;
  707. }
  708. static int adv76xx_s_register(struct v4l2_subdev *sd,
  709. const struct v4l2_dbg_register *reg)
  710. {
  711. int ret;
  712. ret = adv76xx_write_reg(sd, reg->reg, reg->val);
  713. if (ret < 0) {
  714. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  715. adv76xx_inv_register(sd);
  716. return ret;
  717. }
  718. return 0;
  719. }
  720. #endif
  721. static unsigned int adv7604_read_cable_det(struct v4l2_subdev *sd)
  722. {
  723. u8 value = io_read(sd, 0x6f);
  724. return ((value & 0x10) >> 4)
  725. | ((value & 0x08) >> 2)
  726. | ((value & 0x04) << 0)
  727. | ((value & 0x02) << 2);
  728. }
  729. static unsigned int adv7611_read_cable_det(struct v4l2_subdev *sd)
  730. {
  731. u8 value = io_read(sd, 0x6f);
  732. return value & 1;
  733. }
  734. static unsigned int adv7612_read_cable_det(struct v4l2_subdev *sd)
  735. {
  736. /* Reads CABLE_DET_A_RAW. For input B support, need to
  737. * account for bit 7 [MSB] of 0x6a (ie. CABLE_DET_B_RAW)
  738. */
  739. u8 value = io_read(sd, 0x6f);
  740. return value & 1;
  741. }
  742. static int adv76xx_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
  743. {
  744. struct adv76xx_state *state = to_state(sd);
  745. const struct adv76xx_chip_info *info = state->info;
  746. u16 cable_det = info->read_cable_det(sd);
  747. return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl, cable_det);
  748. }
  749. static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
  750. u8 prim_mode,
  751. const struct adv76xx_video_standards *predef_vid_timings,
  752. const struct v4l2_dv_timings *timings)
  753. {
  754. int i;
  755. for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
  756. if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
  757. is_digital_input(sd) ? 250000 : 1000000, false))
  758. continue;
  759. io_write(sd, 0x00, predef_vid_timings[i].vid_std); /* video std */
  760. io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) +
  761. prim_mode); /* v_freq and prim mode */
  762. return 0;
  763. }
  764. return -1;
  765. }
  766. static int configure_predefined_video_timings(struct v4l2_subdev *sd,
  767. struct v4l2_dv_timings *timings)
  768. {
  769. struct adv76xx_state *state = to_state(sd);
  770. int err;
  771. v4l2_dbg(1, debug, sd, "%s", __func__);
  772. if (adv76xx_has_afe(state)) {
  773. /* reset to default values */
  774. io_write(sd, 0x16, 0x43);
  775. io_write(sd, 0x17, 0x5a);
  776. }
  777. /* disable embedded syncs for auto graphics mode */
  778. cp_write_clr_set(sd, 0x81, 0x10, 0x00);
  779. cp_write(sd, 0x8f, 0x00);
  780. cp_write(sd, 0x90, 0x00);
  781. cp_write(sd, 0xa2, 0x00);
  782. cp_write(sd, 0xa3, 0x00);
  783. cp_write(sd, 0xa4, 0x00);
  784. cp_write(sd, 0xa5, 0x00);
  785. cp_write(sd, 0xa6, 0x00);
  786. cp_write(sd, 0xa7, 0x00);
  787. cp_write(sd, 0xab, 0x00);
  788. cp_write(sd, 0xac, 0x00);
  789. if (is_analog_input(sd)) {
  790. err = find_and_set_predefined_video_timings(sd,
  791. 0x01, adv7604_prim_mode_comp, timings);
  792. if (err)
  793. err = find_and_set_predefined_video_timings(sd,
  794. 0x02, adv7604_prim_mode_gr, timings);
  795. } else if (is_digital_input(sd)) {
  796. err = find_and_set_predefined_video_timings(sd,
  797. 0x05, adv76xx_prim_mode_hdmi_comp, timings);
  798. if (err)
  799. err = find_and_set_predefined_video_timings(sd,
  800. 0x06, adv76xx_prim_mode_hdmi_gr, timings);
  801. } else {
  802. v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
  803. __func__, state->selected_input);
  804. err = -1;
  805. }
  806. return err;
  807. }
  808. static void configure_custom_video_timings(struct v4l2_subdev *sd,
  809. const struct v4l2_bt_timings *bt)
  810. {
  811. struct adv76xx_state *state = to_state(sd);
  812. u32 width = htotal(bt);
  813. u32 height = vtotal(bt);
  814. u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
  815. u16 cp_start_eav = width - bt->hfrontporch;
  816. u16 cp_start_vbi = height - bt->vfrontporch;
  817. u16 cp_end_vbi = bt->vsync + bt->vbackporch;
  818. u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
  819. ((width * (ADV76XX_FSC / 100)) / ((u32)bt->pixelclock / 100)) : 0;
  820. const u8 pll[2] = {
  821. 0xc0 | ((width >> 8) & 0x1f),
  822. width & 0xff
  823. };
  824. v4l2_dbg(2, debug, sd, "%s\n", __func__);
  825. if (is_analog_input(sd)) {
  826. /* auto graphics */
  827. io_write(sd, 0x00, 0x07); /* video std */
  828. io_write(sd, 0x01, 0x02); /* prim mode */
  829. /* enable embedded syncs for auto graphics mode */
  830. cp_write_clr_set(sd, 0x81, 0x10, 0x10);
  831. /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
  832. /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
  833. /* IO-map reg. 0x16 and 0x17 should be written in sequence */
  834. if (regmap_raw_write(state->regmap[ADV76XX_PAGE_IO],
  835. 0x16, pll, 2))
  836. v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
  837. /* active video - horizontal timing */
  838. cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
  839. cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) |
  840. ((cp_start_eav >> 8) & 0x0f));
  841. cp_write(sd, 0xa4, cp_start_eav & 0xff);
  842. /* active video - vertical timing */
  843. cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
  844. cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
  845. ((cp_end_vbi >> 8) & 0xf));
  846. cp_write(sd, 0xa7, cp_end_vbi & 0xff);
  847. } else if (is_digital_input(sd)) {
  848. /* set default prim_mode/vid_std for HDMI
  849. according to [REF_03, c. 4.2] */
  850. io_write(sd, 0x00, 0x02); /* video std */
  851. io_write(sd, 0x01, 0x06); /* prim mode */
  852. } else {
  853. v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
  854. __func__, state->selected_input);
  855. }
  856. cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
  857. cp_write(sd, 0x90, ch1_fr_ll & 0xff);
  858. cp_write(sd, 0xab, (height >> 4) & 0xff);
  859. cp_write(sd, 0xac, (height & 0x0f) << 4);
  860. }
  861. static void adv76xx_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
  862. {
  863. struct adv76xx_state *state = to_state(sd);
  864. u8 offset_buf[4];
  865. if (auto_offset) {
  866. offset_a = 0x3ff;
  867. offset_b = 0x3ff;
  868. offset_c = 0x3ff;
  869. }
  870. v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
  871. __func__, auto_offset ? "Auto" : "Manual",
  872. offset_a, offset_b, offset_c);
  873. offset_buf[0] = (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
  874. offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
  875. offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
  876. offset_buf[3] = offset_c & 0x0ff;
  877. /* Registers must be written in this order with no i2c access in between */
  878. if (regmap_raw_write(state->regmap[ADV76XX_PAGE_CP],
  879. 0x77, offset_buf, 4))
  880. v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
  881. }
  882. static void adv76xx_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
  883. {
  884. struct adv76xx_state *state = to_state(sd);
  885. u8 gain_buf[4];
  886. u8 gain_man = 1;
  887. u8 agc_mode_man = 1;
  888. if (auto_gain) {
  889. gain_man = 0;
  890. agc_mode_man = 0;
  891. gain_a = 0x100;
  892. gain_b = 0x100;
  893. gain_c = 0x100;
  894. }
  895. v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
  896. __func__, auto_gain ? "Auto" : "Manual",
  897. gain_a, gain_b, gain_c);
  898. gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
  899. gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
  900. gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
  901. gain_buf[3] = ((gain_c & 0x0ff));
  902. /* Registers must be written in this order with no i2c access in between */
  903. if (regmap_raw_write(state->regmap[ADV76XX_PAGE_CP],
  904. 0x73, gain_buf, 4))
  905. v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
  906. }
  907. static void set_rgb_quantization_range(struct v4l2_subdev *sd)
  908. {
  909. struct adv76xx_state *state = to_state(sd);
  910. bool rgb_output = io_read(sd, 0x02) & 0x02;
  911. bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
  912. u8 y = HDMI_COLORSPACE_RGB;
  913. if (hdmi_signal && (io_read(sd, 0x60) & 1))
  914. y = infoframe_read(sd, 0x01) >> 5;
  915. v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
  916. __func__, state->rgb_quantization_range,
  917. rgb_output, hdmi_signal);
  918. adv76xx_set_gain(sd, true, 0x0, 0x0, 0x0);
  919. adv76xx_set_offset(sd, true, 0x0, 0x0, 0x0);
  920. io_write_clr_set(sd, 0x02, 0x04, rgb_output ? 0 : 4);
  921. switch (state->rgb_quantization_range) {
  922. case V4L2_DV_RGB_RANGE_AUTO:
  923. if (state->selected_input == ADV7604_PAD_VGA_RGB) {
  924. /* Receiving analog RGB signal
  925. * Set RGB full range (0-255) */
  926. io_write_clr_set(sd, 0x02, 0xf0, 0x10);
  927. break;
  928. }
  929. if (state->selected_input == ADV7604_PAD_VGA_COMP) {
  930. /* Receiving analog YPbPr signal
  931. * Set automode */
  932. io_write_clr_set(sd, 0x02, 0xf0, 0xf0);
  933. break;
  934. }
  935. if (hdmi_signal) {
  936. /* Receiving HDMI signal
  937. * Set automode */
  938. io_write_clr_set(sd, 0x02, 0xf0, 0xf0);
  939. break;
  940. }
  941. /* Receiving DVI-D signal
  942. * ADV7604 selects RGB limited range regardless of
  943. * input format (CE/IT) in automatic mode */
  944. if (state->timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) {
  945. /* RGB limited range (16-235) */
  946. io_write_clr_set(sd, 0x02, 0xf0, 0x00);
  947. } else {
  948. /* RGB full range (0-255) */
  949. io_write_clr_set(sd, 0x02, 0xf0, 0x10);
  950. if (is_digital_input(sd) && rgb_output) {
  951. adv76xx_set_offset(sd, false, 0x40, 0x40, 0x40);
  952. } else {
  953. adv76xx_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
  954. adv76xx_set_offset(sd, false, 0x70, 0x70, 0x70);
  955. }
  956. }
  957. break;
  958. case V4L2_DV_RGB_RANGE_LIMITED:
  959. if (state->selected_input == ADV7604_PAD_VGA_COMP) {
  960. /* YCrCb limited range (16-235) */
  961. io_write_clr_set(sd, 0x02, 0xf0, 0x20);
  962. break;
  963. }
  964. if (y != HDMI_COLORSPACE_RGB)
  965. break;
  966. /* RGB limited range (16-235) */
  967. io_write_clr_set(sd, 0x02, 0xf0, 0x00);
  968. break;
  969. case V4L2_DV_RGB_RANGE_FULL:
  970. if (state->selected_input == ADV7604_PAD_VGA_COMP) {
  971. /* YCrCb full range (0-255) */
  972. io_write_clr_set(sd, 0x02, 0xf0, 0x60);
  973. break;
  974. }
  975. if (y != HDMI_COLORSPACE_RGB)
  976. break;
  977. /* RGB full range (0-255) */
  978. io_write_clr_set(sd, 0x02, 0xf0, 0x10);
  979. if (is_analog_input(sd) || hdmi_signal)
  980. break;
  981. /* Adjust gain/offset for DVI-D signals only */
  982. if (rgb_output) {
  983. adv76xx_set_offset(sd, false, 0x40, 0x40, 0x40);
  984. } else {
  985. adv76xx_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
  986. adv76xx_set_offset(sd, false, 0x70, 0x70, 0x70);
  987. }
  988. break;
  989. }
  990. }
  991. static int adv76xx_s_ctrl(struct v4l2_ctrl *ctrl)
  992. {
  993. struct v4l2_subdev *sd =
  994. &container_of(ctrl->handler, struct adv76xx_state, hdl)->sd;
  995. struct adv76xx_state *state = to_state(sd);
  996. switch (ctrl->id) {
  997. case V4L2_CID_BRIGHTNESS:
  998. cp_write(sd, 0x3c, ctrl->val);
  999. return 0;
  1000. case V4L2_CID_CONTRAST:
  1001. cp_write(sd, 0x3a, ctrl->val);
  1002. return 0;
  1003. case V4L2_CID_SATURATION:
  1004. cp_write(sd, 0x3b, ctrl->val);
  1005. return 0;
  1006. case V4L2_CID_HUE:
  1007. cp_write(sd, 0x3d, ctrl->val);
  1008. return 0;
  1009. case V4L2_CID_DV_RX_RGB_RANGE:
  1010. state->rgb_quantization_range = ctrl->val;
  1011. set_rgb_quantization_range(sd);
  1012. return 0;
  1013. case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
  1014. if (!adv76xx_has_afe(state))
  1015. return -EINVAL;
  1016. /* Set the analog sampling phase. This is needed to find the
  1017. best sampling phase for analog video: an application or
  1018. driver has to try a number of phases and analyze the picture
  1019. quality before settling on the best performing phase. */
  1020. afe_write(sd, 0xc8, ctrl->val);
  1021. return 0;
  1022. case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
  1023. /* Use the default blue color for free running mode,
  1024. or supply your own. */
  1025. cp_write_clr_set(sd, 0xbf, 0x04, ctrl->val << 2);
  1026. return 0;
  1027. case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
  1028. cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
  1029. cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
  1030. cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
  1031. return 0;
  1032. }
  1033. return -EINVAL;
  1034. }
  1035. static int adv76xx_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  1036. {
  1037. struct v4l2_subdev *sd =
  1038. &container_of(ctrl->handler, struct adv76xx_state, hdl)->sd;
  1039. if (ctrl->id == V4L2_CID_DV_RX_IT_CONTENT_TYPE) {
  1040. ctrl->val = V4L2_DV_IT_CONTENT_TYPE_NO_ITC;
  1041. if ((io_read(sd, 0x60) & 1) && (infoframe_read(sd, 0x03) & 0x80))
  1042. ctrl->val = (infoframe_read(sd, 0x05) >> 4) & 3;
  1043. return 0;
  1044. }
  1045. return -EINVAL;
  1046. }
  1047. /* ----------------------------------------------------------------------- */
  1048. static inline bool no_power(struct v4l2_subdev *sd)
  1049. {
  1050. /* Entire chip or CP powered off */
  1051. return io_read(sd, 0x0c) & 0x24;
  1052. }
  1053. static inline bool no_signal_tmds(struct v4l2_subdev *sd)
  1054. {
  1055. struct adv76xx_state *state = to_state(sd);
  1056. return !(io_read(sd, 0x6a) & (0x10 >> state->selected_input));
  1057. }
  1058. static inline bool no_lock_tmds(struct v4l2_subdev *sd)
  1059. {
  1060. struct adv76xx_state *state = to_state(sd);
  1061. const struct adv76xx_chip_info *info = state->info;
  1062. return (io_read(sd, 0x6a) & info->tdms_lock_mask) != info->tdms_lock_mask;
  1063. }
  1064. static inline bool is_hdmi(struct v4l2_subdev *sd)
  1065. {
  1066. return hdmi_read(sd, 0x05) & 0x80;
  1067. }
  1068. static inline bool no_lock_sspd(struct v4l2_subdev *sd)
  1069. {
  1070. struct adv76xx_state *state = to_state(sd);
  1071. /*
  1072. * Chips without a AFE don't expose registers for the SSPD, so just assume
  1073. * that we have a lock.
  1074. */
  1075. if (adv76xx_has_afe(state))
  1076. return false;
  1077. /* TODO channel 2 */
  1078. return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
  1079. }
  1080. static inline bool no_lock_stdi(struct v4l2_subdev *sd)
  1081. {
  1082. /* TODO channel 2 */
  1083. return !(cp_read(sd, 0xb1) & 0x80);
  1084. }
  1085. static inline bool no_signal(struct v4l2_subdev *sd)
  1086. {
  1087. bool ret;
  1088. ret = no_power(sd);
  1089. ret |= no_lock_stdi(sd);
  1090. ret |= no_lock_sspd(sd);
  1091. if (is_digital_input(sd)) {
  1092. ret |= no_lock_tmds(sd);
  1093. ret |= no_signal_tmds(sd);
  1094. }
  1095. return ret;
  1096. }
  1097. static inline bool no_lock_cp(struct v4l2_subdev *sd)
  1098. {
  1099. struct adv76xx_state *state = to_state(sd);
  1100. if (!adv76xx_has_afe(state))
  1101. return false;
  1102. /* CP has detected a non standard number of lines on the incoming
  1103. video compared to what it is configured to receive by s_dv_timings */
  1104. return io_read(sd, 0x12) & 0x01;
  1105. }
  1106. static inline bool in_free_run(struct v4l2_subdev *sd)
  1107. {
  1108. return cp_read(sd, 0xff) & 0x10;
  1109. }
  1110. static int adv76xx_g_input_status(struct v4l2_subdev *sd, u32 *status)
  1111. {
  1112. *status = 0;
  1113. *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
  1114. *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
  1115. if (!in_free_run(sd) && no_lock_cp(sd))
  1116. *status |= is_digital_input(sd) ?
  1117. V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
  1118. v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
  1119. return 0;
  1120. }
  1121. /* ----------------------------------------------------------------------- */
  1122. struct stdi_readback {
  1123. u16 bl, lcf, lcvs;
  1124. u8 hs_pol, vs_pol;
  1125. bool interlaced;
  1126. };
  1127. static int stdi2dv_timings(struct v4l2_subdev *sd,
  1128. struct stdi_readback *stdi,
  1129. struct v4l2_dv_timings *timings)
  1130. {
  1131. struct adv76xx_state *state = to_state(sd);
  1132. u32 hfreq = (ADV76XX_FSC * 8) / stdi->bl;
  1133. u32 pix_clk;
  1134. int i;
  1135. for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
  1136. const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
  1137. if (!v4l2_valid_dv_timings(&v4l2_dv_timings_presets[i],
  1138. adv76xx_get_dv_timings_cap(sd, -1),
  1139. adv76xx_check_dv_timings, NULL))
  1140. continue;
  1141. if (vtotal(bt) != stdi->lcf + 1)
  1142. continue;
  1143. if (bt->vsync != stdi->lcvs)
  1144. continue;
  1145. pix_clk = hfreq * htotal(bt);
  1146. if ((pix_clk < bt->pixelclock + 1000000) &&
  1147. (pix_clk > bt->pixelclock - 1000000)) {
  1148. *timings = v4l2_dv_timings_presets[i];
  1149. return 0;
  1150. }
  1151. }
  1152. if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs, 0,
  1153. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  1154. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  1155. false, timings))
  1156. return 0;
  1157. if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
  1158. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  1159. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  1160. false, state->aspect_ratio, timings))
  1161. return 0;
  1162. v4l2_dbg(2, debug, sd,
  1163. "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
  1164. __func__, stdi->lcvs, stdi->lcf, stdi->bl,
  1165. stdi->hs_pol, stdi->vs_pol);
  1166. return -1;
  1167. }
  1168. static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
  1169. {
  1170. struct adv76xx_state *state = to_state(sd);
  1171. const struct adv76xx_chip_info *info = state->info;
  1172. u8 polarity;
  1173. if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
  1174. v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
  1175. return -1;
  1176. }
  1177. /* read STDI */
  1178. stdi->bl = cp_read16(sd, 0xb1, 0x3fff);
  1179. stdi->lcf = cp_read16(sd, info->lcf_reg, 0x7ff);
  1180. stdi->lcvs = cp_read(sd, 0xb3) >> 3;
  1181. stdi->interlaced = io_read(sd, 0x12) & 0x10;
  1182. if (adv76xx_has_afe(state)) {
  1183. /* read SSPD */
  1184. polarity = cp_read(sd, 0xb5);
  1185. if ((polarity & 0x03) == 0x01) {
  1186. stdi->hs_pol = polarity & 0x10
  1187. ? (polarity & 0x08 ? '+' : '-') : 'x';
  1188. stdi->vs_pol = polarity & 0x40
  1189. ? (polarity & 0x20 ? '+' : '-') : 'x';
  1190. } else {
  1191. stdi->hs_pol = 'x';
  1192. stdi->vs_pol = 'x';
  1193. }
  1194. } else {
  1195. polarity = hdmi_read(sd, 0x05);
  1196. stdi->hs_pol = polarity & 0x20 ? '+' : '-';
  1197. stdi->vs_pol = polarity & 0x10 ? '+' : '-';
  1198. }
  1199. if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
  1200. v4l2_dbg(2, debug, sd,
  1201. "%s: signal lost during readout of STDI/SSPD\n", __func__);
  1202. return -1;
  1203. }
  1204. if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
  1205. v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
  1206. memset(stdi, 0, sizeof(struct stdi_readback));
  1207. return -1;
  1208. }
  1209. v4l2_dbg(2, debug, sd,
  1210. "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
  1211. __func__, stdi->lcf, stdi->bl, stdi->lcvs,
  1212. stdi->hs_pol, stdi->vs_pol,
  1213. stdi->interlaced ? "interlaced" : "progressive");
  1214. return 0;
  1215. }
  1216. static int adv76xx_enum_dv_timings(struct v4l2_subdev *sd,
  1217. struct v4l2_enum_dv_timings *timings)
  1218. {
  1219. struct adv76xx_state *state = to_state(sd);
  1220. if (timings->pad >= state->source_pad)
  1221. return -EINVAL;
  1222. return v4l2_enum_dv_timings_cap(timings,
  1223. adv76xx_get_dv_timings_cap(sd, timings->pad),
  1224. adv76xx_check_dv_timings, NULL);
  1225. }
  1226. static int adv76xx_dv_timings_cap(struct v4l2_subdev *sd,
  1227. struct v4l2_dv_timings_cap *cap)
  1228. {
  1229. struct adv76xx_state *state = to_state(sd);
  1230. unsigned int pad = cap->pad;
  1231. if (cap->pad >= state->source_pad)
  1232. return -EINVAL;
  1233. *cap = *adv76xx_get_dv_timings_cap(sd, pad);
  1234. cap->pad = pad;
  1235. return 0;
  1236. }
  1237. /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
  1238. if the format is listed in adv76xx_timings[] */
  1239. static void adv76xx_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
  1240. struct v4l2_dv_timings *timings)
  1241. {
  1242. v4l2_find_dv_timings_cap(timings, adv76xx_get_dv_timings_cap(sd, -1),
  1243. is_digital_input(sd) ? 250000 : 1000000,
  1244. adv76xx_check_dv_timings, NULL);
  1245. }
  1246. static unsigned int adv7604_read_hdmi_pixelclock(struct v4l2_subdev *sd)
  1247. {
  1248. unsigned int freq;
  1249. int a, b;
  1250. a = hdmi_read(sd, 0x06);
  1251. b = hdmi_read(sd, 0x3b);
  1252. if (a < 0 || b < 0)
  1253. return 0;
  1254. freq = a * 1000000 + ((b & 0x30) >> 4) * 250000;
  1255. if (is_hdmi(sd)) {
  1256. /* adjust for deep color mode */
  1257. unsigned bits_per_channel = ((hdmi_read(sd, 0x0b) & 0x60) >> 4) + 8;
  1258. freq = freq * 8 / bits_per_channel;
  1259. }
  1260. return freq;
  1261. }
  1262. static unsigned int adv7611_read_hdmi_pixelclock(struct v4l2_subdev *sd)
  1263. {
  1264. int a, b;
  1265. a = hdmi_read(sd, 0x51);
  1266. b = hdmi_read(sd, 0x52);
  1267. if (a < 0 || b < 0)
  1268. return 0;
  1269. return ((a << 1) | (b >> 7)) * 1000000 + (b & 0x7f) * 1000000 / 128;
  1270. }
  1271. static int adv76xx_query_dv_timings(struct v4l2_subdev *sd,
  1272. struct v4l2_dv_timings *timings)
  1273. {
  1274. struct adv76xx_state *state = to_state(sd);
  1275. const struct adv76xx_chip_info *info = state->info;
  1276. struct v4l2_bt_timings *bt = &timings->bt;
  1277. struct stdi_readback stdi;
  1278. if (!timings)
  1279. return -EINVAL;
  1280. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1281. if (no_signal(sd)) {
  1282. state->restart_stdi_once = true;
  1283. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  1284. return -ENOLINK;
  1285. }
  1286. /* read STDI */
  1287. if (read_stdi(sd, &stdi)) {
  1288. v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
  1289. return -ENOLINK;
  1290. }
  1291. bt->interlaced = stdi.interlaced ?
  1292. V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
  1293. if (is_digital_input(sd)) {
  1294. timings->type = V4L2_DV_BT_656_1120;
  1295. bt->width = hdmi_read16(sd, 0x07, info->linewidth_mask);
  1296. bt->height = hdmi_read16(sd, 0x09, info->field0_height_mask);
  1297. bt->pixelclock = info->read_hdmi_pixelclock(sd);
  1298. bt->hfrontporch = hdmi_read16(sd, 0x20, info->hfrontporch_mask);
  1299. bt->hsync = hdmi_read16(sd, 0x22, info->hsync_mask);
  1300. bt->hbackporch = hdmi_read16(sd, 0x24, info->hbackporch_mask);
  1301. bt->vfrontporch = hdmi_read16(sd, 0x2a,
  1302. info->field0_vfrontporch_mask) / 2;
  1303. bt->vsync = hdmi_read16(sd, 0x2e, info->field0_vsync_mask) / 2;
  1304. bt->vbackporch = hdmi_read16(sd, 0x32,
  1305. info->field0_vbackporch_mask) / 2;
  1306. bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
  1307. ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
  1308. if (bt->interlaced == V4L2_DV_INTERLACED) {
  1309. bt->height += hdmi_read16(sd, 0x0b,
  1310. info->field1_height_mask);
  1311. bt->il_vfrontporch = hdmi_read16(sd, 0x2c,
  1312. info->field1_vfrontporch_mask) / 2;
  1313. bt->il_vsync = hdmi_read16(sd, 0x30,
  1314. info->field1_vsync_mask) / 2;
  1315. bt->il_vbackporch = hdmi_read16(sd, 0x34,
  1316. info->field1_vbackporch_mask) / 2;
  1317. }
  1318. adv76xx_fill_optional_dv_timings_fields(sd, timings);
  1319. } else {
  1320. /* find format
  1321. * Since LCVS values are inaccurate [REF_03, p. 275-276],
  1322. * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
  1323. */
  1324. if (!stdi2dv_timings(sd, &stdi, timings))
  1325. goto found;
  1326. stdi.lcvs += 1;
  1327. v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
  1328. if (!stdi2dv_timings(sd, &stdi, timings))
  1329. goto found;
  1330. stdi.lcvs -= 2;
  1331. v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
  1332. if (stdi2dv_timings(sd, &stdi, timings)) {
  1333. /*
  1334. * The STDI block may measure wrong values, especially
  1335. * for lcvs and lcf. If the driver can not find any
  1336. * valid timing, the STDI block is restarted to measure
  1337. * the video timings again. The function will return an
  1338. * error, but the restart of STDI will generate a new
  1339. * STDI interrupt and the format detection process will
  1340. * restart.
  1341. */
  1342. if (state->restart_stdi_once) {
  1343. v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
  1344. /* TODO restart STDI for Sync Channel 2 */
  1345. /* enter one-shot mode */
  1346. cp_write_clr_set(sd, 0x86, 0x06, 0x00);
  1347. /* trigger STDI restart */
  1348. cp_write_clr_set(sd, 0x86, 0x06, 0x04);
  1349. /* reset to continuous mode */
  1350. cp_write_clr_set(sd, 0x86, 0x06, 0x02);
  1351. state->restart_stdi_once = false;
  1352. return -ENOLINK;
  1353. }
  1354. v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
  1355. return -ERANGE;
  1356. }
  1357. state->restart_stdi_once = true;
  1358. }
  1359. found:
  1360. if (no_signal(sd)) {
  1361. v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
  1362. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1363. return -ENOLINK;
  1364. }
  1365. if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
  1366. (is_digital_input(sd) && bt->pixelclock > 225000000)) {
  1367. v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
  1368. __func__, (u32)bt->pixelclock);
  1369. return -ERANGE;
  1370. }
  1371. if (debug > 1)
  1372. v4l2_print_dv_timings(sd->name, "adv76xx_query_dv_timings: ",
  1373. timings, true);
  1374. return 0;
  1375. }
  1376. static int adv76xx_s_dv_timings(struct v4l2_subdev *sd,
  1377. struct v4l2_dv_timings *timings)
  1378. {
  1379. struct adv76xx_state *state = to_state(sd);
  1380. struct v4l2_bt_timings *bt;
  1381. int err;
  1382. if (!timings)
  1383. return -EINVAL;
  1384. if (v4l2_match_dv_timings(&state->timings, timings, 0, false)) {
  1385. v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
  1386. return 0;
  1387. }
  1388. bt = &timings->bt;
  1389. if (!v4l2_valid_dv_timings(timings, adv76xx_get_dv_timings_cap(sd, -1),
  1390. adv76xx_check_dv_timings, NULL))
  1391. return -ERANGE;
  1392. adv76xx_fill_optional_dv_timings_fields(sd, timings);
  1393. state->timings = *timings;
  1394. cp_write_clr_set(sd, 0x91, 0x40, bt->interlaced ? 0x40 : 0x00);
  1395. /* Use prim_mode and vid_std when available */
  1396. err = configure_predefined_video_timings(sd, timings);
  1397. if (err) {
  1398. /* custom settings when the video format
  1399. does not have prim_mode/vid_std */
  1400. configure_custom_video_timings(sd, bt);
  1401. }
  1402. set_rgb_quantization_range(sd);
  1403. if (debug > 1)
  1404. v4l2_print_dv_timings(sd->name, "adv76xx_s_dv_timings: ",
  1405. timings, true);
  1406. return 0;
  1407. }
  1408. static int adv76xx_g_dv_timings(struct v4l2_subdev *sd,
  1409. struct v4l2_dv_timings *timings)
  1410. {
  1411. struct adv76xx_state *state = to_state(sd);
  1412. *timings = state->timings;
  1413. return 0;
  1414. }
  1415. static void adv7604_set_termination(struct v4l2_subdev *sd, bool enable)
  1416. {
  1417. hdmi_write(sd, 0x01, enable ? 0x00 : 0x78);
  1418. }
  1419. static void adv7611_set_termination(struct v4l2_subdev *sd, bool enable)
  1420. {
  1421. hdmi_write(sd, 0x83, enable ? 0xfe : 0xff);
  1422. }
  1423. static void enable_input(struct v4l2_subdev *sd)
  1424. {
  1425. struct adv76xx_state *state = to_state(sd);
  1426. if (is_analog_input(sd)) {
  1427. io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
  1428. } else if (is_digital_input(sd)) {
  1429. hdmi_write_clr_set(sd, 0x00, 0x03, state->selected_input);
  1430. state->info->set_termination(sd, true);
  1431. io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
  1432. hdmi_write_clr_set(sd, 0x1a, 0x10, 0x00); /* Unmute audio */
  1433. } else {
  1434. v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
  1435. __func__, state->selected_input);
  1436. }
  1437. }
  1438. static void disable_input(struct v4l2_subdev *sd)
  1439. {
  1440. struct adv76xx_state *state = to_state(sd);
  1441. hdmi_write_clr_set(sd, 0x1a, 0x10, 0x10); /* Mute audio */
  1442. msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 7.16.10] */
  1443. io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
  1444. state->info->set_termination(sd, false);
  1445. }
  1446. static void select_input(struct v4l2_subdev *sd)
  1447. {
  1448. struct adv76xx_state *state = to_state(sd);
  1449. const struct adv76xx_chip_info *info = state->info;
  1450. if (is_analog_input(sd)) {
  1451. adv76xx_write_reg_seq(sd, info->recommended_settings[0]);
  1452. afe_write(sd, 0x00, 0x08); /* power up ADC */
  1453. afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
  1454. afe_write(sd, 0xc8, 0x00); /* phase control */
  1455. } else if (is_digital_input(sd)) {
  1456. hdmi_write(sd, 0x00, state->selected_input & 0x03);
  1457. adv76xx_write_reg_seq(sd, info->recommended_settings[1]);
  1458. if (adv76xx_has_afe(state)) {
  1459. afe_write(sd, 0x00, 0xff); /* power down ADC */
  1460. afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
  1461. afe_write(sd, 0xc8, 0x40); /* phase control */
  1462. }
  1463. cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
  1464. cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
  1465. cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
  1466. } else {
  1467. v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
  1468. __func__, state->selected_input);
  1469. }
  1470. }
  1471. static int adv76xx_s_routing(struct v4l2_subdev *sd,
  1472. u32 input, u32 output, u32 config)
  1473. {
  1474. struct adv76xx_state *state = to_state(sd);
  1475. v4l2_dbg(2, debug, sd, "%s: input %d, selected input %d",
  1476. __func__, input, state->selected_input);
  1477. if (input == state->selected_input)
  1478. return 0;
  1479. if (input > state->info->max_port)
  1480. return -EINVAL;
  1481. state->selected_input = input;
  1482. disable_input(sd);
  1483. select_input(sd);
  1484. enable_input(sd);
  1485. v4l2_subdev_notify_event(sd, &adv76xx_ev_fmt);
  1486. return 0;
  1487. }
  1488. static int adv76xx_enum_mbus_code(struct v4l2_subdev *sd,
  1489. struct v4l2_subdev_pad_config *cfg,
  1490. struct v4l2_subdev_mbus_code_enum *code)
  1491. {
  1492. struct adv76xx_state *state = to_state(sd);
  1493. if (code->index >= state->info->nformats)
  1494. return -EINVAL;
  1495. code->code = state->info->formats[code->index].code;
  1496. return 0;
  1497. }
  1498. static void adv76xx_fill_format(struct adv76xx_state *state,
  1499. struct v4l2_mbus_framefmt *format)
  1500. {
  1501. memset(format, 0, sizeof(*format));
  1502. format->width = state->timings.bt.width;
  1503. format->height = state->timings.bt.height;
  1504. format->field = V4L2_FIELD_NONE;
  1505. format->colorspace = V4L2_COLORSPACE_SRGB;
  1506. if (state->timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO)
  1507. format->colorspace = (state->timings.bt.height <= 576) ?
  1508. V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
  1509. }
  1510. /*
  1511. * Compute the op_ch_sel value required to obtain on the bus the component order
  1512. * corresponding to the selected format taking into account bus reordering
  1513. * applied by the board at the output of the device.
  1514. *
  1515. * The following table gives the op_ch_value from the format component order
  1516. * (expressed as op_ch_sel value in column) and the bus reordering (expressed as
  1517. * adv76xx_bus_order value in row).
  1518. *
  1519. * | GBR(0) GRB(1) BGR(2) RGB(3) BRG(4) RBG(5)
  1520. * ----------+-------------------------------------------------
  1521. * RGB (NOP) | GBR GRB BGR RGB BRG RBG
  1522. * GRB (1-2) | BGR RGB GBR GRB RBG BRG
  1523. * RBG (2-3) | GRB GBR BRG RBG BGR RGB
  1524. * BGR (1-3) | RBG BRG RGB BGR GRB GBR
  1525. * BRG (ROR) | BRG RBG GRB GBR RGB BGR
  1526. * GBR (ROL) | RGB BGR RBG BRG GBR GRB
  1527. */
  1528. static unsigned int adv76xx_op_ch_sel(struct adv76xx_state *state)
  1529. {
  1530. #define _SEL(a,b,c,d,e,f) { \
  1531. ADV76XX_OP_CH_SEL_##a, ADV76XX_OP_CH_SEL_##b, ADV76XX_OP_CH_SEL_##c, \
  1532. ADV76XX_OP_CH_SEL_##d, ADV76XX_OP_CH_SEL_##e, ADV76XX_OP_CH_SEL_##f }
  1533. #define _BUS(x) [ADV7604_BUS_ORDER_##x]
  1534. static const unsigned int op_ch_sel[6][6] = {
  1535. _BUS(RGB) /* NOP */ = _SEL(GBR, GRB, BGR, RGB, BRG, RBG),
  1536. _BUS(GRB) /* 1-2 */ = _SEL(BGR, RGB, GBR, GRB, RBG, BRG),
  1537. _BUS(RBG) /* 2-3 */ = _SEL(GRB, GBR, BRG, RBG, BGR, RGB),
  1538. _BUS(BGR) /* 1-3 */ = _SEL(RBG, BRG, RGB, BGR, GRB, GBR),
  1539. _BUS(BRG) /* ROR */ = _SEL(BRG, RBG, GRB, GBR, RGB, BGR),
  1540. _BUS(GBR) /* ROL */ = _SEL(RGB, BGR, RBG, BRG, GBR, GRB),
  1541. };
  1542. return op_ch_sel[state->pdata.bus_order][state->format->op_ch_sel >> 5];
  1543. }
  1544. static void adv76xx_setup_format(struct adv76xx_state *state)
  1545. {
  1546. struct v4l2_subdev *sd = &state->sd;
  1547. io_write_clr_set(sd, 0x02, 0x02,
  1548. state->format->rgb_out ? ADV76XX_RGB_OUT : 0);
  1549. io_write(sd, 0x03, state->format->op_format_sel |
  1550. state->pdata.op_format_mode_sel);
  1551. io_write_clr_set(sd, 0x04, 0xe0, adv76xx_op_ch_sel(state));
  1552. io_write_clr_set(sd, 0x05, 0x01,
  1553. state->format->swap_cb_cr ? ADV76XX_OP_SWAP_CB_CR : 0);
  1554. set_rgb_quantization_range(sd);
  1555. }
  1556. static int adv76xx_get_format(struct v4l2_subdev *sd,
  1557. struct v4l2_subdev_pad_config *cfg,
  1558. struct v4l2_subdev_format *format)
  1559. {
  1560. struct adv76xx_state *state = to_state(sd);
  1561. if (format->pad != state->source_pad)
  1562. return -EINVAL;
  1563. adv76xx_fill_format(state, &format->format);
  1564. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1565. struct v4l2_mbus_framefmt *fmt;
  1566. fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
  1567. format->format.code = fmt->code;
  1568. } else {
  1569. format->format.code = state->format->code;
  1570. }
  1571. return 0;
  1572. }
  1573. static int adv76xx_get_selection(struct v4l2_subdev *sd,
  1574. struct v4l2_subdev_pad_config *cfg,
  1575. struct v4l2_subdev_selection *sel)
  1576. {
  1577. struct adv76xx_state *state = to_state(sd);
  1578. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  1579. return -EINVAL;
  1580. /* Only CROP, CROP_DEFAULT and CROP_BOUNDS are supported */
  1581. if (sel->target > V4L2_SEL_TGT_CROP_BOUNDS)
  1582. return -EINVAL;
  1583. sel->r.left = 0;
  1584. sel->r.top = 0;
  1585. sel->r.width = state->timings.bt.width;
  1586. sel->r.height = state->timings.bt.height;
  1587. return 0;
  1588. }
  1589. static int adv76xx_set_format(struct v4l2_subdev *sd,
  1590. struct v4l2_subdev_pad_config *cfg,
  1591. struct v4l2_subdev_format *format)
  1592. {
  1593. struct adv76xx_state *state = to_state(sd);
  1594. const struct adv76xx_format_info *info;
  1595. if (format->pad != state->source_pad)
  1596. return -EINVAL;
  1597. info = adv76xx_format_info(state, format->format.code);
  1598. if (info == NULL)
  1599. info = adv76xx_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
  1600. adv76xx_fill_format(state, &format->format);
  1601. format->format.code = info->code;
  1602. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1603. struct v4l2_mbus_framefmt *fmt;
  1604. fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
  1605. fmt->code = format->format.code;
  1606. } else {
  1607. state->format = info;
  1608. adv76xx_setup_format(state);
  1609. }
  1610. return 0;
  1611. }
  1612. #if IS_ENABLED(CONFIG_VIDEO_ADV7604_CEC)
  1613. static void adv76xx_cec_tx_raw_status(struct v4l2_subdev *sd, u8 tx_raw_status)
  1614. {
  1615. struct adv76xx_state *state = to_state(sd);
  1616. if ((cec_read(sd, 0x11) & 0x01) == 0) {
  1617. v4l2_dbg(1, debug, sd, "%s: tx raw: tx disabled\n", __func__);
  1618. return;
  1619. }
  1620. if (tx_raw_status & 0x02) {
  1621. v4l2_dbg(1, debug, sd, "%s: tx raw: arbitration lost\n",
  1622. __func__);
  1623. cec_transmit_done(state->cec_adap, CEC_TX_STATUS_ARB_LOST,
  1624. 1, 0, 0, 0);
  1625. }
  1626. if (tx_raw_status & 0x04) {
  1627. u8 status;
  1628. u8 nack_cnt;
  1629. u8 low_drive_cnt;
  1630. v4l2_dbg(1, debug, sd, "%s: tx raw: retry failed\n", __func__);
  1631. /*
  1632. * We set this status bit since this hardware performs
  1633. * retransmissions.
  1634. */
  1635. status = CEC_TX_STATUS_MAX_RETRIES;
  1636. nack_cnt = cec_read(sd, 0x14) & 0xf;
  1637. if (nack_cnt)
  1638. status |= CEC_TX_STATUS_NACK;
  1639. low_drive_cnt = cec_read(sd, 0x14) >> 4;
  1640. if (low_drive_cnt)
  1641. status |= CEC_TX_STATUS_LOW_DRIVE;
  1642. cec_transmit_done(state->cec_adap, status,
  1643. 0, nack_cnt, low_drive_cnt, 0);
  1644. return;
  1645. }
  1646. if (tx_raw_status & 0x01) {
  1647. v4l2_dbg(1, debug, sd, "%s: tx raw: ready ok\n", __func__);
  1648. cec_transmit_done(state->cec_adap, CEC_TX_STATUS_OK, 0, 0, 0, 0);
  1649. return;
  1650. }
  1651. }
  1652. static void adv76xx_cec_isr(struct v4l2_subdev *sd, bool *handled)
  1653. {
  1654. struct adv76xx_state *state = to_state(sd);
  1655. u8 cec_irq;
  1656. /* cec controller */
  1657. cec_irq = io_read(sd, 0x4d) & 0x0f;
  1658. if (!cec_irq)
  1659. return;
  1660. v4l2_dbg(1, debug, sd, "%s: cec: irq 0x%x\n", __func__, cec_irq);
  1661. adv76xx_cec_tx_raw_status(sd, cec_irq);
  1662. if (cec_irq & 0x08) {
  1663. struct cec_msg msg;
  1664. msg.len = cec_read(sd, 0x25) & 0x1f;
  1665. if (msg.len > 16)
  1666. msg.len = 16;
  1667. if (msg.len) {
  1668. u8 i;
  1669. for (i = 0; i < msg.len; i++)
  1670. msg.msg[i] = cec_read(sd, i + 0x15);
  1671. cec_write(sd, 0x26, 0x01); /* re-enable rx */
  1672. cec_received_msg(state->cec_adap, &msg);
  1673. }
  1674. }
  1675. /* note: the bit order is swapped between 0x4d and 0x4e */
  1676. cec_irq = ((cec_irq & 0x08) >> 3) | ((cec_irq & 0x04) >> 1) |
  1677. ((cec_irq & 0x02) << 1) | ((cec_irq & 0x01) << 3);
  1678. io_write(sd, 0x4e, cec_irq);
  1679. if (handled)
  1680. *handled = true;
  1681. }
  1682. static int adv76xx_cec_adap_enable(struct cec_adapter *adap, bool enable)
  1683. {
  1684. struct adv76xx_state *state = adap->priv;
  1685. struct v4l2_subdev *sd = &state->sd;
  1686. if (!state->cec_enabled_adap && enable) {
  1687. cec_write_clr_set(sd, 0x2a, 0x01, 0x01); /* power up cec */
  1688. cec_write(sd, 0x2c, 0x01); /* cec soft reset */
  1689. cec_write_clr_set(sd, 0x11, 0x01, 0); /* initially disable tx */
  1690. /* enabled irqs: */
  1691. /* tx: ready */
  1692. /* tx: arbitration lost */
  1693. /* tx: retry timeout */
  1694. /* rx: ready */
  1695. io_write_clr_set(sd, 0x50, 0x0f, 0x0f);
  1696. cec_write(sd, 0x26, 0x01); /* enable rx */
  1697. } else if (state->cec_enabled_adap && !enable) {
  1698. /* disable cec interrupts */
  1699. io_write_clr_set(sd, 0x50, 0x0f, 0x00);
  1700. /* disable address mask 1-3 */
  1701. cec_write_clr_set(sd, 0x27, 0x70, 0x00);
  1702. /* power down cec section */
  1703. cec_write_clr_set(sd, 0x2a, 0x01, 0x00);
  1704. state->cec_valid_addrs = 0;
  1705. }
  1706. state->cec_enabled_adap = enable;
  1707. adv76xx_s_detect_tx_5v_ctrl(sd);
  1708. return 0;
  1709. }
  1710. static int adv76xx_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
  1711. {
  1712. struct adv76xx_state *state = adap->priv;
  1713. struct v4l2_subdev *sd = &state->sd;
  1714. unsigned int i, free_idx = ADV76XX_MAX_ADDRS;
  1715. if (!state->cec_enabled_adap)
  1716. return addr == CEC_LOG_ADDR_INVALID ? 0 : -EIO;
  1717. if (addr == CEC_LOG_ADDR_INVALID) {
  1718. cec_write_clr_set(sd, 0x27, 0x70, 0);
  1719. state->cec_valid_addrs = 0;
  1720. return 0;
  1721. }
  1722. for (i = 0; i < ADV76XX_MAX_ADDRS; i++) {
  1723. bool is_valid = state->cec_valid_addrs & (1 << i);
  1724. if (free_idx == ADV76XX_MAX_ADDRS && !is_valid)
  1725. free_idx = i;
  1726. if (is_valid && state->cec_addr[i] == addr)
  1727. return 0;
  1728. }
  1729. if (i == ADV76XX_MAX_ADDRS) {
  1730. i = free_idx;
  1731. if (i == ADV76XX_MAX_ADDRS)
  1732. return -ENXIO;
  1733. }
  1734. state->cec_addr[i] = addr;
  1735. state->cec_valid_addrs |= 1 << i;
  1736. switch (i) {
  1737. case 0:
  1738. /* enable address mask 0 */
  1739. cec_write_clr_set(sd, 0x27, 0x10, 0x10);
  1740. /* set address for mask 0 */
  1741. cec_write_clr_set(sd, 0x28, 0x0f, addr);
  1742. break;
  1743. case 1:
  1744. /* enable address mask 1 */
  1745. cec_write_clr_set(sd, 0x27, 0x20, 0x20);
  1746. /* set address for mask 1 */
  1747. cec_write_clr_set(sd, 0x28, 0xf0, addr << 4);
  1748. break;
  1749. case 2:
  1750. /* enable address mask 2 */
  1751. cec_write_clr_set(sd, 0x27, 0x40, 0x40);
  1752. /* set address for mask 1 */
  1753. cec_write_clr_set(sd, 0x29, 0x0f, addr);
  1754. break;
  1755. }
  1756. return 0;
  1757. }
  1758. static int adv76xx_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  1759. u32 signal_free_time, struct cec_msg *msg)
  1760. {
  1761. struct adv76xx_state *state = adap->priv;
  1762. struct v4l2_subdev *sd = &state->sd;
  1763. u8 len = msg->len;
  1764. unsigned int i;
  1765. /*
  1766. * The number of retries is the number of attempts - 1, but retry
  1767. * at least once. It's not clear if a value of 0 is allowed, so
  1768. * let's do at least one retry.
  1769. */
  1770. cec_write_clr_set(sd, 0x12, 0x70, max(1, attempts - 1) << 4);
  1771. if (len > 16) {
  1772. v4l2_err(sd, "%s: len exceeded 16 (%d)\n", __func__, len);
  1773. return -EINVAL;
  1774. }
  1775. /* write data */
  1776. for (i = 0; i < len; i++)
  1777. cec_write(sd, i, msg->msg[i]);
  1778. /* set length (data + header) */
  1779. cec_write(sd, 0x10, len);
  1780. /* start transmit, enable tx */
  1781. cec_write(sd, 0x11, 0x01);
  1782. return 0;
  1783. }
  1784. static const struct cec_adap_ops adv76xx_cec_adap_ops = {
  1785. .adap_enable = adv76xx_cec_adap_enable,
  1786. .adap_log_addr = adv76xx_cec_adap_log_addr,
  1787. .adap_transmit = adv76xx_cec_adap_transmit,
  1788. };
  1789. #endif
  1790. static int adv76xx_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  1791. {
  1792. struct adv76xx_state *state = to_state(sd);
  1793. const struct adv76xx_chip_info *info = state->info;
  1794. const u8 irq_reg_0x43 = io_read(sd, 0x43);
  1795. const u8 irq_reg_0x6b = io_read(sd, 0x6b);
  1796. const u8 irq_reg_0x70 = io_read(sd, 0x70);
  1797. u8 fmt_change_digital;
  1798. u8 fmt_change;
  1799. u8 tx_5v;
  1800. if (irq_reg_0x43)
  1801. io_write(sd, 0x44, irq_reg_0x43);
  1802. if (irq_reg_0x70)
  1803. io_write(sd, 0x71, irq_reg_0x70);
  1804. if (irq_reg_0x6b)
  1805. io_write(sd, 0x6c, irq_reg_0x6b);
  1806. v4l2_dbg(2, debug, sd, "%s: ", __func__);
  1807. /* format change */
  1808. fmt_change = irq_reg_0x43 & 0x98;
  1809. fmt_change_digital = is_digital_input(sd)
  1810. ? irq_reg_0x6b & info->fmt_change_digital_mask
  1811. : 0;
  1812. if (fmt_change || fmt_change_digital) {
  1813. v4l2_dbg(1, debug, sd,
  1814. "%s: fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
  1815. __func__, fmt_change, fmt_change_digital);
  1816. v4l2_subdev_notify_event(sd, &adv76xx_ev_fmt);
  1817. if (handled)
  1818. *handled = true;
  1819. }
  1820. /* HDMI/DVI mode */
  1821. if (irq_reg_0x6b & 0x01) {
  1822. v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
  1823. (io_read(sd, 0x6a) & 0x01) ? "HDMI" : "DVI");
  1824. set_rgb_quantization_range(sd);
  1825. if (handled)
  1826. *handled = true;
  1827. }
  1828. #if IS_ENABLED(CONFIG_VIDEO_ADV7604_CEC)
  1829. /* cec */
  1830. adv76xx_cec_isr(sd, handled);
  1831. #endif
  1832. /* tx 5v detect */
  1833. tx_5v = irq_reg_0x70 & info->cable_det_mask;
  1834. if (tx_5v) {
  1835. v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
  1836. adv76xx_s_detect_tx_5v_ctrl(sd);
  1837. if (handled)
  1838. *handled = true;
  1839. }
  1840. return 0;
  1841. }
  1842. static int adv76xx_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  1843. {
  1844. struct adv76xx_state *state = to_state(sd);
  1845. u8 *data = NULL;
  1846. memset(edid->reserved, 0, sizeof(edid->reserved));
  1847. switch (edid->pad) {
  1848. case ADV76XX_PAD_HDMI_PORT_A:
  1849. case ADV7604_PAD_HDMI_PORT_B:
  1850. case ADV7604_PAD_HDMI_PORT_C:
  1851. case ADV7604_PAD_HDMI_PORT_D:
  1852. if (state->edid.present & (1 << edid->pad))
  1853. data = state->edid.edid;
  1854. break;
  1855. default:
  1856. return -EINVAL;
  1857. }
  1858. if (edid->start_block == 0 && edid->blocks == 0) {
  1859. edid->blocks = data ? state->edid.blocks : 0;
  1860. return 0;
  1861. }
  1862. if (data == NULL)
  1863. return -ENODATA;
  1864. if (edid->start_block >= state->edid.blocks)
  1865. return -EINVAL;
  1866. if (edid->start_block + edid->blocks > state->edid.blocks)
  1867. edid->blocks = state->edid.blocks - edid->start_block;
  1868. memcpy(edid->edid, data + edid->start_block * 128, edid->blocks * 128);
  1869. return 0;
  1870. }
  1871. static int adv76xx_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  1872. {
  1873. struct adv76xx_state *state = to_state(sd);
  1874. const struct adv76xx_chip_info *info = state->info;
  1875. unsigned int spa_loc;
  1876. u16 pa;
  1877. int err;
  1878. int i;
  1879. memset(edid->reserved, 0, sizeof(edid->reserved));
  1880. if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
  1881. return -EINVAL;
  1882. if (edid->start_block != 0)
  1883. return -EINVAL;
  1884. if (edid->blocks == 0) {
  1885. /* Disable hotplug and I2C access to EDID RAM from DDC port */
  1886. state->edid.present &= ~(1 << edid->pad);
  1887. adv76xx_set_hpd(state, state->edid.present);
  1888. rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, state->edid.present);
  1889. /* Fall back to a 16:9 aspect ratio */
  1890. state->aspect_ratio.numerator = 16;
  1891. state->aspect_ratio.denominator = 9;
  1892. if (!state->edid.present)
  1893. state->edid.blocks = 0;
  1894. v4l2_dbg(2, debug, sd, "%s: clear EDID pad %d, edid.present = 0x%x\n",
  1895. __func__, edid->pad, state->edid.present);
  1896. return 0;
  1897. }
  1898. if (edid->blocks > 2) {
  1899. edid->blocks = 2;
  1900. return -E2BIG;
  1901. }
  1902. pa = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, &spa_loc);
  1903. err = cec_phys_addr_validate(pa, &pa, NULL);
  1904. if (err)
  1905. return err;
  1906. v4l2_dbg(2, debug, sd, "%s: write EDID pad %d, edid.present = 0x%x\n",
  1907. __func__, edid->pad, state->edid.present);
  1908. /* Disable hotplug and I2C access to EDID RAM from DDC port */
  1909. cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
  1910. adv76xx_set_hpd(state, 0);
  1911. rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, 0x00);
  1912. /*
  1913. * Return an error if no location of the source physical address
  1914. * was found.
  1915. */
  1916. if (spa_loc == 0)
  1917. return -EINVAL;
  1918. switch (edid->pad) {
  1919. case ADV76XX_PAD_HDMI_PORT_A:
  1920. state->spa_port_a[0] = edid->edid[spa_loc];
  1921. state->spa_port_a[1] = edid->edid[spa_loc + 1];
  1922. break;
  1923. case ADV7604_PAD_HDMI_PORT_B:
  1924. rep_write(sd, 0x70, edid->edid[spa_loc]);
  1925. rep_write(sd, 0x71, edid->edid[spa_loc + 1]);
  1926. break;
  1927. case ADV7604_PAD_HDMI_PORT_C:
  1928. rep_write(sd, 0x72, edid->edid[spa_loc]);
  1929. rep_write(sd, 0x73, edid->edid[spa_loc + 1]);
  1930. break;
  1931. case ADV7604_PAD_HDMI_PORT_D:
  1932. rep_write(sd, 0x74, edid->edid[spa_loc]);
  1933. rep_write(sd, 0x75, edid->edid[spa_loc + 1]);
  1934. break;
  1935. default:
  1936. return -EINVAL;
  1937. }
  1938. if (info->type == ADV7604) {
  1939. rep_write(sd, 0x76, spa_loc & 0xff);
  1940. rep_write_clr_set(sd, 0x77, 0x40, (spa_loc & 0x100) >> 2);
  1941. } else {
  1942. /* ADV7612 Software Manual Rev. A, p. 15 */
  1943. rep_write(sd, 0x70, spa_loc & 0xff);
  1944. rep_write_clr_set(sd, 0x71, 0x01, (spa_loc & 0x100) >> 8);
  1945. }
  1946. edid->edid[spa_loc] = state->spa_port_a[0];
  1947. edid->edid[spa_loc + 1] = state->spa_port_a[1];
  1948. memcpy(state->edid.edid, edid->edid, 128 * edid->blocks);
  1949. state->edid.blocks = edid->blocks;
  1950. state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
  1951. edid->edid[0x16]);
  1952. state->edid.present |= 1 << edid->pad;
  1953. err = edid_write_block(sd, 128 * edid->blocks, state->edid.edid);
  1954. if (err < 0) {
  1955. v4l2_err(sd, "error %d writing edid pad %d\n", err, edid->pad);
  1956. return err;
  1957. }
  1958. /* adv76xx calculates the checksums and enables I2C access to internal
  1959. EDID RAM from DDC port. */
  1960. rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, state->edid.present);
  1961. for (i = 0; i < 1000; i++) {
  1962. if (rep_read(sd, info->edid_status_reg) & state->edid.present)
  1963. break;
  1964. mdelay(1);
  1965. }
  1966. if (i == 1000) {
  1967. v4l2_err(sd, "error enabling edid (0x%x)\n", state->edid.present);
  1968. return -EIO;
  1969. }
  1970. cec_s_phys_addr(state->cec_adap, pa, false);
  1971. /* enable hotplug after 100 ms */
  1972. schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 10);
  1973. return 0;
  1974. }
  1975. /*********** avi info frame CEA-861-E **************/
  1976. static const struct adv76xx_cfg_read_infoframe adv76xx_cri[] = {
  1977. { "AVI", 0x01, 0xe0, 0x00 },
  1978. { "Audio", 0x02, 0xe3, 0x1c },
  1979. { "SDP", 0x04, 0xe6, 0x2a },
  1980. { "Vendor", 0x10, 0xec, 0x54 }
  1981. };
  1982. static int adv76xx_read_infoframe(struct v4l2_subdev *sd, int index,
  1983. union hdmi_infoframe *frame)
  1984. {
  1985. uint8_t buffer[32];
  1986. u8 len;
  1987. int i;
  1988. if (!(io_read(sd, 0x60) & adv76xx_cri[index].present_mask)) {
  1989. v4l2_info(sd, "%s infoframe not received\n",
  1990. adv76xx_cri[index].desc);
  1991. return -ENOENT;
  1992. }
  1993. for (i = 0; i < 3; i++)
  1994. buffer[i] = infoframe_read(sd,
  1995. adv76xx_cri[index].head_addr + i);
  1996. len = buffer[2] + 1;
  1997. if (len + 3 > sizeof(buffer)) {
  1998. v4l2_err(sd, "%s: invalid %s infoframe length %d\n", __func__,
  1999. adv76xx_cri[index].desc, len);
  2000. return -ENOENT;
  2001. }
  2002. for (i = 0; i < len; i++)
  2003. buffer[i + 3] = infoframe_read(sd,
  2004. adv76xx_cri[index].payload_addr + i);
  2005. if (hdmi_infoframe_unpack(frame, buffer) < 0) {
  2006. v4l2_err(sd, "%s: unpack of %s infoframe failed\n", __func__,
  2007. adv76xx_cri[index].desc);
  2008. return -ENOENT;
  2009. }
  2010. return 0;
  2011. }
  2012. static void adv76xx_log_infoframes(struct v4l2_subdev *sd)
  2013. {
  2014. int i;
  2015. if (!is_hdmi(sd)) {
  2016. v4l2_info(sd, "receive DVI-D signal, no infoframes\n");
  2017. return;
  2018. }
  2019. for (i = 0; i < ARRAY_SIZE(adv76xx_cri); i++) {
  2020. union hdmi_infoframe frame;
  2021. struct i2c_client *client = v4l2_get_subdevdata(sd);
  2022. if (adv76xx_read_infoframe(sd, i, &frame))
  2023. return;
  2024. hdmi_infoframe_log(KERN_INFO, &client->dev, &frame);
  2025. }
  2026. }
  2027. static int adv76xx_log_status(struct v4l2_subdev *sd)
  2028. {
  2029. struct adv76xx_state *state = to_state(sd);
  2030. const struct adv76xx_chip_info *info = state->info;
  2031. struct v4l2_dv_timings timings;
  2032. struct stdi_readback stdi;
  2033. u8 reg_io_0x02 = io_read(sd, 0x02);
  2034. u8 edid_enabled;
  2035. u8 cable_det;
  2036. static const char * const csc_coeff_sel_rb[16] = {
  2037. "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
  2038. "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
  2039. "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
  2040. "reserved", "reserved", "reserved", "reserved", "manual"
  2041. };
  2042. static const char * const input_color_space_txt[16] = {
  2043. "RGB limited range (16-235)", "RGB full range (0-255)",
  2044. "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
  2045. "xvYCC Bt.601", "xvYCC Bt.709",
  2046. "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
  2047. "invalid", "invalid", "invalid", "invalid", "invalid",
  2048. "invalid", "invalid", "automatic"
  2049. };
  2050. static const char * const hdmi_color_space_txt[16] = {
  2051. "RGB limited range (16-235)", "RGB full range (0-255)",
  2052. "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
  2053. "xvYCC Bt.601", "xvYCC Bt.709",
  2054. "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
  2055. "sYCC", "Adobe YCC 601", "AdobeRGB", "invalid", "invalid",
  2056. "invalid", "invalid", "invalid"
  2057. };
  2058. static const char * const rgb_quantization_range_txt[] = {
  2059. "Automatic",
  2060. "RGB limited range (16-235)",
  2061. "RGB full range (0-255)",
  2062. };
  2063. static const char * const deep_color_mode_txt[4] = {
  2064. "8-bits per channel",
  2065. "10-bits per channel",
  2066. "12-bits per channel",
  2067. "16-bits per channel (not supported)"
  2068. };
  2069. v4l2_info(sd, "-----Chip status-----\n");
  2070. v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
  2071. edid_enabled = rep_read(sd, info->edid_status_reg);
  2072. v4l2_info(sd, "EDID enabled port A: %s, B: %s, C: %s, D: %s\n",
  2073. ((edid_enabled & 0x01) ? "Yes" : "No"),
  2074. ((edid_enabled & 0x02) ? "Yes" : "No"),
  2075. ((edid_enabled & 0x04) ? "Yes" : "No"),
  2076. ((edid_enabled & 0x08) ? "Yes" : "No"));
  2077. v4l2_info(sd, "CEC: %s\n", state->cec_enabled_adap ?
  2078. "enabled" : "disabled");
  2079. if (state->cec_enabled_adap) {
  2080. int i;
  2081. for (i = 0; i < ADV76XX_MAX_ADDRS; i++) {
  2082. bool is_valid = state->cec_valid_addrs & (1 << i);
  2083. if (is_valid)
  2084. v4l2_info(sd, "CEC Logical Address: 0x%x\n",
  2085. state->cec_addr[i]);
  2086. }
  2087. }
  2088. v4l2_info(sd, "-----Signal status-----\n");
  2089. cable_det = info->read_cable_det(sd);
  2090. v4l2_info(sd, "Cable detected (+5V power) port A: %s, B: %s, C: %s, D: %s\n",
  2091. ((cable_det & 0x01) ? "Yes" : "No"),
  2092. ((cable_det & 0x02) ? "Yes" : "No"),
  2093. ((cable_det & 0x04) ? "Yes" : "No"),
  2094. ((cable_det & 0x08) ? "Yes" : "No"));
  2095. v4l2_info(sd, "TMDS signal detected: %s\n",
  2096. no_signal_tmds(sd) ? "false" : "true");
  2097. v4l2_info(sd, "TMDS signal locked: %s\n",
  2098. no_lock_tmds(sd) ? "false" : "true");
  2099. v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
  2100. v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
  2101. v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
  2102. v4l2_info(sd, "CP free run: %s\n",
  2103. (in_free_run(sd)) ? "on" : "off");
  2104. v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
  2105. io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
  2106. (io_read(sd, 0x01) & 0x70) >> 4);
  2107. v4l2_info(sd, "-----Video Timings-----\n");
  2108. if (read_stdi(sd, &stdi))
  2109. v4l2_info(sd, "STDI: not locked\n");
  2110. else
  2111. v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
  2112. stdi.lcf, stdi.bl, stdi.lcvs,
  2113. stdi.interlaced ? "interlaced" : "progressive",
  2114. stdi.hs_pol, stdi.vs_pol);
  2115. if (adv76xx_query_dv_timings(sd, &timings))
  2116. v4l2_info(sd, "No video detected\n");
  2117. else
  2118. v4l2_print_dv_timings(sd->name, "Detected format: ",
  2119. &timings, true);
  2120. v4l2_print_dv_timings(sd->name, "Configured format: ",
  2121. &state->timings, true);
  2122. if (no_signal(sd))
  2123. return 0;
  2124. v4l2_info(sd, "-----Color space-----\n");
  2125. v4l2_info(sd, "RGB quantization range ctrl: %s\n",
  2126. rgb_quantization_range_txt[state->rgb_quantization_range]);
  2127. v4l2_info(sd, "Input color space: %s\n",
  2128. input_color_space_txt[reg_io_0x02 >> 4]);
  2129. v4l2_info(sd, "Output color space: %s %s, alt-gamma %s\n",
  2130. (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
  2131. (((reg_io_0x02 >> 2) & 0x01) ^ (reg_io_0x02 & 0x01)) ?
  2132. "(16-235)" : "(0-255)",
  2133. (reg_io_0x02 & 0x08) ? "enabled" : "disabled");
  2134. v4l2_info(sd, "Color space conversion: %s\n",
  2135. csc_coeff_sel_rb[cp_read(sd, info->cp_csc) >> 4]);
  2136. if (!is_digital_input(sd))
  2137. return 0;
  2138. v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
  2139. v4l2_info(sd, "Digital video port selected: %c\n",
  2140. (hdmi_read(sd, 0x00) & 0x03) + 'A');
  2141. v4l2_info(sd, "HDCP encrypted content: %s\n",
  2142. (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
  2143. v4l2_info(sd, "HDCP keys read: %s%s\n",
  2144. (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
  2145. (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
  2146. if (is_hdmi(sd)) {
  2147. bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
  2148. bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
  2149. bool audio_mute = io_read(sd, 0x65) & 0x40;
  2150. v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
  2151. audio_pll_locked ? "locked" : "not locked",
  2152. audio_sample_packet_detect ? "detected" : "not detected",
  2153. audio_mute ? "muted" : "enabled");
  2154. if (audio_pll_locked && audio_sample_packet_detect) {
  2155. v4l2_info(sd, "Audio format: %s\n",
  2156. (hdmi_read(sd, 0x07) & 0x20) ? "multi-channel" : "stereo");
  2157. }
  2158. v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
  2159. (hdmi_read(sd, 0x5c) << 8) +
  2160. (hdmi_read(sd, 0x5d) & 0xf0));
  2161. v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
  2162. (hdmi_read(sd, 0x5e) << 8) +
  2163. hdmi_read(sd, 0x5f));
  2164. v4l2_info(sd, "AV Mute: %s\n", (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
  2165. v4l2_info(sd, "Deep color mode: %s\n", deep_color_mode_txt[(hdmi_read(sd, 0x0b) & 0x60) >> 5]);
  2166. v4l2_info(sd, "HDMI colorspace: %s\n", hdmi_color_space_txt[hdmi_read(sd, 0x53) & 0xf]);
  2167. adv76xx_log_infoframes(sd);
  2168. }
  2169. return 0;
  2170. }
  2171. static int adv76xx_subscribe_event(struct v4l2_subdev *sd,
  2172. struct v4l2_fh *fh,
  2173. struct v4l2_event_subscription *sub)
  2174. {
  2175. switch (sub->type) {
  2176. case V4L2_EVENT_SOURCE_CHANGE:
  2177. return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
  2178. case V4L2_EVENT_CTRL:
  2179. return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
  2180. default:
  2181. return -EINVAL;
  2182. }
  2183. }
  2184. static int adv76xx_registered(struct v4l2_subdev *sd)
  2185. {
  2186. struct adv76xx_state *state = to_state(sd);
  2187. int err;
  2188. err = cec_register_adapter(state->cec_adap);
  2189. if (err)
  2190. cec_delete_adapter(state->cec_adap);
  2191. return err;
  2192. }
  2193. static void adv76xx_unregistered(struct v4l2_subdev *sd)
  2194. {
  2195. struct adv76xx_state *state = to_state(sd);
  2196. cec_unregister_adapter(state->cec_adap);
  2197. }
  2198. /* ----------------------------------------------------------------------- */
  2199. static const struct v4l2_ctrl_ops adv76xx_ctrl_ops = {
  2200. .s_ctrl = adv76xx_s_ctrl,
  2201. .g_volatile_ctrl = adv76xx_g_volatile_ctrl,
  2202. };
  2203. static const struct v4l2_subdev_core_ops adv76xx_core_ops = {
  2204. .log_status = adv76xx_log_status,
  2205. .interrupt_service_routine = adv76xx_isr,
  2206. .subscribe_event = adv76xx_subscribe_event,
  2207. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  2208. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2209. .g_register = adv76xx_g_register,
  2210. .s_register = adv76xx_s_register,
  2211. #endif
  2212. };
  2213. static const struct v4l2_subdev_video_ops adv76xx_video_ops = {
  2214. .s_routing = adv76xx_s_routing,
  2215. .g_input_status = adv76xx_g_input_status,
  2216. .s_dv_timings = adv76xx_s_dv_timings,
  2217. .g_dv_timings = adv76xx_g_dv_timings,
  2218. .query_dv_timings = adv76xx_query_dv_timings,
  2219. };
  2220. static const struct v4l2_subdev_pad_ops adv76xx_pad_ops = {
  2221. .enum_mbus_code = adv76xx_enum_mbus_code,
  2222. .get_selection = adv76xx_get_selection,
  2223. .get_fmt = adv76xx_get_format,
  2224. .set_fmt = adv76xx_set_format,
  2225. .get_edid = adv76xx_get_edid,
  2226. .set_edid = adv76xx_set_edid,
  2227. .dv_timings_cap = adv76xx_dv_timings_cap,
  2228. .enum_dv_timings = adv76xx_enum_dv_timings,
  2229. };
  2230. static const struct v4l2_subdev_ops adv76xx_ops = {
  2231. .core = &adv76xx_core_ops,
  2232. .video = &adv76xx_video_ops,
  2233. .pad = &adv76xx_pad_ops,
  2234. };
  2235. static const struct v4l2_subdev_internal_ops adv76xx_int_ops = {
  2236. .registered = adv76xx_registered,
  2237. .unregistered = adv76xx_unregistered,
  2238. };
  2239. /* -------------------------- custom ctrls ---------------------------------- */
  2240. static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
  2241. .ops = &adv76xx_ctrl_ops,
  2242. .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
  2243. .name = "Analog Sampling Phase",
  2244. .type = V4L2_CTRL_TYPE_INTEGER,
  2245. .min = 0,
  2246. .max = 0x1f,
  2247. .step = 1,
  2248. .def = 0,
  2249. };
  2250. static const struct v4l2_ctrl_config adv76xx_ctrl_free_run_color_manual = {
  2251. .ops = &adv76xx_ctrl_ops,
  2252. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
  2253. .name = "Free Running Color, Manual",
  2254. .type = V4L2_CTRL_TYPE_BOOLEAN,
  2255. .min = false,
  2256. .max = true,
  2257. .step = 1,
  2258. .def = false,
  2259. };
  2260. static const struct v4l2_ctrl_config adv76xx_ctrl_free_run_color = {
  2261. .ops = &adv76xx_ctrl_ops,
  2262. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
  2263. .name = "Free Running Color",
  2264. .type = V4L2_CTRL_TYPE_INTEGER,
  2265. .min = 0x0,
  2266. .max = 0xffffff,
  2267. .step = 0x1,
  2268. .def = 0x0,
  2269. };
  2270. /* ----------------------------------------------------------------------- */
  2271. static int adv76xx_core_init(struct v4l2_subdev *sd)
  2272. {
  2273. struct adv76xx_state *state = to_state(sd);
  2274. const struct adv76xx_chip_info *info = state->info;
  2275. struct adv76xx_platform_data *pdata = &state->pdata;
  2276. hdmi_write(sd, 0x48,
  2277. (pdata->disable_pwrdnb ? 0x80 : 0) |
  2278. (pdata->disable_cable_det_rst ? 0x40 : 0));
  2279. disable_input(sd);
  2280. if (pdata->default_input >= 0 &&
  2281. pdata->default_input < state->source_pad) {
  2282. state->selected_input = pdata->default_input;
  2283. select_input(sd);
  2284. enable_input(sd);
  2285. }
  2286. /* power */
  2287. io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
  2288. io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
  2289. cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
  2290. /* video format */
  2291. io_write_clr_set(sd, 0x02, 0x0f, pdata->alt_gamma << 3);
  2292. io_write_clr_set(sd, 0x05, 0x0e, pdata->blank_data << 3 |
  2293. pdata->insert_av_codes << 2 |
  2294. pdata->replicate_av_codes << 1);
  2295. adv76xx_setup_format(state);
  2296. cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
  2297. /* VS, HS polarities */
  2298. io_write(sd, 0x06, 0xa0 | pdata->inv_vs_pol << 2 |
  2299. pdata->inv_hs_pol << 1 | pdata->inv_llc_pol);
  2300. /* Adjust drive strength */
  2301. io_write(sd, 0x14, 0x40 | pdata->dr_str_data << 4 |
  2302. pdata->dr_str_clk << 2 |
  2303. pdata->dr_str_sync);
  2304. cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
  2305. cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
  2306. cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
  2307. ADI recommended setting [REF_01, c. 2.3.3] */
  2308. cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
  2309. ADI recommended setting [REF_01, c. 2.3.3] */
  2310. cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
  2311. for digital formats */
  2312. /* HDMI audio */
  2313. hdmi_write_clr_set(sd, 0x15, 0x03, 0x03); /* Mute on FIFO over-/underflow [REF_01, c. 1.2.18] */
  2314. hdmi_write_clr_set(sd, 0x1a, 0x0e, 0x08); /* Wait 1 s before unmute */
  2315. hdmi_write_clr_set(sd, 0x68, 0x06, 0x06); /* FIFO reset on over-/underflow [REF_01, c. 1.2.19] */
  2316. /* TODO from platform data */
  2317. afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
  2318. if (adv76xx_has_afe(state)) {
  2319. afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
  2320. io_write_clr_set(sd, 0x30, 1 << 4, pdata->output_bus_lsb_to_msb << 4);
  2321. }
  2322. /* interrupts */
  2323. io_write(sd, 0x40, 0xc0 | pdata->int1_config); /* Configure INT1 */
  2324. io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
  2325. io_write(sd, 0x6e, info->fmt_change_digital_mask); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
  2326. io_write(sd, 0x73, info->cable_det_mask); /* Enable cable detection (+5v) interrupts */
  2327. info->setup_irqs(sd);
  2328. return v4l2_ctrl_handler_setup(sd->ctrl_handler);
  2329. }
  2330. static void adv7604_setup_irqs(struct v4l2_subdev *sd)
  2331. {
  2332. io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
  2333. }
  2334. static void adv7611_setup_irqs(struct v4l2_subdev *sd)
  2335. {
  2336. io_write(sd, 0x41, 0xd0); /* STDI irq for any change, disable INT2 */
  2337. }
  2338. static void adv7612_setup_irqs(struct v4l2_subdev *sd)
  2339. {
  2340. io_write(sd, 0x41, 0xd0); /* disable INT2 */
  2341. }
  2342. static void adv76xx_unregister_clients(struct adv76xx_state *state)
  2343. {
  2344. unsigned int i;
  2345. for (i = 1; i < ARRAY_SIZE(state->i2c_clients); ++i) {
  2346. if (state->i2c_clients[i])
  2347. i2c_unregister_device(state->i2c_clients[i]);
  2348. }
  2349. }
  2350. static struct i2c_client *adv76xx_dummy_client(struct v4l2_subdev *sd,
  2351. u8 addr, u8 io_reg)
  2352. {
  2353. struct i2c_client *client = v4l2_get_subdevdata(sd);
  2354. if (addr)
  2355. io_write(sd, io_reg, addr << 1);
  2356. return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
  2357. }
  2358. static const struct adv76xx_reg_seq adv7604_recommended_settings_afe[] = {
  2359. /* reset ADI recommended settings for HDMI: */
  2360. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
  2361. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
  2362. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
  2363. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x3d), 0x00 }, /* DDC bus active pull-up control */
  2364. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x3e), 0x74 }, /* TMDS PLL optimization */
  2365. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
  2366. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x57), 0x74 }, /* TMDS PLL optimization */
  2367. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x58), 0x63 }, /* TMDS PLL optimization */
  2368. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
  2369. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
  2370. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x93), 0x88 }, /* equaliser */
  2371. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x94), 0x2e }, /* equaliser */
  2372. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x96), 0x00 }, /* enable automatic EQ changing */
  2373. /* set ADI recommended settings for digitizer */
  2374. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
  2375. { ADV76XX_REG(ADV76XX_PAGE_AFE, 0x12), 0x7b }, /* ADC noise shaping filter controls */
  2376. { ADV76XX_REG(ADV76XX_PAGE_AFE, 0x0c), 0x1f }, /* CP core gain controls */
  2377. { ADV76XX_REG(ADV76XX_PAGE_CP, 0x3e), 0x04 }, /* CP core pre-gain control */
  2378. { ADV76XX_REG(ADV76XX_PAGE_CP, 0xc3), 0x39 }, /* CP coast control. Graphics mode */
  2379. { ADV76XX_REG(ADV76XX_PAGE_CP, 0x40), 0x5c }, /* CP core pre-gain control. Graphics mode */
  2380. { ADV76XX_REG_SEQ_TERM, 0 },
  2381. };
  2382. static const struct adv76xx_reg_seq adv7604_recommended_settings_hdmi[] = {
  2383. /* set ADI recommended settings for HDMI: */
  2384. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
  2385. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x0d), 0x84 }, /* HDMI filter optimization */
  2386. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x3d), 0x10 }, /* DDC bus active pull-up control */
  2387. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x3e), 0x39 }, /* TMDS PLL optimization */
  2388. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
  2389. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x57), 0xb6 }, /* TMDS PLL optimization */
  2390. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x58), 0x03 }, /* TMDS PLL optimization */
  2391. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
  2392. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
  2393. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x93), 0x8b }, /* equaliser */
  2394. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x94), 0x2d }, /* equaliser */
  2395. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x96), 0x01 }, /* enable automatic EQ changing */
  2396. /* reset ADI recommended settings for digitizer */
  2397. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
  2398. { ADV76XX_REG(ADV76XX_PAGE_AFE, 0x12), 0xfb }, /* ADC noise shaping filter controls */
  2399. { ADV76XX_REG(ADV76XX_PAGE_AFE, 0x0c), 0x0d }, /* CP core gain controls */
  2400. { ADV76XX_REG_SEQ_TERM, 0 },
  2401. };
  2402. static const struct adv76xx_reg_seq adv7611_recommended_settings_hdmi[] = {
  2403. /* ADV7611 Register Settings Recommendations Rev 1.5, May 2014 */
  2404. { ADV76XX_REG(ADV76XX_PAGE_CP, 0x6c), 0x00 },
  2405. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x9b), 0x03 },
  2406. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x6f), 0x08 },
  2407. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x85), 0x1f },
  2408. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x87), 0x70 },
  2409. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x57), 0xda },
  2410. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x58), 0x01 },
  2411. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x03), 0x98 },
  2412. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x4c), 0x44 },
  2413. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x8d), 0x04 },
  2414. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x8e), 0x1e },
  2415. { ADV76XX_REG_SEQ_TERM, 0 },
  2416. };
  2417. static const struct adv76xx_reg_seq adv7612_recommended_settings_hdmi[] = {
  2418. { ADV76XX_REG(ADV76XX_PAGE_CP, 0x6c), 0x00 },
  2419. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x9b), 0x03 },
  2420. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x6f), 0x08 },
  2421. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x85), 0x1f },
  2422. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x87), 0x70 },
  2423. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x57), 0xda },
  2424. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x58), 0x01 },
  2425. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x03), 0x98 },
  2426. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x4c), 0x44 },
  2427. { ADV76XX_REG_SEQ_TERM, 0 },
  2428. };
  2429. static const struct adv76xx_chip_info adv76xx_chip_info[] = {
  2430. [ADV7604] = {
  2431. .type = ADV7604,
  2432. .has_afe = true,
  2433. .max_port = ADV7604_PAD_VGA_COMP,
  2434. .num_dv_ports = 4,
  2435. .edid_enable_reg = 0x77,
  2436. .edid_status_reg = 0x7d,
  2437. .lcf_reg = 0xb3,
  2438. .tdms_lock_mask = 0xe0,
  2439. .cable_det_mask = 0x1e,
  2440. .fmt_change_digital_mask = 0xc1,
  2441. .cp_csc = 0xfc,
  2442. .formats = adv7604_formats,
  2443. .nformats = ARRAY_SIZE(adv7604_formats),
  2444. .set_termination = adv7604_set_termination,
  2445. .setup_irqs = adv7604_setup_irqs,
  2446. .read_hdmi_pixelclock = adv7604_read_hdmi_pixelclock,
  2447. .read_cable_det = adv7604_read_cable_det,
  2448. .recommended_settings = {
  2449. [0] = adv7604_recommended_settings_afe,
  2450. [1] = adv7604_recommended_settings_hdmi,
  2451. },
  2452. .num_recommended_settings = {
  2453. [0] = ARRAY_SIZE(adv7604_recommended_settings_afe),
  2454. [1] = ARRAY_SIZE(adv7604_recommended_settings_hdmi),
  2455. },
  2456. .page_mask = BIT(ADV76XX_PAGE_IO) | BIT(ADV7604_PAGE_AVLINK) |
  2457. BIT(ADV76XX_PAGE_CEC) | BIT(ADV76XX_PAGE_INFOFRAME) |
  2458. BIT(ADV7604_PAGE_ESDP) | BIT(ADV7604_PAGE_DPP) |
  2459. BIT(ADV76XX_PAGE_AFE) | BIT(ADV76XX_PAGE_REP) |
  2460. BIT(ADV76XX_PAGE_EDID) | BIT(ADV76XX_PAGE_HDMI) |
  2461. BIT(ADV76XX_PAGE_TEST) | BIT(ADV76XX_PAGE_CP) |
  2462. BIT(ADV7604_PAGE_VDP),
  2463. .linewidth_mask = 0xfff,
  2464. .field0_height_mask = 0xfff,
  2465. .field1_height_mask = 0xfff,
  2466. .hfrontporch_mask = 0x3ff,
  2467. .hsync_mask = 0x3ff,
  2468. .hbackporch_mask = 0x3ff,
  2469. .field0_vfrontporch_mask = 0x1fff,
  2470. .field0_vsync_mask = 0x1fff,
  2471. .field0_vbackporch_mask = 0x1fff,
  2472. .field1_vfrontporch_mask = 0x1fff,
  2473. .field1_vsync_mask = 0x1fff,
  2474. .field1_vbackporch_mask = 0x1fff,
  2475. },
  2476. [ADV7611] = {
  2477. .type = ADV7611,
  2478. .has_afe = false,
  2479. .max_port = ADV76XX_PAD_HDMI_PORT_A,
  2480. .num_dv_ports = 1,
  2481. .edid_enable_reg = 0x74,
  2482. .edid_status_reg = 0x76,
  2483. .lcf_reg = 0xa3,
  2484. .tdms_lock_mask = 0x43,
  2485. .cable_det_mask = 0x01,
  2486. .fmt_change_digital_mask = 0x03,
  2487. .cp_csc = 0xf4,
  2488. .formats = adv7611_formats,
  2489. .nformats = ARRAY_SIZE(adv7611_formats),
  2490. .set_termination = adv7611_set_termination,
  2491. .setup_irqs = adv7611_setup_irqs,
  2492. .read_hdmi_pixelclock = adv7611_read_hdmi_pixelclock,
  2493. .read_cable_det = adv7611_read_cable_det,
  2494. .recommended_settings = {
  2495. [1] = adv7611_recommended_settings_hdmi,
  2496. },
  2497. .num_recommended_settings = {
  2498. [1] = ARRAY_SIZE(adv7611_recommended_settings_hdmi),
  2499. },
  2500. .page_mask = BIT(ADV76XX_PAGE_IO) | BIT(ADV76XX_PAGE_CEC) |
  2501. BIT(ADV76XX_PAGE_INFOFRAME) | BIT(ADV76XX_PAGE_AFE) |
  2502. BIT(ADV76XX_PAGE_REP) | BIT(ADV76XX_PAGE_EDID) |
  2503. BIT(ADV76XX_PAGE_HDMI) | BIT(ADV76XX_PAGE_CP),
  2504. .linewidth_mask = 0x1fff,
  2505. .field0_height_mask = 0x1fff,
  2506. .field1_height_mask = 0x1fff,
  2507. .hfrontporch_mask = 0x1fff,
  2508. .hsync_mask = 0x1fff,
  2509. .hbackporch_mask = 0x1fff,
  2510. .field0_vfrontporch_mask = 0x3fff,
  2511. .field0_vsync_mask = 0x3fff,
  2512. .field0_vbackporch_mask = 0x3fff,
  2513. .field1_vfrontporch_mask = 0x3fff,
  2514. .field1_vsync_mask = 0x3fff,
  2515. .field1_vbackporch_mask = 0x3fff,
  2516. },
  2517. [ADV7612] = {
  2518. .type = ADV7612,
  2519. .has_afe = false,
  2520. .max_port = ADV76XX_PAD_HDMI_PORT_A, /* B not supported */
  2521. .num_dv_ports = 1, /* normally 2 */
  2522. .edid_enable_reg = 0x74,
  2523. .edid_status_reg = 0x76,
  2524. .lcf_reg = 0xa3,
  2525. .tdms_lock_mask = 0x43,
  2526. .cable_det_mask = 0x01,
  2527. .fmt_change_digital_mask = 0x03,
  2528. .cp_csc = 0xf4,
  2529. .formats = adv7612_formats,
  2530. .nformats = ARRAY_SIZE(adv7612_formats),
  2531. .set_termination = adv7611_set_termination,
  2532. .setup_irqs = adv7612_setup_irqs,
  2533. .read_hdmi_pixelclock = adv7611_read_hdmi_pixelclock,
  2534. .read_cable_det = adv7612_read_cable_det,
  2535. .recommended_settings = {
  2536. [1] = adv7612_recommended_settings_hdmi,
  2537. },
  2538. .num_recommended_settings = {
  2539. [1] = ARRAY_SIZE(adv7612_recommended_settings_hdmi),
  2540. },
  2541. .page_mask = BIT(ADV76XX_PAGE_IO) | BIT(ADV76XX_PAGE_CEC) |
  2542. BIT(ADV76XX_PAGE_INFOFRAME) | BIT(ADV76XX_PAGE_AFE) |
  2543. BIT(ADV76XX_PAGE_REP) | BIT(ADV76XX_PAGE_EDID) |
  2544. BIT(ADV76XX_PAGE_HDMI) | BIT(ADV76XX_PAGE_CP),
  2545. .linewidth_mask = 0x1fff,
  2546. .field0_height_mask = 0x1fff,
  2547. .field1_height_mask = 0x1fff,
  2548. .hfrontporch_mask = 0x1fff,
  2549. .hsync_mask = 0x1fff,
  2550. .hbackporch_mask = 0x1fff,
  2551. .field0_vfrontporch_mask = 0x3fff,
  2552. .field0_vsync_mask = 0x3fff,
  2553. .field0_vbackporch_mask = 0x3fff,
  2554. .field1_vfrontporch_mask = 0x3fff,
  2555. .field1_vsync_mask = 0x3fff,
  2556. .field1_vbackporch_mask = 0x3fff,
  2557. },
  2558. };
  2559. static const struct i2c_device_id adv76xx_i2c_id[] = {
  2560. { "adv7604", (kernel_ulong_t)&adv76xx_chip_info[ADV7604] },
  2561. { "adv7611", (kernel_ulong_t)&adv76xx_chip_info[ADV7611] },
  2562. { "adv7612", (kernel_ulong_t)&adv76xx_chip_info[ADV7612] },
  2563. { }
  2564. };
  2565. MODULE_DEVICE_TABLE(i2c, adv76xx_i2c_id);
  2566. static const struct of_device_id adv76xx_of_id[] __maybe_unused = {
  2567. { .compatible = "adi,adv7611", .data = &adv76xx_chip_info[ADV7611] },
  2568. { .compatible = "adi,adv7612", .data = &adv76xx_chip_info[ADV7612] },
  2569. { }
  2570. };
  2571. MODULE_DEVICE_TABLE(of, adv76xx_of_id);
  2572. static int adv76xx_parse_dt(struct adv76xx_state *state)
  2573. {
  2574. struct v4l2_of_endpoint bus_cfg;
  2575. struct device_node *endpoint;
  2576. struct device_node *np;
  2577. unsigned int flags;
  2578. int ret;
  2579. u32 v;
  2580. np = state->i2c_clients[ADV76XX_PAGE_IO]->dev.of_node;
  2581. /* Parse the endpoint. */
  2582. endpoint = of_graph_get_next_endpoint(np, NULL);
  2583. if (!endpoint)
  2584. return -EINVAL;
  2585. ret = v4l2_of_parse_endpoint(endpoint, &bus_cfg);
  2586. if (ret) {
  2587. of_node_put(endpoint);
  2588. return ret;
  2589. }
  2590. if (!of_property_read_u32(endpoint, "default-input", &v))
  2591. state->pdata.default_input = v;
  2592. else
  2593. state->pdata.default_input = -1;
  2594. of_node_put(endpoint);
  2595. flags = bus_cfg.bus.parallel.flags;
  2596. if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  2597. state->pdata.inv_hs_pol = 1;
  2598. if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  2599. state->pdata.inv_vs_pol = 1;
  2600. if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  2601. state->pdata.inv_llc_pol = 1;
  2602. if (bus_cfg.bus_type == V4L2_MBUS_BT656)
  2603. state->pdata.insert_av_codes = 1;
  2604. /* Disable the interrupt for now as no DT-based board uses it. */
  2605. state->pdata.int1_config = ADV76XX_INT1_CONFIG_DISABLED;
  2606. /* Use the default I2C addresses. */
  2607. state->pdata.i2c_addresses[ADV7604_PAGE_AVLINK] = 0x42;
  2608. state->pdata.i2c_addresses[ADV76XX_PAGE_CEC] = 0x40;
  2609. state->pdata.i2c_addresses[ADV76XX_PAGE_INFOFRAME] = 0x3e;
  2610. state->pdata.i2c_addresses[ADV7604_PAGE_ESDP] = 0x38;
  2611. state->pdata.i2c_addresses[ADV7604_PAGE_DPP] = 0x3c;
  2612. state->pdata.i2c_addresses[ADV76XX_PAGE_AFE] = 0x26;
  2613. state->pdata.i2c_addresses[ADV76XX_PAGE_REP] = 0x32;
  2614. state->pdata.i2c_addresses[ADV76XX_PAGE_EDID] = 0x36;
  2615. state->pdata.i2c_addresses[ADV76XX_PAGE_HDMI] = 0x34;
  2616. state->pdata.i2c_addresses[ADV76XX_PAGE_TEST] = 0x30;
  2617. state->pdata.i2c_addresses[ADV76XX_PAGE_CP] = 0x22;
  2618. state->pdata.i2c_addresses[ADV7604_PAGE_VDP] = 0x24;
  2619. /* Hardcode the remaining platform data fields. */
  2620. state->pdata.disable_pwrdnb = 0;
  2621. state->pdata.disable_cable_det_rst = 0;
  2622. state->pdata.blank_data = 1;
  2623. state->pdata.op_format_mode_sel = ADV7604_OP_FORMAT_MODE0;
  2624. state->pdata.bus_order = ADV7604_BUS_ORDER_RGB;
  2625. return 0;
  2626. }
  2627. static const struct regmap_config adv76xx_regmap_cnf[] = {
  2628. {
  2629. .name = "io",
  2630. .reg_bits = 8,
  2631. .val_bits = 8,
  2632. .max_register = 0xff,
  2633. .cache_type = REGCACHE_NONE,
  2634. },
  2635. {
  2636. .name = "avlink",
  2637. .reg_bits = 8,
  2638. .val_bits = 8,
  2639. .max_register = 0xff,
  2640. .cache_type = REGCACHE_NONE,
  2641. },
  2642. {
  2643. .name = "cec",
  2644. .reg_bits = 8,
  2645. .val_bits = 8,
  2646. .max_register = 0xff,
  2647. .cache_type = REGCACHE_NONE,
  2648. },
  2649. {
  2650. .name = "infoframe",
  2651. .reg_bits = 8,
  2652. .val_bits = 8,
  2653. .max_register = 0xff,
  2654. .cache_type = REGCACHE_NONE,
  2655. },
  2656. {
  2657. .name = "esdp",
  2658. .reg_bits = 8,
  2659. .val_bits = 8,
  2660. .max_register = 0xff,
  2661. .cache_type = REGCACHE_NONE,
  2662. },
  2663. {
  2664. .name = "epp",
  2665. .reg_bits = 8,
  2666. .val_bits = 8,
  2667. .max_register = 0xff,
  2668. .cache_type = REGCACHE_NONE,
  2669. },
  2670. {
  2671. .name = "afe",
  2672. .reg_bits = 8,
  2673. .val_bits = 8,
  2674. .max_register = 0xff,
  2675. .cache_type = REGCACHE_NONE,
  2676. },
  2677. {
  2678. .name = "rep",
  2679. .reg_bits = 8,
  2680. .val_bits = 8,
  2681. .max_register = 0xff,
  2682. .cache_type = REGCACHE_NONE,
  2683. },
  2684. {
  2685. .name = "edid",
  2686. .reg_bits = 8,
  2687. .val_bits = 8,
  2688. .max_register = 0xff,
  2689. .cache_type = REGCACHE_NONE,
  2690. },
  2691. {
  2692. .name = "hdmi",
  2693. .reg_bits = 8,
  2694. .val_bits = 8,
  2695. .max_register = 0xff,
  2696. .cache_type = REGCACHE_NONE,
  2697. },
  2698. {
  2699. .name = "test",
  2700. .reg_bits = 8,
  2701. .val_bits = 8,
  2702. .max_register = 0xff,
  2703. .cache_type = REGCACHE_NONE,
  2704. },
  2705. {
  2706. .name = "cp",
  2707. .reg_bits = 8,
  2708. .val_bits = 8,
  2709. .max_register = 0xff,
  2710. .cache_type = REGCACHE_NONE,
  2711. },
  2712. {
  2713. .name = "vdp",
  2714. .reg_bits = 8,
  2715. .val_bits = 8,
  2716. .max_register = 0xff,
  2717. .cache_type = REGCACHE_NONE,
  2718. },
  2719. };
  2720. static int configure_regmap(struct adv76xx_state *state, int region)
  2721. {
  2722. int err;
  2723. if (!state->i2c_clients[region])
  2724. return -ENODEV;
  2725. state->regmap[region] =
  2726. devm_regmap_init_i2c(state->i2c_clients[region],
  2727. &adv76xx_regmap_cnf[region]);
  2728. if (IS_ERR(state->regmap[region])) {
  2729. err = PTR_ERR(state->regmap[region]);
  2730. v4l_err(state->i2c_clients[region],
  2731. "Error initializing regmap %d with error %d\n",
  2732. region, err);
  2733. return -EINVAL;
  2734. }
  2735. return 0;
  2736. }
  2737. static int configure_regmaps(struct adv76xx_state *state)
  2738. {
  2739. int i, err;
  2740. for (i = ADV7604_PAGE_AVLINK ; i < ADV76XX_PAGE_MAX; i++) {
  2741. err = configure_regmap(state, i);
  2742. if (err && (err != -ENODEV))
  2743. return err;
  2744. }
  2745. return 0;
  2746. }
  2747. static void adv76xx_reset(struct adv76xx_state *state)
  2748. {
  2749. if (state->reset_gpio) {
  2750. /* ADV76XX can be reset by a low reset pulse of minimum 5 ms. */
  2751. gpiod_set_value_cansleep(state->reset_gpio, 0);
  2752. usleep_range(5000, 10000);
  2753. gpiod_set_value_cansleep(state->reset_gpio, 1);
  2754. /* It is recommended to wait 5 ms after the low pulse before */
  2755. /* an I2C write is performed to the ADV76XX. */
  2756. usleep_range(5000, 10000);
  2757. }
  2758. }
  2759. static int adv76xx_probe(struct i2c_client *client,
  2760. const struct i2c_device_id *id)
  2761. {
  2762. static const struct v4l2_dv_timings cea640x480 =
  2763. V4L2_DV_BT_CEA_640X480P59_94;
  2764. struct adv76xx_state *state;
  2765. struct v4l2_ctrl_handler *hdl;
  2766. struct v4l2_ctrl *ctrl;
  2767. struct v4l2_subdev *sd;
  2768. unsigned int i;
  2769. unsigned int val, val2;
  2770. int err;
  2771. /* Check if the adapter supports the needed features */
  2772. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  2773. return -EIO;
  2774. v4l_dbg(1, debug, client, "detecting adv76xx client on address 0x%x\n",
  2775. client->addr << 1);
  2776. state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
  2777. if (!state) {
  2778. v4l_err(client, "Could not allocate adv76xx_state memory!\n");
  2779. return -ENOMEM;
  2780. }
  2781. state->i2c_clients[ADV76XX_PAGE_IO] = client;
  2782. /* initialize variables */
  2783. state->restart_stdi_once = true;
  2784. state->selected_input = ~0;
  2785. if (IS_ENABLED(CONFIG_OF) && client->dev.of_node) {
  2786. const struct of_device_id *oid;
  2787. oid = of_match_node(adv76xx_of_id, client->dev.of_node);
  2788. state->info = oid->data;
  2789. err = adv76xx_parse_dt(state);
  2790. if (err < 0) {
  2791. v4l_err(client, "DT parsing error\n");
  2792. return err;
  2793. }
  2794. } else if (client->dev.platform_data) {
  2795. struct adv76xx_platform_data *pdata = client->dev.platform_data;
  2796. state->info = (const struct adv76xx_chip_info *)id->driver_data;
  2797. state->pdata = *pdata;
  2798. } else {
  2799. v4l_err(client, "No platform data!\n");
  2800. return -ENODEV;
  2801. }
  2802. /* Request GPIOs. */
  2803. for (i = 0; i < state->info->num_dv_ports; ++i) {
  2804. state->hpd_gpio[i] =
  2805. devm_gpiod_get_index_optional(&client->dev, "hpd", i,
  2806. GPIOD_OUT_LOW);
  2807. if (IS_ERR(state->hpd_gpio[i]))
  2808. return PTR_ERR(state->hpd_gpio[i]);
  2809. if (state->hpd_gpio[i])
  2810. v4l_info(client, "Handling HPD %u GPIO\n", i);
  2811. }
  2812. state->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  2813. GPIOD_OUT_HIGH);
  2814. if (IS_ERR(state->reset_gpio))
  2815. return PTR_ERR(state->reset_gpio);
  2816. adv76xx_reset(state);
  2817. state->timings = cea640x480;
  2818. state->format = adv76xx_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
  2819. sd = &state->sd;
  2820. v4l2_i2c_subdev_init(sd, client, &adv76xx_ops);
  2821. snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
  2822. id->name, i2c_adapter_id(client->adapter),
  2823. client->addr);
  2824. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  2825. sd->internal_ops = &adv76xx_int_ops;
  2826. /* Configure IO Regmap region */
  2827. err = configure_regmap(state, ADV76XX_PAGE_IO);
  2828. if (err) {
  2829. v4l2_err(sd, "Error configuring IO regmap region\n");
  2830. return -ENODEV;
  2831. }
  2832. /*
  2833. * Verify that the chip is present. On ADV7604 the RD_INFO register only
  2834. * identifies the revision, while on ADV7611 it identifies the model as
  2835. * well. Use the HDMI slave address on ADV7604 and RD_INFO on ADV7611.
  2836. */
  2837. switch (state->info->type) {
  2838. case ADV7604:
  2839. err = regmap_read(state->regmap[ADV76XX_PAGE_IO], 0xfb, &val);
  2840. if (err) {
  2841. v4l2_err(sd, "Error %d reading IO Regmap\n", err);
  2842. return -ENODEV;
  2843. }
  2844. if (val != 0x68) {
  2845. v4l2_err(sd, "not an adv7604 on address 0x%x\n",
  2846. client->addr << 1);
  2847. return -ENODEV;
  2848. }
  2849. break;
  2850. case ADV7611:
  2851. case ADV7612:
  2852. err = regmap_read(state->regmap[ADV76XX_PAGE_IO],
  2853. 0xea,
  2854. &val);
  2855. if (err) {
  2856. v4l2_err(sd, "Error %d reading IO Regmap\n", err);
  2857. return -ENODEV;
  2858. }
  2859. val2 = val << 8;
  2860. err = regmap_read(state->regmap[ADV76XX_PAGE_IO],
  2861. 0xeb,
  2862. &val);
  2863. if (err) {
  2864. v4l2_err(sd, "Error %d reading IO Regmap\n", err);
  2865. return -ENODEV;
  2866. }
  2867. val |= val2;
  2868. if ((state->info->type == ADV7611 && val != 0x2051) ||
  2869. (state->info->type == ADV7612 && val != 0x2041)) {
  2870. v4l2_err(sd, "not an adv761x on address 0x%x\n",
  2871. client->addr << 1);
  2872. return -ENODEV;
  2873. }
  2874. break;
  2875. }
  2876. /* control handlers */
  2877. hdl = &state->hdl;
  2878. v4l2_ctrl_handler_init(hdl, adv76xx_has_afe(state) ? 9 : 8);
  2879. v4l2_ctrl_new_std(hdl, &adv76xx_ctrl_ops,
  2880. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  2881. v4l2_ctrl_new_std(hdl, &adv76xx_ctrl_ops,
  2882. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  2883. v4l2_ctrl_new_std(hdl, &adv76xx_ctrl_ops,
  2884. V4L2_CID_SATURATION, 0, 255, 1, 128);
  2885. v4l2_ctrl_new_std(hdl, &adv76xx_ctrl_ops,
  2886. V4L2_CID_HUE, 0, 128, 1, 0);
  2887. ctrl = v4l2_ctrl_new_std_menu(hdl, &adv76xx_ctrl_ops,
  2888. V4L2_CID_DV_RX_IT_CONTENT_TYPE, V4L2_DV_IT_CONTENT_TYPE_NO_ITC,
  2889. 0, V4L2_DV_IT_CONTENT_TYPE_NO_ITC);
  2890. if (ctrl)
  2891. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  2892. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  2893. V4L2_CID_DV_RX_POWER_PRESENT, 0,
  2894. (1 << state->info->num_dv_ports) - 1, 0, 0);
  2895. state->rgb_quantization_range_ctrl =
  2896. v4l2_ctrl_new_std_menu(hdl, &adv76xx_ctrl_ops,
  2897. V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
  2898. 0, V4L2_DV_RGB_RANGE_AUTO);
  2899. /* custom controls */
  2900. if (adv76xx_has_afe(state))
  2901. state->analog_sampling_phase_ctrl =
  2902. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
  2903. state->free_run_color_manual_ctrl =
  2904. v4l2_ctrl_new_custom(hdl, &adv76xx_ctrl_free_run_color_manual, NULL);
  2905. state->free_run_color_ctrl =
  2906. v4l2_ctrl_new_custom(hdl, &adv76xx_ctrl_free_run_color, NULL);
  2907. sd->ctrl_handler = hdl;
  2908. if (hdl->error) {
  2909. err = hdl->error;
  2910. goto err_hdl;
  2911. }
  2912. if (adv76xx_s_detect_tx_5v_ctrl(sd)) {
  2913. err = -ENODEV;
  2914. goto err_hdl;
  2915. }
  2916. for (i = 1; i < ADV76XX_PAGE_MAX; ++i) {
  2917. if (!(BIT(i) & state->info->page_mask))
  2918. continue;
  2919. state->i2c_clients[i] =
  2920. adv76xx_dummy_client(sd, state->pdata.i2c_addresses[i],
  2921. 0xf2 + i);
  2922. if (state->i2c_clients[i] == NULL) {
  2923. err = -ENOMEM;
  2924. v4l2_err(sd, "failed to create i2c client %u\n", i);
  2925. goto err_i2c;
  2926. }
  2927. }
  2928. INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
  2929. adv76xx_delayed_work_enable_hotplug);
  2930. state->source_pad = state->info->num_dv_ports
  2931. + (state->info->has_afe ? 2 : 0);
  2932. for (i = 0; i < state->source_pad; ++i)
  2933. state->pads[i].flags = MEDIA_PAD_FL_SINK;
  2934. state->pads[state->source_pad].flags = MEDIA_PAD_FL_SOURCE;
  2935. err = media_entity_pads_init(&sd->entity, state->source_pad + 1,
  2936. state->pads);
  2937. if (err)
  2938. goto err_work_queues;
  2939. /* Configure regmaps */
  2940. err = configure_regmaps(state);
  2941. if (err)
  2942. goto err_entity;
  2943. err = adv76xx_core_init(sd);
  2944. if (err)
  2945. goto err_entity;
  2946. #if IS_ENABLED(CONFIG_VIDEO_ADV7604_CEC)
  2947. state->cec_adap = cec_allocate_adapter(&adv76xx_cec_adap_ops,
  2948. state, dev_name(&client->dev),
  2949. CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS |
  2950. CEC_CAP_PASSTHROUGH | CEC_CAP_RC, ADV76XX_MAX_ADDRS,
  2951. &client->dev);
  2952. err = PTR_ERR_OR_ZERO(state->cec_adap);
  2953. if (err)
  2954. goto err_entity;
  2955. #endif
  2956. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  2957. client->addr << 1, client->adapter->name);
  2958. err = v4l2_async_register_subdev(sd);
  2959. if (err)
  2960. goto err_entity;
  2961. return 0;
  2962. err_entity:
  2963. media_entity_cleanup(&sd->entity);
  2964. err_work_queues:
  2965. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  2966. err_i2c:
  2967. adv76xx_unregister_clients(state);
  2968. err_hdl:
  2969. v4l2_ctrl_handler_free(hdl);
  2970. return err;
  2971. }
  2972. /* ----------------------------------------------------------------------- */
  2973. static int adv76xx_remove(struct i2c_client *client)
  2974. {
  2975. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2976. struct adv76xx_state *state = to_state(sd);
  2977. /* disable interrupts */
  2978. io_write(sd, 0x40, 0);
  2979. io_write(sd, 0x41, 0);
  2980. io_write(sd, 0x46, 0);
  2981. io_write(sd, 0x6e, 0);
  2982. io_write(sd, 0x73, 0);
  2983. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  2984. v4l2_async_unregister_subdev(sd);
  2985. media_entity_cleanup(&sd->entity);
  2986. adv76xx_unregister_clients(to_state(sd));
  2987. v4l2_ctrl_handler_free(sd->ctrl_handler);
  2988. return 0;
  2989. }
  2990. /* ----------------------------------------------------------------------- */
  2991. static struct i2c_driver adv76xx_driver = {
  2992. .driver = {
  2993. .name = "adv7604",
  2994. .of_match_table = of_match_ptr(adv76xx_of_id),
  2995. },
  2996. .probe = adv76xx_probe,
  2997. .remove = adv76xx_remove,
  2998. .id_table = adv76xx_i2c_id,
  2999. };
  3000. module_i2c_driver(adv76xx_driver);