Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2007-2008 Extreme Engineering Solutions, Inc. |
| 3 | * |
| 4 | * Author: Nate Case <ncase@xes-inc.com> |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of version 2 of |
| 7 | * the GNU General Public License. See the file COPYING in the main |
| 8 | * directory of this archive for more details. |
| 9 | * |
| 10 | * LED driver for various PCA955x I2C LED drivers |
| 11 | * |
| 12 | * Supported devices: |
| 13 | * |
| 14 | * Device Description 7-bit slave address |
| 15 | * ------ ----------- ------------------- |
| 16 | * PCA9550 2-bit driver 0x60 .. 0x61 |
| 17 | * PCA9551 8-bit driver 0x60 .. 0x67 |
| 18 | * PCA9552 16-bit driver 0x60 .. 0x67 |
| 19 | * PCA9553/01 4-bit driver 0x62 |
| 20 | * PCA9553/02 4-bit driver 0x63 |
| 21 | * |
| 22 | * Philips PCA955x LED driver chips follow a register map as shown below: |
| 23 | * |
| 24 | * Control Register Description |
| 25 | * ---------------- ----------- |
| 26 | * 0x0 Input register 0 |
| 27 | * .. |
| 28 | * NUM_INPUT_REGS - 1 Last Input register X |
| 29 | * |
| 30 | * NUM_INPUT_REGS Frequency prescaler 0 |
| 31 | * NUM_INPUT_REGS + 1 PWM register 0 |
| 32 | * NUM_INPUT_REGS + 2 Frequency prescaler 1 |
| 33 | * NUM_INPUT_REGS + 3 PWM register 1 |
| 34 | * |
| 35 | * NUM_INPUT_REGS + 4 LED selector 0 |
| 36 | * NUM_INPUT_REGS + 4 |
| 37 | * + NUM_LED_REGS - 1 Last LED selector |
| 38 | * |
| 39 | * where NUM_INPUT_REGS and NUM_LED_REGS vary depending on how many |
| 40 | * bits the chip supports. |
| 41 | */ |
| 42 | |
Tin Huynh | 44b3e31 | 2016-12-02 11:39:13 +0700 | [diff] [blame] | 43 | #include <linux/acpi.h> |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 44 | #include <linux/ctype.h> |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame^] | 45 | #include <linux/delay.h> |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 46 | #include <linux/err.h> |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame^] | 47 | #include <linux/gpio.h> |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 48 | #include <linux/i2c.h> |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame^] | 49 | #include <linux/leds.h> |
| 50 | #include <linux/module.h> |
| 51 | #include <linux/of_device.h> |
| 52 | #include <linux/of.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame^] | 54 | #include <linux/string.h> |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 55 | |
| 56 | /* LED select registers determine the source that drives LED outputs */ |
| 57 | #define PCA955X_LS_LED_ON 0x0 /* Output LOW */ |
| 58 | #define PCA955X_LS_LED_OFF 0x1 /* Output HI-Z */ |
| 59 | #define PCA955X_LS_BLINK0 0x2 /* Blink at PWM0 rate */ |
| 60 | #define PCA955X_LS_BLINK1 0x3 /* Blink at PWM1 rate */ |
| 61 | |
| 62 | enum pca955x_type { |
| 63 | pca9550, |
| 64 | pca9551, |
| 65 | pca9552, |
| 66 | pca9553, |
| 67 | }; |
| 68 | |
| 69 | struct pca955x_chipdef { |
| 70 | int bits; |
| 71 | u8 slv_addr; /* 7-bit slave address mask */ |
| 72 | int slv_addr_shift; /* Number of bits to ignore */ |
| 73 | }; |
| 74 | |
| 75 | static struct pca955x_chipdef pca955x_chipdefs[] = { |
| 76 | [pca9550] = { |
| 77 | .bits = 2, |
| 78 | .slv_addr = /* 110000x */ 0x60, |
| 79 | .slv_addr_shift = 1, |
| 80 | }, |
| 81 | [pca9551] = { |
| 82 | .bits = 8, |
| 83 | .slv_addr = /* 1100xxx */ 0x60, |
| 84 | .slv_addr_shift = 3, |
| 85 | }, |
| 86 | [pca9552] = { |
| 87 | .bits = 16, |
| 88 | .slv_addr = /* 1100xxx */ 0x60, |
| 89 | .slv_addr_shift = 3, |
| 90 | }, |
| 91 | [pca9553] = { |
| 92 | .bits = 4, |
| 93 | .slv_addr = /* 110001x */ 0x62, |
| 94 | .slv_addr_shift = 1, |
| 95 | }, |
| 96 | }; |
| 97 | |
| 98 | static const struct i2c_device_id pca955x_id[] = { |
| 99 | { "pca9550", pca9550 }, |
| 100 | { "pca9551", pca9551 }, |
| 101 | { "pca9552", pca9552 }, |
| 102 | { "pca9553", pca9553 }, |
| 103 | { } |
| 104 | }; |
| 105 | MODULE_DEVICE_TABLE(i2c, pca955x_id); |
| 106 | |
Tin Huynh | 44b3e31 | 2016-12-02 11:39:13 +0700 | [diff] [blame] | 107 | static const struct acpi_device_id pca955x_acpi_ids[] = { |
| 108 | { "PCA9550", pca9550 }, |
| 109 | { "PCA9551", pca9551 }, |
| 110 | { "PCA9552", pca9552 }, |
| 111 | { "PCA9553", pca9553 }, |
| 112 | { } |
| 113 | }; |
| 114 | MODULE_DEVICE_TABLE(acpi, pca955x_acpi_ids); |
| 115 | |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 116 | struct pca955x { |
| 117 | struct mutex lock; |
| 118 | struct pca955x_led *leds; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 119 | struct pca955x_chipdef *chipdef; |
| 120 | struct i2c_client *client; |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | struct pca955x_led { |
| 124 | struct pca955x *pca955x; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 125 | struct led_classdev led_cdev; |
| 126 | int led_num; /* 0 .. 15 potentially */ |
| 127 | char name[32]; |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame^] | 128 | const char *default_trigger; |
| 129 | }; |
| 130 | |
| 131 | struct pca955x_platform_data { |
| 132 | struct pca955x_led *leds; |
| 133 | int num_leds; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | /* 8 bits per input register */ |
| 137 | static inline int pca95xx_num_input_regs(int bits) |
| 138 | { |
| 139 | return (bits + 7) / 8; |
| 140 | } |
| 141 | |
| 142 | /* 4 bits per LED selector register */ |
| 143 | static inline int pca95xx_num_led_regs(int bits) |
| 144 | { |
| 145 | return (bits + 3) / 4; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Return an LED selector register value based on an existing one, with |
| 150 | * the appropriate 2-bit state value set for the given LED number (0-3). |
| 151 | */ |
| 152 | static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state) |
| 153 | { |
| 154 | return (oldval & (~(0x3 << (led_num << 1)))) | |
| 155 | ((state & 0x3) << (led_num << 1)); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Write to frequency prescaler register, used to program the |
| 160 | * period of the PWM output. period = (PSCx + 1) / 38 |
| 161 | */ |
| 162 | static void pca955x_write_psc(struct i2c_client *client, int n, u8 val) |
| 163 | { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 164 | struct pca955x *pca955x = i2c_get_clientdata(client); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 165 | |
| 166 | i2c_smbus_write_byte_data(client, |
| 167 | pca95xx_num_input_regs(pca955x->chipdef->bits) + 2*n, |
| 168 | val); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Write to PWM register, which determines the duty cycle of the |
| 173 | * output. LED is OFF when the count is less than the value of this |
| 174 | * register, and ON when it is greater. If PWMx == 0, LED is always OFF. |
| 175 | * |
| 176 | * Duty cycle is (256 - PWMx) / 256 |
| 177 | */ |
| 178 | static void pca955x_write_pwm(struct i2c_client *client, int n, u8 val) |
| 179 | { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 180 | struct pca955x *pca955x = i2c_get_clientdata(client); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 181 | |
| 182 | i2c_smbus_write_byte_data(client, |
| 183 | pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + 2*n, |
| 184 | val); |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Write to LED selector register, which determines the source that |
| 189 | * drives the LED output. |
| 190 | */ |
| 191 | static void pca955x_write_ls(struct i2c_client *client, int n, u8 val) |
| 192 | { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 193 | struct pca955x *pca955x = i2c_get_clientdata(client); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 194 | |
| 195 | i2c_smbus_write_byte_data(client, |
| 196 | pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n, |
| 197 | val); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Read the LED selector register, which determines the source that |
| 202 | * drives the LED output. |
| 203 | */ |
| 204 | static u8 pca955x_read_ls(struct i2c_client *client, int n) |
| 205 | { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 206 | struct pca955x *pca955x = i2c_get_clientdata(client); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 207 | |
| 208 | return (u8) i2c_smbus_read_byte_data(client, |
| 209 | pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n); |
| 210 | } |
| 211 | |
Andrew Lunn | c3482b8 | 2015-08-20 12:32:35 +0200 | [diff] [blame] | 212 | static int pca955x_led_set(struct led_classdev *led_cdev, |
| 213 | enum led_brightness value) |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 214 | { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 215 | struct pca955x_led *pca955x_led; |
| 216 | struct pca955x *pca955x; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 217 | u8 ls; |
| 218 | int chip_ls; /* which LSx to use (0-3 potentially) */ |
| 219 | int ls_led; /* which set of bits within LSx to use (0-3) */ |
| 220 | |
Andrew Lunn | c3482b8 | 2015-08-20 12:32:35 +0200 | [diff] [blame] | 221 | pca955x_led = container_of(led_cdev, struct pca955x_led, led_cdev); |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 222 | pca955x = pca955x_led->pca955x; |
| 223 | |
| 224 | chip_ls = pca955x_led->led_num / 4; |
| 225 | ls_led = pca955x_led->led_num % 4; |
| 226 | |
| 227 | mutex_lock(&pca955x->lock); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 228 | |
| 229 | ls = pca955x_read_ls(pca955x->client, chip_ls); |
| 230 | |
Andrew Lunn | c3482b8 | 2015-08-20 12:32:35 +0200 | [diff] [blame] | 231 | switch (value) { |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 232 | case LED_FULL: |
| 233 | ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON); |
| 234 | break; |
| 235 | case LED_OFF: |
| 236 | ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_OFF); |
| 237 | break; |
| 238 | case LED_HALF: |
| 239 | ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK0); |
| 240 | break; |
| 241 | default: |
| 242 | /* |
| 243 | * Use PWM1 for all other values. This has the unwanted |
| 244 | * side effect of making all LEDs on the chip share the |
| 245 | * same brightness level if set to a value other than |
| 246 | * OFF, HALF, or FULL. But, this is probably better than |
| 247 | * just turning off for all other values. |
| 248 | */ |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 249 | pca955x_write_pwm(pca955x->client, 1, |
Andrew Lunn | c3482b8 | 2015-08-20 12:32:35 +0200 | [diff] [blame] | 250 | 255 - value); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 251 | ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1); |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | pca955x_write_ls(pca955x->client, chip_ls, ls); |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 256 | |
| 257 | mutex_unlock(&pca955x->lock); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 258 | |
Andrew Lunn | c3482b8 | 2015-08-20 12:32:35 +0200 | [diff] [blame] | 259 | return 0; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame^] | 262 | #if IS_ENABLED(CONFIG_OF) |
| 263 | static struct pca955x_platform_data * |
| 264 | pca955x_pdata_of_init(struct i2c_client *client, struct pca955x_chipdef *chip) |
| 265 | { |
| 266 | struct device_node *np = client->dev.of_node; |
| 267 | struct device_node *child; |
| 268 | struct pca955x_platform_data *pdata; |
| 269 | int count; |
| 270 | |
| 271 | count = of_get_child_count(np); |
| 272 | if (!count || count > chip->bits) |
| 273 | return ERR_PTR(-ENODEV); |
| 274 | |
| 275 | pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); |
| 276 | if (!pdata) |
| 277 | return ERR_PTR(-ENOMEM); |
| 278 | |
| 279 | pdata->leds = devm_kzalloc(&client->dev, |
| 280 | sizeof(struct pca955x_led) * chip->bits, |
| 281 | GFP_KERNEL); |
| 282 | if (!pdata->leds) |
| 283 | return ERR_PTR(-ENOMEM); |
| 284 | |
| 285 | for_each_child_of_node(np, child) { |
| 286 | const char *name; |
| 287 | u32 reg; |
| 288 | int res; |
| 289 | |
| 290 | res = of_property_read_u32(child, "reg", ®); |
| 291 | if ((res != 0) || (reg >= chip->bits)) |
| 292 | continue; |
| 293 | |
| 294 | if (of_property_read_string(child, "label", &name)) |
| 295 | name = child->name; |
| 296 | |
| 297 | snprintf(pdata->leds[reg].name, sizeof(pdata->leds[reg].name), |
| 298 | "%s", name); |
| 299 | |
| 300 | of_property_read_string(child, "linux,default-trigger", |
| 301 | &pdata->leds[reg].default_trigger); |
| 302 | } |
| 303 | |
| 304 | pdata->num_leds = chip->bits; |
| 305 | |
| 306 | return pdata; |
| 307 | } |
| 308 | |
| 309 | static const struct of_device_id of_pca955x_match[] = { |
| 310 | { .compatible = "nxp,pca9550", .data = (void *)pca9550 }, |
| 311 | { .compatible = "nxp,pca9551", .data = (void *)pca9551 }, |
| 312 | { .compatible = "nxp,pca9552", .data = (void *)pca9552 }, |
| 313 | { .compatible = "nxp,pca9553", .data = (void *)pca9553 }, |
| 314 | {}, |
| 315 | }; |
| 316 | |
| 317 | MODULE_DEVICE_TABLE(of, of_pca955x_match); |
| 318 | #else |
| 319 | static struct pca955x_platform_data * |
| 320 | pca955x_pdata_of_init(struct i2c_client *client, struct pca955x_chipdef *chip) |
| 321 | { |
| 322 | return ERR_PTR(-ENODEV); |
| 323 | } |
| 324 | #endif |
| 325 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 326 | static int pca955x_probe(struct i2c_client *client, |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 327 | const struct i2c_device_id *id) |
| 328 | { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 329 | struct pca955x *pca955x; |
| 330 | struct pca955x_led *pca955x_led; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 331 | struct pca955x_chipdef *chip; |
| 332 | struct i2c_adapter *adapter; |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 333 | int i, err; |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame^] | 334 | struct pca955x_platform_data *pdata; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 335 | |
Tin Huynh | 44b3e31 | 2016-12-02 11:39:13 +0700 | [diff] [blame] | 336 | if (id) { |
| 337 | chip = &pca955x_chipdefs[id->driver_data]; |
| 338 | } else { |
| 339 | const struct acpi_device_id *acpi_id; |
| 340 | |
| 341 | acpi_id = acpi_match_device(pca955x_acpi_ids, &client->dev); |
| 342 | if (!acpi_id) |
| 343 | return -ENODEV; |
| 344 | chip = &pca955x_chipdefs[acpi_id->driver_data]; |
| 345 | } |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 346 | adapter = to_i2c_adapter(client->dev.parent); |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 347 | pdata = dev_get_platdata(&client->dev); |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame^] | 348 | if (!pdata) { |
| 349 | pdata = pca955x_pdata_of_init(client, chip); |
| 350 | if (IS_ERR(pdata)) |
| 351 | return PTR_ERR(pdata); |
| 352 | } |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 353 | |
| 354 | /* Make sure the slave address / chip type combo given is possible */ |
| 355 | if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) != |
| 356 | chip->slv_addr) { |
| 357 | dev_err(&client->dev, "invalid slave address %02x\n", |
| 358 | client->addr); |
| 359 | return -ENODEV; |
| 360 | } |
| 361 | |
Sachin Kamat | b40b0c1 | 2012-11-25 12:21:47 +0530 | [diff] [blame] | 362 | dev_info(&client->dev, "leds-pca955x: Using %s %d-bit LED driver at " |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 363 | "slave address 0x%02x\n", |
Tin Huynh | 44b3e31 | 2016-12-02 11:39:13 +0700 | [diff] [blame] | 364 | client->name, chip->bits, client->addr); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 365 | |
Tin Huynh | aace34c | 2017-05-22 16:19:20 +0700 | [diff] [blame] | 366 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 367 | return -EIO; |
| 368 | |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame^] | 369 | if (pdata->num_leds != chip->bits) { |
| 370 | dev_err(&client->dev, |
| 371 | "board info claims %d LEDs on a %d-bit chip\n", |
| 372 | pdata->num_leds, chip->bits); |
| 373 | return -ENODEV; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 374 | } |
| 375 | |
Bryan Wu | 73759f6 | 2012-07-04 12:09:05 +0800 | [diff] [blame] | 376 | pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL); |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 377 | if (!pca955x) |
| 378 | return -ENOMEM; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 379 | |
Bryan Wu | 73759f6 | 2012-07-04 12:09:05 +0800 | [diff] [blame] | 380 | pca955x->leds = devm_kzalloc(&client->dev, |
| 381 | sizeof(*pca955x_led) * chip->bits, GFP_KERNEL); |
| 382 | if (!pca955x->leds) |
| 383 | return -ENOMEM; |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 384 | |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 385 | i2c_set_clientdata(client, pca955x); |
| 386 | |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 387 | mutex_init(&pca955x->lock); |
| 388 | pca955x->client = client; |
| 389 | pca955x->chipdef = chip; |
| 390 | |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 391 | for (i = 0; i < chip->bits; i++) { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 392 | pca955x_led = &pca955x->leds[i]; |
| 393 | pca955x_led->led_num = i; |
| 394 | pca955x_led->pca955x = pca955x; |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 395 | |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 396 | /* Platform data can specify LED names and default triggers */ |
| 397 | if (pdata) { |
| 398 | if (pdata->leds[i].name) |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 399 | snprintf(pca955x_led->name, |
| 400 | sizeof(pca955x_led->name), "pca955x:%s", |
| 401 | pdata->leds[i].name); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 402 | if (pdata->leds[i].default_trigger) |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 403 | pca955x_led->led_cdev.default_trigger = |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 404 | pdata->leds[i].default_trigger; |
| 405 | } else { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 406 | snprintf(pca955x_led->name, sizeof(pca955x_led->name), |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 407 | "pca955x:%d", i); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 408 | } |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 409 | |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 410 | pca955x_led->led_cdev.name = pca955x_led->name; |
Andrew Lunn | c3482b8 | 2015-08-20 12:32:35 +0200 | [diff] [blame] | 411 | pca955x_led->led_cdev.brightness_set_blocking = pca955x_led_set; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 412 | |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 413 | err = led_classdev_register(&client->dev, |
| 414 | &pca955x_led->led_cdev); |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 415 | if (err < 0) |
| 416 | goto exit; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* Turn off LEDs */ |
| 420 | for (i = 0; i < pca95xx_num_led_regs(chip->bits); i++) |
| 421 | pca955x_write_ls(client, i, 0x55); |
| 422 | |
| 423 | /* PWM0 is used for half brightness or 50% duty cycle */ |
| 424 | pca955x_write_pwm(client, 0, 255-LED_HALF); |
| 425 | |
| 426 | /* PWM1 is used for variable brightness, default to OFF */ |
| 427 | pca955x_write_pwm(client, 1, 0); |
| 428 | |
| 429 | /* Set to fast frequency so we do not see flashing */ |
| 430 | pca955x_write_psc(client, 0, 0); |
| 431 | pca955x_write_psc(client, 1, 0); |
| 432 | |
| 433 | return 0; |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 434 | |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 435 | exit: |
Andrew Lunn | c3482b8 | 2015-08-20 12:32:35 +0200 | [diff] [blame] | 436 | while (i--) |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 437 | led_classdev_unregister(&pca955x->leds[i].led_cdev); |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 438 | |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 439 | return err; |
| 440 | } |
| 441 | |
Bill Pemberton | 678e8a6 | 2012-11-19 13:26:00 -0500 | [diff] [blame] | 442 | static int pca955x_remove(struct i2c_client *client) |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 443 | { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 444 | struct pca955x *pca955x = i2c_get_clientdata(client); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 445 | int i; |
| 446 | |
Andrew Lunn | c3482b8 | 2015-08-20 12:32:35 +0200 | [diff] [blame] | 447 | for (i = 0; i < pca955x->chipdef->bits; i++) |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 448 | led_classdev_unregister(&pca955x->leds[i].led_cdev); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 449 | |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | static struct i2c_driver pca955x_driver = { |
| 454 | .driver = { |
| 455 | .name = "leds-pca955x", |
Tin Huynh | 44b3e31 | 2016-12-02 11:39:13 +0700 | [diff] [blame] | 456 | .acpi_match_table = ACPI_PTR(pca955x_acpi_ids), |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame^] | 457 | .of_match_table = of_match_ptr(of_pca955x_match), |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 458 | }, |
| 459 | .probe = pca955x_probe, |
Bill Pemberton | df07cf8 | 2012-11-19 13:20:20 -0500 | [diff] [blame] | 460 | .remove = pca955x_remove, |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 461 | .id_table = pca955x_id, |
| 462 | }; |
| 463 | |
Axel Lin | 09a0d18 | 2012-01-10 15:09:27 -0800 | [diff] [blame] | 464 | module_i2c_driver(pca955x_driver); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 465 | |
| 466 | MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>"); |
| 467 | MODULE_DESCRIPTION("PCA955x LED driver"); |
| 468 | MODULE_LICENSE("GPL v2"); |