adv7842.c 103 KB

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