leds-pca955x.c 15 KB

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