leds-pca955x.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * Copyright 2007-2008 Extreme Engineering Solutions, Inc.
  3. *
  4. * Author: Nate Case <ncase@xes-inc.com>
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * LED driver for various PCA955x I2C LED drivers
  11. *
  12. * Supported devices:
  13. *
  14. * Device Description 7-bit slave address
  15. * ------ ----------- -------------------
  16. * PCA9550 2-bit driver 0x60 .. 0x61
  17. * PCA9551 8-bit driver 0x60 .. 0x67
  18. * PCA9552 16-bit driver 0x60 .. 0x67
  19. * PCA9553/01 4-bit driver 0x62
  20. * PCA9553/02 4-bit driver 0x63
  21. *
  22. * Philips PCA955x LED driver chips follow a register map as shown below:
  23. *
  24. * Control Register Description
  25. * ---------------- -----------
  26. * 0x0 Input register 0
  27. * ..
  28. * NUM_INPUT_REGS - 1 Last Input register X
  29. *
  30. * NUM_INPUT_REGS Frequency prescaler 0
  31. * NUM_INPUT_REGS + 1 PWM register 0
  32. * NUM_INPUT_REGS + 2 Frequency prescaler 1
  33. * NUM_INPUT_REGS + 3 PWM register 1
  34. *
  35. * NUM_INPUT_REGS + 4 LED selector 0
  36. * NUM_INPUT_REGS + 4
  37. * + NUM_LED_REGS - 1 Last LED selector
  38. *
  39. * where NUM_INPUT_REGS and NUM_LED_REGS vary depending on how many
  40. * bits the chip supports.
  41. */
  42. #include <linux/acpi.h>
  43. #include <linux/ctype.h>
  44. #include <linux/delay.h>
  45. #include <linux/err.h>
  46. #include <linux/gpio.h>
  47. #include <linux/i2c.h>
  48. #include <linux/leds.h>
  49. #include <linux/module.h>
  50. #include <linux/of_device.h>
  51. #include <linux/of.h>
  52. #include <linux/slab.h>
  53. #include <linux/string.h>
  54. #include <dt-bindings/leds/leds-pca955x.h>
  55. /* LED select registers determine the source that drives LED outputs */
  56. #define PCA955X_LS_LED_ON 0x0 /* Output LOW */
  57. #define PCA955X_LS_LED_OFF 0x1 /* Output HI-Z */
  58. #define PCA955X_LS_BLINK0 0x2 /* Blink at PWM0 rate */
  59. #define PCA955X_LS_BLINK1 0x3 /* Blink at PWM1 rate */
  60. enum pca955x_type {
  61. pca9550,
  62. pca9551,
  63. pca9552,
  64. pca9553,
  65. };
  66. struct pca955x_chipdef {
  67. int bits;
  68. u8 slv_addr; /* 7-bit slave address mask */
  69. int slv_addr_shift; /* Number of bits to ignore */
  70. };
  71. static struct pca955x_chipdef pca955x_chipdefs[] = {
  72. [pca9550] = {
  73. .bits = 2,
  74. .slv_addr = /* 110000x */ 0x60,
  75. .slv_addr_shift = 1,
  76. },
  77. [pca9551] = {
  78. .bits = 8,
  79. .slv_addr = /* 1100xxx */ 0x60,
  80. .slv_addr_shift = 3,
  81. },
  82. [pca9552] = {
  83. .bits = 16,
  84. .slv_addr = /* 1100xxx */ 0x60,
  85. .slv_addr_shift = 3,
  86. },
  87. [pca9553] = {
  88. .bits = 4,
  89. .slv_addr = /* 110001x */ 0x62,
  90. .slv_addr_shift = 1,
  91. },
  92. };
  93. static const struct i2c_device_id pca955x_id[] = {
  94. { "pca9550", pca9550 },
  95. { "pca9551", pca9551 },
  96. { "pca9552", pca9552 },
  97. { "pca9553", pca9553 },
  98. { }
  99. };
  100. MODULE_DEVICE_TABLE(i2c, pca955x_id);
  101. static const struct acpi_device_id pca955x_acpi_ids[] = {
  102. { "PCA9550", pca9550 },
  103. { "PCA9551", pca9551 },
  104. { "PCA9552", pca9552 },
  105. { "PCA9553", pca9553 },
  106. { }
  107. };
  108. MODULE_DEVICE_TABLE(acpi, pca955x_acpi_ids);
  109. struct pca955x {
  110. struct mutex lock;
  111. struct pca955x_led *leds;
  112. struct pca955x_chipdef *chipdef;
  113. struct i2c_client *client;
  114. #ifdef CONFIG_LEDS_PCA955X_GPIO
  115. struct gpio_chip gpio;
  116. #endif
  117. };
  118. struct pca955x_led {
  119. struct pca955x *pca955x;
  120. struct led_classdev led_cdev;
  121. int led_num; /* 0 .. 15 potentially */
  122. char name[32];
  123. u32 type;
  124. const char *default_trigger;
  125. };
  126. struct pca955x_platform_data {
  127. struct pca955x_led *leds;
  128. int num_leds;
  129. };
  130. /* 8 bits per input register */
  131. static inline int pca95xx_num_input_regs(int bits)
  132. {
  133. return (bits + 7) / 8;
  134. }
  135. /* 4 bits per LED selector register */
  136. static inline int pca95xx_num_led_regs(int bits)
  137. {
  138. return (bits + 3) / 4;
  139. }
  140. /*
  141. * Return an LED selector register value based on an existing one, with
  142. * the appropriate 2-bit state value set for the given LED number (0-3).
  143. */
  144. static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
  145. {
  146. return (oldval & (~(0x3 << (led_num << 1)))) |
  147. ((state & 0x3) << (led_num << 1));
  148. }
  149. /*
  150. * Write to frequency prescaler register, used to program the
  151. * period of the PWM output. period = (PSCx + 1) / 38
  152. */
  153. static int pca955x_write_psc(struct i2c_client *client, int n, u8 val)
  154. {
  155. struct pca955x *pca955x = i2c_get_clientdata(client);
  156. int ret;
  157. ret = i2c_smbus_write_byte_data(client,
  158. pca95xx_num_input_regs(pca955x->chipdef->bits) + 2*n,
  159. val);
  160. if (ret < 0)
  161. dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
  162. __func__, n, val, ret);
  163. return ret;
  164. }
  165. /*
  166. * Write to PWM register, which determines the duty cycle of the
  167. * output. LED is OFF when the count is less than the value of this
  168. * register, and ON when it is greater. If PWMx == 0, LED is always OFF.
  169. *
  170. * Duty cycle is (256 - PWMx) / 256
  171. */
  172. static int pca955x_write_pwm(struct i2c_client *client, int n, u8 val)
  173. {
  174. struct pca955x *pca955x = i2c_get_clientdata(client);
  175. int ret;
  176. ret = i2c_smbus_write_byte_data(client,
  177. pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + 2*n,
  178. val);
  179. if (ret < 0)
  180. dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
  181. __func__, n, val, ret);
  182. return ret;
  183. }
  184. /*
  185. * Write to LED selector register, which determines the source that
  186. * drives the LED output.
  187. */
  188. static int pca955x_write_ls(struct i2c_client *client, int n, u8 val)
  189. {
  190. struct pca955x *pca955x = i2c_get_clientdata(client);
  191. int ret;
  192. ret = i2c_smbus_write_byte_data(client,
  193. pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n,
  194. val);
  195. if (ret < 0)
  196. dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
  197. __func__, n, val, ret);
  198. return ret;
  199. }
  200. /*
  201. * Read the LED selector register, which determines the source that
  202. * drives the LED output.
  203. */
  204. static int pca955x_read_ls(struct i2c_client *client, int n, u8 *val)
  205. {
  206. struct pca955x *pca955x = i2c_get_clientdata(client);
  207. int ret;
  208. ret = i2c_smbus_read_byte_data(client,
  209. pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n);
  210. if (ret < 0) {
  211. dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
  212. __func__, n, ret);
  213. return ret;
  214. }
  215. *val = (u8)ret;
  216. return 0;
  217. }
  218. static int pca955x_led_set(struct led_classdev *led_cdev,
  219. enum led_brightness value)
  220. {
  221. struct pca955x_led *pca955x_led;
  222. struct pca955x *pca955x;
  223. u8 ls;
  224. int chip_ls; /* which LSx to use (0-3 potentially) */
  225. int ls_led; /* which set of bits within LSx to use (0-3) */
  226. int ret;
  227. pca955x_led = container_of(led_cdev, struct pca955x_led, led_cdev);
  228. pca955x = pca955x_led->pca955x;
  229. chip_ls = pca955x_led->led_num / 4;
  230. ls_led = pca955x_led->led_num % 4;
  231. mutex_lock(&pca955x->lock);
  232. ret = pca955x_read_ls(pca955x->client, chip_ls, &ls);
  233. if (ret)
  234. goto out;
  235. switch (value) {
  236. case LED_FULL:
  237. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON);
  238. break;
  239. case LED_OFF:
  240. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_OFF);
  241. break;
  242. case LED_HALF:
  243. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK0);
  244. break;
  245. default:
  246. /*
  247. * Use PWM1 for all other values. This has the unwanted
  248. * side effect of making all LEDs on the chip share the
  249. * same brightness level if set to a value other than
  250. * OFF, HALF, or FULL. But, this is probably better than
  251. * just turning off for all other values.
  252. */
  253. ret = pca955x_write_pwm(pca955x->client, 1, 255 - value);
  254. if (ret)
  255. goto out;
  256. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1);
  257. break;
  258. }
  259. ret = pca955x_write_ls(pca955x->client, chip_ls, ls);
  260. out:
  261. mutex_unlock(&pca955x->lock);
  262. return ret;
  263. }
  264. #ifdef CONFIG_LEDS_PCA955X_GPIO
  265. /*
  266. * Read the INPUT register, which contains the state of LEDs.
  267. */
  268. static int pca955x_read_input(struct i2c_client *client, int n, u8 *val)
  269. {
  270. int ret = i2c_smbus_read_byte_data(client, n);
  271. if (ret < 0) {
  272. dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
  273. __func__, n, ret);
  274. return ret;
  275. }
  276. *val = (u8)ret;
  277. return 0;
  278. }
  279. static int pca955x_gpio_request_pin(struct gpio_chip *gc, unsigned int offset)
  280. {
  281. struct pca955x *pca955x = gpiochip_get_data(gc);
  282. struct pca955x_led *led = &pca955x->leds[offset];
  283. if (led->type == PCA955X_TYPE_GPIO)
  284. return 0;
  285. return -EBUSY;
  286. }
  287. static int pca955x_set_value(struct gpio_chip *gc, unsigned int offset,
  288. int val)
  289. {
  290. struct pca955x *pca955x = gpiochip_get_data(gc);
  291. struct pca955x_led *led = &pca955x->leds[offset];
  292. if (val)
  293. return pca955x_led_set(&led->led_cdev, LED_FULL);
  294. else
  295. return pca955x_led_set(&led->led_cdev, LED_OFF);
  296. }
  297. static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset,
  298. int val)
  299. {
  300. pca955x_set_value(gc, offset, val);
  301. }
  302. static int pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset)
  303. {
  304. struct pca955x *pca955x = gpiochip_get_data(gc);
  305. struct pca955x_led *led = &pca955x->leds[offset];
  306. u8 reg = 0;
  307. /* There is nothing we can do about errors */
  308. pca955x_read_input(pca955x->client, led->led_num / 8, &reg);
  309. return !!(reg & (1 << (led->led_num % 8)));
  310. }
  311. static int pca955x_gpio_direction_input(struct gpio_chip *gc,
  312. unsigned int offset)
  313. {
  314. /* To use as input ensure pin is not driven */
  315. return pca955x_set_value(gc, offset, 0);
  316. }
  317. static int pca955x_gpio_direction_output(struct gpio_chip *gc,
  318. unsigned int offset, int val)
  319. {
  320. return pca955x_set_value(gc, offset, val);
  321. }
  322. #endif /* CONFIG_LEDS_PCA955X_GPIO */
  323. #if IS_ENABLED(CONFIG_OF)
  324. static struct pca955x_platform_data *
  325. pca955x_pdata_of_init(struct i2c_client *client, struct pca955x_chipdef *chip)
  326. {
  327. struct device_node *np = client->dev.of_node;
  328. struct device_node *child;
  329. struct pca955x_platform_data *pdata;
  330. int count;
  331. count = of_get_child_count(np);
  332. if (!count || count > chip->bits)
  333. return ERR_PTR(-ENODEV);
  334. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  335. if (!pdata)
  336. return ERR_PTR(-ENOMEM);
  337. pdata->leds = devm_kzalloc(&client->dev,
  338. sizeof(struct pca955x_led) * chip->bits,
  339. GFP_KERNEL);
  340. if (!pdata->leds)
  341. return ERR_PTR(-ENOMEM);
  342. for_each_child_of_node(np, child) {
  343. const char *name;
  344. u32 reg;
  345. int res;
  346. res = of_property_read_u32(child, "reg", &reg);
  347. if ((res != 0) || (reg >= chip->bits))
  348. continue;
  349. if (of_property_read_string(child, "label", &name))
  350. name = child->name;
  351. snprintf(pdata->leds[reg].name, sizeof(pdata->leds[reg].name),
  352. "%s", name);
  353. pdata->leds[reg].type = PCA955X_TYPE_LED;
  354. of_property_read_u32(child, "type", &pdata->leds[reg].type);
  355. of_property_read_string(child, "linux,default-trigger",
  356. &pdata->leds[reg].default_trigger);
  357. }
  358. pdata->num_leds = chip->bits;
  359. return pdata;
  360. }
  361. static const struct of_device_id of_pca955x_match[] = {
  362. { .compatible = "nxp,pca9550", .data = (void *)pca9550 },
  363. { .compatible = "nxp,pca9551", .data = (void *)pca9551 },
  364. { .compatible = "nxp,pca9552", .data = (void *)pca9552 },
  365. { .compatible = "nxp,pca9553", .data = (void *)pca9553 },
  366. {},
  367. };
  368. MODULE_DEVICE_TABLE(of, of_pca955x_match);
  369. #else
  370. static struct pca955x_platform_data *
  371. pca955x_pdata_of_init(struct i2c_client *client, struct pca955x_chipdef *chip)
  372. {
  373. return ERR_PTR(-ENODEV);
  374. }
  375. #endif
  376. static int pca955x_probe(struct i2c_client *client,
  377. const struct i2c_device_id *id)
  378. {
  379. struct pca955x *pca955x;
  380. struct pca955x_led *pca955x_led;
  381. struct pca955x_chipdef *chip;
  382. struct i2c_adapter *adapter;
  383. int i, err;
  384. struct pca955x_platform_data *pdata;
  385. int ngpios = 0;
  386. if (id) {
  387. chip = &pca955x_chipdefs[id->driver_data];
  388. } else {
  389. const struct acpi_device_id *acpi_id;
  390. acpi_id = acpi_match_device(pca955x_acpi_ids, &client->dev);
  391. if (!acpi_id)
  392. return -ENODEV;
  393. chip = &pca955x_chipdefs[acpi_id->driver_data];
  394. }
  395. adapter = to_i2c_adapter(client->dev.parent);
  396. pdata = dev_get_platdata(&client->dev);
  397. if (!pdata) {
  398. pdata = pca955x_pdata_of_init(client, chip);
  399. if (IS_ERR(pdata))
  400. return PTR_ERR(pdata);
  401. }
  402. /* Make sure the slave address / chip type combo given is possible */
  403. if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) !=
  404. chip->slv_addr) {
  405. dev_err(&client->dev, "invalid slave address %02x\n",
  406. client->addr);
  407. return -ENODEV;
  408. }
  409. dev_info(&client->dev, "leds-pca955x: Using %s %d-bit LED driver at "
  410. "slave address 0x%02x\n",
  411. client->name, chip->bits, client->addr);
  412. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  413. return -EIO;
  414. if (pdata->num_leds != chip->bits) {
  415. dev_err(&client->dev,
  416. "board info claims %d LEDs on a %d-bit chip\n",
  417. pdata->num_leds, chip->bits);
  418. return -ENODEV;
  419. }
  420. pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
  421. if (!pca955x)
  422. return -ENOMEM;
  423. pca955x->leds = devm_kzalloc(&client->dev,
  424. sizeof(*pca955x_led) * chip->bits, GFP_KERNEL);
  425. if (!pca955x->leds)
  426. return -ENOMEM;
  427. i2c_set_clientdata(client, pca955x);
  428. mutex_init(&pca955x->lock);
  429. pca955x->client = client;
  430. pca955x->chipdef = chip;
  431. for (i = 0; i < chip->bits; i++) {
  432. pca955x_led = &pca955x->leds[i];
  433. pca955x_led->led_num = i;
  434. pca955x_led->pca955x = pca955x;
  435. pca955x_led->type = pdata->leds[i].type;
  436. switch (pca955x_led->type) {
  437. case PCA955X_TYPE_NONE:
  438. break;
  439. case PCA955X_TYPE_GPIO:
  440. ngpios++;
  441. break;
  442. case PCA955X_TYPE_LED:
  443. /*
  444. * Platform data can specify LED names and
  445. * default triggers
  446. */
  447. if (pdata->leds[i].name[0] == '\0')
  448. snprintf(pdata->leds[i].name,
  449. sizeof(pdata->leds[i].name), "%d", i);
  450. snprintf(pca955x_led->name,
  451. sizeof(pca955x_led->name), "pca955x:%s",
  452. pdata->leds[i].name);
  453. if (pdata->leds[i].default_trigger)
  454. pca955x_led->led_cdev.default_trigger =
  455. pdata->leds[i].default_trigger;
  456. pca955x_led->led_cdev.name = pca955x_led->name;
  457. pca955x_led->led_cdev.brightness_set_blocking =
  458. pca955x_led_set;
  459. err = devm_led_classdev_register(&client->dev,
  460. &pca955x_led->led_cdev);
  461. if (err)
  462. return err;
  463. /* Turn off LED */
  464. err = pca955x_led_set(&pca955x_led->led_cdev, LED_OFF);
  465. if (err)
  466. return err;
  467. }
  468. }
  469. /* PWM0 is used for half brightness or 50% duty cycle */
  470. err = pca955x_write_pwm(client, 0, 255 - LED_HALF);
  471. if (err)
  472. return err;
  473. /* PWM1 is used for variable brightness, default to OFF */
  474. err = pca955x_write_pwm(client, 1, 0);
  475. if (err)
  476. return err;
  477. /* Set to fast frequency so we do not see flashing */
  478. err = pca955x_write_psc(client, 0, 0);
  479. if (err)
  480. return err;
  481. err = pca955x_write_psc(client, 1, 0);
  482. if (err)
  483. return err;
  484. #ifdef CONFIG_LEDS_PCA955X_GPIO
  485. if (ngpios) {
  486. pca955x->gpio.label = "gpio-pca955x";
  487. pca955x->gpio.direction_input = pca955x_gpio_direction_input;
  488. pca955x->gpio.direction_output = pca955x_gpio_direction_output;
  489. pca955x->gpio.set = pca955x_gpio_set_value;
  490. pca955x->gpio.get = pca955x_gpio_get_value;
  491. pca955x->gpio.request = pca955x_gpio_request_pin;
  492. pca955x->gpio.can_sleep = 1;
  493. pca955x->gpio.base = -1;
  494. pca955x->gpio.ngpio = ngpios;
  495. pca955x->gpio.parent = &client->dev;
  496. pca955x->gpio.owner = THIS_MODULE;
  497. err = devm_gpiochip_add_data(&client->dev, &pca955x->gpio,
  498. pca955x);
  499. if (err) {
  500. /* Use data->gpio.dev as a flag for freeing gpiochip */
  501. pca955x->gpio.parent = NULL;
  502. dev_warn(&client->dev, "could not add gpiochip\n");
  503. return err;
  504. }
  505. dev_info(&client->dev, "gpios %i...%i\n",
  506. pca955x->gpio.base, pca955x->gpio.base +
  507. pca955x->gpio.ngpio - 1);
  508. }
  509. #endif
  510. return 0;
  511. }
  512. static struct i2c_driver pca955x_driver = {
  513. .driver = {
  514. .name = "leds-pca955x",
  515. .acpi_match_table = ACPI_PTR(pca955x_acpi_ids),
  516. .of_match_table = of_match_ptr(of_pca955x_match),
  517. },
  518. .probe = pca955x_probe,
  519. .id_table = pca955x_id,
  520. };
  521. module_i2c_driver(pca955x_driver);
  522. MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>");
  523. MODULE_DESCRIPTION("PCA955x LED driver");
  524. MODULE_LICENSE("GPL v2");