leds-pca955x.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2007-2008 Extreme Engineering Solutions, Inc.
  4. *
  5. * Author: Nate Case <[email protected]>
  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/bitops.h>
  40. #include <linux/ctype.h>
  41. #include <linux/delay.h>
  42. #include <linux/err.h>
  43. #include <linux/gpio/driver.h>
  44. #include <linux/i2c.h>
  45. #include <linux/leds.h>
  46. #include <linux/module.h>
  47. #include <linux/of.h>
  48. #include <linux/property.h>
  49. #include <linux/slab.h>
  50. #include <linux/string.h>
  51. #include <dt-bindings/leds/leds-pca955x.h>
  52. /* LED select registers determine the source that drives LED outputs */
  53. #define PCA955X_LS_LED_ON 0x0 /* Output LOW */
  54. #define PCA955X_LS_LED_OFF 0x1 /* Output HI-Z */
  55. #define PCA955X_LS_BLINK0 0x2 /* Blink at PWM0 rate */
  56. #define PCA955X_LS_BLINK1 0x3 /* Blink at PWM1 rate */
  57. #define PCA955X_GPIO_INPUT LED_OFF
  58. #define PCA955X_GPIO_HIGH LED_OFF
  59. #define PCA955X_GPIO_LOW LED_FULL
  60. enum pca955x_type {
  61. pca9550,
  62. pca9551,
  63. pca9552,
  64. ibm_pca9552,
  65. pca9553,
  66. };
  67. struct pca955x_chipdef {
  68. int bits;
  69. u8 slv_addr; /* 7-bit slave address mask */
  70. int slv_addr_shift; /* Number of bits to ignore */
  71. };
  72. static struct pca955x_chipdef pca955x_chipdefs[] = {
  73. [pca9550] = {
  74. .bits = 2,
  75. .slv_addr = /* 110000x */ 0x60,
  76. .slv_addr_shift = 1,
  77. },
  78. [pca9551] = {
  79. .bits = 8,
  80. .slv_addr = /* 1100xxx */ 0x60,
  81. .slv_addr_shift = 3,
  82. },
  83. [pca9552] = {
  84. .bits = 16,
  85. .slv_addr = /* 1100xxx */ 0x60,
  86. .slv_addr_shift = 3,
  87. },
  88. [ibm_pca9552] = {
  89. .bits = 16,
  90. .slv_addr = /* 0110xxx */ 0x30,
  91. .slv_addr_shift = 3,
  92. },
  93. [pca9553] = {
  94. .bits = 4,
  95. .slv_addr = /* 110001x */ 0x62,
  96. .slv_addr_shift = 1,
  97. },
  98. };
  99. static const struct i2c_device_id pca955x_id[] = {
  100. { "pca9550", pca9550 },
  101. { "pca9551", pca9551 },
  102. { "pca9552", pca9552 },
  103. { "ibm-pca9552", ibm_pca9552 },
  104. { "pca9553", pca9553 },
  105. { }
  106. };
  107. MODULE_DEVICE_TABLE(i2c, pca955x_id);
  108. struct pca955x {
  109. struct mutex lock;
  110. struct pca955x_led *leds;
  111. struct pca955x_chipdef *chipdef;
  112. struct i2c_client *client;
  113. unsigned long active_pins;
  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. u32 type;
  123. int default_state;
  124. struct fwnode_handle *fwnode;
  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. u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + (2 * n);
  157. int ret;
  158. ret = i2c_smbus_write_byte_data(client, cmd, val);
  159. if (ret < 0)
  160. dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
  161. __func__, n, val, ret);
  162. return ret;
  163. }
  164. /*
  165. * Write to PWM register, which determines the duty cycle of the
  166. * output. LED is OFF when the count is less than the value of this
  167. * register, and ON when it is greater. If PWMx == 0, LED is always OFF.
  168. *
  169. * Duty cycle is (256 - PWMx) / 256
  170. */
  171. static int pca955x_write_pwm(struct i2c_client *client, int n, u8 val)
  172. {
  173. struct pca955x *pca955x = i2c_get_clientdata(client);
  174. u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
  175. int ret;
  176. ret = i2c_smbus_write_byte_data(client, cmd, 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. u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n;
  190. int ret;
  191. ret = i2c_smbus_write_byte_data(client, cmd, val);
  192. if (ret < 0)
  193. dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
  194. __func__, n, val, ret);
  195. return ret;
  196. }
  197. /*
  198. * Read the LED selector register, which determines the source that
  199. * drives the LED output.
  200. */
  201. static int pca955x_read_ls(struct i2c_client *client, int n, u8 *val)
  202. {
  203. struct pca955x *pca955x = i2c_get_clientdata(client);
  204. u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n;
  205. int ret;
  206. ret = i2c_smbus_read_byte_data(client, cmd);
  207. if (ret < 0) {
  208. dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
  209. __func__, n, ret);
  210. return ret;
  211. }
  212. *val = (u8)ret;
  213. return 0;
  214. }
  215. static int pca955x_read_pwm(struct i2c_client *client, int n, u8 *val)
  216. {
  217. struct pca955x *pca955x = i2c_get_clientdata(client);
  218. u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
  219. int ret;
  220. ret = i2c_smbus_read_byte_data(client, cmd);
  221. if (ret < 0) {
  222. dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
  223. __func__, n, ret);
  224. return ret;
  225. }
  226. *val = (u8)ret;
  227. return 0;
  228. }
  229. static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
  230. {
  231. struct pca955x_led *pca955x_led = container_of(led_cdev,
  232. struct pca955x_led,
  233. led_cdev);
  234. struct pca955x *pca955x = pca955x_led->pca955x;
  235. u8 ls, pwm;
  236. int ret;
  237. ret = pca955x_read_ls(pca955x->client, pca955x_led->led_num / 4, &ls);
  238. if (ret)
  239. return ret;
  240. ls = (ls >> ((pca955x_led->led_num % 4) << 1)) & 0x3;
  241. switch (ls) {
  242. case PCA955X_LS_LED_ON:
  243. ret = LED_FULL;
  244. break;
  245. case PCA955X_LS_LED_OFF:
  246. ret = LED_OFF;
  247. break;
  248. case PCA955X_LS_BLINK0:
  249. ret = LED_HALF;
  250. break;
  251. case PCA955X_LS_BLINK1:
  252. ret = pca955x_read_pwm(pca955x->client, 1, &pwm);
  253. if (ret)
  254. return ret;
  255. ret = 255 - pwm;
  256. break;
  257. }
  258. return ret;
  259. }
  260. static int pca955x_led_set(struct led_classdev *led_cdev,
  261. enum led_brightness value)
  262. {
  263. struct pca955x_led *pca955x_led;
  264. struct pca955x *pca955x;
  265. u8 ls;
  266. int chip_ls; /* which LSx to use (0-3 potentially) */
  267. int ls_led; /* which set of bits within LSx to use (0-3) */
  268. int ret;
  269. pca955x_led = container_of(led_cdev, struct pca955x_led, led_cdev);
  270. pca955x = pca955x_led->pca955x;
  271. chip_ls = pca955x_led->led_num / 4;
  272. ls_led = pca955x_led->led_num % 4;
  273. mutex_lock(&pca955x->lock);
  274. ret = pca955x_read_ls(pca955x->client, chip_ls, &ls);
  275. if (ret)
  276. goto out;
  277. switch (value) {
  278. case LED_FULL:
  279. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON);
  280. break;
  281. case LED_OFF:
  282. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_OFF);
  283. break;
  284. case LED_HALF:
  285. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK0);
  286. break;
  287. default:
  288. /*
  289. * Use PWM1 for all other values. This has the unwanted
  290. * side effect of making all LEDs on the chip share the
  291. * same brightness level if set to a value other than
  292. * OFF, HALF, or FULL. But, this is probably better than
  293. * just turning off for all other values.
  294. */
  295. ret = pca955x_write_pwm(pca955x->client, 1, 255 - value);
  296. if (ret)
  297. goto out;
  298. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1);
  299. break;
  300. }
  301. ret = pca955x_write_ls(pca955x->client, chip_ls, ls);
  302. out:
  303. mutex_unlock(&pca955x->lock);
  304. return ret;
  305. }
  306. #ifdef CONFIG_LEDS_PCA955X_GPIO
  307. /*
  308. * Read the INPUT register, which contains the state of LEDs.
  309. */
  310. static int pca955x_read_input(struct i2c_client *client, int n, u8 *val)
  311. {
  312. int ret = i2c_smbus_read_byte_data(client, n);
  313. if (ret < 0) {
  314. dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
  315. __func__, n, ret);
  316. return ret;
  317. }
  318. *val = (u8)ret;
  319. return 0;
  320. }
  321. static int pca955x_gpio_request_pin(struct gpio_chip *gc, unsigned int offset)
  322. {
  323. struct pca955x *pca955x = gpiochip_get_data(gc);
  324. return test_and_set_bit(offset, &pca955x->active_pins) ? -EBUSY : 0;
  325. }
  326. static void pca955x_gpio_free_pin(struct gpio_chip *gc, unsigned int offset)
  327. {
  328. struct pca955x *pca955x = gpiochip_get_data(gc);
  329. clear_bit(offset, &pca955x->active_pins);
  330. }
  331. static int pca955x_set_value(struct gpio_chip *gc, unsigned int offset,
  332. int val)
  333. {
  334. struct pca955x *pca955x = gpiochip_get_data(gc);
  335. struct pca955x_led *led = &pca955x->leds[offset];
  336. if (val)
  337. return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_HIGH);
  338. return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_LOW);
  339. }
  340. static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset,
  341. int val)
  342. {
  343. pca955x_set_value(gc, offset, val);
  344. }
  345. static int pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset)
  346. {
  347. struct pca955x *pca955x = gpiochip_get_data(gc);
  348. struct pca955x_led *led = &pca955x->leds[offset];
  349. u8 reg = 0;
  350. /* There is nothing we can do about errors */
  351. pca955x_read_input(pca955x->client, led->led_num / 8, &reg);
  352. return !!(reg & (1 << (led->led_num % 8)));
  353. }
  354. static int pca955x_gpio_direction_input(struct gpio_chip *gc,
  355. unsigned int offset)
  356. {
  357. struct pca955x *pca955x = gpiochip_get_data(gc);
  358. struct pca955x_led *led = &pca955x->leds[offset];
  359. /* To use as input ensure pin is not driven. */
  360. return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_INPUT);
  361. }
  362. static int pca955x_gpio_direction_output(struct gpio_chip *gc,
  363. unsigned int offset, int val)
  364. {
  365. return pca955x_set_value(gc, offset, val);
  366. }
  367. #endif /* CONFIG_LEDS_PCA955X_GPIO */
  368. static struct pca955x_platform_data *
  369. pca955x_get_pdata(struct i2c_client *client, struct pca955x_chipdef *chip)
  370. {
  371. struct pca955x_platform_data *pdata;
  372. struct pca955x_led *led;
  373. struct fwnode_handle *child;
  374. int count;
  375. count = device_get_child_node_count(&client->dev);
  376. if (count > chip->bits)
  377. return ERR_PTR(-ENODEV);
  378. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  379. if (!pdata)
  380. return ERR_PTR(-ENOMEM);
  381. pdata->leds = devm_kcalloc(&client->dev,
  382. chip->bits, sizeof(struct pca955x_led),
  383. GFP_KERNEL);
  384. if (!pdata->leds)
  385. return ERR_PTR(-ENOMEM);
  386. device_for_each_child_node(&client->dev, child) {
  387. const char *state;
  388. u32 reg;
  389. int res;
  390. res = fwnode_property_read_u32(child, "reg", &reg);
  391. if ((res != 0) || (reg >= chip->bits))
  392. continue;
  393. led = &pdata->leds[reg];
  394. led->type = PCA955X_TYPE_LED;
  395. led->fwnode = child;
  396. fwnode_property_read_u32(child, "type", &led->type);
  397. if (!fwnode_property_read_string(child, "default-state",
  398. &state)) {
  399. if (!strcmp(state, "keep"))
  400. led->default_state = LEDS_GPIO_DEFSTATE_KEEP;
  401. else if (!strcmp(state, "on"))
  402. led->default_state = LEDS_GPIO_DEFSTATE_ON;
  403. else
  404. led->default_state = LEDS_GPIO_DEFSTATE_OFF;
  405. } else {
  406. led->default_state = LEDS_GPIO_DEFSTATE_OFF;
  407. }
  408. }
  409. pdata->num_leds = chip->bits;
  410. return pdata;
  411. }
  412. static const struct of_device_id of_pca955x_match[] = {
  413. { .compatible = "nxp,pca9550", .data = (void *)pca9550 },
  414. { .compatible = "nxp,pca9551", .data = (void *)pca9551 },
  415. { .compatible = "nxp,pca9552", .data = (void *)pca9552 },
  416. { .compatible = "ibm,pca9552", .data = (void *)ibm_pca9552 },
  417. { .compatible = "nxp,pca9553", .data = (void *)pca9553 },
  418. {},
  419. };
  420. MODULE_DEVICE_TABLE(of, of_pca955x_match);
  421. static int pca955x_probe(struct i2c_client *client)
  422. {
  423. struct pca955x *pca955x;
  424. struct pca955x_led *pca955x_led;
  425. struct pca955x_chipdef *chip;
  426. struct led_classdev *led;
  427. struct led_init_data init_data;
  428. struct i2c_adapter *adapter;
  429. int i, err;
  430. struct pca955x_platform_data *pdata;
  431. bool set_default_label = false;
  432. bool keep_pwm = false;
  433. char default_label[8];
  434. enum pca955x_type chip_type;
  435. const void *md = device_get_match_data(&client->dev);
  436. if (md) {
  437. chip_type = (enum pca955x_type)md;
  438. } else {
  439. const struct i2c_device_id *id = i2c_match_id(pca955x_id,
  440. client);
  441. if (id) {
  442. chip_type = (enum pca955x_type)id->driver_data;
  443. } else {
  444. dev_err(&client->dev, "unknown chip\n");
  445. return -ENODEV;
  446. }
  447. }
  448. chip = &pca955x_chipdefs[chip_type];
  449. adapter = client->adapter;
  450. pdata = dev_get_platdata(&client->dev);
  451. if (!pdata) {
  452. pdata = pca955x_get_pdata(client, chip);
  453. if (IS_ERR(pdata))
  454. return PTR_ERR(pdata);
  455. }
  456. /* Make sure the slave address / chip type combo given is possible */
  457. if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) !=
  458. chip->slv_addr) {
  459. dev_err(&client->dev, "invalid slave address %02x\n",
  460. client->addr);
  461. return -ENODEV;
  462. }
  463. dev_info(&client->dev, "leds-pca955x: Using %s %d-bit LED driver at "
  464. "slave address 0x%02x\n", client->name, chip->bits,
  465. client->addr);
  466. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  467. return -EIO;
  468. if (pdata->num_leds != chip->bits) {
  469. dev_err(&client->dev,
  470. "board info claims %d LEDs on a %d-bit chip\n",
  471. pdata->num_leds, chip->bits);
  472. return -ENODEV;
  473. }
  474. pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
  475. if (!pca955x)
  476. return -ENOMEM;
  477. pca955x->leds = devm_kcalloc(&client->dev, chip->bits,
  478. sizeof(*pca955x_led), GFP_KERNEL);
  479. if (!pca955x->leds)
  480. return -ENOMEM;
  481. i2c_set_clientdata(client, pca955x);
  482. mutex_init(&pca955x->lock);
  483. pca955x->client = client;
  484. pca955x->chipdef = chip;
  485. init_data.devname_mandatory = false;
  486. init_data.devicename = "pca955x";
  487. for (i = 0; i < chip->bits; i++) {
  488. pca955x_led = &pca955x->leds[i];
  489. pca955x_led->led_num = i;
  490. pca955x_led->pca955x = pca955x;
  491. pca955x_led->type = pdata->leds[i].type;
  492. switch (pca955x_led->type) {
  493. case PCA955X_TYPE_NONE:
  494. case PCA955X_TYPE_GPIO:
  495. break;
  496. case PCA955X_TYPE_LED:
  497. led = &pca955x_led->led_cdev;
  498. led->brightness_set_blocking = pca955x_led_set;
  499. led->brightness_get = pca955x_led_get;
  500. if (pdata->leds[i].default_state ==
  501. LEDS_GPIO_DEFSTATE_OFF) {
  502. err = pca955x_led_set(led, LED_OFF);
  503. if (err)
  504. return err;
  505. } else if (pdata->leds[i].default_state ==
  506. LEDS_GPIO_DEFSTATE_ON) {
  507. err = pca955x_led_set(led, LED_FULL);
  508. if (err)
  509. return err;
  510. }
  511. init_data.fwnode = pdata->leds[i].fwnode;
  512. if (is_of_node(init_data.fwnode)) {
  513. if (to_of_node(init_data.fwnode)->name[0] ==
  514. '\0')
  515. set_default_label = true;
  516. else
  517. set_default_label = false;
  518. } else {
  519. set_default_label = true;
  520. }
  521. if (set_default_label) {
  522. snprintf(default_label, sizeof(default_label),
  523. "%d", i);
  524. init_data.default_label = default_label;
  525. } else {
  526. init_data.default_label = NULL;
  527. }
  528. err = devm_led_classdev_register_ext(&client->dev, led,
  529. &init_data);
  530. if (err)
  531. return err;
  532. set_bit(i, &pca955x->active_pins);
  533. /*
  534. * For default-state == "keep", let the core update the
  535. * brightness from the hardware, then check the
  536. * brightness to see if it's using PWM1. If so, PWM1
  537. * should not be written below.
  538. */
  539. if (pdata->leds[i].default_state ==
  540. LEDS_GPIO_DEFSTATE_KEEP) {
  541. if (led->brightness != LED_FULL &&
  542. led->brightness != LED_OFF &&
  543. led->brightness != LED_HALF)
  544. keep_pwm = true;
  545. }
  546. }
  547. }
  548. /* PWM0 is used for half brightness or 50% duty cycle */
  549. err = pca955x_write_pwm(client, 0, 255 - LED_HALF);
  550. if (err)
  551. return err;
  552. if (!keep_pwm) {
  553. /* PWM1 is used for variable brightness, default to OFF */
  554. err = pca955x_write_pwm(client, 1, 0);
  555. if (err)
  556. return err;
  557. }
  558. /* Set to fast frequency so we do not see flashing */
  559. err = pca955x_write_psc(client, 0, 0);
  560. if (err)
  561. return err;
  562. err = pca955x_write_psc(client, 1, 0);
  563. if (err)
  564. return err;
  565. #ifdef CONFIG_LEDS_PCA955X_GPIO
  566. pca955x->gpio.label = "gpio-pca955x";
  567. pca955x->gpio.direction_input = pca955x_gpio_direction_input;
  568. pca955x->gpio.direction_output = pca955x_gpio_direction_output;
  569. pca955x->gpio.set = pca955x_gpio_set_value;
  570. pca955x->gpio.get = pca955x_gpio_get_value;
  571. pca955x->gpio.request = pca955x_gpio_request_pin;
  572. pca955x->gpio.free = pca955x_gpio_free_pin;
  573. pca955x->gpio.can_sleep = 1;
  574. pca955x->gpio.base = -1;
  575. pca955x->gpio.ngpio = chip->bits;
  576. pca955x->gpio.parent = &client->dev;
  577. pca955x->gpio.owner = THIS_MODULE;
  578. err = devm_gpiochip_add_data(&client->dev, &pca955x->gpio,
  579. pca955x);
  580. if (err) {
  581. /* Use data->gpio.dev as a flag for freeing gpiochip */
  582. pca955x->gpio.parent = NULL;
  583. dev_warn(&client->dev, "could not add gpiochip\n");
  584. return err;
  585. }
  586. dev_info(&client->dev, "gpios %i...%i\n",
  587. pca955x->gpio.base, pca955x->gpio.base +
  588. pca955x->gpio.ngpio - 1);
  589. #endif
  590. return 0;
  591. }
  592. static struct i2c_driver pca955x_driver = {
  593. .driver = {
  594. .name = "leds-pca955x",
  595. .of_match_table = of_pca955x_match,
  596. },
  597. .probe_new = pca955x_probe,
  598. .id_table = pca955x_id,
  599. };
  600. module_i2c_driver(pca955x_driver);
  601. MODULE_AUTHOR("Nate Case <[email protected]>");
  602. MODULE_DESCRIPTION("PCA955x LED driver");
  603. MODULE_LICENSE("GPL v2");