Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * lp5523.c - LP5523 LED 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-lp5523.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 | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 40 | |
| 41 | #define LP5523_REG_ENABLE 0x00 |
| 42 | #define LP5523_REG_OP_MODE 0x01 |
| 43 | #define LP5523_REG_RATIOMETRIC_MSB 0x02 |
| 44 | #define LP5523_REG_RATIOMETRIC_LSB 0x03 |
| 45 | #define LP5523_REG_ENABLE_LEDS_MSB 0x04 |
| 46 | #define LP5523_REG_ENABLE_LEDS_LSB 0x05 |
| 47 | #define LP5523_REG_LED_CNTRL_BASE 0x06 |
| 48 | #define LP5523_REG_LED_PWM_BASE 0x16 |
| 49 | #define LP5523_REG_LED_CURRENT_BASE 0x26 |
| 50 | #define LP5523_REG_CONFIG 0x36 |
| 51 | #define LP5523_REG_CHANNEL1_PC 0x37 |
| 52 | #define LP5523_REG_CHANNEL2_PC 0x38 |
| 53 | #define LP5523_REG_CHANNEL3_PC 0x39 |
| 54 | #define LP5523_REG_STATUS 0x3a |
| 55 | #define LP5523_REG_GPO 0x3b |
| 56 | #define LP5523_REG_VARIABLE 0x3c |
| 57 | #define LP5523_REG_RESET 0x3d |
| 58 | #define LP5523_REG_TEMP_CTRL 0x3e |
| 59 | #define LP5523_REG_TEMP_READ 0x3f |
| 60 | #define LP5523_REG_TEMP_WRITE 0x40 |
| 61 | #define LP5523_REG_LED_TEST_CTRL 0x41 |
| 62 | #define LP5523_REG_LED_TEST_ADC 0x42 |
| 63 | #define LP5523_REG_ENG1_VARIABLE 0x45 |
| 64 | #define LP5523_REG_ENG2_VARIABLE 0x46 |
| 65 | #define LP5523_REG_ENG3_VARIABLE 0x47 |
| 66 | #define LP5523_REG_MASTER_FADER1 0x48 |
| 67 | #define LP5523_REG_MASTER_FADER2 0x49 |
| 68 | #define LP5523_REG_MASTER_FADER3 0x4a |
| 69 | #define LP5523_REG_CH1_PROG_START 0x4c |
| 70 | #define LP5523_REG_CH2_PROG_START 0x4d |
| 71 | #define LP5523_REG_CH3_PROG_START 0x4e |
| 72 | #define LP5523_REG_PROG_PAGE_SEL 0x4f |
| 73 | #define LP5523_REG_PROG_MEM 0x50 |
| 74 | |
| 75 | #define LP5523_CMD_LOAD 0x15 /* 00010101 */ |
| 76 | #define LP5523_CMD_RUN 0x2a /* 00101010 */ |
| 77 | #define LP5523_CMD_DISABLED 0x00 /* 00000000 */ |
| 78 | |
| 79 | #define LP5523_ENABLE 0x40 |
| 80 | #define LP5523_AUTO_INC 0x40 |
| 81 | #define LP5523_PWR_SAVE 0x20 |
| 82 | #define LP5523_PWM_PWR_SAVE 0x04 |
| 83 | #define LP5523_CP_1 0x08 |
| 84 | #define LP5523_CP_1_5 0x10 |
| 85 | #define LP5523_CP_AUTO 0x18 |
| 86 | #define LP5523_INT_CLK 0x01 |
| 87 | #define LP5523_AUTO_CLK 0x02 |
| 88 | #define LP5523_EN_LEDTEST 0x80 |
| 89 | #define LP5523_LEDTEST_DONE 0x80 |
| 90 | |
| 91 | #define LP5523_DEFAULT_CURRENT 50 /* microAmps */ |
| 92 | #define LP5523_PROGRAM_LENGTH 32 /* in bytes */ |
| 93 | #define LP5523_PROGRAM_PAGES 6 |
| 94 | #define LP5523_ADC_SHORTCIRC_LIM 80 |
| 95 | |
| 96 | #define LP5523_LEDS 9 |
| 97 | #define LP5523_ENGINES 3 |
| 98 | |
| 99 | #define LP5523_ENG_MASK_BASE 0x30 /* 00110000 */ |
| 100 | |
| 101 | #define LP5523_ENG_STATUS_MASK 0x07 /* 00000111 */ |
| 102 | |
| 103 | #define LP5523_IRQ_FLAGS IRQF_TRIGGER_FALLING |
| 104 | |
| 105 | #define LP5523_EXT_CLK_USED 0x08 |
| 106 | |
| 107 | #define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led))) |
| 108 | #define SHIFT_MASK(id) (((id) - 1) * 2) |
| 109 | |
Kim, Milo | 27d7704 | 2012-09-04 15:06:18 +0800 | [diff] [blame] | 110 | enum lp5523_chip_id { |
| 111 | LP5523, |
| 112 | LP55231, |
| 113 | }; |
| 114 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 115 | struct lp5523_engine { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 116 | int id; |
| 117 | u8 mode; |
| 118 | u8 prog_page; |
| 119 | u8 mux_page; |
| 120 | u16 led_mux; |
| 121 | u8 engine_mask; |
| 122 | }; |
| 123 | |
| 124 | struct lp5523_led { |
| 125 | int id; |
| 126 | u8 chan_nr; |
| 127 | u8 led_current; |
| 128 | u8 max_current; |
| 129 | struct led_classdev cdev; |
| 130 | struct work_struct brightness_work; |
| 131 | u8 brightness; |
| 132 | }; |
| 133 | |
| 134 | struct lp5523_chip { |
| 135 | struct mutex lock; /* Serialize control */ |
| 136 | struct i2c_client *client; |
| 137 | struct lp5523_engine engines[LP5523_ENGINES]; |
| 138 | struct lp5523_led leds[LP5523_LEDS]; |
| 139 | struct lp5523_platform_data *pdata; |
| 140 | u8 num_channels; |
| 141 | u8 num_leds; |
| 142 | }; |
| 143 | |
Samu Onkalo | 87dbf62 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 144 | static inline struct lp5523_led *cdev_to_led(struct led_classdev *cdev) |
| 145 | { |
| 146 | return container_of(cdev, struct lp5523_led, cdev); |
| 147 | } |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 148 | |
Samu Onkalo | 87dbf62 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 149 | static inline struct lp5523_chip *engine_to_lp5523(struct lp5523_engine *engine) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 150 | { |
| 151 | return container_of(engine, struct lp5523_chip, |
| 152 | engines[engine->id - 1]); |
| 153 | } |
| 154 | |
Samu Onkalo | 87dbf62 | 2010-11-24 12:57:03 -0800 | [diff] [blame] | 155 | static inline struct lp5523_chip *led_to_lp5523(struct lp5523_led *led) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 156 | { |
| 157 | return container_of(led, struct lp5523_chip, |
| 158 | leds[led->id]); |
| 159 | } |
| 160 | |
Kim, Milo | 6f6365f | 2012-08-21 17:03:58 +0800 | [diff] [blame] | 161 | static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 162 | static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode); |
Andrew Morton | d06cb46 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 163 | static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 164 | |
| 165 | static void lp5523_led_brightness_work(struct work_struct *work); |
| 166 | |
| 167 | static int lp5523_write(struct i2c_client *client, u8 reg, u8 value) |
| 168 | { |
| 169 | return i2c_smbus_write_byte_data(client, reg, value); |
| 170 | } |
| 171 | |
| 172 | static int lp5523_read(struct i2c_client *client, u8 reg, u8 *buf) |
| 173 | { |
| 174 | s32 ret = i2c_smbus_read_byte_data(client, reg); |
| 175 | |
| 176 | if (ret < 0) |
Sachin Kamat | 5ebab74 | 2012-11-20 01:59:01 -0800 | [diff] [blame] | 177 | return ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 178 | |
| 179 | *buf = ret; |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int lp5523_detect(struct i2c_client *client) |
| 184 | { |
| 185 | int ret; |
| 186 | u8 buf; |
| 187 | |
Kim, Milo | 469eba0 | 2012-08-21 17:04:02 +0800 | [diff] [blame] | 188 | ret = lp5523_write(client, LP5523_REG_ENABLE, LP5523_ENABLE); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 189 | if (ret) |
| 190 | return ret; |
| 191 | ret = lp5523_read(client, LP5523_REG_ENABLE, &buf); |
| 192 | if (ret) |
| 193 | return ret; |
| 194 | if (buf == 0x40) |
| 195 | return 0; |
| 196 | else |
| 197 | return -ENODEV; |
| 198 | } |
| 199 | |
| 200 | static int lp5523_configure(struct i2c_client *client) |
| 201 | { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 202 | int ret = 0; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 203 | |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame^] | 204 | ret = lp5523_write(client, LP5523_REG_ENABLE, LP5523_ENABLE); |
| 205 | if (ret) |
| 206 | return ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 207 | |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 208 | /* Chip startup time is 500 us, 1 - 2 ms gives some margin */ |
| 209 | usleep_range(1000, 2000); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 210 | |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame^] | 211 | ret = lp5523_write(client, LP5523_REG_CONFIG, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 212 | LP5523_AUTO_INC | LP5523_PWR_SAVE | |
| 213 | LP5523_CP_AUTO | LP5523_AUTO_CLK | |
| 214 | LP5523_PWM_PWR_SAVE); |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame^] | 215 | if (ret) |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 216 | return ret; |
| 217 | |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame^] | 218 | /* turn on all leds */ |
| 219 | ret = lp5523_write(client, LP5523_REG_ENABLE_LEDS_MSB, 0x01); |
| 220 | if (ret) |
| 221 | return ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 222 | |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame^] | 223 | return lp5523_write(client, LP5523_REG_ENABLE_LEDS_LSB, 0xff); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode) |
| 227 | { |
| 228 | struct lp5523_chip *chip = engine_to_lp5523(engine); |
| 229 | struct i2c_client *client = chip->client; |
| 230 | int ret; |
| 231 | u8 engine_state; |
| 232 | |
| 233 | ret = lp5523_read(client, LP5523_REG_OP_MODE, &engine_state); |
| 234 | if (ret) |
| 235 | goto fail; |
| 236 | |
| 237 | engine_state &= ~(engine->engine_mask); |
| 238 | |
| 239 | /* set mode only for this engine */ |
| 240 | mode &= engine->engine_mask; |
| 241 | |
| 242 | engine_state |= mode; |
| 243 | |
| 244 | ret |= lp5523_write(client, LP5523_REG_OP_MODE, engine_state); |
| 245 | fail: |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | static int lp5523_load_mux(struct lp5523_engine *engine, u16 mux) |
| 250 | { |
| 251 | struct lp5523_chip *chip = engine_to_lp5523(engine); |
| 252 | struct i2c_client *client = chip->client; |
| 253 | int ret = 0; |
| 254 | |
| 255 | ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD); |
| 256 | |
| 257 | ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, engine->mux_page); |
| 258 | ret |= lp5523_write(client, LP5523_REG_PROG_MEM, |
| 259 | (u8)(mux >> 8)); |
| 260 | ret |= lp5523_write(client, LP5523_REG_PROG_MEM + 1, (u8)(mux)); |
| 261 | engine->led_mux = mux; |
| 262 | |
| 263 | return ret; |
| 264 | } |
| 265 | |
Andrew Morton | d06cb46 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 266 | static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 267 | { |
| 268 | struct lp5523_chip *chip = engine_to_lp5523(engine); |
| 269 | struct i2c_client *client = chip->client; |
| 270 | |
| 271 | int ret = 0; |
| 272 | |
| 273 | ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD); |
| 274 | |
| 275 | ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, |
| 276 | engine->prog_page); |
| 277 | ret |= i2c_smbus_write_i2c_block_data(client, LP5523_REG_PROG_MEM, |
| 278 | LP5523_PROGRAM_LENGTH, pattern); |
| 279 | |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | static int lp5523_run_program(struct lp5523_engine *engine) |
| 284 | { |
| 285 | struct lp5523_chip *chip = engine_to_lp5523(engine); |
| 286 | struct i2c_client *client = chip->client; |
| 287 | int ret; |
| 288 | |
| 289 | ret = lp5523_write(client, LP5523_REG_ENABLE, |
| 290 | LP5523_CMD_RUN | LP5523_ENABLE); |
| 291 | if (ret) |
| 292 | goto fail; |
| 293 | |
| 294 | ret = lp5523_set_engine_mode(engine, LP5523_CMD_RUN); |
| 295 | fail: |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len) |
| 300 | { |
| 301 | int i; |
| 302 | u16 tmp_mux = 0; |
Kim, Milo | 469eba0 | 2012-08-21 17:04:02 +0800 | [diff] [blame] | 303 | |
| 304 | len = min_t(int, len, LP5523_LEDS); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 305 | for (i = 0; i < len; i++) { |
| 306 | switch (buf[i]) { |
| 307 | case '1': |
| 308 | tmp_mux |= (1 << i); |
| 309 | break; |
| 310 | case '0': |
| 311 | break; |
| 312 | case '\n': |
| 313 | i = len; |
| 314 | break; |
| 315 | default: |
| 316 | return -1; |
| 317 | } |
| 318 | } |
| 319 | *mux = tmp_mux; |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static void lp5523_mux_to_array(u16 led_mux, char *array) |
| 325 | { |
| 326 | int i, pos = 0; |
| 327 | for (i = 0; i < LP5523_LEDS; i++) |
| 328 | pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i)); |
| 329 | |
| 330 | array[pos] = '\0'; |
| 331 | } |
| 332 | |
| 333 | /*--------------------------------------------------------------*/ |
| 334 | /* Sysfs interface */ |
| 335 | /*--------------------------------------------------------------*/ |
| 336 | |
| 337 | static ssize_t show_engine_leds(struct device *dev, |
| 338 | struct device_attribute *attr, |
| 339 | char *buf, int nr) |
| 340 | { |
| 341 | struct i2c_client *client = to_i2c_client(dev); |
| 342 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 343 | char mux[LP5523_LEDS + 1]; |
| 344 | |
| 345 | lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux); |
| 346 | |
| 347 | return sprintf(buf, "%s\n", mux); |
| 348 | } |
| 349 | |
| 350 | #define show_leds(nr) \ |
| 351 | static ssize_t show_engine##nr##_leds(struct device *dev, \ |
| 352 | struct device_attribute *attr, \ |
| 353 | char *buf) \ |
| 354 | { \ |
| 355 | return show_engine_leds(dev, attr, buf, nr); \ |
| 356 | } |
| 357 | show_leds(1) |
| 358 | show_leds(2) |
| 359 | show_leds(3) |
| 360 | |
| 361 | static ssize_t store_engine_leds(struct device *dev, |
| 362 | struct device_attribute *attr, |
| 363 | const char *buf, size_t len, int nr) |
| 364 | { |
| 365 | struct i2c_client *client = to_i2c_client(dev); |
| 366 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 367 | u16 mux = 0; |
Samu Onkalo | fbac081 | 2011-01-12 16:59:18 -0800 | [diff] [blame] | 368 | ssize_t ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 369 | |
| 370 | if (lp5523_mux_parse(buf, &mux, len)) |
| 371 | return -EINVAL; |
| 372 | |
Samu Onkalo | fbac081 | 2011-01-12 16:59:18 -0800 | [diff] [blame] | 373 | mutex_lock(&chip->lock); |
| 374 | ret = -EINVAL; |
| 375 | if (chip->engines[nr - 1].mode != LP5523_CMD_LOAD) |
| 376 | goto leave; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 377 | |
Samu Onkalo | fbac081 | 2011-01-12 16:59:18 -0800 | [diff] [blame] | 378 | if (lp5523_load_mux(&chip->engines[nr - 1], mux)) |
| 379 | goto leave; |
| 380 | |
| 381 | ret = len; |
| 382 | leave: |
| 383 | mutex_unlock(&chip->lock); |
| 384 | return ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | #define store_leds(nr) \ |
| 388 | static ssize_t store_engine##nr##_leds(struct device *dev, \ |
| 389 | struct device_attribute *attr, \ |
| 390 | const char *buf, size_t len) \ |
| 391 | { \ |
| 392 | return store_engine_leds(dev, attr, buf, len, nr); \ |
| 393 | } |
| 394 | store_leds(1) |
| 395 | store_leds(2) |
| 396 | store_leds(3) |
| 397 | |
| 398 | static ssize_t lp5523_selftest(struct device *dev, |
| 399 | struct device_attribute *attr, |
| 400 | char *buf) |
| 401 | { |
| 402 | struct i2c_client *client = to_i2c_client(dev); |
| 403 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 404 | int i, ret, pos = 0; |
| 405 | int led = 0; |
| 406 | u8 status, adc, vdd; |
| 407 | |
| 408 | mutex_lock(&chip->lock); |
| 409 | |
| 410 | ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); |
| 411 | if (ret < 0) |
| 412 | goto fail; |
| 413 | |
| 414 | /* Check that ext clock is really in use if requested */ |
| 415 | if ((chip->pdata) && (chip->pdata->clock_mode == LP5523_CLOCK_EXT)) |
| 416 | if ((status & LP5523_EXT_CLK_USED) == 0) |
| 417 | goto fail; |
| 418 | |
| 419 | /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */ |
| 420 | lp5523_write(chip->client, LP5523_REG_LED_TEST_CTRL, |
| 421 | LP5523_EN_LEDTEST | 16); |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 422 | usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */ |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 423 | ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 424 | if (ret < 0) |
| 425 | goto fail; |
| 426 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 427 | if (!(status & LP5523_LEDTEST_DONE)) |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 428 | usleep_range(3000, 6000); /* Was not ready. Wait little bit */ |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 429 | |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 430 | ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &vdd); |
| 431 | if (ret < 0) |
| 432 | goto fail; |
| 433 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 434 | vdd--; /* There may be some fluctuation in measurement */ |
| 435 | |
| 436 | for (i = 0; i < LP5523_LEDS; i++) { |
| 437 | /* Skip non-existing channels */ |
| 438 | if (chip->pdata->led_config[i].led_current == 0) |
| 439 | continue; |
| 440 | |
| 441 | /* Set default current */ |
| 442 | lp5523_write(chip->client, |
| 443 | LP5523_REG_LED_CURRENT_BASE + i, |
| 444 | chip->pdata->led_config[i].led_current); |
| 445 | |
| 446 | lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0xff); |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 447 | /* let current stabilize 2 - 4ms before measurements start */ |
| 448 | usleep_range(2000, 4000); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 449 | lp5523_write(chip->client, |
| 450 | LP5523_REG_LED_TEST_CTRL, |
| 451 | LP5523_EN_LEDTEST | i); |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 452 | /* ADC conversion time is 2.7 ms typically */ |
| 453 | usleep_range(3000, 6000); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 454 | ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 455 | if (ret < 0) |
| 456 | goto fail; |
| 457 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 458 | if (!(status & LP5523_LEDTEST_DONE)) |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 459 | usleep_range(3000, 6000);/* Was not ready. Wait. */ |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 460 | ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &adc); |
| 461 | if (ret < 0) |
| 462 | goto fail; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 463 | |
| 464 | if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM) |
| 465 | pos += sprintf(buf + pos, "LED %d FAIL\n", i); |
| 466 | |
| 467 | lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0x00); |
| 468 | |
| 469 | /* Restore current */ |
| 470 | lp5523_write(chip->client, |
| 471 | LP5523_REG_LED_CURRENT_BASE + i, |
| 472 | chip->leds[led].led_current); |
| 473 | led++; |
| 474 | } |
| 475 | if (pos == 0) |
| 476 | pos = sprintf(buf, "OK\n"); |
| 477 | goto release_lock; |
| 478 | fail: |
| 479 | pos = sprintf(buf, "FAIL\n"); |
| 480 | |
| 481 | release_lock: |
| 482 | mutex_unlock(&chip->lock); |
| 483 | |
| 484 | return pos; |
| 485 | } |
| 486 | |
| 487 | static void lp5523_set_brightness(struct led_classdev *cdev, |
| 488 | enum led_brightness brightness) |
| 489 | { |
| 490 | struct lp5523_led *led = cdev_to_led(cdev); |
| 491 | |
| 492 | led->brightness = (u8)brightness; |
| 493 | |
| 494 | schedule_work(&led->brightness_work); |
| 495 | } |
| 496 | |
| 497 | static void lp5523_led_brightness_work(struct work_struct *work) |
| 498 | { |
| 499 | struct lp5523_led *led = container_of(work, |
| 500 | struct lp5523_led, |
| 501 | brightness_work); |
| 502 | struct lp5523_chip *chip = led_to_lp5523(led); |
| 503 | struct i2c_client *client = chip->client; |
| 504 | |
| 505 | mutex_lock(&chip->lock); |
| 506 | |
| 507 | lp5523_write(client, LP5523_REG_LED_PWM_BASE + led->chan_nr, |
| 508 | led->brightness); |
| 509 | |
| 510 | mutex_unlock(&chip->lock); |
| 511 | } |
| 512 | |
| 513 | static int lp5523_do_store_load(struct lp5523_engine *engine, |
| 514 | const char *buf, size_t len) |
| 515 | { |
| 516 | struct lp5523_chip *chip = engine_to_lp5523(engine); |
| 517 | struct i2c_client *client = chip->client; |
| 518 | int ret, nrchars, offset = 0, i = 0; |
| 519 | char c[3]; |
| 520 | unsigned cmd; |
| 521 | u8 pattern[LP5523_PROGRAM_LENGTH] = {0}; |
| 522 | |
Kim, Milo | 469eba0 | 2012-08-21 17:04:02 +0800 | [diff] [blame] | 523 | if (engine->mode != LP5523_CMD_LOAD) |
| 524 | return -EINVAL; |
| 525 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 526 | while ((offset < len - 1) && (i < LP5523_PROGRAM_LENGTH)) { |
| 527 | /* separate sscanfs because length is working only for %s */ |
| 528 | ret = sscanf(buf + offset, "%2s%n ", c, &nrchars); |
| 529 | ret = sscanf(c, "%2x", &cmd); |
| 530 | if (ret != 1) |
| 531 | goto fail; |
| 532 | pattern[i] = (u8)cmd; |
| 533 | |
| 534 | offset += nrchars; |
| 535 | i++; |
| 536 | } |
| 537 | |
| 538 | /* Each instruction is 16bit long. Check that length is even */ |
| 539 | if (i % 2) |
| 540 | goto fail; |
| 541 | |
| 542 | mutex_lock(&chip->lock); |
Kim, Milo | 469eba0 | 2012-08-21 17:04:02 +0800 | [diff] [blame] | 543 | ret = lp5523_load_program(engine, pattern); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 544 | mutex_unlock(&chip->lock); |
| 545 | |
| 546 | if (ret) { |
| 547 | dev_err(&client->dev, "failed loading pattern\n"); |
| 548 | return ret; |
| 549 | } |
| 550 | |
| 551 | return len; |
| 552 | fail: |
| 553 | dev_err(&client->dev, "wrong pattern format\n"); |
| 554 | return -EINVAL; |
| 555 | } |
| 556 | |
| 557 | static ssize_t store_engine_load(struct device *dev, |
| 558 | struct device_attribute *attr, |
| 559 | const char *buf, size_t len, int nr) |
| 560 | { |
| 561 | struct i2c_client *client = to_i2c_client(dev); |
| 562 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 563 | return lp5523_do_store_load(&chip->engines[nr - 1], buf, len); |
| 564 | } |
| 565 | |
| 566 | #define store_load(nr) \ |
| 567 | static ssize_t store_engine##nr##_load(struct device *dev, \ |
| 568 | struct device_attribute *attr, \ |
| 569 | const char *buf, size_t len) \ |
| 570 | { \ |
| 571 | return store_engine_load(dev, attr, buf, len, nr); \ |
| 572 | } |
| 573 | store_load(1) |
| 574 | store_load(2) |
| 575 | store_load(3) |
| 576 | |
| 577 | static ssize_t show_engine_mode(struct device *dev, |
| 578 | struct device_attribute *attr, |
| 579 | char *buf, int nr) |
| 580 | { |
| 581 | struct i2c_client *client = to_i2c_client(dev); |
| 582 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 583 | switch (chip->engines[nr - 1].mode) { |
| 584 | case LP5523_CMD_RUN: |
| 585 | return sprintf(buf, "run\n"); |
| 586 | case LP5523_CMD_LOAD: |
| 587 | return sprintf(buf, "load\n"); |
| 588 | case LP5523_CMD_DISABLED: |
| 589 | return sprintf(buf, "disabled\n"); |
| 590 | default: |
| 591 | return sprintf(buf, "disabled\n"); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | #define show_mode(nr) \ |
| 596 | static ssize_t show_engine##nr##_mode(struct device *dev, \ |
| 597 | struct device_attribute *attr, \ |
| 598 | char *buf) \ |
| 599 | { \ |
| 600 | return show_engine_mode(dev, attr, buf, nr); \ |
| 601 | } |
| 602 | show_mode(1) |
| 603 | show_mode(2) |
| 604 | show_mode(3) |
| 605 | |
| 606 | static ssize_t store_engine_mode(struct device *dev, |
| 607 | struct device_attribute *attr, |
| 608 | const char *buf, size_t len, int nr) |
| 609 | { |
| 610 | struct i2c_client *client = to_i2c_client(dev); |
| 611 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 612 | struct lp5523_engine *engine = &chip->engines[nr - 1]; |
| 613 | mutex_lock(&chip->lock); |
| 614 | |
| 615 | if (!strncmp(buf, "run", 3)) |
| 616 | lp5523_set_mode(engine, LP5523_CMD_RUN); |
| 617 | else if (!strncmp(buf, "load", 4)) |
| 618 | lp5523_set_mode(engine, LP5523_CMD_LOAD); |
| 619 | else if (!strncmp(buf, "disabled", 8)) |
| 620 | lp5523_set_mode(engine, LP5523_CMD_DISABLED); |
| 621 | |
| 622 | mutex_unlock(&chip->lock); |
| 623 | return len; |
| 624 | } |
| 625 | |
| 626 | #define store_mode(nr) \ |
| 627 | static ssize_t store_engine##nr##_mode(struct device *dev, \ |
| 628 | struct device_attribute *attr, \ |
| 629 | const char *buf, size_t len) \ |
| 630 | { \ |
| 631 | return store_engine_mode(dev, attr, buf, len, nr); \ |
| 632 | } |
| 633 | store_mode(1) |
| 634 | store_mode(2) |
| 635 | store_mode(3) |
| 636 | |
| 637 | static ssize_t show_max_current(struct device *dev, |
| 638 | struct device_attribute *attr, |
| 639 | char *buf) |
| 640 | { |
| 641 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 642 | struct lp5523_led *led = cdev_to_led(led_cdev); |
| 643 | |
| 644 | return sprintf(buf, "%d\n", led->max_current); |
| 645 | } |
| 646 | |
| 647 | static ssize_t show_current(struct device *dev, |
| 648 | struct device_attribute *attr, |
| 649 | char *buf) |
| 650 | { |
| 651 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 652 | struct lp5523_led *led = cdev_to_led(led_cdev); |
| 653 | |
| 654 | return sprintf(buf, "%d\n", led->led_current); |
| 655 | } |
| 656 | |
| 657 | static ssize_t store_current(struct device *dev, |
| 658 | struct device_attribute *attr, |
| 659 | const char *buf, size_t len) |
| 660 | { |
| 661 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 662 | struct lp5523_led *led = cdev_to_led(led_cdev); |
| 663 | struct lp5523_chip *chip = led_to_lp5523(led); |
| 664 | ssize_t ret; |
| 665 | unsigned long curr; |
| 666 | |
Jingoo Han | eef4aa6 | 2012-11-18 20:51:47 -0800 | [diff] [blame] | 667 | if (kstrtoul(buf, 0, &curr)) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 668 | return -EINVAL; |
| 669 | |
| 670 | if (curr > led->max_current) |
| 671 | return -EINVAL; |
| 672 | |
| 673 | mutex_lock(&chip->lock); |
| 674 | ret = lp5523_write(chip->client, |
| 675 | LP5523_REG_LED_CURRENT_BASE + led->chan_nr, |
| 676 | (u8)curr); |
| 677 | mutex_unlock(&chip->lock); |
| 678 | |
| 679 | if (ret < 0) |
| 680 | return ret; |
| 681 | |
| 682 | led->led_current = (u8)curr; |
| 683 | |
| 684 | return len; |
| 685 | } |
| 686 | |
| 687 | /* led class device attributes */ |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 688 | static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 689 | static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL); |
| 690 | |
| 691 | static struct attribute *lp5523_led_attributes[] = { |
| 692 | &dev_attr_led_current.attr, |
| 693 | &dev_attr_max_current.attr, |
| 694 | NULL, |
| 695 | }; |
| 696 | |
| 697 | static struct attribute_group lp5523_led_attribute_group = { |
| 698 | .attrs = lp5523_led_attributes |
| 699 | }; |
| 700 | |
| 701 | /* device attributes */ |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 702 | static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 703 | show_engine1_mode, store_engine1_mode); |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 704 | static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 705 | show_engine2_mode, store_engine2_mode); |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 706 | static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 707 | show_engine3_mode, store_engine3_mode); |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 708 | static DEVICE_ATTR(engine1_leds, S_IRUGO | S_IWUSR, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 709 | show_engine1_leds, store_engine1_leds); |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 710 | static DEVICE_ATTR(engine2_leds, S_IRUGO | S_IWUSR, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 711 | show_engine2_leds, store_engine2_leds); |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 712 | static DEVICE_ATTR(engine3_leds, S_IRUGO | S_IWUSR, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 713 | show_engine3_leds, store_engine3_leds); |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 714 | static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load); |
| 715 | static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load); |
| 716 | static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 717 | static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL); |
| 718 | |
| 719 | static struct attribute *lp5523_attributes[] = { |
| 720 | &dev_attr_engine1_mode.attr, |
| 721 | &dev_attr_engine2_mode.attr, |
| 722 | &dev_attr_engine3_mode.attr, |
| 723 | &dev_attr_selftest.attr, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 724 | &dev_attr_engine1_load.attr, |
| 725 | &dev_attr_engine1_leds.attr, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 726 | &dev_attr_engine2_load.attr, |
| 727 | &dev_attr_engine2_leds.attr, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 728 | &dev_attr_engine3_load.attr, |
| 729 | &dev_attr_engine3_leds.attr, |
Kim, Milo | f162584 | 2012-09-12 20:16:03 +0800 | [diff] [blame] | 730 | NULL, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 731 | }; |
| 732 | |
| 733 | static const struct attribute_group lp5523_group = { |
| 734 | .attrs = lp5523_attributes, |
| 735 | }; |
| 736 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 737 | static int lp5523_register_sysfs(struct i2c_client *client) |
| 738 | { |
| 739 | struct device *dev = &client->dev; |
| 740 | int ret; |
| 741 | |
| 742 | ret = sysfs_create_group(&dev->kobj, &lp5523_group); |
| 743 | if (ret < 0) |
| 744 | return ret; |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static void lp5523_unregister_sysfs(struct i2c_client *client) |
| 750 | { |
| 751 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 752 | struct device *dev = &client->dev; |
| 753 | int i; |
| 754 | |
| 755 | sysfs_remove_group(&dev->kobj, &lp5523_group); |
| 756 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 757 | for (i = 0; i < chip->num_leds; i++) |
| 758 | sysfs_remove_group(&chip->leds[i].cdev.dev->kobj, |
| 759 | &lp5523_led_attribute_group); |
| 760 | } |
| 761 | |
| 762 | /*--------------------------------------------------------------*/ |
| 763 | /* Set chip operating mode */ |
| 764 | /*--------------------------------------------------------------*/ |
Kim, Milo | 6f6365f | 2012-08-21 17:03:58 +0800 | [diff] [blame] | 765 | static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 766 | { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 767 | /* if in that mode already do nothing, except for run */ |
| 768 | if (mode == engine->mode && mode != LP5523_CMD_RUN) |
Kim, Milo | 6f6365f | 2012-08-21 17:03:58 +0800 | [diff] [blame] | 769 | return; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 770 | |
Kim, Milo | 6f6365f | 2012-08-21 17:03:58 +0800 | [diff] [blame] | 771 | switch (mode) { |
| 772 | case LP5523_CMD_RUN: |
| 773 | lp5523_run_program(engine); |
| 774 | break; |
| 775 | case LP5523_CMD_LOAD: |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 776 | lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED); |
| 777 | lp5523_set_engine_mode(engine, LP5523_CMD_LOAD); |
Kim, Milo | 6f6365f | 2012-08-21 17:03:58 +0800 | [diff] [blame] | 778 | break; |
| 779 | case LP5523_CMD_DISABLED: |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 780 | lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED); |
Kim, Milo | 6f6365f | 2012-08-21 17:03:58 +0800 | [diff] [blame] | 781 | break; |
| 782 | default: |
| 783 | return; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 784 | } |
| 785 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 786 | engine->mode = mode; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /*--------------------------------------------------------------*/ |
| 790 | /* Probe, Attach, Remove */ |
| 791 | /*--------------------------------------------------------------*/ |
| 792 | static int __init lp5523_init_engine(struct lp5523_engine *engine, int id) |
| 793 | { |
| 794 | if (id < 1 || id > LP5523_ENGINES) |
| 795 | return -1; |
| 796 | engine->id = id; |
| 797 | engine->engine_mask = LP5523_ENG_MASK_BASE >> SHIFT_MASK(id); |
| 798 | engine->prog_page = id - 1; |
| 799 | engine->mux_page = id + 2; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 804 | static int lp5523_init_led(struct lp5523_led *led, struct device *dev, |
Kim, Milo | 56a1e9a | 2012-09-04 15:06:26 +0800 | [diff] [blame] | 805 | int chan, struct lp5523_platform_data *pdata, |
| 806 | const char *chip_name) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 807 | { |
| 808 | char name[32]; |
| 809 | int res; |
| 810 | |
| 811 | if (chan >= LP5523_LEDS) |
| 812 | return -EINVAL; |
| 813 | |
| 814 | if (pdata->led_config[chan].led_current) { |
| 815 | led->led_current = pdata->led_config[chan].led_current; |
| 816 | led->max_current = pdata->led_config[chan].max_current; |
| 817 | led->chan_nr = pdata->led_config[chan].chan_nr; |
| 818 | |
| 819 | if (led->chan_nr >= LP5523_LEDS) { |
| 820 | dev_err(dev, "Use channel numbers between 0 and %d\n", |
| 821 | LP5523_LEDS - 1); |
| 822 | return -EINVAL; |
| 823 | } |
| 824 | |
Kim, Milo | 94b43b6 | 2012-08-22 15:32:29 +0800 | [diff] [blame] | 825 | if (pdata->led_config[chan].name) { |
| 826 | led->cdev.name = pdata->led_config[chan].name; |
| 827 | } else { |
| 828 | snprintf(name, sizeof(name), "%s:channel%d", |
Kim, Milo | 56a1e9a | 2012-09-04 15:06:26 +0800 | [diff] [blame] | 829 | pdata->label ? : chip_name, chan); |
Kim, Milo | 94b43b6 | 2012-08-22 15:32:29 +0800 | [diff] [blame] | 830 | led->cdev.name = name; |
| 831 | } |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 832 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 833 | led->cdev.brightness_set = lp5523_set_brightness; |
| 834 | res = led_classdev_register(dev, &led->cdev); |
| 835 | if (res < 0) { |
| 836 | dev_err(dev, "couldn't register led on channel %d\n", |
| 837 | chan); |
| 838 | return res; |
| 839 | } |
| 840 | res = sysfs_create_group(&led->cdev.dev->kobj, |
| 841 | &lp5523_led_attribute_group); |
| 842 | if (res < 0) { |
| 843 | dev_err(dev, "couldn't register current attribute\n"); |
| 844 | led_classdev_unregister(&led->cdev); |
| 845 | return res; |
| 846 | } |
| 847 | } else { |
| 848 | led->led_current = 0; |
| 849 | } |
| 850 | return 0; |
| 851 | } |
| 852 | |
Milo(Woogyom) Kim | f652480 | 2013-02-05 17:53:40 +0900 | [diff] [blame] | 853 | static int lp5523_register_leds(struct lp5523_chip *chip, const char *name) |
| 854 | { |
| 855 | struct lp5523_platform_data *pdata = chip->pdata; |
| 856 | struct i2c_client *client = chip->client; |
| 857 | int i; |
| 858 | int led; |
| 859 | int ret; |
| 860 | |
| 861 | /* Initialize leds */ |
| 862 | chip->num_channels = pdata->num_channels; |
| 863 | chip->num_leds = 0; |
| 864 | led = 0; |
| 865 | for (i = 0; i < pdata->num_channels; i++) { |
| 866 | /* Do not initialize channels that are not connected */ |
| 867 | if (pdata->led_config[i].led_current == 0) |
| 868 | continue; |
| 869 | |
| 870 | INIT_WORK(&chip->leds[led].brightness_work, |
| 871 | lp5523_led_brightness_work); |
| 872 | |
| 873 | ret = lp5523_init_led(&chip->leds[led], &client->dev, i, pdata, |
| 874 | name); |
| 875 | if (ret) { |
| 876 | dev_err(&client->dev, "error initializing leds\n"); |
| 877 | return ret; |
| 878 | } |
| 879 | chip->num_leds++; |
| 880 | |
| 881 | chip->leds[led].id = led; |
| 882 | /* Set LED current */ |
| 883 | lp5523_write(client, |
| 884 | LP5523_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr, |
| 885 | chip->leds[led].led_current); |
| 886 | |
| 887 | led++; |
| 888 | } |
| 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
Milo(Woogyom) Kim | 1904f83 | 2013-02-05 17:56:23 +0900 | [diff] [blame] | 893 | static void lp5523_unregister_leds(struct lp5523_chip *chip) |
| 894 | { |
| 895 | int i; |
| 896 | |
| 897 | for (i = 0; i < chip->num_leds; i++) { |
| 898 | led_classdev_unregister(&chip->leds[i].cdev); |
| 899 | flush_work(&chip->leds[i].brightness_work); |
| 900 | } |
| 901 | } |
| 902 | |
Milo(Woogyom) Kim | 86eb774 | 2013-02-05 17:57:02 +0900 | [diff] [blame] | 903 | static void lp5523_reset_device(struct lp5523_chip *chip) |
| 904 | { |
| 905 | struct i2c_client *client = chip->client; |
| 906 | |
| 907 | lp5523_write(client, LP5523_REG_RESET, 0xff); |
| 908 | } |
| 909 | |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 910 | static void lp5523_deinit_device(struct lp5523_chip *chip); |
Milo(Woogyom) Kim | 944f7b1 | 2013-02-05 17:49:46 +0900 | [diff] [blame] | 911 | static int lp5523_init_device(struct lp5523_chip *chip) |
| 912 | { |
| 913 | struct lp5523_platform_data *pdata = chip->pdata; |
| 914 | struct i2c_client *client = chip->client; |
| 915 | int ret; |
| 916 | |
| 917 | if (pdata->setup_resources) { |
| 918 | ret = pdata->setup_resources(); |
| 919 | if (ret < 0) |
| 920 | return ret; |
| 921 | } |
| 922 | |
| 923 | if (pdata->enable) { |
| 924 | pdata->enable(0); |
| 925 | usleep_range(1000, 2000); /* Keep enable down at least 1ms */ |
| 926 | pdata->enable(1); |
| 927 | usleep_range(1000, 2000); /* 500us abs min. */ |
| 928 | } |
| 929 | |
Milo(Woogyom) Kim | 86eb774 | 2013-02-05 17:57:02 +0900 | [diff] [blame] | 930 | lp5523_reset_device(chip); |
| 931 | |
Milo(Woogyom) Kim | 944f7b1 | 2013-02-05 17:49:46 +0900 | [diff] [blame] | 932 | usleep_range(10000, 20000); /* |
| 933 | * Exact value is not available. 10 - 20ms |
| 934 | * appears to be enough for reset. |
| 935 | */ |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 936 | ret = lp5523_detect(client); |
| 937 | if (ret) |
| 938 | goto err; |
| 939 | |
| 940 | ret = lp5523_configure(client); |
| 941 | if (ret < 0) { |
| 942 | dev_err(&client->dev, "error configuring chip\n"); |
| 943 | goto err_config; |
| 944 | } |
| 945 | |
| 946 | return 0; |
| 947 | |
| 948 | err_config: |
| 949 | lp5523_deinit_device(chip); |
| 950 | err: |
| 951 | return ret; |
Milo(Woogyom) Kim | 944f7b1 | 2013-02-05 17:49:46 +0900 | [diff] [blame] | 952 | } |
| 953 | |
Milo(Woogyom) Kim | 1a991485 | 2013-02-05 17:50:36 +0900 | [diff] [blame] | 954 | static void lp5523_deinit_device(struct lp5523_chip *chip) |
| 955 | { |
| 956 | struct lp5523_platform_data *pdata = chip->pdata; |
| 957 | |
| 958 | if (pdata->enable) |
| 959 | pdata->enable(0); |
| 960 | if (pdata->release_resources) |
| 961 | pdata->release_resources(); |
| 962 | } |
| 963 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 964 | static int lp5523_probe(struct i2c_client *client, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 965 | const struct i2c_device_id *id) |
| 966 | { |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 967 | struct lp5523_chip *old_chip = NULL; |
Milo(Woogyom) Kim | f652480 | 2013-02-05 17:53:40 +0900 | [diff] [blame] | 968 | int ret, i; |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 969 | struct lp55xx_chip *chip; |
| 970 | struct lp55xx_led *led; |
| 971 | struct lp55xx_platform_data *pdata = client->dev.platform_data; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 972 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 973 | if (!pdata) { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 974 | dev_err(&client->dev, "no platform data\n"); |
Bryan Wu | 94ca4bc | 2012-07-04 11:44:18 +0800 | [diff] [blame] | 975 | return -EINVAL; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 976 | } |
| 977 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 978 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
| 979 | if (!chip) |
| 980 | return -ENOMEM; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 981 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 982 | led = devm_kzalloc(&client->dev, |
| 983 | sizeof(*led) * pdata->num_channels, GFP_KERNEL); |
| 984 | if (!led) |
| 985 | return -ENOMEM; |
| 986 | |
| 987 | chip->cl = client; |
| 988 | chip->pdata = pdata; |
| 989 | |
| 990 | mutex_init(&chip->lock); |
| 991 | |
| 992 | i2c_set_clientdata(client, led); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 993 | |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 994 | ret = lp5523_init_device(old_chip); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 995 | if (ret) |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 996 | goto err_init; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 997 | |
Kim, Milo | 56a1e9a | 2012-09-04 15:06:26 +0800 | [diff] [blame] | 998 | dev_info(&client->dev, "%s Programmable led chip found\n", id->name); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 999 | |
| 1000 | /* Initialize engines */ |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 1001 | for (i = 0; i < ARRAY_SIZE(old_chip->engines); i++) { |
| 1002 | ret = lp5523_init_engine(&old_chip->engines[i], i + 1); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1003 | if (ret) { |
| 1004 | dev_err(&client->dev, "error initializing engine\n"); |
Bryan Wu | 94ca4bc | 2012-07-04 11:44:18 +0800 | [diff] [blame] | 1005 | goto fail1; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1006 | } |
| 1007 | } |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1008 | |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 1009 | ret = lp5523_register_leds(old_chip, id->name); |
Milo(Woogyom) Kim | f652480 | 2013-02-05 17:53:40 +0900 | [diff] [blame] | 1010 | if (ret) |
| 1011 | goto fail2; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1012 | |
| 1013 | ret = lp5523_register_sysfs(client); |
| 1014 | if (ret) { |
| 1015 | dev_err(&client->dev, "registering sysfs failed\n"); |
Bryan Wu | 94ca4bc | 2012-07-04 11:44:18 +0800 | [diff] [blame] | 1016 | goto fail2; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1017 | } |
| 1018 | return ret; |
Bryan Wu | 94ca4bc | 2012-07-04 11:44:18 +0800 | [diff] [blame] | 1019 | fail2: |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 1020 | lp5523_unregister_leds(old_chip); |
Bryan Wu | 94ca4bc | 2012-07-04 11:44:18 +0800 | [diff] [blame] | 1021 | fail1: |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 1022 | lp5523_deinit_device(old_chip); |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 1023 | err_init: |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | static int lp5523_remove(struct i2c_client *client) |
| 1028 | { |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 1029 | struct lp5523_chip *old_chip = i2c_get_clientdata(client); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1030 | |
Kim, Milo | 23301b7 | 2012-09-12 20:16:00 +0800 | [diff] [blame] | 1031 | /* Disable engine mode */ |
| 1032 | lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED); |
| 1033 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1034 | lp5523_unregister_sysfs(client); |
| 1035 | |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 1036 | lp5523_unregister_leds(old_chip); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1037 | |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 1038 | lp5523_deinit_device(old_chip); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | static const struct i2c_device_id lp5523_id[] = { |
Kim, Milo | 27d7704 | 2012-09-04 15:06:18 +0800 | [diff] [blame] | 1043 | { "lp5523", LP5523 }, |
| 1044 | { "lp55231", LP55231 }, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1045 | { } |
| 1046 | }; |
| 1047 | |
| 1048 | MODULE_DEVICE_TABLE(i2c, lp5523_id); |
| 1049 | |
| 1050 | static struct i2c_driver lp5523_driver = { |
| 1051 | .driver = { |
Kim, Milo | 27d7704 | 2012-09-04 15:06:18 +0800 | [diff] [blame] | 1052 | .name = "lp5523x", |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1053 | }, |
| 1054 | .probe = lp5523_probe, |
| 1055 | .remove = lp5523_remove, |
| 1056 | .id_table = lp5523_id, |
| 1057 | }; |
| 1058 | |
Axel Lin | 09a0d18 | 2012-01-10 15:09:27 -0800 | [diff] [blame] | 1059 | module_i2c_driver(lp5523_driver); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1060 | |
| 1061 | MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>"); |
| 1062 | MODULE_DESCRIPTION("LP5523 LED engine"); |
| 1063 | MODULE_LICENSE("GPL"); |