adv7842.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221
  1. /*
  2. * adv7842 - Analog Devices ADV7842 video decoder driver
  3. *
  4. * Copyright 2013 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, ADV7842,
  23. * Register Settings Recommendations, Rev. 1.9, April 2011
  24. * REF_02 - Analog devices, Software User Guide, UG-206,
  25. * ADV7842 I2C Register Maps, Rev. 0, November 2010
  26. * REF_03 - Analog devices, Hardware User Guide, UG-214,
  27. * ADV7842 Fast Switching 2:1 HDMI 1.4 Receiver with 3D-Comb
  28. * Decoder and Digitizer , Rev. 0, January 2011
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/slab.h>
  33. #include <linux/i2c.h>
  34. #include <linux/delay.h>
  35. #include <linux/videodev2.h>
  36. #include <linux/workqueue.h>
  37. #include <linux/v4l2-dv-timings.h>
  38. #include <media/v4l2-device.h>
  39. #include <media/v4l2-ctrls.h>
  40. #include <media/v4l2-dv-timings.h>
  41. #include <media/adv7842.h>
  42. static int debug;
  43. module_param(debug, int, 0644);
  44. MODULE_PARM_DESC(debug, "debug level (0-2)");
  45. MODULE_DESCRIPTION("Analog Devices ADV7842 video decoder driver");
  46. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
  47. MODULE_AUTHOR("Martin Bugge <marbugge@cisco.com>");
  48. MODULE_LICENSE("GPL");
  49. /* ADV7842 system clock frequency */
  50. #define ADV7842_fsc (28636360)
  51. /*
  52. **********************************************************************
  53. *
  54. * Arrays with configuration parameters for the ADV7842
  55. *
  56. **********************************************************************
  57. */
  58. struct adv7842_state {
  59. struct adv7842_platform_data pdata;
  60. struct v4l2_subdev sd;
  61. struct media_pad pad;
  62. struct v4l2_ctrl_handler hdl;
  63. enum adv7842_mode mode;
  64. struct v4l2_dv_timings timings;
  65. enum adv7842_vid_std_select vid_std_select;
  66. v4l2_std_id norm;
  67. struct {
  68. u8 edid[256];
  69. u32 present;
  70. } hdmi_edid;
  71. struct {
  72. u8 edid[256];
  73. u32 present;
  74. } vga_edid;
  75. struct v4l2_fract aspect_ratio;
  76. u32 rgb_quantization_range;
  77. bool is_cea_format;
  78. struct workqueue_struct *work_queues;
  79. struct delayed_work delayed_work_enable_hotplug;
  80. bool restart_stdi_once;
  81. bool hdmi_port_a;
  82. /* i2c clients */
  83. struct i2c_client *i2c_sdp_io;
  84. struct i2c_client *i2c_sdp;
  85. struct i2c_client *i2c_cp;
  86. struct i2c_client *i2c_vdp;
  87. struct i2c_client *i2c_afe;
  88. struct i2c_client *i2c_hdmi;
  89. struct i2c_client *i2c_repeater;
  90. struct i2c_client *i2c_edid;
  91. struct i2c_client *i2c_infoframe;
  92. struct i2c_client *i2c_cec;
  93. struct i2c_client *i2c_avlink;
  94. /* controls */
  95. struct v4l2_ctrl *detect_tx_5v_ctrl;
  96. struct v4l2_ctrl *analog_sampling_phase_ctrl;
  97. struct v4l2_ctrl *free_run_color_ctrl_manual;
  98. struct v4l2_ctrl *free_run_color_ctrl;
  99. struct v4l2_ctrl *rgb_quantization_range_ctrl;
  100. };
  101. /* Unsupported timings. This device cannot support 720p30. */
  102. static const struct v4l2_dv_timings adv7842_timings_exceptions[] = {
  103. V4L2_DV_BT_CEA_1280X720P30,
  104. { }
  105. };
  106. static bool adv7842_check_dv_timings(const struct v4l2_dv_timings *t, void *hdl)
  107. {
  108. int i;
  109. for (i = 0; adv7842_timings_exceptions[i].bt.width; i++)
  110. if (v4l2_match_dv_timings(t, adv7842_timings_exceptions + i, 0))
  111. return false;
  112. return true;
  113. }
  114. struct adv7842_video_standards {
  115. struct v4l2_dv_timings timings;
  116. u8 vid_std;
  117. u8 v_freq;
  118. };
  119. /* sorted by number of lines */
  120. static const struct adv7842_video_standards adv7842_prim_mode_comp[] = {
  121. /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
  122. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  123. { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
  124. { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
  125. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  126. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  127. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  128. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  129. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  130. /* TODO add 1920x1080P60_RB (CVT timing) */
  131. { },
  132. };
  133. /* sorted by number of lines */
  134. static const struct adv7842_video_standards adv7842_prim_mode_gr[] = {
  135. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  136. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  137. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  138. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  139. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  140. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  141. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  142. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  143. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  144. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  145. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  146. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  147. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  148. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  149. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  150. { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
  151. { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
  152. { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
  153. { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
  154. { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
  155. /* TODO add 1600X1200P60_RB (not a DMT timing) */
  156. { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
  157. { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
  158. { },
  159. };
  160. /* sorted by number of lines */
  161. static const struct adv7842_video_standards adv7842_prim_mode_hdmi_comp[] = {
  162. { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
  163. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  164. { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
  165. { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
  166. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  167. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  168. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  169. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  170. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  171. { },
  172. };
  173. /* sorted by number of lines */
  174. static const struct adv7842_video_standards adv7842_prim_mode_hdmi_gr[] = {
  175. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  176. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  177. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  178. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  179. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  180. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  181. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  182. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  183. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  184. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  185. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  186. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  187. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  188. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  189. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  190. { },
  191. };
  192. /* ----------------------------------------------------------------------- */
  193. static inline struct adv7842_state *to_state(struct v4l2_subdev *sd)
  194. {
  195. return container_of(sd, struct adv7842_state, sd);
  196. }
  197. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  198. {
  199. return &container_of(ctrl->handler, struct adv7842_state, hdl)->sd;
  200. }
  201. static inline unsigned hblanking(const struct v4l2_bt_timings *t)
  202. {
  203. return V4L2_DV_BT_BLANKING_WIDTH(t);
  204. }
  205. static inline unsigned htotal(const struct v4l2_bt_timings *t)
  206. {
  207. return V4L2_DV_BT_FRAME_WIDTH(t);
  208. }
  209. static inline unsigned vblanking(const struct v4l2_bt_timings *t)
  210. {
  211. return V4L2_DV_BT_BLANKING_HEIGHT(t);
  212. }
  213. static inline unsigned vtotal(const struct v4l2_bt_timings *t)
  214. {
  215. return V4L2_DV_BT_FRAME_HEIGHT(t);
  216. }
  217. /* ----------------------------------------------------------------------- */
  218. static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
  219. u8 command, bool check)
  220. {
  221. union i2c_smbus_data data;
  222. if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  223. I2C_SMBUS_READ, command,
  224. I2C_SMBUS_BYTE_DATA, &data))
  225. return data.byte;
  226. if (check)
  227. v4l_err(client, "error reading %02x, %02x\n",
  228. client->addr, command);
  229. return -EIO;
  230. }
  231. static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
  232. {
  233. int i;
  234. for (i = 0; i < 3; i++) {
  235. int ret = adv_smbus_read_byte_data_check(client, command, true);
  236. if (ret >= 0) {
  237. if (i)
  238. v4l_err(client, "read ok after %d retries\n", i);
  239. return ret;
  240. }
  241. }
  242. v4l_err(client, "read failed\n");
  243. return -EIO;
  244. }
  245. static s32 adv_smbus_write_byte_data(struct i2c_client *client,
  246. u8 command, u8 value)
  247. {
  248. union i2c_smbus_data data;
  249. int err;
  250. int i;
  251. data.byte = value;
  252. for (i = 0; i < 3; i++) {
  253. err = i2c_smbus_xfer(client->adapter, client->addr,
  254. client->flags,
  255. I2C_SMBUS_WRITE, command,
  256. I2C_SMBUS_BYTE_DATA, &data);
  257. if (!err)
  258. break;
  259. }
  260. if (err < 0)
  261. v4l_err(client, "error writing %02x, %02x, %02x\n",
  262. client->addr, command, value);
  263. return err;
  264. }
  265. static void adv_smbus_write_byte_no_check(struct i2c_client *client,
  266. u8 command, u8 value)
  267. {
  268. union i2c_smbus_data data;
  269. data.byte = value;
  270. i2c_smbus_xfer(client->adapter, client->addr,
  271. client->flags,
  272. I2C_SMBUS_WRITE, command,
  273. I2C_SMBUS_BYTE_DATA, &data);
  274. }
  275. static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
  276. u8 command, unsigned length, const u8 *values)
  277. {
  278. union i2c_smbus_data data;
  279. if (length > I2C_SMBUS_BLOCK_MAX)
  280. length = I2C_SMBUS_BLOCK_MAX;
  281. data.block[0] = length;
  282. memcpy(data.block + 1, values, length);
  283. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  284. I2C_SMBUS_WRITE, command,
  285. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  286. }
  287. /* ----------------------------------------------------------------------- */
  288. static inline int io_read(struct v4l2_subdev *sd, u8 reg)
  289. {
  290. struct i2c_client *client = v4l2_get_subdevdata(sd);
  291. return adv_smbus_read_byte_data(client, reg);
  292. }
  293. static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  294. {
  295. struct i2c_client *client = v4l2_get_subdevdata(sd);
  296. return adv_smbus_write_byte_data(client, reg, val);
  297. }
  298. static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  299. {
  300. return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
  301. }
  302. static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
  303. {
  304. struct adv7842_state *state = to_state(sd);
  305. return adv_smbus_read_byte_data(state->i2c_avlink, reg);
  306. }
  307. static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  308. {
  309. struct adv7842_state *state = to_state(sd);
  310. return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
  311. }
  312. static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
  313. {
  314. struct adv7842_state *state = to_state(sd);
  315. return adv_smbus_read_byte_data(state->i2c_cec, reg);
  316. }
  317. static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  318. {
  319. struct adv7842_state *state = to_state(sd);
  320. return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
  321. }
  322. static inline int cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  323. {
  324. return cec_write(sd, reg, (cec_read(sd, reg) & mask) | val);
  325. }
  326. static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
  327. {
  328. struct adv7842_state *state = to_state(sd);
  329. return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
  330. }
  331. static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  332. {
  333. struct adv7842_state *state = to_state(sd);
  334. return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
  335. }
  336. static inline int sdp_io_read(struct v4l2_subdev *sd, u8 reg)
  337. {
  338. struct adv7842_state *state = to_state(sd);
  339. return adv_smbus_read_byte_data(state->i2c_sdp_io, reg);
  340. }
  341. static inline int sdp_io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  342. {
  343. struct adv7842_state *state = to_state(sd);
  344. return adv_smbus_write_byte_data(state->i2c_sdp_io, reg, val);
  345. }
  346. static inline int sdp_io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  347. {
  348. return sdp_io_write(sd, reg, (sdp_io_read(sd, reg) & mask) | val);
  349. }
  350. static inline int sdp_read(struct v4l2_subdev *sd, u8 reg)
  351. {
  352. struct adv7842_state *state = to_state(sd);
  353. return adv_smbus_read_byte_data(state->i2c_sdp, reg);
  354. }
  355. static inline int sdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  356. {
  357. struct adv7842_state *state = to_state(sd);
  358. return adv_smbus_write_byte_data(state->i2c_sdp, reg, val);
  359. }
  360. static inline int sdp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  361. {
  362. return sdp_write(sd, reg, (sdp_read(sd, reg) & mask) | val);
  363. }
  364. static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
  365. {
  366. struct adv7842_state *state = to_state(sd);
  367. return adv_smbus_read_byte_data(state->i2c_afe, reg);
  368. }
  369. static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  370. {
  371. struct adv7842_state *state = to_state(sd);
  372. return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
  373. }
  374. static inline int afe_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  375. {
  376. return afe_write(sd, reg, (afe_read(sd, reg) & mask) | val);
  377. }
  378. static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
  379. {
  380. struct adv7842_state *state = to_state(sd);
  381. return adv_smbus_read_byte_data(state->i2c_repeater, reg);
  382. }
  383. static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  384. {
  385. struct adv7842_state *state = to_state(sd);
  386. return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
  387. }
  388. static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  389. {
  390. return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
  391. }
  392. static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
  393. {
  394. struct adv7842_state *state = to_state(sd);
  395. return adv_smbus_read_byte_data(state->i2c_edid, reg);
  396. }
  397. static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  398. {
  399. struct adv7842_state *state = to_state(sd);
  400. return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
  401. }
  402. static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
  403. {
  404. struct adv7842_state *state = to_state(sd);
  405. return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
  406. }
  407. static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  408. {
  409. struct adv7842_state *state = to_state(sd);
  410. return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
  411. }
  412. static inline int hdmi_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  413. {
  414. return hdmi_write(sd, reg, (hdmi_read(sd, reg) & mask) | val);
  415. }
  416. static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
  417. {
  418. struct adv7842_state *state = to_state(sd);
  419. return adv_smbus_read_byte_data(state->i2c_cp, reg);
  420. }
  421. static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  422. {
  423. struct adv7842_state *state = to_state(sd);
  424. return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
  425. }
  426. static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  427. {
  428. return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
  429. }
  430. static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
  431. {
  432. struct adv7842_state *state = to_state(sd);
  433. return adv_smbus_read_byte_data(state->i2c_vdp, reg);
  434. }
  435. static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  436. {
  437. struct adv7842_state *state = to_state(sd);
  438. return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
  439. }
  440. static void main_reset(struct v4l2_subdev *sd)
  441. {
  442. struct i2c_client *client = v4l2_get_subdevdata(sd);
  443. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  444. adv_smbus_write_byte_no_check(client, 0xff, 0x80);
  445. mdelay(5);
  446. }
  447. /* ----------------------------------------------------------------------- */
  448. static inline bool is_analog_input(struct v4l2_subdev *sd)
  449. {
  450. struct adv7842_state *state = to_state(sd);
  451. return ((state->mode == ADV7842_MODE_RGB) ||
  452. (state->mode == ADV7842_MODE_COMP));
  453. }
  454. static inline bool is_digital_input(struct v4l2_subdev *sd)
  455. {
  456. struct adv7842_state *state = to_state(sd);
  457. return state->mode == ADV7842_MODE_HDMI;
  458. }
  459. static const struct v4l2_dv_timings_cap adv7842_timings_cap_analog = {
  460. .type = V4L2_DV_BT_656_1120,
  461. /* keep this initialization for compatibility with GCC < 4.4.6 */
  462. .reserved = { 0 },
  463. V4L2_INIT_BT_TIMINGS(0, 1920, 0, 1200, 25000000, 170000000,
  464. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  465. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
  466. V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
  467. V4L2_DV_BT_CAP_CUSTOM)
  468. };
  469. static const struct v4l2_dv_timings_cap adv7842_timings_cap_digital = {
  470. .type = V4L2_DV_BT_656_1120,
  471. /* keep this initialization for compatibility with GCC < 4.4.6 */
  472. .reserved = { 0 },
  473. V4L2_INIT_BT_TIMINGS(0, 1920, 0, 1200, 25000000, 225000000,
  474. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  475. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
  476. V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
  477. V4L2_DV_BT_CAP_CUSTOM)
  478. };
  479. static inline const struct v4l2_dv_timings_cap *
  480. adv7842_get_dv_timings_cap(struct v4l2_subdev *sd)
  481. {
  482. return is_digital_input(sd) ? &adv7842_timings_cap_digital :
  483. &adv7842_timings_cap_analog;
  484. }
  485. /* ----------------------------------------------------------------------- */
  486. static void adv7842_delayed_work_enable_hotplug(struct work_struct *work)
  487. {
  488. struct delayed_work *dwork = to_delayed_work(work);
  489. struct adv7842_state *state = container_of(dwork,
  490. struct adv7842_state, delayed_work_enable_hotplug);
  491. struct v4l2_subdev *sd = &state->sd;
  492. int present = state->hdmi_edid.present;
  493. u8 mask = 0;
  494. v4l2_dbg(2, debug, sd, "%s: enable hotplug on ports: 0x%x\n",
  495. __func__, present);
  496. if (present & (0x04 << ADV7842_EDID_PORT_A))
  497. mask |= 0x20;
  498. if (present & (0x04 << ADV7842_EDID_PORT_B))
  499. mask |= 0x10;
  500. io_write_and_or(sd, 0x20, 0xcf, mask);
  501. }
  502. static int edid_write_vga_segment(struct v4l2_subdev *sd)
  503. {
  504. struct i2c_client *client = v4l2_get_subdevdata(sd);
  505. struct adv7842_state *state = to_state(sd);
  506. const u8 *val = state->vga_edid.edid;
  507. int err = 0;
  508. int i;
  509. v4l2_dbg(2, debug, sd, "%s: write EDID on VGA port\n", __func__);
  510. /* HPA disable on port A and B */
  511. io_write_and_or(sd, 0x20, 0xcf, 0x00);
  512. /* Disable I2C access to internal EDID ram from VGA DDC port */
  513. rep_write_and_or(sd, 0x7f, 0x7f, 0x00);
  514. /* edid segment pointer '1' for VGA port */
  515. rep_write_and_or(sd, 0x77, 0xef, 0x10);
  516. for (i = 0; !err && i < 256; i += I2C_SMBUS_BLOCK_MAX)
  517. err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
  518. I2C_SMBUS_BLOCK_MAX, val + i);
  519. if (err)
  520. return err;
  521. /* Calculates the checksums and enables I2C access
  522. * to internal EDID ram from VGA DDC port.
  523. */
  524. rep_write_and_or(sd, 0x7f, 0x7f, 0x80);
  525. for (i = 0; i < 1000; i++) {
  526. if (rep_read(sd, 0x79) & 0x20)
  527. break;
  528. mdelay(1);
  529. }
  530. if (i == 1000) {
  531. v4l_err(client, "error enabling edid on VGA port\n");
  532. return -EIO;
  533. }
  534. /* enable hotplug after 200 ms */
  535. queue_delayed_work(state->work_queues,
  536. &state->delayed_work_enable_hotplug, HZ / 5);
  537. return 0;
  538. }
  539. static int edid_spa_location(const u8 *edid)
  540. {
  541. u8 d;
  542. /*
  543. * TODO, improve and update for other CEA extensions
  544. * currently only for 1 segment (256 bytes),
  545. * i.e. 1 extension block and CEA revision 3.
  546. */
  547. if ((edid[0x7e] != 1) ||
  548. (edid[0x80] != 0x02) ||
  549. (edid[0x81] != 0x03)) {
  550. return -EINVAL;
  551. }
  552. /*
  553. * search Vendor Specific Data Block (tag 3)
  554. */
  555. d = edid[0x82] & 0x7f;
  556. if (d > 4) {
  557. int i = 0x84;
  558. int end = 0x80 + d;
  559. do {
  560. u8 tag = edid[i]>>5;
  561. u8 len = edid[i] & 0x1f;
  562. if ((tag == 3) && (len >= 5))
  563. return i + 4;
  564. i += len + 1;
  565. } while (i < end);
  566. }
  567. return -EINVAL;
  568. }
  569. static int edid_write_hdmi_segment(struct v4l2_subdev *sd, u8 port)
  570. {
  571. struct i2c_client *client = v4l2_get_subdevdata(sd);
  572. struct adv7842_state *state = to_state(sd);
  573. const u8 *val = state->hdmi_edid.edid;
  574. int spa_loc = edid_spa_location(val);
  575. int err = 0;
  576. int i;
  577. v4l2_dbg(2, debug, sd, "%s: write EDID on port %c (spa at 0x%x)\n",
  578. __func__, (port == ADV7842_EDID_PORT_A) ? 'A' : 'B', spa_loc);
  579. /* HPA disable on port A and B */
  580. io_write_and_or(sd, 0x20, 0xcf, 0x00);
  581. /* Disable I2C access to internal EDID ram from HDMI DDC ports */
  582. rep_write_and_or(sd, 0x77, 0xf3, 0x00);
  583. if (!state->hdmi_edid.present)
  584. return 0;
  585. /* edid segment pointer '0' for HDMI ports */
  586. rep_write_and_or(sd, 0x77, 0xef, 0x00);
  587. for (i = 0; !err && i < 256; i += I2C_SMBUS_BLOCK_MAX)
  588. err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
  589. I2C_SMBUS_BLOCK_MAX, val + i);
  590. if (err)
  591. return err;
  592. if (spa_loc < 0)
  593. spa_loc = 0xc0; /* Default value [REF_02, p. 199] */
  594. if (port == ADV7842_EDID_PORT_A) {
  595. rep_write(sd, 0x72, val[spa_loc]);
  596. rep_write(sd, 0x73, val[spa_loc + 1]);
  597. } else {
  598. rep_write(sd, 0x74, val[spa_loc]);
  599. rep_write(sd, 0x75, val[spa_loc + 1]);
  600. }
  601. rep_write(sd, 0x76, spa_loc & 0xff);
  602. rep_write_and_or(sd, 0x77, 0xbf, (spa_loc >> 2) & 0x40);
  603. /* Calculates the checksums and enables I2C access to internal
  604. * EDID ram from HDMI DDC ports
  605. */
  606. rep_write_and_or(sd, 0x77, 0xf3, state->hdmi_edid.present);
  607. for (i = 0; i < 1000; i++) {
  608. if (rep_read(sd, 0x7d) & state->hdmi_edid.present)
  609. break;
  610. mdelay(1);
  611. }
  612. if (i == 1000) {
  613. v4l_err(client, "error enabling edid on port %c\n",
  614. (port == ADV7842_EDID_PORT_A) ? 'A' : 'B');
  615. return -EIO;
  616. }
  617. /* enable hotplug after 200 ms */
  618. queue_delayed_work(state->work_queues,
  619. &state->delayed_work_enable_hotplug, HZ / 5);
  620. return 0;
  621. }
  622. /* ----------------------------------------------------------------------- */
  623. #ifdef CONFIG_VIDEO_ADV_DEBUG
  624. static void adv7842_inv_register(struct v4l2_subdev *sd)
  625. {
  626. v4l2_info(sd, "0x000-0x0ff: IO Map\n");
  627. v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
  628. v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
  629. v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
  630. v4l2_info(sd, "0x400-0x4ff: SDP_IO Map\n");
  631. v4l2_info(sd, "0x500-0x5ff: SDP Map\n");
  632. v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
  633. v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
  634. v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
  635. v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
  636. v4l2_info(sd, "0xa00-0xaff: CP Map\n");
  637. v4l2_info(sd, "0xb00-0xbff: VDP Map\n");
  638. }
  639. static int adv7842_g_register(struct v4l2_subdev *sd,
  640. struct v4l2_dbg_register *reg)
  641. {
  642. reg->size = 1;
  643. switch (reg->reg >> 8) {
  644. case 0:
  645. reg->val = io_read(sd, reg->reg & 0xff);
  646. break;
  647. case 1:
  648. reg->val = avlink_read(sd, reg->reg & 0xff);
  649. break;
  650. case 2:
  651. reg->val = cec_read(sd, reg->reg & 0xff);
  652. break;
  653. case 3:
  654. reg->val = infoframe_read(sd, reg->reg & 0xff);
  655. break;
  656. case 4:
  657. reg->val = sdp_io_read(sd, reg->reg & 0xff);
  658. break;
  659. case 5:
  660. reg->val = sdp_read(sd, reg->reg & 0xff);
  661. break;
  662. case 6:
  663. reg->val = afe_read(sd, reg->reg & 0xff);
  664. break;
  665. case 7:
  666. reg->val = rep_read(sd, reg->reg & 0xff);
  667. break;
  668. case 8:
  669. reg->val = edid_read(sd, reg->reg & 0xff);
  670. break;
  671. case 9:
  672. reg->val = hdmi_read(sd, reg->reg & 0xff);
  673. break;
  674. case 0xa:
  675. reg->val = cp_read(sd, reg->reg & 0xff);
  676. break;
  677. case 0xb:
  678. reg->val = vdp_read(sd, reg->reg & 0xff);
  679. break;
  680. default:
  681. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  682. adv7842_inv_register(sd);
  683. break;
  684. }
  685. return 0;
  686. }
  687. static int adv7842_s_register(struct v4l2_subdev *sd,
  688. const struct v4l2_dbg_register *reg)
  689. {
  690. u8 val = reg->val & 0xff;
  691. switch (reg->reg >> 8) {
  692. case 0:
  693. io_write(sd, reg->reg & 0xff, val);
  694. break;
  695. case 1:
  696. avlink_write(sd, reg->reg & 0xff, val);
  697. break;
  698. case 2:
  699. cec_write(sd, reg->reg & 0xff, val);
  700. break;
  701. case 3:
  702. infoframe_write(sd, reg->reg & 0xff, val);
  703. break;
  704. case 4:
  705. sdp_io_write(sd, reg->reg & 0xff, val);
  706. break;
  707. case 5:
  708. sdp_write(sd, reg->reg & 0xff, val);
  709. break;
  710. case 6:
  711. afe_write(sd, reg->reg & 0xff, val);
  712. break;
  713. case 7:
  714. rep_write(sd, reg->reg & 0xff, val);
  715. break;
  716. case 8:
  717. edid_write(sd, reg->reg & 0xff, val);
  718. break;
  719. case 9:
  720. hdmi_write(sd, reg->reg & 0xff, val);
  721. break;
  722. case 0xa:
  723. cp_write(sd, reg->reg & 0xff, val);
  724. break;
  725. case 0xb:
  726. vdp_write(sd, reg->reg & 0xff, val);
  727. break;
  728. default:
  729. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  730. adv7842_inv_register(sd);
  731. break;
  732. }
  733. return 0;
  734. }
  735. #endif
  736. static int adv7842_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
  737. {
  738. struct adv7842_state *state = to_state(sd);
  739. int prev = v4l2_ctrl_g_ctrl(state->detect_tx_5v_ctrl);
  740. u8 reg_io_6f = io_read(sd, 0x6f);
  741. int val = 0;
  742. if (reg_io_6f & 0x02)
  743. val |= 1; /* port A */
  744. if (reg_io_6f & 0x01)
  745. val |= 2; /* port B */
  746. v4l2_dbg(1, debug, sd, "%s: 0x%x -> 0x%x\n", __func__, prev, val);
  747. if (val != prev)
  748. return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl, val);
  749. return 0;
  750. }
  751. static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
  752. u8 prim_mode,
  753. const struct adv7842_video_standards *predef_vid_timings,
  754. const struct v4l2_dv_timings *timings)
  755. {
  756. int i;
  757. for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
  758. if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
  759. is_digital_input(sd) ? 250000 : 1000000))
  760. continue;
  761. /* video std */
  762. io_write(sd, 0x00, predef_vid_timings[i].vid_std);
  763. /* v_freq and prim mode */
  764. io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) + prim_mode);
  765. return 0;
  766. }
  767. return -1;
  768. }
  769. static int configure_predefined_video_timings(struct v4l2_subdev *sd,
  770. struct v4l2_dv_timings *timings)
  771. {
  772. struct adv7842_state *state = to_state(sd);
  773. int err;
  774. v4l2_dbg(1, debug, sd, "%s\n", __func__);
  775. /* reset to default values */
  776. io_write(sd, 0x16, 0x43);
  777. io_write(sd, 0x17, 0x5a);
  778. /* disable embedded syncs for auto graphics mode */
  779. cp_write_and_or(sd, 0x81, 0xef, 0x00);
  780. cp_write(sd, 0x26, 0x00);
  781. cp_write(sd, 0x27, 0x00);
  782. cp_write(sd, 0x28, 0x00);
  783. cp_write(sd, 0x29, 0x00);
  784. cp_write(sd, 0x8f, 0x40);
  785. cp_write(sd, 0x90, 0x00);
  786. cp_write(sd, 0xa5, 0x00);
  787. cp_write(sd, 0xa6, 0x00);
  788. cp_write(sd, 0xa7, 0x00);
  789. cp_write(sd, 0xab, 0x00);
  790. cp_write(sd, 0xac, 0x00);
  791. switch (state->mode) {
  792. case ADV7842_MODE_COMP:
  793. case ADV7842_MODE_RGB:
  794. err = find_and_set_predefined_video_timings(sd,
  795. 0x01, adv7842_prim_mode_comp, timings);
  796. if (err)
  797. err = find_and_set_predefined_video_timings(sd,
  798. 0x02, adv7842_prim_mode_gr, timings);
  799. break;
  800. case ADV7842_MODE_HDMI:
  801. err = find_and_set_predefined_video_timings(sd,
  802. 0x05, adv7842_prim_mode_hdmi_comp, timings);
  803. if (err)
  804. err = find_and_set_predefined_video_timings(sd,
  805. 0x06, adv7842_prim_mode_hdmi_gr, timings);
  806. break;
  807. default:
  808. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  809. __func__, state->mode);
  810. err = -1;
  811. break;
  812. }
  813. return err;
  814. }
  815. static void configure_custom_video_timings(struct v4l2_subdev *sd,
  816. const struct v4l2_bt_timings *bt)
  817. {
  818. struct adv7842_state *state = to_state(sd);
  819. struct i2c_client *client = v4l2_get_subdevdata(sd);
  820. u32 width = htotal(bt);
  821. u32 height = vtotal(bt);
  822. u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
  823. u16 cp_start_eav = width - bt->hfrontporch;
  824. u16 cp_start_vbi = height - bt->vfrontporch + 1;
  825. u16 cp_end_vbi = bt->vsync + bt->vbackporch + 1;
  826. u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
  827. ((width * (ADV7842_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
  828. const u8 pll[2] = {
  829. 0xc0 | ((width >> 8) & 0x1f),
  830. width & 0xff
  831. };
  832. v4l2_dbg(2, debug, sd, "%s\n", __func__);
  833. switch (state->mode) {
  834. case ADV7842_MODE_COMP:
  835. case ADV7842_MODE_RGB:
  836. /* auto graphics */
  837. io_write(sd, 0x00, 0x07); /* video std */
  838. io_write(sd, 0x01, 0x02); /* prim mode */
  839. /* enable embedded syncs for auto graphics mode */
  840. cp_write_and_or(sd, 0x81, 0xef, 0x10);
  841. /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
  842. /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
  843. /* IO-map reg. 0x16 and 0x17 should be written in sequence */
  844. if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll)) {
  845. v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
  846. break;
  847. }
  848. /* active video - horizontal timing */
  849. cp_write(sd, 0x26, (cp_start_sav >> 8) & 0xf);
  850. cp_write(sd, 0x27, (cp_start_sav & 0xff));
  851. cp_write(sd, 0x28, (cp_start_eav >> 8) & 0xf);
  852. cp_write(sd, 0x29, (cp_start_eav & 0xff));
  853. /* active video - vertical timing */
  854. cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
  855. cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
  856. ((cp_end_vbi >> 8) & 0xf));
  857. cp_write(sd, 0xa7, cp_end_vbi & 0xff);
  858. break;
  859. case ADV7842_MODE_HDMI:
  860. /* set default prim_mode/vid_std for HDMI
  861. according to [REF_03, c. 4.2] */
  862. io_write(sd, 0x00, 0x02); /* video std */
  863. io_write(sd, 0x01, 0x06); /* prim mode */
  864. break;
  865. default:
  866. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  867. __func__, state->mode);
  868. break;
  869. }
  870. cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
  871. cp_write(sd, 0x90, ch1_fr_ll & 0xff);
  872. cp_write(sd, 0xab, (height >> 4) & 0xff);
  873. cp_write(sd, 0xac, (height & 0x0f) << 4);
  874. }
  875. static void adv7842_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
  876. {
  877. struct adv7842_state *state = to_state(sd);
  878. u8 offset_buf[4];
  879. if (auto_offset) {
  880. offset_a = 0x3ff;
  881. offset_b = 0x3ff;
  882. offset_c = 0x3ff;
  883. }
  884. v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
  885. __func__, auto_offset ? "Auto" : "Manual",
  886. offset_a, offset_b, offset_c);
  887. offset_buf[0]= (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
  888. offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
  889. offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
  890. offset_buf[3] = offset_c & 0x0ff;
  891. /* Registers must be written in this order with no i2c access in between */
  892. if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x77, 4, offset_buf))
  893. v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
  894. }
  895. static void adv7842_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
  896. {
  897. struct adv7842_state *state = to_state(sd);
  898. u8 gain_buf[4];
  899. u8 gain_man = 1;
  900. u8 agc_mode_man = 1;
  901. if (auto_gain) {
  902. gain_man = 0;
  903. agc_mode_man = 0;
  904. gain_a = 0x100;
  905. gain_b = 0x100;
  906. gain_c = 0x100;
  907. }
  908. v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
  909. __func__, auto_gain ? "Auto" : "Manual",
  910. gain_a, gain_b, gain_c);
  911. gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
  912. gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
  913. gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
  914. gain_buf[3] = ((gain_c & 0x0ff));
  915. /* Registers must be written in this order with no i2c access in between */
  916. if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x73, 4, gain_buf))
  917. v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
  918. }
  919. static void set_rgb_quantization_range(struct v4l2_subdev *sd)
  920. {
  921. struct adv7842_state *state = to_state(sd);
  922. bool rgb_output = io_read(sd, 0x02) & 0x02;
  923. bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
  924. v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
  925. __func__, state->rgb_quantization_range,
  926. rgb_output, hdmi_signal);
  927. adv7842_set_gain(sd, true, 0x0, 0x0, 0x0);
  928. adv7842_set_offset(sd, true, 0x0, 0x0, 0x0);
  929. switch (state->rgb_quantization_range) {
  930. case V4L2_DV_RGB_RANGE_AUTO:
  931. if (state->mode == ADV7842_MODE_RGB) {
  932. /* Receiving analog RGB signal
  933. * Set RGB full range (0-255) */
  934. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  935. break;
  936. }
  937. if (state->mode == ADV7842_MODE_COMP) {
  938. /* Receiving analog YPbPr signal
  939. * Set automode */
  940. io_write_and_or(sd, 0x02, 0x0f, 0xf0);
  941. break;
  942. }
  943. if (hdmi_signal) {
  944. /* Receiving HDMI signal
  945. * Set automode */
  946. io_write_and_or(sd, 0x02, 0x0f, 0xf0);
  947. break;
  948. }
  949. /* Receiving DVI-D signal
  950. * ADV7842 selects RGB limited range regardless of
  951. * input format (CE/IT) in automatic mode */
  952. if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
  953. /* RGB limited range (16-235) */
  954. io_write_and_or(sd, 0x02, 0x0f, 0x00);
  955. } else {
  956. /* RGB full range (0-255) */
  957. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  958. if (is_digital_input(sd) && rgb_output) {
  959. adv7842_set_offset(sd, false, 0x40, 0x40, 0x40);
  960. } else {
  961. adv7842_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
  962. adv7842_set_offset(sd, false, 0x70, 0x70, 0x70);
  963. }
  964. }
  965. break;
  966. case V4L2_DV_RGB_RANGE_LIMITED:
  967. if (state->mode == ADV7842_MODE_COMP) {
  968. /* YCrCb limited range (16-235) */
  969. io_write_and_or(sd, 0x02, 0x0f, 0x20);
  970. break;
  971. }
  972. /* RGB limited range (16-235) */
  973. io_write_and_or(sd, 0x02, 0x0f, 0x00);
  974. break;
  975. case V4L2_DV_RGB_RANGE_FULL:
  976. if (state->mode == ADV7842_MODE_COMP) {
  977. /* YCrCb full range (0-255) */
  978. io_write_and_or(sd, 0x02, 0x0f, 0x60);
  979. break;
  980. }
  981. /* RGB full range (0-255) */
  982. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  983. if (is_analog_input(sd) || hdmi_signal)
  984. break;
  985. /* Adjust gain/offset for DVI-D signals only */
  986. if (rgb_output) {
  987. adv7842_set_offset(sd, false, 0x40, 0x40, 0x40);
  988. } else {
  989. adv7842_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
  990. adv7842_set_offset(sd, false, 0x70, 0x70, 0x70);
  991. }
  992. break;
  993. }
  994. }
  995. static int adv7842_s_ctrl(struct v4l2_ctrl *ctrl)
  996. {
  997. struct v4l2_subdev *sd = to_sd(ctrl);
  998. struct adv7842_state *state = to_state(sd);
  999. /* TODO SDP ctrls
  1000. contrast/brightness/hue/free run is acting a bit strange,
  1001. not sure if sdp csc is correct.
  1002. */
  1003. switch (ctrl->id) {
  1004. /* standard ctrls */
  1005. case V4L2_CID_BRIGHTNESS:
  1006. cp_write(sd, 0x3c, ctrl->val);
  1007. sdp_write(sd, 0x14, ctrl->val);
  1008. /* ignore lsb sdp 0x17[3:2] */
  1009. return 0;
  1010. case V4L2_CID_CONTRAST:
  1011. cp_write(sd, 0x3a, ctrl->val);
  1012. sdp_write(sd, 0x13, ctrl->val);
  1013. /* ignore lsb sdp 0x17[1:0] */
  1014. return 0;
  1015. case V4L2_CID_SATURATION:
  1016. cp_write(sd, 0x3b, ctrl->val);
  1017. sdp_write(sd, 0x15, ctrl->val);
  1018. /* ignore lsb sdp 0x17[5:4] */
  1019. return 0;
  1020. case V4L2_CID_HUE:
  1021. cp_write(sd, 0x3d, ctrl->val);
  1022. sdp_write(sd, 0x16, ctrl->val);
  1023. /* ignore lsb sdp 0x17[7:6] */
  1024. return 0;
  1025. /* custom ctrls */
  1026. case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
  1027. afe_write(sd, 0xc8, ctrl->val);
  1028. return 0;
  1029. case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
  1030. cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
  1031. sdp_write_and_or(sd, 0xdd, ~0x04, (ctrl->val << 2));
  1032. return 0;
  1033. case V4L2_CID_ADV_RX_FREE_RUN_COLOR: {
  1034. u8 R = (ctrl->val & 0xff0000) >> 16;
  1035. u8 G = (ctrl->val & 0x00ff00) >> 8;
  1036. u8 B = (ctrl->val & 0x0000ff);
  1037. /* RGB -> YUV, numerical approximation */
  1038. int Y = 66 * R + 129 * G + 25 * B;
  1039. int U = -38 * R - 74 * G + 112 * B;
  1040. int V = 112 * R - 94 * G - 18 * B;
  1041. /* Scale down to 8 bits with rounding */
  1042. Y = (Y + 128) >> 8;
  1043. U = (U + 128) >> 8;
  1044. V = (V + 128) >> 8;
  1045. /* make U,V positive */
  1046. Y += 16;
  1047. U += 128;
  1048. V += 128;
  1049. v4l2_dbg(1, debug, sd, "R %x, G %x, B %x\n", R, G, B);
  1050. v4l2_dbg(1, debug, sd, "Y %x, U %x, V %x\n", Y, U, V);
  1051. /* CP */
  1052. cp_write(sd, 0xc1, R);
  1053. cp_write(sd, 0xc0, G);
  1054. cp_write(sd, 0xc2, B);
  1055. /* SDP */
  1056. sdp_write(sd, 0xde, Y);
  1057. sdp_write(sd, 0xdf, (V & 0xf0) | ((U >> 4) & 0x0f));
  1058. return 0;
  1059. }
  1060. case V4L2_CID_DV_RX_RGB_RANGE:
  1061. state->rgb_quantization_range = ctrl->val;
  1062. set_rgb_quantization_range(sd);
  1063. return 0;
  1064. }
  1065. return -EINVAL;
  1066. }
  1067. static inline bool no_power(struct v4l2_subdev *sd)
  1068. {
  1069. return io_read(sd, 0x0c) & 0x24;
  1070. }
  1071. static inline bool no_cp_signal(struct v4l2_subdev *sd)
  1072. {
  1073. return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0) || !(cp_read(sd, 0xb1) & 0x80);
  1074. }
  1075. static inline bool is_hdmi(struct v4l2_subdev *sd)
  1076. {
  1077. return hdmi_read(sd, 0x05) & 0x80;
  1078. }
  1079. static int adv7842_g_input_status(struct v4l2_subdev *sd, u32 *status)
  1080. {
  1081. struct adv7842_state *state = to_state(sd);
  1082. *status = 0;
  1083. if (io_read(sd, 0x0c) & 0x24)
  1084. *status |= V4L2_IN_ST_NO_POWER;
  1085. if (state->mode == ADV7842_MODE_SDP) {
  1086. /* status from SDP block */
  1087. if (!(sdp_read(sd, 0x5A) & 0x01))
  1088. *status |= V4L2_IN_ST_NO_SIGNAL;
  1089. v4l2_dbg(1, debug, sd, "%s: SDP status = 0x%x\n",
  1090. __func__, *status);
  1091. return 0;
  1092. }
  1093. /* status from CP block */
  1094. if ((cp_read(sd, 0xb5) & 0xd0) != 0xd0 ||
  1095. !(cp_read(sd, 0xb1) & 0x80))
  1096. /* TODO channel 2 */
  1097. *status |= V4L2_IN_ST_NO_SIGNAL;
  1098. if (is_digital_input(sd) && ((io_read(sd, 0x74) & 0x03) != 0x03))
  1099. *status |= V4L2_IN_ST_NO_SIGNAL;
  1100. v4l2_dbg(1, debug, sd, "%s: CP status = 0x%x\n",
  1101. __func__, *status);
  1102. return 0;
  1103. }
  1104. struct stdi_readback {
  1105. u16 bl, lcf, lcvs;
  1106. u8 hs_pol, vs_pol;
  1107. bool interlaced;
  1108. };
  1109. static int stdi2dv_timings(struct v4l2_subdev *sd,
  1110. struct stdi_readback *stdi,
  1111. struct v4l2_dv_timings *timings)
  1112. {
  1113. struct adv7842_state *state = to_state(sd);
  1114. u32 hfreq = (ADV7842_fsc * 8) / stdi->bl;
  1115. u32 pix_clk;
  1116. int i;
  1117. for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
  1118. const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
  1119. if (!v4l2_valid_dv_timings(&v4l2_dv_timings_presets[i],
  1120. adv7842_get_dv_timings_cap(sd),
  1121. adv7842_check_dv_timings, NULL))
  1122. continue;
  1123. if (vtotal(bt) != stdi->lcf + 1)
  1124. continue;
  1125. if (bt->vsync != stdi->lcvs)
  1126. continue;
  1127. pix_clk = hfreq * htotal(bt);
  1128. if ((pix_clk < bt->pixelclock + 1000000) &&
  1129. (pix_clk > bt->pixelclock - 1000000)) {
  1130. *timings = v4l2_dv_timings_presets[i];
  1131. return 0;
  1132. }
  1133. }
  1134. if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
  1135. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  1136. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  1137. timings))
  1138. return 0;
  1139. if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
  1140. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  1141. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  1142. state->aspect_ratio, timings))
  1143. return 0;
  1144. v4l2_dbg(2, debug, sd,
  1145. "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
  1146. __func__, stdi->lcvs, stdi->lcf, stdi->bl,
  1147. stdi->hs_pol, stdi->vs_pol);
  1148. return -1;
  1149. }
  1150. static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
  1151. {
  1152. u32 status;
  1153. adv7842_g_input_status(sd, &status);
  1154. if (status & V4L2_IN_ST_NO_SIGNAL) {
  1155. v4l2_dbg(2, debug, sd, "%s: no signal\n", __func__);
  1156. return -ENOLINK;
  1157. }
  1158. stdi->bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
  1159. stdi->lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
  1160. stdi->lcvs = cp_read(sd, 0xb3) >> 3;
  1161. if ((cp_read(sd, 0xb5) & 0x80) && ((cp_read(sd, 0xb5) & 0x03) == 0x01)) {
  1162. stdi->hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
  1163. ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
  1164. stdi->vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
  1165. ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
  1166. } else {
  1167. stdi->hs_pol = 'x';
  1168. stdi->vs_pol = 'x';
  1169. }
  1170. stdi->interlaced = (cp_read(sd, 0xb1) & 0x40) ? true : false;
  1171. if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
  1172. v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
  1173. return -ENOLINK;
  1174. }
  1175. v4l2_dbg(2, debug, sd,
  1176. "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
  1177. __func__, stdi->lcf, stdi->bl, stdi->lcvs,
  1178. stdi->hs_pol, stdi->vs_pol,
  1179. stdi->interlaced ? "interlaced" : "progressive");
  1180. return 0;
  1181. }
  1182. static int adv7842_enum_dv_timings(struct v4l2_subdev *sd,
  1183. struct v4l2_enum_dv_timings *timings)
  1184. {
  1185. if (timings->pad != 0)
  1186. return -EINVAL;
  1187. return v4l2_enum_dv_timings_cap(timings,
  1188. adv7842_get_dv_timings_cap(sd), adv7842_check_dv_timings, NULL);
  1189. }
  1190. static int adv7842_dv_timings_cap(struct v4l2_subdev *sd,
  1191. struct v4l2_dv_timings_cap *cap)
  1192. {
  1193. if (cap->pad != 0)
  1194. return -EINVAL;
  1195. *cap = *adv7842_get_dv_timings_cap(sd);
  1196. return 0;
  1197. }
  1198. /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
  1199. if the format is listed in adv7842_timings[] */
  1200. static void adv7842_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
  1201. struct v4l2_dv_timings *timings)
  1202. {
  1203. v4l2_find_dv_timings_cap(timings, adv7842_get_dv_timings_cap(sd),
  1204. is_digital_input(sd) ? 250000 : 1000000,
  1205. adv7842_check_dv_timings, NULL);
  1206. }
  1207. static int adv7842_query_dv_timings(struct v4l2_subdev *sd,
  1208. struct v4l2_dv_timings *timings)
  1209. {
  1210. struct adv7842_state *state = to_state(sd);
  1211. struct v4l2_bt_timings *bt = &timings->bt;
  1212. struct stdi_readback stdi = { 0 };
  1213. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  1214. /* SDP block */
  1215. if (state->mode == ADV7842_MODE_SDP)
  1216. return -ENODATA;
  1217. /* read STDI */
  1218. if (read_stdi(sd, &stdi)) {
  1219. state->restart_stdi_once = true;
  1220. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  1221. return -ENOLINK;
  1222. }
  1223. bt->interlaced = stdi.interlaced ?
  1224. V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
  1225. if (is_digital_input(sd)) {
  1226. uint32_t freq;
  1227. timings->type = V4L2_DV_BT_656_1120;
  1228. bt->width = (hdmi_read(sd, 0x07) & 0x0f) * 256 + hdmi_read(sd, 0x08);
  1229. bt->height = (hdmi_read(sd, 0x09) & 0x0f) * 256 + hdmi_read(sd, 0x0a);
  1230. freq = ((hdmi_read(sd, 0x51) << 1) + (hdmi_read(sd, 0x52) >> 7)) * 1000000;
  1231. freq += ((hdmi_read(sd, 0x52) & 0x7f) * 7813);
  1232. if (is_hdmi(sd)) {
  1233. /* adjust for deep color mode */
  1234. freq = freq * 8 / (((hdmi_read(sd, 0x0b) & 0xc0) >> 6) * 2 + 8);
  1235. }
  1236. bt->pixelclock = freq;
  1237. bt->hfrontporch = (hdmi_read(sd, 0x20) & 0x03) * 256 +
  1238. hdmi_read(sd, 0x21);
  1239. bt->hsync = (hdmi_read(sd, 0x22) & 0x03) * 256 +
  1240. hdmi_read(sd, 0x23);
  1241. bt->hbackporch = (hdmi_read(sd, 0x24) & 0x03) * 256 +
  1242. hdmi_read(sd, 0x25);
  1243. bt->vfrontporch = ((hdmi_read(sd, 0x2a) & 0x1f) * 256 +
  1244. hdmi_read(sd, 0x2b)) / 2;
  1245. bt->vsync = ((hdmi_read(sd, 0x2e) & 0x1f) * 256 +
  1246. hdmi_read(sd, 0x2f)) / 2;
  1247. bt->vbackporch = ((hdmi_read(sd, 0x32) & 0x1f) * 256 +
  1248. hdmi_read(sd, 0x33)) / 2;
  1249. bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
  1250. ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
  1251. if (bt->interlaced == V4L2_DV_INTERLACED) {
  1252. bt->height += (hdmi_read(sd, 0x0b) & 0x0f) * 256 +
  1253. hdmi_read(sd, 0x0c);
  1254. bt->il_vfrontporch = ((hdmi_read(sd, 0x2c) & 0x1f) * 256 +
  1255. hdmi_read(sd, 0x2d)) / 2;
  1256. bt->il_vsync = ((hdmi_read(sd, 0x30) & 0x1f) * 256 +
  1257. hdmi_read(sd, 0x31)) / 2;
  1258. bt->vbackporch = ((hdmi_read(sd, 0x34) & 0x1f) * 256 +
  1259. hdmi_read(sd, 0x35)) / 2;
  1260. }
  1261. adv7842_fill_optional_dv_timings_fields(sd, timings);
  1262. } else {
  1263. /* find format
  1264. * Since LCVS values are inaccurate [REF_03, p. 339-340],
  1265. * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
  1266. */
  1267. if (!stdi2dv_timings(sd, &stdi, timings))
  1268. goto found;
  1269. stdi.lcvs += 1;
  1270. v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
  1271. if (!stdi2dv_timings(sd, &stdi, timings))
  1272. goto found;
  1273. stdi.lcvs -= 2;
  1274. v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
  1275. if (stdi2dv_timings(sd, &stdi, timings)) {
  1276. /*
  1277. * The STDI block may measure wrong values, especially
  1278. * for lcvs and lcf. If the driver can not find any
  1279. * valid timing, the STDI block is restarted to measure
  1280. * the video timings again. The function will return an
  1281. * error, but the restart of STDI will generate a new
  1282. * STDI interrupt and the format detection process will
  1283. * restart.
  1284. */
  1285. if (state->restart_stdi_once) {
  1286. v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
  1287. /* TODO restart STDI for Sync Channel 2 */
  1288. /* enter one-shot mode */
  1289. cp_write_and_or(sd, 0x86, 0xf9, 0x00);
  1290. /* trigger STDI restart */
  1291. cp_write_and_or(sd, 0x86, 0xf9, 0x04);
  1292. /* reset to continuous mode */
  1293. cp_write_and_or(sd, 0x86, 0xf9, 0x02);
  1294. state->restart_stdi_once = false;
  1295. return -ENOLINK;
  1296. }
  1297. v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
  1298. return -ERANGE;
  1299. }
  1300. state->restart_stdi_once = true;
  1301. }
  1302. found:
  1303. if (debug > 1)
  1304. v4l2_print_dv_timings(sd->name, "adv7842_query_dv_timings:",
  1305. timings, true);
  1306. return 0;
  1307. }
  1308. static int adv7842_s_dv_timings(struct v4l2_subdev *sd,
  1309. struct v4l2_dv_timings *timings)
  1310. {
  1311. struct adv7842_state *state = to_state(sd);
  1312. struct v4l2_bt_timings *bt;
  1313. int err;
  1314. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  1315. if (state->mode == ADV7842_MODE_SDP)
  1316. return -ENODATA;
  1317. if (v4l2_match_dv_timings(&state->timings, timings, 0)) {
  1318. v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
  1319. return 0;
  1320. }
  1321. bt = &timings->bt;
  1322. if (!v4l2_valid_dv_timings(timings, adv7842_get_dv_timings_cap(sd),
  1323. adv7842_check_dv_timings, NULL))
  1324. return -ERANGE;
  1325. adv7842_fill_optional_dv_timings_fields(sd, timings);
  1326. state->timings = *timings;
  1327. cp_write(sd, 0x91, bt->interlaced ? 0x40 : 0x00);
  1328. /* Use prim_mode and vid_std when available */
  1329. err = configure_predefined_video_timings(sd, timings);
  1330. if (err) {
  1331. /* custom settings when the video format
  1332. does not have prim_mode/vid_std */
  1333. configure_custom_video_timings(sd, bt);
  1334. }
  1335. set_rgb_quantization_range(sd);
  1336. if (debug > 1)
  1337. v4l2_print_dv_timings(sd->name, "adv7842_s_dv_timings: ",
  1338. timings, true);
  1339. return 0;
  1340. }
  1341. static int adv7842_g_dv_timings(struct v4l2_subdev *sd,
  1342. struct v4l2_dv_timings *timings)
  1343. {
  1344. struct adv7842_state *state = to_state(sd);
  1345. if (state->mode == ADV7842_MODE_SDP)
  1346. return -ENODATA;
  1347. *timings = state->timings;
  1348. return 0;
  1349. }
  1350. static void enable_input(struct v4l2_subdev *sd)
  1351. {
  1352. struct adv7842_state *state = to_state(sd);
  1353. set_rgb_quantization_range(sd);
  1354. switch (state->mode) {
  1355. case ADV7842_MODE_SDP:
  1356. case ADV7842_MODE_COMP:
  1357. case ADV7842_MODE_RGB:
  1358. io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
  1359. break;
  1360. case ADV7842_MODE_HDMI:
  1361. hdmi_write(sd, 0x01, 0x00); /* Enable HDMI clock terminators */
  1362. io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
  1363. hdmi_write_and_or(sd, 0x1a, 0xef, 0x00); /* Unmute audio */
  1364. break;
  1365. default:
  1366. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  1367. __func__, state->mode);
  1368. break;
  1369. }
  1370. }
  1371. static void disable_input(struct v4l2_subdev *sd)
  1372. {
  1373. hdmi_write_and_or(sd, 0x1a, 0xef, 0x10); /* Mute audio [REF_01, c. 2.2.2] */
  1374. msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 8.29] */
  1375. io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
  1376. hdmi_write(sd, 0x01, 0x78); /* Disable HDMI clock terminators */
  1377. }
  1378. static void sdp_csc_coeff(struct v4l2_subdev *sd,
  1379. const struct adv7842_sdp_csc_coeff *c)
  1380. {
  1381. /* csc auto/manual */
  1382. sdp_io_write_and_or(sd, 0xe0, 0xbf, c->manual ? 0x00 : 0x40);
  1383. if (!c->manual)
  1384. return;
  1385. /* csc scaling */
  1386. sdp_io_write_and_or(sd, 0xe0, 0x7f, c->scaling == 2 ? 0x80 : 0x00);
  1387. /* A coeff */
  1388. sdp_io_write_and_or(sd, 0xe0, 0xe0, c->A1 >> 8);
  1389. sdp_io_write(sd, 0xe1, c->A1);
  1390. sdp_io_write_and_or(sd, 0xe2, 0xe0, c->A2 >> 8);
  1391. sdp_io_write(sd, 0xe3, c->A2);
  1392. sdp_io_write_and_or(sd, 0xe4, 0xe0, c->A3 >> 8);
  1393. sdp_io_write(sd, 0xe5, c->A3);
  1394. /* A scale */
  1395. sdp_io_write_and_or(sd, 0xe6, 0x80, c->A4 >> 8);
  1396. sdp_io_write(sd, 0xe7, c->A4);
  1397. /* B coeff */
  1398. sdp_io_write_and_or(sd, 0xe8, 0xe0, c->B1 >> 8);
  1399. sdp_io_write(sd, 0xe9, c->B1);
  1400. sdp_io_write_and_or(sd, 0xea, 0xe0, c->B2 >> 8);
  1401. sdp_io_write(sd, 0xeb, c->B2);
  1402. sdp_io_write_and_or(sd, 0xec, 0xe0, c->B3 >> 8);
  1403. sdp_io_write(sd, 0xed, c->B3);
  1404. /* B scale */
  1405. sdp_io_write_and_or(sd, 0xee, 0x80, c->B4 >> 8);
  1406. sdp_io_write(sd, 0xef, c->B4);
  1407. /* C coeff */
  1408. sdp_io_write_and_or(sd, 0xf0, 0xe0, c->C1 >> 8);
  1409. sdp_io_write(sd, 0xf1, c->C1);
  1410. sdp_io_write_and_or(sd, 0xf2, 0xe0, c->C2 >> 8);
  1411. sdp_io_write(sd, 0xf3, c->C2);
  1412. sdp_io_write_and_or(sd, 0xf4, 0xe0, c->C3 >> 8);
  1413. sdp_io_write(sd, 0xf5, c->C3);
  1414. /* C scale */
  1415. sdp_io_write_and_or(sd, 0xf6, 0x80, c->C4 >> 8);
  1416. sdp_io_write(sd, 0xf7, c->C4);
  1417. }
  1418. static void select_input(struct v4l2_subdev *sd,
  1419. enum adv7842_vid_std_select vid_std_select)
  1420. {
  1421. struct adv7842_state *state = to_state(sd);
  1422. switch (state->mode) {
  1423. case ADV7842_MODE_SDP:
  1424. io_write(sd, 0x00, vid_std_select); /* video std: CVBS or YC mode */
  1425. io_write(sd, 0x01, 0); /* prim mode */
  1426. /* enable embedded syncs for auto graphics mode */
  1427. cp_write_and_or(sd, 0x81, 0xef, 0x10);
  1428. afe_write(sd, 0x00, 0x00); /* power up ADC */
  1429. afe_write(sd, 0xc8, 0x00); /* phase control */
  1430. io_write(sd, 0xdd, 0x90); /* Manual 2x output clock */
  1431. /* script says register 0xde, which don't exist in manual */
  1432. /* Manual analog input muxing mode, CVBS (6.4)*/
  1433. afe_write_and_or(sd, 0x02, 0x7f, 0x80);
  1434. if (vid_std_select == ADV7842_SDP_VID_STD_CVBS_SD_4x1) {
  1435. afe_write(sd, 0x03, 0xa0); /* ADC0 to AIN10 (CVBS), ADC1 N/C*/
  1436. afe_write(sd, 0x04, 0x00); /* ADC2 N/C,ADC3 N/C*/
  1437. } else {
  1438. afe_write(sd, 0x03, 0xa0); /* ADC0 to AIN10 (CVBS), ADC1 N/C*/
  1439. afe_write(sd, 0x04, 0xc0); /* ADC2 to AIN12, ADC3 N/C*/
  1440. }
  1441. afe_write(sd, 0x0c, 0x1f); /* ADI recommend write */
  1442. afe_write(sd, 0x12, 0x63); /* ADI recommend write */
  1443. sdp_io_write(sd, 0xb2, 0x60); /* Disable AV codes */
  1444. sdp_io_write(sd, 0xc8, 0xe3); /* Disable Ancillary data */
  1445. /* SDP recommended settings */
  1446. sdp_write(sd, 0x00, 0x3F); /* Autodetect PAL NTSC (not SECAM) */
  1447. sdp_write(sd, 0x01, 0x00); /* Pedestal Off */
  1448. sdp_write(sd, 0x03, 0xE4); /* Manual VCR Gain Luma 0x40B */
  1449. sdp_write(sd, 0x04, 0x0B); /* Manual Luma setting */
  1450. sdp_write(sd, 0x05, 0xC3); /* Manual Chroma setting 0x3FE */
  1451. sdp_write(sd, 0x06, 0xFE); /* Manual Chroma setting */
  1452. sdp_write(sd, 0x12, 0x0D); /* Frame TBC,I_P, 3D comb enabled */
  1453. sdp_write(sd, 0xA7, 0x00); /* ADI Recommended Write */
  1454. sdp_io_write(sd, 0xB0, 0x00); /* Disable H and v blanking */
  1455. /* deinterlacer enabled and 3D comb */
  1456. sdp_write_and_or(sd, 0x12, 0xf6, 0x09);
  1457. break;
  1458. case ADV7842_MODE_COMP:
  1459. case ADV7842_MODE_RGB:
  1460. /* Automatic analog input muxing mode */
  1461. afe_write_and_or(sd, 0x02, 0x7f, 0x00);
  1462. /* set mode and select free run resolution */
  1463. io_write(sd, 0x00, vid_std_select); /* video std */
  1464. io_write(sd, 0x01, 0x02); /* prim mode */
  1465. cp_write_and_or(sd, 0x81, 0xef, 0x10); /* enable embedded syncs
  1466. for auto graphics mode */
  1467. afe_write(sd, 0x00, 0x00); /* power up ADC */
  1468. afe_write(sd, 0xc8, 0x00); /* phase control */
  1469. if (state->mode == ADV7842_MODE_COMP) {
  1470. /* force to YCrCb */
  1471. io_write_and_or(sd, 0x02, 0x0f, 0x60);
  1472. } else {
  1473. /* force to RGB */
  1474. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  1475. }
  1476. /* set ADI recommended settings for digitizer */
  1477. /* "ADV7842 Register Settings Recommendations
  1478. * (rev. 1.8, November 2010)" p. 9. */
  1479. afe_write(sd, 0x0c, 0x1f); /* ADC Range improvement */
  1480. afe_write(sd, 0x12, 0x63); /* ADC Range improvement */
  1481. /* set to default gain for RGB */
  1482. cp_write(sd, 0x73, 0x10);
  1483. cp_write(sd, 0x74, 0x04);
  1484. cp_write(sd, 0x75, 0x01);
  1485. cp_write(sd, 0x76, 0x00);
  1486. cp_write(sd, 0x3e, 0x04); /* CP core pre-gain control */
  1487. cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
  1488. cp_write(sd, 0x40, 0x5c); /* CP core pre-gain control. Graphics mode */
  1489. break;
  1490. case ADV7842_MODE_HDMI:
  1491. /* Automatic analog input muxing mode */
  1492. afe_write_and_or(sd, 0x02, 0x7f, 0x00);
  1493. /* set mode and select free run resolution */
  1494. if (state->hdmi_port_a)
  1495. hdmi_write(sd, 0x00, 0x02); /* select port A */
  1496. else
  1497. hdmi_write(sd, 0x00, 0x03); /* select port B */
  1498. io_write(sd, 0x00, vid_std_select); /* video std */
  1499. io_write(sd, 0x01, 5); /* prim mode */
  1500. cp_write_and_or(sd, 0x81, 0xef, 0x00); /* disable embedded syncs
  1501. for auto graphics mode */
  1502. /* set ADI recommended settings for HDMI: */
  1503. /* "ADV7842 Register Settings Recommendations
  1504. * (rev. 1.8, November 2010)" p. 3. */
  1505. hdmi_write(sd, 0xc0, 0x00);
  1506. hdmi_write(sd, 0x0d, 0x34); /* ADI recommended write */
  1507. hdmi_write(sd, 0x3d, 0x10); /* ADI recommended write */
  1508. hdmi_write(sd, 0x44, 0x85); /* TMDS PLL optimization */
  1509. hdmi_write(sd, 0x46, 0x1f); /* ADI recommended write */
  1510. hdmi_write(sd, 0x57, 0xb6); /* TMDS PLL optimization */
  1511. hdmi_write(sd, 0x58, 0x03); /* TMDS PLL optimization */
  1512. hdmi_write(sd, 0x60, 0x88); /* TMDS PLL optimization */
  1513. hdmi_write(sd, 0x61, 0x88); /* TMDS PLL optimization */
  1514. hdmi_write(sd, 0x6c, 0x18); /* Disable ISRC clearing bit,
  1515. Improve robustness */
  1516. hdmi_write(sd, 0x75, 0x10); /* DDC drive strength */
  1517. hdmi_write(sd, 0x85, 0x1f); /* equaliser */
  1518. hdmi_write(sd, 0x87, 0x70); /* ADI recommended write */
  1519. hdmi_write(sd, 0x89, 0x04); /* equaliser */
  1520. hdmi_write(sd, 0x8a, 0x1e); /* equaliser */
  1521. hdmi_write(sd, 0x93, 0x04); /* equaliser */
  1522. hdmi_write(sd, 0x94, 0x1e); /* equaliser */
  1523. hdmi_write(sd, 0x99, 0xa1); /* ADI recommended write */
  1524. hdmi_write(sd, 0x9b, 0x09); /* ADI recommended write */
  1525. hdmi_write(sd, 0x9d, 0x02); /* equaliser */
  1526. afe_write(sd, 0x00, 0xff); /* power down ADC */
  1527. afe_write(sd, 0xc8, 0x40); /* phase control */
  1528. /* set to default gain for HDMI */
  1529. cp_write(sd, 0x73, 0x10);
  1530. cp_write(sd, 0x74, 0x04);
  1531. cp_write(sd, 0x75, 0x01);
  1532. cp_write(sd, 0x76, 0x00);
  1533. /* reset ADI recommended settings for digitizer */
  1534. /* "ADV7842 Register Settings Recommendations
  1535. * (rev. 2.5, June 2010)" p. 17. */
  1536. afe_write(sd, 0x12, 0xfb); /* ADC noise shaping filter controls */
  1537. afe_write(sd, 0x0c, 0x0d); /* CP core gain controls */
  1538. cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
  1539. /* CP coast control */
  1540. cp_write(sd, 0xc3, 0x33); /* Component mode */
  1541. /* color space conversion, autodetect color space */
  1542. io_write_and_or(sd, 0x02, 0x0f, 0xf0);
  1543. break;
  1544. default:
  1545. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  1546. __func__, state->mode);
  1547. break;
  1548. }
  1549. }
  1550. static int adv7842_s_routing(struct v4l2_subdev *sd,
  1551. u32 input, u32 output, u32 config)
  1552. {
  1553. struct adv7842_state *state = to_state(sd);
  1554. v4l2_dbg(2, debug, sd, "%s: input %d\n", __func__, input);
  1555. switch (input) {
  1556. case ADV7842_SELECT_HDMI_PORT_A:
  1557. state->mode = ADV7842_MODE_HDMI;
  1558. state->vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P;
  1559. state->hdmi_port_a = true;
  1560. break;
  1561. case ADV7842_SELECT_HDMI_PORT_B:
  1562. state->mode = ADV7842_MODE_HDMI;
  1563. state->vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P;
  1564. state->hdmi_port_a = false;
  1565. break;
  1566. case ADV7842_SELECT_VGA_COMP:
  1567. state->mode = ADV7842_MODE_COMP;
  1568. state->vid_std_select = ADV7842_RGB_VID_STD_AUTO_GRAPH_MODE;
  1569. break;
  1570. case ADV7842_SELECT_VGA_RGB:
  1571. state->mode = ADV7842_MODE_RGB;
  1572. state->vid_std_select = ADV7842_RGB_VID_STD_AUTO_GRAPH_MODE;
  1573. break;
  1574. case ADV7842_SELECT_SDP_CVBS:
  1575. state->mode = ADV7842_MODE_SDP;
  1576. state->vid_std_select = ADV7842_SDP_VID_STD_CVBS_SD_4x1;
  1577. break;
  1578. case ADV7842_SELECT_SDP_YC:
  1579. state->mode = ADV7842_MODE_SDP;
  1580. state->vid_std_select = ADV7842_SDP_VID_STD_YC_SD4_x1;
  1581. break;
  1582. default:
  1583. return -EINVAL;
  1584. }
  1585. disable_input(sd);
  1586. select_input(sd, state->vid_std_select);
  1587. enable_input(sd);
  1588. v4l2_subdev_notify(sd, ADV7842_FMT_CHANGE, NULL);
  1589. return 0;
  1590. }
  1591. static int adv7842_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
  1592. enum v4l2_mbus_pixelcode *code)
  1593. {
  1594. if (index)
  1595. return -EINVAL;
  1596. /* Good enough for now */
  1597. *code = V4L2_MBUS_FMT_FIXED;
  1598. return 0;
  1599. }
  1600. static int adv7842_g_mbus_fmt(struct v4l2_subdev *sd,
  1601. struct v4l2_mbus_framefmt *fmt)
  1602. {
  1603. struct adv7842_state *state = to_state(sd);
  1604. fmt->width = state->timings.bt.width;
  1605. fmt->height = state->timings.bt.height;
  1606. fmt->code = V4L2_MBUS_FMT_FIXED;
  1607. fmt->field = V4L2_FIELD_NONE;
  1608. if (state->mode == ADV7842_MODE_SDP) {
  1609. /* SPD block */
  1610. if (!(sdp_read(sd, 0x5A) & 0x01))
  1611. return -EINVAL;
  1612. fmt->width = 720;
  1613. /* valid signal */
  1614. if (state->norm & V4L2_STD_525_60)
  1615. fmt->height = 480;
  1616. else
  1617. fmt->height = 576;
  1618. fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  1619. return 0;
  1620. }
  1621. if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
  1622. fmt->colorspace = (state->timings.bt.height <= 576) ?
  1623. V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
  1624. }
  1625. return 0;
  1626. }
  1627. static void adv7842_irq_enable(struct v4l2_subdev *sd, bool enable)
  1628. {
  1629. if (enable) {
  1630. /* Enable SSPD, STDI and CP locked/unlocked interrupts */
  1631. io_write(sd, 0x46, 0x9c);
  1632. /* ESDP_50HZ_DET interrupt */
  1633. io_write(sd, 0x5a, 0x10);
  1634. /* Enable CABLE_DET_A/B_ST (+5v) interrupt */
  1635. io_write(sd, 0x73, 0x03);
  1636. /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
  1637. io_write(sd, 0x78, 0x03);
  1638. /* Enable SDP Standard Detection Change and SDP Video Detected */
  1639. io_write(sd, 0xa0, 0x09);
  1640. /* Enable HDMI_MODE interrupt */
  1641. io_write(sd, 0x69, 0x08);
  1642. } else {
  1643. io_write(sd, 0x46, 0x0);
  1644. io_write(sd, 0x5a, 0x0);
  1645. io_write(sd, 0x73, 0x0);
  1646. io_write(sd, 0x78, 0x0);
  1647. io_write(sd, 0xa0, 0x0);
  1648. io_write(sd, 0x69, 0x0);
  1649. }
  1650. }
  1651. static int adv7842_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  1652. {
  1653. struct adv7842_state *state = to_state(sd);
  1654. u8 fmt_change_cp, fmt_change_digital, fmt_change_sdp;
  1655. u8 irq_status[6];
  1656. adv7842_irq_enable(sd, false);
  1657. /* read status */
  1658. irq_status[0] = io_read(sd, 0x43);
  1659. irq_status[1] = io_read(sd, 0x57);
  1660. irq_status[2] = io_read(sd, 0x70);
  1661. irq_status[3] = io_read(sd, 0x75);
  1662. irq_status[4] = io_read(sd, 0x9d);
  1663. irq_status[5] = io_read(sd, 0x66);
  1664. /* and clear */
  1665. if (irq_status[0])
  1666. io_write(sd, 0x44, irq_status[0]);
  1667. if (irq_status[1])
  1668. io_write(sd, 0x58, irq_status[1]);
  1669. if (irq_status[2])
  1670. io_write(sd, 0x71, irq_status[2]);
  1671. if (irq_status[3])
  1672. io_write(sd, 0x76, irq_status[3]);
  1673. if (irq_status[4])
  1674. io_write(sd, 0x9e, irq_status[4]);
  1675. if (irq_status[5])
  1676. io_write(sd, 0x67, irq_status[5]);
  1677. adv7842_irq_enable(sd, true);
  1678. v4l2_dbg(1, debug, sd, "%s: irq %x, %x, %x, %x, %x, %x\n", __func__,
  1679. irq_status[0], irq_status[1], irq_status[2],
  1680. irq_status[3], irq_status[4], irq_status[5]);
  1681. /* format change CP */
  1682. fmt_change_cp = irq_status[0] & 0x9c;
  1683. /* format change SDP */
  1684. if (state->mode == ADV7842_MODE_SDP)
  1685. fmt_change_sdp = (irq_status[1] & 0x30) | (irq_status[4] & 0x09);
  1686. else
  1687. fmt_change_sdp = 0;
  1688. /* digital format CP */
  1689. if (is_digital_input(sd))
  1690. fmt_change_digital = irq_status[3] & 0x03;
  1691. else
  1692. fmt_change_digital = 0;
  1693. /* format change */
  1694. if (fmt_change_cp || fmt_change_digital || fmt_change_sdp) {
  1695. v4l2_dbg(1, debug, sd,
  1696. "%s: fmt_change_cp = 0x%x, fmt_change_digital = 0x%x, fmt_change_sdp = 0x%x\n",
  1697. __func__, fmt_change_cp, fmt_change_digital,
  1698. fmt_change_sdp);
  1699. v4l2_subdev_notify(sd, ADV7842_FMT_CHANGE, NULL);
  1700. if (handled)
  1701. *handled = true;
  1702. }
  1703. /* HDMI/DVI mode */
  1704. if (irq_status[5] & 0x08) {
  1705. v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
  1706. (io_read(sd, 0x65) & 0x08) ? "HDMI" : "DVI");
  1707. set_rgb_quantization_range(sd);
  1708. if (handled)
  1709. *handled = true;
  1710. }
  1711. /* tx 5v detect */
  1712. if (irq_status[2] & 0x3) {
  1713. v4l2_dbg(1, debug, sd, "%s: irq tx_5v\n", __func__);
  1714. adv7842_s_detect_tx_5v_ctrl(sd);
  1715. if (handled)
  1716. *handled = true;
  1717. }
  1718. return 0;
  1719. }
  1720. static int adv7842_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  1721. {
  1722. struct adv7842_state *state = to_state(sd);
  1723. u8 *data = NULL;
  1724. if (edid->pad > ADV7842_EDID_PORT_VGA)
  1725. return -EINVAL;
  1726. if (edid->blocks == 0)
  1727. return -EINVAL;
  1728. if (edid->blocks > 2)
  1729. return -EINVAL;
  1730. if (edid->start_block > 1)
  1731. return -EINVAL;
  1732. if (edid->start_block == 1)
  1733. edid->blocks = 1;
  1734. switch (edid->pad) {
  1735. case ADV7842_EDID_PORT_A:
  1736. case ADV7842_EDID_PORT_B:
  1737. if (state->hdmi_edid.present & (0x04 << edid->pad))
  1738. data = state->hdmi_edid.edid;
  1739. break;
  1740. case ADV7842_EDID_PORT_VGA:
  1741. if (state->vga_edid.present)
  1742. data = state->vga_edid.edid;
  1743. break;
  1744. default:
  1745. return -EINVAL;
  1746. }
  1747. if (!data)
  1748. return -ENODATA;
  1749. memcpy(edid->edid,
  1750. data + edid->start_block * 128,
  1751. edid->blocks * 128);
  1752. return 0;
  1753. }
  1754. static int adv7842_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *e)
  1755. {
  1756. struct adv7842_state *state = to_state(sd);
  1757. int err = 0;
  1758. if (e->pad > ADV7842_EDID_PORT_VGA)
  1759. return -EINVAL;
  1760. if (e->start_block != 0)
  1761. return -EINVAL;
  1762. if (e->blocks > 2)
  1763. return -E2BIG;
  1764. /* todo, per edid */
  1765. state->aspect_ratio = v4l2_calc_aspect_ratio(e->edid[0x15],
  1766. e->edid[0x16]);
  1767. switch (e->pad) {
  1768. case ADV7842_EDID_PORT_VGA:
  1769. memset(&state->vga_edid.edid, 0, 256);
  1770. state->vga_edid.present = e->blocks ? 0x1 : 0x0;
  1771. memcpy(&state->vga_edid.edid, e->edid, 128 * e->blocks);
  1772. err = edid_write_vga_segment(sd);
  1773. break;
  1774. case ADV7842_EDID_PORT_A:
  1775. case ADV7842_EDID_PORT_B:
  1776. memset(&state->hdmi_edid.edid, 0, 256);
  1777. if (e->blocks)
  1778. state->hdmi_edid.present |= 0x04 << e->pad;
  1779. else
  1780. state->hdmi_edid.present &= ~(0x04 << e->pad);
  1781. memcpy(&state->hdmi_edid.edid, e->edid, 128 * e->blocks);
  1782. err = edid_write_hdmi_segment(sd, e->pad);
  1783. break;
  1784. default:
  1785. return -EINVAL;
  1786. }
  1787. if (err < 0)
  1788. v4l2_err(sd, "error %d writing edid on port %d\n", err, e->pad);
  1789. return err;
  1790. }
  1791. /*********** avi info frame CEA-861-E **************/
  1792. /* TODO move to common library */
  1793. struct avi_info_frame {
  1794. uint8_t f17;
  1795. uint8_t y10;
  1796. uint8_t a0;
  1797. uint8_t b10;
  1798. uint8_t s10;
  1799. uint8_t c10;
  1800. uint8_t m10;
  1801. uint8_t r3210;
  1802. uint8_t itc;
  1803. uint8_t ec210;
  1804. uint8_t q10;
  1805. uint8_t sc10;
  1806. uint8_t f47;
  1807. uint8_t vic;
  1808. uint8_t yq10;
  1809. uint8_t cn10;
  1810. uint8_t pr3210;
  1811. uint16_t etb;
  1812. uint16_t sbb;
  1813. uint16_t elb;
  1814. uint16_t srb;
  1815. };
  1816. static const char *y10_txt[4] = {
  1817. "RGB",
  1818. "YCbCr 4:2:2",
  1819. "YCbCr 4:4:4",
  1820. "Future",
  1821. };
  1822. static const char *c10_txt[4] = {
  1823. "No Data",
  1824. "SMPTE 170M",
  1825. "ITU-R 709",
  1826. "Extended Colorimetry information valied",
  1827. };
  1828. static const char *itc_txt[2] = {
  1829. "No Data",
  1830. "IT content",
  1831. };
  1832. static const char *ec210_txt[8] = {
  1833. "xvYCC601",
  1834. "xvYCC709",
  1835. "sYCC601",
  1836. "AdobeYCC601",
  1837. "AdobeRGB",
  1838. "5 reserved",
  1839. "6 reserved",
  1840. "7 reserved",
  1841. };
  1842. static const char *q10_txt[4] = {
  1843. "Default",
  1844. "Limited Range",
  1845. "Full Range",
  1846. "Reserved",
  1847. };
  1848. static void parse_avi_infoframe(struct v4l2_subdev *sd, uint8_t *buf,
  1849. struct avi_info_frame *avi)
  1850. {
  1851. avi->f17 = (buf[1] >> 7) & 0x1;
  1852. avi->y10 = (buf[1] >> 5) & 0x3;
  1853. avi->a0 = (buf[1] >> 4) & 0x1;
  1854. avi->b10 = (buf[1] >> 2) & 0x3;
  1855. avi->s10 = buf[1] & 0x3;
  1856. avi->c10 = (buf[2] >> 6) & 0x3;
  1857. avi->m10 = (buf[2] >> 4) & 0x3;
  1858. avi->r3210 = buf[2] & 0xf;
  1859. avi->itc = (buf[3] >> 7) & 0x1;
  1860. avi->ec210 = (buf[3] >> 4) & 0x7;
  1861. avi->q10 = (buf[3] >> 2) & 0x3;
  1862. avi->sc10 = buf[3] & 0x3;
  1863. avi->f47 = (buf[4] >> 7) & 0x1;
  1864. avi->vic = buf[4] & 0x7f;
  1865. avi->yq10 = (buf[5] >> 6) & 0x3;
  1866. avi->cn10 = (buf[5] >> 4) & 0x3;
  1867. avi->pr3210 = buf[5] & 0xf;
  1868. avi->etb = buf[6] + 256*buf[7];
  1869. avi->sbb = buf[8] + 256*buf[9];
  1870. avi->elb = buf[10] + 256*buf[11];
  1871. avi->srb = buf[12] + 256*buf[13];
  1872. }
  1873. static void print_avi_infoframe(struct v4l2_subdev *sd)
  1874. {
  1875. int i;
  1876. uint8_t buf[14];
  1877. u8 avi_len;
  1878. u8 avi_ver;
  1879. struct avi_info_frame avi;
  1880. if (!(hdmi_read(sd, 0x05) & 0x80)) {
  1881. v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
  1882. return;
  1883. }
  1884. if (!(io_read(sd, 0x60) & 0x01)) {
  1885. v4l2_info(sd, "AVI infoframe not received\n");
  1886. return;
  1887. }
  1888. if (io_read(sd, 0x88) & 0x10) {
  1889. v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
  1890. io_write(sd, 0x8a, 0x10); /* clear AVI_INF_CKS_ERR_RAW */
  1891. if (io_read(sd, 0x88) & 0x10) {
  1892. v4l2_info(sd, "AVI infoframe checksum error still present\n");
  1893. io_write(sd, 0x8a, 0x10); /* clear AVI_INF_CKS_ERR_RAW */
  1894. }
  1895. }
  1896. avi_len = infoframe_read(sd, 0xe2);
  1897. avi_ver = infoframe_read(sd, 0xe1);
  1898. v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
  1899. avi_ver, avi_len);
  1900. if (avi_ver != 0x02)
  1901. return;
  1902. for (i = 0; i < 14; i++)
  1903. buf[i] = infoframe_read(sd, i);
  1904. v4l2_info(sd, "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1905. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
  1906. buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
  1907. parse_avi_infoframe(sd, buf, &avi);
  1908. if (avi.vic)
  1909. v4l2_info(sd, "\tVIC: %d\n", avi.vic);
  1910. if (avi.itc)
  1911. v4l2_info(sd, "\t%s\n", itc_txt[avi.itc]);
  1912. if (avi.y10)
  1913. v4l2_info(sd, "\t%s %s\n", y10_txt[avi.y10], !avi.c10 ? "" :
  1914. (avi.c10 == 0x3 ? ec210_txt[avi.ec210] : c10_txt[avi.c10]));
  1915. else
  1916. v4l2_info(sd, "\t%s %s\n", y10_txt[avi.y10], q10_txt[avi.q10]);
  1917. }
  1918. static const char * const prim_mode_txt[] = {
  1919. "SDP",
  1920. "Component",
  1921. "Graphics",
  1922. "Reserved",
  1923. "CVBS & HDMI AUDIO",
  1924. "HDMI-Comp",
  1925. "HDMI-GR",
  1926. "Reserved",
  1927. "Reserved",
  1928. "Reserved",
  1929. "Reserved",
  1930. "Reserved",
  1931. "Reserved",
  1932. "Reserved",
  1933. "Reserved",
  1934. "Reserved",
  1935. };
  1936. static int adv7842_sdp_log_status(struct v4l2_subdev *sd)
  1937. {
  1938. /* SDP (Standard definition processor) block */
  1939. uint8_t sdp_signal_detected = sdp_read(sd, 0x5A) & 0x01;
  1940. v4l2_info(sd, "Chip powered %s\n", no_power(sd) ? "off" : "on");
  1941. v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x\n",
  1942. io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f);
  1943. v4l2_info(sd, "SDP: free run: %s\n",
  1944. (sdp_read(sd, 0x56) & 0x01) ? "on" : "off");
  1945. v4l2_info(sd, "SDP: %s\n", sdp_signal_detected ?
  1946. "valid SD/PR signal detected" : "invalid/no signal");
  1947. if (sdp_signal_detected) {
  1948. static const char * const sdp_std_txt[] = {
  1949. "NTSC-M/J",
  1950. "1?",
  1951. "NTSC-443",
  1952. "60HzSECAM",
  1953. "PAL-M",
  1954. "5?",
  1955. "PAL-60",
  1956. "7?", "8?", "9?", "a?", "b?",
  1957. "PAL-CombN",
  1958. "d?",
  1959. "PAL-BGHID",
  1960. "SECAM"
  1961. };
  1962. v4l2_info(sd, "SDP: standard %s\n",
  1963. sdp_std_txt[sdp_read(sd, 0x52) & 0x0f]);
  1964. v4l2_info(sd, "SDP: %s\n",
  1965. (sdp_read(sd, 0x59) & 0x08) ? "50Hz" : "60Hz");
  1966. v4l2_info(sd, "SDP: %s\n",
  1967. (sdp_read(sd, 0x57) & 0x08) ? "Interlaced" : "Progressive");
  1968. v4l2_info(sd, "SDP: deinterlacer %s\n",
  1969. (sdp_read(sd, 0x12) & 0x08) ? "enabled" : "disabled");
  1970. v4l2_info(sd, "SDP: csc %s mode\n",
  1971. (sdp_io_read(sd, 0xe0) & 0x40) ? "auto" : "manual");
  1972. }
  1973. return 0;
  1974. }
  1975. static int adv7842_cp_log_status(struct v4l2_subdev *sd)
  1976. {
  1977. /* CP block */
  1978. struct adv7842_state *state = to_state(sd);
  1979. struct v4l2_dv_timings timings;
  1980. uint8_t reg_io_0x02 = io_read(sd, 0x02);
  1981. uint8_t reg_io_0x21 = io_read(sd, 0x21);
  1982. uint8_t reg_rep_0x77 = rep_read(sd, 0x77);
  1983. uint8_t reg_rep_0x7d = rep_read(sd, 0x7d);
  1984. bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
  1985. bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
  1986. bool audio_mute = io_read(sd, 0x65) & 0x40;
  1987. static const char * const csc_coeff_sel_rb[16] = {
  1988. "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
  1989. "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
  1990. "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
  1991. "reserved", "reserved", "reserved", "reserved", "manual"
  1992. };
  1993. static const char * const input_color_space_txt[16] = {
  1994. "RGB limited range (16-235)", "RGB full range (0-255)",
  1995. "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
  1996. "xvYCC Bt.601", "xvYCC Bt.709",
  1997. "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
  1998. "invalid", "invalid", "invalid", "invalid", "invalid",
  1999. "invalid", "invalid", "automatic"
  2000. };
  2001. static const char * const rgb_quantization_range_txt[] = {
  2002. "Automatic",
  2003. "RGB limited range (16-235)",
  2004. "RGB full range (0-255)",
  2005. };
  2006. static const char * const deep_color_mode_txt[4] = {
  2007. "8-bits per channel",
  2008. "10-bits per channel",
  2009. "12-bits per channel",
  2010. "16-bits per channel (not supported)"
  2011. };
  2012. v4l2_info(sd, "-----Chip status-----\n");
  2013. v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
  2014. v4l2_info(sd, "HDMI/DVI-D port selected: %s\n",
  2015. state->hdmi_port_a ? "A" : "B");
  2016. v4l2_info(sd, "EDID A %s, B %s\n",
  2017. ((reg_rep_0x7d & 0x04) && (reg_rep_0x77 & 0x04)) ?
  2018. "enabled" : "disabled",
  2019. ((reg_rep_0x7d & 0x08) && (reg_rep_0x77 & 0x08)) ?
  2020. "enabled" : "disabled");
  2021. v4l2_info(sd, "HPD A %s, B %s\n",
  2022. reg_io_0x21 & 0x02 ? "enabled" : "disabled",
  2023. reg_io_0x21 & 0x01 ? "enabled" : "disabled");
  2024. v4l2_info(sd, "CEC %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
  2025. "enabled" : "disabled");
  2026. v4l2_info(sd, "-----Signal status-----\n");
  2027. if (state->hdmi_port_a) {
  2028. v4l2_info(sd, "Cable detected (+5V power): %s\n",
  2029. io_read(sd, 0x6f) & 0x02 ? "true" : "false");
  2030. v4l2_info(sd, "TMDS signal detected: %s\n",
  2031. (io_read(sd, 0x6a) & 0x02) ? "true" : "false");
  2032. v4l2_info(sd, "TMDS signal locked: %s\n",
  2033. (io_read(sd, 0x6a) & 0x20) ? "true" : "false");
  2034. } else {
  2035. v4l2_info(sd, "Cable detected (+5V power):%s\n",
  2036. io_read(sd, 0x6f) & 0x01 ? "true" : "false");
  2037. v4l2_info(sd, "TMDS signal detected: %s\n",
  2038. (io_read(sd, 0x6a) & 0x01) ? "true" : "false");
  2039. v4l2_info(sd, "TMDS signal locked: %s\n",
  2040. (io_read(sd, 0x6a) & 0x10) ? "true" : "false");
  2041. }
  2042. v4l2_info(sd, "CP free run: %s\n",
  2043. (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
  2044. v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
  2045. io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
  2046. (io_read(sd, 0x01) & 0x70) >> 4);
  2047. v4l2_info(sd, "-----Video Timings-----\n");
  2048. if (no_cp_signal(sd)) {
  2049. v4l2_info(sd, "STDI: not locked\n");
  2050. } else {
  2051. uint32_t bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
  2052. uint32_t lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
  2053. uint32_t lcvs = cp_read(sd, 0xb3) >> 3;
  2054. uint32_t fcl = ((cp_read(sd, 0xb8) & 0x1f) << 8) | cp_read(sd, 0xb9);
  2055. char hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
  2056. ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
  2057. char vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
  2058. ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
  2059. v4l2_info(sd,
  2060. "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, fcl = %d, %s, %chsync, %cvsync\n",
  2061. lcf, bl, lcvs, fcl,
  2062. (cp_read(sd, 0xb1) & 0x40) ?
  2063. "interlaced" : "progressive",
  2064. hs_pol, vs_pol);
  2065. }
  2066. if (adv7842_query_dv_timings(sd, &timings))
  2067. v4l2_info(sd, "No video detected\n");
  2068. else
  2069. v4l2_print_dv_timings(sd->name, "Detected format: ",
  2070. &timings, true);
  2071. v4l2_print_dv_timings(sd->name, "Configured format: ",
  2072. &state->timings, true);
  2073. if (no_cp_signal(sd))
  2074. return 0;
  2075. v4l2_info(sd, "-----Color space-----\n");
  2076. v4l2_info(sd, "RGB quantization range ctrl: %s\n",
  2077. rgb_quantization_range_txt[state->rgb_quantization_range]);
  2078. v4l2_info(sd, "Input color space: %s\n",
  2079. input_color_space_txt[reg_io_0x02 >> 4]);
  2080. v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
  2081. (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
  2082. (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
  2083. ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
  2084. "enabled" : "disabled");
  2085. v4l2_info(sd, "Color space conversion: %s\n",
  2086. csc_coeff_sel_rb[cp_read(sd, 0xf4) >> 4]);
  2087. if (!is_digital_input(sd))
  2088. return 0;
  2089. v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
  2090. v4l2_info(sd, "HDCP encrypted content: %s\n",
  2091. (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
  2092. v4l2_info(sd, "HDCP keys read: %s%s\n",
  2093. (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
  2094. (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
  2095. if (!is_hdmi(sd))
  2096. return 0;
  2097. v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
  2098. audio_pll_locked ? "locked" : "not locked",
  2099. audio_sample_packet_detect ? "detected" : "not detected",
  2100. audio_mute ? "muted" : "enabled");
  2101. if (audio_pll_locked && audio_sample_packet_detect) {
  2102. v4l2_info(sd, "Audio format: %s\n",
  2103. (hdmi_read(sd, 0x07) & 0x40) ? "multi-channel" : "stereo");
  2104. }
  2105. v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
  2106. (hdmi_read(sd, 0x5c) << 8) +
  2107. (hdmi_read(sd, 0x5d) & 0xf0));
  2108. v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
  2109. (hdmi_read(sd, 0x5e) << 8) +
  2110. hdmi_read(sd, 0x5f));
  2111. v4l2_info(sd, "AV Mute: %s\n",
  2112. (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
  2113. v4l2_info(sd, "Deep color mode: %s\n",
  2114. deep_color_mode_txt[hdmi_read(sd, 0x0b) >> 6]);
  2115. print_avi_infoframe(sd);
  2116. return 0;
  2117. }
  2118. static int adv7842_log_status(struct v4l2_subdev *sd)
  2119. {
  2120. struct adv7842_state *state = to_state(sd);
  2121. if (state->mode == ADV7842_MODE_SDP)
  2122. return adv7842_sdp_log_status(sd);
  2123. return adv7842_cp_log_status(sd);
  2124. }
  2125. static int adv7842_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  2126. {
  2127. struct adv7842_state *state = to_state(sd);
  2128. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  2129. if (state->mode != ADV7842_MODE_SDP)
  2130. return -ENODATA;
  2131. if (!(sdp_read(sd, 0x5A) & 0x01)) {
  2132. *std = 0;
  2133. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  2134. return 0;
  2135. }
  2136. switch (sdp_read(sd, 0x52) & 0x0f) {
  2137. case 0:
  2138. /* NTSC-M/J */
  2139. *std &= V4L2_STD_NTSC;
  2140. break;
  2141. case 2:
  2142. /* NTSC-443 */
  2143. *std &= V4L2_STD_NTSC_443;
  2144. break;
  2145. case 3:
  2146. /* 60HzSECAM */
  2147. *std &= V4L2_STD_SECAM;
  2148. break;
  2149. case 4:
  2150. /* PAL-M */
  2151. *std &= V4L2_STD_PAL_M;
  2152. break;
  2153. case 6:
  2154. /* PAL-60 */
  2155. *std &= V4L2_STD_PAL_60;
  2156. break;
  2157. case 0xc:
  2158. /* PAL-CombN */
  2159. *std &= V4L2_STD_PAL_Nc;
  2160. break;
  2161. case 0xe:
  2162. /* PAL-BGHID */
  2163. *std &= V4L2_STD_PAL;
  2164. break;
  2165. case 0xf:
  2166. /* SECAM */
  2167. *std &= V4L2_STD_SECAM;
  2168. break;
  2169. default:
  2170. *std &= V4L2_STD_ALL;
  2171. break;
  2172. }
  2173. return 0;
  2174. }
  2175. static void adv7842_s_sdp_io(struct v4l2_subdev *sd, struct adv7842_sdp_io_sync_adjustment *s)
  2176. {
  2177. if (s && s->adjust) {
  2178. sdp_io_write(sd, 0x94, (s->hs_beg >> 8) & 0xf);
  2179. sdp_io_write(sd, 0x95, s->hs_beg & 0xff);
  2180. sdp_io_write(sd, 0x96, (s->hs_width >> 8) & 0xf);
  2181. sdp_io_write(sd, 0x97, s->hs_width & 0xff);
  2182. sdp_io_write(sd, 0x98, (s->de_beg >> 8) & 0xf);
  2183. sdp_io_write(sd, 0x99, s->de_beg & 0xff);
  2184. sdp_io_write(sd, 0x9a, (s->de_end >> 8) & 0xf);
  2185. sdp_io_write(sd, 0x9b, s->de_end & 0xff);
  2186. sdp_io_write(sd, 0xa8, s->vs_beg_o);
  2187. sdp_io_write(sd, 0xa9, s->vs_beg_e);
  2188. sdp_io_write(sd, 0xaa, s->vs_end_o);
  2189. sdp_io_write(sd, 0xab, s->vs_end_e);
  2190. sdp_io_write(sd, 0xac, s->de_v_beg_o);
  2191. sdp_io_write(sd, 0xad, s->de_v_beg_e);
  2192. sdp_io_write(sd, 0xae, s->de_v_end_o);
  2193. sdp_io_write(sd, 0xaf, s->de_v_end_e);
  2194. } else {
  2195. /* set to default */
  2196. sdp_io_write(sd, 0x94, 0x00);
  2197. sdp_io_write(sd, 0x95, 0x00);
  2198. sdp_io_write(sd, 0x96, 0x00);
  2199. sdp_io_write(sd, 0x97, 0x20);
  2200. sdp_io_write(sd, 0x98, 0x00);
  2201. sdp_io_write(sd, 0x99, 0x00);
  2202. sdp_io_write(sd, 0x9a, 0x00);
  2203. sdp_io_write(sd, 0x9b, 0x00);
  2204. sdp_io_write(sd, 0xa8, 0x04);
  2205. sdp_io_write(sd, 0xa9, 0x04);
  2206. sdp_io_write(sd, 0xaa, 0x04);
  2207. sdp_io_write(sd, 0xab, 0x04);
  2208. sdp_io_write(sd, 0xac, 0x04);
  2209. sdp_io_write(sd, 0xad, 0x04);
  2210. sdp_io_write(sd, 0xae, 0x04);
  2211. sdp_io_write(sd, 0xaf, 0x04);
  2212. }
  2213. }
  2214. static int adv7842_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  2215. {
  2216. struct adv7842_state *state = to_state(sd);
  2217. struct adv7842_platform_data *pdata = &state->pdata;
  2218. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  2219. if (state->mode != ADV7842_MODE_SDP)
  2220. return -ENODATA;
  2221. if (norm & V4L2_STD_625_50)
  2222. adv7842_s_sdp_io(sd, &pdata->sdp_io_sync_625);
  2223. else if (norm & V4L2_STD_525_60)
  2224. adv7842_s_sdp_io(sd, &pdata->sdp_io_sync_525);
  2225. else
  2226. adv7842_s_sdp_io(sd, NULL);
  2227. if (norm & V4L2_STD_ALL) {
  2228. state->norm = norm;
  2229. return 0;
  2230. }
  2231. return -EINVAL;
  2232. }
  2233. static int adv7842_g_std(struct v4l2_subdev *sd, v4l2_std_id *norm)
  2234. {
  2235. struct adv7842_state *state = to_state(sd);
  2236. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  2237. if (state->mode != ADV7842_MODE_SDP)
  2238. return -ENODATA;
  2239. *norm = state->norm;
  2240. return 0;
  2241. }
  2242. /* ----------------------------------------------------------------------- */
  2243. static int adv7842_core_init(struct v4l2_subdev *sd)
  2244. {
  2245. struct adv7842_state *state = to_state(sd);
  2246. struct adv7842_platform_data *pdata = &state->pdata;
  2247. hdmi_write(sd, 0x48,
  2248. (pdata->disable_pwrdnb ? 0x80 : 0) |
  2249. (pdata->disable_cable_det_rst ? 0x40 : 0));
  2250. disable_input(sd);
  2251. /*
  2252. * Disable I2C access to internal EDID ram from HDMI DDC ports
  2253. * Disable auto edid enable when leaving powerdown mode
  2254. */
  2255. rep_write_and_or(sd, 0x77, 0xd3, 0x20);
  2256. /* power */
  2257. io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
  2258. io_write(sd, 0x15, 0x80); /* Power up pads */
  2259. /* video format */
  2260. io_write(sd, 0x02,
  2261. 0xf0 |
  2262. pdata->alt_gamma << 3 |
  2263. pdata->op_656_range << 2 |
  2264. pdata->rgb_out << 1 |
  2265. pdata->alt_data_sat << 0);
  2266. io_write(sd, 0x03, pdata->op_format_sel);
  2267. io_write_and_or(sd, 0x04, 0x1f, pdata->op_ch_sel << 5);
  2268. io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
  2269. pdata->insert_av_codes << 2 |
  2270. pdata->replicate_av_codes << 1 |
  2271. pdata->invert_cbcr << 0);
  2272. /* HDMI audio */
  2273. hdmi_write_and_or(sd, 0x1a, 0xf1, 0x08); /* Wait 1 s before unmute */
  2274. /* Drive strength */
  2275. io_write_and_or(sd, 0x14, 0xc0,
  2276. pdata->dr_str_data << 4 |
  2277. pdata->dr_str_clk << 2 |
  2278. pdata->dr_str_sync);
  2279. /* HDMI free run */
  2280. cp_write_and_or(sd, 0xba, 0xfc, pdata->hdmi_free_run_enable |
  2281. (pdata->hdmi_free_run_mode << 1));
  2282. /* SPD free run */
  2283. sdp_write_and_or(sd, 0xdd, 0xf0, pdata->sdp_free_run_force |
  2284. (pdata->sdp_free_run_cbar_en << 1) |
  2285. (pdata->sdp_free_run_man_col_en << 2) |
  2286. (pdata->sdp_free_run_auto << 3));
  2287. /* TODO from platform data */
  2288. cp_write(sd, 0x69, 0x14); /* Enable CP CSC */
  2289. io_write(sd, 0x06, 0xa6); /* positive VS and HS and DE */
  2290. cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
  2291. afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
  2292. afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
  2293. io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
  2294. sdp_csc_coeff(sd, &pdata->sdp_csc_coeff);
  2295. /* todo, improve settings for sdram */
  2296. if (pdata->sd_ram_size >= 128) {
  2297. sdp_write(sd, 0x12, 0x0d); /* Frame TBC,3D comb enabled */
  2298. if (pdata->sd_ram_ddr) {
  2299. /* SDP setup for the AD eval board */
  2300. sdp_io_write(sd, 0x6f, 0x00); /* DDR mode */
  2301. sdp_io_write(sd, 0x75, 0x0a); /* 128 MB memory size */
  2302. sdp_io_write(sd, 0x7a, 0xa5); /* Timing Adjustment */
  2303. sdp_io_write(sd, 0x7b, 0x8f); /* Timing Adjustment */
  2304. sdp_io_write(sd, 0x60, 0x01); /* SDRAM reset */
  2305. } else {
  2306. sdp_io_write(sd, 0x75, 0x0a); /* 64 MB memory size ?*/
  2307. sdp_io_write(sd, 0x74, 0x00); /* must be zero for sdr sdram */
  2308. sdp_io_write(sd, 0x79, 0x33); /* CAS latency to 3,
  2309. depends on memory */
  2310. sdp_io_write(sd, 0x6f, 0x01); /* SDR mode */
  2311. sdp_io_write(sd, 0x7a, 0xa5); /* Timing Adjustment */
  2312. sdp_io_write(sd, 0x7b, 0x8f); /* Timing Adjustment */
  2313. sdp_io_write(sd, 0x60, 0x01); /* SDRAM reset */
  2314. }
  2315. } else {
  2316. /*
  2317. * Manual UG-214, rev 0 is bit confusing on this bit
  2318. * but a '1' disables any signal if the Ram is active.
  2319. */
  2320. sdp_io_write(sd, 0x29, 0x10); /* Tristate memory interface */
  2321. }
  2322. select_input(sd, pdata->vid_std_select);
  2323. enable_input(sd);
  2324. if (pdata->hpa_auto) {
  2325. /* HPA auto, HPA 0.5s after Edid set and Cable detect */
  2326. hdmi_write(sd, 0x69, 0x5c);
  2327. } else {
  2328. /* HPA manual */
  2329. hdmi_write(sd, 0x69, 0xa3);
  2330. /* HPA disable on port A and B */
  2331. io_write_and_or(sd, 0x20, 0xcf, 0x00);
  2332. }
  2333. /* LLC */
  2334. io_write(sd, 0x19, 0x80 | pdata->llc_dll_phase);
  2335. io_write(sd, 0x33, 0x40);
  2336. /* interrupts */
  2337. io_write(sd, 0x40, 0xf2); /* Configure INT1 */
  2338. adv7842_irq_enable(sd, true);
  2339. return v4l2_ctrl_handler_setup(sd->ctrl_handler);
  2340. }
  2341. /* ----------------------------------------------------------------------- */
  2342. static int adv7842_ddr_ram_test(struct v4l2_subdev *sd)
  2343. {
  2344. /*
  2345. * From ADV784x external Memory test.pdf
  2346. *
  2347. * Reset must just been performed before running test.
  2348. * Recommended to reset after test.
  2349. */
  2350. int i;
  2351. int pass = 0;
  2352. int fail = 0;
  2353. int complete = 0;
  2354. io_write(sd, 0x00, 0x01); /* Program SDP 4x1 */
  2355. io_write(sd, 0x01, 0x00); /* Program SDP mode */
  2356. afe_write(sd, 0x80, 0x92); /* SDP Recommeneded Write */
  2357. afe_write(sd, 0x9B, 0x01); /* SDP Recommeneded Write ADV7844ES1 */
  2358. afe_write(sd, 0x9C, 0x60); /* SDP Recommeneded Write ADV7844ES1 */
  2359. afe_write(sd, 0x9E, 0x02); /* SDP Recommeneded Write ADV7844ES1 */
  2360. afe_write(sd, 0xA0, 0x0B); /* SDP Recommeneded Write ADV7844ES1 */
  2361. afe_write(sd, 0xC3, 0x02); /* Memory BIST Initialisation */
  2362. io_write(sd, 0x0C, 0x40); /* Power up ADV7844 */
  2363. io_write(sd, 0x15, 0xBA); /* Enable outputs */
  2364. sdp_write(sd, 0x12, 0x00); /* Disable 3D comb, Frame TBC & 3DNR */
  2365. io_write(sd, 0xFF, 0x04); /* Reset memory controller */
  2366. mdelay(5);
  2367. sdp_write(sd, 0x12, 0x00); /* Disable 3D Comb, Frame TBC & 3DNR */
  2368. sdp_io_write(sd, 0x2A, 0x01); /* Memory BIST Initialisation */
  2369. sdp_io_write(sd, 0x7c, 0x19); /* Memory BIST Initialisation */
  2370. sdp_io_write(sd, 0x80, 0x87); /* Memory BIST Initialisation */
  2371. sdp_io_write(sd, 0x81, 0x4a); /* Memory BIST Initialisation */
  2372. sdp_io_write(sd, 0x82, 0x2c); /* Memory BIST Initialisation */
  2373. sdp_io_write(sd, 0x83, 0x0e); /* Memory BIST Initialisation */
  2374. sdp_io_write(sd, 0x84, 0x94); /* Memory BIST Initialisation */
  2375. sdp_io_write(sd, 0x85, 0x62); /* Memory BIST Initialisation */
  2376. sdp_io_write(sd, 0x7d, 0x00); /* Memory BIST Initialisation */
  2377. sdp_io_write(sd, 0x7e, 0x1a); /* Memory BIST Initialisation */
  2378. mdelay(5);
  2379. sdp_io_write(sd, 0xd9, 0xd5); /* Enable BIST Test */
  2380. sdp_write(sd, 0x12, 0x05); /* Enable FRAME TBC & 3D COMB */
  2381. mdelay(20);
  2382. for (i = 0; i < 10; i++) {
  2383. u8 result = sdp_io_read(sd, 0xdb);
  2384. if (result & 0x10) {
  2385. complete++;
  2386. if (result & 0x20)
  2387. fail++;
  2388. else
  2389. pass++;
  2390. }
  2391. mdelay(20);
  2392. }
  2393. v4l2_dbg(1, debug, sd,
  2394. "Ram Test: completed %d of %d: pass %d, fail %d\n",
  2395. complete, i, pass, fail);
  2396. if (!complete || fail)
  2397. return -EIO;
  2398. return 0;
  2399. }
  2400. static void adv7842_rewrite_i2c_addresses(struct v4l2_subdev *sd,
  2401. struct adv7842_platform_data *pdata)
  2402. {
  2403. io_write(sd, 0xf1, pdata->i2c_sdp << 1);
  2404. io_write(sd, 0xf2, pdata->i2c_sdp_io << 1);
  2405. io_write(sd, 0xf3, pdata->i2c_avlink << 1);
  2406. io_write(sd, 0xf4, pdata->i2c_cec << 1);
  2407. io_write(sd, 0xf5, pdata->i2c_infoframe << 1);
  2408. io_write(sd, 0xf8, pdata->i2c_afe << 1);
  2409. io_write(sd, 0xf9, pdata->i2c_repeater << 1);
  2410. io_write(sd, 0xfa, pdata->i2c_edid << 1);
  2411. io_write(sd, 0xfb, pdata->i2c_hdmi << 1);
  2412. io_write(sd, 0xfd, pdata->i2c_cp << 1);
  2413. io_write(sd, 0xfe, pdata->i2c_vdp << 1);
  2414. }
  2415. static int adv7842_command_ram_test(struct v4l2_subdev *sd)
  2416. {
  2417. struct i2c_client *client = v4l2_get_subdevdata(sd);
  2418. struct adv7842_state *state = to_state(sd);
  2419. struct adv7842_platform_data *pdata = client->dev.platform_data;
  2420. struct v4l2_dv_timings timings;
  2421. int ret = 0;
  2422. if (!pdata)
  2423. return -ENODEV;
  2424. if (!pdata->sd_ram_size || !pdata->sd_ram_ddr) {
  2425. v4l2_info(sd, "no sdram or no ddr sdram\n");
  2426. return -EINVAL;
  2427. }
  2428. main_reset(sd);
  2429. adv7842_rewrite_i2c_addresses(sd, pdata);
  2430. /* run ram test */
  2431. ret = adv7842_ddr_ram_test(sd);
  2432. main_reset(sd);
  2433. adv7842_rewrite_i2c_addresses(sd, pdata);
  2434. /* and re-init chip and state */
  2435. adv7842_core_init(sd);
  2436. disable_input(sd);
  2437. select_input(sd, state->vid_std_select);
  2438. enable_input(sd);
  2439. edid_write_vga_segment(sd);
  2440. edid_write_hdmi_segment(sd, ADV7842_EDID_PORT_A);
  2441. edid_write_hdmi_segment(sd, ADV7842_EDID_PORT_B);
  2442. timings = state->timings;
  2443. memset(&state->timings, 0, sizeof(struct v4l2_dv_timings));
  2444. adv7842_s_dv_timings(sd, &timings);
  2445. return ret;
  2446. }
  2447. static long adv7842_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  2448. {
  2449. switch (cmd) {
  2450. case ADV7842_CMD_RAM_TEST:
  2451. return adv7842_command_ram_test(sd);
  2452. }
  2453. return -ENOTTY;
  2454. }
  2455. /* ----------------------------------------------------------------------- */
  2456. static const struct v4l2_ctrl_ops adv7842_ctrl_ops = {
  2457. .s_ctrl = adv7842_s_ctrl,
  2458. };
  2459. static const struct v4l2_subdev_core_ops adv7842_core_ops = {
  2460. .log_status = adv7842_log_status,
  2461. .ioctl = adv7842_ioctl,
  2462. .interrupt_service_routine = adv7842_isr,
  2463. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2464. .g_register = adv7842_g_register,
  2465. .s_register = adv7842_s_register,
  2466. #endif
  2467. };
  2468. static const struct v4l2_subdev_video_ops adv7842_video_ops = {
  2469. .g_std = adv7842_g_std,
  2470. .s_std = adv7842_s_std,
  2471. .s_routing = adv7842_s_routing,
  2472. .querystd = adv7842_querystd,
  2473. .g_input_status = adv7842_g_input_status,
  2474. .s_dv_timings = adv7842_s_dv_timings,
  2475. .g_dv_timings = adv7842_g_dv_timings,
  2476. .query_dv_timings = adv7842_query_dv_timings,
  2477. .enum_mbus_fmt = adv7842_enum_mbus_fmt,
  2478. .g_mbus_fmt = adv7842_g_mbus_fmt,
  2479. .try_mbus_fmt = adv7842_g_mbus_fmt,
  2480. .s_mbus_fmt = adv7842_g_mbus_fmt,
  2481. };
  2482. static const struct v4l2_subdev_pad_ops adv7842_pad_ops = {
  2483. .get_edid = adv7842_get_edid,
  2484. .set_edid = adv7842_set_edid,
  2485. .enum_dv_timings = adv7842_enum_dv_timings,
  2486. .dv_timings_cap = adv7842_dv_timings_cap,
  2487. };
  2488. static const struct v4l2_subdev_ops adv7842_ops = {
  2489. .core = &adv7842_core_ops,
  2490. .video = &adv7842_video_ops,
  2491. .pad = &adv7842_pad_ops,
  2492. };
  2493. /* -------------------------- custom ctrls ---------------------------------- */
  2494. static const struct v4l2_ctrl_config adv7842_ctrl_analog_sampling_phase = {
  2495. .ops = &adv7842_ctrl_ops,
  2496. .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
  2497. .name = "Analog Sampling Phase",
  2498. .type = V4L2_CTRL_TYPE_INTEGER,
  2499. .min = 0,
  2500. .max = 0x1f,
  2501. .step = 1,
  2502. .def = 0,
  2503. };
  2504. static const struct v4l2_ctrl_config adv7842_ctrl_free_run_color_manual = {
  2505. .ops = &adv7842_ctrl_ops,
  2506. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
  2507. .name = "Free Running Color, Manual",
  2508. .type = V4L2_CTRL_TYPE_BOOLEAN,
  2509. .max = 1,
  2510. .step = 1,
  2511. .def = 1,
  2512. };
  2513. static const struct v4l2_ctrl_config adv7842_ctrl_free_run_color = {
  2514. .ops = &adv7842_ctrl_ops,
  2515. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
  2516. .name = "Free Running Color",
  2517. .type = V4L2_CTRL_TYPE_INTEGER,
  2518. .max = 0xffffff,
  2519. .step = 0x1,
  2520. };
  2521. static void adv7842_unregister_clients(struct v4l2_subdev *sd)
  2522. {
  2523. struct adv7842_state *state = to_state(sd);
  2524. if (state->i2c_avlink)
  2525. i2c_unregister_device(state->i2c_avlink);
  2526. if (state->i2c_cec)
  2527. i2c_unregister_device(state->i2c_cec);
  2528. if (state->i2c_infoframe)
  2529. i2c_unregister_device(state->i2c_infoframe);
  2530. if (state->i2c_sdp_io)
  2531. i2c_unregister_device(state->i2c_sdp_io);
  2532. if (state->i2c_sdp)
  2533. i2c_unregister_device(state->i2c_sdp);
  2534. if (state->i2c_afe)
  2535. i2c_unregister_device(state->i2c_afe);
  2536. if (state->i2c_repeater)
  2537. i2c_unregister_device(state->i2c_repeater);
  2538. if (state->i2c_edid)
  2539. i2c_unregister_device(state->i2c_edid);
  2540. if (state->i2c_hdmi)
  2541. i2c_unregister_device(state->i2c_hdmi);
  2542. if (state->i2c_cp)
  2543. i2c_unregister_device(state->i2c_cp);
  2544. if (state->i2c_vdp)
  2545. i2c_unregister_device(state->i2c_vdp);
  2546. state->i2c_avlink = NULL;
  2547. state->i2c_cec = NULL;
  2548. state->i2c_infoframe = NULL;
  2549. state->i2c_sdp_io = NULL;
  2550. state->i2c_sdp = NULL;
  2551. state->i2c_afe = NULL;
  2552. state->i2c_repeater = NULL;
  2553. state->i2c_edid = NULL;
  2554. state->i2c_hdmi = NULL;
  2555. state->i2c_cp = NULL;
  2556. state->i2c_vdp = NULL;
  2557. }
  2558. static struct i2c_client *adv7842_dummy_client(struct v4l2_subdev *sd, const char *desc,
  2559. u8 addr, u8 io_reg)
  2560. {
  2561. struct i2c_client *client = v4l2_get_subdevdata(sd);
  2562. struct i2c_client *cp;
  2563. io_write(sd, io_reg, addr << 1);
  2564. if (addr == 0) {
  2565. v4l2_err(sd, "no %s i2c addr configured\n", desc);
  2566. return NULL;
  2567. }
  2568. cp = i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
  2569. if (!cp)
  2570. v4l2_err(sd, "register %s on i2c addr 0x%x failed\n", desc, addr);
  2571. return cp;
  2572. }
  2573. static int adv7842_register_clients(struct v4l2_subdev *sd)
  2574. {
  2575. struct adv7842_state *state = to_state(sd);
  2576. struct adv7842_platform_data *pdata = &state->pdata;
  2577. state->i2c_avlink = adv7842_dummy_client(sd, "avlink", pdata->i2c_avlink, 0xf3);
  2578. state->i2c_cec = adv7842_dummy_client(sd, "cec", pdata->i2c_cec, 0xf4);
  2579. state->i2c_infoframe = adv7842_dummy_client(sd, "infoframe", pdata->i2c_infoframe, 0xf5);
  2580. state->i2c_sdp_io = adv7842_dummy_client(sd, "sdp_io", pdata->i2c_sdp_io, 0xf2);
  2581. state->i2c_sdp = adv7842_dummy_client(sd, "sdp", pdata->i2c_sdp, 0xf1);
  2582. state->i2c_afe = adv7842_dummy_client(sd, "afe", pdata->i2c_afe, 0xf8);
  2583. state->i2c_repeater = adv7842_dummy_client(sd, "repeater", pdata->i2c_repeater, 0xf9);
  2584. state->i2c_edid = adv7842_dummy_client(sd, "edid", pdata->i2c_edid, 0xfa);
  2585. state->i2c_hdmi = adv7842_dummy_client(sd, "hdmi", pdata->i2c_hdmi, 0xfb);
  2586. state->i2c_cp = adv7842_dummy_client(sd, "cp", pdata->i2c_cp, 0xfd);
  2587. state->i2c_vdp = adv7842_dummy_client(sd, "vdp", pdata->i2c_vdp, 0xfe);
  2588. if (!state->i2c_avlink ||
  2589. !state->i2c_cec ||
  2590. !state->i2c_infoframe ||
  2591. !state->i2c_sdp_io ||
  2592. !state->i2c_sdp ||
  2593. !state->i2c_afe ||
  2594. !state->i2c_repeater ||
  2595. !state->i2c_edid ||
  2596. !state->i2c_hdmi ||
  2597. !state->i2c_cp ||
  2598. !state->i2c_vdp)
  2599. return -1;
  2600. return 0;
  2601. }
  2602. static int adv7842_probe(struct i2c_client *client,
  2603. const struct i2c_device_id *id)
  2604. {
  2605. struct adv7842_state *state;
  2606. static const struct v4l2_dv_timings cea640x480 =
  2607. V4L2_DV_BT_CEA_640X480P59_94;
  2608. struct adv7842_platform_data *pdata = client->dev.platform_data;
  2609. struct v4l2_ctrl_handler *hdl;
  2610. struct v4l2_subdev *sd;
  2611. u16 rev;
  2612. int err;
  2613. /* Check if the adapter supports the needed features */
  2614. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  2615. return -EIO;
  2616. v4l_dbg(1, debug, client, "detecting adv7842 client on address 0x%x\n",
  2617. client->addr << 1);
  2618. if (!pdata) {
  2619. v4l_err(client, "No platform data!\n");
  2620. return -ENODEV;
  2621. }
  2622. state = devm_kzalloc(&client->dev, sizeof(struct adv7842_state), GFP_KERNEL);
  2623. if (!state) {
  2624. v4l_err(client, "Could not allocate adv7842_state memory!\n");
  2625. return -ENOMEM;
  2626. }
  2627. /* platform data */
  2628. state->pdata = *pdata;
  2629. state->timings = cea640x480;
  2630. sd = &state->sd;
  2631. v4l2_i2c_subdev_init(sd, client, &adv7842_ops);
  2632. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  2633. state->mode = pdata->mode;
  2634. state->hdmi_port_a = pdata->input == ADV7842_SELECT_HDMI_PORT_A;
  2635. state->restart_stdi_once = true;
  2636. /* i2c access to adv7842? */
  2637. rev = adv_smbus_read_byte_data_check(client, 0xea, false) << 8 |
  2638. adv_smbus_read_byte_data_check(client, 0xeb, false);
  2639. if (rev != 0x2012) {
  2640. v4l2_info(sd, "got rev=0x%04x on first read attempt\n", rev);
  2641. rev = adv_smbus_read_byte_data_check(client, 0xea, false) << 8 |
  2642. adv_smbus_read_byte_data_check(client, 0xeb, false);
  2643. }
  2644. if (rev != 0x2012) {
  2645. v4l2_info(sd, "not an adv7842 on address 0x%x (rev=0x%04x)\n",
  2646. client->addr << 1, rev);
  2647. return -ENODEV;
  2648. }
  2649. if (pdata->chip_reset)
  2650. main_reset(sd);
  2651. /* control handlers */
  2652. hdl = &state->hdl;
  2653. v4l2_ctrl_handler_init(hdl, 6);
  2654. /* add in ascending ID order */
  2655. v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
  2656. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  2657. v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
  2658. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  2659. v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
  2660. V4L2_CID_SATURATION, 0, 255, 1, 128);
  2661. v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
  2662. V4L2_CID_HUE, 0, 128, 1, 0);
  2663. /* custom controls */
  2664. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  2665. V4L2_CID_DV_RX_POWER_PRESENT, 0, 3, 0, 0);
  2666. state->analog_sampling_phase_ctrl = v4l2_ctrl_new_custom(hdl,
  2667. &adv7842_ctrl_analog_sampling_phase, NULL);
  2668. state->free_run_color_ctrl_manual = v4l2_ctrl_new_custom(hdl,
  2669. &adv7842_ctrl_free_run_color_manual, NULL);
  2670. state->free_run_color_ctrl = v4l2_ctrl_new_custom(hdl,
  2671. &adv7842_ctrl_free_run_color, NULL);
  2672. state->rgb_quantization_range_ctrl =
  2673. v4l2_ctrl_new_std_menu(hdl, &adv7842_ctrl_ops,
  2674. V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
  2675. 0, V4L2_DV_RGB_RANGE_AUTO);
  2676. sd->ctrl_handler = hdl;
  2677. if (hdl->error) {
  2678. err = hdl->error;
  2679. goto err_hdl;
  2680. }
  2681. state->detect_tx_5v_ctrl->is_private = true;
  2682. state->rgb_quantization_range_ctrl->is_private = true;
  2683. state->analog_sampling_phase_ctrl->is_private = true;
  2684. state->free_run_color_ctrl_manual->is_private = true;
  2685. state->free_run_color_ctrl->is_private = true;
  2686. if (adv7842_s_detect_tx_5v_ctrl(sd)) {
  2687. err = -ENODEV;
  2688. goto err_hdl;
  2689. }
  2690. if (adv7842_register_clients(sd) < 0) {
  2691. err = -ENOMEM;
  2692. v4l2_err(sd, "failed to create all i2c clients\n");
  2693. goto err_i2c;
  2694. }
  2695. /* work queues */
  2696. state->work_queues = create_singlethread_workqueue(client->name);
  2697. if (!state->work_queues) {
  2698. v4l2_err(sd, "Could not create work queue\n");
  2699. err = -ENOMEM;
  2700. goto err_i2c;
  2701. }
  2702. INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
  2703. adv7842_delayed_work_enable_hotplug);
  2704. state->pad.flags = MEDIA_PAD_FL_SOURCE;
  2705. err = media_entity_init(&sd->entity, 1, &state->pad, 0);
  2706. if (err)
  2707. goto err_work_queues;
  2708. err = adv7842_core_init(sd);
  2709. if (err)
  2710. goto err_entity;
  2711. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  2712. client->addr << 1, client->adapter->name);
  2713. return 0;
  2714. err_entity:
  2715. media_entity_cleanup(&sd->entity);
  2716. err_work_queues:
  2717. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  2718. destroy_workqueue(state->work_queues);
  2719. err_i2c:
  2720. adv7842_unregister_clients(sd);
  2721. err_hdl:
  2722. v4l2_ctrl_handler_free(hdl);
  2723. return err;
  2724. }
  2725. /* ----------------------------------------------------------------------- */
  2726. static int adv7842_remove(struct i2c_client *client)
  2727. {
  2728. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2729. struct adv7842_state *state = to_state(sd);
  2730. adv7842_irq_enable(sd, false);
  2731. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  2732. destroy_workqueue(state->work_queues);
  2733. v4l2_device_unregister_subdev(sd);
  2734. media_entity_cleanup(&sd->entity);
  2735. adv7842_unregister_clients(sd);
  2736. v4l2_ctrl_handler_free(sd->ctrl_handler);
  2737. return 0;
  2738. }
  2739. /* ----------------------------------------------------------------------- */
  2740. static struct i2c_device_id adv7842_id[] = {
  2741. { "adv7842", 0 },
  2742. { }
  2743. };
  2744. MODULE_DEVICE_TABLE(i2c, adv7842_id);
  2745. /* ----------------------------------------------------------------------- */
  2746. static struct i2c_driver adv7842_driver = {
  2747. .driver = {
  2748. .owner = THIS_MODULE,
  2749. .name = "adv7842",
  2750. },
  2751. .probe = adv7842_probe,
  2752. .remove = adv7842_remove,
  2753. .id_table = adv7842_id,
  2754. };
  2755. module_i2c_driver(adv7842_driver);