adv7604.c 87 KB

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