leds-pca963x.c 13 KB

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