blob: 4a55dc18656c51ce876cd4e086e78d26c11fd4e5 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Steffen Trumtrar88b613e2013-05-30 09:50:12 +02002/*
3 * Driver for PCA9685 16-channel 12-bit PWM LED controller
4 *
5 * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
Clemens Gruber01ec8472015-07-23 17:19:02 +02006 * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com>
Steffen Trumtrar88b613e2013-05-30 09:50:12 +02007 *
8 * based on the pwm-twl-led.c driver
Steffen Trumtrar88b613e2013-05-30 09:50:12 +02009 */
10
Andy Shevchenko912b8432015-10-07 13:18:49 +030011#include <linux/acpi.h>
Mika Westerbergbccec892016-09-20 17:40:56 +030012#include <linux/gpio/driver.h>
Steffen Trumtrar88b613e2013-05-30 09:50:12 +020013#include <linux/i2c.h>
14#include <linux/module.h>
Mika Westerbergbccec892016-09-20 17:40:56 +030015#include <linux/mutex.h>
Steffen Trumtrar88b613e2013-05-30 09:50:12 +020016#include <linux/platform_device.h>
Andy Shevchenko912b8432015-10-07 13:18:49 +030017#include <linux/property.h>
Steffen Trumtrar88b613e2013-05-30 09:50:12 +020018#include <linux/pwm.h>
19#include <linux/regmap.h>
20#include <linux/slab.h>
Clemens Gruber01ec8472015-07-23 17:19:02 +020021#include <linux/delay.h>
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -040022#include <linux/pm_runtime.h>
Sven Van Asbroeck9cc5f232020-04-01 19:01:06 +020023#include <linux/bitmap.h>
Clemens Gruber01ec8472015-07-23 17:19:02 +020024
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 Trumtrar88b613e2013-05-30 09:50:12 +020032
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 Gruber01ec8472015-07-23 17:19:02 +020050#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 Trumtrar88b613e2013-05-30 09:50:12 +020057#define PCA9685_NUMREGS 0xFF
58#define PCA9685_MAXCHAN 0x10
59
David Jandere1057a82020-08-28 14:14:14 +020060#define LED_FULL BIT(4)
David Janderbce54362020-08-28 14:14:15 +020061#define MODE1_ALLCALL BIT(0)
62#define MODE1_SUB3 BIT(1)
63#define MODE1_SUB2 BIT(2)
64#define MODE1_SUB1 BIT(3)
David Jandere1057a82020-08-28 14:14:14 +020065#define MODE1_SLEEP BIT(4)
66#define MODE2_INVRT BIT(4)
67#define MODE2_OUTDRV BIT(2)
Steffen Trumtrar88b613e2013-05-30 09:50:12 +020068
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
74struct pca9685 {
75 struct pwm_chip chip;
76 struct regmap *regmap;
Clemens Gruber01ec8472015-07-23 17:19:02 +020077 int period_ns;
Mika Westerbergbccec892016-09-20 17:40:56 +030078#if IS_ENABLED(CONFIG_GPIOLIB)
79 struct mutex lock;
80 struct gpio_chip gpio;
Sven Van Asbroeck9cc5f232020-04-01 19:01:06 +020081 DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1);
Mika Westerbergbccec892016-09-20 17:40:56 +030082#endif
Steffen Trumtrar88b613e2013-05-30 09:50:12 +020083};
84
85static inline struct pca9685 *to_pca(struct pwm_chip *chip)
86{
87 return container_of(chip, struct pca9685, chip);
88}
89
Mika Westerbergbccec892016-09-20 17:40:56 +030090#if IS_ENABLED(CONFIG_GPIOLIB)
Sven Van Asbroeck9cc5f232020-04-01 19:01:06 +020091static 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 Jander316b676b2020-08-28 14:14:13 +020098 * "All LEDs" channel:
Sven Van Asbroeck9cc5f232020-04-01 19:01:06 +020099 * 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 Jander316b676b2020-08-28 14:14:13 +0200107 * Regular channel:
Sven Van Asbroeck9cc5f232020-04-01 19:01:06 +0200108 * 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);
116out:
117 mutex_unlock(&pca->lock);
118 return is_inuse;
119}
120
121static 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 Westerbergbccec892016-09-20 17:40:56 +0300128static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
129{
130 struct pca9685 *pca = gpiochip_get_data(gpio);
Mika Westerbergbccec892016-09-20 17:40:56 +0300131
Sven Van Asbroeck9cc5f232020-04-01 19:01:06 +0200132 if (pca9685_pwm_test_and_set_inuse(pca, offset))
Mika Westerbergbccec892016-09-20 17:40:56 +0300133 return -EBUSY;
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400134 pm_runtime_get_sync(pca->chip.dev);
Mika Westerbergbccec892016-09-20 17:40:56 +0300135 return 0;
136}
137
Mika Westerbergbccec892016-09-20 17:40:56 +0300138static 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
149static 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 Asbroeckc40c4612017-04-13 08:58:11 -0400164static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
165{
166 struct pca9685 *pca = gpiochip_get_data(gpio);
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400167
168 pca9685_pwm_gpio_set(gpio, offset, 0);
169 pm_runtime_put(pca->chip.dev);
Sven Van Asbroeck9cc5f232020-04-01 19:01:06 +0200170 pca9685_pwm_clear_inuse(pca, offset);
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400171}
172
Mika Westerbergbccec892016-09-20 17:40:56 +0300173static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
174 unsigned int offset)
175{
176 /* Always out */
Rishi Guptaa37507d2020-03-11 21:22:20 +0530177 return GPIO_LINE_DIRECTION_OUT;
Mika Westerbergbccec892016-09-20 17:40:56 +0300178}
179
180static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
181 unsigned int offset)
182{
183 return -EINVAL;
184}
185
186static 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 */
200static 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 Asbroeck9cc5f232020-04-01 19:01:06 +0200222static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca,
223 int pwm_idx)
Mika Westerbergbccec892016-09-20 17:40:56 +0300224{
225 return false;
226}
227
Sven Van Asbroeck9cc5f232020-04-01 19:01:06 +0200228static inline void
229pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
230{
231}
232
Mika Westerbergbccec892016-09-20 17:40:56 +0300233static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
234{
235 return 0;
236}
237#endif
238
Sven Van Asbroeck08293262017-04-21 09:19:02 -0400239static void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable)
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400240{
241 regmap_update_bits(pca->regmap, PCA9685_MODE1,
Sven Van Asbroeck08293262017-04-21 09:19:02 -0400242 MODE1_SLEEP, enable ? MODE1_SLEEP : 0);
243 if (!enable) {
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400244 /* Wait 500us for the oscillator to be back up */
245 udelay(500);
246 }
247}
248
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200249static 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 Gruber01ec8472015-07-23 17:19:02 +0200255 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 Asbroeckc40c4612017-04-13 08:58:11 -0400263 /*
David Jander316b676b2020-08-28 14:14:13 +0200264 * Putting the chip briefly into SLEEP mode
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400265 * at this point won't interfere with the
266 * pm_runtime framework, because the pm_runtime
267 * state is guaranteed active here.
268 */
Clemens Gruber01ec8472015-07-23 17:19:02 +0200269 /* Put chip into sleep mode */
Sven Van Asbroeck08293262017-04-21 09:19:02 -0400270 pca9685_set_sleep_mode(pca, true);
Clemens Gruber01ec8472015-07-23 17:19:02 +0200271
272 /* Change the chip-wide output frequency */
273 regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
274
275 /* Wake the chip up */
Sven Van Asbroeck08293262017-04-21 09:19:02 -0400276 pca9685_set_sleep_mode(pca, false);
Clemens Gruber01ec8472015-07-23 17:19:02 +0200277
278 pca->period_ns = period_ns;
Clemens Gruber01ec8472015-07-23 17:19:02 +0200279 } else {
280 dev_err(chip->dev,
281 "prescaler not set: period out of bounds!\n");
282 return -EINVAL;
283 }
284 }
285
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200286 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 Gruber4a627b52015-07-23 17:19:01 +0200298 /* 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 Trumtrar88b613e2013-05-30 09:50:12 +0200314 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 Gruber01ec8472015-07-23 17:19:02 +0200324 duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200325 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 Gruber4a627b52015-07-23 17:19:01 +0200341 /* 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 Trumtrar88b613e2013-05-30 09:50:12 +0200349 return 0;
350}
351
352static 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
389static 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
410static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
411{
412 struct pca9685 *pca = to_pca(chip);
413
Sven Van Asbroeck9cc5f232020-04-01 19:01:06 +0200414 if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm))
Mika Westerbergbccec892016-09-20 17:40:56 +0300415 return -EBUSY;
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400416 pm_runtime_get_sync(chip->dev);
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200417
418 return 0;
419}
420
421static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
422{
Sven Van Asbroeck9cc5f232020-04-01 19:01:06 +0200423 struct pca9685 *pca = to_pca(chip);
424
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400425 pca9685_pwm_disable(chip, pwm);
426 pm_runtime_put(chip->dev);
Sven Van Asbroeck9cc5f232020-04-01 19:01:06 +0200427 pca9685_pwm_clear_inuse(pca, pwm->hwpwm);
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200428}
429
430static 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 Reding3dd0a902013-06-12 13:18:29 +0200436 .owner = THIS_MODULE,
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200437};
438
Krzysztof Kozlowskic456fbb2015-02-24 10:40:24 +0100439static const struct regmap_config pca9685_regmap_i2c_config = {
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200440 .reg_bits = 8,
441 .val_bits = 8,
442 .max_register = PCA9685_NUMREGS,
443 .cache_type = REGCACHE_NONE,
444};
445
446static int pca9685_pwm_probe(struct i2c_client *client,
447 const struct i2c_device_id *id)
448{
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200449 struct pca9685 *pca;
David Janderbce54362020-08-28 14:14:15 +0200450 unsigned int reg;
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200451 int ret;
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200452
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 Gruber01ec8472015-07-23 17:19:02 +0200464 pca->period_ns = PCA9685_DEFAULT_PERIOD;
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200465
466 i2c_set_clientdata(client, pca);
467
David Janderbce54362020-08-28 14:14:15 +0200468 regmap_read(pca->regmap, PCA9685_MODE2, &reg);
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200469
Andy Shevchenko912b8432015-10-07 13:18:49 +0300470 if (device_property_read_bool(&client->dev, "invert"))
David Janderbce54362020-08-28 14:14:15 +0200471 reg |= MODE2_INVRT;
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200472 else
David Janderbce54362020-08-28 14:14:15 +0200473 reg &= ~MODE2_INVRT;
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200474
Andy Shevchenko912b8432015-10-07 13:18:49 +0300475 if (device_property_read_bool(&client->dev, "open-drain"))
David Janderbce54362020-08-28 14:14:15 +0200476 reg &= ~MODE2_OUTDRV;
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200477 else
David Janderbce54362020-08-28 14:14:15 +0200478 reg |= MODE2_OUTDRV;
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200479
David Janderbce54362020-08-28 14:14:15 +0200480 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, &reg);
484 reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3);
485 regmap_write(pca->regmap, PCA9685_MODE1, reg);
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200486
David Jander316b676b2020-08-28 14:14:13 +0200487 /* Clear all "full off" bits */
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200488 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 Jander316b676b2020-08-28 14:14:13 +0200492 /* Add an extra channel for ALL_LED */
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200493 pca->chip.npwm = PCA9685_MAXCHAN + 1;
494
495 pca->chip.dev = &client->dev;
496 pca->chip.base = -1;
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200497
Mika Westerbergbccec892016-09-20 17:40:56 +0300498 ret = pwmchip_add(&pca->chip);
499 if (ret < 0)
500 return ret;
501
502 ret = pca9685_pwm_gpio_probe(pca);
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400503 if (ret < 0) {
Mika Westerbergbccec892016-09-20 17:40:56 +0300504 pwmchip_remove(&pca->chip);
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400505 return ret;
506 }
Mika Westerbergbccec892016-09-20 17:40:56 +0300507
David Jander316b676b2020-08-28 14:14:13 +0200508 /* The chip comes out of power-up in the active state */
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400509 pm_runtime_set_active(&client->dev);
510 /*
David Jander316b676b2020-08-28 14:14:13 +0200511 * Enable will put the chip into suspend, which is what we
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400512 * want as all outputs are disabled at this point
513 */
514 pm_runtime_enable(&client->dev);
515
516 return 0;
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200517}
518
519static int pca9685_pwm_remove(struct i2c_client *client)
520{
521 struct pca9685 *pca = i2c_get_clientdata(client);
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400522 int ret;
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200523
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400524 ret = pwmchip_remove(&pca->chip);
525 if (ret)
526 return ret;
527 pm_runtime_disable(&client->dev);
528 return 0;
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200529}
530
Rishi Gupta408a7592020-03-11 21:13:49 +0530531static int __maybe_unused pca9685_pwm_runtime_suspend(struct device *dev)
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400532{
533 struct i2c_client *client = to_i2c_client(dev);
534 struct pca9685 *pca = i2c_get_clientdata(client);
535
Sven Van Asbroeck08293262017-04-21 09:19:02 -0400536 pca9685_set_sleep_mode(pca, true);
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400537 return 0;
538}
539
Rishi Gupta408a7592020-03-11 21:13:49 +0530540static int __maybe_unused pca9685_pwm_runtime_resume(struct device *dev)
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400541{
542 struct i2c_client *client = to_i2c_client(dev);
543 struct pca9685 *pca = i2c_get_clientdata(client);
544
Sven Van Asbroeck08293262017-04-21 09:19:02 -0400545 pca9685_set_sleep_mode(pca, false);
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400546 return 0;
547}
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400548
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200549static const struct i2c_device_id pca9685_id[] = {
550 { "pca9685", 0 },
551 { /* sentinel */ },
552};
553MODULE_DEVICE_TABLE(i2c, pca9685_id);
554
Andy Shevchenko912b8432015-10-07 13:18:49 +0300555#ifdef CONFIG_ACPI
556static const struct acpi_device_id pca9685_acpi_ids[] = {
557 { "INT3492", 0 },
558 { /* sentinel */ },
559};
560MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
561#endif
562
563#ifdef CONFIG_OF
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200564static const struct of_device_id pca9685_dt_ids[] = {
565 { .compatible = "nxp,pca9685-pwm", },
566 { /* sentinel */ }
567};
568MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
Andy Shevchenko912b8432015-10-07 13:18:49 +0300569#endif
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200570
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400571static 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 Trumtrar88b613e2013-05-30 09:50:12 +0200576static struct i2c_driver pca9685_i2c_driver = {
577 .driver = {
578 .name = "pca9685-pwm",
Andy Shevchenko912b8432015-10-07 13:18:49 +0300579 .acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
580 .of_match_table = of_match_ptr(pca9685_dt_ids),
Sven Van Asbroeckc40c4612017-04-13 08:58:11 -0400581 .pm = &pca9685_pwm_pm,
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200582 },
583 .probe = pca9685_pwm_probe,
584 .remove = pca9685_pwm_remove,
585 .id_table = pca9685_id,
586};
587
588module_i2c_driver(pca9685_i2c_driver);
589
590MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
591MODULE_DESCRIPTION("PWM driver for PCA9685");
592MODULE_LICENSE("GPL");