adv748x-hdmi.c 20 KB

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