/*-
* 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.
*/
/*
* 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
/*
* 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 *);
/* #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;
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);
}
/* Only active-high is supported. */
if (conf->polarity != PWM_ACTIVE_HIGH) {
device_printf(sc->sc_dev,
"set_config: invalid polarity: %d\n", conf->polarity);
error = EINVAL;
goto out;
}
if (sc->sc_period != conf->period) {
/*
* Formula for the prescale is:
*
* ( clk_freq )
* round( ----------------- ) - 1
* ( 4096 * pwm_freq )
*
* pwm_freq == 1000000000 / period.
*
* To do the rounding step, we scale the oscillator_freq
* by 100, check for the rounding condition, and then
* de-scale before the subtraction step.
*/
const u_int pwm_freq = 1000000000 / conf->period;
u_int prescale = (sc->sc_clk_freq * 100) /
(PCA9685_PWM_TICKS * pwm_freq);
if ((prescale % 100) >= 50)
prescale += 100;
prescale = (prescale / 100) - 1;
if (prescale < PCA9685_PRESCALE_MIN ||
prescale > PCA9685_PRESCALE_MAX) {
device_printf(sc->sc_dev,
"set_config: invalid period: %uns\n", conf->period);
error = EINVAL;
goto out;
}
error = iic_acquire_bus(sc->sc_i2c, 0);
if (error) {
device_printf(sc->sc_dev,
"set_config: unable to acquire I2C bus\n");
goto out;
}
uint8_t mode1;
error = pcapwm_read1(sc, PCA9685_MODE1, &mode1);
if (error) {
device_printf(sc->sc_dev,
"set_config: unable to read MODE1\n");
goto out_release_i2c;
}
/* Disable the internal oscillator. */
mode1 |= MODE1_SLEEP;
error = pcapwm_write1(sc, PCA9685_MODE1, mode1);
if (error) {
device_printf(sc->sc_dev,
"set_config: unable to write MODE1\n");
goto out_release_i2c;
}
/* Update the prescale register. */
error = pcapwm_write1(sc, PCA9685_PRE_SCALE,
(uint8_t)(prescale & 0xff));
if (error) {
device_printf(sc->sc_dev,
"set_config: unable to write PRE_SCALE\n");
goto out_release_i2c;
}
/*
* If we're using an external clock source, keep the
* internal oscillator turned off.
*
* XXX The datasheet is a little ambiguous about how this
* XXX is supposed to work -- on the same page it says to
* XXX perform this procedure, and also that PWM control of
* XXX the channels is not possible when the oscillator is
* XXX disabled. I haven't tested this with an external
* XXX oscillator yet, so I don't know for sure.
*/
if (sc->sc_ext_clk) {
mode1 |= MODE1_EXTCLK;
} else {
mode1 &= ~MODE1_SLEEP;
}
/*
* We rely on auto-increment for the PWM register updates.
*/
mode1 |= MODE1_AI;
error = pcapwm_write1(sc, PCA9685_MODE1, mode1);
if (error) {
device_printf(sc->sc_dev,
"set_config: unable to write MODE1\n");
goto out_release_i2c;
}
iic_release_bus(sc->sc_i2c, 0);
if (sc->sc_ext_clk == false) {
/* Wait for 500us for the clock to settle. */
delay(500);
}
sc->sc_period = conf->period;
}
uint16_t on_tick, off_tick;
/*
* The PWM framework doesn't support the phase-shift / start-delay
* feature of this chip, so all duty cycles start at 0 ticks.
*/
/*
* For full-on and full-off, use the magic FULL-{ON,OFF} values
* described in the data sheet.
*/
if (conf->duty_cycle == 0) {
on_tick = 0;
off_tick = PCA9685_PWM_TICKS;
} else if (conf->duty_cycle == sc->sc_period) {
on_tick = PCA9685_PWM_TICKS;
off_tick = 0;
} else {
uint64_t ticks =
PCA9685_PWM_TICKS * (uint64_t)conf->duty_cycle;
/* Scale up so we can check if we need to round. */
ticks = (ticks * 100) / sc->sc_period;
/* Round up. */
if (ticks % 100)
ticks += 100;
ticks /= 100;
if (ticks >= PCA9685_PWM_TICKS) {
ticks = PCA9685_PWM_TICKS - 1;
}
on_tick = 0;
off_tick = (u_int)ticks;
}
error = pcapwm_program_channel(sc, chan, on_tick, off_tick);
if (error) {
device_printf(sc->sc_dev,
"set_config: unable to program channel %u\n",
chan->ch_number);
goto out;
}