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