leds-pca963x.c 11 KB

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