pwm-pca9685.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Driver for PCA9685 16-channel 12-bit PWM LED controller
  3. *
  4. * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
  5. *
  6. * based on the pwm-twl-led.c driver
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/i2c.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pwm.h>
  24. #include <linux/regmap.h>
  25. #include <linux/slab.h>
  26. #define PCA9685_MODE1 0x00
  27. #define PCA9685_MODE2 0x01
  28. #define PCA9685_SUBADDR1 0x02
  29. #define PCA9685_SUBADDR2 0x03
  30. #define PCA9685_SUBADDR3 0x04
  31. #define PCA9685_ALLCALLADDR 0x05
  32. #define PCA9685_LEDX_ON_L 0x06
  33. #define PCA9685_LEDX_ON_H 0x07
  34. #define PCA9685_LEDX_OFF_L 0x08
  35. #define PCA9685_LEDX_OFF_H 0x09
  36. #define PCA9685_ALL_LED_ON_L 0xFA
  37. #define PCA9685_ALL_LED_ON_H 0xFB
  38. #define PCA9685_ALL_LED_OFF_L 0xFC
  39. #define PCA9685_ALL_LED_OFF_H 0xFD
  40. #define PCA9685_PRESCALE 0xFE
  41. #define PCA9685_NUMREGS 0xFF
  42. #define PCA9685_MAXCHAN 0x10
  43. #define LED_FULL (1 << 4)
  44. #define MODE1_SLEEP (1 << 4)
  45. #define MODE2_INVRT (1 << 4)
  46. #define MODE2_OUTDRV (1 << 2)
  47. #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
  48. #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
  49. #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
  50. #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
  51. struct pca9685 {
  52. struct pwm_chip chip;
  53. struct regmap *regmap;
  54. int active_cnt;
  55. };
  56. static inline struct pca9685 *to_pca(struct pwm_chip *chip)
  57. {
  58. return container_of(chip, struct pca9685, chip);
  59. }
  60. static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  61. int duty_ns, int period_ns)
  62. {
  63. struct pca9685 *pca = to_pca(chip);
  64. unsigned long long duty;
  65. unsigned int reg;
  66. if (duty_ns < 1) {
  67. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  68. reg = PCA9685_ALL_LED_OFF_H;
  69. else
  70. reg = LED_N_OFF_H(pwm->hwpwm);
  71. regmap_write(pca->regmap, reg, LED_FULL);
  72. return 0;
  73. }
  74. if (duty_ns == period_ns) {
  75. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  76. reg = PCA9685_ALL_LED_ON_H;
  77. else
  78. reg = LED_N_ON_H(pwm->hwpwm);
  79. regmap_write(pca->regmap, reg, LED_FULL);
  80. return 0;
  81. }
  82. duty = 4096 * (unsigned long long)duty_ns;
  83. duty = DIV_ROUND_UP_ULL(duty, period_ns);
  84. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  85. reg = PCA9685_ALL_LED_OFF_L;
  86. else
  87. reg = LED_N_OFF_L(pwm->hwpwm);
  88. regmap_write(pca->regmap, reg, (int)duty & 0xff);
  89. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  90. reg = PCA9685_ALL_LED_OFF_H;
  91. else
  92. reg = LED_N_OFF_H(pwm->hwpwm);
  93. regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
  94. return 0;
  95. }
  96. static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  97. {
  98. struct pca9685 *pca = to_pca(chip);
  99. unsigned int reg;
  100. /*
  101. * The PWM subsystem does not support a pre-delay.
  102. * So, set the ON-timeout to 0
  103. */
  104. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  105. reg = PCA9685_ALL_LED_ON_L;
  106. else
  107. reg = LED_N_ON_L(pwm->hwpwm);
  108. regmap_write(pca->regmap, reg, 0);
  109. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  110. reg = PCA9685_ALL_LED_ON_H;
  111. else
  112. reg = LED_N_ON_H(pwm->hwpwm);
  113. regmap_write(pca->regmap, reg, 0);
  114. /*
  115. * Clear the full-off bit.
  116. * It has precedence over the others and must be off.
  117. */
  118. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  119. reg = PCA9685_ALL_LED_OFF_H;
  120. else
  121. reg = LED_N_OFF_H(pwm->hwpwm);
  122. regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
  123. return 0;
  124. }
  125. static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  126. {
  127. struct pca9685 *pca = to_pca(chip);
  128. unsigned int reg;
  129. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  130. reg = PCA9685_ALL_LED_OFF_H;
  131. else
  132. reg = LED_N_OFF_H(pwm->hwpwm);
  133. regmap_write(pca->regmap, reg, LED_FULL);
  134. /* Clear the LED_OFF counter. */
  135. if (pwm->hwpwm >= PCA9685_MAXCHAN)
  136. reg = PCA9685_ALL_LED_OFF_L;
  137. else
  138. reg = LED_N_OFF_L(pwm->hwpwm);
  139. regmap_write(pca->regmap, reg, 0x0);
  140. }
  141. static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  142. {
  143. struct pca9685 *pca = to_pca(chip);
  144. if (pca->active_cnt++ == 0)
  145. return regmap_update_bits(pca->regmap, PCA9685_MODE1,
  146. MODE1_SLEEP, 0x0);
  147. return 0;
  148. }
  149. static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  150. {
  151. struct pca9685 *pca = to_pca(chip);
  152. if (--pca->active_cnt == 0)
  153. regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
  154. MODE1_SLEEP);
  155. }
  156. static const struct pwm_ops pca9685_pwm_ops = {
  157. .enable = pca9685_pwm_enable,
  158. .disable = pca9685_pwm_disable,
  159. .config = pca9685_pwm_config,
  160. .request = pca9685_pwm_request,
  161. .free = pca9685_pwm_free,
  162. .owner = THIS_MODULE,
  163. };
  164. static struct regmap_config pca9685_regmap_i2c_config = {
  165. .reg_bits = 8,
  166. .val_bits = 8,
  167. .max_register = PCA9685_NUMREGS,
  168. .cache_type = REGCACHE_NONE,
  169. };
  170. static int pca9685_pwm_probe(struct i2c_client *client,
  171. const struct i2c_device_id *id)
  172. {
  173. struct device_node *np = client->dev.of_node;
  174. struct pca9685 *pca;
  175. int ret;
  176. int mode2;
  177. pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
  178. if (!pca)
  179. return -ENOMEM;
  180. pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
  181. if (IS_ERR(pca->regmap)) {
  182. ret = PTR_ERR(pca->regmap);
  183. dev_err(&client->dev, "Failed to initialize register map: %d\n",
  184. ret);
  185. return ret;
  186. }
  187. i2c_set_clientdata(client, pca);
  188. regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
  189. if (of_property_read_bool(np, "invert"))
  190. mode2 |= MODE2_INVRT;
  191. else
  192. mode2 &= ~MODE2_INVRT;
  193. if (of_property_read_bool(np, "open-drain"))
  194. mode2 &= ~MODE2_OUTDRV;
  195. else
  196. mode2 |= MODE2_OUTDRV;
  197. regmap_write(pca->regmap, PCA9685_MODE2, mode2);
  198. /* clear all "full off" bits */
  199. regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
  200. regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
  201. pca->chip.ops = &pca9685_pwm_ops;
  202. /* add an extra channel for ALL_LED */
  203. pca->chip.npwm = PCA9685_MAXCHAN + 1;
  204. pca->chip.dev = &client->dev;
  205. pca->chip.base = -1;
  206. pca->chip.can_sleep = true;
  207. return pwmchip_add(&pca->chip);
  208. }
  209. static int pca9685_pwm_remove(struct i2c_client *client)
  210. {
  211. struct pca9685 *pca = i2c_get_clientdata(client);
  212. regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
  213. MODE1_SLEEP);
  214. return pwmchip_remove(&pca->chip);
  215. }
  216. static const struct i2c_device_id pca9685_id[] = {
  217. { "pca9685", 0 },
  218. { /* sentinel */ },
  219. };
  220. MODULE_DEVICE_TABLE(i2c, pca9685_id);
  221. static const struct of_device_id pca9685_dt_ids[] = {
  222. { .compatible = "nxp,pca9685-pwm", },
  223. { /* sentinel */ }
  224. };
  225. MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
  226. static struct i2c_driver pca9685_i2c_driver = {
  227. .driver = {
  228. .name = "pca9685-pwm",
  229. .owner = THIS_MODULE,
  230. .of_match_table = pca9685_dt_ids,
  231. },
  232. .probe = pca9685_pwm_probe,
  233. .remove = pca9685_pwm_remove,
  234. .id_table = pca9685_id,
  235. };
  236. module_i2c_driver(pca9685_i2c_driver);
  237. MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
  238. MODULE_DESCRIPTION("PWM driver for PCA9685");
  239. MODULE_LICENSE("GPL");