adv748x-hdmi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. * Driver for Analog Devices ADV748X HDMI receiver and Component Processor (CP)
  3. *
  4. * Copyright (C) 2017 Renesas Electronics Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <media/v4l2-ctrls.h>
  14. #include <media/v4l2-device.h>
  15. #include <media/v4l2-dv-timings.h>
  16. #include <media/v4l2-ioctl.h>
  17. #include <uapi/linux/v4l2-dv-timings.h>
  18. #include "adv748x.h"
  19. /* -----------------------------------------------------------------------------
  20. * HDMI and CP
  21. */
  22. #define ADV748X_HDMI_MIN_WIDTH 640
  23. #define ADV748X_HDMI_MAX_WIDTH 1920
  24. #define ADV748X_HDMI_MIN_HEIGHT 480
  25. #define ADV748X_HDMI_MAX_HEIGHT 1200
  26. /* V4L2_DV_BT_CEA_720X480I59_94 - 0.5 MHz */
  27. #define ADV748X_HDMI_MIN_PIXELCLOCK 13000000
  28. /* V4L2_DV_BT_DMT_1600X1200P60 */
  29. #define ADV748X_HDMI_MAX_PIXELCLOCK 162000000
  30. static const struct v4l2_dv_timings_cap adv748x_hdmi_timings_cap = {
  31. .type = V4L2_DV_BT_656_1120,
  32. /* keep this initialization for compatibility with GCC < 4.4.6 */
  33. .reserved = { 0 },
  34. V4L2_INIT_BT_TIMINGS(ADV748X_HDMI_MIN_WIDTH, ADV748X_HDMI_MAX_WIDTH,
  35. ADV748X_HDMI_MIN_HEIGHT, ADV748X_HDMI_MAX_HEIGHT,
  36. ADV748X_HDMI_MIN_PIXELCLOCK,
  37. ADV748X_HDMI_MAX_PIXELCLOCK,
  38. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT,
  39. V4L2_DV_BT_CAP_PROGRESSIVE)
  40. };
  41. struct adv748x_hdmi_video_standards {
  42. struct v4l2_dv_timings timings;
  43. u8 vid_std;
  44. u8 v_freq;
  45. };
  46. static const struct adv748x_hdmi_video_standards
  47. adv748x_hdmi_video_standards[] = {
  48. { V4L2_DV_BT_CEA_720X480P59_94, 0x4a, 0x00 },
  49. { V4L2_DV_BT_CEA_720X576P50, 0x4b, 0x00 },
  50. { V4L2_DV_BT_CEA_1280X720P60, 0x53, 0x00 },
  51. { V4L2_DV_BT_CEA_1280X720P50, 0x53, 0x01 },
  52. { V4L2_DV_BT_CEA_1280X720P30, 0x53, 0x02 },
  53. { V4L2_DV_BT_CEA_1280X720P25, 0x53, 0x03 },
  54. { V4L2_DV_BT_CEA_1280X720P24, 0x53, 0x04 },
  55. { V4L2_DV_BT_CEA_1920X1080P60, 0x5e, 0x00 },
  56. { V4L2_DV_BT_CEA_1920X1080P50, 0x5e, 0x01 },
  57. { V4L2_DV_BT_CEA_1920X1080P30, 0x5e, 0x02 },
  58. { V4L2_DV_BT_CEA_1920X1080P25, 0x5e, 0x03 },
  59. { V4L2_DV_BT_CEA_1920X1080P24, 0x5e, 0x04 },
  60. /* SVGA */
  61. { V4L2_DV_BT_DMT_800X600P56, 0x80, 0x00 },
  62. { V4L2_DV_BT_DMT_800X600P60, 0x81, 0x00 },
  63. { V4L2_DV_BT_DMT_800X600P72, 0x82, 0x00 },
  64. { V4L2_DV_BT_DMT_800X600P75, 0x83, 0x00 },
  65. { V4L2_DV_BT_DMT_800X600P85, 0x84, 0x00 },
  66. /* SXGA */
  67. { V4L2_DV_BT_DMT_1280X1024P60, 0x85, 0x00 },
  68. { V4L2_DV_BT_DMT_1280X1024P75, 0x86, 0x00 },
  69. /* VGA */
  70. { V4L2_DV_BT_DMT_640X480P60, 0x88, 0x00 },
  71. { V4L2_DV_BT_DMT_640X480P72, 0x89, 0x00 },
  72. { V4L2_DV_BT_DMT_640X480P75, 0x8a, 0x00 },
  73. { V4L2_DV_BT_DMT_640X480P85, 0x8b, 0x00 },
  74. /* XGA */
  75. { V4L2_DV_BT_DMT_1024X768P60, 0x8c, 0x00 },
  76. { V4L2_DV_BT_DMT_1024X768P70, 0x8d, 0x00 },
  77. { V4L2_DV_BT_DMT_1024X768P75, 0x8e, 0x00 },
  78. { V4L2_DV_BT_DMT_1024X768P85, 0x8f, 0x00 },
  79. /* UXGA */
  80. { V4L2_DV_BT_DMT_1600X1200P60, 0x96, 0x00 },
  81. };
  82. static void adv748x_hdmi_fill_format(struct adv748x_hdmi *hdmi,
  83. struct v4l2_mbus_framefmt *fmt)
  84. {
  85. memset(fmt, 0, sizeof(*fmt));
  86. fmt->code = MEDIA_BUS_FMT_RGB888_1X24;
  87. fmt->field = hdmi->timings.bt.interlaced ?
  88. V4L2_FIELD_ALTERNATE : V4L2_FIELD_NONE;
  89. /* TODO: The colorspace depends on the AVI InfoFrame contents */
  90. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  91. fmt->width = hdmi->timings.bt.width;
  92. fmt->height = hdmi->timings.bt.height;
  93. if (fmt->field == V4L2_FIELD_ALTERNATE)
  94. fmt->height /= 2;
  95. }
  96. static void adv748x_fill_optional_dv_timings(struct v4l2_dv_timings *timings)
  97. {
  98. v4l2_find_dv_timings_cap(timings, &adv748x_hdmi_timings_cap,
  99. 250000, NULL, NULL);
  100. }
  101. static bool adv748x_hdmi_has_signal(struct adv748x_state *state)
  102. {
  103. int val;
  104. /* Check that VERT_FILTER and DE_REGEN is locked */
  105. val = hdmi_read(state, ADV748X_HDMI_LW1);
  106. return (val & ADV748X_HDMI_LW1_VERT_FILTER) &&
  107. (val & ADV748X_HDMI_LW1_DE_REGEN);
  108. }
  109. static int adv748x_hdmi_read_pixelclock(struct adv748x_state *state)
  110. {
  111. int a, b;
  112. a = hdmi_read(state, ADV748X_HDMI_TMDS_1);
  113. b = hdmi_read(state, ADV748X_HDMI_TMDS_2);
  114. if (a < 0 || b < 0)
  115. return -ENODATA;
  116. /*
  117. * The high 9 bits store TMDS frequency measurement in MHz
  118. * The low 7 bits of TMDS_2 store the 7-bit TMDS fractional frequency
  119. * measurement in 1/128 MHz
  120. */
  121. return ((a << 1) | (b >> 7)) * 1000000 + (b & 0x7f) * 1000000 / 128;
  122. }
  123. /*
  124. * adv748x_hdmi_set_de_timings: Adjust horizontal picture offset through DE
  125. *
  126. * HDMI CP uses a Data Enable synchronisation timing reference
  127. *
  128. * Vary the leading and trailing edge position of the DE signal output by the CP
  129. * core. Values are stored as signed-twos-complement in one-pixel-clock units
  130. *
  131. * The start and end are shifted equally by the 10-bit shift value.
  132. */
  133. static void adv748x_hdmi_set_de_timings(struct adv748x_state *state, int shift)
  134. {
  135. u8 high, low;
  136. /* POS_HIGH stores bits 8 and 9 of both the start and end */
  137. high = ADV748X_CP_DE_POS_HIGH_SET;
  138. high |= (shift & 0x300) >> 8;
  139. low = shift & 0xff;
  140. /* The sequence of the writes is important and must be followed */
  141. cp_write(state, ADV748X_CP_DE_POS_HIGH, high);
  142. cp_write(state, ADV748X_CP_DE_POS_END_LOW, low);
  143. high |= (shift & 0x300) >> 6;
  144. cp_write(state, ADV748X_CP_DE_POS_HIGH, high);
  145. cp_write(state, ADV748X_CP_DE_POS_START_LOW, low);
  146. }
  147. static int adv748x_hdmi_set_video_timings(struct adv748x_state *state,
  148. const struct v4l2_dv_timings *timings)
  149. {
  150. const struct adv748x_hdmi_video_standards *stds =
  151. adv748x_hdmi_video_standards;
  152. unsigned int i;
  153. for (i = 0; i < ARRAY_SIZE(adv748x_hdmi_video_standards); i++) {
  154. if (!v4l2_match_dv_timings(timings, &stds[i].timings, 250000,
  155. false))
  156. continue;
  157. }
  158. if (i >= ARRAY_SIZE(adv748x_hdmi_video_standards))
  159. return -EINVAL;
  160. /*
  161. * When setting cp_vid_std to either 720p, 1080i, or 1080p, the video
  162. * will get shifted horizontally to the left in active video mode.
  163. * The de_h_start and de_h_end controls are used to centre the picture
  164. * correctly
  165. */
  166. switch (stds[i].vid_std) {
  167. case 0x53: /* 720p */
  168. adv748x_hdmi_set_de_timings(state, -40);
  169. break;
  170. case 0x54: /* 1080i */
  171. case 0x5e: /* 1080p */
  172. adv748x_hdmi_set_de_timings(state, -44);
  173. break;
  174. default:
  175. adv748x_hdmi_set_de_timings(state, 0);
  176. break;
  177. }
  178. io_write(state, ADV748X_IO_VID_STD, stds[i].vid_std);
  179. io_clrset(state, ADV748X_IO_DATAPATH, ADV748X_IO_DATAPATH_VFREQ_M,
  180. stds[i].v_freq << ADV748X_IO_DATAPATH_VFREQ_SHIFT);
  181. return 0;
  182. }
  183. /* -----------------------------------------------------------------------------
  184. * v4l2_subdev_video_ops
  185. */
  186. static int adv748x_hdmi_s_dv_timings(struct v4l2_subdev *sd,
  187. struct v4l2_dv_timings *timings)
  188. {
  189. struct adv748x_hdmi *hdmi = adv748x_sd_to_hdmi(sd);
  190. struct adv748x_state *state = adv748x_hdmi_to_state(hdmi);
  191. int ret;
  192. if (!timings)
  193. return -EINVAL;
  194. if (v4l2_match_dv_timings(&hdmi->timings, timings, 0, false))
  195. return 0;
  196. if (!v4l2_valid_dv_timings(timings, &adv748x_hdmi_timings_cap,
  197. NULL, NULL))
  198. return -ERANGE;
  199. adv748x_fill_optional_dv_timings(timings);
  200. mutex_lock(&state->mutex);
  201. ret = adv748x_hdmi_set_video_timings(state, timings);
  202. if (ret)
  203. goto error;
  204. hdmi->timings = *timings;
  205. cp_clrset(state, ADV748X_CP_VID_ADJ_2, ADV748X_CP_VID_ADJ_2_INTERLACED,
  206. timings->bt.interlaced ?
  207. ADV748X_CP_VID_ADJ_2_INTERLACED : 0);
  208. mutex_unlock(&state->mutex);
  209. return 0;
  210. error:
  211. mutex_unlock(&state->mutex);
  212. return ret;
  213. }
  214. static int adv748x_hdmi_g_dv_timings(struct v4l2_subdev *sd,
  215. struct v4l2_dv_timings *timings)
  216. {
  217. struct adv748x_hdmi *hdmi = adv748x_sd_to_hdmi(sd);
  218. struct adv748x_state *state = adv748x_hdmi_to_state(hdmi);
  219. mutex_lock(&state->mutex);
  220. *timings = hdmi->timings;
  221. mutex_unlock(&state->mutex);
  222. return 0;
  223. }
  224. static int adv748x_hdmi_query_dv_timings(struct v4l2_subdev *sd,
  225. struct v4l2_dv_timings *timings)
  226. {
  227. struct adv748x_hdmi *hdmi = adv748x_sd_to_hdmi(sd);
  228. struct adv748x_state *state = adv748x_hdmi_to_state(hdmi);
  229. struct v4l2_bt_timings *bt = &timings->bt;
  230. int pixelclock;
  231. int polarity;
  232. if (!timings)
  233. return -EINVAL;
  234. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  235. if (!adv748x_hdmi_has_signal(state))
  236. return -ENOLINK;
  237. pixelclock = adv748x_hdmi_read_pixelclock(state);
  238. if (pixelclock < 0)
  239. return -ENODATA;
  240. timings->type = V4L2_DV_BT_656_1120;
  241. bt->pixelclock = pixelclock;
  242. bt->interlaced = hdmi_read(state, ADV748X_HDMI_F1H1) &
  243. ADV748X_HDMI_F1H1_INTERLACED ?
  244. V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
  245. bt->width = hdmi_read16(state, ADV748X_HDMI_LW1,
  246. ADV748X_HDMI_LW1_WIDTH_MASK);
  247. bt->height = hdmi_read16(state, ADV748X_HDMI_F0H1,
  248. ADV748X_HDMI_F0H1_HEIGHT_MASK);
  249. bt->hfrontporch = hdmi_read16(state, ADV748X_HDMI_HFRONT_PORCH,
  250. ADV748X_HDMI_HFRONT_PORCH_MASK);
  251. bt->hsync = hdmi_read16(state, ADV748X_HDMI_HSYNC_WIDTH,
  252. ADV748X_HDMI_HSYNC_WIDTH_MASK);
  253. bt->hbackporch = hdmi_read16(state, ADV748X_HDMI_HBACK_PORCH,
  254. ADV748X_HDMI_HBACK_PORCH_MASK);
  255. bt->vfrontporch = hdmi_read16(state, ADV748X_HDMI_VFRONT_PORCH,
  256. ADV748X_HDMI_VFRONT_PORCH_MASK) / 2;
  257. bt->vsync = hdmi_read16(state, ADV748X_HDMI_VSYNC_WIDTH,
  258. ADV748X_HDMI_VSYNC_WIDTH_MASK) / 2;
  259. bt->vbackporch = hdmi_read16(state, ADV748X_HDMI_VBACK_PORCH,
  260. ADV748X_HDMI_VBACK_PORCH_MASK) / 2;
  261. polarity = hdmi_read(state, 0x05);
  262. bt->polarities = (polarity & BIT(4) ? V4L2_DV_VSYNC_POS_POL : 0) |
  263. (polarity & BIT(5) ? V4L2_DV_HSYNC_POS_POL : 0);
  264. if (bt->interlaced == V4L2_DV_INTERLACED) {
  265. bt->height += hdmi_read16(state, 0x0b, 0x1fff);
  266. bt->il_vfrontporch = hdmi_read16(state, 0x2c, 0x3fff) / 2;
  267. bt->il_vsync = hdmi_read16(state, 0x30, 0x3fff) / 2;
  268. bt->il_vbackporch = hdmi_read16(state, 0x34, 0x3fff) / 2;
  269. }
  270. adv748x_fill_optional_dv_timings(timings);
  271. /*
  272. * No interrupt handling is implemented yet.
  273. * There should be an IRQ when a cable is plugged and the new timings
  274. * should be figured out and stored to state.
  275. */
  276. hdmi->timings = *timings;
  277. return 0;
  278. }
  279. static int adv748x_hdmi_g_input_status(struct v4l2_subdev *sd, u32 *status)
  280. {
  281. struct adv748x_hdmi *hdmi = adv748x_sd_to_hdmi(sd);
  282. struct adv748x_state *state = adv748x_hdmi_to_state(hdmi);
  283. mutex_lock(&state->mutex);
  284. *status = adv748x_hdmi_has_signal(state) ? 0 : V4L2_IN_ST_NO_SIGNAL;
  285. mutex_unlock(&state->mutex);
  286. return 0;
  287. }
  288. static int adv748x_hdmi_s_stream(struct v4l2_subdev *sd, int enable)
  289. {
  290. struct adv748x_hdmi *hdmi = adv748x_sd_to_hdmi(sd);
  291. struct adv748x_state *state = adv748x_hdmi_to_state(hdmi);
  292. int ret;
  293. mutex_lock(&state->mutex);
  294. ret = adv748x_txa_power(state, enable);
  295. if (ret)
  296. goto done;
  297. if (adv748x_hdmi_has_signal(state))
  298. adv_dbg(state, "Detected HDMI signal\n");
  299. else
  300. adv_dbg(state, "Couldn't detect HDMI video signal\n");
  301. done:
  302. mutex_unlock(&state->mutex);
  303. return ret;
  304. }
  305. static int adv748x_hdmi_g_pixelaspect(struct v4l2_subdev *sd,
  306. struct v4l2_fract *aspect)
  307. {
  308. aspect->numerator = 1;
  309. aspect->denominator = 1;
  310. return 0;
  311. }
  312. static const struct v4l2_subdev_video_ops adv748x_video_ops_hdmi = {
  313. .s_dv_timings = adv748x_hdmi_s_dv_timings,
  314. .g_dv_timings = adv748x_hdmi_g_dv_timings,
  315. .query_dv_timings = adv748x_hdmi_query_dv_timings,
  316. .g_input_status = adv748x_hdmi_g_input_status,
  317. .s_stream = adv748x_hdmi_s_stream,
  318. .g_pixelaspect = adv748x_hdmi_g_pixelaspect,
  319. };
  320. /* -----------------------------------------------------------------------------
  321. * v4l2_subdev_pad_ops
  322. */
  323. static int adv748x_hdmi_propagate_pixelrate(struct adv748x_hdmi *hdmi)
  324. {
  325. struct v4l2_subdev *tx;
  326. struct v4l2_dv_timings timings;
  327. tx = adv748x_get_remote_sd(&hdmi->pads[ADV748X_HDMI_SOURCE]);
  328. if (!tx)
  329. return -ENOLINK;
  330. adv748x_hdmi_query_dv_timings(&hdmi->sd, &timings);
  331. return adv748x_csi2_set_pixelrate(tx, timings.bt.pixelclock);
  332. }
  333. static int adv748x_hdmi_enum_mbus_code(struct v4l2_subdev *sd,
  334. struct v4l2_subdev_pad_config *cfg,
  335. struct v4l2_subdev_mbus_code_enum *code)
  336. {
  337. if (code->index != 0)
  338. return -EINVAL;
  339. code->code = MEDIA_BUS_FMT_RGB888_1X24;
  340. return 0;
  341. }
  342. static int adv748x_hdmi_get_format(struct v4l2_subdev *sd,
  343. struct v4l2_subdev_pad_config *cfg,
  344. struct v4l2_subdev_format *sdformat)
  345. {
  346. struct adv748x_hdmi *hdmi = adv748x_sd_to_hdmi(sd);
  347. struct v4l2_mbus_framefmt *mbusformat;
  348. if (sdformat->pad != ADV748X_HDMI_SOURCE)
  349. return -EINVAL;
  350. if (sdformat->which == V4L2_SUBDEV_FORMAT_TRY) {
  351. mbusformat = v4l2_subdev_get_try_format(sd, cfg, sdformat->pad);
  352. sdformat->format = *mbusformat;
  353. } else {
  354. adv748x_hdmi_fill_format(hdmi, &sdformat->format);
  355. adv748x_hdmi_propagate_pixelrate(hdmi);
  356. }
  357. return 0;
  358. }
  359. static int adv748x_hdmi_set_format(struct v4l2_subdev *sd,
  360. struct v4l2_subdev_pad_config *cfg,
  361. struct v4l2_subdev_format *sdformat)
  362. {
  363. struct v4l2_mbus_framefmt *mbusformat;
  364. if (sdformat->pad != ADV748X_HDMI_SOURCE)
  365. return -EINVAL;
  366. if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  367. return adv748x_hdmi_get_format(sd, cfg, sdformat);
  368. mbusformat = v4l2_subdev_get_try_format(sd, cfg, sdformat->pad);
  369. *mbusformat = sdformat->format;
  370. return 0;
  371. }
  372. static int adv748x_hdmi_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  373. {
  374. struct adv748x_hdmi *hdmi = adv748x_sd_to_hdmi(sd);
  375. memset(edid->reserved, 0, sizeof(edid->reserved));
  376. if (!hdmi->edid.present)
  377. return -ENODATA;
  378. if (edid->start_block == 0 && edid->blocks == 0) {
  379. edid->blocks = hdmi->edid.blocks;
  380. return 0;
  381. }
  382. if (edid->start_block >= hdmi->edid.blocks)
  383. return -EINVAL;
  384. if (edid->start_block + edid->blocks > hdmi->edid.blocks)
  385. edid->blocks = hdmi->edid.blocks - edid->start_block;
  386. memcpy(edid->edid, hdmi->edid.edid + edid->start_block * 128,
  387. edid->blocks * 128);
  388. return 0;
  389. }
  390. static inline int adv748x_hdmi_edid_write_block(struct adv748x_hdmi *hdmi,
  391. unsigned int total_len, const u8 *val)
  392. {
  393. struct adv748x_state *state = adv748x_hdmi_to_state(hdmi);
  394. int err = 0;
  395. int i = 0;
  396. int len = 0;
  397. adv_dbg(state, "%s: write EDID block (%d byte)\n",
  398. __func__, total_len);
  399. while (!err && i < total_len) {
  400. len = (total_len - i) > I2C_SMBUS_BLOCK_MAX ?
  401. I2C_SMBUS_BLOCK_MAX :
  402. (total_len - i);
  403. err = adv748x_write_block(state, ADV748X_PAGE_EDID,
  404. i, val + i, len);
  405. i += len;
  406. }
  407. return err;
  408. }
  409. static int adv748x_hdmi_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  410. {
  411. struct adv748x_hdmi *hdmi = adv748x_sd_to_hdmi(sd);
  412. struct adv748x_state *state = adv748x_hdmi_to_state(hdmi);
  413. int err;
  414. memset(edid->reserved, 0, sizeof(edid->reserved));
  415. if (edid->start_block != 0)
  416. return -EINVAL;
  417. if (edid->blocks == 0) {
  418. hdmi->edid.blocks = 0;
  419. hdmi->edid.present = 0;
  420. /* Fall back to a 16:9 aspect ratio */
  421. hdmi->aspect_ratio.numerator = 16;
  422. hdmi->aspect_ratio.denominator = 9;
  423. /* Disable the EDID */
  424. repeater_write(state, ADV748X_REPEATER_EDID_SZ,
  425. edid->blocks << ADV748X_REPEATER_EDID_SZ_SHIFT);
  426. repeater_write(state, ADV748X_REPEATER_EDID_CTL, 0);
  427. return 0;
  428. }
  429. if (edid->blocks > 4) {
  430. edid->blocks = 4;
  431. return -E2BIG;
  432. }
  433. memcpy(hdmi->edid.edid, edid->edid, 128 * edid->blocks);
  434. hdmi->edid.blocks = edid->blocks;
  435. hdmi->edid.present = true;
  436. hdmi->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
  437. edid->edid[0x16]);
  438. err = adv748x_hdmi_edid_write_block(hdmi, 128 * edid->blocks,
  439. hdmi->edid.edid);
  440. if (err < 0) {
  441. v4l2_err(sd, "error %d writing edid pad %d\n", err, edid->pad);
  442. return err;
  443. }
  444. repeater_write(state, ADV748X_REPEATER_EDID_SZ,
  445. edid->blocks << ADV748X_REPEATER_EDID_SZ_SHIFT);
  446. repeater_write(state, ADV748X_REPEATER_EDID_CTL,
  447. ADV748X_REPEATER_EDID_CTL_EN);
  448. return 0;
  449. }
  450. static bool adv748x_hdmi_check_dv_timings(const struct v4l2_dv_timings *timings,
  451. void *hdl)
  452. {
  453. const struct adv748x_hdmi_video_standards *stds =
  454. adv748x_hdmi_video_standards;
  455. unsigned int i;
  456. for (i = 0; stds[i].timings.bt.width; i++)
  457. if (v4l2_match_dv_timings(timings, &stds[i].timings, 0, false))
  458. return true;
  459. return false;
  460. }
  461. static int adv748x_hdmi_enum_dv_timings(struct v4l2_subdev *sd,
  462. struct v4l2_enum_dv_timings *timings)
  463. {
  464. return v4l2_enum_dv_timings_cap(timings, &adv748x_hdmi_timings_cap,
  465. adv748x_hdmi_check_dv_timings, NULL);
  466. }
  467. static int adv748x_hdmi_dv_timings_cap(struct v4l2_subdev *sd,
  468. struct v4l2_dv_timings_cap *cap)
  469. {
  470. *cap = adv748x_hdmi_timings_cap;
  471. return 0;
  472. }
  473. static const struct v4l2_subdev_pad_ops adv748x_pad_ops_hdmi = {
  474. .enum_mbus_code = adv748x_hdmi_enum_mbus_code,
  475. .set_fmt = adv748x_hdmi_set_format,
  476. .get_fmt = adv748x_hdmi_get_format,
  477. .get_edid = adv748x_hdmi_get_edid,
  478. .set_edid = adv748x_hdmi_set_edid,
  479. .dv_timings_cap = adv748x_hdmi_dv_timings_cap,
  480. .enum_dv_timings = adv748x_hdmi_enum_dv_timings,
  481. };
  482. /* -----------------------------------------------------------------------------
  483. * v4l2_subdev_ops
  484. */
  485. static const struct v4l2_subdev_ops adv748x_ops_hdmi = {
  486. .video = &adv748x_video_ops_hdmi,
  487. .pad = &adv748x_pad_ops_hdmi,
  488. };
  489. /* -----------------------------------------------------------------------------
  490. * Controls
  491. */
  492. static const char * const hdmi_ctrl_patgen_menu[] = {
  493. "Disabled",
  494. "Solid Color",
  495. "Color Bars",
  496. "Ramp Grey",
  497. "Ramp Blue",
  498. "Ramp Red",
  499. "Checkered"
  500. };
  501. static int adv748x_hdmi_s_ctrl(struct v4l2_ctrl *ctrl)
  502. {
  503. struct adv748x_hdmi *hdmi = adv748x_ctrl_to_hdmi(ctrl);
  504. struct adv748x_state *state = adv748x_hdmi_to_state(hdmi);
  505. int ret;
  506. u8 pattern;
  507. /* Enable video adjustment first */
  508. ret = cp_clrset(state, ADV748X_CP_VID_ADJ,
  509. ADV748X_CP_VID_ADJ_ENABLE,
  510. ADV748X_CP_VID_ADJ_ENABLE);
  511. if (ret < 0)
  512. return ret;
  513. switch (ctrl->id) {
  514. case V4L2_CID_BRIGHTNESS:
  515. ret = cp_write(state, ADV748X_CP_BRI, ctrl->val);
  516. break;
  517. case V4L2_CID_HUE:
  518. ret = cp_write(state, ADV748X_CP_HUE, ctrl->val);
  519. break;
  520. case V4L2_CID_CONTRAST:
  521. ret = cp_write(state, ADV748X_CP_CON, ctrl->val);
  522. break;
  523. case V4L2_CID_SATURATION:
  524. ret = cp_write(state, ADV748X_CP_SAT, ctrl->val);
  525. break;
  526. case V4L2_CID_TEST_PATTERN:
  527. pattern = ctrl->val;
  528. /* Pattern is 0-indexed. Ctrl Menu is 1-indexed */
  529. if (pattern) {
  530. pattern--;
  531. pattern |= ADV748X_CP_PAT_GEN_EN;
  532. }
  533. ret = cp_write(state, ADV748X_CP_PAT_GEN, pattern);
  534. break;
  535. default:
  536. return -EINVAL;
  537. }
  538. return ret;
  539. }
  540. static const struct v4l2_ctrl_ops adv748x_hdmi_ctrl_ops = {
  541. .s_ctrl = adv748x_hdmi_s_ctrl,
  542. };
  543. static int adv748x_hdmi_init_controls(struct adv748x_hdmi *hdmi)
  544. {
  545. struct adv748x_state *state = adv748x_hdmi_to_state(hdmi);
  546. v4l2_ctrl_handler_init(&hdmi->ctrl_hdl, 5);
  547. /* Use our mutex for the controls */
  548. hdmi->ctrl_hdl.lock = &state->mutex;
  549. v4l2_ctrl_new_std(&hdmi->ctrl_hdl, &adv748x_hdmi_ctrl_ops,
  550. V4L2_CID_BRIGHTNESS, ADV748X_CP_BRI_MIN,
  551. ADV748X_CP_BRI_MAX, 1, ADV748X_CP_BRI_DEF);
  552. v4l2_ctrl_new_std(&hdmi->ctrl_hdl, &adv748x_hdmi_ctrl_ops,
  553. V4L2_CID_CONTRAST, ADV748X_CP_CON_MIN,
  554. ADV748X_CP_CON_MAX, 1, ADV748X_CP_CON_DEF);
  555. v4l2_ctrl_new_std(&hdmi->ctrl_hdl, &adv748x_hdmi_ctrl_ops,
  556. V4L2_CID_SATURATION, ADV748X_CP_SAT_MIN,
  557. ADV748X_CP_SAT_MAX, 1, ADV748X_CP_SAT_DEF);
  558. v4l2_ctrl_new_std(&hdmi->ctrl_hdl, &adv748x_hdmi_ctrl_ops,
  559. V4L2_CID_HUE, ADV748X_CP_HUE_MIN,
  560. ADV748X_CP_HUE_MAX, 1, ADV748X_CP_HUE_DEF);
  561. /*
  562. * Todo: V4L2_CID_DV_RX_POWER_PRESENT should also be supported when
  563. * interrupts are handled correctly
  564. */
  565. v4l2_ctrl_new_std_menu_items(&hdmi->ctrl_hdl, &adv748x_hdmi_ctrl_ops,
  566. V4L2_CID_TEST_PATTERN,
  567. ARRAY_SIZE(hdmi_ctrl_patgen_menu) - 1,
  568. 0, 0, hdmi_ctrl_patgen_menu);
  569. hdmi->sd.ctrl_handler = &hdmi->ctrl_hdl;
  570. if (hdmi->ctrl_hdl.error) {
  571. v4l2_ctrl_handler_free(&hdmi->ctrl_hdl);
  572. return hdmi->ctrl_hdl.error;
  573. }
  574. return v4l2_ctrl_handler_setup(&hdmi->ctrl_hdl);
  575. }
  576. int adv748x_hdmi_init(struct adv748x_hdmi *hdmi)
  577. {
  578. struct adv748x_state *state = adv748x_hdmi_to_state(hdmi);
  579. static const struct v4l2_dv_timings cea1280x720 =
  580. V4L2_DV_BT_CEA_1280X720P30;
  581. int ret;
  582. hdmi->timings = cea1280x720;
  583. /* Initialise a default 16:9 aspect ratio */
  584. hdmi->aspect_ratio.numerator = 16;
  585. hdmi->aspect_ratio.denominator = 9;
  586. adv748x_subdev_init(&hdmi->sd, state, &adv748x_ops_hdmi,
  587. MEDIA_ENT_F_IO_DTV, "hdmi");
  588. hdmi->pads[ADV748X_HDMI_SINK].flags = MEDIA_PAD_FL_SINK;
  589. hdmi->pads[ADV748X_HDMI_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  590. ret = media_entity_pads_init(&hdmi->sd.entity,
  591. ADV748X_HDMI_NR_PADS, hdmi->pads);
  592. if (ret)
  593. return ret;
  594. ret = adv748x_hdmi_init_controls(hdmi);
  595. if (ret)
  596. goto err_free_media;
  597. return 0;
  598. err_free_media:
  599. media_entity_cleanup(&hdmi->sd.entity);
  600. return ret;
  601. }
  602. void adv748x_hdmi_cleanup(struct adv748x_hdmi *hdmi)
  603. {
  604. v4l2_device_unregister_subdev(&hdmi->sd);
  605. media_entity_cleanup(&hdmi->sd.entity);
  606. v4l2_ctrl_handler_free(&hdmi->ctrl_hdl);
  607. }