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