Home | History | Annotate | Line # | Download | only in i2c
pca9685.c revision 1.1
      1  1.1  thorpej /* $NetBSD: pca9685.c,v 1.1 2019/07/24 05:25:32 thorpej Exp $ */
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2018, 2019 Jason R. Thorpe
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1  thorpej  *
     16  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  thorpej  */
     28  1.1  thorpej 
     29  1.1  thorpej #include <sys/cdefs.h>
     30  1.1  thorpej __KERNEL_RCSID(0, "$NetBSD: pca9685.c,v 1.1 2019/07/24 05:25:32 thorpej Exp $");
     31  1.1  thorpej 
     32  1.1  thorpej #include <sys/param.h>
     33  1.1  thorpej #include <sys/systm.h>
     34  1.1  thorpej #include <sys/device.h>
     35  1.1  thorpej #include <sys/conf.h>
     36  1.1  thorpej #include <sys/bus.h>
     37  1.1  thorpej #include <sys/kernel.h>
     38  1.1  thorpej #include <sys/kmem.h>
     39  1.1  thorpej #include <sys/mutex.h>
     40  1.1  thorpej #include <sys/proc.h>
     41  1.1  thorpej #include <sys/sysctl.h>
     42  1.1  thorpej 
     43  1.1  thorpej #include <dev/i2c/i2cvar.h>
     44  1.1  thorpej #include <dev/i2c/pca9685reg.h>
     45  1.1  thorpej 
     46  1.1  thorpej #include <dev/pwm/pwmvar.h>
     47  1.1  thorpej 
     48  1.1  thorpej #include <dev/fdt/fdtvar.h>
     49  1.1  thorpej 
     50  1.1  thorpej /*
     51  1.1  thorpej  * Special channel number used to indicate that we want to set the
     52  1.1  thorpej  * pulse mode for all channels on this controller.
     53  1.1  thorpej  */
     54  1.1  thorpej #define	PCA9685_ALL_CHANNELS	PCA9685_NCHANNELS
     55  1.1  thorpej 
     56  1.1  thorpej struct pcapwm_channel {
     57  1.1  thorpej 	struct pwm_controller	ch_controller;
     58  1.1  thorpej 	struct pwm_config	ch_conf;
     59  1.1  thorpej 	u_int			ch_number;
     60  1.1  thorpej };
     61  1.1  thorpej 
     62  1.1  thorpej struct pcapwm_softc {
     63  1.1  thorpej 	device_t	sc_dev;
     64  1.1  thorpej 	i2c_tag_t	sc_i2c;
     65  1.1  thorpej 	i2c_addr_t	sc_addr;
     66  1.1  thorpej 	int		sc_i2c_flags;
     67  1.1  thorpej 
     68  1.1  thorpej 	/*
     69  1.1  thorpej 	 * Locking order is:
     70  1.1  thorpej 	 *	pcapwm mutex -> i2c bus
     71  1.1  thorpej 	 */
     72  1.1  thorpej 	kmutex_t	sc_lock;
     73  1.1  thorpej 
     74  1.1  thorpej 	/*
     75  1.1  thorpej 	 * The PCA9685 only has a single pre-scaler, so the configured
     76  1.1  thorpej 	 * PWM frequency / period is shared by all channels.
     77  1.1  thorpej 	 */
     78  1.1  thorpej 	u_int		sc_period;	/* nanoseconds */
     79  1.1  thorpej 	u_int		sc_clk_freq;
     80  1.1  thorpej 	bool		sc_ext_clk;
     81  1.1  thorpej 	bool		sc_invert;	/* "invert" property specified */
     82  1.1  thorpej 	bool		sc_open_drain;	/* "open-drain" property specified */
     83  1.1  thorpej 
     84  1.1  thorpej 	/*
     85  1.1  thorpej 	 * +1 because we treat channel "16" as the all-channels
     86  1.1  thorpej 	 * pseudo-channel.
     87  1.1  thorpej 	 */
     88  1.1  thorpej 	struct pcapwm_channel sc_channels[PCA9685_NCHANNELS+1];
     89  1.1  thorpej };
     90  1.1  thorpej 
     91  1.1  thorpej static int	pcapwm_pwm_enable(struct pwm_controller *, bool);
     92  1.1  thorpej static int	pcapwm_pwm_get_config(struct pwm_controller *,
     93  1.1  thorpej 		    struct pwm_config *);
     94  1.1  thorpej static int	pcapwm_pwm_set_config(struct pwm_controller *,
     95  1.1  thorpej 		    const struct pwm_config *);
     96  1.1  thorpej 
     97  1.1  thorpej static const struct device_compatible_entry compat_data[] = {
     98  1.1  thorpej 	{ "nxp,pca9685-pwm",	0 },
     99  1.1  thorpej 	{ NULL }
    100  1.1  thorpej };
    101  1.1  thorpej 
    102  1.1  thorpej static int
    103  1.1  thorpej pcapwm_read1(struct pcapwm_softc * const sc, uint8_t reg, uint8_t *valp)
    104  1.1  thorpej {
    105  1.1  thorpej 
    106  1.1  thorpej 	return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    107  1.1  thorpej 			sc->sc_addr, &reg, sizeof(reg),
    108  1.1  thorpej 			valp, sizeof(*valp), cold ? I2C_F_POLL : 0);
    109  1.1  thorpej }
    110  1.1  thorpej 
    111  1.1  thorpej static int
    112  1.1  thorpej pcapwm_write1(struct pcapwm_softc * const sc, uint8_t reg, uint8_t val)
    113  1.1  thorpej {
    114  1.1  thorpej 
    115  1.1  thorpej 	return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    116  1.1  thorpej 			sc->sc_addr, &reg, sizeof(reg),
    117  1.1  thorpej 			&val, sizeof(val), cold ? I2C_F_POLL : 0);
    118  1.1  thorpej }
    119  1.1  thorpej 
    120  1.1  thorpej static int
    121  1.1  thorpej pcapwm_read_LEDn(struct pcapwm_softc * const sc, uint8_t reg, uint8_t *buf,
    122  1.1  thorpej 		 size_t buflen)
    123  1.1  thorpej {
    124  1.1  thorpej 
    125  1.1  thorpej 	/* We rely on register auto-increment being enabled. */
    126  1.1  thorpej 	return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    127  1.1  thorpej 			sc->sc_addr, &reg, sizeof(reg),
    128  1.1  thorpej 			buf, buflen, cold ? I2C_F_POLL : 0);
    129  1.1  thorpej }
    130  1.1  thorpej 
    131  1.1  thorpej static int
    132  1.1  thorpej pcapwm_write_LEDn(struct pcapwm_softc * const sc, uint8_t reg, uint8_t *buf,
    133  1.1  thorpej 		  size_t buflen)
    134  1.1  thorpej {
    135  1.1  thorpej 
    136  1.1  thorpej 	/* We rely on register auto-increment being enabled. */
    137  1.1  thorpej 	return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    138  1.1  thorpej 			sc->sc_addr, &reg, sizeof(reg),
    139  1.1  thorpej 			buf, buflen, cold ? I2C_F_POLL : 0);
    140  1.1  thorpej }
    141  1.1  thorpej 
    142  1.1  thorpej static int
    143  1.1  thorpej pcapwm_program_channel(struct pcapwm_softc * const sc,
    144  1.1  thorpej 		       struct pcapwm_channel * const chan,
    145  1.1  thorpej 		       uint16_t on_tick, uint16_t off_tick)
    146  1.1  thorpej {
    147  1.1  thorpej 	const uint8_t reg = chan->ch_number == PCA9685_ALL_CHANNELS ?
    148  1.1  thorpej 	    PCA9685_ALL_LED_ON_L : PCA9685_LEDx_ON_L(chan->ch_number);
    149  1.1  thorpej 	uint8_t regs[4];
    150  1.1  thorpej 
    151  1.1  thorpej 	regs[0] = (uint8_t)(on_tick & 0xff);
    152  1.1  thorpej 	regs[1] = (uint8_t)((on_tick >> 8) & 0xff);
    153  1.1  thorpej 	regs[2] = (uint8_t)(off_tick & 0xff);
    154  1.1  thorpej 	regs[3] = (uint8_t)((off_tick >> 8) & 0xff);
    155  1.1  thorpej 
    156  1.1  thorpej 	return pcapwm_write_LEDn(sc, reg, regs, sizeof(regs));
    157  1.1  thorpej }
    158  1.1  thorpej 
    159  1.1  thorpej static int
    160  1.1  thorpej pcapwm_inspect_channel(struct pcapwm_softc * const sc,
    161  1.1  thorpej 		       struct pcapwm_channel * const chan,
    162  1.1  thorpej 		       uint16_t *on_tickp, uint16_t *off_tickp)
    163  1.1  thorpej {
    164  1.1  thorpej 	const uint8_t reg = chan->ch_number == PCA9685_ALL_CHANNELS ?
    165  1.1  thorpej 	    PCA9685_ALL_LED_ON_L : PCA9685_LEDx_ON_L(chan->ch_number);
    166  1.1  thorpej 	uint8_t regs[4];
    167  1.1  thorpej 	int error;
    168  1.1  thorpej 
    169  1.1  thorpej 	error = pcapwm_read_LEDn(sc, reg, regs, sizeof(regs));
    170  1.1  thorpej 	if (error) {
    171  1.1  thorpej 		return error;
    172  1.1  thorpej 	}
    173  1.1  thorpej 
    174  1.1  thorpej 	*on_tickp = regs[0] | (((uint16_t)regs[1]) << 8);
    175  1.1  thorpej 	*off_tickp = regs[2] | (((uint16_t)regs[3]) << 8);
    176  1.1  thorpej 
    177  1.1  thorpej 	return 0;
    178  1.1  thorpej }
    179  1.1  thorpej 
    180  1.1  thorpej static struct pcapwm_channel *
    181  1.1  thorpej pcapwm_lookup_channel(struct pcapwm_softc * const sc, u_int index)
    182  1.1  thorpej {
    183  1.1  thorpej 
    184  1.1  thorpej 	if (index > PCA9685_ALL_CHANNELS)
    185  1.1  thorpej 		return NULL;
    186  1.1  thorpej 
    187  1.1  thorpej 	return &sc->sc_channels[index];
    188  1.1  thorpej }
    189  1.1  thorpej 
    190  1.1  thorpej static void
    191  1.1  thorpej pcapwm_init_channel(struct pcapwm_softc * const sc, u_int index)
    192  1.1  thorpej {
    193  1.1  thorpej 	struct pcapwm_channel * const chan = pcapwm_lookup_channel(sc, index);
    194  1.1  thorpej 
    195  1.1  thorpej 	KASSERT(chan != NULL);
    196  1.1  thorpej 
    197  1.1  thorpej 	chan->ch_number = index;
    198  1.1  thorpej 
    199  1.1  thorpej 	chan->ch_controller.pwm_enable = pcapwm_pwm_enable;
    200  1.1  thorpej 	chan->ch_controller.pwm_get_config = pcapwm_pwm_get_config;
    201  1.1  thorpej 	chan->ch_controller.pwm_set_config = pcapwm_pwm_set_config;
    202  1.1  thorpej 
    203  1.1  thorpej 	chan->ch_controller.pwm_dev = sc->sc_dev;
    204  1.1  thorpej 	chan->ch_controller.pwm_priv = chan;
    205  1.1  thorpej }
    206  1.1  thorpej 
    207  1.1  thorpej static pwm_tag_t
    208  1.1  thorpej pcapwm_get_tag(device_t dev, const void *data, size_t len)
    209  1.1  thorpej {
    210  1.1  thorpej 	struct pcapwm_softc * const sc = device_private(dev);
    211  1.1  thorpej 	const u_int *pwm = data;
    212  1.1  thorpej 
    213  1.1  thorpej 	/* #pwm-cells == 2 in the PCA9685 DT bindings. */
    214  1.1  thorpej 	if (len != 12)
    215  1.1  thorpej 		return NULL;
    216  1.1  thorpej 
    217  1.1  thorpej 	/* Channel 16 is the special call-channels channel. */
    218  1.1  thorpej 	const u_int index = be32toh(pwm[1]);
    219  1.1  thorpej 	struct pcapwm_channel * const chan = pcapwm_lookup_channel(sc, index);
    220  1.1  thorpej 	if (chan == NULL)
    221  1.1  thorpej 		return NULL;
    222  1.1  thorpej 
    223  1.1  thorpej 	const u_int period = be32toh(pwm[2]);
    224  1.1  thorpej 
    225  1.1  thorpej 	mutex_enter(&sc->sc_lock);
    226  1.1  thorpej 
    227  1.1  thorpej 	/*
    228  1.1  thorpej 	 * XXX Should we reflect the value of the "invert" property in
    229  1.1  thorpej 	 * pwm_config::polarity?  I'm thinking not, but...
    230  1.1  thorpej 	 */
    231  1.1  thorpej 	chan->ch_conf.period = period;
    232  1.1  thorpej 	chan->ch_conf.polarity = PWM_ACTIVE_HIGH;
    233  1.1  thorpej 
    234  1.1  thorpej 	mutex_exit(&sc->sc_lock);
    235  1.1  thorpej 
    236  1.1  thorpej 	return &chan->ch_controller;
    237  1.1  thorpej }
    238  1.1  thorpej 
    239  1.1  thorpej static struct fdtbus_pwm_controller_func pcapwm_pwm_funcs = {
    240  1.1  thorpej 	.get_tag = pcapwm_get_tag,
    241  1.1  thorpej };
    242  1.1  thorpej 
    243  1.1  thorpej static int
    244  1.1  thorpej pcapwm_pwm_enable(pwm_tag_t pwm, bool enable)
    245  1.1  thorpej {
    246  1.1  thorpej 	struct pcapwm_softc * const sc = device_private(pwm->pwm_dev);
    247  1.1  thorpej 	struct pcapwm_channel * const chan = pwm->pwm_priv;
    248  1.1  thorpej 	int error;
    249  1.1  thorpej 
    250  1.1  thorpej 	if (enable) {
    251  1.1  thorpej 		/* Set whatever is programmed for the channel. */
    252  1.1  thorpej 		error = pwm_set_config(pwm, &chan->ch_conf);
    253  1.1  thorpej 		if (error) {
    254  1.1  thorpej 			device_printf(sc->sc_dev,
    255  1.1  thorpej 			    "enable: unable to set config for channel %u\n",
    256  1.1  thorpej 			    chan->ch_number);
    257  1.1  thorpej 		}
    258  1.1  thorpej 		return error;
    259  1.1  thorpej 	}
    260  1.1  thorpej 
    261  1.1  thorpej 	mutex_enter(&sc->sc_lock);
    262  1.1  thorpej 
    263  1.1  thorpej 	error = pcapwm_program_channel(sc, chan, 0, PCA9685_PWM_TICKS);
    264  1.1  thorpej 	if (error) {
    265  1.1  thorpej 		device_printf(sc->sc_dev,
    266  1.1  thorpej 		    "disable: unable to program channel %u\n",
    267  1.1  thorpej 		    chan->ch_number);
    268  1.1  thorpej 	}
    269  1.1  thorpej 
    270  1.1  thorpej 	mutex_exit(&sc->sc_lock);
    271  1.1  thorpej 
    272  1.1  thorpej 	return error;
    273  1.1  thorpej }
    274  1.1  thorpej 
    275  1.1  thorpej static int
    276  1.1  thorpej pcapwm_pwm_get_config(pwm_tag_t pwm, struct pwm_config *conf)
    277  1.1  thorpej {
    278  1.1  thorpej 	struct pcapwm_softc * const sc = device_private(pwm->pwm_dev);
    279  1.1  thorpej 	struct pcapwm_channel * const chan = pwm->pwm_priv;
    280  1.1  thorpej 	uint16_t on_tick, off_tick;
    281  1.1  thorpej 	u_int duty_cycle;
    282  1.1  thorpej 	int error;
    283  1.1  thorpej 
    284  1.1  thorpej 	mutex_enter(&sc->sc_lock);
    285  1.1  thorpej 
    286  1.1  thorpej 	error = pcapwm_inspect_channel(sc, chan, &on_tick, &off_tick);
    287  1.1  thorpej 	if (error) {
    288  1.1  thorpej 		device_printf(sc->sc_dev,
    289  1.1  thorpej 		    "get_config: unable to inspect channel %u\n",
    290  1.1  thorpej 		        chan->ch_number);
    291  1.1  thorpej 		goto out;
    292  1.1  thorpej 	}
    293  1.1  thorpej 
    294  1.1  thorpej 	if (on_tick & PCA9685_PWM_TICKS) {
    295  1.1  thorpej 		duty_cycle = chan->ch_conf.period;
    296  1.1  thorpej 	} else if (off_tick & PCA9685_PWM_TICKS) {
    297  1.1  thorpej 		duty_cycle = 0;
    298  1.1  thorpej 	} else {
    299  1.1  thorpej 		/*
    300  1.1  thorpej 		 * Compute the number of ticks, accounting for a non-zero
    301  1.1  thorpej 		 * on-tick (which the hardware can do, even if the software
    302  1.1  thorpej 		 * can't).
    303  1.1  thorpej 		 */
    304  1.1  thorpej 		int signed_off_tick = off_tick;
    305  1.1  thorpej 		signed_off_tick -= on_tick;
    306  1.1  thorpej 		if (signed_off_tick < 0)
    307  1.1  thorpej 			signed_off_tick += PCA9685_PWM_TICKS;
    308  1.1  thorpej 		const uint64_t nticks = signed_off_tick;
    309  1.1  thorpej 		duty_cycle = (u_int)((nticks * chan->ch_conf.period) /
    310  1.1  thorpej 		    PCA9685_PWM_TICKS);
    311  1.1  thorpej 	}
    312  1.1  thorpej 
    313  1.1  thorpej 	*conf = chan->ch_conf;
    314  1.1  thorpej 	conf->duty_cycle = duty_cycle;
    315  1.1  thorpej 
    316  1.1  thorpej  out:
    317  1.1  thorpej 	mutex_exit(&sc->sc_lock);
    318  1.1  thorpej 
    319  1.1  thorpej 	return error;
    320  1.1  thorpej }
    321  1.1  thorpej 
    322  1.1  thorpej static int
    323  1.1  thorpej pcapwm_pwm_set_config(pwm_tag_t pwm, const struct pwm_config *conf)
    324  1.1  thorpej {
    325  1.1  thorpej 	struct pcapwm_softc * const sc = device_private(pwm->pwm_dev);
    326  1.1  thorpej 	struct pcapwm_channel * const chan = pwm->pwm_priv;
    327  1.1  thorpej 	int error = 0;
    328  1.1  thorpej 
    329  1.1  thorpej 	mutex_enter(&sc->sc_lock);
    330  1.1  thorpej 
    331  1.1  thorpej 	/* Only active-high is supported. */
    332  1.1  thorpej 	if (conf->polarity != PWM_ACTIVE_HIGH) {
    333  1.1  thorpej 		device_printf(sc->sc_dev,
    334  1.1  thorpej 		    "set_config: invalid polarity: %d\n", conf->polarity);
    335  1.1  thorpej 		error = EINVAL;
    336  1.1  thorpej 		goto out;
    337  1.1  thorpej 	}
    338  1.1  thorpej 
    339  1.1  thorpej 	if (sc->sc_period != conf->period) {
    340  1.1  thorpej 		/*
    341  1.1  thorpej 		 * Formula for the prescale is:
    342  1.1  thorpej 		 *
    343  1.1  thorpej 		 *           (     clk_freq      )
    344  1.1  thorpej 		 *      round( ----------------- ) - 1
    345  1.1  thorpej 		 *           (  4096 * pwm_freq  )
    346  1.1  thorpej 		 *
    347  1.1  thorpej 		 * pwm_freq == 1000000000 / period.
    348  1.1  thorpej 		 *
    349  1.1  thorpej 		 * To do the rounding step, we scale the oscillator_freq
    350  1.1  thorpej 		 * by 100, check for the rounding condition, and then
    351  1.1  thorpej 		 * de-scale before the subtraction step.
    352  1.1  thorpej 		 */
    353  1.1  thorpej 		const u_int pwm_freq = 1000000000 / conf->period;
    354  1.1  thorpej 		u_int prescale = (sc->sc_clk_freq * 100) /
    355  1.1  thorpej 		    (PCA9685_PWM_TICKS * pwm_freq);
    356  1.1  thorpej 		if ((prescale % 100) >= 50)
    357  1.1  thorpej 			prescale += 100;
    358  1.1  thorpej 		prescale = (prescale / 100) - 1;
    359  1.1  thorpej 		if (prescale < PCA9685_PRESCALE_MIN ||
    360  1.1  thorpej 		    prescale > PCA9685_PRESCALE_MAX) {
    361  1.1  thorpej 			device_printf(sc->sc_dev,
    362  1.1  thorpej 			    "set_config: invalid period: %uns\n", conf->period);
    363  1.1  thorpej 			error = EINVAL;
    364  1.1  thorpej 			goto out;
    365  1.1  thorpej 		}
    366  1.1  thorpej 
    367  1.1  thorpej 		uint8_t mode1;
    368  1.1  thorpej 		error = pcapwm_read1(sc, PCA9685_MODE1, &mode1);
    369  1.1  thorpej 		if (error) {
    370  1.1  thorpej 			device_printf(sc->sc_dev,
    371  1.1  thorpej 			    "set_config: unable to read MODE1\n");
    372  1.1  thorpej 			goto out;
    373  1.1  thorpej 		}
    374  1.1  thorpej 
    375  1.1  thorpej 		/* Disable the internal oscillator. */
    376  1.1  thorpej 		mode1 |= MODE1_SLEEP;
    377  1.1  thorpej 		error = pcapwm_write1(sc, PCA9685_MODE1, mode1);
    378  1.1  thorpej 		if (error) {
    379  1.1  thorpej 			device_printf(sc->sc_dev,
    380  1.1  thorpej 			    "set_config: unable to write MODE1\n");
    381  1.1  thorpej 			goto out;
    382  1.1  thorpej 		}
    383  1.1  thorpej 
    384  1.1  thorpej 		/* Update the prescale register. */
    385  1.1  thorpej 		error = pcapwm_write1(sc, PCA9685_PRE_SCALE,
    386  1.1  thorpej 		    (uint8_t)(prescale & 0xff));
    387  1.1  thorpej 		if (error) {
    388  1.1  thorpej 			device_printf(sc->sc_dev,
    389  1.1  thorpej 			    "set_config: unable to write PRE_SCALE\n");
    390  1.1  thorpej 			goto out;
    391  1.1  thorpej 		}
    392  1.1  thorpej 
    393  1.1  thorpej 		/*
    394  1.1  thorpej 		 * If we're using an external clock source, keep the
    395  1.1  thorpej 		 * internal oscillator turned off.
    396  1.1  thorpej 		 *
    397  1.1  thorpej 		 * XXX The datasheet is a little ambiguous about how this
    398  1.1  thorpej 		 * XXX is supposed to work -- on the same page it says to
    399  1.1  thorpej 		 * XXX perform this procedure, and also that PWM control of
    400  1.1  thorpej 		 * XXX the channels is not possible when the oscillator is
    401  1.1  thorpej 		 * XXX disabled.  I haven't tested this with an external
    402  1.1  thorpej 		 * XXX oscillator yet, so I don't know for sure.
    403  1.1  thorpej 		 */
    404  1.1  thorpej 		if (sc->sc_ext_clk) {
    405  1.1  thorpej 			mode1 |= MODE1_EXTCLK;
    406  1.1  thorpej 		} else {
    407  1.1  thorpej 			mode1 &= ~MODE1_SLEEP;
    408  1.1  thorpej 		}
    409  1.1  thorpej 
    410  1.1  thorpej 		/*
    411  1.1  thorpej 		 * We rely on auto-increment for the PWM register updates.
    412  1.1  thorpej 		 */
    413  1.1  thorpej 		mode1 |= MODE1_AI;
    414  1.1  thorpej 
    415  1.1  thorpej 		error = pcapwm_write1(sc, PCA9685_MODE1, mode1);
    416  1.1  thorpej 		if (error) {
    417  1.1  thorpej 			device_printf(sc->sc_dev,
    418  1.1  thorpej 			    "set_config: unable to write MODE1\n");
    419  1.1  thorpej 			goto out;
    420  1.1  thorpej 		}
    421  1.1  thorpej 
    422  1.1  thorpej 		if (sc->sc_ext_clk == false) {
    423  1.1  thorpej 			/* Wait for 500us for the clock to settle. */
    424  1.1  thorpej 			delay(500);
    425  1.1  thorpej 		}
    426  1.1  thorpej 
    427  1.1  thorpej 		sc->sc_period = conf->period;
    428  1.1  thorpej 	}
    429  1.1  thorpej 
    430  1.1  thorpej 	uint16_t on_tick, off_tick;
    431  1.1  thorpej 
    432  1.1  thorpej 	/*
    433  1.1  thorpej 	 * The PWM framework doesn't support the phase-shift / start-delay
    434  1.1  thorpej 	 * feature of this chip, so all duty cycles start at 0 ticks.
    435  1.1  thorpej 	 */
    436  1.1  thorpej 
    437  1.1  thorpej 	/*
    438  1.1  thorpej 	 * For full-on and full-off, use the magic FULL-{ON,OFF} values
    439  1.1  thorpej 	 * described in the data sheet.
    440  1.1  thorpej 	 */
    441  1.1  thorpej 	if (conf->duty_cycle == 0) {
    442  1.1  thorpej 		on_tick = 0;
    443  1.1  thorpej 		off_tick = PCA9685_PWM_TICKS;
    444  1.1  thorpej 	} else if (conf->duty_cycle == sc->sc_period) {
    445  1.1  thorpej 		on_tick = PCA9685_PWM_TICKS;
    446  1.1  thorpej 		off_tick = 0;
    447  1.1  thorpej 	} else {
    448  1.1  thorpej 		uint64_t ticks =
    449  1.1  thorpej 		    PCA9685_PWM_TICKS * (uint64_t)conf->duty_cycle;
    450  1.1  thorpej 		/* Scale up so we can check if we need to round. */
    451  1.1  thorpej 		ticks = (ticks * 100) / sc->sc_period;
    452  1.1  thorpej 		/* Round up. */
    453  1.1  thorpej 		if (ticks % 100)
    454  1.1  thorpej 			ticks += 100;
    455  1.1  thorpej 		ticks /= 100;
    456  1.1  thorpej 		if (ticks >= PCA9685_PWM_TICKS) {
    457  1.1  thorpej 			ticks = PCA9685_PWM_TICKS - 1;
    458  1.1  thorpej 		}
    459  1.1  thorpej 
    460  1.1  thorpej 		on_tick = 0;
    461  1.1  thorpej 		off_tick = (u_int)ticks;
    462  1.1  thorpej 	}
    463  1.1  thorpej 
    464  1.1  thorpej 	error = pcapwm_program_channel(sc, chan, on_tick, off_tick);
    465  1.1  thorpej 	if (error) {
    466  1.1  thorpej 		device_printf(sc->sc_dev,
    467  1.1  thorpej 		    "set_config: unable to program channel %u\n",
    468  1.1  thorpej 		    chan->ch_number);
    469  1.1  thorpej 		goto out;
    470  1.1  thorpej 	}
    471  1.1  thorpej 
    472  1.1  thorpej 	chan->ch_conf = *conf;
    473  1.1  thorpej 
    474  1.1  thorpej  out:
    475  1.1  thorpej 	mutex_exit(&sc->sc_lock);
    476  1.1  thorpej 
    477  1.1  thorpej 	return error;
    478  1.1  thorpej }
    479  1.1  thorpej 
    480  1.1  thorpej static int
    481  1.1  thorpej pcapwm_match(device_t parent, cfdata_t cf, void *aux)
    482  1.1  thorpej {
    483  1.1  thorpej 	struct i2c_attach_args * const ia = aux;
    484  1.1  thorpej 	int match_result;
    485  1.1  thorpej 
    486  1.1  thorpej 	if (iic_use_direct_match(ia, cf, compat_data, &match_result)) {
    487  1.1  thorpej 		return match_result;
    488  1.1  thorpej 	}
    489  1.1  thorpej 
    490  1.1  thorpej 	/* This device is direct-config only. */
    491  1.1  thorpej 
    492  1.1  thorpej 	return 0;
    493  1.1  thorpej }
    494  1.1  thorpej 
    495  1.1  thorpej static void
    496  1.1  thorpej pcapwm_attach(device_t parent, device_t self, void *aux)
    497  1.1  thorpej {
    498  1.1  thorpej 	struct pcapwm_softc * const sc = device_private(self);
    499  1.1  thorpej 	struct i2c_attach_args * const ia = aux;
    500  1.1  thorpej 	struct clk *clk;
    501  1.1  thorpej 	const int phandle = (int)ia->ia_cookie;
    502  1.1  thorpej 	u_int index;
    503  1.1  thorpej 	int error;
    504  1.1  thorpej 
    505  1.1  thorpej 	sc->sc_dev = self;
    506  1.1  thorpej 	sc->sc_i2c = ia->ia_tag;
    507  1.1  thorpej 	sc->sc_addr = ia->ia_addr;
    508  1.1  thorpej 
    509  1.1  thorpej 	aprint_naive("\n");
    510  1.1  thorpej 	aprint_normal(": PCA9685 PWM controller\n");
    511  1.1  thorpej 
    512  1.1  thorpej 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    513  1.1  thorpej 
    514  1.1  thorpej 	for (index = 0; index <= PCA9685_ALL_CHANNELS; index++) {
    515  1.1  thorpej 		pcapwm_init_channel(sc, index);
    516  1.1  thorpej 	}
    517  1.1  thorpej 
    518  1.1  thorpej 	clk = fdtbus_clock_get_index(phandle, 0);
    519  1.1  thorpej 	if (clk != NULL) {
    520  1.1  thorpej 		sc->sc_ext_clk = true;
    521  1.1  thorpej 		sc->sc_clk_freq = clk_get_rate(clk);
    522  1.1  thorpej 	} else {
    523  1.1  thorpej 		sc->sc_clk_freq = PCA9685_INTERNAL_FREQ;
    524  1.1  thorpej 	}
    525  1.1  thorpej 
    526  1.1  thorpej 	if (of_hasprop(phandle, "invert")) {
    527  1.1  thorpej 		sc->sc_invert = true;
    528  1.1  thorpej 	}
    529  1.1  thorpej 
    530  1.1  thorpej 	if (of_hasprop(phandle, "open-drain")) {
    531  1.1  thorpej 		sc->sc_open_drain = true;
    532  1.1  thorpej 	}
    533  1.1  thorpej 
    534  1.1  thorpej 	/*
    535  1.1  thorpej 	 * XXX No DT bindings for the OUTNEx configurations in
    536  1.1  thorpej 	 * MODE2.
    537  1.1  thorpej 	 */
    538  1.1  thorpej 
    539  1.1  thorpej 	/*
    540  1.1  thorpej 	 * Set up the outputs.  We want the channel to update when
    541  1.1  thorpej 	 * we send the I2C "STOP" condition.
    542  1.1  thorpej 	 */
    543  1.1  thorpej 	uint8_t mode2;
    544  1.1  thorpej 	error = pcapwm_read1(sc, PCA9685_MODE2, &mode2);
    545  1.1  thorpej 	if (error == 0) {
    546  1.1  thorpej 		mode2 &= ~(MODE2_OUTDRV | MODE2_OCH | MODE2_INVRT);
    547  1.1  thorpej 		if (sc->sc_invert) {
    548  1.1  thorpej 			mode2 |= MODE2_INVRT;
    549  1.1  thorpej 		}
    550  1.1  thorpej 		if (sc->sc_open_drain == false) {
    551  1.1  thorpej 			mode2 |= MODE2_OUTDRV;
    552  1.1  thorpej 		}
    553  1.1  thorpej 		error = pcapwm_write1(sc, PCA9685_MODE2, mode2);
    554  1.1  thorpej 	}
    555  1.1  thorpej 	if (error) {
    556  1.1  thorpej 		aprint_error_dev(sc->sc_dev, "failed to configure MODE2\n");
    557  1.1  thorpej 		return;
    558  1.1  thorpej 	}
    559  1.1  thorpej 
    560  1.1  thorpej 	fdtbus_register_pwm_controller(self, phandle,
    561  1.1  thorpej 	    &pcapwm_pwm_funcs);
    562  1.1  thorpej }
    563  1.1  thorpej 
    564  1.1  thorpej CFATTACH_DECL_NEW(pcapwm, sizeof(struct pcapwm_softc),
    565  1.1  thorpej     pcapwm_match, pcapwm_attach, NULL, NULL);
    566