leds-pca955x.c 15 KB

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