adv7604.c 107 KB

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