1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2021-2023 Digiteq Automotive
6 * This module handles all the sysfs info/configuration that is related to the
10 #include <linux/device.h>
11 #include <linux/nospec.h>
12 #include "mgb4_core.h"
14 #include "mgb4_vout.h"
17 #include "mgb4_sysfs.h"
19 static int loopin_cnt(struct mgb4_vin_dev *vindev)
21 struct mgb4_vout_dev *voutdev;
25 for (i = 0; i < MGB4_VOUT_DEVICES; i++) {
26 voutdev = vindev->mgbdev->vout[i];
30 config = mgb4_read_reg(&voutdev->mgbdev->video,
31 voutdev->config->regs.config);
32 if ((config & 0xc) >> 2 == vindev->config->id)
39 static bool is_busy(struct video_device *dev)
43 mutex_lock(dev->lock);
44 ret = vb2_is_busy(dev->queue);
45 mutex_unlock(dev->lock);
50 /* Common for both FPDL3 and GMSL */
52 static ssize_t output_id_show(struct device *dev,
53 struct device_attribute *attr, char *buf)
55 struct video_device *vdev = to_video_device(dev);
56 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
58 return sprintf(buf, "%d\n", voutdev->config->id);
61 static ssize_t video_source_show(struct device *dev,
62 struct device_attribute *attr, char *buf)
64 struct video_device *vdev = to_video_device(dev);
65 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
66 u32 config = mgb4_read_reg(&voutdev->mgbdev->video,
67 voutdev->config->regs.config);
69 return sprintf(buf, "%u\n", (config & 0xc) >> 2);
73 * Video source change may affect the buffer queue of ANY video input/output on
74 * the card thus if any of the inputs/outputs is in use, we do not allow
77 * As we do not want to lock all the video devices at the same time, a two-stage
78 * locking strategy is used. In addition to the video device locking there is
79 * a global (PCI device) variable "io_reconfig" atomically checked/set when
80 * the reconfiguration is running. All the video devices check the variable in
81 * their queue_setup() functions and do not allow to start the queue when
82 * the reconfiguration has started.
84 static ssize_t video_source_store(struct device *dev,
85 struct device_attribute *attr,
86 const char *buf, size_t count)
88 struct video_device *vdev = to_video_device(dev);
89 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
90 struct mgb4_dev *mgbdev = voutdev->mgbdev;
91 struct mgb4_vin_dev *loopin_new = NULL, *loopin_old = NULL;
97 ret = kstrtoul(buf, 10, &val);
103 if (test_and_set_bit(0, &mgbdev->io_reconfig))
107 for (i = 0; i < MGB4_VIN_DEVICES; i++)
108 if (mgbdev->vin[i] && is_busy(&mgbdev->vin[i]->vdev))
110 for (i = 0; i < MGB4_VOUT_DEVICES; i++)
111 if (mgbdev->vout[i] && is_busy(&mgbdev->vout[i]->vdev))
114 config = mgb4_read_reg(&mgbdev->video, voutdev->config->regs.config);
116 if (((config & 0xc) >> 2) < MGB4_VIN_DEVICES)
117 loopin_old = mgbdev->vin[(config & 0xc) >> 2];
118 if (val < MGB4_VIN_DEVICES) {
119 val = array_index_nospec(val, MGB4_VIN_DEVICES);
120 loopin_new = mgbdev->vin[val];
122 if (loopin_old && loopin_cnt(loopin_old) == 1)
123 mgb4_mask_reg(&mgbdev->video, loopin_old->config->regs.config,
126 mgb4_mask_reg(&mgbdev->video, loopin_new->config->regs.config,
129 if (val == voutdev->config->id + MGB4_VIN_DEVICES)
130 mgb4_write_reg(&mgbdev->video, voutdev->config->regs.config,
133 mgb4_write_reg(&mgbdev->video, voutdev->config->regs.config,
136 mgb4_mask_reg(&mgbdev->video, voutdev->config->regs.config, 0xc,
141 clear_bit(0, &mgbdev->io_reconfig);
146 static ssize_t display_width_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
149 struct video_device *vdev = to_video_device(dev);
150 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
151 u32 config = mgb4_read_reg(&voutdev->mgbdev->video,
152 voutdev->config->regs.resolution);
154 return sprintf(buf, "%u\n", config >> 16);
157 static ssize_t display_width_store(struct device *dev,
158 struct device_attribute *attr,
159 const char *buf, size_t count)
161 struct video_device *vdev = to_video_device(dev);
162 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
166 ret = kstrtoul(buf, 10, &val);
172 mutex_lock(voutdev->vdev.lock);
173 if (vb2_is_busy(voutdev->vdev.queue)) {
174 mutex_unlock(voutdev->vdev.lock);
178 mgb4_mask_reg(&voutdev->mgbdev->video, voutdev->config->regs.resolution,
179 0xFFFF0000, val << 16);
181 mutex_unlock(voutdev->vdev.lock);
186 static ssize_t display_height_show(struct device *dev,
187 struct device_attribute *attr, char *buf)
189 struct video_device *vdev = to_video_device(dev);
190 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
191 u32 config = mgb4_read_reg(&voutdev->mgbdev->video,
192 voutdev->config->regs.resolution);
194 return sprintf(buf, "%u\n", config & 0xFFFF);
197 static ssize_t display_height_store(struct device *dev,
198 struct device_attribute *attr,
199 const char *buf, size_t count)
201 struct video_device *vdev = to_video_device(dev);
202 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
206 ret = kstrtoul(buf, 10, &val);
212 mutex_lock(voutdev->vdev.lock);
213 if (vb2_is_busy(voutdev->vdev.queue)) {
214 mutex_unlock(voutdev->vdev.lock);
218 mgb4_mask_reg(&voutdev->mgbdev->video, voutdev->config->regs.resolution,
221 mutex_unlock(voutdev->vdev.lock);
226 static ssize_t frame_rate_show(struct device *dev,
227 struct device_attribute *attr, char *buf)
229 struct video_device *vdev = to_video_device(dev);
230 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
231 u32 period = mgb4_read_reg(&voutdev->mgbdev->video,
232 voutdev->config->regs.frame_limit);
234 return sprintf(buf, "%u\n", period ? MGB4_HW_FREQ / period : 0);
238 * Frame rate change is expected to be called on live streams. Video device
239 * locking/queue check is not needed.
241 static ssize_t frame_rate_store(struct device *dev,
242 struct device_attribute *attr, const char *buf,
245 struct video_device *vdev = to_video_device(dev);
246 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
250 ret = kstrtoul(buf, 10, &val);
254 limit = val ? MGB4_HW_FREQ / val : 0;
255 mgb4_write_reg(&voutdev->mgbdev->video,
256 voutdev->config->regs.frame_limit, limit);
261 static ssize_t hsync_width_show(struct device *dev,
262 struct device_attribute *attr, char *buf)
264 struct video_device *vdev = to_video_device(dev);
265 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
266 u32 sig = mgb4_read_reg(&voutdev->mgbdev->video,
267 voutdev->config->regs.hsync);
269 return sprintf(buf, "%u\n", (sig & 0x00FF0000) >> 16);
273 * HSYNC width change is expected to be called on live streams. Video device
274 * locking/queue check is not needed.
276 static ssize_t hsync_width_store(struct device *dev,
277 struct device_attribute *attr, const char *buf,
280 struct video_device *vdev = to_video_device(dev);
281 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
285 ret = kstrtoul(buf, 10, &val);
291 mgb4_mask_reg(&voutdev->mgbdev->video, voutdev->config->regs.hsync,
292 0x00FF0000, val << 16);
297 static ssize_t vsync_width_show(struct device *dev,
298 struct device_attribute *attr, char *buf)
300 struct video_device *vdev = to_video_device(dev);
301 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
302 u32 sig = mgb4_read_reg(&voutdev->mgbdev->video,
303 voutdev->config->regs.vsync);
305 return sprintf(buf, "%u\n", (sig & 0x00FF0000) >> 16);
309 * VSYNC vidth change is expected to be called on live streams. Video device
310 * locking/queue check is not needed.
312 static ssize_t vsync_width_store(struct device *dev,
313 struct device_attribute *attr, const char *buf,
316 struct video_device *vdev = to_video_device(dev);
317 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
321 ret = kstrtoul(buf, 10, &val);
327 mgb4_mask_reg(&voutdev->mgbdev->video, voutdev->config->regs.vsync,
328 0x00FF0000, val << 16);
333 static ssize_t hback_porch_show(struct device *dev,
334 struct device_attribute *attr, char *buf)
336 struct video_device *vdev = to_video_device(dev);
337 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
338 u32 sig = mgb4_read_reg(&voutdev->mgbdev->video,
339 voutdev->config->regs.hsync);
341 return sprintf(buf, "%u\n", (sig & 0x0000FF00) >> 8);
345 * hback porch change is expected to be called on live streams. Video device
346 * locking/queue check is not needed.
348 static ssize_t hback_porch_store(struct device *dev,
349 struct device_attribute *attr, const char *buf,
352 struct video_device *vdev = to_video_device(dev);
353 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
357 ret = kstrtoul(buf, 10, &val);
363 mgb4_mask_reg(&voutdev->mgbdev->video, voutdev->config->regs.hsync,
364 0x0000FF00, val << 8);
369 static ssize_t vback_porch_show(struct device *dev,
370 struct device_attribute *attr, char *buf)
372 struct video_device *vdev = to_video_device(dev);
373 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
374 u32 sig = mgb4_read_reg(&voutdev->mgbdev->video,
375 voutdev->config->regs.vsync);
377 return sprintf(buf, "%u\n", (sig & 0x0000FF00) >> 8);
381 * vback porch change is expected to be called on live streams. Video device
382 * locking/queue check is not needed.
384 static ssize_t vback_porch_store(struct device *dev,
385 struct device_attribute *attr, const char *buf,
388 struct video_device *vdev = to_video_device(dev);
389 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
393 ret = kstrtoul(buf, 10, &val);
399 mgb4_mask_reg(&voutdev->mgbdev->video, voutdev->config->regs.vsync,
400 0x0000FF00, val << 8);
405 static ssize_t hfront_porch_show(struct device *dev,
406 struct device_attribute *attr, char *buf)
408 struct video_device *vdev = to_video_device(dev);
409 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
410 u32 sig = mgb4_read_reg(&voutdev->mgbdev->video,
411 voutdev->config->regs.hsync);
413 return sprintf(buf, "%u\n", (sig & 0x000000FF));
417 * hfront porch change is expected to be called on live streams. Video device
418 * locking/queue check is not needed.
420 static ssize_t hfront_porch_store(struct device *dev,
421 struct device_attribute *attr,
422 const char *buf, size_t count)
424 struct video_device *vdev = to_video_device(dev);
425 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
429 ret = kstrtoul(buf, 10, &val);
435 mgb4_mask_reg(&voutdev->mgbdev->video, voutdev->config->regs.hsync,
441 static ssize_t vfront_porch_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
444 struct video_device *vdev = to_video_device(dev);
445 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
446 u32 sig = mgb4_read_reg(&voutdev->mgbdev->video,
447 voutdev->config->regs.vsync);
449 return sprintf(buf, "%u\n", (sig & 0x000000FF));
453 * vfront porch change is expected to be called on live streams. Video device
454 * locking/queue check is not needed.
456 static ssize_t vfront_porch_store(struct device *dev,
457 struct device_attribute *attr, const char *buf,
460 struct video_device *vdev = to_video_device(dev);
461 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
465 ret = kstrtoul(buf, 10, &val);
471 mgb4_mask_reg(&voutdev->mgbdev->video, voutdev->config->regs.vsync,
479 static ssize_t hsync_polarity_show(struct device *dev,
480 struct device_attribute *attr, char *buf)
482 struct video_device *vdev = to_video_device(dev);
483 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
484 u32 config = mgb4_read_reg(&voutdev->mgbdev->video,
485 voutdev->config->regs.hsync);
487 return sprintf(buf, "%u\n", (config & (1U << 31)) >> 31);
491 * HSYNC polarity change is expected to be called on live streams. Video device
492 * locking/queue check is not needed.
494 static ssize_t hsync_polarity_store(struct device *dev,
495 struct device_attribute *attr,
496 const char *buf, size_t count)
498 struct video_device *vdev = to_video_device(dev);
499 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
503 ret = kstrtoul(buf, 10, &val);
509 mgb4_mask_reg(&voutdev->mgbdev->video, voutdev->config->regs.hsync,
510 (1U << 31), val << 31);
515 static ssize_t vsync_polarity_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
518 struct video_device *vdev = to_video_device(dev);
519 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
520 u32 config = mgb4_read_reg(&voutdev->mgbdev->video,
521 voutdev->config->regs.vsync);
523 return sprintf(buf, "%u\n", (config & (1U << 31)) >> 31);
527 * VSYNC polarity change is expected to be called on live streams. Video device
528 * locking/queue check is not needed.
530 static ssize_t vsync_polarity_store(struct device *dev,
531 struct device_attribute *attr,
532 const char *buf, size_t count)
534 struct video_device *vdev = to_video_device(dev);
535 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
539 ret = kstrtoul(buf, 10, &val);
545 mgb4_mask_reg(&voutdev->mgbdev->video, voutdev->config->regs.vsync,
546 (1U << 31), val << 31);
551 static ssize_t de_polarity_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
554 struct video_device *vdev = to_video_device(dev);
555 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
556 u32 config = mgb4_read_reg(&voutdev->mgbdev->video,
557 voutdev->config->regs.vsync);
559 return sprintf(buf, "%u\n", (config & (1U << 30)) >> 30);
563 * DE polarity change is expected to be called on live streams. Video device
564 * locking/queue check is not needed.
566 static ssize_t de_polarity_store(struct device *dev,
567 struct device_attribute *attr, const char *buf,
570 struct video_device *vdev = to_video_device(dev);
571 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
575 ret = kstrtoul(buf, 10, &val);
581 mgb4_mask_reg(&voutdev->mgbdev->video, voutdev->config->regs.vsync,
582 (1U << 30), val << 30);
587 static ssize_t fpdl3_output_width_show(struct device *dev,
588 struct device_attribute *attr, char *buf)
590 struct video_device *vdev = to_video_device(dev);
591 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
594 mutex_lock(&voutdev->mgbdev->i2c_lock);
595 ret = mgb4_i2c_read_byte(&voutdev->ser, 0x5B);
596 mutex_unlock(&voutdev->mgbdev->i2c_lock);
600 switch ((u8)ret & 0x03) {
602 return sprintf(buf, "0\n");
604 return sprintf(buf, "1\n");
606 return sprintf(buf, "2\n");
613 * FPD-Link width change is expected to be called on live streams. Video device
614 * locking/queue check is not needed.
616 static ssize_t fpdl3_output_width_store(struct device *dev,
617 struct device_attribute *attr,
618 const char *buf, size_t count)
620 struct video_device *vdev = to_video_device(dev);
621 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
626 ret = kstrtoul(buf, 10, &val);
644 mutex_lock(&voutdev->mgbdev->i2c_lock);
645 ret = mgb4_i2c_mask_byte(&voutdev->ser, 0x5B, 0x03, i2c_data);
646 mutex_unlock(&voutdev->mgbdev->i2c_lock);
653 static ssize_t pclk_frequency_show(struct device *dev,
654 struct device_attribute *attr, char *buf)
656 struct video_device *vdev = to_video_device(dev);
657 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
659 return sprintf(buf, "%u\n", voutdev->freq);
662 static ssize_t pclk_frequency_store(struct device *dev,
663 struct device_attribute *attr,
664 const char *buf, size_t count)
666 struct video_device *vdev = to_video_device(dev);
667 struct mgb4_vout_dev *voutdev = video_get_drvdata(vdev);
672 ret = kstrtoul(buf, 10, &val);
676 mutex_lock(voutdev->vdev.lock);
677 if (vb2_is_busy(voutdev->vdev.queue)) {
678 mutex_unlock(voutdev->vdev.lock);
682 dp = (val > 50000) ? 1 : 0;
683 voutdev->freq = mgb4_cmt_set_vout_freq(voutdev, val >> dp) << dp;
685 mgb4_mask_reg(&voutdev->mgbdev->video, voutdev->config->regs.config,
687 mutex_lock(&voutdev->mgbdev->i2c_lock);
688 ret = mgb4_i2c_mask_byte(&voutdev->ser, 0x4F, 1 << 6, ((~dp) & 1) << 6);
689 mutex_unlock(&voutdev->mgbdev->i2c_lock);
691 mutex_unlock(voutdev->vdev.lock);
693 return (ret < 0) ? -EIO : count;
696 static DEVICE_ATTR_RO(output_id);
697 static DEVICE_ATTR_RW(video_source);
698 static DEVICE_ATTR_RW(display_width);
699 static DEVICE_ATTR_RW(display_height);
700 static DEVICE_ATTR_RW(frame_rate);
701 static DEVICE_ATTR_RW(hsync_polarity);
702 static DEVICE_ATTR_RW(vsync_polarity);
703 static DEVICE_ATTR_RW(de_polarity);
704 static DEVICE_ATTR_RW(pclk_frequency);
705 static DEVICE_ATTR_RW(hsync_width);
706 static DEVICE_ATTR_RW(vsync_width);
707 static DEVICE_ATTR_RW(hback_porch);
708 static DEVICE_ATTR_RW(hfront_porch);
709 static DEVICE_ATTR_RW(vback_porch);
710 static DEVICE_ATTR_RW(vfront_porch);
712 static DEVICE_ATTR_RW(fpdl3_output_width);
714 struct attribute *mgb4_fpdl3_out_attrs[] = {
715 &dev_attr_output_id.attr,
716 &dev_attr_video_source.attr,
717 &dev_attr_display_width.attr,
718 &dev_attr_display_height.attr,
719 &dev_attr_frame_rate.attr,
720 &dev_attr_hsync_polarity.attr,
721 &dev_attr_vsync_polarity.attr,
722 &dev_attr_de_polarity.attr,
723 &dev_attr_pclk_frequency.attr,
724 &dev_attr_hsync_width.attr,
725 &dev_attr_vsync_width.attr,
726 &dev_attr_hback_porch.attr,
727 &dev_attr_hfront_porch.attr,
728 &dev_attr_vback_porch.attr,
729 &dev_attr_vfront_porch.attr,
730 &dev_attr_fpdl3_output_width.attr,
734 struct attribute *mgb4_gmsl_out_attrs[] = {
735 &dev_attr_output_id.attr,
736 &dev_attr_video_source.attr,
737 &dev_attr_display_width.attr,
738 &dev_attr_display_height.attr,
739 &dev_attr_frame_rate.attr,