tvp5150.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. /*
  2. * tvp5150 - Texas Instruments TVP5150A/AM1 and TVP5151 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 <dt-bindings/media/tvp5150.h>
  8. #include <linux/i2c.h>
  9. #include <linux/slab.h>
  10. #include <linux/videodev2.h>
  11. #include <linux/delay.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/module.h>
  14. #include <media/v4l2-async.h>
  15. #include <media/v4l2-device.h>
  16. #include <media/v4l2-ctrls.h>
  17. #include <media/v4l2-of.h>
  18. #include <media/v4l2-mc.h>
  19. #include "tvp5150_reg.h"
  20. #define TVP5150_H_MAX 720U
  21. #define TVP5150_V_MAX_525_60 480U
  22. #define TVP5150_V_MAX_OTHERS 576U
  23. #define TVP5150_MAX_CROP_LEFT 511
  24. #define TVP5150_MAX_CROP_TOP 127
  25. #define TVP5150_CROP_SHIFT 2
  26. MODULE_DESCRIPTION("Texas Instruments TVP5150A/TVP5150AM1/TVP5151 video decoder driver");
  27. MODULE_AUTHOR("Mauro Carvalho Chehab");
  28. MODULE_LICENSE("GPL");
  29. static int debug;
  30. module_param(debug, int, 0644);
  31. MODULE_PARM_DESC(debug, "Debug level (0-2)");
  32. struct tvp5150 {
  33. struct v4l2_subdev sd;
  34. #ifdef CONFIG_MEDIA_CONTROLLER
  35. struct media_pad pads[DEMOD_NUM_PADS];
  36. struct media_entity input_ent[TVP5150_INPUT_NUM];
  37. struct media_pad input_pad[TVP5150_INPUT_NUM];
  38. #endif
  39. struct v4l2_ctrl_handler hdl;
  40. struct v4l2_rect rect;
  41. v4l2_std_id norm; /* Current set standard */
  42. u32 input;
  43. u32 output;
  44. int enable;
  45. u16 dev_id;
  46. u16 rom_ver;
  47. enum v4l2_mbus_type mbus_type;
  48. };
  49. static inline struct tvp5150 *to_tvp5150(struct v4l2_subdev *sd)
  50. {
  51. return container_of(sd, struct tvp5150, sd);
  52. }
  53. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  54. {
  55. return &container_of(ctrl->handler, struct tvp5150, hdl)->sd;
  56. }
  57. static int tvp5150_read(struct v4l2_subdev *sd, unsigned char addr)
  58. {
  59. struct i2c_client *c = v4l2_get_subdevdata(sd);
  60. int rc;
  61. rc = i2c_smbus_read_byte_data(c, addr);
  62. if (rc < 0) {
  63. v4l2_err(sd, "i2c i/o error: rc == %d\n", rc);
  64. return rc;
  65. }
  66. v4l2_dbg(2, debug, sd, "tvp5150: read 0x%02x = 0x%02x\n", addr, rc);
  67. return rc;
  68. }
  69. static int tvp5150_write(struct v4l2_subdev *sd, unsigned char addr,
  70. unsigned char value)
  71. {
  72. struct i2c_client *c = v4l2_get_subdevdata(sd);
  73. int rc;
  74. v4l2_dbg(2, debug, sd, "tvp5150: writing 0x%02x 0x%02x\n", addr, value);
  75. rc = i2c_smbus_write_byte_data(c, addr, value);
  76. if (rc < 0)
  77. v4l2_err(sd, "i2c i/o error: rc == %d\n", rc);
  78. return rc;
  79. }
  80. static void dump_reg_range(struct v4l2_subdev *sd, char *s, u8 init,
  81. const u8 end, int max_line)
  82. {
  83. int i = 0;
  84. while (init != (u8)(end + 1)) {
  85. if ((i % max_line) == 0) {
  86. if (i > 0)
  87. printk("\n");
  88. printk("tvp5150: %s reg 0x%02x = ", s, init);
  89. }
  90. printk("%02x ", tvp5150_read(sd, init));
  91. init++;
  92. i++;
  93. }
  94. printk("\n");
  95. }
  96. static int tvp5150_log_status(struct v4l2_subdev *sd)
  97. {
  98. printk("tvp5150: Video input source selection #1 = 0x%02x\n",
  99. tvp5150_read(sd, TVP5150_VD_IN_SRC_SEL_1));
  100. printk("tvp5150: Analog channel controls = 0x%02x\n",
  101. tvp5150_read(sd, TVP5150_ANAL_CHL_CTL));
  102. printk("tvp5150: Operation mode controls = 0x%02x\n",
  103. tvp5150_read(sd, TVP5150_OP_MODE_CTL));
  104. printk("tvp5150: Miscellaneous controls = 0x%02x\n",
  105. tvp5150_read(sd, TVP5150_MISC_CTL));
  106. printk("tvp5150: Autoswitch mask= 0x%02x\n",
  107. tvp5150_read(sd, TVP5150_AUTOSW_MSK));
  108. printk("tvp5150: Color killer threshold control = 0x%02x\n",
  109. tvp5150_read(sd, TVP5150_COLOR_KIL_THSH_CTL));
  110. printk("tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
  111. tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_1),
  112. tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_2),
  113. tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_3));
  114. printk("tvp5150: Brightness control = 0x%02x\n",
  115. tvp5150_read(sd, TVP5150_BRIGHT_CTL));
  116. printk("tvp5150: Color saturation control = 0x%02x\n",
  117. tvp5150_read(sd, TVP5150_SATURATION_CTL));
  118. printk("tvp5150: Hue control = 0x%02x\n",
  119. tvp5150_read(sd, TVP5150_HUE_CTL));
  120. printk("tvp5150: Contrast control = 0x%02x\n",
  121. tvp5150_read(sd, TVP5150_CONTRAST_CTL));
  122. printk("tvp5150: Outputs and data rates select = 0x%02x\n",
  123. tvp5150_read(sd, TVP5150_DATA_RATE_SEL));
  124. printk("tvp5150: Configuration shared pins = 0x%02x\n",
  125. tvp5150_read(sd, TVP5150_CONF_SHARED_PIN));
  126. printk("tvp5150: Active video cropping start = 0x%02x%02x\n",
  127. tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_MSB),
  128. tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_LSB));
  129. printk("tvp5150: Active video cropping stop = 0x%02x%02x\n",
  130. tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_MSB),
  131. tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_LSB));
  132. printk("tvp5150: Genlock/RTC = 0x%02x\n",
  133. tvp5150_read(sd, TVP5150_GENLOCK));
  134. printk("tvp5150: Horizontal sync start = 0x%02x\n",
  135. tvp5150_read(sd, TVP5150_HORIZ_SYNC_START));
  136. printk("tvp5150: Vertical blanking start = 0x%02x\n",
  137. tvp5150_read(sd, TVP5150_VERT_BLANKING_START));
  138. printk("tvp5150: Vertical blanking stop = 0x%02x\n",
  139. tvp5150_read(sd, TVP5150_VERT_BLANKING_STOP));
  140. printk("tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
  141. tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_1),
  142. tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_2));
  143. printk("tvp5150: Interrupt reset register B = 0x%02x\n",
  144. tvp5150_read(sd, TVP5150_INT_RESET_REG_B));
  145. printk("tvp5150: Interrupt enable register B = 0x%02x\n",
  146. tvp5150_read(sd, TVP5150_INT_ENABLE_REG_B));
  147. printk("tvp5150: Interrupt configuration register B = 0x%02x\n",
  148. tvp5150_read(sd, TVP5150_INTT_CONFIG_REG_B));
  149. printk("tvp5150: Video standard = 0x%02x\n",
  150. tvp5150_read(sd, TVP5150_VIDEO_STD));
  151. printk("tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
  152. tvp5150_read(sd, TVP5150_CB_GAIN_FACT),
  153. tvp5150_read(sd, TVP5150_CR_GAIN_FACTOR));
  154. printk("tvp5150: Macrovision on counter = 0x%02x\n",
  155. tvp5150_read(sd, TVP5150_MACROVISION_ON_CTR));
  156. printk("tvp5150: Macrovision off counter = 0x%02x\n",
  157. tvp5150_read(sd, TVP5150_MACROVISION_OFF_CTR));
  158. printk("tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
  159. (tvp5150_read(sd, TVP5150_REV_SELECT) & 1) ? 3 : 4);
  160. printk("tvp5150: Device ID = %02x%02x\n",
  161. tvp5150_read(sd, TVP5150_MSB_DEV_ID),
  162. tvp5150_read(sd, TVP5150_LSB_DEV_ID));
  163. printk("tvp5150: ROM version = (hex) %02x.%02x\n",
  164. tvp5150_read(sd, TVP5150_ROM_MAJOR_VER),
  165. tvp5150_read(sd, TVP5150_ROM_MINOR_VER));
  166. printk("tvp5150: Vertical line count = 0x%02x%02x\n",
  167. tvp5150_read(sd, TVP5150_VERT_LN_COUNT_MSB),
  168. tvp5150_read(sd, TVP5150_VERT_LN_COUNT_LSB));
  169. printk("tvp5150: Interrupt status register B = 0x%02x\n",
  170. tvp5150_read(sd, TVP5150_INT_STATUS_REG_B));
  171. printk("tvp5150: Interrupt active register B = 0x%02x\n",
  172. tvp5150_read(sd, TVP5150_INT_ACTIVE_REG_B));
  173. printk("tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
  174. tvp5150_read(sd, TVP5150_STATUS_REG_1),
  175. tvp5150_read(sd, TVP5150_STATUS_REG_2),
  176. tvp5150_read(sd, TVP5150_STATUS_REG_3),
  177. tvp5150_read(sd, TVP5150_STATUS_REG_4),
  178. tvp5150_read(sd, TVP5150_STATUS_REG_5));
  179. dump_reg_range(sd, "Teletext filter 1", TVP5150_TELETEXT_FIL1_INI,
  180. TVP5150_TELETEXT_FIL1_END, 8);
  181. dump_reg_range(sd, "Teletext filter 2", TVP5150_TELETEXT_FIL2_INI,
  182. TVP5150_TELETEXT_FIL2_END, 8);
  183. printk("tvp5150: Teletext filter enable = 0x%02x\n",
  184. tvp5150_read(sd, TVP5150_TELETEXT_FIL_ENA));
  185. printk("tvp5150: Interrupt status register A = 0x%02x\n",
  186. tvp5150_read(sd, TVP5150_INT_STATUS_REG_A));
  187. printk("tvp5150: Interrupt enable register A = 0x%02x\n",
  188. tvp5150_read(sd, TVP5150_INT_ENABLE_REG_A));
  189. printk("tvp5150: Interrupt configuration = 0x%02x\n",
  190. tvp5150_read(sd, TVP5150_INT_CONF));
  191. printk("tvp5150: VDP status register = 0x%02x\n",
  192. tvp5150_read(sd, TVP5150_VDP_STATUS_REG));
  193. printk("tvp5150: FIFO word count = 0x%02x\n",
  194. tvp5150_read(sd, TVP5150_FIFO_WORD_COUNT));
  195. printk("tvp5150: FIFO interrupt threshold = 0x%02x\n",
  196. tvp5150_read(sd, TVP5150_FIFO_INT_THRESHOLD));
  197. printk("tvp5150: FIFO reset = 0x%02x\n",
  198. tvp5150_read(sd, TVP5150_FIFO_RESET));
  199. printk("tvp5150: Line number interrupt = 0x%02x\n",
  200. tvp5150_read(sd, TVP5150_LINE_NUMBER_INT));
  201. printk("tvp5150: Pixel alignment register = 0x%02x%02x\n",
  202. tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_HIGH),
  203. tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_LOW));
  204. printk("tvp5150: FIFO output control = 0x%02x\n",
  205. tvp5150_read(sd, TVP5150_FIFO_OUT_CTRL));
  206. printk("tvp5150: Full field enable = 0x%02x\n",
  207. tvp5150_read(sd, TVP5150_FULL_FIELD_ENA));
  208. printk("tvp5150: Full field mode register = 0x%02x\n",
  209. tvp5150_read(sd, TVP5150_FULL_FIELD_MODE_REG));
  210. dump_reg_range(sd, "CC data", TVP5150_CC_DATA_INI,
  211. TVP5150_CC_DATA_END, 8);
  212. dump_reg_range(sd, "WSS data", TVP5150_WSS_DATA_INI,
  213. TVP5150_WSS_DATA_END, 8);
  214. dump_reg_range(sd, "VPS data", TVP5150_VPS_DATA_INI,
  215. TVP5150_VPS_DATA_END, 8);
  216. dump_reg_range(sd, "VITC data", TVP5150_VITC_DATA_INI,
  217. TVP5150_VITC_DATA_END, 10);
  218. dump_reg_range(sd, "Line mode", TVP5150_LINE_MODE_INI,
  219. TVP5150_LINE_MODE_END, 8);
  220. return 0;
  221. }
  222. /****************************************************************************
  223. Basic functions
  224. ****************************************************************************/
  225. static inline void tvp5150_selmux(struct v4l2_subdev *sd)
  226. {
  227. int opmode = 0;
  228. struct tvp5150 *decoder = to_tvp5150(sd);
  229. int input = 0;
  230. int val;
  231. /* Only tvp5150am1 and tvp5151 have signal generator support */
  232. if ((decoder->dev_id == 0x5150 && decoder->rom_ver == 0x0400) ||
  233. (decoder->dev_id == 0x5151 && decoder->rom_ver == 0x0100)) {
  234. if (!decoder->enable)
  235. input = 8;
  236. }
  237. switch (decoder->input) {
  238. case TVP5150_COMPOSITE1:
  239. input |= 2;
  240. /* fall through */
  241. case TVP5150_COMPOSITE0:
  242. break;
  243. case TVP5150_SVIDEO:
  244. default:
  245. input |= 1;
  246. break;
  247. }
  248. v4l2_dbg(1, debug, sd, "Selecting video route: route input=%i, output=%i "
  249. "=> tvp5150 input=%i, opmode=%i\n",
  250. decoder->input, decoder->output,
  251. input, opmode);
  252. tvp5150_write(sd, TVP5150_OP_MODE_CTL, opmode);
  253. tvp5150_write(sd, TVP5150_VD_IN_SRC_SEL_1, input);
  254. /*
  255. * Setup the FID/GLCO/VLK/HVLK and INTREQ/GPCL/VBLK output signals. For
  256. * S-Video we output the vertical lock (VLK) signal on FID/GLCO/VLK/HVLK
  257. * and set INTREQ/GPCL/VBLK to logic 0. For composite we output the
  258. * field indicator (FID) signal on FID/GLCO/VLK/HVLK and set
  259. * INTREQ/GPCL/VBLK to logic 1.
  260. */
  261. val = tvp5150_read(sd, TVP5150_MISC_CTL);
  262. if (val < 0) {
  263. v4l2_err(sd, "%s: failed with error = %d\n", __func__, val);
  264. return;
  265. }
  266. if (decoder->input == TVP5150_SVIDEO)
  267. val = (val & ~TVP5150_MISC_CTL_GPCL) | TVP5150_MISC_CTL_HVLK;
  268. else
  269. val = (val & ~TVP5150_MISC_CTL_HVLK) | TVP5150_MISC_CTL_GPCL;
  270. tvp5150_write(sd, TVP5150_MISC_CTL, val);
  271. };
  272. struct i2c_reg_value {
  273. unsigned char reg;
  274. unsigned char value;
  275. };
  276. /* Default values as sugested at TVP5150AM1 datasheet */
  277. static const struct i2c_reg_value tvp5150_init_default[] = {
  278. { /* 0x00 */
  279. TVP5150_VD_IN_SRC_SEL_1,0x00
  280. },
  281. { /* 0x01 */
  282. TVP5150_ANAL_CHL_CTL,0x15
  283. },
  284. { /* 0x02 */
  285. TVP5150_OP_MODE_CTL,0x00
  286. },
  287. { /* 0x03 */
  288. TVP5150_MISC_CTL,0x01
  289. },
  290. { /* 0x06 */
  291. TVP5150_COLOR_KIL_THSH_CTL,0x10
  292. },
  293. { /* 0x07 */
  294. TVP5150_LUMA_PROC_CTL_1,0x60
  295. },
  296. { /* 0x08 */
  297. TVP5150_LUMA_PROC_CTL_2,0x00
  298. },
  299. { /* 0x09 */
  300. TVP5150_BRIGHT_CTL,0x80
  301. },
  302. { /* 0x0a */
  303. TVP5150_SATURATION_CTL,0x80
  304. },
  305. { /* 0x0b */
  306. TVP5150_HUE_CTL,0x00
  307. },
  308. { /* 0x0c */
  309. TVP5150_CONTRAST_CTL,0x80
  310. },
  311. { /* 0x0d */
  312. TVP5150_DATA_RATE_SEL,0x47
  313. },
  314. { /* 0x0e */
  315. TVP5150_LUMA_PROC_CTL_3,0x00
  316. },
  317. { /* 0x0f */
  318. TVP5150_CONF_SHARED_PIN,0x08
  319. },
  320. { /* 0x11 */
  321. TVP5150_ACT_VD_CROP_ST_MSB,0x00
  322. },
  323. { /* 0x12 */
  324. TVP5150_ACT_VD_CROP_ST_LSB,0x00
  325. },
  326. { /* 0x13 */
  327. TVP5150_ACT_VD_CROP_STP_MSB,0x00
  328. },
  329. { /* 0x14 */
  330. TVP5150_ACT_VD_CROP_STP_LSB,0x00
  331. },
  332. { /* 0x15 */
  333. TVP5150_GENLOCK,0x01
  334. },
  335. { /* 0x16 */
  336. TVP5150_HORIZ_SYNC_START,0x80
  337. },
  338. { /* 0x18 */
  339. TVP5150_VERT_BLANKING_START,0x00
  340. },
  341. { /* 0x19 */
  342. TVP5150_VERT_BLANKING_STOP,0x00
  343. },
  344. { /* 0x1a */
  345. TVP5150_CHROMA_PROC_CTL_1,0x0c
  346. },
  347. { /* 0x1b */
  348. TVP5150_CHROMA_PROC_CTL_2,0x14
  349. },
  350. { /* 0x1c */
  351. TVP5150_INT_RESET_REG_B,0x00
  352. },
  353. { /* 0x1d */
  354. TVP5150_INT_ENABLE_REG_B,0x00
  355. },
  356. { /* 0x1e */
  357. TVP5150_INTT_CONFIG_REG_B,0x00
  358. },
  359. { /* 0x28 */
  360. TVP5150_VIDEO_STD,0x00
  361. },
  362. { /* 0x2e */
  363. TVP5150_MACROVISION_ON_CTR,0x0f
  364. },
  365. { /* 0x2f */
  366. TVP5150_MACROVISION_OFF_CTR,0x01
  367. },
  368. { /* 0xbb */
  369. TVP5150_TELETEXT_FIL_ENA,0x00
  370. },
  371. { /* 0xc0 */
  372. TVP5150_INT_STATUS_REG_A,0x00
  373. },
  374. { /* 0xc1 */
  375. TVP5150_INT_ENABLE_REG_A,0x00
  376. },
  377. { /* 0xc2 */
  378. TVP5150_INT_CONF,0x04
  379. },
  380. { /* 0xc8 */
  381. TVP5150_FIFO_INT_THRESHOLD,0x80
  382. },
  383. { /* 0xc9 */
  384. TVP5150_FIFO_RESET,0x00
  385. },
  386. { /* 0xca */
  387. TVP5150_LINE_NUMBER_INT,0x00
  388. },
  389. { /* 0xcb */
  390. TVP5150_PIX_ALIGN_REG_LOW,0x4e
  391. },
  392. { /* 0xcc */
  393. TVP5150_PIX_ALIGN_REG_HIGH,0x00
  394. },
  395. { /* 0xcd */
  396. TVP5150_FIFO_OUT_CTRL,0x01
  397. },
  398. { /* 0xcf */
  399. TVP5150_FULL_FIELD_ENA,0x00
  400. },
  401. { /* 0xd0 */
  402. TVP5150_LINE_MODE_INI,0x00
  403. },
  404. { /* 0xfc */
  405. TVP5150_FULL_FIELD_MODE_REG,0x7f
  406. },
  407. { /* end of data */
  408. 0xff,0xff
  409. }
  410. };
  411. /* Default values as sugested at TVP5150AM1 datasheet */
  412. static const struct i2c_reg_value tvp5150_init_enable[] = {
  413. {
  414. TVP5150_CONF_SHARED_PIN, 2
  415. },{ /* Automatic offset and AGC enabled */
  416. TVP5150_ANAL_CHL_CTL, 0x15
  417. },{ /* Activate YCrCb output 0x9 or 0xd ? */
  418. TVP5150_MISC_CTL, TVP5150_MISC_CTL_GPCL |
  419. TVP5150_MISC_CTL_INTREQ_OE |
  420. TVP5150_MISC_CTL_YCBCR_OE |
  421. TVP5150_MISC_CTL_SYNC_OE |
  422. TVP5150_MISC_CTL_VBLANK |
  423. TVP5150_MISC_CTL_CLOCK_OE,
  424. },{ /* Activates video std autodetection for all standards */
  425. TVP5150_AUTOSW_MSK, 0x0
  426. },{ /* Default format: 0x47. For 4:2:2: 0x40 */
  427. TVP5150_DATA_RATE_SEL, 0x47
  428. },{
  429. TVP5150_CHROMA_PROC_CTL_1, 0x0c
  430. },{
  431. TVP5150_CHROMA_PROC_CTL_2, 0x54
  432. },{ /* Non documented, but initialized on WinTV USB2 */
  433. 0x27, 0x20
  434. },{
  435. 0xff,0xff
  436. }
  437. };
  438. struct tvp5150_vbi_type {
  439. unsigned int vbi_type;
  440. unsigned int ini_line;
  441. unsigned int end_line;
  442. unsigned int by_field :1;
  443. };
  444. struct i2c_vbi_ram_value {
  445. u16 reg;
  446. struct tvp5150_vbi_type type;
  447. unsigned char values[16];
  448. };
  449. /* This struct have the values for each supported VBI Standard
  450. * by
  451. tvp5150_vbi_types should follow the same order as vbi_ram_default
  452. * value 0 means rom position 0x10, value 1 means rom position 0x30
  453. * and so on. There are 16 possible locations from 0 to 15.
  454. */
  455. static struct i2c_vbi_ram_value vbi_ram_default[] =
  456. {
  457. /* FIXME: Current api doesn't handle all VBI types, those not
  458. yet supported are placed under #if 0 */
  459. #if 0
  460. {0x010, /* Teletext, SECAM, WST System A */
  461. {V4L2_SLICED_TELETEXT_SECAM,6,23,1},
  462. { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
  463. 0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
  464. },
  465. #endif
  466. {0x030, /* Teletext, PAL, WST System B */
  467. {V4L2_SLICED_TELETEXT_B,6,22,1},
  468. { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
  469. 0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
  470. },
  471. #if 0
  472. {0x050, /* Teletext, PAL, WST System C */
  473. {V4L2_SLICED_TELETEXT_PAL_C,6,22,1},
  474. { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
  475. 0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
  476. },
  477. {0x070, /* Teletext, NTSC, WST System B */
  478. {V4L2_SLICED_TELETEXT_NTSC_B,10,21,1},
  479. { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
  480. 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
  481. },
  482. {0x090, /* Tetetext, NTSC NABTS System C */
  483. {V4L2_SLICED_TELETEXT_NTSC_C,10,21,1},
  484. { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
  485. 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
  486. },
  487. {0x0b0, /* Teletext, NTSC-J, NABTS System D */
  488. {V4L2_SLICED_TELETEXT_NTSC_D,10,21,1},
  489. { 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
  490. 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
  491. },
  492. {0x0d0, /* Closed Caption, PAL/SECAM */
  493. {V4L2_SLICED_CAPTION_625,22,22,1},
  494. { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
  495. 0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
  496. },
  497. #endif
  498. {0x0f0, /* Closed Caption, NTSC */
  499. {V4L2_SLICED_CAPTION_525,21,21,1},
  500. { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
  501. 0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
  502. },
  503. {0x110, /* Wide Screen Signal, PAL/SECAM */
  504. {V4L2_SLICED_WSS_625,23,23,1},
  505. { 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
  506. 0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
  507. },
  508. #if 0
  509. {0x130, /* Wide Screen Signal, NTSC C */
  510. {V4L2_SLICED_WSS_525,20,20,1},
  511. { 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
  512. 0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
  513. },
  514. {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
  515. {V4l2_SLICED_VITC_625,6,22,0},
  516. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
  517. 0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
  518. },
  519. {0x170, /* Vertical Interval Timecode (VITC), NTSC */
  520. {V4l2_SLICED_VITC_525,10,20,0},
  521. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
  522. 0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
  523. },
  524. #endif
  525. {0x190, /* Video Program System (VPS), PAL */
  526. {V4L2_SLICED_VPS,16,16,0},
  527. { 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
  528. 0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
  529. },
  530. /* 0x1d0 User programmable */
  531. /* End of struct */
  532. { (u16)-1 }
  533. };
  534. static int tvp5150_write_inittab(struct v4l2_subdev *sd,
  535. const struct i2c_reg_value *regs)
  536. {
  537. while (regs->reg != 0xff) {
  538. tvp5150_write(sd, regs->reg, regs->value);
  539. regs++;
  540. }
  541. return 0;
  542. }
  543. static int tvp5150_vdp_init(struct v4l2_subdev *sd,
  544. const struct i2c_vbi_ram_value *regs)
  545. {
  546. unsigned int i;
  547. /* Disable Full Field */
  548. tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
  549. /* Before programming, Line mode should be at 0xff */
  550. for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
  551. tvp5150_write(sd, i, 0xff);
  552. /* Load Ram Table */
  553. while (regs->reg != (u16)-1) {
  554. tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_HIGH, regs->reg >> 8);
  555. tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_LOW, regs->reg);
  556. for (i = 0; i < 16; i++)
  557. tvp5150_write(sd, TVP5150_VDP_CONF_RAM_DATA, regs->values[i]);
  558. regs++;
  559. }
  560. return 0;
  561. }
  562. /* Fills VBI capabilities based on i2c_vbi_ram_value struct */
  563. static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev *sd,
  564. struct v4l2_sliced_vbi_cap *cap)
  565. {
  566. const struct i2c_vbi_ram_value *regs = vbi_ram_default;
  567. int line;
  568. v4l2_dbg(1, debug, sd, "g_sliced_vbi_cap\n");
  569. memset(cap, 0, sizeof *cap);
  570. while (regs->reg != (u16)-1 ) {
  571. for (line=regs->type.ini_line;line<=regs->type.end_line;line++) {
  572. cap->service_lines[0][line] |= regs->type.vbi_type;
  573. }
  574. cap->service_set |= regs->type.vbi_type;
  575. regs++;
  576. }
  577. return 0;
  578. }
  579. /* Set vbi processing
  580. * type - one of tvp5150_vbi_types
  581. * line - line to gather data
  582. * fields: bit 0 field1, bit 1, field2
  583. * flags (default=0xf0) is a bitmask, were set means:
  584. * bit 7: enable filtering null bytes on CC
  585. * bit 6: send data also to FIFO
  586. * bit 5: don't allow data with errors on FIFO
  587. * bit 4: enable ECC when possible
  588. * pix_align = pix alignment:
  589. * LSB = field1
  590. * MSB = field2
  591. */
  592. static int tvp5150_set_vbi(struct v4l2_subdev *sd,
  593. const struct i2c_vbi_ram_value *regs,
  594. unsigned int type,u8 flags, int line,
  595. const int fields)
  596. {
  597. struct tvp5150 *decoder = to_tvp5150(sd);
  598. v4l2_std_id std = decoder->norm;
  599. u8 reg;
  600. int pos=0;
  601. if (std == V4L2_STD_ALL) {
  602. v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
  603. return 0;
  604. } else if (std & V4L2_STD_625_50) {
  605. /* Don't follow NTSC Line number convension */
  606. line += 3;
  607. }
  608. if (line<6||line>27)
  609. return 0;
  610. while (regs->reg != (u16)-1 ) {
  611. if ((type & regs->type.vbi_type) &&
  612. (line>=regs->type.ini_line) &&
  613. (line<=regs->type.end_line)) {
  614. type=regs->type.vbi_type;
  615. break;
  616. }
  617. regs++;
  618. pos++;
  619. }
  620. if (regs->reg == (u16)-1)
  621. return 0;
  622. type=pos | (flags & 0xf0);
  623. reg=((line-6)<<1)+TVP5150_LINE_MODE_INI;
  624. if (fields&1) {
  625. tvp5150_write(sd, reg, type);
  626. }
  627. if (fields&2) {
  628. tvp5150_write(sd, reg+1, type);
  629. }
  630. return type;
  631. }
  632. static int tvp5150_get_vbi(struct v4l2_subdev *sd,
  633. const struct i2c_vbi_ram_value *regs, int line)
  634. {
  635. struct tvp5150 *decoder = to_tvp5150(sd);
  636. v4l2_std_id std = decoder->norm;
  637. u8 reg;
  638. int pos, type = 0;
  639. int i, ret = 0;
  640. if (std == V4L2_STD_ALL) {
  641. v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
  642. return 0;
  643. } else if (std & V4L2_STD_625_50) {
  644. /* Don't follow NTSC Line number convension */
  645. line += 3;
  646. }
  647. if (line < 6 || line > 27)
  648. return 0;
  649. reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
  650. for (i = 0; i <= 1; i++) {
  651. ret = tvp5150_read(sd, reg + i);
  652. if (ret < 0) {
  653. v4l2_err(sd, "%s: failed with error = %d\n",
  654. __func__, ret);
  655. return 0;
  656. }
  657. pos = ret & 0x0f;
  658. if (pos < 0x0f)
  659. type |= regs[pos].type.vbi_type;
  660. }
  661. return type;
  662. }
  663. static int tvp5150_set_std(struct v4l2_subdev *sd, v4l2_std_id std)
  664. {
  665. struct tvp5150 *decoder = to_tvp5150(sd);
  666. int fmt = 0;
  667. decoder->norm = std;
  668. /* First tests should be against specific std */
  669. if (std == V4L2_STD_NTSC_443) {
  670. fmt = VIDEO_STD_NTSC_4_43_BIT;
  671. } else if (std == V4L2_STD_PAL_M) {
  672. fmt = VIDEO_STD_PAL_M_BIT;
  673. } else if (std == V4L2_STD_PAL_N || std == V4L2_STD_PAL_Nc) {
  674. fmt = VIDEO_STD_PAL_COMBINATION_N_BIT;
  675. } else {
  676. /* Then, test against generic ones */
  677. if (std & V4L2_STD_NTSC)
  678. fmt = VIDEO_STD_NTSC_MJ_BIT;
  679. else if (std & V4L2_STD_PAL)
  680. fmt = VIDEO_STD_PAL_BDGHIN_BIT;
  681. else if (std & V4L2_STD_SECAM)
  682. fmt = VIDEO_STD_SECAM_BIT;
  683. }
  684. v4l2_dbg(1, debug, sd, "Set video std register to %d.\n", fmt);
  685. tvp5150_write(sd, TVP5150_VIDEO_STD, fmt);
  686. return 0;
  687. }
  688. static int tvp5150_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  689. {
  690. struct tvp5150 *decoder = to_tvp5150(sd);
  691. if (decoder->norm == std)
  692. return 0;
  693. /* Change cropping height limits */
  694. if (std & V4L2_STD_525_60)
  695. decoder->rect.height = TVP5150_V_MAX_525_60;
  696. else
  697. decoder->rect.height = TVP5150_V_MAX_OTHERS;
  698. return tvp5150_set_std(sd, std);
  699. }
  700. static int tvp5150_reset(struct v4l2_subdev *sd, u32 val)
  701. {
  702. struct tvp5150 *decoder = to_tvp5150(sd);
  703. /* Initializes TVP5150 to its default values */
  704. tvp5150_write_inittab(sd, tvp5150_init_default);
  705. /* Initializes VDP registers */
  706. tvp5150_vdp_init(sd, vbi_ram_default);
  707. /* Selects decoder input */
  708. tvp5150_selmux(sd);
  709. /* Initializes TVP5150 to stream enabled values */
  710. tvp5150_write_inittab(sd, tvp5150_init_enable);
  711. /* Initialize image preferences */
  712. v4l2_ctrl_handler_setup(&decoder->hdl);
  713. tvp5150_set_std(sd, decoder->norm);
  714. if (decoder->mbus_type == V4L2_MBUS_PARALLEL)
  715. tvp5150_write(sd, TVP5150_DATA_RATE_SEL, 0x40);
  716. return 0;
  717. };
  718. static int tvp5150_s_ctrl(struct v4l2_ctrl *ctrl)
  719. {
  720. struct v4l2_subdev *sd = to_sd(ctrl);
  721. struct tvp5150 *decoder = to_tvp5150(sd);
  722. switch (ctrl->id) {
  723. case V4L2_CID_BRIGHTNESS:
  724. tvp5150_write(sd, TVP5150_BRIGHT_CTL, ctrl->val);
  725. return 0;
  726. case V4L2_CID_CONTRAST:
  727. tvp5150_write(sd, TVP5150_CONTRAST_CTL, ctrl->val);
  728. return 0;
  729. case V4L2_CID_SATURATION:
  730. tvp5150_write(sd, TVP5150_SATURATION_CTL, ctrl->val);
  731. return 0;
  732. case V4L2_CID_HUE:
  733. tvp5150_write(sd, TVP5150_HUE_CTL, ctrl->val);
  734. break;
  735. case V4L2_CID_TEST_PATTERN:
  736. decoder->enable = ctrl->val ? false : true;
  737. tvp5150_selmux(sd);
  738. return 0;
  739. }
  740. return -EINVAL;
  741. }
  742. static v4l2_std_id tvp5150_read_std(struct v4l2_subdev *sd)
  743. {
  744. int val = tvp5150_read(sd, TVP5150_STATUS_REG_5);
  745. switch (val & 0x0F) {
  746. case 0x01:
  747. return V4L2_STD_NTSC;
  748. case 0x03:
  749. return V4L2_STD_PAL;
  750. case 0x05:
  751. return V4L2_STD_PAL_M;
  752. case 0x07:
  753. return V4L2_STD_PAL_N | V4L2_STD_PAL_Nc;
  754. case 0x09:
  755. return V4L2_STD_NTSC_443;
  756. case 0xb:
  757. return V4L2_STD_SECAM;
  758. default:
  759. return V4L2_STD_UNKNOWN;
  760. }
  761. }
  762. static int tvp5150_fill_fmt(struct v4l2_subdev *sd,
  763. struct v4l2_subdev_pad_config *cfg,
  764. struct v4l2_subdev_format *format)
  765. {
  766. struct v4l2_mbus_framefmt *f;
  767. struct tvp5150 *decoder = to_tvp5150(sd);
  768. if (!format || format->pad)
  769. return -EINVAL;
  770. f = &format->format;
  771. f->width = decoder->rect.width;
  772. f->height = decoder->rect.height / 2;
  773. f->code = MEDIA_BUS_FMT_UYVY8_2X8;
  774. f->field = V4L2_FIELD_ALTERNATE;
  775. f->colorspace = V4L2_COLORSPACE_SMPTE170M;
  776. v4l2_dbg(1, debug, sd, "width = %d, height = %d\n", f->width,
  777. f->height);
  778. return 0;
  779. }
  780. static int tvp5150_set_selection(struct v4l2_subdev *sd,
  781. struct v4l2_subdev_pad_config *cfg,
  782. struct v4l2_subdev_selection *sel)
  783. {
  784. struct tvp5150 *decoder = to_tvp5150(sd);
  785. struct v4l2_rect rect = sel->r;
  786. v4l2_std_id std;
  787. int hmax;
  788. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
  789. sel->target != V4L2_SEL_TGT_CROP)
  790. return -EINVAL;
  791. v4l2_dbg(1, debug, sd, "%s left=%d, top=%d, width=%d, height=%d\n",
  792. __func__, rect.left, rect.top, rect.width, rect.height);
  793. /* tvp5150 has some special limits */
  794. rect.left = clamp(rect.left, 0, TVP5150_MAX_CROP_LEFT);
  795. rect.width = clamp_t(unsigned int, rect.width,
  796. TVP5150_H_MAX - TVP5150_MAX_CROP_LEFT - rect.left,
  797. TVP5150_H_MAX - rect.left);
  798. rect.top = clamp(rect.top, 0, TVP5150_MAX_CROP_TOP);
  799. /* Calculate height based on current standard */
  800. if (decoder->norm == V4L2_STD_ALL)
  801. std = tvp5150_read_std(sd);
  802. else
  803. std = decoder->norm;
  804. if (std & V4L2_STD_525_60)
  805. hmax = TVP5150_V_MAX_525_60;
  806. else
  807. hmax = TVP5150_V_MAX_OTHERS;
  808. rect.height = clamp_t(unsigned int, rect.height,
  809. hmax - TVP5150_MAX_CROP_TOP - rect.top,
  810. hmax - rect.top);
  811. tvp5150_write(sd, TVP5150_VERT_BLANKING_START, rect.top);
  812. tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP,
  813. rect.top + rect.height - hmax);
  814. tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_MSB,
  815. rect.left >> TVP5150_CROP_SHIFT);
  816. tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_LSB,
  817. rect.left | (1 << TVP5150_CROP_SHIFT));
  818. tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_MSB,
  819. (rect.left + rect.width - TVP5150_MAX_CROP_LEFT) >>
  820. TVP5150_CROP_SHIFT);
  821. tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_LSB,
  822. rect.left + rect.width - TVP5150_MAX_CROP_LEFT);
  823. decoder->rect = rect;
  824. return 0;
  825. }
  826. static int tvp5150_get_selection(struct v4l2_subdev *sd,
  827. struct v4l2_subdev_pad_config *cfg,
  828. struct v4l2_subdev_selection *sel)
  829. {
  830. struct tvp5150 *decoder = container_of(sd, struct tvp5150, sd);
  831. v4l2_std_id std;
  832. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  833. return -EINVAL;
  834. switch (sel->target) {
  835. case V4L2_SEL_TGT_CROP_BOUNDS:
  836. case V4L2_SEL_TGT_CROP_DEFAULT:
  837. sel->r.left = 0;
  838. sel->r.top = 0;
  839. sel->r.width = TVP5150_H_MAX;
  840. /* Calculate height based on current standard */
  841. if (decoder->norm == V4L2_STD_ALL)
  842. std = tvp5150_read_std(sd);
  843. else
  844. std = decoder->norm;
  845. if (std & V4L2_STD_525_60)
  846. sel->r.height = TVP5150_V_MAX_525_60;
  847. else
  848. sel->r.height = TVP5150_V_MAX_OTHERS;
  849. return 0;
  850. case V4L2_SEL_TGT_CROP:
  851. sel->r = decoder->rect;
  852. return 0;
  853. default:
  854. return -EINVAL;
  855. }
  856. }
  857. static int tvp5150_g_mbus_config(struct v4l2_subdev *sd,
  858. struct v4l2_mbus_config *cfg)
  859. {
  860. struct tvp5150 *decoder = to_tvp5150(sd);
  861. cfg->type = decoder->mbus_type;
  862. cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING
  863. | V4L2_MBUS_FIELD_EVEN_LOW | V4L2_MBUS_DATA_ACTIVE_HIGH;
  864. return 0;
  865. }
  866. /****************************************************************************
  867. V4L2 subdev pad ops
  868. ****************************************************************************/
  869. static int tvp5150_enum_mbus_code(struct v4l2_subdev *sd,
  870. struct v4l2_subdev_pad_config *cfg,
  871. struct v4l2_subdev_mbus_code_enum *code)
  872. {
  873. if (code->pad || code->index)
  874. return -EINVAL;
  875. code->code = MEDIA_BUS_FMT_UYVY8_2X8;
  876. return 0;
  877. }
  878. static int tvp5150_enum_frame_size(struct v4l2_subdev *sd,
  879. struct v4l2_subdev_pad_config *cfg,
  880. struct v4l2_subdev_frame_size_enum *fse)
  881. {
  882. struct tvp5150 *decoder = to_tvp5150(sd);
  883. if (fse->index >= 8 || fse->code != MEDIA_BUS_FMT_UYVY8_2X8)
  884. return -EINVAL;
  885. fse->code = MEDIA_BUS_FMT_UYVY8_2X8;
  886. fse->min_width = decoder->rect.width;
  887. fse->max_width = decoder->rect.width;
  888. fse->min_height = decoder->rect.height / 2;
  889. fse->max_height = decoder->rect.height / 2;
  890. return 0;
  891. }
  892. /****************************************************************************
  893. Media entity ops
  894. ****************************************************************************/
  895. static int tvp5150_link_setup(struct media_entity *entity,
  896. const struct media_pad *local,
  897. const struct media_pad *remote, u32 flags)
  898. {
  899. #ifdef CONFIG_MEDIA_CONTROLLER
  900. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  901. struct tvp5150 *decoder = to_tvp5150(sd);
  902. int i;
  903. for (i = 0; i < TVP5150_INPUT_NUM; i++) {
  904. if (remote->entity == &decoder->input_ent[i])
  905. break;
  906. }
  907. /* Do nothing for entities that are not input connectors */
  908. if (i == TVP5150_INPUT_NUM)
  909. return 0;
  910. decoder->input = i;
  911. tvp5150_selmux(sd);
  912. #endif
  913. return 0;
  914. }
  915. static const struct media_entity_operations tvp5150_sd_media_ops = {
  916. .link_setup = tvp5150_link_setup,
  917. };
  918. /****************************************************************************
  919. I2C Command
  920. ****************************************************************************/
  921. static int tvp5150_s_stream(struct v4l2_subdev *sd, int enable)
  922. {
  923. struct tvp5150 *decoder = to_tvp5150(sd);
  924. int val;
  925. /* Enable or disable the video output signals. */
  926. val = tvp5150_read(sd, TVP5150_MISC_CTL);
  927. if (val < 0)
  928. return val;
  929. val &= ~(TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_SYNC_OE |
  930. TVP5150_MISC_CTL_CLOCK_OE);
  931. if (enable) {
  932. /*
  933. * Enable the YCbCr and clock outputs. In discrete sync mode
  934. * (non-BT.656) additionally enable the the sync outputs.
  935. */
  936. val |= TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_CLOCK_OE;
  937. if (decoder->mbus_type == V4L2_MBUS_PARALLEL)
  938. val |= TVP5150_MISC_CTL_SYNC_OE;
  939. }
  940. tvp5150_write(sd, TVP5150_MISC_CTL, val);
  941. return 0;
  942. }
  943. static int tvp5150_s_routing(struct v4l2_subdev *sd,
  944. u32 input, u32 output, u32 config)
  945. {
  946. struct tvp5150 *decoder = to_tvp5150(sd);
  947. decoder->input = input;
  948. decoder->output = output;
  949. if (output == TVP5150_BLACK_SCREEN)
  950. decoder->enable = false;
  951. else
  952. decoder->enable = true;
  953. tvp5150_selmux(sd);
  954. return 0;
  955. }
  956. static int tvp5150_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
  957. {
  958. /* this is for capturing 36 raw vbi lines
  959. if there's a way to cut off the beginning 2 vbi lines
  960. with the tvp5150 then the vbi line count could be lowered
  961. to 17 lines/field again, although I couldn't find a register
  962. which could do that cropping */
  963. if (fmt->sample_format == V4L2_PIX_FMT_GREY)
  964. tvp5150_write(sd, TVP5150_LUMA_PROC_CTL_1, 0x70);
  965. if (fmt->count[0] == 18 && fmt->count[1] == 18) {
  966. tvp5150_write(sd, TVP5150_VERT_BLANKING_START, 0x00);
  967. tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP, 0x01);
  968. }
  969. return 0;
  970. }
  971. static int tvp5150_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
  972. {
  973. int i;
  974. if (svbi->service_set != 0) {
  975. for (i = 0; i <= 23; i++) {
  976. svbi->service_lines[1][i] = 0;
  977. svbi->service_lines[0][i] =
  978. tvp5150_set_vbi(sd, vbi_ram_default,
  979. svbi->service_lines[0][i], 0xf0, i, 3);
  980. }
  981. /* Enables FIFO */
  982. tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 1);
  983. } else {
  984. /* Disables FIFO*/
  985. tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 0);
  986. /* Disable Full Field */
  987. tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
  988. /* Disable Line modes */
  989. for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
  990. tvp5150_write(sd, i, 0xff);
  991. }
  992. return 0;
  993. }
  994. static int tvp5150_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
  995. {
  996. int i, mask = 0;
  997. memset(svbi->service_lines, 0, sizeof(svbi->service_lines));
  998. for (i = 0; i <= 23; i++) {
  999. svbi->service_lines[0][i] =
  1000. tvp5150_get_vbi(sd, vbi_ram_default, i);
  1001. mask |= svbi->service_lines[0][i];
  1002. }
  1003. svbi->service_set = mask;
  1004. return 0;
  1005. }
  1006. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1007. static int tvp5150_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  1008. {
  1009. int res;
  1010. res = tvp5150_read(sd, reg->reg & 0xff);
  1011. if (res < 0) {
  1012. v4l2_err(sd, "%s: failed with error = %d\n", __func__, res);
  1013. return res;
  1014. }
  1015. reg->val = res;
  1016. reg->size = 1;
  1017. return 0;
  1018. }
  1019. static int tvp5150_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
  1020. {
  1021. return tvp5150_write(sd, reg->reg & 0xff, reg->val & 0xff);
  1022. }
  1023. #endif
  1024. static int tvp5150_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  1025. {
  1026. int status = tvp5150_read(sd, 0x88);
  1027. vt->signal = ((status & 0x04) && (status & 0x02)) ? 0xffff : 0x0;
  1028. return 0;
  1029. }
  1030. static int tvp5150_registered(struct v4l2_subdev *sd)
  1031. {
  1032. #ifdef CONFIG_MEDIA_CONTROLLER
  1033. struct tvp5150 *decoder = to_tvp5150(sd);
  1034. int ret = 0;
  1035. int i;
  1036. for (i = 0; i < TVP5150_INPUT_NUM; i++) {
  1037. struct media_entity *input = &decoder->input_ent[i];
  1038. struct media_pad *pad = &decoder->input_pad[i];
  1039. if (!input->name)
  1040. continue;
  1041. decoder->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
  1042. ret = media_entity_pads_init(input, 1, pad);
  1043. if (ret < 0)
  1044. return ret;
  1045. ret = media_device_register_entity(sd->v4l2_dev->mdev, input);
  1046. if (ret < 0)
  1047. return ret;
  1048. ret = media_create_pad_link(input, 0, &sd->entity,
  1049. DEMOD_PAD_IF_INPUT, 0);
  1050. if (ret < 0) {
  1051. media_device_unregister_entity(input);
  1052. return ret;
  1053. }
  1054. }
  1055. #endif
  1056. return 0;
  1057. }
  1058. /* ----------------------------------------------------------------------- */
  1059. static const struct v4l2_ctrl_ops tvp5150_ctrl_ops = {
  1060. .s_ctrl = tvp5150_s_ctrl,
  1061. };
  1062. static const struct v4l2_subdev_core_ops tvp5150_core_ops = {
  1063. .log_status = tvp5150_log_status,
  1064. .reset = tvp5150_reset,
  1065. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1066. .g_register = tvp5150_g_register,
  1067. .s_register = tvp5150_s_register,
  1068. #endif
  1069. };
  1070. static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops = {
  1071. .g_tuner = tvp5150_g_tuner,
  1072. };
  1073. static const struct v4l2_subdev_video_ops tvp5150_video_ops = {
  1074. .s_std = tvp5150_s_std,
  1075. .s_stream = tvp5150_s_stream,
  1076. .s_routing = tvp5150_s_routing,
  1077. .g_mbus_config = tvp5150_g_mbus_config,
  1078. };
  1079. static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops = {
  1080. .g_sliced_vbi_cap = tvp5150_g_sliced_vbi_cap,
  1081. .g_sliced_fmt = tvp5150_g_sliced_fmt,
  1082. .s_sliced_fmt = tvp5150_s_sliced_fmt,
  1083. .s_raw_fmt = tvp5150_s_raw_fmt,
  1084. };
  1085. static const struct v4l2_subdev_pad_ops tvp5150_pad_ops = {
  1086. .enum_mbus_code = tvp5150_enum_mbus_code,
  1087. .enum_frame_size = tvp5150_enum_frame_size,
  1088. .set_fmt = tvp5150_fill_fmt,
  1089. .get_fmt = tvp5150_fill_fmt,
  1090. .get_selection = tvp5150_get_selection,
  1091. .set_selection = tvp5150_set_selection,
  1092. };
  1093. static const struct v4l2_subdev_ops tvp5150_ops = {
  1094. .core = &tvp5150_core_ops,
  1095. .tuner = &tvp5150_tuner_ops,
  1096. .video = &tvp5150_video_ops,
  1097. .vbi = &tvp5150_vbi_ops,
  1098. .pad = &tvp5150_pad_ops,
  1099. };
  1100. static const struct v4l2_subdev_internal_ops tvp5150_internal_ops = {
  1101. .registered = tvp5150_registered,
  1102. };
  1103. /****************************************************************************
  1104. I2C Client & Driver
  1105. ****************************************************************************/
  1106. static int tvp5150_detect_version(struct tvp5150 *core)
  1107. {
  1108. struct v4l2_subdev *sd = &core->sd;
  1109. struct i2c_client *c = v4l2_get_subdevdata(sd);
  1110. unsigned int i;
  1111. u8 regs[4];
  1112. int res;
  1113. /*
  1114. * Read consequent registers - TVP5150_MSB_DEV_ID, TVP5150_LSB_DEV_ID,
  1115. * TVP5150_ROM_MAJOR_VER, TVP5150_ROM_MINOR_VER
  1116. */
  1117. for (i = 0; i < 4; i++) {
  1118. res = tvp5150_read(sd, TVP5150_MSB_DEV_ID + i);
  1119. if (res < 0)
  1120. return res;
  1121. regs[i] = res;
  1122. }
  1123. core->dev_id = (regs[0] << 8) | regs[1];
  1124. core->rom_ver = (regs[2] << 8) | regs[3];
  1125. v4l2_info(sd, "tvp%04x (%u.%u) chip found @ 0x%02x (%s)\n",
  1126. core->dev_id, regs[2], regs[3], c->addr << 1,
  1127. c->adapter->name);
  1128. if (core->dev_id == 0x5150 && core->rom_ver == 0x0321) {
  1129. v4l2_info(sd, "tvp5150a detected.\n");
  1130. } else if (core->dev_id == 0x5150 && core->rom_ver == 0x0400) {
  1131. v4l2_info(sd, "tvp5150am1 detected.\n");
  1132. /* ITU-T BT.656.4 timing */
  1133. tvp5150_write(sd, TVP5150_REV_SELECT, 0);
  1134. } else if (core->dev_id == 0x5151 && core->rom_ver == 0x0100) {
  1135. v4l2_info(sd, "tvp5151 detected.\n");
  1136. } else {
  1137. v4l2_info(sd, "*** unknown tvp%04x chip detected.\n",
  1138. core->dev_id);
  1139. }
  1140. return 0;
  1141. }
  1142. static int tvp5150_init(struct i2c_client *c)
  1143. {
  1144. struct gpio_desc *pdn_gpio;
  1145. struct gpio_desc *reset_gpio;
  1146. pdn_gpio = devm_gpiod_get_optional(&c->dev, "pdn", GPIOD_OUT_HIGH);
  1147. if (IS_ERR(pdn_gpio))
  1148. return PTR_ERR(pdn_gpio);
  1149. if (pdn_gpio) {
  1150. gpiod_set_value_cansleep(pdn_gpio, 0);
  1151. /* Delay time between power supplies active and reset */
  1152. msleep(20);
  1153. }
  1154. reset_gpio = devm_gpiod_get_optional(&c->dev, "reset", GPIOD_OUT_HIGH);
  1155. if (IS_ERR(reset_gpio))
  1156. return PTR_ERR(reset_gpio);
  1157. if (reset_gpio) {
  1158. /* RESETB pulse duration */
  1159. ndelay(500);
  1160. gpiod_set_value_cansleep(reset_gpio, 0);
  1161. /* Delay time between end of reset to I2C active */
  1162. usleep_range(200, 250);
  1163. }
  1164. return 0;
  1165. }
  1166. static int tvp5150_parse_dt(struct tvp5150 *decoder, struct device_node *np)
  1167. {
  1168. struct v4l2_of_endpoint bus_cfg;
  1169. struct device_node *ep;
  1170. #ifdef CONFIG_MEDIA_CONTROLLER
  1171. struct device_node *connectors, *child;
  1172. struct media_entity *input;
  1173. const char *name;
  1174. u32 input_type;
  1175. #endif
  1176. unsigned int flags;
  1177. int ret = 0;
  1178. ep = of_graph_get_next_endpoint(np, NULL);
  1179. if (!ep)
  1180. return -EINVAL;
  1181. ret = v4l2_of_parse_endpoint(ep, &bus_cfg);
  1182. if (ret)
  1183. goto err;
  1184. flags = bus_cfg.bus.parallel.flags;
  1185. if (bus_cfg.bus_type == V4L2_MBUS_PARALLEL &&
  1186. !(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH &&
  1187. flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH &&
  1188. flags & V4L2_MBUS_FIELD_EVEN_LOW)) {
  1189. ret = -EINVAL;
  1190. goto err;
  1191. }
  1192. decoder->mbus_type = bus_cfg.bus_type;
  1193. #ifdef CONFIG_MEDIA_CONTROLLER
  1194. connectors = of_get_child_by_name(np, "connectors");
  1195. if (!connectors)
  1196. goto err;
  1197. for_each_available_child_of_node(connectors, child) {
  1198. ret = of_property_read_u32(child, "input", &input_type);
  1199. if (ret) {
  1200. v4l2_err(&decoder->sd,
  1201. "missing type property in node %s\n",
  1202. child->name);
  1203. goto err_connector;
  1204. }
  1205. if (input_type >= TVP5150_INPUT_NUM) {
  1206. ret = -EINVAL;
  1207. goto err_connector;
  1208. }
  1209. input = &decoder->input_ent[input_type];
  1210. /* Each input connector can only be defined once */
  1211. if (input->name) {
  1212. v4l2_err(&decoder->sd,
  1213. "input %s with same type already exists\n",
  1214. input->name);
  1215. ret = -EINVAL;
  1216. goto err_connector;
  1217. }
  1218. switch (input_type) {
  1219. case TVP5150_COMPOSITE0:
  1220. case TVP5150_COMPOSITE1:
  1221. input->function = MEDIA_ENT_F_CONN_COMPOSITE;
  1222. break;
  1223. case TVP5150_SVIDEO:
  1224. input->function = MEDIA_ENT_F_CONN_SVIDEO;
  1225. break;
  1226. }
  1227. input->flags = MEDIA_ENT_FL_CONNECTOR;
  1228. ret = of_property_read_string(child, "label", &name);
  1229. if (ret < 0) {
  1230. v4l2_err(&decoder->sd,
  1231. "missing label property in node %s\n",
  1232. child->name);
  1233. goto err_connector;
  1234. }
  1235. input->name = name;
  1236. }
  1237. err_connector:
  1238. of_node_put(connectors);
  1239. #endif
  1240. err:
  1241. of_node_put(ep);
  1242. return ret;
  1243. }
  1244. static const char * const tvp5150_test_patterns[2] = {
  1245. "Disabled",
  1246. "Black screen"
  1247. };
  1248. static int tvp5150_probe(struct i2c_client *c,
  1249. const struct i2c_device_id *id)
  1250. {
  1251. struct tvp5150 *core;
  1252. struct v4l2_subdev *sd;
  1253. struct device_node *np = c->dev.of_node;
  1254. int res;
  1255. /* Check if the adapter supports the needed features */
  1256. if (!i2c_check_functionality(c->adapter,
  1257. I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  1258. return -EIO;
  1259. res = tvp5150_init(c);
  1260. if (res)
  1261. return res;
  1262. core = devm_kzalloc(&c->dev, sizeof(*core), GFP_KERNEL);
  1263. if (!core)
  1264. return -ENOMEM;
  1265. sd = &core->sd;
  1266. if (IS_ENABLED(CONFIG_OF) && np) {
  1267. res = tvp5150_parse_dt(core, np);
  1268. if (res) {
  1269. v4l2_err(sd, "DT parsing error: %d\n", res);
  1270. return res;
  1271. }
  1272. } else {
  1273. /* Default to BT.656 embedded sync */
  1274. core->mbus_type = V4L2_MBUS_BT656;
  1275. }
  1276. v4l2_i2c_subdev_init(sd, c, &tvp5150_ops);
  1277. sd->internal_ops = &tvp5150_internal_ops;
  1278. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1279. #if defined(CONFIG_MEDIA_CONTROLLER)
  1280. core->pads[DEMOD_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
  1281. core->pads[DEMOD_PAD_VID_OUT].flags = MEDIA_PAD_FL_SOURCE;
  1282. core->pads[DEMOD_PAD_VBI_OUT].flags = MEDIA_PAD_FL_SOURCE;
  1283. sd->entity.function = MEDIA_ENT_F_ATV_DECODER;
  1284. res = media_entity_pads_init(&sd->entity, DEMOD_NUM_PADS, core->pads);
  1285. if (res < 0)
  1286. return res;
  1287. sd->entity.ops = &tvp5150_sd_media_ops;
  1288. #endif
  1289. res = tvp5150_detect_version(core);
  1290. if (res < 0)
  1291. return res;
  1292. core->norm = V4L2_STD_ALL; /* Default is autodetect */
  1293. core->input = TVP5150_COMPOSITE1;
  1294. core->enable = true;
  1295. v4l2_ctrl_handler_init(&core->hdl, 5);
  1296. v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
  1297. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  1298. v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
  1299. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  1300. v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
  1301. V4L2_CID_SATURATION, 0, 255, 1, 128);
  1302. v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
  1303. V4L2_CID_HUE, -128, 127, 1, 0);
  1304. v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
  1305. V4L2_CID_PIXEL_RATE, 27000000,
  1306. 27000000, 1, 27000000);
  1307. v4l2_ctrl_new_std_menu_items(&core->hdl, &tvp5150_ctrl_ops,
  1308. V4L2_CID_TEST_PATTERN,
  1309. ARRAY_SIZE(tvp5150_test_patterns),
  1310. 0, 0, tvp5150_test_patterns);
  1311. sd->ctrl_handler = &core->hdl;
  1312. if (core->hdl.error) {
  1313. res = core->hdl.error;
  1314. goto err;
  1315. }
  1316. /* Default is no cropping */
  1317. core->rect.top = 0;
  1318. if (tvp5150_read_std(sd) & V4L2_STD_525_60)
  1319. core->rect.height = TVP5150_V_MAX_525_60;
  1320. else
  1321. core->rect.height = TVP5150_V_MAX_OTHERS;
  1322. core->rect.left = 0;
  1323. core->rect.width = TVP5150_H_MAX;
  1324. tvp5150_reset(sd, 0); /* Calls v4l2_ctrl_handler_setup() */
  1325. res = v4l2_async_register_subdev(sd);
  1326. if (res < 0)
  1327. goto err;
  1328. if (debug > 1)
  1329. tvp5150_log_status(sd);
  1330. return 0;
  1331. err:
  1332. v4l2_ctrl_handler_free(&core->hdl);
  1333. return res;
  1334. }
  1335. static int tvp5150_remove(struct i2c_client *c)
  1336. {
  1337. struct v4l2_subdev *sd = i2c_get_clientdata(c);
  1338. struct tvp5150 *decoder = to_tvp5150(sd);
  1339. v4l2_dbg(1, debug, sd,
  1340. "tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
  1341. c->addr << 1);
  1342. v4l2_async_unregister_subdev(sd);
  1343. v4l2_ctrl_handler_free(&decoder->hdl);
  1344. return 0;
  1345. }
  1346. /* ----------------------------------------------------------------------- */
  1347. static const struct i2c_device_id tvp5150_id[] = {
  1348. { "tvp5150", 0 },
  1349. { }
  1350. };
  1351. MODULE_DEVICE_TABLE(i2c, tvp5150_id);
  1352. #if IS_ENABLED(CONFIG_OF)
  1353. static const struct of_device_id tvp5150_of_match[] = {
  1354. { .compatible = "ti,tvp5150", },
  1355. { /* sentinel */ },
  1356. };
  1357. MODULE_DEVICE_TABLE(of, tvp5150_of_match);
  1358. #endif
  1359. static struct i2c_driver tvp5150_driver = {
  1360. .driver = {
  1361. .of_match_table = of_match_ptr(tvp5150_of_match),
  1362. .name = "tvp5150",
  1363. },
  1364. .probe = tvp5150_probe,
  1365. .remove = tvp5150_remove,
  1366. .id_table = tvp5150_id,
  1367. };
  1368. module_i2c_driver(tvp5150_driver);