adv7842.c 104 KB

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