tvp7002.c 35 KB

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