Lines Matching +full:pwm +full:- +full:leds
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for PCA9685 16-channel 12-bit PWM LED controller
8 * based on the pwm-twl-led.c driver
18 #include <linux/pwm.h>
28 * PWM channels requested afterwards must use a period that results in the same
97 /* No PWM enabled: Change allowed */ in pca9685_prescaler_can_change()
98 if (bitmap_empty(pca->pwms_enabled, PCA9685_MAXCHAN + 1)) in pca9685_prescaler_can_change()
100 /* More than one PWM enabled: Change not allowed */ in pca9685_prescaler_can_change()
101 if (bitmap_weight(pca->pwms_enabled, PCA9685_MAXCHAN + 1) > 1) in pca9685_prescaler_can_change()
104 * Only one PWM enabled: Change allowed if the PWM about to in pca9685_prescaler_can_change()
107 return test_bit(channel, pca->pwms_enabled); in pca9685_prescaler_can_change()
112 struct device *dev = pca->chip.dev; in pca9685_read_reg()
115 err = regmap_read(pca->regmap, reg, val); in pca9685_read_reg()
124 struct device *dev = pca->chip.dev; in pca9685_write_reg()
127 err = regmap_write(pca->regmap, reg, val); in pca9685_write_reg()
134 /* Helper function to set the duty cycle ratio to duty/4096 (e.g. duty=2048 -> 50%) */
137 struct pwm_device *pwm = &pca->chip.pwms[channel]; in pca9685_pwm_set_duty() local
152 if (pwm->state.usage_power && channel < PCA9685_MAXCHAN) { in pca9685_pwm_set_duty()
176 struct pwm_device *pwm = &pca->chip.pwms[channel]; in pca9685_pwm_get_duty() local
180 /* HW does not support reading state of "all LEDs" channel */ in pca9685_pwm_get_duty()
198 if (!pwm->state.usage_power) in pca9685_pwm_get_duty()
207 return (off - on) & (PCA9685_COUNTER_RANGE - 1); in pca9685_pwm_get_duty()
215 mutex_lock(&pca->lock); in pca9685_pwm_test_and_set_inuse()
218 * "All LEDs" channel: in pca9685_pwm_test_and_set_inuse()
221 if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) { in pca9685_pwm_test_and_set_inuse()
228 * pretend already in use if the "all LEDs" channel is requested in pca9685_pwm_test_and_set_inuse()
230 if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) { in pca9685_pwm_test_and_set_inuse()
235 is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse); in pca9685_pwm_test_and_set_inuse()
237 mutex_unlock(&pca->lock); in pca9685_pwm_test_and_set_inuse()
243 mutex_lock(&pca->lock); in pca9685_pwm_clear_inuse()
244 clear_bit(pwm_idx, pca->pwms_inuse); in pca9685_pwm_clear_inuse()
245 mutex_unlock(&pca->lock); in pca9685_pwm_clear_inuse()
253 return -EBUSY; in pca9685_pwm_gpio_request()
254 pm_runtime_get_sync(pca->chip.dev); in pca9685_pwm_gpio_request()
278 pm_runtime_put(pca->chip.dev); in pca9685_pwm_gpio_free()
292 return -EINVAL; in pca9685_pwm_gpio_direction_input()
304 * The PCA9685 has a bit for turning the PWM output full off or on. Some
307 * PWM channel.
311 struct device *dev = pca->chip.dev; in pca9685_pwm_gpio_probe()
313 pca->gpio.label = dev_name(dev); in pca9685_pwm_gpio_probe()
314 pca->gpio.parent = dev; in pca9685_pwm_gpio_probe()
315 pca->gpio.request = pca9685_pwm_gpio_request; in pca9685_pwm_gpio_probe()
316 pca->gpio.free = pca9685_pwm_gpio_free; in pca9685_pwm_gpio_probe()
317 pca->gpio.get_direction = pca9685_pwm_gpio_get_direction; in pca9685_pwm_gpio_probe()
318 pca->gpio.direction_input = pca9685_pwm_gpio_direction_input; in pca9685_pwm_gpio_probe()
319 pca->gpio.direction_output = pca9685_pwm_gpio_direction_output; in pca9685_pwm_gpio_probe()
320 pca->gpio.get = pca9685_pwm_gpio_get; in pca9685_pwm_gpio_probe()
321 pca->gpio.set = pca9685_pwm_gpio_set; in pca9685_pwm_gpio_probe()
322 pca->gpio.base = -1; in pca9685_pwm_gpio_probe()
323 pca->gpio.ngpio = PCA9685_MAXCHAN; in pca9685_pwm_gpio_probe()
324 pca->gpio.can_sleep = true; in pca9685_pwm_gpio_probe()
326 return devm_gpiochip_add_data(dev, &pca->gpio, pca); in pca9685_pwm_gpio_probe()
348 struct device *dev = pca->chip.dev; in pca9685_set_sleep_mode()
349 int err = regmap_update_bits(pca->regmap, PCA9685_MODE1, in pca9685_set_sleep_mode()
363 static int __pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, in __pca9685_pwm_apply() argument
370 if (state->polarity != PWM_POLARITY_NORMAL) in __pca9685_pwm_apply()
371 return -EINVAL; in __pca9685_pwm_apply()
373 prescale = DIV_ROUND_CLOSEST_ULL(PCA9685_OSC_CLOCK_MHZ * state->period, in __pca9685_pwm_apply()
374 PCA9685_COUNTER_RANGE * 1000) - 1; in __pca9685_pwm_apply()
376 dev_err(chip->dev, "pwm not changed: period out of bounds!\n"); in __pca9685_pwm_apply()
377 return -EINVAL; in __pca9685_pwm_apply()
380 if (!state->enabled) { in __pca9685_pwm_apply()
381 pca9685_pwm_set_duty(pca, pwm->hwpwm, 0); in __pca9685_pwm_apply()
387 if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) { in __pca9685_pwm_apply()
388 dev_err(chip->dev, in __pca9685_pwm_apply()
389 "pwm not changed: periods of enabled pwms must match!\n"); in __pca9685_pwm_apply()
390 return -EBUSY; in __pca9685_pwm_apply()
402 /* Change the chip-wide output frequency */ in __pca9685_pwm_apply()
409 duty = PCA9685_COUNTER_RANGE * state->duty_cycle; in __pca9685_pwm_apply()
410 duty = DIV_ROUND_UP_ULL(duty, state->period); in __pca9685_pwm_apply()
411 pca9685_pwm_set_duty(pca, pwm->hwpwm, duty); in __pca9685_pwm_apply()
415 static int pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, in pca9685_pwm_apply() argument
421 mutex_lock(&pca->lock); in pca9685_pwm_apply()
422 ret = __pca9685_pwm_apply(chip, pwm, state); in pca9685_pwm_apply()
424 if (state->enabled) in pca9685_pwm_apply()
425 set_bit(pwm->hwpwm, pca->pwms_enabled); in pca9685_pwm_apply()
427 clear_bit(pwm->hwpwm, pca->pwms_enabled); in pca9685_pwm_apply()
429 mutex_unlock(&pca->lock); in pca9685_pwm_apply()
434 static void pca9685_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, in pca9685_pwm_get_state() argument
441 /* Calculate (chip-wide) period from prescale value */ in pca9685_pwm_get_state()
448 state->period = (PCA9685_COUNTER_RANGE * 1000 / PCA9685_OSC_CLOCK_MHZ) * in pca9685_pwm_get_state()
451 /* The (per-channel) polarity is fixed */ in pca9685_pwm_get_state()
452 state->polarity = PWM_POLARITY_NORMAL; in pca9685_pwm_get_state()
454 if (pwm->hwpwm >= PCA9685_MAXCHAN) { in pca9685_pwm_get_state()
456 * The "all LEDs" channel does not support HW readout in pca9685_pwm_get_state()
459 state->duty_cycle = 0; in pca9685_pwm_get_state()
460 state->enabled = false; in pca9685_pwm_get_state()
464 state->enabled = true; in pca9685_pwm_get_state()
465 duty = pca9685_pwm_get_duty(pca, pwm->hwpwm); in pca9685_pwm_get_state()
466 state->duty_cycle = DIV_ROUND_DOWN_ULL(duty * state->period, PCA9685_COUNTER_RANGE); in pca9685_pwm_get_state()
469 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) in pca9685_pwm_request() argument
473 if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm)) in pca9685_pwm_request()
474 return -EBUSY; in pca9685_pwm_request()
476 if (pwm->hwpwm < PCA9685_MAXCHAN) { in pca9685_pwm_request()
477 /* PWMs - except the "all LEDs" channel - default to enabled */ in pca9685_pwm_request()
478 mutex_lock(&pca->lock); in pca9685_pwm_request()
479 set_bit(pwm->hwpwm, pca->pwms_enabled); in pca9685_pwm_request()
480 mutex_unlock(&pca->lock); in pca9685_pwm_request()
483 pm_runtime_get_sync(chip->dev); in pca9685_pwm_request()
488 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) in pca9685_pwm_free() argument
492 mutex_lock(&pca->lock); in pca9685_pwm_free()
493 pca9685_pwm_set_duty(pca, pwm->hwpwm, 0); in pca9685_pwm_free()
494 clear_bit(pwm->hwpwm, pca->pwms_enabled); in pca9685_pwm_free()
495 mutex_unlock(&pca->lock); in pca9685_pwm_free()
497 pm_runtime_put(chip->dev); in pca9685_pwm_free()
498 pca9685_pwm_clear_inuse(pca, pwm->hwpwm); in pca9685_pwm_free()
523 pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL); in pca9685_pwm_probe()
525 return -ENOMEM; in pca9685_pwm_probe()
527 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config); in pca9685_pwm_probe()
528 if (IS_ERR(pca->regmap)) { in pca9685_pwm_probe()
529 ret = PTR_ERR(pca->regmap); in pca9685_pwm_probe()
530 dev_err(&client->dev, "Failed to initialize register map: %d\n", in pca9685_pwm_probe()
537 mutex_init(&pca->lock); in pca9685_pwm_probe()
543 if (device_property_read_bool(&client->dev, "invert")) in pca9685_pwm_probe()
548 if (device_property_read_bool(&client->dev, "open-drain")) in pca9685_pwm_probe()
568 pca->chip.ops = &pca9685_pwm_ops; in pca9685_pwm_probe()
570 pca->chip.npwm = PCA9685_MAXCHAN + 1; in pca9685_pwm_probe()
572 pca->chip.dev = &client->dev; in pca9685_pwm_probe()
574 ret = pwmchip_add(&pca->chip); in pca9685_pwm_probe()
580 pwmchip_remove(&pca->chip); in pca9685_pwm_probe()
584 pm_runtime_enable(&client->dev); in pca9685_pwm_probe()
586 if (pm_runtime_enabled(&client->dev)) { in pca9685_pwm_probe()
588 * Although the chip comes out of power-up in the sleep state, in pca9685_pwm_probe()
592 pm_runtime_set_suspended(&client->dev); in pca9685_pwm_probe()
605 pwmchip_remove(&pca->chip); in pca9685_pwm_remove()
607 if (!pm_runtime_enabled(&client->dev)) { in pca9685_pwm_remove()
612 pm_runtime_disable(&client->dev); in pca9685_pwm_remove()
649 { .compatible = "nxp,pca9685-pwm", },
662 .name = "pca9685-pwm",
675 MODULE_DESCRIPTION("PWM driver for PCA9685");