Lines Matching +full:off +full:- +full:chip
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
26 * Because the PCA9685 has only one prescaler per chip, only the first channel
88 static inline struct pca9685 *to_pca(struct pwm_chip *chip) in to_pca() argument
90 return pwmchip_get_drvdata(chip); in to_pca()
97 if (bitmap_empty(pca->pwms_enabled, PCA9685_MAXCHAN + 1)) in pca9685_prescaler_can_change()
100 if (bitmap_weight(pca->pwms_enabled, PCA9685_MAXCHAN + 1) > 1) in pca9685_prescaler_can_change()
106 return test_bit(channel, pca->pwms_enabled); in pca9685_prescaler_can_change()
109 static int pca9685_read_reg(struct pwm_chip *chip, unsigned int reg, unsigned int *val) in pca9685_read_reg() argument
111 struct pca9685 *pca = to_pca(chip); in pca9685_read_reg()
112 struct device *dev = pwmchip_parent(chip); in pca9685_read_reg()
115 err = regmap_read(pca->regmap, reg, val); in pca9685_read_reg()
122 static int pca9685_write_reg(struct pwm_chip *chip, unsigned int reg, unsigned int val) in pca9685_write_reg() argument
124 struct pca9685 *pca = to_pca(chip); in pca9685_write_reg()
125 struct device *dev = pwmchip_parent(chip); in pca9685_write_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%) */
136 static void pca9685_pwm_set_duty(struct pwm_chip *chip, int channel, unsigned int duty) in pca9685_pwm_set_duty() argument
138 struct pwm_device *pwm = &chip->pwms[channel]; in pca9685_pwm_set_duty()
139 unsigned int on, off; in pca9685_pwm_set_duty() local
142 /* Set the full OFF bit, which has the highest precedence */ in pca9685_pwm_set_duty()
143 pca9685_write_reg(chip, REG_OFF_H(channel), LED_FULL); in pca9685_pwm_set_duty()
146 /* Set the full ON bit and clear the full OFF bit */ in pca9685_pwm_set_duty()
147 pca9685_write_reg(chip, REG_ON_H(channel), LED_FULL); in pca9685_pwm_set_duty()
148 pca9685_write_reg(chip, REG_OFF_H(channel), 0); in pca9685_pwm_set_duty()
153 if (pwm->state.usage_power && channel < PCA9685_MAXCHAN) { in pca9685_pwm_set_duty()
165 off = (on + duty) % PCA9685_COUNTER_RANGE; in pca9685_pwm_set_duty()
168 pca9685_write_reg(chip, REG_ON_L(channel), on & 0xff); in pca9685_pwm_set_duty()
169 pca9685_write_reg(chip, REG_ON_H(channel), (on >> 8) & 0xf); in pca9685_pwm_set_duty()
170 /* Set OFF time (clears full OFF bit) */ in pca9685_pwm_set_duty()
171 pca9685_write_reg(chip, REG_OFF_L(channel), off & 0xff); in pca9685_pwm_set_duty()
172 pca9685_write_reg(chip, REG_OFF_H(channel), (off >> 8) & 0xf); in pca9685_pwm_set_duty()
175 static unsigned int pca9685_pwm_get_duty(struct pwm_chip *chip, int channel) in pca9685_pwm_get_duty() argument
177 struct pwm_device *pwm = &chip->pwms[channel]; in pca9685_pwm_get_duty()
178 unsigned int off = 0, on = 0, val = 0; in pca9685_pwm_get_duty() local
185 pca9685_read_reg(chip, LED_N_OFF_H(channel), &off); in pca9685_pwm_get_duty()
186 if (off & LED_FULL) { in pca9685_pwm_get_duty()
187 /* Full OFF bit is set */ in pca9685_pwm_get_duty()
191 pca9685_read_reg(chip, LED_N_ON_H(channel), &on); in pca9685_pwm_get_duty()
197 pca9685_read_reg(chip, LED_N_OFF_L(channel), &val); in pca9685_pwm_get_duty()
198 off = ((off & 0xf) << 8) | (val & 0xff); in pca9685_pwm_get_duty()
199 if (!pwm->state.usage_power) in pca9685_pwm_get_duty()
200 return off; in pca9685_pwm_get_duty()
203 if (pca9685_read_reg(chip, LED_N_ON_L(channel), &val)) { 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()
251 struct pwm_chip *chip = gpiochip_get_data(gpio); in pca9685_pwm_gpio_request() local
252 struct pca9685 *pca = to_pca(chip); in pca9685_pwm_gpio_request()
255 return -EBUSY; in pca9685_pwm_gpio_request()
256 pm_runtime_get_sync(pwmchip_parent(chip)); in pca9685_pwm_gpio_request()
262 struct pwm_chip *chip = gpiochip_get_data(gpio); in pca9685_pwm_gpio_get() local
264 return pca9685_pwm_get_duty(chip, offset) != 0; in pca9685_pwm_gpio_get()
270 struct pwm_chip *chip = gpiochip_get_data(gpio); in pca9685_pwm_gpio_set() local
272 pca9685_pwm_set_duty(chip, offset, value ? PCA9685_COUNTER_RANGE : 0); in pca9685_pwm_gpio_set()
277 struct pwm_chip *chip = gpiochip_get_data(gpio); in pca9685_pwm_gpio_free() local
278 struct pca9685 *pca = to_pca(chip); in pca9685_pwm_gpio_free()
280 pca9685_pwm_set_duty(chip, offset, 0); in pca9685_pwm_gpio_free()
281 pm_runtime_put(pwmchip_parent(chip)); in pca9685_pwm_gpio_free()
285 static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip, in pca9685_pwm_gpio_get_direction() argument
295 return -EINVAL; in pca9685_pwm_gpio_direction_input()
307 * The PCA9685 has a bit for turning the PWM output full off or on. Some
309 * expose a GPIO chip here which can exclusively take over the underlying
312 static int pca9685_pwm_gpio_probe(struct pwm_chip *chip) in pca9685_pwm_gpio_probe() argument
314 struct pca9685 *pca = to_pca(chip); in pca9685_pwm_gpio_probe()
315 struct device *dev = pwmchip_parent(chip); in pca9685_pwm_gpio_probe()
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()
344 static inline int pca9685_pwm_gpio_probe(struct pwm_chip *chip) in pca9685_pwm_gpio_probe() argument
350 static void pca9685_set_sleep_mode(struct pwm_chip *chip, bool enable) in pca9685_set_sleep_mode() argument
352 struct device *dev = pwmchip_parent(chip); in pca9685_set_sleep_mode()
353 struct pca9685 *pca = to_pca(chip); in pca9685_set_sleep_mode()
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
371 struct pca9685 *pca = to_pca(chip); in __pca9685_pwm_apply()
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()
390 pca9685_read_reg(chip, PCA9685_PRESCALE, &val); in __pca9685_pwm_apply()
392 if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) { in __pca9685_pwm_apply()
393 dev_err(pwmchip_parent(chip), in __pca9685_pwm_apply()
395 return -EBUSY; in __pca9685_pwm_apply()
399 * Putting the chip briefly into SLEEP mode in __pca9685_pwm_apply()
404 /* Put chip into sleep mode */ in __pca9685_pwm_apply()
405 pca9685_set_sleep_mode(chip, true); in __pca9685_pwm_apply()
407 /* Change the chip-wide output frequency */ in __pca9685_pwm_apply()
408 pca9685_write_reg(chip, PCA9685_PRESCALE, prescale); in __pca9685_pwm_apply()
410 /* Wake the chip up */ in __pca9685_pwm_apply()
411 pca9685_set_sleep_mode(chip, false); 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
423 struct pca9685 *pca = to_pca(chip); in pca9685_pwm_apply()
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()
446 pca9685_read_reg(chip, PCA9685_PRESCALE, &val); 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
477 struct pca9685 *pca = to_pca(chip); in pca9685_pwm_request()
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()
489 pm_runtime_get_sync(pwmchip_parent(chip)); in pca9685_pwm_request()
494 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) in pca9685_pwm_free() argument
496 struct pca9685 *pca = to_pca(chip); in pca9685_pwm_free()
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()
503 pm_runtime_put(pwmchip_parent(chip)); in pca9685_pwm_free()
504 pca9685_pwm_clear_inuse(pca, pwm->hwpwm); in pca9685_pwm_free()
523 struct pwm_chip *chip; in pca9685_pwm_probe() local
529 chip = devm_pwmchip_alloc(&client->dev, PCA9685_MAXCHAN + 1, sizeof(*pca)); in pca9685_pwm_probe()
530 if (IS_ERR(chip)) in pca9685_pwm_probe()
531 return PTR_ERR(chip); in pca9685_pwm_probe()
532 pca = to_pca(chip); 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()
542 i2c_set_clientdata(client, chip); in pca9685_pwm_probe()
544 mutex_init(&pca->lock); in pca9685_pwm_probe()
546 ret = pca9685_read_reg(chip, PCA9685_MODE2, ®); 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()
560 ret = pca9685_write_reg(chip, PCA9685_MODE2, reg); in pca9685_pwm_probe()
565 pca9685_read_reg(chip, PCA9685_MODE1, ®); in pca9685_pwm_probe()
567 pca9685_write_reg(chip, PCA9685_MODE1, reg); in pca9685_pwm_probe()
569 /* Reset OFF/ON registers to POR default */ in pca9685_pwm_probe()
570 pca9685_write_reg(chip, PCA9685_ALL_LED_OFF_L, 0); in pca9685_pwm_probe()
571 pca9685_write_reg(chip, PCA9685_ALL_LED_OFF_H, LED_FULL); in pca9685_pwm_probe()
572 pca9685_write_reg(chip, PCA9685_ALL_LED_ON_L, 0); in pca9685_pwm_probe()
573 pca9685_write_reg(chip, PCA9685_ALL_LED_ON_H, LED_FULL); in pca9685_pwm_probe()
575 chip->ops = &pca9685_pwm_ops; in pca9685_pwm_probe()
577 ret = pwmchip_add(chip); in pca9685_pwm_probe()
581 ret = pca9685_pwm_gpio_probe(chip); in pca9685_pwm_probe()
583 pwmchip_remove(chip); 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()
594 pca9685_set_sleep_mode(chip, true); in pca9685_pwm_probe()
595 pm_runtime_set_suspended(&client->dev); in pca9685_pwm_probe()
597 /* Wake the chip up if runtime PM is disabled */ in pca9685_pwm_probe()
598 pca9685_set_sleep_mode(chip, false); in pca9685_pwm_probe()
606 struct pwm_chip *chip = i2c_get_clientdata(client); in pca9685_pwm_remove() local
608 pwmchip_remove(chip); in pca9685_pwm_remove()
610 if (!pm_runtime_enabled(&client->dev)) { in pca9685_pwm_remove()
611 /* Put chip in sleep state if runtime PM is disabled */ in pca9685_pwm_remove()
612 pca9685_set_sleep_mode(chip, true); in pca9685_pwm_remove()
615 pm_runtime_disable(&client->dev); in pca9685_pwm_remove()
621 struct pwm_chip *chip = i2c_get_clientdata(client); in pca9685_pwm_runtime_suspend() local
623 pca9685_set_sleep_mode(chip, true); in pca9685_pwm_runtime_suspend()
630 struct pwm_chip *chip = i2c_get_clientdata(client); in pca9685_pwm_runtime_resume() local
632 pca9685_set_sleep_mode(chip, false); in pca9685_pwm_runtime_resume()
652 { .compatible = "nxp,pca9685-pwm", },
665 .name = "pca9685-pwm",