adv7604.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978
  1. /*
  2. * adv7604 - Analog Devices ADV7604 video decoder driver
  3. *
  4. * Copyright 2012 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  5. *
  6. * This program is free software; you may redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. *
  19. */
  20. /*
  21. * References (c = chapter, p = page):
  22. * REF_01 - Analog devices, ADV7604, Register Settings Recommendations,
  23. * Revision 2.5, June 2010
  24. * REF_02 - Analog devices, Register map documentation, Documentation of
  25. * the register maps, Software manual, Rev. F, June 2010
  26. * REF_03 - Analog devices, ADV7604, Hardware Manual, Rev. F, August 2010
  27. */
  28. #include <linux/delay.h>
  29. #include <linux/gpio/consumer.h>
  30. #include <linux/i2c.h>
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/slab.h>
  34. #include <linux/v4l2-dv-timings.h>
  35. #include <linux/videodev2.h>
  36. #include <linux/workqueue.h>
  37. #include <media/adv7604.h>
  38. #include <media/v4l2-ctrls.h>
  39. #include <media/v4l2-device.h>
  40. #include <media/v4l2-dv-timings.h>
  41. #include <media/v4l2-of.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 ADV7604 video decoder driver");
  46. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
  47. MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
  48. MODULE_LICENSE("GPL");
  49. /* ADV7604 system clock frequency */
  50. #define ADV7604_fsc (28636360)
  51. #define ADV7604_RGB_OUT (1 << 1)
  52. #define ADV7604_OP_FORMAT_SEL_8BIT (0 << 0)
  53. #define ADV7604_OP_FORMAT_SEL_10BIT (1 << 0)
  54. #define ADV7604_OP_FORMAT_SEL_12BIT (2 << 0)
  55. #define ADV7604_OP_MODE_SEL_SDR_422 (0 << 5)
  56. #define ADV7604_OP_MODE_SEL_DDR_422 (1 << 5)
  57. #define ADV7604_OP_MODE_SEL_SDR_444 (2 << 5)
  58. #define ADV7604_OP_MODE_SEL_DDR_444 (3 << 5)
  59. #define ADV7604_OP_MODE_SEL_SDR_422_2X (4 << 5)
  60. #define ADV7604_OP_MODE_SEL_ADI_CM (5 << 5)
  61. #define ADV7604_OP_CH_SEL_GBR (0 << 5)
  62. #define ADV7604_OP_CH_SEL_GRB (1 << 5)
  63. #define ADV7604_OP_CH_SEL_BGR (2 << 5)
  64. #define ADV7604_OP_CH_SEL_RGB (3 << 5)
  65. #define ADV7604_OP_CH_SEL_BRG (4 << 5)
  66. #define ADV7604_OP_CH_SEL_RBG (5 << 5)
  67. #define ADV7604_OP_SWAP_CB_CR (1 << 0)
  68. enum adv7604_type {
  69. ADV7604,
  70. ADV7611,
  71. };
  72. struct adv7604_reg_seq {
  73. unsigned int reg;
  74. u8 val;
  75. };
  76. struct adv7604_format_info {
  77. u32 code;
  78. u8 op_ch_sel;
  79. bool rgb_out;
  80. bool swap_cb_cr;
  81. u8 op_format_sel;
  82. };
  83. struct adv7604_chip_info {
  84. enum adv7604_type type;
  85. bool has_afe;
  86. unsigned int max_port;
  87. unsigned int num_dv_ports;
  88. unsigned int edid_enable_reg;
  89. unsigned int edid_status_reg;
  90. unsigned int lcf_reg;
  91. unsigned int cable_det_mask;
  92. unsigned int tdms_lock_mask;
  93. unsigned int fmt_change_digital_mask;
  94. const struct adv7604_format_info *formats;
  95. unsigned int nformats;
  96. void (*set_termination)(struct v4l2_subdev *sd, bool enable);
  97. void (*setup_irqs)(struct v4l2_subdev *sd);
  98. unsigned int (*read_hdmi_pixelclock)(struct v4l2_subdev *sd);
  99. unsigned int (*read_cable_det)(struct v4l2_subdev *sd);
  100. /* 0 = AFE, 1 = HDMI */
  101. const struct adv7604_reg_seq *recommended_settings[2];
  102. unsigned int num_recommended_settings[2];
  103. unsigned long page_mask;
  104. };
  105. /*
  106. **********************************************************************
  107. *
  108. * Arrays with configuration parameters for the ADV7604
  109. *
  110. **********************************************************************
  111. */
  112. struct adv7604_state {
  113. const struct adv7604_chip_info *info;
  114. struct adv7604_platform_data pdata;
  115. struct gpio_desc *hpd_gpio[4];
  116. struct v4l2_subdev sd;
  117. struct media_pad pads[ADV7604_PAD_MAX];
  118. unsigned int source_pad;
  119. struct v4l2_ctrl_handler hdl;
  120. enum adv7604_pad selected_input;
  121. struct v4l2_dv_timings timings;
  122. const struct adv7604_format_info *format;
  123. struct {
  124. u8 edid[256];
  125. u32 present;
  126. unsigned blocks;
  127. } edid;
  128. u16 spa_port_a[2];
  129. struct v4l2_fract aspect_ratio;
  130. u32 rgb_quantization_range;
  131. struct workqueue_struct *work_queues;
  132. struct delayed_work delayed_work_enable_hotplug;
  133. bool restart_stdi_once;
  134. /* i2c clients */
  135. struct i2c_client *i2c_clients[ADV7604_PAGE_MAX];
  136. /* controls */
  137. struct v4l2_ctrl *detect_tx_5v_ctrl;
  138. struct v4l2_ctrl *analog_sampling_phase_ctrl;
  139. struct v4l2_ctrl *free_run_color_manual_ctrl;
  140. struct v4l2_ctrl *free_run_color_ctrl;
  141. struct v4l2_ctrl *rgb_quantization_range_ctrl;
  142. };
  143. static bool adv7604_has_afe(struct adv7604_state *state)
  144. {
  145. return state->info->has_afe;
  146. }
  147. /* Supported CEA and DMT timings */
  148. static const struct v4l2_dv_timings adv7604_timings[] = {
  149. V4L2_DV_BT_CEA_720X480P59_94,
  150. V4L2_DV_BT_CEA_720X576P50,
  151. V4L2_DV_BT_CEA_1280X720P24,
  152. V4L2_DV_BT_CEA_1280X720P25,
  153. V4L2_DV_BT_CEA_1280X720P50,
  154. V4L2_DV_BT_CEA_1280X720P60,
  155. V4L2_DV_BT_CEA_1920X1080P24,
  156. V4L2_DV_BT_CEA_1920X1080P25,
  157. V4L2_DV_BT_CEA_1920X1080P30,
  158. V4L2_DV_BT_CEA_1920X1080P50,
  159. V4L2_DV_BT_CEA_1920X1080P60,
  160. /* sorted by DMT ID */
  161. V4L2_DV_BT_DMT_640X350P85,
  162. V4L2_DV_BT_DMT_640X400P85,
  163. V4L2_DV_BT_DMT_720X400P85,
  164. V4L2_DV_BT_DMT_640X480P60,
  165. V4L2_DV_BT_DMT_640X480P72,
  166. V4L2_DV_BT_DMT_640X480P75,
  167. V4L2_DV_BT_DMT_640X480P85,
  168. V4L2_DV_BT_DMT_800X600P56,
  169. V4L2_DV_BT_DMT_800X600P60,
  170. V4L2_DV_BT_DMT_800X600P72,
  171. V4L2_DV_BT_DMT_800X600P75,
  172. V4L2_DV_BT_DMT_800X600P85,
  173. V4L2_DV_BT_DMT_848X480P60,
  174. V4L2_DV_BT_DMT_1024X768P60,
  175. V4L2_DV_BT_DMT_1024X768P70,
  176. V4L2_DV_BT_DMT_1024X768P75,
  177. V4L2_DV_BT_DMT_1024X768P85,
  178. V4L2_DV_BT_DMT_1152X864P75,
  179. V4L2_DV_BT_DMT_1280X768P60_RB,
  180. V4L2_DV_BT_DMT_1280X768P60,
  181. V4L2_DV_BT_DMT_1280X768P75,
  182. V4L2_DV_BT_DMT_1280X768P85,
  183. V4L2_DV_BT_DMT_1280X800P60_RB,
  184. V4L2_DV_BT_DMT_1280X800P60,
  185. V4L2_DV_BT_DMT_1280X800P75,
  186. V4L2_DV_BT_DMT_1280X800P85,
  187. V4L2_DV_BT_DMT_1280X960P60,
  188. V4L2_DV_BT_DMT_1280X960P85,
  189. V4L2_DV_BT_DMT_1280X1024P60,
  190. V4L2_DV_BT_DMT_1280X1024P75,
  191. V4L2_DV_BT_DMT_1280X1024P85,
  192. V4L2_DV_BT_DMT_1360X768P60,
  193. V4L2_DV_BT_DMT_1400X1050P60_RB,
  194. V4L2_DV_BT_DMT_1400X1050P60,
  195. V4L2_DV_BT_DMT_1400X1050P75,
  196. V4L2_DV_BT_DMT_1400X1050P85,
  197. V4L2_DV_BT_DMT_1440X900P60_RB,
  198. V4L2_DV_BT_DMT_1440X900P60,
  199. V4L2_DV_BT_DMT_1600X1200P60,
  200. V4L2_DV_BT_DMT_1680X1050P60_RB,
  201. V4L2_DV_BT_DMT_1680X1050P60,
  202. V4L2_DV_BT_DMT_1792X1344P60,
  203. V4L2_DV_BT_DMT_1856X1392P60,
  204. V4L2_DV_BT_DMT_1920X1200P60_RB,
  205. V4L2_DV_BT_DMT_1366X768P60_RB,
  206. V4L2_DV_BT_DMT_1366X768P60,
  207. V4L2_DV_BT_DMT_1920X1080P60,
  208. { },
  209. };
  210. struct adv7604_video_standards {
  211. struct v4l2_dv_timings timings;
  212. u8 vid_std;
  213. u8 v_freq;
  214. };
  215. /* sorted by number of lines */
  216. static const struct adv7604_video_standards adv7604_prim_mode_comp[] = {
  217. /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
  218. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  219. { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
  220. { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
  221. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  222. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  223. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  224. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  225. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  226. /* TODO add 1920x1080P60_RB (CVT timing) */
  227. { },
  228. };
  229. /* sorted by number of lines */
  230. static const struct adv7604_video_standards adv7604_prim_mode_gr[] = {
  231. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  232. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  233. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  234. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  235. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  236. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  237. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  238. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  239. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  240. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  241. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  242. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  243. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  244. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  245. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  246. { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
  247. { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
  248. { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
  249. { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
  250. { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
  251. /* TODO add 1600X1200P60_RB (not a DMT timing) */
  252. { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
  253. { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
  254. { },
  255. };
  256. /* sorted by number of lines */
  257. static const struct adv7604_video_standards adv7604_prim_mode_hdmi_comp[] = {
  258. { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
  259. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  260. { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
  261. { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
  262. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  263. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  264. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  265. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  266. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  267. { },
  268. };
  269. /* sorted by number of lines */
  270. static const struct adv7604_video_standards adv7604_prim_mode_hdmi_gr[] = {
  271. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  272. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  273. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  274. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  275. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  276. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  277. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  278. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  279. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  280. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  281. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  282. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  283. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  284. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  285. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  286. { },
  287. };
  288. /* ----------------------------------------------------------------------- */
  289. static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
  290. {
  291. return container_of(sd, struct adv7604_state, sd);
  292. }
  293. static inline unsigned hblanking(const struct v4l2_bt_timings *t)
  294. {
  295. return V4L2_DV_BT_BLANKING_WIDTH(t);
  296. }
  297. static inline unsigned htotal(const struct v4l2_bt_timings *t)
  298. {
  299. return V4L2_DV_BT_FRAME_WIDTH(t);
  300. }
  301. static inline unsigned vblanking(const struct v4l2_bt_timings *t)
  302. {
  303. return V4L2_DV_BT_BLANKING_HEIGHT(t);
  304. }
  305. static inline unsigned vtotal(const struct v4l2_bt_timings *t)
  306. {
  307. return V4L2_DV_BT_FRAME_HEIGHT(t);
  308. }
  309. /* ----------------------------------------------------------------------- */
  310. static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
  311. u8 command, bool check)
  312. {
  313. union i2c_smbus_data data;
  314. if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  315. I2C_SMBUS_READ, command,
  316. I2C_SMBUS_BYTE_DATA, &data))
  317. return data.byte;
  318. if (check)
  319. v4l_err(client, "error reading %02x, %02x\n",
  320. client->addr, command);
  321. return -EIO;
  322. }
  323. static s32 adv_smbus_read_byte_data(struct adv7604_state *state,
  324. enum adv7604_page page, u8 command)
  325. {
  326. return adv_smbus_read_byte_data_check(state->i2c_clients[page],
  327. command, true);
  328. }
  329. static s32 adv_smbus_write_byte_data(struct adv7604_state *state,
  330. enum adv7604_page page, u8 command,
  331. u8 value)
  332. {
  333. struct i2c_client *client = state->i2c_clients[page];
  334. union i2c_smbus_data data;
  335. int err;
  336. int i;
  337. data.byte = value;
  338. for (i = 0; i < 3; i++) {
  339. err = i2c_smbus_xfer(client->adapter, client->addr,
  340. client->flags,
  341. I2C_SMBUS_WRITE, command,
  342. I2C_SMBUS_BYTE_DATA, &data);
  343. if (!err)
  344. break;
  345. }
  346. if (err < 0)
  347. v4l_err(client, "error writing %02x, %02x, %02x\n",
  348. client->addr, command, value);
  349. return err;
  350. }
  351. static s32 adv_smbus_write_i2c_block_data(struct adv7604_state *state,
  352. enum adv7604_page page, u8 command,
  353. unsigned length, const u8 *values)
  354. {
  355. struct i2c_client *client = state->i2c_clients[page];
  356. union i2c_smbus_data data;
  357. if (length > I2C_SMBUS_BLOCK_MAX)
  358. length = I2C_SMBUS_BLOCK_MAX;
  359. data.block[0] = length;
  360. memcpy(data.block + 1, values, length);
  361. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  362. I2C_SMBUS_WRITE, command,
  363. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  364. }
  365. /* ----------------------------------------------------------------------- */
  366. static inline int io_read(struct v4l2_subdev *sd, u8 reg)
  367. {
  368. struct adv7604_state *state = to_state(sd);
  369. return adv_smbus_read_byte_data(state, ADV7604_PAGE_IO, reg);
  370. }
  371. static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  372. {
  373. struct adv7604_state *state = to_state(sd);
  374. return adv_smbus_write_byte_data(state, ADV7604_PAGE_IO, reg, val);
  375. }
  376. static inline int io_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  377. {
  378. return io_write(sd, reg, (io_read(sd, reg) & ~mask) | val);
  379. }
  380. static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
  381. {
  382. struct adv7604_state *state = to_state(sd);
  383. return adv_smbus_read_byte_data(state, ADV7604_PAGE_AVLINK, reg);
  384. }
  385. static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  386. {
  387. struct adv7604_state *state = to_state(sd);
  388. return adv_smbus_write_byte_data(state, ADV7604_PAGE_AVLINK, reg, val);
  389. }
  390. static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
  391. {
  392. struct adv7604_state *state = to_state(sd);
  393. return adv_smbus_read_byte_data(state, ADV7604_PAGE_CEC, reg);
  394. }
  395. static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  396. {
  397. struct adv7604_state *state = to_state(sd);
  398. return adv_smbus_write_byte_data(state, ADV7604_PAGE_CEC, reg, val);
  399. }
  400. static inline int cec_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  401. {
  402. return cec_write(sd, reg, (cec_read(sd, reg) & ~mask) | val);
  403. }
  404. static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
  405. {
  406. struct adv7604_state *state = to_state(sd);
  407. return adv_smbus_read_byte_data(state, ADV7604_PAGE_INFOFRAME, reg);
  408. }
  409. static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  410. {
  411. struct adv7604_state *state = to_state(sd);
  412. return adv_smbus_write_byte_data(state, ADV7604_PAGE_INFOFRAME,
  413. reg, val);
  414. }
  415. static inline int esdp_read(struct v4l2_subdev *sd, u8 reg)
  416. {
  417. struct adv7604_state *state = to_state(sd);
  418. return adv_smbus_read_byte_data(state, ADV7604_PAGE_ESDP, reg);
  419. }
  420. static inline int esdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  421. {
  422. struct adv7604_state *state = to_state(sd);
  423. return adv_smbus_write_byte_data(state, ADV7604_PAGE_ESDP, reg, val);
  424. }
  425. static inline int dpp_read(struct v4l2_subdev *sd, u8 reg)
  426. {
  427. struct adv7604_state *state = to_state(sd);
  428. return adv_smbus_read_byte_data(state, ADV7604_PAGE_DPP, reg);
  429. }
  430. static inline int dpp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  431. {
  432. struct adv7604_state *state = to_state(sd);
  433. return adv_smbus_write_byte_data(state, ADV7604_PAGE_DPP, reg, val);
  434. }
  435. static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
  436. {
  437. struct adv7604_state *state = to_state(sd);
  438. return adv_smbus_read_byte_data(state, ADV7604_PAGE_AFE, reg);
  439. }
  440. static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  441. {
  442. struct adv7604_state *state = to_state(sd);
  443. return adv_smbus_write_byte_data(state, ADV7604_PAGE_AFE, reg, val);
  444. }
  445. static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
  446. {
  447. struct adv7604_state *state = to_state(sd);
  448. return adv_smbus_read_byte_data(state, ADV7604_PAGE_REP, reg);
  449. }
  450. static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  451. {
  452. struct adv7604_state *state = to_state(sd);
  453. return adv_smbus_write_byte_data(state, ADV7604_PAGE_REP, reg, val);
  454. }
  455. static inline int rep_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  456. {
  457. return rep_write(sd, reg, (rep_read(sd, reg) & ~mask) | val);
  458. }
  459. static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
  460. {
  461. struct adv7604_state *state = to_state(sd);
  462. return adv_smbus_read_byte_data(state, ADV7604_PAGE_EDID, reg);
  463. }
  464. static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  465. {
  466. struct adv7604_state *state = to_state(sd);
  467. return adv_smbus_write_byte_data(state, ADV7604_PAGE_EDID, reg, val);
  468. }
  469. static inline int edid_read_block(struct v4l2_subdev *sd, unsigned len, u8 *val)
  470. {
  471. struct adv7604_state *state = to_state(sd);
  472. struct i2c_client *client = state->i2c_clients[ADV7604_PAGE_EDID];
  473. u8 msgbuf0[1] = { 0 };
  474. u8 msgbuf1[256];
  475. struct i2c_msg msg[2] = {
  476. {
  477. .addr = client->addr,
  478. .len = 1,
  479. .buf = msgbuf0
  480. },
  481. {
  482. .addr = client->addr,
  483. .flags = I2C_M_RD,
  484. .len = len,
  485. .buf = msgbuf1
  486. },
  487. };
  488. if (i2c_transfer(client->adapter, msg, 2) < 0)
  489. return -EIO;
  490. memcpy(val, msgbuf1, len);
  491. return 0;
  492. }
  493. static inline int edid_write_block(struct v4l2_subdev *sd,
  494. unsigned len, const u8 *val)
  495. {
  496. struct adv7604_state *state = to_state(sd);
  497. int err = 0;
  498. int i;
  499. v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n", __func__, len);
  500. for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
  501. err = adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_EDID,
  502. i, I2C_SMBUS_BLOCK_MAX, val + i);
  503. return err;
  504. }
  505. static void adv7604_set_hpd(struct adv7604_state *state, unsigned int hpd)
  506. {
  507. unsigned int i;
  508. for (i = 0; i < state->info->num_dv_ports; ++i) {
  509. if (IS_ERR(state->hpd_gpio[i]))
  510. continue;
  511. gpiod_set_value_cansleep(state->hpd_gpio[i], hpd & BIT(i));
  512. }
  513. v4l2_subdev_notify(&state->sd, ADV7604_HOTPLUG, &hpd);
  514. }
  515. static void adv7604_delayed_work_enable_hotplug(struct work_struct *work)
  516. {
  517. struct delayed_work *dwork = to_delayed_work(work);
  518. struct adv7604_state *state = container_of(dwork, struct adv7604_state,
  519. delayed_work_enable_hotplug);
  520. struct v4l2_subdev *sd = &state->sd;
  521. v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
  522. adv7604_set_hpd(state, state->edid.present);
  523. }
  524. static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
  525. {
  526. struct adv7604_state *state = to_state(sd);
  527. return adv_smbus_read_byte_data(state, ADV7604_PAGE_HDMI, reg);
  528. }
  529. static u16 hdmi_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
  530. {
  531. return ((hdmi_read(sd, reg) << 8) | hdmi_read(sd, reg + 1)) & mask;
  532. }
  533. static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  534. {
  535. struct adv7604_state *state = to_state(sd);
  536. return adv_smbus_write_byte_data(state, ADV7604_PAGE_HDMI, reg, val);
  537. }
  538. static inline int hdmi_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  539. {
  540. return hdmi_write(sd, reg, (hdmi_read(sd, reg) & ~mask) | val);
  541. }
  542. static inline int test_read(struct v4l2_subdev *sd, u8 reg)
  543. {
  544. struct adv7604_state *state = to_state(sd);
  545. return adv_smbus_read_byte_data(state, ADV7604_PAGE_TEST, reg);
  546. }
  547. static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  548. {
  549. struct adv7604_state *state = to_state(sd);
  550. return adv_smbus_write_byte_data(state, ADV7604_PAGE_TEST, reg, val);
  551. }
  552. static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
  553. {
  554. struct adv7604_state *state = to_state(sd);
  555. return adv_smbus_read_byte_data(state, ADV7604_PAGE_CP, reg);
  556. }
  557. static u16 cp_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
  558. {
  559. return ((cp_read(sd, reg) << 8) | cp_read(sd, reg + 1)) & mask;
  560. }
  561. static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  562. {
  563. struct adv7604_state *state = to_state(sd);
  564. return adv_smbus_write_byte_data(state, ADV7604_PAGE_CP, reg, val);
  565. }
  566. static inline int cp_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  567. {
  568. return cp_write(sd, reg, (cp_read(sd, reg) & ~mask) | val);
  569. }
  570. static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
  571. {
  572. struct adv7604_state *state = to_state(sd);
  573. return adv_smbus_read_byte_data(state, ADV7604_PAGE_VDP, reg);
  574. }
  575. static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  576. {
  577. struct adv7604_state *state = to_state(sd);
  578. return adv_smbus_write_byte_data(state, ADV7604_PAGE_VDP, reg, val);
  579. }
  580. #define ADV7604_REG(page, offset) (((page) << 8) | (offset))
  581. #define ADV7604_REG_SEQ_TERM 0xffff
  582. #ifdef CONFIG_VIDEO_ADV_DEBUG
  583. static int adv7604_read_reg(struct v4l2_subdev *sd, unsigned int reg)
  584. {
  585. struct adv7604_state *state = to_state(sd);
  586. unsigned int page = reg >> 8;
  587. if (!(BIT(page) & state->info->page_mask))
  588. return -EINVAL;
  589. reg &= 0xff;
  590. return adv_smbus_read_byte_data(state, page, reg);
  591. }
  592. #endif
  593. static int adv7604_write_reg(struct v4l2_subdev *sd, unsigned int reg, u8 val)
  594. {
  595. struct adv7604_state *state = to_state(sd);
  596. unsigned int page = reg >> 8;
  597. if (!(BIT(page) & state->info->page_mask))
  598. return -EINVAL;
  599. reg &= 0xff;
  600. return adv_smbus_write_byte_data(state, page, reg, val);
  601. }
  602. static void adv7604_write_reg_seq(struct v4l2_subdev *sd,
  603. const struct adv7604_reg_seq *reg_seq)
  604. {
  605. unsigned int i;
  606. for (i = 0; reg_seq[i].reg != ADV7604_REG_SEQ_TERM; i++)
  607. adv7604_write_reg(sd, reg_seq[i].reg, reg_seq[i].val);
  608. }
  609. /* -----------------------------------------------------------------------------
  610. * Format helpers
  611. */
  612. static const struct adv7604_format_info adv7604_formats[] = {
  613. { MEDIA_BUS_FMT_RGB888_1X24, ADV7604_OP_CH_SEL_RGB, true, false,
  614. ADV7604_OP_MODE_SEL_SDR_444 | ADV7604_OP_FORMAT_SEL_8BIT },
  615. { MEDIA_BUS_FMT_YUYV8_2X8, ADV7604_OP_CH_SEL_RGB, false, false,
  616. ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
  617. { MEDIA_BUS_FMT_YVYU8_2X8, ADV7604_OP_CH_SEL_RGB, false, true,
  618. ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
  619. { MEDIA_BUS_FMT_YUYV10_2X10, ADV7604_OP_CH_SEL_RGB, false, false,
  620. ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
  621. { MEDIA_BUS_FMT_YVYU10_2X10, ADV7604_OP_CH_SEL_RGB, false, true,
  622. ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
  623. { MEDIA_BUS_FMT_YUYV12_2X12, ADV7604_OP_CH_SEL_RGB, false, false,
  624. ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
  625. { MEDIA_BUS_FMT_YVYU12_2X12, ADV7604_OP_CH_SEL_RGB, false, true,
  626. ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
  627. { MEDIA_BUS_FMT_UYVY8_1X16, ADV7604_OP_CH_SEL_RBG, false, false,
  628. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
  629. { MEDIA_BUS_FMT_VYUY8_1X16, ADV7604_OP_CH_SEL_RBG, false, true,
  630. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
  631. { MEDIA_BUS_FMT_YUYV8_1X16, ADV7604_OP_CH_SEL_RGB, false, false,
  632. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
  633. { MEDIA_BUS_FMT_YVYU8_1X16, ADV7604_OP_CH_SEL_RGB, false, true,
  634. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
  635. { MEDIA_BUS_FMT_UYVY10_1X20, ADV7604_OP_CH_SEL_RBG, false, false,
  636. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
  637. { MEDIA_BUS_FMT_VYUY10_1X20, ADV7604_OP_CH_SEL_RBG, false, true,
  638. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
  639. { MEDIA_BUS_FMT_YUYV10_1X20, ADV7604_OP_CH_SEL_RGB, false, false,
  640. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
  641. { MEDIA_BUS_FMT_YVYU10_1X20, ADV7604_OP_CH_SEL_RGB, false, true,
  642. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
  643. { MEDIA_BUS_FMT_UYVY12_1X24, ADV7604_OP_CH_SEL_RBG, false, false,
  644. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
  645. { MEDIA_BUS_FMT_VYUY12_1X24, ADV7604_OP_CH_SEL_RBG, false, true,
  646. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
  647. { MEDIA_BUS_FMT_YUYV12_1X24, ADV7604_OP_CH_SEL_RGB, false, false,
  648. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
  649. { MEDIA_BUS_FMT_YVYU12_1X24, ADV7604_OP_CH_SEL_RGB, false, true,
  650. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
  651. };
  652. static const struct adv7604_format_info adv7611_formats[] = {
  653. { MEDIA_BUS_FMT_RGB888_1X24, ADV7604_OP_CH_SEL_RGB, true, false,
  654. ADV7604_OP_MODE_SEL_SDR_444 | ADV7604_OP_FORMAT_SEL_8BIT },
  655. { MEDIA_BUS_FMT_YUYV8_2X8, ADV7604_OP_CH_SEL_RGB, false, false,
  656. ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
  657. { MEDIA_BUS_FMT_YVYU8_2X8, ADV7604_OP_CH_SEL_RGB, false, true,
  658. ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
  659. { MEDIA_BUS_FMT_YUYV12_2X12, ADV7604_OP_CH_SEL_RGB, false, false,
  660. ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
  661. { MEDIA_BUS_FMT_YVYU12_2X12, ADV7604_OP_CH_SEL_RGB, false, true,
  662. ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
  663. { MEDIA_BUS_FMT_UYVY8_1X16, ADV7604_OP_CH_SEL_RBG, false, false,
  664. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
  665. { MEDIA_BUS_FMT_VYUY8_1X16, ADV7604_OP_CH_SEL_RBG, false, true,
  666. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
  667. { MEDIA_BUS_FMT_YUYV8_1X16, ADV7604_OP_CH_SEL_RGB, false, false,
  668. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
  669. { MEDIA_BUS_FMT_YVYU8_1X16, ADV7604_OP_CH_SEL_RGB, false, true,
  670. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
  671. { MEDIA_BUS_FMT_UYVY12_1X24, ADV7604_OP_CH_SEL_RBG, false, false,
  672. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
  673. { MEDIA_BUS_FMT_VYUY12_1X24, ADV7604_OP_CH_SEL_RBG, false, true,
  674. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
  675. { MEDIA_BUS_FMT_YUYV12_1X24, ADV7604_OP_CH_SEL_RGB, false, false,
  676. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
  677. { MEDIA_BUS_FMT_YVYU12_1X24, ADV7604_OP_CH_SEL_RGB, false, true,
  678. ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
  679. };
  680. static const struct adv7604_format_info *
  681. adv7604_format_info(struct adv7604_state *state, u32 code)
  682. {
  683. unsigned int i;
  684. for (i = 0; i < state->info->nformats; ++i) {
  685. if (state->info->formats[i].code == code)
  686. return &state->info->formats[i];
  687. }
  688. return NULL;
  689. }
  690. /* ----------------------------------------------------------------------- */
  691. static inline bool is_analog_input(struct v4l2_subdev *sd)
  692. {
  693. struct adv7604_state *state = to_state(sd);
  694. return state->selected_input == ADV7604_PAD_VGA_RGB ||
  695. state->selected_input == ADV7604_PAD_VGA_COMP;
  696. }
  697. static inline bool is_digital_input(struct v4l2_subdev *sd)
  698. {
  699. struct adv7604_state *state = to_state(sd);
  700. return state->selected_input == ADV7604_PAD_HDMI_PORT_A ||
  701. state->selected_input == ADV7604_PAD_HDMI_PORT_B ||
  702. state->selected_input == ADV7604_PAD_HDMI_PORT_C ||
  703. state->selected_input == ADV7604_PAD_HDMI_PORT_D;
  704. }
  705. /* ----------------------------------------------------------------------- */
  706. #ifdef CONFIG_VIDEO_ADV_DEBUG
  707. static void adv7604_inv_register(struct v4l2_subdev *sd)
  708. {
  709. v4l2_info(sd, "0x000-0x0ff: IO Map\n");
  710. v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
  711. v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
  712. v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
  713. v4l2_info(sd, "0x400-0x4ff: ESDP Map\n");
  714. v4l2_info(sd, "0x500-0x5ff: DPP Map\n");
  715. v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
  716. v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
  717. v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
  718. v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
  719. v4l2_info(sd, "0xa00-0xaff: Test Map\n");
  720. v4l2_info(sd, "0xb00-0xbff: CP Map\n");
  721. v4l2_info(sd, "0xc00-0xcff: VDP Map\n");
  722. }
  723. static int adv7604_g_register(struct v4l2_subdev *sd,
  724. struct v4l2_dbg_register *reg)
  725. {
  726. int ret;
  727. ret = adv7604_read_reg(sd, reg->reg);
  728. if (ret < 0) {
  729. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  730. adv7604_inv_register(sd);
  731. return ret;
  732. }
  733. reg->size = 1;
  734. reg->val = ret;
  735. return 0;
  736. }
  737. static int adv7604_s_register(struct v4l2_subdev *sd,
  738. const struct v4l2_dbg_register *reg)
  739. {
  740. int ret;
  741. ret = adv7604_write_reg(sd, reg->reg, reg->val);
  742. if (ret < 0) {
  743. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  744. adv7604_inv_register(sd);
  745. return ret;
  746. }
  747. return 0;
  748. }
  749. #endif
  750. static unsigned int adv7604_read_cable_det(struct v4l2_subdev *sd)
  751. {
  752. u8 value = io_read(sd, 0x6f);
  753. return ((value & 0x10) >> 4)
  754. | ((value & 0x08) >> 2)
  755. | ((value & 0x04) << 0)
  756. | ((value & 0x02) << 2);
  757. }
  758. static unsigned int adv7611_read_cable_det(struct v4l2_subdev *sd)
  759. {
  760. u8 value = io_read(sd, 0x6f);
  761. return value & 1;
  762. }
  763. static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
  764. {
  765. struct adv7604_state *state = to_state(sd);
  766. const struct adv7604_chip_info *info = state->info;
  767. return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
  768. info->read_cable_det(sd));
  769. }
  770. static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
  771. u8 prim_mode,
  772. const struct adv7604_video_standards *predef_vid_timings,
  773. const struct v4l2_dv_timings *timings)
  774. {
  775. int i;
  776. for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
  777. if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
  778. is_digital_input(sd) ? 250000 : 1000000))
  779. continue;
  780. io_write(sd, 0x00, predef_vid_timings[i].vid_std); /* video std */
  781. io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) +
  782. prim_mode); /* v_freq and prim mode */
  783. return 0;
  784. }
  785. return -1;
  786. }
  787. static int configure_predefined_video_timings(struct v4l2_subdev *sd,
  788. struct v4l2_dv_timings *timings)
  789. {
  790. struct adv7604_state *state = to_state(sd);
  791. int err;
  792. v4l2_dbg(1, debug, sd, "%s", __func__);
  793. if (adv7604_has_afe(state)) {
  794. /* reset to default values */
  795. io_write(sd, 0x16, 0x43);
  796. io_write(sd, 0x17, 0x5a);
  797. }
  798. /* disable embedded syncs for auto graphics mode */
  799. cp_write_clr_set(sd, 0x81, 0x10, 0x00);
  800. cp_write(sd, 0x8f, 0x00);
  801. cp_write(sd, 0x90, 0x00);
  802. cp_write(sd, 0xa2, 0x00);
  803. cp_write(sd, 0xa3, 0x00);
  804. cp_write(sd, 0xa4, 0x00);
  805. cp_write(sd, 0xa5, 0x00);
  806. cp_write(sd, 0xa6, 0x00);
  807. cp_write(sd, 0xa7, 0x00);
  808. cp_write(sd, 0xab, 0x00);
  809. cp_write(sd, 0xac, 0x00);
  810. if (is_analog_input(sd)) {
  811. err = find_and_set_predefined_video_timings(sd,
  812. 0x01, adv7604_prim_mode_comp, timings);
  813. if (err)
  814. err = find_and_set_predefined_video_timings(sd,
  815. 0x02, adv7604_prim_mode_gr, timings);
  816. } else if (is_digital_input(sd)) {
  817. err = find_and_set_predefined_video_timings(sd,
  818. 0x05, adv7604_prim_mode_hdmi_comp, timings);
  819. if (err)
  820. err = find_and_set_predefined_video_timings(sd,
  821. 0x06, adv7604_prim_mode_hdmi_gr, timings);
  822. } else {
  823. v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
  824. __func__, state->selected_input);
  825. err = -1;
  826. }
  827. return err;
  828. }
  829. static void configure_custom_video_timings(struct v4l2_subdev *sd,
  830. const struct v4l2_bt_timings *bt)
  831. {
  832. struct adv7604_state *state = to_state(sd);
  833. u32 width = htotal(bt);
  834. u32 height = vtotal(bt);
  835. u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
  836. u16 cp_start_eav = width - bt->hfrontporch;
  837. u16 cp_start_vbi = height - bt->vfrontporch;
  838. u16 cp_end_vbi = bt->vsync + bt->vbackporch;
  839. u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
  840. ((width * (ADV7604_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
  841. const u8 pll[2] = {
  842. 0xc0 | ((width >> 8) & 0x1f),
  843. width & 0xff
  844. };
  845. v4l2_dbg(2, debug, sd, "%s\n", __func__);
  846. if (is_analog_input(sd)) {
  847. /* auto graphics */
  848. io_write(sd, 0x00, 0x07); /* video std */
  849. io_write(sd, 0x01, 0x02); /* prim mode */
  850. /* enable embedded syncs for auto graphics mode */
  851. cp_write_clr_set(sd, 0x81, 0x10, 0x10);
  852. /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
  853. /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
  854. /* IO-map reg. 0x16 and 0x17 should be written in sequence */
  855. if (adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_IO,
  856. 0x16, 2, pll))
  857. v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
  858. /* active video - horizontal timing */
  859. cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
  860. cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) |
  861. ((cp_start_eav >> 8) & 0x0f));
  862. cp_write(sd, 0xa4, cp_start_eav & 0xff);
  863. /* active video - vertical timing */
  864. cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
  865. cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
  866. ((cp_end_vbi >> 8) & 0xf));
  867. cp_write(sd, 0xa7, cp_end_vbi & 0xff);
  868. } else if (is_digital_input(sd)) {
  869. /* set default prim_mode/vid_std for HDMI
  870. according to [REF_03, c. 4.2] */
  871. io_write(sd, 0x00, 0x02); /* video std */
  872. io_write(sd, 0x01, 0x06); /* prim mode */
  873. } else {
  874. v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
  875. __func__, state->selected_input);
  876. }
  877. cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
  878. cp_write(sd, 0x90, ch1_fr_ll & 0xff);
  879. cp_write(sd, 0xab, (height >> 4) & 0xff);
  880. cp_write(sd, 0xac, (height & 0x0f) << 4);
  881. }
  882. static void adv7604_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
  883. {
  884. struct adv7604_state *state = to_state(sd);
  885. u8 offset_buf[4];
  886. if (auto_offset) {
  887. offset_a = 0x3ff;
  888. offset_b = 0x3ff;
  889. offset_c = 0x3ff;
  890. }
  891. v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
  892. __func__, auto_offset ? "Auto" : "Manual",
  893. offset_a, offset_b, offset_c);
  894. offset_buf[0] = (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
  895. offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
  896. offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
  897. offset_buf[3] = offset_c & 0x0ff;
  898. /* Registers must be written in this order with no i2c access in between */
  899. if (adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_CP,
  900. 0x77, 4, offset_buf))
  901. v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
  902. }
  903. static void adv7604_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
  904. {
  905. struct adv7604_state *state = to_state(sd);
  906. u8 gain_buf[4];
  907. u8 gain_man = 1;
  908. u8 agc_mode_man = 1;
  909. if (auto_gain) {
  910. gain_man = 0;
  911. agc_mode_man = 0;
  912. gain_a = 0x100;
  913. gain_b = 0x100;
  914. gain_c = 0x100;
  915. }
  916. v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
  917. __func__, auto_gain ? "Auto" : "Manual",
  918. gain_a, gain_b, gain_c);
  919. gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
  920. gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
  921. gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
  922. gain_buf[3] = ((gain_c & 0x0ff));
  923. /* Registers must be written in this order with no i2c access in between */
  924. if (adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_CP,
  925. 0x73, 4, gain_buf))
  926. v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
  927. }
  928. static void set_rgb_quantization_range(struct v4l2_subdev *sd)
  929. {
  930. struct adv7604_state *state = to_state(sd);
  931. bool rgb_output = io_read(sd, 0x02) & 0x02;
  932. bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
  933. v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
  934. __func__, state->rgb_quantization_range,
  935. rgb_output, hdmi_signal);
  936. adv7604_set_gain(sd, true, 0x0, 0x0, 0x0);
  937. adv7604_set_offset(sd, true, 0x0, 0x0, 0x0);
  938. switch (state->rgb_quantization_range) {
  939. case V4L2_DV_RGB_RANGE_AUTO:
  940. if (state->selected_input == ADV7604_PAD_VGA_RGB) {
  941. /* Receiving analog RGB signal
  942. * Set RGB full range (0-255) */
  943. io_write_clr_set(sd, 0x02, 0xf0, 0x10);
  944. break;
  945. }
  946. if (state->selected_input == ADV7604_PAD_VGA_COMP) {
  947. /* Receiving analog YPbPr signal
  948. * Set automode */
  949. io_write_clr_set(sd, 0x02, 0xf0, 0xf0);
  950. break;
  951. }
  952. if (hdmi_signal) {
  953. /* Receiving HDMI signal
  954. * Set automode */
  955. io_write_clr_set(sd, 0x02, 0xf0, 0xf0);
  956. break;
  957. }
  958. /* Receiving DVI-D signal
  959. * ADV7604 selects RGB limited range regardless of
  960. * input format (CE/IT) in automatic mode */
  961. if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
  962. /* RGB limited range (16-235) */
  963. io_write_clr_set(sd, 0x02, 0xf0, 0x00);
  964. } else {
  965. /* RGB full range (0-255) */
  966. io_write_clr_set(sd, 0x02, 0xf0, 0x10);
  967. if (is_digital_input(sd) && rgb_output) {
  968. adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
  969. } else {
  970. adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
  971. adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
  972. }
  973. }
  974. break;
  975. case V4L2_DV_RGB_RANGE_LIMITED:
  976. if (state->selected_input == ADV7604_PAD_VGA_COMP) {
  977. /* YCrCb limited range (16-235) */
  978. io_write_clr_set(sd, 0x02, 0xf0, 0x20);
  979. break;
  980. }
  981. /* RGB limited range (16-235) */
  982. io_write_clr_set(sd, 0x02, 0xf0, 0x00);
  983. break;
  984. case V4L2_DV_RGB_RANGE_FULL:
  985. if (state->selected_input == ADV7604_PAD_VGA_COMP) {
  986. /* YCrCb full range (0-255) */
  987. io_write_clr_set(sd, 0x02, 0xf0, 0x60);
  988. break;
  989. }
  990. /* RGB full range (0-255) */
  991. io_write_clr_set(sd, 0x02, 0xf0, 0x10);
  992. if (is_analog_input(sd) || hdmi_signal)
  993. break;
  994. /* Adjust gain/offset for DVI-D signals only */
  995. if (rgb_output) {
  996. adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
  997. } else {
  998. adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
  999. adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
  1000. }
  1001. break;
  1002. }
  1003. }
  1004. static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
  1005. {
  1006. struct v4l2_subdev *sd =
  1007. &container_of(ctrl->handler, struct adv7604_state, hdl)->sd;
  1008. struct adv7604_state *state = to_state(sd);
  1009. switch (ctrl->id) {
  1010. case V4L2_CID_BRIGHTNESS:
  1011. cp_write(sd, 0x3c, ctrl->val);
  1012. return 0;
  1013. case V4L2_CID_CONTRAST:
  1014. cp_write(sd, 0x3a, ctrl->val);
  1015. return 0;
  1016. case V4L2_CID_SATURATION:
  1017. cp_write(sd, 0x3b, ctrl->val);
  1018. return 0;
  1019. case V4L2_CID_HUE:
  1020. cp_write(sd, 0x3d, ctrl->val);
  1021. return 0;
  1022. case V4L2_CID_DV_RX_RGB_RANGE:
  1023. state->rgb_quantization_range = ctrl->val;
  1024. set_rgb_quantization_range(sd);
  1025. return 0;
  1026. case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
  1027. if (!adv7604_has_afe(state))
  1028. return -EINVAL;
  1029. /* Set the analog sampling phase. This is needed to find the
  1030. best sampling phase for analog video: an application or
  1031. driver has to try a number of phases and analyze the picture
  1032. quality before settling on the best performing phase. */
  1033. afe_write(sd, 0xc8, ctrl->val);
  1034. return 0;
  1035. case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
  1036. /* Use the default blue color for free running mode,
  1037. or supply your own. */
  1038. cp_write_clr_set(sd, 0xbf, 0x04, ctrl->val << 2);
  1039. return 0;
  1040. case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
  1041. cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
  1042. cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
  1043. cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
  1044. return 0;
  1045. }
  1046. return -EINVAL;
  1047. }
  1048. /* ----------------------------------------------------------------------- */
  1049. static inline bool no_power(struct v4l2_subdev *sd)
  1050. {
  1051. /* Entire chip or CP powered off */
  1052. return io_read(sd, 0x0c) & 0x24;
  1053. }
  1054. static inline bool no_signal_tmds(struct v4l2_subdev *sd)
  1055. {
  1056. struct adv7604_state *state = to_state(sd);
  1057. return !(io_read(sd, 0x6a) & (0x10 >> state->selected_input));
  1058. }
  1059. static inline bool no_lock_tmds(struct v4l2_subdev *sd)
  1060. {
  1061. struct adv7604_state *state = to_state(sd);
  1062. const struct adv7604_chip_info *info = state->info;
  1063. return (io_read(sd, 0x6a) & info->tdms_lock_mask) != info->tdms_lock_mask;
  1064. }
  1065. static inline bool is_hdmi(struct v4l2_subdev *sd)
  1066. {
  1067. return hdmi_read(sd, 0x05) & 0x80;
  1068. }
  1069. static inline bool no_lock_sspd(struct v4l2_subdev *sd)
  1070. {
  1071. struct adv7604_state *state = to_state(sd);
  1072. /*
  1073. * Chips without a AFE don't expose registers for the SSPD, so just assume
  1074. * that we have a lock.
  1075. */
  1076. if (adv7604_has_afe(state))
  1077. return false;
  1078. /* TODO channel 2 */
  1079. return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
  1080. }
  1081. static inline bool no_lock_stdi(struct v4l2_subdev *sd)
  1082. {
  1083. /* TODO channel 2 */
  1084. return !(cp_read(sd, 0xb1) & 0x80);
  1085. }
  1086. static inline bool no_signal(struct v4l2_subdev *sd)
  1087. {
  1088. bool ret;
  1089. ret = no_power(sd);
  1090. ret |= no_lock_stdi(sd);
  1091. ret |= no_lock_sspd(sd);
  1092. if (is_digital_input(sd)) {
  1093. ret |= no_lock_tmds(sd);
  1094. ret |= no_signal_tmds(sd);
  1095. }
  1096. return ret;
  1097. }
  1098. static inline bool no_lock_cp(struct v4l2_subdev *sd)
  1099. {
  1100. struct adv7604_state *state = to_state(sd);
  1101. if (!adv7604_has_afe(state))
  1102. return false;
  1103. /* CP has detected a non standard number of lines on the incoming
  1104. video compared to what it is configured to receive by s_dv_timings */
  1105. return io_read(sd, 0x12) & 0x01;
  1106. }
  1107. static int adv7604_g_input_status(struct v4l2_subdev *sd, u32 *status)
  1108. {
  1109. *status = 0;
  1110. *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
  1111. *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
  1112. if (no_lock_cp(sd))
  1113. *status |= is_digital_input(sd) ? V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
  1114. v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
  1115. return 0;
  1116. }
  1117. /* ----------------------------------------------------------------------- */
  1118. struct stdi_readback {
  1119. u16 bl, lcf, lcvs;
  1120. u8 hs_pol, vs_pol;
  1121. bool interlaced;
  1122. };
  1123. static int stdi2dv_timings(struct v4l2_subdev *sd,
  1124. struct stdi_readback *stdi,
  1125. struct v4l2_dv_timings *timings)
  1126. {
  1127. struct adv7604_state *state = to_state(sd);
  1128. u32 hfreq = (ADV7604_fsc * 8) / stdi->bl;
  1129. u32 pix_clk;
  1130. int i;
  1131. for (i = 0; adv7604_timings[i].bt.height; i++) {
  1132. if (vtotal(&adv7604_timings[i].bt) != stdi->lcf + 1)
  1133. continue;
  1134. if (adv7604_timings[i].bt.vsync != stdi->lcvs)
  1135. continue;
  1136. pix_clk = hfreq * htotal(&adv7604_timings[i].bt);
  1137. if ((pix_clk < adv7604_timings[i].bt.pixelclock + 1000000) &&
  1138. (pix_clk > adv7604_timings[i].bt.pixelclock - 1000000)) {
  1139. *timings = adv7604_timings[i];
  1140. return 0;
  1141. }
  1142. }
  1143. if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
  1144. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  1145. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  1146. timings))
  1147. return 0;
  1148. if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
  1149. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  1150. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  1151. state->aspect_ratio, timings))
  1152. return 0;
  1153. v4l2_dbg(2, debug, sd,
  1154. "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
  1155. __func__, stdi->lcvs, stdi->lcf, stdi->bl,
  1156. stdi->hs_pol, stdi->vs_pol);
  1157. return -1;
  1158. }
  1159. static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
  1160. {
  1161. struct adv7604_state *state = to_state(sd);
  1162. const struct adv7604_chip_info *info = state->info;
  1163. u8 polarity;
  1164. if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
  1165. v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
  1166. return -1;
  1167. }
  1168. /* read STDI */
  1169. stdi->bl = cp_read16(sd, 0xb1, 0x3fff);
  1170. stdi->lcf = cp_read16(sd, info->lcf_reg, 0x7ff);
  1171. stdi->lcvs = cp_read(sd, 0xb3) >> 3;
  1172. stdi->interlaced = io_read(sd, 0x12) & 0x10;
  1173. if (adv7604_has_afe(state)) {
  1174. /* read SSPD */
  1175. polarity = cp_read(sd, 0xb5);
  1176. if ((polarity & 0x03) == 0x01) {
  1177. stdi->hs_pol = polarity & 0x10
  1178. ? (polarity & 0x08 ? '+' : '-') : 'x';
  1179. stdi->vs_pol = polarity & 0x40
  1180. ? (polarity & 0x20 ? '+' : '-') : 'x';
  1181. } else {
  1182. stdi->hs_pol = 'x';
  1183. stdi->vs_pol = 'x';
  1184. }
  1185. } else {
  1186. polarity = hdmi_read(sd, 0x05);
  1187. stdi->hs_pol = polarity & 0x20 ? '+' : '-';
  1188. stdi->vs_pol = polarity & 0x10 ? '+' : '-';
  1189. }
  1190. if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
  1191. v4l2_dbg(2, debug, sd,
  1192. "%s: signal lost during readout of STDI/SSPD\n", __func__);
  1193. return -1;
  1194. }
  1195. if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
  1196. v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
  1197. memset(stdi, 0, sizeof(struct stdi_readback));
  1198. return -1;
  1199. }
  1200. v4l2_dbg(2, debug, sd,
  1201. "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
  1202. __func__, stdi->lcf, stdi->bl, stdi->lcvs,
  1203. stdi->hs_pol, stdi->vs_pol,
  1204. stdi->interlaced ? "interlaced" : "progressive");
  1205. return 0;
  1206. }
  1207. static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
  1208. struct v4l2_enum_dv_timings *timings)
  1209. {
  1210. struct adv7604_state *state = to_state(sd);
  1211. if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1)
  1212. return -EINVAL;
  1213. if (timings->pad >= state->source_pad)
  1214. return -EINVAL;
  1215. memset(timings->reserved, 0, sizeof(timings->reserved));
  1216. timings->timings = adv7604_timings[timings->index];
  1217. return 0;
  1218. }
  1219. static int adv7604_dv_timings_cap(struct v4l2_subdev *sd,
  1220. struct v4l2_dv_timings_cap *cap)
  1221. {
  1222. struct adv7604_state *state = to_state(sd);
  1223. if (cap->pad >= state->source_pad)
  1224. return -EINVAL;
  1225. cap->type = V4L2_DV_BT_656_1120;
  1226. cap->bt.max_width = 1920;
  1227. cap->bt.max_height = 1200;
  1228. cap->bt.min_pixelclock = 25000000;
  1229. switch (cap->pad) {
  1230. case ADV7604_PAD_HDMI_PORT_A:
  1231. case ADV7604_PAD_HDMI_PORT_B:
  1232. case ADV7604_PAD_HDMI_PORT_C:
  1233. case ADV7604_PAD_HDMI_PORT_D:
  1234. cap->bt.max_pixelclock = 225000000;
  1235. break;
  1236. case ADV7604_PAD_VGA_RGB:
  1237. case ADV7604_PAD_VGA_COMP:
  1238. default:
  1239. cap->bt.max_pixelclock = 170000000;
  1240. break;
  1241. }
  1242. cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  1243. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
  1244. cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
  1245. V4L2_DV_BT_CAP_REDUCED_BLANKING | V4L2_DV_BT_CAP_CUSTOM;
  1246. return 0;
  1247. }
  1248. /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
  1249. if the format is listed in adv7604_timings[] */
  1250. static void adv7604_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
  1251. struct v4l2_dv_timings *timings)
  1252. {
  1253. int i;
  1254. for (i = 0; adv7604_timings[i].bt.width; i++) {
  1255. if (v4l2_match_dv_timings(timings, &adv7604_timings[i],
  1256. is_digital_input(sd) ? 250000 : 1000000)) {
  1257. *timings = adv7604_timings[i];
  1258. break;
  1259. }
  1260. }
  1261. }
  1262. static unsigned int adv7604_read_hdmi_pixelclock(struct v4l2_subdev *sd)
  1263. {
  1264. unsigned int freq;
  1265. int a, b;
  1266. a = hdmi_read(sd, 0x06);
  1267. b = hdmi_read(sd, 0x3b);
  1268. if (a < 0 || b < 0)
  1269. return 0;
  1270. freq = a * 1000000 + ((b & 0x30) >> 4) * 250000;
  1271. if (is_hdmi(sd)) {
  1272. /* adjust for deep color mode */
  1273. unsigned bits_per_channel = ((hdmi_read(sd, 0x0b) & 0x60) >> 4) + 8;
  1274. freq = freq * 8 / bits_per_channel;
  1275. }
  1276. return freq;
  1277. }
  1278. static unsigned int adv7611_read_hdmi_pixelclock(struct v4l2_subdev *sd)
  1279. {
  1280. int a, b;
  1281. a = hdmi_read(sd, 0x51);
  1282. b = hdmi_read(sd, 0x52);
  1283. if (a < 0 || b < 0)
  1284. return 0;
  1285. return ((a << 1) | (b >> 7)) * 1000000 + (b & 0x7f) * 1000000 / 128;
  1286. }
  1287. static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
  1288. struct v4l2_dv_timings *timings)
  1289. {
  1290. struct adv7604_state *state = to_state(sd);
  1291. const struct adv7604_chip_info *info = state->info;
  1292. struct v4l2_bt_timings *bt = &timings->bt;
  1293. struct stdi_readback stdi;
  1294. if (!timings)
  1295. return -EINVAL;
  1296. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1297. if (no_signal(sd)) {
  1298. state->restart_stdi_once = true;
  1299. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  1300. return -ENOLINK;
  1301. }
  1302. /* read STDI */
  1303. if (read_stdi(sd, &stdi)) {
  1304. v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
  1305. return -ENOLINK;
  1306. }
  1307. bt->interlaced = stdi.interlaced ?
  1308. V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
  1309. if (is_digital_input(sd)) {
  1310. timings->type = V4L2_DV_BT_656_1120;
  1311. /* FIXME: All masks are incorrect for ADV7611 */
  1312. bt->width = hdmi_read16(sd, 0x07, 0xfff);
  1313. bt->height = hdmi_read16(sd, 0x09, 0xfff);
  1314. bt->pixelclock = info->read_hdmi_pixelclock(sd);
  1315. bt->hfrontporch = hdmi_read16(sd, 0x20, 0x3ff);
  1316. bt->hsync = hdmi_read16(sd, 0x22, 0x3ff);
  1317. bt->hbackporch = hdmi_read16(sd, 0x24, 0x3ff);
  1318. bt->vfrontporch = hdmi_read16(sd, 0x2a, 0x1fff) / 2;
  1319. bt->vsync = hdmi_read16(sd, 0x2e, 0x1fff) / 2;
  1320. bt->vbackporch = hdmi_read16(sd, 0x32, 0x1fff) / 2;
  1321. bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
  1322. ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
  1323. if (bt->interlaced == V4L2_DV_INTERLACED) {
  1324. bt->height += hdmi_read16(sd, 0x0b, 0xfff);
  1325. bt->il_vfrontporch = hdmi_read16(sd, 0x2c, 0x1fff) / 2;
  1326. bt->il_vsync = hdmi_read16(sd, 0x30, 0x1fff) / 2;
  1327. bt->il_vbackporch = hdmi_read16(sd, 0x34, 0x1fff) / 2;
  1328. }
  1329. adv7604_fill_optional_dv_timings_fields(sd, timings);
  1330. } else {
  1331. /* find format
  1332. * Since LCVS values are inaccurate [REF_03, p. 275-276],
  1333. * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
  1334. */
  1335. if (!stdi2dv_timings(sd, &stdi, timings))
  1336. goto found;
  1337. stdi.lcvs += 1;
  1338. v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
  1339. if (!stdi2dv_timings(sd, &stdi, timings))
  1340. goto found;
  1341. stdi.lcvs -= 2;
  1342. v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
  1343. if (stdi2dv_timings(sd, &stdi, timings)) {
  1344. /*
  1345. * The STDI block may measure wrong values, especially
  1346. * for lcvs and lcf. If the driver can not find any
  1347. * valid timing, the STDI block is restarted to measure
  1348. * the video timings again. The function will return an
  1349. * error, but the restart of STDI will generate a new
  1350. * STDI interrupt and the format detection process will
  1351. * restart.
  1352. */
  1353. if (state->restart_stdi_once) {
  1354. v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
  1355. /* TODO restart STDI for Sync Channel 2 */
  1356. /* enter one-shot mode */
  1357. cp_write_clr_set(sd, 0x86, 0x06, 0x00);
  1358. /* trigger STDI restart */
  1359. cp_write_clr_set(sd, 0x86, 0x06, 0x04);
  1360. /* reset to continuous mode */
  1361. cp_write_clr_set(sd, 0x86, 0x06, 0x02);
  1362. state->restart_stdi_once = false;
  1363. return -ENOLINK;
  1364. }
  1365. v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
  1366. return -ERANGE;
  1367. }
  1368. state->restart_stdi_once = true;
  1369. }
  1370. found:
  1371. if (no_signal(sd)) {
  1372. v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
  1373. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1374. return -ENOLINK;
  1375. }
  1376. if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
  1377. (is_digital_input(sd) && bt->pixelclock > 225000000)) {
  1378. v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
  1379. __func__, (u32)bt->pixelclock);
  1380. return -ERANGE;
  1381. }
  1382. if (debug > 1)
  1383. v4l2_print_dv_timings(sd->name, "adv7604_query_dv_timings: ",
  1384. timings, true);
  1385. return 0;
  1386. }
  1387. static int adv7604_s_dv_timings(struct v4l2_subdev *sd,
  1388. struct v4l2_dv_timings *timings)
  1389. {
  1390. struct adv7604_state *state = to_state(sd);
  1391. struct v4l2_bt_timings *bt;
  1392. int err;
  1393. if (!timings)
  1394. return -EINVAL;
  1395. if (v4l2_match_dv_timings(&state->timings, timings, 0)) {
  1396. v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
  1397. return 0;
  1398. }
  1399. bt = &timings->bt;
  1400. if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
  1401. (is_digital_input(sd) && bt->pixelclock > 225000000)) {
  1402. v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
  1403. __func__, (u32)bt->pixelclock);
  1404. return -ERANGE;
  1405. }
  1406. adv7604_fill_optional_dv_timings_fields(sd, timings);
  1407. state->timings = *timings;
  1408. cp_write_clr_set(sd, 0x91, 0x40, bt->interlaced ? 0x40 : 0x00);
  1409. /* Use prim_mode and vid_std when available */
  1410. err = configure_predefined_video_timings(sd, timings);
  1411. if (err) {
  1412. /* custom settings when the video format
  1413. does not have prim_mode/vid_std */
  1414. configure_custom_video_timings(sd, bt);
  1415. }
  1416. set_rgb_quantization_range(sd);
  1417. if (debug > 1)
  1418. v4l2_print_dv_timings(sd->name, "adv7604_s_dv_timings: ",
  1419. timings, true);
  1420. return 0;
  1421. }
  1422. static int adv7604_g_dv_timings(struct v4l2_subdev *sd,
  1423. struct v4l2_dv_timings *timings)
  1424. {
  1425. struct adv7604_state *state = to_state(sd);
  1426. *timings = state->timings;
  1427. return 0;
  1428. }
  1429. static void adv7604_set_termination(struct v4l2_subdev *sd, bool enable)
  1430. {
  1431. hdmi_write(sd, 0x01, enable ? 0x00 : 0x78);
  1432. }
  1433. static void adv7611_set_termination(struct v4l2_subdev *sd, bool enable)
  1434. {
  1435. hdmi_write(sd, 0x83, enable ? 0xfe : 0xff);
  1436. }
  1437. static void enable_input(struct v4l2_subdev *sd)
  1438. {
  1439. struct adv7604_state *state = to_state(sd);
  1440. if (is_analog_input(sd)) {
  1441. io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
  1442. } else if (is_digital_input(sd)) {
  1443. hdmi_write_clr_set(sd, 0x00, 0x03, state->selected_input);
  1444. state->info->set_termination(sd, true);
  1445. io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
  1446. hdmi_write_clr_set(sd, 0x1a, 0x10, 0x00); /* Unmute audio */
  1447. } else {
  1448. v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
  1449. __func__, state->selected_input);
  1450. }
  1451. }
  1452. static void disable_input(struct v4l2_subdev *sd)
  1453. {
  1454. struct adv7604_state *state = to_state(sd);
  1455. hdmi_write_clr_set(sd, 0x1a, 0x10, 0x10); /* Mute audio */
  1456. msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 7.16.10] */
  1457. io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
  1458. state->info->set_termination(sd, false);
  1459. }
  1460. static void select_input(struct v4l2_subdev *sd)
  1461. {
  1462. struct adv7604_state *state = to_state(sd);
  1463. const struct adv7604_chip_info *info = state->info;
  1464. if (is_analog_input(sd)) {
  1465. adv7604_write_reg_seq(sd, info->recommended_settings[0]);
  1466. afe_write(sd, 0x00, 0x08); /* power up ADC */
  1467. afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
  1468. afe_write(sd, 0xc8, 0x00); /* phase control */
  1469. } else if (is_digital_input(sd)) {
  1470. hdmi_write(sd, 0x00, state->selected_input & 0x03);
  1471. adv7604_write_reg_seq(sd, info->recommended_settings[1]);
  1472. if (adv7604_has_afe(state)) {
  1473. afe_write(sd, 0x00, 0xff); /* power down ADC */
  1474. afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
  1475. afe_write(sd, 0xc8, 0x40); /* phase control */
  1476. }
  1477. cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
  1478. cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
  1479. cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
  1480. } else {
  1481. v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
  1482. __func__, state->selected_input);
  1483. }
  1484. }
  1485. static int adv7604_s_routing(struct v4l2_subdev *sd,
  1486. u32 input, u32 output, u32 config)
  1487. {
  1488. struct adv7604_state *state = to_state(sd);
  1489. v4l2_dbg(2, debug, sd, "%s: input %d, selected input %d",
  1490. __func__, input, state->selected_input);
  1491. if (input == state->selected_input)
  1492. return 0;
  1493. if (input > state->info->max_port)
  1494. return -EINVAL;
  1495. state->selected_input = input;
  1496. disable_input(sd);
  1497. select_input(sd);
  1498. enable_input(sd);
  1499. return 0;
  1500. }
  1501. static int adv7604_enum_mbus_code(struct v4l2_subdev *sd,
  1502. struct v4l2_subdev_fh *fh,
  1503. struct v4l2_subdev_mbus_code_enum *code)
  1504. {
  1505. struct adv7604_state *state = to_state(sd);
  1506. if (code->index >= state->info->nformats)
  1507. return -EINVAL;
  1508. code->code = state->info->formats[code->index].code;
  1509. return 0;
  1510. }
  1511. static void adv7604_fill_format(struct adv7604_state *state,
  1512. struct v4l2_mbus_framefmt *format)
  1513. {
  1514. memset(format, 0, sizeof(*format));
  1515. format->width = state->timings.bt.width;
  1516. format->height = state->timings.bt.height;
  1517. format->field = V4L2_FIELD_NONE;
  1518. if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861)
  1519. format->colorspace = (state->timings.bt.height <= 576) ?
  1520. V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
  1521. }
  1522. /*
  1523. * Compute the op_ch_sel value required to obtain on the bus the component order
  1524. * corresponding to the selected format taking into account bus reordering
  1525. * applied by the board at the output of the device.
  1526. *
  1527. * The following table gives the op_ch_value from the format component order
  1528. * (expressed as op_ch_sel value in column) and the bus reordering (expressed as
  1529. * adv7604_bus_order value in row).
  1530. *
  1531. * | GBR(0) GRB(1) BGR(2) RGB(3) BRG(4) RBG(5)
  1532. * ----------+-------------------------------------------------
  1533. * RGB (NOP) | GBR GRB BGR RGB BRG RBG
  1534. * GRB (1-2) | BGR RGB GBR GRB RBG BRG
  1535. * RBG (2-3) | GRB GBR BRG RBG BGR RGB
  1536. * BGR (1-3) | RBG BRG RGB BGR GRB GBR
  1537. * BRG (ROR) | BRG RBG GRB GBR RGB BGR
  1538. * GBR (ROL) | RGB BGR RBG BRG GBR GRB
  1539. */
  1540. static unsigned int adv7604_op_ch_sel(struct adv7604_state *state)
  1541. {
  1542. #define _SEL(a,b,c,d,e,f) { \
  1543. ADV7604_OP_CH_SEL_##a, ADV7604_OP_CH_SEL_##b, ADV7604_OP_CH_SEL_##c, \
  1544. ADV7604_OP_CH_SEL_##d, ADV7604_OP_CH_SEL_##e, ADV7604_OP_CH_SEL_##f }
  1545. #define _BUS(x) [ADV7604_BUS_ORDER_##x]
  1546. static const unsigned int op_ch_sel[6][6] = {
  1547. _BUS(RGB) /* NOP */ = _SEL(GBR, GRB, BGR, RGB, BRG, RBG),
  1548. _BUS(GRB) /* 1-2 */ = _SEL(BGR, RGB, GBR, GRB, RBG, BRG),
  1549. _BUS(RBG) /* 2-3 */ = _SEL(GRB, GBR, BRG, RBG, BGR, RGB),
  1550. _BUS(BGR) /* 1-3 */ = _SEL(RBG, BRG, RGB, BGR, GRB, GBR),
  1551. _BUS(BRG) /* ROR */ = _SEL(BRG, RBG, GRB, GBR, RGB, BGR),
  1552. _BUS(GBR) /* ROL */ = _SEL(RGB, BGR, RBG, BRG, GBR, GRB),
  1553. };
  1554. return op_ch_sel[state->pdata.bus_order][state->format->op_ch_sel >> 5];
  1555. }
  1556. static void adv7604_setup_format(struct adv7604_state *state)
  1557. {
  1558. struct v4l2_subdev *sd = &state->sd;
  1559. io_write_clr_set(sd, 0x02, 0x02,
  1560. state->format->rgb_out ? ADV7604_RGB_OUT : 0);
  1561. io_write(sd, 0x03, state->format->op_format_sel |
  1562. state->pdata.op_format_mode_sel);
  1563. io_write_clr_set(sd, 0x04, 0xe0, adv7604_op_ch_sel(state));
  1564. io_write_clr_set(sd, 0x05, 0x01,
  1565. state->format->swap_cb_cr ? ADV7604_OP_SWAP_CB_CR : 0);
  1566. }
  1567. static int adv7604_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1568. struct v4l2_subdev_format *format)
  1569. {
  1570. struct adv7604_state *state = to_state(sd);
  1571. if (format->pad != state->source_pad)
  1572. return -EINVAL;
  1573. adv7604_fill_format(state, &format->format);
  1574. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1575. struct v4l2_mbus_framefmt *fmt;
  1576. fmt = v4l2_subdev_get_try_format(fh, format->pad);
  1577. format->format.code = fmt->code;
  1578. } else {
  1579. format->format.code = state->format->code;
  1580. }
  1581. return 0;
  1582. }
  1583. static int adv7604_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1584. struct v4l2_subdev_format *format)
  1585. {
  1586. struct adv7604_state *state = to_state(sd);
  1587. const struct adv7604_format_info *info;
  1588. if (format->pad != state->source_pad)
  1589. return -EINVAL;
  1590. info = adv7604_format_info(state, format->format.code);
  1591. if (info == NULL)
  1592. info = adv7604_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
  1593. adv7604_fill_format(state, &format->format);
  1594. format->format.code = info->code;
  1595. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1596. struct v4l2_mbus_framefmt *fmt;
  1597. fmt = v4l2_subdev_get_try_format(fh, format->pad);
  1598. fmt->code = format->format.code;
  1599. } else {
  1600. state->format = info;
  1601. adv7604_setup_format(state);
  1602. }
  1603. return 0;
  1604. }
  1605. static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  1606. {
  1607. struct adv7604_state *state = to_state(sd);
  1608. const struct adv7604_chip_info *info = state->info;
  1609. const u8 irq_reg_0x43 = io_read(sd, 0x43);
  1610. const u8 irq_reg_0x6b = io_read(sd, 0x6b);
  1611. const u8 irq_reg_0x70 = io_read(sd, 0x70);
  1612. u8 fmt_change_digital;
  1613. u8 fmt_change;
  1614. u8 tx_5v;
  1615. if (irq_reg_0x43)
  1616. io_write(sd, 0x44, irq_reg_0x43);
  1617. if (irq_reg_0x70)
  1618. io_write(sd, 0x71, irq_reg_0x70);
  1619. if (irq_reg_0x6b)
  1620. io_write(sd, 0x6c, irq_reg_0x6b);
  1621. v4l2_dbg(2, debug, sd, "%s: ", __func__);
  1622. /* format change */
  1623. fmt_change = irq_reg_0x43 & 0x98;
  1624. fmt_change_digital = is_digital_input(sd)
  1625. ? irq_reg_0x6b & info->fmt_change_digital_mask
  1626. : 0;
  1627. if (fmt_change || fmt_change_digital) {
  1628. v4l2_dbg(1, debug, sd,
  1629. "%s: fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
  1630. __func__, fmt_change, fmt_change_digital);
  1631. v4l2_subdev_notify(sd, ADV7604_FMT_CHANGE, NULL);
  1632. if (handled)
  1633. *handled = true;
  1634. }
  1635. /* HDMI/DVI mode */
  1636. if (irq_reg_0x6b & 0x01) {
  1637. v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
  1638. (io_read(sd, 0x6a) & 0x01) ? "HDMI" : "DVI");
  1639. set_rgb_quantization_range(sd);
  1640. if (handled)
  1641. *handled = true;
  1642. }
  1643. /* tx 5v detect */
  1644. tx_5v = io_read(sd, 0x70) & info->cable_det_mask;
  1645. if (tx_5v) {
  1646. v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
  1647. io_write(sd, 0x71, tx_5v);
  1648. adv7604_s_detect_tx_5v_ctrl(sd);
  1649. if (handled)
  1650. *handled = true;
  1651. }
  1652. return 0;
  1653. }
  1654. static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  1655. {
  1656. struct adv7604_state *state = to_state(sd);
  1657. u8 *data = NULL;
  1658. memset(edid->reserved, 0, sizeof(edid->reserved));
  1659. switch (edid->pad) {
  1660. case ADV7604_PAD_HDMI_PORT_A:
  1661. case ADV7604_PAD_HDMI_PORT_B:
  1662. case ADV7604_PAD_HDMI_PORT_C:
  1663. case ADV7604_PAD_HDMI_PORT_D:
  1664. if (state->edid.present & (1 << edid->pad))
  1665. data = state->edid.edid;
  1666. break;
  1667. default:
  1668. return -EINVAL;
  1669. }
  1670. if (edid->start_block == 0 && edid->blocks == 0) {
  1671. edid->blocks = data ? state->edid.blocks : 0;
  1672. return 0;
  1673. }
  1674. if (data == NULL)
  1675. return -ENODATA;
  1676. if (edid->start_block >= state->edid.blocks)
  1677. return -EINVAL;
  1678. if (edid->start_block + edid->blocks > state->edid.blocks)
  1679. edid->blocks = state->edid.blocks - edid->start_block;
  1680. memcpy(edid->edid, data + edid->start_block * 128, edid->blocks * 128);
  1681. return 0;
  1682. }
  1683. static int get_edid_spa_location(const u8 *edid)
  1684. {
  1685. u8 d;
  1686. if ((edid[0x7e] != 1) ||
  1687. (edid[0x80] != 0x02) ||
  1688. (edid[0x81] != 0x03)) {
  1689. return -1;
  1690. }
  1691. /* search Vendor Specific Data Block (tag 3) */
  1692. d = edid[0x82] & 0x7f;
  1693. if (d > 4) {
  1694. int i = 0x84;
  1695. int end = 0x80 + d;
  1696. do {
  1697. u8 tag = edid[i] >> 5;
  1698. u8 len = edid[i] & 0x1f;
  1699. if ((tag == 3) && (len >= 5))
  1700. return i + 4;
  1701. i += len + 1;
  1702. } while (i < end);
  1703. }
  1704. return -1;
  1705. }
  1706. static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  1707. {
  1708. struct adv7604_state *state = to_state(sd);
  1709. const struct adv7604_chip_info *info = state->info;
  1710. int spa_loc;
  1711. int err;
  1712. int i;
  1713. memset(edid->reserved, 0, sizeof(edid->reserved));
  1714. if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
  1715. return -EINVAL;
  1716. if (edid->start_block != 0)
  1717. return -EINVAL;
  1718. if (edid->blocks == 0) {
  1719. /* Disable hotplug and I2C access to EDID RAM from DDC port */
  1720. state->edid.present &= ~(1 << edid->pad);
  1721. adv7604_set_hpd(state, state->edid.present);
  1722. rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, state->edid.present);
  1723. /* Fall back to a 16:9 aspect ratio */
  1724. state->aspect_ratio.numerator = 16;
  1725. state->aspect_ratio.denominator = 9;
  1726. if (!state->edid.present)
  1727. state->edid.blocks = 0;
  1728. v4l2_dbg(2, debug, sd, "%s: clear EDID pad %d, edid.present = 0x%x\n",
  1729. __func__, edid->pad, state->edid.present);
  1730. return 0;
  1731. }
  1732. if (edid->blocks > 2) {
  1733. edid->blocks = 2;
  1734. return -E2BIG;
  1735. }
  1736. v4l2_dbg(2, debug, sd, "%s: write EDID pad %d, edid.present = 0x%x\n",
  1737. __func__, edid->pad, state->edid.present);
  1738. /* Disable hotplug and I2C access to EDID RAM from DDC port */
  1739. cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
  1740. adv7604_set_hpd(state, 0);
  1741. rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, 0x00);
  1742. spa_loc = get_edid_spa_location(edid->edid);
  1743. if (spa_loc < 0)
  1744. spa_loc = 0xc0; /* Default value [REF_02, p. 116] */
  1745. switch (edid->pad) {
  1746. case ADV7604_PAD_HDMI_PORT_A:
  1747. state->spa_port_a[0] = edid->edid[spa_loc];
  1748. state->spa_port_a[1] = edid->edid[spa_loc + 1];
  1749. break;
  1750. case ADV7604_PAD_HDMI_PORT_B:
  1751. rep_write(sd, 0x70, edid->edid[spa_loc]);
  1752. rep_write(sd, 0x71, edid->edid[spa_loc + 1]);
  1753. break;
  1754. case ADV7604_PAD_HDMI_PORT_C:
  1755. rep_write(sd, 0x72, edid->edid[spa_loc]);
  1756. rep_write(sd, 0x73, edid->edid[spa_loc + 1]);
  1757. break;
  1758. case ADV7604_PAD_HDMI_PORT_D:
  1759. rep_write(sd, 0x74, edid->edid[spa_loc]);
  1760. rep_write(sd, 0x75, edid->edid[spa_loc + 1]);
  1761. break;
  1762. default:
  1763. return -EINVAL;
  1764. }
  1765. if (info->type == ADV7604) {
  1766. rep_write(sd, 0x76, spa_loc & 0xff);
  1767. rep_write_clr_set(sd, 0x77, 0x40, (spa_loc & 0x100) >> 2);
  1768. } else {
  1769. /* FIXME: Where is the SPA location LSB register ? */
  1770. rep_write_clr_set(sd, 0x71, 0x01, (spa_loc & 0x100) >> 8);
  1771. }
  1772. edid->edid[spa_loc] = state->spa_port_a[0];
  1773. edid->edid[spa_loc + 1] = state->spa_port_a[1];
  1774. memcpy(state->edid.edid, edid->edid, 128 * edid->blocks);
  1775. state->edid.blocks = edid->blocks;
  1776. state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
  1777. edid->edid[0x16]);
  1778. state->edid.present |= 1 << edid->pad;
  1779. err = edid_write_block(sd, 128 * edid->blocks, state->edid.edid);
  1780. if (err < 0) {
  1781. v4l2_err(sd, "error %d writing edid pad %d\n", err, edid->pad);
  1782. return err;
  1783. }
  1784. /* adv7604 calculates the checksums and enables I2C access to internal
  1785. EDID RAM from DDC port. */
  1786. rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, state->edid.present);
  1787. for (i = 0; i < 1000; i++) {
  1788. if (rep_read(sd, info->edid_status_reg) & state->edid.present)
  1789. break;
  1790. mdelay(1);
  1791. }
  1792. if (i == 1000) {
  1793. v4l2_err(sd, "error enabling edid (0x%x)\n", state->edid.present);
  1794. return -EIO;
  1795. }
  1796. /* enable hotplug after 100 ms */
  1797. queue_delayed_work(state->work_queues,
  1798. &state->delayed_work_enable_hotplug, HZ / 10);
  1799. return 0;
  1800. }
  1801. /*********** avi info frame CEA-861-E **************/
  1802. static void print_avi_infoframe(struct v4l2_subdev *sd)
  1803. {
  1804. int i;
  1805. u8 buf[14];
  1806. u8 avi_len;
  1807. u8 avi_ver;
  1808. if (!is_hdmi(sd)) {
  1809. v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
  1810. return;
  1811. }
  1812. if (!(io_read(sd, 0x60) & 0x01)) {
  1813. v4l2_info(sd, "AVI infoframe not received\n");
  1814. return;
  1815. }
  1816. if (io_read(sd, 0x83) & 0x01) {
  1817. v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
  1818. io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
  1819. if (io_read(sd, 0x83) & 0x01) {
  1820. v4l2_info(sd, "AVI infoframe checksum error still present\n");
  1821. io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
  1822. }
  1823. }
  1824. avi_len = infoframe_read(sd, 0xe2);
  1825. avi_ver = infoframe_read(sd, 0xe1);
  1826. v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
  1827. avi_ver, avi_len);
  1828. if (avi_ver != 0x02)
  1829. return;
  1830. for (i = 0; i < 14; i++)
  1831. buf[i] = infoframe_read(sd, i);
  1832. v4l2_info(sd,
  1833. "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1834. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
  1835. buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
  1836. }
  1837. static int adv7604_log_status(struct v4l2_subdev *sd)
  1838. {
  1839. struct adv7604_state *state = to_state(sd);
  1840. const struct adv7604_chip_info *info = state->info;
  1841. struct v4l2_dv_timings timings;
  1842. struct stdi_readback stdi;
  1843. u8 reg_io_0x02 = io_read(sd, 0x02);
  1844. u8 edid_enabled;
  1845. u8 cable_det;
  1846. static const char * const csc_coeff_sel_rb[16] = {
  1847. "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
  1848. "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
  1849. "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
  1850. "reserved", "reserved", "reserved", "reserved", "manual"
  1851. };
  1852. static const char * const input_color_space_txt[16] = {
  1853. "RGB limited range (16-235)", "RGB full range (0-255)",
  1854. "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
  1855. "xvYCC Bt.601", "xvYCC Bt.709",
  1856. "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
  1857. "invalid", "invalid", "invalid", "invalid", "invalid",
  1858. "invalid", "invalid", "automatic"
  1859. };
  1860. static const char * const rgb_quantization_range_txt[] = {
  1861. "Automatic",
  1862. "RGB limited range (16-235)",
  1863. "RGB full range (0-255)",
  1864. };
  1865. static const char * const deep_color_mode_txt[4] = {
  1866. "8-bits per channel",
  1867. "10-bits per channel",
  1868. "12-bits per channel",
  1869. "16-bits per channel (not supported)"
  1870. };
  1871. v4l2_info(sd, "-----Chip status-----\n");
  1872. v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
  1873. edid_enabled = rep_read(sd, info->edid_status_reg);
  1874. v4l2_info(sd, "EDID enabled port A: %s, B: %s, C: %s, D: %s\n",
  1875. ((edid_enabled & 0x01) ? "Yes" : "No"),
  1876. ((edid_enabled & 0x02) ? "Yes" : "No"),
  1877. ((edid_enabled & 0x04) ? "Yes" : "No"),
  1878. ((edid_enabled & 0x08) ? "Yes" : "No"));
  1879. v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
  1880. "enabled" : "disabled");
  1881. v4l2_info(sd, "-----Signal status-----\n");
  1882. cable_det = info->read_cable_det(sd);
  1883. v4l2_info(sd, "Cable detected (+5V power) port A: %s, B: %s, C: %s, D: %s\n",
  1884. ((cable_det & 0x01) ? "Yes" : "No"),
  1885. ((cable_det & 0x02) ? "Yes" : "No"),
  1886. ((cable_det & 0x04) ? "Yes" : "No"),
  1887. ((cable_det & 0x08) ? "Yes" : "No"));
  1888. v4l2_info(sd, "TMDS signal detected: %s\n",
  1889. no_signal_tmds(sd) ? "false" : "true");
  1890. v4l2_info(sd, "TMDS signal locked: %s\n",
  1891. no_lock_tmds(sd) ? "false" : "true");
  1892. v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
  1893. v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
  1894. v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
  1895. v4l2_info(sd, "CP free run: %s\n",
  1896. (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
  1897. v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
  1898. io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
  1899. (io_read(sd, 0x01) & 0x70) >> 4);
  1900. v4l2_info(sd, "-----Video Timings-----\n");
  1901. if (read_stdi(sd, &stdi))
  1902. v4l2_info(sd, "STDI: not locked\n");
  1903. else
  1904. v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
  1905. stdi.lcf, stdi.bl, stdi.lcvs,
  1906. stdi.interlaced ? "interlaced" : "progressive",
  1907. stdi.hs_pol, stdi.vs_pol);
  1908. if (adv7604_query_dv_timings(sd, &timings))
  1909. v4l2_info(sd, "No video detected\n");
  1910. else
  1911. v4l2_print_dv_timings(sd->name, "Detected format: ",
  1912. &timings, true);
  1913. v4l2_print_dv_timings(sd->name, "Configured format: ",
  1914. &state->timings, true);
  1915. if (no_signal(sd))
  1916. return 0;
  1917. v4l2_info(sd, "-----Color space-----\n");
  1918. v4l2_info(sd, "RGB quantization range ctrl: %s\n",
  1919. rgb_quantization_range_txt[state->rgb_quantization_range]);
  1920. v4l2_info(sd, "Input color space: %s\n",
  1921. input_color_space_txt[reg_io_0x02 >> 4]);
  1922. v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
  1923. (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
  1924. (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
  1925. ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
  1926. "enabled" : "disabled");
  1927. v4l2_info(sd, "Color space conversion: %s\n",
  1928. csc_coeff_sel_rb[cp_read(sd, 0xfc) >> 4]);
  1929. if (!is_digital_input(sd))
  1930. return 0;
  1931. v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
  1932. v4l2_info(sd, "Digital video port selected: %c\n",
  1933. (hdmi_read(sd, 0x00) & 0x03) + 'A');
  1934. v4l2_info(sd, "HDCP encrypted content: %s\n",
  1935. (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
  1936. v4l2_info(sd, "HDCP keys read: %s%s\n",
  1937. (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
  1938. (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
  1939. if (is_hdmi(sd)) {
  1940. bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
  1941. bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
  1942. bool audio_mute = io_read(sd, 0x65) & 0x40;
  1943. v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
  1944. audio_pll_locked ? "locked" : "not locked",
  1945. audio_sample_packet_detect ? "detected" : "not detected",
  1946. audio_mute ? "muted" : "enabled");
  1947. if (audio_pll_locked && audio_sample_packet_detect) {
  1948. v4l2_info(sd, "Audio format: %s\n",
  1949. (hdmi_read(sd, 0x07) & 0x20) ? "multi-channel" : "stereo");
  1950. }
  1951. v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
  1952. (hdmi_read(sd, 0x5c) << 8) +
  1953. (hdmi_read(sd, 0x5d) & 0xf0));
  1954. v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
  1955. (hdmi_read(sd, 0x5e) << 8) +
  1956. hdmi_read(sd, 0x5f));
  1957. v4l2_info(sd, "AV Mute: %s\n", (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
  1958. v4l2_info(sd, "Deep color mode: %s\n", deep_color_mode_txt[(hdmi_read(sd, 0x0b) & 0x60) >> 5]);
  1959. print_avi_infoframe(sd);
  1960. }
  1961. return 0;
  1962. }
  1963. /* ----------------------------------------------------------------------- */
  1964. static const struct v4l2_ctrl_ops adv7604_ctrl_ops = {
  1965. .s_ctrl = adv7604_s_ctrl,
  1966. };
  1967. static const struct v4l2_subdev_core_ops adv7604_core_ops = {
  1968. .log_status = adv7604_log_status,
  1969. .interrupt_service_routine = adv7604_isr,
  1970. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1971. .g_register = adv7604_g_register,
  1972. .s_register = adv7604_s_register,
  1973. #endif
  1974. };
  1975. static const struct v4l2_subdev_video_ops adv7604_video_ops = {
  1976. .s_routing = adv7604_s_routing,
  1977. .g_input_status = adv7604_g_input_status,
  1978. .s_dv_timings = adv7604_s_dv_timings,
  1979. .g_dv_timings = adv7604_g_dv_timings,
  1980. .query_dv_timings = adv7604_query_dv_timings,
  1981. };
  1982. static const struct v4l2_subdev_pad_ops adv7604_pad_ops = {
  1983. .enum_mbus_code = adv7604_enum_mbus_code,
  1984. .get_fmt = adv7604_get_format,
  1985. .set_fmt = adv7604_set_format,
  1986. .get_edid = adv7604_get_edid,
  1987. .set_edid = adv7604_set_edid,
  1988. .dv_timings_cap = adv7604_dv_timings_cap,
  1989. .enum_dv_timings = adv7604_enum_dv_timings,
  1990. };
  1991. static const struct v4l2_subdev_ops adv7604_ops = {
  1992. .core = &adv7604_core_ops,
  1993. .video = &adv7604_video_ops,
  1994. .pad = &adv7604_pad_ops,
  1995. };
  1996. /* -------------------------- custom ctrls ---------------------------------- */
  1997. static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
  1998. .ops = &adv7604_ctrl_ops,
  1999. .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
  2000. .name = "Analog Sampling Phase",
  2001. .type = V4L2_CTRL_TYPE_INTEGER,
  2002. .min = 0,
  2003. .max = 0x1f,
  2004. .step = 1,
  2005. .def = 0,
  2006. };
  2007. static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color_manual = {
  2008. .ops = &adv7604_ctrl_ops,
  2009. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
  2010. .name = "Free Running Color, Manual",
  2011. .type = V4L2_CTRL_TYPE_BOOLEAN,
  2012. .min = false,
  2013. .max = true,
  2014. .step = 1,
  2015. .def = false,
  2016. };
  2017. static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color = {
  2018. .ops = &adv7604_ctrl_ops,
  2019. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
  2020. .name = "Free Running Color",
  2021. .type = V4L2_CTRL_TYPE_INTEGER,
  2022. .min = 0x0,
  2023. .max = 0xffffff,
  2024. .step = 0x1,
  2025. .def = 0x0,
  2026. };
  2027. /* ----------------------------------------------------------------------- */
  2028. static int adv7604_core_init(struct v4l2_subdev *sd)
  2029. {
  2030. struct adv7604_state *state = to_state(sd);
  2031. const struct adv7604_chip_info *info = state->info;
  2032. struct adv7604_platform_data *pdata = &state->pdata;
  2033. hdmi_write(sd, 0x48,
  2034. (pdata->disable_pwrdnb ? 0x80 : 0) |
  2035. (pdata->disable_cable_det_rst ? 0x40 : 0));
  2036. disable_input(sd);
  2037. if (pdata->default_input >= 0 &&
  2038. pdata->default_input < state->source_pad) {
  2039. state->selected_input = pdata->default_input;
  2040. select_input(sd);
  2041. enable_input(sd);
  2042. }
  2043. /* power */
  2044. io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
  2045. io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
  2046. cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
  2047. /* video format */
  2048. io_write_clr_set(sd, 0x02, 0x0f,
  2049. pdata->alt_gamma << 3 |
  2050. pdata->op_656_range << 2 |
  2051. pdata->alt_data_sat << 0);
  2052. io_write_clr_set(sd, 0x05, 0x0e, pdata->blank_data << 3 |
  2053. pdata->insert_av_codes << 2 |
  2054. pdata->replicate_av_codes << 1);
  2055. adv7604_setup_format(state);
  2056. cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
  2057. /* VS, HS polarities */
  2058. io_write(sd, 0x06, 0xa0 | pdata->inv_vs_pol << 2 |
  2059. pdata->inv_hs_pol << 1 | pdata->inv_llc_pol);
  2060. /* Adjust drive strength */
  2061. io_write(sd, 0x14, 0x40 | pdata->dr_str_data << 4 |
  2062. pdata->dr_str_clk << 2 |
  2063. pdata->dr_str_sync);
  2064. cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
  2065. cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
  2066. cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
  2067. ADI recommended setting [REF_01, c. 2.3.3] */
  2068. cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
  2069. ADI recommended setting [REF_01, c. 2.3.3] */
  2070. cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
  2071. for digital formats */
  2072. /* HDMI audio */
  2073. hdmi_write_clr_set(sd, 0x15, 0x03, 0x03); /* Mute on FIFO over-/underflow [REF_01, c. 1.2.18] */
  2074. hdmi_write_clr_set(sd, 0x1a, 0x0e, 0x08); /* Wait 1 s before unmute */
  2075. hdmi_write_clr_set(sd, 0x68, 0x06, 0x06); /* FIFO reset on over-/underflow [REF_01, c. 1.2.19] */
  2076. /* TODO from platform data */
  2077. afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
  2078. if (adv7604_has_afe(state)) {
  2079. afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
  2080. io_write_clr_set(sd, 0x30, 1 << 4, pdata->output_bus_lsb_to_msb << 4);
  2081. }
  2082. /* interrupts */
  2083. io_write(sd, 0x40, 0xc0 | pdata->int1_config); /* Configure INT1 */
  2084. io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
  2085. io_write(sd, 0x6e, info->fmt_change_digital_mask); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
  2086. io_write(sd, 0x73, info->cable_det_mask); /* Enable cable detection (+5v) interrupts */
  2087. info->setup_irqs(sd);
  2088. return v4l2_ctrl_handler_setup(sd->ctrl_handler);
  2089. }
  2090. static void adv7604_setup_irqs(struct v4l2_subdev *sd)
  2091. {
  2092. io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
  2093. }
  2094. static void adv7611_setup_irqs(struct v4l2_subdev *sd)
  2095. {
  2096. io_write(sd, 0x41, 0xd0); /* STDI irq for any change, disable INT2 */
  2097. }
  2098. static void adv7604_unregister_clients(struct adv7604_state *state)
  2099. {
  2100. unsigned int i;
  2101. for (i = 1; i < ARRAY_SIZE(state->i2c_clients); ++i) {
  2102. if (state->i2c_clients[i])
  2103. i2c_unregister_device(state->i2c_clients[i]);
  2104. }
  2105. }
  2106. static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
  2107. u8 addr, u8 io_reg)
  2108. {
  2109. struct i2c_client *client = v4l2_get_subdevdata(sd);
  2110. if (addr)
  2111. io_write(sd, io_reg, addr << 1);
  2112. return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
  2113. }
  2114. static const struct adv7604_reg_seq adv7604_recommended_settings_afe[] = {
  2115. /* reset ADI recommended settings for HDMI: */
  2116. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
  2117. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
  2118. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
  2119. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x00 }, /* DDC bus active pull-up control */
  2120. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x74 }, /* TMDS PLL optimization */
  2121. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
  2122. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0x74 }, /* TMDS PLL optimization */
  2123. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x63 }, /* TMDS PLL optimization */
  2124. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
  2125. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
  2126. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x88 }, /* equaliser */
  2127. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2e }, /* equaliser */
  2128. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x00 }, /* enable automatic EQ changing */
  2129. /* set ADI recommended settings for digitizer */
  2130. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
  2131. { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0x7b }, /* ADC noise shaping filter controls */
  2132. { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x1f }, /* CP core gain controls */
  2133. { ADV7604_REG(ADV7604_PAGE_CP, 0x3e), 0x04 }, /* CP core pre-gain control */
  2134. { ADV7604_REG(ADV7604_PAGE_CP, 0xc3), 0x39 }, /* CP coast control. Graphics mode */
  2135. { ADV7604_REG(ADV7604_PAGE_CP, 0x40), 0x5c }, /* CP core pre-gain control. Graphics mode */
  2136. { ADV7604_REG_SEQ_TERM, 0 },
  2137. };
  2138. static const struct adv7604_reg_seq adv7604_recommended_settings_hdmi[] = {
  2139. /* set ADI recommended settings for HDMI: */
  2140. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
  2141. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x84 }, /* HDMI filter optimization */
  2142. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x10 }, /* DDC bus active pull-up control */
  2143. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x39 }, /* TMDS PLL optimization */
  2144. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
  2145. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xb6 }, /* TMDS PLL optimization */
  2146. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x03 }, /* TMDS PLL optimization */
  2147. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
  2148. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
  2149. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x8b }, /* equaliser */
  2150. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2d }, /* equaliser */
  2151. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x01 }, /* enable automatic EQ changing */
  2152. /* reset ADI recommended settings for digitizer */
  2153. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
  2154. { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0xfb }, /* ADC noise shaping filter controls */
  2155. { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x0d }, /* CP core gain controls */
  2156. { ADV7604_REG_SEQ_TERM, 0 },
  2157. };
  2158. static const struct adv7604_reg_seq adv7611_recommended_settings_hdmi[] = {
  2159. /* ADV7611 Register Settings Recommendations Rev 1.5, May 2014 */
  2160. { ADV7604_REG(ADV7604_PAGE_CP, 0x6c), 0x00 },
  2161. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x9b), 0x03 },
  2162. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x6f), 0x08 },
  2163. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x85), 0x1f },
  2164. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x87), 0x70 },
  2165. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xda },
  2166. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x01 },
  2167. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x03), 0x98 },
  2168. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4c), 0x44 },
  2169. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x04 },
  2170. { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x1e },
  2171. { ADV7604_REG_SEQ_TERM, 0 },
  2172. };
  2173. static const struct adv7604_chip_info adv7604_chip_info[] = {
  2174. [ADV7604] = {
  2175. .type = ADV7604,
  2176. .has_afe = true,
  2177. .max_port = ADV7604_PAD_VGA_COMP,
  2178. .num_dv_ports = 4,
  2179. .edid_enable_reg = 0x77,
  2180. .edid_status_reg = 0x7d,
  2181. .lcf_reg = 0xb3,
  2182. .tdms_lock_mask = 0xe0,
  2183. .cable_det_mask = 0x1e,
  2184. .fmt_change_digital_mask = 0xc1,
  2185. .formats = adv7604_formats,
  2186. .nformats = ARRAY_SIZE(adv7604_formats),
  2187. .set_termination = adv7604_set_termination,
  2188. .setup_irqs = adv7604_setup_irqs,
  2189. .read_hdmi_pixelclock = adv7604_read_hdmi_pixelclock,
  2190. .read_cable_det = adv7604_read_cable_det,
  2191. .recommended_settings = {
  2192. [0] = adv7604_recommended_settings_afe,
  2193. [1] = adv7604_recommended_settings_hdmi,
  2194. },
  2195. .num_recommended_settings = {
  2196. [0] = ARRAY_SIZE(adv7604_recommended_settings_afe),
  2197. [1] = ARRAY_SIZE(adv7604_recommended_settings_hdmi),
  2198. },
  2199. .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_AVLINK) |
  2200. BIT(ADV7604_PAGE_CEC) | BIT(ADV7604_PAGE_INFOFRAME) |
  2201. BIT(ADV7604_PAGE_ESDP) | BIT(ADV7604_PAGE_DPP) |
  2202. BIT(ADV7604_PAGE_AFE) | BIT(ADV7604_PAGE_REP) |
  2203. BIT(ADV7604_PAGE_EDID) | BIT(ADV7604_PAGE_HDMI) |
  2204. BIT(ADV7604_PAGE_TEST) | BIT(ADV7604_PAGE_CP) |
  2205. BIT(ADV7604_PAGE_VDP),
  2206. },
  2207. [ADV7611] = {
  2208. .type = ADV7611,
  2209. .has_afe = false,
  2210. .max_port = ADV7604_PAD_HDMI_PORT_A,
  2211. .num_dv_ports = 1,
  2212. .edid_enable_reg = 0x74,
  2213. .edid_status_reg = 0x76,
  2214. .lcf_reg = 0xa3,
  2215. .tdms_lock_mask = 0x43,
  2216. .cable_det_mask = 0x01,
  2217. .fmt_change_digital_mask = 0x03,
  2218. .formats = adv7611_formats,
  2219. .nformats = ARRAY_SIZE(adv7611_formats),
  2220. .set_termination = adv7611_set_termination,
  2221. .setup_irqs = adv7611_setup_irqs,
  2222. .read_hdmi_pixelclock = adv7611_read_hdmi_pixelclock,
  2223. .read_cable_det = adv7611_read_cable_det,
  2224. .recommended_settings = {
  2225. [1] = adv7611_recommended_settings_hdmi,
  2226. },
  2227. .num_recommended_settings = {
  2228. [1] = ARRAY_SIZE(adv7611_recommended_settings_hdmi),
  2229. },
  2230. .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_CEC) |
  2231. BIT(ADV7604_PAGE_INFOFRAME) | BIT(ADV7604_PAGE_AFE) |
  2232. BIT(ADV7604_PAGE_REP) | BIT(ADV7604_PAGE_EDID) |
  2233. BIT(ADV7604_PAGE_HDMI) | BIT(ADV7604_PAGE_CP),
  2234. },
  2235. };
  2236. static struct i2c_device_id adv7604_i2c_id[] = {
  2237. { "adv7604", (kernel_ulong_t)&adv7604_chip_info[ADV7604] },
  2238. { "adv7611", (kernel_ulong_t)&adv7604_chip_info[ADV7611] },
  2239. { }
  2240. };
  2241. MODULE_DEVICE_TABLE(i2c, adv7604_i2c_id);
  2242. static struct of_device_id adv7604_of_id[] __maybe_unused = {
  2243. { .compatible = "adi,adv7611", .data = &adv7604_chip_info[ADV7611] },
  2244. { }
  2245. };
  2246. MODULE_DEVICE_TABLE(of, adv7604_of_id);
  2247. static int adv7604_parse_dt(struct adv7604_state *state)
  2248. {
  2249. struct v4l2_of_endpoint bus_cfg;
  2250. struct device_node *endpoint;
  2251. struct device_node *np;
  2252. unsigned int flags;
  2253. np = state->i2c_clients[ADV7604_PAGE_IO]->dev.of_node;
  2254. /* Parse the endpoint. */
  2255. endpoint = of_graph_get_next_endpoint(np, NULL);
  2256. if (!endpoint)
  2257. return -EINVAL;
  2258. v4l2_of_parse_endpoint(endpoint, &bus_cfg);
  2259. of_node_put(endpoint);
  2260. flags = bus_cfg.bus.parallel.flags;
  2261. if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  2262. state->pdata.inv_hs_pol = 1;
  2263. if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  2264. state->pdata.inv_vs_pol = 1;
  2265. if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  2266. state->pdata.inv_llc_pol = 1;
  2267. if (bus_cfg.bus_type == V4L2_MBUS_BT656) {
  2268. state->pdata.insert_av_codes = 1;
  2269. state->pdata.op_656_range = 1;
  2270. }
  2271. /* Disable the interrupt for now as no DT-based board uses it. */
  2272. state->pdata.int1_config = ADV7604_INT1_CONFIG_DISABLED;
  2273. /* Use the default I2C addresses. */
  2274. state->pdata.i2c_addresses[ADV7604_PAGE_AVLINK] = 0x42;
  2275. state->pdata.i2c_addresses[ADV7604_PAGE_CEC] = 0x40;
  2276. state->pdata.i2c_addresses[ADV7604_PAGE_INFOFRAME] = 0x3e;
  2277. state->pdata.i2c_addresses[ADV7604_PAGE_ESDP] = 0x38;
  2278. state->pdata.i2c_addresses[ADV7604_PAGE_DPP] = 0x3c;
  2279. state->pdata.i2c_addresses[ADV7604_PAGE_AFE] = 0x26;
  2280. state->pdata.i2c_addresses[ADV7604_PAGE_REP] = 0x32;
  2281. state->pdata.i2c_addresses[ADV7604_PAGE_EDID] = 0x36;
  2282. state->pdata.i2c_addresses[ADV7604_PAGE_HDMI] = 0x34;
  2283. state->pdata.i2c_addresses[ADV7604_PAGE_TEST] = 0x30;
  2284. state->pdata.i2c_addresses[ADV7604_PAGE_CP] = 0x22;
  2285. state->pdata.i2c_addresses[ADV7604_PAGE_VDP] = 0x24;
  2286. /* Hardcode the remaining platform data fields. */
  2287. state->pdata.disable_pwrdnb = 0;
  2288. state->pdata.disable_cable_det_rst = 0;
  2289. state->pdata.default_input = -1;
  2290. state->pdata.blank_data = 1;
  2291. state->pdata.alt_data_sat = 1;
  2292. state->pdata.op_format_mode_sel = ADV7604_OP_FORMAT_MODE0;
  2293. state->pdata.bus_order = ADV7604_BUS_ORDER_RGB;
  2294. return 0;
  2295. }
  2296. static int adv7604_probe(struct i2c_client *client,
  2297. const struct i2c_device_id *id)
  2298. {
  2299. static const struct v4l2_dv_timings cea640x480 =
  2300. V4L2_DV_BT_CEA_640X480P59_94;
  2301. struct adv7604_state *state;
  2302. struct v4l2_ctrl_handler *hdl;
  2303. struct v4l2_subdev *sd;
  2304. unsigned int i;
  2305. u16 val;
  2306. int err;
  2307. /* Check if the adapter supports the needed features */
  2308. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  2309. return -EIO;
  2310. v4l_dbg(1, debug, client, "detecting adv7604 client on address 0x%x\n",
  2311. client->addr << 1);
  2312. state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
  2313. if (!state) {
  2314. v4l_err(client, "Could not allocate adv7604_state memory!\n");
  2315. return -ENOMEM;
  2316. }
  2317. state->i2c_clients[ADV7604_PAGE_IO] = client;
  2318. /* initialize variables */
  2319. state->restart_stdi_once = true;
  2320. state->selected_input = ~0;
  2321. if (IS_ENABLED(CONFIG_OF) && client->dev.of_node) {
  2322. const struct of_device_id *oid;
  2323. oid = of_match_node(adv7604_of_id, client->dev.of_node);
  2324. state->info = oid->data;
  2325. err = adv7604_parse_dt(state);
  2326. if (err < 0) {
  2327. v4l_err(client, "DT parsing error\n");
  2328. return err;
  2329. }
  2330. } else if (client->dev.platform_data) {
  2331. struct adv7604_platform_data *pdata = client->dev.platform_data;
  2332. state->info = (const struct adv7604_chip_info *)id->driver_data;
  2333. state->pdata = *pdata;
  2334. } else {
  2335. v4l_err(client, "No platform data!\n");
  2336. return -ENODEV;
  2337. }
  2338. /* Request GPIOs. */
  2339. for (i = 0; i < state->info->num_dv_ports; ++i) {
  2340. state->hpd_gpio[i] =
  2341. devm_gpiod_get_index(&client->dev, "hpd", i);
  2342. if (IS_ERR(state->hpd_gpio[i]))
  2343. continue;
  2344. gpiod_direction_output(state->hpd_gpio[i], 0);
  2345. v4l_info(client, "Handling HPD %u GPIO\n", i);
  2346. }
  2347. state->timings = cea640x480;
  2348. state->format = adv7604_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
  2349. sd = &state->sd;
  2350. v4l2_i2c_subdev_init(sd, client, &adv7604_ops);
  2351. snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
  2352. id->name, i2c_adapter_id(client->adapter),
  2353. client->addr);
  2354. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  2355. /*
  2356. * Verify that the chip is present. On ADV7604 the RD_INFO register only
  2357. * identifies the revision, while on ADV7611 it identifies the model as
  2358. * well. Use the HDMI slave address on ADV7604 and RD_INFO on ADV7611.
  2359. */
  2360. if (state->info->type == ADV7604) {
  2361. val = adv_smbus_read_byte_data_check(client, 0xfb, false);
  2362. if (val != 0x68) {
  2363. v4l2_info(sd, "not an adv7604 on address 0x%x\n",
  2364. client->addr << 1);
  2365. return -ENODEV;
  2366. }
  2367. } else {
  2368. val = (adv_smbus_read_byte_data_check(client, 0xea, false) << 8)
  2369. | (adv_smbus_read_byte_data_check(client, 0xeb, false) << 0);
  2370. if (val != 0x2051) {
  2371. v4l2_info(sd, "not an adv7611 on address 0x%x\n",
  2372. client->addr << 1);
  2373. return -ENODEV;
  2374. }
  2375. }
  2376. /* control handlers */
  2377. hdl = &state->hdl;
  2378. v4l2_ctrl_handler_init(hdl, adv7604_has_afe(state) ? 9 : 8);
  2379. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  2380. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  2381. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  2382. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  2383. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  2384. V4L2_CID_SATURATION, 0, 255, 1, 128);
  2385. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  2386. V4L2_CID_HUE, 0, 128, 1, 0);
  2387. /* private controls */
  2388. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  2389. V4L2_CID_DV_RX_POWER_PRESENT, 0,
  2390. (1 << state->info->num_dv_ports) - 1, 0, 0);
  2391. state->rgb_quantization_range_ctrl =
  2392. v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops,
  2393. V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
  2394. 0, V4L2_DV_RGB_RANGE_AUTO);
  2395. /* custom controls */
  2396. if (adv7604_has_afe(state))
  2397. state->analog_sampling_phase_ctrl =
  2398. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
  2399. state->free_run_color_manual_ctrl =
  2400. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL);
  2401. state->free_run_color_ctrl =
  2402. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color, NULL);
  2403. sd->ctrl_handler = hdl;
  2404. if (hdl->error) {
  2405. err = hdl->error;
  2406. goto err_hdl;
  2407. }
  2408. state->detect_tx_5v_ctrl->is_private = true;
  2409. state->rgb_quantization_range_ctrl->is_private = true;
  2410. if (adv7604_has_afe(state))
  2411. state->analog_sampling_phase_ctrl->is_private = true;
  2412. state->free_run_color_manual_ctrl->is_private = true;
  2413. state->free_run_color_ctrl->is_private = true;
  2414. if (adv7604_s_detect_tx_5v_ctrl(sd)) {
  2415. err = -ENODEV;
  2416. goto err_hdl;
  2417. }
  2418. for (i = 1; i < ADV7604_PAGE_MAX; ++i) {
  2419. if (!(BIT(i) & state->info->page_mask))
  2420. continue;
  2421. state->i2c_clients[i] =
  2422. adv7604_dummy_client(sd, state->pdata.i2c_addresses[i],
  2423. 0xf2 + i);
  2424. if (state->i2c_clients[i] == NULL) {
  2425. err = -ENOMEM;
  2426. v4l2_err(sd, "failed to create i2c client %u\n", i);
  2427. goto err_i2c;
  2428. }
  2429. }
  2430. /* work queues */
  2431. state->work_queues = create_singlethread_workqueue(client->name);
  2432. if (!state->work_queues) {
  2433. v4l2_err(sd, "Could not create work queue\n");
  2434. err = -ENOMEM;
  2435. goto err_i2c;
  2436. }
  2437. INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
  2438. adv7604_delayed_work_enable_hotplug);
  2439. state->source_pad = state->info->num_dv_ports
  2440. + (state->info->has_afe ? 2 : 0);
  2441. for (i = 0; i < state->source_pad; ++i)
  2442. state->pads[i].flags = MEDIA_PAD_FL_SINK;
  2443. state->pads[state->source_pad].flags = MEDIA_PAD_FL_SOURCE;
  2444. err = media_entity_init(&sd->entity, state->source_pad + 1,
  2445. state->pads, 0);
  2446. if (err)
  2447. goto err_work_queues;
  2448. err = adv7604_core_init(sd);
  2449. if (err)
  2450. goto err_entity;
  2451. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  2452. client->addr << 1, client->adapter->name);
  2453. err = v4l2_async_register_subdev(sd);
  2454. if (err)
  2455. goto err_entity;
  2456. return 0;
  2457. err_entity:
  2458. media_entity_cleanup(&sd->entity);
  2459. err_work_queues:
  2460. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  2461. destroy_workqueue(state->work_queues);
  2462. err_i2c:
  2463. adv7604_unregister_clients(state);
  2464. err_hdl:
  2465. v4l2_ctrl_handler_free(hdl);
  2466. return err;
  2467. }
  2468. /* ----------------------------------------------------------------------- */
  2469. static int adv7604_remove(struct i2c_client *client)
  2470. {
  2471. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2472. struct adv7604_state *state = to_state(sd);
  2473. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  2474. destroy_workqueue(state->work_queues);
  2475. v4l2_async_unregister_subdev(sd);
  2476. v4l2_device_unregister_subdev(sd);
  2477. media_entity_cleanup(&sd->entity);
  2478. adv7604_unregister_clients(to_state(sd));
  2479. v4l2_ctrl_handler_free(sd->ctrl_handler);
  2480. return 0;
  2481. }
  2482. /* ----------------------------------------------------------------------- */
  2483. static struct i2c_driver adv7604_driver = {
  2484. .driver = {
  2485. .owner = THIS_MODULE,
  2486. .name = "adv7604",
  2487. .of_match_table = of_match_ptr(adv7604_of_id),
  2488. },
  2489. .probe = adv7604_probe,
  2490. .remove = adv7604_remove,
  2491. .id_table = adv7604_i2c_id,
  2492. };
  2493. module_i2c_driver(adv7604_driver);