leds-pca963x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 <[email protected]>
  7. * Author: Ricardo Ribalda <[email protected]>
  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_MODE2_OUTDRV 0x04 /* Open-drain or totem pole */
  40. #define PCA963X_MODE2_INVRT 0x10 /* Normal or inverted direction */
  41. #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */
  42. #define PCA963X_MODE1 0x00
  43. #define PCA963X_MODE2 0x01
  44. #define PCA963X_PWM_BASE 0x02
  45. enum pca963x_type {
  46. pca9633,
  47. pca9634,
  48. pca9635,
  49. };
  50. struct pca963x_chipdef {
  51. u8 grppwm;
  52. u8 grpfreq;
  53. u8 ledout_base;
  54. int n_leds;
  55. unsigned int scaling;
  56. };
  57. static struct pca963x_chipdef pca963x_chipdefs[] = {
  58. [pca9633] = {
  59. .grppwm = 0x6,
  60. .grpfreq = 0x7,
  61. .ledout_base = 0x8,
  62. .n_leds = 4,
  63. },
  64. [pca9634] = {
  65. .grppwm = 0xa,
  66. .grpfreq = 0xb,
  67. .ledout_base = 0xc,
  68. .n_leds = 8,
  69. },
  70. [pca9635] = {
  71. .grppwm = 0x12,
  72. .grpfreq = 0x13,
  73. .ledout_base = 0x14,
  74. .n_leds = 16,
  75. },
  76. };
  77. /* Total blink period in milliseconds */
  78. #define PCA963X_BLINK_PERIOD_MIN 42
  79. #define PCA963X_BLINK_PERIOD_MAX 10667
  80. static const struct i2c_device_id pca963x_id[] = {
  81. { "pca9632", pca9633 },
  82. { "pca9633", pca9633 },
  83. { "pca9634", pca9634 },
  84. { "pca9635", pca9635 },
  85. { }
  86. };
  87. MODULE_DEVICE_TABLE(i2c, pca963x_id);
  88. struct pca963x;
  89. struct pca963x_led {
  90. struct pca963x *chip;
  91. struct led_classdev led_cdev;
  92. int led_num; /* 0 .. 15 potentially */
  93. bool blinking;
  94. u8 gdc;
  95. u8 gfrq;
  96. };
  97. struct pca963x {
  98. struct pca963x_chipdef *chipdef;
  99. struct mutex mutex;
  100. struct i2c_client *client;
  101. unsigned long leds_on;
  102. struct pca963x_led leds[];
  103. };
  104. static int pca963x_brightness(struct pca963x_led *led,
  105. enum led_brightness brightness)
  106. {
  107. struct i2c_client *client = led->chip->client;
  108. struct pca963x_chipdef *chipdef = led->chip->chipdef;
  109. u8 ledout_addr, ledout, mask, val;
  110. int shift;
  111. int ret;
  112. ledout_addr = chipdef->ledout_base + (led->led_num / 4);
  113. shift = 2 * (led->led_num % 4);
  114. mask = 0x3 << shift;
  115. ledout = i2c_smbus_read_byte_data(client, ledout_addr);
  116. switch (brightness) {
  117. case LED_FULL:
  118. if (led->blinking) {
  119. val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
  120. ret = i2c_smbus_write_byte_data(client,
  121. PCA963X_PWM_BASE +
  122. led->led_num,
  123. LED_FULL);
  124. } else {
  125. val = (ledout & ~mask) | (PCA963X_LED_ON << shift);
  126. }
  127. ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
  128. break;
  129. case LED_OFF:
  130. val = ledout & ~mask;
  131. ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
  132. led->blinking = false;
  133. break;
  134. default:
  135. ret = i2c_smbus_write_byte_data(client,
  136. PCA963X_PWM_BASE +
  137. led->led_num,
  138. brightness);
  139. if (ret < 0)
  140. return ret;
  141. if (led->blinking)
  142. val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
  143. else
  144. val = (ledout & ~mask) | (PCA963X_LED_PWM << shift);
  145. ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
  146. break;
  147. }
  148. return ret;
  149. }
  150. static void pca963x_blink(struct pca963x_led *led)
  151. {
  152. struct i2c_client *client = led->chip->client;
  153. struct pca963x_chipdef *chipdef = led->chip->chipdef;
  154. u8 ledout_addr, ledout, mask, val, mode2;
  155. int shift;
  156. ledout_addr = chipdef->ledout_base + (led->led_num / 4);
  157. shift = 2 * (led->led_num % 4);
  158. mask = 0x3 << shift;
  159. mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
  160. i2c_smbus_write_byte_data(client, chipdef->grppwm, led->gdc);
  161. i2c_smbus_write_byte_data(client, chipdef->grpfreq, led->gfrq);
  162. if (!(mode2 & PCA963X_MODE2_DMBLNK))
  163. i2c_smbus_write_byte_data(client, PCA963X_MODE2,
  164. mode2 | PCA963X_MODE2_DMBLNK);
  165. mutex_lock(&led->chip->mutex);
  166. ledout = i2c_smbus_read_byte_data(client, ledout_addr);
  167. if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) {
  168. val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
  169. i2c_smbus_write_byte_data(client, ledout_addr, val);
  170. }
  171. mutex_unlock(&led->chip->mutex);
  172. led->blinking = true;
  173. }
  174. static int pca963x_power_state(struct pca963x_led *led)
  175. {
  176. struct i2c_client *client = led->chip->client;
  177. unsigned long *leds_on = &led->chip->leds_on;
  178. unsigned long cached_leds = *leds_on;
  179. if (led->led_cdev.brightness)
  180. set_bit(led->led_num, leds_on);
  181. else
  182. clear_bit(led->led_num, leds_on);
  183. if (!(*leds_on) != !cached_leds)
  184. return i2c_smbus_write_byte_data(client, PCA963X_MODE1,
  185. *leds_on ? 0 : BIT(4));
  186. return 0;
  187. }
  188. static int pca963x_led_set(struct led_classdev *led_cdev,
  189. enum led_brightness value)
  190. {
  191. struct pca963x_led *led;
  192. int ret;
  193. led = container_of(led_cdev, struct pca963x_led, led_cdev);
  194. mutex_lock(&led->chip->mutex);
  195. ret = pca963x_brightness(led, value);
  196. if (ret < 0)
  197. goto unlock;
  198. ret = pca963x_power_state(led);
  199. unlock:
  200. mutex_unlock(&led->chip->mutex);
  201. return ret;
  202. }
  203. static unsigned int pca963x_period_scale(struct pca963x_led *led,
  204. unsigned int val)
  205. {
  206. unsigned int scaling = led->chip->chipdef->scaling;
  207. return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
  208. }
  209. static int pca963x_blink_set(struct led_classdev *led_cdev,
  210. unsigned long *delay_on, unsigned long *delay_off)
  211. {
  212. unsigned long time_on, time_off, period;
  213. struct pca963x_led *led;
  214. u8 gdc, gfrq;
  215. led = container_of(led_cdev, struct pca963x_led, led_cdev);
  216. time_on = *delay_on;
  217. time_off = *delay_off;
  218. /* If both zero, pick reasonable defaults of 500ms each */
  219. if (!time_on && !time_off) {
  220. time_on = 500;
  221. time_off = 500;
  222. }
  223. period = pca963x_period_scale(led, time_on + time_off);
  224. /* If period not supported by hardware, default to someting sane. */
  225. if ((period < PCA963X_BLINK_PERIOD_MIN) ||
  226. (period > PCA963X_BLINK_PERIOD_MAX)) {
  227. time_on = 500;
  228. time_off = 500;
  229. period = pca963x_period_scale(led, 1000);
  230. }
  231. /*
  232. * From manual: duty cycle = (GDC / 256) ->
  233. * (time_on / period) = (GDC / 256) ->
  234. * GDC = ((time_on * 256) / period)
  235. */
  236. gdc = (pca963x_period_scale(led, time_on) * 256) / period;
  237. /*
  238. * From manual: period = ((GFRQ + 1) / 24) in seconds.
  239. * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
  240. * GFRQ = ((period * 24 / 1000) - 1)
  241. */
  242. gfrq = (period * 24 / 1000) - 1;
  243. led->gdc = gdc;
  244. led->gfrq = gfrq;
  245. pca963x_blink(led);
  246. led->led_cdev.brightness = LED_FULL;
  247. pca963x_led_set(led_cdev, LED_FULL);
  248. *delay_on = time_on;
  249. *delay_off = time_off;
  250. return 0;
  251. }
  252. static int pca963x_register_leds(struct i2c_client *client,
  253. struct pca963x *chip)
  254. {
  255. struct pca963x_chipdef *chipdef = chip->chipdef;
  256. struct pca963x_led *led = chip->leds;
  257. struct device *dev = &client->dev;
  258. struct fwnode_handle *child;
  259. bool hw_blink;
  260. s32 mode2;
  261. u32 reg;
  262. int ret;
  263. if (device_property_read_u32(dev, "nxp,period-scale",
  264. &chipdef->scaling))
  265. chipdef->scaling = 1000;
  266. hw_blink = device_property_read_bool(dev, "nxp,hw-blink");
  267. mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
  268. if (mode2 < 0)
  269. return mode2;
  270. /* default to open-drain unless totem pole (push-pull) is specified */
  271. if (device_property_read_bool(dev, "nxp,totem-pole"))
  272. mode2 |= PCA963X_MODE2_OUTDRV;
  273. else
  274. mode2 &= ~PCA963X_MODE2_OUTDRV;
  275. /* default to non-inverted output, unless inverted is specified */
  276. if (device_property_read_bool(dev, "nxp,inverted-out"))
  277. mode2 |= PCA963X_MODE2_INVRT;
  278. else
  279. mode2 &= ~PCA963X_MODE2_INVRT;
  280. ret = i2c_smbus_write_byte_data(client, PCA963X_MODE2, mode2);
  281. if (ret < 0)
  282. return ret;
  283. device_for_each_child_node(dev, child) {
  284. struct led_init_data init_data = {};
  285. char default_label[32];
  286. ret = fwnode_property_read_u32(child, "reg", &reg);
  287. if (ret || reg >= chipdef->n_leds) {
  288. dev_err(dev, "Invalid 'reg' property for node %pfw\n",
  289. child);
  290. ret = -EINVAL;
  291. goto err;
  292. }
  293. led->led_num = reg;
  294. led->chip = chip;
  295. led->led_cdev.brightness_set_blocking = pca963x_led_set;
  296. if (hw_blink)
  297. led->led_cdev.blink_set = pca963x_blink_set;
  298. led->blinking = false;
  299. init_data.fwnode = child;
  300. /* for backwards compatibility */
  301. init_data.devicename = "pca963x";
  302. snprintf(default_label, sizeof(default_label), "%d:%.2x:%u",
  303. client->adapter->nr, client->addr, reg);
  304. init_data.default_label = default_label;
  305. ret = devm_led_classdev_register_ext(dev, &led->led_cdev,
  306. &init_data);
  307. if (ret) {
  308. dev_err(dev, "Failed to register LED for node %pfw\n",
  309. child);
  310. goto err;
  311. }
  312. ++led;
  313. }
  314. return 0;
  315. err:
  316. fwnode_handle_put(child);
  317. return ret;
  318. }
  319. static const struct of_device_id of_pca963x_match[] = {
  320. { .compatible = "nxp,pca9632", },
  321. { .compatible = "nxp,pca9633", },
  322. { .compatible = "nxp,pca9634", },
  323. { .compatible = "nxp,pca9635", },
  324. {},
  325. };
  326. MODULE_DEVICE_TABLE(of, of_pca963x_match);
  327. static int pca963x_probe(struct i2c_client *client,
  328. const struct i2c_device_id *id)
  329. {
  330. struct device *dev = &client->dev;
  331. struct pca963x_chipdef *chipdef;
  332. struct pca963x *chip;
  333. int i, count;
  334. chipdef = &pca963x_chipdefs[id->driver_data];
  335. count = device_get_child_node_count(dev);
  336. if (!count || count > chipdef->n_leds) {
  337. dev_err(dev, "Node %pfw must define between 1 and %d LEDs\n",
  338. dev_fwnode(dev), chipdef->n_leds);
  339. return -EINVAL;
  340. }
  341. chip = devm_kzalloc(dev, struct_size(chip, leds, count), GFP_KERNEL);
  342. if (!chip)
  343. return -ENOMEM;
  344. i2c_set_clientdata(client, chip);
  345. mutex_init(&chip->mutex);
  346. chip->chipdef = chipdef;
  347. chip->client = client;
  348. /* Turn off LEDs by default*/
  349. for (i = 0; i < chipdef->n_leds / 4; i++)
  350. i2c_smbus_write_byte_data(client, chipdef->ledout_base + i, 0x00);
  351. /* Disable LED all-call address, and power down initially */
  352. i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
  353. return pca963x_register_leds(client, chip);
  354. }
  355. static struct i2c_driver pca963x_driver = {
  356. .driver = {
  357. .name = "leds-pca963x",
  358. .of_match_table = of_pca963x_match,
  359. },
  360. .probe = pca963x_probe,
  361. .id_table = pca963x_id,
  362. };
  363. module_i2c_driver(pca963x_driver);
  364. MODULE_AUTHOR("Peter Meerwald <[email protected]>");
  365. MODULE_DESCRIPTION("PCA963X LED driver");
  366. MODULE_LICENSE("GPL v2");