pwm-pca9685.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. /*
  34. * Because the PCA9685 has only one prescaler per chip, changing the period of
  35. * one channel affects the period of all 16 PWM outputs!
  36. * However, the ratio between each configured duty cycle and the chip-wide
  37. * period remains constant, because the OFF time is set in proportion to the
  38. * counter range.
  39. */
  40. #define PCA9685_MODE1 0x00
  41. #define PCA9685_MODE2 0x01
  42. #define PCA9685_SUBADDR1 0x02
  43. #define PCA9685_SUBADDR2 0x03
  44. #define PCA9685_SUBADDR3 0x04
  45. #define PCA9685_ALLCALLADDR 0x05
  46. #define PCA9685_LEDX_ON_L 0x06
  47. #define PCA9685_LEDX_ON_H 0x07
  48. #define PCA9685_LEDX_OFF_L 0x08
  49. #define PCA9685_LEDX_OFF_H 0x09
  50. #define PCA9685_ALL_LED_ON_L 0xFA
  51. #define PCA9685_ALL_LED_ON_H 0xFB
  52. #define PCA9685_ALL_LED_OFF_L 0xFC
  53. #define PCA9685_ALL_LED_OFF_H 0xFD
  54. #define PCA9685_PRESCALE 0xFE
  55. #define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */
  56. #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */
  57. #define PCA9685_COUNTER_RANGE 4096
  58. #define PCA9685_DEFAULT_PERIOD 5000000 /* Default period_ns = 1/200 Hz */
  59. #define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */
  60. #define PCA9685_NUMREGS 0xFF
  61. #define PCA9685_MAXCHAN 0x10
  62. #define LED_FULL (1 << 4)
  63. #define MODE1_SLEEP (1 << 4)
  64. #define MODE2_INVRT (1 << 4)
  65. #define MODE2_OUTDRV (1 << 2)
  66. #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
  67. #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
  68. #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
  69. #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
  70. struct pca9685 {
  71. struct pwm_chip chip;
  72. struct regmap *regmap;
  73. int duty_ns;
  74. int period_ns;
  75. #if IS_ENABLED(CONFIG_GPIOLIB)
  76. struct mutex lock;
  77. struct gpio_chip gpio;
  78. #endif
  79. };
  80. static inline struct pca9685 *to_pca(struct pwm_chip *chip)
  81. {
  82. return container_of(chip, struct pca9685, chip);
  83. }
  84. #if IS_ENABLED(CONFIG_GPIOLIB)
  85. static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
  86. {
  87. struct pca9685 *pca = gpiochip_get_data(gpio);
  88. struct pwm_device *pwm;
  89. mutex_lock(&pca->lock);
  90. pwm = &pca->chip.pwms[offset];
  91. if (pwm->flags & (PWMF_REQUESTED | PWMF_EXPORTED)) {
  92. mutex_unlock(&pca->lock);
  93. return -EBUSY;
  94. }
  95. pwm_set_chip_data(pwm, (void *)1);
  96. mutex_unlock(&pca->lock);
  97. pm_runtime_get_sync(pca->chip.dev);
  98. return 0;
  99. }
  100. static bool pca9685_pwm_is_gpio(struct pca9685 *pca, struct pwm_device *pwm)
  101. {
  102. bool is_gpio = false;
  103. mutex_lock(&pca->lock);
  104. if (pwm->hwpwm >= PCA9685_MAXCHAN) {
  105. unsigned int i;
  106. /*
  107. * Check if any of the GPIOs are requested and in that case
  108. * prevent using the "all LEDs" channel.
  109. */
  110. for (i = 0; i < pca->gpio.ngpio; i++)
  111. if (gpiochip_is_requested(&pca->gpio, i)) {
  112. is_gpio = true;
  113. break;
  114. }
  115. } else if (pwm_get_chip_data(pwm)) {
  116. is_gpio = true;
  117. }
  118. mutex_unlock(&pca->lock);
  119. return is_gpio;
  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. struct pwm_device *pwm;
  145. pca9685_pwm_gpio_set(gpio, offset, 0);
  146. pm_runtime_put(pca->chip.dev);
  147. mutex_lock(&pca->lock);
  148. pwm = &pca->chip.pwms[offset];
  149. pwm_set_chip_data(pwm, NULL);
  150. mutex_unlock(&pca->lock);
  151. }
  152. static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
  153. unsigned int offset)
  154. {
  155. /* Always out */
  156. return 0;
  157. }
  158. static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
  159. unsigned int offset)
  160. {
  161. return -EINVAL;
  162. }
  163. static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
  164. unsigned int offset, int value)
  165. {
  166. pca9685_pwm_gpio_set(gpio, offset, value);
  167. return 0;
  168. }
  169. /*
  170. * The PCA9685 has a bit for turning the PWM output full off or on. Some
  171. * boards like Intel Galileo actually uses these as normal GPIOs so we
  172. * expose a GPIO chip here which can exclusively take over the underlying
  173. * PWM channel.
  174. */
  175. static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
  176. {
  177. struct device *dev = pca->chip.dev;
  178. mutex_init(&pca->lock);
  179. pca->gpio.label = dev_name(dev);
  180. pca->gpio.parent = dev;
  181. pca->gpio.request = pca9685_pwm_gpio_request;
  182. pca->gpio.free = pca9685_pwm_gpio_free;
  183. pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
  184. pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
  185. pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
  186. pca->gpio.get = pca9685_pwm_gpio_get;
  187. pca->gpio.set = pca9685_pwm_gpio_set;
  188. pca->gpio.base = -1;
  189. pca->gpio.ngpio = PCA9685_MAXCHAN;
  190. pca->gpio.can_sleep = true;
  191. return devm_gpiochip_add_data(dev, &pca->gpio, pca);
  192. }
  193. #else
  194. static inline bool pca9685_pwm_is_gpio(struct pca9685 *pca,
  195. struct pwm_device *pwm)
  196. {
  197. return false;
  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. pca->duty_ns = duty_ns;
  245. if (duty_ns < 1) {
  246. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  247. reg = PCA9685_ALL_LED_OFF_H;
  248. else
  249. reg = LED_N_OFF_H(pwm->hwpwm);
  250. regmap_write(pca->regmap, reg, LED_FULL);
  251. return 0;
  252. }
  253. if (duty_ns == period_ns) {
  254. /* Clear both OFF registers */
  255. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  256. reg = PCA9685_ALL_LED_OFF_L;
  257. else
  258. reg = LED_N_OFF_L(pwm->hwpwm);
  259. regmap_write(pca->regmap, reg, 0x0);
  260. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  261. reg = PCA9685_ALL_LED_OFF_H;
  262. else
  263. reg = LED_N_OFF_H(pwm->hwpwm);
  264. regmap_write(pca->regmap, reg, 0x0);
  265. /* Set the full ON bit */
  266. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  267. reg = PCA9685_ALL_LED_ON_H;
  268. else
  269. reg = LED_N_ON_H(pwm->hwpwm);
  270. regmap_write(pca->regmap, reg, LED_FULL);
  271. return 0;
  272. }
  273. duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
  274. duty = DIV_ROUND_UP_ULL(duty, period_ns);
  275. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  276. reg = PCA9685_ALL_LED_OFF_L;
  277. else
  278. reg = LED_N_OFF_L(pwm->hwpwm);
  279. regmap_write(pca->regmap, reg, (int)duty & 0xff);
  280. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  281. reg = PCA9685_ALL_LED_OFF_H;
  282. else
  283. reg = LED_N_OFF_H(pwm->hwpwm);
  284. regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
  285. /* Clear the full ON bit, otherwise the set OFF time has no effect */
  286. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  287. reg = PCA9685_ALL_LED_ON_H;
  288. else
  289. reg = LED_N_ON_H(pwm->hwpwm);
  290. regmap_write(pca->regmap, reg, 0);
  291. return 0;
  292. }
  293. static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  294. {
  295. struct pca9685 *pca = to_pca(chip);
  296. unsigned int reg;
  297. /*
  298. * The PWM subsystem does not support a pre-delay.
  299. * So, set the ON-timeout to 0
  300. */
  301. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  302. reg = PCA9685_ALL_LED_ON_L;
  303. else
  304. reg = LED_N_ON_L(pwm->hwpwm);
  305. regmap_write(pca->regmap, reg, 0);
  306. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  307. reg = PCA9685_ALL_LED_ON_H;
  308. else
  309. reg = LED_N_ON_H(pwm->hwpwm);
  310. regmap_write(pca->regmap, reg, 0);
  311. /*
  312. * Clear the full-off bit.
  313. * It has precedence over the others and must be off.
  314. */
  315. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  316. reg = PCA9685_ALL_LED_OFF_H;
  317. else
  318. reg = LED_N_OFF_H(pwm->hwpwm);
  319. regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
  320. return 0;
  321. }
  322. static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  323. {
  324. struct pca9685 *pca = to_pca(chip);
  325. unsigned int reg;
  326. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  327. reg = PCA9685_ALL_LED_OFF_H;
  328. else
  329. reg = LED_N_OFF_H(pwm->hwpwm);
  330. regmap_write(pca->regmap, reg, LED_FULL);
  331. /* Clear the LED_OFF counter. */
  332. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  333. reg = PCA9685_ALL_LED_OFF_L;
  334. else
  335. reg = LED_N_OFF_L(pwm->hwpwm);
  336. regmap_write(pca->regmap, reg, 0x0);
  337. }
  338. static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  339. {
  340. struct pca9685 *pca = to_pca(chip);
  341. if (pca9685_pwm_is_gpio(pca, pwm))
  342. return -EBUSY;
  343. pm_runtime_get_sync(chip->dev);
  344. return 0;
  345. }
  346. static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  347. {
  348. pca9685_pwm_disable(chip, pwm);
  349. pm_runtime_put(chip->dev);
  350. }
  351. static const struct pwm_ops pca9685_pwm_ops = {
  352. .enable = pca9685_pwm_enable,
  353. .disable = pca9685_pwm_disable,
  354. .config = pca9685_pwm_config,
  355. .request = pca9685_pwm_request,
  356. .free = pca9685_pwm_free,
  357. .owner = THIS_MODULE,
  358. };
  359. static const struct regmap_config pca9685_regmap_i2c_config = {
  360. .reg_bits = 8,
  361. .val_bits = 8,
  362. .max_register = PCA9685_NUMREGS,
  363. .cache_type = REGCACHE_NONE,
  364. };
  365. static int pca9685_pwm_probe(struct i2c_client *client,
  366. const struct i2c_device_id *id)
  367. {
  368. struct pca9685 *pca;
  369. int ret;
  370. int mode2;
  371. pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
  372. if (!pca)
  373. return -ENOMEM;
  374. pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
  375. if (IS_ERR(pca->regmap)) {
  376. ret = PTR_ERR(pca->regmap);
  377. dev_err(&client->dev, "Failed to initialize register map: %d\n",
  378. ret);
  379. return ret;
  380. }
  381. pca->duty_ns = 0;
  382. pca->period_ns = PCA9685_DEFAULT_PERIOD;
  383. i2c_set_clientdata(client, pca);
  384. regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
  385. if (device_property_read_bool(&client->dev, "invert"))
  386. mode2 |= MODE2_INVRT;
  387. else
  388. mode2 &= ~MODE2_INVRT;
  389. if (device_property_read_bool(&client->dev, "open-drain"))
  390. mode2 &= ~MODE2_OUTDRV;
  391. else
  392. mode2 |= MODE2_OUTDRV;
  393. regmap_write(pca->regmap, PCA9685_MODE2, mode2);
  394. /* clear all "full off" bits */
  395. regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
  396. regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
  397. pca->chip.ops = &pca9685_pwm_ops;
  398. /* add an extra channel for ALL_LED */
  399. pca->chip.npwm = PCA9685_MAXCHAN + 1;
  400. pca->chip.dev = &client->dev;
  401. pca->chip.base = -1;
  402. ret = pwmchip_add(&pca->chip);
  403. if (ret < 0)
  404. return ret;
  405. ret = pca9685_pwm_gpio_probe(pca);
  406. if (ret < 0) {
  407. pwmchip_remove(&pca->chip);
  408. return ret;
  409. }
  410. /* the chip comes out of power-up in the active state */
  411. pm_runtime_set_active(&client->dev);
  412. /*
  413. * enable will put the chip into suspend, which is what we
  414. * want as all outputs are disabled at this point
  415. */
  416. pm_runtime_enable(&client->dev);
  417. return 0;
  418. }
  419. static int pca9685_pwm_remove(struct i2c_client *client)
  420. {
  421. struct pca9685 *pca = i2c_get_clientdata(client);
  422. int ret;
  423. ret = pwmchip_remove(&pca->chip);
  424. if (ret)
  425. return ret;
  426. pm_runtime_disable(&client->dev);
  427. return 0;
  428. }
  429. #ifdef CONFIG_PM
  430. static int pca9685_pwm_runtime_suspend(struct device *dev)
  431. {
  432. struct i2c_client *client = to_i2c_client(dev);
  433. struct pca9685 *pca = i2c_get_clientdata(client);
  434. pca9685_set_sleep_mode(pca, true);
  435. return 0;
  436. }
  437. static int pca9685_pwm_runtime_resume(struct device *dev)
  438. {
  439. struct i2c_client *client = to_i2c_client(dev);
  440. struct pca9685 *pca = i2c_get_clientdata(client);
  441. pca9685_set_sleep_mode(pca, false);
  442. return 0;
  443. }
  444. #endif
  445. static const struct i2c_device_id pca9685_id[] = {
  446. { "pca9685", 0 },
  447. { /* sentinel */ },
  448. };
  449. MODULE_DEVICE_TABLE(i2c, pca9685_id);
  450. #ifdef CONFIG_ACPI
  451. static const struct acpi_device_id pca9685_acpi_ids[] = {
  452. { "INT3492", 0 },
  453. { /* sentinel */ },
  454. };
  455. MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
  456. #endif
  457. #ifdef CONFIG_OF
  458. static const struct of_device_id pca9685_dt_ids[] = {
  459. { .compatible = "nxp,pca9685-pwm", },
  460. { /* sentinel */ }
  461. };
  462. MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
  463. #endif
  464. static const struct dev_pm_ops pca9685_pwm_pm = {
  465. SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend,
  466. pca9685_pwm_runtime_resume, NULL)
  467. };
  468. static struct i2c_driver pca9685_i2c_driver = {
  469. .driver = {
  470. .name = "pca9685-pwm",
  471. .acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
  472. .of_match_table = of_match_ptr(pca9685_dt_ids),
  473. .pm = &pca9685_pwm_pm,
  474. },
  475. .probe = pca9685_pwm_probe,
  476. .remove = pca9685_pwm_remove,
  477. .id_table = pca9685_id,
  478. };
  479. module_i2c_driver(pca9685_i2c_driver);
  480. MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
  481. MODULE_DESCRIPTION("PWM driver for PCA9685");
  482. MODULE_LICENSE("GPL");