Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Driver for PCA9685 16-channel 12-bit PWM LED controller |
| 3 | * |
| 4 | * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de> |
Clemens Gruber | 01ec847 | 2015-07-23 17:19:02 +0200 | [diff] [blame] | 5 | * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com> |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 6 | * |
| 7 | * based on the pwm-twl-led.c driver |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License version 2 as published by |
| 11 | * the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 16 | * more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
Andy Shevchenko | 912b843 | 2015-10-07 13:18:49 +0300 | [diff] [blame] | 22 | #include <linux/acpi.h> |
Mika Westerberg | bccec89 | 2016-09-20 17:40:56 +0300 | [diff] [blame] | 23 | #include <linux/gpio/driver.h> |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 24 | #include <linux/i2c.h> |
| 25 | #include <linux/module.h> |
Mika Westerberg | bccec89 | 2016-09-20 17:40:56 +0300 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 27 | #include <linux/platform_device.h> |
Andy Shevchenko | 912b843 | 2015-10-07 13:18:49 +0300 | [diff] [blame] | 28 | #include <linux/property.h> |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 29 | #include <linux/pwm.h> |
| 30 | #include <linux/regmap.h> |
| 31 | #include <linux/slab.h> |
Clemens Gruber | 01ec847 | 2015-07-23 17:19:02 +0200 | [diff] [blame] | 32 | #include <linux/delay.h> |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 33 | #include <linux/pm_runtime.h> |
Clemens Gruber | 01ec847 | 2015-07-23 17:19:02 +0200 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * Because the PCA9685 has only one prescaler per chip, changing the period of |
| 37 | * one channel affects the period of all 16 PWM outputs! |
| 38 | * However, the ratio between each configured duty cycle and the chip-wide |
| 39 | * period remains constant, because the OFF time is set in proportion to the |
| 40 | * counter range. |
| 41 | */ |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 42 | |
| 43 | #define PCA9685_MODE1 0x00 |
| 44 | #define PCA9685_MODE2 0x01 |
| 45 | #define PCA9685_SUBADDR1 0x02 |
| 46 | #define PCA9685_SUBADDR2 0x03 |
| 47 | #define PCA9685_SUBADDR3 0x04 |
| 48 | #define PCA9685_ALLCALLADDR 0x05 |
| 49 | #define PCA9685_LEDX_ON_L 0x06 |
| 50 | #define PCA9685_LEDX_ON_H 0x07 |
| 51 | #define PCA9685_LEDX_OFF_L 0x08 |
| 52 | #define PCA9685_LEDX_OFF_H 0x09 |
| 53 | |
| 54 | #define PCA9685_ALL_LED_ON_L 0xFA |
| 55 | #define PCA9685_ALL_LED_ON_H 0xFB |
| 56 | #define PCA9685_ALL_LED_OFF_L 0xFC |
| 57 | #define PCA9685_ALL_LED_OFF_H 0xFD |
| 58 | #define PCA9685_PRESCALE 0xFE |
| 59 | |
Clemens Gruber | 01ec847 | 2015-07-23 17:19:02 +0200 | [diff] [blame] | 60 | #define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */ |
| 61 | #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */ |
| 62 | |
| 63 | #define PCA9685_COUNTER_RANGE 4096 |
| 64 | #define PCA9685_DEFAULT_PERIOD 5000000 /* Default period_ns = 1/200 Hz */ |
| 65 | #define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */ |
| 66 | |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 67 | #define PCA9685_NUMREGS 0xFF |
| 68 | #define PCA9685_MAXCHAN 0x10 |
| 69 | |
| 70 | #define LED_FULL (1 << 4) |
| 71 | #define MODE1_SLEEP (1 << 4) |
| 72 | #define MODE2_INVRT (1 << 4) |
| 73 | #define MODE2_OUTDRV (1 << 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 | struct pca9685 { |
| 81 | struct pwm_chip chip; |
| 82 | struct regmap *regmap; |
Clemens Gruber | 01ec847 | 2015-07-23 17:19:02 +0200 | [diff] [blame] | 83 | int duty_ns; |
| 84 | int period_ns; |
Mika Westerberg | bccec89 | 2016-09-20 17:40:56 +0300 | [diff] [blame] | 85 | #if IS_ENABLED(CONFIG_GPIOLIB) |
| 86 | struct mutex lock; |
| 87 | struct gpio_chip gpio; |
| 88 | #endif |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | static inline struct pca9685 *to_pca(struct pwm_chip *chip) |
| 92 | { |
| 93 | return container_of(chip, struct pca9685, chip); |
| 94 | } |
| 95 | |
Mika Westerberg | bccec89 | 2016-09-20 17:40:56 +0300 | [diff] [blame] | 96 | #if IS_ENABLED(CONFIG_GPIOLIB) |
| 97 | static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset) |
| 98 | { |
| 99 | struct pca9685 *pca = gpiochip_get_data(gpio); |
| 100 | struct pwm_device *pwm; |
| 101 | |
| 102 | mutex_lock(&pca->lock); |
| 103 | |
| 104 | pwm = &pca->chip.pwms[offset]; |
| 105 | |
| 106 | if (pwm->flags & (PWMF_REQUESTED | PWMF_EXPORTED)) { |
| 107 | mutex_unlock(&pca->lock); |
| 108 | return -EBUSY; |
| 109 | } |
| 110 | |
| 111 | pwm_set_chip_data(pwm, (void *)1); |
| 112 | |
| 113 | mutex_unlock(&pca->lock); |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 114 | pm_runtime_get_sync(pca->chip.dev); |
Mika Westerberg | bccec89 | 2016-09-20 17:40:56 +0300 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
Mika Westerberg | bccec89 | 2016-09-20 17:40:56 +0300 | [diff] [blame] | 118 | static bool pca9685_pwm_is_gpio(struct pca9685 *pca, struct pwm_device *pwm) |
| 119 | { |
| 120 | bool is_gpio = false; |
| 121 | |
| 122 | mutex_lock(&pca->lock); |
| 123 | |
| 124 | if (pwm->hwpwm >= PCA9685_MAXCHAN) { |
| 125 | unsigned int i; |
| 126 | |
| 127 | /* |
| 128 | * Check if any of the GPIOs are requested and in that case |
| 129 | * prevent using the "all LEDs" channel. |
| 130 | */ |
| 131 | for (i = 0; i < pca->gpio.ngpio; i++) |
| 132 | if (gpiochip_is_requested(&pca->gpio, i)) { |
| 133 | is_gpio = true; |
| 134 | break; |
| 135 | } |
| 136 | } else if (pwm_get_chip_data(pwm)) { |
| 137 | is_gpio = true; |
| 138 | } |
| 139 | |
| 140 | mutex_unlock(&pca->lock); |
| 141 | return is_gpio; |
| 142 | } |
| 143 | |
| 144 | static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset) |
| 145 | { |
| 146 | struct pca9685 *pca = gpiochip_get_data(gpio); |
| 147 | struct pwm_device *pwm = &pca->chip.pwms[offset]; |
| 148 | unsigned int value; |
| 149 | |
| 150 | regmap_read(pca->regmap, LED_N_ON_H(pwm->hwpwm), &value); |
| 151 | |
| 152 | return value & LED_FULL; |
| 153 | } |
| 154 | |
| 155 | static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset, |
| 156 | int value) |
| 157 | { |
| 158 | struct pca9685 *pca = gpiochip_get_data(gpio); |
| 159 | struct pwm_device *pwm = &pca->chip.pwms[offset]; |
| 160 | unsigned int on = value ? LED_FULL : 0; |
| 161 | |
| 162 | /* Clear both OFF registers */ |
| 163 | regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm), 0); |
| 164 | regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm), 0); |
| 165 | |
| 166 | /* Set the full ON bit */ |
| 167 | regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), on); |
| 168 | } |
| 169 | |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 170 | static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset) |
| 171 | { |
| 172 | struct pca9685 *pca = gpiochip_get_data(gpio); |
| 173 | struct pwm_device *pwm; |
| 174 | |
| 175 | pca9685_pwm_gpio_set(gpio, offset, 0); |
| 176 | pm_runtime_put(pca->chip.dev); |
| 177 | mutex_lock(&pca->lock); |
| 178 | pwm = &pca->chip.pwms[offset]; |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 179 | mutex_unlock(&pca->lock); |
| 180 | } |
| 181 | |
Mika Westerberg | bccec89 | 2016-09-20 17:40:56 +0300 | [diff] [blame] | 182 | static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip, |
| 183 | unsigned int offset) |
| 184 | { |
| 185 | /* Always out */ |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio, |
| 190 | unsigned int offset) |
| 191 | { |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | |
| 195 | static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio, |
| 196 | unsigned int offset, int value) |
| 197 | { |
| 198 | pca9685_pwm_gpio_set(gpio, offset, value); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * The PCA9685 has a bit for turning the PWM output full off or on. Some |
| 205 | * boards like Intel Galileo actually uses these as normal GPIOs so we |
| 206 | * expose a GPIO chip here which can exclusively take over the underlying |
| 207 | * PWM channel. |
| 208 | */ |
| 209 | static int pca9685_pwm_gpio_probe(struct pca9685 *pca) |
| 210 | { |
| 211 | struct device *dev = pca->chip.dev; |
| 212 | |
| 213 | mutex_init(&pca->lock); |
| 214 | |
| 215 | pca->gpio.label = dev_name(dev); |
| 216 | pca->gpio.parent = dev; |
| 217 | pca->gpio.request = pca9685_pwm_gpio_request; |
| 218 | pca->gpio.free = pca9685_pwm_gpio_free; |
| 219 | pca->gpio.get_direction = pca9685_pwm_gpio_get_direction; |
| 220 | pca->gpio.direction_input = pca9685_pwm_gpio_direction_input; |
| 221 | pca->gpio.direction_output = pca9685_pwm_gpio_direction_output; |
| 222 | pca->gpio.get = pca9685_pwm_gpio_get; |
| 223 | pca->gpio.set = pca9685_pwm_gpio_set; |
| 224 | pca->gpio.base = -1; |
| 225 | pca->gpio.ngpio = PCA9685_MAXCHAN; |
| 226 | pca->gpio.can_sleep = true; |
| 227 | |
| 228 | return devm_gpiochip_add_data(dev, &pca->gpio, pca); |
| 229 | } |
| 230 | #else |
| 231 | static inline bool pca9685_pwm_is_gpio(struct pca9685 *pca, |
| 232 | struct pwm_device *pwm) |
| 233 | { |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca) |
| 238 | { |
| 239 | return 0; |
| 240 | } |
| 241 | #endif |
| 242 | |
Sven Van Asbroeck | 0829326 | 2017-04-21 09:19:02 -0400 | [diff] [blame] | 243 | static void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable) |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 244 | { |
| 245 | regmap_update_bits(pca->regmap, PCA9685_MODE1, |
Sven Van Asbroeck | 0829326 | 2017-04-21 09:19:02 -0400 | [diff] [blame] | 246 | MODE1_SLEEP, enable ? MODE1_SLEEP : 0); |
| 247 | if (!enable) { |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 248 | /* Wait 500us for the oscillator to be back up */ |
| 249 | udelay(500); |
| 250 | } |
| 251 | } |
| 252 | |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 253 | static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, |
| 254 | int duty_ns, int period_ns) |
| 255 | { |
| 256 | struct pca9685 *pca = to_pca(chip); |
| 257 | unsigned long long duty; |
| 258 | unsigned int reg; |
Clemens Gruber | 01ec847 | 2015-07-23 17:19:02 +0200 | [diff] [blame] | 259 | int prescale; |
| 260 | |
| 261 | if (period_ns != pca->period_ns) { |
| 262 | prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns, |
| 263 | PCA9685_COUNTER_RANGE * 1000) - 1; |
| 264 | |
| 265 | if (prescale >= PCA9685_PRESCALE_MIN && |
| 266 | prescale <= PCA9685_PRESCALE_MAX) { |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 267 | /* |
| 268 | * putting the chip briefly into SLEEP mode |
| 269 | * at this point won't interfere with the |
| 270 | * pm_runtime framework, because the pm_runtime |
| 271 | * state is guaranteed active here. |
| 272 | */ |
Clemens Gruber | 01ec847 | 2015-07-23 17:19:02 +0200 | [diff] [blame] | 273 | /* Put chip into sleep mode */ |
Sven Van Asbroeck | 0829326 | 2017-04-21 09:19:02 -0400 | [diff] [blame] | 274 | pca9685_set_sleep_mode(pca, true); |
Clemens Gruber | 01ec847 | 2015-07-23 17:19:02 +0200 | [diff] [blame] | 275 | |
| 276 | /* Change the chip-wide output frequency */ |
| 277 | regmap_write(pca->regmap, PCA9685_PRESCALE, prescale); |
| 278 | |
| 279 | /* Wake the chip up */ |
Sven Van Asbroeck | 0829326 | 2017-04-21 09:19:02 -0400 | [diff] [blame] | 280 | pca9685_set_sleep_mode(pca, false); |
Clemens Gruber | 01ec847 | 2015-07-23 17:19:02 +0200 | [diff] [blame] | 281 | |
| 282 | pca->period_ns = period_ns; |
Clemens Gruber | 01ec847 | 2015-07-23 17:19:02 +0200 | [diff] [blame] | 283 | } else { |
| 284 | dev_err(chip->dev, |
| 285 | "prescaler not set: period out of bounds!\n"); |
| 286 | return -EINVAL; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | pca->duty_ns = duty_ns; |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 291 | |
| 292 | if (duty_ns < 1) { |
| 293 | if (pwm->hwpwm >= PCA9685_MAXCHAN) |
| 294 | reg = PCA9685_ALL_LED_OFF_H; |
| 295 | else |
| 296 | reg = LED_N_OFF_H(pwm->hwpwm); |
| 297 | |
| 298 | regmap_write(pca->regmap, reg, LED_FULL); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | if (duty_ns == period_ns) { |
Clemens Gruber | 4a627b5 | 2015-07-23 17:19:01 +0200 | [diff] [blame] | 304 | /* Clear both OFF registers */ |
| 305 | if (pwm->hwpwm >= PCA9685_MAXCHAN) |
| 306 | reg = PCA9685_ALL_LED_OFF_L; |
| 307 | else |
| 308 | reg = LED_N_OFF_L(pwm->hwpwm); |
| 309 | |
| 310 | regmap_write(pca->regmap, reg, 0x0); |
| 311 | |
| 312 | if (pwm->hwpwm >= PCA9685_MAXCHAN) |
| 313 | reg = PCA9685_ALL_LED_OFF_H; |
| 314 | else |
| 315 | reg = LED_N_OFF_H(pwm->hwpwm); |
| 316 | |
| 317 | regmap_write(pca->regmap, reg, 0x0); |
| 318 | |
| 319 | /* Set the full ON bit */ |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 320 | if (pwm->hwpwm >= PCA9685_MAXCHAN) |
| 321 | reg = PCA9685_ALL_LED_ON_H; |
| 322 | else |
| 323 | reg = LED_N_ON_H(pwm->hwpwm); |
| 324 | |
| 325 | regmap_write(pca->regmap, reg, LED_FULL); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
Clemens Gruber | 01ec847 | 2015-07-23 17:19:02 +0200 | [diff] [blame] | 330 | duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns; |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 331 | duty = DIV_ROUND_UP_ULL(duty, period_ns); |
| 332 | |
| 333 | if (pwm->hwpwm >= PCA9685_MAXCHAN) |
| 334 | reg = PCA9685_ALL_LED_OFF_L; |
| 335 | else |
| 336 | reg = LED_N_OFF_L(pwm->hwpwm); |
| 337 | |
| 338 | regmap_write(pca->regmap, reg, (int)duty & 0xff); |
| 339 | |
| 340 | if (pwm->hwpwm >= PCA9685_MAXCHAN) |
| 341 | reg = PCA9685_ALL_LED_OFF_H; |
| 342 | else |
| 343 | reg = LED_N_OFF_H(pwm->hwpwm); |
| 344 | |
| 345 | regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf); |
| 346 | |
Clemens Gruber | 4a627b5 | 2015-07-23 17:19:01 +0200 | [diff] [blame] | 347 | /* Clear the full ON bit, otherwise the set OFF time has no effect */ |
| 348 | if (pwm->hwpwm >= PCA9685_MAXCHAN) |
| 349 | reg = PCA9685_ALL_LED_ON_H; |
| 350 | else |
| 351 | reg = LED_N_ON_H(pwm->hwpwm); |
| 352 | |
| 353 | regmap_write(pca->regmap, reg, 0); |
| 354 | |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) |
| 359 | { |
| 360 | struct pca9685 *pca = to_pca(chip); |
| 361 | unsigned int reg; |
| 362 | |
| 363 | /* |
| 364 | * The PWM subsystem does not support a pre-delay. |
| 365 | * So, set the ON-timeout to 0 |
| 366 | */ |
| 367 | if (pwm->hwpwm >= PCA9685_MAXCHAN) |
| 368 | reg = PCA9685_ALL_LED_ON_L; |
| 369 | else |
| 370 | reg = LED_N_ON_L(pwm->hwpwm); |
| 371 | |
| 372 | regmap_write(pca->regmap, reg, 0); |
| 373 | |
| 374 | if (pwm->hwpwm >= PCA9685_MAXCHAN) |
| 375 | reg = PCA9685_ALL_LED_ON_H; |
| 376 | else |
| 377 | reg = LED_N_ON_H(pwm->hwpwm); |
| 378 | |
| 379 | regmap_write(pca->regmap, reg, 0); |
| 380 | |
| 381 | /* |
| 382 | * Clear the full-off bit. |
| 383 | * It has precedence over the others and must be off. |
| 384 | */ |
| 385 | if (pwm->hwpwm >= PCA9685_MAXCHAN) |
| 386 | reg = PCA9685_ALL_LED_OFF_H; |
| 387 | else |
| 388 | reg = LED_N_OFF_H(pwm->hwpwm); |
| 389 | |
| 390 | regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0); |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) |
| 396 | { |
| 397 | struct pca9685 *pca = to_pca(chip); |
| 398 | unsigned int reg; |
| 399 | |
| 400 | if (pwm->hwpwm >= PCA9685_MAXCHAN) |
| 401 | reg = PCA9685_ALL_LED_OFF_H; |
| 402 | else |
| 403 | reg = LED_N_OFF_H(pwm->hwpwm); |
| 404 | |
| 405 | regmap_write(pca->regmap, reg, LED_FULL); |
| 406 | |
| 407 | /* Clear the LED_OFF counter. */ |
| 408 | if (pwm->hwpwm >= PCA9685_MAXCHAN) |
| 409 | reg = PCA9685_ALL_LED_OFF_L; |
| 410 | else |
| 411 | reg = LED_N_OFF_L(pwm->hwpwm); |
| 412 | |
| 413 | regmap_write(pca->regmap, reg, 0x0); |
| 414 | } |
| 415 | |
| 416 | static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) |
| 417 | { |
| 418 | struct pca9685 *pca = to_pca(chip); |
| 419 | |
Mika Westerberg | bccec89 | 2016-09-20 17:40:56 +0300 | [diff] [blame] | 420 | if (pca9685_pwm_is_gpio(pca, pwm)) |
| 421 | return -EBUSY; |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 422 | pm_runtime_get_sync(chip->dev); |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) |
| 428 | { |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 429 | pca9685_pwm_disable(chip, pwm); |
| 430 | pm_runtime_put(chip->dev); |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static const struct pwm_ops pca9685_pwm_ops = { |
| 434 | .enable = pca9685_pwm_enable, |
| 435 | .disable = pca9685_pwm_disable, |
| 436 | .config = pca9685_pwm_config, |
| 437 | .request = pca9685_pwm_request, |
| 438 | .free = pca9685_pwm_free, |
Thierry Reding | 3dd0a90 | 2013-06-12 13:18:29 +0200 | [diff] [blame] | 439 | .owner = THIS_MODULE, |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 440 | }; |
| 441 | |
Krzysztof Kozlowski | c456fbb | 2015-02-24 10:40:24 +0100 | [diff] [blame] | 442 | static const struct regmap_config pca9685_regmap_i2c_config = { |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 443 | .reg_bits = 8, |
| 444 | .val_bits = 8, |
| 445 | .max_register = PCA9685_NUMREGS, |
| 446 | .cache_type = REGCACHE_NONE, |
| 447 | }; |
| 448 | |
| 449 | static int pca9685_pwm_probe(struct i2c_client *client, |
| 450 | const struct i2c_device_id *id) |
| 451 | { |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 452 | struct pca9685 *pca; |
| 453 | int ret; |
| 454 | int mode2; |
| 455 | |
| 456 | pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL); |
| 457 | if (!pca) |
| 458 | return -ENOMEM; |
| 459 | |
| 460 | pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config); |
| 461 | if (IS_ERR(pca->regmap)) { |
| 462 | ret = PTR_ERR(pca->regmap); |
| 463 | dev_err(&client->dev, "Failed to initialize register map: %d\n", |
| 464 | ret); |
| 465 | return ret; |
| 466 | } |
Clemens Gruber | 01ec847 | 2015-07-23 17:19:02 +0200 | [diff] [blame] | 467 | pca->duty_ns = 0; |
| 468 | pca->period_ns = PCA9685_DEFAULT_PERIOD; |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 469 | |
| 470 | i2c_set_clientdata(client, pca); |
| 471 | |
| 472 | regmap_read(pca->regmap, PCA9685_MODE2, &mode2); |
| 473 | |
Andy Shevchenko | 912b843 | 2015-10-07 13:18:49 +0300 | [diff] [blame] | 474 | if (device_property_read_bool(&client->dev, "invert")) |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 475 | mode2 |= MODE2_INVRT; |
| 476 | else |
| 477 | mode2 &= ~MODE2_INVRT; |
| 478 | |
Andy Shevchenko | 912b843 | 2015-10-07 13:18:49 +0300 | [diff] [blame] | 479 | if (device_property_read_bool(&client->dev, "open-drain")) |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 480 | mode2 &= ~MODE2_OUTDRV; |
| 481 | else |
| 482 | mode2 |= MODE2_OUTDRV; |
| 483 | |
| 484 | regmap_write(pca->regmap, PCA9685_MODE2, mode2); |
| 485 | |
| 486 | /* clear all "full off" bits */ |
| 487 | regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0); |
| 488 | regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0); |
| 489 | |
| 490 | pca->chip.ops = &pca9685_pwm_ops; |
| 491 | /* add an extra channel for ALL_LED */ |
| 492 | pca->chip.npwm = PCA9685_MAXCHAN + 1; |
| 493 | |
| 494 | pca->chip.dev = &client->dev; |
| 495 | pca->chip.base = -1; |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 496 | |
Mika Westerberg | bccec89 | 2016-09-20 17:40:56 +0300 | [diff] [blame] | 497 | ret = pwmchip_add(&pca->chip); |
| 498 | if (ret < 0) |
| 499 | return ret; |
| 500 | |
| 501 | ret = pca9685_pwm_gpio_probe(pca); |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 502 | if (ret < 0) { |
Mika Westerberg | bccec89 | 2016-09-20 17:40:56 +0300 | [diff] [blame] | 503 | pwmchip_remove(&pca->chip); |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 504 | return ret; |
| 505 | } |
Mika Westerberg | bccec89 | 2016-09-20 17:40:56 +0300 | [diff] [blame] | 506 | |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 507 | /* the chip comes out of power-up in the active state */ |
| 508 | pm_runtime_set_active(&client->dev); |
| 509 | /* |
| 510 | * enable will put the chip into suspend, which is what we |
| 511 | * want as all outputs are disabled at this point |
| 512 | */ |
| 513 | pm_runtime_enable(&client->dev); |
| 514 | |
| 515 | return 0; |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | static int pca9685_pwm_remove(struct i2c_client *client) |
| 519 | { |
| 520 | struct pca9685 *pca = i2c_get_clientdata(client); |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 521 | int ret; |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 522 | |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 523 | ret = pwmchip_remove(&pca->chip); |
| 524 | if (ret) |
| 525 | return ret; |
| 526 | pm_runtime_disable(&client->dev); |
| 527 | return 0; |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 530 | #ifdef CONFIG_PM |
| 531 | static int pca9685_pwm_runtime_suspend(struct device *dev) |
| 532 | { |
| 533 | struct i2c_client *client = to_i2c_client(dev); |
| 534 | struct pca9685 *pca = i2c_get_clientdata(client); |
| 535 | |
Sven Van Asbroeck | 0829326 | 2017-04-21 09:19:02 -0400 | [diff] [blame] | 536 | pca9685_set_sleep_mode(pca, true); |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static int pca9685_pwm_runtime_resume(struct device *dev) |
| 541 | { |
| 542 | struct i2c_client *client = to_i2c_client(dev); |
| 543 | struct pca9685 *pca = i2c_get_clientdata(client); |
| 544 | |
Sven Van Asbroeck | 0829326 | 2017-04-21 09:19:02 -0400 | [diff] [blame] | 545 | pca9685_set_sleep_mode(pca, false); |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 546 | return 0; |
| 547 | } |
| 548 | #endif |
| 549 | |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 550 | static const struct i2c_device_id pca9685_id[] = { |
| 551 | { "pca9685", 0 }, |
| 552 | { /* sentinel */ }, |
| 553 | }; |
| 554 | MODULE_DEVICE_TABLE(i2c, pca9685_id); |
| 555 | |
Andy Shevchenko | 912b843 | 2015-10-07 13:18:49 +0300 | [diff] [blame] | 556 | #ifdef CONFIG_ACPI |
| 557 | static const struct acpi_device_id pca9685_acpi_ids[] = { |
| 558 | { "INT3492", 0 }, |
| 559 | { /* sentinel */ }, |
| 560 | }; |
| 561 | MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids); |
| 562 | #endif |
| 563 | |
| 564 | #ifdef CONFIG_OF |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 565 | static const struct of_device_id pca9685_dt_ids[] = { |
| 566 | { .compatible = "nxp,pca9685-pwm", }, |
| 567 | { /* sentinel */ } |
| 568 | }; |
| 569 | MODULE_DEVICE_TABLE(of, pca9685_dt_ids); |
Andy Shevchenko | 912b843 | 2015-10-07 13:18:49 +0300 | [diff] [blame] | 570 | #endif |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 571 | |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 572 | static const struct dev_pm_ops pca9685_pwm_pm = { |
| 573 | SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend, |
| 574 | pca9685_pwm_runtime_resume, NULL) |
| 575 | }; |
| 576 | |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 577 | static struct i2c_driver pca9685_i2c_driver = { |
| 578 | .driver = { |
| 579 | .name = "pca9685-pwm", |
Andy Shevchenko | 912b843 | 2015-10-07 13:18:49 +0300 | [diff] [blame] | 580 | .acpi_match_table = ACPI_PTR(pca9685_acpi_ids), |
| 581 | .of_match_table = of_match_ptr(pca9685_dt_ids), |
Sven Van Asbroeck | c40c461 | 2017-04-13 08:58:11 -0400 | [diff] [blame] | 582 | .pm = &pca9685_pwm_pm, |
Steffen Trumtrar | 88b613e | 2013-05-30 09:50:12 +0200 | [diff] [blame] | 583 | }, |
| 584 | .probe = pca9685_pwm_probe, |
| 585 | .remove = pca9685_pwm_remove, |
| 586 | .id_table = pca9685_id, |
| 587 | }; |
| 588 | |
| 589 | module_i2c_driver(pca9685_i2c_driver); |
| 590 | |
| 591 | MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>"); |
| 592 | MODULE_DESCRIPTION("PWM driver for PCA9685"); |
| 593 | MODULE_LICENSE("GPL"); |