adv7842.c 102 KB

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