adv7604.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  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-dv-timings.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 V4L2_DV_BT_BLANKING_WIDTH(t);
  244. }
  245. static inline unsigned htotal(const struct v4l2_bt_timings *t)
  246. {
  247. return V4L2_DV_BT_FRAME_WIDTH(t);
  248. }
  249. static inline unsigned vblanking(const struct v4l2_bt_timings *t)
  250. {
  251. return V4L2_DV_BT_BLANKING_HEIGHT(t);
  252. }
  253. static inline unsigned vtotal(const struct v4l2_bt_timings *t)
  254. {
  255. return V4L2_DV_BT_FRAME_HEIGHT(t);
  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. reg->size = 1;
  542. switch (reg->reg >> 8) {
  543. case 0:
  544. reg->val = io_read(sd, reg->reg & 0xff);
  545. break;
  546. case 1:
  547. reg->val = avlink_read(sd, reg->reg & 0xff);
  548. break;
  549. case 2:
  550. reg->val = cec_read(sd, reg->reg & 0xff);
  551. break;
  552. case 3:
  553. reg->val = infoframe_read(sd, reg->reg & 0xff);
  554. break;
  555. case 4:
  556. reg->val = esdp_read(sd, reg->reg & 0xff);
  557. break;
  558. case 5:
  559. reg->val = dpp_read(sd, reg->reg & 0xff);
  560. break;
  561. case 6:
  562. reg->val = afe_read(sd, reg->reg & 0xff);
  563. break;
  564. case 7:
  565. reg->val = rep_read(sd, reg->reg & 0xff);
  566. break;
  567. case 8:
  568. reg->val = edid_read(sd, reg->reg & 0xff);
  569. break;
  570. case 9:
  571. reg->val = hdmi_read(sd, reg->reg & 0xff);
  572. break;
  573. case 0xa:
  574. reg->val = test_read(sd, reg->reg & 0xff);
  575. break;
  576. case 0xb:
  577. reg->val = cp_read(sd, reg->reg & 0xff);
  578. break;
  579. case 0xc:
  580. reg->val = vdp_read(sd, reg->reg & 0xff);
  581. break;
  582. default:
  583. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  584. adv7604_inv_register(sd);
  585. break;
  586. }
  587. return 0;
  588. }
  589. static int adv7604_s_register(struct v4l2_subdev *sd,
  590. const struct v4l2_dbg_register *reg)
  591. {
  592. switch (reg->reg >> 8) {
  593. case 0:
  594. io_write(sd, reg->reg & 0xff, reg->val & 0xff);
  595. break;
  596. case 1:
  597. avlink_write(sd, reg->reg & 0xff, reg->val & 0xff);
  598. break;
  599. case 2:
  600. cec_write(sd, reg->reg & 0xff, reg->val & 0xff);
  601. break;
  602. case 3:
  603. infoframe_write(sd, reg->reg & 0xff, reg->val & 0xff);
  604. break;
  605. case 4:
  606. esdp_write(sd, reg->reg & 0xff, reg->val & 0xff);
  607. break;
  608. case 5:
  609. dpp_write(sd, reg->reg & 0xff, reg->val & 0xff);
  610. break;
  611. case 6:
  612. afe_write(sd, reg->reg & 0xff, reg->val & 0xff);
  613. break;
  614. case 7:
  615. rep_write(sd, reg->reg & 0xff, reg->val & 0xff);
  616. break;
  617. case 8:
  618. edid_write(sd, reg->reg & 0xff, reg->val & 0xff);
  619. break;
  620. case 9:
  621. hdmi_write(sd, reg->reg & 0xff, reg->val & 0xff);
  622. break;
  623. case 0xa:
  624. test_write(sd, reg->reg & 0xff, reg->val & 0xff);
  625. break;
  626. case 0xb:
  627. cp_write(sd, reg->reg & 0xff, reg->val & 0xff);
  628. break;
  629. case 0xc:
  630. vdp_write(sd, reg->reg & 0xff, reg->val & 0xff);
  631. break;
  632. default:
  633. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  634. adv7604_inv_register(sd);
  635. break;
  636. }
  637. return 0;
  638. }
  639. #endif
  640. static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
  641. {
  642. struct adv7604_state *state = to_state(sd);
  643. /* port A only */
  644. return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
  645. ((io_read(sd, 0x6f) & 0x10) >> 4));
  646. }
  647. static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
  648. u8 prim_mode,
  649. const struct adv7604_video_standards *predef_vid_timings,
  650. const struct v4l2_dv_timings *timings)
  651. {
  652. struct adv7604_state *state = to_state(sd);
  653. int i;
  654. for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
  655. if (!v4l_match_dv_timings(timings, &predef_vid_timings[i].timings,
  656. DIGITAL_INPUT ? 250000 : 1000000))
  657. continue;
  658. io_write(sd, 0x00, predef_vid_timings[i].vid_std); /* video std */
  659. io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) +
  660. prim_mode); /* v_freq and prim mode */
  661. return 0;
  662. }
  663. return -1;
  664. }
  665. static int configure_predefined_video_timings(struct v4l2_subdev *sd,
  666. struct v4l2_dv_timings *timings)
  667. {
  668. struct adv7604_state *state = to_state(sd);
  669. int err;
  670. v4l2_dbg(1, debug, sd, "%s", __func__);
  671. /* reset to default values */
  672. io_write(sd, 0x16, 0x43);
  673. io_write(sd, 0x17, 0x5a);
  674. /* disable embedded syncs for auto graphics mode */
  675. cp_write_and_or(sd, 0x81, 0xef, 0x00);
  676. cp_write(sd, 0x8f, 0x00);
  677. cp_write(sd, 0x90, 0x00);
  678. cp_write(sd, 0xa2, 0x00);
  679. cp_write(sd, 0xa3, 0x00);
  680. cp_write(sd, 0xa4, 0x00);
  681. cp_write(sd, 0xa5, 0x00);
  682. cp_write(sd, 0xa6, 0x00);
  683. cp_write(sd, 0xa7, 0x00);
  684. cp_write(sd, 0xab, 0x00);
  685. cp_write(sd, 0xac, 0x00);
  686. switch (state->mode) {
  687. case ADV7604_MODE_COMP:
  688. case ADV7604_MODE_GR:
  689. err = find_and_set_predefined_video_timings(sd,
  690. 0x01, adv7604_prim_mode_comp, timings);
  691. if (err)
  692. err = find_and_set_predefined_video_timings(sd,
  693. 0x02, adv7604_prim_mode_gr, timings);
  694. break;
  695. case ADV7604_MODE_HDMI:
  696. err = find_and_set_predefined_video_timings(sd,
  697. 0x05, adv7604_prim_mode_hdmi_comp, timings);
  698. if (err)
  699. err = find_and_set_predefined_video_timings(sd,
  700. 0x06, adv7604_prim_mode_hdmi_gr, timings);
  701. break;
  702. default:
  703. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  704. __func__, state->mode);
  705. err = -1;
  706. break;
  707. }
  708. return err;
  709. }
  710. static void configure_custom_video_timings(struct v4l2_subdev *sd,
  711. const struct v4l2_bt_timings *bt)
  712. {
  713. struct adv7604_state *state = to_state(sd);
  714. struct i2c_client *client = v4l2_get_subdevdata(sd);
  715. u32 width = htotal(bt);
  716. u32 height = vtotal(bt);
  717. u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
  718. u16 cp_start_eav = width - bt->hfrontporch;
  719. u16 cp_start_vbi = height - bt->vfrontporch;
  720. u16 cp_end_vbi = bt->vsync + bt->vbackporch;
  721. u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
  722. ((width * (ADV7604_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
  723. const u8 pll[2] = {
  724. 0xc0 | ((width >> 8) & 0x1f),
  725. width & 0xff
  726. };
  727. v4l2_dbg(2, debug, sd, "%s\n", __func__);
  728. switch (state->mode) {
  729. case ADV7604_MODE_COMP:
  730. case ADV7604_MODE_GR:
  731. /* auto graphics */
  732. io_write(sd, 0x00, 0x07); /* video std */
  733. io_write(sd, 0x01, 0x02); /* prim mode */
  734. /* enable embedded syncs for auto graphics mode */
  735. cp_write_and_or(sd, 0x81, 0xef, 0x10);
  736. /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
  737. /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
  738. /* IO-map reg. 0x16 and 0x17 should be written in sequence */
  739. if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll)) {
  740. v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
  741. break;
  742. }
  743. /* active video - horizontal timing */
  744. cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
  745. cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) |
  746. ((cp_start_eav >> 8) & 0x0f));
  747. cp_write(sd, 0xa4, cp_start_eav & 0xff);
  748. /* active video - vertical timing */
  749. cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
  750. cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
  751. ((cp_end_vbi >> 8) & 0xf));
  752. cp_write(sd, 0xa7, cp_end_vbi & 0xff);
  753. break;
  754. case ADV7604_MODE_HDMI:
  755. /* set default prim_mode/vid_std for HDMI
  756. accoring to [REF_03, c. 4.2] */
  757. io_write(sd, 0x00, 0x02); /* video std */
  758. io_write(sd, 0x01, 0x06); /* prim mode */
  759. break;
  760. default:
  761. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  762. __func__, state->mode);
  763. break;
  764. }
  765. cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
  766. cp_write(sd, 0x90, ch1_fr_ll & 0xff);
  767. cp_write(sd, 0xab, (height >> 4) & 0xff);
  768. cp_write(sd, 0xac, (height & 0x0f) << 4);
  769. }
  770. static void set_rgb_quantization_range(struct v4l2_subdev *sd)
  771. {
  772. struct adv7604_state *state = to_state(sd);
  773. switch (state->rgb_quantization_range) {
  774. case V4L2_DV_RGB_RANGE_AUTO:
  775. /* automatic */
  776. if (DIGITAL_INPUT && !(hdmi_read(sd, 0x05) & 0x80)) {
  777. /* receiving DVI-D signal */
  778. /* ADV7604 selects RGB limited range regardless of
  779. input format (CE/IT) in automatic mode */
  780. if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
  781. /* RGB limited range (16-235) */
  782. io_write_and_or(sd, 0x02, 0x0f, 0x00);
  783. } else {
  784. /* RGB full range (0-255) */
  785. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  786. }
  787. } else {
  788. /* receiving HDMI or analog signal, set automode */
  789. io_write_and_or(sd, 0x02, 0x0f, 0xf0);
  790. }
  791. break;
  792. case V4L2_DV_RGB_RANGE_LIMITED:
  793. /* RGB limited range (16-235) */
  794. io_write_and_or(sd, 0x02, 0x0f, 0x00);
  795. break;
  796. case V4L2_DV_RGB_RANGE_FULL:
  797. /* RGB full range (0-255) */
  798. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  799. break;
  800. }
  801. }
  802. static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
  803. {
  804. struct v4l2_subdev *sd = to_sd(ctrl);
  805. struct adv7604_state *state = to_state(sd);
  806. switch (ctrl->id) {
  807. case V4L2_CID_BRIGHTNESS:
  808. cp_write(sd, 0x3c, ctrl->val);
  809. return 0;
  810. case V4L2_CID_CONTRAST:
  811. cp_write(sd, 0x3a, ctrl->val);
  812. return 0;
  813. case V4L2_CID_SATURATION:
  814. cp_write(sd, 0x3b, ctrl->val);
  815. return 0;
  816. case V4L2_CID_HUE:
  817. cp_write(sd, 0x3d, ctrl->val);
  818. return 0;
  819. case V4L2_CID_DV_RX_RGB_RANGE:
  820. state->rgb_quantization_range = ctrl->val;
  821. set_rgb_quantization_range(sd);
  822. return 0;
  823. case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
  824. /* Set the analog sampling phase. This is needed to find the
  825. best sampling phase for analog video: an application or
  826. driver has to try a number of phases and analyze the picture
  827. quality before settling on the best performing phase. */
  828. afe_write(sd, 0xc8, ctrl->val);
  829. return 0;
  830. case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
  831. /* Use the default blue color for free running mode,
  832. or supply your own. */
  833. cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
  834. return 0;
  835. case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
  836. cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
  837. cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
  838. cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
  839. return 0;
  840. }
  841. return -EINVAL;
  842. }
  843. /* ----------------------------------------------------------------------- */
  844. static inline bool no_power(struct v4l2_subdev *sd)
  845. {
  846. /* Entire chip or CP powered off */
  847. return io_read(sd, 0x0c) & 0x24;
  848. }
  849. static inline bool no_signal_tmds(struct v4l2_subdev *sd)
  850. {
  851. /* TODO port B, C and D */
  852. return !(io_read(sd, 0x6a) & 0x10);
  853. }
  854. static inline bool no_lock_tmds(struct v4l2_subdev *sd)
  855. {
  856. return (io_read(sd, 0x6a) & 0xe0) != 0xe0;
  857. }
  858. static inline bool no_lock_sspd(struct v4l2_subdev *sd)
  859. {
  860. /* TODO channel 2 */
  861. return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
  862. }
  863. static inline bool no_lock_stdi(struct v4l2_subdev *sd)
  864. {
  865. /* TODO channel 2 */
  866. return !(cp_read(sd, 0xb1) & 0x80);
  867. }
  868. static inline bool no_signal(struct v4l2_subdev *sd)
  869. {
  870. struct adv7604_state *state = to_state(sd);
  871. bool ret;
  872. ret = no_power(sd);
  873. ret |= no_lock_stdi(sd);
  874. ret |= no_lock_sspd(sd);
  875. if (DIGITAL_INPUT) {
  876. ret |= no_lock_tmds(sd);
  877. ret |= no_signal_tmds(sd);
  878. }
  879. return ret;
  880. }
  881. static inline bool no_lock_cp(struct v4l2_subdev *sd)
  882. {
  883. /* CP has detected a non standard number of lines on the incoming
  884. video compared to what it is configured to receive by s_dv_timings */
  885. return io_read(sd, 0x12) & 0x01;
  886. }
  887. static int adv7604_g_input_status(struct v4l2_subdev *sd, u32 *status)
  888. {
  889. struct adv7604_state *state = to_state(sd);
  890. *status = 0;
  891. *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
  892. *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
  893. if (no_lock_cp(sd))
  894. *status |= DIGITAL_INPUT ? V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
  895. v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
  896. return 0;
  897. }
  898. /* ----------------------------------------------------------------------- */
  899. static void adv7604_print_timings(struct v4l2_subdev *sd,
  900. struct v4l2_dv_timings *timings, const char *txt, bool detailed)
  901. {
  902. struct v4l2_bt_timings *bt = &timings->bt;
  903. u32 htot, vtot;
  904. if (timings->type != V4L2_DV_BT_656_1120)
  905. return;
  906. htot = htotal(bt);
  907. vtot = vtotal(bt);
  908. v4l2_info(sd, "%s %dx%d%s%d (%dx%d)",
  909. txt, bt->width, bt->height, bt->interlaced ? "i" : "p",
  910. (htot * vtot) > 0 ? ((u32)bt->pixelclock /
  911. (htot * vtot)) : 0,
  912. htot, vtot);
  913. if (detailed) {
  914. v4l2_info(sd, " horizontal: fp = %d, %ssync = %d, bp = %d\n",
  915. bt->hfrontporch,
  916. (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
  917. bt->hsync, bt->hbackporch);
  918. v4l2_info(sd, " vertical: fp = %d, %ssync = %d, bp = %d\n",
  919. bt->vfrontporch,
  920. (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
  921. bt->vsync, bt->vbackporch);
  922. v4l2_info(sd, " pixelclock: %lld, flags: 0x%x, standards: 0x%x\n",
  923. bt->pixelclock, bt->flags, bt->standards);
  924. }
  925. }
  926. struct stdi_readback {
  927. u16 bl, lcf, lcvs;
  928. u8 hs_pol, vs_pol;
  929. bool interlaced;
  930. };
  931. static int stdi2dv_timings(struct v4l2_subdev *sd,
  932. struct stdi_readback *stdi,
  933. struct v4l2_dv_timings *timings)
  934. {
  935. struct adv7604_state *state = to_state(sd);
  936. u32 hfreq = (ADV7604_fsc * 8) / stdi->bl;
  937. u32 pix_clk;
  938. int i;
  939. for (i = 0; adv7604_timings[i].bt.height; i++) {
  940. if (vtotal(&adv7604_timings[i].bt) != stdi->lcf + 1)
  941. continue;
  942. if (adv7604_timings[i].bt.vsync != stdi->lcvs)
  943. continue;
  944. pix_clk = hfreq * htotal(&adv7604_timings[i].bt);
  945. if ((pix_clk < adv7604_timings[i].bt.pixelclock + 1000000) &&
  946. (pix_clk > adv7604_timings[i].bt.pixelclock - 1000000)) {
  947. *timings = adv7604_timings[i];
  948. return 0;
  949. }
  950. }
  951. if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
  952. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  953. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  954. timings))
  955. return 0;
  956. if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
  957. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  958. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  959. state->aspect_ratio, timings))
  960. return 0;
  961. v4l2_dbg(2, debug, sd,
  962. "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
  963. __func__, stdi->lcvs, stdi->lcf, stdi->bl,
  964. stdi->hs_pol, stdi->vs_pol);
  965. return -1;
  966. }
  967. static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
  968. {
  969. if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
  970. v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
  971. return -1;
  972. }
  973. /* read STDI */
  974. stdi->bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
  975. stdi->lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
  976. stdi->lcvs = cp_read(sd, 0xb3) >> 3;
  977. stdi->interlaced = io_read(sd, 0x12) & 0x10;
  978. /* read SSPD */
  979. if ((cp_read(sd, 0xb5) & 0x03) == 0x01) {
  980. stdi->hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
  981. ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
  982. stdi->vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
  983. ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
  984. } else {
  985. stdi->hs_pol = 'x';
  986. stdi->vs_pol = 'x';
  987. }
  988. if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
  989. v4l2_dbg(2, debug, sd,
  990. "%s: signal lost during readout of STDI/SSPD\n", __func__);
  991. return -1;
  992. }
  993. if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
  994. v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
  995. memset(stdi, 0, sizeof(struct stdi_readback));
  996. return -1;
  997. }
  998. v4l2_dbg(2, debug, sd,
  999. "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
  1000. __func__, stdi->lcf, stdi->bl, stdi->lcvs,
  1001. stdi->hs_pol, stdi->vs_pol,
  1002. stdi->interlaced ? "interlaced" : "progressive");
  1003. return 0;
  1004. }
  1005. static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
  1006. struct v4l2_enum_dv_timings *timings)
  1007. {
  1008. if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1)
  1009. return -EINVAL;
  1010. memset(timings->reserved, 0, sizeof(timings->reserved));
  1011. timings->timings = adv7604_timings[timings->index];
  1012. return 0;
  1013. }
  1014. static int adv7604_dv_timings_cap(struct v4l2_subdev *sd,
  1015. struct v4l2_dv_timings_cap *cap)
  1016. {
  1017. struct adv7604_state *state = to_state(sd);
  1018. cap->type = V4L2_DV_BT_656_1120;
  1019. cap->bt.max_width = 1920;
  1020. cap->bt.max_height = 1200;
  1021. cap->bt.min_pixelclock = 27000000;
  1022. if (DIGITAL_INPUT)
  1023. cap->bt.max_pixelclock = 225000000;
  1024. else
  1025. cap->bt.max_pixelclock = 170000000;
  1026. cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  1027. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
  1028. cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
  1029. V4L2_DV_BT_CAP_REDUCED_BLANKING | V4L2_DV_BT_CAP_CUSTOM;
  1030. return 0;
  1031. }
  1032. /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
  1033. if the format is listed in adv7604_timings[] */
  1034. static void adv7604_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
  1035. struct v4l2_dv_timings *timings)
  1036. {
  1037. struct adv7604_state *state = to_state(sd);
  1038. int i;
  1039. for (i = 0; adv7604_timings[i].bt.width; i++) {
  1040. if (v4l_match_dv_timings(timings, &adv7604_timings[i],
  1041. DIGITAL_INPUT ? 250000 : 1000000)) {
  1042. *timings = adv7604_timings[i];
  1043. break;
  1044. }
  1045. }
  1046. }
  1047. static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
  1048. struct v4l2_dv_timings *timings)
  1049. {
  1050. struct adv7604_state *state = to_state(sd);
  1051. struct v4l2_bt_timings *bt = &timings->bt;
  1052. struct stdi_readback stdi;
  1053. if (!timings)
  1054. return -EINVAL;
  1055. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1056. if (no_signal(sd)) {
  1057. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  1058. return -ENOLINK;
  1059. }
  1060. /* read STDI */
  1061. if (read_stdi(sd, &stdi)) {
  1062. v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
  1063. return -ENOLINK;
  1064. }
  1065. bt->interlaced = stdi.interlaced ?
  1066. V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
  1067. if (DIGITAL_INPUT) {
  1068. timings->type = V4L2_DV_BT_656_1120;
  1069. bt->width = (hdmi_read(sd, 0x07) & 0x0f) * 256 + hdmi_read(sd, 0x08);
  1070. bt->height = (hdmi_read(sd, 0x09) & 0x0f) * 256 + hdmi_read(sd, 0x0a);
  1071. bt->pixelclock = (hdmi_read(sd, 0x06) * 1000000) +
  1072. ((hdmi_read(sd, 0x3b) & 0x30) >> 4) * 250000;
  1073. bt->hfrontporch = (hdmi_read(sd, 0x20) & 0x03) * 256 +
  1074. hdmi_read(sd, 0x21);
  1075. bt->hsync = (hdmi_read(sd, 0x22) & 0x03) * 256 +
  1076. hdmi_read(sd, 0x23);
  1077. bt->hbackporch = (hdmi_read(sd, 0x24) & 0x03) * 256 +
  1078. hdmi_read(sd, 0x25);
  1079. bt->vfrontporch = ((hdmi_read(sd, 0x2a) & 0x1f) * 256 +
  1080. hdmi_read(sd, 0x2b)) / 2;
  1081. bt->vsync = ((hdmi_read(sd, 0x2e) & 0x1f) * 256 +
  1082. hdmi_read(sd, 0x2f)) / 2;
  1083. bt->vbackporch = ((hdmi_read(sd, 0x32) & 0x1f) * 256 +
  1084. hdmi_read(sd, 0x33)) / 2;
  1085. bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
  1086. ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
  1087. if (bt->interlaced == V4L2_DV_INTERLACED) {
  1088. bt->height += (hdmi_read(sd, 0x0b) & 0x0f) * 256 +
  1089. hdmi_read(sd, 0x0c);
  1090. bt->il_vfrontporch = ((hdmi_read(sd, 0x2c) & 0x1f) * 256 +
  1091. hdmi_read(sd, 0x2d)) / 2;
  1092. bt->il_vsync = ((hdmi_read(sd, 0x30) & 0x1f) * 256 +
  1093. hdmi_read(sd, 0x31)) / 2;
  1094. bt->vbackporch = ((hdmi_read(sd, 0x34) & 0x1f) * 256 +
  1095. hdmi_read(sd, 0x35)) / 2;
  1096. }
  1097. adv7604_fill_optional_dv_timings_fields(sd, timings);
  1098. } else {
  1099. /* find format
  1100. * Since LCVS values are inaccurate [REF_03, p. 275-276],
  1101. * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
  1102. */
  1103. if (!stdi2dv_timings(sd, &stdi, timings))
  1104. goto found;
  1105. stdi.lcvs += 1;
  1106. v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
  1107. if (!stdi2dv_timings(sd, &stdi, timings))
  1108. goto found;
  1109. stdi.lcvs -= 2;
  1110. v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
  1111. if (stdi2dv_timings(sd, &stdi, timings)) {
  1112. /*
  1113. * The STDI block may measure wrong values, especially
  1114. * for lcvs and lcf. If the driver can not find any
  1115. * valid timing, the STDI block is restarted to measure
  1116. * the video timings again. The function will return an
  1117. * error, but the restart of STDI will generate a new
  1118. * STDI interrupt and the format detection process will
  1119. * restart.
  1120. */
  1121. if (state->restart_stdi_once) {
  1122. v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
  1123. /* TODO restart STDI for Sync Channel 2 */
  1124. /* enter one-shot mode */
  1125. cp_write_and_or(sd, 0x86, 0xf9, 0x00);
  1126. /* trigger STDI restart */
  1127. cp_write_and_or(sd, 0x86, 0xf9, 0x04);
  1128. /* reset to continuous mode */
  1129. cp_write_and_or(sd, 0x86, 0xf9, 0x02);
  1130. state->restart_stdi_once = false;
  1131. return -ENOLINK;
  1132. }
  1133. v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
  1134. return -ERANGE;
  1135. }
  1136. state->restart_stdi_once = true;
  1137. }
  1138. found:
  1139. if (no_signal(sd)) {
  1140. v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
  1141. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1142. return -ENOLINK;
  1143. }
  1144. if ((!DIGITAL_INPUT && bt->pixelclock > 170000000) ||
  1145. (DIGITAL_INPUT && bt->pixelclock > 225000000)) {
  1146. v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
  1147. __func__, (u32)bt->pixelclock);
  1148. return -ERANGE;
  1149. }
  1150. if (debug > 1)
  1151. adv7604_print_timings(sd, timings,
  1152. "adv7604_query_dv_timings:", true);
  1153. return 0;
  1154. }
  1155. static int adv7604_s_dv_timings(struct v4l2_subdev *sd,
  1156. struct v4l2_dv_timings *timings)
  1157. {
  1158. struct adv7604_state *state = to_state(sd);
  1159. struct v4l2_bt_timings *bt;
  1160. int err;
  1161. if (!timings)
  1162. return -EINVAL;
  1163. bt = &timings->bt;
  1164. if ((!DIGITAL_INPUT && bt->pixelclock > 170000000) ||
  1165. (DIGITAL_INPUT && bt->pixelclock > 225000000)) {
  1166. v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
  1167. __func__, (u32)bt->pixelclock);
  1168. return -ERANGE;
  1169. }
  1170. adv7604_fill_optional_dv_timings_fields(sd, timings);
  1171. state->timings = *timings;
  1172. cp_write(sd, 0x91, bt->interlaced ? 0x50 : 0x10);
  1173. /* Use prim_mode and vid_std when available */
  1174. err = configure_predefined_video_timings(sd, timings);
  1175. if (err) {
  1176. /* custom settings when the video format
  1177. does not have prim_mode/vid_std */
  1178. configure_custom_video_timings(sd, bt);
  1179. }
  1180. set_rgb_quantization_range(sd);
  1181. if (debug > 1)
  1182. adv7604_print_timings(sd, timings,
  1183. "adv7604_s_dv_timings:", true);
  1184. return 0;
  1185. }
  1186. static int adv7604_g_dv_timings(struct v4l2_subdev *sd,
  1187. struct v4l2_dv_timings *timings)
  1188. {
  1189. struct adv7604_state *state = to_state(sd);
  1190. *timings = state->timings;
  1191. return 0;
  1192. }
  1193. static void enable_input(struct v4l2_subdev *sd)
  1194. {
  1195. struct adv7604_state *state = to_state(sd);
  1196. switch (state->mode) {
  1197. case ADV7604_MODE_COMP:
  1198. case ADV7604_MODE_GR:
  1199. /* enable */
  1200. io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
  1201. break;
  1202. case ADV7604_MODE_HDMI:
  1203. /* enable */
  1204. hdmi_write(sd, 0x1a, 0x0a); /* Unmute audio */
  1205. hdmi_write(sd, 0x01, 0x00); /* Enable HDMI clock terminators */
  1206. io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
  1207. break;
  1208. default:
  1209. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  1210. __func__, state->mode);
  1211. break;
  1212. }
  1213. }
  1214. static void disable_input(struct v4l2_subdev *sd)
  1215. {
  1216. /* disable */
  1217. io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
  1218. hdmi_write(sd, 0x1a, 0x1a); /* Mute audio */
  1219. hdmi_write(sd, 0x01, 0x78); /* Disable HDMI clock terminators */
  1220. }
  1221. static void select_input(struct v4l2_subdev *sd)
  1222. {
  1223. struct adv7604_state *state = to_state(sd);
  1224. switch (state->mode) {
  1225. case ADV7604_MODE_COMP:
  1226. case ADV7604_MODE_GR:
  1227. /* reset ADI recommended settings for HDMI: */
  1228. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
  1229. hdmi_write(sd, 0x0d, 0x04); /* HDMI filter optimization */
  1230. hdmi_write(sd, 0x3d, 0x00); /* DDC bus active pull-up control */
  1231. hdmi_write(sd, 0x3e, 0x74); /* TMDS PLL optimization */
  1232. hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */
  1233. hdmi_write(sd, 0x57, 0x74); /* TMDS PLL optimization */
  1234. hdmi_write(sd, 0x58, 0x63); /* TMDS PLL optimization */
  1235. hdmi_write(sd, 0x8d, 0x18); /* equaliser */
  1236. hdmi_write(sd, 0x8e, 0x34); /* equaliser */
  1237. hdmi_write(sd, 0x93, 0x88); /* equaliser */
  1238. hdmi_write(sd, 0x94, 0x2e); /* equaliser */
  1239. hdmi_write(sd, 0x96, 0x00); /* enable automatic EQ changing */
  1240. afe_write(sd, 0x00, 0x08); /* power up ADC */
  1241. afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
  1242. afe_write(sd, 0xc8, 0x00); /* phase control */
  1243. /* set ADI recommended settings for digitizer */
  1244. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
  1245. afe_write(sd, 0x12, 0x7b); /* ADC noise shaping filter controls */
  1246. afe_write(sd, 0x0c, 0x1f); /* CP core gain controls */
  1247. cp_write(sd, 0x3e, 0x04); /* CP core pre-gain control */
  1248. cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
  1249. cp_write(sd, 0x40, 0x5c); /* CP core pre-gain control. Graphics mode */
  1250. break;
  1251. case ADV7604_MODE_HDMI:
  1252. /* set ADI recommended settings for HDMI: */
  1253. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
  1254. hdmi_write(sd, 0x0d, 0x84); /* HDMI filter optimization */
  1255. hdmi_write(sd, 0x3d, 0x10); /* DDC bus active pull-up control */
  1256. hdmi_write(sd, 0x3e, 0x39); /* TMDS PLL optimization */
  1257. hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */
  1258. hdmi_write(sd, 0x57, 0xb6); /* TMDS PLL optimization */
  1259. hdmi_write(sd, 0x58, 0x03); /* TMDS PLL optimization */
  1260. hdmi_write(sd, 0x8d, 0x18); /* equaliser */
  1261. hdmi_write(sd, 0x8e, 0x34); /* equaliser */
  1262. hdmi_write(sd, 0x93, 0x8b); /* equaliser */
  1263. hdmi_write(sd, 0x94, 0x2d); /* equaliser */
  1264. hdmi_write(sd, 0x96, 0x01); /* enable automatic EQ changing */
  1265. afe_write(sd, 0x00, 0xff); /* power down ADC */
  1266. afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
  1267. afe_write(sd, 0xc8, 0x40); /* phase control */
  1268. /* reset ADI recommended settings for digitizer */
  1269. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
  1270. afe_write(sd, 0x12, 0xfb); /* ADC noise shaping filter controls */
  1271. afe_write(sd, 0x0c, 0x0d); /* CP core gain controls */
  1272. cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
  1273. cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
  1274. cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
  1275. break;
  1276. default:
  1277. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  1278. __func__, state->mode);
  1279. break;
  1280. }
  1281. }
  1282. static int adv7604_s_routing(struct v4l2_subdev *sd,
  1283. u32 input, u32 output, u32 config)
  1284. {
  1285. struct adv7604_state *state = to_state(sd);
  1286. v4l2_dbg(2, debug, sd, "%s: input %d", __func__, input);
  1287. state->mode = input;
  1288. disable_input(sd);
  1289. select_input(sd);
  1290. enable_input(sd);
  1291. return 0;
  1292. }
  1293. static int adv7604_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
  1294. enum v4l2_mbus_pixelcode *code)
  1295. {
  1296. if (index)
  1297. return -EINVAL;
  1298. /* Good enough for now */
  1299. *code = V4L2_MBUS_FMT_FIXED;
  1300. return 0;
  1301. }
  1302. static int adv7604_g_mbus_fmt(struct v4l2_subdev *sd,
  1303. struct v4l2_mbus_framefmt *fmt)
  1304. {
  1305. struct adv7604_state *state = to_state(sd);
  1306. fmt->width = state->timings.bt.width;
  1307. fmt->height = state->timings.bt.height;
  1308. fmt->code = V4L2_MBUS_FMT_FIXED;
  1309. fmt->field = V4L2_FIELD_NONE;
  1310. if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
  1311. fmt->colorspace = (state->timings.bt.height <= 576) ?
  1312. V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
  1313. }
  1314. return 0;
  1315. }
  1316. static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  1317. {
  1318. struct adv7604_state *state = to_state(sd);
  1319. u8 fmt_change, fmt_change_digital, tx_5v;
  1320. /* format change */
  1321. fmt_change = io_read(sd, 0x43) & 0x98;
  1322. if (fmt_change)
  1323. io_write(sd, 0x44, fmt_change);
  1324. fmt_change_digital = DIGITAL_INPUT ? (io_read(sd, 0x6b) & 0xc0) : 0;
  1325. if (fmt_change_digital)
  1326. io_write(sd, 0x6c, fmt_change_digital);
  1327. if (fmt_change || fmt_change_digital) {
  1328. v4l2_dbg(1, debug, sd,
  1329. "%s: ADV7604_FMT_CHANGE, fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
  1330. __func__, fmt_change, fmt_change_digital);
  1331. v4l2_subdev_notify(sd, ADV7604_FMT_CHANGE, NULL);
  1332. if (handled)
  1333. *handled = true;
  1334. }
  1335. /* tx 5v detect */
  1336. tx_5v = io_read(sd, 0x70) & 0x10;
  1337. if (tx_5v) {
  1338. v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
  1339. io_write(sd, 0x71, tx_5v);
  1340. adv7604_s_detect_tx_5v_ctrl(sd);
  1341. if (handled)
  1342. *handled = true;
  1343. }
  1344. return 0;
  1345. }
  1346. static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
  1347. {
  1348. struct adv7604_state *state = to_state(sd);
  1349. if (edid->pad != 0)
  1350. return -EINVAL;
  1351. if (edid->blocks == 0)
  1352. return -EINVAL;
  1353. if (edid->start_block >= state->edid_blocks)
  1354. return -EINVAL;
  1355. if (edid->start_block + edid->blocks > state->edid_blocks)
  1356. edid->blocks = state->edid_blocks - edid->start_block;
  1357. if (!edid->edid)
  1358. return -EINVAL;
  1359. memcpy(edid->edid + edid->start_block * 128,
  1360. state->edid + edid->start_block * 128,
  1361. edid->blocks * 128);
  1362. return 0;
  1363. }
  1364. static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
  1365. {
  1366. struct adv7604_state *state = to_state(sd);
  1367. int err;
  1368. if (edid->pad != 0)
  1369. return -EINVAL;
  1370. if (edid->start_block != 0)
  1371. return -EINVAL;
  1372. if (edid->blocks == 0) {
  1373. /* Pull down the hotplug pin */
  1374. v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)0);
  1375. /* Disables I2C access to internal EDID ram from DDC port */
  1376. rep_write_and_or(sd, 0x77, 0xf0, 0x0);
  1377. state->edid_blocks = 0;
  1378. /* Fall back to a 16:9 aspect ratio */
  1379. state->aspect_ratio.numerator = 16;
  1380. state->aspect_ratio.denominator = 9;
  1381. return 0;
  1382. }
  1383. if (edid->blocks > 2)
  1384. return -E2BIG;
  1385. if (!edid->edid)
  1386. return -EINVAL;
  1387. memcpy(state->edid, edid->edid, 128 * edid->blocks);
  1388. state->edid_blocks = edid->blocks;
  1389. state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
  1390. edid->edid[0x16]);
  1391. err = edid_write_block(sd, 128 * edid->blocks, state->edid);
  1392. if (err < 0)
  1393. v4l2_err(sd, "error %d writing edid\n", err);
  1394. return err;
  1395. }
  1396. /*********** avi info frame CEA-861-E **************/
  1397. static void print_avi_infoframe(struct v4l2_subdev *sd)
  1398. {
  1399. int i;
  1400. u8 buf[14];
  1401. u8 avi_len;
  1402. u8 avi_ver;
  1403. if (!(hdmi_read(sd, 0x05) & 0x80)) {
  1404. v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
  1405. return;
  1406. }
  1407. if (!(io_read(sd, 0x60) & 0x01)) {
  1408. v4l2_info(sd, "AVI infoframe not received\n");
  1409. return;
  1410. }
  1411. if (io_read(sd, 0x83) & 0x01) {
  1412. v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
  1413. io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
  1414. if (io_read(sd, 0x83) & 0x01) {
  1415. v4l2_info(sd, "AVI infoframe checksum error still present\n");
  1416. io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
  1417. }
  1418. }
  1419. avi_len = infoframe_read(sd, 0xe2);
  1420. avi_ver = infoframe_read(sd, 0xe1);
  1421. v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
  1422. avi_ver, avi_len);
  1423. if (avi_ver != 0x02)
  1424. return;
  1425. for (i = 0; i < 14; i++)
  1426. buf[i] = infoframe_read(sd, i);
  1427. v4l2_info(sd,
  1428. "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1429. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
  1430. buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
  1431. }
  1432. static int adv7604_log_status(struct v4l2_subdev *sd)
  1433. {
  1434. struct adv7604_state *state = to_state(sd);
  1435. struct v4l2_dv_timings timings;
  1436. struct stdi_readback stdi;
  1437. u8 reg_io_0x02 = io_read(sd, 0x02);
  1438. char *csc_coeff_sel_rb[16] = {
  1439. "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
  1440. "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
  1441. "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
  1442. "reserved", "reserved", "reserved", "reserved", "manual"
  1443. };
  1444. char *input_color_space_txt[16] = {
  1445. "RGB limited range (16-235)", "RGB full range (0-255)",
  1446. "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
  1447. "XvYCC Bt.601", "XvYCC Bt.709",
  1448. "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
  1449. "invalid", "invalid", "invalid", "invalid", "invalid",
  1450. "invalid", "invalid", "automatic"
  1451. };
  1452. char *rgb_quantization_range_txt[] = {
  1453. "Automatic",
  1454. "RGB limited range (16-235)",
  1455. "RGB full range (0-255)",
  1456. };
  1457. v4l2_info(sd, "-----Chip status-----\n");
  1458. v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
  1459. v4l2_info(sd, "Connector type: %s\n", state->connector_hdmi ?
  1460. "HDMI" : (DIGITAL_INPUT ? "DVI-D" : "DVI-A"));
  1461. v4l2_info(sd, "EDID: %s\n", ((rep_read(sd, 0x7d) & 0x01) &&
  1462. (rep_read(sd, 0x77) & 0x01)) ? "enabled" : "disabled ");
  1463. v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
  1464. "enabled" : "disabled");
  1465. v4l2_info(sd, "-----Signal status-----\n");
  1466. v4l2_info(sd, "Cable detected (+5V power): %s\n",
  1467. (io_read(sd, 0x6f) & 0x10) ? "true" : "false");
  1468. v4l2_info(sd, "TMDS signal detected: %s\n",
  1469. no_signal_tmds(sd) ? "false" : "true");
  1470. v4l2_info(sd, "TMDS signal locked: %s\n",
  1471. no_lock_tmds(sd) ? "false" : "true");
  1472. v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
  1473. v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
  1474. v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
  1475. v4l2_info(sd, "CP free run: %s\n",
  1476. (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
  1477. v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
  1478. io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
  1479. (io_read(sd, 0x01) & 0x70) >> 4);
  1480. v4l2_info(sd, "-----Video Timings-----\n");
  1481. if (read_stdi(sd, &stdi))
  1482. v4l2_info(sd, "STDI: not locked\n");
  1483. else
  1484. v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
  1485. stdi.lcf, stdi.bl, stdi.lcvs,
  1486. stdi.interlaced ? "interlaced" : "progressive",
  1487. stdi.hs_pol, stdi.vs_pol);
  1488. if (adv7604_query_dv_timings(sd, &timings))
  1489. v4l2_info(sd, "No video detected\n");
  1490. else
  1491. adv7604_print_timings(sd, &timings, "Detected format:", true);
  1492. adv7604_print_timings(sd, &state->timings, "Configured format:", true);
  1493. v4l2_info(sd, "-----Color space-----\n");
  1494. v4l2_info(sd, "RGB quantization range ctrl: %s\n",
  1495. rgb_quantization_range_txt[state->rgb_quantization_range]);
  1496. v4l2_info(sd, "Input color space: %s\n",
  1497. input_color_space_txt[reg_io_0x02 >> 4]);
  1498. v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
  1499. (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
  1500. (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
  1501. ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
  1502. "enabled" : "disabled");
  1503. v4l2_info(sd, "Color space conversion: %s\n",
  1504. csc_coeff_sel_rb[cp_read(sd, 0xfc) >> 4]);
  1505. /* Digital video */
  1506. if (DIGITAL_INPUT) {
  1507. v4l2_info(sd, "-----HDMI status-----\n");
  1508. v4l2_info(sd, "HDCP encrypted content: %s\n",
  1509. hdmi_read(sd, 0x05) & 0x40 ? "true" : "false");
  1510. print_avi_infoframe(sd);
  1511. }
  1512. return 0;
  1513. }
  1514. /* ----------------------------------------------------------------------- */
  1515. static const struct v4l2_ctrl_ops adv7604_ctrl_ops = {
  1516. .s_ctrl = adv7604_s_ctrl,
  1517. };
  1518. static const struct v4l2_subdev_core_ops adv7604_core_ops = {
  1519. .log_status = adv7604_log_status,
  1520. .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
  1521. .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
  1522. .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
  1523. .g_ctrl = v4l2_subdev_g_ctrl,
  1524. .s_ctrl = v4l2_subdev_s_ctrl,
  1525. .queryctrl = v4l2_subdev_queryctrl,
  1526. .querymenu = v4l2_subdev_querymenu,
  1527. .interrupt_service_routine = adv7604_isr,
  1528. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1529. .g_register = adv7604_g_register,
  1530. .s_register = adv7604_s_register,
  1531. #endif
  1532. };
  1533. static const struct v4l2_subdev_video_ops adv7604_video_ops = {
  1534. .s_routing = adv7604_s_routing,
  1535. .g_input_status = adv7604_g_input_status,
  1536. .s_dv_timings = adv7604_s_dv_timings,
  1537. .g_dv_timings = adv7604_g_dv_timings,
  1538. .query_dv_timings = adv7604_query_dv_timings,
  1539. .enum_dv_timings = adv7604_enum_dv_timings,
  1540. .dv_timings_cap = adv7604_dv_timings_cap,
  1541. .enum_mbus_fmt = adv7604_enum_mbus_fmt,
  1542. .g_mbus_fmt = adv7604_g_mbus_fmt,
  1543. .try_mbus_fmt = adv7604_g_mbus_fmt,
  1544. .s_mbus_fmt = adv7604_g_mbus_fmt,
  1545. };
  1546. static const struct v4l2_subdev_pad_ops adv7604_pad_ops = {
  1547. .get_edid = adv7604_get_edid,
  1548. .set_edid = adv7604_set_edid,
  1549. };
  1550. static const struct v4l2_subdev_ops adv7604_ops = {
  1551. .core = &adv7604_core_ops,
  1552. .video = &adv7604_video_ops,
  1553. .pad = &adv7604_pad_ops,
  1554. };
  1555. /* -------------------------- custom ctrls ---------------------------------- */
  1556. static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
  1557. .ops = &adv7604_ctrl_ops,
  1558. .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
  1559. .name = "Analog Sampling Phase",
  1560. .type = V4L2_CTRL_TYPE_INTEGER,
  1561. .min = 0,
  1562. .max = 0x1f,
  1563. .step = 1,
  1564. .def = 0,
  1565. };
  1566. static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color_manual = {
  1567. .ops = &adv7604_ctrl_ops,
  1568. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
  1569. .name = "Free Running Color, Manual",
  1570. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1571. .min = false,
  1572. .max = true,
  1573. .step = 1,
  1574. .def = false,
  1575. };
  1576. static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color = {
  1577. .ops = &adv7604_ctrl_ops,
  1578. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
  1579. .name = "Free Running Color",
  1580. .type = V4L2_CTRL_TYPE_INTEGER,
  1581. .min = 0x0,
  1582. .max = 0xffffff,
  1583. .step = 0x1,
  1584. .def = 0x0,
  1585. };
  1586. /* ----------------------------------------------------------------------- */
  1587. static int adv7604_core_init(struct v4l2_subdev *sd)
  1588. {
  1589. struct adv7604_state *state = to_state(sd);
  1590. struct adv7604_platform_data *pdata = &state->pdata;
  1591. hdmi_write(sd, 0x48,
  1592. (pdata->disable_pwrdnb ? 0x80 : 0) |
  1593. (pdata->disable_cable_det_rst ? 0x40 : 0));
  1594. disable_input(sd);
  1595. /* power */
  1596. io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
  1597. io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
  1598. cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
  1599. /* video format */
  1600. io_write_and_or(sd, 0x02, 0xf0,
  1601. pdata->alt_gamma << 3 |
  1602. pdata->op_656_range << 2 |
  1603. pdata->rgb_out << 1 |
  1604. pdata->alt_data_sat << 0);
  1605. io_write(sd, 0x03, pdata->op_format_sel);
  1606. io_write_and_or(sd, 0x04, 0x1f, pdata->op_ch_sel << 5);
  1607. io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
  1608. pdata->insert_av_codes << 2 |
  1609. pdata->replicate_av_codes << 1 |
  1610. pdata->invert_cbcr << 0);
  1611. /* TODO from platform data */
  1612. cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
  1613. io_write(sd, 0x06, 0xa6); /* positive VS and HS */
  1614. io_write(sd, 0x14, 0x7f); /* Drive strength adjusted to max */
  1615. cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
  1616. cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
  1617. cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
  1618. ADI recommended setting [REF_01, c. 2.3.3] */
  1619. cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
  1620. ADI recommended setting [REF_01, c. 2.3.3] */
  1621. cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
  1622. for digital formats */
  1623. /* TODO from platform data */
  1624. afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
  1625. afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
  1626. io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
  1627. /* interrupts */
  1628. io_write(sd, 0x40, 0xc2); /* Configure INT1 */
  1629. io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
  1630. io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
  1631. io_write(sd, 0x6e, 0xc0); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
  1632. io_write(sd, 0x73, 0x10); /* Enable CABLE_DET_A_ST (+5v) interrupt */
  1633. return v4l2_ctrl_handler_setup(sd->ctrl_handler);
  1634. }
  1635. static void adv7604_unregister_clients(struct adv7604_state *state)
  1636. {
  1637. if (state->i2c_avlink)
  1638. i2c_unregister_device(state->i2c_avlink);
  1639. if (state->i2c_cec)
  1640. i2c_unregister_device(state->i2c_cec);
  1641. if (state->i2c_infoframe)
  1642. i2c_unregister_device(state->i2c_infoframe);
  1643. if (state->i2c_esdp)
  1644. i2c_unregister_device(state->i2c_esdp);
  1645. if (state->i2c_dpp)
  1646. i2c_unregister_device(state->i2c_dpp);
  1647. if (state->i2c_afe)
  1648. i2c_unregister_device(state->i2c_afe);
  1649. if (state->i2c_repeater)
  1650. i2c_unregister_device(state->i2c_repeater);
  1651. if (state->i2c_edid)
  1652. i2c_unregister_device(state->i2c_edid);
  1653. if (state->i2c_hdmi)
  1654. i2c_unregister_device(state->i2c_hdmi);
  1655. if (state->i2c_test)
  1656. i2c_unregister_device(state->i2c_test);
  1657. if (state->i2c_cp)
  1658. i2c_unregister_device(state->i2c_cp);
  1659. if (state->i2c_vdp)
  1660. i2c_unregister_device(state->i2c_vdp);
  1661. }
  1662. static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
  1663. u8 addr, u8 io_reg)
  1664. {
  1665. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1666. if (addr)
  1667. io_write(sd, io_reg, addr << 1);
  1668. return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
  1669. }
  1670. static int adv7604_probe(struct i2c_client *client,
  1671. const struct i2c_device_id *id)
  1672. {
  1673. struct adv7604_state *state;
  1674. struct adv7604_platform_data *pdata = client->dev.platform_data;
  1675. struct v4l2_ctrl_handler *hdl;
  1676. struct v4l2_subdev *sd;
  1677. int err;
  1678. /* Check if the adapter supports the needed features */
  1679. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1680. return -EIO;
  1681. v4l_dbg(1, debug, client, "detecting adv7604 client on address 0x%x\n",
  1682. client->addr << 1);
  1683. state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
  1684. if (!state) {
  1685. v4l_err(client, "Could not allocate adv7604_state memory!\n");
  1686. return -ENOMEM;
  1687. }
  1688. /* platform data */
  1689. if (!pdata) {
  1690. v4l_err(client, "No platform data!\n");
  1691. return -ENODEV;
  1692. }
  1693. memcpy(&state->pdata, pdata, sizeof(state->pdata));
  1694. sd = &state->sd;
  1695. v4l2_i2c_subdev_init(sd, client, &adv7604_ops);
  1696. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1697. state->connector_hdmi = pdata->connector_hdmi;
  1698. /* i2c access to adv7604? */
  1699. if (adv_smbus_read_byte_data_check(client, 0xfb, false) != 0x68) {
  1700. v4l2_info(sd, "not an adv7604 on address 0x%x\n",
  1701. client->addr << 1);
  1702. return -ENODEV;
  1703. }
  1704. /* control handlers */
  1705. hdl = &state->hdl;
  1706. v4l2_ctrl_handler_init(hdl, 9);
  1707. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  1708. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  1709. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  1710. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  1711. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  1712. V4L2_CID_SATURATION, 0, 255, 1, 128);
  1713. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  1714. V4L2_CID_HUE, 0, 128, 1, 0);
  1715. /* private controls */
  1716. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  1717. V4L2_CID_DV_RX_POWER_PRESENT, 0, 1, 0, 0);
  1718. state->detect_tx_5v_ctrl->is_private = true;
  1719. state->rgb_quantization_range_ctrl =
  1720. v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops,
  1721. V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
  1722. 0, V4L2_DV_RGB_RANGE_AUTO);
  1723. state->rgb_quantization_range_ctrl->is_private = true;
  1724. /* custom controls */
  1725. state->analog_sampling_phase_ctrl =
  1726. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
  1727. state->analog_sampling_phase_ctrl->is_private = true;
  1728. state->free_run_color_manual_ctrl =
  1729. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL);
  1730. state->free_run_color_manual_ctrl->is_private = true;
  1731. state->free_run_color_ctrl =
  1732. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color, NULL);
  1733. state->free_run_color_ctrl->is_private = true;
  1734. sd->ctrl_handler = hdl;
  1735. if (hdl->error) {
  1736. err = hdl->error;
  1737. goto err_hdl;
  1738. }
  1739. if (adv7604_s_detect_tx_5v_ctrl(sd)) {
  1740. err = -ENODEV;
  1741. goto err_hdl;
  1742. }
  1743. state->i2c_avlink = adv7604_dummy_client(sd, pdata->i2c_avlink, 0xf3);
  1744. state->i2c_cec = adv7604_dummy_client(sd, pdata->i2c_cec, 0xf4);
  1745. state->i2c_infoframe = adv7604_dummy_client(sd, pdata->i2c_infoframe, 0xf5);
  1746. state->i2c_esdp = adv7604_dummy_client(sd, pdata->i2c_esdp, 0xf6);
  1747. state->i2c_dpp = adv7604_dummy_client(sd, pdata->i2c_dpp, 0xf7);
  1748. state->i2c_afe = adv7604_dummy_client(sd, pdata->i2c_afe, 0xf8);
  1749. state->i2c_repeater = adv7604_dummy_client(sd, pdata->i2c_repeater, 0xf9);
  1750. state->i2c_edid = adv7604_dummy_client(sd, pdata->i2c_edid, 0xfa);
  1751. state->i2c_hdmi = adv7604_dummy_client(sd, pdata->i2c_hdmi, 0xfb);
  1752. state->i2c_test = adv7604_dummy_client(sd, pdata->i2c_test, 0xfc);
  1753. state->i2c_cp = adv7604_dummy_client(sd, pdata->i2c_cp, 0xfd);
  1754. state->i2c_vdp = adv7604_dummy_client(sd, pdata->i2c_vdp, 0xfe);
  1755. if (!state->i2c_avlink || !state->i2c_cec || !state->i2c_infoframe ||
  1756. !state->i2c_esdp || !state->i2c_dpp || !state->i2c_afe ||
  1757. !state->i2c_repeater || !state->i2c_edid || !state->i2c_hdmi ||
  1758. !state->i2c_test || !state->i2c_cp || !state->i2c_vdp) {
  1759. err = -ENOMEM;
  1760. v4l2_err(sd, "failed to create all i2c clients\n");
  1761. goto err_i2c;
  1762. }
  1763. state->restart_stdi_once = true;
  1764. /* work queues */
  1765. state->work_queues = create_singlethread_workqueue(client->name);
  1766. if (!state->work_queues) {
  1767. v4l2_err(sd, "Could not create work queue\n");
  1768. err = -ENOMEM;
  1769. goto err_i2c;
  1770. }
  1771. INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
  1772. adv7604_delayed_work_enable_hotplug);
  1773. state->pad.flags = MEDIA_PAD_FL_SOURCE;
  1774. err = media_entity_init(&sd->entity, 1, &state->pad, 0);
  1775. if (err)
  1776. goto err_work_queues;
  1777. err = adv7604_core_init(sd);
  1778. if (err)
  1779. goto err_entity;
  1780. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  1781. client->addr << 1, client->adapter->name);
  1782. return 0;
  1783. err_entity:
  1784. media_entity_cleanup(&sd->entity);
  1785. err_work_queues:
  1786. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  1787. destroy_workqueue(state->work_queues);
  1788. err_i2c:
  1789. adv7604_unregister_clients(state);
  1790. err_hdl:
  1791. v4l2_ctrl_handler_free(hdl);
  1792. return err;
  1793. }
  1794. /* ----------------------------------------------------------------------- */
  1795. static int adv7604_remove(struct i2c_client *client)
  1796. {
  1797. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1798. struct adv7604_state *state = to_state(sd);
  1799. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  1800. destroy_workqueue(state->work_queues);
  1801. v4l2_device_unregister_subdev(sd);
  1802. media_entity_cleanup(&sd->entity);
  1803. adv7604_unregister_clients(to_state(sd));
  1804. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1805. return 0;
  1806. }
  1807. /* ----------------------------------------------------------------------- */
  1808. static struct i2c_device_id adv7604_id[] = {
  1809. { "adv7604", 0 },
  1810. { }
  1811. };
  1812. MODULE_DEVICE_TABLE(i2c, adv7604_id);
  1813. static struct i2c_driver adv7604_driver = {
  1814. .driver = {
  1815. .owner = THIS_MODULE,
  1816. .name = "adv7604",
  1817. },
  1818. .probe = adv7604_probe,
  1819. .remove = adv7604_remove,
  1820. .id_table = adv7604_id,
  1821. };
  1822. module_i2c_driver(adv7604_driver);