pwm-pca9685.c 9.5 KB

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