pwm-pca9685.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. /*
  33. * Because the PCA9685 has only one prescaler per chip, changing the period of
  34. * one channel affects the period of all 16 PWM outputs!
  35. * However, the ratio between each configured duty cycle and the chip-wide
  36. * period remains constant, because the OFF time is set in proportion to the
  37. * counter range.
  38. */
  39. #define PCA9685_MODE1 0x00
  40. #define PCA9685_MODE2 0x01
  41. #define PCA9685_SUBADDR1 0x02
  42. #define PCA9685_SUBADDR2 0x03
  43. #define PCA9685_SUBADDR3 0x04
  44. #define PCA9685_ALLCALLADDR 0x05
  45. #define PCA9685_LEDX_ON_L 0x06
  46. #define PCA9685_LEDX_ON_H 0x07
  47. #define PCA9685_LEDX_OFF_L 0x08
  48. #define PCA9685_LEDX_OFF_H 0x09
  49. #define PCA9685_ALL_LED_ON_L 0xFA
  50. #define PCA9685_ALL_LED_ON_H 0xFB
  51. #define PCA9685_ALL_LED_OFF_L 0xFC
  52. #define PCA9685_ALL_LED_OFF_H 0xFD
  53. #define PCA9685_PRESCALE 0xFE
  54. #define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */
  55. #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */
  56. #define PCA9685_COUNTER_RANGE 4096
  57. #define PCA9685_DEFAULT_PERIOD 5000000 /* Default period_ns = 1/200 Hz */
  58. #define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */
  59. #define PCA9685_NUMREGS 0xFF
  60. #define PCA9685_MAXCHAN 0x10
  61. #define LED_FULL (1 << 4)
  62. #define MODE1_SLEEP (1 << 4)
  63. #define MODE2_INVRT (1 << 4)
  64. #define MODE2_OUTDRV (1 << 2)
  65. #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
  66. #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
  67. #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
  68. #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
  69. struct pca9685 {
  70. struct pwm_chip chip;
  71. struct regmap *regmap;
  72. int active_cnt;
  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. return 0;
  98. }
  99. static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
  100. {
  101. struct pca9685 *pca = gpiochip_get_data(gpio);
  102. struct pwm_device *pwm;
  103. mutex_lock(&pca->lock);
  104. pwm = &pca->chip.pwms[offset];
  105. pwm_set_chip_data(pwm, NULL);
  106. mutex_unlock(&pca->lock);
  107. }
  108. static bool pca9685_pwm_is_gpio(struct pca9685 *pca, struct pwm_device *pwm)
  109. {
  110. bool is_gpio = false;
  111. mutex_lock(&pca->lock);
  112. if (pwm->hwpwm >= PCA9685_MAXCHAN) {
  113. unsigned int i;
  114. /*
  115. * Check if any of the GPIOs are requested and in that case
  116. * prevent using the "all LEDs" channel.
  117. */
  118. for (i = 0; i < pca->gpio.ngpio; i++)
  119. if (gpiochip_is_requested(&pca->gpio, i)) {
  120. is_gpio = true;
  121. break;
  122. }
  123. } else if (pwm_get_chip_data(pwm)) {
  124. is_gpio = true;
  125. }
  126. mutex_unlock(&pca->lock);
  127. return is_gpio;
  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 int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
  150. unsigned int offset)
  151. {
  152. /* Always out */
  153. return 0;
  154. }
  155. static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
  156. unsigned int offset)
  157. {
  158. return -EINVAL;
  159. }
  160. static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
  161. unsigned int offset, int value)
  162. {
  163. pca9685_pwm_gpio_set(gpio, offset, value);
  164. return 0;
  165. }
  166. /*
  167. * The PCA9685 has a bit for turning the PWM output full off or on. Some
  168. * boards like Intel Galileo actually uses these as normal GPIOs so we
  169. * expose a GPIO chip here which can exclusively take over the underlying
  170. * PWM channel.
  171. */
  172. static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
  173. {
  174. struct device *dev = pca->chip.dev;
  175. mutex_init(&pca->lock);
  176. pca->gpio.label = dev_name(dev);
  177. pca->gpio.parent = dev;
  178. pca->gpio.request = pca9685_pwm_gpio_request;
  179. pca->gpio.free = pca9685_pwm_gpio_free;
  180. pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
  181. pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
  182. pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
  183. pca->gpio.get = pca9685_pwm_gpio_get;
  184. pca->gpio.set = pca9685_pwm_gpio_set;
  185. pca->gpio.base = -1;
  186. pca->gpio.ngpio = PCA9685_MAXCHAN;
  187. pca->gpio.can_sleep = true;
  188. return devm_gpiochip_add_data(dev, &pca->gpio, pca);
  189. }
  190. #else
  191. static inline bool pca9685_pwm_is_gpio(struct pca9685 *pca,
  192. struct pwm_device *pwm)
  193. {
  194. return false;
  195. }
  196. static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
  197. {
  198. return 0;
  199. }
  200. #endif
  201. static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  202. int duty_ns, int period_ns)
  203. {
  204. struct pca9685 *pca = to_pca(chip);
  205. unsigned long long duty;
  206. unsigned int reg;
  207. int prescale;
  208. if (period_ns != pca->period_ns) {
  209. prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns,
  210. PCA9685_COUNTER_RANGE * 1000) - 1;
  211. if (prescale >= PCA9685_PRESCALE_MIN &&
  212. prescale <= PCA9685_PRESCALE_MAX) {
  213. /* Put chip into sleep mode */
  214. regmap_update_bits(pca->regmap, PCA9685_MODE1,
  215. MODE1_SLEEP, MODE1_SLEEP);
  216. /* Change the chip-wide output frequency */
  217. regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
  218. /* Wake the chip up */
  219. regmap_update_bits(pca->regmap, PCA9685_MODE1,
  220. MODE1_SLEEP, 0x0);
  221. /* Wait 500us for the oscillator to be back up */
  222. udelay(500);
  223. pca->period_ns = period_ns;
  224. } else {
  225. dev_err(chip->dev,
  226. "prescaler not set: period out of bounds!\n");
  227. return -EINVAL;
  228. }
  229. }
  230. pca->duty_ns = duty_ns;
  231. if (duty_ns < 1) {
  232. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  233. reg = PCA9685_ALL_LED_OFF_H;
  234. else
  235. reg = LED_N_OFF_H(pwm->hwpwm);
  236. regmap_write(pca->regmap, reg, LED_FULL);
  237. return 0;
  238. }
  239. if (duty_ns == period_ns) {
  240. /* Clear both OFF registers */
  241. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  242. reg = PCA9685_ALL_LED_OFF_L;
  243. else
  244. reg = LED_N_OFF_L(pwm->hwpwm);
  245. regmap_write(pca->regmap, reg, 0x0);
  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, 0x0);
  251. /* Set the full ON bit */
  252. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  253. reg = PCA9685_ALL_LED_ON_H;
  254. else
  255. reg = LED_N_ON_H(pwm->hwpwm);
  256. regmap_write(pca->regmap, reg, LED_FULL);
  257. return 0;
  258. }
  259. duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
  260. duty = DIV_ROUND_UP_ULL(duty, period_ns);
  261. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  262. reg = PCA9685_ALL_LED_OFF_L;
  263. else
  264. reg = LED_N_OFF_L(pwm->hwpwm);
  265. regmap_write(pca->regmap, reg, (int)duty & 0xff);
  266. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  267. reg = PCA9685_ALL_LED_OFF_H;
  268. else
  269. reg = LED_N_OFF_H(pwm->hwpwm);
  270. regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
  271. /* Clear the full ON bit, otherwise the set OFF time has no effect */
  272. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  273. reg = PCA9685_ALL_LED_ON_H;
  274. else
  275. reg = LED_N_ON_H(pwm->hwpwm);
  276. regmap_write(pca->regmap, reg, 0);
  277. return 0;
  278. }
  279. static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  280. {
  281. struct pca9685 *pca = to_pca(chip);
  282. unsigned int reg;
  283. /*
  284. * The PWM subsystem does not support a pre-delay.
  285. * So, set the ON-timeout to 0
  286. */
  287. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  288. reg = PCA9685_ALL_LED_ON_L;
  289. else
  290. reg = LED_N_ON_L(pwm->hwpwm);
  291. regmap_write(pca->regmap, reg, 0);
  292. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  293. reg = PCA9685_ALL_LED_ON_H;
  294. else
  295. reg = LED_N_ON_H(pwm->hwpwm);
  296. regmap_write(pca->regmap, reg, 0);
  297. /*
  298. * Clear the full-off bit.
  299. * It has precedence over the others and must be off.
  300. */
  301. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  302. reg = PCA9685_ALL_LED_OFF_H;
  303. else
  304. reg = LED_N_OFF_H(pwm->hwpwm);
  305. regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
  306. return 0;
  307. }
  308. static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  309. {
  310. struct pca9685 *pca = to_pca(chip);
  311. unsigned int reg;
  312. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  313. reg = PCA9685_ALL_LED_OFF_H;
  314. else
  315. reg = LED_N_OFF_H(pwm->hwpwm);
  316. regmap_write(pca->regmap, reg, LED_FULL);
  317. /* Clear the LED_OFF counter. */
  318. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  319. reg = PCA9685_ALL_LED_OFF_L;
  320. else
  321. reg = LED_N_OFF_L(pwm->hwpwm);
  322. regmap_write(pca->regmap, reg, 0x0);
  323. }
  324. static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  325. {
  326. struct pca9685 *pca = to_pca(chip);
  327. if (pca9685_pwm_is_gpio(pca, pwm))
  328. return -EBUSY;
  329. if (pca->active_cnt++ == 0)
  330. return regmap_update_bits(pca->regmap, PCA9685_MODE1,
  331. MODE1_SLEEP, 0x0);
  332. return 0;
  333. }
  334. static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  335. {
  336. struct pca9685 *pca = to_pca(chip);
  337. if (--pca->active_cnt == 0)
  338. regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
  339. MODE1_SLEEP);
  340. }
  341. static const struct pwm_ops pca9685_pwm_ops = {
  342. .enable = pca9685_pwm_enable,
  343. .disable = pca9685_pwm_disable,
  344. .config = pca9685_pwm_config,
  345. .request = pca9685_pwm_request,
  346. .free = pca9685_pwm_free,
  347. .owner = THIS_MODULE,
  348. };
  349. static const struct regmap_config pca9685_regmap_i2c_config = {
  350. .reg_bits = 8,
  351. .val_bits = 8,
  352. .max_register = PCA9685_NUMREGS,
  353. .cache_type = REGCACHE_NONE,
  354. };
  355. static int pca9685_pwm_probe(struct i2c_client *client,
  356. const struct i2c_device_id *id)
  357. {
  358. struct pca9685 *pca;
  359. int ret;
  360. int mode2;
  361. pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
  362. if (!pca)
  363. return -ENOMEM;
  364. pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
  365. if (IS_ERR(pca->regmap)) {
  366. ret = PTR_ERR(pca->regmap);
  367. dev_err(&client->dev, "Failed to initialize register map: %d\n",
  368. ret);
  369. return ret;
  370. }
  371. pca->duty_ns = 0;
  372. pca->period_ns = PCA9685_DEFAULT_PERIOD;
  373. i2c_set_clientdata(client, pca);
  374. regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
  375. if (device_property_read_bool(&client->dev, "invert"))
  376. mode2 |= MODE2_INVRT;
  377. else
  378. mode2 &= ~MODE2_INVRT;
  379. if (device_property_read_bool(&client->dev, "open-drain"))
  380. mode2 &= ~MODE2_OUTDRV;
  381. else
  382. mode2 |= MODE2_OUTDRV;
  383. regmap_write(pca->regmap, PCA9685_MODE2, mode2);
  384. /* clear all "full off" bits */
  385. regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
  386. regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
  387. pca->chip.ops = &pca9685_pwm_ops;
  388. /* add an extra channel for ALL_LED */
  389. pca->chip.npwm = PCA9685_MAXCHAN + 1;
  390. pca->chip.dev = &client->dev;
  391. pca->chip.base = -1;
  392. ret = pwmchip_add(&pca->chip);
  393. if (ret < 0)
  394. return ret;
  395. ret = pca9685_pwm_gpio_probe(pca);
  396. if (ret < 0)
  397. pwmchip_remove(&pca->chip);
  398. return ret;
  399. }
  400. static int pca9685_pwm_remove(struct i2c_client *client)
  401. {
  402. struct pca9685 *pca = i2c_get_clientdata(client);
  403. regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
  404. MODE1_SLEEP);
  405. return pwmchip_remove(&pca->chip);
  406. }
  407. static const struct i2c_device_id pca9685_id[] = {
  408. { "pca9685", 0 },
  409. { /* sentinel */ },
  410. };
  411. MODULE_DEVICE_TABLE(i2c, pca9685_id);
  412. #ifdef CONFIG_ACPI
  413. static const struct acpi_device_id pca9685_acpi_ids[] = {
  414. { "INT3492", 0 },
  415. { /* sentinel */ },
  416. };
  417. MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
  418. #endif
  419. #ifdef CONFIG_OF
  420. static const struct of_device_id pca9685_dt_ids[] = {
  421. { .compatible = "nxp,pca9685-pwm", },
  422. { /* sentinel */ }
  423. };
  424. MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
  425. #endif
  426. static struct i2c_driver pca9685_i2c_driver = {
  427. .driver = {
  428. .name = "pca9685-pwm",
  429. .acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
  430. .of_match_table = of_match_ptr(pca9685_dt_ids),
  431. },
  432. .probe = pca9685_pwm_probe,
  433. .remove = pca9685_pwm_remove,
  434. .id_table = pca9685_id,
  435. };
  436. module_i2c_driver(pca9685_i2c_driver);
  437. MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
  438. MODULE_DESCRIPTION("PWM driver for PCA9685");
  439. MODULE_LICENSE("GPL");