Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/dev/i2c Add a driver for the NXP PCA9685 16-channel, 12-...
details: https://anonhg.NetBSD.org/src/rev/80a8efd0b979
branches: trunk
changeset: 962510:80a8efd0b979
user: thorpej <thorpej%NetBSD.org@localhost>
date: Wed Jul 24 05:25:32 2019 +0000
description:
Add a driver for the NXP PCA9685 16-channel, 12-bit PWM Fm+ LED controller.
diffstat:
sys/dev/i2c/files.i2c | 7 +-
sys/dev/i2c/pca9685.c | 565 +++++++++++++++++++++++++++++++++++++++++++++++
sys/dev/i2c/pca9685reg.h | 160 +++++++++++++
3 files changed, 731 insertions(+), 1 deletions(-)
diffs (truncated from 751 to 300 lines):
diff -r 34ebda0c30ed -r 80a8efd0b979 sys/dev/i2c/files.i2c
--- a/sys/dev/i2c/files.i2c Wed Jul 24 04:58:26 2019 +0000
+++ b/sys/dev/i2c/files.i2c Wed Jul 24 05:25:32 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: files.i2c,v 1.99 2019/03/17 01:03:47 tnn Exp $
+# $NetBSD: files.i2c,v 1.100 2019/07/24 05:25:32 thorpej Exp $
obsolete defflag opt_i2cbus.h I2C_SCAN
define i2cbus { }
@@ -366,3 +366,8 @@
device anxedp: edid, videomode, drmkms, drmkms_i2c
attach anxedp at iic
file dev/i2c/anxedp.c anxedp
+
+# NXP PCA9685 16-channel, 12-bit PWM Fm+ LED controller
+device pcapwm: pwm
+attach pcapwm at iic
+file dev/i2c/pca9685.c pcapwm
diff -r 34ebda0c30ed -r 80a8efd0b979 sys/dev/i2c/pca9685.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/i2c/pca9685.c Wed Jul 24 05:25:32 2019 +0000
@@ -0,0 +1,565 @@
+/* $NetBSD: pca9685.c,v 1.1 2019/07/24 05:25:32 thorpej Exp $ */
+
+/*-
+ * Copyright (c) 2018, 2019 Jason R. Thorpe
+ * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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: pca9685.c,v 1.1 2019/07/24 05:25:32 thorpej Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/conf.h>
+#include <sys/bus.h>
+#include <sys/kernel.h>
+#include <sys/kmem.h>
+#include <sys/mutex.h>
+#include <sys/proc.h>
+#include <sys/sysctl.h>
+
+#include <dev/i2c/i2cvar.h>
+#include <dev/i2c/pca9685reg.h>
+
+#include <dev/pwm/pwmvar.h>
+
+#include <dev/fdt/fdtvar.h>
+
+/*
+ * Special channel number used to indicate that we want to set the
+ * pulse mode for all channels on this controller.
+ */
+#define PCA9685_ALL_CHANNELS PCA9685_NCHANNELS
+
+struct pcapwm_channel {
+ struct pwm_controller ch_controller;
+ struct pwm_config ch_conf;
+ u_int ch_number;
+};
+
+struct pcapwm_softc {
+ device_t sc_dev;
+ i2c_tag_t sc_i2c;
+ i2c_addr_t sc_addr;
+ int sc_i2c_flags;
+
+ /*
+ * Locking order is:
+ * pcapwm mutex -> i2c bus
+ */
+ kmutex_t sc_lock;
+
+ /*
+ * The PCA9685 only has a single pre-scaler, so the configured
+ * PWM frequency / period is shared by all channels.
+ */
+ u_int sc_period; /* nanoseconds */
+ u_int sc_clk_freq;
+ bool sc_ext_clk;
+ bool sc_invert; /* "invert" property specified */
+ bool sc_open_drain; /* "open-drain" property specified */
+
+ /*
+ * +1 because we treat channel "16" as the all-channels
+ * pseudo-channel.
+ */
+ struct pcapwm_channel sc_channels[PCA9685_NCHANNELS+1];
+};
+
+static int pcapwm_pwm_enable(struct pwm_controller *, bool);
+static int pcapwm_pwm_get_config(struct pwm_controller *,
+ struct pwm_config *);
+static int pcapwm_pwm_set_config(struct pwm_controller *,
+ const struct pwm_config *);
+
+static const struct device_compatible_entry compat_data[] = {
+ { "nxp,pca9685-pwm", 0 },
+ { NULL }
+};
+
+static int
+pcapwm_read1(struct pcapwm_softc * const sc, uint8_t reg, uint8_t *valp)
+{
+
+ return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
+ sc->sc_addr, ®, sizeof(reg),
+ valp, sizeof(*valp), cold ? I2C_F_POLL : 0);
+}
+
+static int
+pcapwm_write1(struct pcapwm_softc * const sc, uint8_t reg, uint8_t val)
+{
+
+ return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
+ sc->sc_addr, ®, sizeof(reg),
+ &val, sizeof(val), cold ? I2C_F_POLL : 0);
+}
+
+static int
+pcapwm_read_LEDn(struct pcapwm_softc * const sc, uint8_t reg, uint8_t *buf,
+ size_t buflen)
+{
+
+ /* We rely on register auto-increment being enabled. */
+ return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
+ sc->sc_addr, ®, sizeof(reg),
+ buf, buflen, cold ? I2C_F_POLL : 0);
+}
+
+static int
+pcapwm_write_LEDn(struct pcapwm_softc * const sc, uint8_t reg, uint8_t *buf,
+ size_t buflen)
+{
+
+ /* We rely on register auto-increment being enabled. */
+ return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
+ sc->sc_addr, ®, sizeof(reg),
+ buf, buflen, cold ? I2C_F_POLL : 0);
+}
+
+static int
+pcapwm_program_channel(struct pcapwm_softc * const sc,
+ struct pcapwm_channel * const chan,
+ uint16_t on_tick, uint16_t off_tick)
+{
+ const uint8_t reg = chan->ch_number == PCA9685_ALL_CHANNELS ?
+ PCA9685_ALL_LED_ON_L : PCA9685_LEDx_ON_L(chan->ch_number);
+ uint8_t regs[4];
+
+ regs[0] = (uint8_t)(on_tick & 0xff);
+ regs[1] = (uint8_t)((on_tick >> 8) & 0xff);
+ regs[2] = (uint8_t)(off_tick & 0xff);
+ regs[3] = (uint8_t)((off_tick >> 8) & 0xff);
+
+ return pcapwm_write_LEDn(sc, reg, regs, sizeof(regs));
+}
+
+static int
+pcapwm_inspect_channel(struct pcapwm_softc * const sc,
+ struct pcapwm_channel * const chan,
+ uint16_t *on_tickp, uint16_t *off_tickp)
+{
+ const uint8_t reg = chan->ch_number == PCA9685_ALL_CHANNELS ?
+ PCA9685_ALL_LED_ON_L : PCA9685_LEDx_ON_L(chan->ch_number);
+ uint8_t regs[4];
+ int error;
+
+ error = pcapwm_read_LEDn(sc, reg, regs, sizeof(regs));
+ if (error) {
+ return error;
+ }
+
+ *on_tickp = regs[0] | (((uint16_t)regs[1]) << 8);
+ *off_tickp = regs[2] | (((uint16_t)regs[3]) << 8);
+
+ return 0;
+}
+
+static struct pcapwm_channel *
+pcapwm_lookup_channel(struct pcapwm_softc * const sc, u_int index)
+{
+
+ if (index > PCA9685_ALL_CHANNELS)
+ return NULL;
+
+ return &sc->sc_channels[index];
+}
+
+static void
+pcapwm_init_channel(struct pcapwm_softc * const sc, u_int index)
+{
+ struct pcapwm_channel * const chan = pcapwm_lookup_channel(sc, index);
+
+ KASSERT(chan != NULL);
+
+ chan->ch_number = index;
+
+ chan->ch_controller.pwm_enable = pcapwm_pwm_enable;
+ chan->ch_controller.pwm_get_config = pcapwm_pwm_get_config;
+ chan->ch_controller.pwm_set_config = pcapwm_pwm_set_config;
+
+ chan->ch_controller.pwm_dev = sc->sc_dev;
+ chan->ch_controller.pwm_priv = chan;
+}
+
+static pwm_tag_t
+pcapwm_get_tag(device_t dev, const void *data, size_t len)
+{
+ struct pcapwm_softc * const sc = device_private(dev);
+ const u_int *pwm = data;
+
+ /* #pwm-cells == 2 in the PCA9685 DT bindings. */
+ if (len != 12)
+ return NULL;
+
+ /* Channel 16 is the special call-channels channel. */
+ const u_int index = be32toh(pwm[1]);
+ struct pcapwm_channel * const chan = pcapwm_lookup_channel(sc, index);
+ if (chan == NULL)
+ return NULL;
+
+ const u_int period = be32toh(pwm[2]);
+
+ mutex_enter(&sc->sc_lock);
+
+ /*
+ * XXX Should we reflect the value of the "invert" property in
+ * pwm_config::polarity? I'm thinking not, but...
+ */
+ chan->ch_conf.period = period;
+ chan->ch_conf.polarity = PWM_ACTIVE_HIGH;
+
+ mutex_exit(&sc->sc_lock);
+
+ return &chan->ch_controller;
+}
+
+static struct fdtbus_pwm_controller_func pcapwm_pwm_funcs = {
+ .get_tag = pcapwm_get_tag,
+};
+
+static int
+pcapwm_pwm_enable(pwm_tag_t pwm, bool enable)
+{
+ struct pcapwm_softc * const sc = device_private(pwm->pwm_dev);
+ struct pcapwm_channel * const chan = pwm->pwm_priv;
+ int error;
+
+ if (enable) {
+ /* Set whatever is programmed for the channel. */
+ error = pwm_set_config(pwm, &chan->ch_conf);
+ if (error) {
+ device_printf(sc->sc_dev,
+ "enable: unable to set config for channel %u\n",
+ chan->ch_number);
+ }
+ return error;
+ }
+
+ mutex_enter(&sc->sc_lock);
+
+ error = pcapwm_program_channel(sc, chan, 0, PCA9685_PWM_TICKS);
+ if (error) {
+ device_printf(sc->sc_dev,
+ "disable: unable to program channel %u\n",
+ chan->ch_number);
+ }
+
+ mutex_exit(&sc->sc_lock);
+
+ return error;
+}
+
+static int
+pcapwm_pwm_get_config(pwm_tag_t pwm, struct pwm_config *conf)
+{
+ struct pcapwm_softc * const sc = device_private(pwm->pwm_dev);
Home |
Main Index |
Thread Index |
Old Index