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