Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/fdt Add support for PWM backlights.



details:   https://anonhg.NetBSD.org/src/rev/43f753cbbb28
branches:  trunk
changeset: 318828:43f753cbbb28
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Sun May 06 10:33:21 2018 +0000
description:
Add support for PWM backlights.

diffstat:

 sys/dev/fdt/fdt_pwm.c       |  116 +++++++++++++++++
 sys/dev/fdt/fdtvar.h        |   11 +-
 sys/dev/fdt/files.fdt       |    9 +-
 sys/dev/fdt/pwm_backlight.c |  293 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 426 insertions(+), 3 deletions(-)

diffs (truncated from 498 to 300 lines):

diff -r a3ec9774e511 -r 43f753cbbb28 sys/dev/fdt/fdt_pwm.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/fdt/fdt_pwm.c     Sun May 06 10:33:21 2018 +0000
@@ -0,0 +1,116 @@
+/* $NetBSD: fdt_pwm.c,v 1.1 2018/05/06 10:33:21 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2018 Jared McNeill <jmcneill%invisible.ca@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: fdt_pwm.c,v 1.1 2018/05/06 10:33:21 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/kmem.h>
+
+#include <libfdt.h>
+#include <dev/fdt/fdtvar.h>
+
+struct fdtbus_pwm_controller {
+       device_t pc_dev;
+       int pc_phandle;
+       const struct fdtbus_pwm_controller_func *pc_funcs;
+
+       struct fdtbus_pwm_controller *pc_next;
+};
+
+static struct fdtbus_pwm_controller *fdtbus_pc = NULL;
+
+int
+fdtbus_register_pwm_controller(device_t dev, int phandle,
+    const struct fdtbus_pwm_controller_func *funcs)
+{
+       struct fdtbus_pwm_controller *pc;
+
+       pc = kmem_alloc(sizeof(*pc), KM_SLEEP);
+       pc->pc_dev = dev;
+       pc->pc_phandle = phandle;
+       pc->pc_funcs = funcs;
+
+       pc->pc_next = fdtbus_pc;
+       fdtbus_pc = pc;
+
+       return 0;
+}
+
+static struct fdtbus_pwm_controller *
+fdtbus_get_pwm_controller(int phandle)
+{
+       struct fdtbus_pwm_controller *pc;
+
+       for (pc = fdtbus_pc; pc; pc = pc->pc_next) {
+               if (pc->pc_phandle == phandle) {
+                       return pc;
+               }
+       }
+
+       return NULL;
+}
+
+pwm_tag_t
+fdtbus_pwm_acquire(int phandle, const char *prop)
+{
+       return fdtbus_pwm_acquire_index(phandle, prop, 0);
+}
+
+pwm_tag_t
+fdtbus_pwm_acquire_index(int phandle, const char *prop, int index)
+{
+       struct fdtbus_pwm_controller *pc;
+       const uint32_t *pwms, *p;
+       u_int n, pwm_cells;
+       int len, resid;
+
+       pwms = fdtbus_get_prop(phandle, prop, &len);
+       if (pwms == NULL)
+               return NULL;
+
+       p = pwms;
+       for (n = 0, resid = len; resid > 0; n++) {
+               const int pc_phandle =
+                   fdtbus_get_phandle_from_native(be32toh(p[0]));
+               if (of_getprop_uint32(pc_phandle, "#pwm-cells", &pwm_cells))
+                       break;
+               if (n == index) {
+                       pc = fdtbus_get_pwm_controller(pc_phandle);
+                       if (pc == NULL)
+                               return NULL;
+                       return pc->pc_funcs->get_tag(pc->pc_dev,
+                           &p[0], (pwm_cells + 1) * 4);
+               }
+               resid -= (pwm_cells + 1) * 4;
+               p += pwm_cells + 1;
+       }
+
+       return NULL;
+}
diff -r a3ec9774e511 -r 43f753cbbb28 sys/dev/fdt/fdtvar.h
--- a/sys/dev/fdt/fdtvar.h      Sun May 06 10:32:33 2018 +0000
+++ b/sys/dev/fdt/fdtvar.h      Sun May 06 10:33:21 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fdtvar.h,v 1.29 2018/04/07 18:05:08 bouyer Exp $ */
+/* $NetBSD: fdtvar.h,v 1.30 2018/05/06 10:33:21 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -34,6 +34,7 @@
 #include <sys/termios.h>
 
 #include <dev/i2c/i2cvar.h>
+#include <dev/pwm/pwmvar.h>
 #include <dev/clk/clk.h>
 
 #include <dev/clock_subr.h>
@@ -185,6 +186,10 @@
        int     (*enable)(device_t, void *, bool);
 };
 
+struct fdtbus_pwm_controller_func {
+       pwm_tag_t (*get_tag)(device_t, const void *, size_t);
+};
+
 struct fdtbus_mmc_pwrseq;
 
 struct fdtbus_mmc_pwrseq_func {
@@ -234,6 +239,8 @@
                    const struct fdtbus_power_controller_func *);
 int            fdtbus_register_phy_controller(device_t, int,
                    const struct fdtbus_phy_controller_func *);
+int            fdtbus_register_pwm_controller(device_t, int,
+                   const struct fdtbus_pwm_controller_func *);
 int            fdtbus_register_mmc_pwrseq(device_t, int,
                    const struct fdtbus_mmc_pwrseq_func *);
 
@@ -257,6 +264,8 @@
 void           fdtbus_gpio_write(struct fdtbus_gpio_pin *, int);
 int            fdtbus_gpio_read_raw(struct fdtbus_gpio_pin *);
 void           fdtbus_gpio_write_raw(struct fdtbus_gpio_pin *, int);
+pwm_tag_t      fdtbus_pwm_acquire(int, const char *);
+pwm_tag_t      fdtbus_pwm_acquire_index(int, const char *, int);
 void           fdtbus_pinctrl_configure(void);
 int            fdtbus_pinctrl_set_config_index(int, u_int);
 int            fdtbus_pinctrl_set_config(int, const char *);
diff -r a3ec9774e511 -r 43f753cbbb28 sys/dev/fdt/files.fdt
--- a/sys/dev/fdt/files.fdt     Sun May 06 10:32:33 2018 +0000
+++ b/sys/dev/fdt/files.fdt     Sun May 06 10:33:21 2018 +0000
@@ -1,10 +1,10 @@
-# $NetBSD: files.fdt,v 1.24 2018/05/01 23:59:15 jmcneill Exp $
+# $NetBSD: files.fdt,v 1.25 2018/05/06 10:33:21 jmcneill Exp $
 
 include        "external/bsd/libfdt/conf/files.libfdt"
 
 defflag        opt_fdt.h                               FDT: libfdt, ofw_subr
 
-define fdtbus { } : clk
+define fdtbus { } : clk, pwm
 
 device fdt { [pass = 10] } : fdtbus
 attach fdt at fdtbus
@@ -54,6 +54,7 @@
 file   dev/fdt/fdt_mmc_pwrseq.c                fdtbus
 file   dev/fdt/fdt_phy.c                       fdtbus
 file   dev/fdt/fdt_power.c                     fdtbus
+file   dev/fdt/fdt_pwm.c                       fdtbus
 file   dev/fdt/fdt_regulator.c                 fdtbus
 file   dev/fdt/fdt_reset.c                     fdtbus
 file   dev/fdt/fdt_rtc.c                       fdtbus
@@ -67,6 +68,10 @@
 attach mmcpwrseq at fdt
 file   dev/fdt/mmc_pwrseq_simple.c             mmcpwrseq
 
+device pwmbacklight
+attach pwmbacklight at fdt
+file   dev/fdt/pwm_backlight.c                 pwmbacklight
+
 define fdt_display_timing
 file   dev/fdt/display_timing.c                fdt_display_timing
 
diff -r a3ec9774e511 -r 43f753cbbb28 sys/dev/fdt/pwm_backlight.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/fdt/pwm_backlight.c       Sun May 06 10:33:21 2018 +0000
@@ -0,0 +1,293 @@
+/* $NetBSD: pwm_backlight.c,v 1.1 2018/05/06 10:33:21 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2018 Jared McNeill <jmcneill%invisible.ca@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: pwm_backlight.c,v 1.1 2018/05/06 10:33:21 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/device.h>
+#include <sys/systm.h>
+#include <sys/sysctl.h>
+#include <sys/kmem.h>
+#include <sys/gpio.h>
+
+#include <dev/pwm/pwmvar.h>
+
+#include <dev/fdt/fdtvar.h>
+
+struct pwm_backlight_softc {
+       device_t                sc_dev;
+       pwm_tag_t               sc_pwm;
+       struct fdtbus_gpio_pin *sc_pin;
+
+       u_int                   *sc_levels;
+       u_int                   sc_nlevels;
+
+       char                    *sc_levelstr;
+};
+
+static int     pwm_backlight_match(device_t, cfdata_t, void *);
+static void    pwm_backlight_attach(device_t, device_t, void *);
+
+static void    pwm_backlight_sysctl_init(struct pwm_backlight_softc *);
+static void    pwm_backlight_pmf_init(struct pwm_backlight_softc *);
+static void    pwm_backlight_set(struct pwm_backlight_softc *, u_int);
+static u_int   pwm_backlight_get(struct pwm_backlight_softc *);
+
+static const char *compatible[] = {
+       "pwm-backlight",
+       NULL
+};
+
+CFATTACH_DECL_NEW(pwmbacklight, sizeof(struct pwm_backlight_softc),
+       pwm_backlight_match, pwm_backlight_attach, NULL, NULL);
+
+static int
+pwm_backlight_match(device_t parent, cfdata_t cf, void *aux)
+{
+       struct fdt_attach_args * const faa = aux;
+
+       return of_match_compatible(faa->faa_phandle, compatible);
+}
+
+static void
+pwm_backlight_attach(device_t parent, device_t self, void *aux)
+{
+       struct pwm_backlight_softc * const sc = device_private(self);
+       struct fdt_attach_args * const faa = aux;
+       const int phandle = faa->faa_phandle;
+       const u_int *levels;
+       u_int default_level;
+       u_int n;
+       int len;
+
+       sc->sc_dev = self;
+       sc->sc_pwm = fdtbus_pwm_acquire(phandle, "pwms");
+       if (sc->sc_pwm == NULL) {
+               aprint_error(": couldn't acquire pwm\n");
+               return;
+       }



Home | Main Index | Thread Index | Old Index