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