pwm-pca9685.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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/i2c.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/property.h>
  26. #include <linux/pwm.h>
  27. #include <linux/regmap.h>
  28. #include <linux/slab.h>
  29. #include <linux/delay.h>
  30. /*
  31. * Because the PCA9685 has only one prescaler per chip, changing the period of
  32. * one channel affects the period of all 16 PWM outputs!
  33. * However, the ratio between each configured duty cycle and the chip-wide
  34. * period remains constant, because the OFF time is set in proportion to the
  35. * counter range.
  36. */
  37. #define PCA9685_MODE1 0x00
  38. #define PCA9685_MODE2 0x01
  39. #define PCA9685_SUBADDR1 0x02
  40. #define PCA9685_SUBADDR2 0x03
  41. #define PCA9685_SUBADDR3 0x04
  42. #define PCA9685_ALLCALLADDR 0x05
  43. #define PCA9685_LEDX_ON_L 0x06
  44. #define PCA9685_LEDX_ON_H 0x07
  45. #define PCA9685_LEDX_OFF_L 0x08
  46. #define PCA9685_LEDX_OFF_H 0x09
  47. #define PCA9685_ALL_LED_ON_L 0xFA
  48. #define PCA9685_ALL_LED_ON_H 0xFB
  49. #define PCA9685_ALL_LED_OFF_L 0xFC
  50. #define PCA9685_ALL_LED_OFF_H 0xFD
  51. #define PCA9685_PRESCALE 0xFE
  52. #define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */
  53. #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */
  54. #define PCA9685_COUNTER_RANGE 4096
  55. #define PCA9685_DEFAULT_PERIOD 5000000 /* Default period_ns = 1/200 Hz */
  56. #define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */
  57. #define PCA9685_NUMREGS 0xFF
  58. #define PCA9685_MAXCHAN 0x10
  59. #define LED_FULL (1 << 4)
  60. #define MODE1_RESTART (1 << 7)
  61. #define MODE1_SLEEP (1 << 4)
  62. #define MODE2_INVRT (1 << 4)
  63. #define MODE2_OUTDRV (1 << 2)
  64. #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
  65. #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
  66. #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
  67. #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
  68. struct pca9685 {
  69. struct pwm_chip chip;
  70. struct regmap *regmap;
  71. int active_cnt;
  72. int duty_ns;
  73. int period_ns;
  74. };
  75. static inline struct pca9685 *to_pca(struct pwm_chip *chip)
  76. {
  77. return container_of(chip, struct pca9685, chip);
  78. }
  79. static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  80. int duty_ns, int period_ns)
  81. {
  82. struct pca9685 *pca = to_pca(chip);
  83. unsigned long long duty;
  84. unsigned int reg;
  85. int prescale;
  86. if (period_ns != pca->period_ns) {
  87. prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns,
  88. PCA9685_COUNTER_RANGE * 1000) - 1;
  89. if (prescale >= PCA9685_PRESCALE_MIN &&
  90. prescale <= PCA9685_PRESCALE_MAX) {
  91. /* Put chip into sleep mode */
  92. regmap_update_bits(pca->regmap, PCA9685_MODE1,
  93. MODE1_SLEEP, MODE1_SLEEP);
  94. /* Change the chip-wide output frequency */
  95. regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
  96. /* Wake the chip up */
  97. regmap_update_bits(pca->regmap, PCA9685_MODE1,
  98. MODE1_SLEEP, 0x0);
  99. /* Wait 500us for the oscillator to be back up */
  100. udelay(500);
  101. pca->period_ns = period_ns;
  102. /*
  103. * If the duty cycle did not change, restart PWM with
  104. * the same duty cycle to period ratio and return.
  105. */
  106. if (duty_ns == pca->duty_ns) {
  107. regmap_update_bits(pca->regmap, PCA9685_MODE1,
  108. MODE1_RESTART, 0x1);
  109. return 0;
  110. }
  111. } else {
  112. dev_err(chip->dev,
  113. "prescaler not set: period out of bounds!\n");
  114. return -EINVAL;
  115. }
  116. }
  117. pca->duty_ns = duty_ns;
  118. if (duty_ns < 1) {
  119. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  120. reg = PCA9685_ALL_LED_OFF_H;
  121. else
  122. reg = LED_N_OFF_H(pwm->hwpwm);
  123. regmap_write(pca->regmap, reg, LED_FULL);
  124. return 0;
  125. }
  126. if (duty_ns == period_ns) {
  127. /* Clear both OFF registers */
  128. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  129. reg = PCA9685_ALL_LED_OFF_L;
  130. else
  131. reg = LED_N_OFF_L(pwm->hwpwm);
  132. regmap_write(pca->regmap, reg, 0x0);
  133. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  134. reg = PCA9685_ALL_LED_OFF_H;
  135. else
  136. reg = LED_N_OFF_H(pwm->hwpwm);
  137. regmap_write(pca->regmap, reg, 0x0);
  138. /* Set the full ON bit */
  139. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  140. reg = PCA9685_ALL_LED_ON_H;
  141. else
  142. reg = LED_N_ON_H(pwm->hwpwm);
  143. regmap_write(pca->regmap, reg, LED_FULL);
  144. return 0;
  145. }
  146. duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
  147. duty = DIV_ROUND_UP_ULL(duty, period_ns);
  148. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  149. reg = PCA9685_ALL_LED_OFF_L;
  150. else
  151. reg = LED_N_OFF_L(pwm->hwpwm);
  152. regmap_write(pca->regmap, reg, (int)duty & 0xff);
  153. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  154. reg = PCA9685_ALL_LED_OFF_H;
  155. else
  156. reg = LED_N_OFF_H(pwm->hwpwm);
  157. regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
  158. /* Clear the full ON bit, otherwise the set OFF time has no effect */
  159. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  160. reg = PCA9685_ALL_LED_ON_H;
  161. else
  162. reg = LED_N_ON_H(pwm->hwpwm);
  163. regmap_write(pca->regmap, reg, 0);
  164. return 0;
  165. }
  166. static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  167. {
  168. struct pca9685 *pca = to_pca(chip);
  169. unsigned int reg;
  170. /*
  171. * The PWM subsystem does not support a pre-delay.
  172. * So, set the ON-timeout to 0
  173. */
  174. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  175. reg = PCA9685_ALL_LED_ON_L;
  176. else
  177. reg = LED_N_ON_L(pwm->hwpwm);
  178. regmap_write(pca->regmap, reg, 0);
  179. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  180. reg = PCA9685_ALL_LED_ON_H;
  181. else
  182. reg = LED_N_ON_H(pwm->hwpwm);
  183. regmap_write(pca->regmap, reg, 0);
  184. /*
  185. * Clear the full-off bit.
  186. * It has precedence over the others and must be off.
  187. */
  188. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  189. reg = PCA9685_ALL_LED_OFF_H;
  190. else
  191. reg = LED_N_OFF_H(pwm->hwpwm);
  192. regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
  193. return 0;
  194. }
  195. static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  196. {
  197. struct pca9685 *pca = to_pca(chip);
  198. unsigned int reg;
  199. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  200. reg = PCA9685_ALL_LED_OFF_H;
  201. else
  202. reg = LED_N_OFF_H(pwm->hwpwm);
  203. regmap_write(pca->regmap, reg, LED_FULL);
  204. /* Clear the LED_OFF counter. */
  205. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  206. reg = PCA9685_ALL_LED_OFF_L;
  207. else
  208. reg = LED_N_OFF_L(pwm->hwpwm);
  209. regmap_write(pca->regmap, reg, 0x0);
  210. }
  211. static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  212. {
  213. struct pca9685 *pca = to_pca(chip);
  214. if (pca->active_cnt++ == 0)
  215. return regmap_update_bits(pca->regmap, PCA9685_MODE1,
  216. MODE1_SLEEP, 0x0);
  217. return 0;
  218. }
  219. static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  220. {
  221. struct pca9685 *pca = to_pca(chip);
  222. if (--pca->active_cnt == 0)
  223. regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
  224. MODE1_SLEEP);
  225. }
  226. static const struct pwm_ops pca9685_pwm_ops = {
  227. .enable = pca9685_pwm_enable,
  228. .disable = pca9685_pwm_disable,
  229. .config = pca9685_pwm_config,
  230. .request = pca9685_pwm_request,
  231. .free = pca9685_pwm_free,
  232. .owner = THIS_MODULE,
  233. };
  234. static const struct regmap_config pca9685_regmap_i2c_config = {
  235. .reg_bits = 8,
  236. .val_bits = 8,
  237. .max_register = PCA9685_NUMREGS,
  238. .cache_type = REGCACHE_NONE,
  239. };
  240. static int pca9685_pwm_probe(struct i2c_client *client,
  241. const struct i2c_device_id *id)
  242. {
  243. struct pca9685 *pca;
  244. int ret;
  245. int mode2;
  246. pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
  247. if (!pca)
  248. return -ENOMEM;
  249. pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
  250. if (IS_ERR(pca->regmap)) {
  251. ret = PTR_ERR(pca->regmap);
  252. dev_err(&client->dev, "Failed to initialize register map: %d\n",
  253. ret);
  254. return ret;
  255. }
  256. pca->duty_ns = 0;
  257. pca->period_ns = PCA9685_DEFAULT_PERIOD;
  258. i2c_set_clientdata(client, pca);
  259. regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
  260. if (device_property_read_bool(&client->dev, "invert"))
  261. mode2 |= MODE2_INVRT;
  262. else
  263. mode2 &= ~MODE2_INVRT;
  264. if (device_property_read_bool(&client->dev, "open-drain"))
  265. mode2 &= ~MODE2_OUTDRV;
  266. else
  267. mode2 |= MODE2_OUTDRV;
  268. regmap_write(pca->regmap, PCA9685_MODE2, mode2);
  269. /* clear all "full off" bits */
  270. regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
  271. regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
  272. pca->chip.ops = &pca9685_pwm_ops;
  273. /* add an extra channel for ALL_LED */
  274. pca->chip.npwm = PCA9685_MAXCHAN + 1;
  275. pca->chip.dev = &client->dev;
  276. pca->chip.base = -1;
  277. pca->chip.can_sleep = true;
  278. return pwmchip_add(&pca->chip);
  279. }
  280. static int pca9685_pwm_remove(struct i2c_client *client)
  281. {
  282. struct pca9685 *pca = i2c_get_clientdata(client);
  283. regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
  284. MODE1_SLEEP);
  285. return pwmchip_remove(&pca->chip);
  286. }
  287. static const struct i2c_device_id pca9685_id[] = {
  288. { "pca9685", 0 },
  289. { /* sentinel */ },
  290. };
  291. MODULE_DEVICE_TABLE(i2c, pca9685_id);
  292. #ifdef CONFIG_ACPI
  293. static const struct acpi_device_id pca9685_acpi_ids[] = {
  294. { "INT3492", 0 },
  295. { /* sentinel */ },
  296. };
  297. MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
  298. #endif
  299. #ifdef CONFIG_OF
  300. static const struct of_device_id pca9685_dt_ids[] = {
  301. { .compatible = "nxp,pca9685-pwm", },
  302. { /* sentinel */ }
  303. };
  304. MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
  305. #endif
  306. static struct i2c_driver pca9685_i2c_driver = {
  307. .driver = {
  308. .name = "pca9685-pwm",
  309. .acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
  310. .of_match_table = of_match_ptr(pca9685_dt_ids),
  311. },
  312. .probe = pca9685_pwm_probe,
  313. .remove = pca9685_pwm_remove,
  314. .id_table = pca9685_id,
  315. };
  316. module_i2c_driver(pca9685_i2c_driver);
  317. MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
  318. MODULE_DESCRIPTION("PWM driver for PCA9685");
  319. MODULE_LICENSE("GPL");