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