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 | |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 97 | #define LP5523_MAX_LEDS 9 |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 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]; |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 139 | struct lp5523_led leds[LP5523_MAX_LEDS]; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 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 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 166 | static int lp5523_write(struct i2c_client *client, u8 reg, u8 value) |
| 167 | { |
| 168 | return i2c_smbus_write_byte_data(client, reg, value); |
| 169 | } |
| 170 | |
| 171 | static int lp5523_read(struct i2c_client *client, u8 reg, u8 *buf) |
| 172 | { |
| 173 | s32 ret = i2c_smbus_read_byte_data(client, reg); |
| 174 | |
| 175 | if (ret < 0) |
Sachin Kamat | 5ebab74 | 2012-11-20 01:59:01 -0800 | [diff] [blame] | 176 | return ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 177 | |
| 178 | *buf = ret; |
| 179 | return 0; |
| 180 | } |
| 181 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 182 | static int lp5523_post_init_device(struct lp55xx_chip *chip) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 183 | { |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 184 | int ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 185 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 186 | ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE); |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame] | 187 | if (ret) |
| 188 | return ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 189 | |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 190 | /* Chip startup time is 500 us, 1 - 2 ms gives some margin */ |
| 191 | usleep_range(1000, 2000); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 192 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 193 | ret = lp55xx_write(chip, LP5523_REG_CONFIG, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 194 | LP5523_AUTO_INC | LP5523_PWR_SAVE | |
| 195 | LP5523_CP_AUTO | LP5523_AUTO_CLK | |
| 196 | LP5523_PWM_PWR_SAVE); |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame] | 197 | if (ret) |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 198 | return ret; |
| 199 | |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame] | 200 | /* turn on all leds */ |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 201 | ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01); |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame] | 202 | if (ret) |
| 203 | return ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 204 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 205 | return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode) |
| 209 | { |
| 210 | struct lp5523_chip *chip = engine_to_lp5523(engine); |
| 211 | struct i2c_client *client = chip->client; |
| 212 | int ret; |
| 213 | u8 engine_state; |
| 214 | |
| 215 | ret = lp5523_read(client, LP5523_REG_OP_MODE, &engine_state); |
| 216 | if (ret) |
| 217 | goto fail; |
| 218 | |
| 219 | engine_state &= ~(engine->engine_mask); |
| 220 | |
| 221 | /* set mode only for this engine */ |
| 222 | mode &= engine->engine_mask; |
| 223 | |
| 224 | engine_state |= mode; |
| 225 | |
| 226 | ret |= lp5523_write(client, LP5523_REG_OP_MODE, engine_state); |
| 227 | fail: |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | static int lp5523_load_mux(struct lp5523_engine *engine, u16 mux) |
| 232 | { |
| 233 | struct lp5523_chip *chip = engine_to_lp5523(engine); |
| 234 | struct i2c_client *client = chip->client; |
| 235 | int ret = 0; |
| 236 | |
| 237 | ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD); |
| 238 | |
| 239 | ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, engine->mux_page); |
| 240 | ret |= lp5523_write(client, LP5523_REG_PROG_MEM, |
| 241 | (u8)(mux >> 8)); |
| 242 | ret |= lp5523_write(client, LP5523_REG_PROG_MEM + 1, (u8)(mux)); |
| 243 | engine->led_mux = mux; |
| 244 | |
| 245 | return ret; |
| 246 | } |
| 247 | |
Andrew Morton | d06cb46 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 248 | static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 249 | { |
| 250 | struct lp5523_chip *chip = engine_to_lp5523(engine); |
| 251 | struct i2c_client *client = chip->client; |
| 252 | |
| 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, |
| 258 | engine->prog_page); |
| 259 | ret |= i2c_smbus_write_i2c_block_data(client, LP5523_REG_PROG_MEM, |
| 260 | LP5523_PROGRAM_LENGTH, pattern); |
| 261 | |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | static int lp5523_run_program(struct lp5523_engine *engine) |
| 266 | { |
| 267 | struct lp5523_chip *chip = engine_to_lp5523(engine); |
| 268 | struct i2c_client *client = chip->client; |
| 269 | int ret; |
| 270 | |
| 271 | ret = lp5523_write(client, LP5523_REG_ENABLE, |
| 272 | LP5523_CMD_RUN | LP5523_ENABLE); |
| 273 | if (ret) |
| 274 | goto fail; |
| 275 | |
| 276 | ret = lp5523_set_engine_mode(engine, LP5523_CMD_RUN); |
| 277 | fail: |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len) |
| 282 | { |
| 283 | int i; |
| 284 | u16 tmp_mux = 0; |
Kim, Milo | 469eba0 | 2012-08-21 17:04:02 +0800 | [diff] [blame] | 285 | |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 286 | len = min_t(int, len, LP5523_MAX_LEDS); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 287 | for (i = 0; i < len; i++) { |
| 288 | switch (buf[i]) { |
| 289 | case '1': |
| 290 | tmp_mux |= (1 << i); |
| 291 | break; |
| 292 | case '0': |
| 293 | break; |
| 294 | case '\n': |
| 295 | i = len; |
| 296 | break; |
| 297 | default: |
| 298 | return -1; |
| 299 | } |
| 300 | } |
| 301 | *mux = tmp_mux; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static void lp5523_mux_to_array(u16 led_mux, char *array) |
| 307 | { |
| 308 | int i, pos = 0; |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 309 | for (i = 0; i < LP5523_MAX_LEDS; i++) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 310 | pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i)); |
| 311 | |
| 312 | array[pos] = '\0'; |
| 313 | } |
| 314 | |
| 315 | /*--------------------------------------------------------------*/ |
| 316 | /* Sysfs interface */ |
| 317 | /*--------------------------------------------------------------*/ |
| 318 | |
| 319 | static ssize_t show_engine_leds(struct device *dev, |
| 320 | struct device_attribute *attr, |
| 321 | char *buf, int nr) |
| 322 | { |
| 323 | struct i2c_client *client = to_i2c_client(dev); |
| 324 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 325 | char mux[LP5523_MAX_LEDS + 1]; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 326 | |
| 327 | lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux); |
| 328 | |
| 329 | return sprintf(buf, "%s\n", mux); |
| 330 | } |
| 331 | |
| 332 | #define show_leds(nr) \ |
| 333 | static ssize_t show_engine##nr##_leds(struct device *dev, \ |
| 334 | struct device_attribute *attr, \ |
| 335 | char *buf) \ |
| 336 | { \ |
| 337 | return show_engine_leds(dev, attr, buf, nr); \ |
| 338 | } |
| 339 | show_leds(1) |
| 340 | show_leds(2) |
| 341 | show_leds(3) |
| 342 | |
| 343 | static ssize_t store_engine_leds(struct device *dev, |
| 344 | struct device_attribute *attr, |
| 345 | const char *buf, size_t len, int nr) |
| 346 | { |
| 347 | struct i2c_client *client = to_i2c_client(dev); |
| 348 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 349 | u16 mux = 0; |
Samu Onkalo | fbac081 | 2011-01-12 16:59:18 -0800 | [diff] [blame] | 350 | ssize_t ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 351 | |
| 352 | if (lp5523_mux_parse(buf, &mux, len)) |
| 353 | return -EINVAL; |
| 354 | |
Samu Onkalo | fbac081 | 2011-01-12 16:59:18 -0800 | [diff] [blame] | 355 | mutex_lock(&chip->lock); |
| 356 | ret = -EINVAL; |
| 357 | if (chip->engines[nr - 1].mode != LP5523_CMD_LOAD) |
| 358 | goto leave; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 359 | |
Samu Onkalo | fbac081 | 2011-01-12 16:59:18 -0800 | [diff] [blame] | 360 | if (lp5523_load_mux(&chip->engines[nr - 1], mux)) |
| 361 | goto leave; |
| 362 | |
| 363 | ret = len; |
| 364 | leave: |
| 365 | mutex_unlock(&chip->lock); |
| 366 | return ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | #define store_leds(nr) \ |
| 370 | static ssize_t store_engine##nr##_leds(struct device *dev, \ |
| 371 | struct device_attribute *attr, \ |
| 372 | const char *buf, size_t len) \ |
| 373 | { \ |
| 374 | return store_engine_leds(dev, attr, buf, len, nr); \ |
| 375 | } |
| 376 | store_leds(1) |
| 377 | store_leds(2) |
| 378 | store_leds(3) |
| 379 | |
| 380 | static ssize_t lp5523_selftest(struct device *dev, |
| 381 | struct device_attribute *attr, |
| 382 | char *buf) |
| 383 | { |
| 384 | struct i2c_client *client = to_i2c_client(dev); |
| 385 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 386 | int i, ret, pos = 0; |
| 387 | int led = 0; |
| 388 | u8 status, adc, vdd; |
| 389 | |
| 390 | mutex_lock(&chip->lock); |
| 391 | |
| 392 | ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); |
| 393 | if (ret < 0) |
| 394 | goto fail; |
| 395 | |
| 396 | /* Check that ext clock is really in use if requested */ |
| 397 | if ((chip->pdata) && (chip->pdata->clock_mode == LP5523_CLOCK_EXT)) |
| 398 | if ((status & LP5523_EXT_CLK_USED) == 0) |
| 399 | goto fail; |
| 400 | |
| 401 | /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */ |
| 402 | lp5523_write(chip->client, LP5523_REG_LED_TEST_CTRL, |
| 403 | LP5523_EN_LEDTEST | 16); |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 404 | usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */ |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 405 | ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 406 | if (ret < 0) |
| 407 | goto fail; |
| 408 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 409 | if (!(status & LP5523_LEDTEST_DONE)) |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 410 | usleep_range(3000, 6000); /* Was not ready. Wait little bit */ |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 411 | |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 412 | ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &vdd); |
| 413 | if (ret < 0) |
| 414 | goto fail; |
| 415 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 416 | vdd--; /* There may be some fluctuation in measurement */ |
| 417 | |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 418 | for (i = 0; i < LP5523_MAX_LEDS; i++) { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 419 | /* Skip non-existing channels */ |
| 420 | if (chip->pdata->led_config[i].led_current == 0) |
| 421 | continue; |
| 422 | |
| 423 | /* Set default current */ |
| 424 | lp5523_write(chip->client, |
| 425 | LP5523_REG_LED_CURRENT_BASE + i, |
| 426 | chip->pdata->led_config[i].led_current); |
| 427 | |
| 428 | lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0xff); |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 429 | /* let current stabilize 2 - 4ms before measurements start */ |
| 430 | usleep_range(2000, 4000); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 431 | lp5523_write(chip->client, |
| 432 | LP5523_REG_LED_TEST_CTRL, |
| 433 | LP5523_EN_LEDTEST | i); |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 434 | /* ADC conversion time is 2.7 ms typically */ |
| 435 | usleep_range(3000, 6000); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 436 | ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 437 | if (ret < 0) |
| 438 | goto fail; |
| 439 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 440 | if (!(status & LP5523_LEDTEST_DONE)) |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 441 | usleep_range(3000, 6000);/* Was not ready. Wait. */ |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 442 | ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &adc); |
| 443 | if (ret < 0) |
| 444 | goto fail; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 445 | |
| 446 | if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM) |
| 447 | pos += sprintf(buf + pos, "LED %d FAIL\n", i); |
| 448 | |
| 449 | lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0x00); |
| 450 | |
| 451 | /* Restore current */ |
| 452 | lp5523_write(chip->client, |
| 453 | LP5523_REG_LED_CURRENT_BASE + i, |
| 454 | chip->leds[led].led_current); |
| 455 | led++; |
| 456 | } |
| 457 | if (pos == 0) |
| 458 | pos = sprintf(buf, "OK\n"); |
| 459 | goto release_lock; |
| 460 | fail: |
| 461 | pos = sprintf(buf, "FAIL\n"); |
| 462 | |
| 463 | release_lock: |
| 464 | mutex_unlock(&chip->lock); |
| 465 | |
| 466 | return pos; |
| 467 | } |
| 468 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 469 | static void lp5523_led_brightness_work(struct work_struct *work) |
| 470 | { |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame^] | 471 | struct lp55xx_led *led = container_of(work, struct lp55xx_led, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 472 | brightness_work); |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame^] | 473 | struct lp55xx_chip *chip = led->chip; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 474 | |
| 475 | mutex_lock(&chip->lock); |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame^] | 476 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 477 | led->brightness); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 478 | mutex_unlock(&chip->lock); |
| 479 | } |
| 480 | |
| 481 | static int lp5523_do_store_load(struct lp5523_engine *engine, |
| 482 | const char *buf, size_t len) |
| 483 | { |
| 484 | struct lp5523_chip *chip = engine_to_lp5523(engine); |
| 485 | struct i2c_client *client = chip->client; |
| 486 | int ret, nrchars, offset = 0, i = 0; |
| 487 | char c[3]; |
| 488 | unsigned cmd; |
| 489 | u8 pattern[LP5523_PROGRAM_LENGTH] = {0}; |
| 490 | |
Kim, Milo | 469eba0 | 2012-08-21 17:04:02 +0800 | [diff] [blame] | 491 | if (engine->mode != LP5523_CMD_LOAD) |
| 492 | return -EINVAL; |
| 493 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 494 | while ((offset < len - 1) && (i < LP5523_PROGRAM_LENGTH)) { |
| 495 | /* separate sscanfs because length is working only for %s */ |
| 496 | ret = sscanf(buf + offset, "%2s%n ", c, &nrchars); |
| 497 | ret = sscanf(c, "%2x", &cmd); |
| 498 | if (ret != 1) |
| 499 | goto fail; |
| 500 | pattern[i] = (u8)cmd; |
| 501 | |
| 502 | offset += nrchars; |
| 503 | i++; |
| 504 | } |
| 505 | |
| 506 | /* Each instruction is 16bit long. Check that length is even */ |
| 507 | if (i % 2) |
| 508 | goto fail; |
| 509 | |
| 510 | mutex_lock(&chip->lock); |
Kim, Milo | 469eba0 | 2012-08-21 17:04:02 +0800 | [diff] [blame] | 511 | ret = lp5523_load_program(engine, pattern); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 512 | mutex_unlock(&chip->lock); |
| 513 | |
| 514 | if (ret) { |
| 515 | dev_err(&client->dev, "failed loading pattern\n"); |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | return len; |
| 520 | fail: |
| 521 | dev_err(&client->dev, "wrong pattern format\n"); |
| 522 | return -EINVAL; |
| 523 | } |
| 524 | |
| 525 | static ssize_t store_engine_load(struct device *dev, |
| 526 | struct device_attribute *attr, |
| 527 | const char *buf, size_t len, int nr) |
| 528 | { |
| 529 | struct i2c_client *client = to_i2c_client(dev); |
| 530 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 531 | return lp5523_do_store_load(&chip->engines[nr - 1], buf, len); |
| 532 | } |
| 533 | |
| 534 | #define store_load(nr) \ |
| 535 | static ssize_t store_engine##nr##_load(struct device *dev, \ |
| 536 | struct device_attribute *attr, \ |
| 537 | const char *buf, size_t len) \ |
| 538 | { \ |
| 539 | return store_engine_load(dev, attr, buf, len, nr); \ |
| 540 | } |
| 541 | store_load(1) |
| 542 | store_load(2) |
| 543 | store_load(3) |
| 544 | |
| 545 | static ssize_t show_engine_mode(struct device *dev, |
| 546 | struct device_attribute *attr, |
| 547 | char *buf, int nr) |
| 548 | { |
| 549 | struct i2c_client *client = to_i2c_client(dev); |
| 550 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 551 | switch (chip->engines[nr - 1].mode) { |
| 552 | case LP5523_CMD_RUN: |
| 553 | return sprintf(buf, "run\n"); |
| 554 | case LP5523_CMD_LOAD: |
| 555 | return sprintf(buf, "load\n"); |
| 556 | case LP5523_CMD_DISABLED: |
| 557 | return sprintf(buf, "disabled\n"); |
| 558 | default: |
| 559 | return sprintf(buf, "disabled\n"); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | #define show_mode(nr) \ |
| 564 | static ssize_t show_engine##nr##_mode(struct device *dev, \ |
| 565 | struct device_attribute *attr, \ |
| 566 | char *buf) \ |
| 567 | { \ |
| 568 | return show_engine_mode(dev, attr, buf, nr); \ |
| 569 | } |
| 570 | show_mode(1) |
| 571 | show_mode(2) |
| 572 | show_mode(3) |
| 573 | |
| 574 | static ssize_t store_engine_mode(struct device *dev, |
| 575 | struct device_attribute *attr, |
| 576 | const char *buf, size_t len, int nr) |
| 577 | { |
| 578 | struct i2c_client *client = to_i2c_client(dev); |
| 579 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 580 | struct lp5523_engine *engine = &chip->engines[nr - 1]; |
| 581 | mutex_lock(&chip->lock); |
| 582 | |
| 583 | if (!strncmp(buf, "run", 3)) |
| 584 | lp5523_set_mode(engine, LP5523_CMD_RUN); |
| 585 | else if (!strncmp(buf, "load", 4)) |
| 586 | lp5523_set_mode(engine, LP5523_CMD_LOAD); |
| 587 | else if (!strncmp(buf, "disabled", 8)) |
| 588 | lp5523_set_mode(engine, LP5523_CMD_DISABLED); |
| 589 | |
| 590 | mutex_unlock(&chip->lock); |
| 591 | return len; |
| 592 | } |
| 593 | |
| 594 | #define store_mode(nr) \ |
| 595 | static ssize_t store_engine##nr##_mode(struct device *dev, \ |
| 596 | struct device_attribute *attr, \ |
| 597 | const char *buf, size_t len) \ |
| 598 | { \ |
| 599 | return store_engine_mode(dev, attr, buf, len, nr); \ |
| 600 | } |
| 601 | store_mode(1) |
| 602 | store_mode(2) |
| 603 | store_mode(3) |
| 604 | |
| 605 | static ssize_t show_max_current(struct device *dev, |
| 606 | struct device_attribute *attr, |
| 607 | char *buf) |
| 608 | { |
| 609 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 610 | struct lp5523_led *led = cdev_to_led(led_cdev); |
| 611 | |
| 612 | return sprintf(buf, "%d\n", led->max_current); |
| 613 | } |
| 614 | |
| 615 | static ssize_t show_current(struct device *dev, |
| 616 | struct device_attribute *attr, |
| 617 | char *buf) |
| 618 | { |
| 619 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 620 | struct lp5523_led *led = cdev_to_led(led_cdev); |
| 621 | |
| 622 | return sprintf(buf, "%d\n", led->led_current); |
| 623 | } |
| 624 | |
| 625 | static ssize_t store_current(struct device *dev, |
| 626 | struct device_attribute *attr, |
| 627 | const char *buf, size_t len) |
| 628 | { |
| 629 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 630 | struct lp5523_led *led = cdev_to_led(led_cdev); |
| 631 | struct lp5523_chip *chip = led_to_lp5523(led); |
| 632 | ssize_t ret; |
| 633 | unsigned long curr; |
| 634 | |
Jingoo Han | eef4aa6 | 2012-11-18 20:51:47 -0800 | [diff] [blame] | 635 | if (kstrtoul(buf, 0, &curr)) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 636 | return -EINVAL; |
| 637 | |
| 638 | if (curr > led->max_current) |
| 639 | return -EINVAL; |
| 640 | |
| 641 | mutex_lock(&chip->lock); |
| 642 | ret = lp5523_write(chip->client, |
| 643 | LP5523_REG_LED_CURRENT_BASE + led->chan_nr, |
| 644 | (u8)curr); |
| 645 | mutex_unlock(&chip->lock); |
| 646 | |
| 647 | if (ret < 0) |
| 648 | return ret; |
| 649 | |
| 650 | led->led_current = (u8)curr; |
| 651 | |
| 652 | return len; |
| 653 | } |
| 654 | |
| 655 | /* led class device attributes */ |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 656 | 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] | 657 | static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL); |
| 658 | |
| 659 | static struct attribute *lp5523_led_attributes[] = { |
| 660 | &dev_attr_led_current.attr, |
| 661 | &dev_attr_max_current.attr, |
| 662 | NULL, |
| 663 | }; |
| 664 | |
| 665 | static struct attribute_group lp5523_led_attribute_group = { |
| 666 | .attrs = lp5523_led_attributes |
| 667 | }; |
| 668 | |
| 669 | /* device attributes */ |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 670 | static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 671 | show_engine1_mode, store_engine1_mode); |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 672 | static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 673 | show_engine2_mode, store_engine2_mode); |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 674 | static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 675 | show_engine3_mode, store_engine3_mode); |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 676 | static DEVICE_ATTR(engine1_leds, S_IRUGO | S_IWUSR, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 677 | show_engine1_leds, store_engine1_leds); |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 678 | static DEVICE_ATTR(engine2_leds, S_IRUGO | S_IWUSR, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 679 | show_engine2_leds, store_engine2_leds); |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 680 | static DEVICE_ATTR(engine3_leds, S_IRUGO | S_IWUSR, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 681 | show_engine3_leds, store_engine3_leds); |
Vasiliy Kulikov | ccd7510 | 2011-03-22 16:30:20 -0700 | [diff] [blame] | 682 | static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load); |
| 683 | static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load); |
| 684 | static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 685 | static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL); |
| 686 | |
| 687 | static struct attribute *lp5523_attributes[] = { |
| 688 | &dev_attr_engine1_mode.attr, |
| 689 | &dev_attr_engine2_mode.attr, |
| 690 | &dev_attr_engine3_mode.attr, |
| 691 | &dev_attr_selftest.attr, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 692 | &dev_attr_engine1_load.attr, |
| 693 | &dev_attr_engine1_leds.attr, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 694 | &dev_attr_engine2_load.attr, |
| 695 | &dev_attr_engine2_leds.attr, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 696 | &dev_attr_engine3_load.attr, |
| 697 | &dev_attr_engine3_leds.attr, |
Kim, Milo | f162584 | 2012-09-12 20:16:03 +0800 | [diff] [blame] | 698 | NULL, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 699 | }; |
| 700 | |
| 701 | static const struct attribute_group lp5523_group = { |
| 702 | .attrs = lp5523_attributes, |
| 703 | }; |
| 704 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 705 | static int lp5523_register_sysfs(struct i2c_client *client) |
| 706 | { |
| 707 | struct device *dev = &client->dev; |
| 708 | int ret; |
| 709 | |
| 710 | ret = sysfs_create_group(&dev->kobj, &lp5523_group); |
| 711 | if (ret < 0) |
| 712 | return ret; |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | static void lp5523_unregister_sysfs(struct i2c_client *client) |
| 718 | { |
| 719 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 720 | struct device *dev = &client->dev; |
| 721 | int i; |
| 722 | |
| 723 | sysfs_remove_group(&dev->kobj, &lp5523_group); |
| 724 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 725 | for (i = 0; i < chip->num_leds; i++) |
| 726 | sysfs_remove_group(&chip->leds[i].cdev.dev->kobj, |
| 727 | &lp5523_led_attribute_group); |
| 728 | } |
| 729 | |
| 730 | /*--------------------------------------------------------------*/ |
| 731 | /* Set chip operating mode */ |
| 732 | /*--------------------------------------------------------------*/ |
Kim, Milo | 6f6365f | 2012-08-21 17:03:58 +0800 | [diff] [blame] | 733 | static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 734 | { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 735 | /* if in that mode already do nothing, except for run */ |
| 736 | if (mode == engine->mode && mode != LP5523_CMD_RUN) |
Kim, Milo | 6f6365f | 2012-08-21 17:03:58 +0800 | [diff] [blame] | 737 | return; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 738 | |
Kim, Milo | 6f6365f | 2012-08-21 17:03:58 +0800 | [diff] [blame] | 739 | switch (mode) { |
| 740 | case LP5523_CMD_RUN: |
| 741 | lp5523_run_program(engine); |
| 742 | break; |
| 743 | case LP5523_CMD_LOAD: |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 744 | lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED); |
| 745 | lp5523_set_engine_mode(engine, LP5523_CMD_LOAD); |
Kim, Milo | 6f6365f | 2012-08-21 17:03:58 +0800 | [diff] [blame] | 746 | break; |
| 747 | case LP5523_CMD_DISABLED: |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 748 | lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED); |
Kim, Milo | 6f6365f | 2012-08-21 17:03:58 +0800 | [diff] [blame] | 749 | break; |
| 750 | default: |
| 751 | return; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 752 | } |
| 753 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 754 | engine->mode = mode; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /*--------------------------------------------------------------*/ |
| 758 | /* Probe, Attach, Remove */ |
| 759 | /*--------------------------------------------------------------*/ |
Milo(Woogyom) Kim | 1904f83 | 2013-02-05 17:56:23 +0900 | [diff] [blame] | 760 | static void lp5523_unregister_leds(struct lp5523_chip *chip) |
| 761 | { |
| 762 | int i; |
| 763 | |
| 764 | for (i = 0; i < chip->num_leds; i++) { |
| 765 | led_classdev_unregister(&chip->leds[i].cdev); |
| 766 | flush_work(&chip->leds[i].brightness_work); |
| 767 | } |
| 768 | } |
| 769 | |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 770 | /* Chip specific configurations */ |
| 771 | static struct lp55xx_device_config lp5523_cfg = { |
| 772 | .reset = { |
| 773 | .addr = LP5523_REG_RESET, |
| 774 | .val = LP5523_RESET, |
| 775 | }, |
Milo(Woogyom) Kim | e3a700d | 2013-02-05 18:09:56 +0900 | [diff] [blame] | 776 | .enable = { |
| 777 | .addr = LP5523_REG_ENABLE, |
| 778 | .val = LP5523_ENABLE, |
| 779 | }, |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 780 | .max_channel = LP5523_MAX_LEDS, |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 781 | .post_init_device = lp5523_post_init_device, |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame^] | 782 | .brightness_work_fn = lp5523_led_brightness_work, |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 783 | }; |
| 784 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 785 | static int lp5523_probe(struct i2c_client *client, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 786 | const struct i2c_device_id *id) |
| 787 | { |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 788 | struct lp5523_chip *old_chip = NULL; |
Milo(Woogyom) Kim | 22ebeb4 | 2013-02-05 18:58:35 +0900 | [diff] [blame] | 789 | int ret; |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 790 | struct lp55xx_chip *chip; |
| 791 | struct lp55xx_led *led; |
| 792 | struct lp55xx_platform_data *pdata = client->dev.platform_data; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 793 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 794 | if (!pdata) { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 795 | dev_err(&client->dev, "no platform data\n"); |
Bryan Wu | 94ca4bc | 2012-07-04 11:44:18 +0800 | [diff] [blame] | 796 | return -EINVAL; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 797 | } |
| 798 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 799 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
| 800 | if (!chip) |
| 801 | return -ENOMEM; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 802 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 803 | led = devm_kzalloc(&client->dev, |
| 804 | sizeof(*led) * pdata->num_channels, GFP_KERNEL); |
| 805 | if (!led) |
| 806 | return -ENOMEM; |
| 807 | |
| 808 | chip->cl = client; |
| 809 | chip->pdata = pdata; |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 810 | chip->cfg = &lp5523_cfg; |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 811 | |
| 812 | mutex_init(&chip->lock); |
| 813 | |
| 814 | i2c_set_clientdata(client, led); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 815 | |
Milo(Woogyom) Kim | 22ebeb4 | 2013-02-05 18:58:35 +0900 | [diff] [blame] | 816 | ret = lp55xx_init_device(chip); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 817 | if (ret) |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 818 | goto err_init; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 819 | |
Kim, Milo | 56a1e9a | 2012-09-04 15:06:26 +0800 | [diff] [blame] | 820 | dev_info(&client->dev, "%s Programmable led chip found\n", id->name); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 821 | |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 822 | ret = lp55xx_register_leds(led, chip); |
Milo(Woogyom) Kim | f652480 | 2013-02-05 17:53:40 +0900 | [diff] [blame] | 823 | if (ret) |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 824 | goto err_register_leds; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 825 | |
| 826 | ret = lp5523_register_sysfs(client); |
| 827 | if (ret) { |
| 828 | dev_err(&client->dev, "registering sysfs failed\n"); |
Bryan Wu | 94ca4bc | 2012-07-04 11:44:18 +0800 | [diff] [blame] | 829 | goto fail2; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 830 | } |
| 831 | return ret; |
Bryan Wu | 94ca4bc | 2012-07-04 11:44:18 +0800 | [diff] [blame] | 832 | fail2: |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 833 | lp5523_unregister_leds(old_chip); |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 834 | err_register_leds: |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 835 | lp55xx_deinit_device(chip); |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 836 | err_init: |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 837 | return ret; |
| 838 | } |
| 839 | |
| 840 | static int lp5523_remove(struct i2c_client *client) |
| 841 | { |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 842 | struct lp5523_chip *old_chip = i2c_get_clientdata(client); |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 843 | struct lp55xx_led *led = i2c_get_clientdata(client); |
| 844 | struct lp55xx_chip *chip = led->chip; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 845 | |
Kim, Milo | 23301b7 | 2012-09-12 20:16:00 +0800 | [diff] [blame] | 846 | /* Disable engine mode */ |
| 847 | lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED); |
| 848 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 849 | lp5523_unregister_sysfs(client); |
| 850 | |
Milo(Woogyom) Kim | 945c700 | 2013-02-05 18:02:26 +0900 | [diff] [blame] | 851 | lp5523_unregister_leds(old_chip); |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 852 | lp55xx_deinit_device(chip); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 853 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | static const struct i2c_device_id lp5523_id[] = { |
Kim, Milo | 27d7704 | 2012-09-04 15:06:18 +0800 | [diff] [blame] | 858 | { "lp5523", LP5523 }, |
| 859 | { "lp55231", LP55231 }, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 860 | { } |
| 861 | }; |
| 862 | |
| 863 | MODULE_DEVICE_TABLE(i2c, lp5523_id); |
| 864 | |
| 865 | static struct i2c_driver lp5523_driver = { |
| 866 | .driver = { |
Kim, Milo | 27d7704 | 2012-09-04 15:06:18 +0800 | [diff] [blame] | 867 | .name = "lp5523x", |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 868 | }, |
| 869 | .probe = lp5523_probe, |
| 870 | .remove = lp5523_remove, |
| 871 | .id_table = lp5523_id, |
| 872 | }; |
| 873 | |
Axel Lin | 09a0d18 | 2012-01-10 15:09:27 -0800 | [diff] [blame] | 874 | module_i2c_driver(lp5523_driver); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 875 | |
| 876 | MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>"); |
| 877 | MODULE_DESCRIPTION("LP5523 LED engine"); |
| 878 | MODULE_LICENSE("GPL"); |