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> |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 37 | #include <linux/platform_data/leds-lp55xx.h> |
| 38 | |
| 39 | #include "leds-lp55xx-common.h" |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 40 | |
| 41 | #define LP5521_PROGRAM_LENGTH 32 /* in bytes */ |
| 42 | |
| 43 | #define LP5521_MAX_LEDS 3 /* Maximum number of LEDs */ |
| 44 | #define LP5521_MAX_ENGINES 3 /* Maximum number of engines */ |
| 45 | |
| 46 | #define LP5521_ENG_MASK_BASE 0x30 /* 00110000 */ |
| 47 | #define LP5521_ENG_STATUS_MASK 0x07 /* 00000111 */ |
| 48 | |
| 49 | #define LP5521_CMD_LOAD 0x15 /* 00010101 */ |
| 50 | #define LP5521_CMD_RUN 0x2a /* 00101010 */ |
| 51 | #define LP5521_CMD_DIRECT 0x3f /* 00111111 */ |
| 52 | #define LP5521_CMD_DISABLED 0x00 /* 00000000 */ |
| 53 | |
| 54 | /* Registers */ |
| 55 | #define LP5521_REG_ENABLE 0x00 |
| 56 | #define LP5521_REG_OP_MODE 0x01 |
| 57 | #define LP5521_REG_R_PWM 0x02 |
| 58 | #define LP5521_REG_G_PWM 0x03 |
| 59 | #define LP5521_REG_B_PWM 0x04 |
| 60 | #define LP5521_REG_R_CURRENT 0x05 |
| 61 | #define LP5521_REG_G_CURRENT 0x06 |
| 62 | #define LP5521_REG_B_CURRENT 0x07 |
| 63 | #define LP5521_REG_CONFIG 0x08 |
| 64 | #define LP5521_REG_R_CHANNEL_PC 0x09 |
| 65 | #define LP5521_REG_G_CHANNEL_PC 0x0A |
| 66 | #define LP5521_REG_B_CHANNEL_PC 0x0B |
| 67 | #define LP5521_REG_STATUS 0x0C |
| 68 | #define LP5521_REG_RESET 0x0D |
| 69 | #define LP5521_REG_GPO 0x0E |
| 70 | #define LP5521_REG_R_PROG_MEM 0x10 |
| 71 | #define LP5521_REG_G_PROG_MEM 0x30 |
| 72 | #define LP5521_REG_B_PROG_MEM 0x50 |
| 73 | |
| 74 | #define LP5521_PROG_MEM_BASE LP5521_REG_R_PROG_MEM |
| 75 | #define LP5521_PROG_MEM_SIZE 0x20 |
| 76 | |
| 77 | /* Base register to set LED current */ |
| 78 | #define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT |
| 79 | |
| 80 | /* Base register to set the brightness */ |
| 81 | #define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM |
| 82 | |
| 83 | /* Bits in ENABLE register */ |
| 84 | #define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */ |
| 85 | #define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */ |
| 86 | #define LP5521_EXEC_RUN 0x2A |
Kim, Milo | 32a2f74 | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 87 | #define LP5521_ENABLE_DEFAULT \ |
| 88 | (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM) |
| 89 | #define LP5521_ENABLE_RUN_PROGRAM \ |
| 90 | (LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 91 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 92 | /* Status */ |
| 93 | #define LP5521_EXT_CLK_USED 0x08 |
| 94 | |
Srinidhi KASAGAR | b3c49c0 | 2011-10-31 17:12:24 -0700 | [diff] [blame] | 95 | /* default R channel current register value */ |
| 96 | #define LP5521_REG_R_CURR_DEFAULT 0xAF |
| 97 | |
Kim, Milo | 011af7b | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 98 | /* Pattern Mode */ |
| 99 | #define PATTERN_OFF 0 |
| 100 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 101 | struct lp5521_engine { |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 102 | int id; |
| 103 | u8 mode; |
| 104 | u8 prog_page; |
| 105 | u8 engine_mask; |
| 106 | }; |
| 107 | |
| 108 | struct lp5521_led { |
| 109 | int id; |
| 110 | u8 chan_nr; |
| 111 | u8 led_current; |
| 112 | u8 max_current; |
| 113 | struct led_classdev cdev; |
| 114 | struct work_struct brightness_work; |
| 115 | u8 brightness; |
| 116 | }; |
| 117 | |
| 118 | struct lp5521_chip { |
| 119 | struct lp5521_platform_data *pdata; |
| 120 | struct mutex lock; /* Serialize control */ |
| 121 | struct i2c_client *client; |
| 122 | struct lp5521_engine engines[LP5521_MAX_ENGINES]; |
| 123 | struct lp5521_led leds[LP5521_MAX_LEDS]; |
| 124 | u8 num_channels; |
| 125 | u8 num_leds; |
| 126 | }; |
| 127 | |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame^] | 128 | static inline void lp5521_wait_enable_done(void) |
| 129 | { |
| 130 | /* it takes more 488 us to update ENABLE register */ |
| 131 | usleep_range(500, 600); |
| 132 | } |
| 133 | |
Samu Onkalo | 9fdb18b | 2010-11-24 12:57:02 -0800 | [diff] [blame] | 134 | static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev) |
| 135 | { |
| 136 | return container_of(cdev, struct lp5521_led, cdev); |
| 137 | } |
| 138 | |
| 139 | static inline struct lp5521_chip *engine_to_lp5521(struct lp5521_engine *engine) |
| 140 | { |
| 141 | return container_of(engine, struct lp5521_chip, |
| 142 | engines[engine->id - 1]); |
| 143 | } |
| 144 | |
| 145 | static inline struct lp5521_chip *led_to_lp5521(struct lp5521_led *led) |
| 146 | { |
| 147 | return container_of(led, struct lp5521_chip, |
| 148 | leds[led->id]); |
| 149 | } |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 150 | |
| 151 | static void lp5521_led_brightness_work(struct work_struct *work); |
| 152 | |
| 153 | static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value) |
| 154 | { |
| 155 | return i2c_smbus_write_byte_data(client, reg, value); |
| 156 | } |
| 157 | |
| 158 | static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf) |
| 159 | { |
| 160 | s32 ret; |
| 161 | |
| 162 | ret = i2c_smbus_read_byte_data(client, reg); |
| 163 | if (ret < 0) |
Sachin Kamat | 313e8b5 | 2012-11-20 01:59:00 -0800 | [diff] [blame] | 164 | return ret; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 165 | |
| 166 | *buf = ret; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int lp5521_set_engine_mode(struct lp5521_engine *engine, u8 mode) |
| 171 | { |
| 172 | struct lp5521_chip *chip = engine_to_lp5521(engine); |
| 173 | struct i2c_client *client = chip->client; |
| 174 | int ret; |
| 175 | u8 engine_state; |
| 176 | |
| 177 | /* Only transition between RUN and DIRECT mode are handled here */ |
| 178 | if (mode == LP5521_CMD_LOAD) |
| 179 | return 0; |
| 180 | |
| 181 | if (mode == LP5521_CMD_DISABLED) |
| 182 | mode = LP5521_CMD_DIRECT; |
| 183 | |
| 184 | ret = lp5521_read(client, LP5521_REG_OP_MODE, &engine_state); |
Axel Lin | fa0ea0e | 2011-10-31 17:12:12 -0700 | [diff] [blame] | 185 | if (ret < 0) |
| 186 | return ret; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 187 | |
| 188 | /* set mode only for this engine */ |
| 189 | engine_state &= ~(engine->engine_mask); |
| 190 | mode &= engine->engine_mask; |
| 191 | engine_state |= mode; |
Axel Lin | fa0ea0e | 2011-10-31 17:12:12 -0700 | [diff] [blame] | 192 | return lp5521_write(client, LP5521_REG_OP_MODE, engine_state); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern) |
| 196 | { |
| 197 | struct lp5521_chip *chip = engine_to_lp5521(eng); |
| 198 | struct i2c_client *client = chip->client; |
| 199 | int ret; |
| 200 | int addr; |
| 201 | u8 mode; |
| 202 | |
| 203 | /* move current engine to direct mode and remember the state */ |
| 204 | ret = lp5521_set_engine_mode(eng, LP5521_CMD_DIRECT); |
Dan Carpenter | 5bc9ad7 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 205 | if (ret) |
| 206 | return ret; |
| 207 | |
Samu Onkalo | 09c76b0 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 208 | /* Mode change requires min 500 us delay. 1 - 2 ms with margin */ |
| 209 | usleep_range(1000, 2000); |
Dan Carpenter | 5bc9ad7 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 210 | ret = lp5521_read(client, LP5521_REG_OP_MODE, &mode); |
| 211 | if (ret) |
| 212 | return ret; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 213 | |
| 214 | /* For loading, all the engines to load mode */ |
| 215 | lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT); |
Samu Onkalo | 09c76b0 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 216 | /* Mode change requires min 500 us delay. 1 - 2 ms with margin */ |
| 217 | usleep_range(1000, 2000); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 218 | lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_LOAD); |
Samu Onkalo | 09c76b0 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 219 | /* Mode change requires min 500 us delay. 1 - 2 ms with margin */ |
| 220 | usleep_range(1000, 2000); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 221 | |
| 222 | addr = LP5521_PROG_MEM_BASE + eng->prog_page * LP5521_PROG_MEM_SIZE; |
| 223 | i2c_smbus_write_i2c_block_data(client, |
| 224 | addr, |
| 225 | LP5521_PROG_MEM_SIZE, |
| 226 | pattern); |
| 227 | |
Dan Carpenter | 5bc9ad7 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 228 | return lp5521_write(client, LP5521_REG_OP_MODE, mode); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static int lp5521_set_led_current(struct lp5521_chip *chip, int led, u8 curr) |
| 232 | { |
| 233 | return lp5521_write(chip->client, |
| 234 | LP5521_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr, |
| 235 | curr); |
| 236 | } |
| 237 | |
Samu Onkalo | d4e7ad0 | 2011-01-12 16:59:19 -0800 | [diff] [blame] | 238 | static int lp5521_configure(struct i2c_client *client) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 239 | { |
| 240 | struct lp5521_chip *chip = i2c_get_clientdata(client); |
| 241 | int ret; |
Kim, Milo | 3b49aac | 2012-03-23 15:02:08 -0700 | [diff] [blame] | 242 | u8 cfg; |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame^] | 243 | u8 val; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 244 | |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame^] | 245 | /* |
| 246 | * Make sure that the chip is reset by reading back the r channel |
| 247 | * current reg. This is dummy read is required on some platforms - |
| 248 | * otherwise further access to the R G B channels in the |
| 249 | * LP5521_REG_ENABLE register will not have any effect - strange! |
| 250 | */ |
| 251 | ret = lp5521_read(client, LP5521_REG_R_CURRENT, &val); |
| 252 | if (ret) { |
| 253 | dev_err(&client->dev, "error in resetting chip\n"); |
| 254 | return ret; |
| 255 | } |
| 256 | if (val != LP5521_REG_R_CURR_DEFAULT) { |
| 257 | dev_err(&client->dev, |
| 258 | "unexpected data in register (expected 0x%x got 0x%x)\n", |
| 259 | LP5521_REG_R_CURR_DEFAULT, val); |
| 260 | ret = -EINVAL; |
| 261 | return ret; |
| 262 | } |
| 263 | usleep_range(10000, 20000); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 264 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 265 | /* Set all PWMs to direct control mode */ |
Kim, Milo | 32a2f74 | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 266 | ret = lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 267 | |
Kim, Milo | 3b49aac | 2012-03-23 15:02:08 -0700 | [diff] [blame] | 268 | cfg = chip->pdata->update_config ? |
| 269 | : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT); |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame^] | 270 | ret = lp5521_write(client, LP5521_REG_CONFIG, cfg); |
| 271 | if (ret) |
| 272 | return ret; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 273 | |
| 274 | /* Initialize all channels PWM to zero -> leds off */ |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame^] | 275 | lp5521_write(client, LP5521_REG_R_PWM, 0); |
| 276 | lp5521_write(client, LP5521_REG_G_PWM, 0); |
| 277 | lp5521_write(client, LP5521_REG_B_PWM, 0); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 278 | |
| 279 | /* Set engines are set to run state when OP_MODE enables engines */ |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame^] | 280 | ret = lp5521_write(client, LP5521_REG_ENABLE, |
Kim, Milo | 32a2f74 | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 281 | LP5521_ENABLE_RUN_PROGRAM); |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame^] | 282 | if (ret) |
| 283 | return ret; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 284 | |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame^] | 285 | lp5521_wait_enable_done(); |
| 286 | |
| 287 | return 0; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf) |
| 291 | { |
| 292 | int ret; |
| 293 | u8 status; |
| 294 | |
| 295 | ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status); |
| 296 | if (ret < 0) |
| 297 | return ret; |
| 298 | |
| 299 | /* Check that ext clock is really in use if requested */ |
| 300 | if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT) |
| 301 | if ((status & LP5521_EXT_CLK_USED) == 0) |
| 302 | return -EIO; |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static void lp5521_set_brightness(struct led_classdev *cdev, |
| 307 | enum led_brightness brightness) |
| 308 | { |
| 309 | struct lp5521_led *led = cdev_to_led(cdev); |
| 310 | led->brightness = (u8)brightness; |
| 311 | schedule_work(&led->brightness_work); |
| 312 | } |
| 313 | |
| 314 | static void lp5521_led_brightness_work(struct work_struct *work) |
| 315 | { |
| 316 | struct lp5521_led *led = container_of(work, |
| 317 | struct lp5521_led, |
| 318 | brightness_work); |
| 319 | struct lp5521_chip *chip = led_to_lp5521(led); |
| 320 | struct i2c_client *client = chip->client; |
| 321 | |
| 322 | mutex_lock(&chip->lock); |
| 323 | lp5521_write(client, LP5521_REG_LED_PWM_BASE + led->chan_nr, |
| 324 | led->brightness); |
| 325 | mutex_unlock(&chip->lock); |
| 326 | } |
| 327 | |
| 328 | /* Detect the chip by setting its ENABLE register and reading it back. */ |
| 329 | static int lp5521_detect(struct i2c_client *client) |
| 330 | { |
| 331 | int ret; |
| 332 | u8 buf; |
| 333 | |
Kim, Milo | 32a2f74 | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 334 | ret = lp5521_write(client, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 335 | if (ret) |
| 336 | return ret; |
Samu Onkalo | 09c76b0 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 337 | /* enable takes 500us. 1 - 2 ms leaves some margin */ |
| 338 | usleep_range(1000, 2000); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 339 | ret = lp5521_read(client, LP5521_REG_ENABLE, &buf); |
| 340 | if (ret) |
| 341 | return ret; |
Kim, Milo | 32a2f74 | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 342 | if (buf != LP5521_ENABLE_DEFAULT) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 343 | return -ENODEV; |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | /* Set engine mode and create appropriate sysfs attributes, if required. */ |
| 349 | static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode) |
| 350 | { |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 351 | int ret = 0; |
| 352 | |
| 353 | /* if in that mode already do nothing, except for run */ |
| 354 | if (mode == engine->mode && mode != LP5521_CMD_RUN) |
| 355 | return 0; |
| 356 | |
| 357 | if (mode == LP5521_CMD_RUN) { |
| 358 | ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN); |
| 359 | } else if (mode == LP5521_CMD_LOAD) { |
| 360 | lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED); |
| 361 | lp5521_set_engine_mode(engine, LP5521_CMD_LOAD); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 362 | } else if (mode == LP5521_CMD_DISABLED) { |
| 363 | lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED); |
| 364 | } |
| 365 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 366 | engine->mode = mode; |
| 367 | |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | static int lp5521_do_store_load(struct lp5521_engine *engine, |
| 372 | const char *buf, size_t len) |
| 373 | { |
| 374 | struct lp5521_chip *chip = engine_to_lp5521(engine); |
| 375 | struct i2c_client *client = chip->client; |
| 376 | int ret, nrchars, offset = 0, i = 0; |
| 377 | char c[3]; |
| 378 | unsigned cmd; |
| 379 | u8 pattern[LP5521_PROGRAM_LENGTH] = {0}; |
| 380 | |
| 381 | while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) { |
| 382 | /* separate sscanfs because length is working only for %s */ |
| 383 | ret = sscanf(buf + offset, "%2s%n ", c, &nrchars); |
Vasiliy Kulikov | 2260209 | 2011-01-12 16:59:14 -0800 | [diff] [blame] | 384 | if (ret != 2) |
| 385 | goto fail; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 386 | ret = sscanf(c, "%2x", &cmd); |
| 387 | if (ret != 1) |
| 388 | goto fail; |
| 389 | pattern[i] = (u8)cmd; |
| 390 | |
| 391 | offset += nrchars; |
| 392 | i++; |
| 393 | } |
| 394 | |
| 395 | /* Each instruction is 16bit long. Check that length is even */ |
| 396 | if (i % 2) |
| 397 | goto fail; |
| 398 | |
| 399 | mutex_lock(&chip->lock); |
Samu Onkalo | d4e7ad0 | 2011-01-12 16:59:19 -0800 | [diff] [blame] | 400 | if (engine->mode == LP5521_CMD_LOAD) |
| 401 | ret = lp5521_load_program(engine, pattern); |
| 402 | else |
| 403 | ret = -EINVAL; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 404 | mutex_unlock(&chip->lock); |
| 405 | |
| 406 | if (ret) { |
| 407 | dev_err(&client->dev, "failed loading pattern\n"); |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | return len; |
| 412 | fail: |
| 413 | dev_err(&client->dev, "wrong pattern format\n"); |
| 414 | return -EINVAL; |
| 415 | } |
| 416 | |
| 417 | static ssize_t store_engine_load(struct device *dev, |
| 418 | struct device_attribute *attr, |
| 419 | const char *buf, size_t len, int nr) |
| 420 | { |
| 421 | struct i2c_client *client = to_i2c_client(dev); |
| 422 | struct lp5521_chip *chip = i2c_get_clientdata(client); |
| 423 | return lp5521_do_store_load(&chip->engines[nr - 1], buf, len); |
| 424 | } |
| 425 | |
| 426 | #define store_load(nr) \ |
| 427 | static ssize_t store_engine##nr##_load(struct device *dev, \ |
| 428 | struct device_attribute *attr, \ |
| 429 | const char *buf, size_t len) \ |
| 430 | { \ |
| 431 | return store_engine_load(dev, attr, buf, len, nr); \ |
| 432 | } |
| 433 | store_load(1) |
| 434 | store_load(2) |
| 435 | store_load(3) |
| 436 | |
| 437 | static ssize_t show_engine_mode(struct device *dev, |
| 438 | struct device_attribute *attr, |
| 439 | char *buf, int nr) |
| 440 | { |
| 441 | struct i2c_client *client = to_i2c_client(dev); |
| 442 | struct lp5521_chip *chip = i2c_get_clientdata(client); |
| 443 | switch (chip->engines[nr - 1].mode) { |
| 444 | case LP5521_CMD_RUN: |
| 445 | return sprintf(buf, "run\n"); |
| 446 | case LP5521_CMD_LOAD: |
| 447 | return sprintf(buf, "load\n"); |
| 448 | case LP5521_CMD_DISABLED: |
| 449 | return sprintf(buf, "disabled\n"); |
| 450 | default: |
| 451 | return sprintf(buf, "disabled\n"); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | #define show_mode(nr) \ |
| 456 | static ssize_t show_engine##nr##_mode(struct device *dev, \ |
| 457 | struct device_attribute *attr, \ |
| 458 | char *buf) \ |
| 459 | { \ |
| 460 | return show_engine_mode(dev, attr, buf, nr); \ |
| 461 | } |
| 462 | show_mode(1) |
| 463 | show_mode(2) |
| 464 | show_mode(3) |
| 465 | |
| 466 | static ssize_t store_engine_mode(struct device *dev, |
| 467 | struct device_attribute *attr, |
| 468 | const char *buf, size_t len, int nr) |
| 469 | { |
| 470 | struct i2c_client *client = to_i2c_client(dev); |
| 471 | struct lp5521_chip *chip = i2c_get_clientdata(client); |
| 472 | struct lp5521_engine *engine = &chip->engines[nr - 1]; |
| 473 | mutex_lock(&chip->lock); |
| 474 | |
| 475 | if (!strncmp(buf, "run", 3)) |
| 476 | lp5521_set_mode(engine, LP5521_CMD_RUN); |
| 477 | else if (!strncmp(buf, "load", 4)) |
| 478 | lp5521_set_mode(engine, LP5521_CMD_LOAD); |
| 479 | else if (!strncmp(buf, "disabled", 8)) |
| 480 | lp5521_set_mode(engine, LP5521_CMD_DISABLED); |
| 481 | |
| 482 | mutex_unlock(&chip->lock); |
| 483 | return len; |
| 484 | } |
| 485 | |
| 486 | #define store_mode(nr) \ |
| 487 | static ssize_t store_engine##nr##_mode(struct device *dev, \ |
| 488 | struct device_attribute *attr, \ |
| 489 | const char *buf, size_t len) \ |
| 490 | { \ |
| 491 | return store_engine_mode(dev, attr, buf, len, nr); \ |
| 492 | } |
| 493 | store_mode(1) |
| 494 | store_mode(2) |
| 495 | store_mode(3) |
| 496 | |
| 497 | static ssize_t show_max_current(struct device *dev, |
| 498 | struct device_attribute *attr, |
| 499 | char *buf) |
| 500 | { |
| 501 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 502 | struct lp5521_led *led = cdev_to_led(led_cdev); |
| 503 | |
| 504 | return sprintf(buf, "%d\n", led->max_current); |
| 505 | } |
| 506 | |
| 507 | static ssize_t show_current(struct device *dev, |
| 508 | struct device_attribute *attr, |
| 509 | char *buf) |
| 510 | { |
| 511 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 512 | struct lp5521_led *led = cdev_to_led(led_cdev); |
| 513 | |
| 514 | return sprintf(buf, "%d\n", led->led_current); |
| 515 | } |
| 516 | |
| 517 | static ssize_t store_current(struct device *dev, |
| 518 | struct device_attribute *attr, |
| 519 | const char *buf, size_t len) |
| 520 | { |
| 521 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 522 | struct lp5521_led *led = cdev_to_led(led_cdev); |
| 523 | struct lp5521_chip *chip = led_to_lp5521(led); |
| 524 | ssize_t ret; |
| 525 | unsigned long curr; |
| 526 | |
Kim, Milo | 011af7b | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 527 | if (kstrtoul(buf, 0, &curr)) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 528 | return -EINVAL; |
| 529 | |
| 530 | if (curr > led->max_current) |
| 531 | return -EINVAL; |
| 532 | |
| 533 | mutex_lock(&chip->lock); |
| 534 | ret = lp5521_set_led_current(chip, led->id, curr); |
| 535 | mutex_unlock(&chip->lock); |
| 536 | |
| 537 | if (ret < 0) |
| 538 | return ret; |
| 539 | |
| 540 | led->led_current = (u8)curr; |
| 541 | |
| 542 | return len; |
| 543 | } |
| 544 | |
| 545 | static ssize_t lp5521_selftest(struct device *dev, |
| 546 | struct device_attribute *attr, |
| 547 | char *buf) |
| 548 | { |
| 549 | struct i2c_client *client = to_i2c_client(dev); |
| 550 | struct lp5521_chip *chip = i2c_get_clientdata(client); |
| 551 | int ret; |
| 552 | |
| 553 | mutex_lock(&chip->lock); |
| 554 | ret = lp5521_run_selftest(chip, buf); |
| 555 | mutex_unlock(&chip->lock); |
| 556 | return sprintf(buf, "%s\n", ret ? "FAIL" : "OK"); |
| 557 | } |
| 558 | |
Kim, Milo | 011af7b | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 559 | static void lp5521_clear_program_memory(struct i2c_client *cl) |
| 560 | { |
| 561 | int i; |
| 562 | u8 rgb_mem[] = { |
| 563 | LP5521_REG_R_PROG_MEM, |
| 564 | LP5521_REG_G_PROG_MEM, |
| 565 | LP5521_REG_B_PROG_MEM, |
| 566 | }; |
| 567 | |
| 568 | for (i = 0; i < ARRAY_SIZE(rgb_mem); i++) { |
| 569 | lp5521_write(cl, rgb_mem[i], 0); |
| 570 | lp5521_write(cl, rgb_mem[i] + 1, 0); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | static void lp5521_write_program_memory(struct i2c_client *cl, |
| 575 | u8 base, u8 *rgb, int size) |
| 576 | { |
| 577 | int i; |
| 578 | |
| 579 | if (!rgb || size <= 0) |
| 580 | return; |
| 581 | |
| 582 | for (i = 0; i < size; i++) |
| 583 | lp5521_write(cl, base + i, *(rgb + i)); |
| 584 | |
| 585 | lp5521_write(cl, base + i, 0); |
| 586 | lp5521_write(cl, base + i + 1, 0); |
| 587 | } |
| 588 | |
| 589 | static inline struct lp5521_led_pattern *lp5521_get_pattern |
| 590 | (struct lp5521_chip *chip, u8 offset) |
| 591 | { |
| 592 | struct lp5521_led_pattern *ptn; |
| 593 | ptn = chip->pdata->patterns + (offset - 1); |
| 594 | return ptn; |
| 595 | } |
| 596 | |
| 597 | static void lp5521_run_led_pattern(int mode, struct lp5521_chip *chip) |
| 598 | { |
| 599 | struct lp5521_led_pattern *ptn; |
| 600 | struct i2c_client *cl = chip->client; |
| 601 | int num_patterns = chip->pdata->num_patterns; |
| 602 | |
| 603 | if (mode > num_patterns || !(chip->pdata->patterns)) |
| 604 | return; |
| 605 | |
| 606 | if (mode == PATTERN_OFF) { |
Kim, Milo | 32a2f74 | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 607 | lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT); |
Kim, Milo | 011af7b | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 608 | usleep_range(1000, 2000); |
| 609 | lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT); |
| 610 | } else { |
| 611 | ptn = lp5521_get_pattern(chip, mode); |
| 612 | if (!ptn) |
| 613 | return; |
| 614 | |
| 615 | lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_LOAD); |
| 616 | usleep_range(1000, 2000); |
| 617 | |
| 618 | lp5521_clear_program_memory(cl); |
| 619 | |
| 620 | lp5521_write_program_memory(cl, LP5521_REG_R_PROG_MEM, |
| 621 | ptn->r, ptn->size_r); |
| 622 | lp5521_write_program_memory(cl, LP5521_REG_G_PROG_MEM, |
| 623 | ptn->g, ptn->size_g); |
| 624 | lp5521_write_program_memory(cl, LP5521_REG_B_PROG_MEM, |
| 625 | ptn->b, ptn->size_b); |
| 626 | |
| 627 | lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_RUN); |
| 628 | usleep_range(1000, 2000); |
Kim, Milo | 32a2f74 | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 629 | lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM); |
Kim, Milo | 011af7b | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
| 633 | static ssize_t store_led_pattern(struct device *dev, |
| 634 | struct device_attribute *attr, |
| 635 | const char *buf, size_t len) |
| 636 | { |
| 637 | struct lp5521_chip *chip = i2c_get_clientdata(to_i2c_client(dev)); |
| 638 | unsigned long val; |
| 639 | int ret; |
| 640 | |
Jingoo Han | 69b44c1 | 2012-11-18 20:51:20 -0800 | [diff] [blame] | 641 | ret = kstrtoul(buf, 16, &val); |
Kim, Milo | 011af7b | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 642 | if (ret) |
| 643 | return ret; |
| 644 | |
| 645 | lp5521_run_led_pattern(val, chip); |
| 646 | |
| 647 | return len; |
| 648 | } |
| 649 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 650 | /* led class device attributes */ |
Vasiliy Kulikov | 67d1da7 | 2011-03-22 16:30:19 -0700 | [diff] [blame] | 651 | 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] | 652 | static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL); |
| 653 | |
| 654 | static struct attribute *lp5521_led_attributes[] = { |
| 655 | &dev_attr_led_current.attr, |
| 656 | &dev_attr_max_current.attr, |
| 657 | NULL, |
| 658 | }; |
| 659 | |
| 660 | static struct attribute_group lp5521_led_attribute_group = { |
| 661 | .attrs = lp5521_led_attributes |
| 662 | }; |
| 663 | |
| 664 | /* device attributes */ |
Vasiliy Kulikov | 67d1da7 | 2011-03-22 16:30:19 -0700 | [diff] [blame] | 665 | static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 666 | show_engine1_mode, store_engine1_mode); |
Vasiliy Kulikov | 67d1da7 | 2011-03-22 16:30:19 -0700 | [diff] [blame] | 667 | static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 668 | show_engine2_mode, store_engine2_mode); |
Vasiliy Kulikov | 67d1da7 | 2011-03-22 16:30:19 -0700 | [diff] [blame] | 669 | static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 670 | show_engine3_mode, store_engine3_mode); |
Vasiliy Kulikov | 67d1da7 | 2011-03-22 16:30:19 -0700 | [diff] [blame] | 671 | static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load); |
| 672 | static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load); |
| 673 | static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 674 | static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL); |
Kim, Milo | 011af7b | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 675 | static DEVICE_ATTR(led_pattern, S_IWUSR, NULL, store_led_pattern); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 676 | |
| 677 | static struct attribute *lp5521_attributes[] = { |
| 678 | &dev_attr_engine1_mode.attr, |
| 679 | &dev_attr_engine2_mode.attr, |
| 680 | &dev_attr_engine3_mode.attr, |
| 681 | &dev_attr_selftest.attr, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 682 | &dev_attr_engine1_load.attr, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 683 | &dev_attr_engine2_load.attr, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 684 | &dev_attr_engine3_load.attr, |
Kim, Milo | 011af7b | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 685 | &dev_attr_led_pattern.attr, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 686 | NULL |
| 687 | }; |
| 688 | |
| 689 | static const struct attribute_group lp5521_group = { |
| 690 | .attrs = lp5521_attributes, |
| 691 | }; |
| 692 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 693 | static int lp5521_register_sysfs(struct i2c_client *client) |
| 694 | { |
| 695 | struct device *dev = &client->dev; |
| 696 | return sysfs_create_group(&dev->kobj, &lp5521_group); |
| 697 | } |
| 698 | |
| 699 | static void lp5521_unregister_sysfs(struct i2c_client *client) |
| 700 | { |
| 701 | struct lp5521_chip *chip = i2c_get_clientdata(client); |
| 702 | struct device *dev = &client->dev; |
| 703 | int i; |
| 704 | |
| 705 | sysfs_remove_group(&dev->kobj, &lp5521_group); |
| 706 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 707 | for (i = 0; i < chip->num_leds; i++) |
| 708 | sysfs_remove_group(&chip->leds[i].cdev.dev->kobj, |
| 709 | &lp5521_led_attribute_group); |
| 710 | } |
| 711 | |
Milo(Woogyom) Kim | 86eb774 | 2013-02-05 17:57:02 +0900 | [diff] [blame] | 712 | static void lp5521_reset_device(struct lp5521_chip *chip) |
| 713 | { |
| 714 | struct i2c_client *client = chip->client; |
| 715 | |
| 716 | lp5521_write(client, LP5521_REG_RESET, 0xff); |
| 717 | } |
| 718 | |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 719 | static void lp5521_deinit_device(struct lp5521_chip *chip); |
Milo(Woogyom) Kim | 944f7b1 | 2013-02-05 17:49:46 +0900 | [diff] [blame] | 720 | static int lp5521_init_device(struct lp5521_chip *chip) |
| 721 | { |
| 722 | struct lp5521_platform_data *pdata = chip->pdata; |
| 723 | struct i2c_client *client = chip->client; |
| 724 | int ret; |
Milo(Woogyom) Kim | 944f7b1 | 2013-02-05 17:49:46 +0900 | [diff] [blame] | 725 | |
| 726 | if (pdata->setup_resources) { |
| 727 | ret = pdata->setup_resources(); |
| 728 | if (ret < 0) |
| 729 | return ret; |
| 730 | } |
| 731 | |
| 732 | if (pdata->enable) { |
| 733 | pdata->enable(0); |
| 734 | usleep_range(1000, 2000); /* Keep enable down at least 1ms */ |
| 735 | pdata->enable(1); |
| 736 | usleep_range(1000, 2000); /* 500us abs min. */ |
| 737 | } |
| 738 | |
Milo(Woogyom) Kim | 86eb774 | 2013-02-05 17:57:02 +0900 | [diff] [blame] | 739 | lp5521_reset_device(chip); |
| 740 | |
Milo(Woogyom) Kim | 944f7b1 | 2013-02-05 17:49:46 +0900 | [diff] [blame] | 741 | usleep_range(10000, 20000); /* |
| 742 | * Exact value is not available. 10 - 20ms |
| 743 | * appears to be enough for reset. |
| 744 | */ |
| 745 | |
Milo(Woogyom) Kim | 944f7b1 | 2013-02-05 17:49:46 +0900 | [diff] [blame] | 746 | ret = lp5521_detect(client); |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 747 | if (ret) { |
Milo(Woogyom) Kim | 944f7b1 | 2013-02-05 17:49:46 +0900 | [diff] [blame] | 748 | dev_err(&client->dev, "Chip not found\n"); |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 749 | goto err; |
| 750 | } |
Milo(Woogyom) Kim | 944f7b1 | 2013-02-05 17:49:46 +0900 | [diff] [blame] | 751 | |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 752 | ret = lp5521_configure(client); |
| 753 | if (ret < 0) { |
| 754 | dev_err(&client->dev, "error configuring chip\n"); |
| 755 | goto err_config; |
| 756 | } |
| 757 | |
| 758 | return 0; |
| 759 | |
| 760 | err_config: |
| 761 | lp5521_deinit_device(chip); |
| 762 | err: |
Milo(Woogyom) Kim | 944f7b1 | 2013-02-05 17:49:46 +0900 | [diff] [blame] | 763 | return ret; |
| 764 | } |
| 765 | |
Milo(Woogyom) Kim | 1a99148 | 2013-02-05 17:50:36 +0900 | [diff] [blame] | 766 | static void lp5521_deinit_device(struct lp5521_chip *chip) |
| 767 | { |
| 768 | struct lp5521_platform_data *pdata = chip->pdata; |
| 769 | |
| 770 | if (pdata->enable) |
| 771 | pdata->enable(0); |
| 772 | if (pdata->release_resources) |
| 773 | pdata->release_resources(); |
| 774 | } |
| 775 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 776 | static int lp5521_init_led(struct lp5521_led *led, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 777 | struct i2c_client *client, |
| 778 | int chan, struct lp5521_platform_data *pdata) |
| 779 | { |
| 780 | struct device *dev = &client->dev; |
| 781 | char name[32]; |
| 782 | int res; |
| 783 | |
| 784 | if (chan >= LP5521_MAX_LEDS) |
| 785 | return -EINVAL; |
| 786 | |
| 787 | if (pdata->led_config[chan].led_current == 0) |
| 788 | return 0; |
| 789 | |
| 790 | led->led_current = pdata->led_config[chan].led_current; |
| 791 | led->max_current = pdata->led_config[chan].max_current; |
| 792 | led->chan_nr = pdata->led_config[chan].chan_nr; |
| 793 | |
| 794 | if (led->chan_nr >= LP5521_MAX_LEDS) { |
| 795 | dev_err(dev, "Use channel numbers between 0 and %d\n", |
| 796 | LP5521_MAX_LEDS - 1); |
| 797 | return -EINVAL; |
| 798 | } |
| 799 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 800 | led->cdev.brightness_set = lp5521_set_brightness; |
Kim, Milo | 5ae4e8a | 2012-03-23 15:02:08 -0700 | [diff] [blame] | 801 | if (pdata->led_config[chan].name) { |
| 802 | led->cdev.name = pdata->led_config[chan].name; |
| 803 | } else { |
| 804 | snprintf(name, sizeof(name), "%s:channel%d", |
| 805 | pdata->label ?: client->name, chan); |
| 806 | led->cdev.name = name; |
| 807 | } |
| 808 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 809 | res = led_classdev_register(dev, &led->cdev); |
| 810 | if (res < 0) { |
| 811 | dev_err(dev, "couldn't register led on channel %d\n", chan); |
| 812 | return res; |
| 813 | } |
| 814 | |
| 815 | res = sysfs_create_group(&led->cdev.dev->kobj, |
| 816 | &lp5521_led_attribute_group); |
| 817 | if (res < 0) { |
| 818 | dev_err(dev, "couldn't register current attribute\n"); |
| 819 | led_classdev_unregister(&led->cdev); |
| 820 | return res; |
| 821 | } |
| 822 | return 0; |
| 823 | } |
| 824 | |
Milo(Woogyom) Kim | f652480 | 2013-02-05 17:53:40 +0900 | [diff] [blame] | 825 | static int lp5521_register_leds(struct lp5521_chip *chip) |
| 826 | { |
| 827 | struct lp5521_platform_data *pdata = chip->pdata; |
| 828 | struct i2c_client *client = chip->client; |
| 829 | int i; |
| 830 | int led; |
| 831 | int ret; |
| 832 | |
| 833 | /* Initialize leds */ |
| 834 | chip->num_channels = pdata->num_channels; |
| 835 | chip->num_leds = 0; |
| 836 | led = 0; |
| 837 | for (i = 0; i < pdata->num_channels; i++) { |
| 838 | /* Do not initialize channels that are not connected */ |
| 839 | if (pdata->led_config[i].led_current == 0) |
| 840 | continue; |
| 841 | |
| 842 | ret = lp5521_init_led(&chip->leds[led], client, i, pdata); |
| 843 | if (ret) { |
| 844 | dev_err(&client->dev, "error initializing leds\n"); |
| 845 | return ret; |
| 846 | } |
| 847 | chip->num_leds++; |
| 848 | |
| 849 | chip->leds[led].id = led; |
| 850 | /* Set initial LED current */ |
| 851 | lp5521_set_led_current(chip, led, |
| 852 | chip->leds[led].led_current); |
| 853 | |
| 854 | INIT_WORK(&(chip->leds[led].brightness_work), |
| 855 | lp5521_led_brightness_work); |
| 856 | |
| 857 | led++; |
| 858 | } |
| 859 | |
| 860 | return 0; |
| 861 | } |
| 862 | |
Milo(Woogyom) Kim | 1904f83 | 2013-02-05 17:56:23 +0900 | [diff] [blame] | 863 | static void lp5521_unregister_leds(struct lp5521_chip *chip) |
| 864 | { |
| 865 | int i; |
| 866 | |
| 867 | for (i = 0; i < chip->num_leds; i++) { |
| 868 | led_classdev_unregister(&chip->leds[i].cdev); |
| 869 | cancel_work_sync(&chip->leds[i].brightness_work); |
| 870 | } |
| 871 | } |
| 872 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 873 | static int lp5521_probe(struct i2c_client *client, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 874 | const struct i2c_device_id *id) |
| 875 | { |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 876 | struct lp5521_chip *old_chip = NULL; |
Milo(Woogyom) Kim | 1904f83 | 2013-02-05 17:56:23 +0900 | [diff] [blame] | 877 | int ret; |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 878 | struct lp55xx_chip *chip; |
| 879 | struct lp55xx_led *led; |
| 880 | struct lp55xx_platform_data *pdata = client->dev.platform_data; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 881 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 882 | if (!pdata) { |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 883 | dev_err(&client->dev, "no platform data\n"); |
Bryan Wu | e430dc0 | 2012-07-04 11:16:09 +0800 | [diff] [blame] | 884 | return -EINVAL; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 885 | } |
| 886 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 887 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
| 888 | if (!chip) |
| 889 | return -ENOMEM; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 890 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 891 | led = devm_kzalloc(&client->dev, |
| 892 | sizeof(*led) * pdata->num_channels, GFP_KERNEL); |
| 893 | if (!led) |
| 894 | return -ENOMEM; |
| 895 | |
| 896 | chip->cl = client; |
| 897 | chip->pdata = pdata; |
| 898 | |
| 899 | mutex_init(&chip->lock); |
| 900 | |
| 901 | i2c_set_clientdata(client, led); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 902 | |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 903 | ret = lp5521_init_device(old_chip); |
Milo(Woogyom) Kim | 944f7b1 | 2013-02-05 17:49:46 +0900 | [diff] [blame] | 904 | if (ret) |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 905 | goto err_init; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 906 | |
| 907 | dev_info(&client->dev, "%s programmable led chip found\n", id->name); |
| 908 | |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 909 | ret = lp5521_register_leds(old_chip); |
Milo(Woogyom) Kim | f652480 | 2013-02-05 17:53:40 +0900 | [diff] [blame] | 910 | if (ret) |
| 911 | goto fail2; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 912 | |
| 913 | ret = lp5521_register_sysfs(client); |
| 914 | if (ret) { |
| 915 | dev_err(&client->dev, "registering sysfs failed\n"); |
Bryan Wu | e430dc0 | 2012-07-04 11:16:09 +0800 | [diff] [blame] | 916 | goto fail2; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 917 | } |
| 918 | return ret; |
Bryan Wu | e430dc0 | 2012-07-04 11:16:09 +0800 | [diff] [blame] | 919 | fail2: |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 920 | lp5521_unregister_leds(old_chip); |
| 921 | lp5521_deinit_device(old_chip); |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 922 | err_init: |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 923 | return ret; |
| 924 | } |
| 925 | |
Bill Pemberton | 678e8a6 | 2012-11-19 13:26:00 -0500 | [diff] [blame] | 926 | static int lp5521_remove(struct i2c_client *client) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 927 | { |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 928 | struct lp5521_chip *old_chip = i2c_get_clientdata(client); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 929 | |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 930 | lp5521_run_led_pattern(PATTERN_OFF, old_chip); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 931 | lp5521_unregister_sysfs(client); |
| 932 | |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 933 | lp5521_unregister_leds(old_chip); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 934 | |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 935 | lp5521_deinit_device(old_chip); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | static const struct i2c_device_id lp5521_id[] = { |
| 940 | { "lp5521", 0 }, /* Three channel chip */ |
| 941 | { } |
| 942 | }; |
| 943 | MODULE_DEVICE_TABLE(i2c, lp5521_id); |
| 944 | |
| 945 | static struct i2c_driver lp5521_driver = { |
| 946 | .driver = { |
| 947 | .name = "lp5521", |
| 948 | }, |
| 949 | .probe = lp5521_probe, |
Bill Pemberton | df07cf8 | 2012-11-19 13:20:20 -0500 | [diff] [blame] | 950 | .remove = lp5521_remove, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 951 | .id_table = lp5521_id, |
| 952 | }; |
| 953 | |
Axel Lin | 09a0d18 | 2012-01-10 15:09:27 -0800 | [diff] [blame] | 954 | module_i2c_driver(lp5521_driver); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 955 | |
| 956 | MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo"); |
| 957 | MODULE_DESCRIPTION("LP5521 LED engine"); |
| 958 | MODULE_LICENSE("GPL v2"); |