leds-pca963x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright 2011 bct electronic GmbH
  3. * Copyright 2013 Qtechnology/AS
  4. *
  5. * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
  6. * Author: Ricardo Ribalda <ricardo.ribalda@gmail.com>
  7. *
  8. * Based on leds-pca955x.c
  9. *
  10. * This file is subject to the terms and conditions of version 2 of
  11. * the GNU General Public License. See the file COPYING in the main
  12. * directory of this archive for more details.
  13. *
  14. * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
  15. * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
  16. *
  17. * Note that hardware blinking violates the leds infrastructure driver
  18. * interface since the hardware only supports blinking all LEDs with the
  19. * same delay_on/delay_off rates. That is, only the LEDs that are set to
  20. * blink will actually blink but all LEDs that are set to blink will blink
  21. * in identical fashion. The delay_on/delay_off values of the last LED
  22. * that is set to blink will be used for all of the blinking LEDs.
  23. * Hardware blinking is disabled by default but can be enabled by setting
  24. * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
  25. * or by adding the 'nxp,hw-blink' property to the DTS.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/delay.h>
  29. #include <linux/string.h>
  30. #include <linux/ctype.h>
  31. #include <linux/leds.h>
  32. #include <linux/err.h>
  33. #include <linux/i2c.h>
  34. #include <linux/slab.h>
  35. #include <linux/of.h>
  36. #include <linux/platform_data/leds-pca963x.h>
  37. /* LED select registers determine the source that drives LED outputs */
  38. #define PCA963X_LED_OFF 0x0 /* LED driver off */
  39. #define PCA963X_LED_ON 0x1 /* LED driver on */
  40. #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */
  41. #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
  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. };
  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_led;
  89. struct pca963x {
  90. struct pca963x_chipdef *chipdef;
  91. struct mutex mutex;
  92. struct i2c_client *client;
  93. struct pca963x_led *leds;
  94. };
  95. struct pca963x_led {
  96. struct pca963x *chip;
  97. struct led_classdev led_cdev;
  98. int led_num; /* 0 .. 15 potentially */
  99. char name[32];
  100. u8 gdc;
  101. u8 gfrq;
  102. };
  103. static int pca963x_brightness(struct pca963x_led *pca963x,
  104. enum led_brightness brightness)
  105. {
  106. u8 ledout_addr = pca963x->chip->chipdef->ledout_base
  107. + (pca963x->led_num / 4);
  108. u8 ledout;
  109. int shift = 2 * (pca963x->led_num % 4);
  110. u8 mask = 0x3 << shift;
  111. int ret;
  112. mutex_lock(&pca963x->chip->mutex);
  113. ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
  114. switch (brightness) {
  115. case LED_FULL:
  116. ret = i2c_smbus_write_byte_data(pca963x->chip->client,
  117. ledout_addr,
  118. (ledout & ~mask) | (PCA963X_LED_ON << shift));
  119. break;
  120. case LED_OFF:
  121. ret = i2c_smbus_write_byte_data(pca963x->chip->client,
  122. ledout_addr, ledout & ~mask);
  123. break;
  124. default:
  125. ret = i2c_smbus_write_byte_data(pca963x->chip->client,
  126. PCA963X_PWM_BASE + pca963x->led_num,
  127. brightness);
  128. if (ret < 0)
  129. goto unlock;
  130. ret = i2c_smbus_write_byte_data(pca963x->chip->client,
  131. ledout_addr,
  132. (ledout & ~mask) | (PCA963X_LED_PWM << shift));
  133. break;
  134. }
  135. unlock:
  136. mutex_unlock(&pca963x->chip->mutex);
  137. return ret;
  138. }
  139. static void pca963x_blink(struct pca963x_led *pca963x)
  140. {
  141. u8 ledout_addr = pca963x->chip->chipdef->ledout_base +
  142. (pca963x->led_num / 4);
  143. u8 ledout;
  144. u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
  145. PCA963X_MODE2);
  146. int shift = 2 * (pca963x->led_num % 4);
  147. u8 mask = 0x3 << shift;
  148. i2c_smbus_write_byte_data(pca963x->chip->client,
  149. pca963x->chip->chipdef->grppwm, pca963x->gdc);
  150. i2c_smbus_write_byte_data(pca963x->chip->client,
  151. pca963x->chip->chipdef->grpfreq, pca963x->gfrq);
  152. if (!(mode2 & PCA963X_MODE2_DMBLNK))
  153. i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
  154. mode2 | PCA963X_MODE2_DMBLNK);
  155. mutex_lock(&pca963x->chip->mutex);
  156. ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
  157. if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift))
  158. i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
  159. (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift));
  160. mutex_unlock(&pca963x->chip->mutex);
  161. }
  162. static int pca963x_led_set(struct led_classdev *led_cdev,
  163. enum led_brightness value)
  164. {
  165. struct pca963x_led *pca963x;
  166. pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
  167. return pca963x_brightness(pca963x, value);
  168. }
  169. static int pca963x_blink_set(struct led_classdev *led_cdev,
  170. unsigned long *delay_on, unsigned long *delay_off)
  171. {
  172. struct pca963x_led *pca963x;
  173. unsigned long time_on, time_off, period;
  174. u8 gdc, gfrq;
  175. pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
  176. time_on = *delay_on;
  177. time_off = *delay_off;
  178. /* If both zero, pick reasonable defaults of 500ms each */
  179. if (!time_on && !time_off) {
  180. time_on = 500;
  181. time_off = 500;
  182. }
  183. period = time_on + time_off;
  184. /* If period not supported by hardware, default to someting sane. */
  185. if ((period < PCA963X_BLINK_PERIOD_MIN) ||
  186. (period > PCA963X_BLINK_PERIOD_MAX)) {
  187. time_on = 500;
  188. time_off = 500;
  189. period = time_on + time_off;
  190. }
  191. /*
  192. * From manual: duty cycle = (GDC / 256) ->
  193. * (time_on / period) = (GDC / 256) ->
  194. * GDC = ((time_on * 256) / period)
  195. */
  196. gdc = (time_on * 256) / period;
  197. /*
  198. * From manual: period = ((GFRQ + 1) / 24) in seconds.
  199. * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
  200. * GFRQ = ((period * 24 / 1000) - 1)
  201. */
  202. gfrq = (period * 24 / 1000) - 1;
  203. pca963x->gdc = gdc;
  204. pca963x->gfrq = gfrq;
  205. pca963x_blink(pca963x);
  206. *delay_on = time_on;
  207. *delay_off = time_off;
  208. return 0;
  209. }
  210. #if IS_ENABLED(CONFIG_OF)
  211. static struct pca963x_platform_data *
  212. pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
  213. {
  214. struct device_node *np = client->dev.of_node, *child;
  215. struct pca963x_platform_data *pdata;
  216. struct led_info *pca963x_leds;
  217. int count;
  218. count = of_get_child_count(np);
  219. if (!count || count > chip->n_leds)
  220. return ERR_PTR(-ENODEV);
  221. pca963x_leds = devm_kzalloc(&client->dev,
  222. sizeof(struct led_info) * chip->n_leds, GFP_KERNEL);
  223. if (!pca963x_leds)
  224. return ERR_PTR(-ENOMEM);
  225. for_each_child_of_node(np, child) {
  226. struct led_info led = {};
  227. u32 reg;
  228. int res;
  229. res = of_property_read_u32(child, "reg", &reg);
  230. if ((res != 0) || (reg >= chip->n_leds))
  231. continue;
  232. led.name =
  233. of_get_property(child, "label", NULL) ? : child->name;
  234. led.default_trigger =
  235. of_get_property(child, "linux,default-trigger", NULL);
  236. pca963x_leds[reg] = led;
  237. }
  238. pdata = devm_kzalloc(&client->dev,
  239. sizeof(struct pca963x_platform_data), GFP_KERNEL);
  240. if (!pdata)
  241. return ERR_PTR(-ENOMEM);
  242. pdata->leds.leds = pca963x_leds;
  243. pdata->leds.num_leds = chip->n_leds;
  244. /* default to open-drain unless totem pole (push-pull) is specified */
  245. if (of_property_read_bool(np, "nxp,totem-pole"))
  246. pdata->outdrv = PCA963X_TOTEM_POLE;
  247. else
  248. pdata->outdrv = PCA963X_OPEN_DRAIN;
  249. /* default to software blinking unless hardware blinking is specified */
  250. if (of_property_read_bool(np, "nxp,hw-blink"))
  251. pdata->blink_type = PCA963X_HW_BLINK;
  252. else
  253. pdata->blink_type = PCA963X_SW_BLINK;
  254. return pdata;
  255. }
  256. static const struct of_device_id of_pca963x_match[] = {
  257. { .compatible = "nxp,pca9632", },
  258. { .compatible = "nxp,pca9633", },
  259. { .compatible = "nxp,pca9634", },
  260. { .compatible = "nxp,pca9635", },
  261. {},
  262. };
  263. MODULE_DEVICE_TABLE(of, of_pca963x_match);
  264. #else
  265. static struct pca963x_platform_data *
  266. pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
  267. {
  268. return ERR_PTR(-ENODEV);
  269. }
  270. #endif
  271. static int pca963x_probe(struct i2c_client *client,
  272. const struct i2c_device_id *id)
  273. {
  274. struct pca963x *pca963x_chip;
  275. struct pca963x_led *pca963x;
  276. struct pca963x_platform_data *pdata;
  277. struct pca963x_chipdef *chip;
  278. int i, err;
  279. chip = &pca963x_chipdefs[id->driver_data];
  280. pdata = dev_get_platdata(&client->dev);
  281. if (!pdata) {
  282. pdata = pca963x_dt_init(client, chip);
  283. if (IS_ERR(pdata)) {
  284. dev_warn(&client->dev, "could not parse configuration\n");
  285. pdata = NULL;
  286. }
  287. }
  288. if (pdata && (pdata->leds.num_leds < 1 ||
  289. pdata->leds.num_leds > chip->n_leds)) {
  290. dev_err(&client->dev, "board info must claim 1-%d LEDs",
  291. chip->n_leds);
  292. return -EINVAL;
  293. }
  294. pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip),
  295. GFP_KERNEL);
  296. if (!pca963x_chip)
  297. return -ENOMEM;
  298. pca963x = devm_kzalloc(&client->dev, chip->n_leds * sizeof(*pca963x),
  299. GFP_KERNEL);
  300. if (!pca963x)
  301. return -ENOMEM;
  302. i2c_set_clientdata(client, pca963x_chip);
  303. mutex_init(&pca963x_chip->mutex);
  304. pca963x_chip->chipdef = chip;
  305. pca963x_chip->client = client;
  306. pca963x_chip->leds = pca963x;
  307. /* Turn off LEDs by default*/
  308. for (i = 0; i < chip->n_leds / 4; i++)
  309. i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00);
  310. for (i = 0; i < chip->n_leds; i++) {
  311. pca963x[i].led_num = i;
  312. pca963x[i].chip = pca963x_chip;
  313. /* Platform data can specify LED names and default triggers */
  314. if (pdata && i < pdata->leds.num_leds) {
  315. if (pdata->leds.leds[i].name)
  316. snprintf(pca963x[i].name,
  317. sizeof(pca963x[i].name), "pca963x:%s",
  318. pdata->leds.leds[i].name);
  319. if (pdata->leds.leds[i].default_trigger)
  320. pca963x[i].led_cdev.default_trigger =
  321. pdata->leds.leds[i].default_trigger;
  322. }
  323. if (!pdata || i >= pdata->leds.num_leds ||
  324. !pdata->leds.leds[i].name)
  325. snprintf(pca963x[i].name, sizeof(pca963x[i].name),
  326. "pca963x:%d:%.2x:%d", client->adapter->nr,
  327. client->addr, i);
  328. pca963x[i].led_cdev.name = pca963x[i].name;
  329. pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set;
  330. if (pdata && pdata->blink_type == PCA963X_HW_BLINK)
  331. pca963x[i].led_cdev.blink_set = pca963x_blink_set;
  332. err = led_classdev_register(&client->dev, &pca963x[i].led_cdev);
  333. if (err < 0)
  334. goto exit;
  335. }
  336. /* Disable LED all-call address and set normal mode */
  337. i2c_smbus_write_byte_data(client, PCA963X_MODE1, 0x00);
  338. if (pdata) {
  339. /* Configure output: open-drain or totem pole (push-pull) */
  340. if (pdata->outdrv == PCA963X_OPEN_DRAIN)
  341. i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x01);
  342. else
  343. i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x05);
  344. }
  345. return 0;
  346. exit:
  347. while (i--)
  348. led_classdev_unregister(&pca963x[i].led_cdev);
  349. return err;
  350. }
  351. static int pca963x_remove(struct i2c_client *client)
  352. {
  353. struct pca963x *pca963x = i2c_get_clientdata(client);
  354. int i;
  355. for (i = 0; i < pca963x->chipdef->n_leds; i++)
  356. led_classdev_unregister(&pca963x->leds[i].led_cdev);
  357. return 0;
  358. }
  359. static struct i2c_driver pca963x_driver = {
  360. .driver = {
  361. .name = "leds-pca963x",
  362. .of_match_table = of_match_ptr(of_pca963x_match),
  363. },
  364. .probe = pca963x_probe,
  365. .remove = pca963x_remove,
  366. .id_table = pca963x_id,
  367. };
  368. module_i2c_driver(pca963x_driver);
  369. MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
  370. MODULE_DESCRIPTION("PCA963X LED driver");
  371. MODULE_LICENSE("GPL v2");