leds-pca963x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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/workqueue.h>
  35. #include <linux/slab.h>
  36. #include <linux/of.h>
  37. #include <linux/platform_data/leds-pca963x.h>
  38. /* LED select registers determine the source that drives LED outputs */
  39. #define PCA963X_LED_OFF 0x0 /* LED driver off */
  40. #define PCA963X_LED_ON 0x1 /* LED driver on */
  41. #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */
  42. #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
  43. #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */
  44. #define PCA963X_MODE1 0x00
  45. #define PCA963X_MODE2 0x01
  46. #define PCA963X_PWM_BASE 0x02
  47. enum pca963x_type {
  48. pca9633,
  49. pca9634,
  50. pca9635,
  51. };
  52. struct pca963x_chipdef {
  53. u8 grppwm;
  54. u8 grpfreq;
  55. u8 ledout_base;
  56. int n_leds;
  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. enum pca963x_cmd {
  90. BRIGHTNESS_SET,
  91. BLINK_SET,
  92. };
  93. struct pca963x_led;
  94. struct pca963x {
  95. struct pca963x_chipdef *chipdef;
  96. struct mutex mutex;
  97. struct i2c_client *client;
  98. struct pca963x_led *leds;
  99. };
  100. struct pca963x_led {
  101. struct pca963x *chip;
  102. struct work_struct work;
  103. enum led_brightness brightness;
  104. struct led_classdev led_cdev;
  105. int led_num; /* 0 .. 15 potentially */
  106. enum pca963x_cmd cmd;
  107. char name[32];
  108. u8 gdc;
  109. u8 gfrq;
  110. };
  111. static void pca963x_brightness_work(struct pca963x_led *pca963x)
  112. {
  113. u8 ledout_addr = pca963x->chip->chipdef->ledout_base
  114. + (pca963x->led_num / 4);
  115. u8 ledout;
  116. int shift = 2 * (pca963x->led_num % 4);
  117. u8 mask = 0x3 << shift;
  118. mutex_lock(&pca963x->chip->mutex);
  119. ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
  120. switch (pca963x->brightness) {
  121. case LED_FULL:
  122. i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
  123. (ledout & ~mask) | (PCA963X_LED_ON << shift));
  124. break;
  125. case LED_OFF:
  126. i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
  127. ledout & ~mask);
  128. break;
  129. default:
  130. i2c_smbus_write_byte_data(pca963x->chip->client,
  131. PCA963X_PWM_BASE + pca963x->led_num,
  132. pca963x->brightness);
  133. i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
  134. (ledout & ~mask) | (PCA963X_LED_PWM << shift));
  135. break;
  136. }
  137. mutex_unlock(&pca963x->chip->mutex);
  138. }
  139. static void pca963x_blink_work(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 void pca963x_work(struct work_struct *work)
  163. {
  164. struct pca963x_led *pca963x = container_of(work,
  165. struct pca963x_led, work);
  166. switch (pca963x->cmd) {
  167. case BRIGHTNESS_SET:
  168. pca963x_brightness_work(pca963x);
  169. break;
  170. case BLINK_SET:
  171. pca963x_blink_work(pca963x);
  172. break;
  173. }
  174. }
  175. static void pca963x_led_set(struct led_classdev *led_cdev,
  176. enum led_brightness value)
  177. {
  178. struct pca963x_led *pca963x;
  179. pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
  180. pca963x->cmd = BRIGHTNESS_SET;
  181. pca963x->brightness = value;
  182. /*
  183. * Must use workqueue for the actual I/O since I2C operations
  184. * can sleep.
  185. */
  186. schedule_work(&pca963x->work);
  187. }
  188. static int pca963x_blink_set(struct led_classdev *led_cdev,
  189. unsigned long *delay_on, unsigned long *delay_off)
  190. {
  191. struct pca963x_led *pca963x;
  192. unsigned long time_on, time_off, period;
  193. u8 gdc, gfrq;
  194. pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
  195. time_on = *delay_on;
  196. time_off = *delay_off;
  197. /* If both zero, pick reasonable defaults of 500ms each */
  198. if (!time_on && !time_off) {
  199. time_on = 500;
  200. time_off = 500;
  201. }
  202. period = time_on + time_off;
  203. /* If period not supported by hardware, default to someting sane. */
  204. if ((period < PCA963X_BLINK_PERIOD_MIN) ||
  205. (period > PCA963X_BLINK_PERIOD_MAX)) {
  206. time_on = 500;
  207. time_off = 500;
  208. period = time_on + time_off;
  209. }
  210. /*
  211. * From manual: duty cycle = (GDC / 256) ->
  212. * (time_on / period) = (GDC / 256) ->
  213. * GDC = ((time_on * 256) / period)
  214. */
  215. gdc = (time_on * 256) / period;
  216. /*
  217. * From manual: period = ((GFRQ + 1) / 24) in seconds.
  218. * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
  219. * GFRQ = ((period * 24 / 1000) - 1)
  220. */
  221. gfrq = (period * 24 / 1000) - 1;
  222. pca963x->cmd = BLINK_SET;
  223. pca963x->gdc = gdc;
  224. pca963x->gfrq = gfrq;
  225. /*
  226. * Must use workqueue for the actual I/O since I2C operations
  227. * can sleep.
  228. */
  229. schedule_work(&pca963x->work);
  230. *delay_on = time_on;
  231. *delay_off = time_off;
  232. return 0;
  233. }
  234. #if IS_ENABLED(CONFIG_OF)
  235. static struct pca963x_platform_data *
  236. pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
  237. {
  238. struct device_node *np = client->dev.of_node, *child;
  239. struct pca963x_platform_data *pdata;
  240. struct led_info *pca963x_leds;
  241. int count;
  242. count = of_get_child_count(np);
  243. if (!count || count > chip->n_leds)
  244. return ERR_PTR(-ENODEV);
  245. pca963x_leds = devm_kzalloc(&client->dev,
  246. sizeof(struct led_info) * chip->n_leds, GFP_KERNEL);
  247. if (!pca963x_leds)
  248. return ERR_PTR(-ENOMEM);
  249. for_each_child_of_node(np, child) {
  250. struct led_info led = {};
  251. u32 reg;
  252. int res;
  253. res = of_property_read_u32(child, "reg", &reg);
  254. if ((res != 0) || (reg >= chip->n_leds))
  255. continue;
  256. led.name =
  257. of_get_property(child, "label", NULL) ? : child->name;
  258. led.default_trigger =
  259. of_get_property(child, "linux,default-trigger", NULL);
  260. pca963x_leds[reg] = led;
  261. }
  262. pdata = devm_kzalloc(&client->dev,
  263. sizeof(struct pca963x_platform_data), GFP_KERNEL);
  264. if (!pdata)
  265. return ERR_PTR(-ENOMEM);
  266. pdata->leds.leds = pca963x_leds;
  267. pdata->leds.num_leds = chip->n_leds;
  268. /* default to open-drain unless totem pole (push-pull) is specified */
  269. if (of_property_read_bool(np, "nxp,totem-pole"))
  270. pdata->outdrv = PCA963X_TOTEM_POLE;
  271. else
  272. pdata->outdrv = PCA963X_OPEN_DRAIN;
  273. /* default to software blinking unless hardware blinking is specified */
  274. if (of_property_read_bool(np, "nxp,hw-blink"))
  275. pdata->blink_type = PCA963X_HW_BLINK;
  276. else
  277. pdata->blink_type = PCA963X_SW_BLINK;
  278. return pdata;
  279. }
  280. static const struct of_device_id of_pca963x_match[] = {
  281. { .compatible = "nxp,pca9632", },
  282. { .compatible = "nxp,pca9633", },
  283. { .compatible = "nxp,pca9634", },
  284. { .compatible = "nxp,pca9635", },
  285. {},
  286. };
  287. MODULE_DEVICE_TABLE(of, of_pca963x_match);
  288. #else
  289. static struct pca963x_platform_data *
  290. pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
  291. {
  292. return ERR_PTR(-ENODEV);
  293. }
  294. #endif
  295. static int pca963x_probe(struct i2c_client *client,
  296. const struct i2c_device_id *id)
  297. {
  298. struct pca963x *pca963x_chip;
  299. struct pca963x_led *pca963x;
  300. struct pca963x_platform_data *pdata;
  301. struct pca963x_chipdef *chip;
  302. int i, err;
  303. chip = &pca963x_chipdefs[id->driver_data];
  304. pdata = dev_get_platdata(&client->dev);
  305. if (!pdata) {
  306. pdata = pca963x_dt_init(client, chip);
  307. if (IS_ERR(pdata)) {
  308. dev_warn(&client->dev, "could not parse configuration\n");
  309. pdata = NULL;
  310. }
  311. }
  312. if (pdata && (pdata->leds.num_leds < 1 ||
  313. pdata->leds.num_leds > chip->n_leds)) {
  314. dev_err(&client->dev, "board info must claim 1-%d LEDs",
  315. chip->n_leds);
  316. return -EINVAL;
  317. }
  318. pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip),
  319. GFP_KERNEL);
  320. if (!pca963x_chip)
  321. return -ENOMEM;
  322. pca963x = devm_kzalloc(&client->dev, chip->n_leds * sizeof(*pca963x),
  323. GFP_KERNEL);
  324. if (!pca963x)
  325. return -ENOMEM;
  326. i2c_set_clientdata(client, pca963x_chip);
  327. mutex_init(&pca963x_chip->mutex);
  328. pca963x_chip->chipdef = chip;
  329. pca963x_chip->client = client;
  330. pca963x_chip->leds = pca963x;
  331. /* Turn off LEDs by default*/
  332. for (i = 0; i < chip->n_leds / 4; i++)
  333. i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00);
  334. for (i = 0; i < chip->n_leds; i++) {
  335. pca963x[i].led_num = i;
  336. pca963x[i].chip = pca963x_chip;
  337. /* Platform data can specify LED names and default triggers */
  338. if (pdata && i < pdata->leds.num_leds) {
  339. if (pdata->leds.leds[i].name)
  340. snprintf(pca963x[i].name,
  341. sizeof(pca963x[i].name), "pca963x:%s",
  342. pdata->leds.leds[i].name);
  343. if (pdata->leds.leds[i].default_trigger)
  344. pca963x[i].led_cdev.default_trigger =
  345. pdata->leds.leds[i].default_trigger;
  346. }
  347. if (!pdata || i >= pdata->leds.num_leds ||
  348. !pdata->leds.leds[i].name)
  349. snprintf(pca963x[i].name, sizeof(pca963x[i].name),
  350. "pca963x:%d:%.2x:%d", client->adapter->nr,
  351. client->addr, i);
  352. pca963x[i].led_cdev.name = pca963x[i].name;
  353. pca963x[i].led_cdev.brightness_set = pca963x_led_set;
  354. if (pdata && pdata->blink_type == PCA963X_HW_BLINK)
  355. pca963x[i].led_cdev.blink_set = pca963x_blink_set;
  356. INIT_WORK(&pca963x[i].work, pca963x_work);
  357. err = led_classdev_register(&client->dev, &pca963x[i].led_cdev);
  358. if (err < 0)
  359. goto exit;
  360. }
  361. /* Disable LED all-call address and set normal mode */
  362. i2c_smbus_write_byte_data(client, PCA963X_MODE1, 0x00);
  363. if (pdata) {
  364. /* Configure output: open-drain or totem pole (push-pull) */
  365. if (pdata->outdrv == PCA963X_OPEN_DRAIN)
  366. i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x01);
  367. else
  368. i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x05);
  369. }
  370. return 0;
  371. exit:
  372. while (i--) {
  373. led_classdev_unregister(&pca963x[i].led_cdev);
  374. cancel_work_sync(&pca963x[i].work);
  375. }
  376. return err;
  377. }
  378. static int pca963x_remove(struct i2c_client *client)
  379. {
  380. struct pca963x *pca963x = i2c_get_clientdata(client);
  381. int i;
  382. for (i = 0; i < pca963x->chipdef->n_leds; i++) {
  383. led_classdev_unregister(&pca963x->leds[i].led_cdev);
  384. cancel_work_sync(&pca963x->leds[i].work);
  385. }
  386. return 0;
  387. }
  388. static struct i2c_driver pca963x_driver = {
  389. .driver = {
  390. .name = "leds-pca963x",
  391. .of_match_table = of_match_ptr(of_pca963x_match),
  392. },
  393. .probe = pca963x_probe,
  394. .remove = pca963x_remove,
  395. .id_table = pca963x_id,
  396. };
  397. module_i2c_driver(pca963x_driver);
  398. MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
  399. MODULE_DESCRIPTION("PCA963X LED driver");
  400. MODULE_LICENSE("GPL v2");