leds-pca963x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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/acpi.h>
  28. #include <linux/module.h>
  29. #include <linux/delay.h>
  30. #include <linux/string.h>
  31. #include <linux/ctype.h>
  32. #include <linux/leds.h>
  33. #include <linux/err.h>
  34. #include <linux/i2c.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_OUTDRV 0x04 /* Open-drain or totem pole */
  44. #define PCA963X_MODE2_INVRT 0x10 /* Normal or inverted direction */
  45. #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */
  46. #define PCA963X_MODE1 0x00
  47. #define PCA963X_MODE2 0x01
  48. #define PCA963X_PWM_BASE 0x02
  49. enum pca963x_type {
  50. pca9633,
  51. pca9634,
  52. pca9635,
  53. };
  54. struct pca963x_chipdef {
  55. u8 grppwm;
  56. u8 grpfreq;
  57. u8 ledout_base;
  58. int n_leds;
  59. unsigned int scaling;
  60. };
  61. static struct pca963x_chipdef pca963x_chipdefs[] = {
  62. [pca9633] = {
  63. .grppwm = 0x6,
  64. .grpfreq = 0x7,
  65. .ledout_base = 0x8,
  66. .n_leds = 4,
  67. },
  68. [pca9634] = {
  69. .grppwm = 0xa,
  70. .grpfreq = 0xb,
  71. .ledout_base = 0xc,
  72. .n_leds = 8,
  73. },
  74. [pca9635] = {
  75. .grppwm = 0x12,
  76. .grpfreq = 0x13,
  77. .ledout_base = 0x14,
  78. .n_leds = 16,
  79. },
  80. };
  81. /* Total blink period in milliseconds */
  82. #define PCA963X_BLINK_PERIOD_MIN 42
  83. #define PCA963X_BLINK_PERIOD_MAX 10667
  84. static const struct i2c_device_id pca963x_id[] = {
  85. { "pca9632", pca9633 },
  86. { "pca9633", pca9633 },
  87. { "pca9634", pca9634 },
  88. { "pca9635", pca9635 },
  89. { }
  90. };
  91. MODULE_DEVICE_TABLE(i2c, pca963x_id);
  92. static const struct acpi_device_id pca963x_acpi_ids[] = {
  93. { "PCA9632", pca9633 },
  94. { "PCA9633", pca9633 },
  95. { "PCA9634", pca9634 },
  96. { "PCA9635", pca9635 },
  97. { }
  98. };
  99. MODULE_DEVICE_TABLE(acpi, pca963x_acpi_ids);
  100. struct pca963x_led;
  101. struct pca963x {
  102. struct pca963x_chipdef *chipdef;
  103. struct mutex mutex;
  104. struct i2c_client *client;
  105. struct pca963x_led *leds;
  106. unsigned long leds_on;
  107. };
  108. struct pca963x_led {
  109. struct pca963x *chip;
  110. struct led_classdev led_cdev;
  111. int led_num; /* 0 .. 15 potentially */
  112. char name[32];
  113. u8 gdc;
  114. u8 gfrq;
  115. };
  116. static int pca963x_brightness(struct pca963x_led *pca963x,
  117. enum led_brightness brightness)
  118. {
  119. u8 ledout_addr = pca963x->chip->chipdef->ledout_base
  120. + (pca963x->led_num / 4);
  121. u8 ledout;
  122. int shift = 2 * (pca963x->led_num % 4);
  123. u8 mask = 0x3 << shift;
  124. int ret;
  125. ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
  126. switch (brightness) {
  127. case LED_FULL:
  128. ret = i2c_smbus_write_byte_data(pca963x->chip->client,
  129. ledout_addr,
  130. (ledout & ~mask) | (PCA963X_LED_ON << shift));
  131. break;
  132. case LED_OFF:
  133. ret = i2c_smbus_write_byte_data(pca963x->chip->client,
  134. ledout_addr, ledout & ~mask);
  135. break;
  136. default:
  137. ret = i2c_smbus_write_byte_data(pca963x->chip->client,
  138. PCA963X_PWM_BASE + pca963x->led_num,
  139. brightness);
  140. if (ret < 0)
  141. return ret;
  142. ret = i2c_smbus_write_byte_data(pca963x->chip->client,
  143. ledout_addr,
  144. (ledout & ~mask) | (PCA963X_LED_PWM << shift));
  145. break;
  146. }
  147. return ret;
  148. }
  149. static void pca963x_blink(struct pca963x_led *pca963x)
  150. {
  151. u8 ledout_addr = pca963x->chip->chipdef->ledout_base +
  152. (pca963x->led_num / 4);
  153. u8 ledout;
  154. u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
  155. PCA963X_MODE2);
  156. int shift = 2 * (pca963x->led_num % 4);
  157. u8 mask = 0x3 << shift;
  158. i2c_smbus_write_byte_data(pca963x->chip->client,
  159. pca963x->chip->chipdef->grppwm, pca963x->gdc);
  160. i2c_smbus_write_byte_data(pca963x->chip->client,
  161. pca963x->chip->chipdef->grpfreq, pca963x->gfrq);
  162. if (!(mode2 & PCA963X_MODE2_DMBLNK))
  163. i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
  164. mode2 | PCA963X_MODE2_DMBLNK);
  165. mutex_lock(&pca963x->chip->mutex);
  166. ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
  167. if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift))
  168. i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
  169. (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift));
  170. mutex_unlock(&pca963x->chip->mutex);
  171. }
  172. static int pca963x_power_state(struct pca963x_led *pca963x)
  173. {
  174. unsigned long *leds_on = &pca963x->chip->leds_on;
  175. unsigned long cached_leds = pca963x->chip->leds_on;
  176. if (pca963x->led_cdev.brightness)
  177. set_bit(pca963x->led_num, leds_on);
  178. else
  179. clear_bit(pca963x->led_num, leds_on);
  180. if (!(*leds_on) != !cached_leds)
  181. return i2c_smbus_write_byte_data(pca963x->chip->client,
  182. PCA963X_MODE1, *leds_on ? 0 : BIT(4));
  183. return 0;
  184. }
  185. static int pca963x_led_set(struct led_classdev *led_cdev,
  186. enum led_brightness value)
  187. {
  188. struct pca963x_led *pca963x;
  189. int ret;
  190. pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
  191. mutex_lock(&pca963x->chip->mutex);
  192. ret = pca963x_brightness(pca963x, value);
  193. if (ret < 0)
  194. goto unlock;
  195. ret = pca963x_power_state(pca963x);
  196. unlock:
  197. mutex_unlock(&pca963x->chip->mutex);
  198. return ret;
  199. }
  200. static unsigned int pca963x_period_scale(struct pca963x_led *pca963x,
  201. unsigned int val)
  202. {
  203. unsigned int scaling = pca963x->chip->chipdef->scaling;
  204. return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
  205. }
  206. static int pca963x_blink_set(struct led_classdev *led_cdev,
  207. unsigned long *delay_on, unsigned long *delay_off)
  208. {
  209. struct pca963x_led *pca963x;
  210. unsigned long time_on, time_off, period;
  211. u8 gdc, gfrq;
  212. pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
  213. time_on = *delay_on;
  214. time_off = *delay_off;
  215. /* If both zero, pick reasonable defaults of 500ms each */
  216. if (!time_on && !time_off) {
  217. time_on = 500;
  218. time_off = 500;
  219. }
  220. period = pca963x_period_scale(pca963x, time_on + time_off);
  221. /* If period not supported by hardware, default to someting sane. */
  222. if ((period < PCA963X_BLINK_PERIOD_MIN) ||
  223. (period > PCA963X_BLINK_PERIOD_MAX)) {
  224. time_on = 500;
  225. time_off = 500;
  226. period = pca963x_period_scale(pca963x, 1000);
  227. }
  228. /*
  229. * From manual: duty cycle = (GDC / 256) ->
  230. * (time_on / period) = (GDC / 256) ->
  231. * GDC = ((time_on * 256) / period)
  232. */
  233. gdc = (pca963x_period_scale(pca963x, time_on) * 256) / period;
  234. /*
  235. * From manual: period = ((GFRQ + 1) / 24) in seconds.
  236. * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
  237. * GFRQ = ((period * 24 / 1000) - 1)
  238. */
  239. gfrq = (period * 24 / 1000) - 1;
  240. pca963x->gdc = gdc;
  241. pca963x->gfrq = gfrq;
  242. pca963x_blink(pca963x);
  243. *delay_on = time_on;
  244. *delay_off = time_off;
  245. return 0;
  246. }
  247. #if IS_ENABLED(CONFIG_OF)
  248. static struct pca963x_platform_data *
  249. pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
  250. {
  251. struct device_node *np = client->dev.of_node, *child;
  252. struct pca963x_platform_data *pdata;
  253. struct led_info *pca963x_leds;
  254. int count;
  255. count = of_get_child_count(np);
  256. if (!count || count > chip->n_leds)
  257. return ERR_PTR(-ENODEV);
  258. pca963x_leds = devm_kcalloc(&client->dev,
  259. chip->n_leds, sizeof(struct led_info), GFP_KERNEL);
  260. if (!pca963x_leds)
  261. return ERR_PTR(-ENOMEM);
  262. for_each_child_of_node(np, child) {
  263. struct led_info led = {};
  264. u32 reg;
  265. int res;
  266. res = of_property_read_u32(child, "reg", &reg);
  267. if ((res != 0) || (reg >= chip->n_leds))
  268. continue;
  269. led.name =
  270. of_get_property(child, "label", NULL) ? : child->name;
  271. led.default_trigger =
  272. of_get_property(child, "linux,default-trigger", NULL);
  273. pca963x_leds[reg] = led;
  274. }
  275. pdata = devm_kzalloc(&client->dev,
  276. sizeof(struct pca963x_platform_data), GFP_KERNEL);
  277. if (!pdata)
  278. return ERR_PTR(-ENOMEM);
  279. pdata->leds.leds = pca963x_leds;
  280. pdata->leds.num_leds = chip->n_leds;
  281. /* default to open-drain unless totem pole (push-pull) is specified */
  282. if (of_property_read_bool(np, "nxp,totem-pole"))
  283. pdata->outdrv = PCA963X_TOTEM_POLE;
  284. else
  285. pdata->outdrv = PCA963X_OPEN_DRAIN;
  286. /* default to software blinking unless hardware blinking is specified */
  287. if (of_property_read_bool(np, "nxp,hw-blink"))
  288. pdata->blink_type = PCA963X_HW_BLINK;
  289. else
  290. pdata->blink_type = PCA963X_SW_BLINK;
  291. if (of_property_read_u32(np, "nxp,period-scale", &chip->scaling))
  292. chip->scaling = 1000;
  293. /* default to non-inverted output, unless inverted is specified */
  294. if (of_property_read_bool(np, "nxp,inverted-out"))
  295. pdata->dir = PCA963X_INVERTED;
  296. else
  297. pdata->dir = PCA963X_NORMAL;
  298. return pdata;
  299. }
  300. static const struct of_device_id of_pca963x_match[] = {
  301. { .compatible = "nxp,pca9632", },
  302. { .compatible = "nxp,pca9633", },
  303. { .compatible = "nxp,pca9634", },
  304. { .compatible = "nxp,pca9635", },
  305. {},
  306. };
  307. MODULE_DEVICE_TABLE(of, of_pca963x_match);
  308. #else
  309. static struct pca963x_platform_data *
  310. pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
  311. {
  312. return ERR_PTR(-ENODEV);
  313. }
  314. #endif
  315. static int pca963x_probe(struct i2c_client *client,
  316. const struct i2c_device_id *id)
  317. {
  318. struct pca963x *pca963x_chip;
  319. struct pca963x_led *pca963x;
  320. struct pca963x_platform_data *pdata;
  321. struct pca963x_chipdef *chip;
  322. int i, err;
  323. if (id) {
  324. chip = &pca963x_chipdefs[id->driver_data];
  325. } else {
  326. const struct acpi_device_id *acpi_id;
  327. acpi_id = acpi_match_device(pca963x_acpi_ids, &client->dev);
  328. if (!acpi_id)
  329. return -ENODEV;
  330. chip = &pca963x_chipdefs[acpi_id->driver_data];
  331. }
  332. pdata = dev_get_platdata(&client->dev);
  333. if (!pdata) {
  334. pdata = pca963x_dt_init(client, chip);
  335. if (IS_ERR(pdata)) {
  336. dev_warn(&client->dev, "could not parse configuration\n");
  337. pdata = NULL;
  338. }
  339. }
  340. if (pdata && (pdata->leds.num_leds < 1 ||
  341. pdata->leds.num_leds > chip->n_leds)) {
  342. dev_err(&client->dev, "board info must claim 1-%d LEDs",
  343. chip->n_leds);
  344. return -EINVAL;
  345. }
  346. pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip),
  347. GFP_KERNEL);
  348. if (!pca963x_chip)
  349. return -ENOMEM;
  350. pca963x = devm_kcalloc(&client->dev, chip->n_leds, sizeof(*pca963x),
  351. GFP_KERNEL);
  352. if (!pca963x)
  353. return -ENOMEM;
  354. i2c_set_clientdata(client, pca963x_chip);
  355. mutex_init(&pca963x_chip->mutex);
  356. pca963x_chip->chipdef = chip;
  357. pca963x_chip->client = client;
  358. pca963x_chip->leds = pca963x;
  359. /* Turn off LEDs by default*/
  360. for (i = 0; i < chip->n_leds / 4; i++)
  361. i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00);
  362. for (i = 0; i < chip->n_leds; i++) {
  363. pca963x[i].led_num = i;
  364. pca963x[i].chip = pca963x_chip;
  365. /* Platform data can specify LED names and default triggers */
  366. if (pdata && i < pdata->leds.num_leds) {
  367. if (pdata->leds.leds[i].name)
  368. snprintf(pca963x[i].name,
  369. sizeof(pca963x[i].name), "pca963x:%s",
  370. pdata->leds.leds[i].name);
  371. if (pdata->leds.leds[i].default_trigger)
  372. pca963x[i].led_cdev.default_trigger =
  373. pdata->leds.leds[i].default_trigger;
  374. }
  375. if (!pdata || i >= pdata->leds.num_leds ||
  376. !pdata->leds.leds[i].name)
  377. snprintf(pca963x[i].name, sizeof(pca963x[i].name),
  378. "pca963x:%d:%.2x:%d", client->adapter->nr,
  379. client->addr, i);
  380. pca963x[i].led_cdev.name = pca963x[i].name;
  381. pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set;
  382. if (pdata && pdata->blink_type == PCA963X_HW_BLINK)
  383. pca963x[i].led_cdev.blink_set = pca963x_blink_set;
  384. err = led_classdev_register(&client->dev, &pca963x[i].led_cdev);
  385. if (err < 0)
  386. goto exit;
  387. }
  388. /* Disable LED all-call address, and power down initially */
  389. i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
  390. if (pdata) {
  391. u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
  392. PCA963X_MODE2);
  393. /* Configure output: open-drain or totem pole (push-pull) */
  394. if (pdata->outdrv == PCA963X_OPEN_DRAIN)
  395. mode2 &= ~PCA963X_MODE2_OUTDRV;
  396. else
  397. mode2 |= PCA963X_MODE2_OUTDRV;
  398. /* Configure direction: normal or inverted */
  399. if (pdata->dir == PCA963X_INVERTED)
  400. mode2 |= PCA963X_MODE2_INVRT;
  401. i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
  402. mode2);
  403. }
  404. return 0;
  405. exit:
  406. while (i--)
  407. led_classdev_unregister(&pca963x[i].led_cdev);
  408. return err;
  409. }
  410. static int pca963x_remove(struct i2c_client *client)
  411. {
  412. struct pca963x *pca963x = i2c_get_clientdata(client);
  413. int i;
  414. for (i = 0; i < pca963x->chipdef->n_leds; i++)
  415. led_classdev_unregister(&pca963x->leds[i].led_cdev);
  416. return 0;
  417. }
  418. static struct i2c_driver pca963x_driver = {
  419. .driver = {
  420. .name = "leds-pca963x",
  421. .of_match_table = of_match_ptr(of_pca963x_match),
  422. .acpi_match_table = ACPI_PTR(pca963x_acpi_ids),
  423. },
  424. .probe = pca963x_probe,
  425. .remove = pca963x_remove,
  426. .id_table = pca963x_id,
  427. };
  428. module_i2c_driver(pca963x_driver);
  429. MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
  430. MODULE_DESCRIPTION("PCA963X LED driver");
  431. MODULE_LICENSE("GPL v2");