adv7604.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136
  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/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/slab.h>
  31. #include <linux/i2c.h>
  32. #include <linux/delay.h>
  33. #include <linux/videodev2.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/v4l2-dv-timings.h>
  36. #include <media/v4l2-device.h>
  37. #include <media/v4l2-ctrls.h>
  38. #include <media/v4l2-chip-ident.h>
  39. #include <media/adv7604.h>
  40. static int debug;
  41. module_param(debug, int, 0644);
  42. MODULE_PARM_DESC(debug, "debug level (0-2)");
  43. MODULE_DESCRIPTION("Analog Devices ADV7604 video decoder driver");
  44. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
  45. MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
  46. MODULE_LICENSE("GPL");
  47. /* ADV7604 system clock frequency */
  48. #define ADV7604_fsc (28636360)
  49. #define DIGITAL_INPUT (state->mode == ADV7604_MODE_HDMI)
  50. /*
  51. **********************************************************************
  52. *
  53. * Arrays with configuration parameters for the ADV7604
  54. *
  55. **********************************************************************
  56. */
  57. struct adv7604_state {
  58. struct adv7604_platform_data pdata;
  59. struct v4l2_subdev sd;
  60. struct media_pad pad;
  61. struct v4l2_ctrl_handler hdl;
  62. enum adv7604_mode mode;
  63. struct v4l2_dv_timings timings;
  64. u8 edid[256];
  65. unsigned edid_blocks;
  66. struct v4l2_fract aspect_ratio;
  67. u32 rgb_quantization_range;
  68. struct workqueue_struct *work_queues;
  69. struct delayed_work delayed_work_enable_hotplug;
  70. bool connector_hdmi;
  71. bool restart_stdi_once;
  72. /* i2c clients */
  73. struct i2c_client *i2c_avlink;
  74. struct i2c_client *i2c_cec;
  75. struct i2c_client *i2c_infoframe;
  76. struct i2c_client *i2c_esdp;
  77. struct i2c_client *i2c_dpp;
  78. struct i2c_client *i2c_afe;
  79. struct i2c_client *i2c_repeater;
  80. struct i2c_client *i2c_edid;
  81. struct i2c_client *i2c_hdmi;
  82. struct i2c_client *i2c_test;
  83. struct i2c_client *i2c_cp;
  84. struct i2c_client *i2c_vdp;
  85. /* controls */
  86. struct v4l2_ctrl *detect_tx_5v_ctrl;
  87. struct v4l2_ctrl *analog_sampling_phase_ctrl;
  88. struct v4l2_ctrl *free_run_color_manual_ctrl;
  89. struct v4l2_ctrl *free_run_color_ctrl;
  90. struct v4l2_ctrl *rgb_quantization_range_ctrl;
  91. };
  92. /* Supported CEA and DMT timings */
  93. static const struct v4l2_dv_timings adv7604_timings[] = {
  94. V4L2_DV_BT_CEA_720X480P59_94,
  95. V4L2_DV_BT_CEA_720X576P50,
  96. V4L2_DV_BT_CEA_1280X720P24,
  97. V4L2_DV_BT_CEA_1280X720P25,
  98. V4L2_DV_BT_CEA_1280X720P50,
  99. V4L2_DV_BT_CEA_1280X720P60,
  100. V4L2_DV_BT_CEA_1920X1080P24,
  101. V4L2_DV_BT_CEA_1920X1080P25,
  102. V4L2_DV_BT_CEA_1920X1080P30,
  103. V4L2_DV_BT_CEA_1920X1080P50,
  104. V4L2_DV_BT_CEA_1920X1080P60,
  105. /* sorted by DMT ID */
  106. V4L2_DV_BT_DMT_640X350P85,
  107. V4L2_DV_BT_DMT_640X400P85,
  108. V4L2_DV_BT_DMT_720X400P85,
  109. V4L2_DV_BT_DMT_640X480P60,
  110. V4L2_DV_BT_DMT_640X480P72,
  111. V4L2_DV_BT_DMT_640X480P75,
  112. V4L2_DV_BT_DMT_640X480P85,
  113. V4L2_DV_BT_DMT_800X600P56,
  114. V4L2_DV_BT_DMT_800X600P60,
  115. V4L2_DV_BT_DMT_800X600P72,
  116. V4L2_DV_BT_DMT_800X600P75,
  117. V4L2_DV_BT_DMT_800X600P85,
  118. V4L2_DV_BT_DMT_848X480P60,
  119. V4L2_DV_BT_DMT_1024X768P60,
  120. V4L2_DV_BT_DMT_1024X768P70,
  121. V4L2_DV_BT_DMT_1024X768P75,
  122. V4L2_DV_BT_DMT_1024X768P85,
  123. V4L2_DV_BT_DMT_1152X864P75,
  124. V4L2_DV_BT_DMT_1280X768P60_RB,
  125. V4L2_DV_BT_DMT_1280X768P60,
  126. V4L2_DV_BT_DMT_1280X768P75,
  127. V4L2_DV_BT_DMT_1280X768P85,
  128. V4L2_DV_BT_DMT_1280X800P60_RB,
  129. V4L2_DV_BT_DMT_1280X800P60,
  130. V4L2_DV_BT_DMT_1280X800P75,
  131. V4L2_DV_BT_DMT_1280X800P85,
  132. V4L2_DV_BT_DMT_1280X960P60,
  133. V4L2_DV_BT_DMT_1280X960P85,
  134. V4L2_DV_BT_DMT_1280X1024P60,
  135. V4L2_DV_BT_DMT_1280X1024P75,
  136. V4L2_DV_BT_DMT_1280X1024P85,
  137. V4L2_DV_BT_DMT_1360X768P60,
  138. V4L2_DV_BT_DMT_1400X1050P60_RB,
  139. V4L2_DV_BT_DMT_1400X1050P60,
  140. V4L2_DV_BT_DMT_1400X1050P75,
  141. V4L2_DV_BT_DMT_1400X1050P85,
  142. V4L2_DV_BT_DMT_1440X900P60_RB,
  143. V4L2_DV_BT_DMT_1440X900P60,
  144. V4L2_DV_BT_DMT_1600X1200P60,
  145. V4L2_DV_BT_DMT_1680X1050P60_RB,
  146. V4L2_DV_BT_DMT_1680X1050P60,
  147. V4L2_DV_BT_DMT_1792X1344P60,
  148. V4L2_DV_BT_DMT_1856X1392P60,
  149. V4L2_DV_BT_DMT_1920X1200P60_RB,
  150. V4L2_DV_BT_DMT_1366X768P60,
  151. V4L2_DV_BT_DMT_1920X1080P60,
  152. { },
  153. };
  154. struct adv7604_video_standards {
  155. struct v4l2_dv_timings timings;
  156. u8 vid_std;
  157. u8 v_freq;
  158. };
  159. /* sorted by number of lines */
  160. static const struct adv7604_video_standards adv7604_prim_mode_comp[] = {
  161. /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
  162. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  163. { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
  164. { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
  165. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  166. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  167. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  168. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  169. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  170. /* TODO add 1920x1080P60_RB (CVT timing) */
  171. { },
  172. };
  173. /* sorted by number of lines */
  174. static const struct adv7604_video_standards adv7604_prim_mode_gr[] = {
  175. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  176. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  177. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  178. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  179. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  180. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  181. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  182. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  183. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  184. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  185. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  186. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  187. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  188. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  189. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  190. { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
  191. { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
  192. { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
  193. { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
  194. { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
  195. /* TODO add 1600X1200P60_RB (not a DMT timing) */
  196. { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
  197. { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
  198. { },
  199. };
  200. /* sorted by number of lines */
  201. static const struct adv7604_video_standards adv7604_prim_mode_hdmi_comp[] = {
  202. { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
  203. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  204. { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
  205. { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
  206. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  207. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  208. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  209. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  210. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  211. { },
  212. };
  213. /* sorted by number of lines */
  214. static const struct adv7604_video_standards adv7604_prim_mode_hdmi_gr[] = {
  215. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  216. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  217. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  218. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  219. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  220. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  221. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  222. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  223. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  224. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  225. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  226. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  227. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  228. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  229. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  230. { },
  231. };
  232. /* ----------------------------------------------------------------------- */
  233. static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
  234. {
  235. return container_of(sd, struct adv7604_state, sd);
  236. }
  237. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  238. {
  239. return &container_of(ctrl->handler, struct adv7604_state, hdl)->sd;
  240. }
  241. static inline unsigned hblanking(const struct v4l2_bt_timings *t)
  242. {
  243. return t->hfrontporch + t->hsync + t->hbackporch;
  244. }
  245. static inline unsigned htotal(const struct v4l2_bt_timings *t)
  246. {
  247. return t->width + t->hfrontporch + t->hsync + t->hbackporch;
  248. }
  249. static inline unsigned vblanking(const struct v4l2_bt_timings *t)
  250. {
  251. return t->vfrontporch + t->vsync + t->vbackporch;
  252. }
  253. static inline unsigned vtotal(const struct v4l2_bt_timings *t)
  254. {
  255. return t->height + t->vfrontporch + t->vsync + t->vbackporch;
  256. }
  257. /* ----------------------------------------------------------------------- */
  258. static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
  259. u8 command, bool check)
  260. {
  261. union i2c_smbus_data data;
  262. if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  263. I2C_SMBUS_READ, command,
  264. I2C_SMBUS_BYTE_DATA, &data))
  265. return data.byte;
  266. if (check)
  267. v4l_err(client, "error reading %02x, %02x\n",
  268. client->addr, command);
  269. return -EIO;
  270. }
  271. static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
  272. {
  273. return adv_smbus_read_byte_data_check(client, command, true);
  274. }
  275. static s32 adv_smbus_write_byte_data(struct i2c_client *client,
  276. u8 command, u8 value)
  277. {
  278. union i2c_smbus_data data;
  279. int err;
  280. int i;
  281. data.byte = value;
  282. for (i = 0; i < 3; i++) {
  283. err = i2c_smbus_xfer(client->adapter, client->addr,
  284. client->flags,
  285. I2C_SMBUS_WRITE, command,
  286. I2C_SMBUS_BYTE_DATA, &data);
  287. if (!err)
  288. break;
  289. }
  290. if (err < 0)
  291. v4l_err(client, "error writing %02x, %02x, %02x\n",
  292. client->addr, command, value);
  293. return err;
  294. }
  295. static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
  296. u8 command, unsigned length, const u8 *values)
  297. {
  298. union i2c_smbus_data data;
  299. if (length > I2C_SMBUS_BLOCK_MAX)
  300. length = I2C_SMBUS_BLOCK_MAX;
  301. data.block[0] = length;
  302. memcpy(data.block + 1, values, length);
  303. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  304. I2C_SMBUS_WRITE, command,
  305. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  306. }
  307. /* ----------------------------------------------------------------------- */
  308. static inline int io_read(struct v4l2_subdev *sd, u8 reg)
  309. {
  310. struct i2c_client *client = v4l2_get_subdevdata(sd);
  311. return adv_smbus_read_byte_data(client, reg);
  312. }
  313. static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  314. {
  315. struct i2c_client *client = v4l2_get_subdevdata(sd);
  316. return adv_smbus_write_byte_data(client, reg, val);
  317. }
  318. static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  319. {
  320. return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
  321. }
  322. static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
  323. {
  324. struct adv7604_state *state = to_state(sd);
  325. return adv_smbus_read_byte_data(state->i2c_avlink, reg);
  326. }
  327. static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  328. {
  329. struct adv7604_state *state = to_state(sd);
  330. return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
  331. }
  332. static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
  333. {
  334. struct adv7604_state *state = to_state(sd);
  335. return adv_smbus_read_byte_data(state->i2c_cec, reg);
  336. }
  337. static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  338. {
  339. struct adv7604_state *state = to_state(sd);
  340. return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
  341. }
  342. static inline int cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  343. {
  344. return cec_write(sd, reg, (cec_read(sd, reg) & mask) | val);
  345. }
  346. static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
  347. {
  348. struct adv7604_state *state = to_state(sd);
  349. return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
  350. }
  351. static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  352. {
  353. struct adv7604_state *state = to_state(sd);
  354. return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
  355. }
  356. static inline int esdp_read(struct v4l2_subdev *sd, u8 reg)
  357. {
  358. struct adv7604_state *state = to_state(sd);
  359. return adv_smbus_read_byte_data(state->i2c_esdp, reg);
  360. }
  361. static inline int esdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  362. {
  363. struct adv7604_state *state = to_state(sd);
  364. return adv_smbus_write_byte_data(state->i2c_esdp, reg, val);
  365. }
  366. static inline int dpp_read(struct v4l2_subdev *sd, u8 reg)
  367. {
  368. struct adv7604_state *state = to_state(sd);
  369. return adv_smbus_read_byte_data(state->i2c_dpp, reg);
  370. }
  371. static inline int dpp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  372. {
  373. struct adv7604_state *state = to_state(sd);
  374. return adv_smbus_write_byte_data(state->i2c_dpp, reg, val);
  375. }
  376. static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
  377. {
  378. struct adv7604_state *state = to_state(sd);
  379. return adv_smbus_read_byte_data(state->i2c_afe, reg);
  380. }
  381. static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  382. {
  383. struct adv7604_state *state = to_state(sd);
  384. return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
  385. }
  386. static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
  387. {
  388. struct adv7604_state *state = to_state(sd);
  389. return adv_smbus_read_byte_data(state->i2c_repeater, reg);
  390. }
  391. static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  392. {
  393. struct adv7604_state *state = to_state(sd);
  394. return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
  395. }
  396. static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  397. {
  398. return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
  399. }
  400. static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
  401. {
  402. struct adv7604_state *state = to_state(sd);
  403. return adv_smbus_read_byte_data(state->i2c_edid, reg);
  404. }
  405. static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  406. {
  407. struct adv7604_state *state = to_state(sd);
  408. return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
  409. }
  410. static inline int edid_read_block(struct v4l2_subdev *sd, unsigned len, u8 *val)
  411. {
  412. struct adv7604_state *state = to_state(sd);
  413. struct i2c_client *client = state->i2c_edid;
  414. u8 msgbuf0[1] = { 0 };
  415. u8 msgbuf1[256];
  416. struct i2c_msg msg[2] = {
  417. {
  418. .addr = client->addr,
  419. .len = 1,
  420. .buf = msgbuf0
  421. },
  422. {
  423. .addr = client->addr,
  424. .flags = I2C_M_RD,
  425. .len = len,
  426. .buf = msgbuf1
  427. },
  428. };
  429. if (i2c_transfer(client->adapter, msg, 2) < 0)
  430. return -EIO;
  431. memcpy(val, msgbuf1, len);
  432. return 0;
  433. }
  434. static void adv7604_delayed_work_enable_hotplug(struct work_struct *work)
  435. {
  436. struct delayed_work *dwork = to_delayed_work(work);
  437. struct adv7604_state *state = container_of(dwork, struct adv7604_state,
  438. delayed_work_enable_hotplug);
  439. struct v4l2_subdev *sd = &state->sd;
  440. v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
  441. v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)1);
  442. }
  443. static inline int edid_write_block(struct v4l2_subdev *sd,
  444. unsigned len, const u8 *val)
  445. {
  446. struct i2c_client *client = v4l2_get_subdevdata(sd);
  447. struct adv7604_state *state = to_state(sd);
  448. int err = 0;
  449. int i;
  450. v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n", __func__, len);
  451. v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)0);
  452. /* Disables I2C access to internal EDID ram from DDC port */
  453. rep_write_and_or(sd, 0x77, 0xf0, 0x0);
  454. for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
  455. err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
  456. I2C_SMBUS_BLOCK_MAX, val + i);
  457. if (err)
  458. return err;
  459. /* adv7604 calculates the checksums and enables I2C access to internal
  460. EDID ram from DDC port. */
  461. rep_write_and_or(sd, 0x77, 0xf0, 0x1);
  462. for (i = 0; i < 1000; i++) {
  463. if (rep_read(sd, 0x7d) & 1)
  464. break;
  465. mdelay(1);
  466. }
  467. if (i == 1000) {
  468. v4l_err(client, "error enabling edid\n");
  469. return -EIO;
  470. }
  471. /* enable hotplug after 100 ms */
  472. queue_delayed_work(state->work_queues,
  473. &state->delayed_work_enable_hotplug, HZ / 10);
  474. return 0;
  475. }
  476. static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
  477. {
  478. struct adv7604_state *state = to_state(sd);
  479. return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
  480. }
  481. static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  482. {
  483. struct adv7604_state *state = to_state(sd);
  484. return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
  485. }
  486. static inline int test_read(struct v4l2_subdev *sd, u8 reg)
  487. {
  488. struct adv7604_state *state = to_state(sd);
  489. return adv_smbus_read_byte_data(state->i2c_test, reg);
  490. }
  491. static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  492. {
  493. struct adv7604_state *state = to_state(sd);
  494. return adv_smbus_write_byte_data(state->i2c_test, reg, val);
  495. }
  496. static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
  497. {
  498. struct adv7604_state *state = to_state(sd);
  499. return adv_smbus_read_byte_data(state->i2c_cp, reg);
  500. }
  501. static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  502. {
  503. struct adv7604_state *state = to_state(sd);
  504. return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
  505. }
  506. static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  507. {
  508. return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
  509. }
  510. static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
  511. {
  512. struct adv7604_state *state = to_state(sd);
  513. return adv_smbus_read_byte_data(state->i2c_vdp, reg);
  514. }
  515. static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  516. {
  517. struct adv7604_state *state = to_state(sd);
  518. return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
  519. }
  520. /* ----------------------------------------------------------------------- */
  521. #ifdef CONFIG_VIDEO_ADV_DEBUG
  522. static void adv7604_inv_register(struct v4l2_subdev *sd)
  523. {
  524. v4l2_info(sd, "0x000-0x0ff: IO Map\n");
  525. v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
  526. v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
  527. v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
  528. v4l2_info(sd, "0x400-0x4ff: ESDP Map\n");
  529. v4l2_info(sd, "0x500-0x5ff: DPP Map\n");
  530. v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
  531. v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
  532. v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
  533. v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
  534. v4l2_info(sd, "0xa00-0xaff: Test Map\n");
  535. v4l2_info(sd, "0xb00-0xbff: CP Map\n");
  536. v4l2_info(sd, "0xc00-0xcff: VDP Map\n");
  537. }
  538. static int adv7604_g_register(struct v4l2_subdev *sd,
  539. struct v4l2_dbg_register *reg)
  540. {
  541. struct i2c_client *client = v4l2_get_subdevdata(sd);
  542. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  543. return -EINVAL;
  544. if (!capable(CAP_SYS_ADMIN))
  545. return -EPERM;
  546. reg->size = 1;
  547. switch (reg->reg >> 8) {
  548. case 0:
  549. reg->val = io_read(sd, reg->reg & 0xff);
  550. break;
  551. case 1:
  552. reg->val = avlink_read(sd, reg->reg & 0xff);
  553. break;
  554. case 2:
  555. reg->val = cec_read(sd, reg->reg & 0xff);
  556. break;
  557. case 3:
  558. reg->val = infoframe_read(sd, reg->reg & 0xff);
  559. break;
  560. case 4:
  561. reg->val = esdp_read(sd, reg->reg & 0xff);
  562. break;
  563. case 5:
  564. reg->val = dpp_read(sd, reg->reg & 0xff);
  565. break;
  566. case 6:
  567. reg->val = afe_read(sd, reg->reg & 0xff);
  568. break;
  569. case 7:
  570. reg->val = rep_read(sd, reg->reg & 0xff);
  571. break;
  572. case 8:
  573. reg->val = edid_read(sd, reg->reg & 0xff);
  574. break;
  575. case 9:
  576. reg->val = hdmi_read(sd, reg->reg & 0xff);
  577. break;
  578. case 0xa:
  579. reg->val = test_read(sd, reg->reg & 0xff);
  580. break;
  581. case 0xb:
  582. reg->val = cp_read(sd, reg->reg & 0xff);
  583. break;
  584. case 0xc:
  585. reg->val = vdp_read(sd, reg->reg & 0xff);
  586. break;
  587. default:
  588. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  589. adv7604_inv_register(sd);
  590. break;
  591. }
  592. return 0;
  593. }
  594. static int adv7604_s_register(struct v4l2_subdev *sd,
  595. struct v4l2_dbg_register *reg)
  596. {
  597. struct i2c_client *client = v4l2_get_subdevdata(sd);
  598. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  599. return -EINVAL;
  600. if (!capable(CAP_SYS_ADMIN))
  601. return -EPERM;
  602. switch (reg->reg >> 8) {
  603. case 0:
  604. io_write(sd, reg->reg & 0xff, reg->val & 0xff);
  605. break;
  606. case 1:
  607. avlink_write(sd, reg->reg & 0xff, reg->val & 0xff);
  608. break;
  609. case 2:
  610. cec_write(sd, reg->reg & 0xff, reg->val & 0xff);
  611. break;
  612. case 3:
  613. infoframe_write(sd, reg->reg & 0xff, reg->val & 0xff);
  614. break;
  615. case 4:
  616. esdp_write(sd, reg->reg & 0xff, reg->val & 0xff);
  617. break;
  618. case 5:
  619. dpp_write(sd, reg->reg & 0xff, reg->val & 0xff);
  620. break;
  621. case 6:
  622. afe_write(sd, reg->reg & 0xff, reg->val & 0xff);
  623. break;
  624. case 7:
  625. rep_write(sd, reg->reg & 0xff, reg->val & 0xff);
  626. break;
  627. case 8:
  628. edid_write(sd, reg->reg & 0xff, reg->val & 0xff);
  629. break;
  630. case 9:
  631. hdmi_write(sd, reg->reg & 0xff, reg->val & 0xff);
  632. break;
  633. case 0xa:
  634. test_write(sd, reg->reg & 0xff, reg->val & 0xff);
  635. break;
  636. case 0xb:
  637. cp_write(sd, reg->reg & 0xff, reg->val & 0xff);
  638. break;
  639. case 0xc:
  640. vdp_write(sd, reg->reg & 0xff, reg->val & 0xff);
  641. break;
  642. default:
  643. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  644. adv7604_inv_register(sd);
  645. break;
  646. }
  647. return 0;
  648. }
  649. #endif
  650. static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
  651. {
  652. struct adv7604_state *state = to_state(sd);
  653. /* port A only */
  654. return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
  655. ((io_read(sd, 0x6f) & 0x10) >> 4));
  656. }
  657. static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
  658. u8 prim_mode,
  659. const struct adv7604_video_standards *predef_vid_timings,
  660. const struct v4l2_dv_timings *timings)
  661. {
  662. struct adv7604_state *state = to_state(sd);
  663. int i;
  664. for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
  665. if (!v4l_match_dv_timings(timings, &predef_vid_timings[i].timings,
  666. DIGITAL_INPUT ? 250000 : 1000000))
  667. continue;
  668. io_write(sd, 0x00, predef_vid_timings[i].vid_std); /* video std */
  669. io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) +
  670. prim_mode); /* v_freq and prim mode */
  671. return 0;
  672. }
  673. return -1;
  674. }
  675. static int configure_predefined_video_timings(struct v4l2_subdev *sd,
  676. struct v4l2_dv_timings *timings)
  677. {
  678. struct adv7604_state *state = to_state(sd);
  679. int err;
  680. v4l2_dbg(1, debug, sd, "%s", __func__);
  681. /* reset to default values */
  682. io_write(sd, 0x16, 0x43);
  683. io_write(sd, 0x17, 0x5a);
  684. /* disable embedded syncs for auto graphics mode */
  685. cp_write_and_or(sd, 0x81, 0xef, 0x00);
  686. cp_write(sd, 0x8f, 0x00);
  687. cp_write(sd, 0x90, 0x00);
  688. cp_write(sd, 0xa2, 0x00);
  689. cp_write(sd, 0xa3, 0x00);
  690. cp_write(sd, 0xa4, 0x00);
  691. cp_write(sd, 0xa5, 0x00);
  692. cp_write(sd, 0xa6, 0x00);
  693. cp_write(sd, 0xa7, 0x00);
  694. cp_write(sd, 0xab, 0x00);
  695. cp_write(sd, 0xac, 0x00);
  696. switch (state->mode) {
  697. case ADV7604_MODE_COMP:
  698. case ADV7604_MODE_GR:
  699. err = find_and_set_predefined_video_timings(sd,
  700. 0x01, adv7604_prim_mode_comp, timings);
  701. if (err)
  702. err = find_and_set_predefined_video_timings(sd,
  703. 0x02, adv7604_prim_mode_gr, timings);
  704. break;
  705. case ADV7604_MODE_HDMI:
  706. err = find_and_set_predefined_video_timings(sd,
  707. 0x05, adv7604_prim_mode_hdmi_comp, timings);
  708. if (err)
  709. err = find_and_set_predefined_video_timings(sd,
  710. 0x06, adv7604_prim_mode_hdmi_gr, timings);
  711. break;
  712. default:
  713. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  714. __func__, state->mode);
  715. err = -1;
  716. break;
  717. }
  718. return err;
  719. }
  720. static void configure_custom_video_timings(struct v4l2_subdev *sd,
  721. const struct v4l2_bt_timings *bt)
  722. {
  723. struct adv7604_state *state = to_state(sd);
  724. struct i2c_client *client = v4l2_get_subdevdata(sd);
  725. u32 width = htotal(bt);
  726. u32 height = vtotal(bt);
  727. u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
  728. u16 cp_start_eav = width - bt->hfrontporch;
  729. u16 cp_start_vbi = height - bt->vfrontporch;
  730. u16 cp_end_vbi = bt->vsync + bt->vbackporch;
  731. u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
  732. ((width * (ADV7604_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
  733. const u8 pll[2] = {
  734. 0xc0 | ((width >> 8) & 0x1f),
  735. width & 0xff
  736. };
  737. v4l2_dbg(2, debug, sd, "%s\n", __func__);
  738. switch (state->mode) {
  739. case ADV7604_MODE_COMP:
  740. case ADV7604_MODE_GR:
  741. /* auto graphics */
  742. io_write(sd, 0x00, 0x07); /* video std */
  743. io_write(sd, 0x01, 0x02); /* prim mode */
  744. /* enable embedded syncs for auto graphics mode */
  745. cp_write_and_or(sd, 0x81, 0xef, 0x10);
  746. /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
  747. /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
  748. /* IO-map reg. 0x16 and 0x17 should be written in sequence */
  749. if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll)) {
  750. v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
  751. break;
  752. }
  753. /* active video - horizontal timing */
  754. cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
  755. cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) |
  756. ((cp_start_eav >> 8) & 0x0f));
  757. cp_write(sd, 0xa4, cp_start_eav & 0xff);
  758. /* active video - vertical timing */
  759. cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
  760. cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
  761. ((cp_end_vbi >> 8) & 0xf));
  762. cp_write(sd, 0xa7, cp_end_vbi & 0xff);
  763. break;
  764. case ADV7604_MODE_HDMI:
  765. /* set default prim_mode/vid_std for HDMI
  766. accoring to [REF_03, c. 4.2] */
  767. io_write(sd, 0x00, 0x02); /* video std */
  768. io_write(sd, 0x01, 0x06); /* prim mode */
  769. break;
  770. default:
  771. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  772. __func__, state->mode);
  773. break;
  774. }
  775. cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
  776. cp_write(sd, 0x90, ch1_fr_ll & 0xff);
  777. cp_write(sd, 0xab, (height >> 4) & 0xff);
  778. cp_write(sd, 0xac, (height & 0x0f) << 4);
  779. }
  780. static void set_rgb_quantization_range(struct v4l2_subdev *sd)
  781. {
  782. struct adv7604_state *state = to_state(sd);
  783. switch (state->rgb_quantization_range) {
  784. case V4L2_DV_RGB_RANGE_AUTO:
  785. /* automatic */
  786. if (DIGITAL_INPUT && !(hdmi_read(sd, 0x05) & 0x80)) {
  787. /* receiving DVI-D signal */
  788. /* ADV7604 selects RGB limited range regardless of
  789. input format (CE/IT) in automatic mode */
  790. if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
  791. /* RGB limited range (16-235) */
  792. io_write_and_or(sd, 0x02, 0x0f, 0x00);
  793. } else {
  794. /* RGB full range (0-255) */
  795. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  796. }
  797. } else {
  798. /* receiving HDMI or analog signal, set automode */
  799. io_write_and_or(sd, 0x02, 0x0f, 0xf0);
  800. }
  801. break;
  802. case V4L2_DV_RGB_RANGE_LIMITED:
  803. /* RGB limited range (16-235) */
  804. io_write_and_or(sd, 0x02, 0x0f, 0x00);
  805. break;
  806. case V4L2_DV_RGB_RANGE_FULL:
  807. /* RGB full range (0-255) */
  808. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  809. break;
  810. }
  811. }
  812. static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
  813. {
  814. struct v4l2_subdev *sd = to_sd(ctrl);
  815. struct adv7604_state *state = to_state(sd);
  816. switch (ctrl->id) {
  817. case V4L2_CID_BRIGHTNESS:
  818. cp_write(sd, 0x3c, ctrl->val);
  819. return 0;
  820. case V4L2_CID_CONTRAST:
  821. cp_write(sd, 0x3a, ctrl->val);
  822. return 0;
  823. case V4L2_CID_SATURATION:
  824. cp_write(sd, 0x3b, ctrl->val);
  825. return 0;
  826. case V4L2_CID_HUE:
  827. cp_write(sd, 0x3d, ctrl->val);
  828. return 0;
  829. case V4L2_CID_DV_RX_RGB_RANGE:
  830. state->rgb_quantization_range = ctrl->val;
  831. set_rgb_quantization_range(sd);
  832. return 0;
  833. case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
  834. /* Set the analog sampling phase. This is needed to find the
  835. best sampling phase for analog video: an application or
  836. driver has to try a number of phases and analyze the picture
  837. quality before settling on the best performing phase. */
  838. afe_write(sd, 0xc8, ctrl->val);
  839. return 0;
  840. case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
  841. /* Use the default blue color for free running mode,
  842. or supply your own. */
  843. cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
  844. return 0;
  845. case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
  846. cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
  847. cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
  848. cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
  849. return 0;
  850. }
  851. return -EINVAL;
  852. }
  853. static int adv7604_g_chip_ident(struct v4l2_subdev *sd,
  854. struct v4l2_dbg_chip_ident *chip)
  855. {
  856. struct i2c_client *client = v4l2_get_subdevdata(sd);
  857. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7604, 0);
  858. }
  859. /* ----------------------------------------------------------------------- */
  860. static inline bool no_power(struct v4l2_subdev *sd)
  861. {
  862. /* Entire chip or CP powered off */
  863. return io_read(sd, 0x0c) & 0x24;
  864. }
  865. static inline bool no_signal_tmds(struct v4l2_subdev *sd)
  866. {
  867. /* TODO port B, C and D */
  868. return !(io_read(sd, 0x6a) & 0x10);
  869. }
  870. static inline bool no_lock_tmds(struct v4l2_subdev *sd)
  871. {
  872. return (io_read(sd, 0x6a) & 0xe0) != 0xe0;
  873. }
  874. static inline bool no_lock_sspd(struct v4l2_subdev *sd)
  875. {
  876. /* TODO channel 2 */
  877. return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
  878. }
  879. static inline bool no_lock_stdi(struct v4l2_subdev *sd)
  880. {
  881. /* TODO channel 2 */
  882. return !(cp_read(sd, 0xb1) & 0x80);
  883. }
  884. static inline bool no_signal(struct v4l2_subdev *sd)
  885. {
  886. struct adv7604_state *state = to_state(sd);
  887. bool ret;
  888. ret = no_power(sd);
  889. ret |= no_lock_stdi(sd);
  890. ret |= no_lock_sspd(sd);
  891. if (DIGITAL_INPUT) {
  892. ret |= no_lock_tmds(sd);
  893. ret |= no_signal_tmds(sd);
  894. }
  895. return ret;
  896. }
  897. static inline bool no_lock_cp(struct v4l2_subdev *sd)
  898. {
  899. /* CP has detected a non standard number of lines on the incoming
  900. video compared to what it is configured to receive by s_dv_timings */
  901. return io_read(sd, 0x12) & 0x01;
  902. }
  903. static int adv7604_g_input_status(struct v4l2_subdev *sd, u32 *status)
  904. {
  905. struct adv7604_state *state = to_state(sd);
  906. *status = 0;
  907. *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
  908. *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
  909. if (no_lock_cp(sd))
  910. *status |= DIGITAL_INPUT ? V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
  911. v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
  912. return 0;
  913. }
  914. /* ----------------------------------------------------------------------- */
  915. static void adv7604_print_timings(struct v4l2_subdev *sd,
  916. struct v4l2_dv_timings *timings, const char *txt, bool detailed)
  917. {
  918. struct v4l2_bt_timings *bt = &timings->bt;
  919. u32 htot, vtot;
  920. if (timings->type != V4L2_DV_BT_656_1120)
  921. return;
  922. htot = htotal(bt);
  923. vtot = vtotal(bt);
  924. v4l2_info(sd, "%s %dx%d%s%d (%dx%d)",
  925. txt, bt->width, bt->height, bt->interlaced ? "i" : "p",
  926. (htot * vtot) > 0 ? ((u32)bt->pixelclock /
  927. (htot * vtot)) : 0,
  928. htot, vtot);
  929. if (detailed) {
  930. v4l2_info(sd, " horizontal: fp = %d, %ssync = %d, bp = %d\n",
  931. bt->hfrontporch,
  932. (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
  933. bt->hsync, bt->hbackporch);
  934. v4l2_info(sd, " vertical: fp = %d, %ssync = %d, bp = %d\n",
  935. bt->vfrontporch,
  936. (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
  937. bt->vsync, bt->vbackporch);
  938. v4l2_info(sd, " pixelclock: %lld, flags: 0x%x, standards: 0x%x\n",
  939. bt->pixelclock, bt->flags, bt->standards);
  940. }
  941. }
  942. struct stdi_readback {
  943. u16 bl, lcf, lcvs;
  944. u8 hs_pol, vs_pol;
  945. bool interlaced;
  946. };
  947. static int stdi2dv_timings(struct v4l2_subdev *sd,
  948. struct stdi_readback *stdi,
  949. struct v4l2_dv_timings *timings)
  950. {
  951. struct adv7604_state *state = to_state(sd);
  952. u32 hfreq = (ADV7604_fsc * 8) / stdi->bl;
  953. u32 pix_clk;
  954. int i;
  955. for (i = 0; adv7604_timings[i].bt.height; i++) {
  956. if (vtotal(&adv7604_timings[i].bt) != stdi->lcf + 1)
  957. continue;
  958. if (adv7604_timings[i].bt.vsync != stdi->lcvs)
  959. continue;
  960. pix_clk = hfreq * htotal(&adv7604_timings[i].bt);
  961. if ((pix_clk < adv7604_timings[i].bt.pixelclock + 1000000) &&
  962. (pix_clk > adv7604_timings[i].bt.pixelclock - 1000000)) {
  963. *timings = adv7604_timings[i];
  964. return 0;
  965. }
  966. }
  967. if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
  968. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  969. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  970. timings))
  971. return 0;
  972. if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
  973. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  974. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  975. state->aspect_ratio, timings))
  976. return 0;
  977. v4l2_dbg(2, debug, sd,
  978. "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
  979. __func__, stdi->lcvs, stdi->lcf, stdi->bl,
  980. stdi->hs_pol, stdi->vs_pol);
  981. return -1;
  982. }
  983. static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
  984. {
  985. if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
  986. v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
  987. return -1;
  988. }
  989. /* read STDI */
  990. stdi->bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
  991. stdi->lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
  992. stdi->lcvs = cp_read(sd, 0xb3) >> 3;
  993. stdi->interlaced = io_read(sd, 0x12) & 0x10;
  994. /* read SSPD */
  995. if ((cp_read(sd, 0xb5) & 0x03) == 0x01) {
  996. stdi->hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
  997. ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
  998. stdi->vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
  999. ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
  1000. } else {
  1001. stdi->hs_pol = 'x';
  1002. stdi->vs_pol = 'x';
  1003. }
  1004. if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
  1005. v4l2_dbg(2, debug, sd,
  1006. "%s: signal lost during readout of STDI/SSPD\n", __func__);
  1007. return -1;
  1008. }
  1009. if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
  1010. v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
  1011. memset(stdi, 0, sizeof(struct stdi_readback));
  1012. return -1;
  1013. }
  1014. v4l2_dbg(2, debug, sd,
  1015. "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
  1016. __func__, stdi->lcf, stdi->bl, stdi->lcvs,
  1017. stdi->hs_pol, stdi->vs_pol,
  1018. stdi->interlaced ? "interlaced" : "progressive");
  1019. return 0;
  1020. }
  1021. static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
  1022. struct v4l2_enum_dv_timings *timings)
  1023. {
  1024. if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1)
  1025. return -EINVAL;
  1026. memset(timings->reserved, 0, sizeof(timings->reserved));
  1027. timings->timings = adv7604_timings[timings->index];
  1028. return 0;
  1029. }
  1030. static int adv7604_dv_timings_cap(struct v4l2_subdev *sd,
  1031. struct v4l2_dv_timings_cap *cap)
  1032. {
  1033. struct adv7604_state *state = to_state(sd);
  1034. cap->type = V4L2_DV_BT_656_1120;
  1035. cap->bt.max_width = 1920;
  1036. cap->bt.max_height = 1200;
  1037. cap->bt.min_pixelclock = 27000000;
  1038. if (DIGITAL_INPUT)
  1039. cap->bt.max_pixelclock = 225000000;
  1040. else
  1041. cap->bt.max_pixelclock = 170000000;
  1042. cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  1043. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
  1044. cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
  1045. V4L2_DV_BT_CAP_REDUCED_BLANKING | V4L2_DV_BT_CAP_CUSTOM;
  1046. return 0;
  1047. }
  1048. /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
  1049. if the format is listed in adv7604_timings[] */
  1050. static void adv7604_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
  1051. struct v4l2_dv_timings *timings)
  1052. {
  1053. struct adv7604_state *state = to_state(sd);
  1054. int i;
  1055. for (i = 0; adv7604_timings[i].bt.width; i++) {
  1056. if (v4l_match_dv_timings(timings, &adv7604_timings[i],
  1057. DIGITAL_INPUT ? 250000 : 1000000)) {
  1058. *timings = adv7604_timings[i];
  1059. break;
  1060. }
  1061. }
  1062. }
  1063. static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
  1064. struct v4l2_dv_timings *timings)
  1065. {
  1066. struct adv7604_state *state = to_state(sd);
  1067. struct v4l2_bt_timings *bt = &timings->bt;
  1068. struct stdi_readback stdi;
  1069. if (!timings)
  1070. return -EINVAL;
  1071. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1072. if (no_signal(sd)) {
  1073. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  1074. return -ENOLINK;
  1075. }
  1076. /* read STDI */
  1077. if (read_stdi(sd, &stdi)) {
  1078. v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
  1079. return -ENOLINK;
  1080. }
  1081. bt->interlaced = stdi.interlaced ?
  1082. V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
  1083. if (DIGITAL_INPUT) {
  1084. timings->type = V4L2_DV_BT_656_1120;
  1085. bt->width = (hdmi_read(sd, 0x07) & 0x0f) * 256 + hdmi_read(sd, 0x08);
  1086. bt->height = (hdmi_read(sd, 0x09) & 0x0f) * 256 + hdmi_read(sd, 0x0a);
  1087. bt->pixelclock = (hdmi_read(sd, 0x06) * 1000000) +
  1088. ((hdmi_read(sd, 0x3b) & 0x30) >> 4) * 250000;
  1089. bt->hfrontporch = (hdmi_read(sd, 0x20) & 0x03) * 256 +
  1090. hdmi_read(sd, 0x21);
  1091. bt->hsync = (hdmi_read(sd, 0x22) & 0x03) * 256 +
  1092. hdmi_read(sd, 0x23);
  1093. bt->hbackporch = (hdmi_read(sd, 0x24) & 0x03) * 256 +
  1094. hdmi_read(sd, 0x25);
  1095. bt->vfrontporch = ((hdmi_read(sd, 0x2a) & 0x1f) * 256 +
  1096. hdmi_read(sd, 0x2b)) / 2;
  1097. bt->vsync = ((hdmi_read(sd, 0x2e) & 0x1f) * 256 +
  1098. hdmi_read(sd, 0x2f)) / 2;
  1099. bt->vbackporch = ((hdmi_read(sd, 0x32) & 0x1f) * 256 +
  1100. hdmi_read(sd, 0x33)) / 2;
  1101. bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
  1102. ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
  1103. if (bt->interlaced == V4L2_DV_INTERLACED) {
  1104. bt->height += (hdmi_read(sd, 0x0b) & 0x0f) * 256 +
  1105. hdmi_read(sd, 0x0c);
  1106. bt->il_vfrontporch = ((hdmi_read(sd, 0x2c) & 0x1f) * 256 +
  1107. hdmi_read(sd, 0x2d)) / 2;
  1108. bt->il_vsync = ((hdmi_read(sd, 0x30) & 0x1f) * 256 +
  1109. hdmi_read(sd, 0x31)) / 2;
  1110. bt->vbackporch = ((hdmi_read(sd, 0x34) & 0x1f) * 256 +
  1111. hdmi_read(sd, 0x35)) / 2;
  1112. }
  1113. adv7604_fill_optional_dv_timings_fields(sd, timings);
  1114. } else {
  1115. /* find format
  1116. * Since LCVS values are inaccurate [REF_03, p. 275-276],
  1117. * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
  1118. */
  1119. if (!stdi2dv_timings(sd, &stdi, timings))
  1120. goto found;
  1121. stdi.lcvs += 1;
  1122. v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
  1123. if (!stdi2dv_timings(sd, &stdi, timings))
  1124. goto found;
  1125. stdi.lcvs -= 2;
  1126. v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
  1127. if (stdi2dv_timings(sd, &stdi, timings)) {
  1128. /*
  1129. * The STDI block may measure wrong values, especially
  1130. * for lcvs and lcf. If the driver can not find any
  1131. * valid timing, the STDI block is restarted to measure
  1132. * the video timings again. The function will return an
  1133. * error, but the restart of STDI will generate a new
  1134. * STDI interrupt and the format detection process will
  1135. * restart.
  1136. */
  1137. if (state->restart_stdi_once) {
  1138. v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
  1139. /* TODO restart STDI for Sync Channel 2 */
  1140. /* enter one-shot mode */
  1141. cp_write_and_or(sd, 0x86, 0xf9, 0x00);
  1142. /* trigger STDI restart */
  1143. cp_write_and_or(sd, 0x86, 0xf9, 0x04);
  1144. /* reset to continuous mode */
  1145. cp_write_and_or(sd, 0x86, 0xf9, 0x02);
  1146. state->restart_stdi_once = false;
  1147. return -ENOLINK;
  1148. }
  1149. v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
  1150. return -ERANGE;
  1151. }
  1152. state->restart_stdi_once = true;
  1153. }
  1154. found:
  1155. if (no_signal(sd)) {
  1156. v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
  1157. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1158. return -ENOLINK;
  1159. }
  1160. if ((!DIGITAL_INPUT && bt->pixelclock > 170000000) ||
  1161. (DIGITAL_INPUT && bt->pixelclock > 225000000)) {
  1162. v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
  1163. __func__, (u32)bt->pixelclock);
  1164. return -ERANGE;
  1165. }
  1166. if (debug > 1)
  1167. adv7604_print_timings(sd, timings,
  1168. "adv7604_query_dv_timings:", true);
  1169. return 0;
  1170. }
  1171. static int adv7604_s_dv_timings(struct v4l2_subdev *sd,
  1172. struct v4l2_dv_timings *timings)
  1173. {
  1174. struct adv7604_state *state = to_state(sd);
  1175. struct v4l2_bt_timings *bt;
  1176. int err;
  1177. if (!timings)
  1178. return -EINVAL;
  1179. bt = &timings->bt;
  1180. if ((!DIGITAL_INPUT && bt->pixelclock > 170000000) ||
  1181. (DIGITAL_INPUT && bt->pixelclock > 225000000)) {
  1182. v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
  1183. __func__, (u32)bt->pixelclock);
  1184. return -ERANGE;
  1185. }
  1186. adv7604_fill_optional_dv_timings_fields(sd, timings);
  1187. state->timings = *timings;
  1188. cp_write(sd, 0x91, bt->interlaced ? 0x50 : 0x10);
  1189. /* Use prim_mode and vid_std when available */
  1190. err = configure_predefined_video_timings(sd, timings);
  1191. if (err) {
  1192. /* custom settings when the video format
  1193. does not have prim_mode/vid_std */
  1194. configure_custom_video_timings(sd, bt);
  1195. }
  1196. set_rgb_quantization_range(sd);
  1197. if (debug > 1)
  1198. adv7604_print_timings(sd, timings,
  1199. "adv7604_s_dv_timings:", true);
  1200. return 0;
  1201. }
  1202. static int adv7604_g_dv_timings(struct v4l2_subdev *sd,
  1203. struct v4l2_dv_timings *timings)
  1204. {
  1205. struct adv7604_state *state = to_state(sd);
  1206. *timings = state->timings;
  1207. return 0;
  1208. }
  1209. static void enable_input(struct v4l2_subdev *sd)
  1210. {
  1211. struct adv7604_state *state = to_state(sd);
  1212. switch (state->mode) {
  1213. case ADV7604_MODE_COMP:
  1214. case ADV7604_MODE_GR:
  1215. /* enable */
  1216. io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
  1217. break;
  1218. case ADV7604_MODE_HDMI:
  1219. /* enable */
  1220. hdmi_write(sd, 0x1a, 0x0a); /* Unmute audio */
  1221. hdmi_write(sd, 0x01, 0x00); /* Enable HDMI clock terminators */
  1222. io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
  1223. break;
  1224. default:
  1225. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  1226. __func__, state->mode);
  1227. break;
  1228. }
  1229. }
  1230. static void disable_input(struct v4l2_subdev *sd)
  1231. {
  1232. /* disable */
  1233. io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
  1234. hdmi_write(sd, 0x1a, 0x1a); /* Mute audio */
  1235. hdmi_write(sd, 0x01, 0x78); /* Disable HDMI clock terminators */
  1236. }
  1237. static void select_input(struct v4l2_subdev *sd)
  1238. {
  1239. struct adv7604_state *state = to_state(sd);
  1240. switch (state->mode) {
  1241. case ADV7604_MODE_COMP:
  1242. case ADV7604_MODE_GR:
  1243. /* reset ADI recommended settings for HDMI: */
  1244. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
  1245. hdmi_write(sd, 0x0d, 0x04); /* HDMI filter optimization */
  1246. hdmi_write(sd, 0x3d, 0x00); /* DDC bus active pull-up control */
  1247. hdmi_write(sd, 0x3e, 0x74); /* TMDS PLL optimization */
  1248. hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */
  1249. hdmi_write(sd, 0x57, 0x74); /* TMDS PLL optimization */
  1250. hdmi_write(sd, 0x58, 0x63); /* TMDS PLL optimization */
  1251. hdmi_write(sd, 0x8d, 0x18); /* equaliser */
  1252. hdmi_write(sd, 0x8e, 0x34); /* equaliser */
  1253. hdmi_write(sd, 0x93, 0x88); /* equaliser */
  1254. hdmi_write(sd, 0x94, 0x2e); /* equaliser */
  1255. hdmi_write(sd, 0x96, 0x00); /* enable automatic EQ changing */
  1256. afe_write(sd, 0x00, 0x08); /* power up ADC */
  1257. afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
  1258. afe_write(sd, 0xc8, 0x00); /* phase control */
  1259. /* set ADI recommended settings for digitizer */
  1260. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
  1261. afe_write(sd, 0x12, 0x7b); /* ADC noise shaping filter controls */
  1262. afe_write(sd, 0x0c, 0x1f); /* CP core gain controls */
  1263. cp_write(sd, 0x3e, 0x04); /* CP core pre-gain control */
  1264. cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
  1265. cp_write(sd, 0x40, 0x5c); /* CP core pre-gain control. Graphics mode */
  1266. break;
  1267. case ADV7604_MODE_HDMI:
  1268. /* set ADI recommended settings for HDMI: */
  1269. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
  1270. hdmi_write(sd, 0x0d, 0x84); /* HDMI filter optimization */
  1271. hdmi_write(sd, 0x3d, 0x10); /* DDC bus active pull-up control */
  1272. hdmi_write(sd, 0x3e, 0x39); /* TMDS PLL optimization */
  1273. hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */
  1274. hdmi_write(sd, 0x57, 0xb6); /* TMDS PLL optimization */
  1275. hdmi_write(sd, 0x58, 0x03); /* TMDS PLL optimization */
  1276. hdmi_write(sd, 0x8d, 0x18); /* equaliser */
  1277. hdmi_write(sd, 0x8e, 0x34); /* equaliser */
  1278. hdmi_write(sd, 0x93, 0x8b); /* equaliser */
  1279. hdmi_write(sd, 0x94, 0x2d); /* equaliser */
  1280. hdmi_write(sd, 0x96, 0x01); /* enable automatic EQ changing */
  1281. afe_write(sd, 0x00, 0xff); /* power down ADC */
  1282. afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
  1283. afe_write(sd, 0xc8, 0x40); /* phase control */
  1284. /* reset ADI recommended settings for digitizer */
  1285. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
  1286. afe_write(sd, 0x12, 0xfb); /* ADC noise shaping filter controls */
  1287. afe_write(sd, 0x0c, 0x0d); /* CP core gain controls */
  1288. cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
  1289. cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
  1290. cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
  1291. break;
  1292. default:
  1293. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  1294. __func__, state->mode);
  1295. break;
  1296. }
  1297. }
  1298. static int adv7604_s_routing(struct v4l2_subdev *sd,
  1299. u32 input, u32 output, u32 config)
  1300. {
  1301. struct adv7604_state *state = to_state(sd);
  1302. v4l2_dbg(2, debug, sd, "%s: input %d", __func__, input);
  1303. state->mode = input;
  1304. disable_input(sd);
  1305. select_input(sd);
  1306. enable_input(sd);
  1307. return 0;
  1308. }
  1309. static int adv7604_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
  1310. enum v4l2_mbus_pixelcode *code)
  1311. {
  1312. if (index)
  1313. return -EINVAL;
  1314. /* Good enough for now */
  1315. *code = V4L2_MBUS_FMT_FIXED;
  1316. return 0;
  1317. }
  1318. static int adv7604_g_mbus_fmt(struct v4l2_subdev *sd,
  1319. struct v4l2_mbus_framefmt *fmt)
  1320. {
  1321. struct adv7604_state *state = to_state(sd);
  1322. fmt->width = state->timings.bt.width;
  1323. fmt->height = state->timings.bt.height;
  1324. fmt->code = V4L2_MBUS_FMT_FIXED;
  1325. fmt->field = V4L2_FIELD_NONE;
  1326. if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
  1327. fmt->colorspace = (state->timings.bt.height <= 576) ?
  1328. V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
  1329. }
  1330. return 0;
  1331. }
  1332. static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  1333. {
  1334. struct adv7604_state *state = to_state(sd);
  1335. u8 fmt_change, fmt_change_digital, tx_5v;
  1336. /* format change */
  1337. fmt_change = io_read(sd, 0x43) & 0x98;
  1338. if (fmt_change)
  1339. io_write(sd, 0x44, fmt_change);
  1340. fmt_change_digital = DIGITAL_INPUT ? (io_read(sd, 0x6b) & 0xc0) : 0;
  1341. if (fmt_change_digital)
  1342. io_write(sd, 0x6c, fmt_change_digital);
  1343. if (fmt_change || fmt_change_digital) {
  1344. v4l2_dbg(1, debug, sd,
  1345. "%s: ADV7604_FMT_CHANGE, fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
  1346. __func__, fmt_change, fmt_change_digital);
  1347. v4l2_subdev_notify(sd, ADV7604_FMT_CHANGE, NULL);
  1348. if (handled)
  1349. *handled = true;
  1350. }
  1351. /* tx 5v detect */
  1352. tx_5v = io_read(sd, 0x70) & 0x10;
  1353. if (tx_5v) {
  1354. v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
  1355. io_write(sd, 0x71, tx_5v);
  1356. adv7604_s_detect_tx_5v_ctrl(sd);
  1357. if (handled)
  1358. *handled = true;
  1359. }
  1360. return 0;
  1361. }
  1362. static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
  1363. {
  1364. struct adv7604_state *state = to_state(sd);
  1365. if (edid->pad != 0)
  1366. return -EINVAL;
  1367. if (edid->blocks == 0)
  1368. return -EINVAL;
  1369. if (edid->start_block >= state->edid_blocks)
  1370. return -EINVAL;
  1371. if (edid->start_block + edid->blocks > state->edid_blocks)
  1372. edid->blocks = state->edid_blocks - edid->start_block;
  1373. if (!edid->edid)
  1374. return -EINVAL;
  1375. memcpy(edid->edid + edid->start_block * 128,
  1376. state->edid + edid->start_block * 128,
  1377. edid->blocks * 128);
  1378. return 0;
  1379. }
  1380. static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
  1381. {
  1382. struct adv7604_state *state = to_state(sd);
  1383. int err;
  1384. if (edid->pad != 0)
  1385. return -EINVAL;
  1386. if (edid->start_block != 0)
  1387. return -EINVAL;
  1388. if (edid->blocks == 0) {
  1389. /* Pull down the hotplug pin */
  1390. v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)0);
  1391. /* Disables I2C access to internal EDID ram from DDC port */
  1392. rep_write_and_or(sd, 0x77, 0xf0, 0x0);
  1393. state->edid_blocks = 0;
  1394. /* Fall back to a 16:9 aspect ratio */
  1395. state->aspect_ratio.numerator = 16;
  1396. state->aspect_ratio.denominator = 9;
  1397. return 0;
  1398. }
  1399. if (edid->blocks > 2)
  1400. return -E2BIG;
  1401. if (!edid->edid)
  1402. return -EINVAL;
  1403. memcpy(state->edid, edid->edid, 128 * edid->blocks);
  1404. state->edid_blocks = edid->blocks;
  1405. state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
  1406. edid->edid[0x16]);
  1407. err = edid_write_block(sd, 128 * edid->blocks, state->edid);
  1408. if (err < 0)
  1409. v4l2_err(sd, "error %d writing edid\n", err);
  1410. return err;
  1411. }
  1412. /*********** avi info frame CEA-861-E **************/
  1413. static void print_avi_infoframe(struct v4l2_subdev *sd)
  1414. {
  1415. int i;
  1416. u8 buf[14];
  1417. u8 avi_len;
  1418. u8 avi_ver;
  1419. if (!(hdmi_read(sd, 0x05) & 0x80)) {
  1420. v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
  1421. return;
  1422. }
  1423. if (!(io_read(sd, 0x60) & 0x01)) {
  1424. v4l2_info(sd, "AVI infoframe not received\n");
  1425. return;
  1426. }
  1427. if (io_read(sd, 0x83) & 0x01) {
  1428. v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
  1429. io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
  1430. if (io_read(sd, 0x83) & 0x01) {
  1431. v4l2_info(sd, "AVI infoframe checksum error still present\n");
  1432. io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
  1433. }
  1434. }
  1435. avi_len = infoframe_read(sd, 0xe2);
  1436. avi_ver = infoframe_read(sd, 0xe1);
  1437. v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
  1438. avi_ver, avi_len);
  1439. if (avi_ver != 0x02)
  1440. return;
  1441. for (i = 0; i < 14; i++)
  1442. buf[i] = infoframe_read(sd, i);
  1443. v4l2_info(sd,
  1444. "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1445. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
  1446. buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
  1447. }
  1448. static int adv7604_log_status(struct v4l2_subdev *sd)
  1449. {
  1450. struct adv7604_state *state = to_state(sd);
  1451. struct v4l2_dv_timings timings;
  1452. struct stdi_readback stdi;
  1453. u8 reg_io_0x02 = io_read(sd, 0x02);
  1454. char *csc_coeff_sel_rb[16] = {
  1455. "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
  1456. "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
  1457. "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
  1458. "reserved", "reserved", "reserved", "reserved", "manual"
  1459. };
  1460. char *input_color_space_txt[16] = {
  1461. "RGB limited range (16-235)", "RGB full range (0-255)",
  1462. "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
  1463. "XvYCC Bt.601", "XvYCC Bt.709",
  1464. "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
  1465. "invalid", "invalid", "invalid", "invalid", "invalid",
  1466. "invalid", "invalid", "automatic"
  1467. };
  1468. char *rgb_quantization_range_txt[] = {
  1469. "Automatic",
  1470. "RGB limited range (16-235)",
  1471. "RGB full range (0-255)",
  1472. };
  1473. v4l2_info(sd, "-----Chip status-----\n");
  1474. v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
  1475. v4l2_info(sd, "Connector type: %s\n", state->connector_hdmi ?
  1476. "HDMI" : (DIGITAL_INPUT ? "DVI-D" : "DVI-A"));
  1477. v4l2_info(sd, "EDID: %s\n", ((rep_read(sd, 0x7d) & 0x01) &&
  1478. (rep_read(sd, 0x77) & 0x01)) ? "enabled" : "disabled ");
  1479. v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
  1480. "enabled" : "disabled");
  1481. v4l2_info(sd, "-----Signal status-----\n");
  1482. v4l2_info(sd, "Cable detected (+5V power): %s\n",
  1483. (io_read(sd, 0x6f) & 0x10) ? "true" : "false");
  1484. v4l2_info(sd, "TMDS signal detected: %s\n",
  1485. no_signal_tmds(sd) ? "false" : "true");
  1486. v4l2_info(sd, "TMDS signal locked: %s\n",
  1487. no_lock_tmds(sd) ? "false" : "true");
  1488. v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
  1489. v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
  1490. v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
  1491. v4l2_info(sd, "CP free run: %s\n",
  1492. (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
  1493. v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
  1494. io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
  1495. (io_read(sd, 0x01) & 0x70) >> 4);
  1496. v4l2_info(sd, "-----Video Timings-----\n");
  1497. if (read_stdi(sd, &stdi))
  1498. v4l2_info(sd, "STDI: not locked\n");
  1499. else
  1500. v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
  1501. stdi.lcf, stdi.bl, stdi.lcvs,
  1502. stdi.interlaced ? "interlaced" : "progressive",
  1503. stdi.hs_pol, stdi.vs_pol);
  1504. if (adv7604_query_dv_timings(sd, &timings))
  1505. v4l2_info(sd, "No video detected\n");
  1506. else
  1507. adv7604_print_timings(sd, &timings, "Detected format:", true);
  1508. adv7604_print_timings(sd, &state->timings, "Configured format:", true);
  1509. v4l2_info(sd, "-----Color space-----\n");
  1510. v4l2_info(sd, "RGB quantization range ctrl: %s\n",
  1511. rgb_quantization_range_txt[state->rgb_quantization_range]);
  1512. v4l2_info(sd, "Input color space: %s\n",
  1513. input_color_space_txt[reg_io_0x02 >> 4]);
  1514. v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
  1515. (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
  1516. (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
  1517. ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
  1518. "enabled" : "disabled");
  1519. v4l2_info(sd, "Color space conversion: %s\n",
  1520. csc_coeff_sel_rb[cp_read(sd, 0xfc) >> 4]);
  1521. /* Digital video */
  1522. if (DIGITAL_INPUT) {
  1523. v4l2_info(sd, "-----HDMI status-----\n");
  1524. v4l2_info(sd, "HDCP encrypted content: %s\n",
  1525. hdmi_read(sd, 0x05) & 0x40 ? "true" : "false");
  1526. print_avi_infoframe(sd);
  1527. }
  1528. return 0;
  1529. }
  1530. /* ----------------------------------------------------------------------- */
  1531. static const struct v4l2_ctrl_ops adv7604_ctrl_ops = {
  1532. .s_ctrl = adv7604_s_ctrl,
  1533. };
  1534. static const struct v4l2_subdev_core_ops adv7604_core_ops = {
  1535. .log_status = adv7604_log_status,
  1536. .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
  1537. .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
  1538. .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
  1539. .g_ctrl = v4l2_subdev_g_ctrl,
  1540. .s_ctrl = v4l2_subdev_s_ctrl,
  1541. .queryctrl = v4l2_subdev_queryctrl,
  1542. .querymenu = v4l2_subdev_querymenu,
  1543. .g_chip_ident = adv7604_g_chip_ident,
  1544. .interrupt_service_routine = adv7604_isr,
  1545. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1546. .g_register = adv7604_g_register,
  1547. .s_register = adv7604_s_register,
  1548. #endif
  1549. };
  1550. static const struct v4l2_subdev_video_ops adv7604_video_ops = {
  1551. .s_routing = adv7604_s_routing,
  1552. .g_input_status = adv7604_g_input_status,
  1553. .s_dv_timings = adv7604_s_dv_timings,
  1554. .g_dv_timings = adv7604_g_dv_timings,
  1555. .query_dv_timings = adv7604_query_dv_timings,
  1556. .enum_dv_timings = adv7604_enum_dv_timings,
  1557. .dv_timings_cap = adv7604_dv_timings_cap,
  1558. .enum_mbus_fmt = adv7604_enum_mbus_fmt,
  1559. .g_mbus_fmt = adv7604_g_mbus_fmt,
  1560. .try_mbus_fmt = adv7604_g_mbus_fmt,
  1561. .s_mbus_fmt = adv7604_g_mbus_fmt,
  1562. };
  1563. static const struct v4l2_subdev_pad_ops adv7604_pad_ops = {
  1564. .get_edid = adv7604_get_edid,
  1565. .set_edid = adv7604_set_edid,
  1566. };
  1567. static const struct v4l2_subdev_ops adv7604_ops = {
  1568. .core = &adv7604_core_ops,
  1569. .video = &adv7604_video_ops,
  1570. .pad = &adv7604_pad_ops,
  1571. };
  1572. /* -------------------------- custom ctrls ---------------------------------- */
  1573. static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
  1574. .ops = &adv7604_ctrl_ops,
  1575. .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
  1576. .name = "Analog Sampling Phase",
  1577. .type = V4L2_CTRL_TYPE_INTEGER,
  1578. .min = 0,
  1579. .max = 0x1f,
  1580. .step = 1,
  1581. .def = 0,
  1582. };
  1583. static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color_manual = {
  1584. .ops = &adv7604_ctrl_ops,
  1585. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
  1586. .name = "Free Running Color, Manual",
  1587. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1588. .min = false,
  1589. .max = true,
  1590. .step = 1,
  1591. .def = false,
  1592. };
  1593. static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color = {
  1594. .ops = &adv7604_ctrl_ops,
  1595. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
  1596. .name = "Free Running Color",
  1597. .type = V4L2_CTRL_TYPE_INTEGER,
  1598. .min = 0x0,
  1599. .max = 0xffffff,
  1600. .step = 0x1,
  1601. .def = 0x0,
  1602. };
  1603. /* ----------------------------------------------------------------------- */
  1604. static int adv7604_core_init(struct v4l2_subdev *sd)
  1605. {
  1606. struct adv7604_state *state = to_state(sd);
  1607. struct adv7604_platform_data *pdata = &state->pdata;
  1608. hdmi_write(sd, 0x48,
  1609. (pdata->disable_pwrdnb ? 0x80 : 0) |
  1610. (pdata->disable_cable_det_rst ? 0x40 : 0));
  1611. disable_input(sd);
  1612. /* power */
  1613. io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
  1614. io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
  1615. cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
  1616. /* video format */
  1617. io_write_and_or(sd, 0x02, 0xf0,
  1618. pdata->alt_gamma << 3 |
  1619. pdata->op_656_range << 2 |
  1620. pdata->rgb_out << 1 |
  1621. pdata->alt_data_sat << 0);
  1622. io_write(sd, 0x03, pdata->op_format_sel);
  1623. io_write_and_or(sd, 0x04, 0x1f, pdata->op_ch_sel << 5);
  1624. io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
  1625. pdata->insert_av_codes << 2 |
  1626. pdata->replicate_av_codes << 1 |
  1627. pdata->invert_cbcr << 0);
  1628. /* TODO from platform data */
  1629. cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
  1630. io_write(sd, 0x06, 0xa6); /* positive VS and HS */
  1631. io_write(sd, 0x14, 0x7f); /* Drive strength adjusted to max */
  1632. cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
  1633. cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
  1634. cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
  1635. ADI recommended setting [REF_01, c. 2.3.3] */
  1636. cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
  1637. ADI recommended setting [REF_01, c. 2.3.3] */
  1638. cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
  1639. for digital formats */
  1640. /* TODO from platform data */
  1641. afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
  1642. afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
  1643. io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
  1644. /* interrupts */
  1645. io_write(sd, 0x40, 0xc2); /* Configure INT1 */
  1646. io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
  1647. io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
  1648. io_write(sd, 0x6e, 0xc0); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
  1649. io_write(sd, 0x73, 0x10); /* Enable CABLE_DET_A_ST (+5v) interrupt */
  1650. return v4l2_ctrl_handler_setup(sd->ctrl_handler);
  1651. }
  1652. static void adv7604_unregister_clients(struct adv7604_state *state)
  1653. {
  1654. if (state->i2c_avlink)
  1655. i2c_unregister_device(state->i2c_avlink);
  1656. if (state->i2c_cec)
  1657. i2c_unregister_device(state->i2c_cec);
  1658. if (state->i2c_infoframe)
  1659. i2c_unregister_device(state->i2c_infoframe);
  1660. if (state->i2c_esdp)
  1661. i2c_unregister_device(state->i2c_esdp);
  1662. if (state->i2c_dpp)
  1663. i2c_unregister_device(state->i2c_dpp);
  1664. if (state->i2c_afe)
  1665. i2c_unregister_device(state->i2c_afe);
  1666. if (state->i2c_repeater)
  1667. i2c_unregister_device(state->i2c_repeater);
  1668. if (state->i2c_edid)
  1669. i2c_unregister_device(state->i2c_edid);
  1670. if (state->i2c_hdmi)
  1671. i2c_unregister_device(state->i2c_hdmi);
  1672. if (state->i2c_test)
  1673. i2c_unregister_device(state->i2c_test);
  1674. if (state->i2c_cp)
  1675. i2c_unregister_device(state->i2c_cp);
  1676. if (state->i2c_vdp)
  1677. i2c_unregister_device(state->i2c_vdp);
  1678. }
  1679. static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
  1680. u8 addr, u8 io_reg)
  1681. {
  1682. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1683. if (addr)
  1684. io_write(sd, io_reg, addr << 1);
  1685. return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
  1686. }
  1687. static int adv7604_probe(struct i2c_client *client,
  1688. const struct i2c_device_id *id)
  1689. {
  1690. struct adv7604_state *state;
  1691. struct adv7604_platform_data *pdata = client->dev.platform_data;
  1692. struct v4l2_ctrl_handler *hdl;
  1693. struct v4l2_subdev *sd;
  1694. int err;
  1695. /* Check if the adapter supports the needed features */
  1696. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1697. return -EIO;
  1698. v4l_dbg(1, debug, client, "detecting adv7604 client on address 0x%x\n",
  1699. client->addr << 1);
  1700. state = kzalloc(sizeof(struct adv7604_state), GFP_KERNEL);
  1701. if (!state) {
  1702. v4l_err(client, "Could not allocate adv7604_state memory!\n");
  1703. return -ENOMEM;
  1704. }
  1705. /* platform data */
  1706. if (!pdata) {
  1707. v4l_err(client, "No platform data!\n");
  1708. err = -ENODEV;
  1709. goto err_state;
  1710. }
  1711. memcpy(&state->pdata, pdata, sizeof(state->pdata));
  1712. sd = &state->sd;
  1713. v4l2_i2c_subdev_init(sd, client, &adv7604_ops);
  1714. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1715. state->connector_hdmi = pdata->connector_hdmi;
  1716. /* i2c access to adv7604? */
  1717. if (adv_smbus_read_byte_data_check(client, 0xfb, false) != 0x68) {
  1718. v4l2_info(sd, "not an adv7604 on address 0x%x\n",
  1719. client->addr << 1);
  1720. err = -ENODEV;
  1721. goto err_state;
  1722. }
  1723. /* control handlers */
  1724. hdl = &state->hdl;
  1725. v4l2_ctrl_handler_init(hdl, 9);
  1726. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  1727. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  1728. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  1729. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  1730. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  1731. V4L2_CID_SATURATION, 0, 255, 1, 128);
  1732. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  1733. V4L2_CID_HUE, 0, 128, 1, 0);
  1734. /* private controls */
  1735. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  1736. V4L2_CID_DV_RX_POWER_PRESENT, 0, 1, 0, 0);
  1737. state->detect_tx_5v_ctrl->is_private = true;
  1738. state->rgb_quantization_range_ctrl =
  1739. v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops,
  1740. V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
  1741. 0, V4L2_DV_RGB_RANGE_AUTO);
  1742. state->rgb_quantization_range_ctrl->is_private = true;
  1743. /* custom controls */
  1744. state->analog_sampling_phase_ctrl =
  1745. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
  1746. state->analog_sampling_phase_ctrl->is_private = true;
  1747. state->free_run_color_manual_ctrl =
  1748. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL);
  1749. state->free_run_color_manual_ctrl->is_private = true;
  1750. state->free_run_color_ctrl =
  1751. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color, NULL);
  1752. state->free_run_color_ctrl->is_private = true;
  1753. sd->ctrl_handler = hdl;
  1754. if (hdl->error) {
  1755. err = hdl->error;
  1756. goto err_hdl;
  1757. }
  1758. if (adv7604_s_detect_tx_5v_ctrl(sd)) {
  1759. err = -ENODEV;
  1760. goto err_hdl;
  1761. }
  1762. state->i2c_avlink = adv7604_dummy_client(sd, pdata->i2c_avlink, 0xf3);
  1763. state->i2c_cec = adv7604_dummy_client(sd, pdata->i2c_cec, 0xf4);
  1764. state->i2c_infoframe = adv7604_dummy_client(sd, pdata->i2c_infoframe, 0xf5);
  1765. state->i2c_esdp = adv7604_dummy_client(sd, pdata->i2c_esdp, 0xf6);
  1766. state->i2c_dpp = adv7604_dummy_client(sd, pdata->i2c_dpp, 0xf7);
  1767. state->i2c_afe = adv7604_dummy_client(sd, pdata->i2c_afe, 0xf8);
  1768. state->i2c_repeater = adv7604_dummy_client(sd, pdata->i2c_repeater, 0xf9);
  1769. state->i2c_edid = adv7604_dummy_client(sd, pdata->i2c_edid, 0xfa);
  1770. state->i2c_hdmi = adv7604_dummy_client(sd, pdata->i2c_hdmi, 0xfb);
  1771. state->i2c_test = adv7604_dummy_client(sd, pdata->i2c_test, 0xfc);
  1772. state->i2c_cp = adv7604_dummy_client(sd, pdata->i2c_cp, 0xfd);
  1773. state->i2c_vdp = adv7604_dummy_client(sd, pdata->i2c_vdp, 0xfe);
  1774. if (!state->i2c_avlink || !state->i2c_cec || !state->i2c_infoframe ||
  1775. !state->i2c_esdp || !state->i2c_dpp || !state->i2c_afe ||
  1776. !state->i2c_repeater || !state->i2c_edid || !state->i2c_hdmi ||
  1777. !state->i2c_test || !state->i2c_cp || !state->i2c_vdp) {
  1778. err = -ENOMEM;
  1779. v4l2_err(sd, "failed to create all i2c clients\n");
  1780. goto err_i2c;
  1781. }
  1782. state->restart_stdi_once = true;
  1783. /* work queues */
  1784. state->work_queues = create_singlethread_workqueue(client->name);
  1785. if (!state->work_queues) {
  1786. v4l2_err(sd, "Could not create work queue\n");
  1787. err = -ENOMEM;
  1788. goto err_i2c;
  1789. }
  1790. INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
  1791. adv7604_delayed_work_enable_hotplug);
  1792. state->pad.flags = MEDIA_PAD_FL_SOURCE;
  1793. err = media_entity_init(&sd->entity, 1, &state->pad, 0);
  1794. if (err)
  1795. goto err_work_queues;
  1796. err = adv7604_core_init(sd);
  1797. if (err)
  1798. goto err_entity;
  1799. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  1800. client->addr << 1, client->adapter->name);
  1801. return 0;
  1802. err_entity:
  1803. media_entity_cleanup(&sd->entity);
  1804. err_work_queues:
  1805. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  1806. destroy_workqueue(state->work_queues);
  1807. err_i2c:
  1808. adv7604_unregister_clients(state);
  1809. err_hdl:
  1810. v4l2_ctrl_handler_free(hdl);
  1811. err_state:
  1812. kfree(state);
  1813. return err;
  1814. }
  1815. /* ----------------------------------------------------------------------- */
  1816. static int adv7604_remove(struct i2c_client *client)
  1817. {
  1818. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1819. struct adv7604_state *state = to_state(sd);
  1820. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  1821. destroy_workqueue(state->work_queues);
  1822. v4l2_device_unregister_subdev(sd);
  1823. media_entity_cleanup(&sd->entity);
  1824. adv7604_unregister_clients(to_state(sd));
  1825. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1826. kfree(to_state(sd));
  1827. return 0;
  1828. }
  1829. /* ----------------------------------------------------------------------- */
  1830. static struct i2c_device_id adv7604_id[] = {
  1831. { "adv7604", 0 },
  1832. { }
  1833. };
  1834. MODULE_DEVICE_TABLE(i2c, adv7604_id);
  1835. static struct i2c_driver adv7604_driver = {
  1836. .driver = {
  1837. .owner = THIS_MODULE,
  1838. .name = "adv7604",
  1839. },
  1840. .probe = adv7604_probe,
  1841. .remove = adv7604_remove,
  1842. .id_table = adv7604_id,
  1843. };
  1844. module_i2c_driver(adv7604_driver);