Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * LP5521 LED chip driver. |
| 3 | * |
| 4 | * Copyright (C) 2010 Nokia Corporation |
| 5 | * |
| 6 | * Contact: Samu Onkalo <samu.p.onkalo@nokia.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * version 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 20 | * 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/i2c.h> |
| 26 | #include <linux/mutex.h> |
| 27 | #include <linux/gpio.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/ctype.h> |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/wait.h> |
| 33 | #include <linux/leds.h> |
| 34 | #include <linux/leds-lp5521.h> |
| 35 | #include <linux/workqueue.h> |
| 36 | #include <linux/slab.h> |
| 37 | |
| 38 | #define LP5521_PROGRAM_LENGTH 32 /* in bytes */ |
| 39 | |
| 40 | #define LP5521_MAX_LEDS 3 /* Maximum number of LEDs */ |
| 41 | #define LP5521_MAX_ENGINES 3 /* Maximum number of engines */ |
| 42 | |
| 43 | #define LP5521_ENG_MASK_BASE 0x30 /* 00110000 */ |
| 44 | #define LP5521_ENG_STATUS_MASK 0x07 /* 00000111 */ |
| 45 | |
| 46 | #define LP5521_CMD_LOAD 0x15 /* 00010101 */ |
| 47 | #define LP5521_CMD_RUN 0x2a /* 00101010 */ |
| 48 | #define LP5521_CMD_DIRECT 0x3f /* 00111111 */ |
| 49 | #define LP5521_CMD_DISABLED 0x00 /* 00000000 */ |
| 50 | |
| 51 | /* Registers */ |
| 52 | #define LP5521_REG_ENABLE 0x00 |
| 53 | #define LP5521_REG_OP_MODE 0x01 |
| 54 | #define LP5521_REG_R_PWM 0x02 |
| 55 | #define LP5521_REG_G_PWM 0x03 |
| 56 | #define LP5521_REG_B_PWM 0x04 |
| 57 | #define LP5521_REG_R_CURRENT 0x05 |
| 58 | #define LP5521_REG_G_CURRENT 0x06 |
| 59 | #define LP5521_REG_B_CURRENT 0x07 |
| 60 | #define LP5521_REG_CONFIG 0x08 |
| 61 | #define LP5521_REG_R_CHANNEL_PC 0x09 |
| 62 | #define LP5521_REG_G_CHANNEL_PC 0x0A |
| 63 | #define LP5521_REG_B_CHANNEL_PC 0x0B |
| 64 | #define LP5521_REG_STATUS 0x0C |
| 65 | #define LP5521_REG_RESET 0x0D |
| 66 | #define LP5521_REG_GPO 0x0E |
| 67 | #define LP5521_REG_R_PROG_MEM 0x10 |
| 68 | #define LP5521_REG_G_PROG_MEM 0x30 |
| 69 | #define LP5521_REG_B_PROG_MEM 0x50 |
| 70 | |
| 71 | #define LP5521_PROG_MEM_BASE LP5521_REG_R_PROG_MEM |
| 72 | #define LP5521_PROG_MEM_SIZE 0x20 |
| 73 | |
| 74 | /* Base register to set LED current */ |
| 75 | #define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT |
| 76 | |
| 77 | /* Base register to set the brightness */ |
| 78 | #define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM |
| 79 | |
| 80 | /* Bits in ENABLE register */ |
| 81 | #define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */ |
| 82 | #define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */ |
| 83 | #define LP5521_EXEC_RUN 0x2A |
| 84 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 85 | /* Status */ |
| 86 | #define LP5521_EXT_CLK_USED 0x08 |
| 87 | |
Srinidhi KASAGAR | b3c49c0 | 2011-10-31 17:12:24 -0700 | [diff] [blame] | 88 | /* default R channel current register value */ |
| 89 | #define LP5521_REG_R_CURR_DEFAULT 0xAF |
| 90 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 91 | struct lp5521_engine { |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 92 | int id; |
| 93 | u8 mode; |
| 94 | u8 prog_page; |
| 95 | u8 engine_mask; |
| 96 | }; |
| 97 | |
| 98 | struct lp5521_led { |
| 99 | int id; |
| 100 | u8 chan_nr; |
| 101 | u8 led_current; |
| 102 | u8 max_current; |
| 103 | struct led_classdev cdev; |
| 104 | struct work_struct brightness_work; |
| 105 | u8 brightness; |
| 106 | }; |
| 107 | |
| 108 | struct lp5521_chip { |
| 109 | struct lp5521_platform_data *pdata; |
| 110 | struct mutex lock; /* Serialize control */ |
| 111 | struct i2c_client *client; |
| 112 | struct lp5521_engine engines[LP5521_MAX_ENGINES]; |
| 113 | struct lp5521_led leds[LP5521_MAX_LEDS]; |
| 114 | u8 num_channels; |
| 115 | u8 num_leds; |
| 116 | }; |
| 117 | |
Samu Onkalo | 9fdb18b | 2010-11-24 12:57:02 -0800 | [diff] [blame] | 118 | static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev) |
| 119 | { |
| 120 | return container_of(cdev, struct lp5521_led, cdev); |
| 121 | } |
| 122 | |
| 123 | static inline struct lp5521_chip *engine_to_lp5521(struct lp5521_engine *engine) |
| 124 | { |
| 125 | return container_of(engine, struct lp5521_chip, |
| 126 | engines[engine->id - 1]); |
| 127 | } |
| 128 | |
| 129 | static inline struct lp5521_chip *led_to_lp5521(struct lp5521_led *led) |
| 130 | { |
| 131 | return container_of(led, struct lp5521_chip, |
| 132 | leds[led->id]); |
| 133 | } |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 134 | |
| 135 | static void lp5521_led_brightness_work(struct work_struct *work); |
| 136 | |
| 137 | static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value) |
| 138 | { |
| 139 | return i2c_smbus_write_byte_data(client, reg, value); |
| 140 | } |
| 141 | |
| 142 | static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf) |
| 143 | { |
| 144 | s32 ret; |
| 145 | |
| 146 | ret = i2c_smbus_read_byte_data(client, reg); |
| 147 | if (ret < 0) |
| 148 | return -EIO; |
| 149 | |
| 150 | *buf = ret; |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static int lp5521_set_engine_mode(struct lp5521_engine *engine, u8 mode) |
| 155 | { |
| 156 | struct lp5521_chip *chip = engine_to_lp5521(engine); |
| 157 | struct i2c_client *client = chip->client; |
| 158 | int ret; |
| 159 | u8 engine_state; |
| 160 | |
| 161 | /* Only transition between RUN and DIRECT mode are handled here */ |
| 162 | if (mode == LP5521_CMD_LOAD) |
| 163 | return 0; |
| 164 | |
| 165 | if (mode == LP5521_CMD_DISABLED) |
| 166 | mode = LP5521_CMD_DIRECT; |
| 167 | |
| 168 | ret = lp5521_read(client, LP5521_REG_OP_MODE, &engine_state); |
Axel Lin | fa0ea0e | 2011-10-31 17:12:12 -0700 | [diff] [blame] | 169 | if (ret < 0) |
| 170 | return ret; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 171 | |
| 172 | /* set mode only for this engine */ |
| 173 | engine_state &= ~(engine->engine_mask); |
| 174 | mode &= engine->engine_mask; |
| 175 | engine_state |= mode; |
Axel Lin | fa0ea0e | 2011-10-31 17:12:12 -0700 | [diff] [blame] | 176 | return lp5521_write(client, LP5521_REG_OP_MODE, engine_state); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern) |
| 180 | { |
| 181 | struct lp5521_chip *chip = engine_to_lp5521(eng); |
| 182 | struct i2c_client *client = chip->client; |
| 183 | int ret; |
| 184 | int addr; |
| 185 | u8 mode; |
| 186 | |
| 187 | /* move current engine to direct mode and remember the state */ |
| 188 | ret = lp5521_set_engine_mode(eng, LP5521_CMD_DIRECT); |
Samu Onkalo | 09c76b0 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 189 | /* Mode change requires min 500 us delay. 1 - 2 ms with margin */ |
| 190 | usleep_range(1000, 2000); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 191 | ret |= lp5521_read(client, LP5521_REG_OP_MODE, &mode); |
| 192 | |
| 193 | /* For loading, all the engines to load mode */ |
| 194 | lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT); |
Samu Onkalo | 09c76b0 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 195 | /* Mode change requires min 500 us delay. 1 - 2 ms with margin */ |
| 196 | usleep_range(1000, 2000); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 197 | lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_LOAD); |
Samu Onkalo | 09c76b0 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 198 | /* Mode change requires min 500 us delay. 1 - 2 ms with margin */ |
| 199 | usleep_range(1000, 2000); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 200 | |
| 201 | addr = LP5521_PROG_MEM_BASE + eng->prog_page * LP5521_PROG_MEM_SIZE; |
| 202 | i2c_smbus_write_i2c_block_data(client, |
| 203 | addr, |
| 204 | LP5521_PROG_MEM_SIZE, |
| 205 | pattern); |
| 206 | |
| 207 | ret |= lp5521_write(client, LP5521_REG_OP_MODE, mode); |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | static int lp5521_set_led_current(struct lp5521_chip *chip, int led, u8 curr) |
| 212 | { |
| 213 | return lp5521_write(chip->client, |
| 214 | LP5521_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr, |
| 215 | curr); |
| 216 | } |
| 217 | |
Samu Onkalo | d4e7ad0 | 2011-01-12 16:59:19 -0800 | [diff] [blame] | 218 | static void lp5521_init_engine(struct lp5521_chip *chip) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 219 | { |
| 220 | int i; |
| 221 | for (i = 0; i < ARRAY_SIZE(chip->engines); i++) { |
| 222 | chip->engines[i].id = i + 1; |
| 223 | chip->engines[i].engine_mask = LP5521_ENG_MASK_BASE >> (i * 2); |
| 224 | chip->engines[i].prog_page = i; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Samu Onkalo | d4e7ad0 | 2011-01-12 16:59:19 -0800 | [diff] [blame] | 228 | static int lp5521_configure(struct i2c_client *client) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 229 | { |
| 230 | struct lp5521_chip *chip = i2c_get_clientdata(client); |
| 231 | int ret; |
Kim, Milo | 3b49aac | 2012-03-23 15:02:08 -0700 | [diff] [blame^] | 232 | u8 cfg; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 233 | |
Samu Onkalo | d4e7ad0 | 2011-01-12 16:59:19 -0800 | [diff] [blame] | 234 | lp5521_init_engine(chip); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 235 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 236 | /* Set all PWMs to direct control mode */ |
| 237 | ret = lp5521_write(client, LP5521_REG_OP_MODE, 0x3F); |
| 238 | |
Kim, Milo | 3b49aac | 2012-03-23 15:02:08 -0700 | [diff] [blame^] | 239 | cfg = chip->pdata->update_config ? |
| 240 | : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT); |
| 241 | ret |= lp5521_write(client, LP5521_REG_CONFIG, cfg); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 242 | |
| 243 | /* Initialize all channels PWM to zero -> leds off */ |
| 244 | ret |= lp5521_write(client, LP5521_REG_R_PWM, 0); |
| 245 | ret |= lp5521_write(client, LP5521_REG_G_PWM, 0); |
| 246 | ret |= lp5521_write(client, LP5521_REG_B_PWM, 0); |
| 247 | |
| 248 | /* Set engines are set to run state when OP_MODE enables engines */ |
| 249 | ret |= lp5521_write(client, LP5521_REG_ENABLE, |
| 250 | LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM | |
| 251 | LP5521_EXEC_RUN); |
Samu Onkalo | 09c76b0 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 252 | /* enable takes 500us. 1 - 2 ms leaves some margin */ |
| 253 | usleep_range(1000, 2000); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 254 | |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf) |
| 259 | { |
| 260 | int ret; |
| 261 | u8 status; |
| 262 | |
| 263 | ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status); |
| 264 | if (ret < 0) |
| 265 | return ret; |
| 266 | |
| 267 | /* Check that ext clock is really in use if requested */ |
| 268 | if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT) |
| 269 | if ((status & LP5521_EXT_CLK_USED) == 0) |
| 270 | return -EIO; |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | static void lp5521_set_brightness(struct led_classdev *cdev, |
| 275 | enum led_brightness brightness) |
| 276 | { |
| 277 | struct lp5521_led *led = cdev_to_led(cdev); |
| 278 | led->brightness = (u8)brightness; |
| 279 | schedule_work(&led->brightness_work); |
| 280 | } |
| 281 | |
| 282 | static void lp5521_led_brightness_work(struct work_struct *work) |
| 283 | { |
| 284 | struct lp5521_led *led = container_of(work, |
| 285 | struct lp5521_led, |
| 286 | brightness_work); |
| 287 | struct lp5521_chip *chip = led_to_lp5521(led); |
| 288 | struct i2c_client *client = chip->client; |
| 289 | |
| 290 | mutex_lock(&chip->lock); |
| 291 | lp5521_write(client, LP5521_REG_LED_PWM_BASE + led->chan_nr, |
| 292 | led->brightness); |
| 293 | mutex_unlock(&chip->lock); |
| 294 | } |
| 295 | |
| 296 | /* Detect the chip by setting its ENABLE register and reading it back. */ |
| 297 | static int lp5521_detect(struct i2c_client *client) |
| 298 | { |
| 299 | int ret; |
| 300 | u8 buf; |
| 301 | |
| 302 | ret = lp5521_write(client, LP5521_REG_ENABLE, |
| 303 | LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM); |
| 304 | if (ret) |
| 305 | return ret; |
Samu Onkalo | 09c76b0 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 306 | /* enable takes 500us. 1 - 2 ms leaves some margin */ |
| 307 | usleep_range(1000, 2000); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 308 | ret = lp5521_read(client, LP5521_REG_ENABLE, &buf); |
| 309 | if (ret) |
| 310 | return ret; |
| 311 | if (buf != (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)) |
| 312 | return -ENODEV; |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /* Set engine mode and create appropriate sysfs attributes, if required. */ |
| 318 | static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode) |
| 319 | { |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 320 | int ret = 0; |
| 321 | |
| 322 | /* if in that mode already do nothing, except for run */ |
| 323 | if (mode == engine->mode && mode != LP5521_CMD_RUN) |
| 324 | return 0; |
| 325 | |
| 326 | if (mode == LP5521_CMD_RUN) { |
| 327 | ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN); |
| 328 | } else if (mode == LP5521_CMD_LOAD) { |
| 329 | lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED); |
| 330 | lp5521_set_engine_mode(engine, LP5521_CMD_LOAD); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 331 | } else if (mode == LP5521_CMD_DISABLED) { |
| 332 | lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED); |
| 333 | } |
| 334 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 335 | engine->mode = mode; |
| 336 | |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | static int lp5521_do_store_load(struct lp5521_engine *engine, |
| 341 | const char *buf, size_t len) |
| 342 | { |
| 343 | struct lp5521_chip *chip = engine_to_lp5521(engine); |
| 344 | struct i2c_client *client = chip->client; |
| 345 | int ret, nrchars, offset = 0, i = 0; |
| 346 | char c[3]; |
| 347 | unsigned cmd; |
| 348 | u8 pattern[LP5521_PROGRAM_LENGTH] = {0}; |
| 349 | |
| 350 | while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) { |
| 351 | /* separate sscanfs because length is working only for %s */ |
| 352 | ret = sscanf(buf + offset, "%2s%n ", c, &nrchars); |
Vasiliy Kulikov | 2260209 | 2011-01-12 16:59:14 -0800 | [diff] [blame] | 353 | if (ret != 2) |
| 354 | goto fail; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 355 | ret = sscanf(c, "%2x", &cmd); |
| 356 | if (ret != 1) |
| 357 | goto fail; |
| 358 | pattern[i] = (u8)cmd; |
| 359 | |
| 360 | offset += nrchars; |
| 361 | i++; |
| 362 | } |
| 363 | |
| 364 | /* Each instruction is 16bit long. Check that length is even */ |
| 365 | if (i % 2) |
| 366 | goto fail; |
| 367 | |
| 368 | mutex_lock(&chip->lock); |
Samu Onkalo | d4e7ad0 | 2011-01-12 16:59:19 -0800 | [diff] [blame] | 369 | if (engine->mode == LP5521_CMD_LOAD) |
| 370 | ret = lp5521_load_program(engine, pattern); |
| 371 | else |
| 372 | ret = -EINVAL; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 373 | mutex_unlock(&chip->lock); |
| 374 | |
| 375 | if (ret) { |
| 376 | dev_err(&client->dev, "failed loading pattern\n"); |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | return len; |
| 381 | fail: |
| 382 | dev_err(&client->dev, "wrong pattern format\n"); |
| 383 | return -EINVAL; |
| 384 | } |
| 385 | |
| 386 | static ssize_t store_engine_load(struct device *dev, |
| 387 | struct device_attribute *attr, |
| 388 | const char *buf, size_t len, int nr) |
| 389 | { |
| 390 | struct i2c_client *client = to_i2c_client(dev); |
| 391 | struct lp5521_chip *chip = i2c_get_clientdata(client); |
| 392 | return lp5521_do_store_load(&chip->engines[nr - 1], buf, len); |
| 393 | } |
| 394 | |
| 395 | #define store_load(nr) \ |
| 396 | static ssize_t store_engine##nr##_load(struct device *dev, \ |
| 397 | struct device_attribute *attr, \ |
| 398 | const char *buf, size_t len) \ |
| 399 | { \ |
| 400 | return store_engine_load(dev, attr, buf, len, nr); \ |
| 401 | } |
| 402 | store_load(1) |
| 403 | store_load(2) |
| 404 | store_load(3) |
| 405 | |
| 406 | static ssize_t show_engine_mode(struct device *dev, |
| 407 | struct device_attribute *attr, |
| 408 | char *buf, int nr) |
| 409 | { |
| 410 | struct i2c_client *client = to_i2c_client(dev); |
| 411 | struct lp5521_chip *chip = i2c_get_clientdata(client); |
| 412 | switch (chip->engines[nr - 1].mode) { |
| 413 | case LP5521_CMD_RUN: |
| 414 | return sprintf(buf, "run\n"); |
| 415 | case LP5521_CMD_LOAD: |
| 416 | return sprintf(buf, "load\n"); |
| 417 | case LP5521_CMD_DISABLED: |
| 418 | return sprintf(buf, "disabled\n"); |
| 419 | default: |
| 420 | return sprintf(buf, "disabled\n"); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | #define show_mode(nr) \ |
| 425 | static ssize_t show_engine##nr##_mode(struct device *dev, \ |
| 426 | struct device_attribute *attr, \ |
| 427 | char *buf) \ |
| 428 | { \ |
| 429 | return show_engine_mode(dev, attr, buf, nr); \ |
| 430 | } |
| 431 | show_mode(1) |
| 432 | show_mode(2) |
| 433 | show_mode(3) |
| 434 | |
| 435 | static ssize_t store_engine_mode(struct device *dev, |
| 436 | struct device_attribute *attr, |
| 437 | const char *buf, size_t len, int nr) |
| 438 | { |
| 439 | struct i2c_client *client = to_i2c_client(dev); |
| 440 | struct lp5521_chip *chip = i2c_get_clientdata(client); |
| 441 | struct lp5521_engine *engine = &chip->engines[nr - 1]; |
| 442 | mutex_lock(&chip->lock); |
| 443 | |
| 444 | if (!strncmp(buf, "run", 3)) |
| 445 | lp5521_set_mode(engine, LP5521_CMD_RUN); |
| 446 | else if (!strncmp(buf, "load", 4)) |
| 447 | lp5521_set_mode(engine, LP5521_CMD_LOAD); |
| 448 | else if (!strncmp(buf, "disabled", 8)) |
| 449 | lp5521_set_mode(engine, LP5521_CMD_DISABLED); |
| 450 | |
| 451 | mutex_unlock(&chip->lock); |
| 452 | return len; |
| 453 | } |
| 454 | |
| 455 | #define store_mode(nr) \ |
| 456 | static ssize_t store_engine##nr##_mode(struct device *dev, \ |
| 457 | struct device_attribute *attr, \ |
| 458 | const char *buf, size_t len) \ |
| 459 | { \ |
| 460 | return store_engine_mode(dev, attr, buf, len, nr); \ |
| 461 | } |
| 462 | store_mode(1) |
| 463 | store_mode(2) |
| 464 | store_mode(3) |
| 465 | |
| 466 | static ssize_t show_max_current(struct device *dev, |
| 467 | struct device_attribute *attr, |
| 468 | char *buf) |
| 469 | { |
| 470 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 471 | struct lp5521_led *led = cdev_to_led(led_cdev); |
| 472 | |
| 473 | return sprintf(buf, "%d\n", led->max_current); |
| 474 | } |
| 475 | |
| 476 | static ssize_t show_current(struct device *dev, |
| 477 | struct device_attribute *attr, |
| 478 | char *buf) |
| 479 | { |
| 480 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 481 | struct lp5521_led *led = cdev_to_led(led_cdev); |
| 482 | |
| 483 | return sprintf(buf, "%d\n", led->led_current); |
| 484 | } |
| 485 | |
| 486 | static ssize_t store_current(struct device *dev, |
| 487 | struct device_attribute *attr, |
| 488 | const char *buf, size_t len) |
| 489 | { |
| 490 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 491 | struct lp5521_led *led = cdev_to_led(led_cdev); |
| 492 | struct lp5521_chip *chip = led_to_lp5521(led); |
| 493 | ssize_t ret; |
| 494 | unsigned long curr; |
| 495 | |
| 496 | if (strict_strtoul(buf, 0, &curr)) |
| 497 | return -EINVAL; |
| 498 | |
| 499 | if (curr > led->max_current) |
| 500 | return -EINVAL; |
| 501 | |
| 502 | mutex_lock(&chip->lock); |
| 503 | ret = lp5521_set_led_current(chip, led->id, curr); |
| 504 | mutex_unlock(&chip->lock); |
| 505 | |
| 506 | if (ret < 0) |
| 507 | return ret; |
| 508 | |
| 509 | led->led_current = (u8)curr; |
| 510 | |
| 511 | return len; |
| 512 | } |
| 513 | |
| 514 | static ssize_t lp5521_selftest(struct device *dev, |
| 515 | struct device_attribute *attr, |
| 516 | char *buf) |
| 517 | { |
| 518 | struct i2c_client *client = to_i2c_client(dev); |
| 519 | struct lp5521_chip *chip = i2c_get_clientdata(client); |
| 520 | int ret; |
| 521 | |
| 522 | mutex_lock(&chip->lock); |
| 523 | ret = lp5521_run_selftest(chip, buf); |
| 524 | mutex_unlock(&chip->lock); |
| 525 | return sprintf(buf, "%s\n", ret ? "FAIL" : "OK"); |
| 526 | } |
| 527 | |
| 528 | /* led class device attributes */ |
Vasiliy Kulikov | 67d1da7 | 2011-03-22 16:30:19 -0700 | [diff] [blame] | 529 | static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 530 | static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL); |
| 531 | |
| 532 | static struct attribute *lp5521_led_attributes[] = { |
| 533 | &dev_attr_led_current.attr, |
| 534 | &dev_attr_max_current.attr, |
| 535 | NULL, |
| 536 | }; |
| 537 | |
| 538 | static struct attribute_group lp5521_led_attribute_group = { |
| 539 | .attrs = lp5521_led_attributes |
| 540 | }; |
| 541 | |
| 542 | /* device attributes */ |
Vasiliy Kulikov | 67d1da7 | 2011-03-22 16:30:19 -0700 | [diff] [blame] | 543 | static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 544 | show_engine1_mode, store_engine1_mode); |
Vasiliy Kulikov | 67d1da7 | 2011-03-22 16:30:19 -0700 | [diff] [blame] | 545 | static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 546 | show_engine2_mode, store_engine2_mode); |
Vasiliy Kulikov | 67d1da7 | 2011-03-22 16:30:19 -0700 | [diff] [blame] | 547 | static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 548 | show_engine3_mode, store_engine3_mode); |
Vasiliy Kulikov | 67d1da7 | 2011-03-22 16:30:19 -0700 | [diff] [blame] | 549 | static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load); |
| 550 | static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load); |
| 551 | static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 552 | static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL); |
| 553 | |
| 554 | static struct attribute *lp5521_attributes[] = { |
| 555 | &dev_attr_engine1_mode.attr, |
| 556 | &dev_attr_engine2_mode.attr, |
| 557 | &dev_attr_engine3_mode.attr, |
| 558 | &dev_attr_selftest.attr, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 559 | &dev_attr_engine1_load.attr, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 560 | &dev_attr_engine2_load.attr, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 561 | &dev_attr_engine3_load.attr, |
| 562 | NULL |
| 563 | }; |
| 564 | |
| 565 | static const struct attribute_group lp5521_group = { |
| 566 | .attrs = lp5521_attributes, |
| 567 | }; |
| 568 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 569 | static int lp5521_register_sysfs(struct i2c_client *client) |
| 570 | { |
| 571 | struct device *dev = &client->dev; |
| 572 | return sysfs_create_group(&dev->kobj, &lp5521_group); |
| 573 | } |
| 574 | |
| 575 | static void lp5521_unregister_sysfs(struct i2c_client *client) |
| 576 | { |
| 577 | struct lp5521_chip *chip = i2c_get_clientdata(client); |
| 578 | struct device *dev = &client->dev; |
| 579 | int i; |
| 580 | |
| 581 | sysfs_remove_group(&dev->kobj, &lp5521_group); |
| 582 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 583 | for (i = 0; i < chip->num_leds; i++) |
| 584 | sysfs_remove_group(&chip->leds[i].cdev.dev->kobj, |
| 585 | &lp5521_led_attribute_group); |
| 586 | } |
| 587 | |
Ralf Baechle | 5286bd9 | 2011-06-27 16:18:13 -0700 | [diff] [blame] | 588 | static int __devinit lp5521_init_led(struct lp5521_led *led, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 589 | struct i2c_client *client, |
| 590 | int chan, struct lp5521_platform_data *pdata) |
| 591 | { |
| 592 | struct device *dev = &client->dev; |
| 593 | char name[32]; |
| 594 | int res; |
| 595 | |
| 596 | if (chan >= LP5521_MAX_LEDS) |
| 597 | return -EINVAL; |
| 598 | |
| 599 | if (pdata->led_config[chan].led_current == 0) |
| 600 | return 0; |
| 601 | |
| 602 | led->led_current = pdata->led_config[chan].led_current; |
| 603 | led->max_current = pdata->led_config[chan].max_current; |
| 604 | led->chan_nr = pdata->led_config[chan].chan_nr; |
| 605 | |
| 606 | if (led->chan_nr >= LP5521_MAX_LEDS) { |
| 607 | dev_err(dev, "Use channel numbers between 0 and %d\n", |
| 608 | LP5521_MAX_LEDS - 1); |
| 609 | return -EINVAL; |
| 610 | } |
| 611 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 612 | led->cdev.brightness_set = lp5521_set_brightness; |
Kim, Milo | 5ae4e8a | 2012-03-23 15:02:08 -0700 | [diff] [blame] | 613 | if (pdata->led_config[chan].name) { |
| 614 | led->cdev.name = pdata->led_config[chan].name; |
| 615 | } else { |
| 616 | snprintf(name, sizeof(name), "%s:channel%d", |
| 617 | pdata->label ?: client->name, chan); |
| 618 | led->cdev.name = name; |
| 619 | } |
| 620 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 621 | res = led_classdev_register(dev, &led->cdev); |
| 622 | if (res < 0) { |
| 623 | dev_err(dev, "couldn't register led on channel %d\n", chan); |
| 624 | return res; |
| 625 | } |
| 626 | |
| 627 | res = sysfs_create_group(&led->cdev.dev->kobj, |
| 628 | &lp5521_led_attribute_group); |
| 629 | if (res < 0) { |
| 630 | dev_err(dev, "couldn't register current attribute\n"); |
| 631 | led_classdev_unregister(&led->cdev); |
| 632 | return res; |
| 633 | } |
| 634 | return 0; |
| 635 | } |
| 636 | |
Ralf Baechle | 5286bd9 | 2011-06-27 16:18:13 -0700 | [diff] [blame] | 637 | static int __devinit lp5521_probe(struct i2c_client *client, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 638 | const struct i2c_device_id *id) |
| 639 | { |
| 640 | struct lp5521_chip *chip; |
| 641 | struct lp5521_platform_data *pdata; |
| 642 | int ret, i, led; |
Srinidhi KASAGAR | b3c49c0 | 2011-10-31 17:12:24 -0700 | [diff] [blame] | 643 | u8 buf; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 644 | |
| 645 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
| 646 | if (!chip) |
| 647 | return -ENOMEM; |
| 648 | |
| 649 | i2c_set_clientdata(client, chip); |
| 650 | chip->client = client; |
| 651 | |
| 652 | pdata = client->dev.platform_data; |
| 653 | |
| 654 | if (!pdata) { |
| 655 | dev_err(&client->dev, "no platform data\n"); |
| 656 | ret = -EINVAL; |
| 657 | goto fail1; |
| 658 | } |
| 659 | |
| 660 | mutex_init(&chip->lock); |
| 661 | |
| 662 | chip->pdata = pdata; |
| 663 | |
| 664 | if (pdata->setup_resources) { |
| 665 | ret = pdata->setup_resources(); |
| 666 | if (ret < 0) |
| 667 | goto fail1; |
| 668 | } |
| 669 | |
| 670 | if (pdata->enable) { |
| 671 | pdata->enable(0); |
Samu Onkalo | 09c76b0 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 672 | usleep_range(1000, 2000); /* Keep enable down at least 1ms */ |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 673 | pdata->enable(1); |
Samu Onkalo | 09c76b0 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 674 | usleep_range(1000, 2000); /* 500us abs min. */ |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 675 | } |
| 676 | |
Samu Onkalo | 95ea8ee | 2010-11-24 12:57:05 -0800 | [diff] [blame] | 677 | lp5521_write(client, LP5521_REG_RESET, 0xff); |
| 678 | usleep_range(10000, 20000); /* |
| 679 | * Exact value is not available. 10 - 20ms |
| 680 | * appears to be enough for reset. |
| 681 | */ |
Srinidhi KASAGAR | b3c49c0 | 2011-10-31 17:12:24 -0700 | [diff] [blame] | 682 | |
| 683 | /* |
| 684 | * Make sure that the chip is reset by reading back the r channel |
| 685 | * current reg. This is dummy read is required on some platforms - |
| 686 | * otherwise further access to the R G B channels in the |
| 687 | * LP5521_REG_ENABLE register will not have any effect - strange! |
| 688 | */ |
| 689 | lp5521_read(client, LP5521_REG_R_CURRENT, &buf); |
| 690 | if (buf != LP5521_REG_R_CURR_DEFAULT) { |
Masanari Iida | 3a2fd4a | 2012-03-23 15:02:06 -0700 | [diff] [blame] | 691 | dev_err(&client->dev, "error in resetting chip\n"); |
Srinidhi KASAGAR | b3c49c0 | 2011-10-31 17:12:24 -0700 | [diff] [blame] | 692 | goto fail2; |
| 693 | } |
| 694 | usleep_range(10000, 20000); |
| 695 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 696 | ret = lp5521_detect(client); |
| 697 | |
| 698 | if (ret) { |
| 699 | dev_err(&client->dev, "Chip not found\n"); |
| 700 | goto fail2; |
| 701 | } |
| 702 | |
| 703 | dev_info(&client->dev, "%s programmable led chip found\n", id->name); |
| 704 | |
Samu Onkalo | d4e7ad0 | 2011-01-12 16:59:19 -0800 | [diff] [blame] | 705 | ret = lp5521_configure(client); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 706 | if (ret < 0) { |
| 707 | dev_err(&client->dev, "error configuring chip\n"); |
| 708 | goto fail2; |
| 709 | } |
| 710 | |
| 711 | /* Initialize leds */ |
| 712 | chip->num_channels = pdata->num_channels; |
| 713 | chip->num_leds = 0; |
| 714 | led = 0; |
| 715 | for (i = 0; i < pdata->num_channels; i++) { |
| 716 | /* Do not initialize channels that are not connected */ |
| 717 | if (pdata->led_config[i].led_current == 0) |
| 718 | continue; |
| 719 | |
| 720 | ret = lp5521_init_led(&chip->leds[led], client, i, pdata); |
| 721 | if (ret) { |
| 722 | dev_err(&client->dev, "error initializing leds\n"); |
| 723 | goto fail3; |
| 724 | } |
| 725 | chip->num_leds++; |
| 726 | |
| 727 | chip->leds[led].id = led; |
| 728 | /* Set initial LED current */ |
| 729 | lp5521_set_led_current(chip, led, |
| 730 | chip->leds[led].led_current); |
| 731 | |
| 732 | INIT_WORK(&(chip->leds[led].brightness_work), |
| 733 | lp5521_led_brightness_work); |
| 734 | |
| 735 | led++; |
| 736 | } |
| 737 | |
| 738 | ret = lp5521_register_sysfs(client); |
| 739 | if (ret) { |
| 740 | dev_err(&client->dev, "registering sysfs failed\n"); |
| 741 | goto fail3; |
| 742 | } |
| 743 | return ret; |
| 744 | fail3: |
| 745 | for (i = 0; i < chip->num_leds; i++) { |
| 746 | led_classdev_unregister(&chip->leds[i].cdev); |
| 747 | cancel_work_sync(&chip->leds[i].brightness_work); |
| 748 | } |
| 749 | fail2: |
| 750 | if (pdata->enable) |
| 751 | pdata->enable(0); |
| 752 | if (pdata->release_resources) |
| 753 | pdata->release_resources(); |
| 754 | fail1: |
| 755 | kfree(chip); |
| 756 | return ret; |
| 757 | } |
| 758 | |
Linus Walleij | 19ab5cb | 2011-07-25 17:13:15 -0700 | [diff] [blame] | 759 | static int __devexit lp5521_remove(struct i2c_client *client) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 760 | { |
| 761 | struct lp5521_chip *chip = i2c_get_clientdata(client); |
| 762 | int i; |
| 763 | |
| 764 | lp5521_unregister_sysfs(client); |
| 765 | |
| 766 | for (i = 0; i < chip->num_leds; i++) { |
| 767 | led_classdev_unregister(&chip->leds[i].cdev); |
| 768 | cancel_work_sync(&chip->leds[i].brightness_work); |
| 769 | } |
| 770 | |
| 771 | if (chip->pdata->enable) |
| 772 | chip->pdata->enable(0); |
| 773 | if (chip->pdata->release_resources) |
| 774 | chip->pdata->release_resources(); |
| 775 | kfree(chip); |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | static const struct i2c_device_id lp5521_id[] = { |
| 780 | { "lp5521", 0 }, /* Three channel chip */ |
| 781 | { } |
| 782 | }; |
| 783 | MODULE_DEVICE_TABLE(i2c, lp5521_id); |
| 784 | |
| 785 | static struct i2c_driver lp5521_driver = { |
| 786 | .driver = { |
| 787 | .name = "lp5521", |
| 788 | }, |
| 789 | .probe = lp5521_probe, |
Linus Walleij | 19ab5cb | 2011-07-25 17:13:15 -0700 | [diff] [blame] | 790 | .remove = __devexit_p(lp5521_remove), |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 791 | .id_table = lp5521_id, |
| 792 | }; |
| 793 | |
Axel Lin | 09a0d18 | 2012-01-10 15:09:27 -0800 | [diff] [blame] | 794 | module_i2c_driver(lp5521_driver); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 795 | |
| 796 | MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo"); |
| 797 | MODULE_DESCRIPTION("LP5521 LED engine"); |
| 798 | MODULE_LICENSE("GPL v2"); |