Lines Matching refs:pca

95 static bool pca9685_prescaler_can_change(struct pca9685 *pca, int channel)  in pca9685_prescaler_can_change()  argument
98 if (bitmap_empty(pca->pwms_enabled, PCA9685_MAXCHAN + 1)) in pca9685_prescaler_can_change()
101 if (bitmap_weight(pca->pwms_enabled, PCA9685_MAXCHAN + 1) > 1) in pca9685_prescaler_can_change()
107 return test_bit(channel, pca->pwms_enabled); in pca9685_prescaler_can_change()
110 static int pca9685_read_reg(struct pca9685 *pca, unsigned int reg, unsigned int *val) in pca9685_read_reg() argument
112 struct device *dev = pca->chip.dev; in pca9685_read_reg()
115 err = regmap_read(pca->regmap, reg, val); in pca9685_read_reg()
122 static int pca9685_write_reg(struct pca9685 *pca, unsigned int reg, unsigned int val) in pca9685_write_reg() argument
124 struct device *dev = pca->chip.dev; in pca9685_write_reg()
127 err = regmap_write(pca->regmap, reg, val); in pca9685_write_reg()
135 static void pca9685_pwm_set_duty(struct pca9685 *pca, int channel, unsigned int duty) in pca9685_pwm_set_duty() argument
137 struct pwm_device *pwm = &pca->chip.pwms[channel]; in pca9685_pwm_set_duty()
142 pca9685_write_reg(pca, REG_OFF_H(channel), LED_FULL); in pca9685_pwm_set_duty()
146 pca9685_write_reg(pca, REG_ON_H(channel), LED_FULL); in pca9685_pwm_set_duty()
147 pca9685_write_reg(pca, REG_OFF_H(channel), 0); in pca9685_pwm_set_duty()
167 pca9685_write_reg(pca, REG_ON_L(channel), on & 0xff); in pca9685_pwm_set_duty()
168 pca9685_write_reg(pca, REG_ON_H(channel), (on >> 8) & 0xf); in pca9685_pwm_set_duty()
170 pca9685_write_reg(pca, REG_OFF_L(channel), off & 0xff); in pca9685_pwm_set_duty()
171 pca9685_write_reg(pca, REG_OFF_H(channel), (off >> 8) & 0xf); in pca9685_pwm_set_duty()
174 static unsigned int pca9685_pwm_get_duty(struct pca9685 *pca, int channel) in pca9685_pwm_get_duty() argument
176 struct pwm_device *pwm = &pca->chip.pwms[channel]; in pca9685_pwm_get_duty()
184 pca9685_read_reg(pca, LED_N_OFF_H(channel), &off); in pca9685_pwm_get_duty()
190 pca9685_read_reg(pca, LED_N_ON_H(channel), &on); in pca9685_pwm_get_duty()
196 pca9685_read_reg(pca, LED_N_OFF_L(channel), &val); in pca9685_pwm_get_duty()
202 if (pca9685_read_reg(pca, LED_N_ON_L(channel), &val)) { in pca9685_pwm_get_duty()
211 static bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, int pwm_idx) in pca9685_pwm_test_and_set_inuse() argument
215 mutex_lock(&pca->lock); in pca9685_pwm_test_and_set_inuse()
221 if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) { 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()
241 static void pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx) in pca9685_pwm_clear_inuse() argument
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()
250 struct pca9685 *pca = gpiochip_get_data(gpio); in pca9685_pwm_gpio_request() local
252 if (pca9685_pwm_test_and_set_inuse(pca, offset)) in pca9685_pwm_gpio_request()
254 pm_runtime_get_sync(pca->chip.dev); in pca9685_pwm_gpio_request()
260 struct pca9685 *pca = gpiochip_get_data(gpio); in pca9685_pwm_gpio_get() local
262 return pca9685_pwm_get_duty(pca, offset) != 0; in pca9685_pwm_gpio_get()
268 struct pca9685 *pca = gpiochip_get_data(gpio); in pca9685_pwm_gpio_set() local
270 pca9685_pwm_set_duty(pca, offset, value ? PCA9685_COUNTER_RANGE : 0); in pca9685_pwm_gpio_set()
275 struct pca9685 *pca = gpiochip_get_data(gpio); in pca9685_pwm_gpio_free() local
277 pca9685_pwm_set_duty(pca, offset, 0); in pca9685_pwm_gpio_free()
278 pm_runtime_put(pca->chip.dev); in pca9685_pwm_gpio_free()
279 pca9685_pwm_clear_inuse(pca, offset); in pca9685_pwm_gpio_free()
309 static int pca9685_pwm_gpio_probe(struct pca9685 *pca) in pca9685_pwm_gpio_probe() argument
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()
329 static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, in pca9685_pwm_test_and_set_inuse() argument
336 pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx) in pca9685_pwm_clear_inuse() argument
340 static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca) in pca9685_pwm_gpio_probe() argument
346 static void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable) in pca9685_set_sleep_mode() argument
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()
366 struct pca9685 *pca = to_pca(chip); in __pca9685_pwm_apply() local
381 pca9685_pwm_set_duty(pca, pwm->hwpwm, 0); in __pca9685_pwm_apply()
385 pca9685_read_reg(pca, PCA9685_PRESCALE, &val); in __pca9685_pwm_apply()
387 if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) { in __pca9685_pwm_apply()
400 pca9685_set_sleep_mode(pca, true); in __pca9685_pwm_apply()
403 pca9685_write_reg(pca, PCA9685_PRESCALE, prescale); in __pca9685_pwm_apply()
406 pca9685_set_sleep_mode(pca, false); in __pca9685_pwm_apply()
411 pca9685_pwm_set_duty(pca, pwm->hwpwm, duty); in __pca9685_pwm_apply()
418 struct pca9685 *pca = to_pca(chip); in pca9685_pwm_apply() local
421 mutex_lock(&pca->lock); 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()
437 struct pca9685 *pca = to_pca(chip); in pca9685_pwm_get_state() local
442 pca9685_read_reg(pca, PCA9685_PRESCALE, &val); in pca9685_pwm_get_state()
465 duty = pca9685_pwm_get_duty(pca, pwm->hwpwm); in pca9685_pwm_get_state()
473 struct pca9685 *pca = to_pca(chip); in pca9685_pwm_request() local
475 if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm)) in pca9685_pwm_request()
480 mutex_lock(&pca->lock); in pca9685_pwm_request()
481 set_bit(pwm->hwpwm, pca->pwms_enabled); in pca9685_pwm_request()
482 mutex_unlock(&pca->lock); in pca9685_pwm_request()
492 struct pca9685 *pca = to_pca(chip); in pca9685_pwm_free() local
494 mutex_lock(&pca->lock); in pca9685_pwm_free()
495 pca9685_pwm_set_duty(pca, pwm->hwpwm, 0); in pca9685_pwm_free()
496 clear_bit(pwm->hwpwm, pca->pwms_enabled); in pca9685_pwm_free()
497 mutex_unlock(&pca->lock); in pca9685_pwm_free()
500 pca9685_pwm_clear_inuse(pca, pwm->hwpwm); in pca9685_pwm_free()
520 struct pca9685 *pca; in pca9685_pwm_probe() local
524 pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL); in pca9685_pwm_probe()
525 if (!pca) in pca9685_pwm_probe()
528 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config); in pca9685_pwm_probe()
529 if (IS_ERR(pca->regmap)) { in pca9685_pwm_probe()
530 ret = PTR_ERR(pca->regmap); in pca9685_pwm_probe()
536 i2c_set_clientdata(client, pca); in pca9685_pwm_probe()
538 mutex_init(&pca->lock); in pca9685_pwm_probe()
540 ret = pca9685_read_reg(pca, PCA9685_MODE2, &reg); in pca9685_pwm_probe()
554 ret = pca9685_write_reg(pca, PCA9685_MODE2, reg); in pca9685_pwm_probe()
559 pca9685_read_reg(pca, PCA9685_MODE1, &reg); in pca9685_pwm_probe()
561 pca9685_write_reg(pca, PCA9685_MODE1, reg); in pca9685_pwm_probe()
564 pca9685_write_reg(pca, PCA9685_ALL_LED_OFF_L, 0); in pca9685_pwm_probe()
565 pca9685_write_reg(pca, PCA9685_ALL_LED_OFF_H, LED_FULL); in pca9685_pwm_probe()
566 pca9685_write_reg(pca, PCA9685_ALL_LED_ON_L, 0); in pca9685_pwm_probe()
567 pca9685_write_reg(pca, PCA9685_ALL_LED_ON_H, LED_FULL); in pca9685_pwm_probe()
569 pca->chip.ops = &pca9685_pwm_ops; in pca9685_pwm_probe()
571 pca->chip.npwm = PCA9685_MAXCHAN + 1; in pca9685_pwm_probe()
573 pca->chip.dev = &client->dev; in pca9685_pwm_probe()
575 ret = pwmchip_add(&pca->chip); in pca9685_pwm_probe()
579 ret = pca9685_pwm_gpio_probe(pca); in pca9685_pwm_probe()
581 pwmchip_remove(&pca->chip); in pca9685_pwm_probe()
592 pca9685_set_sleep_mode(pca, true); in pca9685_pwm_probe()
596 pca9685_set_sleep_mode(pca, false); in pca9685_pwm_probe()
604 struct pca9685 *pca = i2c_get_clientdata(client); in pca9685_pwm_remove() local
606 pwmchip_remove(&pca->chip); in pca9685_pwm_remove()
610 pca9685_set_sleep_mode(pca, true); in pca9685_pwm_remove()
619 struct pca9685 *pca = i2c_get_clientdata(client); in pca9685_pwm_runtime_suspend() local
621 pca9685_set_sleep_mode(pca, true); in pca9685_pwm_runtime_suspend()
628 struct pca9685 *pca = i2c_get_clientdata(client); in pca9685_pwm_runtime_resume() local
630 pca9685_set_sleep_mode(pca, false); in pca9685_pwm_runtime_resume()