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 /* Bits in CONFIG register */ 86500fe141SSamu Onkalo #define LP5521_PWM_HF 0x40 /* PWM: 0 = 256Hz, 1 = 558Hz */ 87500fe141SSamu Onkalo #define LP5521_PWRSAVE_EN 0x20 /* 1 = Power save mode */ 88500fe141SSamu Onkalo #define LP5521_CP_MODE_OFF 0 /* Charge pump (CP) off */ 89500fe141SSamu Onkalo #define LP5521_CP_MODE_BYPASS 8 /* CP forced to bypass mode */ 90500fe141SSamu Onkalo #define LP5521_CP_MODE_1X5 0x10 /* CP forced to 1.5x mode */ 91500fe141SSamu Onkalo #define LP5521_CP_MODE_AUTO 0x18 /* Automatic mode selection */ 92500fe141SSamu Onkalo #define LP5521_R_TO_BATT 4 /* R out: 0 = CP, 1 = Vbat */ 93500fe141SSamu Onkalo #define LP5521_CLK_SRC_EXT 0 /* Ext-clk source (CLK_32K) */ 94500fe141SSamu Onkalo #define LP5521_CLK_INT 1 /* Internal clock */ 95500fe141SSamu Onkalo #define LP5521_CLK_AUTO 2 /* Automatic clock selection */ 96500fe141SSamu Onkalo 97500fe141SSamu Onkalo /* Status */ 98500fe141SSamu Onkalo #define LP5521_EXT_CLK_USED 0x08 99500fe141SSamu Onkalo 100500fe141SSamu Onkalo struct lp5521_engine { 101500fe141SSamu Onkalo const struct attribute_group *attributes; 102500fe141SSamu Onkalo int id; 103500fe141SSamu Onkalo u8 mode; 104500fe141SSamu Onkalo u8 prog_page; 105500fe141SSamu Onkalo u8 engine_mask; 106500fe141SSamu Onkalo }; 107500fe141SSamu Onkalo 108500fe141SSamu Onkalo struct lp5521_led { 109500fe141SSamu Onkalo int id; 110500fe141SSamu Onkalo u8 chan_nr; 111500fe141SSamu Onkalo u8 led_current; 112500fe141SSamu Onkalo u8 max_current; 113500fe141SSamu Onkalo struct led_classdev cdev; 114500fe141SSamu Onkalo struct work_struct brightness_work; 115500fe141SSamu Onkalo u8 brightness; 116500fe141SSamu Onkalo }; 117500fe141SSamu Onkalo 118500fe141SSamu Onkalo struct lp5521_chip { 119500fe141SSamu Onkalo struct lp5521_platform_data *pdata; 120500fe141SSamu Onkalo struct mutex lock; /* Serialize control */ 121500fe141SSamu Onkalo struct i2c_client *client; 122500fe141SSamu Onkalo struct lp5521_engine engines[LP5521_MAX_ENGINES]; 123500fe141SSamu Onkalo struct lp5521_led leds[LP5521_MAX_LEDS]; 124500fe141SSamu Onkalo u8 num_channels; 125500fe141SSamu Onkalo u8 num_leds; 126500fe141SSamu Onkalo }; 127500fe141SSamu Onkalo 1289fdb18b6SSamu Onkalo static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev) 1299fdb18b6SSamu Onkalo { 1309fdb18b6SSamu Onkalo return container_of(cdev, struct lp5521_led, cdev); 1319fdb18b6SSamu Onkalo } 1329fdb18b6SSamu Onkalo 1339fdb18b6SSamu Onkalo static inline struct lp5521_chip *engine_to_lp5521(struct lp5521_engine *engine) 1349fdb18b6SSamu Onkalo { 1359fdb18b6SSamu Onkalo return container_of(engine, struct lp5521_chip, 1369fdb18b6SSamu Onkalo engines[engine->id - 1]); 1379fdb18b6SSamu Onkalo } 1389fdb18b6SSamu Onkalo 1399fdb18b6SSamu Onkalo static inline struct lp5521_chip *led_to_lp5521(struct lp5521_led *led) 1409fdb18b6SSamu Onkalo { 1419fdb18b6SSamu Onkalo return container_of(led, struct lp5521_chip, 1429fdb18b6SSamu Onkalo leds[led->id]); 1439fdb18b6SSamu Onkalo } 144500fe141SSamu Onkalo 145500fe141SSamu Onkalo static void lp5521_led_brightness_work(struct work_struct *work); 146500fe141SSamu Onkalo 147500fe141SSamu Onkalo static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value) 148500fe141SSamu Onkalo { 149500fe141SSamu Onkalo return i2c_smbus_write_byte_data(client, reg, value); 150500fe141SSamu Onkalo } 151500fe141SSamu Onkalo 152500fe141SSamu Onkalo static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf) 153500fe141SSamu Onkalo { 154500fe141SSamu Onkalo s32 ret; 155500fe141SSamu Onkalo 156500fe141SSamu Onkalo ret = i2c_smbus_read_byte_data(client, reg); 157500fe141SSamu Onkalo if (ret < 0) 158500fe141SSamu Onkalo return -EIO; 159500fe141SSamu Onkalo 160500fe141SSamu Onkalo *buf = ret; 161500fe141SSamu Onkalo return 0; 162500fe141SSamu Onkalo } 163500fe141SSamu Onkalo 164500fe141SSamu Onkalo static int lp5521_set_engine_mode(struct lp5521_engine *engine, u8 mode) 165500fe141SSamu Onkalo { 166500fe141SSamu Onkalo struct lp5521_chip *chip = engine_to_lp5521(engine); 167500fe141SSamu Onkalo struct i2c_client *client = chip->client; 168500fe141SSamu Onkalo int ret; 169500fe141SSamu Onkalo u8 engine_state; 170500fe141SSamu Onkalo 171500fe141SSamu Onkalo /* Only transition between RUN and DIRECT mode are handled here */ 172500fe141SSamu Onkalo if (mode == LP5521_CMD_LOAD) 173500fe141SSamu Onkalo return 0; 174500fe141SSamu Onkalo 175500fe141SSamu Onkalo if (mode == LP5521_CMD_DISABLED) 176500fe141SSamu Onkalo mode = LP5521_CMD_DIRECT; 177500fe141SSamu Onkalo 178500fe141SSamu Onkalo ret = lp5521_read(client, LP5521_REG_OP_MODE, &engine_state); 179500fe141SSamu Onkalo 180500fe141SSamu Onkalo /* set mode only for this engine */ 181500fe141SSamu Onkalo engine_state &= ~(engine->engine_mask); 182500fe141SSamu Onkalo mode &= engine->engine_mask; 183500fe141SSamu Onkalo engine_state |= mode; 184500fe141SSamu Onkalo ret |= lp5521_write(client, LP5521_REG_OP_MODE, engine_state); 185500fe141SSamu Onkalo 186500fe141SSamu Onkalo return ret; 187500fe141SSamu Onkalo } 188500fe141SSamu Onkalo 189500fe141SSamu Onkalo static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern) 190500fe141SSamu Onkalo { 191500fe141SSamu Onkalo struct lp5521_chip *chip = engine_to_lp5521(eng); 192500fe141SSamu Onkalo struct i2c_client *client = chip->client; 193500fe141SSamu Onkalo int ret; 194500fe141SSamu Onkalo int addr; 195500fe141SSamu Onkalo u8 mode; 196500fe141SSamu Onkalo 197500fe141SSamu Onkalo /* move current engine to direct mode and remember the state */ 198500fe141SSamu Onkalo ret = lp5521_set_engine_mode(eng, LP5521_CMD_DIRECT); 19909c76b0fSSamu Onkalo /* Mode change requires min 500 us delay. 1 - 2 ms with margin */ 20009c76b0fSSamu Onkalo usleep_range(1000, 2000); 201500fe141SSamu Onkalo ret |= lp5521_read(client, LP5521_REG_OP_MODE, &mode); 202500fe141SSamu Onkalo 203500fe141SSamu Onkalo /* For loading, all the engines to load mode */ 204500fe141SSamu Onkalo lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT); 20509c76b0fSSamu Onkalo /* Mode change requires min 500 us delay. 1 - 2 ms with margin */ 20609c76b0fSSamu Onkalo usleep_range(1000, 2000); 207500fe141SSamu Onkalo lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_LOAD); 20809c76b0fSSamu Onkalo /* Mode change requires min 500 us delay. 1 - 2 ms with margin */ 20909c76b0fSSamu Onkalo usleep_range(1000, 2000); 210500fe141SSamu Onkalo 211500fe141SSamu Onkalo addr = LP5521_PROG_MEM_BASE + eng->prog_page * LP5521_PROG_MEM_SIZE; 212500fe141SSamu Onkalo i2c_smbus_write_i2c_block_data(client, 213500fe141SSamu Onkalo addr, 214500fe141SSamu Onkalo LP5521_PROG_MEM_SIZE, 215500fe141SSamu Onkalo pattern); 216500fe141SSamu Onkalo 217500fe141SSamu Onkalo ret |= lp5521_write(client, LP5521_REG_OP_MODE, mode); 218500fe141SSamu Onkalo return ret; 219500fe141SSamu Onkalo } 220500fe141SSamu Onkalo 221500fe141SSamu Onkalo static int lp5521_set_led_current(struct lp5521_chip *chip, int led, u8 curr) 222500fe141SSamu Onkalo { 223500fe141SSamu Onkalo return lp5521_write(chip->client, 224500fe141SSamu Onkalo LP5521_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr, 225500fe141SSamu Onkalo curr); 226500fe141SSamu Onkalo } 227500fe141SSamu Onkalo 228500fe141SSamu Onkalo static void lp5521_init_engine(struct lp5521_chip *chip, 229500fe141SSamu Onkalo const struct attribute_group *attr_group) 230500fe141SSamu Onkalo { 231500fe141SSamu Onkalo int i; 232500fe141SSamu Onkalo for (i = 0; i < ARRAY_SIZE(chip->engines); i++) { 233500fe141SSamu Onkalo chip->engines[i].id = i + 1; 234500fe141SSamu Onkalo chip->engines[i].engine_mask = LP5521_ENG_MASK_BASE >> (i * 2); 235500fe141SSamu Onkalo chip->engines[i].prog_page = i; 236500fe141SSamu Onkalo chip->engines[i].attributes = &attr_group[i]; 237500fe141SSamu Onkalo } 238500fe141SSamu Onkalo } 239500fe141SSamu Onkalo 240500fe141SSamu Onkalo static int lp5521_configure(struct i2c_client *client, 241500fe141SSamu Onkalo const struct attribute_group *attr_group) 242500fe141SSamu Onkalo { 243500fe141SSamu Onkalo struct lp5521_chip *chip = i2c_get_clientdata(client); 244500fe141SSamu Onkalo int ret; 245500fe141SSamu Onkalo 246500fe141SSamu Onkalo lp5521_init_engine(chip, attr_group); 247500fe141SSamu Onkalo 248500fe141SSamu Onkalo /* Set all PWMs to direct control mode */ 249500fe141SSamu Onkalo ret = lp5521_write(client, LP5521_REG_OP_MODE, 0x3F); 250500fe141SSamu Onkalo 251500fe141SSamu Onkalo /* Enable auto-powersave, set charge pump to auto, red to battery */ 252500fe141SSamu Onkalo ret |= lp5521_write(client, LP5521_REG_CONFIG, 253500fe141SSamu Onkalo LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT); 254500fe141SSamu Onkalo 255500fe141SSamu Onkalo /* Initialize all channels PWM to zero -> leds off */ 256500fe141SSamu Onkalo ret |= lp5521_write(client, LP5521_REG_R_PWM, 0); 257500fe141SSamu Onkalo ret |= lp5521_write(client, LP5521_REG_G_PWM, 0); 258500fe141SSamu Onkalo ret |= lp5521_write(client, LP5521_REG_B_PWM, 0); 259500fe141SSamu Onkalo 260500fe141SSamu Onkalo /* Set engines are set to run state when OP_MODE enables engines */ 261500fe141SSamu Onkalo ret |= lp5521_write(client, LP5521_REG_ENABLE, 262500fe141SSamu Onkalo LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM | 263500fe141SSamu Onkalo LP5521_EXEC_RUN); 26409c76b0fSSamu Onkalo /* enable takes 500us. 1 - 2 ms leaves some margin */ 26509c76b0fSSamu Onkalo usleep_range(1000, 2000); 266500fe141SSamu Onkalo 267500fe141SSamu Onkalo return ret; 268500fe141SSamu Onkalo } 269500fe141SSamu Onkalo 270500fe141SSamu Onkalo static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf) 271500fe141SSamu Onkalo { 272500fe141SSamu Onkalo int ret; 273500fe141SSamu Onkalo u8 status; 274500fe141SSamu Onkalo 275500fe141SSamu Onkalo ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status); 276500fe141SSamu Onkalo if (ret < 0) 277500fe141SSamu Onkalo return ret; 278500fe141SSamu Onkalo 279500fe141SSamu Onkalo /* Check that ext clock is really in use if requested */ 280500fe141SSamu Onkalo if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT) 281500fe141SSamu Onkalo if ((status & LP5521_EXT_CLK_USED) == 0) 282500fe141SSamu Onkalo return -EIO; 283500fe141SSamu Onkalo return 0; 284500fe141SSamu Onkalo } 285500fe141SSamu Onkalo 286500fe141SSamu Onkalo static void lp5521_set_brightness(struct led_classdev *cdev, 287500fe141SSamu Onkalo enum led_brightness brightness) 288500fe141SSamu Onkalo { 289500fe141SSamu Onkalo struct lp5521_led *led = cdev_to_led(cdev); 290500fe141SSamu Onkalo led->brightness = (u8)brightness; 291500fe141SSamu Onkalo schedule_work(&led->brightness_work); 292500fe141SSamu Onkalo } 293500fe141SSamu Onkalo 294500fe141SSamu Onkalo static void lp5521_led_brightness_work(struct work_struct *work) 295500fe141SSamu Onkalo { 296500fe141SSamu Onkalo struct lp5521_led *led = container_of(work, 297500fe141SSamu Onkalo struct lp5521_led, 298500fe141SSamu Onkalo brightness_work); 299500fe141SSamu Onkalo struct lp5521_chip *chip = led_to_lp5521(led); 300500fe141SSamu Onkalo struct i2c_client *client = chip->client; 301500fe141SSamu Onkalo 302500fe141SSamu Onkalo mutex_lock(&chip->lock); 303500fe141SSamu Onkalo lp5521_write(client, LP5521_REG_LED_PWM_BASE + led->chan_nr, 304500fe141SSamu Onkalo led->brightness); 305500fe141SSamu Onkalo mutex_unlock(&chip->lock); 306500fe141SSamu Onkalo } 307500fe141SSamu Onkalo 308500fe141SSamu Onkalo /* Detect the chip by setting its ENABLE register and reading it back. */ 309500fe141SSamu Onkalo static int lp5521_detect(struct i2c_client *client) 310500fe141SSamu Onkalo { 311500fe141SSamu Onkalo int ret; 312500fe141SSamu Onkalo u8 buf; 313500fe141SSamu Onkalo 314500fe141SSamu Onkalo ret = lp5521_write(client, LP5521_REG_ENABLE, 315500fe141SSamu Onkalo LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM); 316500fe141SSamu Onkalo if (ret) 317500fe141SSamu Onkalo return ret; 31809c76b0fSSamu Onkalo /* enable takes 500us. 1 - 2 ms leaves some margin */ 31909c76b0fSSamu Onkalo usleep_range(1000, 2000); 320500fe141SSamu Onkalo ret = lp5521_read(client, LP5521_REG_ENABLE, &buf); 321500fe141SSamu Onkalo if (ret) 322500fe141SSamu Onkalo return ret; 323500fe141SSamu Onkalo if (buf != (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)) 324500fe141SSamu Onkalo return -ENODEV; 325500fe141SSamu Onkalo 326500fe141SSamu Onkalo return 0; 327500fe141SSamu Onkalo } 328500fe141SSamu Onkalo 329500fe141SSamu Onkalo /* Set engine mode and create appropriate sysfs attributes, if required. */ 330500fe141SSamu Onkalo static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode) 331500fe141SSamu Onkalo { 332500fe141SSamu Onkalo struct lp5521_chip *chip = engine_to_lp5521(engine); 333500fe141SSamu Onkalo struct i2c_client *client = chip->client; 334500fe141SSamu Onkalo struct device *dev = &client->dev; 335500fe141SSamu Onkalo int ret = 0; 336500fe141SSamu Onkalo 337500fe141SSamu Onkalo /* if in that mode already do nothing, except for run */ 338500fe141SSamu Onkalo if (mode == engine->mode && mode != LP5521_CMD_RUN) 339500fe141SSamu Onkalo return 0; 340500fe141SSamu Onkalo 341500fe141SSamu Onkalo if (mode == LP5521_CMD_RUN) { 342500fe141SSamu Onkalo ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN); 343500fe141SSamu Onkalo } else if (mode == LP5521_CMD_LOAD) { 344500fe141SSamu Onkalo lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED); 345500fe141SSamu Onkalo lp5521_set_engine_mode(engine, LP5521_CMD_LOAD); 346500fe141SSamu Onkalo 347500fe141SSamu Onkalo ret = sysfs_create_group(&dev->kobj, engine->attributes); 348500fe141SSamu Onkalo if (ret) 349500fe141SSamu Onkalo return ret; 350500fe141SSamu Onkalo } else if (mode == LP5521_CMD_DISABLED) { 351500fe141SSamu Onkalo lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED); 352500fe141SSamu Onkalo } 353500fe141SSamu Onkalo 354500fe141SSamu Onkalo /* remove load attribute from sysfs if not in load mode */ 355500fe141SSamu Onkalo if (engine->mode == LP5521_CMD_LOAD && mode != LP5521_CMD_LOAD) 356500fe141SSamu Onkalo sysfs_remove_group(&dev->kobj, engine->attributes); 357500fe141SSamu Onkalo 358500fe141SSamu Onkalo engine->mode = mode; 359500fe141SSamu Onkalo 360500fe141SSamu Onkalo return ret; 361500fe141SSamu Onkalo } 362500fe141SSamu Onkalo 363500fe141SSamu Onkalo static int lp5521_do_store_load(struct lp5521_engine *engine, 364500fe141SSamu Onkalo const char *buf, size_t len) 365500fe141SSamu Onkalo { 366500fe141SSamu Onkalo struct lp5521_chip *chip = engine_to_lp5521(engine); 367500fe141SSamu Onkalo struct i2c_client *client = chip->client; 368500fe141SSamu Onkalo int ret, nrchars, offset = 0, i = 0; 369500fe141SSamu Onkalo char c[3]; 370500fe141SSamu Onkalo unsigned cmd; 371500fe141SSamu Onkalo u8 pattern[LP5521_PROGRAM_LENGTH] = {0}; 372500fe141SSamu Onkalo 373500fe141SSamu Onkalo while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) { 374500fe141SSamu Onkalo /* separate sscanfs because length is working only for %s */ 375500fe141SSamu Onkalo ret = sscanf(buf + offset, "%2s%n ", c, &nrchars); 376*2260209cSVasiliy Kulikov if (ret != 2) 377*2260209cSVasiliy Kulikov goto fail; 378500fe141SSamu Onkalo ret = sscanf(c, "%2x", &cmd); 379500fe141SSamu Onkalo if (ret != 1) 380500fe141SSamu Onkalo goto fail; 381500fe141SSamu Onkalo pattern[i] = (u8)cmd; 382500fe141SSamu Onkalo 383500fe141SSamu Onkalo offset += nrchars; 384500fe141SSamu Onkalo i++; 385500fe141SSamu Onkalo } 386500fe141SSamu Onkalo 387500fe141SSamu Onkalo /* Each instruction is 16bit long. Check that length is even */ 388500fe141SSamu Onkalo if (i % 2) 389500fe141SSamu Onkalo goto fail; 390500fe141SSamu Onkalo 391500fe141SSamu Onkalo mutex_lock(&chip->lock); 392500fe141SSamu Onkalo ret = lp5521_load_program(engine, pattern); 393500fe141SSamu Onkalo mutex_unlock(&chip->lock); 394500fe141SSamu Onkalo 395500fe141SSamu Onkalo if (ret) { 396500fe141SSamu Onkalo dev_err(&client->dev, "failed loading pattern\n"); 397500fe141SSamu Onkalo return ret; 398500fe141SSamu Onkalo } 399500fe141SSamu Onkalo 400500fe141SSamu Onkalo return len; 401500fe141SSamu Onkalo fail: 402500fe141SSamu Onkalo dev_err(&client->dev, "wrong pattern format\n"); 403500fe141SSamu Onkalo return -EINVAL; 404500fe141SSamu Onkalo } 405500fe141SSamu Onkalo 406500fe141SSamu Onkalo static ssize_t store_engine_load(struct device *dev, 407500fe141SSamu Onkalo struct device_attribute *attr, 408500fe141SSamu Onkalo const char *buf, size_t len, 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 return lp5521_do_store_load(&chip->engines[nr - 1], buf, len); 413500fe141SSamu Onkalo } 414500fe141SSamu Onkalo 415500fe141SSamu Onkalo #define store_load(nr) \ 416500fe141SSamu Onkalo static ssize_t store_engine##nr##_load(struct device *dev, \ 417500fe141SSamu Onkalo struct device_attribute *attr, \ 418500fe141SSamu Onkalo const char *buf, size_t len) \ 419500fe141SSamu Onkalo { \ 420500fe141SSamu Onkalo return store_engine_load(dev, attr, buf, len, nr); \ 421500fe141SSamu Onkalo } 422500fe141SSamu Onkalo store_load(1) 423500fe141SSamu Onkalo store_load(2) 424500fe141SSamu Onkalo store_load(3) 425500fe141SSamu Onkalo 426500fe141SSamu Onkalo static ssize_t show_engine_mode(struct device *dev, 427500fe141SSamu Onkalo struct device_attribute *attr, 428500fe141SSamu Onkalo char *buf, int nr) 429500fe141SSamu Onkalo { 430500fe141SSamu Onkalo struct i2c_client *client = to_i2c_client(dev); 431500fe141SSamu Onkalo struct lp5521_chip *chip = i2c_get_clientdata(client); 432500fe141SSamu Onkalo switch (chip->engines[nr - 1].mode) { 433500fe141SSamu Onkalo case LP5521_CMD_RUN: 434500fe141SSamu Onkalo return sprintf(buf, "run\n"); 435500fe141SSamu Onkalo case LP5521_CMD_LOAD: 436500fe141SSamu Onkalo return sprintf(buf, "load\n"); 437500fe141SSamu Onkalo case LP5521_CMD_DISABLED: 438500fe141SSamu Onkalo return sprintf(buf, "disabled\n"); 439500fe141SSamu Onkalo default: 440500fe141SSamu Onkalo return sprintf(buf, "disabled\n"); 441500fe141SSamu Onkalo } 442500fe141SSamu Onkalo } 443500fe141SSamu Onkalo 444500fe141SSamu Onkalo #define show_mode(nr) \ 445500fe141SSamu Onkalo static ssize_t show_engine##nr##_mode(struct device *dev, \ 446500fe141SSamu Onkalo struct device_attribute *attr, \ 447500fe141SSamu Onkalo char *buf) \ 448500fe141SSamu Onkalo { \ 449500fe141SSamu Onkalo return show_engine_mode(dev, attr, buf, nr); \ 450500fe141SSamu Onkalo } 451500fe141SSamu Onkalo show_mode(1) 452500fe141SSamu Onkalo show_mode(2) 453500fe141SSamu Onkalo show_mode(3) 454500fe141SSamu Onkalo 455500fe141SSamu Onkalo static ssize_t store_engine_mode(struct device *dev, 456500fe141SSamu Onkalo struct device_attribute *attr, 457500fe141SSamu Onkalo const char *buf, size_t len, int nr) 458500fe141SSamu Onkalo { 459500fe141SSamu Onkalo struct i2c_client *client = to_i2c_client(dev); 460500fe141SSamu Onkalo struct lp5521_chip *chip = i2c_get_clientdata(client); 461500fe141SSamu Onkalo struct lp5521_engine *engine = &chip->engines[nr - 1]; 462500fe141SSamu Onkalo mutex_lock(&chip->lock); 463500fe141SSamu Onkalo 464500fe141SSamu Onkalo if (!strncmp(buf, "run", 3)) 465500fe141SSamu Onkalo lp5521_set_mode(engine, LP5521_CMD_RUN); 466500fe141SSamu Onkalo else if (!strncmp(buf, "load", 4)) 467500fe141SSamu Onkalo lp5521_set_mode(engine, LP5521_CMD_LOAD); 468500fe141SSamu Onkalo else if (!strncmp(buf, "disabled", 8)) 469500fe141SSamu Onkalo lp5521_set_mode(engine, LP5521_CMD_DISABLED); 470500fe141SSamu Onkalo 471500fe141SSamu Onkalo mutex_unlock(&chip->lock); 472500fe141SSamu Onkalo return len; 473500fe141SSamu Onkalo } 474500fe141SSamu Onkalo 475500fe141SSamu Onkalo #define store_mode(nr) \ 476500fe141SSamu Onkalo static ssize_t store_engine##nr##_mode(struct device *dev, \ 477500fe141SSamu Onkalo struct device_attribute *attr, \ 478500fe141SSamu Onkalo const char *buf, size_t len) \ 479500fe141SSamu Onkalo { \ 480500fe141SSamu Onkalo return store_engine_mode(dev, attr, buf, len, nr); \ 481500fe141SSamu Onkalo } 482500fe141SSamu Onkalo store_mode(1) 483500fe141SSamu Onkalo store_mode(2) 484500fe141SSamu Onkalo store_mode(3) 485500fe141SSamu Onkalo 486500fe141SSamu Onkalo static ssize_t show_max_current(struct device *dev, 487500fe141SSamu Onkalo struct device_attribute *attr, 488500fe141SSamu Onkalo char *buf) 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 493500fe141SSamu Onkalo return sprintf(buf, "%d\n", led->max_current); 494500fe141SSamu Onkalo } 495500fe141SSamu Onkalo 496500fe141SSamu Onkalo static ssize_t show_current(struct device *dev, 497500fe141SSamu Onkalo struct device_attribute *attr, 498500fe141SSamu Onkalo char *buf) 499500fe141SSamu Onkalo { 500500fe141SSamu Onkalo struct led_classdev *led_cdev = dev_get_drvdata(dev); 501500fe141SSamu Onkalo struct lp5521_led *led = cdev_to_led(led_cdev); 502500fe141SSamu Onkalo 503500fe141SSamu Onkalo return sprintf(buf, "%d\n", led->led_current); 504500fe141SSamu Onkalo } 505500fe141SSamu Onkalo 506500fe141SSamu Onkalo static ssize_t store_current(struct device *dev, 507500fe141SSamu Onkalo struct device_attribute *attr, 508500fe141SSamu Onkalo const char *buf, size_t len) 509500fe141SSamu Onkalo { 510500fe141SSamu Onkalo struct led_classdev *led_cdev = dev_get_drvdata(dev); 511500fe141SSamu Onkalo struct lp5521_led *led = cdev_to_led(led_cdev); 512500fe141SSamu Onkalo struct lp5521_chip *chip = led_to_lp5521(led); 513500fe141SSamu Onkalo ssize_t ret; 514500fe141SSamu Onkalo unsigned long curr; 515500fe141SSamu Onkalo 516500fe141SSamu Onkalo if (strict_strtoul(buf, 0, &curr)) 517500fe141SSamu Onkalo return -EINVAL; 518500fe141SSamu Onkalo 519500fe141SSamu Onkalo if (curr > led->max_current) 520500fe141SSamu Onkalo return -EINVAL; 521500fe141SSamu Onkalo 522500fe141SSamu Onkalo mutex_lock(&chip->lock); 523500fe141SSamu Onkalo ret = lp5521_set_led_current(chip, led->id, curr); 524500fe141SSamu Onkalo mutex_unlock(&chip->lock); 525500fe141SSamu Onkalo 526500fe141SSamu Onkalo if (ret < 0) 527500fe141SSamu Onkalo return ret; 528500fe141SSamu Onkalo 529500fe141SSamu Onkalo led->led_current = (u8)curr; 530500fe141SSamu Onkalo 531500fe141SSamu Onkalo return len; 532500fe141SSamu Onkalo } 533500fe141SSamu Onkalo 534500fe141SSamu Onkalo static ssize_t lp5521_selftest(struct device *dev, 535500fe141SSamu Onkalo struct device_attribute *attr, 536500fe141SSamu Onkalo char *buf) 537500fe141SSamu Onkalo { 538500fe141SSamu Onkalo struct i2c_client *client = to_i2c_client(dev); 539500fe141SSamu Onkalo struct lp5521_chip *chip = i2c_get_clientdata(client); 540500fe141SSamu Onkalo int ret; 541500fe141SSamu Onkalo 542500fe141SSamu Onkalo mutex_lock(&chip->lock); 543500fe141SSamu Onkalo ret = lp5521_run_selftest(chip, buf); 544500fe141SSamu Onkalo mutex_unlock(&chip->lock); 545500fe141SSamu Onkalo return sprintf(buf, "%s\n", ret ? "FAIL" : "OK"); 546500fe141SSamu Onkalo } 547500fe141SSamu Onkalo 548500fe141SSamu Onkalo /* led class device attributes */ 549500fe141SSamu Onkalo static DEVICE_ATTR(led_current, S_IRUGO | S_IWUGO, show_current, store_current); 550500fe141SSamu Onkalo static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL); 551500fe141SSamu Onkalo 552500fe141SSamu Onkalo static struct attribute *lp5521_led_attributes[] = { 553500fe141SSamu Onkalo &dev_attr_led_current.attr, 554500fe141SSamu Onkalo &dev_attr_max_current.attr, 555500fe141SSamu Onkalo NULL, 556500fe141SSamu Onkalo }; 557500fe141SSamu Onkalo 558500fe141SSamu Onkalo static struct attribute_group lp5521_led_attribute_group = { 559500fe141SSamu Onkalo .attrs = lp5521_led_attributes 560500fe141SSamu Onkalo }; 561500fe141SSamu Onkalo 562500fe141SSamu Onkalo /* device attributes */ 563500fe141SSamu Onkalo static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUGO, 564500fe141SSamu Onkalo show_engine1_mode, store_engine1_mode); 565500fe141SSamu Onkalo static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUGO, 566500fe141SSamu Onkalo show_engine2_mode, store_engine2_mode); 567500fe141SSamu Onkalo static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUGO, 568500fe141SSamu Onkalo show_engine3_mode, store_engine3_mode); 569500fe141SSamu Onkalo static DEVICE_ATTR(engine1_load, S_IWUGO, NULL, store_engine1_load); 570500fe141SSamu Onkalo static DEVICE_ATTR(engine2_load, S_IWUGO, NULL, store_engine2_load); 571500fe141SSamu Onkalo static DEVICE_ATTR(engine3_load, S_IWUGO, NULL, store_engine3_load); 572500fe141SSamu Onkalo static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL); 573500fe141SSamu Onkalo 574500fe141SSamu Onkalo static struct attribute *lp5521_attributes[] = { 575500fe141SSamu Onkalo &dev_attr_engine1_mode.attr, 576500fe141SSamu Onkalo &dev_attr_engine2_mode.attr, 577500fe141SSamu Onkalo &dev_attr_engine3_mode.attr, 578500fe141SSamu Onkalo &dev_attr_selftest.attr, 579500fe141SSamu Onkalo NULL 580500fe141SSamu Onkalo }; 581500fe141SSamu Onkalo 582500fe141SSamu Onkalo static struct attribute *lp5521_engine1_attributes[] = { 583500fe141SSamu Onkalo &dev_attr_engine1_load.attr, 584500fe141SSamu Onkalo NULL 585500fe141SSamu Onkalo }; 586500fe141SSamu Onkalo 587500fe141SSamu Onkalo static struct attribute *lp5521_engine2_attributes[] = { 588500fe141SSamu Onkalo &dev_attr_engine2_load.attr, 589500fe141SSamu Onkalo NULL 590500fe141SSamu Onkalo }; 591500fe141SSamu Onkalo 592500fe141SSamu Onkalo static struct attribute *lp5521_engine3_attributes[] = { 593500fe141SSamu Onkalo &dev_attr_engine3_load.attr, 594500fe141SSamu Onkalo NULL 595500fe141SSamu Onkalo }; 596500fe141SSamu Onkalo 597500fe141SSamu Onkalo static const struct attribute_group lp5521_group = { 598500fe141SSamu Onkalo .attrs = lp5521_attributes, 599500fe141SSamu Onkalo }; 600500fe141SSamu Onkalo 601500fe141SSamu Onkalo static const struct attribute_group lp5521_engine_group[] = { 602500fe141SSamu Onkalo {.attrs = lp5521_engine1_attributes }, 603500fe141SSamu Onkalo {.attrs = lp5521_engine2_attributes }, 604500fe141SSamu Onkalo {.attrs = lp5521_engine3_attributes }, 605500fe141SSamu Onkalo }; 606500fe141SSamu Onkalo 607500fe141SSamu Onkalo static int lp5521_register_sysfs(struct i2c_client *client) 608500fe141SSamu Onkalo { 609500fe141SSamu Onkalo struct device *dev = &client->dev; 610500fe141SSamu Onkalo return sysfs_create_group(&dev->kobj, &lp5521_group); 611500fe141SSamu Onkalo } 612500fe141SSamu Onkalo 613500fe141SSamu Onkalo static void lp5521_unregister_sysfs(struct i2c_client *client) 614500fe141SSamu Onkalo { 615500fe141SSamu Onkalo struct lp5521_chip *chip = i2c_get_clientdata(client); 616500fe141SSamu Onkalo struct device *dev = &client->dev; 617500fe141SSamu Onkalo int i; 618500fe141SSamu Onkalo 619500fe141SSamu Onkalo sysfs_remove_group(&dev->kobj, &lp5521_group); 620500fe141SSamu Onkalo 621500fe141SSamu Onkalo for (i = 0; i < ARRAY_SIZE(chip->engines); i++) { 622500fe141SSamu Onkalo if (chip->engines[i].mode == LP5521_CMD_LOAD) 623500fe141SSamu Onkalo sysfs_remove_group(&dev->kobj, 624500fe141SSamu Onkalo chip->engines[i].attributes); 625500fe141SSamu Onkalo } 626500fe141SSamu Onkalo 627500fe141SSamu Onkalo for (i = 0; i < chip->num_leds; i++) 628500fe141SSamu Onkalo sysfs_remove_group(&chip->leds[i].cdev.dev->kobj, 629500fe141SSamu Onkalo &lp5521_led_attribute_group); 630500fe141SSamu Onkalo } 631500fe141SSamu Onkalo 632500fe141SSamu Onkalo static int __init lp5521_init_led(struct lp5521_led *led, 633500fe141SSamu Onkalo struct i2c_client *client, 634500fe141SSamu Onkalo int chan, struct lp5521_platform_data *pdata) 635500fe141SSamu Onkalo { 636500fe141SSamu Onkalo struct device *dev = &client->dev; 637500fe141SSamu Onkalo char name[32]; 638500fe141SSamu Onkalo int res; 639500fe141SSamu Onkalo 640500fe141SSamu Onkalo if (chan >= LP5521_MAX_LEDS) 641500fe141SSamu Onkalo return -EINVAL; 642500fe141SSamu Onkalo 643500fe141SSamu Onkalo if (pdata->led_config[chan].led_current == 0) 644500fe141SSamu Onkalo return 0; 645500fe141SSamu Onkalo 646500fe141SSamu Onkalo led->led_current = pdata->led_config[chan].led_current; 647500fe141SSamu Onkalo led->max_current = pdata->led_config[chan].max_current; 648500fe141SSamu Onkalo led->chan_nr = pdata->led_config[chan].chan_nr; 649500fe141SSamu Onkalo 650500fe141SSamu Onkalo if (led->chan_nr >= LP5521_MAX_LEDS) { 651500fe141SSamu Onkalo dev_err(dev, "Use channel numbers between 0 and %d\n", 652500fe141SSamu Onkalo LP5521_MAX_LEDS - 1); 653500fe141SSamu Onkalo return -EINVAL; 654500fe141SSamu Onkalo } 655500fe141SSamu Onkalo 656500fe141SSamu Onkalo snprintf(name, sizeof(name), "%s:channel%d", client->name, chan); 657500fe141SSamu Onkalo led->cdev.brightness_set = lp5521_set_brightness; 658500fe141SSamu Onkalo led->cdev.name = name; 659500fe141SSamu Onkalo res = led_classdev_register(dev, &led->cdev); 660500fe141SSamu Onkalo if (res < 0) { 661500fe141SSamu Onkalo dev_err(dev, "couldn't register led on channel %d\n", chan); 662500fe141SSamu Onkalo return res; 663500fe141SSamu Onkalo } 664500fe141SSamu Onkalo 665500fe141SSamu Onkalo res = sysfs_create_group(&led->cdev.dev->kobj, 666500fe141SSamu Onkalo &lp5521_led_attribute_group); 667500fe141SSamu Onkalo if (res < 0) { 668500fe141SSamu Onkalo dev_err(dev, "couldn't register current attribute\n"); 669500fe141SSamu Onkalo led_classdev_unregister(&led->cdev); 670500fe141SSamu Onkalo return res; 671500fe141SSamu Onkalo } 672500fe141SSamu Onkalo return 0; 673500fe141SSamu Onkalo } 674500fe141SSamu Onkalo 675500fe141SSamu Onkalo static int lp5521_probe(struct i2c_client *client, 676500fe141SSamu Onkalo const struct i2c_device_id *id) 677500fe141SSamu Onkalo { 678500fe141SSamu Onkalo struct lp5521_chip *chip; 679500fe141SSamu Onkalo struct lp5521_platform_data *pdata; 680500fe141SSamu Onkalo int ret, i, led; 681500fe141SSamu Onkalo 682500fe141SSamu Onkalo chip = kzalloc(sizeof(*chip), GFP_KERNEL); 683500fe141SSamu Onkalo if (!chip) 684500fe141SSamu Onkalo return -ENOMEM; 685500fe141SSamu Onkalo 686500fe141SSamu Onkalo i2c_set_clientdata(client, chip); 687500fe141SSamu Onkalo chip->client = client; 688500fe141SSamu Onkalo 689500fe141SSamu Onkalo pdata = client->dev.platform_data; 690500fe141SSamu Onkalo 691500fe141SSamu Onkalo if (!pdata) { 692500fe141SSamu Onkalo dev_err(&client->dev, "no platform data\n"); 693500fe141SSamu Onkalo ret = -EINVAL; 694500fe141SSamu Onkalo goto fail1; 695500fe141SSamu Onkalo } 696500fe141SSamu Onkalo 697500fe141SSamu Onkalo mutex_init(&chip->lock); 698500fe141SSamu Onkalo 699500fe141SSamu Onkalo chip->pdata = pdata; 700500fe141SSamu Onkalo 701500fe141SSamu Onkalo if (pdata->setup_resources) { 702500fe141SSamu Onkalo ret = pdata->setup_resources(); 703500fe141SSamu Onkalo if (ret < 0) 704500fe141SSamu Onkalo goto fail1; 705500fe141SSamu Onkalo } 706500fe141SSamu Onkalo 707500fe141SSamu Onkalo if (pdata->enable) { 708500fe141SSamu Onkalo pdata->enable(0); 70909c76b0fSSamu Onkalo usleep_range(1000, 2000); /* Keep enable down at least 1ms */ 710500fe141SSamu Onkalo pdata->enable(1); 71109c76b0fSSamu Onkalo usleep_range(1000, 2000); /* 500us abs min. */ 712500fe141SSamu Onkalo } 713500fe141SSamu Onkalo 71495ea8eecSSamu Onkalo lp5521_write(client, LP5521_REG_RESET, 0xff); 71595ea8eecSSamu Onkalo usleep_range(10000, 20000); /* 71695ea8eecSSamu Onkalo * Exact value is not available. 10 - 20ms 71795ea8eecSSamu Onkalo * appears to be enough for reset. 71895ea8eecSSamu Onkalo */ 719500fe141SSamu Onkalo ret = lp5521_detect(client); 720500fe141SSamu Onkalo 721500fe141SSamu Onkalo if (ret) { 722500fe141SSamu Onkalo dev_err(&client->dev, "Chip not found\n"); 723500fe141SSamu Onkalo goto fail2; 724500fe141SSamu Onkalo } 725500fe141SSamu Onkalo 726500fe141SSamu Onkalo dev_info(&client->dev, "%s programmable led chip found\n", id->name); 727500fe141SSamu Onkalo 728500fe141SSamu Onkalo ret = lp5521_configure(client, lp5521_engine_group); 729500fe141SSamu Onkalo if (ret < 0) { 730500fe141SSamu Onkalo dev_err(&client->dev, "error configuring chip\n"); 731500fe141SSamu Onkalo goto fail2; 732500fe141SSamu Onkalo } 733500fe141SSamu Onkalo 734500fe141SSamu Onkalo /* Initialize leds */ 735500fe141SSamu Onkalo chip->num_channels = pdata->num_channels; 736500fe141SSamu Onkalo chip->num_leds = 0; 737500fe141SSamu Onkalo led = 0; 738500fe141SSamu Onkalo for (i = 0; i < pdata->num_channels; i++) { 739500fe141SSamu Onkalo /* Do not initialize channels that are not connected */ 740500fe141SSamu Onkalo if (pdata->led_config[i].led_current == 0) 741500fe141SSamu Onkalo continue; 742500fe141SSamu Onkalo 743500fe141SSamu Onkalo ret = lp5521_init_led(&chip->leds[led], client, i, pdata); 744500fe141SSamu Onkalo if (ret) { 745500fe141SSamu Onkalo dev_err(&client->dev, "error initializing leds\n"); 746500fe141SSamu Onkalo goto fail3; 747500fe141SSamu Onkalo } 748500fe141SSamu Onkalo chip->num_leds++; 749500fe141SSamu Onkalo 750500fe141SSamu Onkalo chip->leds[led].id = led; 751500fe141SSamu Onkalo /* Set initial LED current */ 752500fe141SSamu Onkalo lp5521_set_led_current(chip, led, 753500fe141SSamu Onkalo chip->leds[led].led_current); 754500fe141SSamu Onkalo 755500fe141SSamu Onkalo INIT_WORK(&(chip->leds[led].brightness_work), 756500fe141SSamu Onkalo lp5521_led_brightness_work); 757500fe141SSamu Onkalo 758500fe141SSamu Onkalo led++; 759500fe141SSamu Onkalo } 760500fe141SSamu Onkalo 761500fe141SSamu Onkalo ret = lp5521_register_sysfs(client); 762500fe141SSamu Onkalo if (ret) { 763500fe141SSamu Onkalo dev_err(&client->dev, "registering sysfs failed\n"); 764500fe141SSamu Onkalo goto fail3; 765500fe141SSamu Onkalo } 766500fe141SSamu Onkalo return ret; 767500fe141SSamu Onkalo fail3: 768500fe141SSamu Onkalo for (i = 0; i < chip->num_leds; i++) { 769500fe141SSamu Onkalo led_classdev_unregister(&chip->leds[i].cdev); 770500fe141SSamu Onkalo cancel_work_sync(&chip->leds[i].brightness_work); 771500fe141SSamu Onkalo } 772500fe141SSamu Onkalo fail2: 773500fe141SSamu Onkalo if (pdata->enable) 774500fe141SSamu Onkalo pdata->enable(0); 775500fe141SSamu Onkalo if (pdata->release_resources) 776500fe141SSamu Onkalo pdata->release_resources(); 777500fe141SSamu Onkalo fail1: 778500fe141SSamu Onkalo kfree(chip); 779500fe141SSamu Onkalo return ret; 780500fe141SSamu Onkalo } 781500fe141SSamu Onkalo 782500fe141SSamu Onkalo static int lp5521_remove(struct i2c_client *client) 783500fe141SSamu Onkalo { 784500fe141SSamu Onkalo struct lp5521_chip *chip = i2c_get_clientdata(client); 785500fe141SSamu Onkalo int i; 786500fe141SSamu Onkalo 787500fe141SSamu Onkalo lp5521_unregister_sysfs(client); 788500fe141SSamu Onkalo 789500fe141SSamu Onkalo for (i = 0; i < chip->num_leds; i++) { 790500fe141SSamu Onkalo led_classdev_unregister(&chip->leds[i].cdev); 791500fe141SSamu Onkalo cancel_work_sync(&chip->leds[i].brightness_work); 792500fe141SSamu Onkalo } 793500fe141SSamu Onkalo 794500fe141SSamu Onkalo if (chip->pdata->enable) 795500fe141SSamu Onkalo chip->pdata->enable(0); 796500fe141SSamu Onkalo if (chip->pdata->release_resources) 797500fe141SSamu Onkalo chip->pdata->release_resources(); 798500fe141SSamu Onkalo kfree(chip); 799500fe141SSamu Onkalo return 0; 800500fe141SSamu Onkalo } 801500fe141SSamu Onkalo 802500fe141SSamu Onkalo static const struct i2c_device_id lp5521_id[] = { 803500fe141SSamu Onkalo { "lp5521", 0 }, /* Three channel chip */ 804500fe141SSamu Onkalo { } 805500fe141SSamu Onkalo }; 806500fe141SSamu Onkalo MODULE_DEVICE_TABLE(i2c, lp5521_id); 807500fe141SSamu Onkalo 808500fe141SSamu Onkalo static struct i2c_driver lp5521_driver = { 809500fe141SSamu Onkalo .driver = { 810500fe141SSamu Onkalo .name = "lp5521", 811500fe141SSamu Onkalo }, 812500fe141SSamu Onkalo .probe = lp5521_probe, 813500fe141SSamu Onkalo .remove = lp5521_remove, 814500fe141SSamu Onkalo .id_table = lp5521_id, 815500fe141SSamu Onkalo }; 816500fe141SSamu Onkalo 817500fe141SSamu Onkalo static int __init lp5521_init(void) 818500fe141SSamu Onkalo { 819500fe141SSamu Onkalo int ret; 820500fe141SSamu Onkalo 821500fe141SSamu Onkalo ret = i2c_add_driver(&lp5521_driver); 822500fe141SSamu Onkalo 823500fe141SSamu Onkalo if (ret < 0) 824500fe141SSamu Onkalo printk(KERN_ALERT "Adding lp5521 driver failed\n"); 825500fe141SSamu Onkalo 826500fe141SSamu Onkalo return ret; 827500fe141SSamu Onkalo } 828500fe141SSamu Onkalo 829500fe141SSamu Onkalo static void __exit lp5521_exit(void) 830500fe141SSamu Onkalo { 831500fe141SSamu Onkalo i2c_del_driver(&lp5521_driver); 832500fe141SSamu Onkalo } 833500fe141SSamu Onkalo 834500fe141SSamu Onkalo module_init(lp5521_init); 835500fe141SSamu Onkalo module_exit(lp5521_exit); 836500fe141SSamu Onkalo 837500fe141SSamu Onkalo MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo"); 838500fe141SSamu Onkalo MODULE_DESCRIPTION("LP5521 LED engine"); 839500fe141SSamu Onkalo MODULE_LICENSE("GPL v2"); 840