pwm-pca9685.c 18 KB

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