Ingi Kim | b7da8c5 | 2015-05-12 01:37:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * LED driver : leds-ktd2692.c |
| 3 | * |
| 4 | * Copyright (C) 2015 Samsung Electronics |
| 5 | * Ingi Kim <ingi2.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 | |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/gpio/consumer.h> |
| 15 | #include <linux/led-class-flash.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/regulator/consumer.h> |
| 21 | #include <linux/workqueue.h> |
| 22 | |
| 23 | /* Value related the movie mode */ |
| 24 | #define KTD2692_MOVIE_MODE_CURRENT_LEVELS 16 |
| 25 | #define KTD2692_MM_TO_FL_RATIO(x) ((x) / 3) |
| 26 | #define KTD2962_MM_MIN_CURR_THRESHOLD_SCALE 8 |
| 27 | |
| 28 | /* Value related the flash mode */ |
| 29 | #define KTD2692_FLASH_MODE_TIMEOUT_LEVELS 8 |
| 30 | #define KTD2692_FLASH_MODE_TIMEOUT_DISABLE 0 |
| 31 | #define KTD2692_FLASH_MODE_CURR_PERCENT(x) (((x) * 16) / 100) |
| 32 | |
| 33 | /* Macro for getting offset of flash timeout */ |
| 34 | #define GET_TIMEOUT_OFFSET(timeout, step) ((timeout) / (step)) |
| 35 | |
| 36 | /* Base register address */ |
| 37 | #define KTD2692_REG_LVP_BASE 0x00 |
| 38 | #define KTD2692_REG_FLASH_TIMEOUT_BASE 0x20 |
| 39 | #define KTD2692_REG_MM_MIN_CURR_THRESHOLD_BASE 0x40 |
| 40 | #define KTD2692_REG_MOVIE_CURRENT_BASE 0x60 |
| 41 | #define KTD2692_REG_FLASH_CURRENT_BASE 0x80 |
| 42 | #define KTD2692_REG_MODE_BASE 0xA0 |
| 43 | |
| 44 | /* Set bit coding time for expresswire interface */ |
| 45 | #define KTD2692_TIME_RESET_US 700 |
| 46 | #define KTD2692_TIME_DATA_START_TIME_US 10 |
| 47 | #define KTD2692_TIME_HIGH_END_OF_DATA_US 350 |
| 48 | #define KTD2692_TIME_LOW_END_OF_DATA_US 10 |
| 49 | #define KTD2692_TIME_SHORT_BITSET_US 4 |
| 50 | #define KTD2692_TIME_LONG_BITSET_US 12 |
| 51 | |
| 52 | /* KTD2692 default length of name */ |
| 53 | #define KTD2692_NAME_LENGTH 20 |
| 54 | |
| 55 | enum ktd2692_bitset { |
| 56 | KTD2692_LOW = 0, |
| 57 | KTD2692_HIGH, |
| 58 | }; |
| 59 | |
| 60 | /* Movie / Flash Mode Control */ |
| 61 | enum ktd2692_led_mode { |
| 62 | KTD2692_MODE_DISABLE = 0, /* default */ |
| 63 | KTD2692_MODE_MOVIE, |
| 64 | KTD2692_MODE_FLASH, |
| 65 | }; |
| 66 | |
| 67 | struct ktd2692_led_config_data { |
| 68 | /* maximum LED current in movie mode */ |
| 69 | u32 movie_max_microamp; |
| 70 | /* maximum LED current in flash mode */ |
| 71 | u32 flash_max_microamp; |
| 72 | /* maximum flash timeout */ |
| 73 | u32 flash_max_timeout; |
| 74 | /* max LED brightness level */ |
| 75 | enum led_brightness max_brightness; |
| 76 | }; |
| 77 | |
| 78 | struct ktd2692_context { |
| 79 | /* Related LED Flash class device */ |
| 80 | struct led_classdev_flash fled_cdev; |
| 81 | |
| 82 | /* secures access to the device */ |
| 83 | struct mutex lock; |
| 84 | struct regulator *regulator; |
| 85 | struct work_struct work_brightness_set; |
| 86 | |
| 87 | struct gpio_desc *aux_gpio; |
| 88 | struct gpio_desc *ctrl_gpio; |
| 89 | |
| 90 | enum ktd2692_led_mode mode; |
| 91 | enum led_brightness torch_brightness; |
| 92 | }; |
| 93 | |
| 94 | static struct ktd2692_context *fled_cdev_to_led( |
| 95 | struct led_classdev_flash *fled_cdev) |
| 96 | { |
| 97 | return container_of(fled_cdev, struct ktd2692_context, fled_cdev); |
| 98 | } |
| 99 | |
| 100 | static void ktd2692_expresswire_start(struct ktd2692_context *led) |
| 101 | { |
| 102 | gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH); |
| 103 | udelay(KTD2692_TIME_DATA_START_TIME_US); |
| 104 | } |
| 105 | |
| 106 | static void ktd2692_expresswire_reset(struct ktd2692_context *led) |
| 107 | { |
| 108 | gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW); |
| 109 | udelay(KTD2692_TIME_RESET_US); |
| 110 | } |
| 111 | |
| 112 | static void ktd2692_expresswire_end(struct ktd2692_context *led) |
| 113 | { |
| 114 | gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW); |
| 115 | udelay(KTD2692_TIME_LOW_END_OF_DATA_US); |
| 116 | gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH); |
| 117 | udelay(KTD2692_TIME_HIGH_END_OF_DATA_US); |
| 118 | } |
| 119 | |
| 120 | static void ktd2692_expresswire_set_bit(struct ktd2692_context *led, bool bit) |
| 121 | { |
| 122 | /* |
| 123 | * The Low Bit(0) and High Bit(1) is based on a time detection |
| 124 | * algorithm between time low and time high |
| 125 | * Time_(L_LB) : Low time of the Low Bit(0) |
| 126 | * Time_(H_LB) : High time of the LOW Bit(0) |
| 127 | * Time_(L_HB) : Low time of the High Bit(1) |
| 128 | * Time_(H_HB) : High time of the High Bit(1) |
| 129 | * |
| 130 | * It can be simplified to: |
| 131 | * Low Bit(0) : 2 * Time_(H_LB) < Time_(L_LB) |
| 132 | * High Bit(1) : 2 * Time_(L_HB) < Time_(H_HB) |
| 133 | * HIGH ___ ____ _.. _________ ___ |
| 134 | * |_________| |_.. |____| |__| |
| 135 | * LOW <L_LB> <H_LB> <L_HB> <H_HB> |
| 136 | * [ Low Bit (0) ] [ High Bit(1) ] |
| 137 | */ |
| 138 | if (bit) { |
| 139 | gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW); |
| 140 | udelay(KTD2692_TIME_SHORT_BITSET_US); |
| 141 | gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH); |
| 142 | udelay(KTD2692_TIME_LONG_BITSET_US); |
| 143 | } else { |
| 144 | gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW); |
| 145 | udelay(KTD2692_TIME_LONG_BITSET_US); |
| 146 | gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH); |
| 147 | udelay(KTD2692_TIME_SHORT_BITSET_US); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | static void ktd2692_expresswire_write(struct ktd2692_context *led, u8 value) |
| 152 | { |
| 153 | int i; |
| 154 | |
| 155 | ktd2692_expresswire_start(led); |
| 156 | for (i = 7; i >= 0; i--) |
| 157 | ktd2692_expresswire_set_bit(led, value & BIT(i)); |
| 158 | ktd2692_expresswire_end(led); |
| 159 | } |
| 160 | |
| 161 | static void ktd2692_brightness_set(struct ktd2692_context *led, |
| 162 | enum led_brightness brightness) |
| 163 | { |
| 164 | mutex_lock(&led->lock); |
| 165 | |
| 166 | if (brightness == LED_OFF) { |
| 167 | led->mode = KTD2692_MODE_DISABLE; |
| 168 | gpiod_direction_output(led->aux_gpio, KTD2692_LOW); |
| 169 | } else { |
| 170 | ktd2692_expresswire_write(led, brightness | |
| 171 | KTD2692_REG_MOVIE_CURRENT_BASE); |
| 172 | led->mode = KTD2692_MODE_MOVIE; |
| 173 | } |
| 174 | |
| 175 | ktd2692_expresswire_write(led, led->mode | KTD2692_REG_MODE_BASE); |
| 176 | mutex_unlock(&led->lock); |
| 177 | } |
| 178 | |
| 179 | static void ktd2692_brightness_set_work(struct work_struct *work) |
| 180 | { |
| 181 | struct ktd2692_context *led = |
| 182 | container_of(work, struct ktd2692_context, work_brightness_set); |
| 183 | |
| 184 | ktd2692_brightness_set(led, led->torch_brightness); |
| 185 | } |
| 186 | |
| 187 | static void ktd2692_led_brightness_set(struct led_classdev *led_cdev, |
| 188 | enum led_brightness brightness) |
| 189 | { |
| 190 | struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); |
| 191 | struct ktd2692_context *led = fled_cdev_to_led(fled_cdev); |
| 192 | |
| 193 | led->torch_brightness = brightness; |
| 194 | schedule_work(&led->work_brightness_set); |
| 195 | } |
| 196 | |
| 197 | static int ktd2692_led_brightness_set_sync(struct led_classdev *led_cdev, |
| 198 | enum led_brightness brightness) |
| 199 | { |
| 200 | struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); |
| 201 | struct ktd2692_context *led = fled_cdev_to_led(fled_cdev); |
| 202 | |
| 203 | ktd2692_brightness_set(led, brightness); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static int ktd2692_led_flash_strobe_set(struct led_classdev_flash *fled_cdev, |
| 209 | bool state) |
| 210 | { |
| 211 | struct ktd2692_context *led = fled_cdev_to_led(fled_cdev); |
| 212 | struct led_flash_setting *timeout = &fled_cdev->timeout; |
| 213 | u32 flash_tm_reg; |
| 214 | |
| 215 | mutex_lock(&led->lock); |
| 216 | |
| 217 | if (state) { |
| 218 | flash_tm_reg = GET_TIMEOUT_OFFSET(timeout->val, timeout->step); |
| 219 | ktd2692_expresswire_write(led, flash_tm_reg |
| 220 | | KTD2692_REG_FLASH_TIMEOUT_BASE); |
| 221 | |
| 222 | led->mode = KTD2692_MODE_FLASH; |
| 223 | gpiod_direction_output(led->aux_gpio, KTD2692_HIGH); |
| 224 | } else { |
| 225 | led->mode = KTD2692_MODE_DISABLE; |
| 226 | gpiod_direction_output(led->aux_gpio, KTD2692_LOW); |
| 227 | } |
| 228 | |
| 229 | ktd2692_expresswire_write(led, led->mode | KTD2692_REG_MODE_BASE); |
| 230 | |
| 231 | fled_cdev->led_cdev.brightness = LED_OFF; |
| 232 | led->mode = KTD2692_MODE_DISABLE; |
| 233 | |
| 234 | mutex_unlock(&led->lock); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int ktd2692_led_flash_timeout_set(struct led_classdev_flash *fled_cdev, |
| 240 | u32 timeout) |
| 241 | { |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static void ktd2692_init_movie_current_max(struct ktd2692_led_config_data *cfg) |
| 246 | { |
| 247 | u32 offset, step; |
| 248 | u32 movie_current_microamp; |
| 249 | |
| 250 | offset = KTD2692_MOVIE_MODE_CURRENT_LEVELS; |
| 251 | step = KTD2692_MM_TO_FL_RATIO(cfg->flash_max_microamp) |
| 252 | / KTD2692_MOVIE_MODE_CURRENT_LEVELS; |
| 253 | |
| 254 | do { |
| 255 | movie_current_microamp = step * offset; |
| 256 | offset--; |
| 257 | } while ((movie_current_microamp > cfg->movie_max_microamp) && |
| 258 | (offset > 0)); |
| 259 | |
| 260 | cfg->max_brightness = offset; |
| 261 | } |
| 262 | |
| 263 | static void ktd2692_init_flash_timeout(struct led_classdev_flash *fled_cdev, |
| 264 | struct ktd2692_led_config_data *cfg) |
| 265 | { |
| 266 | struct led_flash_setting *setting; |
| 267 | |
| 268 | setting = &fled_cdev->timeout; |
| 269 | setting->min = KTD2692_FLASH_MODE_TIMEOUT_DISABLE; |
| 270 | setting->max = cfg->flash_max_timeout; |
| 271 | setting->step = cfg->flash_max_timeout |
| 272 | / (KTD2692_FLASH_MODE_TIMEOUT_LEVELS - 1); |
| 273 | setting->val = cfg->flash_max_timeout; |
| 274 | } |
| 275 | |
| 276 | static void ktd2692_setup(struct ktd2692_context *led) |
| 277 | { |
| 278 | led->mode = KTD2692_MODE_DISABLE; |
| 279 | ktd2692_expresswire_reset(led); |
| 280 | gpiod_direction_output(led->aux_gpio, KTD2692_LOW); |
| 281 | |
| 282 | ktd2692_expresswire_write(led, (KTD2962_MM_MIN_CURR_THRESHOLD_SCALE - 1) |
| 283 | | KTD2692_REG_MM_MIN_CURR_THRESHOLD_BASE); |
| 284 | ktd2692_expresswire_write(led, KTD2692_FLASH_MODE_CURR_PERCENT(45) |
| 285 | | KTD2692_REG_FLASH_CURRENT_BASE); |
| 286 | } |
| 287 | |
| 288 | static int ktd2692_parse_dt(struct ktd2692_context *led, struct device *dev, |
| 289 | struct ktd2692_led_config_data *cfg) |
| 290 | { |
| 291 | struct device_node *np = dev->of_node; |
| 292 | struct device_node *child_node; |
| 293 | int ret; |
| 294 | |
| 295 | if (!dev->of_node) |
| 296 | return -ENXIO; |
| 297 | |
Uwe Kleine-König | 5e324eb | 2015-06-12 00:32:23 -0700 | [diff] [blame] | 298 | led->ctrl_gpio = devm_gpiod_get(dev, "ctrl", GPIOD_ASIS); |
Ingi Kim | b7da8c5 | 2015-05-12 01:37:57 -0700 | [diff] [blame] | 299 | if (IS_ERR(led->ctrl_gpio)) { |
| 300 | ret = PTR_ERR(led->ctrl_gpio); |
| 301 | dev_err(dev, "cannot get ctrl-gpios %d\n", ret); |
| 302 | return ret; |
| 303 | } |
| 304 | |
Uwe Kleine-König | 5e324eb | 2015-06-12 00:32:23 -0700 | [diff] [blame] | 305 | led->aux_gpio = devm_gpiod_get(dev, "aux", GPIOD_ASIS); |
Ingi Kim | b7da8c5 | 2015-05-12 01:37:57 -0700 | [diff] [blame] | 306 | if (IS_ERR(led->aux_gpio)) { |
| 307 | ret = PTR_ERR(led->aux_gpio); |
| 308 | dev_err(dev, "cannot get aux-gpios %d\n", ret); |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | led->regulator = devm_regulator_get(dev, "vin"); |
| 313 | if (IS_ERR(led->regulator)) |
| 314 | led->regulator = NULL; |
| 315 | |
| 316 | if (led->regulator) { |
| 317 | ret = regulator_enable(led->regulator); |
| 318 | if (ret) |
| 319 | dev_err(dev, "Failed to enable supply: %d\n", ret); |
| 320 | } |
| 321 | |
| 322 | child_node = of_get_next_available_child(np, NULL); |
| 323 | if (!child_node) { |
| 324 | dev_err(dev, "No DT child node found for connected LED.\n"); |
| 325 | return -EINVAL; |
| 326 | } |
| 327 | |
| 328 | led->fled_cdev.led_cdev.name = |
| 329 | of_get_property(child_node, "label", NULL) ? : child_node->name; |
| 330 | |
| 331 | ret = of_property_read_u32(child_node, "led-max-microamp", |
| 332 | &cfg->movie_max_microamp); |
| 333 | if (ret) { |
| 334 | dev_err(dev, "failed to parse led-max-microamp\n"); |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | ret = of_property_read_u32(child_node, "flash-max-microamp", |
| 339 | &cfg->flash_max_microamp); |
| 340 | if (ret) { |
| 341 | dev_err(dev, "failed to parse flash-max-microamp\n"); |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | ret = of_property_read_u32(child_node, "flash-max-timeout-us", |
| 346 | &cfg->flash_max_timeout); |
| 347 | if (ret) |
| 348 | dev_err(dev, "failed to parse flash-max-timeout-us\n"); |
| 349 | |
| 350 | of_node_put(child_node); |
| 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | static const struct led_flash_ops flash_ops = { |
| 355 | .strobe_set = ktd2692_led_flash_strobe_set, |
| 356 | .timeout_set = ktd2692_led_flash_timeout_set, |
| 357 | }; |
| 358 | |
| 359 | static int ktd2692_probe(struct platform_device *pdev) |
| 360 | { |
| 361 | struct ktd2692_context *led; |
| 362 | struct led_classdev *led_cdev; |
| 363 | struct led_classdev_flash *fled_cdev; |
| 364 | struct ktd2692_led_config_data led_cfg; |
| 365 | int ret; |
| 366 | |
| 367 | led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL); |
| 368 | if (!led) |
| 369 | return -ENOMEM; |
| 370 | |
| 371 | fled_cdev = &led->fled_cdev; |
| 372 | led_cdev = &fled_cdev->led_cdev; |
| 373 | |
| 374 | ret = ktd2692_parse_dt(led, &pdev->dev, &led_cfg); |
| 375 | if (ret) |
| 376 | return ret; |
| 377 | |
| 378 | ktd2692_init_flash_timeout(fled_cdev, &led_cfg); |
| 379 | ktd2692_init_movie_current_max(&led_cfg); |
| 380 | |
| 381 | fled_cdev->ops = &flash_ops; |
| 382 | |
| 383 | led_cdev->max_brightness = led_cfg.max_brightness; |
| 384 | led_cdev->brightness_set = ktd2692_led_brightness_set; |
| 385 | led_cdev->brightness_set_sync = ktd2692_led_brightness_set_sync; |
| 386 | led_cdev->flags |= LED_CORE_SUSPENDRESUME | LED_DEV_CAP_FLASH; |
| 387 | |
| 388 | mutex_init(&led->lock); |
| 389 | INIT_WORK(&led->work_brightness_set, ktd2692_brightness_set_work); |
| 390 | |
| 391 | platform_set_drvdata(pdev, led); |
| 392 | |
| 393 | ret = led_classdev_flash_register(&pdev->dev, fled_cdev); |
| 394 | if (ret) { |
| 395 | dev_err(&pdev->dev, "can't register LED %s\n", led_cdev->name); |
| 396 | mutex_destroy(&led->lock); |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | ktd2692_setup(led); |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static int ktd2692_remove(struct platform_device *pdev) |
| 406 | { |
| 407 | struct ktd2692_context *led = platform_get_drvdata(pdev); |
| 408 | int ret; |
| 409 | |
| 410 | led_classdev_flash_unregister(&led->fled_cdev); |
| 411 | cancel_work_sync(&led->work_brightness_set); |
| 412 | |
| 413 | if (led->regulator) { |
| 414 | ret = regulator_disable(led->regulator); |
| 415 | if (ret) |
| 416 | dev_err(&pdev->dev, |
| 417 | "Failed to disable supply: %d\n", ret); |
| 418 | } |
| 419 | |
| 420 | mutex_destroy(&led->lock); |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static const struct of_device_id ktd2692_match[] = { |
| 426 | { .compatible = "kinetic,ktd2692", }, |
| 427 | { /* sentinel */ }, |
| 428 | }; |
Luis de Bethencourt | 3682c7b | 2015-09-01 23:36:15 +0200 | [diff] [blame^] | 429 | MODULE_DEVICE_TABLE(of, ktd2692_match); |
Ingi Kim | b7da8c5 | 2015-05-12 01:37:57 -0700 | [diff] [blame] | 430 | |
| 431 | static struct platform_driver ktd2692_driver = { |
| 432 | .driver = { |
| 433 | .name = "ktd2692", |
| 434 | .of_match_table = ktd2692_match, |
| 435 | }, |
| 436 | .probe = ktd2692_probe, |
| 437 | .remove = ktd2692_remove, |
| 438 | }; |
| 439 | |
| 440 | module_platform_driver(ktd2692_driver); |
| 441 | |
| 442 | MODULE_AUTHOR("Ingi Kim <ingi2.kim@samsung.com>"); |
| 443 | MODULE_DESCRIPTION("Kinetic KTD2692 LED driver"); |
| 444 | MODULE_LICENSE("GPL v2"); |