adv7604.c 104 KB

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