tvp7002.c 34 KB

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