leds-pca963x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2011 bct electronic GmbH
  4. * Copyright 2013 Qtechnology/AS
  5. *
  6. * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
  7. * Author: Ricardo Ribalda <ribalda@kernel.org>
  8. *
  9. * Based on leds-pca955x.c
  10. *
  11. * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
  12. * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
  13. *
  14. * Note that hardware blinking violates the leds infrastructure driver
  15. * interface since the hardware only supports blinking all LEDs with the
  16. * same delay_on/delay_off rates. That is, only the LEDs that are set to
  17. * blink will actually blink but all LEDs that are set to blink will blink
  18. * in identical fashion. The delay_on/delay_off values of the last LED
  19. * that is set to blink will be used for all of the blinking LEDs.
  20. * Hardware blinking is disabled by default but can be enabled by setting
  21. * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
  22. * or by adding the 'nxp,hw-blink' property to the DTS.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/delay.h>
  26. #include <linux/string.h>
  27. #include <linux/ctype.h>
  28. #include <linux/leds.h>
  29. #include <linux/err.h>
  30. #include <linux/i2c.h>
  31. #include <linux/property.h>
  32. #include <linux/slab.h>
  33. #include <linux/of.h>
  34. /* LED select registers determine the source that drives LED outputs */
  35. #define PCA963X_LED_OFF 0x0 /* LED driver off */
  36. #define PCA963X_LED_ON 0x1 /* LED driver on */
  37. #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */
  38. #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
  39. #define PCA963X_MODE1_SLEEP 0x04 /* Normal mode or Low Power mode, oscillator off */
  40. #define PCA963X_MODE2_OUTDRV 0x04 /* Open-drain or totem pole */
  41. #define PCA963X_MODE2_INVRT 0x10 /* Normal or inverted direction */
  42. #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */
  43. #define PCA963X_MODE1 0x00
  44. #define PCA963X_MODE2 0x01
  45. #define PCA963X_PWM_BASE 0x02
  46. enum pca963x_type {
  47. pca9633,
  48. pca9634,
  49. pca9635,
  50. };
  51. struct pca963x_chipdef {
  52. u8 grppwm;
  53. u8 grpfreq;
  54. u8 ledout_base;
  55. int n_leds;
  56. unsigned int scaling;
  57. };
  58. static struct pca963x_chipdef pca963x_chipdefs[] = {
  59. [pca9633] = {
  60. .grppwm = 0x6,
  61. .grpfreq = 0x7,
  62. .ledout_base = 0x8,
  63. .n_leds = 4,
  64. },
  65. [pca9634] = {
  66. .grppwm = 0xa,
  67. .grpfreq = 0xb,
  68. .ledout_base = 0xc,
  69. .n_leds = 8,
  70. },
  71. [pca9635] = {
  72. .grppwm = 0x12,
  73. .grpfreq = 0x13,
  74. .ledout_base = 0x14,
  75. .n_leds = 16,
  76. },
  77. };
  78. /* Total blink period in milliseconds */
  79. #define PCA963X_BLINK_PERIOD_MIN 42
  80. #define PCA963X_BLINK_PERIOD_MAX 10667
  81. static const struct i2c_device_id pca963x_id[] = {
  82. { "pca9632", pca9633 },
  83. { "pca9633", pca9633 },
  84. { "pca9634", pca9634 },
  85. { "pca9635", pca9635 },
  86. { }
  87. };
  88. MODULE_DEVICE_TABLE(i2c, pca963x_id);
  89. struct pca963x;
  90. struct pca963x_led {
  91. struct pca963x *chip;
  92. struct led_classdev led_cdev;
  93. int led_num; /* 0 .. 15 potentially */
  94. bool blinking;
  95. u8 gdc;
  96. u8 gfrq;
  97. };
  98. struct pca963x {
  99. struct pca963x_chipdef *chipdef;
  100. struct mutex mutex;
  101. struct i2c_client *client;
  102. unsigned long leds_on;
  103. struct pca963x_led leds[];
  104. };
  105. static int pca963x_brightness(struct pca963x_led *led,
  106. enum led_brightness brightness)
  107. {
  108. struct i2c_client *client = led->chip->client;
  109. struct pca963x_chipdef *chipdef = led->chip->chipdef;
  110. u8 ledout_addr, ledout, mask, val;
  111. int shift;
  112. int ret;
  113. ledout_addr = chipdef->ledout_base + (led->led_num / 4);
  114. shift = 2 * (led->led_num % 4);
  115. mask = 0x3 << shift;
  116. ledout = i2c_smbus_read_byte_data(client, ledout_addr);
  117. switch (brightness) {
  118. case LED_FULL:
  119. if (led->blinking) {
  120. val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
  121. ret = i2c_smbus_write_byte_data(client,
  122. PCA963X_PWM_BASE +
  123. led->led_num,
  124. LED_FULL);
  125. } else {
  126. val = (ledout & ~mask) | (PCA963X_LED_ON << shift);
  127. }
  128. ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
  129. break;
  130. case LED_OFF:
  131. val = ledout & ~mask;
  132. ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
  133. led->blinking = false;
  134. break;
  135. default:
  136. ret = i2c_smbus_write_byte_data(client,
  137. PCA963X_PWM_BASE +
  138. led->led_num,
  139. brightness);
  140. if (ret < 0)
  141. return ret;
  142. if (led->blinking)
  143. val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
  144. else
  145. val = (ledout & ~mask) | (PCA963X_LED_PWM << shift);
  146. ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
  147. break;
  148. }
  149. return ret;
  150. }
  151. static void pca963x_blink(struct pca963x_led *led)
  152. {
  153. struct i2c_client *client = led->chip->client;
  154. struct pca963x_chipdef *chipdef = led->chip->chipdef;
  155. u8 ledout_addr, ledout, mask, val, mode2;
  156. int shift;
  157. ledout_addr = chipdef->ledout_base + (led->led_num / 4);
  158. shift = 2 * (led->led_num % 4);
  159. mask = 0x3 << shift;
  160. mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
  161. i2c_smbus_write_byte_data(client, chipdef->grppwm, led->gdc);
  162. i2c_smbus_write_byte_data(client, chipdef->grpfreq, led->gfrq);
  163. if (!(mode2 & PCA963X_MODE2_DMBLNK))
  164. i2c_smbus_write_byte_data(client, PCA963X_MODE2,
  165. mode2 | PCA963X_MODE2_DMBLNK);
  166. mutex_lock(&led->chip->mutex);
  167. ledout = i2c_smbus_read_byte_data(client, ledout_addr);
  168. if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) {
  169. val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
  170. i2c_smbus_write_byte_data(client, ledout_addr, val);
  171. }
  172. mutex_unlock(&led->chip->mutex);
  173. led->blinking = true;
  174. }
  175. static int pca963x_power_state(struct pca963x_led *led)
  176. {
  177. struct i2c_client *client = led->chip->client;
  178. unsigned long *leds_on = &led->chip->leds_on;
  179. unsigned long cached_leds = *leds_on;
  180. if (led->led_cdev.brightness)
  181. set_bit(led->led_num, leds_on);
  182. else
  183. clear_bit(led->led_num, leds_on);
  184. if (!(*leds_on) != !cached_leds)
  185. return i2c_smbus_write_byte_data(client, PCA963X_MODE1,
  186. *leds_on ? 0 : BIT(4));
  187. return 0;
  188. }
  189. static int pca963x_led_set(struct led_classdev *led_cdev,
  190. enum led_brightness value)
  191. {
  192. struct pca963x_led *led;
  193. int ret;
  194. led = container_of(led_cdev, struct pca963x_led, led_cdev);
  195. mutex_lock(&led->chip->mutex);
  196. ret = pca963x_brightness(led, value);
  197. if (ret < 0)
  198. goto unlock;
  199. ret = pca963x_power_state(led);
  200. unlock:
  201. mutex_unlock(&led->chip->mutex);
  202. return ret;
  203. }
  204. static unsigned int pca963x_period_scale(struct pca963x_led *led,
  205. unsigned int val)
  206. {
  207. unsigned int scaling = led->chip->chipdef->scaling;
  208. return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
  209. }
  210. static int pca963x_blink_set(struct led_classdev *led_cdev,
  211. unsigned long *delay_on, unsigned long *delay_off)
  212. {
  213. unsigned long time_on, time_off, period;
  214. struct pca963x_led *led;
  215. u8 gdc, gfrq;
  216. led = container_of(led_cdev, struct pca963x_led, led_cdev);
  217. time_on = *delay_on;
  218. time_off = *delay_off;
  219. /* If both zero, pick reasonable defaults of 500ms each */
  220. if (!time_on && !time_off) {
  221. time_on = 500;
  222. time_off = 500;
  223. }
  224. period = pca963x_period_scale(led, time_on + time_off);
  225. /* If period not supported by hardware, default to someting sane. */
  226. if ((period < PCA963X_BLINK_PERIOD_MIN) ||
  227. (period > PCA963X_BLINK_PERIOD_MAX)) {
  228. time_on = 500;
  229. time_off = 500;
  230. period = pca963x_period_scale(led, 1000);
  231. }
  232. /*
  233. * From manual: duty cycle = (GDC / 256) ->
  234. * (time_on / period) = (GDC / 256) ->
  235. * GDC = ((time_on * 256) / period)
  236. */
  237. gdc = (pca963x_period_scale(led, time_on) * 256) / period;
  238. /*
  239. * From manual: period = ((GFRQ + 1) / 24) in seconds.
  240. * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
  241. * GFRQ = ((period * 24 / 1000) - 1)
  242. */
  243. gfrq = (period * 24 / 1000) - 1;
  244. led->gdc = gdc;
  245. led->gfrq = gfrq;
  246. pca963x_blink(led);
  247. led->led_cdev.brightness = LED_FULL;
  248. pca963x_led_set(led_cdev, LED_FULL);
  249. *delay_on = time_on;
  250. *delay_off = time_off;
  251. return 0;
  252. }
  253. static int pca963x_register_leds(struct i2c_client *client,
  254. struct pca963x *chip)
  255. {
  256. struct pca963x_chipdef *chipdef = chip->chipdef;
  257. struct pca963x_led *led = chip->leds;
  258. struct device *dev = &client->dev;
  259. struct fwnode_handle *child;
  260. bool hw_blink;
  261. s32 mode2;
  262. u32 reg;
  263. int ret;
  264. if (device_property_read_u32(dev, "nxp,period-scale",
  265. &chipdef->scaling))
  266. chipdef->scaling = 1000;
  267. hw_blink = device_property_read_bool(dev, "nxp,hw-blink");
  268. mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
  269. if (mode2 < 0)
  270. return mode2;
  271. /* default to open-drain unless totem pole (push-pull) is specified */
  272. if (device_property_read_bool(dev, "nxp,totem-pole"))
  273. mode2 |= PCA963X_MODE2_OUTDRV;
  274. else
  275. mode2 &= ~PCA963X_MODE2_OUTDRV;
  276. /* default to non-inverted output, unless inverted is specified */
  277. if (device_property_read_bool(dev, "nxp,inverted-out"))
  278. mode2 |= PCA963X_MODE2_INVRT;
  279. else
  280. mode2 &= ~PCA963X_MODE2_INVRT;
  281. ret = i2c_smbus_write_byte_data(client, PCA963X_MODE2, mode2);
  282. if (ret < 0)
  283. return ret;
  284. device_for_each_child_node(dev, child) {
  285. struct led_init_data init_data = {};
  286. char default_label[32];
  287. ret = fwnode_property_read_u32(child, "reg", &reg);
  288. if (ret || reg >= chipdef->n_leds) {
  289. dev_err(dev, "Invalid 'reg' property for node %pfw\n",
  290. child);
  291. ret = -EINVAL;
  292. goto err;
  293. }
  294. led->led_num = reg;
  295. led->chip = chip;
  296. led->led_cdev.brightness_set_blocking = pca963x_led_set;
  297. if (hw_blink)
  298. led->led_cdev.blink_set = pca963x_blink_set;
  299. led->blinking = false;
  300. init_data.fwnode = child;
  301. /* for backwards compatibility */
  302. init_data.devicename = "pca963x";
  303. snprintf(default_label, sizeof(default_label), "%d:%.2x:%u",
  304. client->adapter->nr, client->addr, reg);
  305. init_data.default_label = default_label;
  306. ret = devm_led_classdev_register_ext(dev, &led->led_cdev,
  307. &init_data);
  308. if (ret) {
  309. dev_err(dev, "Failed to register LED for node %pfw\n",
  310. child);
  311. goto err;
  312. }
  313. ++led;
  314. }
  315. return 0;
  316. err:
  317. fwnode_handle_put(child);
  318. return ret;
  319. }
  320. static int pca963x_suspend(struct device *dev)
  321. {
  322. struct pca963x *chip = dev_get_drvdata(dev);
  323. u8 reg;
  324. reg = i2c_smbus_read_byte_data(chip->client, PCA963X_MODE1);
  325. reg = reg | BIT(PCA963X_MODE1_SLEEP);
  326. i2c_smbus_write_byte_data(chip->client, PCA963X_MODE1, reg);
  327. return 0;
  328. }
  329. static int pca963x_resume(struct device *dev)
  330. {
  331. struct pca963x *chip = dev_get_drvdata(dev);
  332. u8 reg;
  333. reg = i2c_smbus_read_byte_data(chip->client, PCA963X_MODE1);
  334. reg = reg & ~BIT(PCA963X_MODE1_SLEEP);
  335. i2c_smbus_write_byte_data(chip->client, PCA963X_MODE1, reg);
  336. return 0;
  337. }
  338. static DEFINE_SIMPLE_DEV_PM_OPS(pca963x_pm, pca963x_suspend, pca963x_resume);
  339. static const struct of_device_id of_pca963x_match[] = {
  340. { .compatible = "nxp,pca9632", },
  341. { .compatible = "nxp,pca9633", },
  342. { .compatible = "nxp,pca9634", },
  343. { .compatible = "nxp,pca9635", },
  344. {},
  345. };
  346. MODULE_DEVICE_TABLE(of, of_pca963x_match);
  347. static int pca963x_probe(struct i2c_client *client)
  348. {
  349. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  350. struct device *dev = &client->dev;
  351. struct pca963x_chipdef *chipdef;
  352. struct pca963x *chip;
  353. int i, count;
  354. chipdef = &pca963x_chipdefs[id->driver_data];
  355. count = device_get_child_node_count(dev);
  356. if (!count || count > chipdef->n_leds) {
  357. dev_err(dev, "Node %pfw must define between 1 and %d LEDs\n",
  358. dev_fwnode(dev), chipdef->n_leds);
  359. return -EINVAL;
  360. }
  361. chip = devm_kzalloc(dev, struct_size(chip, leds, count), GFP_KERNEL);
  362. if (!chip)
  363. return -ENOMEM;
  364. i2c_set_clientdata(client, chip);
  365. mutex_init(&chip->mutex);
  366. chip->chipdef = chipdef;
  367. chip->client = client;
  368. /* Turn off LEDs by default*/
  369. for (i = 0; i < chipdef->n_leds / 4; i++)
  370. i2c_smbus_write_byte_data(client, chipdef->ledout_base + i, 0x00);
  371. /* Disable LED all-call address, and power down initially */
  372. i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
  373. return pca963x_register_leds(client, chip);
  374. }
  375. static struct i2c_driver pca963x_driver = {
  376. .driver = {
  377. .name = "leds-pca963x",
  378. .of_match_table = of_pca963x_match,
  379. .pm = pm_sleep_ptr(&pca963x_pm)
  380. },
  381. .probe = pca963x_probe,
  382. .id_table = pca963x_id,
  383. };
  384. module_i2c_driver(pca963x_driver);
  385. MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
  386. MODULE_DESCRIPTION("PCA963X LED driver");
  387. MODULE_LICENSE("GPL v2");