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