adv7604.c 62 KB

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