1500fe141SSamu Onkalo /* 2500fe141SSamu Onkalo * LP5521 LED chip driver. 3500fe141SSamu Onkalo * 4500fe141SSamu Onkalo * Copyright (C) 2010 Nokia Corporation 5500fe141SSamu Onkalo * 6500fe141SSamu Onkalo * Contact: Samu Onkalo <samu.p.onkalo@nokia.com> 7500fe141SSamu Onkalo * 8500fe141SSamu Onkalo * This program is free software; you can redistribute it and/or 9500fe141SSamu Onkalo * modify it under the terms of the GNU General Public License 10500fe141SSamu Onkalo * version 2 as published by the Free Software Foundation. 11500fe141SSamu Onkalo * 12500fe141SSamu Onkalo * This program is distributed in the hope that it will be useful, but 13500fe141SSamu Onkalo * WITHOUT ANY WARRANTY; without even the implied warranty of 14500fe141SSamu Onkalo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15500fe141SSamu Onkalo * General Public License for more details. 16500fe141SSamu Onkalo * 17500fe141SSamu Onkalo * You should have received a copy of the GNU General Public License 18500fe141SSamu Onkalo * along with this program; if not, write to the Free Software 19500fe141SSamu Onkalo * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 20500fe141SSamu Onkalo * 02110-1301 USA 21500fe141SSamu Onkalo */ 22500fe141SSamu Onkalo 23500fe141SSamu Onkalo #include <linux/module.h> 24500fe141SSamu Onkalo #include <linux/init.h> 25500fe141SSamu Onkalo #include <linux/i2c.h> 26500fe141SSamu Onkalo #include <linux/mutex.h> 27500fe141SSamu Onkalo #include <linux/gpio.h> 28500fe141SSamu Onkalo #include <linux/interrupt.h> 29500fe141SSamu Onkalo #include <linux/delay.h> 30500fe141SSamu Onkalo #include <linux/ctype.h> 31500fe141SSamu Onkalo #include <linux/spinlock.h> 32500fe141SSamu Onkalo #include <linux/wait.h> 33500fe141SSamu Onkalo #include <linux/leds.h> 34500fe141SSamu Onkalo #include <linux/leds-lp5521.h> 35500fe141SSamu Onkalo #include <linux/workqueue.h> 36500fe141SSamu Onkalo #include <linux/slab.h> 37500fe141SSamu Onkalo 38500fe141SSamu Onkalo #define LP5521_PROGRAM_LENGTH 32 /* in bytes */ 39500fe141SSamu Onkalo 40500fe141SSamu Onkalo #define LP5521_MAX_LEDS 3 /* Maximum number of LEDs */ 41500fe141SSamu Onkalo #define LP5521_MAX_ENGINES 3 /* Maximum number of engines */ 42500fe141SSamu Onkalo 43500fe141SSamu Onkalo #define LP5521_ENG_MASK_BASE 0x30 /* 00110000 */ 44500fe141SSamu Onkalo #define LP5521_ENG_STATUS_MASK 0x07 /* 00000111 */ 45500fe141SSamu Onkalo 46500fe141SSamu Onkalo #define LP5521_CMD_LOAD 0x15 /* 00010101 */ 47500fe141SSamu Onkalo #define LP5521_CMD_RUN 0x2a /* 00101010 */ 48500fe141SSamu Onkalo #define LP5521_CMD_DIRECT 0x3f /* 00111111 */ 49500fe141SSamu Onkalo #define LP5521_CMD_DISABLED 0x00 /* 00000000 */ 50500fe141SSamu Onkalo 51500fe141SSamu Onkalo /* Registers */ 52500fe141SSamu Onkalo #define LP5521_REG_ENABLE 0x00 53500fe141SSamu Onkalo #define LP5521_REG_OP_MODE 0x01 54500fe141SSamu Onkalo #define LP5521_REG_R_PWM 0x02 55500fe141SSamu Onkalo #define LP5521_REG_G_PWM 0x03 56500fe141SSamu Onkalo #define LP5521_REG_B_PWM 0x04 57500fe141SSamu Onkalo #define LP5521_REG_R_CURRENT 0x05 58500fe141SSamu Onkalo #define LP5521_REG_G_CURRENT 0x06 59500fe141SSamu Onkalo #define LP5521_REG_B_CURRENT 0x07 60500fe141SSamu Onkalo #define LP5521_REG_CONFIG 0x08 61500fe141SSamu Onkalo #define LP5521_REG_R_CHANNEL_PC 0x09 62500fe141SSamu Onkalo #define LP5521_REG_G_CHANNEL_PC 0x0A 63500fe141SSamu Onkalo #define LP5521_REG_B_CHANNEL_PC 0x0B 64500fe141SSamu Onkalo #define LP5521_REG_STATUS 0x0C 65500fe141SSamu Onkalo #define LP5521_REG_RESET 0x0D 66500fe141SSamu Onkalo #define LP5521_REG_GPO 0x0E 67500fe141SSamu Onkalo #define LP5521_REG_R_PROG_MEM 0x10 68500fe141SSamu Onkalo #define LP5521_REG_G_PROG_MEM 0x30 69500fe141SSamu Onkalo #define LP5521_REG_B_PROG_MEM 0x50 70500fe141SSamu Onkalo 71500fe141SSamu Onkalo #define LP5521_PROG_MEM_BASE LP5521_REG_R_PROG_MEM 72500fe141SSamu Onkalo #define LP5521_PROG_MEM_SIZE 0x20 73500fe141SSamu Onkalo 74500fe141SSamu Onkalo /* Base register to set LED current */ 75500fe141SSamu Onkalo #define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT 76500fe141SSamu Onkalo 77500fe141SSamu Onkalo /* Base register to set the brightness */ 78500fe141SSamu Onkalo #define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM 79500fe141SSamu Onkalo 80500fe141SSamu Onkalo /* Bits in ENABLE register */ 81500fe141SSamu Onkalo #define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */ 82500fe141SSamu Onkalo #define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */ 83500fe141SSamu Onkalo #define LP5521_EXEC_RUN 0x2A 84500fe141SSamu Onkalo 85500fe141SSamu Onkalo /* Status */ 86500fe141SSamu Onkalo #define LP5521_EXT_CLK_USED 0x08 87500fe141SSamu Onkalo 88b3c49c05SSrinidhi KASAGAR /* default R channel current register value */ 89b3c49c05SSrinidhi KASAGAR #define LP5521_REG_R_CURR_DEFAULT 0xAF 90b3c49c05SSrinidhi KASAGAR 91500fe141SSamu Onkalo struct lp5521_engine { 92500fe141SSamu Onkalo int id; 93500fe141SSamu Onkalo u8 mode; 94500fe141SSamu Onkalo u8 prog_page; 95500fe141SSamu Onkalo u8 engine_mask; 96500fe141SSamu Onkalo }; 97500fe141SSamu Onkalo 98500fe141SSamu Onkalo struct lp5521_led { 99500fe141SSamu Onkalo int id; 100500fe141SSamu Onkalo u8 chan_nr; 101500fe141SSamu Onkalo u8 led_current; 102500fe141SSamu Onkalo u8 max_current; 103500fe141SSamu Onkalo struct led_classdev cdev; 104500fe141SSamu Onkalo struct work_struct brightness_work; 105500fe141SSamu Onkalo u8 brightness; 106500fe141SSamu Onkalo }; 107500fe141SSamu Onkalo 108500fe141SSamu Onkalo struct lp5521_chip { 109500fe141SSamu Onkalo struct lp5521_platform_data *pdata; 110500fe141SSamu Onkalo struct mutex lock; /* Serialize control */ 111500fe141SSamu Onkalo struct i2c_client *client; 112500fe141SSamu Onkalo struct lp5521_engine engines[LP5521_MAX_ENGINES]; 113500fe141SSamu Onkalo struct lp5521_led leds[LP5521_MAX_LEDS]; 114500fe141SSamu Onkalo u8 num_channels; 115500fe141SSamu Onkalo u8 num_leds; 116500fe141SSamu Onkalo }; 117500fe141SSamu Onkalo 1189fdb18b6SSamu Onkalo static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev) 1199fdb18b6SSamu Onkalo { 1209fdb18b6SSamu Onkalo return container_of(cdev, struct lp5521_led, cdev); 1219fdb18b6SSamu Onkalo } 1229fdb18b6SSamu Onkalo 1239fdb18b6SSamu Onkalo static inline struct lp5521_chip *engine_to_lp5521(struct lp5521_engine *engine) 1249fdb18b6SSamu Onkalo { 1259fdb18b6SSamu Onkalo return container_of(engine, struct lp5521_chip, 1269fdb18b6SSamu Onkalo engines[engine->id - 1]); 1279fdb18b6SSamu Onkalo } 1289fdb18b6SSamu Onkalo 1299fdb18b6SSamu Onkalo static inline struct lp5521_chip *led_to_lp5521(struct lp5521_led *led) 1309fdb18b6SSamu Onkalo { 1319fdb18b6SSamu Onkalo return container_of(led, struct lp5521_chip, 1329fdb18b6SSamu Onkalo leds[led->id]); 1339fdb18b6SSamu Onkalo } 134500fe141SSamu Onkalo 135500fe141SSamu Onkalo static void lp5521_led_brightness_work(struct work_struct *work); 136500fe141SSamu Onkalo 137500fe141SSamu Onkalo static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value) 138500fe141SSamu Onkalo { 139500fe141SSamu Onkalo return i2c_smbus_write_byte_data(client, reg, value); 140500fe141SSamu Onkalo } 141500fe141SSamu Onkalo 142500fe141SSamu Onkalo static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf) 143500fe141SSamu Onkalo { 144500fe141SSamu Onkalo s32 ret; 145500fe141SSamu Onkalo 146500fe141SSamu Onkalo ret = i2c_smbus_read_byte_data(client, reg); 147500fe141SSamu Onkalo if (ret < 0) 148500fe141SSamu Onkalo return -EIO; 149500fe141SSamu Onkalo 150500fe141SSamu Onkalo *buf = ret; 151500fe141SSamu Onkalo return 0; 152500fe141SSamu Onkalo } 153500fe141SSamu Onkalo 154500fe141SSamu Onkalo static int lp5521_set_engine_mode(struct lp5521_engine *engine, u8 mode) 155500fe141SSamu Onkalo { 156500fe141SSamu Onkalo struct lp5521_chip *chip = engine_to_lp5521(engine); 157500fe141SSamu Onkalo struct i2c_client *client = chip->client; 158500fe141SSamu Onkalo int ret; 159500fe141SSamu Onkalo u8 engine_state; 160500fe141SSamu Onkalo 161500fe141SSamu Onkalo /* Only transition between RUN and DIRECT mode are handled here */ 162500fe141SSamu Onkalo if (mode == LP5521_CMD_LOAD) 163500fe141SSamu Onkalo return 0; 164500fe141SSamu Onkalo 165500fe141SSamu Onkalo if (mode == LP5521_CMD_DISABLED) 166500fe141SSamu Onkalo mode = LP5521_CMD_DIRECT; 167500fe141SSamu Onkalo 168500fe141SSamu Onkalo ret = lp5521_read(client, LP5521_REG_OP_MODE, &engine_state); 169fa0ea0e1SAxel Lin if (ret < 0) 170fa0ea0e1SAxel Lin return ret; 171500fe141SSamu Onkalo 172500fe141SSamu Onkalo /* set mode only for this engine */ 173500fe141SSamu Onkalo engine_state &= ~(engine->engine_mask); 174500fe141SSamu Onkalo mode &= engine->engine_mask; 175500fe141SSamu Onkalo engine_state |= mode; 176fa0ea0e1SAxel Lin return lp5521_write(client, LP5521_REG_OP_MODE, engine_state); 177500fe141SSamu Onkalo } 178500fe141SSamu Onkalo 179500fe141SSamu Onkalo static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern) 180500fe141SSamu Onkalo { 181500fe141SSamu Onkalo struct lp5521_chip *chip = engine_to_lp5521(eng); 182500fe141SSamu Onkalo struct i2c_client *client = chip->client; 183500fe141SSamu Onkalo int ret; 184500fe141SSamu Onkalo int addr; 185500fe141SSamu Onkalo u8 mode; 186500fe141SSamu Onkalo 187500fe141SSamu Onkalo /* move current engine to direct mode and remember the state */ 188500fe141SSamu Onkalo ret = lp5521_set_engine_mode(eng, LP5521_CMD_DIRECT); 18909c76b0fSSamu Onkalo /* Mode change requires min 500 us delay. 1 - 2 ms with margin */ 19009c76b0fSSamu Onkalo usleep_range(1000, 2000); 191500fe141SSamu Onkalo ret |= lp5521_read(client, LP5521_REG_OP_MODE, &mode); 192500fe141SSamu Onkalo 193500fe141SSamu Onkalo /* For loading, all the engines to load mode */ 194500fe141SSamu Onkalo lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT); 19509c76b0fSSamu Onkalo /* Mode change requires min 500 us delay. 1 - 2 ms with margin */ 19609c76b0fSSamu Onkalo usleep_range(1000, 2000); 197500fe141SSamu Onkalo lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_LOAD); 19809c76b0fSSamu Onkalo /* Mode change requires min 500 us delay. 1 - 2 ms with margin */ 19909c76b0fSSamu Onkalo usleep_range(1000, 2000); 200500fe141SSamu Onkalo 201500fe141SSamu Onkalo addr = LP5521_PROG_MEM_BASE + eng->prog_page * LP5521_PROG_MEM_SIZE; 202500fe141SSamu Onkalo i2c_smbus_write_i2c_block_data(client, 203500fe141SSamu Onkalo addr, 204500fe141SSamu Onkalo LP5521_PROG_MEM_SIZE, 205500fe141SSamu Onkalo pattern); 206500fe141SSamu Onkalo 207500fe141SSamu Onkalo ret |= lp5521_write(client, LP5521_REG_OP_MODE, mode); 208500fe141SSamu Onkalo return ret; 209500fe141SSamu Onkalo } 210500fe141SSamu Onkalo 211500fe141SSamu Onkalo static int lp5521_set_led_current(struct lp5521_chip *chip, int led, u8 curr) 212500fe141SSamu Onkalo { 213500fe141SSamu Onkalo return lp5521_write(chip->client, 214500fe141SSamu Onkalo LP5521_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr, 215500fe141SSamu Onkalo curr); 216500fe141SSamu Onkalo } 217500fe141SSamu Onkalo 218d4e7ad03SSamu Onkalo static void lp5521_init_engine(struct lp5521_chip *chip) 219500fe141SSamu Onkalo { 220500fe141SSamu Onkalo int i; 221500fe141SSamu Onkalo for (i = 0; i < ARRAY_SIZE(chip->engines); i++) { 222500fe141SSamu Onkalo chip->engines[i].id = i + 1; 223500fe141SSamu Onkalo chip->engines[i].engine_mask = LP5521_ENG_MASK_BASE >> (i * 2); 224500fe141SSamu Onkalo chip->engines[i].prog_page = i; 225500fe141SSamu Onkalo } 226500fe141SSamu Onkalo } 227500fe141SSamu Onkalo 228d4e7ad03SSamu Onkalo static int lp5521_configure(struct i2c_client *client) 229500fe141SSamu Onkalo { 230500fe141SSamu Onkalo struct lp5521_chip *chip = i2c_get_clientdata(client); 231500fe141SSamu Onkalo int ret; 232*3b49aacdSKim, Milo u8 cfg; 233500fe141SSamu Onkalo 234d4e7ad03SSamu Onkalo lp5521_init_engine(chip); 235500fe141SSamu Onkalo 236500fe141SSamu Onkalo /* Set all PWMs to direct control mode */ 237500fe141SSamu Onkalo ret = lp5521_write(client, LP5521_REG_OP_MODE, 0x3F); 238500fe141SSamu Onkalo 239*3b49aacdSKim, Milo cfg = chip->pdata->update_config ? 240*3b49aacdSKim, Milo : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT); 241*3b49aacdSKim, Milo ret |= lp5521_write(client, LP5521_REG_CONFIG, cfg); 242500fe141SSamu Onkalo 243500fe141SSamu Onkalo /* Initialize all channels PWM to zero -> leds off */ 244500fe141SSamu Onkalo ret |= lp5521_write(client, LP5521_REG_R_PWM, 0); 245500fe141SSamu Onkalo ret |= lp5521_write(client, LP5521_REG_G_PWM, 0); 246500fe141SSamu Onkalo ret |= lp5521_write(client, LP5521_REG_B_PWM, 0); 247500fe141SSamu Onkalo 248500fe141SSamu Onkalo /* Set engines are set to run state when OP_MODE enables engines */ 249500fe141SSamu Onkalo ret |= lp5521_write(client, LP5521_REG_ENABLE, 250500fe141SSamu Onkalo LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM | 251500fe141SSamu Onkalo LP5521_EXEC_RUN); 25209c76b0fSSamu Onkalo /* enable takes 500us. 1 - 2 ms leaves some margin */ 25309c76b0fSSamu Onkalo usleep_range(1000, 2000); 254500fe141SSamu Onkalo 255500fe141SSamu Onkalo return ret; 256500fe141SSamu Onkalo } 257500fe141SSamu Onkalo 258500fe141SSamu Onkalo static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf) 259500fe141SSamu Onkalo { 260500fe141SSamu Onkalo int ret; 261500fe141SSamu Onkalo u8 status; 262500fe141SSamu Onkalo 263500fe141SSamu Onkalo ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status); 264500fe141SSamu Onkalo if (ret < 0) 265500fe141SSamu Onkalo return ret; 266500fe141SSamu Onkalo 267500fe141SSamu Onkalo /* Check that ext clock is really in use if requested */ 268500fe141SSamu Onkalo if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT) 269500fe141SSamu Onkalo if ((status & LP5521_EXT_CLK_USED) == 0) 270500fe141SSamu Onkalo return -EIO; 271500fe141SSamu Onkalo return 0; 272500fe141SSamu Onkalo } 273500fe141SSamu Onkalo 274500fe141SSamu Onkalo static void lp5521_set_brightness(struct led_classdev *cdev, 275500fe141SSamu Onkalo enum led_brightness brightness) 276500fe141SSamu Onkalo { 277500fe141SSamu Onkalo struct lp5521_led *led = cdev_to_led(cdev); 278500fe141SSamu Onkalo led->brightness = (u8)brightness; 279500fe141SSamu Onkalo schedule_work(&led->brightness_work); 280500fe141SSamu Onkalo } 281500fe141SSamu Onkalo 282500fe141SSamu Onkalo static void lp5521_led_brightness_work(struct work_struct *work) 283500fe141SSamu Onkalo { 284500fe141SSamu Onkalo struct lp5521_led *led = container_of(work, 285500fe141SSamu Onkalo struct lp5521_led, 286500fe141SSamu Onkalo brightness_work); 287500fe141SSamu Onkalo struct lp5521_chip *chip = led_to_lp5521(led); 288500fe141SSamu Onkalo struct i2c_client *client = chip->client; 289500fe141SSamu Onkalo 290500fe141SSamu Onkalo mutex_lock(&chip->lock); 291500fe141SSamu Onkalo lp5521_write(client, LP5521_REG_LED_PWM_BASE + led->chan_nr, 292500fe141SSamu Onkalo led->brightness); 293500fe141SSamu Onkalo mutex_unlock(&chip->lock); 294500fe141SSamu Onkalo } 295500fe141SSamu Onkalo 296500fe141SSamu Onkalo /* Detect the chip by setting its ENABLE register and reading it back. */ 297500fe141SSamu Onkalo static int lp5521_detect(struct i2c_client *client) 298500fe141SSamu Onkalo { 299500fe141SSamu Onkalo int ret; 300500fe141SSamu Onkalo u8 buf; 301500fe141SSamu Onkalo 302500fe141SSamu Onkalo ret = lp5521_write(client, LP5521_REG_ENABLE, 303500fe141SSamu Onkalo LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM); 304500fe141SSamu Onkalo if (ret) 305500fe141SSamu Onkalo return ret; 30609c76b0fSSamu Onkalo /* enable takes 500us. 1 - 2 ms leaves some margin */ 30709c76b0fSSamu Onkalo usleep_range(1000, 2000); 308500fe141SSamu Onkalo ret = lp5521_read(client, LP5521_REG_ENABLE, &buf); 309500fe141SSamu Onkalo if (ret) 310500fe141SSamu Onkalo return ret; 311500fe141SSamu Onkalo if (buf != (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)) 312500fe141SSamu Onkalo return -ENODEV; 313500fe141SSamu Onkalo 314500fe141SSamu Onkalo return 0; 315500fe141SSamu Onkalo } 316500fe141SSamu Onkalo 317500fe141SSamu Onkalo /* Set engine mode and create appropriate sysfs attributes, if required. */ 318500fe141SSamu Onkalo static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode) 319500fe141SSamu Onkalo { 320500fe141SSamu Onkalo int ret = 0; 321500fe141SSamu Onkalo 322500fe141SSamu Onkalo /* if in that mode already do nothing, except for run */ 323500fe141SSamu Onkalo if (mode == engine->mode && mode != LP5521_CMD_RUN) 324500fe141SSamu Onkalo return 0; 325500fe141SSamu Onkalo 326500fe141SSamu Onkalo if (mode == LP5521_CMD_RUN) { 327500fe141SSamu Onkalo ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN); 328500fe141SSamu Onkalo } else if (mode == LP5521_CMD_LOAD) { 329500fe141SSamu Onkalo lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED); 330500fe141SSamu Onkalo lp5521_set_engine_mode(engine, LP5521_CMD_LOAD); 331500fe141SSamu Onkalo } else if (mode == LP5521_CMD_DISABLED) { 332500fe141SSamu Onkalo lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED); 333500fe141SSamu Onkalo } 334500fe141SSamu Onkalo 335500fe141SSamu Onkalo engine->mode = mode; 336500fe141SSamu Onkalo 337500fe141SSamu Onkalo return ret; 338500fe141SSamu Onkalo } 339500fe141SSamu Onkalo 340500fe141SSamu Onkalo static int lp5521_do_store_load(struct lp5521_engine *engine, 341500fe141SSamu Onkalo const char *buf, size_t len) 342500fe141SSamu Onkalo { 343500fe141SSamu Onkalo struct lp5521_chip *chip = engine_to_lp5521(engine); 344500fe141SSamu Onkalo struct i2c_client *client = chip->client; 345500fe141SSamu Onkalo int ret, nrchars, offset = 0, i = 0; 346500fe141SSamu Onkalo char c[3]; 347500fe141SSamu Onkalo unsigned cmd; 348500fe141SSamu Onkalo u8 pattern[LP5521_PROGRAM_LENGTH] = {0}; 349500fe141SSamu Onkalo 350500fe141SSamu Onkalo while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) { 351500fe141SSamu Onkalo /* separate sscanfs because length is working only for %s */ 352500fe141SSamu Onkalo ret = sscanf(buf + offset, "%2s%n ", c, &nrchars); 3532260209cSVasiliy Kulikov if (ret != 2) 3542260209cSVasiliy Kulikov goto fail; 355500fe141SSamu Onkalo ret = sscanf(c, "%2x", &cmd); 356500fe141SSamu Onkalo if (ret != 1) 357500fe141SSamu Onkalo goto fail; 358500fe141SSamu Onkalo pattern[i] = (u8)cmd; 359500fe141SSamu Onkalo 360500fe141SSamu Onkalo offset += nrchars; 361500fe141SSamu Onkalo i++; 362500fe141SSamu Onkalo } 363500fe141SSamu Onkalo 364500fe141SSamu Onkalo /* Each instruction is 16bit long. Check that length is even */ 365500fe141SSamu Onkalo if (i % 2) 366500fe141SSamu Onkalo goto fail; 367500fe141SSamu Onkalo 368500fe141SSamu Onkalo mutex_lock(&chip->lock); 369d4e7ad03SSamu Onkalo if (engine->mode == LP5521_CMD_LOAD) 370500fe141SSamu Onkalo ret = lp5521_load_program(engine, pattern); 371d4e7ad03SSamu Onkalo else 372d4e7ad03SSamu Onkalo ret = -EINVAL; 373500fe141SSamu Onkalo mutex_unlock(&chip->lock); 374500fe141SSamu Onkalo 375500fe141SSamu Onkalo if (ret) { 376500fe141SSamu Onkalo dev_err(&client->dev, "failed loading pattern\n"); 377500fe141SSamu Onkalo return ret; 378500fe141SSamu Onkalo } 379500fe141SSamu Onkalo 380500fe141SSamu Onkalo return len; 381500fe141SSamu Onkalo fail: 382500fe141SSamu Onkalo dev_err(&client->dev, "wrong pattern format\n"); 383500fe141SSamu Onkalo return -EINVAL; 384500fe141SSamu Onkalo } 385500fe141SSamu Onkalo 386500fe141SSamu Onkalo static ssize_t store_engine_load(struct device *dev, 387500fe141SSamu Onkalo struct device_attribute *attr, 388500fe141SSamu Onkalo const char *buf, size_t len, int nr) 389500fe141SSamu Onkalo { 390500fe141SSamu Onkalo struct i2c_client *client = to_i2c_client(dev); 391500fe141SSamu Onkalo struct lp5521_chip *chip = i2c_get_clientdata(client); 392500fe141SSamu Onkalo return lp5521_do_store_load(&chip->engines[nr - 1], buf, len); 393500fe141SSamu Onkalo } 394500fe141SSamu Onkalo 395500fe141SSamu Onkalo #define store_load(nr) \ 396500fe141SSamu Onkalo static ssize_t store_engine##nr##_load(struct device *dev, \ 397500fe141SSamu Onkalo struct device_attribute *attr, \ 398500fe141SSamu Onkalo const char *buf, size_t len) \ 399500fe141SSamu Onkalo { \ 400500fe141SSamu Onkalo return store_engine_load(dev, attr, buf, len, nr); \ 401500fe141SSamu Onkalo } 402500fe141SSamu Onkalo store_load(1) 403500fe141SSamu Onkalo store_load(2) 404500fe141SSamu Onkalo store_load(3) 405500fe141SSamu Onkalo 406500fe141SSamu Onkalo static ssize_t show_engine_mode(struct device *dev, 407500fe141SSamu Onkalo struct device_attribute *attr, 408500fe141SSamu Onkalo char *buf, int nr) 409500fe141SSamu Onkalo { 410500fe141SSamu Onkalo struct i2c_client *client = to_i2c_client(dev); 411500fe141SSamu Onkalo struct lp5521_chip *chip = i2c_get_clientdata(client); 412500fe141SSamu Onkalo switch (chip->engines[nr - 1].mode) { 413500fe141SSamu Onkalo case LP5521_CMD_RUN: 414500fe141SSamu Onkalo return sprintf(buf, "run\n"); 415500fe141SSamu Onkalo case LP5521_CMD_LOAD: 416500fe141SSamu Onkalo return sprintf(buf, "load\n"); 417500fe141SSamu Onkalo case LP5521_CMD_DISABLED: 418500fe141SSamu Onkalo return sprintf(buf, "disabled\n"); 419500fe141SSamu Onkalo default: 420500fe141SSamu Onkalo return sprintf(buf, "disabled\n"); 421500fe141SSamu Onkalo } 422500fe141SSamu Onkalo } 423500fe141SSamu Onkalo 424500fe141SSamu Onkalo #define show_mode(nr) \ 425500fe141SSamu Onkalo static ssize_t show_engine##nr##_mode(struct device *dev, \ 426500fe141SSamu Onkalo struct device_attribute *attr, \ 427500fe141SSamu Onkalo char *buf) \ 428500fe141SSamu Onkalo { \ 429500fe141SSamu Onkalo return show_engine_mode(dev, attr, buf, nr); \ 430500fe141SSamu Onkalo } 431500fe141SSamu Onkalo show_mode(1) 432500fe141SSamu Onkalo show_mode(2) 433500fe141SSamu Onkalo show_mode(3) 434500fe141SSamu Onkalo 435500fe141SSamu Onkalo static ssize_t store_engine_mode(struct device *dev, 436500fe141SSamu Onkalo struct device_attribute *attr, 437500fe141SSamu Onkalo const char *buf, size_t len, int nr) 438500fe141SSamu Onkalo { 439500fe141SSamu Onkalo struct i2c_client *client = to_i2c_client(dev); 440500fe141SSamu Onkalo struct lp5521_chip *chip = i2c_get_clientdata(client); 441500fe141SSamu Onkalo struct lp5521_engine *engine = &chip->engines[nr - 1]; 442500fe141SSamu Onkalo mutex_lock(&chip->lock); 443500fe141SSamu Onkalo 444500fe141SSamu Onkalo if (!strncmp(buf, "run", 3)) 445500fe141SSamu Onkalo lp5521_set_mode(engine, LP5521_CMD_RUN); 446500fe141SSamu Onkalo else if (!strncmp(buf, "load", 4)) 447500fe141SSamu Onkalo lp5521_set_mode(engine, LP5521_CMD_LOAD); 448500fe141SSamu Onkalo else if (!strncmp(buf, "disabled", 8)) 449500fe141SSamu Onkalo lp5521_set_mode(engine, LP5521_CMD_DISABLED); 450500fe141SSamu Onkalo 451500fe141SSamu Onkalo mutex_unlock(&chip->lock); 452500fe141SSamu Onkalo return len; 453500fe141SSamu Onkalo } 454500fe141SSamu Onkalo 455500fe141SSamu Onkalo #define store_mode(nr) \ 456500fe141SSamu Onkalo static ssize_t store_engine##nr##_mode(struct device *dev, \ 457500fe141SSamu Onkalo struct device_attribute *attr, \ 458500fe141SSamu Onkalo const char *buf, size_t len) \ 459500fe141SSamu Onkalo { \ 460500fe141SSamu Onkalo return store_engine_mode(dev, attr, buf, len, nr); \ 461500fe141SSamu Onkalo } 462500fe141SSamu Onkalo store_mode(1) 463500fe141SSamu Onkalo store_mode(2) 464500fe141SSamu Onkalo store_mode(3) 465500fe141SSamu Onkalo 466500fe141SSamu Onkalo static ssize_t show_max_current(struct device *dev, 467500fe141SSamu Onkalo struct device_attribute *attr, 468500fe141SSamu Onkalo char *buf) 469500fe141SSamu Onkalo { 470500fe141SSamu Onkalo struct led_classdev *led_cdev = dev_get_drvdata(dev); 471500fe141SSamu Onkalo struct lp5521_led *led = cdev_to_led(led_cdev); 472500fe141SSamu Onkalo 473500fe141SSamu Onkalo return sprintf(buf, "%d\n", led->max_current); 474500fe141SSamu Onkalo } 475500fe141SSamu Onkalo 476500fe141SSamu Onkalo static ssize_t show_current(struct device *dev, 477500fe141SSamu Onkalo struct device_attribute *attr, 478500fe141SSamu Onkalo char *buf) 479500fe141SSamu Onkalo { 480500fe141SSamu Onkalo struct led_classdev *led_cdev = dev_get_drvdata(dev); 481500fe141SSamu Onkalo struct lp5521_led *led = cdev_to_led(led_cdev); 482500fe141SSamu Onkalo 483500fe141SSamu Onkalo return sprintf(buf, "%d\n", led->led_current); 484500fe141SSamu Onkalo } 485500fe141SSamu Onkalo 486500fe141SSamu Onkalo static ssize_t store_current(struct device *dev, 487500fe141SSamu Onkalo struct device_attribute *attr, 488500fe141SSamu Onkalo const char *buf, size_t len) 489500fe141SSamu Onkalo { 490500fe141SSamu Onkalo struct led_classdev *led_cdev = dev_get_drvdata(dev); 491500fe141SSamu Onkalo struct lp5521_led *led = cdev_to_led(led_cdev); 492500fe141SSamu Onkalo struct lp5521_chip *chip = led_to_lp5521(led); 493500fe141SSamu Onkalo ssize_t ret; 494500fe141SSamu Onkalo unsigned long curr; 495500fe141SSamu Onkalo 496500fe141SSamu Onkalo if (strict_strtoul(buf, 0, &curr)) 497500fe141SSamu Onkalo return -EINVAL; 498500fe141SSamu Onkalo 499500fe141SSamu Onkalo if (curr > led->max_current) 500500fe141SSamu Onkalo return -EINVAL; 501500fe141SSamu Onkalo 502500fe141SSamu Onkalo mutex_lock(&chip->lock); 503500fe141SSamu Onkalo ret = lp5521_set_led_current(chip, led->id, curr); 504500fe141SSamu Onkalo mutex_unlock(&chip->lock); 505500fe141SSamu Onkalo 506500fe141SSamu Onkalo if (ret < 0) 507500fe141SSamu Onkalo return ret; 508500fe141SSamu Onkalo 509500fe141SSamu Onkalo led->led_current = (u8)curr; 510500fe141SSamu Onkalo 511500fe141SSamu Onkalo return len; 512500fe141SSamu Onkalo } 513500fe141SSamu Onkalo 514500fe141SSamu Onkalo static ssize_t lp5521_selftest(struct device *dev, 515500fe141SSamu Onkalo struct device_attribute *attr, 516500fe141SSamu Onkalo char *buf) 517500fe141SSamu Onkalo { 518500fe141SSamu Onkalo struct i2c_client *client = to_i2c_client(dev); 519500fe141SSamu Onkalo struct lp5521_chip *chip = i2c_get_clientdata(client); 520500fe141SSamu Onkalo int ret; 521500fe141SSamu Onkalo 522500fe141SSamu Onkalo mutex_lock(&chip->lock); 523500fe141SSamu Onkalo ret = lp5521_run_selftest(chip, buf); 524500fe141SSamu Onkalo mutex_unlock(&chip->lock); 525500fe141SSamu Onkalo return sprintf(buf, "%s\n", ret ? "FAIL" : "OK"); 526500fe141SSamu Onkalo } 527500fe141SSamu Onkalo 528500fe141SSamu Onkalo /* led class device attributes */ 52967d1da79SVasiliy Kulikov static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current); 530500fe141SSamu Onkalo static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL); 531500fe141SSamu Onkalo 532500fe141SSamu Onkalo static struct attribute *lp5521_led_attributes[] = { 533500fe141SSamu Onkalo &dev_attr_led_current.attr, 534500fe141SSamu Onkalo &dev_attr_max_current.attr, 535500fe141SSamu Onkalo NULL, 536500fe141SSamu Onkalo }; 537500fe141SSamu Onkalo 538500fe141SSamu Onkalo static struct attribute_group lp5521_led_attribute_group = { 539500fe141SSamu Onkalo .attrs = lp5521_led_attributes 540500fe141SSamu Onkalo }; 541500fe141SSamu Onkalo 542500fe141SSamu Onkalo /* device attributes */ 54367d1da79SVasiliy Kulikov static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR, 544500fe141SSamu Onkalo show_engine1_mode, store_engine1_mode); 54567d1da79SVasiliy Kulikov static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR, 546500fe141SSamu Onkalo show_engine2_mode, store_engine2_mode); 54767d1da79SVasiliy Kulikov static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR, 548500fe141SSamu Onkalo show_engine3_mode, store_engine3_mode); 54967d1da79SVasiliy Kulikov static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load); 55067d1da79SVasiliy Kulikov static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load); 55167d1da79SVasiliy Kulikov static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load); 552500fe141SSamu Onkalo static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL); 553500fe141SSamu Onkalo 554500fe141SSamu Onkalo static struct attribute *lp5521_attributes[] = { 555500fe141SSamu Onkalo &dev_attr_engine1_mode.attr, 556500fe141SSamu Onkalo &dev_attr_engine2_mode.attr, 557500fe141SSamu Onkalo &dev_attr_engine3_mode.attr, 558500fe141SSamu Onkalo &dev_attr_selftest.attr, 559500fe141SSamu Onkalo &dev_attr_engine1_load.attr, 560500fe141SSamu Onkalo &dev_attr_engine2_load.attr, 561500fe141SSamu Onkalo &dev_attr_engine3_load.attr, 562500fe141SSamu Onkalo NULL 563500fe141SSamu Onkalo }; 564500fe141SSamu Onkalo 565500fe141SSamu Onkalo static const struct attribute_group lp5521_group = { 566500fe141SSamu Onkalo .attrs = lp5521_attributes, 567500fe141SSamu Onkalo }; 568500fe141SSamu Onkalo 569500fe141SSamu Onkalo static int lp5521_register_sysfs(struct i2c_client *client) 570500fe141SSamu Onkalo { 571500fe141SSamu Onkalo struct device *dev = &client->dev; 572500fe141SSamu Onkalo return sysfs_create_group(&dev->kobj, &lp5521_group); 573500fe141SSamu Onkalo } 574500fe141SSamu Onkalo 575500fe141SSamu Onkalo static void lp5521_unregister_sysfs(struct i2c_client *client) 576500fe141SSamu Onkalo { 577500fe141SSamu Onkalo struct lp5521_chip *chip = i2c_get_clientdata(client); 578500fe141SSamu Onkalo struct device *dev = &client->dev; 579500fe141SSamu Onkalo int i; 580500fe141SSamu Onkalo 581500fe141SSamu Onkalo sysfs_remove_group(&dev->kobj, &lp5521_group); 582500fe141SSamu Onkalo 583500fe141SSamu Onkalo for (i = 0; i < chip->num_leds; i++) 584500fe141SSamu Onkalo sysfs_remove_group(&chip->leds[i].cdev.dev->kobj, 585500fe141SSamu Onkalo &lp5521_led_attribute_group); 586500fe141SSamu Onkalo } 587500fe141SSamu Onkalo 5885286bd95SRalf Baechle static int __devinit lp5521_init_led(struct lp5521_led *led, 589500fe141SSamu Onkalo struct i2c_client *client, 590500fe141SSamu Onkalo int chan, struct lp5521_platform_data *pdata) 591500fe141SSamu Onkalo { 592500fe141SSamu Onkalo struct device *dev = &client->dev; 593500fe141SSamu Onkalo char name[32]; 594500fe141SSamu Onkalo int res; 595500fe141SSamu Onkalo 596500fe141SSamu Onkalo if (chan >= LP5521_MAX_LEDS) 597500fe141SSamu Onkalo return -EINVAL; 598500fe141SSamu Onkalo 599500fe141SSamu Onkalo if (pdata->led_config[chan].led_current == 0) 600500fe141SSamu Onkalo return 0; 601500fe141SSamu Onkalo 602500fe141SSamu Onkalo led->led_current = pdata->led_config[chan].led_current; 603500fe141SSamu Onkalo led->max_current = pdata->led_config[chan].max_current; 604500fe141SSamu Onkalo led->chan_nr = pdata->led_config[chan].chan_nr; 605500fe141SSamu Onkalo 606500fe141SSamu Onkalo if (led->chan_nr >= LP5521_MAX_LEDS) { 607500fe141SSamu Onkalo dev_err(dev, "Use channel numbers between 0 and %d\n", 608500fe141SSamu Onkalo LP5521_MAX_LEDS - 1); 609500fe141SSamu Onkalo return -EINVAL; 610500fe141SSamu Onkalo } 611500fe141SSamu Onkalo 6125ae4e8a7SKim, Milo led->cdev.brightness_set = lp5521_set_brightness; 6135ae4e8a7SKim, Milo if (pdata->led_config[chan].name) { 6145ae4e8a7SKim, Milo led->cdev.name = pdata->led_config[chan].name; 6155ae4e8a7SKim, Milo } else { 61661a83932SArun Murthy snprintf(name, sizeof(name), "%s:channel%d", 61761a83932SArun Murthy pdata->label ?: client->name, chan); 618500fe141SSamu Onkalo led->cdev.name = name; 6195ae4e8a7SKim, Milo } 6205ae4e8a7SKim, Milo 621500fe141SSamu Onkalo res = led_classdev_register(dev, &led->cdev); 622500fe141SSamu Onkalo if (res < 0) { 623500fe141SSamu Onkalo dev_err(dev, "couldn't register led on channel %d\n", chan); 624500fe141SSamu Onkalo return res; 625500fe141SSamu Onkalo } 626500fe141SSamu Onkalo 627500fe141SSamu Onkalo res = sysfs_create_group(&led->cdev.dev->kobj, 628500fe141SSamu Onkalo &lp5521_led_attribute_group); 629500fe141SSamu Onkalo if (res < 0) { 630500fe141SSamu Onkalo dev_err(dev, "couldn't register current attribute\n"); 631500fe141SSamu Onkalo led_classdev_unregister(&led->cdev); 632500fe141SSamu Onkalo return res; 633500fe141SSamu Onkalo } 634500fe141SSamu Onkalo return 0; 635500fe141SSamu Onkalo } 636500fe141SSamu Onkalo 6375286bd95SRalf Baechle static int __devinit lp5521_probe(struct i2c_client *client, 638500fe141SSamu Onkalo const struct i2c_device_id *id) 639500fe141SSamu Onkalo { 640500fe141SSamu Onkalo struct lp5521_chip *chip; 641500fe141SSamu Onkalo struct lp5521_platform_data *pdata; 642500fe141SSamu Onkalo int ret, i, led; 643b3c49c05SSrinidhi KASAGAR u8 buf; 644500fe141SSamu Onkalo 645500fe141SSamu Onkalo chip = kzalloc(sizeof(*chip), GFP_KERNEL); 646500fe141SSamu Onkalo if (!chip) 647500fe141SSamu Onkalo return -ENOMEM; 648500fe141SSamu Onkalo 649500fe141SSamu Onkalo i2c_set_clientdata(client, chip); 650500fe141SSamu Onkalo chip->client = client; 651500fe141SSamu Onkalo 652500fe141SSamu Onkalo pdata = client->dev.platform_data; 653500fe141SSamu Onkalo 654500fe141SSamu Onkalo if (!pdata) { 655500fe141SSamu Onkalo dev_err(&client->dev, "no platform data\n"); 656500fe141SSamu Onkalo ret = -EINVAL; 657500fe141SSamu Onkalo goto fail1; 658500fe141SSamu Onkalo } 659500fe141SSamu Onkalo 660500fe141SSamu Onkalo mutex_init(&chip->lock); 661500fe141SSamu Onkalo 662500fe141SSamu Onkalo chip->pdata = pdata; 663500fe141SSamu Onkalo 664500fe141SSamu Onkalo if (pdata->setup_resources) { 665500fe141SSamu Onkalo ret = pdata->setup_resources(); 666500fe141SSamu Onkalo if (ret < 0) 667500fe141SSamu Onkalo goto fail1; 668500fe141SSamu Onkalo } 669500fe141SSamu Onkalo 670500fe141SSamu Onkalo if (pdata->enable) { 671500fe141SSamu Onkalo pdata->enable(0); 67209c76b0fSSamu Onkalo usleep_range(1000, 2000); /* Keep enable down at least 1ms */ 673500fe141SSamu Onkalo pdata->enable(1); 67409c76b0fSSamu Onkalo usleep_range(1000, 2000); /* 500us abs min. */ 675500fe141SSamu Onkalo } 676500fe141SSamu Onkalo 67795ea8eecSSamu Onkalo lp5521_write(client, LP5521_REG_RESET, 0xff); 67895ea8eecSSamu Onkalo usleep_range(10000, 20000); /* 67995ea8eecSSamu Onkalo * Exact value is not available. 10 - 20ms 68095ea8eecSSamu Onkalo * appears to be enough for reset. 68195ea8eecSSamu Onkalo */ 682b3c49c05SSrinidhi KASAGAR 683b3c49c05SSrinidhi KASAGAR /* 684b3c49c05SSrinidhi KASAGAR * Make sure that the chip is reset by reading back the r channel 685b3c49c05SSrinidhi KASAGAR * current reg. This is dummy read is required on some platforms - 686b3c49c05SSrinidhi KASAGAR * otherwise further access to the R G B channels in the 687b3c49c05SSrinidhi KASAGAR * LP5521_REG_ENABLE register will not have any effect - strange! 688b3c49c05SSrinidhi KASAGAR */ 689b3c49c05SSrinidhi KASAGAR lp5521_read(client, LP5521_REG_R_CURRENT, &buf); 690b3c49c05SSrinidhi KASAGAR if (buf != LP5521_REG_R_CURR_DEFAULT) { 6913a2fd4a1SMasanari Iida dev_err(&client->dev, "error in resetting chip\n"); 692b3c49c05SSrinidhi KASAGAR goto fail2; 693b3c49c05SSrinidhi KASAGAR } 694b3c49c05SSrinidhi KASAGAR usleep_range(10000, 20000); 695b3c49c05SSrinidhi KASAGAR 696500fe141SSamu Onkalo ret = lp5521_detect(client); 697500fe141SSamu Onkalo 698500fe141SSamu Onkalo if (ret) { 699500fe141SSamu Onkalo dev_err(&client->dev, "Chip not found\n"); 700500fe141SSamu Onkalo goto fail2; 701500fe141SSamu Onkalo } 702500fe141SSamu Onkalo 703500fe141SSamu Onkalo dev_info(&client->dev, "%s programmable led chip found\n", id->name); 704500fe141SSamu Onkalo 705d4e7ad03SSamu Onkalo ret = lp5521_configure(client); 706500fe141SSamu Onkalo if (ret < 0) { 707500fe141SSamu Onkalo dev_err(&client->dev, "error configuring chip\n"); 708500fe141SSamu Onkalo goto fail2; 709500fe141SSamu Onkalo } 710500fe141SSamu Onkalo 711500fe141SSamu Onkalo /* Initialize leds */ 712500fe141SSamu Onkalo chip->num_channels = pdata->num_channels; 713500fe141SSamu Onkalo chip->num_leds = 0; 714500fe141SSamu Onkalo led = 0; 715500fe141SSamu Onkalo for (i = 0; i < pdata->num_channels; i++) { 716500fe141SSamu Onkalo /* Do not initialize channels that are not connected */ 717500fe141SSamu Onkalo if (pdata->led_config[i].led_current == 0) 718500fe141SSamu Onkalo continue; 719500fe141SSamu Onkalo 720500fe141SSamu Onkalo ret = lp5521_init_led(&chip->leds[led], client, i, pdata); 721500fe141SSamu Onkalo if (ret) { 722500fe141SSamu Onkalo dev_err(&client->dev, "error initializing leds\n"); 723500fe141SSamu Onkalo goto fail3; 724500fe141SSamu Onkalo } 725500fe141SSamu Onkalo chip->num_leds++; 726500fe141SSamu Onkalo 727500fe141SSamu Onkalo chip->leds[led].id = led; 728500fe141SSamu Onkalo /* Set initial LED current */ 729500fe141SSamu Onkalo lp5521_set_led_current(chip, led, 730500fe141SSamu Onkalo chip->leds[led].led_current); 731500fe141SSamu Onkalo 732500fe141SSamu Onkalo INIT_WORK(&(chip->leds[led].brightness_work), 733500fe141SSamu Onkalo lp5521_led_brightness_work); 734500fe141SSamu Onkalo 735500fe141SSamu Onkalo led++; 736500fe141SSamu Onkalo } 737500fe141SSamu Onkalo 738500fe141SSamu Onkalo ret = lp5521_register_sysfs(client); 739500fe141SSamu Onkalo if (ret) { 740500fe141SSamu Onkalo dev_err(&client->dev, "registering sysfs failed\n"); 741500fe141SSamu Onkalo goto fail3; 742500fe141SSamu Onkalo } 743500fe141SSamu Onkalo return ret; 744500fe141SSamu Onkalo fail3: 745500fe141SSamu Onkalo for (i = 0; i < chip->num_leds; i++) { 746500fe141SSamu Onkalo led_classdev_unregister(&chip->leds[i].cdev); 747500fe141SSamu Onkalo cancel_work_sync(&chip->leds[i].brightness_work); 748500fe141SSamu Onkalo } 749500fe141SSamu Onkalo fail2: 750500fe141SSamu Onkalo if (pdata->enable) 751500fe141SSamu Onkalo pdata->enable(0); 752500fe141SSamu Onkalo if (pdata->release_resources) 753500fe141SSamu Onkalo pdata->release_resources(); 754500fe141SSamu Onkalo fail1: 755500fe141SSamu Onkalo kfree(chip); 756500fe141SSamu Onkalo return ret; 757500fe141SSamu Onkalo } 758500fe141SSamu Onkalo 75919ab5cb8SLinus Walleij static int __devexit lp5521_remove(struct i2c_client *client) 760500fe141SSamu Onkalo { 761500fe141SSamu Onkalo struct lp5521_chip *chip = i2c_get_clientdata(client); 762500fe141SSamu Onkalo int i; 763500fe141SSamu Onkalo 764500fe141SSamu Onkalo lp5521_unregister_sysfs(client); 765500fe141SSamu Onkalo 766500fe141SSamu Onkalo for (i = 0; i < chip->num_leds; i++) { 767500fe141SSamu Onkalo led_classdev_unregister(&chip->leds[i].cdev); 768500fe141SSamu Onkalo cancel_work_sync(&chip->leds[i].brightness_work); 769500fe141SSamu Onkalo } 770500fe141SSamu Onkalo 771500fe141SSamu Onkalo if (chip->pdata->enable) 772500fe141SSamu Onkalo chip->pdata->enable(0); 773500fe141SSamu Onkalo if (chip->pdata->release_resources) 774500fe141SSamu Onkalo chip->pdata->release_resources(); 775500fe141SSamu Onkalo kfree(chip); 776500fe141SSamu Onkalo return 0; 777500fe141SSamu Onkalo } 778500fe141SSamu Onkalo 779500fe141SSamu Onkalo static const struct i2c_device_id lp5521_id[] = { 780500fe141SSamu Onkalo { "lp5521", 0 }, /* Three channel chip */ 781500fe141SSamu Onkalo { } 782500fe141SSamu Onkalo }; 783500fe141SSamu Onkalo MODULE_DEVICE_TABLE(i2c, lp5521_id); 784500fe141SSamu Onkalo 785500fe141SSamu Onkalo static struct i2c_driver lp5521_driver = { 786500fe141SSamu Onkalo .driver = { 787500fe141SSamu Onkalo .name = "lp5521", 788500fe141SSamu Onkalo }, 789500fe141SSamu Onkalo .probe = lp5521_probe, 79019ab5cb8SLinus Walleij .remove = __devexit_p(lp5521_remove), 791500fe141SSamu Onkalo .id_table = lp5521_id, 792500fe141SSamu Onkalo }; 793500fe141SSamu Onkalo 79409a0d183SAxel Lin module_i2c_driver(lp5521_driver); 795500fe141SSamu Onkalo 796500fe141SSamu Onkalo MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo"); 797500fe141SSamu Onkalo MODULE_DESCRIPTION("LP5521 LED engine"); 798500fe141SSamu Onkalo MODULE_LICENSE("GPL v2"); 799