1 // SPDX-License-Identifier: GPL-2.0
3 * Maxim GMSL2 Deserializer Driver
5 * Copyright (C) 2024 Collabora Ltd.
8 #include <linux/bitfield.h>
9 #include <linux/bitops.h>
10 #include <linux/fwnode.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/i2c-mux.h>
14 #include <linux/module.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
18 #include <media/v4l2-cci.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-fwnode.h>
21 #include <media/v4l2-subdev.h>
23 #define MAX96714_DEVICE_ID 0xc9
24 #define MAX96714F_DEVICE_ID 0xca
25 #define MAX96714_NPORTS 2
26 #define MAX96714_PAD_SINK 0
27 #define MAX96714_PAD_SOURCE 1
30 #define MAX96714_REG13 CCI_REG8(0x0d)
31 #define MAX96714_DEV_REV CCI_REG8(0x0e)
32 #define MAX96714_DEV_REV_MASK GENMASK(3, 0)
33 #define MAX96714_LINK_LOCK CCI_REG8(0x13)
34 #define MAX96714_LINK_LOCK_BIT BIT(3)
35 #define MAX96714_IO_CHK0 CCI_REG8(0x38)
36 #define MAX96714_PATTERN_CLK_FREQ GENMASK(1, 0)
38 #define MAX96714_VIDEO_RX8 CCI_REG8(0x11a)
39 #define MAX96714_VID_LOCK BIT(6)
42 #define MAX96714_PATGEN_0 CCI_REG8(0x240)
43 #define MAX96714_PATGEN_1 CCI_REG8(0x241)
44 #define MAX96714_PATGEN_MODE GENMASK(5, 4)
45 #define MAX96714_PATGEN_VS_DLY CCI_REG24(0x242)
46 #define MAX96714_PATGEN_VS_HIGH CCI_REG24(0x245)
47 #define MAX96714_PATGEN_VS_LOW CCI_REG24(0x248)
48 #define MAX96714_PATGEN_V2H CCI_REG24(0x24b)
49 #define MAX96714_PATGEN_HS_HIGH CCI_REG16(0x24e)
50 #define MAX96714_PATGEN_HS_LOW CCI_REG16(0x250)
51 #define MAX96714_PATGEN_HS_CNT CCI_REG16(0x252)
52 #define MAX96714_PATGEN_V2D CCI_REG24(0x254)
53 #define MAX96714_PATGEN_DE_HIGH CCI_REG16(0x257)
54 #define MAX96714_PATGEN_DE_LOW CCI_REG16(0x259)
55 #define MAX96714_PATGEN_DE_CNT CCI_REG16(0x25B)
56 #define MAX96714_PATGEN_GRAD_INC CCI_REG8(0x25d)
57 #define MAX96714_PATGEN_CHKB_COLOR_A CCI_REG24(0x25E)
58 #define MAX96714_PATGEN_CHKB_COLOR_B CCI_REG24(0x261)
59 #define MAX96714_PATGEN_CHKB_RPT_CNT_A CCI_REG8(0x264)
60 #define MAX96714_PATGEN_CHKB_RPT_CNT_B CCI_REG8(0x265)
61 #define MAX96714_PATGEN_CHKB_ALT CCI_REG8(0x266)
63 #define MAX96714_BACKTOP25 CCI_REG8(0x320)
64 #define CSI_DPLL_FREQ_MASK GENMASK(4, 0)
67 #define MAX96714_MIPI_PHY0 CCI_REG8(0x330)
68 #define MAX96714_FORCE_CSI_OUT BIT(7)
69 #define MAX96714_MIPI_STDBY_N CCI_REG8(0x332)
70 #define MAX96714_MIPI_STDBY_MASK GENMASK(5, 4)
71 #define MAX96714_MIPI_LANE_MAP CCI_REG8(0x333)
72 #define MAX96714_MIPI_POLARITY CCI_REG8(0x335)
73 #define MAX96714_MIPI_POLARITY_MASK GENMASK(5, 0)
76 #define MAX96714_MIPI_LANE_CNT CCI_REG8(0x44a)
77 #define MAX96714_CSI2_LANE_CNT_MASK GENMASK(7, 6)
78 #define MAX96714_MIPI_TX52 CCI_REG8(0x474)
79 #define MAX96714_TUN_EN BIT(0)
81 #define MHZ(v) ((u32)((v) * 1000000U))
83 enum max96714_vpg_mode {
84 MAX96714_VPG_DISABLED = 0,
85 MAX96714_VPG_CHECKERBOARD = 1,
86 MAX96714_VPG_GRADIENT = 2,
89 struct max96714_rxport {
91 struct v4l2_subdev *sd;
93 struct fwnode_handle *ep_fwnode;
95 struct regulator *poc;
98 struct max96714_txport {
99 struct v4l2_fwnode_endpoint vep;
102 struct max96714_priv {
103 struct i2c_client *client;
104 struct regmap *regmap;
105 struct gpio_desc *pd_gpio;
106 struct max96714_rxport rxport;
107 struct i2c_mux_core *mux;
108 u64 enabled_source_streams;
109 struct v4l2_subdev sd;
110 struct media_pad pads[MAX96714_NPORTS];
111 struct v4l2_mbus_config_mipi_csi2 mipi_csi2;
112 struct v4l2_ctrl_handler ctrl_handler;
113 struct v4l2_async_notifier notifier;
115 enum max96714_vpg_mode pattern;
118 static inline struct max96714_priv *sd_to_max96714(struct v4l2_subdev *sd)
120 return container_of(sd, struct max96714_priv, sd);
123 static int max96714_enable_tx_port(struct max96714_priv *priv)
125 return cci_update_bits(priv->regmap, MAX96714_MIPI_STDBY_N,
126 MAX96714_MIPI_STDBY_MASK,
127 MAX96714_MIPI_STDBY_MASK, NULL);
130 static int max96714_disable_tx_port(struct max96714_priv *priv)
132 return cci_update_bits(priv->regmap, MAX96714_MIPI_STDBY_N,
133 MAX96714_MIPI_STDBY_MASK, 0, NULL);
136 static bool max96714_tx_port_enabled(struct max96714_priv *priv)
140 cci_read(priv->regmap, MAX96714_MIPI_STDBY_N, &val, NULL);
142 return val & MAX96714_MIPI_STDBY_MASK;
145 static int max96714_apply_patgen_timing(struct max96714_priv *priv,
146 struct v4l2_subdev_state *state)
148 struct v4l2_mbus_framefmt *fmt =
149 v4l2_subdev_state_get_format(state, MAX96714_PAD_SOURCE);
150 const u32 h_active = fmt->width;
153 const u32 h_bp = 148;
155 const u32 v_active = fmt->height;
162 h_tot = h_active + h_fp + h_sw + h_bp;
163 v_tot = v_active + v_fp + v_sw + v_bp;
165 /* 75 Mhz pixel clock */
166 cci_update_bits(priv->regmap, MAX96714_IO_CHK0,
167 MAX96714_PATTERN_CLK_FREQ, 1, &ret);
169 dev_info(&priv->client->dev, "height: %d width: %d\n", fmt->height,
172 cci_write(priv->regmap, MAX96714_PATGEN_VS_DLY, 0, &ret);
173 cci_write(priv->regmap, MAX96714_PATGEN_VS_HIGH, v_sw * h_tot, &ret);
174 cci_write(priv->regmap, MAX96714_PATGEN_VS_LOW,
175 (v_active + v_fp + v_bp) * h_tot, &ret);
176 cci_write(priv->regmap, MAX96714_PATGEN_HS_HIGH, h_sw, &ret);
177 cci_write(priv->regmap, MAX96714_PATGEN_HS_LOW, h_active + h_fp + h_bp,
179 cci_write(priv->regmap, MAX96714_PATGEN_V2D,
180 h_tot * (v_sw + v_bp) + (h_sw + h_bp), &ret);
181 cci_write(priv->regmap, MAX96714_PATGEN_HS_CNT, v_tot, &ret);
182 cci_write(priv->regmap, MAX96714_PATGEN_DE_HIGH, h_active, &ret);
183 cci_write(priv->regmap, MAX96714_PATGEN_DE_LOW, h_fp + h_sw + h_bp,
185 cci_write(priv->regmap, MAX96714_PATGEN_DE_CNT, v_active, &ret);
187 cci_write(priv->regmap, MAX96714_PATGEN_CHKB_COLOR_A, 0xfecc00, &ret);
189 cci_write(priv->regmap, MAX96714_PATGEN_CHKB_COLOR_B, 0x006aa7, &ret);
190 cci_write(priv->regmap, MAX96714_PATGEN_CHKB_RPT_CNT_A, 0x3c, &ret);
191 cci_write(priv->regmap, MAX96714_PATGEN_CHKB_RPT_CNT_B, 0x3c, &ret);
192 cci_write(priv->regmap, MAX96714_PATGEN_CHKB_ALT, 0x3c, &ret);
193 cci_write(priv->regmap, MAX96714_PATGEN_GRAD_INC, 0x10, &ret);
198 static int max96714_apply_patgen(struct max96714_priv *priv,
199 struct v4l2_subdev_state *state)
205 ret = max96714_apply_patgen_timing(priv, state);
207 cci_write(priv->regmap, MAX96714_PATGEN_0, priv->pattern ? 0xfb : 0,
210 val = FIELD_PREP(MAX96714_PATGEN_MODE, priv->pattern);
211 cci_update_bits(priv->regmap, MAX96714_PATGEN_1, MAX96714_PATGEN_MODE,
216 static int max96714_s_ctrl(struct v4l2_ctrl *ctrl)
218 struct max96714_priv *priv =
219 container_of(ctrl->handler, struct max96714_priv, ctrl_handler);
223 case V4L2_CID_TEST_PATTERN:
224 if (priv->enabled_source_streams)
226 priv->pattern = ctrl->val;
232 ret = cci_update_bits(priv->regmap, MAX96714_MIPI_PHY0,
233 MAX96714_FORCE_CSI_OUT,
234 priv->pattern ? MAX96714_FORCE_CSI_OUT : 0, NULL);
236 /* Pattern generator doesn't work with tunnel mode */
237 return cci_update_bits(priv->regmap, MAX96714_MIPI_TX52,
239 priv->pattern ? 0 : MAX96714_TUN_EN, &ret);
242 static const char * const max96714_test_pattern[] = {
248 static const struct v4l2_ctrl_ops max96714_ctrl_ops = {
249 .s_ctrl = max96714_s_ctrl,
252 static int max96714_enable_streams(struct v4l2_subdev *sd,
253 struct v4l2_subdev_state *state,
254 u32 source_pad, u64 streams_mask)
256 struct max96714_priv *priv = sd_to_max96714(sd);
260 if (!priv->enabled_source_streams)
261 max96714_enable_tx_port(priv);
263 ret = max96714_apply_patgen(priv, state);
267 if (!priv->pattern) {
268 if (!priv->rxport.source.sd) {
274 v4l2_subdev_state_xlate_streams(state,
279 ret = v4l2_subdev_enable_streams(priv->rxport.source.sd,
280 priv->rxport.source.pad,
286 priv->enabled_source_streams |= streams_mask;
291 if (!priv->enabled_source_streams)
292 max96714_disable_tx_port(priv);
297 static int max96714_disable_streams(struct v4l2_subdev *sd,
298 struct v4l2_subdev_state *state,
299 u32 source_pad, u64 streams_mask)
301 struct max96714_priv *priv = sd_to_max96714(sd);
304 if (!priv->pattern) {
308 v4l2_subdev_state_xlate_streams(state,
313 ret = v4l2_subdev_disable_streams(priv->rxport.source.sd,
314 priv->rxport.source.pad,
320 priv->enabled_source_streams &= ~streams_mask;
322 if (!priv->enabled_source_streams)
323 max96714_disable_tx_port(priv);
328 static int max96714_set_fmt(struct v4l2_subdev *sd,
329 struct v4l2_subdev_state *state,
330 struct v4l2_subdev_format *format)
332 struct max96714_priv *priv = sd_to_max96714(sd);
333 struct v4l2_mbus_framefmt *fmt;
335 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
336 priv->enabled_source_streams)
339 /* No transcoding, source and sink formats must match. */
340 if (format->pad == MAX96714_PAD_SOURCE)
341 return v4l2_subdev_get_fmt(sd, state, format);
343 fmt = v4l2_subdev_state_get_format(state, format->pad, format->stream);
347 *fmt = format->format;
349 fmt = v4l2_subdev_state_get_opposite_stream_format(state, format->pad,
354 *fmt = format->format;
359 static int _max96714_set_routing(struct v4l2_subdev *sd,
360 struct v4l2_subdev_state *state,
361 enum v4l2_subdev_format_whence which,
362 struct v4l2_subdev_krouting *routing)
364 static const struct v4l2_mbus_framefmt format = {
367 .code = MEDIA_BUS_FMT_Y8_1X8,
368 .field = V4L2_FIELD_NONE,
373 * Note: we can only support up to V4L2_FRAME_DESC_ENTRY_MAX, until
374 * frame desc is made dynamically allocated.
376 if (routing->num_routes > V4L2_FRAME_DESC_ENTRY_MAX)
379 ret = v4l2_subdev_routing_validate(sd, routing,
380 V4L2_SUBDEV_ROUTING_ONLY_1_TO_1);
384 return v4l2_subdev_set_routing_with_fmt(sd, state, routing, &format);
387 static int max96714_set_routing(struct v4l2_subdev *sd,
388 struct v4l2_subdev_state *state,
389 enum v4l2_subdev_format_whence which,
390 struct v4l2_subdev_krouting *routing)
392 struct max96714_priv *priv = sd_to_max96714(sd);
394 if (which == V4L2_SUBDEV_FORMAT_ACTIVE && priv->enabled_source_streams)
397 return _max96714_set_routing(sd, state, which, routing);
400 static int max96714_init_state(struct v4l2_subdev *sd,
401 struct v4l2_subdev_state *state)
403 struct v4l2_subdev_route routes[] = {
405 .sink_pad = MAX96714_PAD_SINK,
407 .source_pad = MAX96714_PAD_SOURCE,
409 .flags = V4L2_SUBDEV_ROUTE_FL_ACTIVE,
412 struct v4l2_subdev_krouting routing = {
413 .num_routes = ARRAY_SIZE(routes),
417 return _max96714_set_routing(sd, state, V4L2_SUBDEV_FORMAT_ACTIVE,
421 static const struct v4l2_subdev_pad_ops max96714_pad_ops = {
422 .enable_streams = max96714_enable_streams,
423 .disable_streams = max96714_disable_streams,
425 .set_routing = max96714_set_routing,
426 .get_fmt = v4l2_subdev_get_fmt,
427 .set_fmt = max96714_set_fmt,
430 static bool max96714_link_locked(struct max96714_priv *priv)
434 cci_read(priv->regmap, MAX96714_LINK_LOCK, &val, NULL);
436 return val & MAX96714_LINK_LOCK_BIT;
439 static void max96714_link_status(struct max96714_priv *priv)
441 struct device *dev = &priv->client->dev;
443 dev_info(dev, "Link locked:%d\n", max96714_link_locked(priv));
446 static bool max96714_pipe_locked(struct max96714_priv *priv)
450 cci_read(priv->regmap, MAX96714_VIDEO_RX8, &val, NULL);
452 return val & MAX96714_VID_LOCK;
455 static void max96714_pipe_status(struct max96714_priv *priv)
457 struct device *dev = &priv->client->dev;
459 dev_info(dev, "Pipe vidlock:%d\n", max96714_pipe_locked(priv));
462 static void max96714_csi_status(struct max96714_priv *priv)
464 struct device *dev = &priv->client->dev;
467 cci_read(priv->regmap, MAX96714_BACKTOP25, &freq, NULL);
468 freq = FIELD_GET(CSI_DPLL_FREQ_MASK, freq);
470 dev_info(dev, "CSI controller DPLL freq:%u00MHz CSIPHY enabled:%d\n",
471 (u8)freq, max96714_tx_port_enabled(priv));
474 static int max96714_log_status(struct v4l2_subdev *sd)
476 struct max96714_priv *priv = sd_to_max96714(sd);
477 struct device *dev = &priv->client->dev;
479 dev_info(dev, "Deserializer: max96714\n");
481 max96714_link_status(priv);
482 max96714_pipe_status(priv);
483 max96714_csi_status(priv);
488 static const struct v4l2_subdev_core_ops max96714_subdev_core_ops = {
489 .log_status = max96714_log_status,
492 static const struct v4l2_subdev_video_ops max96714_video_ops = {
493 .s_stream = v4l2_subdev_s_stream_helper,
496 static const struct v4l2_subdev_internal_ops max96714_internal_ops = {
497 .init_state = max96714_init_state,
500 static const struct v4l2_subdev_ops max96714_subdev_ops = {
501 .video = &max96714_video_ops,
502 .core = &max96714_subdev_core_ops,
503 .pad = &max96714_pad_ops,
506 static const struct media_entity_operations max96714_entity_ops = {
507 .link_validate = v4l2_subdev_link_validate,
510 static int max96714_notify_bound(struct v4l2_async_notifier *notifier,
511 struct v4l2_subdev *subdev,
512 struct v4l2_async_connection *asd)
514 struct max96714_priv *priv = sd_to_max96714(notifier->sd);
515 struct device *dev = &priv->client->dev;
518 ret = media_entity_get_fwnode_pad(&subdev->entity,
519 priv->rxport.source.ep_fwnode,
520 MEDIA_PAD_FL_SOURCE);
522 dev_err(dev, "Failed to find pad for %s\n", subdev->name);
526 priv->rxport.source.sd = subdev;
527 priv->rxport.source.pad = ret;
529 ret = media_create_pad_link(&priv->rxport.source.sd->entity,
530 priv->rxport.source.pad, &priv->sd.entity,
532 MEDIA_LNK_FL_ENABLED |
533 MEDIA_LNK_FL_IMMUTABLE);
535 dev_err(dev, "Unable to link %s:%u -> %s:%u\n",
536 priv->rxport.source.sd->name, priv->rxport.source.pad,
537 priv->sd.name, MAX96714_PAD_SINK);
544 static const struct v4l2_async_notifier_operations max96714_notify_ops = {
545 .bound = max96714_notify_bound,
548 static int max96714_v4l2_notifier_register(struct max96714_priv *priv)
550 struct device *dev = &priv->client->dev;
551 struct max96714_rxport *rxport = &priv->rxport;
552 struct v4l2_async_connection *asd;
555 if (!rxport->source.ep_fwnode)
558 v4l2_async_subdev_nf_init(&priv->notifier, &priv->sd);
560 asd = v4l2_async_nf_add_fwnode(&priv->notifier,
561 rxport->source.ep_fwnode,
562 struct v4l2_async_connection);
564 dev_err(dev, "Failed to add subdev: %pe", asd);
565 v4l2_async_nf_cleanup(&priv->notifier);
569 priv->notifier.ops = &max96714_notify_ops;
571 ret = v4l2_async_nf_register(&priv->notifier);
573 dev_err(dev, "Failed to register subdev_notifier");
574 v4l2_async_nf_cleanup(&priv->notifier);
581 static int max96714_create_subdev(struct max96714_priv *priv)
583 struct device *dev = &priv->client->dev;
586 v4l2_i2c_subdev_init(&priv->sd, priv->client, &max96714_subdev_ops);
587 priv->sd.internal_ops = &max96714_internal_ops;
589 v4l2_ctrl_handler_init(&priv->ctrl_handler, 1);
590 priv->sd.ctrl_handler = &priv->ctrl_handler;
592 v4l2_ctrl_new_int_menu(&priv->ctrl_handler, NULL, V4L2_CID_LINK_FREQ,
593 0, 0, &priv->tx_link_freq);
594 v4l2_ctrl_new_std_menu_items(&priv->ctrl_handler,
596 V4L2_CID_TEST_PATTERN,
597 ARRAY_SIZE(max96714_test_pattern) - 1,
598 0, 0, max96714_test_pattern);
599 if (priv->ctrl_handler.error) {
600 ret = priv->ctrl_handler.error;
604 priv->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_STREAMS;
605 priv->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
606 priv->sd.entity.ops = &max96714_entity_ops;
608 priv->pads[MAX96714_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
609 priv->pads[MAX96714_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
611 ret = media_entity_pads_init(&priv->sd.entity,
617 priv->sd.state_lock = priv->sd.ctrl_handler->lock;
619 ret = v4l2_subdev_init_finalize(&priv->sd);
621 goto err_entity_cleanup;
623 ret = max96714_v4l2_notifier_register(priv);
625 dev_err(dev, "v4l2 subdev notifier register failed: %d\n", ret);
626 goto err_subdev_cleanup;
629 ret = v4l2_async_register_subdev(&priv->sd);
631 dev_err(dev, "v4l2_async_register_subdev error: %d\n", ret);
632 goto err_unreg_notif;
638 v4l2_async_nf_unregister(&priv->notifier);
639 v4l2_async_nf_cleanup(&priv->notifier);
641 v4l2_subdev_cleanup(&priv->sd);
643 media_entity_cleanup(&priv->sd.entity);
645 v4l2_ctrl_handler_free(&priv->ctrl_handler);
650 static void max96714_destroy_subdev(struct max96714_priv *priv)
652 v4l2_async_nf_unregister(&priv->notifier);
653 v4l2_async_nf_cleanup(&priv->notifier);
654 v4l2_async_unregister_subdev(&priv->sd);
656 v4l2_subdev_cleanup(&priv->sd);
658 media_entity_cleanup(&priv->sd.entity);
659 v4l2_ctrl_handler_free(&priv->ctrl_handler);
662 static int max96714_i2c_mux_select(struct i2c_mux_core *mux, u32 chan)
667 static int max96714_i2c_mux_init(struct max96714_priv *priv)
669 priv->mux = i2c_mux_alloc(priv->client->adapter, &priv->client->dev,
670 1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE,
671 max96714_i2c_mux_select, NULL);
675 return i2c_mux_add_adapter(priv->mux, 0, 0);
678 static int max96714_init_tx_port(struct max96714_priv *priv)
680 struct v4l2_mbus_config_mipi_csi2 *mipi;
681 unsigned long lanes_used = 0;
682 unsigned int val, lane;
685 ret = max96714_disable_tx_port(priv);
687 mipi = &priv->mipi_csi2;
688 val = div_u64(priv->tx_link_freq * 2, MHZ(100));
690 cci_update_bits(priv->regmap, MAX96714_BACKTOP25,
691 CSI_DPLL_FREQ_MASK, val, &ret);
693 val = FIELD_PREP(MAX96714_CSI2_LANE_CNT_MASK, mipi->num_data_lanes - 1);
694 cci_update_bits(priv->regmap, MAX96714_MIPI_LANE_CNT,
695 MAX96714_CSI2_LANE_CNT_MASK, val, &ret);
699 for (lane = 0; lane < mipi->num_data_lanes + 1; lane++) {
700 if (!mipi->lane_polarities[lane])
707 val |= BIT(lane - 1);
713 cci_update_bits(priv->regmap, MAX96714_MIPI_POLARITY,
714 MAX96714_MIPI_POLARITY_MASK, val, &ret);
718 for (lane = 0; lane < mipi->num_data_lanes; lane++) {
719 val |= (mipi->data_lanes[lane] - 1) << (lane * 2);
720 lanes_used |= BIT(mipi->data_lanes[lane] - 1);
724 * Unused lanes need to be mapped as well to not have
725 * the same lanes mapped twice.
727 for (; lane < 4; lane++) {
728 unsigned int idx = find_first_zero_bit(&lanes_used, 4);
730 val |= idx << (lane * 2);
731 lanes_used |= BIT(idx);
734 return cci_write(priv->regmap, MAX96714_MIPI_LANE_MAP, val, &ret);
737 static int max96714_rxport_enable_poc(struct max96714_priv *priv)
739 struct max96714_rxport *rxport = &priv->rxport;
744 return regulator_enable(rxport->poc);
747 static int max96714_rxport_disable_poc(struct max96714_priv *priv)
749 struct max96714_rxport *rxport = &priv->rxport;
754 return regulator_disable(rxport->poc);
757 static int max96714_parse_dt_txport(struct max96714_priv *priv)
759 struct device *dev = &priv->client->dev;
760 struct v4l2_fwnode_endpoint vep = {
761 .bus_type = V4L2_MBUS_CSI2_DPHY
763 struct fwnode_handle *ep_fwnode;
767 ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
768 MAX96714_PAD_SOURCE, 0, 0);
772 ret = v4l2_fwnode_endpoint_alloc_parse(ep_fwnode, &vep);
773 fwnode_handle_put(ep_fwnode);
775 dev_err(dev, "tx: failed to parse endpoint data\n");
779 if (vep.nr_of_link_frequencies != 1) {
784 priv->tx_link_freq = vep.link_frequencies[0];
785 /* Min 50MHz, Max 1250MHz, 50MHz step */
786 if (priv->tx_link_freq < MHZ(50) || priv->tx_link_freq > MHZ(1250) ||
787 (u32)priv->tx_link_freq % MHZ(50)) {
788 dev_err(dev, "tx: invalid link frequency\n");
793 num_data_lanes = vep.bus.mipi_csi2.num_data_lanes;
794 if (num_data_lanes < 1 || num_data_lanes > 4) {
796 "tx: invalid number of data lanes must be 1 to 4\n");
801 memcpy(&priv->mipi_csi2, &vep.bus.mipi_csi2, sizeof(priv->mipi_csi2));
804 v4l2_fwnode_endpoint_free(&vep);
809 static int max96714_parse_dt_rxport(struct max96714_priv *priv)
811 static const char *poc_name = "port0-poc";
812 struct max96714_rxport *rxport = &priv->rxport;
813 struct device *dev = &priv->client->dev;
814 struct fwnode_handle *ep_fwnode;
817 ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
818 MAX96714_PAD_SINK, 0, 0);
822 rxport->source.ep_fwnode = fwnode_graph_get_remote_endpoint(ep_fwnode);
823 fwnode_handle_put(ep_fwnode);
825 if (!rxport->source.ep_fwnode) {
826 dev_err(dev, "rx: no remote endpoint\n");
830 rxport->poc = devm_regulator_get_optional(dev, poc_name);
831 if (IS_ERR(rxport->poc)) {
832 ret = PTR_ERR(rxport->poc);
833 if (ret == -ENODEV) {
836 dev_err(dev, "rx: failed to get POC supply: %d\n", ret);
837 goto err_put_source_ep_fwnode;
843 err_put_source_ep_fwnode:
844 fwnode_handle_put(rxport->source.ep_fwnode);
848 static int max96714_parse_dt(struct max96714_priv *priv)
852 ret = max96714_parse_dt_txport(priv);
856 ret = max96714_parse_dt_rxport(priv);
858 * The deserializer can create a test pattern even if the
859 * rx port is not connected to a serializer.
861 if (ret && ret == -ENOENT)
867 static int max96714_enable_core_hw(struct max96714_priv *priv)
869 struct device *dev = &priv->client->dev;
874 /* wait min 2 ms for reset to complete */
875 gpiod_set_value_cansleep(priv->pd_gpio, 1);
877 gpiod_set_value_cansleep(priv->pd_gpio, 0);
878 /* wait min 2 ms for power up to finish */
882 ret = cci_read(priv->regmap, MAX96714_REG13, &val, NULL);
884 dev_err_probe(dev, ret, "Cannot read first register, abort\n");
888 if (val != MAX96714_DEVICE_ID && val != MAX96714F_DEVICE_ID) {
889 dev_err(dev, "Unsupported device id expected %x got %x\n",
890 MAX96714F_DEVICE_ID, (u8)val);
895 ret = cci_read(priv->regmap, MAX96714_DEV_REV, &val, NULL);
899 dev_dbg(dev, "Found %x (rev %lx)\n", MAX96714F_DEVICE_ID,
900 (u8)val & MAX96714_DEV_REV_MASK);
902 ret = cci_read(priv->regmap, MAX96714_MIPI_TX52, &val, NULL);
906 if (!(val & MAX96714_TUN_EN)) {
907 dev_err(dev, "Only supporting tunnel mode");
915 gpiod_set_value_cansleep(priv->pd_gpio, 1);
919 static void max96714_disable_core_hw(struct max96714_priv *priv)
921 gpiod_set_value_cansleep(priv->pd_gpio, 1);
924 static int max96714_get_hw_resources(struct max96714_priv *priv)
926 struct device *dev = &priv->client->dev;
928 priv->regmap = devm_cci_regmap_init_i2c(priv->client, 16);
929 if (IS_ERR(priv->regmap))
930 return PTR_ERR(priv->regmap);
933 devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
934 if (IS_ERR(priv->pd_gpio))
935 return dev_err_probe(dev, PTR_ERR(priv->pd_gpio),
936 "Cannot get powerdown GPIO\n");
940 static int max96714_probe(struct i2c_client *client)
942 struct device *dev = &client->dev;
943 struct max96714_priv *priv;
946 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
950 priv->client = client;
952 ret = max96714_get_hw_resources(priv);
956 ret = max96714_enable_core_hw(priv);
960 ret = max96714_parse_dt(priv);
962 goto err_disable_core_hw;
964 max96714_init_tx_port(priv);
966 ret = max96714_rxport_enable_poc(priv);
970 ret = max96714_i2c_mux_init(priv);
972 goto err_disable_poc;
974 ret = max96714_create_subdev(priv);
981 i2c_mux_del_adapters(priv->mux);
983 max96714_rxport_disable_poc(priv);
985 fwnode_handle_put(priv->rxport.source.ep_fwnode);
987 max96714_disable_core_hw(priv);
992 static void max96714_remove(struct i2c_client *client)
994 struct v4l2_subdev *sd = i2c_get_clientdata(client);
995 struct max96714_priv *priv = sd_to_max96714(sd);
997 max96714_destroy_subdev(priv);
998 i2c_mux_del_adapters(priv->mux);
999 max96714_rxport_disable_poc(priv);
1000 fwnode_handle_put(priv->rxport.source.ep_fwnode);
1001 max96714_disable_core_hw(priv);
1002 gpiod_set_value_cansleep(priv->pd_gpio, 1);
1005 static const struct of_device_id max96714_of_ids[] = {
1006 { .compatible = "maxim,max96714f" },
1009 MODULE_DEVICE_TABLE(of, max96714_of_ids);
1011 static struct i2c_driver max96714_i2c_driver = {
1014 .of_match_table = max96714_of_ids,
1016 .probe = max96714_probe,
1017 .remove = max96714_remove,
1020 module_i2c_driver(max96714_i2c_driver);
1022 MODULE_LICENSE("GPL");
1023 MODULE_DESCRIPTION("Maxim Integrated GMSL2 Deserializers Driver");