adv7604.c 104 KB

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