18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Driver for PCA9685 16-channel 12-bit PWM LED controller 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de> 68c2ecf20Sopenharmony_ci * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * based on the pwm-twl-led.c driver 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/acpi.h> 128c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h> 138c2ecf20Sopenharmony_ci#include <linux/i2c.h> 148c2ecf20Sopenharmony_ci#include <linux/module.h> 158c2ecf20Sopenharmony_ci#include <linux/mutex.h> 168c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 178c2ecf20Sopenharmony_ci#include <linux/property.h> 188c2ecf20Sopenharmony_ci#include <linux/pwm.h> 198c2ecf20Sopenharmony_ci#include <linux/regmap.h> 208c2ecf20Sopenharmony_ci#include <linux/slab.h> 218c2ecf20Sopenharmony_ci#include <linux/delay.h> 228c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 238c2ecf20Sopenharmony_ci#include <linux/bitmap.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* 268c2ecf20Sopenharmony_ci * Because the PCA9685 has only one prescaler per chip, changing the period of 278c2ecf20Sopenharmony_ci * one channel affects the period of all 16 PWM outputs! 288c2ecf20Sopenharmony_ci * However, the ratio between each configured duty cycle and the chip-wide 298c2ecf20Sopenharmony_ci * period remains constant, because the OFF time is set in proportion to the 308c2ecf20Sopenharmony_ci * counter range. 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define PCA9685_MODE1 0x00 348c2ecf20Sopenharmony_ci#define PCA9685_MODE2 0x01 358c2ecf20Sopenharmony_ci#define PCA9685_SUBADDR1 0x02 368c2ecf20Sopenharmony_ci#define PCA9685_SUBADDR2 0x03 378c2ecf20Sopenharmony_ci#define PCA9685_SUBADDR3 0x04 388c2ecf20Sopenharmony_ci#define PCA9685_ALLCALLADDR 0x05 398c2ecf20Sopenharmony_ci#define PCA9685_LEDX_ON_L 0x06 408c2ecf20Sopenharmony_ci#define PCA9685_LEDX_ON_H 0x07 418c2ecf20Sopenharmony_ci#define PCA9685_LEDX_OFF_L 0x08 428c2ecf20Sopenharmony_ci#define PCA9685_LEDX_OFF_H 0x09 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci#define PCA9685_ALL_LED_ON_L 0xFA 458c2ecf20Sopenharmony_ci#define PCA9685_ALL_LED_ON_H 0xFB 468c2ecf20Sopenharmony_ci#define PCA9685_ALL_LED_OFF_L 0xFC 478c2ecf20Sopenharmony_ci#define PCA9685_ALL_LED_OFF_H 0xFD 488c2ecf20Sopenharmony_ci#define PCA9685_PRESCALE 0xFE 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci#define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */ 518c2ecf20Sopenharmony_ci#define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */ 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci#define PCA9685_COUNTER_RANGE 4096 548c2ecf20Sopenharmony_ci#define PCA9685_DEFAULT_PERIOD 5000000 /* Default period_ns = 1/200 Hz */ 558c2ecf20Sopenharmony_ci#define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */ 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci#define PCA9685_NUMREGS 0xFF 588c2ecf20Sopenharmony_ci#define PCA9685_MAXCHAN 0x10 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci#define LED_FULL BIT(4) 618c2ecf20Sopenharmony_ci#define MODE1_ALLCALL BIT(0) 628c2ecf20Sopenharmony_ci#define MODE1_SUB3 BIT(1) 638c2ecf20Sopenharmony_ci#define MODE1_SUB2 BIT(2) 648c2ecf20Sopenharmony_ci#define MODE1_SUB1 BIT(3) 658c2ecf20Sopenharmony_ci#define MODE1_SLEEP BIT(4) 668c2ecf20Sopenharmony_ci#define MODE2_INVRT BIT(4) 678c2ecf20Sopenharmony_ci#define MODE2_OUTDRV BIT(2) 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci#define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N))) 708c2ecf20Sopenharmony_ci#define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N))) 718c2ecf20Sopenharmony_ci#define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N))) 728c2ecf20Sopenharmony_ci#define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N))) 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistruct pca9685 { 758c2ecf20Sopenharmony_ci struct pwm_chip chip; 768c2ecf20Sopenharmony_ci struct regmap *regmap; 778c2ecf20Sopenharmony_ci int period_ns; 788c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_GPIOLIB) 798c2ecf20Sopenharmony_ci struct mutex lock; 808c2ecf20Sopenharmony_ci struct gpio_chip gpio; 818c2ecf20Sopenharmony_ci DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1); 828c2ecf20Sopenharmony_ci#endif 838c2ecf20Sopenharmony_ci}; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_cistatic inline struct pca9685 *to_pca(struct pwm_chip *chip) 868c2ecf20Sopenharmony_ci{ 878c2ecf20Sopenharmony_ci return container_of(chip, struct pca9685, chip); 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_GPIOLIB) 918c2ecf20Sopenharmony_cistatic bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, int pwm_idx) 928c2ecf20Sopenharmony_ci{ 938c2ecf20Sopenharmony_ci bool is_inuse; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci mutex_lock(&pca->lock); 968c2ecf20Sopenharmony_ci if (pwm_idx >= PCA9685_MAXCHAN) { 978c2ecf20Sopenharmony_ci /* 988c2ecf20Sopenharmony_ci * "All LEDs" channel: 998c2ecf20Sopenharmony_ci * pretend already in use if any of the PWMs are requested 1008c2ecf20Sopenharmony_ci */ 1018c2ecf20Sopenharmony_ci if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) { 1028c2ecf20Sopenharmony_ci is_inuse = true; 1038c2ecf20Sopenharmony_ci goto out; 1048c2ecf20Sopenharmony_ci } 1058c2ecf20Sopenharmony_ci } else { 1068c2ecf20Sopenharmony_ci /* 1078c2ecf20Sopenharmony_ci * Regular channel: 1088c2ecf20Sopenharmony_ci * pretend already in use if the "all LEDs" channel is requested 1098c2ecf20Sopenharmony_ci */ 1108c2ecf20Sopenharmony_ci if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) { 1118c2ecf20Sopenharmony_ci is_inuse = true; 1128c2ecf20Sopenharmony_ci goto out; 1138c2ecf20Sopenharmony_ci } 1148c2ecf20Sopenharmony_ci } 1158c2ecf20Sopenharmony_ci is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse); 1168c2ecf20Sopenharmony_ciout: 1178c2ecf20Sopenharmony_ci mutex_unlock(&pca->lock); 1188c2ecf20Sopenharmony_ci return is_inuse; 1198c2ecf20Sopenharmony_ci} 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_cistatic void pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx) 1228c2ecf20Sopenharmony_ci{ 1238c2ecf20Sopenharmony_ci mutex_lock(&pca->lock); 1248c2ecf20Sopenharmony_ci clear_bit(pwm_idx, pca->pwms_inuse); 1258c2ecf20Sopenharmony_ci mutex_unlock(&pca->lock); 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci struct pca9685 *pca = gpiochip_get_data(gpio); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci if (pca9685_pwm_test_and_set_inuse(pca, offset)) 1338c2ecf20Sopenharmony_ci return -EBUSY; 1348c2ecf20Sopenharmony_ci pm_runtime_get_sync(pca->chip.dev); 1358c2ecf20Sopenharmony_ci return 0; 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci struct pca9685 *pca = gpiochip_get_data(gpio); 1418c2ecf20Sopenharmony_ci struct pwm_device *pwm = &pca->chip.pwms[offset]; 1428c2ecf20Sopenharmony_ci unsigned int value; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci regmap_read(pca->regmap, LED_N_ON_H(pwm->hwpwm), &value); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci return value & LED_FULL; 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_cistatic void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset, 1508c2ecf20Sopenharmony_ci int value) 1518c2ecf20Sopenharmony_ci{ 1528c2ecf20Sopenharmony_ci struct pca9685 *pca = gpiochip_get_data(gpio); 1538c2ecf20Sopenharmony_ci struct pwm_device *pwm = &pca->chip.pwms[offset]; 1548c2ecf20Sopenharmony_ci unsigned int on = value ? LED_FULL : 0; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci /* Clear both OFF registers */ 1578c2ecf20Sopenharmony_ci regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm), 0); 1588c2ecf20Sopenharmony_ci regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm), 0); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci /* Set the full ON bit */ 1618c2ecf20Sopenharmony_ci regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), on); 1628c2ecf20Sopenharmony_ci} 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_cistatic void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci struct pca9685 *pca = gpiochip_get_data(gpio); 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci pca9685_pwm_gpio_set(gpio, offset, 0); 1698c2ecf20Sopenharmony_ci pm_runtime_put(pca->chip.dev); 1708c2ecf20Sopenharmony_ci pca9685_pwm_clear_inuse(pca, offset); 1718c2ecf20Sopenharmony_ci} 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_cistatic int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip, 1748c2ecf20Sopenharmony_ci unsigned int offset) 1758c2ecf20Sopenharmony_ci{ 1768c2ecf20Sopenharmony_ci /* Always out */ 1778c2ecf20Sopenharmony_ci return GPIO_LINE_DIRECTION_OUT; 1788c2ecf20Sopenharmony_ci} 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_cistatic int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio, 1818c2ecf20Sopenharmony_ci unsigned int offset) 1828c2ecf20Sopenharmony_ci{ 1838c2ecf20Sopenharmony_ci return -EINVAL; 1848c2ecf20Sopenharmony_ci} 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_cistatic int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio, 1878c2ecf20Sopenharmony_ci unsigned int offset, int value) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci pca9685_pwm_gpio_set(gpio, offset, value); 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci return 0; 1928c2ecf20Sopenharmony_ci} 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci/* 1958c2ecf20Sopenharmony_ci * The PCA9685 has a bit for turning the PWM output full off or on. Some 1968c2ecf20Sopenharmony_ci * boards like Intel Galileo actually uses these as normal GPIOs so we 1978c2ecf20Sopenharmony_ci * expose a GPIO chip here which can exclusively take over the underlying 1988c2ecf20Sopenharmony_ci * PWM channel. 1998c2ecf20Sopenharmony_ci */ 2008c2ecf20Sopenharmony_cistatic int pca9685_pwm_gpio_probe(struct pca9685 *pca) 2018c2ecf20Sopenharmony_ci{ 2028c2ecf20Sopenharmony_ci struct device *dev = pca->chip.dev; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci mutex_init(&pca->lock); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci pca->gpio.label = dev_name(dev); 2078c2ecf20Sopenharmony_ci pca->gpio.parent = dev; 2088c2ecf20Sopenharmony_ci pca->gpio.request = pca9685_pwm_gpio_request; 2098c2ecf20Sopenharmony_ci pca->gpio.free = pca9685_pwm_gpio_free; 2108c2ecf20Sopenharmony_ci pca->gpio.get_direction = pca9685_pwm_gpio_get_direction; 2118c2ecf20Sopenharmony_ci pca->gpio.direction_input = pca9685_pwm_gpio_direction_input; 2128c2ecf20Sopenharmony_ci pca->gpio.direction_output = pca9685_pwm_gpio_direction_output; 2138c2ecf20Sopenharmony_ci pca->gpio.get = pca9685_pwm_gpio_get; 2148c2ecf20Sopenharmony_ci pca->gpio.set = pca9685_pwm_gpio_set; 2158c2ecf20Sopenharmony_ci pca->gpio.base = -1; 2168c2ecf20Sopenharmony_ci pca->gpio.ngpio = PCA9685_MAXCHAN; 2178c2ecf20Sopenharmony_ci pca->gpio.can_sleep = true; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci return devm_gpiochip_add_data(dev, &pca->gpio, pca); 2208c2ecf20Sopenharmony_ci} 2218c2ecf20Sopenharmony_ci#else 2228c2ecf20Sopenharmony_cistatic inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, 2238c2ecf20Sopenharmony_ci int pwm_idx) 2248c2ecf20Sopenharmony_ci{ 2258c2ecf20Sopenharmony_ci return false; 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cistatic inline void 2298c2ecf20Sopenharmony_cipca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx) 2308c2ecf20Sopenharmony_ci{ 2318c2ecf20Sopenharmony_ci} 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_cistatic inline int pca9685_pwm_gpio_probe(struct pca9685 *pca) 2348c2ecf20Sopenharmony_ci{ 2358c2ecf20Sopenharmony_ci return 0; 2368c2ecf20Sopenharmony_ci} 2378c2ecf20Sopenharmony_ci#endif 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_cistatic void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable) 2408c2ecf20Sopenharmony_ci{ 2418c2ecf20Sopenharmony_ci regmap_update_bits(pca->regmap, PCA9685_MODE1, 2428c2ecf20Sopenharmony_ci MODE1_SLEEP, enable ? MODE1_SLEEP : 0); 2438c2ecf20Sopenharmony_ci if (!enable) { 2448c2ecf20Sopenharmony_ci /* Wait 500us for the oscillator to be back up */ 2458c2ecf20Sopenharmony_ci udelay(500); 2468c2ecf20Sopenharmony_ci } 2478c2ecf20Sopenharmony_ci} 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_cistatic int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 2508c2ecf20Sopenharmony_ci int duty_ns, int period_ns) 2518c2ecf20Sopenharmony_ci{ 2528c2ecf20Sopenharmony_ci struct pca9685 *pca = to_pca(chip); 2538c2ecf20Sopenharmony_ci unsigned long long duty; 2548c2ecf20Sopenharmony_ci unsigned int reg; 2558c2ecf20Sopenharmony_ci int prescale; 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci if (period_ns != pca->period_ns) { 2588c2ecf20Sopenharmony_ci prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns, 2598c2ecf20Sopenharmony_ci PCA9685_COUNTER_RANGE * 1000) - 1; 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci if (prescale >= PCA9685_PRESCALE_MIN && 2628c2ecf20Sopenharmony_ci prescale <= PCA9685_PRESCALE_MAX) { 2638c2ecf20Sopenharmony_ci /* 2648c2ecf20Sopenharmony_ci * Putting the chip briefly into SLEEP mode 2658c2ecf20Sopenharmony_ci * at this point won't interfere with the 2668c2ecf20Sopenharmony_ci * pm_runtime framework, because the pm_runtime 2678c2ecf20Sopenharmony_ci * state is guaranteed active here. 2688c2ecf20Sopenharmony_ci */ 2698c2ecf20Sopenharmony_ci /* Put chip into sleep mode */ 2708c2ecf20Sopenharmony_ci pca9685_set_sleep_mode(pca, true); 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci /* Change the chip-wide output frequency */ 2738c2ecf20Sopenharmony_ci regmap_write(pca->regmap, PCA9685_PRESCALE, prescale); 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci /* Wake the chip up */ 2768c2ecf20Sopenharmony_ci pca9685_set_sleep_mode(pca, false); 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci pca->period_ns = period_ns; 2798c2ecf20Sopenharmony_ci } else { 2808c2ecf20Sopenharmony_ci dev_err(chip->dev, 2818c2ecf20Sopenharmony_ci "prescaler not set: period out of bounds!\n"); 2828c2ecf20Sopenharmony_ci return -EINVAL; 2838c2ecf20Sopenharmony_ci } 2848c2ecf20Sopenharmony_ci } 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci if (duty_ns < 1) { 2878c2ecf20Sopenharmony_ci if (pwm->hwpwm >= PCA9685_MAXCHAN) 2888c2ecf20Sopenharmony_ci reg = PCA9685_ALL_LED_OFF_H; 2898c2ecf20Sopenharmony_ci else 2908c2ecf20Sopenharmony_ci reg = LED_N_OFF_H(pwm->hwpwm); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci regmap_write(pca->regmap, reg, LED_FULL); 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci return 0; 2958c2ecf20Sopenharmony_ci } 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci if (duty_ns == period_ns) { 2988c2ecf20Sopenharmony_ci /* Clear both OFF registers */ 2998c2ecf20Sopenharmony_ci if (pwm->hwpwm >= PCA9685_MAXCHAN) 3008c2ecf20Sopenharmony_ci reg = PCA9685_ALL_LED_OFF_L; 3018c2ecf20Sopenharmony_ci else 3028c2ecf20Sopenharmony_ci reg = LED_N_OFF_L(pwm->hwpwm); 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci regmap_write(pca->regmap, reg, 0x0); 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci if (pwm->hwpwm >= PCA9685_MAXCHAN) 3078c2ecf20Sopenharmony_ci reg = PCA9685_ALL_LED_OFF_H; 3088c2ecf20Sopenharmony_ci else 3098c2ecf20Sopenharmony_ci reg = LED_N_OFF_H(pwm->hwpwm); 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci regmap_write(pca->regmap, reg, 0x0); 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci /* Set the full ON bit */ 3148c2ecf20Sopenharmony_ci if (pwm->hwpwm >= PCA9685_MAXCHAN) 3158c2ecf20Sopenharmony_ci reg = PCA9685_ALL_LED_ON_H; 3168c2ecf20Sopenharmony_ci else 3178c2ecf20Sopenharmony_ci reg = LED_N_ON_H(pwm->hwpwm); 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci regmap_write(pca->regmap, reg, LED_FULL); 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci return 0; 3228c2ecf20Sopenharmony_ci } 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns; 3258c2ecf20Sopenharmony_ci duty = DIV_ROUND_UP_ULL(duty, period_ns); 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci if (pwm->hwpwm >= PCA9685_MAXCHAN) 3288c2ecf20Sopenharmony_ci reg = PCA9685_ALL_LED_OFF_L; 3298c2ecf20Sopenharmony_ci else 3308c2ecf20Sopenharmony_ci reg = LED_N_OFF_L(pwm->hwpwm); 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci regmap_write(pca->regmap, reg, (int)duty & 0xff); 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci if (pwm->hwpwm >= PCA9685_MAXCHAN) 3358c2ecf20Sopenharmony_ci reg = PCA9685_ALL_LED_OFF_H; 3368c2ecf20Sopenharmony_ci else 3378c2ecf20Sopenharmony_ci reg = LED_N_OFF_H(pwm->hwpwm); 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf); 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci /* Clear the full ON bit, otherwise the set OFF time has no effect */ 3428c2ecf20Sopenharmony_ci if (pwm->hwpwm >= PCA9685_MAXCHAN) 3438c2ecf20Sopenharmony_ci reg = PCA9685_ALL_LED_ON_H; 3448c2ecf20Sopenharmony_ci else 3458c2ecf20Sopenharmony_ci reg = LED_N_ON_H(pwm->hwpwm); 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci regmap_write(pca->regmap, reg, 0); 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci return 0; 3508c2ecf20Sopenharmony_ci} 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_cistatic int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) 3538c2ecf20Sopenharmony_ci{ 3548c2ecf20Sopenharmony_ci struct pca9685 *pca = to_pca(chip); 3558c2ecf20Sopenharmony_ci unsigned int reg; 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci /* 3588c2ecf20Sopenharmony_ci * The PWM subsystem does not support a pre-delay. 3598c2ecf20Sopenharmony_ci * So, set the ON-timeout to 0 3608c2ecf20Sopenharmony_ci */ 3618c2ecf20Sopenharmony_ci if (pwm->hwpwm >= PCA9685_MAXCHAN) 3628c2ecf20Sopenharmony_ci reg = PCA9685_ALL_LED_ON_L; 3638c2ecf20Sopenharmony_ci else 3648c2ecf20Sopenharmony_ci reg = LED_N_ON_L(pwm->hwpwm); 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci regmap_write(pca->regmap, reg, 0); 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci if (pwm->hwpwm >= PCA9685_MAXCHAN) 3698c2ecf20Sopenharmony_ci reg = PCA9685_ALL_LED_ON_H; 3708c2ecf20Sopenharmony_ci else 3718c2ecf20Sopenharmony_ci reg = LED_N_ON_H(pwm->hwpwm); 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci regmap_write(pca->regmap, reg, 0); 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci /* 3768c2ecf20Sopenharmony_ci * Clear the full-off bit. 3778c2ecf20Sopenharmony_ci * It has precedence over the others and must be off. 3788c2ecf20Sopenharmony_ci */ 3798c2ecf20Sopenharmony_ci if (pwm->hwpwm >= PCA9685_MAXCHAN) 3808c2ecf20Sopenharmony_ci reg = PCA9685_ALL_LED_OFF_H; 3818c2ecf20Sopenharmony_ci else 3828c2ecf20Sopenharmony_ci reg = LED_N_OFF_H(pwm->hwpwm); 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0); 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci return 0; 3878c2ecf20Sopenharmony_ci} 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_cistatic void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) 3908c2ecf20Sopenharmony_ci{ 3918c2ecf20Sopenharmony_ci struct pca9685 *pca = to_pca(chip); 3928c2ecf20Sopenharmony_ci unsigned int reg; 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci if (pwm->hwpwm >= PCA9685_MAXCHAN) 3958c2ecf20Sopenharmony_ci reg = PCA9685_ALL_LED_OFF_H; 3968c2ecf20Sopenharmony_ci else 3978c2ecf20Sopenharmony_ci reg = LED_N_OFF_H(pwm->hwpwm); 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci regmap_write(pca->regmap, reg, LED_FULL); 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ci /* Clear the LED_OFF counter. */ 4028c2ecf20Sopenharmony_ci if (pwm->hwpwm >= PCA9685_MAXCHAN) 4038c2ecf20Sopenharmony_ci reg = PCA9685_ALL_LED_OFF_L; 4048c2ecf20Sopenharmony_ci else 4058c2ecf20Sopenharmony_ci reg = LED_N_OFF_L(pwm->hwpwm); 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci regmap_write(pca->regmap, reg, 0x0); 4088c2ecf20Sopenharmony_ci} 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_cistatic int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) 4118c2ecf20Sopenharmony_ci{ 4128c2ecf20Sopenharmony_ci struct pca9685 *pca = to_pca(chip); 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm)) 4158c2ecf20Sopenharmony_ci return -EBUSY; 4168c2ecf20Sopenharmony_ci pm_runtime_get_sync(chip->dev); 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci return 0; 4198c2ecf20Sopenharmony_ci} 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_cistatic void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) 4228c2ecf20Sopenharmony_ci{ 4238c2ecf20Sopenharmony_ci struct pca9685 *pca = to_pca(chip); 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci pca9685_pwm_disable(chip, pwm); 4268c2ecf20Sopenharmony_ci pm_runtime_put(chip->dev); 4278c2ecf20Sopenharmony_ci pca9685_pwm_clear_inuse(pca, pwm->hwpwm); 4288c2ecf20Sopenharmony_ci} 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_cistatic const struct pwm_ops pca9685_pwm_ops = { 4318c2ecf20Sopenharmony_ci .enable = pca9685_pwm_enable, 4328c2ecf20Sopenharmony_ci .disable = pca9685_pwm_disable, 4338c2ecf20Sopenharmony_ci .config = pca9685_pwm_config, 4348c2ecf20Sopenharmony_ci .request = pca9685_pwm_request, 4358c2ecf20Sopenharmony_ci .free = pca9685_pwm_free, 4368c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 4378c2ecf20Sopenharmony_ci}; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_cistatic const struct regmap_config pca9685_regmap_i2c_config = { 4408c2ecf20Sopenharmony_ci .reg_bits = 8, 4418c2ecf20Sopenharmony_ci .val_bits = 8, 4428c2ecf20Sopenharmony_ci .max_register = PCA9685_NUMREGS, 4438c2ecf20Sopenharmony_ci .cache_type = REGCACHE_NONE, 4448c2ecf20Sopenharmony_ci}; 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_cistatic int pca9685_pwm_probe(struct i2c_client *client, 4478c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 4488c2ecf20Sopenharmony_ci{ 4498c2ecf20Sopenharmony_ci struct pca9685 *pca; 4508c2ecf20Sopenharmony_ci unsigned int reg; 4518c2ecf20Sopenharmony_ci int ret; 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL); 4548c2ecf20Sopenharmony_ci if (!pca) 4558c2ecf20Sopenharmony_ci return -ENOMEM; 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config); 4588c2ecf20Sopenharmony_ci if (IS_ERR(pca->regmap)) { 4598c2ecf20Sopenharmony_ci ret = PTR_ERR(pca->regmap); 4608c2ecf20Sopenharmony_ci dev_err(&client->dev, "Failed to initialize register map: %d\n", 4618c2ecf20Sopenharmony_ci ret); 4628c2ecf20Sopenharmony_ci return ret; 4638c2ecf20Sopenharmony_ci } 4648c2ecf20Sopenharmony_ci pca->period_ns = PCA9685_DEFAULT_PERIOD; 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci i2c_set_clientdata(client, pca); 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci regmap_read(pca->regmap, PCA9685_MODE2, ®); 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_ci if (device_property_read_bool(&client->dev, "invert")) 4718c2ecf20Sopenharmony_ci reg |= MODE2_INVRT; 4728c2ecf20Sopenharmony_ci else 4738c2ecf20Sopenharmony_ci reg &= ~MODE2_INVRT; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci if (device_property_read_bool(&client->dev, "open-drain")) 4768c2ecf20Sopenharmony_ci reg &= ~MODE2_OUTDRV; 4778c2ecf20Sopenharmony_ci else 4788c2ecf20Sopenharmony_ci reg |= MODE2_OUTDRV; 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci regmap_write(pca->regmap, PCA9685_MODE2, reg); 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci /* Disable all LED ALLCALL and SUBx addresses to avoid bus collisions */ 4838c2ecf20Sopenharmony_ci regmap_read(pca->regmap, PCA9685_MODE1, ®); 4848c2ecf20Sopenharmony_ci reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3); 4858c2ecf20Sopenharmony_ci regmap_write(pca->regmap, PCA9685_MODE1, reg); 4868c2ecf20Sopenharmony_ci 4878c2ecf20Sopenharmony_ci /* Clear all "full off" bits */ 4888c2ecf20Sopenharmony_ci regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0); 4898c2ecf20Sopenharmony_ci regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0); 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci pca->chip.ops = &pca9685_pwm_ops; 4928c2ecf20Sopenharmony_ci /* Add an extra channel for ALL_LED */ 4938c2ecf20Sopenharmony_ci pca->chip.npwm = PCA9685_MAXCHAN + 1; 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci pca->chip.dev = &client->dev; 4968c2ecf20Sopenharmony_ci pca->chip.base = -1; 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci ret = pwmchip_add(&pca->chip); 4998c2ecf20Sopenharmony_ci if (ret < 0) 5008c2ecf20Sopenharmony_ci return ret; 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci ret = pca9685_pwm_gpio_probe(pca); 5038c2ecf20Sopenharmony_ci if (ret < 0) { 5048c2ecf20Sopenharmony_ci pwmchip_remove(&pca->chip); 5058c2ecf20Sopenharmony_ci return ret; 5068c2ecf20Sopenharmony_ci } 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci /* The chip comes out of power-up in the active state */ 5098c2ecf20Sopenharmony_ci pm_runtime_set_active(&client->dev); 5108c2ecf20Sopenharmony_ci /* 5118c2ecf20Sopenharmony_ci * Enable will put the chip into suspend, which is what we 5128c2ecf20Sopenharmony_ci * want as all outputs are disabled at this point 5138c2ecf20Sopenharmony_ci */ 5148c2ecf20Sopenharmony_ci pm_runtime_enable(&client->dev); 5158c2ecf20Sopenharmony_ci 5168c2ecf20Sopenharmony_ci return 0; 5178c2ecf20Sopenharmony_ci} 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_cistatic int pca9685_pwm_remove(struct i2c_client *client) 5208c2ecf20Sopenharmony_ci{ 5218c2ecf20Sopenharmony_ci struct pca9685 *pca = i2c_get_clientdata(client); 5228c2ecf20Sopenharmony_ci int ret; 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci ret = pwmchip_remove(&pca->chip); 5258c2ecf20Sopenharmony_ci if (ret) 5268c2ecf20Sopenharmony_ci return ret; 5278c2ecf20Sopenharmony_ci pm_runtime_disable(&client->dev); 5288c2ecf20Sopenharmony_ci return 0; 5298c2ecf20Sopenharmony_ci} 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_cistatic int __maybe_unused pca9685_pwm_runtime_suspend(struct device *dev) 5328c2ecf20Sopenharmony_ci{ 5338c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 5348c2ecf20Sopenharmony_ci struct pca9685 *pca = i2c_get_clientdata(client); 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ci pca9685_set_sleep_mode(pca, true); 5378c2ecf20Sopenharmony_ci return 0; 5388c2ecf20Sopenharmony_ci} 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_cistatic int __maybe_unused pca9685_pwm_runtime_resume(struct device *dev) 5418c2ecf20Sopenharmony_ci{ 5428c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 5438c2ecf20Sopenharmony_ci struct pca9685 *pca = i2c_get_clientdata(client); 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_ci pca9685_set_sleep_mode(pca, false); 5468c2ecf20Sopenharmony_ci return 0; 5478c2ecf20Sopenharmony_ci} 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_cistatic const struct i2c_device_id pca9685_id[] = { 5508c2ecf20Sopenharmony_ci { "pca9685", 0 }, 5518c2ecf20Sopenharmony_ci { /* sentinel */ }, 5528c2ecf20Sopenharmony_ci}; 5538c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, pca9685_id); 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI 5568c2ecf20Sopenharmony_cistatic const struct acpi_device_id pca9685_acpi_ids[] = { 5578c2ecf20Sopenharmony_ci { "INT3492", 0 }, 5588c2ecf20Sopenharmony_ci { /* sentinel */ }, 5598c2ecf20Sopenharmony_ci}; 5608c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids); 5618c2ecf20Sopenharmony_ci#endif 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ci#ifdef CONFIG_OF 5648c2ecf20Sopenharmony_cistatic const struct of_device_id pca9685_dt_ids[] = { 5658c2ecf20Sopenharmony_ci { .compatible = "nxp,pca9685-pwm", }, 5668c2ecf20Sopenharmony_ci { /* sentinel */ } 5678c2ecf20Sopenharmony_ci}; 5688c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, pca9685_dt_ids); 5698c2ecf20Sopenharmony_ci#endif 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_cistatic const struct dev_pm_ops pca9685_pwm_pm = { 5728c2ecf20Sopenharmony_ci SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend, 5738c2ecf20Sopenharmony_ci pca9685_pwm_runtime_resume, NULL) 5748c2ecf20Sopenharmony_ci}; 5758c2ecf20Sopenharmony_ci 5768c2ecf20Sopenharmony_cistatic struct i2c_driver pca9685_i2c_driver = { 5778c2ecf20Sopenharmony_ci .driver = { 5788c2ecf20Sopenharmony_ci .name = "pca9685-pwm", 5798c2ecf20Sopenharmony_ci .acpi_match_table = ACPI_PTR(pca9685_acpi_ids), 5808c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(pca9685_dt_ids), 5818c2ecf20Sopenharmony_ci .pm = &pca9685_pwm_pm, 5828c2ecf20Sopenharmony_ci }, 5838c2ecf20Sopenharmony_ci .probe = pca9685_pwm_probe, 5848c2ecf20Sopenharmony_ci .remove = pca9685_pwm_remove, 5858c2ecf20Sopenharmony_ci .id_table = pca9685_id, 5868c2ecf20Sopenharmony_ci}; 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_cimodule_i2c_driver(pca9685_i2c_driver); 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_ciMODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>"); 5918c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("PWM driver for PCA9685"); 5928c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 593