* [PATCH 2/6] video: Add backlight support
2015-06-12 9:53 [PATCH 1/6] video: stmfb: Add device tree support Sascha Hauer
@ 2015-06-12 9:53 ` Sascha Hauer
2015-06-12 9:53 ` [PATCH 3/6] video: Add PWM " Sascha Hauer
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2015-06-12 9:53 UTC (permalink / raw)
To: Barebox List
This adds a small backlight layer. It provides a backlight device
on which the brightness parameter can be used to adjust the brightness.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
drivers/video/Kconfig | 4 +++
drivers/video/Makefile | 1 +
drivers/video/backlight.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++
include/video/backlight.h | 20 +++++++++++
4 files changed, 115 insertions(+)
create mode 100644 drivers/video/backlight.c
create mode 100644 include/video/backlight.h
diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig
index f096a54..e108d8a 100644
--- a/drivers/video/Kconfig
+++ b/drivers/video/Kconfig
@@ -93,4 +93,8 @@ config DRIVER_VIDEO_EDID
This enabled support for reading and parsing EDID data from an attached
monitor.
+config DRIVER_VIDEO_BACKLIGHT
+ bool "Add backlight support"
+ help
+ Enable this for backlight support.
endif
diff --git a/drivers/video/Makefile b/drivers/video/Makefile
index ae9f6e5..0655b0f 100644
--- a/drivers/video/Makefile
+++ b/drivers/video/Makefile
@@ -1,6 +1,7 @@
obj-$(CONFIG_VIDEO) += fb.o
obj-$(CONFIG_DRIVER_VIDEO_EDID) += edid.o
obj-$(CONFIG_OFDEVICE) += of_display_timing.o
+obj-$(CONFIG_DRIVER_VIDEO_BACKLIGHT) += backlight.o
obj-$(CONFIG_DRIVER_VIDEO_ATMEL) += atmel_lcdfb.o atmel_lcdfb_core.o
obj-$(CONFIG_DRIVER_VIDEO_ATMEL_HLCD) += atmel_hlcdfb.o atmel_lcdfb_core.o
diff --git a/drivers/video/backlight.c b/drivers/video/backlight.c
new file mode 100644
index 0000000..ddde6f8
--- /dev/null
+++ b/drivers/video/backlight.c
@@ -0,0 +1,90 @@
+#include <common.h>
+#include <driver.h>
+#include <linux/list.h>
+#include <video/backlight.h>
+
+static LIST_HEAD(backlights);
+
+int backlight_set_brightness(struct backlight_device *bl, int brightness)
+{
+ int ret, step, i, num_steps;
+
+ if (brightness > bl->brightness_max)
+ brightness = bl->brightness_max;
+
+ if (brightness == bl->brightness_cur)
+ return 0;
+
+ if (brightness > bl->brightness_cur)
+ step = 1;
+ else
+ step = -1;
+
+ i = bl->brightness_cur;
+
+ num_steps = abs(brightness - bl->brightness_cur);
+
+ while (1) {
+ i += step;
+
+ ret = bl->brightness_set(bl, i);
+ if (ret)
+ return ret;
+
+ if (i == brightness)
+ break;
+
+ udelay(100000 / num_steps);
+ }
+
+
+ bl->brightness_cur = bl->brightness = brightness;
+
+ return ret;
+}
+
+int backlight_set_brightness_default(struct backlight_device *bl)
+{
+ int ret;
+
+ ret = backlight_set_brightness(bl, bl->brightness_default);
+
+ return ret;
+}
+
+static int backlight_brightness_set(struct param_d *p, void *priv)
+{
+ struct backlight_device *bl = priv;
+
+ return backlight_set_brightness(bl, bl->brightness);
+}
+
+int backlight_register(struct backlight_device *bl)
+{
+ int ret;
+
+ sprintf(bl->dev.name, "backlight");
+ bl->dev.id = DEVICE_ID_DYNAMIC;
+
+ ret = register_device(&bl->dev);
+ if (ret)
+ return ret;
+
+ dev_add_param_int(&bl->dev, "brightness", backlight_brightness_set,
+ NULL, &bl->brightness, "%d", bl);
+
+ list_add_tail(&bl->list, &backlights);
+
+ return ret;
+}
+
+struct backlight_device *of_backlight_find(struct device_node *node)
+{
+ struct backlight_device *bl;
+
+ list_for_each_entry(bl, &backlights, list)
+ if (bl->node == node)
+ return bl;
+
+ return NULL;
+}
diff --git a/include/video/backlight.h b/include/video/backlight.h
new file mode 100644
index 0000000..56e0341
--- /dev/null
+++ b/include/video/backlight.h
@@ -0,0 +1,20 @@
+#ifndef __VIDEO_BACKLIGHT_H
+#define __VIDEO_BACKLIGHT_H
+
+struct backlight_device {
+ int brightness;
+ int brightness_cur;
+ int brightness_max;
+ int brightness_default;
+ int (*brightness_set)(struct backlight_device *, int brightness);
+ struct list_head list;
+ struct device_d dev;
+ struct device_node *node;
+};
+
+int backlight_set_brightness(struct backlight_device *, int brightness);
+int backlight_set_brightness_default(struct backlight_device *);
+int backlight_register(struct backlight_device *);
+struct backlight_device *of_backlight_find(struct device_node *node);
+
+#endif /* __VIDEO_BACKLIGHT_H */
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 3/6] video: Add PWM backlight support
2015-06-12 9:53 [PATCH 1/6] video: stmfb: Add device tree support Sascha Hauer
2015-06-12 9:53 ` [PATCH 2/6] video: Add backlight support Sascha Hauer
@ 2015-06-12 9:53 ` Sascha Hauer
2015-06-12 9:58 ` Marc Kleine-Budde
2015-06-12 9:53 ` [PATCH 4/6] ARM: i.MX28: Add PWM clk support Sascha Hauer
` (2 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: Sascha Hauer @ 2015-06-12 9:53 UTC (permalink / raw)
To: Barebox List
This adds a backlight driver for backlights controlled by a PWM.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
drivers/video/Kconfig | 8 ++
drivers/video/Makefile | 1 +
drivers/video/backlight-pwm.c | 199 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 208 insertions(+)
create mode 100644 drivers/video/backlight-pwm.c
diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig
index e108d8a..8e6ae99 100644
--- a/drivers/video/Kconfig
+++ b/drivers/video/Kconfig
@@ -97,4 +97,12 @@ config DRIVER_VIDEO_BACKLIGHT
bool "Add backlight support"
help
Enable this for backlight support.
+
+config DRIVER_VIDEO_BACKLIGHT_PWM
+ bool "PWM backlight support"
+ depends on PWM
+ depends on DRIVER_VIDEO_BACKLIGHT
+ help
+ Enable this to get support for backlight devices driven by a PWM.
+
endif
diff --git a/drivers/video/Makefile b/drivers/video/Makefile
index 0655b0f..76fad5c 100644
--- a/drivers/video/Makefile
+++ b/drivers/video/Makefile
@@ -2,6 +2,7 @@ obj-$(CONFIG_VIDEO) += fb.o
obj-$(CONFIG_DRIVER_VIDEO_EDID) += edid.o
obj-$(CONFIG_OFDEVICE) += of_display_timing.o
obj-$(CONFIG_DRIVER_VIDEO_BACKLIGHT) += backlight.o
+obj-$(CONFIG_DRIVER_VIDEO_BACKLIGHT_PWM) += backlight-pwm.o
obj-$(CONFIG_DRIVER_VIDEO_ATMEL) += atmel_lcdfb.o atmel_lcdfb_core.o
obj-$(CONFIG_DRIVER_VIDEO_ATMEL_HLCD) += atmel_hlcdfb.o atmel_lcdfb_core.o
diff --git a/drivers/video/backlight-pwm.c b/drivers/video/backlight-pwm.c
new file mode 100644
index 0000000..ae0b0ca
--- /dev/null
+++ b/drivers/video/backlight-pwm.c
@@ -0,0 +1,199 @@
+/*
+ * pwm backlight support for barebox
+ *
+ * (C) Copyright 2014 Sascha Hauer, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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.
+ *
+ * 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 <common.h>
+#include <malloc.h>
+#include <init.h>
+#include <video/backlight.h>
+#include <pwm.h>
+#include <linux/err.h>
+#include <of.h>
+#include <gpio.h>
+#include <of_gpio.h>
+#include <asm-generic/div64.h>
+
+struct pwm_backlight {
+ struct backlight_device backlight;
+ struct pwm_device *pwm;
+ uint32_t period;
+ unsigned int *levels;
+ int enable_gpio;
+ int enable_active_high;
+ int max_value;
+ int enabled;
+};
+
+static int backlight_pwm_enable(struct pwm_backlight *pwm_backlight)
+{
+ int ret;
+
+ if (pwm_backlight->enabled)
+ return 0;
+
+ ret = pwm_enable(pwm_backlight->pwm);
+ if (ret)
+ return ret;
+
+ if (gpio_is_valid(pwm_backlight->enable_gpio)) {
+ gpio_direction_output(pwm_backlight->enable_gpio,
+ pwm_backlight->enable_active_high);
+ }
+
+ pwm_backlight->enabled = 1;
+
+ return 0;
+}
+
+static int backlight_pwm_disable(struct pwm_backlight *pwm_backlight)
+{
+ if (!pwm_backlight->enabled)
+ return 0;
+
+ if (gpio_is_valid(pwm_backlight->enable_gpio)) {
+ gpio_direction_output(pwm_backlight->enable_gpio,
+ !pwm_backlight->enable_active_high);
+ /*
+ * Only disable PWM when an enable gpio is present.
+ * The output of the PWM is undefined when the PWM
+ * is disabled.
+ */
+ pwm_disable(pwm_backlight->pwm);
+ pwm_backlight->enabled = 0;
+ }
+
+ return 0;
+}
+
+static int backlight_pwm_set(struct backlight_device *backlight,
+ int brightness)
+{
+ struct pwm_backlight *pwm_backlight = container_of(backlight,
+ struct pwm_backlight, backlight);
+ unsigned long long duty = pwm_backlight->period;
+ unsigned int max = pwm_backlight->backlight.brightness_max;
+
+ duty *= brightness;
+ do_div(duty, max);
+
+ pwm_config(pwm_backlight->pwm, duty, pwm_backlight->period);
+
+ if (brightness)
+ return backlight_pwm_enable(pwm_backlight);
+ else
+ return backlight_pwm_disable(pwm_backlight);
+}
+
+static int pwm_backlight_parse_dt(struct device_d *dev,
+ struct pwm_backlight *pwm_backlight)
+{
+ struct device_node *node = dev->device_node;
+ struct property *prop;
+ int length;
+ u32 value;
+ int ret;
+ enum of_gpio_flags flags;
+
+ if (!node)
+ return -ENODEV;
+
+ /* determine the number of brightness levels */
+ prop = of_find_property(node, "brightness-levels", &length);
+ if (!prop)
+ return -EINVAL;
+
+ pwm_backlight->backlight.brightness_max = length / sizeof(u32);
+
+ /* read brightness levels from DT property */
+ if (pwm_backlight->backlight.brightness_max > 0) {
+ size_t size = sizeof(*pwm_backlight->levels) *
+ pwm_backlight->backlight.brightness_max;
+
+ pwm_backlight->levels = xzalloc(size);
+ if (!pwm_backlight->levels)
+ return -ENOMEM;
+
+ ret = of_property_read_u32_array(node, "brightness-levels",
+ pwm_backlight->levels,
+ pwm_backlight->backlight.brightness_max);
+ if (ret < 0)
+ return ret;
+
+ ret = of_property_read_u32(node, "default-brightness-level",
+ &value);
+ if (ret < 0)
+ return ret;
+
+ pwm_backlight->backlight.brightness_default = value;
+ pwm_backlight->backlight.brightness_max--;
+ }
+
+ pwm_backlight->enable_gpio = of_get_named_gpio_flags(node, "enable-gpios", 0, &flags);
+
+ if (gpio_is_valid(pwm_backlight->enable_gpio)) {
+ if (!(flags & OF_GPIO_ACTIVE_LOW))
+ pwm_backlight->enable_active_high = 1;
+ }
+
+ return 0;
+}
+
+static int backlight_pwm_of_probe(struct device_d *dev)
+{
+ int ret;
+ struct pwm_backlight *pwm_backlight;
+ struct pwm_device *pwm;
+
+ pwm = of_pwm_request(dev->device_node, NULL);
+ if (IS_ERR(pwm))
+ return PTR_ERR(pwm);
+
+ pwm_backlight = xzalloc(sizeof(*pwm_backlight));
+ pwm_backlight->pwm = pwm;
+ pwm_backlight->period = pwm_get_period(pwm);
+
+ ret = pwm_backlight_parse_dt(dev, pwm_backlight);
+ if (ret)
+ return ret;
+
+ pwm_backlight->period = pwm_get_period(pwm_backlight->pwm);
+
+ pwm_backlight->backlight.brightness_set = backlight_pwm_set;
+ pwm_backlight->backlight.node = dev->device_node;
+
+ ret = backlight_register(&pwm_backlight->backlight);
+ if (ret)
+ return ret;
+
+ return 0;
+}
+
+static struct of_device_id backlight_pwm_of_ids[] = {
+ {
+ .compatible = "pwm-backlight",
+ }, {
+ /* sentinel */
+ }
+};
+
+static struct driver_d backlight_pwm_of_driver = {
+ .name = "pwm-backlight",
+ .probe = backlight_pwm_of_probe,
+ .of_compatible = DRV_OF_COMPAT(backlight_pwm_of_ids),
+};
+device_platform_driver(backlight_pwm_of_driver);
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 3/6] video: Add PWM backlight support
2015-06-12 9:53 ` [PATCH 3/6] video: Add PWM " Sascha Hauer
@ 2015-06-12 9:58 ` Marc Kleine-Budde
0 siblings, 0 replies; 7+ messages in thread
From: Marc Kleine-Budde @ 2015-06-12 9:58 UTC (permalink / raw)
To: Sascha Hauer, Barebox List
[-- Attachment #1.1: Type: text/plain, Size: 821 bytes --]
On 06/12/2015 11:53 AM, Sascha Hauer wrote:
> +static·int·backlight_pwm_set(struct·backlight_device·*backlight,
> +» » int·brightness)
> +{
> +» struct·pwm_backlight·*pwm_backlight·=·container_of(backlight,
> +» » » struct·pwm_backlight,·backlight);
> +» unsigned·long·long·duty·=··pwm_backlight->period;
> +» unsigned·int·max·=·pwm_backlight->backlight.brightness_max;
> +
> +········duty·*=·brightness;
> +········do_div(duty,·max);
indent with tab instead of space here.
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Industrial Linux Solutions | Phone: +49-231-2826-924 |
Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 455 bytes --]
[-- Attachment #2: Type: text/plain, Size: 149 bytes --]
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 4/6] ARM: i.MX28: Add PWM clk support
2015-06-12 9:53 [PATCH 1/6] video: stmfb: Add device tree support Sascha Hauer
2015-06-12 9:53 ` [PATCH 2/6] video: Add backlight support Sascha Hauer
2015-06-12 9:53 ` [PATCH 3/6] video: Add PWM " Sascha Hauer
@ 2015-06-12 9:53 ` Sascha Hauer
2015-06-12 9:53 ` [PATCH 5/6] PWM: Allow multiple PWMs per device node Sascha Hauer
2015-06-12 9:53 ` [PATCH 6/6] PWM: Add MXS PWM support Sascha Hauer
4 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2015-06-12 9:53 UTC (permalink / raw)
To: Barebox List
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
arch/arm/mach-mxs/include/mach/imx28-regs.h | 1 +
drivers/clk/mxs/clk-imx28.c | 1 +
2 files changed, 2 insertions(+)
diff --git a/arch/arm/mach-mxs/include/mach/imx28-regs.h b/arch/arm/mach-mxs/include/mach/imx28-regs.h
index de0d882..1a90ec2 100644
--- a/arch/arm/mach-mxs/include/mach/imx28-regs.h
+++ b/arch/arm/mach-mxs/include/mach/imx28-regs.h
@@ -34,6 +34,7 @@
#define IMX_WDT_BASE 0x80056000
#define IMX_I2C0_BASE 0x80058000
#define IMX_I2C1_BASE 0x8005a000
+#define IMX_PWM_BASE 0x80064000
#define IMX_TIM1_BASE 0x80068000
#define IMX_UART0_BASE 0x8006a000
#define IMX_UART1_BASE 0x8006c000
diff --git a/drivers/clk/mxs/clk-imx28.c b/drivers/clk/mxs/clk-imx28.c
index 77a13bc..a408044 100644
--- a/drivers/clk/mxs/clk-imx28.c
+++ b/drivers/clk/mxs/clk-imx28.c
@@ -158,6 +158,7 @@ int __init mx28_clocks_init(void __iomem *regs)
clkdev_add_physbase(clks[uart], IMX_UART3_BASE, NULL);
clkdev_add_physbase(clks[uart], IMX_UART4_BASE, NULL);
clkdev_add_physbase(clks[gpmi], MXS_GPMI_BASE, NULL);
+ clkdev_add_physbase(clks[pwm], IMX_PWM_BASE, NULL);
if (IS_ENABLED(CONFIG_DRIVER_VIDEO_STM))
clkdev_add_physbase(clks[lcdif_comp], IMX_FB_BASE, NULL);
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 5/6] PWM: Allow multiple PWMs per device node
2015-06-12 9:53 [PATCH 1/6] video: stmfb: Add device tree support Sascha Hauer
` (2 preceding siblings ...)
2015-06-12 9:53 ` [PATCH 4/6] ARM: i.MX28: Add PWM clk support Sascha Hauer
@ 2015-06-12 9:53 ` Sascha Hauer
2015-06-12 9:53 ` [PATCH 6/6] PWM: Add MXS PWM support Sascha Hauer
4 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2015-06-12 9:53 UTC (permalink / raw)
To: Barebox List
Matching a device node is enough since a device node may have
multiple PWMs. Check for a id aswell.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
drivers/pwm/core.c | 7 ++++---
include/pwm.h | 2 ++
2 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index cc33dec..3605201 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -181,12 +181,13 @@ struct pwm_device *pwm_request(const char *devname)
}
EXPORT_SYMBOL_GPL(pwm_request);
-static struct pwm_device *of_node_to_pwm_device(struct device_node *np)
+static struct pwm_device *of_node_to_pwm_device(struct device_node *np, int id)
{
struct pwm_device *pwm;
list_for_each_entry(pwm, &pwm_list, node) {
- if (pwm->hwdev && pwm->hwdev->device_node == np)
+ if (pwm->hwdev && pwm->hwdev->device_node == np &&
+ pwm->chip->id == id)
return pwm;
}
@@ -210,7 +211,7 @@ struct pwm_device *of_pwm_request(struct device_node *np, const char *con_id)
return ERR_PTR(ret);
}
- pwm = of_node_to_pwm_device(args.np);
+ pwm = of_node_to_pwm_device(args.np, args.args[0]);
if (IS_ERR(pwm)) {
pr_debug("%s(): PWM chip not found\n", __func__);
return pwm;
diff --git a/include/pwm.h b/include/pwm.h
index 59d86d4..911c760 100644
--- a/include/pwm.h
+++ b/include/pwm.h
@@ -57,12 +57,14 @@ struct pwm_ops {
/**
* struct pwm_chip - abstract a PWM
+ * @id: The id of this pwm
* @devname: unique identifier for this pwm
* @ops: The callbacks for this PWM
* @duty_ns: The duty cycle of the PWM, in nano-seconds
* @period_ns: The period of the PWM, in nano-seconds
*/
struct pwm_chip {
+ int id;
const char *devname;
struct pwm_ops *ops;
int duty_ns;
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 6/6] PWM: Add MXS PWM support
2015-06-12 9:53 [PATCH 1/6] video: stmfb: Add device tree support Sascha Hauer
` (3 preceding siblings ...)
2015-06-12 9:53 ` [PATCH 5/6] PWM: Allow multiple PWMs per device node Sascha Hauer
@ 2015-06-12 9:53 ` Sascha Hauer
4 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2015-06-12 9:53 UTC (permalink / raw)
To: Barebox List
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
drivers/pwm/Kconfig | 6 ++
drivers/pwm/Makefile | 1 +
drivers/pwm/pwm-mxs.c | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 181 insertions(+)
create mode 100644 drivers/pwm/pwm-mxs.c
diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 8324d5e..97c3def 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -22,4 +22,10 @@ config PWM_IMX
help
This enables PWM support for Freescale i.MX SoCs
+config PWM_MXS
+ bool "i.MXs PWM Support"
+ depends on ARCH_MXS
+ help
+ This enables PWM support for Freescale i.MX23/i.MX28 SoCs
+
endif
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index dea9956..46865a2 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -1,3 +1,4 @@
obj-$(CONFIG_PWM) += core.o
obj-$(CONFIG_PWM_PXA) += pxa_pwm.o
obj-$(CONFIG_PWM_IMX) += pwm-imx.o
+obj-$(CONFIG_PWM_MXS) += pwm-mxs.o
diff --git a/drivers/pwm/pwm-mxs.c b/drivers/pwm/pwm-mxs.c
new file mode 100644
index 0000000..e667442
--- /dev/null
+++ b/drivers/pwm/pwm-mxs.c
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2012 Freescale Semiconductor, Inc.
+ *
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+#include <common.h>
+#include <init.h>
+#include <errno.h>
+#include <io.h>
+#include <pwm.h>
+#include <stmp-device.h>
+#include <linux/clk.h>
+#include <linux/err.h>
+#include <asm-generic/div64.h>
+
+#define SET 0x4
+#define CLR 0x8
+#define TOG 0xc
+
+#define PWM_CTRL 0x0
+#define PWM_ACTIVE0 0x10
+#define PWM_PERIOD0 0x20
+#define PERIOD_PERIOD(p) ((p) & 0xffff)
+#define PERIOD_PERIOD_MAX 0x10000
+#define PERIOD_ACTIVE_HIGH (3 << 16)
+#define PERIOD_INACTIVE_LOW (2 << 18)
+#define PERIOD_CDIV(div) (((div) & 0x7) << 20)
+#define PERIOD_CDIV_MAX 8
+
+static const unsigned int cdiv[PERIOD_CDIV_MAX] = {
+ 1, 2, 4, 8, 16, 64, 256, 1024
+};
+
+struct mxs_pwm;
+
+struct mxs_pwm_chip {
+ struct pwm_chip chip;
+ struct mxs_pwm *mxs;
+};
+
+struct mxs_pwm {
+ struct mxs_pwm_chip pwm[8];
+ struct clk *clk;
+ void __iomem *base;
+};
+
+#define to_mxs_pwm_chip(_chip) container_of(_chip, struct mxs_pwm_chip, chip)
+
+static int mxs_pwm_config(struct pwm_chip *chip, int duty_ns, int period_ns)
+{
+ struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
+ int div = 0;
+ unsigned int period_cycles, duty_cycles;
+ unsigned long rate;
+ unsigned long long c;
+
+ rate = clk_get_rate(mxs->mxs->clk);
+ while (1) {
+ c = rate / cdiv[div];
+ c = c * period_ns;
+ do_div(c, 1000000000);
+ if (c < PERIOD_PERIOD_MAX)
+ break;
+ div++;
+ if (div >= PERIOD_CDIV_MAX)
+ return -EINVAL;
+ }
+
+ period_cycles = c;
+ c *= duty_ns;
+ do_div(c, period_ns);
+ duty_cycles = c;
+
+ writel(duty_cycles << 16,
+ mxs->mxs->base + PWM_ACTIVE0 + mxs->chip.id * 0x20);
+ writel(PERIOD_PERIOD(period_cycles) | PERIOD_ACTIVE_HIGH |
+ PERIOD_INACTIVE_LOW | PERIOD_CDIV(div),
+ mxs->mxs->base + PWM_PERIOD0 + mxs->chip.id * 0x20);
+
+ return 0;
+}
+
+static int mxs_pwm_enable(struct pwm_chip *chip)
+{
+ struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
+
+ writel(1 << mxs->chip.id, mxs->mxs->base + PWM_CTRL + SET);
+
+ return 0;
+}
+
+static void mxs_pwm_disable(struct pwm_chip *chip)
+{
+ struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
+
+ writel(1 << mxs->chip.id, mxs->mxs->base + PWM_CTRL + CLR);
+}
+
+static struct pwm_ops mxs_pwm_ops = {
+ .config = mxs_pwm_config,
+ .enable = mxs_pwm_enable,
+ .disable = mxs_pwm_disable,
+};
+
+static int mxs_pwm_probe(struct device_d *dev)
+{
+ struct device_node *np = dev->device_node;
+ struct mxs_pwm *mxs;
+ int ret, i;
+ uint32_t npwm;
+
+ mxs = xzalloc(sizeof(*mxs));
+
+ mxs->base = dev_request_mem_region(dev, 0);
+ if (IS_ERR(mxs->base))
+ return PTR_ERR(mxs->base);
+
+ mxs->clk = clk_get(dev, NULL);
+ if (IS_ERR(mxs->clk))
+ return PTR_ERR(mxs->clk);
+
+ clk_enable(mxs->clk);
+
+ ret = stmp_reset_block(mxs->base, 0);
+ if (ret)
+ return ret;
+
+ ret = of_property_read_u32(np, "fsl,pwm-number", &npwm);
+ if (ret < 0) {
+ dev_err(dev, "failed to get pwm number: %d\n", ret);
+ return ret;
+ }
+
+ for (i = 0; i < npwm; i++) {
+ struct mxs_pwm_chip *mxspwm = &mxs->pwm[i];
+
+ mxspwm->chip.ops = &mxs_pwm_ops;
+ mxspwm->chip.devname = asprintf("pwm%d", i);
+ mxspwm->chip.id = i;
+ mxspwm->mxs = mxs;
+
+ ret = pwmchip_add(&mxspwm->chip, dev);
+ if (ret < 0) {
+ dev_err(dev, "failed to add pwm chip %d\n", ret);
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+static const struct of_device_id mxs_pwm_dt_ids[] = {
+ { .compatible = "fsl,imx23-pwm", },
+ { /* sentinel */ }
+};
+
+static struct driver_d mxs_pwm_driver = {
+ .name = "mxs-pwm",
+ .of_compatible = mxs_pwm_dt_ids,
+ .probe = mxs_pwm_probe,
+};
+
+coredevice_platform_driver(mxs_pwm_driver);
+
+MODULE_ALIAS("platform:mxs-pwm");
+MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
+MODULE_DESCRIPTION("Freescale MXS PWM Driver");
+MODULE_LICENSE("GPL v2");
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 7+ messages in thread