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 <s.trumtrar@pengutronix.de>
6 * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com>
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 * GPIOs do not count as enabled PWMs as they are not using the prescaler.
30 */
31
32 #define PCA9685_MODE1 0x00
33 #define PCA9685_MODE2 0x01
34 #define PCA9685_SUBADDR1 0x02
35 #define PCA9685_SUBADDR2 0x03
36 #define PCA9685_SUBADDR3 0x04
37 #define PCA9685_ALLCALLADDR 0x05
38 #define PCA9685_LEDX_ON_L 0x06
39 #define PCA9685_LEDX_ON_H 0x07
40 #define PCA9685_LEDX_OFF_L 0x08
41 #define PCA9685_LEDX_OFF_H 0x09
42
43 #define PCA9685_ALL_LED_ON_L 0xFA
44 #define PCA9685_ALL_LED_ON_H 0xFB
45 #define PCA9685_ALL_LED_OFF_L 0xFC
46 #define PCA9685_ALL_LED_OFF_H 0xFD
47 #define PCA9685_PRESCALE 0xFE
48
49 #define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */
50 #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */
51
52 #define PCA9685_COUNTER_RANGE 4096
53 #define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */
54
55 #define PCA9685_NUMREGS 0xFF
56 #define PCA9685_MAXCHAN 0x10
57
58 #define LED_FULL BIT(4)
59 #define MODE1_ALLCALL BIT(0)
60 #define MODE1_SUB3 BIT(1)
61 #define MODE1_SUB2 BIT(2)
62 #define MODE1_SUB1 BIT(3)
63 #define MODE1_SLEEP BIT(4)
64 #define MODE2_INVRT BIT(4)
65 #define MODE2_OUTDRV BIT(2)
66
67 #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
68 #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
69 #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
70 #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
71
72 #define REG_ON_H(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_ON_H : LED_N_ON_H((C)))
73 #define REG_ON_L(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_ON_L : LED_N_ON_L((C)))
74 #define REG_OFF_H(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_OFF_H : LED_N_OFF_H((C)))
75 #define REG_OFF_L(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_OFF_L : LED_N_OFF_L((C)))
76
77 struct pca9685 {
78 struct regmap *regmap;
79 struct mutex lock;
80 DECLARE_BITMAP(pwms_enabled, PCA9685_MAXCHAN + 1);
81 #if IS_ENABLED(CONFIG_GPIOLIB)
82 struct gpio_chip gpio;
83 DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1);
84 #endif
85 };
86
to_pca(struct pwm_chip * chip)87 static inline struct pca9685 *to_pca(struct pwm_chip *chip)
88 {
89 return pwmchip_get_drvdata(chip);
90 }
91
92 /* This function is supposed to be called with the lock mutex held */
pca9685_prescaler_can_change(struct pca9685 * pca,int channel)93 static bool pca9685_prescaler_can_change(struct pca9685 *pca, int channel)
94 {
95 /* No PWM enabled: Change allowed */
96 if (bitmap_empty(pca->pwms_enabled, PCA9685_MAXCHAN + 1))
97 return true;
98 /* More than one PWM enabled: Change not allowed */
99 if (bitmap_weight(pca->pwms_enabled, PCA9685_MAXCHAN + 1) > 1)
100 return false;
101 /*
102 * Only one PWM enabled: Change allowed if the PWM about to
103 * be changed is the one that is already enabled
104 */
105 return test_bit(channel, pca->pwms_enabled);
106 }
107
pca9685_read_reg(struct pwm_chip * chip,unsigned int reg,unsigned int * val)108 static int pca9685_read_reg(struct pwm_chip *chip, unsigned int reg, unsigned int *val)
109 {
110 struct pca9685 *pca = to_pca(chip);
111 struct device *dev = pwmchip_parent(chip);
112 int err;
113
114 err = regmap_read(pca->regmap, reg, val);
115 if (err)
116 dev_err(dev, "regmap_read of register 0x%x failed: %pe\n", reg, ERR_PTR(err));
117
118 return err;
119 }
120
pca9685_write_reg(struct pwm_chip * chip,unsigned int reg,unsigned int val)121 static int pca9685_write_reg(struct pwm_chip *chip, unsigned int reg, unsigned int val)
122 {
123 struct pca9685 *pca = to_pca(chip);
124 struct device *dev = pwmchip_parent(chip);
125 int err;
126
127 err = regmap_write(pca->regmap, reg, val);
128 if (err)
129 dev_err(dev, "regmap_write to register 0x%x failed: %pe\n", reg, ERR_PTR(err));
130
131 return err;
132 }
133
134 /* Helper function to set the duty cycle ratio to duty/4096 (e.g. duty=2048 -> 50%) */
pca9685_pwm_set_duty(struct pwm_chip * chip,int channel,unsigned int duty)135 static void pca9685_pwm_set_duty(struct pwm_chip *chip, int channel, unsigned int duty)
136 {
137 struct pwm_device *pwm = &chip->pwms[channel];
138 unsigned int on, off;
139
140 if (duty == 0) {
141 /* Set the full OFF bit, which has the highest precedence */
142 pca9685_write_reg(chip, REG_OFF_H(channel), LED_FULL);
143 return;
144 } else if (duty >= PCA9685_COUNTER_RANGE) {
145 /* Set the full ON bit and clear the full OFF bit */
146 pca9685_write_reg(chip, REG_ON_H(channel), LED_FULL);
147 pca9685_write_reg(chip, REG_OFF_H(channel), 0);
148 return;
149 }
150
151
152 if (pwm->state.usage_power && channel < PCA9685_MAXCHAN) {
153 /*
154 * If usage_power is set, the pca9685 driver will phase shift
155 * the individual channels relative to their channel number.
156 * This improves EMI because the enabled channels no longer
157 * turn on at the same time, while still maintaining the
158 * configured duty cycle / power output.
159 */
160 on = channel * PCA9685_COUNTER_RANGE / PCA9685_MAXCHAN;
161 } else
162 on = 0;
163
164 off = (on + duty) % PCA9685_COUNTER_RANGE;
165
166 /* Set ON time (clears full ON bit) */
167 pca9685_write_reg(chip, REG_ON_L(channel), on & 0xff);
168 pca9685_write_reg(chip, REG_ON_H(channel), (on >> 8) & 0xf);
169 /* Set OFF time (clears full OFF bit) */
170 pca9685_write_reg(chip, REG_OFF_L(channel), off & 0xff);
171 pca9685_write_reg(chip, REG_OFF_H(channel), (off >> 8) & 0xf);
172 }
173
pca9685_pwm_get_duty(struct pwm_chip * chip,int channel)174 static unsigned int pca9685_pwm_get_duty(struct pwm_chip *chip, int channel)
175 {
176 struct pwm_device *pwm = &chip->pwms[channel];
177 unsigned int off = 0, on = 0, val = 0;
178
179 if (WARN_ON(channel >= PCA9685_MAXCHAN)) {
180 /* HW does not support reading state of "all LEDs" channel */
181 return 0;
182 }
183
184 pca9685_read_reg(chip, LED_N_OFF_H(channel), &off);
185 if (off & LED_FULL) {
186 /* Full OFF bit is set */
187 return 0;
188 }
189
190 pca9685_read_reg(chip, LED_N_ON_H(channel), &on);
191 if (on & LED_FULL) {
192 /* Full ON bit is set */
193 return PCA9685_COUNTER_RANGE;
194 }
195
196 pca9685_read_reg(chip, LED_N_OFF_L(channel), &val);
197 off = ((off & 0xf) << 8) | (val & 0xff);
198 if (!pwm->state.usage_power)
199 return off;
200
201 /* Read ON register to calculate duty cycle of staggered output */
202 if (pca9685_read_reg(chip, LED_N_ON_L(channel), &val)) {
203 /* Reset val to 0 in case reading LED_N_ON_L failed */
204 val = 0;
205 }
206 on = ((on & 0xf) << 8) | (val & 0xff);
207 return (off - on) & (PCA9685_COUNTER_RANGE - 1);
208 }
209
210 #if IS_ENABLED(CONFIG_GPIOLIB)
pca9685_pwm_test_and_set_inuse(struct pca9685 * pca,int pwm_idx)211 static bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, int pwm_idx)
212 {
213 bool is_inuse;
214
215 mutex_lock(&pca->lock);
216 if (pwm_idx >= PCA9685_MAXCHAN) {
217 /*
218 * "All LEDs" channel:
219 * pretend already in use if any of the PWMs are requested
220 */
221 if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) {
222 is_inuse = true;
223 goto out;
224 }
225 } else {
226 /*
227 * Regular channel:
228 * pretend already in use if the "all LEDs" channel is requested
229 */
230 if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) {
231 is_inuse = true;
232 goto out;
233 }
234 }
235 is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse);
236 out:
237 mutex_unlock(&pca->lock);
238 return is_inuse;
239 }
240
pca9685_pwm_clear_inuse(struct pca9685 * pca,int pwm_idx)241 static void pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
242 {
243 mutex_lock(&pca->lock);
244 clear_bit(pwm_idx, pca->pwms_inuse);
245 mutex_unlock(&pca->lock);
246 }
247
pca9685_pwm_gpio_request(struct gpio_chip * gpio,unsigned int offset)248 static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
249 {
250 struct pwm_chip *chip = gpiochip_get_data(gpio);
251 struct pca9685 *pca = to_pca(chip);
252
253 if (pca9685_pwm_test_and_set_inuse(pca, offset))
254 return -EBUSY;
255 pm_runtime_get_sync(pwmchip_parent(chip));
256 return 0;
257 }
258
pca9685_pwm_gpio_get(struct gpio_chip * gpio,unsigned int offset)259 static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
260 {
261 struct pwm_chip *chip = gpiochip_get_data(gpio);
262
263 return pca9685_pwm_get_duty(chip, offset) != 0;
264 }
265
pca9685_pwm_gpio_set(struct gpio_chip * gpio,unsigned int offset,int value)266 static int pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
267 int value)
268 {
269 struct pwm_chip *chip = gpiochip_get_data(gpio);
270
271 pca9685_pwm_set_duty(chip, offset, value ? PCA9685_COUNTER_RANGE : 0);
272
273 return 0;
274 }
275
pca9685_pwm_gpio_free(struct gpio_chip * gpio,unsigned int offset)276 static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
277 {
278 struct pwm_chip *chip = gpiochip_get_data(gpio);
279 struct pca9685 *pca = to_pca(chip);
280
281 pca9685_pwm_set_duty(chip, offset, 0);
282 pm_runtime_put(pwmchip_parent(chip));
283 pca9685_pwm_clear_inuse(pca, offset);
284 }
285
pca9685_pwm_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)286 static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
287 unsigned int offset)
288 {
289 /* Always out */
290 return GPIO_LINE_DIRECTION_OUT;
291 }
292
pca9685_pwm_gpio_direction_input(struct gpio_chip * gpio,unsigned int offset)293 static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
294 unsigned int offset)
295 {
296 return -EINVAL;
297 }
298
pca9685_pwm_gpio_direction_output(struct gpio_chip * gpio,unsigned int offset,int value)299 static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
300 unsigned int offset, int value)
301 {
302 pca9685_pwm_gpio_set(gpio, offset, value);
303
304 return 0;
305 }
306
307 /*
308 * The PCA9685 has a bit for turning the PWM output full off or on. Some
309 * boards like Intel Galileo actually uses these as normal GPIOs so we
310 * expose a GPIO chip here which can exclusively take over the underlying
311 * PWM channel.
312 */
pca9685_pwm_gpio_probe(struct pwm_chip * chip)313 static int pca9685_pwm_gpio_probe(struct pwm_chip *chip)
314 {
315 struct pca9685 *pca = to_pca(chip);
316 struct device *dev = pwmchip_parent(chip);
317
318 pca->gpio.label = dev_name(dev);
319 pca->gpio.parent = dev;
320 pca->gpio.request = pca9685_pwm_gpio_request;
321 pca->gpio.free = pca9685_pwm_gpio_free;
322 pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
323 pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
324 pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
325 pca->gpio.get = pca9685_pwm_gpio_get;
326 pca->gpio.set_rv = pca9685_pwm_gpio_set;
327 pca->gpio.base = -1;
328 pca->gpio.ngpio = PCA9685_MAXCHAN;
329 pca->gpio.can_sleep = true;
330
331 return devm_gpiochip_add_data(dev, &pca->gpio, chip);
332 }
333 #else
pca9685_pwm_test_and_set_inuse(struct pca9685 * pca,int pwm_idx)334 static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca,
335 int pwm_idx)
336 {
337 return false;
338 }
339
340 static inline void
pca9685_pwm_clear_inuse(struct pca9685 * pca,int pwm_idx)341 pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
342 {
343 }
344
pca9685_pwm_gpio_probe(struct pwm_chip * chip)345 static inline int pca9685_pwm_gpio_probe(struct pwm_chip *chip)
346 {
347 return 0;
348 }
349 #endif
350
pca9685_set_sleep_mode(struct pwm_chip * chip,bool enable)351 static void pca9685_set_sleep_mode(struct pwm_chip *chip, bool enable)
352 {
353 struct device *dev = pwmchip_parent(chip);
354 struct pca9685 *pca = to_pca(chip);
355 int err = regmap_update_bits(pca->regmap, PCA9685_MODE1,
356 MODE1_SLEEP, enable ? MODE1_SLEEP : 0);
357 if (err) {
358 dev_err(dev, "regmap_update_bits of register 0x%x failed: %pe\n",
359 PCA9685_MODE1, ERR_PTR(err));
360 return;
361 }
362
363 if (!enable) {
364 /* Wait 500us for the oscillator to be back up */
365 udelay(500);
366 }
367 }
368
__pca9685_pwm_apply(struct pwm_chip * chip,struct pwm_device * pwm,const struct pwm_state * state)369 static int __pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
370 const struct pwm_state *state)
371 {
372 struct pca9685 *pca = to_pca(chip);
373 unsigned long long duty, prescale;
374 unsigned int val = 0;
375
376 if (state->polarity != PWM_POLARITY_NORMAL)
377 return -EINVAL;
378
379 prescale = DIV_ROUND_CLOSEST_ULL(PCA9685_OSC_CLOCK_MHZ * state->period,
380 PCA9685_COUNTER_RANGE * 1000) - 1;
381 if (prescale < PCA9685_PRESCALE_MIN || prescale > PCA9685_PRESCALE_MAX) {
382 dev_err(pwmchip_parent(chip), "pwm not changed: period out of bounds!\n");
383 return -EINVAL;
384 }
385
386 if (!state->enabled) {
387 pca9685_pwm_set_duty(chip, pwm->hwpwm, 0);
388 return 0;
389 }
390
391 pca9685_read_reg(chip, PCA9685_PRESCALE, &val);
392 if (prescale != val) {
393 if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) {
394 dev_err(pwmchip_parent(chip),
395 "pwm not changed: periods of enabled pwms must match!\n");
396 return -EBUSY;
397 }
398
399 /*
400 * Putting the chip briefly into SLEEP mode
401 * at this point won't interfere with the
402 * pm_runtime framework, because the pm_runtime
403 * state is guaranteed active here.
404 */
405 /* Put chip into sleep mode */
406 pca9685_set_sleep_mode(chip, true);
407
408 /* Change the chip-wide output frequency */
409 pca9685_write_reg(chip, PCA9685_PRESCALE, prescale);
410
411 /* Wake the chip up */
412 pca9685_set_sleep_mode(chip, false);
413 }
414
415 duty = PCA9685_COUNTER_RANGE * state->duty_cycle;
416 duty = DIV_ROUND_UP_ULL(duty, state->period);
417 pca9685_pwm_set_duty(chip, pwm->hwpwm, duty);
418 return 0;
419 }
420
pca9685_pwm_apply(struct pwm_chip * chip,struct pwm_device * pwm,const struct pwm_state * state)421 static int pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
422 const struct pwm_state *state)
423 {
424 struct pca9685 *pca = to_pca(chip);
425 int ret;
426
427 mutex_lock(&pca->lock);
428 ret = __pca9685_pwm_apply(chip, pwm, state);
429 if (ret == 0) {
430 if (state->enabled)
431 set_bit(pwm->hwpwm, pca->pwms_enabled);
432 else
433 clear_bit(pwm->hwpwm, pca->pwms_enabled);
434 }
435 mutex_unlock(&pca->lock);
436
437 return ret;
438 }
439
pca9685_pwm_get_state(struct pwm_chip * chip,struct pwm_device * pwm,struct pwm_state * state)440 static int pca9685_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
441 struct pwm_state *state)
442 {
443 unsigned long long duty;
444 unsigned int val = 0;
445
446 /* Calculate (chip-wide) period from prescale value */
447 pca9685_read_reg(chip, PCA9685_PRESCALE, &val);
448 /*
449 * PCA9685_OSC_CLOCK_MHZ is 25, i.e. an integer divider of 1000.
450 * The following calculation is therefore only a multiplication
451 * and we are not losing precision.
452 */
453 state->period = (PCA9685_COUNTER_RANGE * 1000 / PCA9685_OSC_CLOCK_MHZ) *
454 (val + 1);
455
456 /* The (per-channel) polarity is fixed */
457 state->polarity = PWM_POLARITY_NORMAL;
458
459 if (pwm->hwpwm >= PCA9685_MAXCHAN) {
460 /*
461 * The "all LEDs" channel does not support HW readout
462 * Return 0 and disabled for backwards compatibility
463 */
464 state->duty_cycle = 0;
465 state->enabled = false;
466 return 0;
467 }
468
469 state->enabled = true;
470 duty = pca9685_pwm_get_duty(chip, pwm->hwpwm);
471 state->duty_cycle = DIV_ROUND_DOWN_ULL(duty * state->period, PCA9685_COUNTER_RANGE);
472
473 return 0;
474 }
475
pca9685_pwm_request(struct pwm_chip * chip,struct pwm_device * pwm)476 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
477 {
478 struct pca9685 *pca = to_pca(chip);
479
480 if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm))
481 return -EBUSY;
482
483 if (pwm->hwpwm < PCA9685_MAXCHAN) {
484 /* PWMs - except the "all LEDs" channel - default to enabled */
485 mutex_lock(&pca->lock);
486 set_bit(pwm->hwpwm, pca->pwms_enabled);
487 mutex_unlock(&pca->lock);
488 }
489
490 pm_runtime_get_sync(pwmchip_parent(chip));
491
492 return 0;
493 }
494
pca9685_pwm_free(struct pwm_chip * chip,struct pwm_device * pwm)495 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
496 {
497 struct pca9685 *pca = to_pca(chip);
498
499 mutex_lock(&pca->lock);
500 pca9685_pwm_set_duty(chip, pwm->hwpwm, 0);
501 clear_bit(pwm->hwpwm, pca->pwms_enabled);
502 mutex_unlock(&pca->lock);
503
504 pm_runtime_put(pwmchip_parent(chip));
505 pca9685_pwm_clear_inuse(pca, pwm->hwpwm);
506 }
507
508 static const struct pwm_ops pca9685_pwm_ops = {
509 .apply = pca9685_pwm_apply,
510 .get_state = pca9685_pwm_get_state,
511 .request = pca9685_pwm_request,
512 .free = pca9685_pwm_free,
513 };
514
515 static const struct regmap_config pca9685_regmap_i2c_config = {
516 .reg_bits = 8,
517 .val_bits = 8,
518 .max_register = PCA9685_NUMREGS,
519 .cache_type = REGCACHE_NONE,
520 };
521
pca9685_pwm_probe(struct i2c_client * client)522 static int pca9685_pwm_probe(struct i2c_client *client)
523 {
524 struct pwm_chip *chip;
525 struct pca9685 *pca;
526 unsigned int reg;
527 int ret;
528
529 /* Add an extra channel for ALL_LED */
530 chip = devm_pwmchip_alloc(&client->dev, PCA9685_MAXCHAN + 1, sizeof(*pca));
531 if (IS_ERR(chip))
532 return PTR_ERR(chip);
533 pca = to_pca(chip);
534
535 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
536 if (IS_ERR(pca->regmap)) {
537 ret = PTR_ERR(pca->regmap);
538 dev_err(&client->dev, "Failed to initialize register map: %d\n",
539 ret);
540 return ret;
541 }
542
543 i2c_set_clientdata(client, chip);
544
545 mutex_init(&pca->lock);
546
547 ret = pca9685_read_reg(chip, PCA9685_MODE2, ®);
548 if (ret)
549 return ret;
550
551 if (device_property_read_bool(&client->dev, "invert"))
552 reg |= MODE2_INVRT;
553 else
554 reg &= ~MODE2_INVRT;
555
556 if (device_property_read_bool(&client->dev, "open-drain"))
557 reg &= ~MODE2_OUTDRV;
558 else
559 reg |= MODE2_OUTDRV;
560
561 ret = pca9685_write_reg(chip, PCA9685_MODE2, reg);
562 if (ret)
563 return ret;
564
565 /* Disable all LED ALLCALL and SUBx addresses to avoid bus collisions */
566 pca9685_read_reg(chip, PCA9685_MODE1, ®);
567 reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3);
568 pca9685_write_reg(chip, PCA9685_MODE1, reg);
569
570 /* Reset OFF/ON registers to POR default */
571 pca9685_write_reg(chip, PCA9685_ALL_LED_OFF_L, 0);
572 pca9685_write_reg(chip, PCA9685_ALL_LED_OFF_H, LED_FULL);
573 pca9685_write_reg(chip, PCA9685_ALL_LED_ON_L, 0);
574 pca9685_write_reg(chip, PCA9685_ALL_LED_ON_H, LED_FULL);
575
576 chip->ops = &pca9685_pwm_ops;
577
578 ret = pwmchip_add(chip);
579 if (ret < 0)
580 return ret;
581
582 ret = pca9685_pwm_gpio_probe(chip);
583 if (ret < 0) {
584 pwmchip_remove(chip);
585 return ret;
586 }
587
588 pm_runtime_enable(&client->dev);
589
590 if (pm_runtime_enabled(&client->dev)) {
591 /*
592 * Although the chip comes out of power-up in the sleep state,
593 * we force it to sleep in case it was woken up before
594 */
595 pca9685_set_sleep_mode(chip, true);
596 pm_runtime_set_suspended(&client->dev);
597 } else {
598 /* Wake the chip up if runtime PM is disabled */
599 pca9685_set_sleep_mode(chip, false);
600 }
601
602 return 0;
603 }
604
pca9685_pwm_remove(struct i2c_client * client)605 static void pca9685_pwm_remove(struct i2c_client *client)
606 {
607 struct pwm_chip *chip = i2c_get_clientdata(client);
608
609 pwmchip_remove(chip);
610
611 if (!pm_runtime_enabled(&client->dev)) {
612 /* Put chip in sleep state if runtime PM is disabled */
613 pca9685_set_sleep_mode(chip, true);
614 }
615
616 pm_runtime_disable(&client->dev);
617 }
618
pca9685_pwm_runtime_suspend(struct device * dev)619 static int __maybe_unused pca9685_pwm_runtime_suspend(struct device *dev)
620 {
621 struct i2c_client *client = to_i2c_client(dev);
622 struct pwm_chip *chip = i2c_get_clientdata(client);
623
624 pca9685_set_sleep_mode(chip, true);
625 return 0;
626 }
627
pca9685_pwm_runtime_resume(struct device * dev)628 static int __maybe_unused pca9685_pwm_runtime_resume(struct device *dev)
629 {
630 struct i2c_client *client = to_i2c_client(dev);
631 struct pwm_chip *chip = i2c_get_clientdata(client);
632
633 pca9685_set_sleep_mode(chip, false);
634 return 0;
635 }
636
637 static const struct i2c_device_id pca9685_id[] = {
638 { "pca9685" },
639 { /* sentinel */ }
640 };
641 MODULE_DEVICE_TABLE(i2c, pca9685_id);
642
643 static const struct acpi_device_id pca9685_acpi_ids[] = {
644 { "INT3492", 0 },
645 { /* sentinel */ },
646 };
647 MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
648
649 static const struct of_device_id pca9685_dt_ids[] = {
650 { .compatible = "nxp,pca9685-pwm", },
651 { /* sentinel */ }
652 };
653 MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
654
655 static const struct dev_pm_ops pca9685_pwm_pm = {
656 SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend,
657 pca9685_pwm_runtime_resume, NULL)
658 };
659
660 static struct i2c_driver pca9685_i2c_driver = {
661 .driver = {
662 .name = "pca9685-pwm",
663 .acpi_match_table = pca9685_acpi_ids,
664 .of_match_table = pca9685_dt_ids,
665 .pm = &pca9685_pwm_pm,
666 },
667 .probe = pca9685_pwm_probe,
668 .remove = pca9685_pwm_remove,
669 .id_table = pca9685_id,
670 };
671
672 module_i2c_driver(pca9685_i2c_driver);
673
674 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
675 MODULE_DESCRIPTION("PWM driver for PCA9685");
676 MODULE_LICENSE("GPL");
677