xilinx-tpg.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*
  2. * Xilinx Test Pattern Generator
  3. *
  4. * Copyright (C) 2013-2015 Ideas on Board
  5. * Copyright (C) 2013-2015 Xilinx, Inc.
  6. *
  7. * Contacts: Hyun Kwon <hyun.kwon@xilinx.com>
  8. * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/device.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/xilinx-v4l2-controls.h>
  20. #include <media/v4l2-async.h>
  21. #include <media/v4l2-ctrls.h>
  22. #include <media/v4l2-subdev.h>
  23. #include "xilinx-vip.h"
  24. #include "xilinx-vtc.h"
  25. #define XTPG_CTRL_STATUS_SLAVE_ERROR (1 << 16)
  26. #define XTPG_CTRL_IRQ_SLAVE_ERROR (1 << 16)
  27. #define XTPG_PATTERN_CONTROL 0x0100
  28. #define XTPG_PATTERN_MASK (0xf << 0)
  29. #define XTPG_PATTERN_CONTROL_CROSS_HAIRS (1 << 4)
  30. #define XTPG_PATTERN_CONTROL_MOVING_BOX (1 << 5)
  31. #define XTPG_PATTERN_CONTROL_COLOR_MASK_SHIFT 6
  32. #define XTPG_PATTERN_CONTROL_COLOR_MASK_MASK (0xf << 6)
  33. #define XTPG_PATTERN_CONTROL_STUCK_PIXEL (1 << 9)
  34. #define XTPG_PATTERN_CONTROL_NOISE (1 << 10)
  35. #define XTPG_PATTERN_CONTROL_MOTION (1 << 12)
  36. #define XTPG_MOTION_SPEED 0x0104
  37. #define XTPG_CROSS_HAIRS 0x0108
  38. #define XTPG_CROSS_HAIRS_ROW_SHIFT 0
  39. #define XTPG_CROSS_HAIRS_ROW_MASK (0xfff << 0)
  40. #define XTPG_CROSS_HAIRS_COLUMN_SHIFT 16
  41. #define XTPG_CROSS_HAIRS_COLUMN_MASK (0xfff << 16)
  42. #define XTPG_ZPLATE_HOR_CONTROL 0x010c
  43. #define XTPG_ZPLATE_VER_CONTROL 0x0110
  44. #define XTPG_ZPLATE_START_SHIFT 0
  45. #define XTPG_ZPLATE_START_MASK (0xffff << 0)
  46. #define XTPG_ZPLATE_SPEED_SHIFT 16
  47. #define XTPG_ZPLATE_SPEED_MASK (0xffff << 16)
  48. #define XTPG_BOX_SIZE 0x0114
  49. #define XTPG_BOX_COLOR 0x0118
  50. #define XTPG_STUCK_PIXEL_THRESH 0x011c
  51. #define XTPG_NOISE_GAIN 0x0120
  52. #define XTPG_BAYER_PHASE 0x0124
  53. #define XTPG_BAYER_PHASE_RGGB 0
  54. #define XTPG_BAYER_PHASE_GRBG 1
  55. #define XTPG_BAYER_PHASE_GBRG 2
  56. #define XTPG_BAYER_PHASE_BGGR 3
  57. #define XTPG_BAYER_PHASE_OFF 4
  58. /*
  59. * The minimum blanking value is one clock cycle for the front porch, one clock
  60. * cycle for the sync pulse and one clock cycle for the back porch.
  61. */
  62. #define XTPG_MIN_HBLANK 3
  63. #define XTPG_MAX_HBLANK (XVTC_MAX_HSIZE - XVIP_MIN_WIDTH)
  64. #define XTPG_MIN_VBLANK 3
  65. #define XTPG_MAX_VBLANK (XVTC_MAX_VSIZE - XVIP_MIN_HEIGHT)
  66. /**
  67. * struct xtpg_device - Xilinx Test Pattern Generator device structure
  68. * @xvip: Xilinx Video IP device
  69. * @pads: media pads
  70. * @npads: number of pads (1 or 2)
  71. * @has_input: whether an input is connected to the sink pad
  72. * @formats: active V4L2 media bus format for each pad
  73. * @default_format: default V4L2 media bus format
  74. * @vip_format: format information corresponding to the active format
  75. * @bayer: boolean flag if TPG is set to any bayer format
  76. * @ctrl_handler: control handler
  77. * @hblank: horizontal blanking control
  78. * @vblank: vertical blanking control
  79. * @pattern: test pattern control
  80. * @streaming: is the video stream active
  81. * @vtc: video timing controller
  82. * @vtmux_gpio: video timing mux GPIO
  83. */
  84. struct xtpg_device {
  85. struct xvip_device xvip;
  86. struct media_pad pads[2];
  87. unsigned int npads;
  88. bool has_input;
  89. struct v4l2_mbus_framefmt formats[2];
  90. struct v4l2_mbus_framefmt default_format;
  91. const struct xvip_video_format *vip_format;
  92. bool bayer;
  93. struct v4l2_ctrl_handler ctrl_handler;
  94. struct v4l2_ctrl *hblank;
  95. struct v4l2_ctrl *vblank;
  96. struct v4l2_ctrl *pattern;
  97. bool streaming;
  98. struct xvtc_device *vtc;
  99. struct gpio_desc *vtmux_gpio;
  100. };
  101. static inline struct xtpg_device *to_tpg(struct v4l2_subdev *subdev)
  102. {
  103. return container_of(subdev, struct xtpg_device, xvip.subdev);
  104. }
  105. static u32 xtpg_get_bayer_phase(unsigned int code)
  106. {
  107. switch (code) {
  108. case MEDIA_BUS_FMT_SRGGB8_1X8:
  109. return XTPG_BAYER_PHASE_RGGB;
  110. case MEDIA_BUS_FMT_SGRBG8_1X8:
  111. return XTPG_BAYER_PHASE_GRBG;
  112. case MEDIA_BUS_FMT_SGBRG8_1X8:
  113. return XTPG_BAYER_PHASE_GBRG;
  114. case MEDIA_BUS_FMT_SBGGR8_1X8:
  115. return XTPG_BAYER_PHASE_BGGR;
  116. default:
  117. return XTPG_BAYER_PHASE_OFF;
  118. }
  119. }
  120. static void __xtpg_update_pattern_control(struct xtpg_device *xtpg,
  121. bool passthrough, bool pattern)
  122. {
  123. u32 pattern_mask = (1 << (xtpg->pattern->maximum + 1)) - 1;
  124. /*
  125. * If the TPG has no sink pad or no input connected to its sink pad
  126. * passthrough mode can't be enabled.
  127. */
  128. if (xtpg->npads == 1 || !xtpg->has_input)
  129. passthrough = false;
  130. /* If passthrough mode is allowed unmask bit 0. */
  131. if (passthrough)
  132. pattern_mask &= ~1;
  133. /* If test pattern mode is allowed unmask all other bits. */
  134. if (pattern)
  135. pattern_mask &= 1;
  136. __v4l2_ctrl_modify_range(xtpg->pattern, 0, xtpg->pattern->maximum,
  137. pattern_mask, pattern ? 9 : 0);
  138. }
  139. static void xtpg_update_pattern_control(struct xtpg_device *xtpg,
  140. bool passthrough, bool pattern)
  141. {
  142. mutex_lock(xtpg->ctrl_handler.lock);
  143. __xtpg_update_pattern_control(xtpg, passthrough, pattern);
  144. mutex_unlock(xtpg->ctrl_handler.lock);
  145. }
  146. /* -----------------------------------------------------------------------------
  147. * V4L2 Subdevice Video Operations
  148. */
  149. static int xtpg_s_stream(struct v4l2_subdev *subdev, int enable)
  150. {
  151. struct xtpg_device *xtpg = to_tpg(subdev);
  152. unsigned int width = xtpg->formats[0].width;
  153. unsigned int height = xtpg->formats[0].height;
  154. bool passthrough;
  155. u32 bayer_phase;
  156. if (!enable) {
  157. xvip_stop(&xtpg->xvip);
  158. if (xtpg->vtc)
  159. xvtc_generator_stop(xtpg->vtc);
  160. xtpg_update_pattern_control(xtpg, true, true);
  161. xtpg->streaming = false;
  162. return 0;
  163. }
  164. xvip_set_frame_size(&xtpg->xvip, &xtpg->formats[0]);
  165. if (xtpg->vtc) {
  166. struct xvtc_config config = {
  167. .hblank_start = width,
  168. .hsync_start = width + 1,
  169. .vblank_start = height,
  170. .vsync_start = height + 1,
  171. };
  172. unsigned int htotal;
  173. unsigned int vtotal;
  174. htotal = min_t(unsigned int, XVTC_MAX_HSIZE,
  175. v4l2_ctrl_g_ctrl(xtpg->hblank) + width);
  176. vtotal = min_t(unsigned int, XVTC_MAX_VSIZE,
  177. v4l2_ctrl_g_ctrl(xtpg->vblank) + height);
  178. config.hsync_end = htotal - 1;
  179. config.hsize = htotal;
  180. config.vsync_end = vtotal - 1;
  181. config.vsize = vtotal;
  182. xvtc_generator_start(xtpg->vtc, &config);
  183. }
  184. /*
  185. * Configure the bayer phase and video timing mux based on the
  186. * operation mode (passthrough or test pattern generation). The test
  187. * pattern can be modified by the control set handler, we thus need to
  188. * take the control lock here to avoid races.
  189. */
  190. mutex_lock(xtpg->ctrl_handler.lock);
  191. xvip_clr_and_set(&xtpg->xvip, XTPG_PATTERN_CONTROL,
  192. XTPG_PATTERN_MASK, xtpg->pattern->cur.val);
  193. /*
  194. * Switching between passthrough and test pattern generation modes isn't
  195. * allowed during streaming, update the control range accordingly.
  196. */
  197. passthrough = xtpg->pattern->cur.val == 0;
  198. __xtpg_update_pattern_control(xtpg, passthrough, !passthrough);
  199. xtpg->streaming = true;
  200. mutex_unlock(xtpg->ctrl_handler.lock);
  201. /*
  202. * For TPG v5.0, the bayer phase needs to be off for the pass through
  203. * mode, otherwise the external input would be subsampled.
  204. */
  205. bayer_phase = passthrough ? XTPG_BAYER_PHASE_OFF
  206. : xtpg_get_bayer_phase(xtpg->formats[0].code);
  207. xvip_write(&xtpg->xvip, XTPG_BAYER_PHASE, bayer_phase);
  208. if (xtpg->vtmux_gpio)
  209. gpiod_set_value_cansleep(xtpg->vtmux_gpio, !passthrough);
  210. xvip_start(&xtpg->xvip);
  211. return 0;
  212. }
  213. /* -----------------------------------------------------------------------------
  214. * V4L2 Subdevice Pad Operations
  215. */
  216. static struct v4l2_mbus_framefmt *
  217. __xtpg_get_pad_format(struct xtpg_device *xtpg,
  218. struct v4l2_subdev_pad_config *cfg,
  219. unsigned int pad, u32 which)
  220. {
  221. switch (which) {
  222. case V4L2_SUBDEV_FORMAT_TRY:
  223. return v4l2_subdev_get_try_format(&xtpg->xvip.subdev, cfg, pad);
  224. case V4L2_SUBDEV_FORMAT_ACTIVE:
  225. return &xtpg->formats[pad];
  226. default:
  227. return NULL;
  228. }
  229. }
  230. static int xtpg_get_format(struct v4l2_subdev *subdev,
  231. struct v4l2_subdev_pad_config *cfg,
  232. struct v4l2_subdev_format *fmt)
  233. {
  234. struct xtpg_device *xtpg = to_tpg(subdev);
  235. fmt->format = *__xtpg_get_pad_format(xtpg, cfg, fmt->pad, fmt->which);
  236. return 0;
  237. }
  238. static int xtpg_set_format(struct v4l2_subdev *subdev,
  239. struct v4l2_subdev_pad_config *cfg,
  240. struct v4l2_subdev_format *fmt)
  241. {
  242. struct xtpg_device *xtpg = to_tpg(subdev);
  243. struct v4l2_mbus_framefmt *__format;
  244. u32 bayer_phase;
  245. __format = __xtpg_get_pad_format(xtpg, cfg, fmt->pad, fmt->which);
  246. /* In two pads mode the source pad format is always identical to the
  247. * sink pad format.
  248. */
  249. if (xtpg->npads == 2 && fmt->pad == 1) {
  250. fmt->format = *__format;
  251. return 0;
  252. }
  253. /* Bayer phase is configurable at runtime */
  254. if (xtpg->bayer) {
  255. bayer_phase = xtpg_get_bayer_phase(fmt->format.code);
  256. if (bayer_phase != XTPG_BAYER_PHASE_OFF)
  257. __format->code = fmt->format.code;
  258. }
  259. xvip_set_format_size(__format, fmt);
  260. fmt->format = *__format;
  261. /* Propagate the format to the source pad. */
  262. if (xtpg->npads == 2) {
  263. __format = __xtpg_get_pad_format(xtpg, cfg, 1, fmt->which);
  264. *__format = fmt->format;
  265. }
  266. return 0;
  267. }
  268. /* -----------------------------------------------------------------------------
  269. * V4L2 Subdevice Operations
  270. */
  271. static int xtpg_enum_frame_size(struct v4l2_subdev *subdev,
  272. struct v4l2_subdev_pad_config *cfg,
  273. struct v4l2_subdev_frame_size_enum *fse)
  274. {
  275. struct v4l2_mbus_framefmt *format;
  276. format = v4l2_subdev_get_try_format(subdev, cfg, fse->pad);
  277. if (fse->index || fse->code != format->code)
  278. return -EINVAL;
  279. /* Min / max values for pad 0 is always fixed in both one and two pads
  280. * modes. In two pads mode, the source pad(= 1) size is identical to
  281. * the sink pad size */
  282. if (fse->pad == 0) {
  283. fse->min_width = XVIP_MIN_WIDTH;
  284. fse->max_width = XVIP_MAX_WIDTH;
  285. fse->min_height = XVIP_MIN_HEIGHT;
  286. fse->max_height = XVIP_MAX_HEIGHT;
  287. } else {
  288. fse->min_width = format->width;
  289. fse->max_width = format->width;
  290. fse->min_height = format->height;
  291. fse->max_height = format->height;
  292. }
  293. return 0;
  294. }
  295. static int xtpg_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  296. {
  297. struct xtpg_device *xtpg = to_tpg(subdev);
  298. struct v4l2_mbus_framefmt *format;
  299. format = v4l2_subdev_get_try_format(subdev, fh->pad, 0);
  300. *format = xtpg->default_format;
  301. if (xtpg->npads == 2) {
  302. format = v4l2_subdev_get_try_format(subdev, fh->pad, 1);
  303. *format = xtpg->default_format;
  304. }
  305. return 0;
  306. }
  307. static int xtpg_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  308. {
  309. return 0;
  310. }
  311. static int xtpg_s_ctrl(struct v4l2_ctrl *ctrl)
  312. {
  313. struct xtpg_device *xtpg = container_of(ctrl->handler,
  314. struct xtpg_device,
  315. ctrl_handler);
  316. switch (ctrl->id) {
  317. case V4L2_CID_TEST_PATTERN:
  318. xvip_clr_and_set(&xtpg->xvip, XTPG_PATTERN_CONTROL,
  319. XTPG_PATTERN_MASK, ctrl->val);
  320. return 0;
  321. case V4L2_CID_XILINX_TPG_CROSS_HAIRS:
  322. xvip_clr_or_set(&xtpg->xvip, XTPG_PATTERN_CONTROL,
  323. XTPG_PATTERN_CONTROL_CROSS_HAIRS, ctrl->val);
  324. return 0;
  325. case V4L2_CID_XILINX_TPG_MOVING_BOX:
  326. xvip_clr_or_set(&xtpg->xvip, XTPG_PATTERN_CONTROL,
  327. XTPG_PATTERN_CONTROL_MOVING_BOX, ctrl->val);
  328. return 0;
  329. case V4L2_CID_XILINX_TPG_COLOR_MASK:
  330. xvip_clr_and_set(&xtpg->xvip, XTPG_PATTERN_CONTROL,
  331. XTPG_PATTERN_CONTROL_COLOR_MASK_MASK,
  332. ctrl->val <<
  333. XTPG_PATTERN_CONTROL_COLOR_MASK_SHIFT);
  334. return 0;
  335. case V4L2_CID_XILINX_TPG_STUCK_PIXEL:
  336. xvip_clr_or_set(&xtpg->xvip, XTPG_PATTERN_CONTROL,
  337. XTPG_PATTERN_CONTROL_STUCK_PIXEL, ctrl->val);
  338. return 0;
  339. case V4L2_CID_XILINX_TPG_NOISE:
  340. xvip_clr_or_set(&xtpg->xvip, XTPG_PATTERN_CONTROL,
  341. XTPG_PATTERN_CONTROL_NOISE, ctrl->val);
  342. return 0;
  343. case V4L2_CID_XILINX_TPG_MOTION:
  344. xvip_clr_or_set(&xtpg->xvip, XTPG_PATTERN_CONTROL,
  345. XTPG_PATTERN_CONTROL_MOTION, ctrl->val);
  346. return 0;
  347. case V4L2_CID_XILINX_TPG_MOTION_SPEED:
  348. xvip_write(&xtpg->xvip, XTPG_MOTION_SPEED, ctrl->val);
  349. return 0;
  350. case V4L2_CID_XILINX_TPG_CROSS_HAIR_ROW:
  351. xvip_clr_and_set(&xtpg->xvip, XTPG_CROSS_HAIRS,
  352. XTPG_CROSS_HAIRS_ROW_MASK,
  353. ctrl->val << XTPG_CROSS_HAIRS_ROW_SHIFT);
  354. return 0;
  355. case V4L2_CID_XILINX_TPG_CROSS_HAIR_COLUMN:
  356. xvip_clr_and_set(&xtpg->xvip, XTPG_CROSS_HAIRS,
  357. XTPG_CROSS_HAIRS_COLUMN_MASK,
  358. ctrl->val << XTPG_CROSS_HAIRS_COLUMN_SHIFT);
  359. return 0;
  360. case V4L2_CID_XILINX_TPG_ZPLATE_HOR_START:
  361. xvip_clr_and_set(&xtpg->xvip, XTPG_ZPLATE_HOR_CONTROL,
  362. XTPG_ZPLATE_START_MASK,
  363. ctrl->val << XTPG_ZPLATE_START_SHIFT);
  364. return 0;
  365. case V4L2_CID_XILINX_TPG_ZPLATE_HOR_SPEED:
  366. xvip_clr_and_set(&xtpg->xvip, XTPG_ZPLATE_HOR_CONTROL,
  367. XTPG_ZPLATE_SPEED_MASK,
  368. ctrl->val << XTPG_ZPLATE_SPEED_SHIFT);
  369. return 0;
  370. case V4L2_CID_XILINX_TPG_ZPLATE_VER_START:
  371. xvip_clr_and_set(&xtpg->xvip, XTPG_ZPLATE_VER_CONTROL,
  372. XTPG_ZPLATE_START_MASK,
  373. ctrl->val << XTPG_ZPLATE_START_SHIFT);
  374. return 0;
  375. case V4L2_CID_XILINX_TPG_ZPLATE_VER_SPEED:
  376. xvip_clr_and_set(&xtpg->xvip, XTPG_ZPLATE_VER_CONTROL,
  377. XTPG_ZPLATE_SPEED_MASK,
  378. ctrl->val << XTPG_ZPLATE_SPEED_SHIFT);
  379. return 0;
  380. case V4L2_CID_XILINX_TPG_BOX_SIZE:
  381. xvip_write(&xtpg->xvip, XTPG_BOX_SIZE, ctrl->val);
  382. return 0;
  383. case V4L2_CID_XILINX_TPG_BOX_COLOR:
  384. xvip_write(&xtpg->xvip, XTPG_BOX_COLOR, ctrl->val);
  385. return 0;
  386. case V4L2_CID_XILINX_TPG_STUCK_PIXEL_THRESH:
  387. xvip_write(&xtpg->xvip, XTPG_STUCK_PIXEL_THRESH, ctrl->val);
  388. return 0;
  389. case V4L2_CID_XILINX_TPG_NOISE_GAIN:
  390. xvip_write(&xtpg->xvip, XTPG_NOISE_GAIN, ctrl->val);
  391. return 0;
  392. }
  393. return 0;
  394. }
  395. static const struct v4l2_ctrl_ops xtpg_ctrl_ops = {
  396. .s_ctrl = xtpg_s_ctrl,
  397. };
  398. static struct v4l2_subdev_core_ops xtpg_core_ops = {
  399. };
  400. static struct v4l2_subdev_video_ops xtpg_video_ops = {
  401. .s_stream = xtpg_s_stream,
  402. };
  403. static struct v4l2_subdev_pad_ops xtpg_pad_ops = {
  404. .enum_mbus_code = xvip_enum_mbus_code,
  405. .enum_frame_size = xtpg_enum_frame_size,
  406. .get_fmt = xtpg_get_format,
  407. .set_fmt = xtpg_set_format,
  408. };
  409. static struct v4l2_subdev_ops xtpg_ops = {
  410. .core = &xtpg_core_ops,
  411. .video = &xtpg_video_ops,
  412. .pad = &xtpg_pad_ops,
  413. };
  414. static const struct v4l2_subdev_internal_ops xtpg_internal_ops = {
  415. .open = xtpg_open,
  416. .close = xtpg_close,
  417. };
  418. /*
  419. * Control Config
  420. */
  421. static const char *const xtpg_pattern_strings[] = {
  422. "Passthrough",
  423. "Horizontal Ramp",
  424. "Vertical Ramp",
  425. "Temporal Ramp",
  426. "Solid Red",
  427. "Solid Green",
  428. "Solid Blue",
  429. "Solid Black",
  430. "Solid White",
  431. "Color Bars",
  432. "Zone Plate",
  433. "Tartan Color Bars",
  434. "Cross Hatch",
  435. "None",
  436. "Vertical/Horizontal Ramps",
  437. "Black/White Checker Board",
  438. };
  439. static struct v4l2_ctrl_config xtpg_ctrls[] = {
  440. {
  441. .ops = &xtpg_ctrl_ops,
  442. .id = V4L2_CID_XILINX_TPG_CROSS_HAIRS,
  443. .name = "Test Pattern: Cross Hairs",
  444. .type = V4L2_CTRL_TYPE_BOOLEAN,
  445. .min = false,
  446. .max = true,
  447. .step = 1,
  448. .def = 0,
  449. }, {
  450. .ops = &xtpg_ctrl_ops,
  451. .id = V4L2_CID_XILINX_TPG_MOVING_BOX,
  452. .name = "Test Pattern: Moving Box",
  453. .type = V4L2_CTRL_TYPE_BOOLEAN,
  454. .min = false,
  455. .max = true,
  456. .step = 1,
  457. .def = 0,
  458. }, {
  459. .ops = &xtpg_ctrl_ops,
  460. .id = V4L2_CID_XILINX_TPG_COLOR_MASK,
  461. .name = "Test Pattern: Color Mask",
  462. .type = V4L2_CTRL_TYPE_BITMASK,
  463. .min = 0,
  464. .max = 0xf,
  465. .def = 0,
  466. }, {
  467. .ops = &xtpg_ctrl_ops,
  468. .id = V4L2_CID_XILINX_TPG_STUCK_PIXEL,
  469. .name = "Test Pattern: Stuck Pixel",
  470. .type = V4L2_CTRL_TYPE_BOOLEAN,
  471. .min = false,
  472. .max = true,
  473. .step = 1,
  474. .def = 0,
  475. }, {
  476. .ops = &xtpg_ctrl_ops,
  477. .id = V4L2_CID_XILINX_TPG_NOISE,
  478. .name = "Test Pattern: Noise",
  479. .type = V4L2_CTRL_TYPE_BOOLEAN,
  480. .min = false,
  481. .max = true,
  482. .step = 1,
  483. .def = 0,
  484. }, {
  485. .ops = &xtpg_ctrl_ops,
  486. .id = V4L2_CID_XILINX_TPG_MOTION,
  487. .name = "Test Pattern: Motion",
  488. .type = V4L2_CTRL_TYPE_BOOLEAN,
  489. .min = false,
  490. .max = true,
  491. .step = 1,
  492. .def = 0,
  493. }, {
  494. .ops = &xtpg_ctrl_ops,
  495. .id = V4L2_CID_XILINX_TPG_MOTION_SPEED,
  496. .name = "Test Pattern: Motion Speed",
  497. .type = V4L2_CTRL_TYPE_INTEGER,
  498. .min = 0,
  499. .max = (1 << 8) - 1,
  500. .step = 1,
  501. .def = 4,
  502. .flags = V4L2_CTRL_FLAG_SLIDER,
  503. }, {
  504. .ops = &xtpg_ctrl_ops,
  505. .id = V4L2_CID_XILINX_TPG_CROSS_HAIR_ROW,
  506. .name = "Test Pattern: Cross Hairs Row",
  507. .type = V4L2_CTRL_TYPE_INTEGER,
  508. .min = 0,
  509. .max = (1 << 12) - 1,
  510. .step = 1,
  511. .def = 0x64,
  512. .flags = V4L2_CTRL_FLAG_SLIDER,
  513. }, {
  514. .ops = &xtpg_ctrl_ops,
  515. .id = V4L2_CID_XILINX_TPG_CROSS_HAIR_COLUMN,
  516. .name = "Test Pattern: Cross Hairs Column",
  517. .type = V4L2_CTRL_TYPE_INTEGER,
  518. .min = 0,
  519. .max = (1 << 12) - 1,
  520. .step = 1,
  521. .def = 0x64,
  522. .flags = V4L2_CTRL_FLAG_SLIDER,
  523. }, {
  524. .ops = &xtpg_ctrl_ops,
  525. .id = V4L2_CID_XILINX_TPG_ZPLATE_HOR_START,
  526. .name = "Test Pattern: Zplate Horizontal Start Pos",
  527. .type = V4L2_CTRL_TYPE_INTEGER,
  528. .min = 0,
  529. .max = (1 << 16) - 1,
  530. .step = 1,
  531. .def = 0x1e,
  532. .flags = V4L2_CTRL_FLAG_SLIDER,
  533. }, {
  534. .ops = &xtpg_ctrl_ops,
  535. .id = V4L2_CID_XILINX_TPG_ZPLATE_HOR_SPEED,
  536. .name = "Test Pattern: Zplate Horizontal Speed",
  537. .type = V4L2_CTRL_TYPE_INTEGER,
  538. .min = 0,
  539. .max = (1 << 16) - 1,
  540. .step = 1,
  541. .def = 0,
  542. .flags = V4L2_CTRL_FLAG_SLIDER,
  543. }, {
  544. .ops = &xtpg_ctrl_ops,
  545. .id = V4L2_CID_XILINX_TPG_ZPLATE_VER_START,
  546. .name = "Test Pattern: Zplate Vertical Start Pos",
  547. .type = V4L2_CTRL_TYPE_INTEGER,
  548. .min = 0,
  549. .max = (1 << 16) - 1,
  550. .step = 1,
  551. .def = 1,
  552. .flags = V4L2_CTRL_FLAG_SLIDER,
  553. }, {
  554. .ops = &xtpg_ctrl_ops,
  555. .id = V4L2_CID_XILINX_TPG_ZPLATE_VER_SPEED,
  556. .name = "Test Pattern: Zplate Vertical Speed",
  557. .type = V4L2_CTRL_TYPE_INTEGER,
  558. .min = 0,
  559. .max = (1 << 16) - 1,
  560. .step = 1,
  561. .def = 0,
  562. .flags = V4L2_CTRL_FLAG_SLIDER,
  563. }, {
  564. .ops = &xtpg_ctrl_ops,
  565. .id = V4L2_CID_XILINX_TPG_BOX_SIZE,
  566. .name = "Test Pattern: Box Size",
  567. .type = V4L2_CTRL_TYPE_INTEGER,
  568. .min = 0,
  569. .max = (1 << 12) - 1,
  570. .step = 1,
  571. .def = 0x32,
  572. .flags = V4L2_CTRL_FLAG_SLIDER,
  573. }, {
  574. .ops = &xtpg_ctrl_ops,
  575. .id = V4L2_CID_XILINX_TPG_BOX_COLOR,
  576. .name = "Test Pattern: Box Color(RGB)",
  577. .type = V4L2_CTRL_TYPE_INTEGER,
  578. .min = 0,
  579. .max = (1 << 24) - 1,
  580. .step = 1,
  581. .def = 0,
  582. }, {
  583. .ops = &xtpg_ctrl_ops,
  584. .id = V4L2_CID_XILINX_TPG_STUCK_PIXEL_THRESH,
  585. .name = "Test Pattern: Stuck Pixel threshold",
  586. .type = V4L2_CTRL_TYPE_INTEGER,
  587. .min = 0,
  588. .max = (1 << 16) - 1,
  589. .step = 1,
  590. .def = 0,
  591. .flags = V4L2_CTRL_FLAG_SLIDER,
  592. }, {
  593. .ops = &xtpg_ctrl_ops,
  594. .id = V4L2_CID_XILINX_TPG_NOISE_GAIN,
  595. .name = "Test Pattern: Noise Gain",
  596. .type = V4L2_CTRL_TYPE_INTEGER,
  597. .min = 0,
  598. .max = (1 << 8) - 1,
  599. .step = 1,
  600. .def = 0,
  601. .flags = V4L2_CTRL_FLAG_SLIDER,
  602. },
  603. };
  604. /* -----------------------------------------------------------------------------
  605. * Media Operations
  606. */
  607. static const struct media_entity_operations xtpg_media_ops = {
  608. .link_validate = v4l2_subdev_link_validate,
  609. };
  610. /* -----------------------------------------------------------------------------
  611. * Power Management
  612. */
  613. static int __maybe_unused xtpg_pm_suspend(struct device *dev)
  614. {
  615. struct xtpg_device *xtpg = dev_get_drvdata(dev);
  616. xvip_suspend(&xtpg->xvip);
  617. return 0;
  618. }
  619. static int __maybe_unused xtpg_pm_resume(struct device *dev)
  620. {
  621. struct xtpg_device *xtpg = dev_get_drvdata(dev);
  622. xvip_resume(&xtpg->xvip);
  623. return 0;
  624. }
  625. /* -----------------------------------------------------------------------------
  626. * Platform Device Driver
  627. */
  628. static int xtpg_parse_of(struct xtpg_device *xtpg)
  629. {
  630. struct device *dev = xtpg->xvip.dev;
  631. struct device_node *node = xtpg->xvip.dev->of_node;
  632. struct device_node *ports;
  633. struct device_node *port;
  634. unsigned int nports = 0;
  635. bool has_endpoint = false;
  636. ports = of_get_child_by_name(node, "ports");
  637. if (ports == NULL)
  638. ports = node;
  639. for_each_child_of_node(ports, port) {
  640. const struct xvip_video_format *format;
  641. struct device_node *endpoint;
  642. if (!port->name || of_node_cmp(port->name, "port"))
  643. continue;
  644. format = xvip_of_get_format(port);
  645. if (IS_ERR(format)) {
  646. dev_err(dev, "invalid format in DT");
  647. return PTR_ERR(format);
  648. }
  649. /* Get and check the format description */
  650. if (!xtpg->vip_format) {
  651. xtpg->vip_format = format;
  652. } else if (xtpg->vip_format != format) {
  653. dev_err(dev, "in/out format mismatch in DT");
  654. return -EINVAL;
  655. }
  656. if (nports == 0) {
  657. endpoint = of_get_next_child(port, NULL);
  658. if (endpoint)
  659. has_endpoint = true;
  660. of_node_put(endpoint);
  661. }
  662. /* Count the number of ports. */
  663. nports++;
  664. }
  665. if (nports != 1 && nports != 2) {
  666. dev_err(dev, "invalid number of ports %u\n", nports);
  667. return -EINVAL;
  668. }
  669. xtpg->npads = nports;
  670. if (nports == 2 && has_endpoint)
  671. xtpg->has_input = true;
  672. return 0;
  673. }
  674. static int xtpg_probe(struct platform_device *pdev)
  675. {
  676. struct v4l2_subdev *subdev;
  677. struct xtpg_device *xtpg;
  678. u32 i, bayer_phase;
  679. int ret;
  680. xtpg = devm_kzalloc(&pdev->dev, sizeof(*xtpg), GFP_KERNEL);
  681. if (!xtpg)
  682. return -ENOMEM;
  683. xtpg->xvip.dev = &pdev->dev;
  684. ret = xtpg_parse_of(xtpg);
  685. if (ret < 0)
  686. return ret;
  687. ret = xvip_init_resources(&xtpg->xvip);
  688. if (ret < 0)
  689. return ret;
  690. xtpg->vtmux_gpio = devm_gpiod_get_optional(&pdev->dev, "timing",
  691. GPIOD_OUT_HIGH);
  692. if (IS_ERR(xtpg->vtmux_gpio)) {
  693. ret = PTR_ERR(xtpg->vtmux_gpio);
  694. goto error_resource;
  695. }
  696. xtpg->vtc = xvtc_of_get(pdev->dev.of_node);
  697. if (IS_ERR(xtpg->vtc)) {
  698. ret = PTR_ERR(xtpg->vtc);
  699. goto error_resource;
  700. }
  701. /* Reset and initialize the core */
  702. xvip_reset(&xtpg->xvip);
  703. /* Initialize V4L2 subdevice and media entity. Pad numbers depend on the
  704. * number of pads.
  705. */
  706. if (xtpg->npads == 2) {
  707. xtpg->pads[0].flags = MEDIA_PAD_FL_SINK;
  708. xtpg->pads[1].flags = MEDIA_PAD_FL_SOURCE;
  709. } else {
  710. xtpg->pads[0].flags = MEDIA_PAD_FL_SOURCE;
  711. }
  712. /* Initialize the default format */
  713. xtpg->default_format.code = xtpg->vip_format->code;
  714. xtpg->default_format.field = V4L2_FIELD_NONE;
  715. xtpg->default_format.colorspace = V4L2_COLORSPACE_SRGB;
  716. xvip_get_frame_size(&xtpg->xvip, &xtpg->default_format);
  717. bayer_phase = xtpg_get_bayer_phase(xtpg->vip_format->code);
  718. if (bayer_phase != XTPG_BAYER_PHASE_OFF)
  719. xtpg->bayer = true;
  720. xtpg->formats[0] = xtpg->default_format;
  721. if (xtpg->npads == 2)
  722. xtpg->formats[1] = xtpg->default_format;
  723. /* Initialize V4L2 subdevice and media entity */
  724. subdev = &xtpg->xvip.subdev;
  725. v4l2_subdev_init(subdev, &xtpg_ops);
  726. subdev->dev = &pdev->dev;
  727. subdev->internal_ops = &xtpg_internal_ops;
  728. strlcpy(subdev->name, dev_name(&pdev->dev), sizeof(subdev->name));
  729. v4l2_set_subdevdata(subdev, xtpg);
  730. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  731. subdev->entity.ops = &xtpg_media_ops;
  732. ret = media_entity_init(&subdev->entity, xtpg->npads, xtpg->pads, 0);
  733. if (ret < 0)
  734. goto error;
  735. v4l2_ctrl_handler_init(&xtpg->ctrl_handler, 3 + ARRAY_SIZE(xtpg_ctrls));
  736. xtpg->vblank = v4l2_ctrl_new_std(&xtpg->ctrl_handler, &xtpg_ctrl_ops,
  737. V4L2_CID_VBLANK, XTPG_MIN_VBLANK,
  738. XTPG_MAX_VBLANK, 1, 100);
  739. xtpg->hblank = v4l2_ctrl_new_std(&xtpg->ctrl_handler, &xtpg_ctrl_ops,
  740. V4L2_CID_HBLANK, XTPG_MIN_HBLANK,
  741. XTPG_MAX_HBLANK, 1, 100);
  742. xtpg->pattern = v4l2_ctrl_new_std_menu_items(&xtpg->ctrl_handler,
  743. &xtpg_ctrl_ops, V4L2_CID_TEST_PATTERN,
  744. ARRAY_SIZE(xtpg_pattern_strings) - 1,
  745. 1, 9, xtpg_pattern_strings);
  746. for (i = 0; i < ARRAY_SIZE(xtpg_ctrls); i++)
  747. v4l2_ctrl_new_custom(&xtpg->ctrl_handler, &xtpg_ctrls[i], NULL);
  748. if (xtpg->ctrl_handler.error) {
  749. dev_err(&pdev->dev, "failed to add controls\n");
  750. ret = xtpg->ctrl_handler.error;
  751. goto error;
  752. }
  753. subdev->ctrl_handler = &xtpg->ctrl_handler;
  754. xtpg_update_pattern_control(xtpg, true, true);
  755. ret = v4l2_ctrl_handler_setup(&xtpg->ctrl_handler);
  756. if (ret < 0) {
  757. dev_err(&pdev->dev, "failed to set controls\n");
  758. goto error;
  759. }
  760. platform_set_drvdata(pdev, xtpg);
  761. xvip_print_version(&xtpg->xvip);
  762. ret = v4l2_async_register_subdev(subdev);
  763. if (ret < 0) {
  764. dev_err(&pdev->dev, "failed to register subdev\n");
  765. goto error;
  766. }
  767. return 0;
  768. error:
  769. v4l2_ctrl_handler_free(&xtpg->ctrl_handler);
  770. media_entity_cleanup(&subdev->entity);
  771. xvtc_put(xtpg->vtc);
  772. error_resource:
  773. xvip_cleanup_resources(&xtpg->xvip);
  774. return ret;
  775. }
  776. static int xtpg_remove(struct platform_device *pdev)
  777. {
  778. struct xtpg_device *xtpg = platform_get_drvdata(pdev);
  779. struct v4l2_subdev *subdev = &xtpg->xvip.subdev;
  780. v4l2_async_unregister_subdev(subdev);
  781. v4l2_ctrl_handler_free(&xtpg->ctrl_handler);
  782. media_entity_cleanup(&subdev->entity);
  783. xvip_cleanup_resources(&xtpg->xvip);
  784. return 0;
  785. }
  786. static SIMPLE_DEV_PM_OPS(xtpg_pm_ops, xtpg_pm_suspend, xtpg_pm_resume);
  787. static const struct of_device_id xtpg_of_id_table[] = {
  788. { .compatible = "xlnx,v-tpg-5.0" },
  789. { }
  790. };
  791. MODULE_DEVICE_TABLE(of, xtpg_of_id_table);
  792. static struct platform_driver xtpg_driver = {
  793. .driver = {
  794. .name = "xilinx-tpg",
  795. .pm = &xtpg_pm_ops,
  796. .of_match_table = xtpg_of_id_table,
  797. },
  798. .probe = xtpg_probe,
  799. .remove = xtpg_remove,
  800. };
  801. module_platform_driver(xtpg_driver);
  802. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  803. MODULE_DESCRIPTION("Xilinx Test Pattern Generator Driver");
  804. MODULE_LICENSE("GPL v2");