This source file includes following definitions.
- to_pca
- pca9685_pwm_test_and_set_inuse
- pca9685_pwm_clear_inuse
- pca9685_pwm_gpio_request
- pca9685_pwm_gpio_get
- pca9685_pwm_gpio_set
- pca9685_pwm_gpio_free
- pca9685_pwm_gpio_get_direction
- pca9685_pwm_gpio_direction_input
- pca9685_pwm_gpio_direction_output
- pca9685_pwm_gpio_probe
- pca9685_pwm_test_and_set_inuse
- pca9685_pwm_clear_inuse
- pca9685_pwm_gpio_probe
- pca9685_set_sleep_mode
- pca9685_pwm_config
- pca9685_pwm_enable
- pca9685_pwm_disable
- pca9685_pwm_request
- pca9685_pwm_free
- pca9685_pwm_probe
- pca9685_pwm_remove
- pca9685_pwm_runtime_suspend
- pca9685_pwm_runtime_resume
1
2
3
4
5
6
7
8
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
27
28
29
30
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
51 #define PCA9685_PRESCALE_MAX 0xFF
52
53 #define PCA9685_COUNTER_RANGE 4096
54 #define PCA9685_DEFAULT_PERIOD 5000000
55 #define PCA9685_OSC_CLOCK_MHZ 25
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
96
97
98 if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) {
99 is_inuse = true;
100 goto out;
101 }
102 } else {
103
104
105
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
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
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
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
193
194
195
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
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
262
263
264
265
266
267 pca9685_set_sleep_mode(pca, true);
268
269
270 regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
271
272
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
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
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
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
358
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
376
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
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
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
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
504 pm_runtime_set_active(&client->dev);
505
506
507
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 { },
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 { },
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 { }
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");