tvp7002.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Texas Instruments Triple 8-/10-BIT 165-/110-MSPS Video and Graphics
  3. * Digitizer with Horizontal PLL registers
  4. *
  5. * Copyright (C) 2009 Texas Instruments Inc
  6. * Author: Santiago Nunez-Corrales <[email protected]>
  7. *
  8. * This code is partially based upon the TVP5150 driver
  9. * written by Mauro Carvalho Chehab <[email protected]>,
  10. * the TVP514x driver written by Vaibhav Hiremath <[email protected]>
  11. * and the TVP7002 driver in the TI LSP 2.10.00.14. Revisions by
  12. * Muralidharan Karicheri and Snehaprabha Narnakaje (TI).
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/i2c.h>
  16. #include <linux/slab.h>
  17. #include <linux/videodev2.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_graph.h>
  21. #include <linux/v4l2-dv-timings.h>
  22. #include <media/i2c/tvp7002.h>
  23. #include <media/v4l2-async.h>
  24. #include <media/v4l2-device.h>
  25. #include <media/v4l2-common.h>
  26. #include <media/v4l2-ctrls.h>
  27. #include <media/v4l2-fwnode.h>
  28. #include "tvp7002_reg.h"
  29. MODULE_DESCRIPTION("TI TVP7002 Video and Graphics Digitizer driver");
  30. MODULE_AUTHOR("Santiago Nunez-Corrales <[email protected]>");
  31. MODULE_LICENSE("GPL");
  32. /* I2C retry attempts */
  33. #define I2C_RETRY_COUNT (5)
  34. /* End of registers */
  35. #define TVP7002_EOR 0x5c
  36. /* Read write definition for registers */
  37. #define TVP7002_READ 0
  38. #define TVP7002_WRITE 1
  39. #define TVP7002_RESERVED 2
  40. /* Interlaced vs progressive mask and shift */
  41. #define TVP7002_IP_SHIFT 5
  42. #define TVP7002_INPR_MASK (0x01 << TVP7002_IP_SHIFT)
  43. /* Shift for CPL and LPF registers */
  44. #define TVP7002_CL_SHIFT 8
  45. #define TVP7002_CL_MASK 0x0f
  46. /* Debug functions */
  47. static bool debug;
  48. module_param(debug, bool, 0644);
  49. MODULE_PARM_DESC(debug, "Debug level (0-2)");
  50. /* Structure for register values */
  51. struct i2c_reg_value {
  52. u8 reg;
  53. u8 value;
  54. u8 type;
  55. };
  56. /*
  57. * Register default values (according to tvp7002 datasheet)
  58. * In the case of read-only registers, the value (0xff) is
  59. * never written. R/W functionality is controlled by the
  60. * writable bit in the register struct definition.
  61. */
  62. static const struct i2c_reg_value tvp7002_init_default[] = {
  63. { TVP7002_CHIP_REV, 0xff, TVP7002_READ },
  64. { TVP7002_HPLL_FDBK_DIV_MSBS, 0x67, TVP7002_WRITE },
  65. { TVP7002_HPLL_FDBK_DIV_LSBS, 0x20, TVP7002_WRITE },
  66. { TVP7002_HPLL_CRTL, 0xa0, TVP7002_WRITE },
  67. { TVP7002_HPLL_PHASE_SEL, 0x80, TVP7002_WRITE },
  68. { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
  69. { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
  70. { TVP7002_HSYNC_OUT_W, 0x60, TVP7002_WRITE },
  71. { TVP7002_B_FINE_GAIN, 0x00, TVP7002_WRITE },
  72. { TVP7002_G_FINE_GAIN, 0x00, TVP7002_WRITE },
  73. { TVP7002_R_FINE_GAIN, 0x00, TVP7002_WRITE },
  74. { TVP7002_B_FINE_OFF_MSBS, 0x80, TVP7002_WRITE },
  75. { TVP7002_G_FINE_OFF_MSBS, 0x80, TVP7002_WRITE },
  76. { TVP7002_R_FINE_OFF_MSBS, 0x80, TVP7002_WRITE },
  77. { TVP7002_SYNC_CTL_1, 0x20, TVP7002_WRITE },
  78. { TVP7002_HPLL_AND_CLAMP_CTL, 0x2e, TVP7002_WRITE },
  79. { TVP7002_SYNC_ON_G_THRS, 0x5d, TVP7002_WRITE },
  80. { TVP7002_SYNC_SEPARATOR_THRS, 0x47, TVP7002_WRITE },
  81. { TVP7002_HPLL_PRE_COAST, 0x00, TVP7002_WRITE },
  82. { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
  83. { TVP7002_SYNC_DETECT_STAT, 0xff, TVP7002_READ },
  84. { TVP7002_OUT_FORMATTER, 0x47, TVP7002_WRITE },
  85. { TVP7002_MISC_CTL_1, 0x01, TVP7002_WRITE },
  86. { TVP7002_MISC_CTL_2, 0x00, TVP7002_WRITE },
  87. { TVP7002_MISC_CTL_3, 0x01, TVP7002_WRITE },
  88. { TVP7002_IN_MUX_SEL_1, 0x00, TVP7002_WRITE },
  89. { TVP7002_IN_MUX_SEL_2, 0x67, TVP7002_WRITE },
  90. { TVP7002_B_AND_G_COARSE_GAIN, 0x77, TVP7002_WRITE },
  91. { TVP7002_R_COARSE_GAIN, 0x07, TVP7002_WRITE },
  92. { TVP7002_FINE_OFF_LSBS, 0x00, TVP7002_WRITE },
  93. { TVP7002_B_COARSE_OFF, 0x10, TVP7002_WRITE },
  94. { TVP7002_G_COARSE_OFF, 0x10, TVP7002_WRITE },
  95. { TVP7002_R_COARSE_OFF, 0x10, TVP7002_WRITE },
  96. { TVP7002_HSOUT_OUT_START, 0x08, TVP7002_WRITE },
  97. { TVP7002_MISC_CTL_4, 0x00, TVP7002_WRITE },
  98. { TVP7002_B_DGTL_ALC_OUT_LSBS, 0xff, TVP7002_READ },
  99. { TVP7002_G_DGTL_ALC_OUT_LSBS, 0xff, TVP7002_READ },
  100. { TVP7002_R_DGTL_ALC_OUT_LSBS, 0xff, TVP7002_READ },
  101. { TVP7002_AUTO_LVL_CTL_ENABLE, 0x80, TVP7002_WRITE },
  102. { TVP7002_DGTL_ALC_OUT_MSBS, 0xff, TVP7002_READ },
  103. { TVP7002_AUTO_LVL_CTL_FILTER, 0x53, TVP7002_WRITE },
  104. { 0x29, 0x08, TVP7002_RESERVED },
  105. { TVP7002_FINE_CLAMP_CTL, 0x07, TVP7002_WRITE },
  106. /* PWR_CTL is controlled only by the probe and reset functions */
  107. { TVP7002_PWR_CTL, 0x00, TVP7002_RESERVED },
  108. { TVP7002_ADC_SETUP, 0x50, TVP7002_WRITE },
  109. { TVP7002_COARSE_CLAMP_CTL, 0x00, TVP7002_WRITE },
  110. { TVP7002_SOG_CLAMP, 0x80, TVP7002_WRITE },
  111. { TVP7002_RGB_COARSE_CLAMP_CTL, 0x8c, TVP7002_WRITE },
  112. { TVP7002_SOG_COARSE_CLAMP_CTL, 0x04, TVP7002_WRITE },
  113. { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
  114. { 0x32, 0x18, TVP7002_RESERVED },
  115. { 0x33, 0x60, TVP7002_RESERVED },
  116. { TVP7002_MVIS_STRIPPER_W, 0xff, TVP7002_RESERVED },
  117. { TVP7002_VSYNC_ALGN, 0x10, TVP7002_WRITE },
  118. { TVP7002_SYNC_BYPASS, 0x00, TVP7002_WRITE },
  119. { TVP7002_L_FRAME_STAT_LSBS, 0xff, TVP7002_READ },
  120. { TVP7002_L_FRAME_STAT_MSBS, 0xff, TVP7002_READ },
  121. { TVP7002_CLK_L_STAT_LSBS, 0xff, TVP7002_READ },
  122. { TVP7002_CLK_L_STAT_MSBS, 0xff, TVP7002_READ },
  123. { TVP7002_HSYNC_W, 0xff, TVP7002_READ },
  124. { TVP7002_VSYNC_W, 0xff, TVP7002_READ },
  125. { TVP7002_L_LENGTH_TOL, 0x03, TVP7002_WRITE },
  126. { 0x3e, 0x60, TVP7002_RESERVED },
  127. { TVP7002_VIDEO_BWTH_CTL, 0x01, TVP7002_WRITE },
  128. { TVP7002_AVID_START_PIXEL_LSBS, 0x01, TVP7002_WRITE },
  129. { TVP7002_AVID_START_PIXEL_MSBS, 0x2c, TVP7002_WRITE },
  130. { TVP7002_AVID_STOP_PIXEL_LSBS, 0x06, TVP7002_WRITE },
  131. { TVP7002_AVID_STOP_PIXEL_MSBS, 0x2c, TVP7002_WRITE },
  132. { TVP7002_VBLK_F_0_START_L_OFF, 0x05, TVP7002_WRITE },
  133. { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
  134. { TVP7002_VBLK_F_0_DURATION, 0x1e, TVP7002_WRITE },
  135. { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
  136. { TVP7002_FBIT_F_0_START_L_OFF, 0x00, TVP7002_WRITE },
  137. { TVP7002_FBIT_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
  138. { TVP7002_YUV_Y_G_COEF_LSBS, 0xe3, TVP7002_WRITE },
  139. { TVP7002_YUV_Y_G_COEF_MSBS, 0x16, TVP7002_WRITE },
  140. { TVP7002_YUV_Y_B_COEF_LSBS, 0x4f, TVP7002_WRITE },
  141. { TVP7002_YUV_Y_B_COEF_MSBS, 0x02, TVP7002_WRITE },
  142. { TVP7002_YUV_Y_R_COEF_LSBS, 0xce, TVP7002_WRITE },
  143. { TVP7002_YUV_Y_R_COEF_MSBS, 0x06, TVP7002_WRITE },
  144. { TVP7002_YUV_U_G_COEF_LSBS, 0xab, TVP7002_WRITE },
  145. { TVP7002_YUV_U_G_COEF_MSBS, 0xf3, TVP7002_WRITE },
  146. { TVP7002_YUV_U_B_COEF_LSBS, 0x00, TVP7002_WRITE },
  147. { TVP7002_YUV_U_B_COEF_MSBS, 0x10, TVP7002_WRITE },
  148. { TVP7002_YUV_U_R_COEF_LSBS, 0x55, TVP7002_WRITE },
  149. { TVP7002_YUV_U_R_COEF_MSBS, 0xfc, TVP7002_WRITE },
  150. { TVP7002_YUV_V_G_COEF_LSBS, 0x78, TVP7002_WRITE },
  151. { TVP7002_YUV_V_G_COEF_MSBS, 0xf1, TVP7002_WRITE },
  152. { TVP7002_YUV_V_B_COEF_LSBS, 0x88, TVP7002_WRITE },
  153. { TVP7002_YUV_V_B_COEF_MSBS, 0xfe, TVP7002_WRITE },
  154. { TVP7002_YUV_V_R_COEF_LSBS, 0x00, TVP7002_WRITE },
  155. { TVP7002_YUV_V_R_COEF_MSBS, 0x10, TVP7002_WRITE },
  156. /* This signals end of register values */
  157. { TVP7002_EOR, 0xff, TVP7002_RESERVED }
  158. };
  159. /* Register parameters for 480P */
  160. static const struct i2c_reg_value tvp7002_parms_480P[] = {
  161. { TVP7002_HPLL_FDBK_DIV_MSBS, 0x35, TVP7002_WRITE },
  162. { TVP7002_HPLL_FDBK_DIV_LSBS, 0xa0, TVP7002_WRITE },
  163. { TVP7002_HPLL_CRTL, 0x02, TVP7002_WRITE },
  164. { TVP7002_AVID_START_PIXEL_LSBS, 0x91, TVP7002_WRITE },
  165. { TVP7002_AVID_START_PIXEL_MSBS, 0x00, TVP7002_WRITE },
  166. { TVP7002_AVID_STOP_PIXEL_LSBS, 0x0B, TVP7002_WRITE },
  167. { TVP7002_AVID_STOP_PIXEL_MSBS, 0x00, TVP7002_WRITE },
  168. { TVP7002_VBLK_F_0_START_L_OFF, 0x03, TVP7002_WRITE },
  169. { TVP7002_VBLK_F_1_START_L_OFF, 0x01, TVP7002_WRITE },
  170. { TVP7002_VBLK_F_0_DURATION, 0x13, TVP7002_WRITE },
  171. { TVP7002_VBLK_F_1_DURATION, 0x13, TVP7002_WRITE },
  172. { TVP7002_ALC_PLACEMENT, 0x18, TVP7002_WRITE },
  173. { TVP7002_CLAMP_START, 0x06, TVP7002_WRITE },
  174. { TVP7002_CLAMP_W, 0x10, TVP7002_WRITE },
  175. { TVP7002_HPLL_PRE_COAST, 0x03, TVP7002_WRITE },
  176. { TVP7002_HPLL_POST_COAST, 0x03, TVP7002_WRITE },
  177. { TVP7002_EOR, 0xff, TVP7002_RESERVED }
  178. };
  179. /* Register parameters for 576P */
  180. static const struct i2c_reg_value tvp7002_parms_576P[] = {
  181. { TVP7002_HPLL_FDBK_DIV_MSBS, 0x36, TVP7002_WRITE },
  182. { TVP7002_HPLL_FDBK_DIV_LSBS, 0x00, TVP7002_WRITE },
  183. { TVP7002_HPLL_CRTL, 0x18, TVP7002_WRITE },
  184. { TVP7002_AVID_START_PIXEL_LSBS, 0x9B, TVP7002_WRITE },
  185. { TVP7002_AVID_START_PIXEL_MSBS, 0x00, TVP7002_WRITE },
  186. { TVP7002_AVID_STOP_PIXEL_LSBS, 0x0F, TVP7002_WRITE },
  187. { TVP7002_AVID_STOP_PIXEL_MSBS, 0x00, TVP7002_WRITE },
  188. { TVP7002_VBLK_F_0_START_L_OFF, 0x00, TVP7002_WRITE },
  189. { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
  190. { TVP7002_VBLK_F_0_DURATION, 0x2D, TVP7002_WRITE },
  191. { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
  192. { TVP7002_ALC_PLACEMENT, 0x18, TVP7002_WRITE },
  193. { TVP7002_CLAMP_START, 0x06, TVP7002_WRITE },
  194. { TVP7002_CLAMP_W, 0x10, TVP7002_WRITE },
  195. { TVP7002_HPLL_PRE_COAST, 0x03, TVP7002_WRITE },
  196. { TVP7002_HPLL_POST_COAST, 0x03, TVP7002_WRITE },
  197. { TVP7002_EOR, 0xff, TVP7002_RESERVED }
  198. };
  199. /* Register parameters for 1080I60 */
  200. static const struct i2c_reg_value tvp7002_parms_1080I60[] = {
  201. { TVP7002_HPLL_FDBK_DIV_MSBS, 0x89, TVP7002_WRITE },
  202. { TVP7002_HPLL_FDBK_DIV_LSBS, 0x80, TVP7002_WRITE },
  203. { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
  204. { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
  205. { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
  206. { TVP7002_AVID_STOP_PIXEL_LSBS, 0x8a, TVP7002_WRITE },
  207. { TVP7002_AVID_STOP_PIXEL_MSBS, 0x08, TVP7002_WRITE },
  208. { TVP7002_VBLK_F_0_START_L_OFF, 0x02, TVP7002_WRITE },
  209. { TVP7002_VBLK_F_1_START_L_OFF, 0x02, TVP7002_WRITE },
  210. { TVP7002_VBLK_F_0_DURATION, 0x16, TVP7002_WRITE },
  211. { TVP7002_VBLK_F_1_DURATION, 0x17, TVP7002_WRITE },
  212. { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
  213. { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
  214. { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
  215. { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
  216. { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
  217. { TVP7002_EOR, 0xff, TVP7002_RESERVED }
  218. };
  219. /* Register parameters for 1080P60 */
  220. static const struct i2c_reg_value tvp7002_parms_1080P60[] = {
  221. { TVP7002_HPLL_FDBK_DIV_MSBS, 0x89, TVP7002_WRITE },
  222. { TVP7002_HPLL_FDBK_DIV_LSBS, 0x80, TVP7002_WRITE },
  223. { TVP7002_HPLL_CRTL, 0xE0, TVP7002_WRITE },
  224. { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
  225. { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
  226. { TVP7002_AVID_STOP_PIXEL_LSBS, 0x8a, TVP7002_WRITE },
  227. { TVP7002_AVID_STOP_PIXEL_MSBS, 0x08, TVP7002_WRITE },
  228. { TVP7002_VBLK_F_0_START_L_OFF, 0x02, TVP7002_WRITE },
  229. { TVP7002_VBLK_F_1_START_L_OFF, 0x02, TVP7002_WRITE },
  230. { TVP7002_VBLK_F_0_DURATION, 0x16, TVP7002_WRITE },
  231. { TVP7002_VBLK_F_1_DURATION, 0x17, TVP7002_WRITE },
  232. { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
  233. { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
  234. { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
  235. { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
  236. { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
  237. { TVP7002_EOR, 0xff, TVP7002_RESERVED }
  238. };
  239. /* Register parameters for 1080I50 */
  240. static const struct i2c_reg_value tvp7002_parms_1080I50[] = {
  241. { TVP7002_HPLL_FDBK_DIV_MSBS, 0xa5, TVP7002_WRITE },
  242. { TVP7002_HPLL_FDBK_DIV_LSBS, 0x00, TVP7002_WRITE },
  243. { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
  244. { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
  245. { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
  246. { TVP7002_AVID_STOP_PIXEL_LSBS, 0x8a, TVP7002_WRITE },
  247. { TVP7002_AVID_STOP_PIXEL_MSBS, 0x08, TVP7002_WRITE },
  248. { TVP7002_VBLK_F_0_START_L_OFF, 0x02, TVP7002_WRITE },
  249. { TVP7002_VBLK_F_1_START_L_OFF, 0x02, TVP7002_WRITE },
  250. { TVP7002_VBLK_F_0_DURATION, 0x16, TVP7002_WRITE },
  251. { TVP7002_VBLK_F_1_DURATION, 0x17, TVP7002_WRITE },
  252. { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
  253. { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
  254. { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
  255. { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
  256. { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
  257. { TVP7002_EOR, 0xff, TVP7002_RESERVED }
  258. };
  259. /* Register parameters for 720P60 */
  260. static const struct i2c_reg_value tvp7002_parms_720P60[] = {
  261. { TVP7002_HPLL_FDBK_DIV_MSBS, 0x67, TVP7002_WRITE },
  262. { TVP7002_HPLL_FDBK_DIV_LSBS, 0x20, TVP7002_WRITE },
  263. { TVP7002_HPLL_CRTL, 0xa0, TVP7002_WRITE },
  264. { TVP7002_AVID_START_PIXEL_LSBS, 0x47, TVP7002_WRITE },
  265. { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
  266. { TVP7002_AVID_STOP_PIXEL_LSBS, 0x4B, TVP7002_WRITE },
  267. { TVP7002_AVID_STOP_PIXEL_MSBS, 0x06, TVP7002_WRITE },
  268. { TVP7002_VBLK_F_0_START_L_OFF, 0x05, TVP7002_WRITE },
  269. { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
  270. { TVP7002_VBLK_F_0_DURATION, 0x2D, TVP7002_WRITE },
  271. { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
  272. { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
  273. { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
  274. { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
  275. { TVP7002_HPLL_PRE_COAST, 0x00, TVP7002_WRITE },
  276. { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
  277. { TVP7002_EOR, 0xff, TVP7002_RESERVED }
  278. };
  279. /* Register parameters for 720P50 */
  280. static const struct i2c_reg_value tvp7002_parms_720P50[] = {
  281. { TVP7002_HPLL_FDBK_DIV_MSBS, 0x7b, TVP7002_WRITE },
  282. { TVP7002_HPLL_FDBK_DIV_LSBS, 0xc0, TVP7002_WRITE },
  283. { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
  284. { TVP7002_AVID_START_PIXEL_LSBS, 0x47, TVP7002_WRITE },
  285. { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
  286. { TVP7002_AVID_STOP_PIXEL_LSBS, 0x4B, TVP7002_WRITE },
  287. { TVP7002_AVID_STOP_PIXEL_MSBS, 0x06, TVP7002_WRITE },
  288. { TVP7002_VBLK_F_0_START_L_OFF, 0x05, TVP7002_WRITE },
  289. { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
  290. { TVP7002_VBLK_F_0_DURATION, 0x2D, TVP7002_WRITE },
  291. { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
  292. { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
  293. { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
  294. { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
  295. { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
  296. { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
  297. { TVP7002_EOR, 0xff, TVP7002_RESERVED }
  298. };
  299. /* Timings definition for handling device operation */
  300. struct tvp7002_timings_definition {
  301. struct v4l2_dv_timings timings;
  302. const struct i2c_reg_value *p_settings;
  303. enum v4l2_colorspace color_space;
  304. enum v4l2_field scanmode;
  305. u16 progressive;
  306. u16 lines_per_frame;
  307. u16 cpl_min;
  308. u16 cpl_max;
  309. };
  310. /* Struct list for digital video timings */
  311. static const struct tvp7002_timings_definition tvp7002_timings[] = {
  312. {
  313. V4L2_DV_BT_CEA_1280X720P60,
  314. tvp7002_parms_720P60,
  315. V4L2_COLORSPACE_REC709,
  316. V4L2_FIELD_NONE,
  317. 1,
  318. 0x2EE,
  319. 135,
  320. 153
  321. },
  322. {
  323. V4L2_DV_BT_CEA_1920X1080I60,
  324. tvp7002_parms_1080I60,
  325. V4L2_COLORSPACE_REC709,
  326. V4L2_FIELD_INTERLACED,
  327. 0,
  328. 0x465,
  329. 181,
  330. 205
  331. },
  332. {
  333. V4L2_DV_BT_CEA_1920X1080I50,
  334. tvp7002_parms_1080I50,
  335. V4L2_COLORSPACE_REC709,
  336. V4L2_FIELD_INTERLACED,
  337. 0,
  338. 0x465,
  339. 217,
  340. 245
  341. },
  342. {
  343. V4L2_DV_BT_CEA_1280X720P50,
  344. tvp7002_parms_720P50,
  345. V4L2_COLORSPACE_REC709,
  346. V4L2_FIELD_NONE,
  347. 1,
  348. 0x2EE,
  349. 163,
  350. 183
  351. },
  352. {
  353. V4L2_DV_BT_CEA_1920X1080P60,
  354. tvp7002_parms_1080P60,
  355. V4L2_COLORSPACE_REC709,
  356. V4L2_FIELD_NONE,
  357. 1,
  358. 0x465,
  359. 90,
  360. 102
  361. },
  362. {
  363. V4L2_DV_BT_CEA_720X480P59_94,
  364. tvp7002_parms_480P,
  365. V4L2_COLORSPACE_SMPTE170M,
  366. V4L2_FIELD_NONE,
  367. 1,
  368. 0x20D,
  369. 0xffff,
  370. 0xffff
  371. },
  372. {
  373. V4L2_DV_BT_CEA_720X576P50,
  374. tvp7002_parms_576P,
  375. V4L2_COLORSPACE_SMPTE170M,
  376. V4L2_FIELD_NONE,
  377. 1,
  378. 0x271,
  379. 0xffff,
  380. 0xffff
  381. }
  382. };
  383. #define NUM_TIMINGS ARRAY_SIZE(tvp7002_timings)
  384. /* Device definition */
  385. struct tvp7002 {
  386. struct v4l2_subdev sd;
  387. struct v4l2_ctrl_handler hdl;
  388. const struct tvp7002_config *pdata;
  389. int ver;
  390. int streaming;
  391. const struct tvp7002_timings_definition *current_timings;
  392. struct media_pad pad;
  393. };
  394. /*
  395. * to_tvp7002 - Obtain device handler TVP7002
  396. * @sd: ptr to v4l2_subdev struct
  397. *
  398. * Returns device handler tvp7002.
  399. */
  400. static inline struct tvp7002 *to_tvp7002(struct v4l2_subdev *sd)
  401. {
  402. return container_of(sd, struct tvp7002, sd);
  403. }
  404. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  405. {
  406. return &container_of(ctrl->handler, struct tvp7002, hdl)->sd;
  407. }
  408. /*
  409. * tvp7002_read - Read a value from a register in an TVP7002
  410. * @sd: ptr to v4l2_subdev struct
  411. * @addr: TVP7002 register address
  412. * @dst: pointer to 8-bit destination
  413. *
  414. * Returns value read if successful, or non-zero (-1) otherwise.
  415. */
  416. static int tvp7002_read(struct v4l2_subdev *sd, u8 addr, u8 *dst)
  417. {
  418. struct i2c_client *c = v4l2_get_subdevdata(sd);
  419. int retry;
  420. int error;
  421. for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
  422. error = i2c_smbus_read_byte_data(c, addr);
  423. if (error >= 0) {
  424. *dst = (u8)error;
  425. return 0;
  426. }
  427. msleep_interruptible(10);
  428. }
  429. v4l2_err(sd, "TVP7002 read error %d\n", error);
  430. return error;
  431. }
  432. /*
  433. * tvp7002_read_err() - Read a register value with error code
  434. * @sd: pointer to standard V4L2 sub-device structure
  435. * @reg: destination register
  436. * @val: value to be read
  437. * @err: pointer to error value
  438. *
  439. * Read a value in a register and save error value in pointer.
  440. * Also update the register table if successful
  441. */
  442. static inline void tvp7002_read_err(struct v4l2_subdev *sd, u8 reg,
  443. u8 *dst, int *err)
  444. {
  445. if (!*err)
  446. *err = tvp7002_read(sd, reg, dst);
  447. }
  448. /*
  449. * tvp7002_write() - Write a value to a register in TVP7002
  450. * @sd: ptr to v4l2_subdev struct
  451. * @addr: TVP7002 register address
  452. * @value: value to be written to the register
  453. *
  454. * Write a value to a register in an TVP7002 decoder device.
  455. * Returns zero if successful, or non-zero otherwise.
  456. */
  457. static int tvp7002_write(struct v4l2_subdev *sd, u8 addr, u8 value)
  458. {
  459. struct i2c_client *c;
  460. int retry;
  461. int error;
  462. c = v4l2_get_subdevdata(sd);
  463. for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
  464. error = i2c_smbus_write_byte_data(c, addr, value);
  465. if (error >= 0)
  466. return 0;
  467. v4l2_warn(sd, "Write: retry ... %d\n", retry);
  468. msleep_interruptible(10);
  469. }
  470. v4l2_err(sd, "TVP7002 write error %d\n", error);
  471. return error;
  472. }
  473. /*
  474. * tvp7002_write_err() - Write a register value with error code
  475. * @sd: pointer to standard V4L2 sub-device structure
  476. * @reg: destination register
  477. * @val: value to be written
  478. * @err: pointer to error value
  479. *
  480. * Write a value in a register and save error value in pointer.
  481. * Also update the register table if successful
  482. */
  483. static inline void tvp7002_write_err(struct v4l2_subdev *sd, u8 reg,
  484. u8 val, int *err)
  485. {
  486. if (!*err)
  487. *err = tvp7002_write(sd, reg, val);
  488. }
  489. /*
  490. * tvp7002_write_inittab() - Write initialization values
  491. * @sd: ptr to v4l2_subdev struct
  492. * @regs: ptr to i2c_reg_value struct
  493. *
  494. * Write initialization values.
  495. * Returns zero or -EINVAL if read operation fails.
  496. */
  497. static int tvp7002_write_inittab(struct v4l2_subdev *sd,
  498. const struct i2c_reg_value *regs)
  499. {
  500. int error = 0;
  501. /* Initialize the first (defined) registers */
  502. while (TVP7002_EOR != regs->reg) {
  503. if (TVP7002_WRITE == regs->type)
  504. tvp7002_write_err(sd, regs->reg, regs->value, &error);
  505. regs++;
  506. }
  507. return error;
  508. }
  509. static int tvp7002_s_dv_timings(struct v4l2_subdev *sd,
  510. struct v4l2_dv_timings *dv_timings)
  511. {
  512. struct tvp7002 *device = to_tvp7002(sd);
  513. const struct v4l2_bt_timings *bt = &dv_timings->bt;
  514. int i;
  515. if (dv_timings->type != V4L2_DV_BT_656_1120)
  516. return -EINVAL;
  517. for (i = 0; i < NUM_TIMINGS; i++) {
  518. const struct v4l2_bt_timings *t = &tvp7002_timings[i].timings.bt;
  519. if (!memcmp(bt, t, &bt->standards - &bt->width)) {
  520. device->current_timings = &tvp7002_timings[i];
  521. return tvp7002_write_inittab(sd, tvp7002_timings[i].p_settings);
  522. }
  523. }
  524. return -EINVAL;
  525. }
  526. static int tvp7002_g_dv_timings(struct v4l2_subdev *sd,
  527. struct v4l2_dv_timings *dv_timings)
  528. {
  529. struct tvp7002 *device = to_tvp7002(sd);
  530. *dv_timings = device->current_timings->timings;
  531. return 0;
  532. }
  533. /*
  534. * tvp7002_s_ctrl() - Set a control
  535. * @ctrl: ptr to v4l2_ctrl struct
  536. *
  537. * Set a control in TVP7002 decoder device.
  538. * Returns zero when successful or -EINVAL if register access fails.
  539. */
  540. static int tvp7002_s_ctrl(struct v4l2_ctrl *ctrl)
  541. {
  542. struct v4l2_subdev *sd = to_sd(ctrl);
  543. int error = 0;
  544. switch (ctrl->id) {
  545. case V4L2_CID_GAIN:
  546. tvp7002_write_err(sd, TVP7002_R_FINE_GAIN, ctrl->val, &error);
  547. tvp7002_write_err(sd, TVP7002_G_FINE_GAIN, ctrl->val, &error);
  548. tvp7002_write_err(sd, TVP7002_B_FINE_GAIN, ctrl->val, &error);
  549. return error;
  550. }
  551. return -EINVAL;
  552. }
  553. /*
  554. * tvp7002_query_dv() - query DV timings
  555. * @sd: pointer to standard V4L2 sub-device structure
  556. * @index: index into the tvp7002_timings array
  557. *
  558. * Returns the current DV timings detected by TVP7002. If no active input is
  559. * detected, returns -EINVAL
  560. */
  561. static int tvp7002_query_dv(struct v4l2_subdev *sd, int *index)
  562. {
  563. const struct tvp7002_timings_definition *timings = tvp7002_timings;
  564. u8 progressive;
  565. u32 lpfr;
  566. u32 cpln;
  567. int error = 0;
  568. u8 lpf_lsb;
  569. u8 lpf_msb;
  570. u8 cpl_lsb;
  571. u8 cpl_msb;
  572. /* Return invalid index if no active input is detected */
  573. *index = NUM_TIMINGS;
  574. /* Read standards from device registers */
  575. tvp7002_read_err(sd, TVP7002_L_FRAME_STAT_LSBS, &lpf_lsb, &error);
  576. tvp7002_read_err(sd, TVP7002_L_FRAME_STAT_MSBS, &lpf_msb, &error);
  577. if (error < 0)
  578. return error;
  579. tvp7002_read_err(sd, TVP7002_CLK_L_STAT_LSBS, &cpl_lsb, &error);
  580. tvp7002_read_err(sd, TVP7002_CLK_L_STAT_MSBS, &cpl_msb, &error);
  581. if (error < 0)
  582. return error;
  583. /* Get lines per frame, clocks per line and interlaced/progresive */
  584. lpfr = lpf_lsb | ((TVP7002_CL_MASK & lpf_msb) << TVP7002_CL_SHIFT);
  585. cpln = cpl_lsb | ((TVP7002_CL_MASK & cpl_msb) << TVP7002_CL_SHIFT);
  586. progressive = (lpf_msb & TVP7002_INPR_MASK) >> TVP7002_IP_SHIFT;
  587. /* Do checking of video modes */
  588. for (*index = 0; *index < NUM_TIMINGS; (*index)++, timings++)
  589. if (lpfr == timings->lines_per_frame &&
  590. progressive == timings->progressive) {
  591. if (timings->cpl_min == 0xffff)
  592. break;
  593. if (cpln >= timings->cpl_min && cpln <= timings->cpl_max)
  594. break;
  595. }
  596. if (*index == NUM_TIMINGS) {
  597. v4l2_dbg(1, debug, sd, "detection failed: lpf = %x, cpl = %x\n",
  598. lpfr, cpln);
  599. return -ENOLINK;
  600. }
  601. /* Update lines per frame and clocks per line info */
  602. v4l2_dbg(1, debug, sd, "detected timings: %d\n", *index);
  603. return 0;
  604. }
  605. static int tvp7002_query_dv_timings(struct v4l2_subdev *sd,
  606. struct v4l2_dv_timings *timings)
  607. {
  608. int index;
  609. int err = tvp7002_query_dv(sd, &index);
  610. if (err)
  611. return err;
  612. *timings = tvp7002_timings[index].timings;
  613. return 0;
  614. }
  615. #ifdef CONFIG_VIDEO_ADV_DEBUG
  616. /*
  617. * tvp7002_g_register() - Get the value of a register
  618. * @sd: ptr to v4l2_subdev struct
  619. * @reg: ptr to v4l2_dbg_register struct
  620. *
  621. * Get the value of a TVP7002 decoder device register.
  622. * Returns zero when successful, -EINVAL if register read fails or
  623. * access to I2C client fails.
  624. */
  625. static int tvp7002_g_register(struct v4l2_subdev *sd,
  626. struct v4l2_dbg_register *reg)
  627. {
  628. u8 val;
  629. int ret;
  630. ret = tvp7002_read(sd, reg->reg & 0xff, &val);
  631. if (ret < 0)
  632. return ret;
  633. reg->val = val;
  634. reg->size = 1;
  635. return 0;
  636. }
  637. /*
  638. * tvp7002_s_register() - set a control
  639. * @sd: ptr to v4l2_subdev struct
  640. * @reg: ptr to v4l2_dbg_register struct
  641. *
  642. * Get the value of a TVP7002 decoder device register.
  643. * Returns zero when successful, -EINVAL if register read fails.
  644. */
  645. static int tvp7002_s_register(struct v4l2_subdev *sd,
  646. const struct v4l2_dbg_register *reg)
  647. {
  648. return tvp7002_write(sd, reg->reg & 0xff, reg->val & 0xff);
  649. }
  650. #endif
  651. /*
  652. * tvp7002_s_stream() - V4L2 decoder i/f handler for s_stream
  653. * @sd: pointer to standard V4L2 sub-device structure
  654. * @enable: streaming enable or disable
  655. *
  656. * Sets streaming to enable or disable, if possible.
  657. */
  658. static int tvp7002_s_stream(struct v4l2_subdev *sd, int enable)
  659. {
  660. struct tvp7002 *device = to_tvp7002(sd);
  661. int error;
  662. if (device->streaming == enable)
  663. return 0;
  664. /* low impedance: on, high impedance: off */
  665. error = tvp7002_write(sd, TVP7002_MISC_CTL_2, enable ? 0x00 : 0x03);
  666. if (error) {
  667. v4l2_dbg(1, debug, sd, "Fail to set streaming\n");
  668. return error;
  669. }
  670. device->streaming = enable;
  671. return 0;
  672. }
  673. /*
  674. * tvp7002_log_status() - Print information about register settings
  675. * @sd: ptr to v4l2_subdev struct
  676. *
  677. * Log register values of a TVP7002 decoder device.
  678. * Returns zero or -EINVAL if read operation fails.
  679. */
  680. static int tvp7002_log_status(struct v4l2_subdev *sd)
  681. {
  682. struct tvp7002 *device = to_tvp7002(sd);
  683. const struct v4l2_bt_timings *bt;
  684. int detected;
  685. /* Find my current timings */
  686. tvp7002_query_dv(sd, &detected);
  687. bt = &device->current_timings->timings.bt;
  688. v4l2_info(sd, "Selected DV Timings: %ux%u\n", bt->width, bt->height);
  689. if (detected == NUM_TIMINGS) {
  690. v4l2_info(sd, "Detected DV Timings: None\n");
  691. } else {
  692. bt = &tvp7002_timings[detected].timings.bt;
  693. v4l2_info(sd, "Detected DV Timings: %ux%u\n",
  694. bt->width, bt->height);
  695. }
  696. v4l2_info(sd, "Streaming enabled: %s\n",
  697. device->streaming ? "yes" : "no");
  698. /* Print the current value of the gain control */
  699. v4l2_ctrl_handler_log_status(&device->hdl, sd->name);
  700. return 0;
  701. }
  702. static int tvp7002_enum_dv_timings(struct v4l2_subdev *sd,
  703. struct v4l2_enum_dv_timings *timings)
  704. {
  705. if (timings->pad != 0)
  706. return -EINVAL;
  707. /* Check requested format index is within range */
  708. if (timings->index >= NUM_TIMINGS)
  709. return -EINVAL;
  710. timings->timings = tvp7002_timings[timings->index].timings;
  711. return 0;
  712. }
  713. static const struct v4l2_ctrl_ops tvp7002_ctrl_ops = {
  714. .s_ctrl = tvp7002_s_ctrl,
  715. };
  716. /*
  717. * tvp7002_enum_mbus_code() - Enum supported digital video format on pad
  718. * @sd: pointer to standard V4L2 sub-device structure
  719. * @cfg: pad configuration
  720. * @code: pointer to subdev enum mbus code struct
  721. *
  722. * Enumerate supported digital video formats for pad.
  723. */
  724. static int
  725. tvp7002_enum_mbus_code(struct v4l2_subdev *sd,
  726. struct v4l2_subdev_state *sd_state,
  727. struct v4l2_subdev_mbus_code_enum *code)
  728. {
  729. /* Check requested format index is within range */
  730. if (code->index != 0)
  731. return -EINVAL;
  732. code->code = MEDIA_BUS_FMT_YUYV10_1X20;
  733. return 0;
  734. }
  735. /*
  736. * tvp7002_get_pad_format() - get video format on pad
  737. * @sd: pointer to standard V4L2 sub-device structure
  738. * @cfg: pad configuration
  739. * @fmt: pointer to subdev format struct
  740. *
  741. * get video format for pad.
  742. */
  743. static int
  744. tvp7002_get_pad_format(struct v4l2_subdev *sd,
  745. struct v4l2_subdev_state *sd_state,
  746. struct v4l2_subdev_format *fmt)
  747. {
  748. struct tvp7002 *tvp7002 = to_tvp7002(sd);
  749. fmt->format.code = MEDIA_BUS_FMT_YUYV10_1X20;
  750. fmt->format.width = tvp7002->current_timings->timings.bt.width;
  751. fmt->format.height = tvp7002->current_timings->timings.bt.height;
  752. fmt->format.field = tvp7002->current_timings->scanmode;
  753. fmt->format.colorspace = tvp7002->current_timings->color_space;
  754. return 0;
  755. }
  756. /*
  757. * tvp7002_set_pad_format() - set video format on pad
  758. * @sd: pointer to standard V4L2 sub-device structure
  759. * @cfg: pad configuration
  760. * @fmt: pointer to subdev format struct
  761. *
  762. * set video format for pad.
  763. */
  764. static int
  765. tvp7002_set_pad_format(struct v4l2_subdev *sd,
  766. struct v4l2_subdev_state *sd_state,
  767. struct v4l2_subdev_format *fmt)
  768. {
  769. return tvp7002_get_pad_format(sd, sd_state, fmt);
  770. }
  771. /* V4L2 core operation handlers */
  772. static const struct v4l2_subdev_core_ops tvp7002_core_ops = {
  773. .log_status = tvp7002_log_status,
  774. #ifdef CONFIG_VIDEO_ADV_DEBUG
  775. .g_register = tvp7002_g_register,
  776. .s_register = tvp7002_s_register,
  777. #endif
  778. };
  779. /* Specific video subsystem operation handlers */
  780. static const struct v4l2_subdev_video_ops tvp7002_video_ops = {
  781. .g_dv_timings = tvp7002_g_dv_timings,
  782. .s_dv_timings = tvp7002_s_dv_timings,
  783. .query_dv_timings = tvp7002_query_dv_timings,
  784. .s_stream = tvp7002_s_stream,
  785. };
  786. /* media pad related operation handlers */
  787. static const struct v4l2_subdev_pad_ops tvp7002_pad_ops = {
  788. .enum_mbus_code = tvp7002_enum_mbus_code,
  789. .get_fmt = tvp7002_get_pad_format,
  790. .set_fmt = tvp7002_set_pad_format,
  791. .enum_dv_timings = tvp7002_enum_dv_timings,
  792. };
  793. /* V4L2 top level operation handlers */
  794. static const struct v4l2_subdev_ops tvp7002_ops = {
  795. .core = &tvp7002_core_ops,
  796. .video = &tvp7002_video_ops,
  797. .pad = &tvp7002_pad_ops,
  798. };
  799. static struct tvp7002_config *
  800. tvp7002_get_pdata(struct i2c_client *client)
  801. {
  802. struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
  803. struct tvp7002_config *pdata = NULL;
  804. struct device_node *endpoint;
  805. unsigned int flags;
  806. if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
  807. return client->dev.platform_data;
  808. endpoint = of_graph_get_next_endpoint(client->dev.of_node, NULL);
  809. if (!endpoint)
  810. return NULL;
  811. if (v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint), &bus_cfg))
  812. goto done;
  813. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  814. if (!pdata)
  815. goto done;
  816. flags = bus_cfg.bus.parallel.flags;
  817. if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  818. pdata->hs_polarity = 1;
  819. if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  820. pdata->vs_polarity = 1;
  821. if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  822. pdata->clk_polarity = 1;
  823. if (flags & V4L2_MBUS_FIELD_EVEN_HIGH)
  824. pdata->fid_polarity = 1;
  825. if (flags & V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH)
  826. pdata->sog_polarity = 1;
  827. done:
  828. of_node_put(endpoint);
  829. return pdata;
  830. }
  831. /*
  832. * tvp7002_probe - Probe a TVP7002 device
  833. * @c: ptr to i2c_client struct
  834. * @id: ptr to i2c_device_id struct
  835. *
  836. * Initialize the TVP7002 device
  837. * Returns zero when successful, -EINVAL if register read fails or
  838. * -EIO if i2c access is not available.
  839. */
  840. static int tvp7002_probe(struct i2c_client *c)
  841. {
  842. struct tvp7002_config *pdata = tvp7002_get_pdata(c);
  843. struct v4l2_subdev *sd;
  844. struct tvp7002 *device;
  845. struct v4l2_dv_timings timings;
  846. int polarity_a;
  847. int polarity_b;
  848. u8 revision;
  849. int error;
  850. if (pdata == NULL) {
  851. dev_err(&c->dev, "No platform data\n");
  852. return -EINVAL;
  853. }
  854. /* Check if the adapter supports the needed features */
  855. if (!i2c_check_functionality(c->adapter,
  856. I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  857. return -EIO;
  858. device = devm_kzalloc(&c->dev, sizeof(struct tvp7002), GFP_KERNEL);
  859. if (!device)
  860. return -ENOMEM;
  861. sd = &device->sd;
  862. device->pdata = pdata;
  863. device->current_timings = tvp7002_timings;
  864. /* Tell v4l2 the device is ready */
  865. v4l2_i2c_subdev_init(sd, c, &tvp7002_ops);
  866. v4l_info(c, "tvp7002 found @ 0x%02x (%s)\n",
  867. c->addr, c->adapter->name);
  868. error = tvp7002_read(sd, TVP7002_CHIP_REV, &revision);
  869. if (error < 0)
  870. return error;
  871. /* Get revision number */
  872. v4l2_info(sd, "Rev. %02x detected.\n", revision);
  873. if (revision != 0x02)
  874. v4l2_info(sd, "Unknown revision detected.\n");
  875. /* Initializes TVP7002 to its default values */
  876. error = tvp7002_write_inittab(sd, tvp7002_init_default);
  877. if (error < 0)
  878. return error;
  879. /* Set polarity information after registers have been set */
  880. polarity_a = 0x20 | device->pdata->hs_polarity << 5
  881. | device->pdata->vs_polarity << 2;
  882. error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity_a);
  883. if (error < 0)
  884. return error;
  885. polarity_b = 0x01 | device->pdata->fid_polarity << 2
  886. | device->pdata->sog_polarity << 1
  887. | device->pdata->clk_polarity;
  888. error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity_b);
  889. if (error < 0)
  890. return error;
  891. /* Set registers according to default video mode */
  892. timings = device->current_timings->timings;
  893. error = tvp7002_s_dv_timings(sd, &timings);
  894. #if defined(CONFIG_MEDIA_CONTROLLER)
  895. device->pad.flags = MEDIA_PAD_FL_SOURCE;
  896. device->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  897. device->sd.entity.function = MEDIA_ENT_F_ATV_DECODER;
  898. error = media_entity_pads_init(&device->sd.entity, 1, &device->pad);
  899. if (error < 0)
  900. return error;
  901. #endif
  902. v4l2_ctrl_handler_init(&device->hdl, 1);
  903. v4l2_ctrl_new_std(&device->hdl, &tvp7002_ctrl_ops,
  904. V4L2_CID_GAIN, 0, 255, 1, 0);
  905. sd->ctrl_handler = &device->hdl;
  906. if (device->hdl.error) {
  907. error = device->hdl.error;
  908. goto error;
  909. }
  910. v4l2_ctrl_handler_setup(&device->hdl);
  911. error = v4l2_async_register_subdev(&device->sd);
  912. if (error)
  913. goto error;
  914. return 0;
  915. error:
  916. v4l2_ctrl_handler_free(&device->hdl);
  917. #if defined(CONFIG_MEDIA_CONTROLLER)
  918. media_entity_cleanup(&device->sd.entity);
  919. #endif
  920. return error;
  921. }
  922. /*
  923. * tvp7002_remove - Remove TVP7002 device support
  924. * @c: ptr to i2c_client struct
  925. *
  926. * Reset the TVP7002 device
  927. * Returns zero.
  928. */
  929. static void tvp7002_remove(struct i2c_client *c)
  930. {
  931. struct v4l2_subdev *sd = i2c_get_clientdata(c);
  932. struct tvp7002 *device = to_tvp7002(sd);
  933. v4l2_dbg(1, debug, sd, "Removing tvp7002 adapter"
  934. "on address 0x%x\n", c->addr);
  935. v4l2_async_unregister_subdev(&device->sd);
  936. #if defined(CONFIG_MEDIA_CONTROLLER)
  937. media_entity_cleanup(&device->sd.entity);
  938. #endif
  939. v4l2_ctrl_handler_free(&device->hdl);
  940. }
  941. /* I2C Device ID table */
  942. static const struct i2c_device_id tvp7002_id[] = {
  943. { "tvp7002", 0 },
  944. { }
  945. };
  946. MODULE_DEVICE_TABLE(i2c, tvp7002_id);
  947. #if IS_ENABLED(CONFIG_OF)
  948. static const struct of_device_id tvp7002_of_match[] = {
  949. { .compatible = "ti,tvp7002", },
  950. { /* sentinel */ },
  951. };
  952. MODULE_DEVICE_TABLE(of, tvp7002_of_match);
  953. #endif
  954. /* I2C driver data */
  955. static struct i2c_driver tvp7002_driver = {
  956. .driver = {
  957. .of_match_table = of_match_ptr(tvp7002_of_match),
  958. .name = TVP7002_MODULE_NAME,
  959. },
  960. .probe_new = tvp7002_probe,
  961. .remove = tvp7002_remove,
  962. .id_table = tvp7002_id,
  963. };
  964. module_i2c_driver(tvp7002_driver);