pwm-pca9685.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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, changing the period of
  25. * one channel affects the period of all 16 PWM outputs!
  26. * However, the ratio between each configured duty cycle and the chip-wide
  27. * period remains constant, because the OFF time is set in proportion to the
  28. * counter range.
  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_DEFAULT_PERIOD 5000000 /* Default period_ns = 1/200 Hz */
  49. #define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */
  50. #define PCA9685_NUMREGS 0xFF
  51. #define PCA9685_MAXCHAN 0x10
  52. #define LED_FULL BIT(4)
  53. #define MODE1_ALLCALL BIT(0)
  54. #define MODE1_SUB3 BIT(1)
  55. #define MODE1_SUB2 BIT(2)
  56. #define MODE1_SUB1 BIT(3)
  57. #define MODE1_SLEEP BIT(4)
  58. #define MODE2_INVRT BIT(4)
  59. #define MODE2_OUTDRV BIT(2)
  60. #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
  61. #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
  62. #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
  63. #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
  64. struct pca9685 {
  65. struct pwm_chip chip;
  66. struct regmap *regmap;
  67. int period_ns;
  68. #if IS_ENABLED(CONFIG_GPIOLIB)
  69. struct mutex lock;
  70. struct gpio_chip gpio;
  71. DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1);
  72. #endif
  73. };
  74. static inline struct pca9685 *to_pca(struct pwm_chip *chip)
  75. {
  76. return container_of(chip, struct pca9685, chip);
  77. }
  78. #if IS_ENABLED(CONFIG_GPIOLIB)
  79. static bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, int pwm_idx)
  80. {
  81. bool is_inuse;
  82. mutex_lock(&pca->lock);
  83. if (pwm_idx >= PCA9685_MAXCHAN) {
  84. /*
  85. * "All LEDs" channel:
  86. * pretend already in use if any of the PWMs are requested
  87. */
  88. if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) {
  89. is_inuse = true;
  90. goto out;
  91. }
  92. } else {
  93. /*
  94. * Regular channel:
  95. * pretend already in use if the "all LEDs" channel is requested
  96. */
  97. if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) {
  98. is_inuse = true;
  99. goto out;
  100. }
  101. }
  102. is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse);
  103. out:
  104. mutex_unlock(&pca->lock);
  105. return is_inuse;
  106. }
  107. static void pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
  108. {
  109. mutex_lock(&pca->lock);
  110. clear_bit(pwm_idx, pca->pwms_inuse);
  111. mutex_unlock(&pca->lock);
  112. }
  113. static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
  114. {
  115. struct pca9685 *pca = gpiochip_get_data(gpio);
  116. if (pca9685_pwm_test_and_set_inuse(pca, offset))
  117. return -EBUSY;
  118. pm_runtime_get_sync(pca->chip.dev);
  119. return 0;
  120. }
  121. static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
  122. {
  123. struct pca9685 *pca = gpiochip_get_data(gpio);
  124. struct pwm_device *pwm = &pca->chip.pwms[offset];
  125. unsigned int value;
  126. regmap_read(pca->regmap, LED_N_ON_H(pwm->hwpwm), &value);
  127. return value & LED_FULL;
  128. }
  129. static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
  130. int value)
  131. {
  132. struct pca9685 *pca = gpiochip_get_data(gpio);
  133. struct pwm_device *pwm = &pca->chip.pwms[offset];
  134. unsigned int on = value ? LED_FULL : 0;
  135. /* Clear both OFF registers */
  136. regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm), 0);
  137. regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm), 0);
  138. /* Set the full ON bit */
  139. regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), on);
  140. }
  141. static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
  142. {
  143. struct pca9685 *pca = gpiochip_get_data(gpio);
  144. pca9685_pwm_gpio_set(gpio, offset, 0);
  145. pm_runtime_put(pca->chip.dev);
  146. pca9685_pwm_clear_inuse(pca, offset);
  147. }
  148. static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
  149. unsigned int offset)
  150. {
  151. /* Always out */
  152. return GPIO_LINE_DIRECTION_OUT;
  153. }
  154. static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
  155. unsigned int offset)
  156. {
  157. return -EINVAL;
  158. }
  159. static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
  160. unsigned int offset, int value)
  161. {
  162. pca9685_pwm_gpio_set(gpio, offset, value);
  163. return 0;
  164. }
  165. /*
  166. * The PCA9685 has a bit for turning the PWM output full off or on. Some
  167. * boards like Intel Galileo actually uses these as normal GPIOs so we
  168. * expose a GPIO chip here which can exclusively take over the underlying
  169. * PWM channel.
  170. */
  171. static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
  172. {
  173. struct device *dev = pca->chip.dev;
  174. mutex_init(&pca->lock);
  175. pca->gpio.label = dev_name(dev);
  176. pca->gpio.parent = dev;
  177. pca->gpio.request = pca9685_pwm_gpio_request;
  178. pca->gpio.free = pca9685_pwm_gpio_free;
  179. pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
  180. pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
  181. pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
  182. pca->gpio.get = pca9685_pwm_gpio_get;
  183. pca->gpio.set = pca9685_pwm_gpio_set;
  184. pca->gpio.base = -1;
  185. pca->gpio.ngpio = PCA9685_MAXCHAN;
  186. pca->gpio.can_sleep = true;
  187. return devm_gpiochip_add_data(dev, &pca->gpio, pca);
  188. }
  189. #else
  190. static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca,
  191. int pwm_idx)
  192. {
  193. return false;
  194. }
  195. static inline void
  196. pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
  197. {
  198. }
  199. static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
  200. {
  201. return 0;
  202. }
  203. #endif
  204. static void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable)
  205. {
  206. regmap_update_bits(pca->regmap, PCA9685_MODE1,
  207. MODE1_SLEEP, enable ? MODE1_SLEEP : 0);
  208. if (!enable) {
  209. /* Wait 500us for the oscillator to be back up */
  210. udelay(500);
  211. }
  212. }
  213. static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  214. int duty_ns, int period_ns)
  215. {
  216. struct pca9685 *pca = to_pca(chip);
  217. unsigned long long duty;
  218. unsigned int reg;
  219. int prescale;
  220. if (period_ns != pca->period_ns) {
  221. prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns,
  222. PCA9685_COUNTER_RANGE * 1000) - 1;
  223. if (prescale >= PCA9685_PRESCALE_MIN &&
  224. prescale <= PCA9685_PRESCALE_MAX) {
  225. /*
  226. * Putting the chip briefly into SLEEP mode
  227. * at this point won't interfere with the
  228. * pm_runtime framework, because the pm_runtime
  229. * state is guaranteed active here.
  230. */
  231. /* Put chip into sleep mode */
  232. pca9685_set_sleep_mode(pca, true);
  233. /* Change the chip-wide output frequency */
  234. regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
  235. /* Wake the chip up */
  236. pca9685_set_sleep_mode(pca, false);
  237. pca->period_ns = period_ns;
  238. } else {
  239. dev_err(chip->dev,
  240. "prescaler not set: period out of bounds!\n");
  241. return -EINVAL;
  242. }
  243. }
  244. if (duty_ns < 1) {
  245. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  246. reg = PCA9685_ALL_LED_OFF_H;
  247. else
  248. reg = LED_N_OFF_H(pwm->hwpwm);
  249. regmap_write(pca->regmap, reg, LED_FULL);
  250. return 0;
  251. }
  252. if (duty_ns == period_ns) {
  253. /* Clear both OFF registers */
  254. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  255. reg = PCA9685_ALL_LED_OFF_L;
  256. else
  257. reg = LED_N_OFF_L(pwm->hwpwm);
  258. regmap_write(pca->regmap, reg, 0x0);
  259. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  260. reg = PCA9685_ALL_LED_OFF_H;
  261. else
  262. reg = LED_N_OFF_H(pwm->hwpwm);
  263. regmap_write(pca->regmap, reg, 0x0);
  264. /* Set the full ON bit */
  265. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  266. reg = PCA9685_ALL_LED_ON_H;
  267. else
  268. reg = LED_N_ON_H(pwm->hwpwm);
  269. regmap_write(pca->regmap, reg, LED_FULL);
  270. return 0;
  271. }
  272. duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
  273. duty = DIV_ROUND_UP_ULL(duty, period_ns);
  274. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  275. reg = PCA9685_ALL_LED_OFF_L;
  276. else
  277. reg = LED_N_OFF_L(pwm->hwpwm);
  278. regmap_write(pca->regmap, reg, (int)duty & 0xff);
  279. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  280. reg = PCA9685_ALL_LED_OFF_H;
  281. else
  282. reg = LED_N_OFF_H(pwm->hwpwm);
  283. regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
  284. /* Clear the full ON bit, otherwise the set OFF time has no effect */
  285. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  286. reg = PCA9685_ALL_LED_ON_H;
  287. else
  288. reg = LED_N_ON_H(pwm->hwpwm);
  289. regmap_write(pca->regmap, reg, 0);
  290. return 0;
  291. }
  292. static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  293. {
  294. struct pca9685 *pca = to_pca(chip);
  295. unsigned int reg;
  296. /*
  297. * The PWM subsystem does not support a pre-delay.
  298. * So, set the ON-timeout to 0
  299. */
  300. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  301. reg = PCA9685_ALL_LED_ON_L;
  302. else
  303. reg = LED_N_ON_L(pwm->hwpwm);
  304. regmap_write(pca->regmap, reg, 0);
  305. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  306. reg = PCA9685_ALL_LED_ON_H;
  307. else
  308. reg = LED_N_ON_H(pwm->hwpwm);
  309. regmap_write(pca->regmap, reg, 0);
  310. /*
  311. * Clear the full-off bit.
  312. * It has precedence over the others and must be off.
  313. */
  314. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  315. reg = PCA9685_ALL_LED_OFF_H;
  316. else
  317. reg = LED_N_OFF_H(pwm->hwpwm);
  318. regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
  319. return 0;
  320. }
  321. static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  322. {
  323. struct pca9685 *pca = to_pca(chip);
  324. unsigned int reg;
  325. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  326. reg = PCA9685_ALL_LED_OFF_H;
  327. else
  328. reg = LED_N_OFF_H(pwm->hwpwm);
  329. regmap_write(pca->regmap, reg, LED_FULL);
  330. /* Clear the LED_OFF counter. */
  331. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  332. reg = PCA9685_ALL_LED_OFF_L;
  333. else
  334. reg = LED_N_OFF_L(pwm->hwpwm);
  335. regmap_write(pca->regmap, reg, 0x0);
  336. }
  337. static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  338. {
  339. struct pca9685 *pca = to_pca(chip);
  340. if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm))
  341. return -EBUSY;
  342. pm_runtime_get_sync(chip->dev);
  343. return 0;
  344. }
  345. static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  346. {
  347. struct pca9685 *pca = to_pca(chip);
  348. pca9685_pwm_disable(chip, pwm);
  349. pm_runtime_put(chip->dev);
  350. pca9685_pwm_clear_inuse(pca, pwm->hwpwm);
  351. }
  352. static const struct pwm_ops pca9685_pwm_ops = {
  353. .enable = pca9685_pwm_enable,
  354. .disable = pca9685_pwm_disable,
  355. .config = pca9685_pwm_config,
  356. .request = pca9685_pwm_request,
  357. .free = pca9685_pwm_free,
  358. .owner = THIS_MODULE,
  359. };
  360. static const struct regmap_config pca9685_regmap_i2c_config = {
  361. .reg_bits = 8,
  362. .val_bits = 8,
  363. .max_register = PCA9685_NUMREGS,
  364. .cache_type = REGCACHE_NONE,
  365. };
  366. static int pca9685_pwm_probe(struct i2c_client *client,
  367. const struct i2c_device_id *id)
  368. {
  369. struct pca9685 *pca;
  370. unsigned int reg;
  371. int ret;
  372. pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
  373. if (!pca)
  374. return -ENOMEM;
  375. pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
  376. if (IS_ERR(pca->regmap)) {
  377. ret = PTR_ERR(pca->regmap);
  378. dev_err(&client->dev, "Failed to initialize register map: %d\n",
  379. ret);
  380. return ret;
  381. }
  382. pca->period_ns = PCA9685_DEFAULT_PERIOD;
  383. i2c_set_clientdata(client, pca);
  384. regmap_read(pca->regmap, PCA9685_MODE2, &reg);
  385. if (device_property_read_bool(&client->dev, "invert"))
  386. reg |= MODE2_INVRT;
  387. else
  388. reg &= ~MODE2_INVRT;
  389. if (device_property_read_bool(&client->dev, "open-drain"))
  390. reg &= ~MODE2_OUTDRV;
  391. else
  392. reg |= MODE2_OUTDRV;
  393. regmap_write(pca->regmap, PCA9685_MODE2, reg);
  394. /* Disable all LED ALLCALL and SUBx addresses to avoid bus collisions */
  395. regmap_read(pca->regmap, PCA9685_MODE1, &reg);
  396. reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3);
  397. regmap_write(pca->regmap, PCA9685_MODE1, reg);
  398. /* Clear all "full off" bits */
  399. regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
  400. regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
  401. pca->chip.ops = &pca9685_pwm_ops;
  402. /* Add an extra channel for ALL_LED */
  403. pca->chip.npwm = PCA9685_MAXCHAN + 1;
  404. pca->chip.dev = &client->dev;
  405. pca->chip.base = -1;
  406. ret = pwmchip_add(&pca->chip);
  407. if (ret < 0)
  408. return ret;
  409. ret = pca9685_pwm_gpio_probe(pca);
  410. if (ret < 0) {
  411. pwmchip_remove(&pca->chip);
  412. return ret;
  413. }
  414. /* The chip comes out of power-up in the active state */
  415. pm_runtime_set_active(&client->dev);
  416. /*
  417. * Enable will put the chip into suspend, which is what we
  418. * want as all outputs are disabled at this point
  419. */
  420. pm_runtime_enable(&client->dev);
  421. return 0;
  422. }
  423. static int pca9685_pwm_remove(struct i2c_client *client)
  424. {
  425. struct pca9685 *pca = i2c_get_clientdata(client);
  426. int ret;
  427. ret = pwmchip_remove(&pca->chip);
  428. if (ret)
  429. return ret;
  430. pm_runtime_disable(&client->dev);
  431. return 0;
  432. }
  433. static int __maybe_unused pca9685_pwm_runtime_suspend(struct device *dev)
  434. {
  435. struct i2c_client *client = to_i2c_client(dev);
  436. struct pca9685 *pca = i2c_get_clientdata(client);
  437. pca9685_set_sleep_mode(pca, true);
  438. return 0;
  439. }
  440. static int __maybe_unused pca9685_pwm_runtime_resume(struct device *dev)
  441. {
  442. struct i2c_client *client = to_i2c_client(dev);
  443. struct pca9685 *pca = i2c_get_clientdata(client);
  444. pca9685_set_sleep_mode(pca, false);
  445. return 0;
  446. }
  447. static const struct i2c_device_id pca9685_id[] = {
  448. { "pca9685", 0 },
  449. { /* sentinel */ },
  450. };
  451. MODULE_DEVICE_TABLE(i2c, pca9685_id);
  452. #ifdef CONFIG_ACPI
  453. static const struct acpi_device_id pca9685_acpi_ids[] = {
  454. { "INT3492", 0 },
  455. { /* sentinel */ },
  456. };
  457. MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
  458. #endif
  459. #ifdef CONFIG_OF
  460. static const struct of_device_id pca9685_dt_ids[] = {
  461. { .compatible = "nxp,pca9685-pwm", },
  462. { /* sentinel */ }
  463. };
  464. MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
  465. #endif
  466. static const struct dev_pm_ops pca9685_pwm_pm = {
  467. SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend,
  468. pca9685_pwm_runtime_resume, NULL)
  469. };
  470. static struct i2c_driver pca9685_i2c_driver = {
  471. .driver = {
  472. .name = "pca9685-pwm",
  473. .acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
  474. .of_match_table = of_match_ptr(pca9685_dt_ids),
  475. .pm = &pca9685_pwm_pm,
  476. },
  477. .probe = pca9685_pwm_probe,
  478. .remove = pca9685_pwm_remove,
  479. .id_table = pca9685_id,
  480. };
  481. module_i2c_driver(pca9685_i2c_driver);
  482. MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
  483. MODULE_DESCRIPTION("PWM driver for PCA9685");
  484. MODULE_LICENSE("GPL");