tvp5150.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. /*
  2. * tvp5150 - Texas Instruments TVP5150A/AM1 video decoder driver
  3. *
  4. * Copyright (c) 2005,2006 Mauro Carvalho Chehab (mchehab@infradead.org)
  5. * This code is placed under the terms of the GNU General Public License v2
  6. */
  7. #include <linux/i2c.h>
  8. #include <linux/slab.h>
  9. #include <linux/videodev2.h>
  10. #include <linux/delay.h>
  11. #include <linux/module.h>
  12. #include <media/v4l2-device.h>
  13. #include <media/tvp5150.h>
  14. #include <media/v4l2-chip-ident.h>
  15. #include <media/v4l2-ctrls.h>
  16. #include "tvp5150_reg.h"
  17. #define TVP5150_H_MAX 720
  18. #define TVP5150_V_MAX_525_60 480
  19. #define TVP5150_V_MAX_OTHERS 576
  20. #define TVP5150_MAX_CROP_LEFT 511
  21. #define TVP5150_MAX_CROP_TOP 127
  22. #define TVP5150_CROP_SHIFT 2
  23. MODULE_DESCRIPTION("Texas Instruments TVP5150A video decoder driver");
  24. MODULE_AUTHOR("Mauro Carvalho Chehab");
  25. MODULE_LICENSE("GPL");
  26. static int debug;
  27. module_param(debug, int, 0);
  28. MODULE_PARM_DESC(debug, "Debug level (0-2)");
  29. struct tvp5150 {
  30. struct v4l2_subdev sd;
  31. struct v4l2_ctrl_handler hdl;
  32. struct v4l2_rect rect;
  33. v4l2_std_id norm; /* Current set standard */
  34. u32 input;
  35. u32 output;
  36. int enable;
  37. };
  38. static inline struct tvp5150 *to_tvp5150(struct v4l2_subdev *sd)
  39. {
  40. return container_of(sd, struct tvp5150, sd);
  41. }
  42. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  43. {
  44. return &container_of(ctrl->handler, struct tvp5150, hdl)->sd;
  45. }
  46. static int tvp5150_read(struct v4l2_subdev *sd, unsigned char addr)
  47. {
  48. struct i2c_client *c = v4l2_get_subdevdata(sd);
  49. unsigned char buffer[1];
  50. int rc;
  51. buffer[0] = addr;
  52. if (1 != (rc = i2c_master_send(c, buffer, 1)))
  53. v4l2_dbg(0, debug, sd, "i2c i/o error: rc == %d (should be 1)\n", rc);
  54. msleep(10);
  55. if (1 != (rc = i2c_master_recv(c, buffer, 1)))
  56. v4l2_dbg(0, debug, sd, "i2c i/o error: rc == %d (should be 1)\n", rc);
  57. v4l2_dbg(2, debug, sd, "tvp5150: read 0x%02x = 0x%02x\n", addr, buffer[0]);
  58. return (buffer[0]);
  59. }
  60. static inline void tvp5150_write(struct v4l2_subdev *sd, unsigned char addr,
  61. unsigned char value)
  62. {
  63. struct i2c_client *c = v4l2_get_subdevdata(sd);
  64. unsigned char buffer[2];
  65. int rc;
  66. buffer[0] = addr;
  67. buffer[1] = value;
  68. v4l2_dbg(2, debug, sd, "tvp5150: writing 0x%02x 0x%02x\n", buffer[0], buffer[1]);
  69. if (2 != (rc = i2c_master_send(c, buffer, 2)))
  70. v4l2_dbg(0, debug, sd, "i2c i/o error: rc == %d (should be 2)\n", rc);
  71. }
  72. static void dump_reg_range(struct v4l2_subdev *sd, char *s, u8 init,
  73. const u8 end, int max_line)
  74. {
  75. int i = 0;
  76. while (init != (u8)(end + 1)) {
  77. if ((i % max_line) == 0) {
  78. if (i > 0)
  79. printk("\n");
  80. printk("tvp5150: %s reg 0x%02x = ", s, init);
  81. }
  82. printk("%02x ", tvp5150_read(sd, init));
  83. init++;
  84. i++;
  85. }
  86. printk("\n");
  87. }
  88. static int tvp5150_log_status(struct v4l2_subdev *sd)
  89. {
  90. printk("tvp5150: Video input source selection #1 = 0x%02x\n",
  91. tvp5150_read(sd, TVP5150_VD_IN_SRC_SEL_1));
  92. printk("tvp5150: Analog channel controls = 0x%02x\n",
  93. tvp5150_read(sd, TVP5150_ANAL_CHL_CTL));
  94. printk("tvp5150: Operation mode controls = 0x%02x\n",
  95. tvp5150_read(sd, TVP5150_OP_MODE_CTL));
  96. printk("tvp5150: Miscellaneous controls = 0x%02x\n",
  97. tvp5150_read(sd, TVP5150_MISC_CTL));
  98. printk("tvp5150: Autoswitch mask= 0x%02x\n",
  99. tvp5150_read(sd, TVP5150_AUTOSW_MSK));
  100. printk("tvp5150: Color killer threshold control = 0x%02x\n",
  101. tvp5150_read(sd, TVP5150_COLOR_KIL_THSH_CTL));
  102. printk("tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
  103. tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_1),
  104. tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_2),
  105. tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_3));
  106. printk("tvp5150: Brightness control = 0x%02x\n",
  107. tvp5150_read(sd, TVP5150_BRIGHT_CTL));
  108. printk("tvp5150: Color saturation control = 0x%02x\n",
  109. tvp5150_read(sd, TVP5150_SATURATION_CTL));
  110. printk("tvp5150: Hue control = 0x%02x\n",
  111. tvp5150_read(sd, TVP5150_HUE_CTL));
  112. printk("tvp5150: Contrast control = 0x%02x\n",
  113. tvp5150_read(sd, TVP5150_CONTRAST_CTL));
  114. printk("tvp5150: Outputs and data rates select = 0x%02x\n",
  115. tvp5150_read(sd, TVP5150_DATA_RATE_SEL));
  116. printk("tvp5150: Configuration shared pins = 0x%02x\n",
  117. tvp5150_read(sd, TVP5150_CONF_SHARED_PIN));
  118. printk("tvp5150: Active video cropping start = 0x%02x%02x\n",
  119. tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_MSB),
  120. tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_LSB));
  121. printk("tvp5150: Active video cropping stop = 0x%02x%02x\n",
  122. tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_MSB),
  123. tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_LSB));
  124. printk("tvp5150: Genlock/RTC = 0x%02x\n",
  125. tvp5150_read(sd, TVP5150_GENLOCK));
  126. printk("tvp5150: Horizontal sync start = 0x%02x\n",
  127. tvp5150_read(sd, TVP5150_HORIZ_SYNC_START));
  128. printk("tvp5150: Vertical blanking start = 0x%02x\n",
  129. tvp5150_read(sd, TVP5150_VERT_BLANKING_START));
  130. printk("tvp5150: Vertical blanking stop = 0x%02x\n",
  131. tvp5150_read(sd, TVP5150_VERT_BLANKING_STOP));
  132. printk("tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
  133. tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_1),
  134. tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_2));
  135. printk("tvp5150: Interrupt reset register B = 0x%02x\n",
  136. tvp5150_read(sd, TVP5150_INT_RESET_REG_B));
  137. printk("tvp5150: Interrupt enable register B = 0x%02x\n",
  138. tvp5150_read(sd, TVP5150_INT_ENABLE_REG_B));
  139. printk("tvp5150: Interrupt configuration register B = 0x%02x\n",
  140. tvp5150_read(sd, TVP5150_INTT_CONFIG_REG_B));
  141. printk("tvp5150: Video standard = 0x%02x\n",
  142. tvp5150_read(sd, TVP5150_VIDEO_STD));
  143. printk("tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
  144. tvp5150_read(sd, TVP5150_CB_GAIN_FACT),
  145. tvp5150_read(sd, TVP5150_CR_GAIN_FACTOR));
  146. printk("tvp5150: Macrovision on counter = 0x%02x\n",
  147. tvp5150_read(sd, TVP5150_MACROVISION_ON_CTR));
  148. printk("tvp5150: Macrovision off counter = 0x%02x\n",
  149. tvp5150_read(sd, TVP5150_MACROVISION_OFF_CTR));
  150. printk("tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
  151. (tvp5150_read(sd, TVP5150_REV_SELECT) & 1) ? 3 : 4);
  152. printk("tvp5150: Device ID = %02x%02x\n",
  153. tvp5150_read(sd, TVP5150_MSB_DEV_ID),
  154. tvp5150_read(sd, TVP5150_LSB_DEV_ID));
  155. printk("tvp5150: ROM version = (hex) %02x.%02x\n",
  156. tvp5150_read(sd, TVP5150_ROM_MAJOR_VER),
  157. tvp5150_read(sd, TVP5150_ROM_MINOR_VER));
  158. printk("tvp5150: Vertical line count = 0x%02x%02x\n",
  159. tvp5150_read(sd, TVP5150_VERT_LN_COUNT_MSB),
  160. tvp5150_read(sd, TVP5150_VERT_LN_COUNT_LSB));
  161. printk("tvp5150: Interrupt status register B = 0x%02x\n",
  162. tvp5150_read(sd, TVP5150_INT_STATUS_REG_B));
  163. printk("tvp5150: Interrupt active register B = 0x%02x\n",
  164. tvp5150_read(sd, TVP5150_INT_ACTIVE_REG_B));
  165. printk("tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
  166. tvp5150_read(sd, TVP5150_STATUS_REG_1),
  167. tvp5150_read(sd, TVP5150_STATUS_REG_2),
  168. tvp5150_read(sd, TVP5150_STATUS_REG_3),
  169. tvp5150_read(sd, TVP5150_STATUS_REG_4),
  170. tvp5150_read(sd, TVP5150_STATUS_REG_5));
  171. dump_reg_range(sd, "Teletext filter 1", TVP5150_TELETEXT_FIL1_INI,
  172. TVP5150_TELETEXT_FIL1_END, 8);
  173. dump_reg_range(sd, "Teletext filter 2", TVP5150_TELETEXT_FIL2_INI,
  174. TVP5150_TELETEXT_FIL2_END, 8);
  175. printk("tvp5150: Teletext filter enable = 0x%02x\n",
  176. tvp5150_read(sd, TVP5150_TELETEXT_FIL_ENA));
  177. printk("tvp5150: Interrupt status register A = 0x%02x\n",
  178. tvp5150_read(sd, TVP5150_INT_STATUS_REG_A));
  179. printk("tvp5150: Interrupt enable register A = 0x%02x\n",
  180. tvp5150_read(sd, TVP5150_INT_ENABLE_REG_A));
  181. printk("tvp5150: Interrupt configuration = 0x%02x\n",
  182. tvp5150_read(sd, TVP5150_INT_CONF));
  183. printk("tvp5150: VDP status register = 0x%02x\n",
  184. tvp5150_read(sd, TVP5150_VDP_STATUS_REG));
  185. printk("tvp5150: FIFO word count = 0x%02x\n",
  186. tvp5150_read(sd, TVP5150_FIFO_WORD_COUNT));
  187. printk("tvp5150: FIFO interrupt threshold = 0x%02x\n",
  188. tvp5150_read(sd, TVP5150_FIFO_INT_THRESHOLD));
  189. printk("tvp5150: FIFO reset = 0x%02x\n",
  190. tvp5150_read(sd, TVP5150_FIFO_RESET));
  191. printk("tvp5150: Line number interrupt = 0x%02x\n",
  192. tvp5150_read(sd, TVP5150_LINE_NUMBER_INT));
  193. printk("tvp5150: Pixel alignment register = 0x%02x%02x\n",
  194. tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_HIGH),
  195. tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_LOW));
  196. printk("tvp5150: FIFO output control = 0x%02x\n",
  197. tvp5150_read(sd, TVP5150_FIFO_OUT_CTRL));
  198. printk("tvp5150: Full field enable = 0x%02x\n",
  199. tvp5150_read(sd, TVP5150_FULL_FIELD_ENA));
  200. printk("tvp5150: Full field mode register = 0x%02x\n",
  201. tvp5150_read(sd, TVP5150_FULL_FIELD_MODE_REG));
  202. dump_reg_range(sd, "CC data", TVP5150_CC_DATA_INI,
  203. TVP5150_CC_DATA_END, 8);
  204. dump_reg_range(sd, "WSS data", TVP5150_WSS_DATA_INI,
  205. TVP5150_WSS_DATA_END, 8);
  206. dump_reg_range(sd, "VPS data", TVP5150_VPS_DATA_INI,
  207. TVP5150_VPS_DATA_END, 8);
  208. dump_reg_range(sd, "VITC data", TVP5150_VITC_DATA_INI,
  209. TVP5150_VITC_DATA_END, 10);
  210. dump_reg_range(sd, "Line mode", TVP5150_LINE_MODE_INI,
  211. TVP5150_LINE_MODE_END, 8);
  212. return 0;
  213. }
  214. /****************************************************************************
  215. Basic functions
  216. ****************************************************************************/
  217. static inline void tvp5150_selmux(struct v4l2_subdev *sd)
  218. {
  219. int opmode = 0;
  220. struct tvp5150 *decoder = to_tvp5150(sd);
  221. int input = 0;
  222. unsigned char val;
  223. if ((decoder->output & TVP5150_BLACK_SCREEN) || !decoder->enable)
  224. input = 8;
  225. switch (decoder->input) {
  226. case TVP5150_COMPOSITE1:
  227. input |= 2;
  228. /* fall through */
  229. case TVP5150_COMPOSITE0:
  230. break;
  231. case TVP5150_SVIDEO:
  232. default:
  233. input |= 1;
  234. break;
  235. }
  236. v4l2_dbg(1, debug, sd, "Selecting video route: route input=%i, output=%i "
  237. "=> tvp5150 input=%i, opmode=%i\n",
  238. decoder->input, decoder->output,
  239. input, opmode);
  240. tvp5150_write(sd, TVP5150_OP_MODE_CTL, opmode);
  241. tvp5150_write(sd, TVP5150_VD_IN_SRC_SEL_1, input);
  242. /* Svideo should enable YCrCb output and disable GPCL output
  243. * For Composite and TV, it should be the reverse
  244. */
  245. val = tvp5150_read(sd, TVP5150_MISC_CTL);
  246. if (decoder->input == TVP5150_SVIDEO)
  247. val = (val & ~0x40) | 0x10;
  248. else
  249. val = (val & ~0x10) | 0x40;
  250. tvp5150_write(sd, TVP5150_MISC_CTL, val);
  251. };
  252. struct i2c_reg_value {
  253. unsigned char reg;
  254. unsigned char value;
  255. };
  256. /* Default values as sugested at TVP5150AM1 datasheet */
  257. static const struct i2c_reg_value tvp5150_init_default[] = {
  258. { /* 0x00 */
  259. TVP5150_VD_IN_SRC_SEL_1,0x00
  260. },
  261. { /* 0x01 */
  262. TVP5150_ANAL_CHL_CTL,0x15
  263. },
  264. { /* 0x02 */
  265. TVP5150_OP_MODE_CTL,0x00
  266. },
  267. { /* 0x03 */
  268. TVP5150_MISC_CTL,0x01
  269. },
  270. { /* 0x06 */
  271. TVP5150_COLOR_KIL_THSH_CTL,0x10
  272. },
  273. { /* 0x07 */
  274. TVP5150_LUMA_PROC_CTL_1,0x60
  275. },
  276. { /* 0x08 */
  277. TVP5150_LUMA_PROC_CTL_2,0x00
  278. },
  279. { /* 0x09 */
  280. TVP5150_BRIGHT_CTL,0x80
  281. },
  282. { /* 0x0a */
  283. TVP5150_SATURATION_CTL,0x80
  284. },
  285. { /* 0x0b */
  286. TVP5150_HUE_CTL,0x00
  287. },
  288. { /* 0x0c */
  289. TVP5150_CONTRAST_CTL,0x80
  290. },
  291. { /* 0x0d */
  292. TVP5150_DATA_RATE_SEL,0x47
  293. },
  294. { /* 0x0e */
  295. TVP5150_LUMA_PROC_CTL_3,0x00
  296. },
  297. { /* 0x0f */
  298. TVP5150_CONF_SHARED_PIN,0x08
  299. },
  300. { /* 0x11 */
  301. TVP5150_ACT_VD_CROP_ST_MSB,0x00
  302. },
  303. { /* 0x12 */
  304. TVP5150_ACT_VD_CROP_ST_LSB,0x00
  305. },
  306. { /* 0x13 */
  307. TVP5150_ACT_VD_CROP_STP_MSB,0x00
  308. },
  309. { /* 0x14 */
  310. TVP5150_ACT_VD_CROP_STP_LSB,0x00
  311. },
  312. { /* 0x15 */
  313. TVP5150_GENLOCK,0x01
  314. },
  315. { /* 0x16 */
  316. TVP5150_HORIZ_SYNC_START,0x80
  317. },
  318. { /* 0x18 */
  319. TVP5150_VERT_BLANKING_START,0x00
  320. },
  321. { /* 0x19 */
  322. TVP5150_VERT_BLANKING_STOP,0x00
  323. },
  324. { /* 0x1a */
  325. TVP5150_CHROMA_PROC_CTL_1,0x0c
  326. },
  327. { /* 0x1b */
  328. TVP5150_CHROMA_PROC_CTL_2,0x14
  329. },
  330. { /* 0x1c */
  331. TVP5150_INT_RESET_REG_B,0x00
  332. },
  333. { /* 0x1d */
  334. TVP5150_INT_ENABLE_REG_B,0x00
  335. },
  336. { /* 0x1e */
  337. TVP5150_INTT_CONFIG_REG_B,0x00
  338. },
  339. { /* 0x28 */
  340. TVP5150_VIDEO_STD,0x00
  341. },
  342. { /* 0x2e */
  343. TVP5150_MACROVISION_ON_CTR,0x0f
  344. },
  345. { /* 0x2f */
  346. TVP5150_MACROVISION_OFF_CTR,0x01
  347. },
  348. { /* 0xbb */
  349. TVP5150_TELETEXT_FIL_ENA,0x00
  350. },
  351. { /* 0xc0 */
  352. TVP5150_INT_STATUS_REG_A,0x00
  353. },
  354. { /* 0xc1 */
  355. TVP5150_INT_ENABLE_REG_A,0x00
  356. },
  357. { /* 0xc2 */
  358. TVP5150_INT_CONF,0x04
  359. },
  360. { /* 0xc8 */
  361. TVP5150_FIFO_INT_THRESHOLD,0x80
  362. },
  363. { /* 0xc9 */
  364. TVP5150_FIFO_RESET,0x00
  365. },
  366. { /* 0xca */
  367. TVP5150_LINE_NUMBER_INT,0x00
  368. },
  369. { /* 0xcb */
  370. TVP5150_PIX_ALIGN_REG_LOW,0x4e
  371. },
  372. { /* 0xcc */
  373. TVP5150_PIX_ALIGN_REG_HIGH,0x00
  374. },
  375. { /* 0xcd */
  376. TVP5150_FIFO_OUT_CTRL,0x01
  377. },
  378. { /* 0xcf */
  379. TVP5150_FULL_FIELD_ENA,0x00
  380. },
  381. { /* 0xd0 */
  382. TVP5150_LINE_MODE_INI,0x00
  383. },
  384. { /* 0xfc */
  385. TVP5150_FULL_FIELD_MODE_REG,0x7f
  386. },
  387. { /* end of data */
  388. 0xff,0xff
  389. }
  390. };
  391. /* Default values as sugested at TVP5150AM1 datasheet */
  392. static const struct i2c_reg_value tvp5150_init_enable[] = {
  393. {
  394. TVP5150_CONF_SHARED_PIN, 2
  395. },{ /* Automatic offset and AGC enabled */
  396. TVP5150_ANAL_CHL_CTL, 0x15
  397. },{ /* Activate YCrCb output 0x9 or 0xd ? */
  398. TVP5150_MISC_CTL, 0x6f
  399. },{ /* Activates video std autodetection for all standards */
  400. TVP5150_AUTOSW_MSK, 0x0
  401. },{ /* Default format: 0x47. For 4:2:2: 0x40 */
  402. TVP5150_DATA_RATE_SEL, 0x47
  403. },{
  404. TVP5150_CHROMA_PROC_CTL_1, 0x0c
  405. },{
  406. TVP5150_CHROMA_PROC_CTL_2, 0x54
  407. },{ /* Non documented, but initialized on WinTV USB2 */
  408. 0x27, 0x20
  409. },{
  410. 0xff,0xff
  411. }
  412. };
  413. struct tvp5150_vbi_type {
  414. unsigned int vbi_type;
  415. unsigned int ini_line;
  416. unsigned int end_line;
  417. unsigned int by_field :1;
  418. };
  419. struct i2c_vbi_ram_value {
  420. u16 reg;
  421. struct tvp5150_vbi_type type;
  422. unsigned char values[16];
  423. };
  424. /* This struct have the values for each supported VBI Standard
  425. * by
  426. tvp5150_vbi_types should follow the same order as vbi_ram_default
  427. * value 0 means rom position 0x10, value 1 means rom position 0x30
  428. * and so on. There are 16 possible locations from 0 to 15.
  429. */
  430. static struct i2c_vbi_ram_value vbi_ram_default[] =
  431. {
  432. /* FIXME: Current api doesn't handle all VBI types, those not
  433. yet supported are placed under #if 0 */
  434. #if 0
  435. {0x010, /* Teletext, SECAM, WST System A */
  436. {V4L2_SLICED_TELETEXT_SECAM,6,23,1},
  437. { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
  438. 0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
  439. },
  440. #endif
  441. {0x030, /* Teletext, PAL, WST System B */
  442. {V4L2_SLICED_TELETEXT_B,6,22,1},
  443. { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
  444. 0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
  445. },
  446. #if 0
  447. {0x050, /* Teletext, PAL, WST System C */
  448. {V4L2_SLICED_TELETEXT_PAL_C,6,22,1},
  449. { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
  450. 0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
  451. },
  452. {0x070, /* Teletext, NTSC, WST System B */
  453. {V4L2_SLICED_TELETEXT_NTSC_B,10,21,1},
  454. { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
  455. 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
  456. },
  457. {0x090, /* Tetetext, NTSC NABTS System C */
  458. {V4L2_SLICED_TELETEXT_NTSC_C,10,21,1},
  459. { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
  460. 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
  461. },
  462. {0x0b0, /* Teletext, NTSC-J, NABTS System D */
  463. {V4L2_SLICED_TELETEXT_NTSC_D,10,21,1},
  464. { 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
  465. 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
  466. },
  467. {0x0d0, /* Closed Caption, PAL/SECAM */
  468. {V4L2_SLICED_CAPTION_625,22,22,1},
  469. { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
  470. 0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
  471. },
  472. #endif
  473. {0x0f0, /* Closed Caption, NTSC */
  474. {V4L2_SLICED_CAPTION_525,21,21,1},
  475. { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
  476. 0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
  477. },
  478. {0x110, /* Wide Screen Signal, PAL/SECAM */
  479. {V4L2_SLICED_WSS_625,23,23,1},
  480. { 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
  481. 0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
  482. },
  483. #if 0
  484. {0x130, /* Wide Screen Signal, NTSC C */
  485. {V4L2_SLICED_WSS_525,20,20,1},
  486. { 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
  487. 0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
  488. },
  489. {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
  490. {V4l2_SLICED_VITC_625,6,22,0},
  491. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
  492. 0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
  493. },
  494. {0x170, /* Vertical Interval Timecode (VITC), NTSC */
  495. {V4l2_SLICED_VITC_525,10,20,0},
  496. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
  497. 0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
  498. },
  499. #endif
  500. {0x190, /* Video Program System (VPS), PAL */
  501. {V4L2_SLICED_VPS,16,16,0},
  502. { 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
  503. 0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
  504. },
  505. /* 0x1d0 User programmable */
  506. /* End of struct */
  507. { (u16)-1 }
  508. };
  509. static int tvp5150_write_inittab(struct v4l2_subdev *sd,
  510. const struct i2c_reg_value *regs)
  511. {
  512. while (regs->reg != 0xff) {
  513. tvp5150_write(sd, regs->reg, regs->value);
  514. regs++;
  515. }
  516. return 0;
  517. }
  518. static int tvp5150_vdp_init(struct v4l2_subdev *sd,
  519. const struct i2c_vbi_ram_value *regs)
  520. {
  521. unsigned int i;
  522. /* Disable Full Field */
  523. tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
  524. /* Before programming, Line mode should be at 0xff */
  525. for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
  526. tvp5150_write(sd, i, 0xff);
  527. /* Load Ram Table */
  528. while (regs->reg != (u16)-1) {
  529. tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_HIGH, regs->reg >> 8);
  530. tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_LOW, regs->reg);
  531. for (i = 0; i < 16; i++)
  532. tvp5150_write(sd, TVP5150_VDP_CONF_RAM_DATA, regs->values[i]);
  533. regs++;
  534. }
  535. return 0;
  536. }
  537. /* Fills VBI capabilities based on i2c_vbi_ram_value struct */
  538. static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev *sd,
  539. struct v4l2_sliced_vbi_cap *cap)
  540. {
  541. const struct i2c_vbi_ram_value *regs = vbi_ram_default;
  542. int line;
  543. v4l2_dbg(1, debug, sd, "g_sliced_vbi_cap\n");
  544. memset(cap, 0, sizeof *cap);
  545. while (regs->reg != (u16)-1 ) {
  546. for (line=regs->type.ini_line;line<=regs->type.end_line;line++) {
  547. cap->service_lines[0][line] |= regs->type.vbi_type;
  548. }
  549. cap->service_set |= regs->type.vbi_type;
  550. regs++;
  551. }
  552. return 0;
  553. }
  554. /* Set vbi processing
  555. * type - one of tvp5150_vbi_types
  556. * line - line to gather data
  557. * fields: bit 0 field1, bit 1, field2
  558. * flags (default=0xf0) is a bitmask, were set means:
  559. * bit 7: enable filtering null bytes on CC
  560. * bit 6: send data also to FIFO
  561. * bit 5: don't allow data with errors on FIFO
  562. * bit 4: enable ECC when possible
  563. * pix_align = pix alignment:
  564. * LSB = field1
  565. * MSB = field2
  566. */
  567. static int tvp5150_set_vbi(struct v4l2_subdev *sd,
  568. const struct i2c_vbi_ram_value *regs,
  569. unsigned int type,u8 flags, int line,
  570. const int fields)
  571. {
  572. struct tvp5150 *decoder = to_tvp5150(sd);
  573. v4l2_std_id std = decoder->norm;
  574. u8 reg;
  575. int pos=0;
  576. if (std == V4L2_STD_ALL) {
  577. v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
  578. return 0;
  579. } else if (std & V4L2_STD_625_50) {
  580. /* Don't follow NTSC Line number convension */
  581. line += 3;
  582. }
  583. if (line<6||line>27)
  584. return 0;
  585. while (regs->reg != (u16)-1 ) {
  586. if ((type & regs->type.vbi_type) &&
  587. (line>=regs->type.ini_line) &&
  588. (line<=regs->type.end_line)) {
  589. type=regs->type.vbi_type;
  590. break;
  591. }
  592. regs++;
  593. pos++;
  594. }
  595. if (regs->reg == (u16)-1)
  596. return 0;
  597. type=pos | (flags & 0xf0);
  598. reg=((line-6)<<1)+TVP5150_LINE_MODE_INI;
  599. if (fields&1) {
  600. tvp5150_write(sd, reg, type);
  601. }
  602. if (fields&2) {
  603. tvp5150_write(sd, reg+1, type);
  604. }
  605. return type;
  606. }
  607. static int tvp5150_get_vbi(struct v4l2_subdev *sd,
  608. const struct i2c_vbi_ram_value *regs, int line)
  609. {
  610. struct tvp5150 *decoder = to_tvp5150(sd);
  611. v4l2_std_id std = decoder->norm;
  612. u8 reg;
  613. int pos, type = 0;
  614. if (std == V4L2_STD_ALL) {
  615. v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
  616. return 0;
  617. } else if (std & V4L2_STD_625_50) {
  618. /* Don't follow NTSC Line number convension */
  619. line += 3;
  620. }
  621. if (line < 6 || line > 27)
  622. return 0;
  623. reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
  624. pos = tvp5150_read(sd, reg) & 0x0f;
  625. if (pos < 0x0f)
  626. type = regs[pos].type.vbi_type;
  627. pos = tvp5150_read(sd, reg + 1) & 0x0f;
  628. if (pos < 0x0f)
  629. type |= regs[pos].type.vbi_type;
  630. return type;
  631. }
  632. static int tvp5150_set_std(struct v4l2_subdev *sd, v4l2_std_id std)
  633. {
  634. struct tvp5150 *decoder = to_tvp5150(sd);
  635. int fmt = 0;
  636. decoder->norm = std;
  637. /* First tests should be against specific std */
  638. if (std == V4L2_STD_ALL) {
  639. fmt = VIDEO_STD_AUTO_SWITCH_BIT; /* Autodetect mode */
  640. } else if (std & V4L2_STD_NTSC_443) {
  641. fmt = VIDEO_STD_NTSC_4_43_BIT;
  642. } else if (std & V4L2_STD_PAL_M) {
  643. fmt = VIDEO_STD_PAL_M_BIT;
  644. } else if (std & (V4L2_STD_PAL_N | V4L2_STD_PAL_Nc)) {
  645. fmt = VIDEO_STD_PAL_COMBINATION_N_BIT;
  646. } else {
  647. /* Then, test against generic ones */
  648. if (std & V4L2_STD_NTSC)
  649. fmt = VIDEO_STD_NTSC_MJ_BIT;
  650. else if (std & V4L2_STD_PAL)
  651. fmt = VIDEO_STD_PAL_BDGHIN_BIT;
  652. else if (std & V4L2_STD_SECAM)
  653. fmt = VIDEO_STD_SECAM_BIT;
  654. }
  655. v4l2_dbg(1, debug, sd, "Set video std register to %d.\n", fmt);
  656. tvp5150_write(sd, TVP5150_VIDEO_STD, fmt);
  657. return 0;
  658. }
  659. static int tvp5150_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  660. {
  661. struct tvp5150 *decoder = to_tvp5150(sd);
  662. if (decoder->norm == std)
  663. return 0;
  664. /* Change cropping height limits */
  665. if (std & V4L2_STD_525_60)
  666. decoder->rect.height = TVP5150_V_MAX_525_60;
  667. else
  668. decoder->rect.height = TVP5150_V_MAX_OTHERS;
  669. return tvp5150_set_std(sd, std);
  670. }
  671. static int tvp5150_reset(struct v4l2_subdev *sd, u32 val)
  672. {
  673. struct tvp5150 *decoder = to_tvp5150(sd);
  674. /* Initializes TVP5150 to its default values */
  675. tvp5150_write_inittab(sd, tvp5150_init_default);
  676. /* Initializes VDP registers */
  677. tvp5150_vdp_init(sd, vbi_ram_default);
  678. /* Selects decoder input */
  679. tvp5150_selmux(sd);
  680. /* Initializes TVP5150 to stream enabled values */
  681. tvp5150_write_inittab(sd, tvp5150_init_enable);
  682. /* Initialize image preferences */
  683. v4l2_ctrl_handler_setup(&decoder->hdl);
  684. tvp5150_set_std(sd, decoder->norm);
  685. return 0;
  686. };
  687. static int tvp5150_s_ctrl(struct v4l2_ctrl *ctrl)
  688. {
  689. struct v4l2_subdev *sd = to_sd(ctrl);
  690. switch (ctrl->id) {
  691. case V4L2_CID_BRIGHTNESS:
  692. tvp5150_write(sd, TVP5150_BRIGHT_CTL, ctrl->val);
  693. return 0;
  694. case V4L2_CID_CONTRAST:
  695. tvp5150_write(sd, TVP5150_CONTRAST_CTL, ctrl->val);
  696. return 0;
  697. case V4L2_CID_SATURATION:
  698. tvp5150_write(sd, TVP5150_SATURATION_CTL, ctrl->val);
  699. return 0;
  700. case V4L2_CID_HUE:
  701. tvp5150_write(sd, TVP5150_HUE_CTL, ctrl->val);
  702. return 0;
  703. }
  704. return -EINVAL;
  705. }
  706. static v4l2_std_id tvp5150_read_std(struct v4l2_subdev *sd)
  707. {
  708. int val = tvp5150_read(sd, TVP5150_STATUS_REG_5);
  709. switch (val & 0x0F) {
  710. case 0x01:
  711. return V4L2_STD_NTSC;
  712. case 0x03:
  713. return V4L2_STD_PAL;
  714. case 0x05:
  715. return V4L2_STD_PAL_M;
  716. case 0x07:
  717. return V4L2_STD_PAL_N | V4L2_STD_PAL_Nc;
  718. case 0x09:
  719. return V4L2_STD_NTSC_443;
  720. case 0xb:
  721. return V4L2_STD_SECAM;
  722. default:
  723. return V4L2_STD_UNKNOWN;
  724. }
  725. }
  726. static int tvp5150_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
  727. enum v4l2_mbus_pixelcode *code)
  728. {
  729. if (index)
  730. return -EINVAL;
  731. *code = V4L2_MBUS_FMT_YUYV8_2X8;
  732. return 0;
  733. }
  734. static int tvp5150_mbus_fmt(struct v4l2_subdev *sd,
  735. struct v4l2_mbus_framefmt *f)
  736. {
  737. struct tvp5150 *decoder = to_tvp5150(sd);
  738. v4l2_std_id std;
  739. if (f == NULL)
  740. return -EINVAL;
  741. tvp5150_reset(sd, 0);
  742. /* Calculate height and width based on current standard */
  743. if (decoder->norm == V4L2_STD_ALL)
  744. std = tvp5150_read_std(sd);
  745. else
  746. std = decoder->norm;
  747. f->width = decoder->rect.width;
  748. f->height = decoder->rect.height;
  749. f->code = V4L2_MBUS_FMT_YUYV8_2X8;
  750. f->field = V4L2_FIELD_SEQ_TB;
  751. f->colorspace = V4L2_COLORSPACE_SMPTE170M;
  752. v4l2_dbg(1, debug, sd, "width = %d, height = %d\n", f->width,
  753. f->height);
  754. return 0;
  755. }
  756. static int tvp5150_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  757. {
  758. struct v4l2_rect rect = a->c;
  759. struct tvp5150 *decoder = to_tvp5150(sd);
  760. v4l2_std_id std;
  761. int hmax;
  762. v4l2_dbg(1, debug, sd, "%s left=%d, top=%d, width=%d, height=%d\n",
  763. __func__, rect.left, rect.top, rect.width, rect.height);
  764. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  765. return -EINVAL;
  766. /* tvp5150 has some special limits */
  767. rect.left = clamp(rect.left, 0, TVP5150_MAX_CROP_LEFT);
  768. rect.width = clamp(rect.width,
  769. TVP5150_H_MAX - TVP5150_MAX_CROP_LEFT - rect.left,
  770. TVP5150_H_MAX - rect.left);
  771. rect.top = clamp(rect.top, 0, TVP5150_MAX_CROP_TOP);
  772. /* Calculate height based on current standard */
  773. if (decoder->norm == V4L2_STD_ALL)
  774. std = tvp5150_read_std(sd);
  775. else
  776. std = decoder->norm;
  777. if (std & V4L2_STD_525_60)
  778. hmax = TVP5150_V_MAX_525_60;
  779. else
  780. hmax = TVP5150_V_MAX_OTHERS;
  781. rect.height = clamp(rect.height,
  782. hmax - TVP5150_MAX_CROP_TOP - rect.top,
  783. hmax - rect.top);
  784. tvp5150_write(sd, TVP5150_VERT_BLANKING_START, rect.top);
  785. tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP,
  786. rect.top + rect.height - hmax);
  787. tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_MSB,
  788. rect.left >> TVP5150_CROP_SHIFT);
  789. tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_LSB,
  790. rect.left | (1 << TVP5150_CROP_SHIFT));
  791. tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_MSB,
  792. (rect.left + rect.width - TVP5150_MAX_CROP_LEFT) >>
  793. TVP5150_CROP_SHIFT);
  794. tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_LSB,
  795. rect.left + rect.width - TVP5150_MAX_CROP_LEFT);
  796. decoder->rect = rect;
  797. return 0;
  798. }
  799. static int tvp5150_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  800. {
  801. struct tvp5150 *decoder = container_of(sd, struct tvp5150, sd);
  802. a->c = decoder->rect;
  803. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  804. return 0;
  805. }
  806. static int tvp5150_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  807. {
  808. struct tvp5150 *decoder = container_of(sd, struct tvp5150, sd);
  809. v4l2_std_id std;
  810. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  811. return -EINVAL;
  812. a->bounds.left = 0;
  813. a->bounds.top = 0;
  814. a->bounds.width = TVP5150_H_MAX;
  815. /* Calculate height based on current standard */
  816. if (decoder->norm == V4L2_STD_ALL)
  817. std = tvp5150_read_std(sd);
  818. else
  819. std = decoder->norm;
  820. if (std & V4L2_STD_525_60)
  821. a->bounds.height = TVP5150_V_MAX_525_60;
  822. else
  823. a->bounds.height = TVP5150_V_MAX_OTHERS;
  824. a->defrect = a->bounds;
  825. a->pixelaspect.numerator = 1;
  826. a->pixelaspect.denominator = 1;
  827. return 0;
  828. }
  829. /****************************************************************************
  830. I2C Command
  831. ****************************************************************************/
  832. static int tvp5150_s_routing(struct v4l2_subdev *sd,
  833. u32 input, u32 output, u32 config)
  834. {
  835. struct tvp5150 *decoder = to_tvp5150(sd);
  836. decoder->input = input;
  837. decoder->output = output;
  838. tvp5150_selmux(sd);
  839. return 0;
  840. }
  841. static int tvp5150_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
  842. {
  843. /* this is for capturing 36 raw vbi lines
  844. if there's a way to cut off the beginning 2 vbi lines
  845. with the tvp5150 then the vbi line count could be lowered
  846. to 17 lines/field again, although I couldn't find a register
  847. which could do that cropping */
  848. if (fmt->sample_format == V4L2_PIX_FMT_GREY)
  849. tvp5150_write(sd, TVP5150_LUMA_PROC_CTL_1, 0x70);
  850. if (fmt->count[0] == 18 && fmt->count[1] == 18) {
  851. tvp5150_write(sd, TVP5150_VERT_BLANKING_START, 0x00);
  852. tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP, 0x01);
  853. }
  854. return 0;
  855. }
  856. static int tvp5150_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
  857. {
  858. int i;
  859. if (svbi->service_set != 0) {
  860. for (i = 0; i <= 23; i++) {
  861. svbi->service_lines[1][i] = 0;
  862. svbi->service_lines[0][i] =
  863. tvp5150_set_vbi(sd, vbi_ram_default,
  864. svbi->service_lines[0][i], 0xf0, i, 3);
  865. }
  866. /* Enables FIFO */
  867. tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 1);
  868. } else {
  869. /* Disables FIFO*/
  870. tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 0);
  871. /* Disable Full Field */
  872. tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
  873. /* Disable Line modes */
  874. for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
  875. tvp5150_write(sd, i, 0xff);
  876. }
  877. return 0;
  878. }
  879. static int tvp5150_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
  880. {
  881. int i, mask = 0;
  882. memset(svbi, 0, sizeof(*svbi));
  883. for (i = 0; i <= 23; i++) {
  884. svbi->service_lines[0][i] =
  885. tvp5150_get_vbi(sd, vbi_ram_default, i);
  886. mask |= svbi->service_lines[0][i];
  887. }
  888. svbi->service_set = mask;
  889. return 0;
  890. }
  891. static int tvp5150_g_chip_ident(struct v4l2_subdev *sd,
  892. struct v4l2_dbg_chip_ident *chip)
  893. {
  894. int rev;
  895. struct i2c_client *client = v4l2_get_subdevdata(sd);
  896. rev = tvp5150_read(sd, TVP5150_ROM_MAJOR_VER) << 8 |
  897. tvp5150_read(sd, TVP5150_ROM_MINOR_VER);
  898. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_TVP5150,
  899. rev);
  900. }
  901. #ifdef CONFIG_VIDEO_ADV_DEBUG
  902. static int tvp5150_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  903. {
  904. struct i2c_client *client = v4l2_get_subdevdata(sd);
  905. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  906. return -EINVAL;
  907. if (!capable(CAP_SYS_ADMIN))
  908. return -EPERM;
  909. reg->val = tvp5150_read(sd, reg->reg & 0xff);
  910. reg->size = 1;
  911. return 0;
  912. }
  913. static int tvp5150_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  914. {
  915. struct i2c_client *client = v4l2_get_subdevdata(sd);
  916. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  917. return -EINVAL;
  918. if (!capable(CAP_SYS_ADMIN))
  919. return -EPERM;
  920. tvp5150_write(sd, reg->reg & 0xff, reg->val & 0xff);
  921. return 0;
  922. }
  923. #endif
  924. static int tvp5150_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  925. {
  926. int status = tvp5150_read(sd, 0x88);
  927. vt->signal = ((status & 0x04) && (status & 0x02)) ? 0xffff : 0x0;
  928. return 0;
  929. }
  930. /* ----------------------------------------------------------------------- */
  931. static const struct v4l2_ctrl_ops tvp5150_ctrl_ops = {
  932. .s_ctrl = tvp5150_s_ctrl,
  933. };
  934. static const struct v4l2_subdev_core_ops tvp5150_core_ops = {
  935. .log_status = tvp5150_log_status,
  936. .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
  937. .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
  938. .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
  939. .g_ctrl = v4l2_subdev_g_ctrl,
  940. .s_ctrl = v4l2_subdev_s_ctrl,
  941. .queryctrl = v4l2_subdev_queryctrl,
  942. .querymenu = v4l2_subdev_querymenu,
  943. .s_std = tvp5150_s_std,
  944. .reset = tvp5150_reset,
  945. .g_chip_ident = tvp5150_g_chip_ident,
  946. #ifdef CONFIG_VIDEO_ADV_DEBUG
  947. .g_register = tvp5150_g_register,
  948. .s_register = tvp5150_s_register,
  949. #endif
  950. };
  951. static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops = {
  952. .g_tuner = tvp5150_g_tuner,
  953. };
  954. static const struct v4l2_subdev_video_ops tvp5150_video_ops = {
  955. .s_routing = tvp5150_s_routing,
  956. .enum_mbus_fmt = tvp5150_enum_mbus_fmt,
  957. .s_mbus_fmt = tvp5150_mbus_fmt,
  958. .try_mbus_fmt = tvp5150_mbus_fmt,
  959. .s_crop = tvp5150_s_crop,
  960. .g_crop = tvp5150_g_crop,
  961. .cropcap = tvp5150_cropcap,
  962. };
  963. static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops = {
  964. .g_sliced_vbi_cap = tvp5150_g_sliced_vbi_cap,
  965. .g_sliced_fmt = tvp5150_g_sliced_fmt,
  966. .s_sliced_fmt = tvp5150_s_sliced_fmt,
  967. .s_raw_fmt = tvp5150_s_raw_fmt,
  968. };
  969. static const struct v4l2_subdev_ops tvp5150_ops = {
  970. .core = &tvp5150_core_ops,
  971. .tuner = &tvp5150_tuner_ops,
  972. .video = &tvp5150_video_ops,
  973. .vbi = &tvp5150_vbi_ops,
  974. };
  975. /****************************************************************************
  976. I2C Client & Driver
  977. ****************************************************************************/
  978. static int tvp5150_probe(struct i2c_client *c,
  979. const struct i2c_device_id *id)
  980. {
  981. struct tvp5150 *core;
  982. struct v4l2_subdev *sd;
  983. u8 msb_id, lsb_id, msb_rom, lsb_rom;
  984. /* Check if the adapter supports the needed features */
  985. if (!i2c_check_functionality(c->adapter,
  986. I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  987. return -EIO;
  988. core = kzalloc(sizeof(struct tvp5150), GFP_KERNEL);
  989. if (!core) {
  990. return -ENOMEM;
  991. }
  992. sd = &core->sd;
  993. v4l2_i2c_subdev_init(sd, c, &tvp5150_ops);
  994. v4l_info(c, "chip found @ 0x%02x (%s)\n",
  995. c->addr << 1, c->adapter->name);
  996. msb_id = tvp5150_read(sd, TVP5150_MSB_DEV_ID);
  997. lsb_id = tvp5150_read(sd, TVP5150_LSB_DEV_ID);
  998. msb_rom = tvp5150_read(sd, TVP5150_ROM_MAJOR_VER);
  999. lsb_rom = tvp5150_read(sd, TVP5150_ROM_MINOR_VER);
  1000. if (msb_rom == 4 && lsb_rom == 0) { /* Is TVP5150AM1 */
  1001. v4l2_info(sd, "tvp%02x%02xam1 detected.\n", msb_id, lsb_id);
  1002. /* ITU-T BT.656.4 timing */
  1003. tvp5150_write(sd, TVP5150_REV_SELECT, 0);
  1004. } else {
  1005. if (msb_rom == 3 || lsb_rom == 0x21) { /* Is TVP5150A */
  1006. v4l2_info(sd, "tvp%02x%02xa detected.\n", msb_id, lsb_id);
  1007. } else {
  1008. v4l2_info(sd, "*** unknown tvp%02x%02x chip detected.\n",
  1009. msb_id, lsb_id);
  1010. v4l2_info(sd, "*** Rom ver is %d.%d\n", msb_rom, lsb_rom);
  1011. }
  1012. }
  1013. core->norm = V4L2_STD_ALL; /* Default is autodetect */
  1014. core->input = TVP5150_COMPOSITE1;
  1015. core->enable = 1;
  1016. v4l2_ctrl_handler_init(&core->hdl, 4);
  1017. v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
  1018. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  1019. v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
  1020. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  1021. v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
  1022. V4L2_CID_SATURATION, 0, 255, 1, 128);
  1023. v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
  1024. V4L2_CID_HUE, -128, 127, 1, 0);
  1025. sd->ctrl_handler = &core->hdl;
  1026. if (core->hdl.error) {
  1027. int err = core->hdl.error;
  1028. v4l2_ctrl_handler_free(&core->hdl);
  1029. kfree(core);
  1030. return err;
  1031. }
  1032. v4l2_ctrl_handler_setup(&core->hdl);
  1033. /* Default is no cropping */
  1034. core->rect.top = 0;
  1035. if (tvp5150_read_std(sd) & V4L2_STD_525_60)
  1036. core->rect.height = TVP5150_V_MAX_525_60;
  1037. else
  1038. core->rect.height = TVP5150_V_MAX_OTHERS;
  1039. core->rect.left = 0;
  1040. core->rect.width = TVP5150_H_MAX;
  1041. if (debug > 1)
  1042. tvp5150_log_status(sd);
  1043. return 0;
  1044. }
  1045. static int tvp5150_remove(struct i2c_client *c)
  1046. {
  1047. struct v4l2_subdev *sd = i2c_get_clientdata(c);
  1048. struct tvp5150 *decoder = to_tvp5150(sd);
  1049. v4l2_dbg(1, debug, sd,
  1050. "tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
  1051. c->addr << 1);
  1052. v4l2_device_unregister_subdev(sd);
  1053. v4l2_ctrl_handler_free(&decoder->hdl);
  1054. kfree(to_tvp5150(sd));
  1055. return 0;
  1056. }
  1057. /* ----------------------------------------------------------------------- */
  1058. static const struct i2c_device_id tvp5150_id[] = {
  1059. { "tvp5150", 0 },
  1060. { }
  1061. };
  1062. MODULE_DEVICE_TABLE(i2c, tvp5150_id);
  1063. static struct i2c_driver tvp5150_driver = {
  1064. .driver = {
  1065. .owner = THIS_MODULE,
  1066. .name = "tvp5150",
  1067. },
  1068. .probe = tvp5150_probe,
  1069. .remove = tvp5150_remove,
  1070. .id_table = tvp5150_id,
  1071. };
  1072. module_i2c_driver(tvp5150_driver);