pwm-pca9685.c 15 KB

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