Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * leds-bd2802.c - RGB LED Driver |
| 3 | * |
| 4 | * Copyright (C) 2009 Samsung Electronics |
| 5 | * Kim Kyuwon <q1.kim@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * Datasheet: http://www.rohm.com/products/databook/driver/pdf/bd2802gu-e.pdf |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/gpio.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/leds.h> |
| 20 | #include <linux/leds-bd2802.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 22 | |
| 23 | |
| 24 | #define LED_CTL(rgb2en, rgb1en) ((rgb2en) << 4 | ((rgb1en) << 0)) |
| 25 | |
| 26 | #define BD2802_LED_OFFSET 0xa |
| 27 | #define BD2802_COLOR_OFFSET 0x3 |
| 28 | |
| 29 | #define BD2802_REG_CLKSETUP 0x00 |
| 30 | #define BD2802_REG_CONTROL 0x01 |
| 31 | #define BD2802_REG_HOURSETUP 0x02 |
| 32 | #define BD2802_REG_CURRENT1SETUP 0x03 |
| 33 | #define BD2802_REG_CURRENT2SETUP 0x04 |
| 34 | #define BD2802_REG_WAVEPATTERN 0x05 |
| 35 | |
| 36 | #define BD2802_CURRENT_032 0x10 /* 3.2mA */ |
| 37 | #define BD2802_CURRENT_000 0x00 /* 0.0mA */ |
| 38 | |
| 39 | #define BD2802_PATTERN_FULL 0x07 |
| 40 | #define BD2802_PATTERN_HALF 0x03 |
| 41 | |
| 42 | enum led_ids { |
| 43 | LED1, |
| 44 | LED2, |
| 45 | LED_NUM, |
| 46 | }; |
| 47 | |
| 48 | enum led_colors { |
| 49 | RED, |
| 50 | GREEN, |
| 51 | BLUE, |
| 52 | }; |
| 53 | |
| 54 | enum led_bits { |
| 55 | BD2802_OFF, |
| 56 | BD2802_BLINK, |
| 57 | BD2802_ON, |
| 58 | }; |
| 59 | |
| 60 | /* |
| 61 | * State '0' : 'off' |
| 62 | * State '1' : 'blink' |
| 63 | * State '2' : 'on'. |
| 64 | */ |
| 65 | struct led_state { |
| 66 | unsigned r:2; |
| 67 | unsigned g:2; |
| 68 | unsigned b:2; |
| 69 | }; |
| 70 | |
| 71 | struct bd2802_led { |
| 72 | struct bd2802_led_platform_data *pdata; |
| 73 | struct i2c_client *client; |
| 74 | struct rw_semaphore rwsem; |
| 75 | struct work_struct work; |
| 76 | |
| 77 | struct led_state led[2]; |
| 78 | |
| 79 | /* |
| 80 | * Making led_classdev as array is not recommended, because array |
| 81 | * members prevent using 'container_of' macro. So repetitive works |
| 82 | * are needed. |
| 83 | */ |
| 84 | struct led_classdev cdev_led1r; |
| 85 | struct led_classdev cdev_led1g; |
| 86 | struct led_classdev cdev_led1b; |
| 87 | struct led_classdev cdev_led2r; |
| 88 | struct led_classdev cdev_led2g; |
| 89 | struct led_classdev cdev_led2b; |
| 90 | |
| 91 | /* |
| 92 | * Advanced Configuration Function(ADF) mode: |
| 93 | * In ADF mode, user can set registers of BD2802GU directly, |
| 94 | * therefore BD2802GU doesn't enter reset state. |
| 95 | */ |
| 96 | int adf_on; |
| 97 | |
| 98 | enum led_ids led_id; |
| 99 | enum led_colors color; |
| 100 | enum led_bits state; |
Kim Kyuwon | 8792f7c | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 101 | |
| 102 | /* General attributes of RGB LEDs */ |
| 103 | int wave_pattern; |
| 104 | int rgb_current; |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | |
| 108 | /*--------------------------------------------------------------*/ |
| 109 | /* BD2802GU helper functions */ |
| 110 | /*--------------------------------------------------------------*/ |
| 111 | |
| 112 | static inline int bd2802_is_rgb_off(struct bd2802_led *led, enum led_ids id, |
| 113 | enum led_colors color) |
| 114 | { |
| 115 | switch (color) { |
| 116 | case RED: |
| 117 | return !led->led[id].r; |
| 118 | case GREEN: |
| 119 | return !led->led[id].g; |
| 120 | case BLUE: |
| 121 | return !led->led[id].b; |
| 122 | default: |
| 123 | dev_err(&led->client->dev, "%s: Invalid color\n", __func__); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static inline int bd2802_is_led_off(struct bd2802_led *led, enum led_ids id) |
| 129 | { |
| 130 | if (led->led[id].r || led->led[id].g || led->led[id].b) |
| 131 | return 0; |
| 132 | |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | static inline int bd2802_is_all_off(struct bd2802_led *led) |
| 137 | { |
| 138 | int i; |
| 139 | |
| 140 | for (i = 0; i < LED_NUM; i++) |
| 141 | if (!bd2802_is_led_off(led, i)) |
| 142 | return 0; |
| 143 | |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | static inline u8 bd2802_get_base_offset(enum led_ids id, enum led_colors color) |
| 148 | { |
| 149 | return id * BD2802_LED_OFFSET + color * BD2802_COLOR_OFFSET; |
| 150 | } |
| 151 | |
| 152 | static inline u8 bd2802_get_reg_addr(enum led_ids id, enum led_colors color, |
| 153 | u8 reg_offset) |
| 154 | { |
| 155 | return reg_offset + bd2802_get_base_offset(id, color); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /*--------------------------------------------------------------*/ |
| 160 | /* BD2802GU core functions */ |
| 161 | /*--------------------------------------------------------------*/ |
| 162 | |
| 163 | static int bd2802_write_byte(struct i2c_client *client, u8 reg, u8 val) |
| 164 | { |
| 165 | int ret = i2c_smbus_write_byte_data(client, reg, val); |
| 166 | if (ret >= 0) |
| 167 | return 0; |
| 168 | |
| 169 | dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", |
| 170 | __func__, reg, val, ret); |
| 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | static void bd2802_update_state(struct bd2802_led *led, enum led_ids id, |
| 176 | enum led_colors color, enum led_bits led_bit) |
| 177 | { |
| 178 | int i; |
| 179 | u8 value; |
| 180 | |
| 181 | for (i = 0; i < LED_NUM; i++) { |
| 182 | if (i == id) { |
| 183 | switch (color) { |
| 184 | case RED: |
| 185 | led->led[i].r = led_bit; |
| 186 | break; |
| 187 | case GREEN: |
| 188 | led->led[i].g = led_bit; |
| 189 | break; |
| 190 | case BLUE: |
| 191 | led->led[i].b = led_bit; |
| 192 | break; |
| 193 | default: |
| 194 | dev_err(&led->client->dev, |
| 195 | "%s: Invalid color\n", __func__); |
| 196 | return; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (led_bit == BD2802_BLINK || led_bit == BD2802_ON) |
| 202 | return; |
| 203 | |
| 204 | if (!bd2802_is_led_off(led, id)) |
| 205 | return; |
| 206 | |
| 207 | if (bd2802_is_all_off(led) && !led->adf_on) { |
| 208 | gpio_set_value(led->pdata->reset_gpio, 0); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * In this case, other led is turned on, and current led is turned |
| 214 | * off. So set RGB LED Control register to stop the current RGB LED |
| 215 | */ |
| 216 | value = (id == LED1) ? LED_CTL(1, 0) : LED_CTL(0, 1); |
| 217 | bd2802_write_byte(led->client, BD2802_REG_CONTROL, value); |
| 218 | } |
| 219 | |
| 220 | static void bd2802_configure(struct bd2802_led *led) |
| 221 | { |
| 222 | struct bd2802_led_platform_data *pdata = led->pdata; |
| 223 | u8 reg; |
| 224 | |
| 225 | reg = bd2802_get_reg_addr(LED1, RED, BD2802_REG_HOURSETUP); |
| 226 | bd2802_write_byte(led->client, reg, pdata->rgb_time); |
| 227 | |
| 228 | reg = bd2802_get_reg_addr(LED2, RED, BD2802_REG_HOURSETUP); |
| 229 | bd2802_write_byte(led->client, reg, pdata->rgb_time); |
| 230 | } |
| 231 | |
| 232 | static void bd2802_reset_cancel(struct bd2802_led *led) |
| 233 | { |
| 234 | gpio_set_value(led->pdata->reset_gpio, 1); |
| 235 | udelay(100); |
| 236 | bd2802_configure(led); |
| 237 | } |
| 238 | |
| 239 | static void bd2802_enable(struct bd2802_led *led, enum led_ids id) |
| 240 | { |
| 241 | enum led_ids other_led = (id == LED1) ? LED2 : LED1; |
| 242 | u8 value, other_led_on; |
| 243 | |
| 244 | other_led_on = !bd2802_is_led_off(led, other_led); |
| 245 | if (id == LED1) |
| 246 | value = LED_CTL(other_led_on, 1); |
| 247 | else |
| 248 | value = LED_CTL(1 , other_led_on); |
| 249 | |
| 250 | bd2802_write_byte(led->client, BD2802_REG_CONTROL, value); |
| 251 | } |
| 252 | |
| 253 | static void bd2802_set_on(struct bd2802_led *led, enum led_ids id, |
| 254 | enum led_colors color) |
| 255 | { |
| 256 | u8 reg; |
| 257 | |
| 258 | if (bd2802_is_all_off(led) && !led->adf_on) |
| 259 | bd2802_reset_cancel(led); |
| 260 | |
| 261 | reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT1SETUP); |
Kim Kyuwon | 8792f7c | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 262 | bd2802_write_byte(led->client, reg, led->rgb_current); |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 263 | reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT2SETUP); |
| 264 | bd2802_write_byte(led->client, reg, BD2802_CURRENT_000); |
| 265 | reg = bd2802_get_reg_addr(id, color, BD2802_REG_WAVEPATTERN); |
| 266 | bd2802_write_byte(led->client, reg, BD2802_PATTERN_FULL); |
| 267 | |
| 268 | bd2802_enable(led, id); |
| 269 | bd2802_update_state(led, id, color, BD2802_ON); |
| 270 | } |
| 271 | |
| 272 | static void bd2802_set_blink(struct bd2802_led *led, enum led_ids id, |
| 273 | enum led_colors color) |
| 274 | { |
| 275 | u8 reg; |
| 276 | |
| 277 | if (bd2802_is_all_off(led) && !led->adf_on) |
| 278 | bd2802_reset_cancel(led); |
| 279 | |
| 280 | reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT1SETUP); |
| 281 | bd2802_write_byte(led->client, reg, BD2802_CURRENT_000); |
| 282 | reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT2SETUP); |
Kim Kyuwon | 8792f7c | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 283 | bd2802_write_byte(led->client, reg, led->rgb_current); |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 284 | reg = bd2802_get_reg_addr(id, color, BD2802_REG_WAVEPATTERN); |
Kim Kyuwon | 8792f7c | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 285 | bd2802_write_byte(led->client, reg, led->wave_pattern); |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 286 | |
| 287 | bd2802_enable(led, id); |
| 288 | bd2802_update_state(led, id, color, BD2802_BLINK); |
| 289 | } |
| 290 | |
| 291 | static void bd2802_turn_on(struct bd2802_led *led, enum led_ids id, |
| 292 | enum led_colors color, enum led_bits led_bit) |
| 293 | { |
| 294 | if (led_bit == BD2802_OFF) { |
| 295 | dev_err(&led->client->dev, |
| 296 | "Only 'blink' and 'on' are allowed\n"); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | if (led_bit == BD2802_BLINK) |
| 301 | bd2802_set_blink(led, id, color); |
| 302 | else |
| 303 | bd2802_set_on(led, id, color); |
| 304 | } |
| 305 | |
| 306 | static void bd2802_turn_off(struct bd2802_led *led, enum led_ids id, |
| 307 | enum led_colors color) |
| 308 | { |
| 309 | u8 reg; |
| 310 | |
| 311 | if (bd2802_is_rgb_off(led, id, color)) |
| 312 | return; |
| 313 | |
| 314 | reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT1SETUP); |
| 315 | bd2802_write_byte(led->client, reg, BD2802_CURRENT_000); |
| 316 | reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT2SETUP); |
| 317 | bd2802_write_byte(led->client, reg, BD2802_CURRENT_000); |
| 318 | |
| 319 | bd2802_update_state(led, id, color, BD2802_OFF); |
| 320 | } |
| 321 | |
| 322 | static void bd2802_restore_state(struct bd2802_led *led) |
| 323 | { |
| 324 | int i; |
| 325 | |
| 326 | for (i = 0; i < LED_NUM; i++) { |
| 327 | if (led->led[i].r) |
| 328 | bd2802_turn_on(led, i, RED, led->led[i].r); |
| 329 | if (led->led[i].g) |
| 330 | bd2802_turn_on(led, i, GREEN, led->led[i].g); |
| 331 | if (led->led[i].b) |
| 332 | bd2802_turn_on(led, i, BLUE, led->led[i].b); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | #define BD2802_SET_REGISTER(reg_addr, reg_name) \ |
| 337 | static ssize_t bd2802_store_reg##reg_addr(struct device *dev, \ |
| 338 | struct device_attribute *attr, const char *buf, size_t count) \ |
| 339 | { \ |
| 340 | struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));\ |
| 341 | unsigned long val; \ |
| 342 | int ret; \ |
| 343 | if (!count) \ |
| 344 | return -EINVAL; \ |
| 345 | ret = strict_strtoul(buf, 16, &val); \ |
| 346 | if (ret) \ |
| 347 | return ret; \ |
| 348 | down_write(&led->rwsem); \ |
| 349 | bd2802_write_byte(led->client, reg_addr, (u8) val); \ |
| 350 | up_write(&led->rwsem); \ |
| 351 | return count; \ |
| 352 | } \ |
| 353 | static struct device_attribute bd2802_reg##reg_addr##_attr = { \ |
| 354 | .attr = {.name = reg_name, .mode = 0644, .owner = THIS_MODULE}, \ |
| 355 | .store = bd2802_store_reg##reg_addr, \ |
| 356 | }; |
| 357 | |
| 358 | BD2802_SET_REGISTER(0x00, "0x00"); |
| 359 | BD2802_SET_REGISTER(0x01, "0x01"); |
| 360 | BD2802_SET_REGISTER(0x02, "0x02"); |
| 361 | BD2802_SET_REGISTER(0x03, "0x03"); |
| 362 | BD2802_SET_REGISTER(0x04, "0x04"); |
| 363 | BD2802_SET_REGISTER(0x05, "0x05"); |
| 364 | BD2802_SET_REGISTER(0x06, "0x06"); |
| 365 | BD2802_SET_REGISTER(0x07, "0x07"); |
| 366 | BD2802_SET_REGISTER(0x08, "0x08"); |
| 367 | BD2802_SET_REGISTER(0x09, "0x09"); |
| 368 | BD2802_SET_REGISTER(0x0a, "0x0a"); |
| 369 | BD2802_SET_REGISTER(0x0b, "0x0b"); |
| 370 | BD2802_SET_REGISTER(0x0c, "0x0c"); |
| 371 | BD2802_SET_REGISTER(0x0d, "0x0d"); |
| 372 | BD2802_SET_REGISTER(0x0e, "0x0e"); |
| 373 | BD2802_SET_REGISTER(0x0f, "0x0f"); |
| 374 | BD2802_SET_REGISTER(0x10, "0x10"); |
| 375 | BD2802_SET_REGISTER(0x11, "0x11"); |
| 376 | BD2802_SET_REGISTER(0x12, "0x12"); |
| 377 | BD2802_SET_REGISTER(0x13, "0x13"); |
| 378 | BD2802_SET_REGISTER(0x14, "0x14"); |
| 379 | BD2802_SET_REGISTER(0x15, "0x15"); |
| 380 | |
| 381 | static struct device_attribute *bd2802_addr_attributes[] = { |
| 382 | &bd2802_reg0x00_attr, |
| 383 | &bd2802_reg0x01_attr, |
| 384 | &bd2802_reg0x02_attr, |
| 385 | &bd2802_reg0x03_attr, |
| 386 | &bd2802_reg0x04_attr, |
| 387 | &bd2802_reg0x05_attr, |
| 388 | &bd2802_reg0x06_attr, |
| 389 | &bd2802_reg0x07_attr, |
| 390 | &bd2802_reg0x08_attr, |
| 391 | &bd2802_reg0x09_attr, |
| 392 | &bd2802_reg0x0a_attr, |
| 393 | &bd2802_reg0x0b_attr, |
| 394 | &bd2802_reg0x0c_attr, |
| 395 | &bd2802_reg0x0d_attr, |
| 396 | &bd2802_reg0x0e_attr, |
| 397 | &bd2802_reg0x0f_attr, |
| 398 | &bd2802_reg0x10_attr, |
| 399 | &bd2802_reg0x11_attr, |
| 400 | &bd2802_reg0x12_attr, |
| 401 | &bd2802_reg0x13_attr, |
| 402 | &bd2802_reg0x14_attr, |
| 403 | &bd2802_reg0x15_attr, |
| 404 | }; |
| 405 | |
| 406 | static void bd2802_enable_adv_conf(struct bd2802_led *led) |
| 407 | { |
| 408 | int i, ret; |
| 409 | |
| 410 | for (i = 0; i < ARRAY_SIZE(bd2802_addr_attributes); i++) { |
| 411 | ret = device_create_file(&led->client->dev, |
| 412 | bd2802_addr_attributes[i]); |
| 413 | if (ret) { |
Kim Kyuwon | 8792f7c | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 414 | dev_err(&led->client->dev, "failed: sysfs file %s\n", |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 415 | bd2802_addr_attributes[i]->attr.name); |
| 416 | goto failed_remove_files; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if (bd2802_is_all_off(led)) |
| 421 | bd2802_reset_cancel(led); |
| 422 | |
| 423 | led->adf_on = 1; |
| 424 | |
| 425 | return; |
| 426 | |
| 427 | failed_remove_files: |
| 428 | for (i--; i >= 0; i--) |
| 429 | device_remove_file(&led->client->dev, |
| 430 | bd2802_addr_attributes[i]); |
| 431 | } |
| 432 | |
| 433 | static void bd2802_disable_adv_conf(struct bd2802_led *led) |
| 434 | { |
| 435 | int i; |
| 436 | |
| 437 | for (i = 0; i < ARRAY_SIZE(bd2802_addr_attributes); i++) |
| 438 | device_remove_file(&led->client->dev, |
| 439 | bd2802_addr_attributes[i]); |
| 440 | |
| 441 | if (bd2802_is_all_off(led)) |
| 442 | gpio_set_value(led->pdata->reset_gpio, 0); |
| 443 | |
| 444 | led->adf_on = 0; |
| 445 | } |
| 446 | |
| 447 | static ssize_t bd2802_show_adv_conf(struct device *dev, |
| 448 | struct device_attribute *attr, char *buf) |
| 449 | { |
| 450 | struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev)); |
| 451 | ssize_t ret; |
| 452 | |
| 453 | down_read(&led->rwsem); |
| 454 | if (led->adf_on) |
| 455 | ret = sprintf(buf, "on\n"); |
| 456 | else |
| 457 | ret = sprintf(buf, "off\n"); |
| 458 | up_read(&led->rwsem); |
| 459 | |
| 460 | return ret; |
| 461 | } |
| 462 | |
| 463 | static ssize_t bd2802_store_adv_conf(struct device *dev, |
| 464 | struct device_attribute *attr, const char *buf, size_t count) |
| 465 | { |
| 466 | struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev)); |
| 467 | |
| 468 | if (!count) |
| 469 | return -EINVAL; |
| 470 | |
| 471 | down_write(&led->rwsem); |
| 472 | if (!led->adf_on && !strncmp(buf, "on", 2)) |
| 473 | bd2802_enable_adv_conf(led); |
| 474 | else if (led->adf_on && !strncmp(buf, "off", 3)) |
| 475 | bd2802_disable_adv_conf(led); |
| 476 | up_write(&led->rwsem); |
| 477 | |
| 478 | return count; |
| 479 | } |
| 480 | |
| 481 | static struct device_attribute bd2802_adv_conf_attr = { |
| 482 | .attr = { |
| 483 | .name = "advanced_configuration", |
| 484 | .mode = 0644, |
| 485 | .owner = THIS_MODULE |
| 486 | }, |
| 487 | .show = bd2802_show_adv_conf, |
| 488 | .store = bd2802_store_adv_conf, |
| 489 | }; |
| 490 | |
Kim Kyuwon | 8792f7c | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 491 | #define BD2802_CONTROL_ATTR(attr_name, name_str) \ |
| 492 | static ssize_t bd2802_show_##attr_name(struct device *dev, \ |
| 493 | struct device_attribute *attr, char *buf) \ |
| 494 | { \ |
| 495 | struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));\ |
| 496 | ssize_t ret; \ |
| 497 | down_read(&led->rwsem); \ |
| 498 | ret = sprintf(buf, "0x%02x\n", led->attr_name); \ |
| 499 | up_read(&led->rwsem); \ |
| 500 | return ret; \ |
| 501 | } \ |
| 502 | static ssize_t bd2802_store_##attr_name(struct device *dev, \ |
| 503 | struct device_attribute *attr, const char *buf, size_t count) \ |
| 504 | { \ |
| 505 | struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));\ |
| 506 | unsigned long val; \ |
| 507 | int ret; \ |
| 508 | if (!count) \ |
| 509 | return -EINVAL; \ |
| 510 | ret = strict_strtoul(buf, 16, &val); \ |
| 511 | if (ret) \ |
| 512 | return ret; \ |
| 513 | down_write(&led->rwsem); \ |
| 514 | led->attr_name = val; \ |
| 515 | up_write(&led->rwsem); \ |
| 516 | return count; \ |
| 517 | } \ |
| 518 | static struct device_attribute bd2802_##attr_name##_attr = { \ |
| 519 | .attr = { \ |
| 520 | .name = name_str, \ |
| 521 | .mode = 0644, \ |
| 522 | .owner = THIS_MODULE \ |
| 523 | }, \ |
| 524 | .show = bd2802_show_##attr_name, \ |
| 525 | .store = bd2802_store_##attr_name, \ |
| 526 | }; |
| 527 | |
| 528 | BD2802_CONTROL_ATTR(wave_pattern, "wave_pattern"); |
| 529 | BD2802_CONTROL_ATTR(rgb_current, "rgb_current"); |
| 530 | |
| 531 | static struct device_attribute *bd2802_attributes[] = { |
| 532 | &bd2802_adv_conf_attr, |
| 533 | &bd2802_wave_pattern_attr, |
| 534 | &bd2802_rgb_current_attr, |
| 535 | }; |
| 536 | |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 537 | static void bd2802_led_work(struct work_struct *work) |
| 538 | { |
| 539 | struct bd2802_led *led = container_of(work, struct bd2802_led, work); |
| 540 | |
| 541 | if (led->state) |
| 542 | bd2802_turn_on(led, led->led_id, led->color, led->state); |
| 543 | else |
| 544 | bd2802_turn_off(led, led->led_id, led->color); |
| 545 | } |
| 546 | |
| 547 | #define BD2802_CONTROL_RGBS(name, id, clr) \ |
| 548 | static void bd2802_set_##name##_brightness(struct led_classdev *led_cdev,\ |
| 549 | enum led_brightness value) \ |
| 550 | { \ |
| 551 | struct bd2802_led *led = \ |
| 552 | container_of(led_cdev, struct bd2802_led, cdev_##name); \ |
| 553 | led->led_id = id; \ |
| 554 | led->color = clr; \ |
| 555 | if (value == LED_OFF) \ |
| 556 | led->state = BD2802_OFF; \ |
| 557 | else \ |
| 558 | led->state = BD2802_ON; \ |
| 559 | schedule_work(&led->work); \ |
| 560 | } \ |
| 561 | static int bd2802_set_##name##_blink(struct led_classdev *led_cdev, \ |
| 562 | unsigned long *delay_on, unsigned long *delay_off) \ |
| 563 | { \ |
| 564 | struct bd2802_led *led = \ |
| 565 | container_of(led_cdev, struct bd2802_led, cdev_##name); \ |
| 566 | if (*delay_on == 0 || *delay_off == 0) \ |
| 567 | return -EINVAL; \ |
| 568 | led->led_id = id; \ |
| 569 | led->color = clr; \ |
| 570 | led->state = BD2802_BLINK; \ |
| 571 | schedule_work(&led->work); \ |
| 572 | return 0; \ |
| 573 | } |
| 574 | |
| 575 | BD2802_CONTROL_RGBS(led1r, LED1, RED); |
| 576 | BD2802_CONTROL_RGBS(led1g, LED1, GREEN); |
| 577 | BD2802_CONTROL_RGBS(led1b, LED1, BLUE); |
| 578 | BD2802_CONTROL_RGBS(led2r, LED2, RED); |
| 579 | BD2802_CONTROL_RGBS(led2g, LED2, GREEN); |
| 580 | BD2802_CONTROL_RGBS(led2b, LED2, BLUE); |
| 581 | |
| 582 | static int bd2802_register_led_classdev(struct bd2802_led *led) |
| 583 | { |
| 584 | int ret; |
| 585 | |
| 586 | INIT_WORK(&led->work, bd2802_led_work); |
| 587 | |
| 588 | led->cdev_led1r.name = "led1_R"; |
| 589 | led->cdev_led1r.brightness = LED_OFF; |
| 590 | led->cdev_led1r.brightness_set = bd2802_set_led1r_brightness; |
| 591 | led->cdev_led1r.blink_set = bd2802_set_led1r_blink; |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 592 | |
| 593 | ret = led_classdev_register(&led->client->dev, &led->cdev_led1r); |
| 594 | if (ret < 0) { |
| 595 | dev_err(&led->client->dev, "couldn't register LED %s\n", |
| 596 | led->cdev_led1r.name); |
| 597 | goto failed_unregister_led1_R; |
| 598 | } |
| 599 | |
| 600 | led->cdev_led1g.name = "led1_G"; |
| 601 | led->cdev_led1g.brightness = LED_OFF; |
| 602 | led->cdev_led1g.brightness_set = bd2802_set_led1g_brightness; |
| 603 | led->cdev_led1g.blink_set = bd2802_set_led1g_blink; |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 604 | |
| 605 | ret = led_classdev_register(&led->client->dev, &led->cdev_led1g); |
| 606 | if (ret < 0) { |
| 607 | dev_err(&led->client->dev, "couldn't register LED %s\n", |
| 608 | led->cdev_led1g.name); |
| 609 | goto failed_unregister_led1_G; |
| 610 | } |
| 611 | |
| 612 | led->cdev_led1b.name = "led1_B"; |
| 613 | led->cdev_led1b.brightness = LED_OFF; |
| 614 | led->cdev_led1b.brightness_set = bd2802_set_led1b_brightness; |
| 615 | led->cdev_led1b.blink_set = bd2802_set_led1b_blink; |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 616 | |
| 617 | ret = led_classdev_register(&led->client->dev, &led->cdev_led1b); |
| 618 | if (ret < 0) { |
| 619 | dev_err(&led->client->dev, "couldn't register LED %s\n", |
| 620 | led->cdev_led1b.name); |
| 621 | goto failed_unregister_led1_B; |
| 622 | } |
| 623 | |
| 624 | led->cdev_led2r.name = "led2_R"; |
| 625 | led->cdev_led2r.brightness = LED_OFF; |
| 626 | led->cdev_led2r.brightness_set = bd2802_set_led2r_brightness; |
| 627 | led->cdev_led2r.blink_set = bd2802_set_led2r_blink; |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 628 | |
| 629 | ret = led_classdev_register(&led->client->dev, &led->cdev_led2r); |
| 630 | if (ret < 0) { |
| 631 | dev_err(&led->client->dev, "couldn't register LED %s\n", |
| 632 | led->cdev_led2r.name); |
| 633 | goto failed_unregister_led2_R; |
| 634 | } |
| 635 | |
| 636 | led->cdev_led2g.name = "led2_G"; |
| 637 | led->cdev_led2g.brightness = LED_OFF; |
| 638 | led->cdev_led2g.brightness_set = bd2802_set_led2g_brightness; |
| 639 | led->cdev_led2g.blink_set = bd2802_set_led2g_blink; |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 640 | |
| 641 | ret = led_classdev_register(&led->client->dev, &led->cdev_led2g); |
| 642 | if (ret < 0) { |
| 643 | dev_err(&led->client->dev, "couldn't register LED %s\n", |
| 644 | led->cdev_led2g.name); |
| 645 | goto failed_unregister_led2_G; |
| 646 | } |
| 647 | |
| 648 | led->cdev_led2b.name = "led2_B"; |
| 649 | led->cdev_led2b.brightness = LED_OFF; |
| 650 | led->cdev_led2b.brightness_set = bd2802_set_led2b_brightness; |
| 651 | led->cdev_led2b.blink_set = bd2802_set_led2b_blink; |
| 652 | led->cdev_led2b.flags |= LED_CORE_SUSPENDRESUME; |
| 653 | |
| 654 | ret = led_classdev_register(&led->client->dev, &led->cdev_led2b); |
| 655 | if (ret < 0) { |
| 656 | dev_err(&led->client->dev, "couldn't register LED %s\n", |
| 657 | led->cdev_led2b.name); |
| 658 | goto failed_unregister_led2_B; |
| 659 | } |
| 660 | |
| 661 | return 0; |
| 662 | |
| 663 | failed_unregister_led2_B: |
| 664 | led_classdev_unregister(&led->cdev_led2g); |
| 665 | failed_unregister_led2_G: |
| 666 | led_classdev_unregister(&led->cdev_led2r); |
| 667 | failed_unregister_led2_R: |
| 668 | led_classdev_unregister(&led->cdev_led1b); |
| 669 | failed_unregister_led1_B: |
| 670 | led_classdev_unregister(&led->cdev_led1g); |
| 671 | failed_unregister_led1_G: |
| 672 | led_classdev_unregister(&led->cdev_led1r); |
| 673 | failed_unregister_led1_R: |
| 674 | |
| 675 | return ret; |
| 676 | } |
| 677 | |
| 678 | static void bd2802_unregister_led_classdev(struct bd2802_led *led) |
| 679 | { |
| 680 | cancel_work_sync(&led->work); |
| 681 | led_classdev_unregister(&led->cdev_led1r); |
| 682 | } |
| 683 | |
| 684 | static int __devinit bd2802_probe(struct i2c_client *client, |
| 685 | const struct i2c_device_id *id) |
| 686 | { |
| 687 | struct bd2802_led *led; |
| 688 | struct bd2802_led_platform_data *pdata; |
Kim Kyuwon | 8792f7c | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 689 | int ret, i; |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 690 | |
| 691 | led = kzalloc(sizeof(struct bd2802_led), GFP_KERNEL); |
| 692 | if (!led) { |
| 693 | dev_err(&client->dev, "failed to allocate driver data\n"); |
| 694 | return -ENOMEM; |
| 695 | } |
| 696 | |
| 697 | led->client = client; |
| 698 | pdata = led->pdata = client->dev.platform_data; |
| 699 | i2c_set_clientdata(client, led); |
| 700 | |
| 701 | /* Configure RESET GPIO (L: RESET, H: RESET cancel) */ |
| 702 | gpio_request(pdata->reset_gpio, "RGB_RESETB"); |
| 703 | gpio_direction_output(pdata->reset_gpio, 1); |
| 704 | |
| 705 | /* Tacss = min 0.1ms */ |
| 706 | udelay(100); |
| 707 | |
| 708 | /* Detect BD2802GU */ |
| 709 | ret = bd2802_write_byte(client, BD2802_REG_CLKSETUP, 0x00); |
| 710 | if (ret < 0) { |
| 711 | dev_err(&client->dev, "failed to detect device\n"); |
| 712 | goto failed_free; |
| 713 | } else |
| 714 | dev_info(&client->dev, "return 0x%02x\n", ret); |
| 715 | |
| 716 | /* To save the power, reset BD2802 after detecting */ |
| 717 | gpio_set_value(led->pdata->reset_gpio, 0); |
| 718 | |
Kim Kyuwon | 8792f7c | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 719 | /* Default attributes */ |
| 720 | led->wave_pattern = BD2802_PATTERN_HALF; |
| 721 | led->rgb_current = BD2802_CURRENT_032; |
| 722 | |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 723 | init_rwsem(&led->rwsem); |
| 724 | |
Kim Kyuwon | 8792f7c | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 725 | for (i = 0; i < ARRAY_SIZE(bd2802_attributes); i++) { |
| 726 | ret = device_create_file(&led->client->dev, |
| 727 | bd2802_attributes[i]); |
| 728 | if (ret) { |
| 729 | dev_err(&led->client->dev, "failed: sysfs file %s\n", |
| 730 | bd2802_attributes[i]->attr.name); |
| 731 | goto failed_unregister_dev_file; |
| 732 | } |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | ret = bd2802_register_led_classdev(led); |
| 736 | if (ret < 0) |
| 737 | goto failed_unregister_dev_file; |
| 738 | |
| 739 | return 0; |
| 740 | |
| 741 | failed_unregister_dev_file: |
Kim Kyuwon | 8792f7c | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 742 | for (i--; i >= 0; i--) |
| 743 | device_remove_file(&led->client->dev, bd2802_attributes[i]); |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 744 | failed_free: |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 745 | kfree(led); |
| 746 | |
| 747 | return ret; |
| 748 | } |
| 749 | |
| 750 | static int __exit bd2802_remove(struct i2c_client *client) |
| 751 | { |
| 752 | struct bd2802_led *led = i2c_get_clientdata(client); |
Kim Kyuwon | 8792f7c | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 753 | int i; |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 754 | |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 755 | gpio_set_value(led->pdata->reset_gpio, 0); |
Kim Kyuwon | 8792f7c | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 756 | bd2802_unregister_led_classdev(led); |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 757 | if (led->adf_on) |
| 758 | bd2802_disable_adv_conf(led); |
Kim Kyuwon | 8792f7c | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 759 | for (i = 0; i < ARRAY_SIZE(bd2802_attributes); i++) |
| 760 | device_remove_file(&led->client->dev, bd2802_attributes[i]); |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 761 | kfree(led); |
| 762 | |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | static int bd2802_suspend(struct i2c_client *client, pm_message_t mesg) |
| 767 | { |
| 768 | struct bd2802_led *led = i2c_get_clientdata(client); |
| 769 | |
| 770 | gpio_set_value(led->pdata->reset_gpio, 0); |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | static int bd2802_resume(struct i2c_client *client) |
| 776 | { |
| 777 | struct bd2802_led *led = i2c_get_clientdata(client); |
| 778 | |
| 779 | if (!bd2802_is_all_off(led) || led->adf_on) { |
Kim Kyuwon | b838901 | 2009-06-10 12:48:48 -0700 | [diff] [blame] | 780 | bd2802_reset_cancel(led); |
Kim Kyuwon | 0b56129 | 2009-03-04 11:59:29 -0800 | [diff] [blame] | 781 | bd2802_restore_state(led); |
| 782 | } |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static const struct i2c_device_id bd2802_id[] = { |
| 788 | { "BD2802", 0 }, |
| 789 | { } |
| 790 | }; |
| 791 | MODULE_DEVICE_TABLE(i2c, bd2802_id); |
| 792 | |
| 793 | static struct i2c_driver bd2802_i2c_driver = { |
| 794 | .driver = { |
| 795 | .name = "BD2802", |
| 796 | }, |
| 797 | .probe = bd2802_probe, |
| 798 | .remove = __exit_p(bd2802_remove), |
| 799 | .suspend = bd2802_suspend, |
| 800 | .resume = bd2802_resume, |
| 801 | .id_table = bd2802_id, |
| 802 | }; |
| 803 | |
| 804 | static int __init bd2802_init(void) |
| 805 | { |
| 806 | return i2c_add_driver(&bd2802_i2c_driver); |
| 807 | } |
| 808 | module_init(bd2802_init); |
| 809 | |
| 810 | static void __exit bd2802_exit(void) |
| 811 | { |
| 812 | i2c_del_driver(&bd2802_i2c_driver); |
| 813 | } |
| 814 | module_exit(bd2802_exit); |
| 815 | |
| 816 | MODULE_AUTHOR("Kim Kyuwon <q1.kim@samsung.com>"); |
| 817 | MODULE_DESCRIPTION("BD2802 LED driver"); |
Kim Kyuwon | 1b18cf4 | 2009-06-10 12:48:50 -0700 | [diff] [blame] | 818 | MODULE_LICENSE("GPL v2"); |