tvp7002.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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 <media/tvp7002.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/v4l2-chip-ident.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ctrls.h>
  36. #include "tvp7002_reg.h"
  37. MODULE_DESCRIPTION("TI TVP7002 Video and Graphics Digitizer driver");
  38. MODULE_AUTHOR("Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>");
  39. MODULE_LICENSE("GPL");
  40. /* Module Name */
  41. #define TVP7002_MODULE_NAME "tvp7002"
  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 int 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. /* Preset definition for handling device operation */
  310. struct tvp7002_preset_definition {
  311. u32 preset;
  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 presets */
  321. static const struct tvp7002_preset_definition tvp7002_presets[] = {
  322. {
  323. V4L2_DV_720P60,
  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_1080I60,
  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_1080I50,
  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_720P50,
  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_1080P60,
  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_480P59_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_576P50,
  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_PRESETS ARRAY_SIZE(tvp7002_presets)
  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_preset_definition *current_preset;
  402. };
  403. /*
  404. * to_tvp7002 - Obtain device handler TVP7002
  405. * @sd: ptr to v4l2_subdev struct
  406. *
  407. * Returns device handler tvp7002.
  408. */
  409. static inline struct tvp7002 *to_tvp7002(struct v4l2_subdev *sd)
  410. {
  411. return container_of(sd, struct tvp7002, sd);
  412. }
  413. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  414. {
  415. return &container_of(ctrl->handler, struct tvp7002, hdl)->sd;
  416. }
  417. /*
  418. * tvp7002_read - Read a value from a register in an TVP7002
  419. * @sd: ptr to v4l2_subdev struct
  420. * @addr: TVP7002 register address
  421. * @dst: pointer to 8-bit destination
  422. *
  423. * Returns value read if successful, or non-zero (-1) otherwise.
  424. */
  425. static int tvp7002_read(struct v4l2_subdev *sd, u8 addr, u8 *dst)
  426. {
  427. struct i2c_client *c = v4l2_get_subdevdata(sd);
  428. int retry;
  429. int error;
  430. for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
  431. error = i2c_smbus_read_byte_data(c, addr);
  432. if (error >= 0) {
  433. *dst = (u8)error;
  434. return 0;
  435. }
  436. msleep_interruptible(10);
  437. }
  438. v4l2_err(sd, "TVP7002 read error %d\n", error);
  439. return error;
  440. }
  441. /*
  442. * tvp7002_read_err() - Read a register value with error code
  443. * @sd: pointer to standard V4L2 sub-device structure
  444. * @reg: destination register
  445. * @val: value to be read
  446. * @err: pointer to error value
  447. *
  448. * Read a value in a register and save error value in pointer.
  449. * Also update the register table if successful
  450. */
  451. static inline void tvp7002_read_err(struct v4l2_subdev *sd, u8 reg,
  452. u8 *dst, int *err)
  453. {
  454. if (!*err)
  455. *err = tvp7002_read(sd, reg, dst);
  456. }
  457. /*
  458. * tvp7002_write() - Write a value to a register in TVP7002
  459. * @sd: ptr to v4l2_subdev struct
  460. * @addr: TVP7002 register address
  461. * @value: value to be written to the register
  462. *
  463. * Write a value to a register in an TVP7002 decoder device.
  464. * Returns zero if successful, or non-zero otherwise.
  465. */
  466. static int tvp7002_write(struct v4l2_subdev *sd, u8 addr, u8 value)
  467. {
  468. struct i2c_client *c;
  469. int retry;
  470. int error;
  471. c = v4l2_get_subdevdata(sd);
  472. for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
  473. error = i2c_smbus_write_byte_data(c, addr, value);
  474. if (error >= 0)
  475. return 0;
  476. v4l2_warn(sd, "Write: retry ... %d\n", retry);
  477. msleep_interruptible(10);
  478. }
  479. v4l2_err(sd, "TVP7002 write error %d\n", error);
  480. return error;
  481. }
  482. /*
  483. * tvp7002_write_err() - Write a register value with error code
  484. * @sd: pointer to standard V4L2 sub-device structure
  485. * @reg: destination register
  486. * @val: value to be written
  487. * @err: pointer to error value
  488. *
  489. * Write a value in a register and save error value in pointer.
  490. * Also update the register table if successful
  491. */
  492. static inline void tvp7002_write_err(struct v4l2_subdev *sd, u8 reg,
  493. u8 val, int *err)
  494. {
  495. if (!*err)
  496. *err = tvp7002_write(sd, reg, val);
  497. }
  498. /*
  499. * tvp7002_g_chip_ident() - Get chip identification number
  500. * @sd: ptr to v4l2_subdev struct
  501. * @chip: ptr to v4l2_dbg_chip_ident struct
  502. *
  503. * Obtains the chip's identification number.
  504. * Returns zero or -EINVAL if read operation fails.
  505. */
  506. static int tvp7002_g_chip_ident(struct v4l2_subdev *sd,
  507. struct v4l2_dbg_chip_ident *chip)
  508. {
  509. u8 rev;
  510. int error;
  511. struct i2c_client *client = v4l2_get_subdevdata(sd);
  512. error = tvp7002_read(sd, TVP7002_CHIP_REV, &rev);
  513. if (error < 0)
  514. return error;
  515. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_TVP7002, rev);
  516. }
  517. /*
  518. * tvp7002_write_inittab() - Write initialization values
  519. * @sd: ptr to v4l2_subdev struct
  520. * @regs: ptr to i2c_reg_value struct
  521. *
  522. * Write initialization values.
  523. * Returns zero or -EINVAL if read operation fails.
  524. */
  525. static int tvp7002_write_inittab(struct v4l2_subdev *sd,
  526. const struct i2c_reg_value *regs)
  527. {
  528. int error = 0;
  529. /* Initialize the first (defined) registers */
  530. while (TVP7002_EOR != regs->reg) {
  531. if (TVP7002_WRITE == regs->type)
  532. tvp7002_write_err(sd, regs->reg, regs->value, &error);
  533. regs++;
  534. }
  535. return error;
  536. }
  537. /*
  538. * tvp7002_s_dv_preset() - Set digital video preset
  539. * @sd: ptr to v4l2_subdev struct
  540. * @dv_preset: ptr to v4l2_dv_preset struct
  541. *
  542. * Set the digital video preset for a TVP7002 decoder device.
  543. * Returns zero when successful or -EINVAL if register access fails.
  544. */
  545. static int tvp7002_s_dv_preset(struct v4l2_subdev *sd,
  546. struct v4l2_dv_preset *dv_preset)
  547. {
  548. struct tvp7002 *device = to_tvp7002(sd);
  549. u32 preset;
  550. int i;
  551. for (i = 0; i < NUM_PRESETS; i++) {
  552. preset = tvp7002_presets[i].preset;
  553. if (preset == dv_preset->preset) {
  554. device->current_preset = &tvp7002_presets[i];
  555. return tvp7002_write_inittab(sd, tvp7002_presets[i].p_settings);
  556. }
  557. }
  558. return -EINVAL;
  559. }
  560. /*
  561. * tvp7002_s_ctrl() - Set a control
  562. * @ctrl: ptr to v4l2_ctrl struct
  563. *
  564. * Set a control in TVP7002 decoder device.
  565. * Returns zero when successful or -EINVAL if register access fails.
  566. */
  567. static int tvp7002_s_ctrl(struct v4l2_ctrl *ctrl)
  568. {
  569. struct v4l2_subdev *sd = to_sd(ctrl);
  570. int error = 0;
  571. switch (ctrl->id) {
  572. case V4L2_CID_GAIN:
  573. tvp7002_write_err(sd, TVP7002_R_FINE_GAIN, ctrl->val, &error);
  574. tvp7002_write_err(sd, TVP7002_G_FINE_GAIN, ctrl->val, &error);
  575. tvp7002_write_err(sd, TVP7002_B_FINE_GAIN, ctrl->val, &error);
  576. return error;
  577. }
  578. return -EINVAL;
  579. }
  580. /*
  581. * tvp7002_mbus_fmt() - V4L2 decoder interface handler for try/s/g_mbus_fmt
  582. * @sd: pointer to standard V4L2 sub-device structure
  583. * @f: pointer to mediabus format structure
  584. *
  585. * Negotiate the image capture size and mediabus format.
  586. * There is only one possible format, so this single function works for
  587. * get, set and try.
  588. */
  589. static int tvp7002_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
  590. {
  591. struct tvp7002 *device = to_tvp7002(sd);
  592. struct v4l2_dv_enum_preset e_preset;
  593. int error;
  594. /* Calculate height and width based on current standard */
  595. error = v4l_fill_dv_preset_info(device->current_preset->preset, &e_preset);
  596. if (error)
  597. return error;
  598. f->width = e_preset.width;
  599. f->height = e_preset.height;
  600. f->code = V4L2_MBUS_FMT_YUYV10_1X20;
  601. f->field = device->current_preset->scanmode;
  602. f->colorspace = device->current_preset->color_space;
  603. v4l2_dbg(1, debug, sd, "MBUS_FMT: Width - %d, Height - %d",
  604. f->width, f->height);
  605. return 0;
  606. }
  607. /*
  608. * tvp7002_query_dv_preset() - query DV preset
  609. * @sd: pointer to standard V4L2 sub-device structure
  610. * @qpreset: standard V4L2 v4l2_dv_preset structure
  611. *
  612. * Returns the current DV preset by TVP7002. If no active input is
  613. * detected, returns -EINVAL
  614. */
  615. static int tvp7002_query_dv_preset(struct v4l2_subdev *sd,
  616. struct v4l2_dv_preset *qpreset)
  617. {
  618. const struct tvp7002_preset_definition *presets = tvp7002_presets;
  619. struct tvp7002 *device;
  620. u8 progressive;
  621. u32 lpfr;
  622. u32 cpln;
  623. int error = 0;
  624. u8 lpf_lsb;
  625. u8 lpf_msb;
  626. u8 cpl_lsb;
  627. u8 cpl_msb;
  628. int index;
  629. /* Return invalid preset if no active input is detected */
  630. qpreset->preset = V4L2_DV_INVALID;
  631. device = to_tvp7002(sd);
  632. /* Read standards from device registers */
  633. tvp7002_read_err(sd, TVP7002_L_FRAME_STAT_LSBS, &lpf_lsb, &error);
  634. tvp7002_read_err(sd, TVP7002_L_FRAME_STAT_MSBS, &lpf_msb, &error);
  635. if (error < 0)
  636. return error;
  637. tvp7002_read_err(sd, TVP7002_CLK_L_STAT_LSBS, &cpl_lsb, &error);
  638. tvp7002_read_err(sd, TVP7002_CLK_L_STAT_MSBS, &cpl_msb, &error);
  639. if (error < 0)
  640. return error;
  641. /* Get lines per frame, clocks per line and interlaced/progresive */
  642. lpfr = lpf_lsb | ((TVP7002_CL_MASK & lpf_msb) << TVP7002_CL_SHIFT);
  643. cpln = cpl_lsb | ((TVP7002_CL_MASK & cpl_msb) << TVP7002_CL_SHIFT);
  644. progressive = (lpf_msb & TVP7002_INPR_MASK) >> TVP7002_IP_SHIFT;
  645. /* Do checking of video modes */
  646. for (index = 0; index < NUM_PRESETS; index++, presets++)
  647. if (lpfr == presets->lines_per_frame &&
  648. progressive == presets->progressive) {
  649. if (presets->cpl_min == 0xffff)
  650. break;
  651. if (cpln >= presets->cpl_min && cpln <= presets->cpl_max)
  652. break;
  653. }
  654. if (index == NUM_PRESETS) {
  655. v4l2_dbg(1, debug, sd, "detection failed: lpf = %x, cpl = %x\n",
  656. lpfr, cpln);
  657. return 0;
  658. }
  659. /* Set values in found preset */
  660. qpreset->preset = presets->preset;
  661. /* Update lines per frame and clocks per line info */
  662. v4l2_dbg(1, debug, sd, "detected preset: %d\n", presets->preset);
  663. return 0;
  664. }
  665. #ifdef CONFIG_VIDEO_ADV_DEBUG
  666. /*
  667. * tvp7002_g_register() - Get the value of a register
  668. * @sd: ptr to v4l2_subdev struct
  669. * @reg: ptr to v4l2_dbg_register struct
  670. *
  671. * Get the value of a TVP7002 decoder device register.
  672. * Returns zero when successful, -EINVAL if register read fails or
  673. * access to I2C client fails, -EPERM if the call is not allowed
  674. * by disabled CAP_SYS_ADMIN.
  675. */
  676. static int tvp7002_g_register(struct v4l2_subdev *sd,
  677. struct v4l2_dbg_register *reg)
  678. {
  679. struct i2c_client *client = v4l2_get_subdevdata(sd);
  680. u8 val;
  681. int ret;
  682. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  683. return -EINVAL;
  684. if (!capable(CAP_SYS_ADMIN))
  685. return -EPERM;
  686. ret = tvp7002_read(sd, reg->reg & 0xff, &val);
  687. reg->val = val;
  688. return ret;
  689. }
  690. /*
  691. * tvp7002_s_register() - set a control
  692. * @sd: ptr to v4l2_subdev struct
  693. * @reg: ptr to v4l2_dbg_register struct
  694. *
  695. * Get the value of a TVP7002 decoder device register.
  696. * Returns zero when successful, -EINVAL if register read fails or
  697. * -EPERM if call not allowed.
  698. */
  699. static int tvp7002_s_register(struct v4l2_subdev *sd,
  700. struct v4l2_dbg_register *reg)
  701. {
  702. struct i2c_client *client = v4l2_get_subdevdata(sd);
  703. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  704. return -EINVAL;
  705. if (!capable(CAP_SYS_ADMIN))
  706. return -EPERM;
  707. return tvp7002_write(sd, reg->reg & 0xff, reg->val & 0xff);
  708. }
  709. #endif
  710. /*
  711. * tvp7002_enum_mbus_fmt() - Enum supported mediabus formats
  712. * @sd: pointer to standard V4L2 sub-device structure
  713. * @index: format index
  714. * @code: pointer to mediabus format
  715. *
  716. * Enumerate supported mediabus formats.
  717. */
  718. static int tvp7002_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
  719. enum v4l2_mbus_pixelcode *code)
  720. {
  721. /* Check requested format index is within range */
  722. if (index)
  723. return -EINVAL;
  724. *code = V4L2_MBUS_FMT_YUYV10_1X20;
  725. return 0;
  726. }
  727. /*
  728. * tvp7002_s_stream() - V4L2 decoder i/f handler for s_stream
  729. * @sd: pointer to standard V4L2 sub-device structure
  730. * @enable: streaming enable or disable
  731. *
  732. * Sets streaming to enable or disable, if possible.
  733. */
  734. static int tvp7002_s_stream(struct v4l2_subdev *sd, int enable)
  735. {
  736. struct tvp7002 *device = to_tvp7002(sd);
  737. int error = 0;
  738. if (device->streaming == enable)
  739. return 0;
  740. if (enable) {
  741. /* Set output state on (low impedance means stream on) */
  742. error = tvp7002_write(sd, TVP7002_MISC_CTL_2, 0x00);
  743. device->streaming = enable;
  744. } else {
  745. /* Set output state off (high impedance means stream off) */
  746. error = tvp7002_write(sd, TVP7002_MISC_CTL_2, 0x03);
  747. if (error)
  748. v4l2_dbg(1, debug, sd, "Unable to stop streaming\n");
  749. device->streaming = enable;
  750. }
  751. return error;
  752. }
  753. /*
  754. * tvp7002_log_status() - Print information about register settings
  755. * @sd: ptr to v4l2_subdev struct
  756. *
  757. * Log register values of a TVP7002 decoder device.
  758. * Returns zero or -EINVAL if read operation fails.
  759. */
  760. static int tvp7002_log_status(struct v4l2_subdev *sd)
  761. {
  762. const struct tvp7002_preset_definition *presets = tvp7002_presets;
  763. struct tvp7002 *device = to_tvp7002(sd);
  764. struct v4l2_dv_enum_preset e_preset;
  765. struct v4l2_dv_preset detected;
  766. int i;
  767. detected.preset = V4L2_DV_INVALID;
  768. /* Find my current standard*/
  769. tvp7002_query_dv_preset(sd, &detected);
  770. /* Print standard related code values */
  771. for (i = 0; i < NUM_PRESETS; i++, presets++)
  772. if (presets->preset == detected.preset)
  773. break;
  774. if (v4l_fill_dv_preset_info(device->current_preset->preset, &e_preset))
  775. return -EINVAL;
  776. v4l2_info(sd, "Selected DV Preset: %s\n", e_preset.name);
  777. v4l2_info(sd, " Pixels per line: %u\n", e_preset.width);
  778. v4l2_info(sd, " Lines per frame: %u\n\n", e_preset.height);
  779. if (i == NUM_PRESETS) {
  780. v4l2_info(sd, "Detected DV Preset: None\n");
  781. } else {
  782. if (v4l_fill_dv_preset_info(presets->preset, &e_preset))
  783. return -EINVAL;
  784. v4l2_info(sd, "Detected DV Preset: %s\n", e_preset.name);
  785. v4l2_info(sd, " Pixels per line: %u\n", e_preset.width);
  786. v4l2_info(sd, " Lines per frame: %u\n\n", e_preset.height);
  787. }
  788. v4l2_info(sd, "Streaming enabled: %s\n",
  789. device->streaming ? "yes" : "no");
  790. /* Print the current value of the gain control */
  791. v4l2_ctrl_handler_log_status(&device->hdl, sd->name);
  792. return 0;
  793. }
  794. /*
  795. * tvp7002_enum_dv_presets() - Enum supported digital video formats
  796. * @sd: pointer to standard V4L2 sub-device structure
  797. * @preset: pointer to format struct
  798. *
  799. * Enumerate supported digital video formats.
  800. */
  801. static int tvp7002_enum_dv_presets(struct v4l2_subdev *sd,
  802. struct v4l2_dv_enum_preset *preset)
  803. {
  804. /* Check requested format index is within range */
  805. if (preset->index >= NUM_PRESETS)
  806. return -EINVAL;
  807. return v4l_fill_dv_preset_info(tvp7002_presets[preset->index].preset, preset);
  808. }
  809. static const struct v4l2_ctrl_ops tvp7002_ctrl_ops = {
  810. .s_ctrl = tvp7002_s_ctrl,
  811. };
  812. /* V4L2 core operation handlers */
  813. static const struct v4l2_subdev_core_ops tvp7002_core_ops = {
  814. .g_chip_ident = tvp7002_g_chip_ident,
  815. .log_status = tvp7002_log_status,
  816. .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
  817. .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
  818. .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
  819. .g_ctrl = v4l2_subdev_g_ctrl,
  820. .s_ctrl = v4l2_subdev_s_ctrl,
  821. .queryctrl = v4l2_subdev_queryctrl,
  822. .querymenu = v4l2_subdev_querymenu,
  823. #ifdef CONFIG_VIDEO_ADV_DEBUG
  824. .g_register = tvp7002_g_register,
  825. .s_register = tvp7002_s_register,
  826. #endif
  827. };
  828. /* Specific video subsystem operation handlers */
  829. static const struct v4l2_subdev_video_ops tvp7002_video_ops = {
  830. .enum_dv_presets = tvp7002_enum_dv_presets,
  831. .s_dv_preset = tvp7002_s_dv_preset,
  832. .query_dv_preset = tvp7002_query_dv_preset,
  833. .s_stream = tvp7002_s_stream,
  834. .g_mbus_fmt = tvp7002_mbus_fmt,
  835. .try_mbus_fmt = tvp7002_mbus_fmt,
  836. .s_mbus_fmt = tvp7002_mbus_fmt,
  837. .enum_mbus_fmt = tvp7002_enum_mbus_fmt,
  838. };
  839. /* V4L2 top level operation handlers */
  840. static const struct v4l2_subdev_ops tvp7002_ops = {
  841. .core = &tvp7002_core_ops,
  842. .video = &tvp7002_video_ops,
  843. };
  844. /*
  845. * tvp7002_probe - Probe a TVP7002 device
  846. * @c: ptr to i2c_client struct
  847. * @id: ptr to i2c_device_id struct
  848. *
  849. * Initialize the TVP7002 device
  850. * Returns zero when successful, -EINVAL if register read fails or
  851. * -EIO if i2c access is not available.
  852. */
  853. static int tvp7002_probe(struct i2c_client *c, const struct i2c_device_id *id)
  854. {
  855. struct v4l2_subdev *sd;
  856. struct tvp7002 *device;
  857. struct v4l2_dv_preset preset;
  858. int polarity_a;
  859. int polarity_b;
  860. u8 revision;
  861. int error;
  862. /* Check if the adapter supports the needed features */
  863. if (!i2c_check_functionality(c->adapter,
  864. I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  865. return -EIO;
  866. if (!c->dev.platform_data) {
  867. v4l_err(c, "No platform data!!\n");
  868. return -ENODEV;
  869. }
  870. device = kzalloc(sizeof(struct tvp7002), GFP_KERNEL);
  871. if (!device)
  872. return -ENOMEM;
  873. sd = &device->sd;
  874. device->pdata = c->dev.platform_data;
  875. device->current_preset = tvp7002_presets;
  876. /* Tell v4l2 the device is ready */
  877. v4l2_i2c_subdev_init(sd, c, &tvp7002_ops);
  878. v4l_info(c, "tvp7002 found @ 0x%02x (%s)\n",
  879. c->addr, c->adapter->name);
  880. error = tvp7002_read(sd, TVP7002_CHIP_REV, &revision);
  881. if (error < 0)
  882. goto found_error;
  883. /* Get revision number */
  884. v4l2_info(sd, "Rev. %02x detected.\n", revision);
  885. if (revision != 0x02)
  886. v4l2_info(sd, "Unknown revision detected.\n");
  887. /* Initializes TVP7002 to its default values */
  888. error = tvp7002_write_inittab(sd, tvp7002_init_default);
  889. if (error < 0)
  890. goto found_error;
  891. /* Set polarity information after registers have been set */
  892. polarity_a = 0x20 | device->pdata->hs_polarity << 5
  893. | device->pdata->vs_polarity << 2;
  894. error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity_a);
  895. if (error < 0)
  896. goto found_error;
  897. polarity_b = 0x01 | device->pdata->fid_polarity << 2
  898. | device->pdata->sog_polarity << 1
  899. | device->pdata->clk_polarity;
  900. error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity_b);
  901. if (error < 0)
  902. goto found_error;
  903. /* Set registers according to default video mode */
  904. preset.preset = device->current_preset->preset;
  905. error = tvp7002_s_dv_preset(sd, &preset);
  906. v4l2_ctrl_handler_init(&device->hdl, 1);
  907. v4l2_ctrl_new_std(&device->hdl, &tvp7002_ctrl_ops,
  908. V4L2_CID_GAIN, 0, 255, 1, 0);
  909. sd->ctrl_handler = &device->hdl;
  910. if (device->hdl.error) {
  911. int err = device->hdl.error;
  912. v4l2_ctrl_handler_free(&device->hdl);
  913. kfree(device);
  914. return err;
  915. }
  916. v4l2_ctrl_handler_setup(&device->hdl);
  917. found_error:
  918. if (error < 0)
  919. kfree(device);
  920. return error;
  921. }
  922. /*
  923. * tvp7002_remove - Remove TVP7002 device support
  924. * @c: ptr to i2c_client struct
  925. *
  926. * Reset the TVP7002 device
  927. * Returns zero.
  928. */
  929. static int tvp7002_remove(struct i2c_client *c)
  930. {
  931. struct v4l2_subdev *sd = i2c_get_clientdata(c);
  932. struct tvp7002 *device = to_tvp7002(sd);
  933. v4l2_dbg(1, debug, sd, "Removing tvp7002 adapter"
  934. "on address 0x%x\n", c->addr);
  935. v4l2_device_unregister_subdev(sd);
  936. v4l2_ctrl_handler_free(&device->hdl);
  937. kfree(device);
  938. return 0;
  939. }
  940. /* I2C Device ID table */
  941. static const struct i2c_device_id tvp7002_id[] = {
  942. { "tvp7002", 0 },
  943. { }
  944. };
  945. MODULE_DEVICE_TABLE(i2c, tvp7002_id);
  946. /* I2C driver data */
  947. static struct i2c_driver tvp7002_driver = {
  948. .driver = {
  949. .owner = THIS_MODULE,
  950. .name = TVP7002_MODULE_NAME,
  951. },
  952. .probe = tvp7002_probe,
  953. .remove = tvp7002_remove,
  954. .id_table = tvp7002_id,
  955. };
  956. /*
  957. * tvp7002_init - Initialize driver via I2C interface
  958. *
  959. * Register the TVP7002 driver.
  960. * Return 0 on success or error code on failure.
  961. */
  962. static int __init tvp7002_init(void)
  963. {
  964. return i2c_add_driver(&tvp7002_driver);
  965. }
  966. /*
  967. * tvp7002_exit - Remove driver via I2C interface
  968. *
  969. * Unregister the TVP7002 driver.
  970. * Returns nothing.
  971. */
  972. static void __exit tvp7002_exit(void)
  973. {
  974. i2c_del_driver(&tvp7002_driver);
  975. }
  976. module_init(tvp7002_init);
  977. module_exit(tvp7002_exit);