adv7604.c 104 KB

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