pwm-pca9685.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for PCA9685 16-channel 12-bit PWM LED controller
  4. *
  5. * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
  6. * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com>
  7. *
  8. * based on the pwm-twl-led.c driver
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/gpio/driver.h>
  12. #include <linux/i2c.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/property.h>
  17. #include <linux/pwm.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #include <linux/delay.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/bitmap.h>
  23. /*
  24. * Because the PCA9685 has only one prescaler per chip, only the first channel
  25. * that is enabled is allowed to change the prescale register.
  26. * PWM channels requested afterwards must use a period that results in the same
  27. * prescale setting as the one set by the first requested channel.
  28. * GPIOs do not count as enabled PWMs as they are not using the prescaler.
  29. */
  30. #define PCA9685_MODE1 0x00
  31. #define PCA9685_MODE2 0x01
  32. #define PCA9685_SUBADDR1 0x02
  33. #define PCA9685_SUBADDR2 0x03
  34. #define PCA9685_SUBADDR3 0x04
  35. #define PCA9685_ALLCALLADDR 0x05
  36. #define PCA9685_LEDX_ON_L 0x06
  37. #define PCA9685_LEDX_ON_H 0x07
  38. #define PCA9685_LEDX_OFF_L 0x08
  39. #define PCA9685_LEDX_OFF_H 0x09
  40. #define PCA9685_ALL_LED_ON_L 0xFA
  41. #define PCA9685_ALL_LED_ON_H 0xFB
  42. #define PCA9685_ALL_LED_OFF_L 0xFC
  43. #define PCA9685_ALL_LED_OFF_H 0xFD
  44. #define PCA9685_PRESCALE 0xFE
  45. #define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */
  46. #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */
  47. #define PCA9685_COUNTER_RANGE 4096
  48. #define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */
  49. #define PCA9685_NUMREGS 0xFF
  50. #define PCA9685_MAXCHAN 0x10
  51. #define LED_FULL BIT(4)
  52. #define MODE1_ALLCALL BIT(0)
  53. #define MODE1_SUB3 BIT(1)
  54. #define MODE1_SUB2 BIT(2)
  55. #define MODE1_SUB1 BIT(3)
  56. #define MODE1_SLEEP BIT(4)
  57. #define MODE2_INVRT BIT(4)
  58. #define MODE2_OUTDRV BIT(2)
  59. #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
  60. #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
  61. #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
  62. #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
  63. #define REG_ON_H(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_ON_H : LED_N_ON_H((C)))
  64. #define REG_ON_L(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_ON_L : LED_N_ON_L((C)))
  65. #define REG_OFF_H(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_OFF_H : LED_N_OFF_H((C)))
  66. #define REG_OFF_L(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_OFF_L : LED_N_OFF_L((C)))
  67. struct pca9685 {
  68. struct regmap *regmap;
  69. struct mutex lock;
  70. DECLARE_BITMAP(pwms_enabled, PCA9685_MAXCHAN + 1);
  71. #if IS_ENABLED(CONFIG_GPIOLIB)
  72. struct gpio_chip gpio;
  73. DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1);
  74. #endif
  75. };
  76. static inline struct pca9685 *to_pca(struct pwm_chip *chip)
  77. {
  78. return pwmchip_get_drvdata(chip);
  79. }
  80. /* This function is supposed to be called with the lock mutex held */
  81. static bool pca9685_prescaler_can_change(struct pca9685 *pca, int channel)
  82. {
  83. /* No PWM enabled: Change allowed */
  84. if (bitmap_empty(pca->pwms_enabled, PCA9685_MAXCHAN + 1))
  85. return true;
  86. /* More than one PWM enabled: Change not allowed */
  87. if (bitmap_weight(pca->pwms_enabled, PCA9685_MAXCHAN + 1) > 1)
  88. return false;
  89. /*
  90. * Only one PWM enabled: Change allowed if the PWM about to
  91. * be changed is the one that is already enabled
  92. */
  93. return test_bit(channel, pca->pwms_enabled);
  94. }
  95. static int pca9685_read_reg(struct pwm_chip *chip, unsigned int reg, unsigned int *val)
  96. {
  97. struct pca9685 *pca = to_pca(chip);
  98. struct device *dev = pwmchip_parent(chip);
  99. int err;
  100. err = regmap_read(pca->regmap, reg, val);
  101. if (err)
  102. dev_err(dev, "regmap_read of register 0x%x failed: %pe\n", reg, ERR_PTR(err));
  103. return err;
  104. }
  105. static int pca9685_write_reg(struct pwm_chip *chip, unsigned int reg, unsigned int val)
  106. {
  107. struct pca9685 *pca = to_pca(chip);
  108. struct device *dev = pwmchip_parent(chip);
  109. int err;
  110. err = regmap_write(pca->regmap, reg, val);
  111. if (err)
  112. dev_err(dev, "regmap_write to register 0x%x failed: %pe\n", reg, ERR_PTR(err));
  113. return err;
  114. }
  115. /* Helper function to set the duty cycle ratio to duty/4096 (e.g. duty=2048 -> 50%) */
  116. static void pca9685_pwm_set_duty(struct pwm_chip *chip, int channel, unsigned int duty)
  117. {
  118. struct pwm_device *pwm = &chip->pwms[channel];
  119. unsigned int on, off;
  120. if (duty == 0) {
  121. /* Set the full OFF bit, which has the highest precedence */
  122. pca9685_write_reg(chip, REG_OFF_H(channel), LED_FULL);
  123. return;
  124. } else if (duty >= PCA9685_COUNTER_RANGE) {
  125. /* Set the full ON bit and clear the full OFF bit */
  126. pca9685_write_reg(chip, REG_ON_H(channel), LED_FULL);
  127. pca9685_write_reg(chip, REG_OFF_H(channel), 0);
  128. return;
  129. }
  130. if (pwm->state.usage_power && channel < PCA9685_MAXCHAN) {
  131. /*
  132. * If usage_power is set, the pca9685 driver will phase shift
  133. * the individual channels relative to their channel number.
  134. * This improves EMI because the enabled channels no longer
  135. * turn on at the same time, while still maintaining the
  136. * configured duty cycle / power output.
  137. */
  138. on = channel * PCA9685_COUNTER_RANGE / PCA9685_MAXCHAN;
  139. } else
  140. on = 0;
  141. off = (on + duty) % PCA9685_COUNTER_RANGE;
  142. /* Set ON time (clears full ON bit) */
  143. pca9685_write_reg(chip, REG_ON_L(channel), on & 0xff);
  144. pca9685_write_reg(chip, REG_ON_H(channel), (on >> 8) & 0xf);
  145. /* Set OFF time (clears full OFF bit) */
  146. pca9685_write_reg(chip, REG_OFF_L(channel), off & 0xff);
  147. pca9685_write_reg(chip, REG_OFF_H(channel), (off >> 8) & 0xf);
  148. }
  149. static unsigned int pca9685_pwm_get_duty(struct pwm_chip *chip, int channel)
  150. {
  151. struct pwm_device *pwm = &chip->pwms[channel];
  152. unsigned int off = 0, on = 0, val = 0;
  153. if (WARN_ON(channel >= PCA9685_MAXCHAN)) {
  154. /* HW does not support reading state of "all LEDs" channel */
  155. return 0;
  156. }
  157. pca9685_read_reg(chip, LED_N_OFF_H(channel), &off);
  158. if (off & LED_FULL) {
  159. /* Full OFF bit is set */
  160. return 0;
  161. }
  162. pca9685_read_reg(chip, LED_N_ON_H(channel), &on);
  163. if (on & LED_FULL) {
  164. /* Full ON bit is set */
  165. return PCA9685_COUNTER_RANGE;
  166. }
  167. pca9685_read_reg(chip, LED_N_OFF_L(channel), &val);
  168. off = ((off & 0xf) << 8) | (val & 0xff);
  169. if (!pwm->state.usage_power)
  170. return off;
  171. /* Read ON register to calculate duty cycle of staggered output */
  172. if (pca9685_read_reg(chip, LED_N_ON_L(channel), &val)) {
  173. /* Reset val to 0 in case reading LED_N_ON_L failed */
  174. val = 0;
  175. }
  176. on = ((on & 0xf) << 8) | (val & 0xff);
  177. return (off - on) & (PCA9685_COUNTER_RANGE - 1);
  178. }
  179. #if IS_ENABLED(CONFIG_GPIOLIB)
  180. static bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, int pwm_idx)
  181. {
  182. bool is_inuse;
  183. mutex_lock(&pca->lock);
  184. if (pwm_idx >= PCA9685_MAXCHAN) {
  185. /*
  186. * "All LEDs" channel:
  187. * pretend already in use if any of the PWMs are requested
  188. */
  189. if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) {
  190. is_inuse = true;
  191. goto out;
  192. }
  193. } else {
  194. /*
  195. * Regular channel:
  196. * pretend already in use if the "all LEDs" channel is requested
  197. */
  198. if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) {
  199. is_inuse = true;
  200. goto out;
  201. }
  202. }
  203. is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse);
  204. out:
  205. mutex_unlock(&pca->lock);
  206. return is_inuse;
  207. }
  208. static void pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
  209. {
  210. mutex_lock(&pca->lock);
  211. clear_bit(pwm_idx, pca->pwms_inuse);
  212. mutex_unlock(&pca->lock);
  213. }
  214. static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
  215. {
  216. struct pwm_chip *chip = gpiochip_get_data(gpio);
  217. struct pca9685 *pca = to_pca(chip);
  218. if (pca9685_pwm_test_and_set_inuse(pca, offset))
  219. return -EBUSY;
  220. pm_runtime_get_sync(pwmchip_parent(chip));
  221. return 0;
  222. }
  223. static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
  224. {
  225. struct pwm_chip *chip = gpiochip_get_data(gpio);
  226. return pca9685_pwm_get_duty(chip, offset) != 0;
  227. }
  228. static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
  229. int value)
  230. {
  231. struct pwm_chip *chip = gpiochip_get_data(gpio);
  232. pca9685_pwm_set_duty(chip, offset, value ? PCA9685_COUNTER_RANGE : 0);
  233. }
  234. static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
  235. {
  236. struct pwm_chip *chip = gpiochip_get_data(gpio);
  237. struct pca9685 *pca = to_pca(chip);
  238. pca9685_pwm_set_duty(chip, offset, 0);
  239. pm_runtime_put(pwmchip_parent(chip));
  240. pca9685_pwm_clear_inuse(pca, offset);
  241. }
  242. static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
  243. unsigned int offset)
  244. {
  245. /* Always out */
  246. return GPIO_LINE_DIRECTION_OUT;
  247. }
  248. static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
  249. unsigned int offset)
  250. {
  251. return -EINVAL;
  252. }
  253. static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
  254. unsigned int offset, int value)
  255. {
  256. pca9685_pwm_gpio_set(gpio, offset, value);
  257. return 0;
  258. }
  259. /*
  260. * The PCA9685 has a bit for turning the PWM output full off or on. Some
  261. * boards like Intel Galileo actually uses these as normal GPIOs so we
  262. * expose a GPIO chip here which can exclusively take over the underlying
  263. * PWM channel.
  264. */
  265. static int pca9685_pwm_gpio_probe(struct pwm_chip *chip)
  266. {
  267. struct pca9685 *pca = to_pca(chip);
  268. struct device *dev = pwmchip_parent(chip);
  269. pca->gpio.label = dev_name(dev);
  270. pca->gpio.parent = dev;
  271. pca->gpio.request = pca9685_pwm_gpio_request;
  272. pca->gpio.free = pca9685_pwm_gpio_free;
  273. pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
  274. pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
  275. pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
  276. pca->gpio.get = pca9685_pwm_gpio_get;
  277. pca->gpio.set = pca9685_pwm_gpio_set;
  278. pca->gpio.base = -1;
  279. pca->gpio.ngpio = PCA9685_MAXCHAN;
  280. pca->gpio.can_sleep = true;
  281. return devm_gpiochip_add_data(dev, &pca->gpio, chip);
  282. }
  283. #else
  284. static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca,
  285. int pwm_idx)
  286. {
  287. return false;
  288. }
  289. static inline void
  290. pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
  291. {
  292. }
  293. static inline int pca9685_pwm_gpio_probe(struct pwm_chip *chip)
  294. {
  295. return 0;
  296. }
  297. #endif
  298. static void pca9685_set_sleep_mode(struct pwm_chip *chip, bool enable)
  299. {
  300. struct device *dev = pwmchip_parent(chip);
  301. struct pca9685 *pca = to_pca(chip);
  302. int err = regmap_update_bits(pca->regmap, PCA9685_MODE1,
  303. MODE1_SLEEP, enable ? MODE1_SLEEP : 0);
  304. if (err) {
  305. dev_err(dev, "regmap_update_bits of register 0x%x failed: %pe\n",
  306. PCA9685_MODE1, ERR_PTR(err));
  307. return;
  308. }
  309. if (!enable) {
  310. /* Wait 500us for the oscillator to be back up */
  311. udelay(500);
  312. }
  313. }
  314. static int __pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  315. const struct pwm_state *state)
  316. {
  317. struct pca9685 *pca = to_pca(chip);
  318. unsigned long long duty, prescale;
  319. unsigned int val = 0;
  320. if (state->polarity != PWM_POLARITY_NORMAL)
  321. return -EINVAL;
  322. prescale = DIV_ROUND_CLOSEST_ULL(PCA9685_OSC_CLOCK_MHZ * state->period,
  323. PCA9685_COUNTER_RANGE * 1000) - 1;
  324. if (prescale < PCA9685_PRESCALE_MIN || prescale > PCA9685_PRESCALE_MAX) {
  325. dev_err(pwmchip_parent(chip), "pwm not changed: period out of bounds!\n");
  326. return -EINVAL;
  327. }
  328. if (!state->enabled) {
  329. pca9685_pwm_set_duty(chip, pwm->hwpwm, 0);
  330. return 0;
  331. }
  332. pca9685_read_reg(chip, PCA9685_PRESCALE, &val);
  333. if (prescale != val) {
  334. if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) {
  335. dev_err(pwmchip_parent(chip),
  336. "pwm not changed: periods of enabled pwms must match!\n");
  337. return -EBUSY;
  338. }
  339. /*
  340. * Putting the chip briefly into SLEEP mode
  341. * at this point won't interfere with the
  342. * pm_runtime framework, because the pm_runtime
  343. * state is guaranteed active here.
  344. */
  345. /* Put chip into sleep mode */
  346. pca9685_set_sleep_mode(chip, true);
  347. /* Change the chip-wide output frequency */
  348. pca9685_write_reg(chip, PCA9685_PRESCALE, prescale);
  349. /* Wake the chip up */
  350. pca9685_set_sleep_mode(chip, false);
  351. }
  352. duty = PCA9685_COUNTER_RANGE * state->duty_cycle;
  353. duty = DIV_ROUND_UP_ULL(duty, state->period);
  354. pca9685_pwm_set_duty(chip, pwm->hwpwm, duty);
  355. return 0;
  356. }
  357. static int pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  358. const struct pwm_state *state)
  359. {
  360. struct pca9685 *pca = to_pca(chip);
  361. int ret;
  362. mutex_lock(&pca->lock);
  363. ret = __pca9685_pwm_apply(chip, pwm, state);
  364. if (ret == 0) {
  365. if (state->enabled)
  366. set_bit(pwm->hwpwm, pca->pwms_enabled);
  367. else
  368. clear_bit(pwm->hwpwm, pca->pwms_enabled);
  369. }
  370. mutex_unlock(&pca->lock);
  371. return ret;
  372. }
  373. static int pca9685_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  374. struct pwm_state *state)
  375. {
  376. unsigned long long duty;
  377. unsigned int val = 0;
  378. /* Calculate (chip-wide) period from prescale value */
  379. pca9685_read_reg(chip, PCA9685_PRESCALE, &val);
  380. /*
  381. * PCA9685_OSC_CLOCK_MHZ is 25, i.e. an integer divider of 1000.
  382. * The following calculation is therefore only a multiplication
  383. * and we are not losing precision.
  384. */
  385. state->period = (PCA9685_COUNTER_RANGE * 1000 / PCA9685_OSC_CLOCK_MHZ) *
  386. (val + 1);
  387. /* The (per-channel) polarity is fixed */
  388. state->polarity = PWM_POLARITY_NORMAL;
  389. if (pwm->hwpwm >= PCA9685_MAXCHAN) {
  390. /*
  391. * The "all LEDs" channel does not support HW readout
  392. * Return 0 and disabled for backwards compatibility
  393. */
  394. state->duty_cycle = 0;
  395. state->enabled = false;
  396. return 0;
  397. }
  398. state->enabled = true;
  399. duty = pca9685_pwm_get_duty(chip, pwm->hwpwm);
  400. state->duty_cycle = DIV_ROUND_DOWN_ULL(duty * state->period, PCA9685_COUNTER_RANGE);
  401. return 0;
  402. }
  403. static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  404. {
  405. struct pca9685 *pca = to_pca(chip);
  406. if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm))
  407. return -EBUSY;
  408. if (pwm->hwpwm < PCA9685_MAXCHAN) {
  409. /* PWMs - except the "all LEDs" channel - default to enabled */
  410. mutex_lock(&pca->lock);
  411. set_bit(pwm->hwpwm, pca->pwms_enabled);
  412. mutex_unlock(&pca->lock);
  413. }
  414. pm_runtime_get_sync(pwmchip_parent(chip));
  415. return 0;
  416. }
  417. static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  418. {
  419. struct pca9685 *pca = to_pca(chip);
  420. mutex_lock(&pca->lock);
  421. pca9685_pwm_set_duty(chip, pwm->hwpwm, 0);
  422. clear_bit(pwm->hwpwm, pca->pwms_enabled);
  423. mutex_unlock(&pca->lock);
  424. pm_runtime_put(pwmchip_parent(chip));
  425. pca9685_pwm_clear_inuse(pca, pwm->hwpwm);
  426. }
  427. static const struct pwm_ops pca9685_pwm_ops = {
  428. .apply = pca9685_pwm_apply,
  429. .get_state = pca9685_pwm_get_state,
  430. .request = pca9685_pwm_request,
  431. .free = pca9685_pwm_free,
  432. };
  433. static const struct regmap_config pca9685_regmap_i2c_config = {
  434. .reg_bits = 8,
  435. .val_bits = 8,
  436. .max_register = PCA9685_NUMREGS,
  437. .cache_type = REGCACHE_NONE,
  438. };
  439. static int pca9685_pwm_probe(struct i2c_client *client)
  440. {
  441. struct pwm_chip *chip;
  442. struct pca9685 *pca;
  443. unsigned int reg;
  444. int ret;
  445. /* Add an extra channel for ALL_LED */
  446. chip = devm_pwmchip_alloc(&client->dev, PCA9685_MAXCHAN + 1, sizeof(*pca));
  447. if (IS_ERR(chip))
  448. return PTR_ERR(chip);
  449. pca = to_pca(chip);
  450. pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
  451. if (IS_ERR(pca->regmap)) {
  452. ret = PTR_ERR(pca->regmap);
  453. dev_err(&client->dev, "Failed to initialize register map: %d\n",
  454. ret);
  455. return ret;
  456. }
  457. i2c_set_clientdata(client, chip);
  458. mutex_init(&pca->lock);
  459. ret = pca9685_read_reg(chip, PCA9685_MODE2, &reg);
  460. if (ret)
  461. return ret;
  462. if (device_property_read_bool(&client->dev, "invert"))
  463. reg |= MODE2_INVRT;
  464. else
  465. reg &= ~MODE2_INVRT;
  466. if (device_property_read_bool(&client->dev, "open-drain"))
  467. reg &= ~MODE2_OUTDRV;
  468. else
  469. reg |= MODE2_OUTDRV;
  470. ret = pca9685_write_reg(chip, PCA9685_MODE2, reg);
  471. if (ret)
  472. return ret;
  473. /* Disable all LED ALLCALL and SUBx addresses to avoid bus collisions */
  474. pca9685_read_reg(chip, PCA9685_MODE1, &reg);
  475. reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3);
  476. pca9685_write_reg(chip, PCA9685_MODE1, reg);
  477. /* Reset OFF/ON registers to POR default */
  478. pca9685_write_reg(chip, PCA9685_ALL_LED_OFF_L, 0);
  479. pca9685_write_reg(chip, PCA9685_ALL_LED_OFF_H, LED_FULL);
  480. pca9685_write_reg(chip, PCA9685_ALL_LED_ON_L, 0);
  481. pca9685_write_reg(chip, PCA9685_ALL_LED_ON_H, LED_FULL);
  482. chip->ops = &pca9685_pwm_ops;
  483. ret = pwmchip_add(chip);
  484. if (ret < 0)
  485. return ret;
  486. ret = pca9685_pwm_gpio_probe(chip);
  487. if (ret < 0) {
  488. pwmchip_remove(chip);
  489. return ret;
  490. }
  491. pm_runtime_enable(&client->dev);
  492. if (pm_runtime_enabled(&client->dev)) {
  493. /*
  494. * Although the chip comes out of power-up in the sleep state,
  495. * we force it to sleep in case it was woken up before
  496. */
  497. pca9685_set_sleep_mode(chip, true);
  498. pm_runtime_set_suspended(&client->dev);
  499. } else {
  500. /* Wake the chip up if runtime PM is disabled */
  501. pca9685_set_sleep_mode(chip, false);
  502. }
  503. return 0;
  504. }
  505. static void pca9685_pwm_remove(struct i2c_client *client)
  506. {
  507. struct pwm_chip *chip = i2c_get_clientdata(client);
  508. pwmchip_remove(chip);
  509. if (!pm_runtime_enabled(&client->dev)) {
  510. /* Put chip in sleep state if runtime PM is disabled */
  511. pca9685_set_sleep_mode(chip, true);
  512. }
  513. pm_runtime_disable(&client->dev);
  514. }
  515. static int __maybe_unused pca9685_pwm_runtime_suspend(struct device *dev)
  516. {
  517. struct i2c_client *client = to_i2c_client(dev);
  518. struct pwm_chip *chip = i2c_get_clientdata(client);
  519. pca9685_set_sleep_mode(chip, true);
  520. return 0;
  521. }
  522. static int __maybe_unused pca9685_pwm_runtime_resume(struct device *dev)
  523. {
  524. struct i2c_client *client = to_i2c_client(dev);
  525. struct pwm_chip *chip = i2c_get_clientdata(client);
  526. pca9685_set_sleep_mode(chip, false);
  527. return 0;
  528. }
  529. static const struct i2c_device_id pca9685_id[] = {
  530. { "pca9685" },
  531. { /* sentinel */ }
  532. };
  533. MODULE_DEVICE_TABLE(i2c, pca9685_id);
  534. #ifdef CONFIG_ACPI
  535. static const struct acpi_device_id pca9685_acpi_ids[] = {
  536. { "INT3492", 0 },
  537. { /* sentinel */ },
  538. };
  539. MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
  540. #endif
  541. #ifdef CONFIG_OF
  542. static const struct of_device_id pca9685_dt_ids[] = {
  543. { .compatible = "nxp,pca9685-pwm", },
  544. { /* sentinel */ }
  545. };
  546. MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
  547. #endif
  548. static const struct dev_pm_ops pca9685_pwm_pm = {
  549. SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend,
  550. pca9685_pwm_runtime_resume, NULL)
  551. };
  552. static struct i2c_driver pca9685_i2c_driver = {
  553. .driver = {
  554. .name = "pca9685-pwm",
  555. .acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
  556. .of_match_table = of_match_ptr(pca9685_dt_ids),
  557. .pm = &pca9685_pwm_pm,
  558. },
  559. .probe = pca9685_pwm_probe,
  560. .remove = pca9685_pwm_remove,
  561. .id_table = pca9685_id,
  562. };
  563. module_i2c_driver(pca9685_i2c_driver);
  564. MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
  565. MODULE_DESCRIPTION("PWM driver for PCA9685");
  566. MODULE_LICENSE("GPL");