leds-pca955x.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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/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. #define PCA955X_BLINK_DEFAULT_MS 1000
  61. enum pca955x_type {
  62. pca9550,
  63. pca9551,
  64. pca9552,
  65. ibm_pca9552,
  66. pca9553,
  67. };
  68. struct pca955x_chipdef {
  69. u8 bits;
  70. u8 slv_addr; /* 7-bit slave address mask */
  71. int slv_addr_shift; /* Number of bits to ignore */
  72. int blink_div; /* PSC divider */
  73. };
  74. static const struct pca955x_chipdef pca955x_chipdefs[] = {
  75. [pca9550] = {
  76. .bits = 2,
  77. .slv_addr = /* 110000x */ 0x60,
  78. .slv_addr_shift = 1,
  79. .blink_div = 44,
  80. },
  81. [pca9551] = {
  82. .bits = 8,
  83. .slv_addr = /* 1100xxx */ 0x60,
  84. .slv_addr_shift = 3,
  85. .blink_div = 38,
  86. },
  87. [pca9552] = {
  88. .bits = 16,
  89. .slv_addr = /* 1100xxx */ 0x60,
  90. .slv_addr_shift = 3,
  91. .blink_div = 44,
  92. },
  93. [ibm_pca9552] = {
  94. .bits = 16,
  95. .slv_addr = /* 0110xxx */ 0x30,
  96. .slv_addr_shift = 3,
  97. .blink_div = 44,
  98. },
  99. [pca9553] = {
  100. .bits = 4,
  101. .slv_addr = /* 110001x */ 0x62,
  102. .slv_addr_shift = 1,
  103. .blink_div = 44,
  104. },
  105. };
  106. struct pca955x {
  107. struct mutex lock;
  108. struct pca955x_led *leds;
  109. const struct pca955x_chipdef *chipdef;
  110. struct i2c_client *client;
  111. unsigned long active_blink;
  112. unsigned long active_pins;
  113. unsigned long blink_period;
  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. enum led_default_state default_state;
  124. struct fwnode_handle *fwnode;
  125. };
  126. #define led_to_pca955x(l) container_of(l, struct pca955x_led, led_cdev)
  127. struct pca955x_platform_data {
  128. struct pca955x_led *leds;
  129. int num_leds;
  130. };
  131. /* 8 bits per input register */
  132. static inline u8 pca955x_num_input_regs(u8 bits)
  133. {
  134. return (bits + 7) / 8;
  135. }
  136. /* 4 bits per LED selector register */
  137. static inline u8 pca955x_num_led_regs(u8 bits)
  138. {
  139. return (bits + 3) / 4;
  140. }
  141. /*
  142. * Return an LED selector register value based on an existing one, with
  143. * the appropriate 2-bit state value set for the given LED number (0-3).
  144. */
  145. static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
  146. {
  147. return (oldval & (~(0x3 << (led_num << 1)))) |
  148. ((state & 0x3) << (led_num << 1));
  149. }
  150. static inline int pca955x_ledstate(u8 ls, int led_num)
  151. {
  152. return (ls >> (led_num << 1)) & 0x3;
  153. }
  154. /*
  155. * Write to frequency prescaler register, used to program the
  156. * period of the PWM output. period = (PSCx + 1) / coeff
  157. * Where for pca9551 chips coeff = 38 and for all other chips coeff = 44
  158. */
  159. static int pca955x_write_psc(struct pca955x *pca955x, int n, u8 val)
  160. {
  161. u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + (2 * n);
  162. int ret;
  163. ret = i2c_smbus_write_byte_data(pca955x->client, cmd, val);
  164. if (ret < 0)
  165. dev_err(&pca955x->client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", __func__, n,
  166. val, ret);
  167. return ret;
  168. }
  169. /*
  170. * Write to PWM register, which determines the duty cycle of the
  171. * output. LED is OFF when the count is less than the value of this
  172. * register, and ON when it is greater. If PWMx == 0, LED is always OFF.
  173. *
  174. * Duty cycle is (256 - PWMx) / 256
  175. */
  176. static int pca955x_write_pwm(struct pca955x *pca955x, int n, u8 val)
  177. {
  178. u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
  179. int ret;
  180. ret = i2c_smbus_write_byte_data(pca955x->client, cmd, val);
  181. if (ret < 0)
  182. dev_err(&pca955x->client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", __func__, n,
  183. val, ret);
  184. return ret;
  185. }
  186. /*
  187. * Write to LED selector register, which determines the source that
  188. * drives the LED output.
  189. */
  190. static int pca955x_write_ls(struct pca955x *pca955x, int n, u8 val)
  191. {
  192. u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 4 + n;
  193. int ret;
  194. ret = i2c_smbus_write_byte_data(pca955x->client, cmd, val);
  195. if (ret < 0)
  196. dev_err(&pca955x->client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", __func__, n,
  197. 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 pca955x *pca955x, int n, u8 *val)
  205. {
  206. u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 4 + n;
  207. int ret;
  208. ret = i2c_smbus_read_byte_data(pca955x->client, cmd);
  209. if (ret < 0) {
  210. dev_err(&pca955x->client->dev, "%s: reg 0x%x, err %d\n", __func__, n, ret);
  211. return ret;
  212. }
  213. *val = (u8)ret;
  214. return 0;
  215. }
  216. static int pca955x_read_pwm(struct pca955x *pca955x, int n, u8 *val)
  217. {
  218. u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
  219. int ret;
  220. ret = i2c_smbus_read_byte_data(pca955x->client, cmd);
  221. if (ret < 0) {
  222. dev_err(&pca955x->client->dev, "%s: reg 0x%x, err %d\n", __func__, n, ret);
  223. return ret;
  224. }
  225. *val = (u8)ret;
  226. return 0;
  227. }
  228. static int pca955x_read_psc(struct pca955x *pca955x, int n, u8 *val)
  229. {
  230. int ret;
  231. u8 cmd;
  232. cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + (2 * n);
  233. ret = i2c_smbus_read_byte_data(pca955x->client, cmd);
  234. if (ret < 0) {
  235. dev_err(&pca955x->client->dev, "%s: reg 0x%x, err %d\n", __func__, n, ret);
  236. return ret;
  237. }
  238. *val = (u8)ret;
  239. return 0;
  240. }
  241. static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
  242. {
  243. struct pca955x_led *pca955x_led = led_to_pca955x(led_cdev);
  244. struct pca955x *pca955x = pca955x_led->pca955x;
  245. u8 ls, pwm;
  246. int ret;
  247. ret = pca955x_read_ls(pca955x, pca955x_led->led_num / 4, &ls);
  248. if (ret)
  249. return ret;
  250. switch (pca955x_ledstate(ls, pca955x_led->led_num % 4)) {
  251. case PCA955X_LS_LED_ON:
  252. case PCA955X_LS_BLINK0:
  253. ret = LED_FULL;
  254. break;
  255. case PCA955X_LS_LED_OFF:
  256. ret = LED_OFF;
  257. break;
  258. case PCA955X_LS_BLINK1:
  259. ret = pca955x_read_pwm(pca955x, 1, &pwm);
  260. if (ret)
  261. return ret;
  262. ret = 255 - pwm;
  263. break;
  264. }
  265. return ret;
  266. }
  267. static int pca955x_led_set(struct led_classdev *led_cdev,
  268. enum led_brightness value)
  269. {
  270. struct pca955x_led *pca955x_led = led_to_pca955x(led_cdev);
  271. struct pca955x *pca955x = pca955x_led->pca955x;
  272. int reg = pca955x_led->led_num / 4;
  273. int bit = pca955x_led->led_num % 4;
  274. u8 ls;
  275. int ret;
  276. mutex_lock(&pca955x->lock);
  277. ret = pca955x_read_ls(pca955x, reg, &ls);
  278. if (ret)
  279. goto out;
  280. if (test_bit(pca955x_led->led_num, &pca955x->active_blink)) {
  281. if (value == LED_OFF) {
  282. clear_bit(pca955x_led->led_num, &pca955x->active_blink);
  283. ls = pca955x_ledsel(ls, bit, PCA955X_LS_LED_OFF);
  284. } else {
  285. /* No variable brightness for blinking LEDs */
  286. goto out;
  287. }
  288. } else {
  289. switch (value) {
  290. case LED_FULL:
  291. ls = pca955x_ledsel(ls, bit, PCA955X_LS_LED_ON);
  292. break;
  293. case LED_OFF:
  294. ls = pca955x_ledsel(ls, bit, PCA955X_LS_LED_OFF);
  295. break;
  296. default:
  297. /*
  298. * Use PWM1 for all other values. This has the unwanted
  299. * side effect of making all LEDs on the chip share the
  300. * same brightness level if set to a value other than
  301. * OFF or FULL. But, this is probably better than just
  302. * turning off for all other values.
  303. */
  304. ret = pca955x_write_pwm(pca955x, 1, 255 - value);
  305. if (ret)
  306. goto out;
  307. ls = pca955x_ledsel(ls, bit, PCA955X_LS_BLINK1);
  308. break;
  309. }
  310. }
  311. ret = pca955x_write_ls(pca955x, reg, ls);
  312. out:
  313. mutex_unlock(&pca955x->lock);
  314. return ret;
  315. }
  316. static u8 pca955x_period_to_psc(struct pca955x *pca955x, unsigned long period)
  317. {
  318. /* psc register value = (blink period * coeff) - 1 */
  319. period *= pca955x->chipdef->blink_div;
  320. period /= MSEC_PER_SEC;
  321. period -= 1;
  322. return period;
  323. }
  324. static unsigned long pca955x_psc_to_period(struct pca955x *pca955x, u8 psc)
  325. {
  326. unsigned long period = psc;
  327. /* blink period = (psc register value + 1) / coeff */
  328. period += 1;
  329. period *= MSEC_PER_SEC;
  330. period /= pca955x->chipdef->blink_div;
  331. return period;
  332. }
  333. static int pca955x_led_blink(struct led_classdev *led_cdev,
  334. unsigned long *delay_on, unsigned long *delay_off)
  335. {
  336. struct pca955x_led *pca955x_led = led_to_pca955x(led_cdev);
  337. struct pca955x *pca955x = pca955x_led->pca955x;
  338. unsigned long period = *delay_on + *delay_off;
  339. int ret = 0;
  340. mutex_lock(&pca955x->lock);
  341. if (period) {
  342. if (*delay_on != *delay_off) {
  343. ret = -EINVAL;
  344. goto out;
  345. }
  346. if (period < pca955x_psc_to_period(pca955x, 0) ||
  347. period > pca955x_psc_to_period(pca955x, 0xff)) {
  348. ret = -EINVAL;
  349. goto out;
  350. }
  351. } else {
  352. period = pca955x->active_blink ? pca955x->blink_period :
  353. PCA955X_BLINK_DEFAULT_MS;
  354. }
  355. if (!pca955x->active_blink ||
  356. pca955x->active_blink == BIT(pca955x_led->led_num) ||
  357. pca955x->blink_period == period) {
  358. u8 psc = pca955x_period_to_psc(pca955x, period);
  359. if (!test_and_set_bit(pca955x_led->led_num,
  360. &pca955x->active_blink)) {
  361. u8 ls;
  362. int reg = pca955x_led->led_num / 4;
  363. int bit = pca955x_led->led_num % 4;
  364. ret = pca955x_read_ls(pca955x, reg, &ls);
  365. if (ret)
  366. goto out;
  367. ls = pca955x_ledsel(ls, bit, PCA955X_LS_BLINK0);
  368. ret = pca955x_write_ls(pca955x, reg, ls);
  369. if (ret)
  370. goto out;
  371. /*
  372. * Force 50% duty cycle to maintain the specified
  373. * blink rate.
  374. */
  375. ret = pca955x_write_pwm(pca955x, 0, 128);
  376. if (ret)
  377. goto out;
  378. }
  379. if (pca955x->blink_period != period) {
  380. pca955x->blink_period = period;
  381. ret = pca955x_write_psc(pca955x, 0, psc);
  382. if (ret)
  383. goto out;
  384. }
  385. period = pca955x_psc_to_period(pca955x, psc);
  386. period /= 2;
  387. *delay_on = period;
  388. *delay_off = period;
  389. } else {
  390. ret = -EBUSY;
  391. }
  392. out:
  393. mutex_unlock(&pca955x->lock);
  394. return ret;
  395. }
  396. #ifdef CONFIG_LEDS_PCA955X_GPIO
  397. /*
  398. * Read the INPUT register, which contains the state of LEDs.
  399. */
  400. static int pca955x_read_input(struct i2c_client *client, int n, u8 *val)
  401. {
  402. int ret = i2c_smbus_read_byte_data(client, n);
  403. if (ret < 0) {
  404. dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
  405. __func__, n, ret);
  406. return ret;
  407. }
  408. *val = (u8)ret;
  409. return 0;
  410. }
  411. static int pca955x_gpio_request_pin(struct gpio_chip *gc, unsigned int offset)
  412. {
  413. struct pca955x *pca955x = gpiochip_get_data(gc);
  414. return test_and_set_bit(offset, &pca955x->active_pins) ? -EBUSY : 0;
  415. }
  416. static void pca955x_gpio_free_pin(struct gpio_chip *gc, unsigned int offset)
  417. {
  418. struct pca955x *pca955x = gpiochip_get_data(gc);
  419. clear_bit(offset, &pca955x->active_pins);
  420. }
  421. static int pca955x_set_value(struct gpio_chip *gc, unsigned int offset,
  422. int val)
  423. {
  424. struct pca955x *pca955x = gpiochip_get_data(gc);
  425. struct pca955x_led *led = &pca955x->leds[offset];
  426. if (val)
  427. return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_HIGH);
  428. return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_LOW);
  429. }
  430. static int pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset,
  431. int val)
  432. {
  433. return pca955x_set_value(gc, offset, val);
  434. }
  435. static int pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset)
  436. {
  437. struct pca955x *pca955x = gpiochip_get_data(gc);
  438. struct pca955x_led *led = &pca955x->leds[offset];
  439. u8 reg = 0;
  440. /* There is nothing we can do about errors */
  441. pca955x_read_input(pca955x->client, led->led_num / 8, &reg);
  442. return !!(reg & (1 << (led->led_num % 8)));
  443. }
  444. static int pca955x_gpio_direction_input(struct gpio_chip *gc,
  445. unsigned int offset)
  446. {
  447. struct pca955x *pca955x = gpiochip_get_data(gc);
  448. struct pca955x_led *led = &pca955x->leds[offset];
  449. /* To use as input ensure pin is not driven. */
  450. return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_INPUT);
  451. }
  452. static int pca955x_gpio_direction_output(struct gpio_chip *gc,
  453. unsigned int offset, int val)
  454. {
  455. return pca955x_set_value(gc, offset, val);
  456. }
  457. #endif /* CONFIG_LEDS_PCA955X_GPIO */
  458. static struct pca955x_platform_data *
  459. pca955x_get_pdata(struct i2c_client *client, const struct pca955x_chipdef *chip)
  460. {
  461. struct pca955x_platform_data *pdata;
  462. struct pca955x_led *led;
  463. struct fwnode_handle *child;
  464. int count;
  465. count = device_get_child_node_count(&client->dev);
  466. if (count > chip->bits)
  467. return ERR_PTR(-ENODEV);
  468. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  469. if (!pdata)
  470. return ERR_PTR(-ENOMEM);
  471. pdata->leds = devm_kcalloc(&client->dev,
  472. chip->bits, sizeof(struct pca955x_led),
  473. GFP_KERNEL);
  474. if (!pdata->leds)
  475. return ERR_PTR(-ENOMEM);
  476. device_for_each_child_node(&client->dev, child) {
  477. u32 reg;
  478. int res;
  479. res = fwnode_property_read_u32(child, "reg", &reg);
  480. if ((res != 0) || (reg >= chip->bits))
  481. continue;
  482. led = &pdata->leds[reg];
  483. led->type = PCA955X_TYPE_LED;
  484. led->fwnode = child;
  485. led->default_state = led_init_default_state_get(child);
  486. fwnode_property_read_u32(child, "type", &led->type);
  487. }
  488. pdata->num_leds = chip->bits;
  489. return pdata;
  490. }
  491. static int pca955x_probe(struct i2c_client *client)
  492. {
  493. struct pca955x *pca955x;
  494. struct pca955x_led *pca955x_led;
  495. const struct pca955x_chipdef *chip;
  496. struct led_classdev *led;
  497. struct led_init_data init_data;
  498. struct i2c_adapter *adapter;
  499. u8 i, nls, psc0;
  500. u8 ls1[4];
  501. u8 ls2[4];
  502. struct pca955x_platform_data *pdata;
  503. bool keep_psc0 = false;
  504. bool set_default_label = false;
  505. char default_label[4];
  506. int bit, err, reg;
  507. chip = i2c_get_match_data(client);
  508. if (!chip)
  509. return dev_err_probe(&client->dev, -ENODEV, "unknown chip\n");
  510. adapter = client->adapter;
  511. pdata = dev_get_platdata(&client->dev);
  512. if (!pdata) {
  513. pdata = pca955x_get_pdata(client, chip);
  514. if (IS_ERR(pdata))
  515. return PTR_ERR(pdata);
  516. }
  517. /* Make sure the slave address / chip type combo given is possible */
  518. if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) !=
  519. chip->slv_addr) {
  520. dev_err(&client->dev, "invalid slave address %02x\n",
  521. client->addr);
  522. return -ENODEV;
  523. }
  524. dev_info(&client->dev, "Using %s %u-bit LED driver at slave address 0x%02x\n",
  525. client->name, chip->bits, client->addr);
  526. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  527. return -EIO;
  528. if (pdata->num_leds != chip->bits) {
  529. dev_err(&client->dev,
  530. "board info claims %d LEDs on a %u-bit chip\n",
  531. pdata->num_leds, chip->bits);
  532. return -ENODEV;
  533. }
  534. pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
  535. if (!pca955x)
  536. return -ENOMEM;
  537. pca955x->leds = devm_kcalloc(&client->dev, chip->bits,
  538. sizeof(*pca955x_led), GFP_KERNEL);
  539. if (!pca955x->leds)
  540. return -ENOMEM;
  541. i2c_set_clientdata(client, pca955x);
  542. mutex_init(&pca955x->lock);
  543. pca955x->client = client;
  544. pca955x->chipdef = chip;
  545. pca955x->blink_period = PCA955X_BLINK_DEFAULT_MS;
  546. init_data.devname_mandatory = false;
  547. init_data.devicename = "pca955x";
  548. nls = pca955x_num_led_regs(chip->bits);
  549. /* Use auto-increment feature to read all the LED selectors at once. */
  550. err = i2c_smbus_read_i2c_block_data(client,
  551. 0x10 | (pca955x_num_input_regs(chip->bits) + 4), nls,
  552. ls1);
  553. if (err < 0)
  554. return err;
  555. for (i = 0; i < nls; i++)
  556. ls2[i] = ls1[i];
  557. for (i = 0; i < chip->bits; i++) {
  558. pca955x_led = &pca955x->leds[i];
  559. pca955x_led->led_num = i;
  560. pca955x_led->pca955x = pca955x;
  561. pca955x_led->type = pdata->leds[i].type;
  562. switch (pca955x_led->type) {
  563. case PCA955X_TYPE_NONE:
  564. case PCA955X_TYPE_GPIO:
  565. break;
  566. case PCA955X_TYPE_LED:
  567. bit = i % 4;
  568. reg = i / 4;
  569. led = &pca955x_led->led_cdev;
  570. led->brightness_set_blocking = pca955x_led_set;
  571. led->brightness_get = pca955x_led_get;
  572. led->blink_set = pca955x_led_blink;
  573. if (pdata->leds[i].default_state == LEDS_DEFSTATE_OFF)
  574. ls2[reg] = pca955x_ledsel(ls2[reg], bit, PCA955X_LS_LED_OFF);
  575. else if (pdata->leds[i].default_state == LEDS_DEFSTATE_ON)
  576. ls2[reg] = pca955x_ledsel(ls2[reg], bit, PCA955X_LS_LED_ON);
  577. else if (pca955x_ledstate(ls2[reg], bit) == PCA955X_LS_BLINK0) {
  578. keep_psc0 = true;
  579. set_bit(i, &pca955x->active_blink);
  580. }
  581. init_data.fwnode = pdata->leds[i].fwnode;
  582. if (is_of_node(init_data.fwnode)) {
  583. if (to_of_node(init_data.fwnode)->name[0] ==
  584. '\0')
  585. set_default_label = true;
  586. else
  587. set_default_label = false;
  588. } else {
  589. set_default_label = true;
  590. }
  591. if (set_default_label) {
  592. snprintf(default_label, sizeof(default_label), "%hhu", i);
  593. init_data.default_label = default_label;
  594. } else {
  595. init_data.default_label = NULL;
  596. }
  597. err = devm_led_classdev_register_ext(&client->dev, led,
  598. &init_data);
  599. if (err)
  600. return err;
  601. set_bit(i, &pca955x->active_pins);
  602. }
  603. }
  604. for (i = 0; i < nls; i++) {
  605. if (ls1[i] != ls2[i]) {
  606. err = pca955x_write_ls(pca955x, i, ls2[i]);
  607. if (err)
  608. return err;
  609. }
  610. }
  611. if (keep_psc0) {
  612. err = pca955x_read_psc(pca955x, 0, &psc0);
  613. } else {
  614. psc0 = pca955x_period_to_psc(pca955x, pca955x->blink_period);
  615. err = pca955x_write_psc(pca955x, 0, psc0);
  616. }
  617. if (err)
  618. return err;
  619. pca955x->blink_period = pca955x_psc_to_period(pca955x, psc0);
  620. /* Set PWM1 to fast frequency so we do not see flashing */
  621. err = pca955x_write_psc(pca955x, 1, 0);
  622. if (err)
  623. return err;
  624. #ifdef CONFIG_LEDS_PCA955X_GPIO
  625. pca955x->gpio.label = "gpio-pca955x";
  626. pca955x->gpio.direction_input = pca955x_gpio_direction_input;
  627. pca955x->gpio.direction_output = pca955x_gpio_direction_output;
  628. pca955x->gpio.set = pca955x_gpio_set_value;
  629. pca955x->gpio.get = pca955x_gpio_get_value;
  630. pca955x->gpio.request = pca955x_gpio_request_pin;
  631. pca955x->gpio.free = pca955x_gpio_free_pin;
  632. pca955x->gpio.can_sleep = 1;
  633. pca955x->gpio.base = -1;
  634. pca955x->gpio.ngpio = chip->bits;
  635. pca955x->gpio.parent = &client->dev;
  636. pca955x->gpio.owner = THIS_MODULE;
  637. err = devm_gpiochip_add_data(&client->dev, &pca955x->gpio,
  638. pca955x);
  639. if (err) {
  640. /* Use data->gpio.dev as a flag for freeing gpiochip */
  641. pca955x->gpio.parent = NULL;
  642. dev_warn(&client->dev, "could not add gpiochip\n");
  643. return err;
  644. }
  645. dev_info(&client->dev, "gpios %i...%i\n",
  646. pca955x->gpio.base, pca955x->gpio.base +
  647. pca955x->gpio.ngpio - 1);
  648. #endif
  649. return 0;
  650. }
  651. static const struct i2c_device_id pca955x_id[] = {
  652. { "pca9550", (kernel_ulong_t)&pca955x_chipdefs[pca9550] },
  653. { "pca9551", (kernel_ulong_t)&pca955x_chipdefs[pca9551] },
  654. { "pca9552", (kernel_ulong_t)&pca955x_chipdefs[pca9552] },
  655. { "ibm-pca9552", (kernel_ulong_t)&pca955x_chipdefs[ibm_pca9552] },
  656. { "pca9553", (kernel_ulong_t)&pca955x_chipdefs[pca9553] },
  657. {}
  658. };
  659. MODULE_DEVICE_TABLE(i2c, pca955x_id);
  660. static const struct of_device_id of_pca955x_match[] = {
  661. { .compatible = "nxp,pca9550", .data = &pca955x_chipdefs[pca9550] },
  662. { .compatible = "nxp,pca9551", .data = &pca955x_chipdefs[pca9551] },
  663. { .compatible = "nxp,pca9552", .data = &pca955x_chipdefs[pca9552] },
  664. { .compatible = "ibm,pca9552", .data = &pca955x_chipdefs[ibm_pca9552] },
  665. { .compatible = "nxp,pca9553", .data = &pca955x_chipdefs[pca9553] },
  666. {}
  667. };
  668. MODULE_DEVICE_TABLE(of, of_pca955x_match);
  669. static struct i2c_driver pca955x_driver = {
  670. .driver = {
  671. .name = "leds-pca955x",
  672. .of_match_table = of_pca955x_match,
  673. },
  674. .probe = pca955x_probe,
  675. .id_table = pca955x_id,
  676. };
  677. module_i2c_driver(pca955x_driver);
  678. MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>");
  679. MODULE_DESCRIPTION("PCA955x LED driver");
  680. MODULE_LICENSE("GPL v2");