pwm-pca9685.c 14 KB

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