pwm-pca9685.c 9.5 KB

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