xilinx-tpg.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xilinx Test Pattern Generator
  4. *
  5. * Copyright (C) 2013-2015 Ideas on Board
  6. * Copyright (C) 2013-2015 Xilinx, Inc.
  7. *
  8. * Contacts: Hyun Kwon <[email protected]>
  9. * Laurent Pinchart <[email protected]>
  10. */
  11. #include <linux/device.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/xilinx-v4l2-controls.h>
  17. #include <media/v4l2-async.h>
  18. #include <media/v4l2-ctrls.h>
  19. #include <media/v4l2-subdev.h>
  20. #include "xilinx-vip.h"
  21. #include "xilinx-vtc.h"
  22. #define XTPG_CTRL_STATUS_SLAVE_ERROR (1 << 16)
  23. #define XTPG_CTRL_IRQ_SLAVE_ERROR (1 << 16)
  24. #define XTPG_PATTERN_CONTROL 0x0100
  25. #define XTPG_PATTERN_MASK (0xf << 0)
  26. #define XTPG_PATTERN_CONTROL_CROSS_HAIRS (1 << 4)
  27. #define XTPG_PATTERN_CONTROL_MOVING_BOX (1 << 5)
  28. #define XTPG_PATTERN_CONTROL_COLOR_MASK_SHIFT 6
  29. #define XTPG_PATTERN_CONTROL_COLOR_MASK_MASK (0xf << 6)
  30. #define XTPG_PATTERN_CONTROL_STUCK_PIXEL (1 << 9)
  31. #define XTPG_PATTERN_CONTROL_NOISE (1 << 10)
  32. #define XTPG_PATTERN_CONTROL_MOTION (1 << 12)
  33. #define XTPG_MOTION_SPEED 0x0104
  34. #define XTPG_CROSS_HAIRS 0x0108
  35. #define XTPG_CROSS_HAIRS_ROW_SHIFT 0
  36. #define XTPG_CROSS_HAIRS_ROW_MASK (0xfff << 0)
  37. #define XTPG_CROSS_HAIRS_COLUMN_SHIFT 16
  38. #define XTPG_CROSS_HAIRS_COLUMN_MASK (0xfff << 16)
  39. #define XTPG_ZPLATE_HOR_CONTROL 0x010c
  40. #define XTPG_ZPLATE_VER_CONTROL 0x0110
  41. #define XTPG_ZPLATE_START_SHIFT 0
  42. #define XTPG_ZPLATE_START_MASK (0xffff << 0)
  43. #define XTPG_ZPLATE_SPEED_SHIFT 16
  44. #define XTPG_ZPLATE_SPEED_MASK (0xffff << 16)
  45. #define XTPG_BOX_SIZE 0x0114
  46. #define XTPG_BOX_COLOR 0x0118
  47. #define XTPG_STUCK_PIXEL_THRESH 0x011c
  48. #define XTPG_NOISE_GAIN 0x0120
  49. #define XTPG_BAYER_PHASE 0x0124
  50. #define XTPG_BAYER_PHASE_RGGB 0
  51. #define XTPG_BAYER_PHASE_GRBG 1
  52. #define XTPG_BAYER_PHASE_GBRG 2
  53. #define XTPG_BAYER_PHASE_BGGR 3
  54. #define XTPG_BAYER_PHASE_OFF 4
  55. /*
  56. * The minimum blanking value is one clock cycle for the front porch, one clock
  57. * cycle for the sync pulse and one clock cycle for the back porch.
  58. */
  59. #define XTPG_MIN_HBLANK 3
  60. #define XTPG_MAX_HBLANK (XVTC_MAX_HSIZE - XVIP_MIN_WIDTH)
  61. #define XTPG_MIN_VBLANK 3
  62. #define XTPG_MAX_VBLANK (XVTC_MAX_VSIZE - XVIP_MIN_HEIGHT)
  63. /**
  64. * struct xtpg_device - Xilinx Test Pattern Generator device structure
  65. * @xvip: Xilinx Video IP device
  66. * @pads: media pads
  67. * @npads: number of pads (1 or 2)
  68. * @has_input: whether an input is connected to the sink pad
  69. * @formats: active V4L2 media bus format for each pad
  70. * @default_format: default V4L2 media bus format
  71. * @vip_format: format information corresponding to the active format
  72. * @bayer: boolean flag if TPG is set to any bayer format
  73. * @ctrl_handler: control handler
  74. * @hblank: horizontal blanking control
  75. * @vblank: vertical blanking control
  76. * @pattern: test pattern control
  77. * @streaming: is the video stream active
  78. * @vtc: video timing controller
  79. * @vtmux_gpio: video timing mux GPIO
  80. */
  81. struct xtpg_device {
  82. struct xvip_device xvip;
  83. struct media_pad pads[2];
  84. unsigned int npads;
  85. bool has_input;
  86. struct v4l2_mbus_framefmt formats[2];
  87. struct v4l2_mbus_framefmt default_format;
  88. const struct xvip_video_format *vip_format;
  89. bool bayer;
  90. struct v4l2_ctrl_handler ctrl_handler;
  91. struct v4l2_ctrl *hblank;
  92. struct v4l2_ctrl *vblank;
  93. struct v4l2_ctrl *pattern;
  94. bool streaming;
  95. struct xvtc_device *vtc;
  96. struct gpio_desc *vtmux_gpio;
  97. };
  98. static inline struct xtpg_device *to_tpg(struct v4l2_subdev *subdev)
  99. {
  100. return container_of(subdev, struct xtpg_device, xvip.subdev);
  101. }
  102. static u32 xtpg_get_bayer_phase(unsigned int code)
  103. {
  104. switch (code) {
  105. case MEDIA_BUS_FMT_SRGGB8_1X8:
  106. return XTPG_BAYER_PHASE_RGGB;
  107. case MEDIA_BUS_FMT_SGRBG8_1X8:
  108. return XTPG_BAYER_PHASE_GRBG;
  109. case MEDIA_BUS_FMT_SGBRG8_1X8:
  110. return XTPG_BAYER_PHASE_GBRG;
  111. case MEDIA_BUS_FMT_SBGGR8_1X8:
  112. return XTPG_BAYER_PHASE_BGGR;
  113. default:
  114. return XTPG_BAYER_PHASE_OFF;
  115. }
  116. }
  117. static void __xtpg_update_pattern_control(struct xtpg_device *xtpg,
  118. bool passthrough, bool pattern)
  119. {
  120. u32 pattern_mask = (1 << (xtpg->pattern->maximum + 1)) - 1;
  121. /*
  122. * If the TPG has no sink pad or no input connected to its sink pad
  123. * passthrough mode can't be enabled.
  124. */
  125. if (xtpg->npads == 1 || !xtpg->has_input)
  126. passthrough = false;
  127. /* If passthrough mode is allowed unmask bit 0. */
  128. if (passthrough)
  129. pattern_mask &= ~1;
  130. /* If test pattern mode is allowed unmask all other bits. */
  131. if (pattern)
  132. pattern_mask &= 1;
  133. __v4l2_ctrl_modify_range(xtpg->pattern, 0, xtpg->pattern->maximum,
  134. pattern_mask, pattern ? 9 : 0);
  135. }
  136. static void xtpg_update_pattern_control(struct xtpg_device *xtpg,
  137. bool passthrough, bool pattern)
  138. {
  139. mutex_lock(xtpg->ctrl_handler.lock);
  140. __xtpg_update_pattern_control(xtpg, passthrough, pattern);
  141. mutex_unlock(xtpg->ctrl_handler.lock);
  142. }
  143. /* -----------------------------------------------------------------------------
  144. * V4L2 Subdevice Video Operations
  145. */
  146. static int xtpg_s_stream(struct v4l2_subdev *subdev, int enable)
  147. {
  148. struct xtpg_device *xtpg = to_tpg(subdev);
  149. unsigned int width = xtpg->formats[0].width;
  150. unsigned int height = xtpg->formats[0].height;
  151. bool passthrough;
  152. u32 bayer_phase;
  153. if (!enable) {
  154. xvip_stop(&xtpg->xvip);
  155. if (xtpg->vtc)
  156. xvtc_generator_stop(xtpg->vtc);
  157. xtpg_update_pattern_control(xtpg, true, true);
  158. xtpg->streaming = false;
  159. return 0;
  160. }
  161. xvip_set_frame_size(&xtpg->xvip, &xtpg->formats[0]);
  162. if (xtpg->vtc) {
  163. struct xvtc_config config = {
  164. .hblank_start = width,
  165. .hsync_start = width + 1,
  166. .vblank_start = height,
  167. .vsync_start = height + 1,
  168. };
  169. unsigned int htotal;
  170. unsigned int vtotal;
  171. htotal = min_t(unsigned int, XVTC_MAX_HSIZE,
  172. v4l2_ctrl_g_ctrl(xtpg->hblank) + width);
  173. vtotal = min_t(unsigned int, XVTC_MAX_VSIZE,
  174. v4l2_ctrl_g_ctrl(xtpg->vblank) + height);
  175. config.hsync_end = htotal - 1;
  176. config.hsize = htotal;
  177. config.vsync_end = vtotal - 1;
  178. config.vsize = vtotal;
  179. xvtc_generator_start(xtpg->vtc, &config);
  180. }
  181. /*
  182. * Configure the bayer phase and video timing mux based on the
  183. * operation mode (passthrough or test pattern generation). The test
  184. * pattern can be modified by the control set handler, we thus need to
  185. * take the control lock here to avoid races.
  186. */
  187. mutex_lock(xtpg->ctrl_handler.lock);
  188. xvip_clr_and_set(&xtpg->xvip, XTPG_PATTERN_CONTROL,
  189. XTPG_PATTERN_MASK, xtpg->pattern->cur.val);
  190. /*
  191. * Switching between passthrough and test pattern generation modes isn't
  192. * allowed during streaming, update the control range accordingly.
  193. */
  194. passthrough = xtpg->pattern->cur.val == 0;
  195. __xtpg_update_pattern_control(xtpg, passthrough, !passthrough);
  196. xtpg->streaming = true;
  197. mutex_unlock(xtpg->ctrl_handler.lock);
  198. /*
  199. * For TPG v5.0, the bayer phase needs to be off for the pass through
  200. * mode, otherwise the external input would be subsampled.
  201. */
  202. bayer_phase = passthrough ? XTPG_BAYER_PHASE_OFF
  203. : xtpg_get_bayer_phase(xtpg->formats[0].code);
  204. xvip_write(&xtpg->xvip, XTPG_BAYER_PHASE, bayer_phase);
  205. if (xtpg->vtmux_gpio)
  206. gpiod_set_value_cansleep(xtpg->vtmux_gpio, !passthrough);
  207. xvip_start(&xtpg->xvip);
  208. return 0;
  209. }
  210. /* -----------------------------------------------------------------------------
  211. * V4L2 Subdevice Pad Operations
  212. */
  213. static struct v4l2_mbus_framefmt *
  214. __xtpg_get_pad_format(struct xtpg_device *xtpg,
  215. struct v4l2_subdev_state *sd_state,
  216. unsigned int pad, u32 which)
  217. {
  218. switch (which) {
  219. case V4L2_SUBDEV_FORMAT_TRY:
  220. return v4l2_subdev_get_try_format(&xtpg->xvip.subdev,
  221. sd_state, pad);
  222. case V4L2_SUBDEV_FORMAT_ACTIVE:
  223. return &xtpg->formats[pad];
  224. default:
  225. return NULL;
  226. }
  227. }
  228. static int xtpg_get_format(struct v4l2_subdev *subdev,
  229. struct v4l2_subdev_state *sd_state,
  230. struct v4l2_subdev_format *fmt)
  231. {
  232. struct xtpg_device *xtpg = to_tpg(subdev);
  233. fmt->format = *__xtpg_get_pad_format(xtpg, sd_state, fmt->pad,
  234. fmt->which);
  235. return 0;
  236. }
  237. static int xtpg_set_format(struct v4l2_subdev *subdev,
  238. struct v4l2_subdev_state *sd_state,
  239. struct v4l2_subdev_format *fmt)
  240. {
  241. struct xtpg_device *xtpg = to_tpg(subdev);
  242. struct v4l2_mbus_framefmt *__format;
  243. u32 bayer_phase;
  244. __format = __xtpg_get_pad_format(xtpg, sd_state, fmt->pad, fmt->which);
  245. /* In two pads mode the source pad format is always identical to the
  246. * sink pad format.
  247. */
  248. if (xtpg->npads == 2 && fmt->pad == 1) {
  249. fmt->format = *__format;
  250. return 0;
  251. }
  252. /* Bayer phase is configurable at runtime */
  253. if (xtpg->bayer) {
  254. bayer_phase = xtpg_get_bayer_phase(fmt->format.code);
  255. if (bayer_phase != XTPG_BAYER_PHASE_OFF)
  256. __format->code = fmt->format.code;
  257. }
  258. xvip_set_format_size(__format, fmt);
  259. fmt->format = *__format;
  260. /* Propagate the format to the source pad. */
  261. if (xtpg->npads == 2) {
  262. __format = __xtpg_get_pad_format(xtpg, sd_state, 1,
  263. 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_state *sd_state,
  273. struct v4l2_subdev_frame_size_enum *fse)
  274. {
  275. struct v4l2_mbus_framefmt *format;
  276. format = v4l2_subdev_get_try_format(subdev, sd_state, 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->state, 0);
  300. *format = xtpg->default_format;
  301. if (xtpg->npads == 2) {
  302. format = v4l2_subdev_get_try_format(subdev, fh->state, 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 const struct v4l2_subdev_core_ops xtpg_core_ops = {
  399. };
  400. static const struct v4l2_subdev_video_ops xtpg_video_ops = {
  401. .s_stream = xtpg_s_stream,
  402. };
  403. static const 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 const 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 (!of_node_name_eq(port, "port"))
  643. continue;
  644. format = xvip_of_get_format(port);
  645. if (IS_ERR(format)) {
  646. dev_err(dev, "invalid format in DT");
  647. of_node_put(port);
  648. return PTR_ERR(format);
  649. }
  650. /* Get and check the format description */
  651. if (!xtpg->vip_format) {
  652. xtpg->vip_format = format;
  653. } else if (xtpg->vip_format != format) {
  654. dev_err(dev, "in/out format mismatch in DT");
  655. of_node_put(port);
  656. return -EINVAL;
  657. }
  658. if (nports == 0) {
  659. endpoint = of_get_next_child(port, NULL);
  660. if (endpoint)
  661. has_endpoint = true;
  662. of_node_put(endpoint);
  663. }
  664. /* Count the number of ports. */
  665. nports++;
  666. }
  667. if (nports != 1 && nports != 2) {
  668. dev_err(dev, "invalid number of ports %u\n", nports);
  669. return -EINVAL;
  670. }
  671. xtpg->npads = nports;
  672. if (nports == 2 && has_endpoint)
  673. xtpg->has_input = true;
  674. return 0;
  675. }
  676. static int xtpg_probe(struct platform_device *pdev)
  677. {
  678. struct v4l2_subdev *subdev;
  679. struct xtpg_device *xtpg;
  680. u32 i, bayer_phase;
  681. int ret;
  682. xtpg = devm_kzalloc(&pdev->dev, sizeof(*xtpg), GFP_KERNEL);
  683. if (!xtpg)
  684. return -ENOMEM;
  685. xtpg->xvip.dev = &pdev->dev;
  686. ret = xtpg_parse_of(xtpg);
  687. if (ret < 0)
  688. return ret;
  689. ret = xvip_init_resources(&xtpg->xvip);
  690. if (ret < 0)
  691. return ret;
  692. xtpg->vtmux_gpio = devm_gpiod_get_optional(&pdev->dev, "timing",
  693. GPIOD_OUT_HIGH);
  694. if (IS_ERR(xtpg->vtmux_gpio)) {
  695. ret = PTR_ERR(xtpg->vtmux_gpio);
  696. goto error_resource;
  697. }
  698. xtpg->vtc = xvtc_of_get(pdev->dev.of_node);
  699. if (IS_ERR(xtpg->vtc)) {
  700. ret = PTR_ERR(xtpg->vtc);
  701. goto error_resource;
  702. }
  703. /* Reset and initialize the core */
  704. xvip_reset(&xtpg->xvip);
  705. /* Initialize V4L2 subdevice and media entity. Pad numbers depend on the
  706. * number of pads.
  707. */
  708. if (xtpg->npads == 2) {
  709. xtpg->pads[0].flags = MEDIA_PAD_FL_SINK;
  710. xtpg->pads[1].flags = MEDIA_PAD_FL_SOURCE;
  711. } else {
  712. xtpg->pads[0].flags = MEDIA_PAD_FL_SOURCE;
  713. }
  714. /* Initialize the default format */
  715. xtpg->default_format.code = xtpg->vip_format->code;
  716. xtpg->default_format.field = V4L2_FIELD_NONE;
  717. xtpg->default_format.colorspace = V4L2_COLORSPACE_SRGB;
  718. xvip_get_frame_size(&xtpg->xvip, &xtpg->default_format);
  719. bayer_phase = xtpg_get_bayer_phase(xtpg->vip_format->code);
  720. if (bayer_phase != XTPG_BAYER_PHASE_OFF)
  721. xtpg->bayer = true;
  722. xtpg->formats[0] = xtpg->default_format;
  723. if (xtpg->npads == 2)
  724. xtpg->formats[1] = xtpg->default_format;
  725. /* Initialize V4L2 subdevice and media entity */
  726. subdev = &xtpg->xvip.subdev;
  727. v4l2_subdev_init(subdev, &xtpg_ops);
  728. subdev->dev = &pdev->dev;
  729. subdev->internal_ops = &xtpg_internal_ops;
  730. strscpy(subdev->name, dev_name(&pdev->dev), sizeof(subdev->name));
  731. v4l2_set_subdevdata(subdev, xtpg);
  732. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  733. subdev->entity.ops = &xtpg_media_ops;
  734. ret = media_entity_pads_init(&subdev->entity, xtpg->npads, xtpg->pads);
  735. if (ret < 0)
  736. goto error;
  737. v4l2_ctrl_handler_init(&xtpg->ctrl_handler, 3 + ARRAY_SIZE(xtpg_ctrls));
  738. xtpg->vblank = v4l2_ctrl_new_std(&xtpg->ctrl_handler, &xtpg_ctrl_ops,
  739. V4L2_CID_VBLANK, XTPG_MIN_VBLANK,
  740. XTPG_MAX_VBLANK, 1, 100);
  741. xtpg->hblank = v4l2_ctrl_new_std(&xtpg->ctrl_handler, &xtpg_ctrl_ops,
  742. V4L2_CID_HBLANK, XTPG_MIN_HBLANK,
  743. XTPG_MAX_HBLANK, 1, 100);
  744. xtpg->pattern = v4l2_ctrl_new_std_menu_items(&xtpg->ctrl_handler,
  745. &xtpg_ctrl_ops, V4L2_CID_TEST_PATTERN,
  746. ARRAY_SIZE(xtpg_pattern_strings) - 1,
  747. 1, 9, xtpg_pattern_strings);
  748. for (i = 0; i < ARRAY_SIZE(xtpg_ctrls); i++)
  749. v4l2_ctrl_new_custom(&xtpg->ctrl_handler, &xtpg_ctrls[i], NULL);
  750. if (xtpg->ctrl_handler.error) {
  751. dev_err(&pdev->dev, "failed to add controls\n");
  752. ret = xtpg->ctrl_handler.error;
  753. goto error;
  754. }
  755. subdev->ctrl_handler = &xtpg->ctrl_handler;
  756. xtpg_update_pattern_control(xtpg, true, true);
  757. ret = v4l2_ctrl_handler_setup(&xtpg->ctrl_handler);
  758. if (ret < 0) {
  759. dev_err(&pdev->dev, "failed to set controls\n");
  760. goto error;
  761. }
  762. platform_set_drvdata(pdev, xtpg);
  763. xvip_print_version(&xtpg->xvip);
  764. ret = v4l2_async_register_subdev(subdev);
  765. if (ret < 0) {
  766. dev_err(&pdev->dev, "failed to register subdev\n");
  767. goto error;
  768. }
  769. return 0;
  770. error:
  771. v4l2_ctrl_handler_free(&xtpg->ctrl_handler);
  772. media_entity_cleanup(&subdev->entity);
  773. xvtc_put(xtpg->vtc);
  774. error_resource:
  775. xvip_cleanup_resources(&xtpg->xvip);
  776. return ret;
  777. }
  778. static int xtpg_remove(struct platform_device *pdev)
  779. {
  780. struct xtpg_device *xtpg = platform_get_drvdata(pdev);
  781. struct v4l2_subdev *subdev = &xtpg->xvip.subdev;
  782. v4l2_async_unregister_subdev(subdev);
  783. v4l2_ctrl_handler_free(&xtpg->ctrl_handler);
  784. media_entity_cleanup(&subdev->entity);
  785. xvip_cleanup_resources(&xtpg->xvip);
  786. return 0;
  787. }
  788. static SIMPLE_DEV_PM_OPS(xtpg_pm_ops, xtpg_pm_suspend, xtpg_pm_resume);
  789. static const struct of_device_id xtpg_of_id_table[] = {
  790. { .compatible = "xlnx,v-tpg-5.0" },
  791. { }
  792. };
  793. MODULE_DEVICE_TABLE(of, xtpg_of_id_table);
  794. static struct platform_driver xtpg_driver = {
  795. .driver = {
  796. .name = "xilinx-tpg",
  797. .pm = &xtpg_pm_ops,
  798. .of_match_table = xtpg_of_id_table,
  799. },
  800. .probe = xtpg_probe,
  801. .remove = xtpg_remove,
  802. };
  803. module_platform_driver(xtpg_driver);
  804. MODULE_AUTHOR("Laurent Pinchart <[email protected]>");
  805. MODULE_DESCRIPTION("Xilinx Test Pattern Generator Driver");
  806. MODULE_LICENSE("GPL v2");