tvp5150.c 42 KB

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