1 // SPDX-License-Identifier: GPL-2.0
3 * Maxim GMSL2 Serializer Driver
5 * Copyright (C) 2024 Collabora Ltd.
8 #include <linux/bitfield.h>
10 #include <linux/clk-provider.h>
11 #include <linux/delay.h>
12 #include <linux/fwnode.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/i2c-mux.h>
15 #include <linux/i2c.h>
16 #include <linux/regmap.h>
18 #include <media/v4l2-cci.h>
19 #include <media/v4l2-fwnode.h>
20 #include <media/v4l2-subdev.h>
22 #define MAX96717_DEVICE_ID 0xbf
23 #define MAX96717F_DEVICE_ID 0xc8
24 #define MAX96717_PORTS 2
25 #define MAX96717_PAD_SINK 0
26 #define MAX96717_PAD_SOURCE 1
28 #define MAX96717_DEFAULT_CLKOUT_RATE 24000000UL
31 #define MAX96717_REG3 CCI_REG8(0x3)
32 #define MAX96717_RCLKSEL GENMASK(1, 0)
33 #define RCLKSEL_REF_PLL CCI_REG8(0x3)
34 #define MAX96717_REG6 CCI_REG8(0x6)
36 #define MAX96717_DEV_ID CCI_REG8(0xd)
37 #define MAX96717_DEV_REV CCI_REG8(0xe)
38 #define MAX96717_DEV_REV_MASK GENMASK(3, 0)
41 #define MAX96717_VIDEO_TX2 CCI_REG8(0x112)
42 #define MAX96717_VIDEO_PCLKDET BIT(7)
45 #define MAX96717_NUM_GPIO 11
46 #define MAX96717_GPIO_REG_A(gpio) CCI_REG8(0x2be + (gpio) * 3)
47 #define MAX96717_GPIO_OUT BIT(4)
48 #define MAX96717_GPIO_IN BIT(3)
49 #define MAX96717_GPIO_RX_EN BIT(2)
50 #define MAX96717_GPIO_TX_EN BIT(1)
51 #define MAX96717_GPIO_OUT_DIS BIT(0)
54 /* MAX96717 only have CSI port 'B' */
55 #define MAX96717_FRONTOP0 CCI_REG8(0x308)
56 #define MAX96717_START_PORT_B BIT(5)
59 #define MAX96717_MIPI_RX1 CCI_REG8(0x331)
60 #define MAX96717_MIPI_LANES_CNT GENMASK(5, 4)
61 #define MAX96717_MIPI_RX2 CCI_REG8(0x332) /* phy1 Lanes map */
62 #define MAX96717_PHY2_LANES_MAP GENMASK(7, 4)
63 #define MAX96717_MIPI_RX3 CCI_REG8(0x333) /* phy2 Lanes map */
64 #define MAX96717_PHY1_LANES_MAP GENMASK(3, 0)
65 #define MAX96717_MIPI_RX4 CCI_REG8(0x334) /* phy1 lane polarities */
66 #define MAX96717_PHY1_LANES_POL GENMASK(6, 4)
67 #define MAX96717_MIPI_RX5 CCI_REG8(0x335) /* phy2 lane polarities */
68 #define MAX96717_PHY2_LANES_POL GENMASK(2, 0)
71 #define MAX96717_MIPI_RX_EXT11 CCI_REG8(0x383)
72 #define MAX96717_TUN_MODE BIT(7)
75 #define REF_VTG0 CCI_REG8(0x3f0)
76 #define REFGEN_PREDEF_EN BIT(6)
77 #define REFGEN_PREDEF_FREQ_MASK GENMASK(5, 4)
78 #define REFGEN_PREDEF_FREQ_ALT BIT(3)
79 #define REFGEN_RST BIT(1)
80 #define REFGEN_EN BIT(0)
83 #define PIO_SLEW_1 CCI_REG8(0x570)
85 struct max96717_priv {
86 struct i2c_client *client;
87 struct regmap *regmap;
88 struct i2c_mux_core *mux;
89 struct v4l2_mbus_config_mipi_csi2 mipi_csi2;
90 struct v4l2_subdev sd;
91 struct media_pad pads[MAX96717_PORTS];
92 struct v4l2_async_notifier notifier;
93 struct v4l2_subdev *source_sd;
95 u64 enabled_source_streams;
98 struct gpio_chip gpio_chip;
101 static inline struct max96717_priv *sd_to_max96717(struct v4l2_subdev *sd)
103 return container_of(sd, struct max96717_priv, sd);
106 static inline struct max96717_priv *clk_hw_to_max96717(struct clk_hw *hw)
108 return container_of(hw, struct max96717_priv, clk_hw);
111 static int max96717_i2c_mux_select(struct i2c_mux_core *mux, u32 chan)
116 static int max96717_i2c_mux_init(struct max96717_priv *priv)
118 priv->mux = i2c_mux_alloc(priv->client->adapter, &priv->client->dev,
119 1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE,
120 max96717_i2c_mux_select, NULL);
124 return i2c_mux_add_adapter(priv->mux, 0, 0);
127 static inline int max96717_start_csi(struct max96717_priv *priv, bool start)
129 return cci_update_bits(priv->regmap, MAX96717_FRONTOP0,
130 MAX96717_START_PORT_B,
131 start ? MAX96717_START_PORT_B : 0, NULL);
134 static int max96717_gpiochip_get(struct gpio_chip *gpiochip,
137 struct max96717_priv *priv = gpiochip_get_data(gpiochip);
141 ret = cci_read(priv->regmap, MAX96717_GPIO_REG_A(offset),
146 if (val & MAX96717_GPIO_OUT_DIS)
147 return !!(val & MAX96717_GPIO_IN);
149 return !!(val & MAX96717_GPIO_OUT);
152 static void max96717_gpiochip_set(struct gpio_chip *gpiochip,
153 unsigned int offset, int value)
155 struct max96717_priv *priv = gpiochip_get_data(gpiochip);
157 cci_update_bits(priv->regmap, MAX96717_GPIO_REG_A(offset),
158 MAX96717_GPIO_OUT, MAX96717_GPIO_OUT, NULL);
161 static int max96717_gpio_get_direction(struct gpio_chip *gpiochip,
164 struct max96717_priv *priv = gpiochip_get_data(gpiochip);
168 ret = cci_read(priv->regmap, MAX96717_GPIO_REG_A(offset), &val, NULL);
172 return !!(val & MAX96717_GPIO_OUT_DIS);
175 static int max96717_gpio_direction_out(struct gpio_chip *gpiochip,
176 unsigned int offset, int value)
178 struct max96717_priv *priv = gpiochip_get_data(gpiochip);
180 return cci_update_bits(priv->regmap, MAX96717_GPIO_REG_A(offset),
181 MAX96717_GPIO_OUT_DIS | MAX96717_GPIO_OUT,
182 value ? MAX96717_GPIO_OUT : 0, NULL);
185 static int max96717_gpio_direction_in(struct gpio_chip *gpiochip,
188 struct max96717_priv *priv = gpiochip_get_data(gpiochip);
190 return cci_update_bits(priv->regmap, MAX96717_GPIO_REG_A(offset),
191 MAX96717_GPIO_OUT_DIS, MAX96717_GPIO_OUT_DIS,
195 static int max96717_gpiochip_probe(struct max96717_priv *priv)
197 struct device *dev = &priv->client->dev;
198 struct gpio_chip *gc = &priv->gpio_chip;
201 gc->label = dev_name(dev);
203 gc->owner = THIS_MODULE;
204 gc->ngpio = MAX96717_NUM_GPIO;
206 gc->can_sleep = true;
207 gc->get_direction = max96717_gpio_get_direction;
208 gc->direction_input = max96717_gpio_direction_in;
209 gc->direction_output = max96717_gpio_direction_out;
210 gc->set = max96717_gpiochip_set;
211 gc->get = max96717_gpiochip_get;
212 gc->of_gpio_n_cells = 2;
214 /* Disable GPIO forwarding */
215 for (i = 0; i < gc->ngpio; i++)
216 cci_update_bits(priv->regmap, MAX96717_GPIO_REG_A(i),
217 MAX96717_GPIO_RX_EN | MAX96717_GPIO_TX_EN,
223 ret = devm_gpiochip_add_data(dev, gc, priv);
225 dev_err(dev, "Unable to create gpio_chip\n");
232 static int _max96717_set_routing(struct v4l2_subdev *sd,
233 struct v4l2_subdev_state *state,
234 struct v4l2_subdev_krouting *routing)
236 static const struct v4l2_mbus_framefmt format = {
239 .code = MEDIA_BUS_FMT_Y8_1X8,
240 .field = V4L2_FIELD_NONE,
244 ret = v4l2_subdev_routing_validate(sd, routing,
245 V4L2_SUBDEV_ROUTING_ONLY_1_TO_1);
249 ret = v4l2_subdev_set_routing_with_fmt(sd, state, routing, &format);
256 static int max96717_set_routing(struct v4l2_subdev *sd,
257 struct v4l2_subdev_state *state,
258 enum v4l2_subdev_format_whence which,
259 struct v4l2_subdev_krouting *routing)
261 struct max96717_priv *priv = sd_to_max96717(sd);
263 if (which == V4L2_SUBDEV_FORMAT_ACTIVE && priv->enabled_source_streams)
266 return _max96717_set_routing(sd, state, routing);
269 static int max96717_set_fmt(struct v4l2_subdev *sd,
270 struct v4l2_subdev_state *state,
271 struct v4l2_subdev_format *format)
273 struct max96717_priv *priv = sd_to_max96717(sd);
274 struct v4l2_mbus_framefmt *fmt;
275 u64 stream_source_mask;
277 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
278 priv->enabled_source_streams)
281 /* No transcoding, source and sink formats must match. */
282 if (format->pad == MAX96717_PAD_SOURCE)
283 return v4l2_subdev_get_fmt(sd, state, format);
285 /* Set sink format */
286 fmt = v4l2_subdev_state_get_format(state, format->pad, format->stream);
290 *fmt = format->format;
292 /* Propagate to source format */
293 fmt = v4l2_subdev_state_get_opposite_stream_format(state, format->pad,
297 *fmt = format->format;
299 stream_source_mask = BIT(format->stream);
301 return v4l2_subdev_state_xlate_streams(state, MAX96717_PAD_SOURCE,
303 &stream_source_mask);
306 static int max96717_init_state(struct v4l2_subdev *sd,
307 struct v4l2_subdev_state *state)
309 struct v4l2_subdev_route routes[] = {
311 .sink_pad = MAX96717_PAD_SINK,
313 .source_pad = MAX96717_PAD_SOURCE,
315 .flags = V4L2_SUBDEV_ROUTE_FL_ACTIVE,
318 struct v4l2_subdev_krouting routing = {
319 .num_routes = ARRAY_SIZE(routes),
323 return _max96717_set_routing(sd, state, &routing);
326 static bool max96717_pipe_pclkdet(struct max96717_priv *priv)
330 cci_read(priv->regmap, MAX96717_VIDEO_TX2, &val, NULL);
332 return val & MAX96717_VIDEO_PCLKDET;
335 static int max96717_log_status(struct v4l2_subdev *sd)
337 struct max96717_priv *priv = sd_to_max96717(sd);
338 struct device *dev = &priv->client->dev;
340 dev_info(dev, "Serializer: max96717\n");
341 dev_info(dev, "Pipe: pclkdet:%d\n", max96717_pipe_pclkdet(priv));
346 static int max96717_enable_streams(struct v4l2_subdev *sd,
347 struct v4l2_subdev_state *state, u32 pad,
350 struct max96717_priv *priv = sd_to_max96717(sd);
351 struct device *dev = &priv->client->dev;
355 sink_streams = v4l2_subdev_state_xlate_streams(state,
360 if (!priv->enabled_source_streams)
361 max96717_start_csi(priv, true);
363 ret = v4l2_subdev_enable_streams(priv->source_sd, priv->source_sd_pad,
366 dev_err(dev, "Fail to start streams:%llu on remote subdev\n",
371 priv->enabled_source_streams |= streams_mask;
376 if (!priv->enabled_source_streams)
377 max96717_start_csi(priv, false);
381 static int max96717_disable_streams(struct v4l2_subdev *sd,
382 struct v4l2_subdev_state *state, u32 pad,
385 struct max96717_priv *priv = sd_to_max96717(sd);
389 * Stop the CSI receiver first then the source,
390 * otherwise the device may become unresponsive
391 * while holding the I2C bus low.
393 priv->enabled_source_streams &= ~streams_mask;
394 if (!priv->enabled_source_streams)
395 max96717_start_csi(priv, false);
397 sink_streams = v4l2_subdev_state_xlate_streams(state,
402 return v4l2_subdev_disable_streams(priv->source_sd, priv->source_sd_pad,
406 static const struct v4l2_subdev_pad_ops max96717_pad_ops = {
407 .enable_streams = max96717_enable_streams,
408 .disable_streams = max96717_disable_streams,
409 .set_routing = max96717_set_routing,
410 .get_fmt = v4l2_subdev_get_fmt,
411 .set_fmt = max96717_set_fmt,
414 static const struct v4l2_subdev_core_ops max96717_subdev_core_ops = {
415 .log_status = max96717_log_status,
418 static const struct v4l2_subdev_internal_ops max96717_internal_ops = {
419 .init_state = max96717_init_state,
422 static const struct v4l2_subdev_ops max96717_subdev_ops = {
423 .core = &max96717_subdev_core_ops,
424 .pad = &max96717_pad_ops,
427 static const struct media_entity_operations max96717_entity_ops = {
428 .link_validate = v4l2_subdev_link_validate,
431 static int max96717_notify_bound(struct v4l2_async_notifier *notifier,
432 struct v4l2_subdev *source_subdev,
433 struct v4l2_async_connection *asd)
435 struct max96717_priv *priv = sd_to_max96717(notifier->sd);
436 struct device *dev = &priv->client->dev;
439 ret = media_entity_get_fwnode_pad(&source_subdev->entity,
440 source_subdev->fwnode,
441 MEDIA_PAD_FL_SOURCE);
443 dev_err(dev, "Failed to find pad for %s\n",
444 source_subdev->name);
448 priv->source_sd = source_subdev;
449 priv->source_sd_pad = ret;
451 ret = media_create_pad_link(&source_subdev->entity, priv->source_sd_pad,
453 MEDIA_LNK_FL_ENABLED |
454 MEDIA_LNK_FL_IMMUTABLE);
456 dev_err(dev, "Unable to link %s:%u -> %s:0\n",
457 source_subdev->name, priv->source_sd_pad,
465 static const struct v4l2_async_notifier_operations max96717_notify_ops = {
466 .bound = max96717_notify_bound,
469 static int max96717_v4l2_notifier_register(struct max96717_priv *priv)
471 struct device *dev = &priv->client->dev;
472 struct v4l2_async_connection *asd;
473 struct fwnode_handle *ep_fwnode;
476 ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
477 MAX96717_PAD_SINK, 0, 0);
479 dev_err(dev, "No graph endpoint\n");
483 v4l2_async_subdev_nf_init(&priv->notifier, &priv->sd);
485 asd = v4l2_async_nf_add_fwnode_remote(&priv->notifier, ep_fwnode,
486 struct v4l2_async_connection);
488 fwnode_handle_put(ep_fwnode);
491 dev_err(dev, "Failed to add subdev: %ld", PTR_ERR(asd));
492 v4l2_async_nf_cleanup(&priv->notifier);
496 priv->notifier.ops = &max96717_notify_ops;
498 ret = v4l2_async_nf_register(&priv->notifier);
500 dev_err(dev, "Failed to register subdev_notifier");
501 v4l2_async_nf_cleanup(&priv->notifier);
508 static int max96717_subdev_init(struct max96717_priv *priv)
510 struct device *dev = &priv->client->dev;
513 v4l2_i2c_subdev_init(&priv->sd, priv->client, &max96717_subdev_ops);
514 priv->sd.internal_ops = &max96717_internal_ops;
516 priv->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_STREAMS;
517 priv->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
518 priv->sd.entity.ops = &max96717_entity_ops;
520 priv->pads[MAX96717_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
521 priv->pads[MAX96717_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
523 ret = media_entity_pads_init(&priv->sd.entity, 2, priv->pads);
525 return dev_err_probe(dev, ret, "Failed to init pads\n");
527 ret = v4l2_subdev_init_finalize(&priv->sd);
529 dev_err_probe(dev, ret,
530 "v4l2 subdev init finalized failed\n");
531 goto err_entity_cleanup;
533 ret = max96717_v4l2_notifier_register(priv);
535 dev_err_probe(dev, ret,
536 "v4l2 subdev notifier register failed\n");
540 ret = v4l2_async_register_subdev(&priv->sd);
542 dev_err_probe(dev, ret, "v4l2_async_register_subdev error\n");
543 goto err_unreg_notif;
549 v4l2_async_nf_unregister(&priv->notifier);
550 v4l2_async_nf_cleanup(&priv->notifier);
552 v4l2_subdev_cleanup(&priv->sd);
554 media_entity_cleanup(&priv->sd.entity);
559 static void max96717_subdev_uninit(struct max96717_priv *priv)
561 v4l2_async_unregister_subdev(&priv->sd);
562 v4l2_async_nf_unregister(&priv->notifier);
563 v4l2_async_nf_cleanup(&priv->notifier);
564 v4l2_subdev_cleanup(&priv->sd);
565 media_entity_cleanup(&priv->sd.entity);
568 struct max96717_pll_predef_freq {
574 static const struct max96717_pll_predef_freq max96717_predef_freqs[] = {
575 { 13500000, true, 0 }, { 19200000, false, 0 },
576 { 24000000, true, 1 }, { 27000000, false, 1 },
577 { 37125000, false, 2 }, { 74250000, false, 3 },
581 max96717_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
583 struct max96717_priv *priv = clk_hw_to_max96717(hw);
585 return max96717_predef_freqs[priv->pll_predef_index].freq;
588 static unsigned int max96717_clk_find_best_index(struct max96717_priv *priv,
592 unsigned long diff_new, diff_old;
597 for (i = 0; i < ARRAY_SIZE(max96717_predef_freqs); i++) {
598 diff_new = abs(rate - max96717_predef_freqs[i].freq);
599 if (diff_new < diff_old) {
608 static long max96717_clk_round_rate(struct clk_hw *hw, unsigned long rate,
609 unsigned long *parent_rate)
611 struct max96717_priv *priv = clk_hw_to_max96717(hw);
612 struct device *dev = &priv->client->dev;
615 idx = max96717_clk_find_best_index(priv, rate);
617 if (rate != max96717_predef_freqs[idx].freq) {
618 dev_warn(dev, "Request CLK freq:%lu, found CLK freq:%lu\n",
619 rate, max96717_predef_freqs[idx].freq);
622 return max96717_predef_freqs[idx].freq;
625 static int max96717_clk_set_rate(struct clk_hw *hw, unsigned long rate,
626 unsigned long parent_rate)
628 struct max96717_priv *priv = clk_hw_to_max96717(hw);
629 unsigned int val, idx;
632 idx = max96717_clk_find_best_index(priv, rate);
634 val = FIELD_PREP(REFGEN_PREDEF_FREQ_MASK,
635 max96717_predef_freqs[idx].val);
637 if (max96717_predef_freqs[idx].is_alt)
638 val |= REFGEN_PREDEF_FREQ_ALT;
640 val |= REFGEN_RST | REFGEN_PREDEF_EN;
642 cci_write(priv->regmap, REF_VTG0, val, &ret);
643 cci_update_bits(priv->regmap, REF_VTG0, REFGEN_RST | REFGEN_EN,
648 priv->pll_predef_index = idx;
653 static int max96717_clk_prepare(struct clk_hw *hw)
655 struct max96717_priv *priv = clk_hw_to_max96717(hw);
657 return cci_update_bits(priv->regmap, MAX96717_REG6, RCLKEN,
661 static void max96717_clk_unprepare(struct clk_hw *hw)
663 struct max96717_priv *priv = clk_hw_to_max96717(hw);
665 cci_update_bits(priv->regmap, MAX96717_REG6, RCLKEN, 0, NULL);
668 static const struct clk_ops max96717_clk_ops = {
669 .prepare = max96717_clk_prepare,
670 .unprepare = max96717_clk_unprepare,
671 .set_rate = max96717_clk_set_rate,
672 .recalc_rate = max96717_clk_recalc_rate,
673 .round_rate = max96717_clk_round_rate,
676 static int max96717_register_clkout(struct max96717_priv *priv)
678 struct device *dev = &priv->client->dev;
679 struct clk_init_data init = { .ops = &max96717_clk_ops };
682 init.name = kasprintf(GFP_KERNEL, "max96717.%s.clk_out",
687 /* RCLKSEL Reference PLL output */
688 ret = cci_update_bits(priv->regmap, MAX96717_REG3, MAX96717_RCLKSEL,
689 MAX96717_RCLKSEL, NULL);
690 /* MFP4 fastest slew rate */
691 cci_update_bits(priv->regmap, PIO_SLEW_1, BIT(5) | BIT(4), 0, &ret);
695 priv->clk_hw.init = &init;
697 /* Initialize to 24 MHz */
698 ret = max96717_clk_set_rate(&priv->clk_hw,
699 MAX96717_DEFAULT_CLKOUT_RATE, 0);
703 ret = devm_clk_hw_register(dev, &priv->clk_hw);
706 return dev_err_probe(dev, ret, "Cannot register clock HW\n");
708 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
711 return dev_err_probe(dev, ret,
712 "Cannot add OF clock provider\n");
721 static int max96717_init_csi_lanes(struct max96717_priv *priv)
723 struct v4l2_mbus_config_mipi_csi2 *mipi = &priv->mipi_csi2;
724 unsigned long lanes_used = 0;
725 unsigned int nlanes, lane, val = 0;
728 nlanes = mipi->num_data_lanes;
730 ret = cci_update_bits(priv->regmap, MAX96717_MIPI_RX1,
731 MAX96717_MIPI_LANES_CNT,
732 FIELD_PREP(MAX96717_MIPI_LANES_CNT,
736 for (lane = 0; lane < nlanes + 1; lane++) {
737 if (!mipi->lane_polarities[lane])
743 val |= BIT(lane - 1);
748 cci_update_bits(priv->regmap, MAX96717_MIPI_RX5,
749 MAX96717_PHY2_LANES_POL,
750 FIELD_PREP(MAX96717_PHY2_LANES_POL, val), &ret);
752 cci_update_bits(priv->regmap, MAX96717_MIPI_RX4,
753 MAX96717_PHY1_LANES_POL,
754 FIELD_PREP(MAX96717_PHY1_LANES_POL,
757 for (lane = 0, val = 0; lane < nlanes; lane++) {
758 val |= (mipi->data_lanes[lane] - 1) << (lane * 2);
759 lanes_used |= BIT(mipi->data_lanes[lane] - 1);
763 * Unused lanes need to be mapped as well to not have
764 * the same lanes mapped twice.
766 for (; lane < 4; lane++) {
767 unsigned int idx = find_first_zero_bit(&lanes_used, 4);
769 val |= idx << (lane * 2);
770 lanes_used |= BIT(idx);
773 cci_update_bits(priv->regmap, MAX96717_MIPI_RX3,
774 MAX96717_PHY1_LANES_MAP,
775 FIELD_PREP(MAX96717_PHY1_LANES_MAP, val), &ret);
777 return cci_update_bits(priv->regmap, MAX96717_MIPI_RX2,
778 MAX96717_PHY2_LANES_MAP,
779 FIELD_PREP(MAX96717_PHY2_LANES_MAP, val >> 4),
783 static int max96717_hw_init(struct max96717_priv *priv)
785 struct device *dev = &priv->client->dev;
789 ret = cci_read(priv->regmap, MAX96717_DEV_ID, &dev_id, NULL);
791 return dev_err_probe(dev, ret,
792 "Fail to read the device id\n");
794 if (dev_id != MAX96717_DEVICE_ID && dev_id != MAX96717F_DEVICE_ID)
795 return dev_err_probe(dev, -EOPNOTSUPP,
796 "Unsupported device id got %x\n", (u8)dev_id);
798 ret = cci_read(priv->regmap, MAX96717_DEV_REV, &val, NULL);
800 return dev_err_probe(dev, ret,
801 "Fail to read device revision");
803 dev_dbg(dev, "Found %x (rev %lx)\n", (u8)dev_id,
804 (u8)val & MAX96717_DEV_REV_MASK);
806 ret = cci_read(priv->regmap, MAX96717_MIPI_RX_EXT11, &val, NULL);
808 return dev_err_probe(dev, ret,
809 "Fail to read mipi rx extension");
811 if (!(val & MAX96717_TUN_MODE))
812 return dev_err_probe(dev, -EOPNOTSUPP,
813 "Only supporting tunnel mode");
815 return max96717_init_csi_lanes(priv);
818 static int max96717_parse_dt(struct max96717_priv *priv)
820 struct device *dev = &priv->client->dev;
821 struct v4l2_fwnode_endpoint vep = {
822 .bus_type = V4L2_MBUS_CSI2_DPHY
824 struct fwnode_handle *ep_fwnode;
825 unsigned char num_data_lanes;
828 ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
829 MAX96717_PAD_SINK, 0, 0);
831 return dev_err_probe(dev, -ENOENT, "no endpoint found\n");
833 ret = v4l2_fwnode_endpoint_parse(ep_fwnode, &vep);
835 fwnode_handle_put(ep_fwnode);
838 return dev_err_probe(dev, ret, "Failed to parse sink endpoint");
840 num_data_lanes = vep.bus.mipi_csi2.num_data_lanes;
841 if (num_data_lanes < 1 || num_data_lanes > 4)
842 return dev_err_probe(dev, -EINVAL,
843 "Invalid data lanes must be 1 to 4\n");
845 memcpy(&priv->mipi_csi2, &vep.bus.mipi_csi2, sizeof(priv->mipi_csi2));
850 static int max96717_probe(struct i2c_client *client)
852 struct device *dev = &client->dev;
853 struct max96717_priv *priv;
856 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
860 priv->client = client;
861 priv->regmap = devm_cci_regmap_init_i2c(client, 16);
862 if (IS_ERR(priv->regmap)) {
863 ret = PTR_ERR(priv->regmap);
864 return dev_err_probe(dev, ret, "Failed to init regmap\n");
867 ret = max96717_parse_dt(priv);
869 return dev_err_probe(dev, ret, "Failed to parse the dt\n");
871 ret = max96717_hw_init(priv);
873 return dev_err_probe(dev, ret,
874 "Failed to initialize the hardware\n");
876 ret = max96717_gpiochip_probe(priv);
878 return dev_err_probe(&client->dev, ret,
879 "Failed to init gpiochip\n");
881 ret = max96717_register_clkout(priv);
883 return dev_err_probe(dev, ret, "Failed to register clkout\n");
885 ret = max96717_subdev_init(priv);
887 return dev_err_probe(dev, ret,
888 "Failed to initialize v4l2 subdev\n");
890 ret = max96717_i2c_mux_init(priv);
892 dev_err_probe(dev, ret, "failed to add remote i2c adapter\n");
893 max96717_subdev_uninit(priv);
899 static void max96717_remove(struct i2c_client *client)
901 struct v4l2_subdev *sd = i2c_get_clientdata(client);
902 struct max96717_priv *priv = sd_to_max96717(sd);
904 max96717_subdev_uninit(priv);
905 i2c_mux_del_adapters(priv->mux);
908 static const struct of_device_id max96717_of_ids[] = {
909 { .compatible = "maxim,max96717f" },
912 MODULE_DEVICE_TABLE(of, max96717_of_ids);
914 static struct i2c_driver max96717_i2c_driver = {
917 .of_match_table = max96717_of_ids,
919 .probe = max96717_probe,
920 .remove = max96717_remove,
923 module_i2c_driver(max96717_i2c_driver);
925 MODULE_DESCRIPTION("Maxim GMSL2 MAX96717 Serializer Driver");
927 MODULE_LICENSE("GPL");