adv7604.c 61 KB

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