adv748x-afe.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for Analog Devices ADV748X 8 channel analog front end (AFE) receiver
  4. * with standard definition processor (SDP)
  5. *
  6. * Copyright (C) 2017 Renesas Electronics Corp.
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/module.h>
  10. #include <linux/mutex.h>
  11. #include <linux/v4l2-dv-timings.h>
  12. #include <media/v4l2-ctrls.h>
  13. #include <media/v4l2-device.h>
  14. #include <media/v4l2-dv-timings.h>
  15. #include <media/v4l2-ioctl.h>
  16. #include "adv748x.h"
  17. /* -----------------------------------------------------------------------------
  18. * SDP
  19. */
  20. #define ADV748X_AFE_STD_AD_PAL_BG_NTSC_J_SECAM 0x0
  21. #define ADV748X_AFE_STD_AD_PAL_BG_NTSC_J_SECAM_PED 0x1
  22. #define ADV748X_AFE_STD_AD_PAL_N_NTSC_J_SECAM 0x2
  23. #define ADV748X_AFE_STD_AD_PAL_N_NTSC_M_SECAM 0x3
  24. #define ADV748X_AFE_STD_NTSC_J 0x4
  25. #define ADV748X_AFE_STD_NTSC_M 0x5
  26. #define ADV748X_AFE_STD_PAL60 0x6
  27. #define ADV748X_AFE_STD_NTSC_443 0x7
  28. #define ADV748X_AFE_STD_PAL_BG 0x8
  29. #define ADV748X_AFE_STD_PAL_N 0x9
  30. #define ADV748X_AFE_STD_PAL_M 0xa
  31. #define ADV748X_AFE_STD_PAL_M_PED 0xb
  32. #define ADV748X_AFE_STD_PAL_COMB_N 0xc
  33. #define ADV748X_AFE_STD_PAL_COMB_N_PED 0xd
  34. #define ADV748X_AFE_STD_PAL_SECAM 0xe
  35. #define ADV748X_AFE_STD_PAL_SECAM_PED 0xf
  36. static int adv748x_afe_read_ro_map(struct adv748x_state *state, u8 reg)
  37. {
  38. int ret;
  39. /* Select SDP Read-Only Main Map */
  40. ret = sdp_write(state, ADV748X_SDP_MAP_SEL,
  41. ADV748X_SDP_MAP_SEL_RO_MAIN);
  42. if (ret < 0)
  43. return ret;
  44. return sdp_read(state, reg);
  45. }
  46. static int adv748x_afe_status(struct adv748x_afe *afe, u32 *signal,
  47. v4l2_std_id *std)
  48. {
  49. struct adv748x_state *state = adv748x_afe_to_state(afe);
  50. int info;
  51. /* Read status from reg 0x10 of SDP RO Map */
  52. info = adv748x_afe_read_ro_map(state, ADV748X_SDP_RO_10);
  53. if (info < 0)
  54. return info;
  55. if (signal)
  56. *signal = info & ADV748X_SDP_RO_10_IN_LOCK ?
  57. 0 : V4L2_IN_ST_NO_SIGNAL;
  58. if (!std)
  59. return 0;
  60. /* Standard not valid if there is no signal */
  61. if (!(info & ADV748X_SDP_RO_10_IN_LOCK)) {
  62. *std = V4L2_STD_UNKNOWN;
  63. return 0;
  64. }
  65. switch (info & 0x70) {
  66. case 0x00:
  67. *std = V4L2_STD_NTSC;
  68. break;
  69. case 0x10:
  70. *std = V4L2_STD_NTSC_443;
  71. break;
  72. case 0x20:
  73. *std = V4L2_STD_PAL_M;
  74. break;
  75. case 0x30:
  76. *std = V4L2_STD_PAL_60;
  77. break;
  78. case 0x40:
  79. *std = V4L2_STD_PAL;
  80. break;
  81. case 0x50:
  82. *std = V4L2_STD_SECAM;
  83. break;
  84. case 0x60:
  85. *std = V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
  86. break;
  87. case 0x70:
  88. *std = V4L2_STD_SECAM;
  89. break;
  90. default:
  91. *std = V4L2_STD_UNKNOWN;
  92. break;
  93. }
  94. return 0;
  95. }
  96. static void adv748x_afe_fill_format(struct adv748x_afe *afe,
  97. struct v4l2_mbus_framefmt *fmt)
  98. {
  99. memset(fmt, 0, sizeof(*fmt));
  100. fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
  101. fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  102. fmt->field = V4L2_FIELD_ALTERNATE;
  103. fmt->width = 720;
  104. fmt->height = afe->curr_norm & V4L2_STD_525_60 ? 480 : 576;
  105. /* Field height */
  106. fmt->height /= 2;
  107. }
  108. static int adv748x_afe_std(v4l2_std_id std)
  109. {
  110. if (std == V4L2_STD_PAL_60)
  111. return ADV748X_AFE_STD_PAL60;
  112. if (std == V4L2_STD_NTSC_443)
  113. return ADV748X_AFE_STD_NTSC_443;
  114. if (std == V4L2_STD_PAL_N)
  115. return ADV748X_AFE_STD_PAL_N;
  116. if (std == V4L2_STD_PAL_M)
  117. return ADV748X_AFE_STD_PAL_M;
  118. if (std == V4L2_STD_PAL_Nc)
  119. return ADV748X_AFE_STD_PAL_COMB_N;
  120. if (std & V4L2_STD_NTSC)
  121. return ADV748X_AFE_STD_NTSC_M;
  122. if (std & V4L2_STD_PAL)
  123. return ADV748X_AFE_STD_PAL_BG;
  124. if (std & V4L2_STD_SECAM)
  125. return ADV748X_AFE_STD_PAL_SECAM;
  126. return -EINVAL;
  127. }
  128. static void adv748x_afe_set_video_standard(struct adv748x_state *state,
  129. int sdpstd)
  130. {
  131. sdp_clrset(state, ADV748X_SDP_VID_SEL, ADV748X_SDP_VID_SEL_MASK,
  132. (sdpstd & 0xf) << ADV748X_SDP_VID_SEL_SHIFT);
  133. }
  134. int adv748x_afe_s_input(struct adv748x_afe *afe, unsigned int input)
  135. {
  136. struct adv748x_state *state = adv748x_afe_to_state(afe);
  137. return sdp_write(state, ADV748X_SDP_INSEL, input);
  138. }
  139. static int adv748x_afe_g_pixelaspect(struct v4l2_subdev *sd,
  140. struct v4l2_fract *aspect)
  141. {
  142. struct adv748x_afe *afe = adv748x_sd_to_afe(sd);
  143. if (afe->curr_norm & V4L2_STD_525_60) {
  144. aspect->numerator = 11;
  145. aspect->denominator = 10;
  146. } else {
  147. aspect->numerator = 54;
  148. aspect->denominator = 59;
  149. }
  150. return 0;
  151. }
  152. /* -----------------------------------------------------------------------------
  153. * v4l2_subdev_video_ops
  154. */
  155. static int adv748x_afe_g_std(struct v4l2_subdev *sd, v4l2_std_id *norm)
  156. {
  157. struct adv748x_afe *afe = adv748x_sd_to_afe(sd);
  158. *norm = afe->curr_norm;
  159. return 0;
  160. }
  161. static int adv748x_afe_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  162. {
  163. struct adv748x_afe *afe = adv748x_sd_to_afe(sd);
  164. struct adv748x_state *state = adv748x_afe_to_state(afe);
  165. int afe_std = adv748x_afe_std(std);
  166. if (afe_std < 0)
  167. return afe_std;
  168. mutex_lock(&state->mutex);
  169. adv748x_afe_set_video_standard(state, afe_std);
  170. afe->curr_norm = std;
  171. mutex_unlock(&state->mutex);
  172. return 0;
  173. }
  174. static int adv748x_afe_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  175. {
  176. struct adv748x_afe *afe = adv748x_sd_to_afe(sd);
  177. struct adv748x_state *state = adv748x_afe_to_state(afe);
  178. int afe_std;
  179. int ret;
  180. mutex_lock(&state->mutex);
  181. if (afe->streaming) {
  182. ret = -EBUSY;
  183. goto unlock;
  184. }
  185. /* Set auto detect mode */
  186. adv748x_afe_set_video_standard(state,
  187. ADV748X_AFE_STD_AD_PAL_BG_NTSC_J_SECAM);
  188. msleep(100);
  189. /* Read detected standard */
  190. ret = adv748x_afe_status(afe, NULL, std);
  191. afe_std = adv748x_afe_std(afe->curr_norm);
  192. if (afe_std < 0)
  193. goto unlock;
  194. /* Restore original state */
  195. adv748x_afe_set_video_standard(state, afe_std);
  196. unlock:
  197. mutex_unlock(&state->mutex);
  198. return ret;
  199. }
  200. static int adv748x_afe_g_tvnorms(struct v4l2_subdev *sd, v4l2_std_id *norm)
  201. {
  202. *norm = V4L2_STD_ALL;
  203. return 0;
  204. }
  205. static int adv748x_afe_g_input_status(struct v4l2_subdev *sd, u32 *status)
  206. {
  207. struct adv748x_afe *afe = adv748x_sd_to_afe(sd);
  208. struct adv748x_state *state = adv748x_afe_to_state(afe);
  209. int ret;
  210. mutex_lock(&state->mutex);
  211. ret = adv748x_afe_status(afe, status, NULL);
  212. mutex_unlock(&state->mutex);
  213. return ret;
  214. }
  215. static int adv748x_afe_s_stream(struct v4l2_subdev *sd, int enable)
  216. {
  217. struct adv748x_afe *afe = adv748x_sd_to_afe(sd);
  218. struct adv748x_state *state = adv748x_afe_to_state(afe);
  219. u32 signal = V4L2_IN_ST_NO_SIGNAL;
  220. int ret;
  221. mutex_lock(&state->mutex);
  222. if (enable) {
  223. ret = adv748x_afe_s_input(afe, afe->input);
  224. if (ret)
  225. goto unlock;
  226. }
  227. ret = adv748x_tx_power(afe->tx, enable);
  228. if (ret)
  229. goto unlock;
  230. afe->streaming = enable;
  231. adv748x_afe_status(afe, &signal, NULL);
  232. if (signal != V4L2_IN_ST_NO_SIGNAL)
  233. adv_dbg(state, "Detected SDP signal\n");
  234. else
  235. adv_dbg(state, "Couldn't detect SDP video signal\n");
  236. unlock:
  237. mutex_unlock(&state->mutex);
  238. return ret;
  239. }
  240. static const struct v4l2_subdev_video_ops adv748x_afe_video_ops = {
  241. .g_std = adv748x_afe_g_std,
  242. .s_std = adv748x_afe_s_std,
  243. .querystd = adv748x_afe_querystd,
  244. .g_tvnorms = adv748x_afe_g_tvnorms,
  245. .g_input_status = adv748x_afe_g_input_status,
  246. .s_stream = adv748x_afe_s_stream,
  247. .g_pixelaspect = adv748x_afe_g_pixelaspect,
  248. };
  249. /* -----------------------------------------------------------------------------
  250. * v4l2_subdev_pad_ops
  251. */
  252. static int adv748x_afe_propagate_pixelrate(struct adv748x_afe *afe)
  253. {
  254. struct v4l2_subdev *tx;
  255. tx = adv748x_get_remote_sd(&afe->pads[ADV748X_AFE_SOURCE]);
  256. if (!tx)
  257. return -ENOLINK;
  258. /*
  259. * The ADV748x ADC sampling frequency is twice the externally supplied
  260. * clock whose frequency is required to be 28.63636 MHz. It oversamples
  261. * with a factor of 4 resulting in a pixel rate of 14.3180180 MHz.
  262. */
  263. return adv748x_csi2_set_pixelrate(tx, 14318180);
  264. }
  265. static int adv748x_afe_enum_mbus_code(struct v4l2_subdev *sd,
  266. struct v4l2_subdev_state *sd_state,
  267. struct v4l2_subdev_mbus_code_enum *code)
  268. {
  269. if (code->index != 0)
  270. return -EINVAL;
  271. code->code = MEDIA_BUS_FMT_UYVY8_2X8;
  272. return 0;
  273. }
  274. static int adv748x_afe_get_format(struct v4l2_subdev *sd,
  275. struct v4l2_subdev_state *sd_state,
  276. struct v4l2_subdev_format *sdformat)
  277. {
  278. struct adv748x_afe *afe = adv748x_sd_to_afe(sd);
  279. struct v4l2_mbus_framefmt *mbusformat;
  280. /* It makes no sense to get the format of the analog sink pads */
  281. if (sdformat->pad != ADV748X_AFE_SOURCE)
  282. return -EINVAL;
  283. if (sdformat->which == V4L2_SUBDEV_FORMAT_TRY) {
  284. mbusformat = v4l2_subdev_get_try_format(sd, sd_state,
  285. sdformat->pad);
  286. sdformat->format = *mbusformat;
  287. } else {
  288. adv748x_afe_fill_format(afe, &sdformat->format);
  289. adv748x_afe_propagate_pixelrate(afe);
  290. }
  291. return 0;
  292. }
  293. static int adv748x_afe_set_format(struct v4l2_subdev *sd,
  294. struct v4l2_subdev_state *sd_state,
  295. struct v4l2_subdev_format *sdformat)
  296. {
  297. struct v4l2_mbus_framefmt *mbusformat;
  298. /* It makes no sense to get the format of the analog sink pads */
  299. if (sdformat->pad != ADV748X_AFE_SOURCE)
  300. return -EINVAL;
  301. if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  302. return adv748x_afe_get_format(sd, sd_state, sdformat);
  303. mbusformat = v4l2_subdev_get_try_format(sd, sd_state, sdformat->pad);
  304. *mbusformat = sdformat->format;
  305. return 0;
  306. }
  307. static const struct v4l2_subdev_pad_ops adv748x_afe_pad_ops = {
  308. .enum_mbus_code = adv748x_afe_enum_mbus_code,
  309. .set_fmt = adv748x_afe_set_format,
  310. .get_fmt = adv748x_afe_get_format,
  311. };
  312. /* -----------------------------------------------------------------------------
  313. * v4l2_subdev_ops
  314. */
  315. static const struct v4l2_subdev_ops adv748x_afe_ops = {
  316. .video = &adv748x_afe_video_ops,
  317. .pad = &adv748x_afe_pad_ops,
  318. };
  319. /* -----------------------------------------------------------------------------
  320. * Controls
  321. */
  322. static const char * const afe_ctrl_frp_menu[] = {
  323. "Disabled",
  324. "Solid Blue",
  325. "Color Bars",
  326. "Grey Ramp",
  327. "Cb Ramp",
  328. "Cr Ramp",
  329. "Boundary"
  330. };
  331. static int adv748x_afe_s_ctrl(struct v4l2_ctrl *ctrl)
  332. {
  333. struct adv748x_afe *afe = adv748x_ctrl_to_afe(ctrl);
  334. struct adv748x_state *state = adv748x_afe_to_state(afe);
  335. bool enable;
  336. int ret;
  337. ret = sdp_write(state, 0x0e, 0x00);
  338. if (ret < 0)
  339. return ret;
  340. switch (ctrl->id) {
  341. case V4L2_CID_BRIGHTNESS:
  342. ret = sdp_write(state, ADV748X_SDP_BRI, ctrl->val);
  343. break;
  344. case V4L2_CID_HUE:
  345. /* Hue is inverted according to HSL chart */
  346. ret = sdp_write(state, ADV748X_SDP_HUE, -ctrl->val);
  347. break;
  348. case V4L2_CID_CONTRAST:
  349. ret = sdp_write(state, ADV748X_SDP_CON, ctrl->val);
  350. break;
  351. case V4L2_CID_SATURATION:
  352. ret = sdp_write(state, ADV748X_SDP_SD_SAT_U, ctrl->val);
  353. if (ret)
  354. break;
  355. ret = sdp_write(state, ADV748X_SDP_SD_SAT_V, ctrl->val);
  356. break;
  357. case V4L2_CID_TEST_PATTERN:
  358. enable = !!ctrl->val;
  359. /* Enable/Disable Color bar test patterns */
  360. ret = sdp_clrset(state, ADV748X_SDP_DEF, ADV748X_SDP_DEF_VAL_EN,
  361. enable);
  362. if (ret)
  363. break;
  364. ret = sdp_clrset(state, ADV748X_SDP_FRP, ADV748X_SDP_FRP_MASK,
  365. enable ? ctrl->val - 1 : 0);
  366. break;
  367. default:
  368. return -EINVAL;
  369. }
  370. return ret;
  371. }
  372. static const struct v4l2_ctrl_ops adv748x_afe_ctrl_ops = {
  373. .s_ctrl = adv748x_afe_s_ctrl,
  374. };
  375. static int adv748x_afe_init_controls(struct adv748x_afe *afe)
  376. {
  377. struct adv748x_state *state = adv748x_afe_to_state(afe);
  378. v4l2_ctrl_handler_init(&afe->ctrl_hdl, 5);
  379. /* Use our mutex for the controls */
  380. afe->ctrl_hdl.lock = &state->mutex;
  381. v4l2_ctrl_new_std(&afe->ctrl_hdl, &adv748x_afe_ctrl_ops,
  382. V4L2_CID_BRIGHTNESS, ADV748X_SDP_BRI_MIN,
  383. ADV748X_SDP_BRI_MAX, 1, ADV748X_SDP_BRI_DEF);
  384. v4l2_ctrl_new_std(&afe->ctrl_hdl, &adv748x_afe_ctrl_ops,
  385. V4L2_CID_CONTRAST, ADV748X_SDP_CON_MIN,
  386. ADV748X_SDP_CON_MAX, 1, ADV748X_SDP_CON_DEF);
  387. v4l2_ctrl_new_std(&afe->ctrl_hdl, &adv748x_afe_ctrl_ops,
  388. V4L2_CID_SATURATION, ADV748X_SDP_SAT_MIN,
  389. ADV748X_SDP_SAT_MAX, 1, ADV748X_SDP_SAT_DEF);
  390. v4l2_ctrl_new_std(&afe->ctrl_hdl, &adv748x_afe_ctrl_ops,
  391. V4L2_CID_HUE, ADV748X_SDP_HUE_MIN,
  392. ADV748X_SDP_HUE_MAX, 1, ADV748X_SDP_HUE_DEF);
  393. v4l2_ctrl_new_std_menu_items(&afe->ctrl_hdl, &adv748x_afe_ctrl_ops,
  394. V4L2_CID_TEST_PATTERN,
  395. ARRAY_SIZE(afe_ctrl_frp_menu) - 1,
  396. 0, 0, afe_ctrl_frp_menu);
  397. afe->sd.ctrl_handler = &afe->ctrl_hdl;
  398. if (afe->ctrl_hdl.error) {
  399. v4l2_ctrl_handler_free(&afe->ctrl_hdl);
  400. return afe->ctrl_hdl.error;
  401. }
  402. return v4l2_ctrl_handler_setup(&afe->ctrl_hdl);
  403. }
  404. int adv748x_afe_init(struct adv748x_afe *afe)
  405. {
  406. struct adv748x_state *state = adv748x_afe_to_state(afe);
  407. int ret;
  408. unsigned int i;
  409. afe->input = 0;
  410. afe->streaming = false;
  411. afe->curr_norm = V4L2_STD_NTSC_M;
  412. adv748x_subdev_init(&afe->sd, state, &adv748x_afe_ops,
  413. MEDIA_ENT_F_ATV_DECODER, "afe");
  414. /* Identify the first connector found as a default input if set */
  415. for (i = ADV748X_PORT_AIN0; i <= ADV748X_PORT_AIN7; i++) {
  416. /* Inputs and ports are 1-indexed to match the data sheet */
  417. if (state->endpoints[i]) {
  418. afe->input = i;
  419. break;
  420. }
  421. }
  422. adv748x_afe_s_input(afe, afe->input);
  423. adv_dbg(state, "AFE Default input set to %d\n", afe->input);
  424. /* Entity pads and sinks are 0-indexed to match the pads */
  425. for (i = ADV748X_AFE_SINK_AIN0; i <= ADV748X_AFE_SINK_AIN7; i++)
  426. afe->pads[i].flags = MEDIA_PAD_FL_SINK;
  427. afe->pads[ADV748X_AFE_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  428. ret = media_entity_pads_init(&afe->sd.entity, ADV748X_AFE_NR_PADS,
  429. afe->pads);
  430. if (ret)
  431. return ret;
  432. ret = adv748x_afe_init_controls(afe);
  433. if (ret)
  434. goto error;
  435. return 0;
  436. error:
  437. media_entity_cleanup(&afe->sd.entity);
  438. return ret;
  439. }
  440. void adv748x_afe_cleanup(struct adv748x_afe *afe)
  441. {
  442. v4l2_device_unregister_subdev(&afe->sd);
  443. media_entity_cleanup(&afe->sd.entity);
  444. v4l2_ctrl_handler_free(&afe->ctrl_hdl);
  445. }