Lines Matching +full:pwm +full:- +full:offset
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
96 /* No PWM enabled: Change allowed */ in pca9685_prescaler_can_change()
97 if (bitmap_empty(pca->pwms_enabled, PCA9685_MAXCHAN + 1)) in pca9685_prescaler_can_change()
99 /* More than one PWM enabled: Change not allowed */ in pca9685_prescaler_can_change()
100 if (bitmap_weight(pca->pwms_enabled, PCA9685_MAXCHAN + 1) > 1) in pca9685_prescaler_can_change()
103 * Only one PWM enabled: Change allowed if the PWM about to in pca9685_prescaler_can_change()
106 return test_bit(channel, pca->pwms_enabled); in pca9685_prescaler_can_change()
115 err = regmap_read(pca->regmap, reg, val); in pca9685_read_reg()
128 err = regmap_write(pca->regmap, reg, val); in pca9685_write_reg()
135 /* Helper function to set the duty cycle ratio to duty/4096 (e.g. duty=2048 -> 50%) */
138 struct pwm_device *pwm = &chip->pwms[channel]; in pca9685_pwm_set_duty() local
153 if (pwm->state.usage_power && channel < PCA9685_MAXCHAN) { in pca9685_pwm_set_duty()
177 struct pwm_device *pwm = &chip->pwms[channel]; in pca9685_pwm_get_duty() local
199 if (!pwm->state.usage_power) in pca9685_pwm_get_duty()
208 return (off - on) & (PCA9685_COUNTER_RANGE - 1); in pca9685_pwm_get_duty()
216 mutex_lock(&pca->lock); in pca9685_pwm_test_and_set_inuse()
222 if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) { in pca9685_pwm_test_and_set_inuse()
231 if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) { in pca9685_pwm_test_and_set_inuse()
236 is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse); in pca9685_pwm_test_and_set_inuse()
238 mutex_unlock(&pca->lock); in pca9685_pwm_test_and_set_inuse()
244 mutex_lock(&pca->lock); in pca9685_pwm_clear_inuse()
245 clear_bit(pwm_idx, pca->pwms_inuse); in pca9685_pwm_clear_inuse()
246 mutex_unlock(&pca->lock); in pca9685_pwm_clear_inuse()
249 static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset) in pca9685_pwm_gpio_request() argument
254 if (pca9685_pwm_test_and_set_inuse(pca, offset)) in pca9685_pwm_gpio_request()
255 return -EBUSY; in pca9685_pwm_gpio_request()
260 static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset) in pca9685_pwm_gpio_get() argument
264 return pca9685_pwm_get_duty(chip, offset) != 0; in pca9685_pwm_gpio_get()
267 static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset, in pca9685_pwm_gpio_set() argument
272 pca9685_pwm_set_duty(chip, offset, value ? PCA9685_COUNTER_RANGE : 0); in pca9685_pwm_gpio_set()
275 static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset) in pca9685_pwm_gpio_free() argument
280 pca9685_pwm_set_duty(chip, offset, 0); in pca9685_pwm_gpio_free()
282 pca9685_pwm_clear_inuse(pca, offset); in pca9685_pwm_gpio_free()
286 unsigned int offset) in pca9685_pwm_gpio_get_direction() argument
293 unsigned int offset) in pca9685_pwm_gpio_direction_input() argument
295 return -EINVAL; in pca9685_pwm_gpio_direction_input()
299 unsigned int offset, int value) in pca9685_pwm_gpio_direction_output() argument
301 pca9685_pwm_gpio_set(gpio, offset, value); in pca9685_pwm_gpio_direction_output()
307 * The PCA9685 has a bit for turning the PWM output full off or on. Some
310 * PWM channel.
317 pca->gpio.label = dev_name(dev); in pca9685_pwm_gpio_probe()
318 pca->gpio.parent = dev; in pca9685_pwm_gpio_probe()
319 pca->gpio.request = pca9685_pwm_gpio_request; in pca9685_pwm_gpio_probe()
320 pca->gpio.free = pca9685_pwm_gpio_free; in pca9685_pwm_gpio_probe()
321 pca->gpio.get_direction = pca9685_pwm_gpio_get_direction; in pca9685_pwm_gpio_probe()
322 pca->gpio.direction_input = pca9685_pwm_gpio_direction_input; in pca9685_pwm_gpio_probe()
323 pca->gpio.direction_output = pca9685_pwm_gpio_direction_output; in pca9685_pwm_gpio_probe()
324 pca->gpio.get = pca9685_pwm_gpio_get; in pca9685_pwm_gpio_probe()
325 pca->gpio.set = pca9685_pwm_gpio_set; in pca9685_pwm_gpio_probe()
326 pca->gpio.base = -1; in pca9685_pwm_gpio_probe()
327 pca->gpio.ngpio = PCA9685_MAXCHAN; in pca9685_pwm_gpio_probe()
328 pca->gpio.can_sleep = true; in pca9685_pwm_gpio_probe()
330 return devm_gpiochip_add_data(dev, &pca->gpio, chip); in pca9685_pwm_gpio_probe()
354 int err = regmap_update_bits(pca->regmap, PCA9685_MODE1, in pca9685_set_sleep_mode()
368 static int __pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, in __pca9685_pwm_apply() argument
375 if (state->polarity != PWM_POLARITY_NORMAL) in __pca9685_pwm_apply()
376 return -EINVAL; in __pca9685_pwm_apply()
378 prescale = DIV_ROUND_CLOSEST_ULL(PCA9685_OSC_CLOCK_MHZ * state->period, in __pca9685_pwm_apply()
379 PCA9685_COUNTER_RANGE * 1000) - 1; in __pca9685_pwm_apply()
381 dev_err(pwmchip_parent(chip), "pwm not changed: period out of bounds!\n"); in __pca9685_pwm_apply()
382 return -EINVAL; in __pca9685_pwm_apply()
385 if (!state->enabled) { in __pca9685_pwm_apply()
386 pca9685_pwm_set_duty(chip, pwm->hwpwm, 0); in __pca9685_pwm_apply()
392 if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) { in __pca9685_pwm_apply()
394 "pwm not changed: periods of enabled pwms must match!\n"); in __pca9685_pwm_apply()
395 return -EBUSY; in __pca9685_pwm_apply()
407 /* Change the chip-wide output frequency */ in __pca9685_pwm_apply()
414 duty = PCA9685_COUNTER_RANGE * state->duty_cycle; in __pca9685_pwm_apply()
415 duty = DIV_ROUND_UP_ULL(duty, state->period); in __pca9685_pwm_apply()
416 pca9685_pwm_set_duty(chip, pwm->hwpwm, duty); in __pca9685_pwm_apply()
420 static int pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, in pca9685_pwm_apply() argument
426 mutex_lock(&pca->lock); in pca9685_pwm_apply()
427 ret = __pca9685_pwm_apply(chip, pwm, state); in pca9685_pwm_apply()
429 if (state->enabled) in pca9685_pwm_apply()
430 set_bit(pwm->hwpwm, pca->pwms_enabled); in pca9685_pwm_apply()
432 clear_bit(pwm->hwpwm, pca->pwms_enabled); in pca9685_pwm_apply()
434 mutex_unlock(&pca->lock); in pca9685_pwm_apply()
439 static int pca9685_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, in pca9685_pwm_get_state() argument
445 /* Calculate (chip-wide) period from prescale value */ in pca9685_pwm_get_state()
452 state->period = (PCA9685_COUNTER_RANGE * 1000 / PCA9685_OSC_CLOCK_MHZ) * in pca9685_pwm_get_state()
455 /* The (per-channel) polarity is fixed */ in pca9685_pwm_get_state()
456 state->polarity = PWM_POLARITY_NORMAL; in pca9685_pwm_get_state()
458 if (pwm->hwpwm >= PCA9685_MAXCHAN) { in pca9685_pwm_get_state()
463 state->duty_cycle = 0; in pca9685_pwm_get_state()
464 state->enabled = false; in pca9685_pwm_get_state()
468 state->enabled = true; in pca9685_pwm_get_state()
469 duty = pca9685_pwm_get_duty(chip, pwm->hwpwm); in pca9685_pwm_get_state()
470 state->duty_cycle = DIV_ROUND_DOWN_ULL(duty * state->period, PCA9685_COUNTER_RANGE); in pca9685_pwm_get_state()
475 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) in pca9685_pwm_request() argument
479 if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm)) in pca9685_pwm_request()
480 return -EBUSY; in pca9685_pwm_request()
482 if (pwm->hwpwm < PCA9685_MAXCHAN) { in pca9685_pwm_request()
483 /* PWMs - except the "all LEDs" channel - default to enabled */ in pca9685_pwm_request()
484 mutex_lock(&pca->lock); in pca9685_pwm_request()
485 set_bit(pwm->hwpwm, pca->pwms_enabled); in pca9685_pwm_request()
486 mutex_unlock(&pca->lock); in pca9685_pwm_request()
494 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) in pca9685_pwm_free() argument
498 mutex_lock(&pca->lock); in pca9685_pwm_free()
499 pca9685_pwm_set_duty(chip, pwm->hwpwm, 0); in pca9685_pwm_free()
500 clear_bit(pwm->hwpwm, pca->pwms_enabled); in pca9685_pwm_free()
501 mutex_unlock(&pca->lock); in pca9685_pwm_free()
504 pca9685_pwm_clear_inuse(pca, pwm->hwpwm); in pca9685_pwm_free()
529 chip = devm_pwmchip_alloc(&client->dev, PCA9685_MAXCHAN + 1, sizeof(*pca)); in pca9685_pwm_probe()
534 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config); in pca9685_pwm_probe()
535 if (IS_ERR(pca->regmap)) { in pca9685_pwm_probe()
536 ret = PTR_ERR(pca->regmap); in pca9685_pwm_probe()
537 dev_err(&client->dev, "Failed to initialize register map: %d\n", in pca9685_pwm_probe()
544 mutex_init(&pca->lock); in pca9685_pwm_probe()
550 if (device_property_read_bool(&client->dev, "invert")) in pca9685_pwm_probe()
555 if (device_property_read_bool(&client->dev, "open-drain")) in pca9685_pwm_probe()
575 chip->ops = &pca9685_pwm_ops; in pca9685_pwm_probe()
587 pm_runtime_enable(&client->dev); in pca9685_pwm_probe()
589 if (pm_runtime_enabled(&client->dev)) { in pca9685_pwm_probe()
591 * Although the chip comes out of power-up in the sleep state, in pca9685_pwm_probe()
595 pm_runtime_set_suspended(&client->dev); in pca9685_pwm_probe()
610 if (!pm_runtime_enabled(&client->dev)) { in pca9685_pwm_remove()
615 pm_runtime_disable(&client->dev); in pca9685_pwm_remove()
652 { .compatible = "nxp,pca9685-pwm", },
665 .name = "pca9685-pwm",
678 MODULE_DESCRIPTION("PWM driver for PCA9685");