adv748x-csi2.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for Analog Devices ADV748X CSI-2 Transmitter
  4. *
  5. * Copyright (C) 2017 Renesas Electronics Corp.
  6. */
  7. #include <linux/module.h>
  8. #include <media/v4l2-ctrls.h>
  9. #include <media/v4l2-device.h>
  10. #include <media/v4l2-ioctl.h>
  11. #include "adv748x.h"
  12. static const unsigned int adv748x_csi2_txa_fmts[] = {
  13. MEDIA_BUS_FMT_UYVY8_1X16,
  14. MEDIA_BUS_FMT_RGB888_1X24,
  15. };
  16. static const unsigned int adv748x_csi2_txb_fmts[] = {
  17. MEDIA_BUS_FMT_UYVY8_1X16,
  18. };
  19. int adv748x_csi2_set_virtual_channel(struct adv748x_csi2 *tx, unsigned int vc)
  20. {
  21. return tx_write(tx, ADV748X_CSI_VC_REF, vc << ADV748X_CSI_VC_REF_SHIFT);
  22. }
  23. /**
  24. * adv748x_csi2_register_link : Register and link internal entities
  25. *
  26. * @tx: CSI2 private entity
  27. * @v4l2_dev: Video registration device
  28. * @src: Source subdevice to establish link
  29. * @src_pad: Pad number of source to link to this @tx
  30. * @enable: Link enabled flag
  31. *
  32. * Ensure that the subdevice is registered against the v4l2_device, and link the
  33. * source pad to the sink pad of the CSI2 bus entity.
  34. */
  35. static int adv748x_csi2_register_link(struct adv748x_csi2 *tx,
  36. struct v4l2_device *v4l2_dev,
  37. struct v4l2_subdev *src,
  38. unsigned int src_pad,
  39. bool enable)
  40. {
  41. int ret;
  42. if (!src->v4l2_dev) {
  43. ret = v4l2_device_register_subdev(v4l2_dev, src);
  44. if (ret)
  45. return ret;
  46. }
  47. ret = media_create_pad_link(&src->entity, src_pad,
  48. &tx->sd.entity, ADV748X_CSI2_SINK,
  49. enable ? MEDIA_LNK_FL_ENABLED : 0);
  50. if (ret)
  51. return ret;
  52. if (enable)
  53. tx->src = src;
  54. return 0;
  55. }
  56. /* -----------------------------------------------------------------------------
  57. * v4l2_subdev_internal_ops
  58. */
  59. static int adv748x_csi2_init_state(struct v4l2_subdev *sd,
  60. struct v4l2_subdev_state *state)
  61. {
  62. static const struct v4l2_mbus_framefmt adv748x_csi2_default_fmt = {
  63. .width = 1280,
  64. .height = 720,
  65. .code = MEDIA_BUS_FMT_UYVY8_1X16,
  66. .colorspace = V4L2_COLORSPACE_SRGB,
  67. .field = V4L2_FIELD_NONE,
  68. .ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT,
  69. .quantization = V4L2_QUANTIZATION_DEFAULT,
  70. .xfer_func = V4L2_XFER_FUNC_DEFAULT,
  71. };
  72. struct v4l2_mbus_framefmt *fmt;
  73. fmt = v4l2_subdev_state_get_format(state, ADV748X_CSI2_SINK);
  74. *fmt = adv748x_csi2_default_fmt;
  75. fmt = v4l2_subdev_state_get_format(state, ADV748X_CSI2_SOURCE);
  76. *fmt = adv748x_csi2_default_fmt;
  77. return 0;
  78. }
  79. /*
  80. * We use the internal registered operation to be able to ensure that our
  81. * incremental subdevices (not connected in the forward path) can be registered
  82. * against the resulting video path and media device.
  83. */
  84. static int adv748x_csi2_registered(struct v4l2_subdev *sd)
  85. {
  86. struct adv748x_csi2 *tx = adv748x_sd_to_csi2(sd);
  87. struct adv748x_state *state = tx->state;
  88. int ret;
  89. adv_dbg(state, "Registered %s (%s)", is_txa(tx) ? "TXA":"TXB",
  90. sd->name);
  91. /*
  92. * Link TXA to AFE and HDMI, and TXB to AFE only as TXB cannot output
  93. * HDMI.
  94. *
  95. * The HDMI->TXA link is enabled by default, as is the AFE->TXB one.
  96. */
  97. if (is_afe_enabled(state)) {
  98. ret = adv748x_csi2_register_link(tx, sd->v4l2_dev,
  99. &state->afe.sd,
  100. ADV748X_AFE_SOURCE,
  101. is_txb(tx));
  102. if (ret)
  103. return ret;
  104. /* TXB can output AFE signals only. */
  105. if (is_txb(tx))
  106. state->afe.tx = tx;
  107. }
  108. /* Register link to HDMI for TXA only. */
  109. if (is_txb(tx) || !is_hdmi_enabled(state))
  110. return 0;
  111. ret = adv748x_csi2_register_link(tx, sd->v4l2_dev, &state->hdmi.sd,
  112. ADV748X_HDMI_SOURCE, true);
  113. if (ret)
  114. return ret;
  115. /* The default HDMI output is TXA. */
  116. state->hdmi.tx = tx;
  117. return 0;
  118. }
  119. static const struct v4l2_subdev_internal_ops adv748x_csi2_internal_ops = {
  120. .init_state = adv748x_csi2_init_state,
  121. .registered = adv748x_csi2_registered,
  122. };
  123. /* -----------------------------------------------------------------------------
  124. * v4l2_subdev_video_ops
  125. */
  126. static int adv748x_csi2_s_stream(struct v4l2_subdev *sd, int enable)
  127. {
  128. struct adv748x_csi2 *tx = adv748x_sd_to_csi2(sd);
  129. struct v4l2_subdev *src;
  130. src = adv748x_get_remote_sd(&tx->pads[ADV748X_CSI2_SINK]);
  131. if (!src)
  132. return -EPIPE;
  133. return v4l2_subdev_call(src, video, s_stream, enable);
  134. }
  135. static const struct v4l2_subdev_video_ops adv748x_csi2_video_ops = {
  136. .s_stream = adv748x_csi2_s_stream,
  137. };
  138. /* -----------------------------------------------------------------------------
  139. * v4l2_subdev_pad_ops
  140. *
  141. * The CSI2 bus pads are ignorant to the data sizes or formats.
  142. * But we must support setting the pad formats for format propagation.
  143. */
  144. static int adv748x_csi2_enum_mbus_code(struct v4l2_subdev *sd,
  145. struct v4l2_subdev_state *sd_state,
  146. struct v4l2_subdev_mbus_code_enum *code)
  147. {
  148. struct adv748x_csi2 *tx = adv748x_sd_to_csi2(sd);
  149. const unsigned int *codes = is_txa(tx) ?
  150. adv748x_csi2_txa_fmts :
  151. adv748x_csi2_txb_fmts;
  152. size_t num_fmts = is_txa(tx) ? ARRAY_SIZE(adv748x_csi2_txa_fmts)
  153. : ARRAY_SIZE(adv748x_csi2_txb_fmts);
  154. /*
  155. * The format available on the source pad is the one applied on the sink
  156. * pad.
  157. */
  158. if (code->pad == ADV748X_CSI2_SOURCE) {
  159. struct v4l2_mbus_framefmt *fmt;
  160. if (code->index)
  161. return -EINVAL;
  162. fmt = v4l2_subdev_state_get_format(sd_state, ADV748X_CSI2_SINK);
  163. code->code = fmt->code;
  164. return 0;
  165. }
  166. if (code->index >= num_fmts)
  167. return -EINVAL;
  168. code->code = codes[code->index];
  169. return 0;
  170. }
  171. static bool adv748x_csi2_is_fmt_supported(struct adv748x_csi2 *tx, u32 code)
  172. {
  173. const unsigned int *codes = is_txa(tx) ?
  174. adv748x_csi2_txa_fmts :
  175. adv748x_csi2_txb_fmts;
  176. size_t num_fmts = is_txa(tx) ? ARRAY_SIZE(adv748x_csi2_txa_fmts)
  177. : ARRAY_SIZE(adv748x_csi2_txb_fmts);
  178. for (unsigned int i = 0; i < num_fmts; i++) {
  179. if (codes[i] == code)
  180. return true;
  181. }
  182. return false;
  183. }
  184. static int adv748x_csi2_set_format(struct v4l2_subdev *sd,
  185. struct v4l2_subdev_state *sd_state,
  186. struct v4l2_subdev_format *sdformat)
  187. {
  188. struct adv748x_csi2 *tx = adv748x_sd_to_csi2(sd);
  189. struct v4l2_mbus_framefmt *mbusformat;
  190. if (sdformat->pad == ADV748X_CSI2_SOURCE)
  191. return v4l2_subdev_get_fmt(sd, sd_state, sdformat);
  192. /*
  193. * Make sure the format is supported, if not default it to
  194. * UYVY8 as it's supported by both TXes.
  195. */
  196. if (!adv748x_csi2_is_fmt_supported(tx, sdformat->format.code))
  197. sdformat->format.code = MEDIA_BUS_FMT_UYVY8_1X16;
  198. mbusformat = v4l2_subdev_state_get_format(sd_state, sdformat->pad);
  199. *mbusformat = sdformat->format;
  200. /* Propagate format to the source pad. */
  201. mbusformat = v4l2_subdev_state_get_format(sd_state, ADV748X_CSI2_SOURCE);
  202. *mbusformat = sdformat->format;
  203. return 0;
  204. }
  205. static int adv748x_csi2_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
  206. struct v4l2_mbus_config *config)
  207. {
  208. struct adv748x_csi2 *tx = adv748x_sd_to_csi2(sd);
  209. if (pad != ADV748X_CSI2_SOURCE)
  210. return -EINVAL;
  211. config->type = V4L2_MBUS_CSI2_DPHY;
  212. config->bus.mipi_csi2.num_data_lanes = tx->active_lanes;
  213. return 0;
  214. }
  215. static const struct v4l2_subdev_pad_ops adv748x_csi2_pad_ops = {
  216. .enum_mbus_code = adv748x_csi2_enum_mbus_code,
  217. .get_fmt = v4l2_subdev_get_fmt,
  218. .set_fmt = adv748x_csi2_set_format,
  219. .get_mbus_config = adv748x_csi2_get_mbus_config,
  220. };
  221. /* -----------------------------------------------------------------------------
  222. * v4l2_subdev_ops
  223. */
  224. static const struct v4l2_subdev_ops adv748x_csi2_ops = {
  225. .video = &adv748x_csi2_video_ops,
  226. .pad = &adv748x_csi2_pad_ops,
  227. };
  228. /* -----------------------------------------------------------------------------
  229. * Subdev module and controls
  230. */
  231. int adv748x_csi2_set_pixelrate(struct v4l2_subdev *sd, s64 rate)
  232. {
  233. struct adv748x_csi2 *tx = adv748x_sd_to_csi2(sd);
  234. if (!tx->pixel_rate)
  235. return -EINVAL;
  236. return v4l2_ctrl_s_ctrl_int64(tx->pixel_rate, rate);
  237. }
  238. static int adv748x_csi2_s_ctrl(struct v4l2_ctrl *ctrl)
  239. {
  240. switch (ctrl->id) {
  241. case V4L2_CID_PIXEL_RATE:
  242. return 0;
  243. default:
  244. return -EINVAL;
  245. }
  246. }
  247. static const struct v4l2_ctrl_ops adv748x_csi2_ctrl_ops = {
  248. .s_ctrl = adv748x_csi2_s_ctrl,
  249. };
  250. static int adv748x_csi2_init_controls(struct adv748x_csi2 *tx)
  251. {
  252. v4l2_ctrl_handler_init(&tx->ctrl_hdl, 1);
  253. tx->pixel_rate = v4l2_ctrl_new_std(&tx->ctrl_hdl,
  254. &adv748x_csi2_ctrl_ops,
  255. V4L2_CID_PIXEL_RATE, 1, INT_MAX,
  256. 1, 1);
  257. tx->sd.ctrl_handler = &tx->ctrl_hdl;
  258. if (tx->ctrl_hdl.error) {
  259. v4l2_ctrl_handler_free(&tx->ctrl_hdl);
  260. return tx->ctrl_hdl.error;
  261. }
  262. return v4l2_ctrl_handler_setup(&tx->ctrl_hdl);
  263. }
  264. int adv748x_csi2_init(struct adv748x_state *state, struct adv748x_csi2 *tx)
  265. {
  266. int ret;
  267. if (!is_tx_enabled(tx))
  268. return 0;
  269. adv748x_subdev_init(&tx->sd, state, &adv748x_csi2_ops,
  270. MEDIA_ENT_F_VID_IF_BRIDGE,
  271. is_txa(tx) ? "txa" : "txb");
  272. /* Register internal ops for incremental subdev registration */
  273. tx->sd.internal_ops = &adv748x_csi2_internal_ops;
  274. tx->pads[ADV748X_CSI2_SINK].flags = MEDIA_PAD_FL_SINK;
  275. tx->pads[ADV748X_CSI2_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  276. ret = media_entity_pads_init(&tx->sd.entity, ADV748X_CSI2_NR_PADS,
  277. tx->pads);
  278. if (ret)
  279. return ret;
  280. ret = v4l2_async_subdev_endpoint_add(&tx->sd,
  281. of_fwnode_handle(state->endpoints[tx->port]));
  282. if (ret)
  283. goto err_free_media;
  284. ret = adv748x_csi2_init_controls(tx);
  285. if (ret)
  286. goto err_cleanup_subdev;
  287. tx->sd.state_lock = &state->mutex;
  288. ret = v4l2_subdev_init_finalize(&tx->sd);
  289. if (ret)
  290. goto err_free_ctrl;
  291. ret = v4l2_async_register_subdev(&tx->sd);
  292. if (ret)
  293. goto err_free_ctrl;
  294. return 0;
  295. err_free_ctrl:
  296. v4l2_ctrl_handler_free(&tx->ctrl_hdl);
  297. err_cleanup_subdev:
  298. v4l2_subdev_cleanup(&tx->sd);
  299. err_free_media:
  300. media_entity_cleanup(&tx->sd.entity);
  301. return ret;
  302. }
  303. void adv748x_csi2_cleanup(struct adv748x_csi2 *tx)
  304. {
  305. if (!is_tx_enabled(tx))
  306. return;
  307. v4l2_async_unregister_subdev(&tx->sd);
  308. media_entity_cleanup(&tx->sd.entity);
  309. v4l2_ctrl_handler_free(&tx->ctrl_hdl);
  310. v4l2_subdev_cleanup(&tx->sd);
  311. }