tvp7002.c 34 KB

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