| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Driver for PCA9685 16-channel 12-bit PWM LED controller |
| 4 |
* |
| 5 |
* Copyright (C) 2013 Steffen Trumtrar <[email protected]> |
| 6 |
* Copyright (C) 2015 Clemens Gruber <[email protected]> |
| 7 |
* |
| 8 |
* based on the pwm-twl-led.c driver |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <linux/gpio/driver.h> |
| 12 |
#include <linux/i2c.h> |
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/mutex.h> |
| 15 |
#include <linux/platform_device.h> |
| 16 |
#include <linux/property.h> |
| 17 |
#include <linux/pwm.h> |
| 18 |
#include <linux/regmap.h> |
| 19 |
#include <linux/slab.h> |
| 20 |
#include <linux/delay.h> |
| 21 |
#include <linux/pm_runtime.h> |
| 22 |
#include <linux/bitmap.h> |
| 23 |
|
| 24 |
/* |
| 25 |
* Because the PCA9685 has only one prescaler per chip, only the first channel |
| 26 |
* that is enabled is allowed to change the prescale register. |
| 27 |
* PWM channels requested afterwards must use a period that results in the same |
| 28 |
* prescale setting as the one set by the first requested channel. |
| 29 |
*/ |
| 30 |
|
| 31 |
#define PCA9685_MODE1 0x00 |
| 32 |
#define PCA9685_MODE2 0x01 |
| 33 |
#define PCA9685_SUBADDR1 0x02 |
| 34 |
#define PCA9685_SUBADDR2 0x03 |
| 35 |
#define PCA9685_SUBADDR3 0x04 |
| 36 |
#define PCA9685_ALLCALLADDR 0x05 |
| 37 |
#define PCA9685_LEDX_ON_L 0x06 |
| 38 |
#define PCA9685_LEDX_ON_H 0x07 |
| 39 |
#define PCA9685_LEDX_OFF_L 0x08 |
| 40 |
#define PCA9685_LEDX_OFF_H 0x09 |
| 41 |
|
| 42 |
#define PCA9685_ALL_LED_ON_L 0xFA |
| 43 |
#define PCA9685_ALL_LED_ON_H 0xFB |
| 44 |
#define PCA9685_ALL_LED_OFF_L 0xFC |
| 45 |
#define PCA9685_ALL_LED_OFF_H 0xFD |
| 46 |
#define PCA9685_PRESCALE 0xFE |
| 47 |
|
| 48 |
#define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */ |
| 49 |
#define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */ |
| 50 |
|
| 51 |
#define PCA9685_COUNTER_RANGE 4096 |
| 52 |
#define PCA9685_OSC_CLOCK_HZ 25000000 /* Internal oscillator with 25 MHz */ |
| 53 |
|
| 54 |
/* |
| 55 |
* The time value of one counter tick. Note that NSEC_PER_SEC is an integer |
| 56 |
* multiple of PCA9685_OSC_CLOCK_HZ, so there is no rounding involved and we're |
| 57 |
* not loosing precision due to the early division. |
| 58 |
*/ |
| 59 |
#define PCA9685_QUANTUM_NS(_prescale) ((NSEC_PER_SEC / PCA9685_OSC_CLOCK_HZ) * (_prescale + 1)) |
| 60 |
|
| 61 |
#define PCA9685_NUMREGS 0xFF |
| 62 |
#define PCA9685_MAXCHAN 0x10 |
| 63 |
|
| 64 |
#define LED_FULL BIT(4) |
| 65 |
#define MODE1_ALLCALL BIT(0) |
| 66 |
#define MODE1_SUB3 BIT(1) |
| 67 |
#define MODE1_SUB2 BIT(2) |
| 68 |
#define MODE1_SUB1 BIT(3) |
| 69 |
#define MODE1_SLEEP BIT(4) |
| 70 |
#define MODE1_AI BIT(5) |
| 71 |
|
| 72 |
#define MODE2_INVRT BIT(4) |
| 73 |
#define MODE2_OUTDRV BIT(2) |
| 74 |
|
| 75 |
#define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N))) |
| 76 |
#define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N))) |
| 77 |
#define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N))) |
| 78 |
#define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N))) |
| 79 |
|
| 80 |
#define REG_ON_H(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_ON_H : LED_N_ON_H((C))) |
| 81 |
#define REG_ON_L(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_ON_L : LED_N_ON_L((C))) |
| 82 |
#define REG_OFF_H(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_OFF_H : LED_N_OFF_H((C))) |
| 83 |
#define REG_OFF_L(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_OFF_L : LED_N_OFF_L((C))) |
| 84 |
|
| 85 |
struct pca9685 { |
| 86 |
struct regmap *regmap; |
| 87 |
struct mutex lock; |
| 88 |
DECLARE_BITMAP(pwms_enabled, PCA9685_MAXCHAN + 1); |
| 89 |
}; |
| 90 |
|
| 91 |
static inline struct pca9685 *to_pca(struct pwm_chip *chip) |
| 92 |
{ |
| 93 |
return pwmchip_get_drvdata(chip); |
| 94 |
} |
| 95 |
|
| 96 |
/* This function is supposed to be called with the lock mutex held */ |
| 97 |
static bool pca9685_prescaler_can_change(struct pca9685 *pca, int channel) |
| 98 |
{ |
| 99 |
/* No PWM enabled: Change allowed */ |
| 100 |
if (bitmap_empty(pca->pwms_enabled, PCA9685_MAXCHAN + 1)) |
| 101 |
return true; |
| 102 |
/* More than one PWM enabled: Change not allowed */ |
| 103 |
if (bitmap_weight(pca->pwms_enabled, PCA9685_MAXCHAN + 1) > 1) |
| 104 |
return false; |
| 105 |
/* |
| 106 |
* Only one PWM enabled: Change allowed if the PWM about to |
| 107 |
* be changed is the one that is already enabled |
| 108 |
*/ |
| 109 |
return test_bit(channel, pca->pwms_enabled); |
| 110 |
} |
| 111 |
|
| 112 |
static int pca9685_read_reg(struct pwm_chip *chip, unsigned int reg, unsigned int *val) |
| 113 |
{ |
| 114 |
struct pca9685 *pca = to_pca(chip); |
| 115 |
struct device *dev = pwmchip_parent(chip); |
| 116 |
int err; |
| 117 |
|
| 118 |
err = regmap_read(pca->regmap, reg, val); |
| 119 |
if (err) |
| 120 |
dev_err(dev, "regmap_read of register 0x%x failed: %pe\n", reg, ERR_PTR(err)); |
| 121 |
|
| 122 |
return err; |
| 123 |
} |
| 124 |
|
| 125 |
static int pca9685_write_reg(struct pwm_chip *chip, unsigned int reg, unsigned int val) |
| 126 |
{ |
| 127 |
struct pca9685 *pca = to_pca(chip); |
| 128 |
struct device *dev = pwmchip_parent(chip); |
| 129 |
int err; |
| 130 |
|
| 131 |
err = regmap_write(pca->regmap, reg, val); |
| 132 |
if (err) |
| 133 |
dev_err(dev, "regmap_write to register 0x%x failed: %pe\n", reg, ERR_PTR(err)); |
| 134 |
|
| 135 |
return err; |
| 136 |
} |
| 137 |
|
| 138 |
static int pca9685_write_4reg(struct pwm_chip *chip, unsigned int reg, u8 val[4]) |
| 139 |
{ |
| 140 |
struct pca9685 *pca = to_pca(chip); |
| 141 |
struct device *dev = pwmchip_parent(chip); |
| 142 |
int err; |
| 143 |
|
| 144 |
err = regmap_bulk_write(pca->regmap, reg, val, 4); |
| 145 |
if (err) |
| 146 |
dev_err(dev, "regmap_write to register 0x%x failed: %pe\n", reg, ERR_PTR(err)); |
| 147 |
|
| 148 |
return err; |
| 149 |
} |
| 150 |
|
| 151 |
static int pca9685_set_sleep_mode(struct pwm_chip *chip, bool enable) |
| 152 |
{ |
| 153 |
struct pca9685 *pca = to_pca(chip); |
| 154 |
int err; |
| 155 |
|
| 156 |
err = regmap_update_bits(pca->regmap, PCA9685_MODE1, |
| 157 |
MODE1_SLEEP, enable ? MODE1_SLEEP : 0); |
| 158 |
if (err) |
| 159 |
return err; |
| 160 |
|
| 161 |
if (!enable) { |
| 162 |
/* Wait 500us for the oscillator to be back up */ |
| 163 |
udelay(500); |
| 164 |
} |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
struct pca9685_waveform { |
| 170 |
u8 onoff[4]; |
| 171 |
u8 prescale; |
| 172 |
}; |
| 173 |
|
| 174 |
static int pca9685_round_waveform_tohw(struct pwm_chip *chip, struct pwm_device *pwm, const struct pwm_waveform *wf, void *_wfhw) |
| 175 |
{ |
| 176 |
struct pca9685_waveform *wfhw = _wfhw; |
| 177 |
struct pca9685 *pca = to_pca(chip); |
| 178 |
unsigned int best_prescale; |
| 179 |
u8 prescale; |
| 180 |
unsigned int period_ns, duty; |
| 181 |
int ret_tohw = 0; |
| 182 |
|
| 183 |
if (!wf->period_length_ns) { |
| 184 |
*wfhw = (typeof(*wfhw)){ |
| 185 |
.onoff = { 0, 0, 0, LED_FULL, }, |
| 186 |
.prescale = 0, |
| 187 |
}; |
| 188 |
|
| 189 |
dev_dbg(&chip->dev, "pwm#%u: %lld/%lld [+%lld] -> [%hhx %hhx %hhx %hhx] PSC:%hhx\n", |
| 190 |
pwm->hwpwm, wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, |
| 191 |
wfhw->onoff[0], wfhw->onoff[1], wfhw->onoff[2], wfhw->onoff[3], wfhw->prescale); |
| 192 |
|
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
if (wf->period_length_ns >= PCA9685_COUNTER_RANGE * PCA9685_QUANTUM_NS(255)) { |
| 197 |
best_prescale = 255; |
| 198 |
} else if (wf->period_length_ns < PCA9685_COUNTER_RANGE * PCA9685_QUANTUM_NS(3)) { |
| 199 |
best_prescale = 3; |
| 200 |
ret_tohw = 1; |
| 201 |
} else { |
| 202 |
best_prescale = (unsigned int)wf->period_length_ns / (PCA9685_COUNTER_RANGE * (NSEC_PER_SEC / PCA9685_OSC_CLOCK_HZ)) - 1; |
| 203 |
} |
| 204 |
|
| 205 |
guard(mutex)(&pca->lock); |
| 206 |
|
| 207 |
if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) { |
| 208 |
unsigned int current_prescale; |
| 209 |
int ret; |
| 210 |
|
| 211 |
ret = regmap_read(pca->regmap, PCA9685_PRESCALE, ¤t_prescale); |
| 212 |
if (ret) |
| 213 |
return ret; |
| 214 |
|
| 215 |
if (current_prescale > best_prescale) |
| 216 |
ret_tohw = 1; |
| 217 |
|
| 218 |
prescale = current_prescale; |
| 219 |
} else { |
| 220 |
prescale = best_prescale; |
| 221 |
} |
| 222 |
|
| 223 |
period_ns = PCA9685_COUNTER_RANGE * PCA9685_QUANTUM_NS(prescale); |
| 224 |
|
| 225 |
duty = (unsigned)min_t(u64, wf->duty_length_ns, period_ns) / PCA9685_QUANTUM_NS(prescale); |
| 226 |
|
| 227 |
if (duty < PCA9685_COUNTER_RANGE) { |
| 228 |
unsigned int on, off; |
| 229 |
|
| 230 |
on = (unsigned)min_t(u64, wf->duty_offset_ns, period_ns) / PCA9685_QUANTUM_NS(prescale); |
| 231 |
off = (on + duty) % PCA9685_COUNTER_RANGE; |
| 232 |
|
| 233 |
/* |
| 234 |
* With a zero duty cycle, it doesn't matter if period was |
| 235 |
* rounded up |
| 236 |
*/ |
| 237 |
if (!duty) |
| 238 |
ret_tohw = 0; |
| 239 |
|
| 240 |
*wfhw = (typeof(*wfhw)){ |
| 241 |
.onoff = { on & 0xff, (on >> 8) & 0xf, off & 0xff, (off >> 8) & 0xf }, |
| 242 |
.prescale = prescale, |
| 243 |
}; |
| 244 |
} else { |
| 245 |
*wfhw = (typeof(*wfhw)){ |
| 246 |
.onoff = { 0, LED_FULL, 0, 0, }, |
| 247 |
.prescale = prescale, |
| 248 |
}; |
| 249 |
} |
| 250 |
|
| 251 |
dev_dbg(&chip->dev, "pwm#%u: %lld/%lld [+%lld] -> %s[%hhx %hhx %hhx %hhx] PSC:%hhx\n", |
| 252 |
pwm->hwpwm, wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, |
| 253 |
ret_tohw ? "#" : "", wfhw->onoff[0], wfhw->onoff[1], wfhw->onoff[2], wfhw->onoff[3], wfhw->prescale); |
| 254 |
|
| 255 |
return ret_tohw; |
| 256 |
} |
| 257 |
|
| 258 |
static int pca9685_round_waveform_fromhw(struct pwm_chip *chip, struct pwm_device *pwm, |
| 259 |
const void *_wfhw, struct pwm_waveform *wf) |
| 260 |
{ |
| 261 |
const struct pca9685_waveform *wfhw = _wfhw; |
| 262 |
struct pca9685 *pca = to_pca(chip); |
| 263 |
unsigned int prescale; |
| 264 |
|
| 265 |
if (wfhw->prescale) |
| 266 |
prescale = wfhw->prescale; |
| 267 |
else |
| 268 |
scoped_guard(mutex, &pca->lock) { |
| 269 |
int ret; |
| 270 |
|
| 271 |
ret = regmap_read(pca->regmap, PCA9685_PRESCALE, &prescale); |
| 272 |
if (ret) |
| 273 |
return ret; |
| 274 |
} |
| 275 |
|
| 276 |
wf->period_length_ns = PCA9685_COUNTER_RANGE * PCA9685_QUANTUM_NS(prescale); |
| 277 |
|
| 278 |
if (wfhw->onoff[3] & LED_FULL) { |
| 279 |
wf->duty_length_ns = 0; |
| 280 |
wf->duty_offset_ns = 0; |
| 281 |
} else if (wfhw->onoff[1] & LED_FULL) { |
| 282 |
wf->duty_length_ns = wf->period_length_ns; |
| 283 |
wf->duty_offset_ns = 0; |
| 284 |
} else { |
| 285 |
unsigned int on = wfhw->onoff[0] | (wfhw->onoff[1] & 0xf) << 8; |
| 286 |
unsigned int off = wfhw->onoff[2] | (wfhw->onoff[3] & 0xf) << 8; |
| 287 |
|
| 288 |
wf->duty_length_ns = (off - on) % PCA9685_COUNTER_RANGE * PCA9685_QUANTUM_NS(prescale); |
| 289 |
wf->duty_offset_ns = on * PCA9685_QUANTUM_NS(prescale); |
| 290 |
} |
| 291 |
|
| 292 |
dev_dbg(&chip->dev, "pwm#%u: [%hhx %hhx %hhx %hhx] PSC:%hhx -> %lld/%lld [+%lld]\n", |
| 293 |
pwm->hwpwm, |
| 294 |
wfhw->onoff[0], wfhw->onoff[1], wfhw->onoff[2], wfhw->onoff[3], wfhw->prescale, |
| 295 |
wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns); |
| 296 |
|
| 297 |
return 0; |
| 298 |
} |
| 299 |
|
| 300 |
static int pca9685_read_waveform(struct pwm_chip *chip, struct pwm_device *pwm, void *_wfhw) |
| 301 |
{ |
| 302 |
struct pca9685_waveform *wfhw = _wfhw; |
| 303 |
struct pca9685 *pca = to_pca(chip); |
| 304 |
unsigned int prescale; |
| 305 |
int ret; |
| 306 |
|
| 307 |
guard(mutex)(&pca->lock); |
| 308 |
|
| 309 |
ret = regmap_bulk_read(pca->regmap, REG_ON_L(pwm->hwpwm), &wfhw->onoff, 4); |
| 310 |
if (ret) |
| 311 |
return ret; |
| 312 |
|
| 313 |
ret = regmap_read(pca->regmap, PCA9685_PRESCALE, &prescale); |
| 314 |
if (ret) |
| 315 |
return ret; |
| 316 |
|
| 317 |
wfhw->prescale = prescale; |
| 318 |
|
| 319 |
return 0; |
| 320 |
} |
| 321 |
|
| 322 |
static int pca9685_write_waveform(struct pwm_chip *chip, struct pwm_device *pwm, const void *_wfhw) |
| 323 |
{ |
| 324 |
const struct pca9685_waveform *wfhw = _wfhw; |
| 325 |
struct pca9685 *pca = to_pca(chip); |
| 326 |
unsigned int current_prescale; |
| 327 |
int ret; |
| 328 |
|
| 329 |
guard(mutex)(&pca->lock); |
| 330 |
|
| 331 |
if (wfhw->prescale) { |
| 332 |
ret = regmap_read(pca->regmap, PCA9685_PRESCALE, ¤t_prescale); |
| 333 |
if (ret) |
| 334 |
return ret; |
| 335 |
|
| 336 |
if (current_prescale != wfhw->prescale) { |
| 337 |
if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) |
| 338 |
return -EBUSY; |
| 339 |
|
| 340 |
/* Put chip into sleep mode */ |
| 341 |
ret = pca9685_set_sleep_mode(chip, true); |
| 342 |
if (ret) |
| 343 |
return ret; |
| 344 |
|
| 345 |
/* Change the chip-wide output frequency */ |
| 346 |
ret = regmap_write(pca->regmap, PCA9685_PRESCALE, wfhw->prescale); |
| 347 |
if (ret) |
| 348 |
return ret; |
| 349 |
|
| 350 |
/* Wake the chip up */ |
| 351 |
ret = pca9685_set_sleep_mode(chip, false); |
| 352 |
if (ret) |
| 353 |
return ret; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
return regmap_bulk_write(pca->regmap, REG_ON_L(pwm->hwpwm), &wfhw->onoff, 4); |
| 358 |
} |
| 359 |
|
| 360 |
static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) |
| 361 |
{ |
| 362 |
struct pca9685 *pca = to_pca(chip); |
| 363 |
|
| 364 |
if (pwm->hwpwm < PCA9685_MAXCHAN) { |
| 365 |
/* PWMs - except the "all LEDs" channel - default to enabled */ |
| 366 |
mutex_lock(&pca->lock); |
| 367 |
set_bit(pwm->hwpwm, pca->pwms_enabled); |
| 368 |
mutex_unlock(&pca->lock); |
| 369 |
} |
| 370 |
|
| 371 |
pm_runtime_get_sync(pwmchip_parent(chip)); |
| 372 |
|
| 373 |
return 0; |
| 374 |
} |
| 375 |
|
| 376 |
static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) |
| 377 |
{ |
| 378 |
struct pca9685 *pca = to_pca(chip); |
| 379 |
|
| 380 |
mutex_lock(&pca->lock); |
| 381 |
clear_bit(pwm->hwpwm, pca->pwms_enabled); |
| 382 |
mutex_unlock(&pca->lock); |
| 383 |
|
| 384 |
pm_runtime_put(pwmchip_parent(chip)); |
| 385 |
} |
| 386 |
|
| 387 |
static const struct pwm_ops pca9685_pwm_ops = { |
| 388 |
.sizeof_wfhw = sizeof(struct pca9685_waveform), |
| 389 |
.round_waveform_tohw = pca9685_round_waveform_tohw, |
| 390 |
.round_waveform_fromhw = pca9685_round_waveform_fromhw, |
| 391 |
.read_waveform = pca9685_read_waveform, |
| 392 |
.write_waveform = pca9685_write_waveform, |
| 393 |
.request = pca9685_pwm_request, |
| 394 |
.free = pca9685_pwm_free, |
| 395 |
}; |
| 396 |
|
| 397 |
static bool pca9685_readable_reg(struct device *dev, unsigned int reg) |
| 398 |
{ |
| 399 |
/* The ALL_LED registers are readable but read as zero */ |
| 400 |
return reg <= REG_OFF_H(15) || reg >= PCA9685_PRESCALE; |
| 401 |
} |
| 402 |
|
| 403 |
static bool pca9685_writeable_reg(struct device *dev, unsigned int reg) |
| 404 |
{ |
| 405 |
return reg <= REG_OFF_H(15) || reg >= PCA9685_ALL_LED_ON_L; |
| 406 |
} |
| 407 |
|
| 408 |
static bool pca9685_volatile_reg(struct device *dev, unsigned int reg) |
| 409 |
{ |
| 410 |
/* |
| 411 |
* Writing to an ALL_LED register affects all LEDi registers, so they |
| 412 |
* are not cachable. :-\ |
| 413 |
*/ |
| 414 |
return reg < PCA9685_PRESCALE; |
| 415 |
} |
| 416 |
|
| 417 |
static const struct regmap_config pca9685_regmap_i2c_config = { |
| 418 |
.reg_bits = 8, |
| 419 |
.val_bits = 8, |
| 420 |
|
| 421 |
.readable_reg = pca9685_readable_reg, |
| 422 |
.writeable_reg = pca9685_writeable_reg, |
| 423 |
.volatile_reg = pca9685_volatile_reg, |
| 424 |
|
| 425 |
.max_register = PCA9685_NUMREGS, |
| 426 |
.cache_type = REGCACHE_MAPLE, |
| 427 |
}; |
| 428 |
|
| 429 |
static int pca9685_pwm_probe(struct i2c_client *client) |
| 430 |
{ |
| 431 |
struct pwm_chip *chip; |
| 432 |
struct pca9685 *pca; |
| 433 |
unsigned int reg; |
| 434 |
int ret; |
| 435 |
|
| 436 |
/* Add an extra channel for ALL_LED */ |
| 437 |
chip = devm_pwmchip_alloc(&client->dev, PCA9685_MAXCHAN + 1, sizeof(*pca)); |
| 438 |
if (IS_ERR(chip)) |
| 439 |
return PTR_ERR(chip); |
| 440 |
pca = to_pca(chip); |
| 441 |
|
| 442 |
pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config); |
| 443 |
if (IS_ERR(pca->regmap)) { |
| 444 |
ret = PTR_ERR(pca->regmap); |
| 445 |
dev_err(&client->dev, "Failed to initialize register map: %d\n", |
| 446 |
ret); |
| 447 |
return ret; |
| 448 |
} |
| 449 |
|
| 450 |
i2c_set_clientdata(client, chip); |
| 451 |
|
| 452 |
mutex_init(&pca->lock); |
| 453 |
|
| 454 |
/* clear MODE2_OCH */ |
| 455 |
reg = 0; |
| 456 |
|
| 457 |
if (device_property_read_bool(&client->dev, "invert")) |
| 458 |
reg |= MODE2_INVRT; |
| 459 |
else |
| 460 |
reg &= ~MODE2_INVRT; |
| 461 |
|
| 462 |
if (device_property_read_bool(&client->dev, "open-drain")) |
| 463 |
reg &= ~MODE2_OUTDRV; |
| 464 |
else |
| 465 |
reg |= MODE2_OUTDRV; |
| 466 |
|
| 467 |
ret = pca9685_write_reg(chip, PCA9685_MODE2, reg); |
| 468 |
if (ret) |
| 469 |
return ret; |
| 470 |
|
| 471 |
/* |
| 472 |
* Disable all LED ALLCALL and SUBx addresses to avoid bus collisions, |
| 473 |
* enable Auto-Increment. |
| 474 |
*/ |
| 475 |
pca9685_read_reg(chip, PCA9685_MODE1, ®); |
| 476 |
reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3); |
| 477 |
reg |= MODE1_AI; |
| 478 |
pca9685_write_reg(chip, PCA9685_MODE1, reg); |
| 479 |
|
| 480 |
/* Reset OFF/ON registers to POR default */ |
| 481 |
ret = pca9685_write_4reg(chip, PCA9685_ALL_LED_ON_L, (u8[]){ 0, LED_FULL, 0, LED_FULL }); |
| 482 |
if (ret < 0) |
| 483 |
return dev_err_probe(&client->dev, ret, "Failed to reset ON/OFF registers\n"); |
| 484 |
|
| 485 |
chip->ops = &pca9685_pwm_ops; |
| 486 |
|
| 487 |
ret = pwmchip_add(chip); |
| 488 |
if (ret < 0) |
| 489 |
return ret; |
| 490 |
|
| 491 |
pm_runtime_enable(&client->dev); |
| 492 |
|
| 493 |
if (pm_runtime_enabled(&client->dev)) { |
| 494 |
/* |
| 495 |
* Although the chip comes out of power-up in the sleep state, |
| 496 |
* we force it to sleep in case it was woken up before |
| 497 |
*/ |
| 498 |
pca9685_set_sleep_mode(chip, true); |
| 499 |
pm_runtime_set_suspended(&client->dev); |
| 500 |
} else { |
| 501 |
/* Wake the chip up if runtime PM is disabled */ |
| 502 |
pca9685_set_sleep_mode(chip, false); |
| 503 |
} |
| 504 |
|
| 505 |
return 0; |
| 506 |
} |
| 507 |
|
| 508 |
static void pca9685_pwm_remove(struct i2c_client *client) |
| 509 |
{ |
| 510 |
struct pwm_chip *chip = i2c_get_clientdata(client); |
| 511 |
|
| 512 |
pwmchip_remove(chip); |
| 513 |
|
| 514 |
if (!pm_runtime_enabled(&client->dev)) { |
| 515 |
/* Put chip in sleep state if runtime PM is disabled */ |
| 516 |
pca9685_set_sleep_mode(chip, true); |
| 517 |
} |
| 518 |
|
| 519 |
pm_runtime_disable(&client->dev); |
| 520 |
} |
| 521 |
|
| 522 |
static int __maybe_unused pca9685_pwm_runtime_suspend(struct device *dev) |
| 523 |
{ |
| 524 |
struct i2c_client *client = to_i2c_client(dev); |
| 525 |
struct pwm_chip *chip = i2c_get_clientdata(client); |
| 526 |
|
| 527 |
pca9685_set_sleep_mode(chip, true); |
| 528 |
return 0; |
| 529 |
} |
| 530 |
|
| 531 |
static int __maybe_unused pca9685_pwm_runtime_resume(struct device *dev) |
| 532 |
{ |
| 533 |
struct i2c_client *client = to_i2c_client(dev); |
| 534 |
struct pwm_chip *chip = i2c_get_clientdata(client); |
| 535 |
|
| 536 |
pca9685_set_sleep_mode(chip, false); |
| 537 |
return 0; |
| 538 |
} |
| 539 |
|
| 540 |
static const struct i2c_device_id pca9685_id[] = { |
| 541 |
{ "pca9685" }, |
| 542 |
{ /* sentinel */ } |
| 543 |
}; |
| 544 |
MODULE_DEVICE_TABLE(i2c, pca9685_id); |
| 545 |
|
| 546 |
static const struct acpi_device_id pca9685_acpi_ids[] = { |
| 547 |
{ "INT3492", 0 }, |
| 548 |
{ /* sentinel */ }, |
| 549 |
}; |
| 550 |
MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids); |
| 551 |
|
| 552 |
static const struct of_device_id pca9685_dt_ids[] = { |
| 553 |
{ .compatible = "nxp,pca9685-pwm", }, |
| 554 |
{ /* sentinel */ } |
| 555 |
}; |
| 556 |
MODULE_DEVICE_TABLE(of, pca9685_dt_ids); |
| 557 |
|
| 558 |
static const struct dev_pm_ops pca9685_pwm_pm = { |
| 559 |
SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend, |
| 560 |
pca9685_pwm_runtime_resume, NULL) |
| 561 |
}; |
| 562 |
|
| 563 |
static struct i2c_driver pca9685_i2c_driver = { |
| 564 |
.driver = { |
| 565 |
.name = "pca9685-pwm", |
| 566 |
.acpi_match_table = pca9685_acpi_ids, |
| 567 |
.of_match_table = pca9685_dt_ids, |
| 568 |
.pm = &pca9685_pwm_pm, |
| 569 |
}, |
| 570 |
.probe = pca9685_pwm_probe, |
| 571 |
.remove = pca9685_pwm_remove, |
| 572 |
.id_table = pca9685_id, |
| 573 |
}; |
| 574 |
|
| 575 |
module_i2c_driver(pca9685_i2c_driver); |
| 576 |
|
| 577 |
MODULE_AUTHOR("Steffen Trumtrar <[email protected]>"); |
| 578 |
MODULE_DESCRIPTION("PWM driver for PCA9685"); |
| 579 |
MODULE_LICENSE("GPL"); |