adv7604.c 96 KB

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