adv7604.c 109 KB

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