adv748x-csi2.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Driver for Analog Devices ADV748X CSI-2 Transmitter
  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-ioctl.h>
  16. #include "adv748x.h"
  17. static bool is_txa(struct adv748x_csi2 *tx)
  18. {
  19. return tx == &tx->state->txa;
  20. }
  21. static int adv748x_csi2_set_virtual_channel(struct adv748x_csi2 *tx,
  22. unsigned int vc)
  23. {
  24. return tx_write(tx, ADV748X_CSI_VC_REF, vc << ADV748X_CSI_VC_REF_SHIFT);
  25. }
  26. /**
  27. * adv748x_csi2_register_link : Register and link internal entities
  28. *
  29. * @tx: CSI2 private entity
  30. * @v4l2_dev: Video registration device
  31. * @src: Source subdevice to establish link
  32. * @src_pad: Pad number of source to link to this @tx
  33. *
  34. * Ensure that the subdevice is registered against the v4l2_device, and link the
  35. * source pad to the sink pad of the CSI2 bus entity.
  36. */
  37. static int adv748x_csi2_register_link(struct adv748x_csi2 *tx,
  38. struct v4l2_device *v4l2_dev,
  39. struct v4l2_subdev *src,
  40. unsigned int src_pad)
  41. {
  42. int enabled = MEDIA_LNK_FL_ENABLED;
  43. int ret;
  44. /*
  45. * Dynamic linking of the AFE is not supported.
  46. * Register the links as immutable.
  47. */
  48. enabled |= MEDIA_LNK_FL_IMMUTABLE;
  49. if (!src->v4l2_dev) {
  50. ret = v4l2_device_register_subdev(v4l2_dev, src);
  51. if (ret)
  52. return ret;
  53. }
  54. return media_create_pad_link(&src->entity, src_pad,
  55. &tx->sd.entity, ADV748X_CSI2_SINK,
  56. enabled);
  57. }
  58. /* -----------------------------------------------------------------------------
  59. * v4l2_subdev_internal_ops
  60. *
  61. * We use the internal registered operation to be able to ensure that our
  62. * incremental subdevices (not connected in the forward path) can be registered
  63. * against the resulting video path and media device.
  64. */
  65. static int adv748x_csi2_registered(struct v4l2_subdev *sd)
  66. {
  67. struct adv748x_csi2 *tx = adv748x_sd_to_csi2(sd);
  68. struct adv748x_state *state = tx->state;
  69. adv_dbg(state, "Registered %s (%s)", is_txa(tx) ? "TXA":"TXB",
  70. sd->name);
  71. /*
  72. * The adv748x hardware allows the AFE to route through the TXA, however
  73. * this is not currently supported in this driver.
  74. *
  75. * Link HDMI->TXA, and AFE->TXB directly.
  76. */
  77. if (is_txa(tx)) {
  78. return adv748x_csi2_register_link(tx, sd->v4l2_dev,
  79. &state->hdmi.sd,
  80. ADV748X_HDMI_SOURCE);
  81. } else {
  82. return adv748x_csi2_register_link(tx, sd->v4l2_dev,
  83. &state->afe.sd,
  84. ADV748X_AFE_SOURCE);
  85. }
  86. }
  87. static const struct v4l2_subdev_internal_ops adv748x_csi2_internal_ops = {
  88. .registered = adv748x_csi2_registered,
  89. };
  90. /* -----------------------------------------------------------------------------
  91. * v4l2_subdev_video_ops
  92. */
  93. static int adv748x_csi2_s_stream(struct v4l2_subdev *sd, int enable)
  94. {
  95. struct adv748x_csi2 *tx = adv748x_sd_to_csi2(sd);
  96. struct v4l2_subdev *src;
  97. src = adv748x_get_remote_sd(&tx->pads[ADV748X_CSI2_SINK]);
  98. if (!src)
  99. return -EPIPE;
  100. return v4l2_subdev_call(src, video, s_stream, enable);
  101. }
  102. static const struct v4l2_subdev_video_ops adv748x_csi2_video_ops = {
  103. .s_stream = adv748x_csi2_s_stream,
  104. };
  105. /* -----------------------------------------------------------------------------
  106. * v4l2_subdev_pad_ops
  107. *
  108. * The CSI2 bus pads are ignorant to the data sizes or formats.
  109. * But we must support setting the pad formats for format propagation.
  110. */
  111. static struct v4l2_mbus_framefmt *
  112. adv748x_csi2_get_pad_format(struct v4l2_subdev *sd,
  113. struct v4l2_subdev_pad_config *cfg,
  114. unsigned int pad, u32 which)
  115. {
  116. struct adv748x_csi2 *tx = adv748x_sd_to_csi2(sd);
  117. if (which == V4L2_SUBDEV_FORMAT_TRY)
  118. return v4l2_subdev_get_try_format(sd, cfg, pad);
  119. return &tx->format;
  120. }
  121. static int adv748x_csi2_get_format(struct v4l2_subdev *sd,
  122. struct v4l2_subdev_pad_config *cfg,
  123. struct v4l2_subdev_format *sdformat)
  124. {
  125. struct adv748x_csi2 *tx = adv748x_sd_to_csi2(sd);
  126. struct adv748x_state *state = tx->state;
  127. struct v4l2_mbus_framefmt *mbusformat;
  128. mbusformat = adv748x_csi2_get_pad_format(sd, cfg, sdformat->pad,
  129. sdformat->which);
  130. if (!mbusformat)
  131. return -EINVAL;
  132. mutex_lock(&state->mutex);
  133. sdformat->format = *mbusformat;
  134. mutex_unlock(&state->mutex);
  135. return 0;
  136. }
  137. static int adv748x_csi2_set_format(struct v4l2_subdev *sd,
  138. struct v4l2_subdev_pad_config *cfg,
  139. struct v4l2_subdev_format *sdformat)
  140. {
  141. struct adv748x_csi2 *tx = adv748x_sd_to_csi2(sd);
  142. struct adv748x_state *state = tx->state;
  143. struct v4l2_mbus_framefmt *mbusformat;
  144. int ret = 0;
  145. mbusformat = adv748x_csi2_get_pad_format(sd, cfg, sdformat->pad,
  146. sdformat->which);
  147. if (!mbusformat)
  148. return -EINVAL;
  149. mutex_lock(&state->mutex);
  150. if (sdformat->pad == ADV748X_CSI2_SOURCE) {
  151. const struct v4l2_mbus_framefmt *sink_fmt;
  152. sink_fmt = adv748x_csi2_get_pad_format(sd, cfg,
  153. ADV748X_CSI2_SINK,
  154. sdformat->which);
  155. if (!sink_fmt) {
  156. ret = -EINVAL;
  157. goto unlock;
  158. }
  159. sdformat->format = *sink_fmt;
  160. }
  161. *mbusformat = sdformat->format;
  162. unlock:
  163. mutex_unlock(&state->mutex);
  164. return ret;
  165. }
  166. static const struct v4l2_subdev_pad_ops adv748x_csi2_pad_ops = {
  167. .get_fmt = adv748x_csi2_get_format,
  168. .set_fmt = adv748x_csi2_set_format,
  169. };
  170. /* -----------------------------------------------------------------------------
  171. * v4l2_subdev_ops
  172. */
  173. static const struct v4l2_subdev_ops adv748x_csi2_ops = {
  174. .video = &adv748x_csi2_video_ops,
  175. .pad = &adv748x_csi2_pad_ops,
  176. };
  177. /* -----------------------------------------------------------------------------
  178. * Subdev module and controls
  179. */
  180. int adv748x_csi2_set_pixelrate(struct v4l2_subdev *sd, s64 rate)
  181. {
  182. struct adv748x_csi2 *tx = adv748x_sd_to_csi2(sd);
  183. if (!tx->pixel_rate)
  184. return -EINVAL;
  185. return v4l2_ctrl_s_ctrl_int64(tx->pixel_rate, rate);
  186. }
  187. static int adv748x_csi2_s_ctrl(struct v4l2_ctrl *ctrl)
  188. {
  189. switch (ctrl->id) {
  190. case V4L2_CID_PIXEL_RATE:
  191. return 0;
  192. default:
  193. return -EINVAL;
  194. }
  195. }
  196. static const struct v4l2_ctrl_ops adv748x_csi2_ctrl_ops = {
  197. .s_ctrl = adv748x_csi2_s_ctrl,
  198. };
  199. static int adv748x_csi2_init_controls(struct adv748x_csi2 *tx)
  200. {
  201. v4l2_ctrl_handler_init(&tx->ctrl_hdl, 1);
  202. tx->pixel_rate = v4l2_ctrl_new_std(&tx->ctrl_hdl,
  203. &adv748x_csi2_ctrl_ops,
  204. V4L2_CID_PIXEL_RATE, 1, INT_MAX,
  205. 1, 1);
  206. tx->sd.ctrl_handler = &tx->ctrl_hdl;
  207. if (tx->ctrl_hdl.error) {
  208. v4l2_ctrl_handler_free(&tx->ctrl_hdl);
  209. return tx->ctrl_hdl.error;
  210. }
  211. return v4l2_ctrl_handler_setup(&tx->ctrl_hdl);
  212. }
  213. int adv748x_csi2_init(struct adv748x_state *state, struct adv748x_csi2 *tx)
  214. {
  215. int ret;
  216. if (!is_tx_enabled(tx))
  217. return 0;
  218. /* Initialise the virtual channel */
  219. adv748x_csi2_set_virtual_channel(tx, 0);
  220. adv748x_subdev_init(&tx->sd, state, &adv748x_csi2_ops,
  221. MEDIA_ENT_F_VID_IF_BRIDGE,
  222. is_txa(tx) ? "txa" : "txb");
  223. /* Ensure that matching is based upon the endpoint fwnodes */
  224. tx->sd.fwnode = of_fwnode_handle(state->endpoints[tx->port]);
  225. /* Register internal ops for incremental subdev registration */
  226. tx->sd.internal_ops = &adv748x_csi2_internal_ops;
  227. tx->pads[ADV748X_CSI2_SINK].flags = MEDIA_PAD_FL_SINK;
  228. tx->pads[ADV748X_CSI2_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  229. ret = media_entity_pads_init(&tx->sd.entity, ADV748X_CSI2_NR_PADS,
  230. tx->pads);
  231. if (ret)
  232. return ret;
  233. ret = adv748x_csi2_init_controls(tx);
  234. if (ret)
  235. goto err_free_media;
  236. ret = v4l2_async_register_subdev(&tx->sd);
  237. if (ret)
  238. goto err_free_ctrl;
  239. return 0;
  240. err_free_ctrl:
  241. v4l2_ctrl_handler_free(&tx->ctrl_hdl);
  242. err_free_media:
  243. media_entity_cleanup(&tx->sd.entity);
  244. return ret;
  245. }
  246. void adv748x_csi2_cleanup(struct adv748x_csi2 *tx)
  247. {
  248. if (!is_tx_enabled(tx))
  249. return;
  250. v4l2_async_unregister_subdev(&tx->sd);
  251. media_entity_cleanup(&tx->sd.entity);
  252. v4l2_ctrl_handler_free(&tx->ctrl_hdl);
  253. }