Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * leds-lm3533.c -- LM3533 LED driver |
| 3 | * |
| 4 | * Copyright (C) 2011-2012 Texas Instruments |
| 5 | * |
| 6 | * Author: Johan Hovold <jhovold@gmail.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 15 | #include <linux/leds.h> |
| 16 | #include <linux/mfd/core.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/slab.h> |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 20 | |
| 21 | #include <linux/mfd/lm3533.h> |
| 22 | |
| 23 | |
| 24 | #define LM3533_LVCTRLBANK_MIN 2 |
| 25 | #define LM3533_LVCTRLBANK_MAX 5 |
| 26 | #define LM3533_LVCTRLBANK_COUNT 4 |
| 27 | #define LM3533_RISEFALLTIME_MAX 7 |
| 28 | #define LM3533_ALS_CHANNEL_LV_MIN 1 |
| 29 | #define LM3533_ALS_CHANNEL_LV_MAX 2 |
| 30 | |
| 31 | #define LM3533_REG_CTRLBANK_BCONF_BASE 0x1b |
| 32 | #define LM3533_REG_PATTERN_ENABLE 0x28 |
| 33 | #define LM3533_REG_PATTERN_LOW_TIME_BASE 0x71 |
| 34 | #define LM3533_REG_PATTERN_HIGH_TIME_BASE 0x72 |
| 35 | #define LM3533_REG_PATTERN_RISETIME_BASE 0x74 |
| 36 | #define LM3533_REG_PATTERN_FALLTIME_BASE 0x75 |
| 37 | |
| 38 | #define LM3533_REG_PATTERN_STEP 0x10 |
| 39 | |
| 40 | #define LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK 0x04 |
| 41 | #define LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK 0x02 |
| 42 | #define LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK 0x01 |
| 43 | |
| 44 | #define LM3533_LED_FLAG_PATTERN_ENABLE 1 |
| 45 | |
| 46 | |
| 47 | struct lm3533_led { |
| 48 | struct lm3533 *lm3533; |
| 49 | struct lm3533_ctrlbank cb; |
| 50 | struct led_classdev cdev; |
| 51 | int id; |
| 52 | |
| 53 | struct mutex mutex; |
| 54 | unsigned long flags; |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | |
| 58 | static inline struct lm3533_led *to_lm3533_led(struct led_classdev *cdev) |
| 59 | { |
| 60 | return container_of(cdev, struct lm3533_led, cdev); |
| 61 | } |
| 62 | |
| 63 | static inline int lm3533_led_get_ctrlbank_id(struct lm3533_led *led) |
| 64 | { |
| 65 | return led->id + 2; |
| 66 | } |
| 67 | |
| 68 | static inline u8 lm3533_led_get_lv_reg(struct lm3533_led *led, u8 base) |
| 69 | { |
| 70 | return base + led->id; |
| 71 | } |
| 72 | |
| 73 | static inline u8 lm3533_led_get_pattern(struct lm3533_led *led) |
| 74 | { |
| 75 | return led->id; |
| 76 | } |
| 77 | |
| 78 | static inline u8 lm3533_led_get_pattern_reg(struct lm3533_led *led, |
| 79 | u8 base) |
| 80 | { |
| 81 | return base + lm3533_led_get_pattern(led) * LM3533_REG_PATTERN_STEP; |
| 82 | } |
| 83 | |
| 84 | static int lm3533_led_pattern_enable(struct lm3533_led *led, int enable) |
| 85 | { |
| 86 | u8 mask; |
| 87 | u8 val; |
| 88 | int pattern; |
| 89 | int state; |
| 90 | int ret = 0; |
| 91 | |
| 92 | dev_dbg(led->cdev.dev, "%s - %d\n", __func__, enable); |
| 93 | |
| 94 | mutex_lock(&led->mutex); |
| 95 | |
| 96 | state = test_bit(LM3533_LED_FLAG_PATTERN_ENABLE, &led->flags); |
| 97 | if ((enable && state) || (!enable && !state)) |
| 98 | goto out; |
| 99 | |
| 100 | pattern = lm3533_led_get_pattern(led); |
| 101 | mask = 1 << (2 * pattern); |
| 102 | |
| 103 | if (enable) |
| 104 | val = mask; |
| 105 | else |
| 106 | val = 0; |
| 107 | |
| 108 | ret = lm3533_update(led->lm3533, LM3533_REG_PATTERN_ENABLE, val, mask); |
| 109 | if (ret) { |
| 110 | dev_err(led->cdev.dev, "failed to enable pattern %d (%d)\n", |
| 111 | pattern, enable); |
| 112 | goto out; |
| 113 | } |
| 114 | |
| 115 | __change_bit(LM3533_LED_FLAG_PATTERN_ENABLE, &led->flags); |
| 116 | out: |
| 117 | mutex_unlock(&led->mutex); |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
Andrew Lunn | a478851 | 2015-08-20 12:02:17 +0200 | [diff] [blame] | 122 | static int lm3533_led_set(struct led_classdev *cdev, |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 123 | enum led_brightness value) |
| 124 | { |
| 125 | struct lm3533_led *led = to_lm3533_led(cdev); |
| 126 | |
| 127 | dev_dbg(led->cdev.dev, "%s - %d\n", __func__, value); |
| 128 | |
Andrew Lunn | a478851 | 2015-08-20 12:02:17 +0200 | [diff] [blame] | 129 | if (value == 0) |
| 130 | lm3533_led_pattern_enable(led, 0); /* disable blink */ |
| 131 | |
| 132 | return lm3533_ctrlbank_set_brightness(&led->cb, value); |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static enum led_brightness lm3533_led_get(struct led_classdev *cdev) |
| 136 | { |
| 137 | struct lm3533_led *led = to_lm3533_led(cdev); |
| 138 | u8 val; |
| 139 | int ret; |
| 140 | |
| 141 | ret = lm3533_ctrlbank_get_brightness(&led->cb, &val); |
| 142 | if (ret) |
| 143 | return ret; |
| 144 | |
| 145 | dev_dbg(led->cdev.dev, "%s - %u\n", __func__, val); |
| 146 | |
| 147 | return val; |
| 148 | } |
| 149 | |
| 150 | /* Pattern generator defines (delays in us). */ |
| 151 | #define LM3533_LED_DELAY1_VMIN 0x00 |
| 152 | #define LM3533_LED_DELAY2_VMIN 0x3d |
| 153 | #define LM3533_LED_DELAY3_VMIN 0x80 |
| 154 | |
| 155 | #define LM3533_LED_DELAY1_VMAX (LM3533_LED_DELAY2_VMIN - 1) |
| 156 | #define LM3533_LED_DELAY2_VMAX (LM3533_LED_DELAY3_VMIN - 1) |
| 157 | #define LM3533_LED_DELAY3_VMAX 0xff |
| 158 | |
| 159 | #define LM3533_LED_DELAY1_TMIN 16384U |
| 160 | #define LM3533_LED_DELAY2_TMIN 1130496U |
| 161 | #define LM3533_LED_DELAY3_TMIN 10305536U |
| 162 | |
| 163 | #define LM3533_LED_DELAY1_TMAX 999424U |
| 164 | #define LM3533_LED_DELAY2_TMAX 9781248U |
| 165 | #define LM3533_LED_DELAY3_TMAX 76890112U |
| 166 | |
| 167 | /* t_step = (t_max - t_min) / (v_max - v_min) */ |
| 168 | #define LM3533_LED_DELAY1_TSTEP 16384 |
| 169 | #define LM3533_LED_DELAY2_TSTEP 131072 |
| 170 | #define LM3533_LED_DELAY3_TSTEP 524288 |
| 171 | |
| 172 | /* Delay limits for hardware accelerated blinking (in ms). */ |
| 173 | #define LM3533_LED_DELAY_ON_MAX \ |
| 174 | ((LM3533_LED_DELAY2_TMAX + LM3533_LED_DELAY2_TSTEP / 2) / 1000) |
| 175 | #define LM3533_LED_DELAY_OFF_MAX \ |
| 176 | ((LM3533_LED_DELAY3_TMAX + LM3533_LED_DELAY3_TSTEP / 2) / 1000) |
| 177 | |
| 178 | /* |
| 179 | * Returns linear map of *t from [t_min,t_max] to [v_min,v_max] with a step |
| 180 | * size of t_step, where |
| 181 | * |
| 182 | * t_step = (t_max - t_min) / (v_max - v_min) |
| 183 | * |
| 184 | * and updates *t to reflect the mapped value. |
| 185 | */ |
| 186 | static u8 time_to_val(unsigned *t, unsigned t_min, unsigned t_step, |
| 187 | u8 v_min, u8 v_max) |
| 188 | { |
| 189 | unsigned val; |
| 190 | |
| 191 | val = (*t + t_step / 2 - t_min) / t_step + v_min; |
| 192 | |
| 193 | *t = t_step * (val - v_min) + t_min; |
| 194 | |
| 195 | return (u8)val; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Returns time code corresponding to *delay (in ms) and updates *delay to |
| 200 | * reflect actual hardware delay. |
| 201 | * |
| 202 | * Hardware supports 256 discrete delay times, divided into three groups with |
| 203 | * the following ranges and step-sizes: |
| 204 | * |
| 205 | * [ 16, 999] [0x00, 0x3e] step 16 ms |
| 206 | * [ 1130, 9781] [0x3d, 0x7f] step 131 ms |
| 207 | * [10306, 76890] [0x80, 0xff] step 524 ms |
| 208 | * |
| 209 | * Note that delay group 3 is only available for delay_off. |
| 210 | */ |
| 211 | static u8 lm3533_led_get_hw_delay(unsigned *delay) |
| 212 | { |
| 213 | unsigned t; |
| 214 | u8 val; |
| 215 | |
| 216 | t = *delay * 1000; |
| 217 | |
| 218 | if (t >= (LM3533_LED_DELAY2_TMAX + LM3533_LED_DELAY3_TMIN) / 2) { |
| 219 | t = clamp(t, LM3533_LED_DELAY3_TMIN, LM3533_LED_DELAY3_TMAX); |
| 220 | val = time_to_val(&t, LM3533_LED_DELAY3_TMIN, |
| 221 | LM3533_LED_DELAY3_TSTEP, |
| 222 | LM3533_LED_DELAY3_VMIN, |
| 223 | LM3533_LED_DELAY3_VMAX); |
| 224 | } else if (t >= (LM3533_LED_DELAY1_TMAX + LM3533_LED_DELAY2_TMIN) / 2) { |
| 225 | t = clamp(t, LM3533_LED_DELAY2_TMIN, LM3533_LED_DELAY2_TMAX); |
| 226 | val = time_to_val(&t, LM3533_LED_DELAY2_TMIN, |
| 227 | LM3533_LED_DELAY2_TSTEP, |
| 228 | LM3533_LED_DELAY2_VMIN, |
| 229 | LM3533_LED_DELAY2_VMAX); |
| 230 | } else { |
| 231 | t = clamp(t, LM3533_LED_DELAY1_TMIN, LM3533_LED_DELAY1_TMAX); |
| 232 | val = time_to_val(&t, LM3533_LED_DELAY1_TMIN, |
| 233 | LM3533_LED_DELAY1_TSTEP, |
| 234 | LM3533_LED_DELAY1_VMIN, |
| 235 | LM3533_LED_DELAY1_VMAX); |
| 236 | } |
| 237 | |
| 238 | *delay = (t + 500) / 1000; |
| 239 | |
| 240 | return val; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Set delay register base to *delay (in ms) and update *delay to reflect |
| 245 | * actual hardware delay used. |
| 246 | */ |
| 247 | static u8 lm3533_led_delay_set(struct lm3533_led *led, u8 base, |
| 248 | unsigned long *delay) |
| 249 | { |
| 250 | unsigned t; |
| 251 | u8 val; |
| 252 | u8 reg; |
| 253 | int ret; |
| 254 | |
| 255 | t = (unsigned)*delay; |
| 256 | |
| 257 | /* Delay group 3 is only available for low time (delay off). */ |
| 258 | if (base != LM3533_REG_PATTERN_LOW_TIME_BASE) |
| 259 | t = min(t, LM3533_LED_DELAY2_TMAX / 1000); |
| 260 | |
| 261 | val = lm3533_led_get_hw_delay(&t); |
| 262 | |
| 263 | dev_dbg(led->cdev.dev, "%s - %lu: %u (0x%02x)\n", __func__, |
| 264 | *delay, t, val); |
| 265 | reg = lm3533_led_get_pattern_reg(led, base); |
| 266 | ret = lm3533_write(led->lm3533, reg, val); |
| 267 | if (ret) |
| 268 | dev_err(led->cdev.dev, "failed to set delay (%02x)\n", reg); |
| 269 | |
| 270 | *delay = t; |
| 271 | |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | static int lm3533_led_delay_on_set(struct lm3533_led *led, unsigned long *t) |
| 276 | { |
| 277 | return lm3533_led_delay_set(led, LM3533_REG_PATTERN_HIGH_TIME_BASE, t); |
| 278 | } |
| 279 | |
| 280 | static int lm3533_led_delay_off_set(struct lm3533_led *led, unsigned long *t) |
| 281 | { |
| 282 | return lm3533_led_delay_set(led, LM3533_REG_PATTERN_LOW_TIME_BASE, t); |
| 283 | } |
| 284 | |
| 285 | static int lm3533_led_blink_set(struct led_classdev *cdev, |
| 286 | unsigned long *delay_on, |
| 287 | unsigned long *delay_off) |
| 288 | { |
| 289 | struct lm3533_led *led = to_lm3533_led(cdev); |
| 290 | int ret; |
| 291 | |
| 292 | dev_dbg(led->cdev.dev, "%s - on = %lu, off = %lu\n", __func__, |
| 293 | *delay_on, *delay_off); |
| 294 | |
| 295 | if (*delay_on > LM3533_LED_DELAY_ON_MAX || |
| 296 | *delay_off > LM3533_LED_DELAY_OFF_MAX) |
| 297 | return -EINVAL; |
| 298 | |
| 299 | if (*delay_on == 0 && *delay_off == 0) { |
| 300 | *delay_on = 500; |
| 301 | *delay_off = 500; |
| 302 | } |
| 303 | |
| 304 | ret = lm3533_led_delay_on_set(led, delay_on); |
| 305 | if (ret) |
| 306 | return ret; |
| 307 | |
| 308 | ret = lm3533_led_delay_off_set(led, delay_off); |
| 309 | if (ret) |
| 310 | return ret; |
| 311 | |
| 312 | return lm3533_led_pattern_enable(led, 1); |
| 313 | } |
| 314 | |
| 315 | static ssize_t show_id(struct device *dev, |
| 316 | struct device_attribute *attr, char *buf) |
| 317 | { |
| 318 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 319 | struct lm3533_led *led = to_lm3533_led(led_cdev); |
| 320 | |
| 321 | return scnprintf(buf, PAGE_SIZE, "%d\n", led->id); |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Pattern generator rise/fall times: |
| 326 | * |
| 327 | * 0 - 2048 us (default) |
| 328 | * 1 - 262 ms |
| 329 | * 2 - 524 ms |
| 330 | * 3 - 1.049 s |
| 331 | * 4 - 2.097 s |
| 332 | * 5 - 4.194 s |
| 333 | * 6 - 8.389 s |
| 334 | * 7 - 16.78 s |
| 335 | */ |
| 336 | static ssize_t show_risefalltime(struct device *dev, |
| 337 | struct device_attribute *attr, |
| 338 | char *buf, u8 base) |
| 339 | { |
| 340 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 341 | struct lm3533_led *led = to_lm3533_led(led_cdev); |
| 342 | ssize_t ret; |
| 343 | u8 reg; |
| 344 | u8 val; |
| 345 | |
| 346 | reg = lm3533_led_get_pattern_reg(led, base); |
| 347 | ret = lm3533_read(led->lm3533, reg, &val); |
| 348 | if (ret) |
| 349 | return ret; |
| 350 | |
| 351 | return scnprintf(buf, PAGE_SIZE, "%x\n", val); |
| 352 | } |
| 353 | |
| 354 | static ssize_t show_risetime(struct device *dev, |
| 355 | struct device_attribute *attr, char *buf) |
| 356 | { |
| 357 | return show_risefalltime(dev, attr, buf, |
| 358 | LM3533_REG_PATTERN_RISETIME_BASE); |
| 359 | } |
| 360 | |
| 361 | static ssize_t show_falltime(struct device *dev, |
| 362 | struct device_attribute *attr, char *buf) |
| 363 | { |
| 364 | return show_risefalltime(dev, attr, buf, |
| 365 | LM3533_REG_PATTERN_FALLTIME_BASE); |
| 366 | } |
| 367 | |
| 368 | static ssize_t store_risefalltime(struct device *dev, |
| 369 | struct device_attribute *attr, |
| 370 | const char *buf, size_t len, u8 base) |
| 371 | { |
| 372 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 373 | struct lm3533_led *led = to_lm3533_led(led_cdev); |
| 374 | u8 val; |
| 375 | u8 reg; |
| 376 | int ret; |
| 377 | |
| 378 | if (kstrtou8(buf, 0, &val) || val > LM3533_RISEFALLTIME_MAX) |
| 379 | return -EINVAL; |
| 380 | |
| 381 | reg = lm3533_led_get_pattern_reg(led, base); |
| 382 | ret = lm3533_write(led->lm3533, reg, val); |
| 383 | if (ret) |
| 384 | return ret; |
| 385 | |
| 386 | return len; |
| 387 | } |
| 388 | |
| 389 | static ssize_t store_risetime(struct device *dev, |
| 390 | struct device_attribute *attr, |
| 391 | const char *buf, size_t len) |
| 392 | { |
| 393 | return store_risefalltime(dev, attr, buf, len, |
| 394 | LM3533_REG_PATTERN_RISETIME_BASE); |
| 395 | } |
| 396 | |
| 397 | static ssize_t store_falltime(struct device *dev, |
| 398 | struct device_attribute *attr, |
| 399 | const char *buf, size_t len) |
| 400 | { |
| 401 | return store_risefalltime(dev, attr, buf, len, |
| 402 | LM3533_REG_PATTERN_FALLTIME_BASE); |
| 403 | } |
| 404 | |
| 405 | static ssize_t show_als_channel(struct device *dev, |
| 406 | struct device_attribute *attr, char *buf) |
| 407 | { |
| 408 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 409 | struct lm3533_led *led = to_lm3533_led(led_cdev); |
| 410 | unsigned channel; |
| 411 | u8 reg; |
| 412 | u8 val; |
| 413 | int ret; |
| 414 | |
| 415 | reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE); |
| 416 | ret = lm3533_read(led->lm3533, reg, &val); |
| 417 | if (ret) |
| 418 | return ret; |
| 419 | |
| 420 | channel = (val & LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK) + 1; |
| 421 | |
| 422 | return scnprintf(buf, PAGE_SIZE, "%u\n", channel); |
| 423 | } |
| 424 | |
| 425 | static ssize_t store_als_channel(struct device *dev, |
| 426 | struct device_attribute *attr, |
| 427 | const char *buf, size_t len) |
| 428 | { |
| 429 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 430 | struct lm3533_led *led = to_lm3533_led(led_cdev); |
| 431 | unsigned channel; |
| 432 | u8 reg; |
| 433 | u8 val; |
| 434 | u8 mask; |
| 435 | int ret; |
| 436 | |
| 437 | if (kstrtouint(buf, 0, &channel)) |
| 438 | return -EINVAL; |
| 439 | |
| 440 | if (channel < LM3533_ALS_CHANNEL_LV_MIN || |
| 441 | channel > LM3533_ALS_CHANNEL_LV_MAX) |
| 442 | return -EINVAL; |
| 443 | |
| 444 | reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE); |
| 445 | mask = LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK; |
| 446 | val = channel - 1; |
| 447 | |
| 448 | ret = lm3533_update(led->lm3533, reg, val, mask); |
| 449 | if (ret) |
| 450 | return ret; |
| 451 | |
| 452 | return len; |
| 453 | } |
| 454 | |
| 455 | static ssize_t show_als_en(struct device *dev, |
| 456 | struct device_attribute *attr, char *buf) |
| 457 | { |
| 458 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 459 | struct lm3533_led *led = to_lm3533_led(led_cdev); |
| 460 | bool enable; |
| 461 | u8 reg; |
| 462 | u8 val; |
| 463 | int ret; |
| 464 | |
| 465 | reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE); |
| 466 | ret = lm3533_read(led->lm3533, reg, &val); |
| 467 | if (ret) |
| 468 | return ret; |
| 469 | |
| 470 | enable = val & LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK; |
| 471 | |
| 472 | return scnprintf(buf, PAGE_SIZE, "%d\n", enable); |
| 473 | } |
| 474 | |
| 475 | static ssize_t store_als_en(struct device *dev, |
| 476 | struct device_attribute *attr, |
| 477 | const char *buf, size_t len) |
| 478 | { |
| 479 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 480 | struct lm3533_led *led = to_lm3533_led(led_cdev); |
| 481 | unsigned enable; |
| 482 | u8 reg; |
| 483 | u8 mask; |
| 484 | u8 val; |
| 485 | int ret; |
| 486 | |
| 487 | if (kstrtouint(buf, 0, &enable)) |
| 488 | return -EINVAL; |
| 489 | |
| 490 | reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE); |
| 491 | mask = LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK; |
| 492 | |
| 493 | if (enable) |
| 494 | val = mask; |
| 495 | else |
| 496 | val = 0; |
| 497 | |
| 498 | ret = lm3533_update(led->lm3533, reg, val, mask); |
| 499 | if (ret) |
| 500 | return ret; |
| 501 | |
| 502 | return len; |
| 503 | } |
| 504 | |
| 505 | static ssize_t show_linear(struct device *dev, |
| 506 | struct device_attribute *attr, char *buf) |
| 507 | { |
| 508 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 509 | struct lm3533_led *led = to_lm3533_led(led_cdev); |
| 510 | u8 reg; |
| 511 | u8 val; |
| 512 | int linear; |
| 513 | int ret; |
| 514 | |
| 515 | reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE); |
| 516 | ret = lm3533_read(led->lm3533, reg, &val); |
| 517 | if (ret) |
| 518 | return ret; |
| 519 | |
| 520 | if (val & LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK) |
| 521 | linear = 1; |
| 522 | else |
| 523 | linear = 0; |
| 524 | |
| 525 | return scnprintf(buf, PAGE_SIZE, "%x\n", linear); |
| 526 | } |
| 527 | |
| 528 | static ssize_t store_linear(struct device *dev, |
| 529 | struct device_attribute *attr, |
| 530 | const char *buf, size_t len) |
| 531 | { |
| 532 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 533 | struct lm3533_led *led = to_lm3533_led(led_cdev); |
| 534 | unsigned long linear; |
| 535 | u8 reg; |
| 536 | u8 mask; |
| 537 | u8 val; |
| 538 | int ret; |
| 539 | |
| 540 | if (kstrtoul(buf, 0, &linear)) |
| 541 | return -EINVAL; |
| 542 | |
| 543 | reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE); |
| 544 | mask = LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK; |
| 545 | |
| 546 | if (linear) |
| 547 | val = mask; |
| 548 | else |
| 549 | val = 0; |
| 550 | |
| 551 | ret = lm3533_update(led->lm3533, reg, val, mask); |
| 552 | if (ret) |
| 553 | return ret; |
| 554 | |
| 555 | return len; |
| 556 | } |
| 557 | |
| 558 | static ssize_t show_pwm(struct device *dev, |
| 559 | struct device_attribute *attr, |
| 560 | char *buf) |
| 561 | { |
| 562 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 563 | struct lm3533_led *led = to_lm3533_led(led_cdev); |
| 564 | u8 val; |
| 565 | int ret; |
| 566 | |
| 567 | ret = lm3533_ctrlbank_get_pwm(&led->cb, &val); |
| 568 | if (ret) |
| 569 | return ret; |
| 570 | |
| 571 | return scnprintf(buf, PAGE_SIZE, "%u\n", val); |
| 572 | } |
| 573 | |
| 574 | static ssize_t store_pwm(struct device *dev, |
| 575 | struct device_attribute *attr, |
| 576 | const char *buf, size_t len) |
| 577 | { |
| 578 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 579 | struct lm3533_led *led = to_lm3533_led(led_cdev); |
| 580 | u8 val; |
| 581 | int ret; |
| 582 | |
| 583 | if (kstrtou8(buf, 0, &val)) |
| 584 | return -EINVAL; |
| 585 | |
| 586 | ret = lm3533_ctrlbank_set_pwm(&led->cb, val); |
| 587 | if (ret) |
| 588 | return ret; |
| 589 | |
| 590 | return len; |
| 591 | } |
| 592 | |
| 593 | static LM3533_ATTR_RW(als_channel); |
| 594 | static LM3533_ATTR_RW(als_en); |
| 595 | static LM3533_ATTR_RW(falltime); |
| 596 | static LM3533_ATTR_RO(id); |
| 597 | static LM3533_ATTR_RW(linear); |
| 598 | static LM3533_ATTR_RW(pwm); |
| 599 | static LM3533_ATTR_RW(risetime); |
| 600 | |
| 601 | static struct attribute *lm3533_led_attributes[] = { |
| 602 | &dev_attr_als_channel.attr, |
| 603 | &dev_attr_als_en.attr, |
| 604 | &dev_attr_falltime.attr, |
| 605 | &dev_attr_id.attr, |
| 606 | &dev_attr_linear.attr, |
| 607 | &dev_attr_pwm.attr, |
| 608 | &dev_attr_risetime.attr, |
| 609 | NULL, |
| 610 | }; |
| 611 | |
| 612 | static umode_t lm3533_led_attr_is_visible(struct kobject *kobj, |
| 613 | struct attribute *attr, int n) |
| 614 | { |
| 615 | struct device *dev = container_of(kobj, struct device, kobj); |
| 616 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 617 | struct lm3533_led *led = to_lm3533_led(led_cdev); |
| 618 | umode_t mode = attr->mode; |
| 619 | |
| 620 | if (attr == &dev_attr_als_channel.attr || |
| 621 | attr == &dev_attr_als_en.attr) { |
| 622 | if (!led->lm3533->have_als) |
| 623 | mode = 0; |
| 624 | } |
| 625 | |
| 626 | return mode; |
| 627 | }; |
| 628 | |
| 629 | static struct attribute_group lm3533_led_attribute_group = { |
| 630 | .is_visible = lm3533_led_attr_is_visible, |
| 631 | .attrs = lm3533_led_attributes |
| 632 | }; |
| 633 | |
Johan Hovold | 9db9386 | 2014-06-25 10:08:46 -0700 | [diff] [blame] | 634 | static const struct attribute_group *lm3533_led_attribute_groups[] = { |
| 635 | &lm3533_led_attribute_group, |
| 636 | NULL |
| 637 | }; |
| 638 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 639 | static int lm3533_led_setup(struct lm3533_led *led, |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 640 | struct lm3533_led_platform_data *pdata) |
| 641 | { |
| 642 | int ret; |
| 643 | |
| 644 | ret = lm3533_ctrlbank_set_max_current(&led->cb, pdata->max_current); |
| 645 | if (ret) |
| 646 | return ret; |
| 647 | |
| 648 | return lm3533_ctrlbank_set_pwm(&led->cb, pdata->pwm); |
| 649 | } |
| 650 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 651 | static int lm3533_led_probe(struct platform_device *pdev) |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 652 | { |
| 653 | struct lm3533 *lm3533; |
| 654 | struct lm3533_led_platform_data *pdata; |
| 655 | struct lm3533_led *led; |
| 656 | int ret; |
| 657 | |
| 658 | dev_dbg(&pdev->dev, "%s\n", __func__); |
| 659 | |
| 660 | lm3533 = dev_get_drvdata(pdev->dev.parent); |
| 661 | if (!lm3533) |
| 662 | return -EINVAL; |
| 663 | |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 664 | pdata = dev_get_platdata(&pdev->dev); |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 665 | if (!pdata) { |
| 666 | dev_err(&pdev->dev, "no platform data\n"); |
| 667 | return -EINVAL; |
| 668 | } |
| 669 | |
| 670 | if (pdev->id < 0 || pdev->id >= LM3533_LVCTRLBANK_COUNT) { |
| 671 | dev_err(&pdev->dev, "illegal LED id %d\n", pdev->id); |
| 672 | return -EINVAL; |
| 673 | } |
| 674 | |
| 675 | led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL); |
| 676 | if (!led) |
| 677 | return -ENOMEM; |
| 678 | |
| 679 | led->lm3533 = lm3533; |
| 680 | led->cdev.name = pdata->name; |
| 681 | led->cdev.default_trigger = pdata->default_trigger; |
Andrew Lunn | a478851 | 2015-08-20 12:02:17 +0200 | [diff] [blame] | 682 | led->cdev.brightness_set_blocking = lm3533_led_set; |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 683 | led->cdev.brightness_get = lm3533_led_get; |
| 684 | led->cdev.blink_set = lm3533_led_blink_set; |
| 685 | led->cdev.brightness = LED_OFF; |
Johan Hovold | 9db9386 | 2014-06-25 10:08:46 -0700 | [diff] [blame] | 686 | led->cdev.groups = lm3533_led_attribute_groups, |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 687 | led->id = pdev->id; |
| 688 | |
| 689 | mutex_init(&led->mutex); |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 690 | |
| 691 | /* The class framework makes a callback to get brightness during |
| 692 | * registration so use parent device (for error reporting) until |
| 693 | * registered. |
| 694 | */ |
| 695 | led->cb.lm3533 = lm3533; |
| 696 | led->cb.id = lm3533_led_get_ctrlbank_id(led); |
| 697 | led->cb.dev = lm3533->dev; |
| 698 | |
| 699 | platform_set_drvdata(pdev, led); |
| 700 | |
Amitoj Kaur Chawla | 50154e2 | 2016-03-07 20:21:42 +0530 | [diff] [blame] | 701 | ret = devm_led_classdev_register(pdev->dev.parent, &led->cdev); |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 702 | if (ret) { |
| 703 | dev_err(&pdev->dev, "failed to register LED %d\n", pdev->id); |
| 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | led->cb.dev = led->cdev.dev; |
| 708 | |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 709 | ret = lm3533_led_setup(led, pdata); |
| 710 | if (ret) |
Amitoj Kaur Chawla | 50154e2 | 2016-03-07 20:21:42 +0530 | [diff] [blame] | 711 | return ret; |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 712 | |
| 713 | ret = lm3533_ctrlbank_enable(&led->cb); |
| 714 | if (ret) |
Amitoj Kaur Chawla | 50154e2 | 2016-03-07 20:21:42 +0530 | [diff] [blame] | 715 | return ret; |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 716 | |
| 717 | return 0; |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Bill Pemberton | 678e8a6 | 2012-11-19 13:26:00 -0500 | [diff] [blame] | 720 | static int lm3533_led_remove(struct platform_device *pdev) |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 721 | { |
| 722 | struct lm3533_led *led = platform_get_drvdata(pdev); |
| 723 | |
| 724 | dev_dbg(&pdev->dev, "%s\n", __func__); |
| 725 | |
| 726 | lm3533_ctrlbank_disable(&led->cb); |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static void lm3533_led_shutdown(struct platform_device *pdev) |
| 732 | { |
| 733 | |
| 734 | struct lm3533_led *led = platform_get_drvdata(pdev); |
| 735 | |
| 736 | dev_dbg(&pdev->dev, "%s\n", __func__); |
| 737 | |
| 738 | lm3533_ctrlbank_disable(&led->cb); |
| 739 | lm3533_led_set(&led->cdev, LED_OFF); /* disable blink */ |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static struct platform_driver lm3533_led_driver = { |
| 743 | .driver = { |
| 744 | .name = "lm3533-leds", |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 745 | }, |
| 746 | .probe = lm3533_led_probe, |
Bill Pemberton | df07cf8 | 2012-11-19 13:20:20 -0500 | [diff] [blame] | 747 | .remove = lm3533_led_remove, |
Johan Hovold | 401dea7 | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 748 | .shutdown = lm3533_led_shutdown, |
| 749 | }; |
| 750 | module_platform_driver(lm3533_led_driver); |
| 751 | |
| 752 | MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>"); |
| 753 | MODULE_DESCRIPTION("LM3533 LED driver"); |
| 754 | MODULE_LICENSE("GPL"); |
| 755 | MODULE_ALIAS("platform:lm3533-leds"); |