From 0c1a3735850b915cc2326e3ad4fa7e73c19ff0dc Mon Sep 17 00:00:00 2001 From: Dave Stevenson Date: Mon, 13 Feb 2017 11:10:50 +0000 Subject: [PATCH 1/3] BCM2835-V4L2: Ensure H264 header bytes get a sensible timestamp H264 header come off VC with 0 timestamps, which means they get a strange timestamp when processed with VC/kernel start times, particularly if used with the inline header option. Remember the last frame timestamp and use that if set, or otherwise use the kernel start time. https://github.com/raspberrypi/linux/issues/1836 Signed-off-by: Dave Stevenson --- .../media/platform/bcm2835/bcm2835-camera.c | 30 +++++++++++++++++-- .../media/platform/bcm2835/bcm2835-camera.h | 2 ++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/drivers/media/platform/bcm2835/bcm2835-camera.c b/drivers/media/platform/bcm2835/bcm2835-camera.c index 4f03949aecf3af..e69731320f4e59 100644 --- a/drivers/media/platform/bcm2835/bcm2835-camera.c +++ b/drivers/media/platform/bcm2835/bcm2835-camera.c @@ -356,8 +356,13 @@ static void buffer_cb(struct vchiq_mmal_instance *instance, } } else { if (dev->capture.frame_count) { - if (dev->capture.vc_start_timestamp != -1 && - pts != 0) { + if (dev->capture.vc_start_timestamp == -1) { + buf->vb.vb2_buf.timestamp = ktime_get_ns(); + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, + "Buffer time set as current time - %lld", + buf->vb.vb2_buf.timestamp); + + } else if(pts != 0) { struct timeval timestamp; s64 runtime_us = pts - dev->capture.vc_start_timestamp; @@ -390,10 +395,27 @@ static void buffer_cb(struct vchiq_mmal_instance *instance, buf->vb.vb2_buf.timestamp = timestamp.tv_sec * 1000000000ULL + timestamp.tv_usec * 1000ULL; } else { - buf->vb.vb2_buf.timestamp = ktime_get_ns(); + if (dev->capture.last_timestamp) { + buf->vb.vb2_buf.timestamp = dev->capture.last_timestamp; + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, + "Buffer time set as last timestamp - %lld", + buf->vb.vb2_buf.timestamp); + } + else { + buf->vb.vb2_buf.timestamp = + dev->capture.kernel_start_ts.tv_sec * 1000000000ULL + + dev->capture.kernel_start_ts.tv_usec * 1000ULL; + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, + "Buffer time set as start timestamp - %lld", + buf->vb.vb2_buf.timestamp); + } } + dev->capture.last_timestamp = buf->vb.vb2_buf.timestamp; vb2_set_plane_payload(&buf->vb.vb2_buf, 0, length); + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, + "Buffer has ts %llu", + dev->capture.last_timestamp); vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE); if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS && @@ -559,6 +581,8 @@ static int start_streaming(struct vb2_queue *vq, unsigned int count) "Start time %lld size %d\n", dev->capture.vc_start_timestamp, parameter_size); + dev->capture.last_timestamp = 0; + v4l2_get_timestamp(&dev->capture.kernel_start_ts); /* enable the camera port */ diff --git a/drivers/media/platform/bcm2835/bcm2835-camera.h b/drivers/media/platform/bcm2835/bcm2835-camera.h index e6aeb7e7e381de..7f8a68916a6700 100644 --- a/drivers/media/platform/bcm2835/bcm2835-camera.h +++ b/drivers/media/platform/bcm2835/bcm2835-camera.h @@ -93,6 +93,8 @@ struct bm2835_mmal_dev { s64 vc_start_timestamp; /* Kernel start timestamp for streaming */ struct timeval kernel_start_ts; + /* Timestamp of last frame */ + u64 last_timestamp; struct vchiq_mmal_port *port; /* port being used for capture */ /* camera port being used for capture */ From d62983d3cb27080c65b7c3f8c1d3c2d5359dae69 Mon Sep 17 00:00:00 2001 From: Dave Stevenson Date: Mon, 13 Feb 2017 13:11:41 +0000 Subject: [PATCH 2/3] BCM2835-V4L2: Correctly denote key frames in encoded data Forward MMAL key frame flags to the V4L2 buffers. Signed-off-by: Dave Stevenson --- drivers/media/platform/bcm2835/bcm2835-camera.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/media/platform/bcm2835/bcm2835-camera.c b/drivers/media/platform/bcm2835/bcm2835-camera.c index e69731320f4e59..6bdec080612604 100644 --- a/drivers/media/platform/bcm2835/bcm2835-camera.c +++ b/drivers/media/platform/bcm2835/bcm2835-camera.c @@ -413,6 +413,9 @@ static void buffer_cb(struct vchiq_mmal_instance *instance, dev->capture.last_timestamp = buf->vb.vb2_buf.timestamp; vb2_set_plane_payload(&buf->vb.vb2_buf, 0, length); + if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_KEYFRAME) + buf->vb.flags |= V4L2_BUF_FLAG_KEYFRAME; + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "Buffer has ts %llu", dev->capture.last_timestamp); From 3c9542da8f091c0653db9c42ebffd18a14e6e8d7 Mon Sep 17 00:00:00 2001 From: Dave Stevenson Date: Mon, 20 Feb 2017 17:01:21 +0000 Subject: [PATCH 3/3] bcm2835-gpio-exp: Driver for GPIO expander via mailbox service Pi3 and Compute Module 3 have a GPIO expander that the VPU communicates with. There is a mailbox service that now allows control of this expander, so add a kernel driver that can make use of it. Pwr_led node added to device-tree for Pi3. Signed-off-by: Dave Stevenson --- arch/arm/boot/dts/bcm2710-rpi-3-b.dts | 22 ++ arch/arm/boot/dts/bcm2710-rpi-cm3.dts | 10 +- arch/arm/configs/bcm2709_defconfig | 1 + drivers/gpio/Kconfig | 7 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-bcm-exp.c | 256 +++++++++++++++++++++ include/soc/bcm2835/raspberrypi-firmware.h | 4 + 7 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 drivers/gpio/gpio-bcm-exp.c diff --git a/arch/arm/boot/dts/bcm2710-rpi-3-b.dts b/arch/arm/boot/dts/bcm2710-rpi-3-b.dts index 78101849441679..173103aaca5038 100644 --- a/arch/arm/boot/dts/bcm2710-rpi-3-b.dts +++ b/arch/arm/boot/dts/bcm2710-rpi-3-b.dts @@ -96,6 +96,14 @@ firmware = <&firmware>; status = "okay"; }; + + expgpio: expgpio { + compatible = "brcm,bcm2835-expgpio"; + gpio-controller; + #gpio-cells = <2>; + firmware = <&firmware>; + status = "okay"; + }; }; &fb { @@ -163,6 +171,16 @@ linux,default-trigger = "mmc0"; gpios = <&virtgpio 0 0>; }; + + pwr_led: pwr { + label = "led1"; + linux,default-trigger = "input"; + gpios = <&expgpio 7 GPIO_ACTIVE_LOW>; + }; +}; + +&hdmi { + hpd-gpios = <&expgpio 4 GPIO_ACTIVE_LOW>; }; &audio { @@ -193,6 +211,10 @@ act_led_activelow = <&act_led>,"gpios:8"; act_led_trigger = <&act_led>,"linux,default-trigger"; + pwr_led_gpio = <&pwr_led>,"gpios:4"; + pwr_led_activelow = <&pwr_led>,"gpios:8"; + pwr_led_trigger = <&pwr_led>,"linux,default-trigger"; + audio = <&audio>,"status"; watchdog = <&watchdog>,"status"; random = <&random>,"status"; diff --git a/arch/arm/boot/dts/bcm2710-rpi-cm3.dts b/arch/arm/boot/dts/bcm2710-rpi-cm3.dts index 3ba6e621856c28..fe402e84cdda88 100644 --- a/arch/arm/boot/dts/bcm2710-rpi-cm3.dts +++ b/arch/arm/boot/dts/bcm2710-rpi-cm3.dts @@ -65,6 +65,14 @@ firmware = <&firmware>; status = "okay"; }; + + expgpio: expgpio { + compatible = "brcm,bcm2835-expgpio"; + gpio-controller; + #gpio-cells = <2>; + firmware = <&firmware>; + status = "okay"; + }; }; &fb { @@ -123,7 +131,7 @@ }; &hdmi { - hpd-gpios = <&gpio 46 GPIO_ACTIVE_LOW>; + hpd-gpios = <&expgpio 0 GPIO_ACTIVE_LOW>; }; &audio { diff --git a/arch/arm/configs/bcm2709_defconfig b/arch/arm/configs/bcm2709_defconfig index 669edd7544d798..764402d236a688 100644 --- a/arch/arm/configs/bcm2709_defconfig +++ b/arch/arm/configs/bcm2709_defconfig @@ -624,6 +624,7 @@ CONFIG_PPS=m CONFIG_PPS_CLIENT_LDISC=m CONFIG_PPS_CLIENT_GPIO=m CONFIG_GPIO_SYSFS=y +CONFIG_GPIO_BCM_EXP=y CONFIG_GPIO_BCM_VIRT=y CONFIG_GPIO_ARIZONA=m CONFIG_GPIO_STMPE=y diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index edcb49f9051b70..6c92504a7d2931 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -128,6 +128,13 @@ config GPIO_AXP209 help Say yes to enable GPIO support for the AXP209 PMIC +config GPIO_BCM_EXP + bool "Broadcom Exp GPIO" + depends on OF_GPIO && RASPBERRYPI_FIRMWARE && (ARCH_BCM2835 || COMPILE_TEST) + help + Turn on GPIO support for Broadcom chips using the firmware mailbox + to communicate with VideoCore on BCM283x chips. + config GPIO_BCM_KONA bool "Broadcom Kona GPIO" depends on OF_GPIO && (ARCH_BCM_MOBILE || COMPILE_TEST) diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index cfb8d4bad6b0b2..8165a63b82a9be 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -30,6 +30,7 @@ obj-$(CONFIG_GPIO_ARIZONA) += gpio-arizona.o obj-$(CONFIG_GPIO_ATH79) += gpio-ath79.o obj-$(CONFIG_GPIO_ASPEED) += gpio-aspeed.o obj-$(CONFIG_GPIO_AXP209) += gpio-axp209.o +obj-$(CONFIG_GPIO_BCM_EXP) += gpio-bcm-exp.o obj-$(CONFIG_GPIO_BCM_KONA) += gpio-bcm-kona.o obj-$(CONFIG_GPIO_BCM_VIRT) += gpio-bcm-virt.o obj-$(CONFIG_GPIO_BRCMSTB) += gpio-brcmstb.o diff --git a/drivers/gpio/gpio-bcm-exp.c b/drivers/gpio/gpio-bcm-exp.c new file mode 100644 index 00000000000000..681a91492d4c33 --- /dev/null +++ b/drivers/gpio/gpio-bcm-exp.c @@ -0,0 +1,256 @@ +/* + * Broadcom expander GPIO driver + * + * Uses the firmware mailbox service to communicate with the + * GPIO expander on the VPU. + * + * Copyright (C) 2017 Raspberry Pi Trading Ltd. + * + * Author: Dave Stevenson + * Based on gpio-bcm-virt.c by Dom Cobley + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#include +#include +#include +#include +#include +#include + +#define MODULE_NAME "brcmexp-gpio" +#define NUM_GPIO 8 + +struct brcmexp_gpio { + struct gpio_chip gc; + struct device *dev; + struct rpi_firmware *fw; +}; + +struct gpio_set_config { + u32 gpio, direction, polarity, term_en, term_pull_up, state; +}; + +struct gpio_get_config { + u32 gpio, direction, polarity, term_en, term_pull_up; +}; + +struct gpio_get_set_state { + u32 gpio, state; +}; + +static int brcmexp_gpio_get_polarity(struct gpio_chip *gc, unsigned int off) +{ + struct brcmexp_gpio *gpio; + struct gpio_get_config get; + int ret; + + gpio = container_of(gc, struct brcmexp_gpio, gc); + + get.gpio = off + gpio->gc.base; /* GPIO to update */ + + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_CONFIG, + &get, sizeof(get)); + if (ret) { + dev_err(gpio->dev, + "Failed to get GPIO %u config (%d)\n", off, ret); + return ret; + } + return get.polarity; +} + +static int brcmexp_gpio_dir_in(struct gpio_chip *gc, unsigned int off) +{ + struct brcmexp_gpio *gpio; + struct gpio_set_config set_in; + int ret; + + gpio = container_of(gc, struct brcmexp_gpio, gc); + + set_in.gpio = off + gpio->gc.base; /* GPIO to update */ + set_in.direction = 0; /* Input */ + set_in.polarity = brcmexp_gpio_get_polarity(gc, off); + /* Retain existing setting */ + set_in.term_en = 0; /* termination disabled */ + set_in.term_pull_up = 0; /* n/a as termination disabled */ + set_in.state = 0; /* n/a as configured as an input */ + + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_CONFIG, + &set_in, sizeof(set_in)); + if (ret) { + dev_err(gpio->dev, + "Failed to set GPIO %u to input (%d)\n", + off, ret); + return ret; + } + return 0; +} + +static int brcmexp_gpio_dir_out(struct gpio_chip *gc, unsigned int off, int val) +{ + struct brcmexp_gpio *gpio; + struct gpio_set_config set_out; + int ret; + + gpio = container_of(gc, struct brcmexp_gpio, gc); + + set_out.gpio = off + gpio->gc.base; /* GPIO to update */ + set_out.direction = 1; /* Output */ + set_out.polarity = brcmexp_gpio_get_polarity(gc, off); + /* Retain existing setting */ + set_out.term_en = 0; /* n/a as an output */ + set_out.term_pull_up = 0; /* n/a as termination disabled */ + set_out.state = val; /* Output state */ + + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_CONFIG, + &set_out, sizeof(set_out)); + if (ret) { + dev_err(gpio->dev, + "Failed to set GPIO %u to output (%d)\n", off, ret); + return ret; + } + return 0; +} + +static int brcmexp_gpio_get_direction(struct gpio_chip *gc, unsigned int off) +{ + struct brcmexp_gpio *gpio; + struct gpio_get_config get; + int ret; + + gpio = container_of(gc, struct brcmexp_gpio, gc); + + get.gpio = off + gpio->gc.base; /* GPIO to update */ + + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_CONFIG, + &get, sizeof(get)); + if (ret) { + dev_err(gpio->dev, + "Failed to get GPIO %u config (%d)\n", off, ret); + return ret; + } + return get.direction ? GPIOF_DIR_OUT : GPIOF_DIR_IN; +} + +static int brcmexp_gpio_get(struct gpio_chip *gc, unsigned int off) +{ + struct brcmexp_gpio *gpio; + struct gpio_get_set_state get; + int ret; + + gpio = container_of(gc, struct brcmexp_gpio, gc); + + get.gpio = off + gpio->gc.base; /* GPIO to update */ + get.state = 0; /* storage for returned value */ + + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_STATE, + &get, sizeof(get)); + if (ret) { + dev_err(gpio->dev, + "Failed to get GPIO %u state (%d)\n", off, ret); + return ret; + } + return !!get.state; +} + +static void brcmexp_gpio_set(struct gpio_chip *gc, unsigned int off, int val) +{ + struct brcmexp_gpio *gpio; + struct gpio_get_set_state set; + int ret; + + gpio = container_of(gc, struct brcmexp_gpio, gc); + + off += gpio->gc.base; + + set.gpio = off + gpio->gc.base; /* GPIO to update */ + set.state = val; /* Output state */ + + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_STATE, + &set, sizeof(set)); + if (ret) + dev_err(gpio->dev, + "Failed to set GPIO %u state (%d)\n", off, ret); +} + +static int brcmexp_gpio_probe(struct platform_device *pdev) +{ + int err = 0; + struct device *dev = &pdev->dev; + struct device_node *np = dev->of_node; + struct device_node *fw_node; + struct rpi_firmware *fw; + struct brcmexp_gpio *ucb; + + fw_node = of_parse_phandle(np, "firmware", 0); + if (!fw_node) { + dev_err(dev, "Missing firmware node\n"); + return -ENOENT; + } + + fw = rpi_firmware_get(fw_node); + if (!fw) + return -EPROBE_DEFER; + + ucb = devm_kzalloc(dev, sizeof(*ucb), GFP_KERNEL); + if (!ucb) + return -EINVAL; + + ucb->fw = fw; + ucb->dev = dev; + ucb->gc.label = MODULE_NAME; + ucb->gc.owner = THIS_MODULE; + ucb->gc.of_node = np; + ucb->gc.base = 128; + ucb->gc.ngpio = NUM_GPIO; + + ucb->gc.direction_input = brcmexp_gpio_dir_in; + ucb->gc.direction_output = brcmexp_gpio_dir_out; + ucb->gc.get_direction = brcmexp_gpio_get_direction; + ucb->gc.get = brcmexp_gpio_get; + ucb->gc.set = brcmexp_gpio_set; + ucb->gc.can_sleep = true; + + err = gpiochip_add(&ucb->gc); + if (err) + return err; + + platform_set_drvdata(pdev, ucb); + + return 0; +} + +static int brcmexp_gpio_remove(struct platform_device *pdev) +{ + struct brcmexp_gpio *ucb = platform_get_drvdata(pdev); + + gpiochip_remove(&ucb->gc); + + return 0; +} + +static const struct of_device_id __maybe_unused brcmexp_gpio_ids[] = { + { .compatible = "brcm,bcm2835-expgpio" }, + { } +}; +MODULE_DEVICE_TABLE(of, brcmexp_gpio_ids); + +static struct platform_driver brcmexp_gpio_driver = { + .driver = { + .name = MODULE_NAME, + .owner = THIS_MODULE, + .of_match_table = of_match_ptr(brcmexp_gpio_ids), + }, + .probe = brcmexp_gpio_probe, + .remove = brcmexp_gpio_remove, +}; +module_platform_driver(brcmexp_gpio_driver); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Dave Stevenson "); +MODULE_DESCRIPTION("brcm-exp GPIO driver"); +MODULE_ALIAS("platform:brcmexp-gpio"); diff --git a/include/soc/bcm2835/raspberrypi-firmware.h b/include/soc/bcm2835/raspberrypi-firmware.h index 2859db09e25bb9..56b3f0fe1ea3d2 100644 --- a/include/soc/bcm2835/raspberrypi-firmware.h +++ b/include/soc/bcm2835/raspberrypi-firmware.h @@ -83,7 +83,11 @@ enum rpi_firmware_property_tag { RPI_FIRMWARE_SET_TURBO = 0x00038009, RPI_FIRMWARE_SET_CUSTOMER_OTP = 0x00038021, RPI_FIRMWARE_SET_DOMAIN_STATE = 0x00038030, + RPI_FIRMWARE_GET_GPIO_STATE = 0x00030041, + RPI_FIRMWARE_SET_GPIO_STATE = 0x00038041, RPI_FIRMWARE_SET_SDHOST_CLOCK = 0x00038042, + RPI_FIRMWARE_GET_GPIO_CONFIG = 0x00030043, + RPI_FIRMWARE_SET_GPIO_CONFIG = 0x00038043, /* Dispmanx TAGS */ RPI_FIRMWARE_FRAMEBUFFER_ALLOCATE = 0x00040001,