* [PATCH 3/4] [media] rc: gpio-ir-tx: add new driver
[not found] <cover.1498992850.git.sean@mess.org>
@ 2017-07-02 11:06 ` Sean Young
2017-07-07 13:58 ` Rob Herring
2017-07-02 11:06 ` [PATCH 4/4] [media] rc: pwm-ir-tx: " Sean Young
1 sibling, 1 reply; 7+ messages in thread
From: Sean Young @ 2017-07-02 11:06 UTC (permalink / raw)
To: linux-media, devicetree, Rob Herring
This is a simple bit-banging GPIO IR TX driver.
Signed-off-by: Sean Young <sean@mess.org>
---
.../devicetree/bindings/leds/irled/gpio-ir-tx.txt | 11 ++
drivers/media/rc/Kconfig | 11 ++
drivers/media/rc/Makefile | 1 +
drivers/media/rc/gpio-ir-tx.c | 189 +++++++++++++++++++++
4 files changed, 212 insertions(+)
create mode 100644 Documentation/devicetree/bindings/leds/irled/gpio-ir-tx.txt
create mode 100644 drivers/media/rc/gpio-ir-tx.c
diff --git a/Documentation/devicetree/bindings/leds/irled/gpio-ir-tx.txt b/Documentation/devicetree/bindings/leds/irled/gpio-ir-tx.txt
new file mode 100644
index 0000000..bf4d4fb
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/irled/gpio-ir-tx.txt
@@ -0,0 +1,11 @@
+Device tree bindings for IR LED connected through gpio pin which is used as
+IR transmitter.
+
+Required properties:
+ - compatible: should be "gpio-ir-tx".
+
+Example:
+ irled@0 {
+ compatible = "gpio-ir-tx";
+ gpios = <&gpio1 2 GPIO_ACTIVE_HIGH>;
+ };
diff --git a/drivers/media/rc/Kconfig b/drivers/media/rc/Kconfig
index 5e83b76..ad54011 100644
--- a/drivers/media/rc/Kconfig
+++ b/drivers/media/rc/Kconfig
@@ -388,6 +388,17 @@ config IR_GPIO_CIR
To compile this driver as a module, choose M here: the module will
be called gpio-ir-recv.
+config IR_GPIO_TX
+ tristate "GPIO IR bit banging transmitter"
+ depends on RC_CORE
+ depends on LIRC
+ ---help---
+ Say Y if you want to a GPIO based IR transmitter. This is a
+ bit banging driver.
+
+ To compile this driver as a module, choose M here: the module will
+ be called gpio-ir-tx.
+
config RC_ST
tristate "ST remote control receiver"
depends on RC_CORE
diff --git a/drivers/media/rc/Makefile b/drivers/media/rc/Makefile
index 245e2c2..3e64a4e 100644
--- a/drivers/media/rc/Makefile
+++ b/drivers/media/rc/Makefile
@@ -32,6 +32,7 @@ obj-$(CONFIG_IR_STREAMZAP) += streamzap.o
obj-$(CONFIG_IR_WINBOND_CIR) += winbond-cir.o
obj-$(CONFIG_RC_LOOPBACK) += rc-loopback.o
obj-$(CONFIG_IR_GPIO_CIR) += gpio-ir-recv.o
+obj-$(CONFIG_IR_GPIO_TX) += gpio-ir-tx.o
obj-$(CONFIG_IR_IGORPLUGUSB) += igorplugusb.o
obj-$(CONFIG_IR_IGUANA) += iguanair.o
obj-$(CONFIG_IR_TTUSBIR) += ttusbir.o
diff --git a/drivers/media/rc/gpio-ir-tx.c b/drivers/media/rc/gpio-ir-tx.c
new file mode 100644
index 0000000..c2ea002
--- /dev/null
+++ b/drivers/media/rc/gpio-ir-tx.c
@@ -0,0 +1,189 @@
+/*
+ * Copyright (C) 2017 Sean Young <sean@mess.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/gpio.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/of.h>
+#include <linux/of_gpio.h>
+#include <linux/platform_device.h>
+#include <media/rc-core.h>
+
+#define DRIVER_NAME "gpio-ir-tx"
+#define DEVICE_NAME "GPIO IR Bit Banging Transmitter"
+
+struct gpio_rc_dev {
+ int gpio_nr;
+ unsigned int carrier;
+ unsigned int duty_cycle;
+ bool active_low;
+ /* we need a spinlock to hold the cpu while transmitting */
+ spinlock_t lock;
+};
+
+static const struct of_device_id gpio_ir_tx_of_match[] = {
+ { .compatible = "gpio-ir-tx", },
+ { },
+};
+MODULE_DEVICE_TABLE(of, gpio_ir_tx_of_match);
+
+static int gpio_ir_tx_set_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
+{
+ struct gpio_rc_dev *gpio_dev = dev->priv;
+
+ gpio_dev->duty_cycle = duty_cycle;
+
+ return 0;
+}
+
+static int gpio_ir_tx_set_carrier(struct rc_dev *dev, u32 carrier)
+{
+ struct gpio_rc_dev *gpio_dev = dev->priv;
+
+ if (!carrier)
+ return -EINVAL;
+
+ gpio_dev->carrier = carrier;
+
+ return 0;
+}
+
+static int gpio_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
+ unsigned int count)
+{
+ struct gpio_rc_dev *gpio_dev = dev->priv;
+ unsigned long flags;
+ ktime_t edge;
+ s64 delta;
+ int i;
+ unsigned int pulse, space;
+
+ /* Ensure the dividend fits into 32 bit */
+ pulse = DIV_ROUND_CLOSEST(gpio_dev->duty_cycle * (NSEC_PER_SEC / 100),
+ gpio_dev->carrier);
+ space = DIV_ROUND_CLOSEST((100l - gpio_dev->duty_cycle) *
+ (NSEC_PER_SEC / 100), gpio_dev->carrier);
+
+ spin_lock_irqsave(&gpio_dev->lock, flags);
+
+ edge = ktime_get();
+
+ for (i = 0; i < count; i++) {
+ if (i % 2) {
+ // space
+ edge = ktime_add_us(edge, txbuf[i]);
+ delta = ktime_us_delta(edge, ktime_get());
+ if (delta > 10) {
+ spin_unlock_irqrestore(&gpio_dev->lock, flags);
+ usleep_range(delta - 10, delta + 10);
+ spin_lock_irqsave(&gpio_dev->lock, flags);
+ } else if (delta > 0) {
+ udelay(delta);
+ }
+ } else {
+ // pulse
+ ktime_t last = ktime_add_us(edge, txbuf[i]);
+
+ while (ktime_get() < last) {
+ gpio_set_value(gpio_dev->gpio_nr,
+ gpio_dev->active_low);
+ edge += pulse;
+ delta = ktime_sub(edge, ktime_get());
+ if (delta > 0)
+ ndelay(delta);
+ gpio_set_value(gpio_dev->gpio_nr,
+ !gpio_dev->active_low);
+ edge += space;
+ delta = ktime_sub(edge, ktime_get());
+ if (delta > 0)
+ ndelay(delta);
+ }
+
+ edge = last;
+ }
+ }
+
+ spin_unlock_irqrestore(&gpio_dev->lock, flags);
+
+ return count;
+}
+
+static int gpio_ir_tx_probe(struct platform_device *pdev)
+{
+ struct gpio_rc_dev *gpio_dev;
+ struct rc_dev *rcdev;
+ enum of_gpio_flags flags;
+ int rc, gpio;
+
+ gpio = of_get_gpio_flags(pdev->dev.of_node, 0, &flags);
+ if (gpio < 0) {
+ if (gpio != -EPROBE_DEFER)
+ dev_err(&pdev->dev, "Failed to get gpio flags (%d)\n",
+ gpio);
+ return -EINVAL;
+ }
+
+ gpio_dev = devm_kzalloc(&pdev->dev, sizeof(*gpio_dev), GFP_KERNEL);
+ if (!gpio_dev)
+ return -ENOMEM;
+
+ rcdev = devm_rc_allocate_device(&pdev->dev, RC_DRIVER_IR_RAW_TX);
+ if (!rcdev)
+ return -ENOMEM;
+
+ rcdev->priv = gpio_dev;
+ rcdev->driver_name = DRIVER_NAME;
+ rcdev->device_name = DEVICE_NAME;
+ rcdev->tx_ir = gpio_ir_tx;
+ rcdev->s_tx_duty_cycle = gpio_ir_tx_set_duty_cycle;
+ rcdev->s_tx_carrier = gpio_ir_tx_set_carrier;
+
+ gpio_dev->gpio_nr = gpio;
+ gpio_dev->active_low = (flags & OF_GPIO_ACTIVE_LOW) != 0;
+ spin_lock_init(&gpio_dev->lock);
+ gpio_dev->carrier = 38000;
+ gpio_dev->duty_cycle = 50;
+
+ rc = devm_gpio_request(&pdev->dev, gpio, "gpio-ir-tx");
+ if (rc < 0)
+ return rc;
+
+ rc = gpio_direction_output(gpio, !gpio_dev->active_low);
+ if (rc < 0)
+ return rc;
+
+ rc = devm_rc_register_device(&pdev->dev, rcdev);
+ if (rc < 0) {
+ dev_err(&pdev->dev, "failed to register rc device\n");
+ return rc;
+ }
+
+ platform_set_drvdata(pdev, gpio_dev);
+
+ return 0;
+}
+
+static struct platform_driver gpio_ir_tx_driver = {
+ .probe = gpio_ir_tx_probe,
+ .driver = {
+ .name = DRIVER_NAME,
+ .of_match_table = of_match_ptr(gpio_ir_tx_of_match),
+ },
+};
+module_platform_driver(gpio_ir_tx_driver);
+
+MODULE_DESCRIPTION("GPIO IR Bit Banging Transmitter");
+MODULE_AUTHOR("Sean Young <sean@mess.org>");
+MODULE_LICENSE("GPL");
--
2.9.4
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 4/4] [media] rc: pwm-ir-tx: add new driver
[not found] <cover.1498992850.git.sean@mess.org>
2017-07-02 11:06 ` [PATCH 3/4] [media] rc: gpio-ir-tx: add new driver Sean Young
@ 2017-07-02 11:06 ` Sean Young
[not found] ` <88fa0219db3388fad7bcc7b20cf30dd41e763aee.1498992850.git.sean-hENCXIMQXOg@public.gmane.org>
1 sibling, 1 reply; 7+ messages in thread
From: Sean Young @ 2017-07-02 11:06 UTC (permalink / raw)
To: linux-media, devicetree, Rob Herring
Cc: Pavel Machek, Ivaylo Dimitrov, Timo Kokkonen
This is new driver which uses pwm, so it is more power-efficient
than the bit banging gpio-ir-tx driver.
Signed-off-by: Sean Young <sean@mess.org>
---
.../devicetree/bindings/leds/irled/pwm-ir-tx.txt | 13 ++
drivers/media/rc/Kconfig | 12 ++
drivers/media/rc/Makefile | 1 +
drivers/media/rc/pwm-ir-tx.c | 165 +++++++++++++++++++++
4 files changed, 191 insertions(+)
create mode 100644 Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt
create mode 100644 drivers/media/rc/pwm-ir-tx.c
diff --git a/Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt b/Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt
new file mode 100644
index 0000000..6887a71
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt
@@ -0,0 +1,13 @@
+Device tree bindings for IR LED connected through pwm pin which is used as
+IR transmitter.
+
+Required properties:
+- compatible: should be "pwm-ir-tx".
+- pwms : PWM property to point to the PWM device (phandle)/port (id) and to
+ specify the period time to be used: <&phandle id period_ns>;
+
+Example:
+ irled {
+ compatible = "pwm-ir-tx";
+ pwms = <&pwm0 0 10000000>;
+ };
diff --git a/drivers/media/rc/Kconfig b/drivers/media/rc/Kconfig
index ad54011..c5338e3 100644
--- a/drivers/media/rc/Kconfig
+++ b/drivers/media/rc/Kconfig
@@ -399,6 +399,18 @@ config IR_GPIO_TX
To compile this driver as a module, choose M here: the module will
be called gpio-ir-tx.
+config IR_PWM_TX
+ tristate "PWM IR transmitter"
+ depends on RC_CORE
+ depends on LIRC
+ depends on PWM
+ ---help---
+ Say Y if you want to use a PWM based IR transmitter. This is
+ more power efficient than the bit banging gpio driver.
+
+ To compile this driver as a module, choose M here: the module will
+ be called pwm-ir-tx.
+
config RC_ST
tristate "ST remote control receiver"
depends on RC_CORE
diff --git a/drivers/media/rc/Makefile b/drivers/media/rc/Makefile
index 3e64a4e..466c402 100644
--- a/drivers/media/rc/Makefile
+++ b/drivers/media/rc/Makefile
@@ -33,6 +33,7 @@ obj-$(CONFIG_IR_WINBOND_CIR) += winbond-cir.o
obj-$(CONFIG_RC_LOOPBACK) += rc-loopback.o
obj-$(CONFIG_IR_GPIO_CIR) += gpio-ir-recv.o
obj-$(CONFIG_IR_GPIO_TX) += gpio-ir-tx.o
+obj-$(CONFIG_IR_PWM_TX) += pwm-ir-tx.o
obj-$(CONFIG_IR_IGORPLUGUSB) += igorplugusb.o
obj-$(CONFIG_IR_IGUANA) += iguanair.o
obj-$(CONFIG_IR_TTUSBIR) += ttusbir.o
diff --git a/drivers/media/rc/pwm-ir-tx.c b/drivers/media/rc/pwm-ir-tx.c
new file mode 100644
index 0000000..1ab10b4
--- /dev/null
+++ b/drivers/media/rc/pwm-ir-tx.c
@@ -0,0 +1,165 @@
+/*
+ * Copyright (C) 2017 Sean Young <sean@mess.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/pwm.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <media/rc-core.h>
+
+#define DRIVER_NAME "pwm-ir-tx"
+#define DEVICE_NAME "PWM IR Transmitter"
+
+struct pwm_ir {
+ struct pwm_device *pwm;
+ unsigned int carrier;
+ unsigned int duty_cycle;
+ /* One transmission at a time */
+ struct mutex lock;
+};
+
+static const struct of_device_id pwm_ir_of_match[] = {
+ { .compatible = "pwm-ir-tx", },
+ { },
+};
+MODULE_DEVICE_TABLE(of, pwm_ir_of_match);
+
+static int pwm_ir_set_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
+{
+ struct pwm_ir *pwm_ir = dev->priv;
+
+ pwm_ir->duty_cycle = duty_cycle;
+
+ return 0;
+}
+
+static int pwm_ir_set_carrier(struct rc_dev *dev, u32 carrier)
+{
+ struct pwm_ir *pwm_ir = dev->priv;
+
+ if (!carrier)
+ return -EINVAL;
+
+ pwm_ir->carrier = carrier;
+
+ return 0;
+}
+
+static int pwm_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
+ unsigned int count)
+{
+ struct pwm_ir *pwm_ir = dev->priv;
+ struct pwm_device *pwm = pwm_ir->pwm;
+ int i, duty, period;
+ ktime_t edge;
+ s64 delta;
+
+ if (mutex_lock_interruptible(&pwm_ir->lock))
+ return -ERESTARTSYS;
+
+ period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, pwm_ir->carrier);
+ duty = DIV_ROUND_CLOSEST(pwm_ir->duty_cycle * period, 100);
+
+ pwm_config(pwm, duty, period);
+
+ edge = ktime_get();
+
+ for (i = 0; i < count; i++) {
+ if (i % 2) // space
+ pwm_disable(pwm);
+ else
+ pwm_enable(pwm);
+
+ edge = ktime_add_us(edge, txbuf[i]);
+ delta = ktime_us_delta(edge, ktime_get());
+ if (delta > 0)
+ usleep_range(delta, delta + 10);
+ }
+
+ pwm_disable(pwm);
+
+ mutex_unlock(&pwm_ir->lock);
+
+ return count;
+}
+
+static int pwm_ir_probe(struct platform_device *pdev)
+{
+ struct pwm_ir *pwm_ir;
+ struct rc_dev *rcdev;
+ int rc;
+
+ pwm_ir = devm_kzalloc(&pdev->dev, sizeof(*pwm_ir), GFP_KERNEL);
+ if (!pwm_ir)
+ return -ENOMEM;
+
+ pwm_ir->pwm = devm_pwm_get(&pdev->dev, NULL);
+ if (IS_ERR(pwm_ir->pwm))
+ return PTR_ERR(pwm_ir->pwm);
+
+ pwm_ir->carrier = 38000;
+ pwm_ir->duty_cycle = 50;
+ mutex_init(&pwm_ir->lock);
+
+ rcdev = devm_rc_allocate_device(&pdev->dev, RC_DRIVER_IR_RAW_TX);
+ if (!rcdev)
+ return -ENOMEM;
+
+ rcdev->priv = pwm_ir;
+ rcdev->driver_name = DRIVER_NAME;
+ rcdev->device_name = DEVICE_NAME;
+ rcdev->tx_ir = pwm_ir_tx;
+ rcdev->s_tx_duty_cycle = pwm_ir_set_duty_cycle;
+ rcdev->s_tx_carrier = pwm_ir_set_carrier;
+
+ rc = devm_rc_register_device(&pdev->dev, rcdev);
+ if (rc < 0) {
+ dev_err(&pdev->dev, "failed to register rc device\n");
+ return rc;
+ }
+
+ platform_set_drvdata(pdev, pwm_ir);
+
+ return 0;
+}
+
+#ifdef CONFIG_PM
+static int pwm_ir_suspend(struct platform_device *dev, pm_message_t state)
+{
+ struct pwm_ir *pwm_ir = platform_get_drvdata(dev);
+
+ if (mutex_is_locked(&pwm_ir->lock))
+ return -EAGAIN;
+
+ return 0;
+}
+#else
+#define pwm_ir_suspend NULL
+#endif
+
+static struct platform_driver pwm_ir_driver = {
+ .probe = pwm_ir_probe,
+ .suspend = pwm_ir_suspend,
+ .driver = {
+ .name = DRIVER_NAME,
+ .of_match_table = of_match_ptr(pwm_ir_of_match),
+ },
+};
+module_platform_driver(pwm_ir_driver);
+
+MODULE_DESCRIPTION("PWM IR Transmitter");
+MODULE_AUTHOR("Sean Young <sean@mess.org>");
+MODULE_LICENSE("GPL");
--
2.9.4
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 4/4] [media] rc: pwm-ir-tx: add new driver
[not found] ` <88fa0219db3388fad7bcc7b20cf30dd41e763aee.1498992850.git.sean-hENCXIMQXOg@public.gmane.org>
@ 2017-07-04 12:58 ` Sean Young
2017-07-07 13:59 ` Rob Herring
1 sibling, 0 replies; 7+ messages in thread
From: Sean Young @ 2017-07-04 12:58 UTC (permalink / raw)
To: linux-media-u79uwXL29TY76Z2rM5mHXA,
devicetree-u79uwXL29TY76Z2rM5mHXA, Rob Herring
Cc: Pavel Machek, Ivaylo Dimitrov, Timo Kokkonen
On Sun, Jul 02, 2017 at 12:06:13PM +0100, Sean Young wrote:
> This is new driver which uses pwm, so it is more power-efficient
> than the bit banging gpio-ir-tx driver.
>
> Signed-off-by: Sean Young <sean-hENCXIMQXOg@public.gmane.org>
> ---
> .../devicetree/bindings/leds/irled/pwm-ir-tx.txt | 13 ++
> drivers/media/rc/Kconfig | 12 ++
> drivers/media/rc/Makefile | 1 +
> drivers/media/rc/pwm-ir-tx.c | 165 +++++++++++++++++++++
> 4 files changed, 191 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt
> create mode 100644 drivers/media/rc/pwm-ir-tx.c
>
> diff --git a/Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt b/Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt
> new file mode 100644
> index 0000000..6887a71
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt
> @@ -0,0 +1,13 @@
> +Device tree bindings for IR LED connected through pwm pin which is used as
> +IR transmitter.
> +
> +Required properties:
> +- compatible: should be "pwm-ir-tx".
> +- pwms : PWM property to point to the PWM device (phandle)/port (id) and to
> + specify the period time to be used: <&phandle id period_ns>;
> +
> +Example:
> + irled {
> + compatible = "pwm-ir-tx";
> + pwms = <&pwm0 0 10000000>;
> + };
> diff --git a/drivers/media/rc/Kconfig b/drivers/media/rc/Kconfig
> index ad54011..c5338e3 100644
> --- a/drivers/media/rc/Kconfig
> +++ b/drivers/media/rc/Kconfig
> @@ -399,6 +399,18 @@ config IR_GPIO_TX
> To compile this driver as a module, choose M here: the module will
> be called gpio-ir-tx.
>
> +config IR_PWM_TX
> + tristate "PWM IR transmitter"
> + depends on RC_CORE
> + depends on LIRC
> + depends on PWM
> + ---help---
> + Say Y if you want to use a PWM based IR transmitter. This is
> + more power efficient than the bit banging gpio driver.
> +
> + To compile this driver as a module, choose M here: the module will
> + be called pwm-ir-tx.
> +
> config RC_ST
> tristate "ST remote control receiver"
> depends on RC_CORE
> diff --git a/drivers/media/rc/Makefile b/drivers/media/rc/Makefile
> index 3e64a4e..466c402 100644
> --- a/drivers/media/rc/Makefile
> +++ b/drivers/media/rc/Makefile
> @@ -33,6 +33,7 @@ obj-$(CONFIG_IR_WINBOND_CIR) += winbond-cir.o
> obj-$(CONFIG_RC_LOOPBACK) += rc-loopback.o
> obj-$(CONFIG_IR_GPIO_CIR) += gpio-ir-recv.o
> obj-$(CONFIG_IR_GPIO_TX) += gpio-ir-tx.o
> +obj-$(CONFIG_IR_PWM_TX) += pwm-ir-tx.o
> obj-$(CONFIG_IR_IGORPLUGUSB) += igorplugusb.o
> obj-$(CONFIG_IR_IGUANA) += iguanair.o
> obj-$(CONFIG_IR_TTUSBIR) += ttusbir.o
> diff --git a/drivers/media/rc/pwm-ir-tx.c b/drivers/media/rc/pwm-ir-tx.c
> new file mode 100644
> index 0000000..1ab10b4
> --- /dev/null
> +++ b/drivers/media/rc/pwm-ir-tx.c
> @@ -0,0 +1,165 @@
> +/*
> + * Copyright (C) 2017 Sean Young <sean-hENCXIMQXOg@public.gmane.org>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU General Public License for more details.
> + */
> +
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/pwm.h>
> +#include <linux/delay.h>
> +#include <linux/slab.h>
> +#include <linux/of.h>
> +#include <linux/platform_device.h>
> +#include <media/rc-core.h>
> +
> +#define DRIVER_NAME "pwm-ir-tx"
> +#define DEVICE_NAME "PWM IR Transmitter"
> +
> +struct pwm_ir {
> + struct pwm_device *pwm;
> + unsigned int carrier;
> + unsigned int duty_cycle;
> + /* One transmission at a time */
> + struct mutex lock;
> +};
> +
> +static const struct of_device_id pwm_ir_of_match[] = {
> + { .compatible = "pwm-ir-tx", },
> + { },
> +};
> +MODULE_DEVICE_TABLE(of, pwm_ir_of_match);
> +
> +static int pwm_ir_set_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
> +{
> + struct pwm_ir *pwm_ir = dev->priv;
> +
> + pwm_ir->duty_cycle = duty_cycle;
> +
> + return 0;
> +}
> +
> +static int pwm_ir_set_carrier(struct rc_dev *dev, u32 carrier)
> +{
> + struct pwm_ir *pwm_ir = dev->priv;
> +
> + if (!carrier)
> + return -EINVAL;
> +
> + pwm_ir->carrier = carrier;
> +
> + return 0;
> +}
> +
> +static int pwm_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
> + unsigned int count)
> +{
> + struct pwm_ir *pwm_ir = dev->priv;
> + struct pwm_device *pwm = pwm_ir->pwm;
> + int i, duty, period;
> + ktime_t edge;
> + s64 delta;
> +
> + if (mutex_lock_interruptible(&pwm_ir->lock))
> + return -ERESTARTSYS;
> +
> + period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, pwm_ir->carrier);
> + duty = DIV_ROUND_CLOSEST(pwm_ir->duty_cycle * period, 100);
> +
> + pwm_config(pwm, duty, period);
> +
> + edge = ktime_get();
> +
> + for (i = 0; i < count; i++) {
> + if (i % 2) // space
> + pwm_disable(pwm);
> + else
> + pwm_enable(pwm);
> +
> + edge = ktime_add_us(edge, txbuf[i]);
> + delta = ktime_us_delta(edge, ktime_get());
> + if (delta > 0)
> + usleep_range(delta, delta + 10);
> + }
> +
> + pwm_disable(pwm);
> +
> + mutex_unlock(&pwm_ir->lock);
> +
> + return count;
> +}
> +
> +static int pwm_ir_probe(struct platform_device *pdev)
> +{
> + struct pwm_ir *pwm_ir;
> + struct rc_dev *rcdev;
> + int rc;
> +
> + pwm_ir = devm_kzalloc(&pdev->dev, sizeof(*pwm_ir), GFP_KERNEL);
> + if (!pwm_ir)
> + return -ENOMEM;
> +
> + pwm_ir->pwm = devm_pwm_get(&pdev->dev, NULL);
> + if (IS_ERR(pwm_ir->pwm))
> + return PTR_ERR(pwm_ir->pwm);
> +
> + pwm_ir->carrier = 38000;
> + pwm_ir->duty_cycle = 50;
> + mutex_init(&pwm_ir->lock);
> +
> + rcdev = devm_rc_allocate_device(&pdev->dev, RC_DRIVER_IR_RAW_TX);
> + if (!rcdev)
> + return -ENOMEM;
> +
> + rcdev->priv = pwm_ir;
> + rcdev->driver_name = DRIVER_NAME;
> + rcdev->device_name = DEVICE_NAME;
> + rcdev->tx_ir = pwm_ir_tx;
> + rcdev->s_tx_duty_cycle = pwm_ir_set_duty_cycle;
> + rcdev->s_tx_carrier = pwm_ir_set_carrier;
> +
> + rc = devm_rc_register_device(&pdev->dev, rcdev);
> + if (rc < 0) {
> + dev_err(&pdev->dev, "failed to register rc device\n");
> + return rc;
> + }
> +
> + platform_set_drvdata(pdev, pwm_ir);
> +
> + return 0;
> +}
> +
> +#ifdef CONFIG_PM
> +static int pwm_ir_suspend(struct platform_device *dev, pm_message_t state)
> +{
> + struct pwm_ir *pwm_ir = platform_get_drvdata(dev);
> +
> + if (mutex_is_locked(&pwm_ir->lock))
> + return -EAGAIN;
> +
> + return 0;
> +}
So this suspend function exists so that suspend does not happen while we're
transmitting; but all processing are frozen before the suspend gets called,
so we're too late in that case.
Besiders, during transmission in pwm_ir_tx() we're never in interruptable
sleep so this problem should not exist anyway.
Sean
> +#else
> +#define pwm_ir_suspend NULL
> +#endif
> +
> +static struct platform_driver pwm_ir_driver = {
> + .probe = pwm_ir_probe,
> + .suspend = pwm_ir_suspend,
> + .driver = {
> + .name = DRIVER_NAME,
> + .of_match_table = of_match_ptr(pwm_ir_of_match),
> + },
> +};
> +module_platform_driver(pwm_ir_driver);
> +
> +MODULE_DESCRIPTION("PWM IR Transmitter");
> +MODULE_AUTHOR("Sean Young <sean-hENCXIMQXOg@public.gmane.org>");
> +MODULE_LICENSE("GPL");
> --
> 2.9.4
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 3/4] [media] rc: gpio-ir-tx: add new driver
2017-07-02 11:06 ` [PATCH 3/4] [media] rc: gpio-ir-tx: add new driver Sean Young
@ 2017-07-07 13:58 ` Rob Herring
0 siblings, 0 replies; 7+ messages in thread
From: Rob Herring @ 2017-07-07 13:58 UTC (permalink / raw)
To: Sean Young; +Cc: linux-media, devicetree
On Sun, Jul 02, 2017 at 12:06:11PM +0100, Sean Young wrote:
> This is a simple bit-banging GPIO IR TX driver.
>
> Signed-off-by: Sean Young <sean@mess.org>
> ---
> .../devicetree/bindings/leds/irled/gpio-ir-tx.txt | 11 ++
Please make this a separate patch.
> drivers/media/rc/Kconfig | 11 ++
> drivers/media/rc/Makefile | 1 +
> drivers/media/rc/gpio-ir-tx.c | 189 +++++++++++++++++++++
> 4 files changed, 212 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/leds/irled/gpio-ir-tx.txt
> create mode 100644 drivers/media/rc/gpio-ir-tx.c
>
> diff --git a/Documentation/devicetree/bindings/leds/irled/gpio-ir-tx.txt b/Documentation/devicetree/bindings/leds/irled/gpio-ir-tx.txt
> new file mode 100644
> index 0000000..bf4d4fb
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/leds/irled/gpio-ir-tx.txt
> @@ -0,0 +1,11 @@
> +Device tree bindings for IR LED connected through gpio pin which is used as
> +IR transmitter.
> +
> +Required properties:
> + - compatible: should be "gpio-ir-tx".
gpios property?
> +
> +Example:
> + irled@0 {
> + compatible = "gpio-ir-tx";
> + gpios = <&gpio1 2 GPIO_ACTIVE_HIGH>;
> + };
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 4/4] [media] rc: pwm-ir-tx: add new driver
[not found] ` <88fa0219db3388fad7bcc7b20cf30dd41e763aee.1498992850.git.sean-hENCXIMQXOg@public.gmane.org>
2017-07-04 12:58 ` Sean Young
@ 2017-07-07 13:59 ` Rob Herring
2017-07-07 16:15 ` Pavel Machek
1 sibling, 1 reply; 7+ messages in thread
From: Rob Herring @ 2017-07-07 13:59 UTC (permalink / raw)
To: Sean Young
Cc: linux-media-u79uwXL29TY76Z2rM5mHXA,
devicetree-u79uwXL29TY76Z2rM5mHXA, Pavel Machek, Ivaylo Dimitrov,
Timo Kokkonen
On Sun, Jul 02, 2017 at 12:06:13PM +0100, Sean Young wrote:
> This is new driver which uses pwm, so it is more power-efficient
> than the bit banging gpio-ir-tx driver.
>
> Signed-off-by: Sean Young <sean-hENCXIMQXOg@public.gmane.org>
> ---
> .../devicetree/bindings/leds/irled/pwm-ir-tx.txt | 13 ++
Please make this a separate patch.
> drivers/media/rc/Kconfig | 12 ++
> drivers/media/rc/Makefile | 1 +
> drivers/media/rc/pwm-ir-tx.c | 165 +++++++++++++++++++++
> 4 files changed, 191 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt
> create mode 100644 drivers/media/rc/pwm-ir-tx.c
>
> diff --git a/Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt b/Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt
> new file mode 100644
> index 0000000..6887a71
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt
> @@ -0,0 +1,13 @@
> +Device tree bindings for IR LED connected through pwm pin which is used as
> +IR transmitter.
> +
> +Required properties:
> +- compatible: should be "pwm-ir-tx".
> +- pwms : PWM property to point to the PWM device (phandle)/port (id) and to
> + specify the period time to be used: <&phandle id period_ns>;
> +
> +Example:
> + irled {
> + compatible = "pwm-ir-tx";
> + pwms = <&pwm0 0 10000000>;
> + };
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 4/4] [media] rc: pwm-ir-tx: add new driver
2017-07-07 13:59 ` Rob Herring
@ 2017-07-07 16:15 ` Pavel Machek
2017-07-10 15:12 ` Rob Herring
0 siblings, 1 reply; 7+ messages in thread
From: Pavel Machek @ 2017-07-07 16:15 UTC (permalink / raw)
To: Rob Herring
Cc: Sean Young, linux-media, devicetree, Ivaylo Dimitrov,
Timo Kokkonen
[-- Attachment #1: Type: text/plain, Size: 1864 bytes --]
On Fri 2017-07-07 08:59:28, Rob Herring wrote:
> On Sun, Jul 02, 2017 at 12:06:13PM +0100, Sean Young wrote:
> > This is new driver which uses pwm, so it is more power-efficient
> > than the bit banging gpio-ir-tx driver.
> >
> > Signed-off-by: Sean Young <sean@mess.org>
> > ---
> > .../devicetree/bindings/leds/irled/pwm-ir-tx.txt | 13 ++
>
> Please make this a separate patch.
Come on... The driver is trivial, and you even quoted the binding
below. Saying "Acked-by:" would not have been that much additional
work...
Thanks,
Pavel
> > drivers/media/rc/Kconfig | 12 ++
> > drivers/media/rc/Makefile | 1 +
> > drivers/media/rc/pwm-ir-tx.c | 165 +++++++++++++++++++++
> > 4 files changed, 191 insertions(+)
> > create mode 100644 Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt
> > create mode 100644 drivers/media/rc/pwm-ir-tx.c
> >
> > diff --git a/Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt b/Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt
> > new file mode 100644
> > index 0000000..6887a71
> > --- /dev/null
> > +++ b/Documentation/devicetree/bindings/leds/irled/pwm-ir-tx.txt
> > @@ -0,0 +1,13 @@
> > +Device tree bindings for IR LED connected through pwm pin which is used as
> > +IR transmitter.
> > +
> > +Required properties:
> > +- compatible: should be "pwm-ir-tx".
> > +- pwms : PWM property to point to the PWM device (phandle)/port (id) and to
> > + specify the period time to be used: <&phandle id period_ns>;
> > +
> > +Example:
> > + irled {
> > + compatible = "pwm-ir-tx";
> > + pwms = <&pwm0 0 10000000>;
> > + };
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 4/4] [media] rc: pwm-ir-tx: add new driver
2017-07-07 16:15 ` Pavel Machek
@ 2017-07-10 15:12 ` Rob Herring
0 siblings, 0 replies; 7+ messages in thread
From: Rob Herring @ 2017-07-10 15:12 UTC (permalink / raw)
To: Pavel Machek
Cc: Sean Young, linux-media@vger.kernel.org,
devicetree@vger.kernel.org, Ivaylo Dimitrov, Timo Kokkonen
On Fri, Jul 7, 2017 at 11:15 AM, Pavel Machek <pavel@ucw.cz> wrote:
> On Fri 2017-07-07 08:59:28, Rob Herring wrote:
>> On Sun, Jul 02, 2017 at 12:06:13PM +0100, Sean Young wrote:
>> > This is new driver which uses pwm, so it is more power-efficient
>> > than the bit banging gpio-ir-tx driver.
>> >
>> > Signed-off-by: Sean Young <sean@mess.org>
>> > ---
>> > .../devicetree/bindings/leds/irled/pwm-ir-tx.txt | 13 ++
>>
>> Please make this a separate patch.
>
> Come on... The driver is trivial, and you even quoted the binding
> below. Saying "Acked-by:" would not have been that much additional
> work...
True, and I don't always ask for it especially when there is no other
comment. However, the gpio-ir-tx patch needs other changes anyway.
Rob
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2017-07-10 15:12 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <cover.1498992850.git.sean@mess.org>
2017-07-02 11:06 ` [PATCH 3/4] [media] rc: gpio-ir-tx: add new driver Sean Young
2017-07-07 13:58 ` Rob Herring
2017-07-02 11:06 ` [PATCH 4/4] [media] rc: pwm-ir-tx: " Sean Young
[not found] ` <88fa0219db3388fad7bcc7b20cf30dd41e763aee.1498992850.git.sean-hENCXIMQXOg@public.gmane.org>
2017-07-04 12:58 ` Sean Young
2017-07-07 13:59 ` Rob Herring
2017-07-07 16:15 ` Pavel Machek
2017-07-10 15:12 ` Rob Herring
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).