1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Maxim MAX9286 GMSL Deserializer Driver
4 *
5 * Copyright (C) 2017-2019 Jacopo Mondi
6 * Copyright (C) 2017-2019 Kieran Bingham
7 * Copyright (C) 2017-2019 Laurent Pinchart
8 * Copyright (C) 2017-2019 Niklas Söderlund
9 * Copyright (C) 2016 Renesas Electronics Corporation
10 * Copyright (C) 2015 Cogent Embedded, Inc.
11 */
12
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/fwnode.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/gpio/machine.h>
19 #include <linux/i2c.h>
20 #include <linux/i2c-mux.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/of_graph.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
26
27 #include <media/v4l2-async.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-fwnode.h>
31 #include <media/v4l2-subdev.h>
32
33 /* Register 0x00 */
34 #define MAX9286_MSTLINKSEL_AUTO (7 << 5)
35 #define MAX9286_MSTLINKSEL(n) ((n) << 5)
36 #define MAX9286_EN_VS_GEN BIT(4)
37 #define MAX9286_LINKEN(n) (1 << (n))
38 /* Register 0x01 */
39 #define MAX9286_FSYNCMODE_ECU (3 << 6)
40 #define MAX9286_FSYNCMODE_EXT (2 << 6)
41 #define MAX9286_FSYNCMODE_INT_OUT (1 << 6)
42 #define MAX9286_FSYNCMODE_INT_HIZ (0 << 6)
43 #define MAX9286_GPIEN BIT(5)
44 #define MAX9286_ENLMO_RSTFSYNC BIT(2)
45 #define MAX9286_FSYNCMETH_AUTO (2 << 0)
46 #define MAX9286_FSYNCMETH_SEMI_AUTO (1 << 0)
47 #define MAX9286_FSYNCMETH_MANUAL (0 << 0)
48 #define MAX9286_REG_FSYNC_PERIOD_L 0x06
49 #define MAX9286_REG_FSYNC_PERIOD_M 0x07
50 #define MAX9286_REG_FSYNC_PERIOD_H 0x08
51 /* Register 0x0a */
52 #define MAX9286_FWDCCEN(n) (1 << ((n) + 4))
53 #define MAX9286_REVCCEN(n) (1 << (n))
54 /* Register 0x0c */
55 #define MAX9286_HVEN BIT(7)
56 #define MAX9286_EDC_6BIT_HAMMING (2 << 5)
57 #define MAX9286_EDC_6BIT_CRC (1 << 5)
58 #define MAX9286_EDC_1BIT_PARITY (0 << 5)
59 #define MAX9286_DESEL BIT(4)
60 #define MAX9286_INVVS BIT(3)
61 #define MAX9286_INVHS BIT(2)
62 #define MAX9286_HVSRC_D0 (2 << 0)
63 #define MAX9286_HVSRC_D14 (1 << 0)
64 #define MAX9286_HVSRC_D18 (0 << 0)
65 /* Register 0x0f */
66 #define MAX9286_0X0F_RESERVED BIT(3)
67 /* Register 0x12 */
68 #define MAX9286_CSILANECNT(n) (((n) - 1) << 6)
69 #define MAX9286_CSIDBL BIT(5)
70 #define MAX9286_DBL BIT(4)
71 #define MAX9286_DATATYPE_USER_8BIT (11 << 0)
72 #define MAX9286_DATATYPE_USER_YUV_12BIT (10 << 0)
73 #define MAX9286_DATATYPE_USER_24BIT (9 << 0)
74 #define MAX9286_DATATYPE_RAW14 (8 << 0)
75 #define MAX9286_DATATYPE_RAW11 (7 << 0)
76 #define MAX9286_DATATYPE_RAW10 (6 << 0)
77 #define MAX9286_DATATYPE_RAW8 (5 << 0)
78 #define MAX9286_DATATYPE_YUV422_10BIT (4 << 0)
79 #define MAX9286_DATATYPE_YUV422_8BIT (3 << 0)
80 #define MAX9286_DATATYPE_RGB555 (2 << 0)
81 #define MAX9286_DATATYPE_RGB565 (1 << 0)
82 #define MAX9286_DATATYPE_RGB888 (0 << 0)
83 /* Register 0x15 */
84 #define MAX9286_VC(n) ((n) << 5)
85 #define MAX9286_VCTYPE BIT(4)
86 #define MAX9286_CSIOUTEN BIT(3)
87 #define MAX9286_0X15_RESV (3 << 0)
88 /* Register 0x1b */
89 #define MAX9286_SWITCHIN(n) (1 << ((n) + 4))
90 #define MAX9286_ENEQ(n) (1 << (n))
91 /* Register 0x27 */
92 #define MAX9286_LOCKED BIT(7)
93 /* Register 0x31 */
94 #define MAX9286_FSYNC_LOCKED BIT(6)
95 /* Register 0x34 */
96 #define MAX9286_I2CLOCACK BIT(7)
97 #define MAX9286_I2CSLVSH_1046NS_469NS (3 << 5)
98 #define MAX9286_I2CSLVSH_938NS_352NS (2 << 5)
99 #define MAX9286_I2CSLVSH_469NS_234NS (1 << 5)
100 #define MAX9286_I2CSLVSH_352NS_117NS (0 << 5)
101 #define MAX9286_I2CMSTBT_837KBPS (7 << 2)
102 #define MAX9286_I2CMSTBT_533KBPS (6 << 2)
103 #define MAX9286_I2CMSTBT_339KBPS (5 << 2)
104 #define MAX9286_I2CMSTBT_173KBPS (4 << 2)
105 #define MAX9286_I2CMSTBT_105KBPS (3 << 2)
106 #define MAX9286_I2CMSTBT_84KBPS (2 << 2)
107 #define MAX9286_I2CMSTBT_28KBPS (1 << 2)
108 #define MAX9286_I2CMSTBT_8KBPS (0 << 2)
109 #define MAX9286_I2CSLVTO_NONE (3 << 0)
110 #define MAX9286_I2CSLVTO_1024US (2 << 0)
111 #define MAX9286_I2CSLVTO_256US (1 << 0)
112 #define MAX9286_I2CSLVTO_64US (0 << 0)
113 /* Register 0x3b */
114 #define MAX9286_REV_TRF(n) ((n) << 4)
115 #define MAX9286_REV_AMP(n) ((((n) - 30) / 10) << 1) /* in mV */
116 #define MAX9286_REV_AMP_X BIT(0)
117 #define MAX9286_REV_AMP_HIGH 170
118 /* Register 0x3f */
119 #define MAX9286_EN_REV_CFG BIT(6)
120 #define MAX9286_REV_FLEN(n) ((n) - 20)
121 /* Register 0x49 */
122 #define MAX9286_VIDEO_DETECT_MASK 0x0f
123 /* Register 0x69 */
124 #define MAX9286_LFLTBMONMASKED BIT(7)
125 #define MAX9286_LOCKMONMASKED BIT(6)
126 #define MAX9286_AUTOCOMBACKEN BIT(5)
127 #define MAX9286_AUTOMASKEN BIT(4)
128 #define MAX9286_MASKLINK(n) ((n) << 0)
129
130 /*
131 * The sink and source pads are created to match the OF graph port numbers so
132 * that their indexes can be used interchangeably.
133 */
134 #define MAX9286_NUM_GMSL 4
135 #define MAX9286_N_SINKS 4
136 #define MAX9286_N_PADS 5
137 #define MAX9286_SRC_PAD 4
138
139 struct max9286_source {
140 struct v4l2_subdev *sd;
141 struct fwnode_handle *fwnode;
142 };
143
144 struct max9286_asd {
145 struct v4l2_async_subdev base;
146 struct max9286_source *source;
147 };
148
to_max9286_asd(struct v4l2_async_subdev * asd)149 static inline struct max9286_asd *to_max9286_asd(struct v4l2_async_subdev *asd)
150 {
151 return container_of(asd, struct max9286_asd, base);
152 }
153
154 struct max9286_priv {
155 struct i2c_client *client;
156 struct gpio_desc *gpiod_pwdn;
157 struct v4l2_subdev sd;
158 struct media_pad pads[MAX9286_N_PADS];
159 struct regulator *regulator;
160
161 struct gpio_chip gpio;
162 u8 gpio_state;
163
164 struct i2c_mux_core *mux;
165 unsigned int mux_channel;
166 bool mux_open;
167
168 /* The initial reverse control channel amplitude. */
169 u32 init_rev_chan_mv;
170 u32 rev_chan_mv;
171
172 u32 gpio_poc[2];
173
174 struct v4l2_ctrl_handler ctrls;
175 struct v4l2_ctrl *pixelrate;
176
177 struct v4l2_mbus_framefmt fmt[MAX9286_N_SINKS];
178
179 /* Protects controls and fmt structures */
180 struct mutex mutex;
181
182 unsigned int nsources;
183 unsigned int source_mask;
184 unsigned int route_mask;
185 unsigned int bound_sources;
186 unsigned int csi2_data_lanes;
187 struct max9286_source sources[MAX9286_NUM_GMSL];
188 struct v4l2_async_notifier notifier;
189 };
190
next_source(struct max9286_priv * priv,struct max9286_source * source)191 static struct max9286_source *next_source(struct max9286_priv *priv,
192 struct max9286_source *source)
193 {
194 if (!source)
195 source = &priv->sources[0];
196 else
197 source++;
198
199 for (; source < &priv->sources[MAX9286_NUM_GMSL]; source++) {
200 if (source->fwnode)
201 return source;
202 }
203
204 return NULL;
205 }
206
207 #define for_each_source(priv, source) \
208 for ((source) = NULL; ((source) = next_source((priv), (source))); )
209
210 #define to_index(priv, source) ((source) - &(priv)->sources[0])
211
sd_to_max9286(struct v4l2_subdev * sd)212 static inline struct max9286_priv *sd_to_max9286(struct v4l2_subdev *sd)
213 {
214 return container_of(sd, struct max9286_priv, sd);
215 }
216
217 /* -----------------------------------------------------------------------------
218 * I2C IO
219 */
220
max9286_read(struct max9286_priv * priv,u8 reg)221 static int max9286_read(struct max9286_priv *priv, u8 reg)
222 {
223 int ret;
224
225 ret = i2c_smbus_read_byte_data(priv->client, reg);
226 if (ret < 0)
227 dev_err(&priv->client->dev,
228 "%s: register 0x%02x read failed (%d)\n",
229 __func__, reg, ret);
230
231 return ret;
232 }
233
max9286_write(struct max9286_priv * priv,u8 reg,u8 val)234 static int max9286_write(struct max9286_priv *priv, u8 reg, u8 val)
235 {
236 int ret;
237
238 ret = i2c_smbus_write_byte_data(priv->client, reg, val);
239 if (ret < 0)
240 dev_err(&priv->client->dev,
241 "%s: register 0x%02x write failed (%d)\n",
242 __func__, reg, ret);
243
244 return ret;
245 }
246
247 /* -----------------------------------------------------------------------------
248 * I2C Multiplexer
249 */
250
max9286_i2c_mux_configure(struct max9286_priv * priv,u8 conf)251 static void max9286_i2c_mux_configure(struct max9286_priv *priv, u8 conf)
252 {
253 max9286_write(priv, 0x0a, conf);
254
255 /*
256 * We must sleep after any change to the forward or reverse channel
257 * configuration.
258 */
259 usleep_range(3000, 5000);
260 }
261
max9286_i2c_mux_open(struct max9286_priv * priv)262 static void max9286_i2c_mux_open(struct max9286_priv *priv)
263 {
264 /* Open all channels on the MAX9286 */
265 max9286_i2c_mux_configure(priv, 0xff);
266
267 priv->mux_open = true;
268 }
269
max9286_i2c_mux_close(struct max9286_priv * priv)270 static void max9286_i2c_mux_close(struct max9286_priv *priv)
271 {
272 /*
273 * Ensure that both the forward and reverse channel are disabled on the
274 * mux, and that the channel ID is invalidated to ensure we reconfigure
275 * on the next max9286_i2c_mux_select() call.
276 */
277 max9286_i2c_mux_configure(priv, 0x00);
278
279 priv->mux_open = false;
280 priv->mux_channel = -1;
281 }
282
max9286_i2c_mux_select(struct i2c_mux_core * muxc,u32 chan)283 static int max9286_i2c_mux_select(struct i2c_mux_core *muxc, u32 chan)
284 {
285 struct max9286_priv *priv = i2c_mux_priv(muxc);
286
287 /* Channel select is disabled when configured in the opened state. */
288 if (priv->mux_open)
289 return 0;
290
291 if (priv->mux_channel == chan)
292 return 0;
293
294 priv->mux_channel = chan;
295
296 max9286_i2c_mux_configure(priv, MAX9286_FWDCCEN(chan) |
297 MAX9286_REVCCEN(chan));
298
299 return 0;
300 }
301
max9286_i2c_mux_init(struct max9286_priv * priv)302 static int max9286_i2c_mux_init(struct max9286_priv *priv)
303 {
304 struct max9286_source *source;
305 int ret;
306
307 if (!i2c_check_functionality(priv->client->adapter,
308 I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
309 return -ENODEV;
310
311 priv->mux = i2c_mux_alloc(priv->client->adapter, &priv->client->dev,
312 priv->nsources, 0, I2C_MUX_LOCKED,
313 max9286_i2c_mux_select, NULL);
314 if (!priv->mux)
315 return -ENOMEM;
316
317 priv->mux->priv = priv;
318
319 for_each_source(priv, source) {
320 unsigned int index = to_index(priv, source);
321
322 ret = i2c_mux_add_adapter(priv->mux, 0, index, 0);
323 if (ret < 0)
324 goto error;
325 }
326
327 return 0;
328
329 error:
330 i2c_mux_del_adapters(priv->mux);
331 return ret;
332 }
333
max9286_configure_i2c(struct max9286_priv * priv,bool localack)334 static void max9286_configure_i2c(struct max9286_priv *priv, bool localack)
335 {
336 u8 config = MAX9286_I2CSLVSH_469NS_234NS | MAX9286_I2CSLVTO_1024US |
337 MAX9286_I2CMSTBT_105KBPS;
338
339 if (localack)
340 config |= MAX9286_I2CLOCACK;
341
342 max9286_write(priv, 0x34, config);
343 usleep_range(3000, 5000);
344 }
345
max9286_reverse_channel_setup(struct max9286_priv * priv,unsigned int chan_amplitude)346 static void max9286_reverse_channel_setup(struct max9286_priv *priv,
347 unsigned int chan_amplitude)
348 {
349 u8 chan_config;
350
351 if (priv->rev_chan_mv == chan_amplitude)
352 return;
353
354 priv->rev_chan_mv = chan_amplitude;
355
356 /* Reverse channel transmission time: default to 1. */
357 chan_config = MAX9286_REV_TRF(1);
358
359 /*
360 * Reverse channel setup.
361 *
362 * - Enable custom reverse channel configuration (through register 0x3f)
363 * and set the first pulse length to 35 clock cycles.
364 * - Adjust reverse channel amplitude: values > 130 are programmed
365 * using the additional +100mV REV_AMP_X boost flag
366 */
367 max9286_write(priv, 0x3f, MAX9286_EN_REV_CFG | MAX9286_REV_FLEN(35));
368
369 if (chan_amplitude > 100) {
370 /* It is not possible to express values (100 < x < 130) */
371 chan_amplitude = max(30U, chan_amplitude - 100);
372 chan_config |= MAX9286_REV_AMP_X;
373 }
374 max9286_write(priv, 0x3b, chan_config | MAX9286_REV_AMP(chan_amplitude));
375 usleep_range(2000, 2500);
376 }
377
378 /*
379 * max9286_check_video_links() - Make sure video links are detected and locked
380 *
381 * Performs safety checks on video link status. Make sure they are detected
382 * and all enabled links are locked.
383 *
384 * Returns 0 for success, -EIO for errors.
385 */
max9286_check_video_links(struct max9286_priv * priv)386 static int max9286_check_video_links(struct max9286_priv *priv)
387 {
388 unsigned int i;
389 int ret;
390
391 /*
392 * Make sure valid video links are detected.
393 * The delay is not characterized in de-serializer manual, wait up
394 * to 5 ms.
395 */
396 for (i = 0; i < 10; i++) {
397 ret = max9286_read(priv, 0x49);
398 if (ret < 0)
399 return -EIO;
400
401 if ((ret & MAX9286_VIDEO_DETECT_MASK) == priv->source_mask)
402 break;
403
404 usleep_range(350, 500);
405 }
406
407 if (i == 10) {
408 dev_err(&priv->client->dev,
409 "Unable to detect video links: 0x%02x\n", ret);
410 return -EIO;
411 }
412
413 /* Make sure all enabled links are locked (4ms max). */
414 for (i = 0; i < 10; i++) {
415 ret = max9286_read(priv, 0x27);
416 if (ret < 0)
417 return -EIO;
418
419 if (ret & MAX9286_LOCKED)
420 break;
421
422 usleep_range(350, 450);
423 }
424
425 if (i == 10) {
426 dev_err(&priv->client->dev, "Not all enabled links locked\n");
427 return -EIO;
428 }
429
430 return 0;
431 }
432
433 /*
434 * max9286_check_config_link() - Detect and wait for configuration links
435 *
436 * Determine if the configuration channel is up and settled for a link.
437 *
438 * Returns 0 for success, -EIO for errors.
439 */
max9286_check_config_link(struct max9286_priv * priv,unsigned int source_mask)440 static int max9286_check_config_link(struct max9286_priv *priv,
441 unsigned int source_mask)
442 {
443 unsigned int conflink_mask = (source_mask & 0x0f) << 4;
444 unsigned int i;
445 int ret;
446
447 /*
448 * Make sure requested configuration links are detected.
449 * The delay is not characterized in the chip manual: wait up
450 * to 5 milliseconds.
451 */
452 for (i = 0; i < 10; i++) {
453 ret = max9286_read(priv, 0x49);
454 if (ret < 0)
455 return -EIO;
456
457 ret &= 0xf0;
458 if (ret == conflink_mask)
459 break;
460
461 usleep_range(350, 500);
462 }
463
464 if (ret != conflink_mask) {
465 dev_err(&priv->client->dev,
466 "Unable to detect configuration links: 0x%02x expected 0x%02x\n",
467 ret, conflink_mask);
468 return -EIO;
469 }
470
471 dev_info(&priv->client->dev,
472 "Successfully detected configuration links after %u loops: 0x%02x\n",
473 i, conflink_mask);
474
475 return 0;
476 }
477
478 /* -----------------------------------------------------------------------------
479 * V4L2 Subdev
480 */
481
max9286_set_pixelrate(struct max9286_priv * priv)482 static int max9286_set_pixelrate(struct max9286_priv *priv)
483 {
484 struct max9286_source *source = NULL;
485 u64 pixelrate = 0;
486
487 for_each_source(priv, source) {
488 struct v4l2_ctrl *ctrl;
489 u64 source_rate = 0;
490
491 /* Pixel rate is mandatory to be reported by sources. */
492 ctrl = v4l2_ctrl_find(source->sd->ctrl_handler,
493 V4L2_CID_PIXEL_RATE);
494 if (!ctrl) {
495 pixelrate = 0;
496 break;
497 }
498
499 /* All source must report the same pixel rate. */
500 source_rate = v4l2_ctrl_g_ctrl_int64(ctrl);
501 if (!pixelrate) {
502 pixelrate = source_rate;
503 } else if (pixelrate != source_rate) {
504 dev_err(&priv->client->dev,
505 "Unable to calculate pixel rate\n");
506 return -EINVAL;
507 }
508 }
509
510 if (!pixelrate) {
511 dev_err(&priv->client->dev,
512 "No pixel rate control available in sources\n");
513 return -EINVAL;
514 }
515
516 /*
517 * The CSI-2 transmitter pixel rate is the single source rate multiplied
518 * by the number of available sources.
519 */
520 return v4l2_ctrl_s_ctrl_int64(priv->pixelrate,
521 pixelrate * priv->nsources);
522 }
523
max9286_notify_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_subdev * asd)524 static int max9286_notify_bound(struct v4l2_async_notifier *notifier,
525 struct v4l2_subdev *subdev,
526 struct v4l2_async_subdev *asd)
527 {
528 struct max9286_priv *priv = sd_to_max9286(notifier->sd);
529 struct max9286_source *source = to_max9286_asd(asd)->source;
530 unsigned int index = to_index(priv, source);
531 unsigned int src_pad;
532 int ret;
533
534 ret = media_entity_get_fwnode_pad(&subdev->entity,
535 source->fwnode,
536 MEDIA_PAD_FL_SOURCE);
537 if (ret < 0) {
538 dev_err(&priv->client->dev,
539 "Failed to find pad for %s\n", subdev->name);
540 return ret;
541 }
542
543 priv->bound_sources |= BIT(index);
544 source->sd = subdev;
545 src_pad = ret;
546
547 ret = media_create_pad_link(&source->sd->entity, src_pad,
548 &priv->sd.entity, index,
549 MEDIA_LNK_FL_ENABLED |
550 MEDIA_LNK_FL_IMMUTABLE);
551 if (ret) {
552 dev_err(&priv->client->dev,
553 "Unable to link %s:%u -> %s:%u\n",
554 source->sd->name, src_pad, priv->sd.name, index);
555 return ret;
556 }
557
558 dev_dbg(&priv->client->dev, "Bound %s pad: %u on index %u\n",
559 subdev->name, src_pad, index);
560
561 /*
562 * As we register a subdev notifiers we won't get a .complete() callback
563 * here, so we have to use bound_sources to identify when all remote
564 * serializers have probed.
565 */
566 if (priv->bound_sources != priv->source_mask)
567 return 0;
568
569 /*
570 * All enabled sources have probed and enabled their reverse control
571 * channels:
572 *
573 * - Increase the reverse channel amplitude to compensate for the
574 * remote ends high threshold
575 * - Verify all configuration links are properly detected
576 * - Disable auto-ack as communication on the control channel are now
577 * stable.
578 */
579 max9286_reverse_channel_setup(priv, MAX9286_REV_AMP_HIGH);
580 max9286_check_config_link(priv, priv->source_mask);
581 max9286_configure_i2c(priv, false);
582
583 return max9286_set_pixelrate(priv);
584 }
585
max9286_notify_unbind(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_subdev * asd)586 static void max9286_notify_unbind(struct v4l2_async_notifier *notifier,
587 struct v4l2_subdev *subdev,
588 struct v4l2_async_subdev *asd)
589 {
590 struct max9286_priv *priv = sd_to_max9286(notifier->sd);
591 struct max9286_source *source = to_max9286_asd(asd)->source;
592 unsigned int index = to_index(priv, source);
593
594 source->sd = NULL;
595 priv->bound_sources &= ~BIT(index);
596 }
597
598 static const struct v4l2_async_notifier_operations max9286_notify_ops = {
599 .bound = max9286_notify_bound,
600 .unbind = max9286_notify_unbind,
601 };
602
max9286_v4l2_notifier_register(struct max9286_priv * priv)603 static int max9286_v4l2_notifier_register(struct max9286_priv *priv)
604 {
605 struct device *dev = &priv->client->dev;
606 struct max9286_source *source = NULL;
607 int ret;
608
609 if (!priv->nsources)
610 return 0;
611
612 v4l2_async_notifier_init(&priv->notifier);
613
614 for_each_source(priv, source) {
615 unsigned int i = to_index(priv, source);
616 struct max9286_asd *mas;
617
618 mas = v4l2_async_notifier_add_fwnode_subdev(&priv->notifier,
619 source->fwnode,
620 struct max9286_asd);
621 if (IS_ERR(mas)) {
622 dev_err(dev, "Failed to add subdev for source %u: %ld",
623 i, PTR_ERR(mas));
624 v4l2_async_notifier_cleanup(&priv->notifier);
625 return PTR_ERR(mas);
626 }
627
628 mas->source = source;
629 }
630
631 priv->notifier.ops = &max9286_notify_ops;
632
633 ret = v4l2_async_subdev_notifier_register(&priv->sd, &priv->notifier);
634 if (ret) {
635 dev_err(dev, "Failed to register subdev_notifier");
636 v4l2_async_notifier_cleanup(&priv->notifier);
637 return ret;
638 }
639
640 return 0;
641 }
642
max9286_v4l2_notifier_unregister(struct max9286_priv * priv)643 static void max9286_v4l2_notifier_unregister(struct max9286_priv *priv)
644 {
645 if (!priv->nsources)
646 return;
647
648 v4l2_async_notifier_unregister(&priv->notifier);
649 v4l2_async_notifier_cleanup(&priv->notifier);
650 }
651
max9286_s_stream(struct v4l2_subdev * sd,int enable)652 static int max9286_s_stream(struct v4l2_subdev *sd, int enable)
653 {
654 struct max9286_priv *priv = sd_to_max9286(sd);
655 struct max9286_source *source;
656 unsigned int i;
657 bool sync = false;
658 int ret;
659
660 if (enable) {
661 /*
662 * The frame sync between cameras is transmitted across the
663 * reverse channel as GPIO. We must open all channels while
664 * streaming to allow this synchronisation signal to be shared.
665 */
666 max9286_i2c_mux_open(priv);
667
668 /* Start all cameras. */
669 for_each_source(priv, source) {
670 ret = v4l2_subdev_call(source->sd, video, s_stream, 1);
671 if (ret)
672 return ret;
673 }
674
675 ret = max9286_check_video_links(priv);
676 if (ret)
677 return ret;
678
679 /*
680 * Wait until frame synchronization is locked.
681 *
682 * Manual says frame sync locking should take ~6 VTS.
683 * From practical experience at least 8 are required. Give
684 * 12 complete frames time (~400ms at 30 fps) to achieve frame
685 * locking before returning error.
686 */
687 for (i = 0; i < 40; i++) {
688 if (max9286_read(priv, 0x31) & MAX9286_FSYNC_LOCKED) {
689 sync = true;
690 break;
691 }
692 usleep_range(9000, 11000);
693 }
694
695 if (!sync) {
696 dev_err(&priv->client->dev,
697 "Failed to get frame synchronization\n");
698 return -EXDEV; /* Invalid cross-device link */
699 }
700
701 /*
702 * Enable CSI output, VC set according to link number.
703 * Bit 7 must be set (chip manual says it's 0 and reserved).
704 */
705 max9286_write(priv, 0x15, 0x80 | MAX9286_VCTYPE |
706 MAX9286_CSIOUTEN | MAX9286_0X15_RESV);
707 } else {
708 max9286_write(priv, 0x15, MAX9286_VCTYPE | MAX9286_0X15_RESV);
709
710 /* Stop all cameras. */
711 for_each_source(priv, source)
712 v4l2_subdev_call(source->sd, video, s_stream, 0);
713
714 max9286_i2c_mux_close(priv);
715 }
716
717 return 0;
718 }
719
max9286_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)720 static int max9286_enum_mbus_code(struct v4l2_subdev *sd,
721 struct v4l2_subdev_state *sd_state,
722 struct v4l2_subdev_mbus_code_enum *code)
723 {
724 if (code->pad || code->index > 0)
725 return -EINVAL;
726
727 code->code = MEDIA_BUS_FMT_UYVY8_1X16;
728
729 return 0;
730 }
731
732 static struct v4l2_mbus_framefmt *
max9286_get_pad_format(struct max9286_priv * priv,struct v4l2_subdev_state * sd_state,unsigned int pad,u32 which)733 max9286_get_pad_format(struct max9286_priv *priv,
734 struct v4l2_subdev_state *sd_state,
735 unsigned int pad, u32 which)
736 {
737 switch (which) {
738 case V4L2_SUBDEV_FORMAT_TRY:
739 return v4l2_subdev_get_try_format(&priv->sd, sd_state, pad);
740 case V4L2_SUBDEV_FORMAT_ACTIVE:
741 return &priv->fmt[pad];
742 default:
743 return NULL;
744 }
745 }
746
max9286_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)747 static int max9286_set_fmt(struct v4l2_subdev *sd,
748 struct v4l2_subdev_state *sd_state,
749 struct v4l2_subdev_format *format)
750 {
751 struct max9286_priv *priv = sd_to_max9286(sd);
752 struct v4l2_mbus_framefmt *cfg_fmt;
753
754 if (format->pad == MAX9286_SRC_PAD)
755 return -EINVAL;
756
757 /* Refuse non YUV422 formats as we hardcode DT to 8 bit YUV422 */
758 switch (format->format.code) {
759 case MEDIA_BUS_FMT_UYVY8_1X16:
760 case MEDIA_BUS_FMT_VYUY8_1X16:
761 case MEDIA_BUS_FMT_YUYV8_1X16:
762 case MEDIA_BUS_FMT_YVYU8_1X16:
763 break;
764 default:
765 format->format.code = MEDIA_BUS_FMT_UYVY8_1X16;
766 break;
767 }
768
769 cfg_fmt = max9286_get_pad_format(priv, sd_state, format->pad,
770 format->which);
771 if (!cfg_fmt)
772 return -EINVAL;
773
774 mutex_lock(&priv->mutex);
775 *cfg_fmt = format->format;
776 mutex_unlock(&priv->mutex);
777
778 return 0;
779 }
780
max9286_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)781 static int max9286_get_fmt(struct v4l2_subdev *sd,
782 struct v4l2_subdev_state *sd_state,
783 struct v4l2_subdev_format *format)
784 {
785 struct max9286_priv *priv = sd_to_max9286(sd);
786 struct v4l2_mbus_framefmt *cfg_fmt;
787 unsigned int pad = format->pad;
788
789 /*
790 * Multiplexed Stream Support: Support link validation by returning the
791 * format of the first bound link. All links must have the same format,
792 * as we do not support mixing and matching of cameras connected to the
793 * max9286.
794 */
795 if (pad == MAX9286_SRC_PAD)
796 pad = __ffs(priv->bound_sources);
797
798 cfg_fmt = max9286_get_pad_format(priv, sd_state, pad, format->which);
799 if (!cfg_fmt)
800 return -EINVAL;
801
802 mutex_lock(&priv->mutex);
803 format->format = *cfg_fmt;
804 mutex_unlock(&priv->mutex);
805
806 return 0;
807 }
808
809 static const struct v4l2_subdev_video_ops max9286_video_ops = {
810 .s_stream = max9286_s_stream,
811 };
812
813 static const struct v4l2_subdev_pad_ops max9286_pad_ops = {
814 .enum_mbus_code = max9286_enum_mbus_code,
815 .get_fmt = max9286_get_fmt,
816 .set_fmt = max9286_set_fmt,
817 };
818
819 static const struct v4l2_subdev_ops max9286_subdev_ops = {
820 .video = &max9286_video_ops,
821 .pad = &max9286_pad_ops,
822 };
823
max9286_init_format(struct v4l2_mbus_framefmt * fmt)824 static void max9286_init_format(struct v4l2_mbus_framefmt *fmt)
825 {
826 fmt->width = 1280;
827 fmt->height = 800;
828 fmt->code = MEDIA_BUS_FMT_UYVY8_1X16;
829 fmt->colorspace = V4L2_COLORSPACE_SRGB;
830 fmt->field = V4L2_FIELD_NONE;
831 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
832 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
833 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
834 }
835
max9286_open(struct v4l2_subdev * subdev,struct v4l2_subdev_fh * fh)836 static int max9286_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
837 {
838 struct v4l2_mbus_framefmt *format;
839 unsigned int i;
840
841 for (i = 0; i < MAX9286_N_SINKS; i++) {
842 format = v4l2_subdev_get_try_format(subdev, fh->state, i);
843 max9286_init_format(format);
844 }
845
846 return 0;
847 }
848
849 static const struct v4l2_subdev_internal_ops max9286_subdev_internal_ops = {
850 .open = max9286_open,
851 };
852
max9286_s_ctrl(struct v4l2_ctrl * ctrl)853 static int max9286_s_ctrl(struct v4l2_ctrl *ctrl)
854 {
855 switch (ctrl->id) {
856 case V4L2_CID_PIXEL_RATE:
857 return 0;
858 default:
859 return -EINVAL;
860 }
861 }
862
863 static const struct v4l2_ctrl_ops max9286_ctrl_ops = {
864 .s_ctrl = max9286_s_ctrl,
865 };
866
max9286_v4l2_register(struct max9286_priv * priv)867 static int max9286_v4l2_register(struct max9286_priv *priv)
868 {
869 struct device *dev = &priv->client->dev;
870 struct fwnode_handle *ep;
871 int ret;
872 int i;
873
874 /* Register v4l2 async notifiers for connected Camera subdevices */
875 ret = max9286_v4l2_notifier_register(priv);
876 if (ret) {
877 dev_err(dev, "Unable to register V4L2 async notifiers\n");
878 return ret;
879 }
880
881 /* Configure V4L2 for the MAX9286 itself */
882
883 for (i = 0; i < MAX9286_N_SINKS; i++)
884 max9286_init_format(&priv->fmt[i]);
885
886 v4l2_i2c_subdev_init(&priv->sd, priv->client, &max9286_subdev_ops);
887 priv->sd.internal_ops = &max9286_subdev_internal_ops;
888 priv->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
889
890 v4l2_ctrl_handler_init(&priv->ctrls, 1);
891 priv->pixelrate = v4l2_ctrl_new_std(&priv->ctrls,
892 &max9286_ctrl_ops,
893 V4L2_CID_PIXEL_RATE,
894 1, INT_MAX, 1, 50000000);
895
896 priv->sd.ctrl_handler = &priv->ctrls;
897 ret = priv->ctrls.error;
898 if (ret)
899 goto err_async;
900
901 priv->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
902
903 priv->pads[MAX9286_SRC_PAD].flags = MEDIA_PAD_FL_SOURCE;
904 for (i = 0; i < MAX9286_SRC_PAD; i++)
905 priv->pads[i].flags = MEDIA_PAD_FL_SINK;
906 ret = media_entity_pads_init(&priv->sd.entity, MAX9286_N_PADS,
907 priv->pads);
908 if (ret)
909 goto err_async;
910
911 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev), MAX9286_SRC_PAD,
912 0, 0);
913 if (!ep) {
914 dev_err(dev, "Unable to retrieve endpoint on \"port@4\"\n");
915 ret = -ENOENT;
916 goto err_async;
917 }
918 priv->sd.fwnode = ep;
919
920 ret = v4l2_async_register_subdev(&priv->sd);
921 if (ret < 0) {
922 dev_err(dev, "Unable to register subdevice\n");
923 goto err_put_node;
924 }
925
926 return 0;
927
928 err_put_node:
929 fwnode_handle_put(ep);
930 err_async:
931 v4l2_ctrl_handler_free(&priv->ctrls);
932 max9286_v4l2_notifier_unregister(priv);
933
934 return ret;
935 }
936
max9286_v4l2_unregister(struct max9286_priv * priv)937 static void max9286_v4l2_unregister(struct max9286_priv *priv)
938 {
939 fwnode_handle_put(priv->sd.fwnode);
940 v4l2_ctrl_handler_free(&priv->ctrls);
941 v4l2_async_unregister_subdev(&priv->sd);
942 max9286_v4l2_notifier_unregister(priv);
943 }
944
945 /* -----------------------------------------------------------------------------
946 * Probe/Remove
947 */
948
max9286_setup(struct max9286_priv * priv)949 static int max9286_setup(struct max9286_priv *priv)
950 {
951 /*
952 * Link ordering values for all enabled links combinations. Orders must
953 * be assigned sequentially from 0 to the number of enabled links
954 * without leaving any hole for disabled links. We thus assign orders to
955 * enabled links first, and use the remaining order values for disabled
956 * links are all links must have a different order value;
957 */
958 static const u8 link_order[] = {
959 (3 << 6) | (2 << 4) | (1 << 2) | (0 << 0), /* xxxx */
960 (3 << 6) | (2 << 4) | (1 << 2) | (0 << 0), /* xxx0 */
961 (3 << 6) | (2 << 4) | (0 << 2) | (1 << 0), /* xx0x */
962 (3 << 6) | (2 << 4) | (1 << 2) | (0 << 0), /* xx10 */
963 (3 << 6) | (0 << 4) | (2 << 2) | (1 << 0), /* x0xx */
964 (3 << 6) | (1 << 4) | (2 << 2) | (0 << 0), /* x1x0 */
965 (3 << 6) | (1 << 4) | (0 << 2) | (2 << 0), /* x10x */
966 (3 << 6) | (1 << 4) | (1 << 2) | (0 << 0), /* x210 */
967 (0 << 6) | (3 << 4) | (2 << 2) | (1 << 0), /* 0xxx */
968 (1 << 6) | (3 << 4) | (2 << 2) | (0 << 0), /* 1xx0 */
969 (1 << 6) | (3 << 4) | (0 << 2) | (2 << 0), /* 1x0x */
970 (2 << 6) | (3 << 4) | (1 << 2) | (0 << 0), /* 2x10 */
971 (1 << 6) | (0 << 4) | (3 << 2) | (2 << 0), /* 10xx */
972 (2 << 6) | (1 << 4) | (3 << 2) | (0 << 0), /* 21x0 */
973 (2 << 6) | (1 << 4) | (0 << 2) | (3 << 0), /* 210x */
974 (3 << 6) | (2 << 4) | (1 << 2) | (0 << 0), /* 3210 */
975 };
976
977 /*
978 * Set the I2C bus speed.
979 *
980 * Enable I2C Local Acknowledge during the probe sequences of the camera
981 * only. This should be disabled after the mux is initialised.
982 */
983 max9286_configure_i2c(priv, true);
984 max9286_reverse_channel_setup(priv, priv->init_rev_chan_mv);
985
986 /*
987 * Enable GMSL links, mask unused ones and autodetect link
988 * used as CSI clock source.
989 */
990 max9286_write(priv, 0x00, MAX9286_MSTLINKSEL_AUTO | priv->route_mask);
991 max9286_write(priv, 0x0b, link_order[priv->route_mask]);
992 max9286_write(priv, 0x69, (0xf & ~priv->route_mask));
993
994 /*
995 * Video format setup:
996 * Disable CSI output, VC is set according to Link number.
997 */
998 max9286_write(priv, 0x15, MAX9286_VCTYPE | MAX9286_0X15_RESV);
999
1000 /* Enable CSI-2 Lane D0-D3 only, DBL mode, YUV422 8-bit. */
1001 max9286_write(priv, 0x12, MAX9286_CSIDBL | MAX9286_DBL |
1002 MAX9286_CSILANECNT(priv->csi2_data_lanes) |
1003 MAX9286_DATATYPE_YUV422_8BIT);
1004
1005 /* Automatic: FRAMESYNC taken from the slowest Link. */
1006 max9286_write(priv, 0x01, MAX9286_FSYNCMODE_INT_HIZ |
1007 MAX9286_FSYNCMETH_AUTO);
1008
1009 /* Enable HS/VS encoding, use D14/15 for HS/VS, invert VS. */
1010 max9286_write(priv, 0x0c, MAX9286_HVEN | MAX9286_INVVS |
1011 MAX9286_HVSRC_D14);
1012
1013 /*
1014 * The overlap window seems to provide additional validation by tracking
1015 * the delay between vsync and frame sync, generating an error if the
1016 * delay is bigger than the programmed window, though it's not yet clear
1017 * what value should be set.
1018 *
1019 * As it's an optional value and can be disabled, we do so by setting
1020 * a 0 overlap value.
1021 */
1022 max9286_write(priv, 0x63, 0);
1023 max9286_write(priv, 0x64, 0);
1024
1025 /*
1026 * Wait for 2ms to allow the link to resynchronize after the
1027 * configuration change.
1028 */
1029 usleep_range(2000, 5000);
1030
1031 return 0;
1032 }
1033
max9286_gpio_set(struct max9286_priv * priv,unsigned int offset,int value)1034 static int max9286_gpio_set(struct max9286_priv *priv, unsigned int offset,
1035 int value)
1036 {
1037 if (value)
1038 priv->gpio_state |= BIT(offset);
1039 else
1040 priv->gpio_state &= ~BIT(offset);
1041
1042 return max9286_write(priv, 0x0f,
1043 MAX9286_0X0F_RESERVED | priv->gpio_state);
1044 }
1045
max9286_gpiochip_set(struct gpio_chip * chip,unsigned int offset,int value)1046 static void max9286_gpiochip_set(struct gpio_chip *chip,
1047 unsigned int offset, int value)
1048 {
1049 struct max9286_priv *priv = gpiochip_get_data(chip);
1050
1051 max9286_gpio_set(priv, offset, value);
1052 }
1053
max9286_gpiochip_get(struct gpio_chip * chip,unsigned int offset)1054 static int max9286_gpiochip_get(struct gpio_chip *chip, unsigned int offset)
1055 {
1056 struct max9286_priv *priv = gpiochip_get_data(chip);
1057
1058 return priv->gpio_state & BIT(offset);
1059 }
1060
max9286_register_gpio(struct max9286_priv * priv)1061 static int max9286_register_gpio(struct max9286_priv *priv)
1062 {
1063 struct device *dev = &priv->client->dev;
1064 struct gpio_chip *gpio = &priv->gpio;
1065 int ret;
1066
1067 /* Configure the GPIO */
1068 gpio->label = dev_name(dev);
1069 gpio->parent = dev;
1070 gpio->owner = THIS_MODULE;
1071 gpio->of_node = dev->of_node;
1072 gpio->ngpio = 2;
1073 gpio->base = -1;
1074 gpio->set = max9286_gpiochip_set;
1075 gpio->get = max9286_gpiochip_get;
1076 gpio->can_sleep = true;
1077
1078 ret = devm_gpiochip_add_data(dev, gpio, priv);
1079 if (ret)
1080 dev_err(dev, "Unable to create gpio_chip\n");
1081
1082 return ret;
1083 }
1084
max9286_parse_gpios(struct max9286_priv * priv)1085 static int max9286_parse_gpios(struct max9286_priv *priv)
1086 {
1087 struct device *dev = &priv->client->dev;
1088 int ret;
1089
1090 /* GPIO values default to high */
1091 priv->gpio_state = BIT(0) | BIT(1);
1092
1093 /*
1094 * Parse the "gpio-poc" vendor property. If the property is not
1095 * specified the camera power is controlled by a regulator.
1096 */
1097 ret = of_property_read_u32_array(dev->of_node, "maxim,gpio-poc",
1098 priv->gpio_poc, 2);
1099 if (ret == -EINVAL) {
1100 /*
1101 * If gpio lines are not used for the camera power, register
1102 * a gpio controller for consumers.
1103 */
1104 ret = max9286_register_gpio(priv);
1105 if (ret)
1106 return ret;
1107
1108 priv->regulator = devm_regulator_get(dev, "poc");
1109 if (IS_ERR(priv->regulator)) {
1110 return dev_err_probe(dev, PTR_ERR(priv->regulator),
1111 "Unable to get PoC regulator (%ld)\n",
1112 PTR_ERR(priv->regulator));
1113 }
1114
1115 return 0;
1116 }
1117
1118 /* If the property is specified make sure it is well formed. */
1119 if (ret || priv->gpio_poc[0] > 1 ||
1120 (priv->gpio_poc[1] != GPIO_ACTIVE_HIGH &&
1121 priv->gpio_poc[1] != GPIO_ACTIVE_LOW)) {
1122 dev_err(dev, "Invalid 'gpio-poc' property\n");
1123 return -EINVAL;
1124 }
1125
1126 return 0;
1127 }
1128
max9286_poc_enable(struct max9286_priv * priv,bool enable)1129 static int max9286_poc_enable(struct max9286_priv *priv, bool enable)
1130 {
1131 int ret;
1132
1133 /* If the regulator is not available, use gpio to control power. */
1134 if (!priv->regulator)
1135 ret = max9286_gpio_set(priv, priv->gpio_poc[0],
1136 enable ^ priv->gpio_poc[1]);
1137 else if (enable)
1138 ret = regulator_enable(priv->regulator);
1139 else
1140 ret = regulator_disable(priv->regulator);
1141
1142 if (ret < 0)
1143 dev_err(&priv->client->dev, "Unable to turn power %s\n",
1144 enable ? "on" : "off");
1145
1146 return ret;
1147 }
1148
max9286_init(struct max9286_priv * priv)1149 static int max9286_init(struct max9286_priv *priv)
1150 {
1151 struct i2c_client *client = priv->client;
1152 int ret;
1153
1154 ret = max9286_poc_enable(priv, true);
1155 if (ret)
1156 return ret;
1157
1158 ret = max9286_setup(priv);
1159 if (ret) {
1160 dev_err(&client->dev, "Unable to setup max9286\n");
1161 goto err_poc_disable;
1162 }
1163
1164 /*
1165 * Register all V4L2 interactions for the MAX9286 and notifiers for
1166 * any subdevices connected.
1167 */
1168 ret = max9286_v4l2_register(priv);
1169 if (ret) {
1170 dev_err(&client->dev, "Failed to register with V4L2\n");
1171 goto err_poc_disable;
1172 }
1173
1174 ret = max9286_i2c_mux_init(priv);
1175 if (ret) {
1176 dev_err(&client->dev, "Unable to initialize I2C multiplexer\n");
1177 goto err_v4l2_register;
1178 }
1179
1180 /* Leave the mux channels disabled until they are selected. */
1181 max9286_i2c_mux_close(priv);
1182
1183 return 0;
1184
1185 err_v4l2_register:
1186 max9286_v4l2_unregister(priv);
1187 err_poc_disable:
1188 max9286_poc_enable(priv, false);
1189
1190 return ret;
1191 }
1192
max9286_cleanup_dt(struct max9286_priv * priv)1193 static void max9286_cleanup_dt(struct max9286_priv *priv)
1194 {
1195 struct max9286_source *source;
1196
1197 for_each_source(priv, source) {
1198 fwnode_handle_put(source->fwnode);
1199 source->fwnode = NULL;
1200 }
1201 }
1202
max9286_parse_dt(struct max9286_priv * priv)1203 static int max9286_parse_dt(struct max9286_priv *priv)
1204 {
1205 struct device *dev = &priv->client->dev;
1206 struct device_node *i2c_mux;
1207 struct device_node *node = NULL;
1208 unsigned int i2c_mux_mask = 0;
1209 u32 reverse_channel_microvolt;
1210
1211 /* Balance the of_node_put() performed by of_find_node_by_name(). */
1212 of_node_get(dev->of_node);
1213 i2c_mux = of_find_node_by_name(dev->of_node, "i2c-mux");
1214 if (!i2c_mux) {
1215 dev_err(dev, "Failed to find i2c-mux node\n");
1216 return -EINVAL;
1217 }
1218
1219 /* Identify which i2c-mux channels are enabled */
1220 for_each_child_of_node(i2c_mux, node) {
1221 u32 id = 0;
1222
1223 of_property_read_u32(node, "reg", &id);
1224 if (id >= MAX9286_NUM_GMSL)
1225 continue;
1226
1227 if (!of_device_is_available(node)) {
1228 dev_dbg(dev, "Skipping disabled I2C bus port %u\n", id);
1229 continue;
1230 }
1231
1232 i2c_mux_mask |= BIT(id);
1233 }
1234 of_node_put(i2c_mux);
1235
1236 /* Parse the endpoints */
1237 for_each_endpoint_of_node(dev->of_node, node) {
1238 struct max9286_source *source;
1239 struct of_endpoint ep;
1240
1241 of_graph_parse_endpoint(node, &ep);
1242 dev_dbg(dev, "Endpoint %pOF on port %d",
1243 ep.local_node, ep.port);
1244
1245 if (ep.port > MAX9286_NUM_GMSL) {
1246 dev_err(dev, "Invalid endpoint %s on port %d",
1247 of_node_full_name(ep.local_node), ep.port);
1248 continue;
1249 }
1250
1251 /* For the source endpoint just parse the bus configuration. */
1252 if (ep.port == MAX9286_SRC_PAD) {
1253 struct v4l2_fwnode_endpoint vep = {
1254 .bus_type = V4L2_MBUS_CSI2_DPHY
1255 };
1256 int ret;
1257
1258 ret = v4l2_fwnode_endpoint_parse(
1259 of_fwnode_handle(node), &vep);
1260 if (ret) {
1261 of_node_put(node);
1262 return ret;
1263 }
1264
1265 priv->csi2_data_lanes =
1266 vep.bus.mipi_csi2.num_data_lanes;
1267
1268 continue;
1269 }
1270
1271 /* Skip if the corresponding GMSL link is unavailable. */
1272 if (!(i2c_mux_mask & BIT(ep.port)))
1273 continue;
1274
1275 if (priv->sources[ep.port].fwnode) {
1276 dev_err(dev,
1277 "Multiple port endpoints are not supported: %d",
1278 ep.port);
1279
1280 continue;
1281 }
1282
1283 source = &priv->sources[ep.port];
1284 source->fwnode = fwnode_graph_get_remote_endpoint(
1285 of_fwnode_handle(node));
1286 if (!source->fwnode) {
1287 dev_err(dev,
1288 "Endpoint %pOF has no remote endpoint connection\n",
1289 ep.local_node);
1290
1291 continue;
1292 }
1293
1294 priv->source_mask |= BIT(ep.port);
1295 priv->nsources++;
1296 }
1297
1298 /*
1299 * Parse the initial value of the reverse channel amplitude from
1300 * the firmware interface and convert it to millivolts.
1301 *
1302 * Default it to 170mV for backward compatibility with DTBs that do not
1303 * provide the property.
1304 */
1305 if (of_property_read_u32(dev->of_node,
1306 "maxim,reverse-channel-microvolt",
1307 &reverse_channel_microvolt))
1308 priv->init_rev_chan_mv = 170;
1309 else
1310 priv->init_rev_chan_mv = reverse_channel_microvolt / 1000U;
1311
1312 priv->route_mask = priv->source_mask;
1313
1314 return 0;
1315 }
1316
max9286_probe(struct i2c_client * client)1317 static int max9286_probe(struct i2c_client *client)
1318 {
1319 struct max9286_priv *priv;
1320 int ret;
1321
1322 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
1323 if (!priv)
1324 return -ENOMEM;
1325
1326 mutex_init(&priv->mutex);
1327
1328 priv->client = client;
1329
1330 priv->gpiod_pwdn = devm_gpiod_get_optional(&client->dev, "enable",
1331 GPIOD_OUT_HIGH);
1332 if (IS_ERR(priv->gpiod_pwdn))
1333 return PTR_ERR(priv->gpiod_pwdn);
1334
1335 gpiod_set_consumer_name(priv->gpiod_pwdn, "max9286-pwdn");
1336 gpiod_set_value_cansleep(priv->gpiod_pwdn, 1);
1337
1338 /* Wait at least 4ms before the I2C lines latch to the address */
1339 if (priv->gpiod_pwdn)
1340 usleep_range(4000, 5000);
1341
1342 /*
1343 * The MAX9286 starts by default with all ports enabled, we disable all
1344 * ports early to ensure that all channels are disabled if we error out
1345 * and keep the bus consistent.
1346 */
1347 max9286_i2c_mux_close(priv);
1348
1349 /*
1350 * The MAX9286 initialises with auto-acknowledge enabled by default.
1351 * This can be invasive to other transactions on the same bus, so
1352 * disable it early. It will be enabled only as and when needed.
1353 */
1354 max9286_configure_i2c(priv, false);
1355
1356 ret = max9286_parse_gpios(priv);
1357 if (ret)
1358 goto err_powerdown;
1359
1360 ret = max9286_parse_dt(priv);
1361 if (ret)
1362 goto err_powerdown;
1363
1364 ret = max9286_init(priv);
1365 if (ret < 0)
1366 goto err_cleanup_dt;
1367
1368 return 0;
1369
1370 err_cleanup_dt:
1371 max9286_cleanup_dt(priv);
1372 err_powerdown:
1373 gpiod_set_value_cansleep(priv->gpiod_pwdn, 0);
1374
1375 return ret;
1376 }
1377
max9286_remove(struct i2c_client * client)1378 static int max9286_remove(struct i2c_client *client)
1379 {
1380 struct max9286_priv *priv = sd_to_max9286(i2c_get_clientdata(client));
1381
1382 i2c_mux_del_adapters(priv->mux);
1383
1384 max9286_v4l2_unregister(priv);
1385
1386 max9286_poc_enable(priv, false);
1387
1388 gpiod_set_value_cansleep(priv->gpiod_pwdn, 0);
1389
1390 max9286_cleanup_dt(priv);
1391
1392 return 0;
1393 }
1394
1395 static const struct of_device_id max9286_dt_ids[] = {
1396 { .compatible = "maxim,max9286" },
1397 {},
1398 };
1399 MODULE_DEVICE_TABLE(of, max9286_dt_ids);
1400
1401 static struct i2c_driver max9286_i2c_driver = {
1402 .driver = {
1403 .name = "max9286",
1404 .of_match_table = of_match_ptr(max9286_dt_ids),
1405 },
1406 .probe_new = max9286_probe,
1407 .remove = max9286_remove,
1408 };
1409
1410 module_i2c_driver(max9286_i2c_driver);
1411
1412 MODULE_DESCRIPTION("Maxim MAX9286 GMSL Deserializer Driver");
1413 MODULE_AUTHOR("Jacopo Mondi, Kieran Bingham, Laurent Pinchart, Niklas Söderlund, Vladimir Barinov");
1414 MODULE_LICENSE("GPL");
1415