Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * LED Flash class driver for the AAT1290 |
| 3 | * 1.5A Step-Up Current Regulator for Flash LEDs |
| 4 | * |
| 5 | * Copyright (C) 2015, Samsung Electronics Co., Ltd. |
| 6 | * Author: Jacek Anaszewski <j.anaszewski@samsung.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 | |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/gpio/consumer.h> |
| 15 | #include <linux/led-class-flash.h> |
| 16 | #include <linux/leds.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/of.h> |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 20 | #include <linux/pinctrl/consumer.h> |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/slab.h> |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 23 | #include <media/v4l2-flash-led-class.h> |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 24 | |
| 25 | #define AAT1290_MOVIE_MODE_CURRENT_ADDR 17 |
| 26 | #define AAT1290_MAX_MM_CURR_PERCENT_0 16 |
| 27 | #define AAT1290_MAX_MM_CURR_PERCENT_100 1 |
| 28 | |
| 29 | #define AAT1290_FLASH_SAFETY_TIMER_ADDR 18 |
| 30 | |
| 31 | #define AAT1290_MOVIE_MODE_CONFIG_ADDR 19 |
| 32 | #define AAT1290_MOVIE_MODE_OFF 1 |
| 33 | #define AAT1290_MOVIE_MODE_ON 3 |
| 34 | |
| 35 | #define AAT1290_MM_CURRENT_RATIO_ADDR 20 |
| 36 | #define AAT1290_MM_TO_FL_1_92 1 |
| 37 | |
| 38 | #define AAT1290_MM_TO_FL_RATIO 1000 / 1920 |
| 39 | #define AAT1290_MAX_MM_CURRENT(fl_max) (fl_max * AAT1290_MM_TO_FL_RATIO) |
| 40 | |
| 41 | #define AAT1290_LATCH_TIME_MIN_US 500 |
| 42 | #define AAT1290_LATCH_TIME_MAX_US 1000 |
| 43 | #define AAT1290_EN_SET_TICK_TIME_US 1 |
| 44 | #define AAT1290_FLEN_OFF_DELAY_TIME_US 10 |
| 45 | #define AAT1290_FLASH_TM_NUM_LEVELS 16 |
| 46 | #define AAT1290_MM_CURRENT_SCALE_SIZE 15 |
| 47 | |
| 48 | |
| 49 | struct aat1290_led_config_data { |
| 50 | /* maximum LED current in movie mode */ |
| 51 | u32 max_mm_current; |
| 52 | /* maximum LED current in flash mode */ |
| 53 | u32 max_flash_current; |
| 54 | /* maximum flash timeout */ |
| 55 | u32 max_flash_tm; |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 56 | /* external strobe capability */ |
| 57 | bool has_external_strobe; |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 58 | /* max LED brightness level */ |
| 59 | enum led_brightness max_brightness; |
| 60 | }; |
| 61 | |
| 62 | struct aat1290_led { |
| 63 | /* platform device data */ |
| 64 | struct platform_device *pdev; |
| 65 | /* secures access to the device */ |
| 66 | struct mutex lock; |
| 67 | |
| 68 | /* corresponding LED Flash class device */ |
| 69 | struct led_classdev_flash fled_cdev; |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 70 | /* V4L2 Flash device */ |
| 71 | struct v4l2_flash *v4l2_flash; |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 72 | |
| 73 | /* FLEN pin */ |
| 74 | struct gpio_desc *gpio_fl_en; |
| 75 | /* EN|SET pin */ |
| 76 | struct gpio_desc *gpio_en_set; |
| 77 | /* movie mode current scale */ |
| 78 | int *mm_current_scale; |
| 79 | /* device mode */ |
| 80 | bool movie_mode; |
| 81 | |
| 82 | /* brightness cache */ |
| 83 | unsigned int torch_brightness; |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static struct aat1290_led *fled_cdev_to_led( |
| 87 | struct led_classdev_flash *fled_cdev) |
| 88 | { |
| 89 | return container_of(fled_cdev, struct aat1290_led, fled_cdev); |
| 90 | } |
| 91 | |
Jacek Anaszewski | 269e92d | 2015-10-07 11:10:46 +0200 | [diff] [blame^] | 92 | static struct led_classdev_flash *led_cdev_to_fled_cdev( |
| 93 | struct led_classdev *led_cdev) |
| 94 | { |
| 95 | return container_of(led_cdev, struct led_classdev_flash, led_cdev); |
| 96 | } |
| 97 | |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 98 | static void aat1290_as2cwire_write(struct aat1290_led *led, int addr, int value) |
| 99 | { |
| 100 | int i; |
| 101 | |
| 102 | gpiod_direction_output(led->gpio_fl_en, 0); |
| 103 | gpiod_direction_output(led->gpio_en_set, 0); |
| 104 | |
| 105 | udelay(AAT1290_FLEN_OFF_DELAY_TIME_US); |
| 106 | |
| 107 | /* write address */ |
| 108 | for (i = 0; i < addr; ++i) { |
| 109 | udelay(AAT1290_EN_SET_TICK_TIME_US); |
| 110 | gpiod_direction_output(led->gpio_en_set, 0); |
| 111 | udelay(AAT1290_EN_SET_TICK_TIME_US); |
| 112 | gpiod_direction_output(led->gpio_en_set, 1); |
| 113 | } |
| 114 | |
| 115 | usleep_range(AAT1290_LATCH_TIME_MIN_US, AAT1290_LATCH_TIME_MAX_US); |
| 116 | |
| 117 | /* write data */ |
| 118 | for (i = 0; i < value; ++i) { |
| 119 | udelay(AAT1290_EN_SET_TICK_TIME_US); |
| 120 | gpiod_direction_output(led->gpio_en_set, 0); |
| 121 | udelay(AAT1290_EN_SET_TICK_TIME_US); |
| 122 | gpiod_direction_output(led->gpio_en_set, 1); |
| 123 | } |
| 124 | |
| 125 | usleep_range(AAT1290_LATCH_TIME_MIN_US, AAT1290_LATCH_TIME_MAX_US); |
| 126 | } |
| 127 | |
| 128 | static void aat1290_set_flash_safety_timer(struct aat1290_led *led, |
| 129 | unsigned int micro_sec) |
| 130 | { |
| 131 | struct led_classdev_flash *fled_cdev = &led->fled_cdev; |
| 132 | struct led_flash_setting *flash_tm = &fled_cdev->timeout; |
| 133 | int flash_tm_reg = AAT1290_FLASH_TM_NUM_LEVELS - |
| 134 | (micro_sec / flash_tm->step) + 1; |
| 135 | |
| 136 | aat1290_as2cwire_write(led, AAT1290_FLASH_SAFETY_TIMER_ADDR, |
| 137 | flash_tm_reg); |
| 138 | } |
| 139 | |
Jacek Anaszewski | 269e92d | 2015-10-07 11:10:46 +0200 | [diff] [blame^] | 140 | /* LED subsystem callbacks */ |
| 141 | |
| 142 | static int aat1290_led_brightness_set(struct led_classdev *led_cdev, |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 143 | enum led_brightness brightness) |
| 144 | { |
Jacek Anaszewski | 269e92d | 2015-10-07 11:10:46 +0200 | [diff] [blame^] | 145 | struct led_classdev_flash *fled_cdev = led_cdev_to_fled_cdev(led_cdev); |
| 146 | struct aat1290_led *led = fled_cdev_to_led(fled_cdev); |
| 147 | |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 148 | mutex_lock(&led->lock); |
| 149 | |
| 150 | if (brightness == 0) { |
| 151 | gpiod_direction_output(led->gpio_fl_en, 0); |
| 152 | gpiod_direction_output(led->gpio_en_set, 0); |
| 153 | led->movie_mode = false; |
| 154 | } else { |
| 155 | if (!led->movie_mode) { |
| 156 | aat1290_as2cwire_write(led, |
| 157 | AAT1290_MM_CURRENT_RATIO_ADDR, |
| 158 | AAT1290_MM_TO_FL_1_92); |
| 159 | led->movie_mode = true; |
| 160 | } |
| 161 | |
| 162 | aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CURRENT_ADDR, |
| 163 | AAT1290_MAX_MM_CURR_PERCENT_0 - brightness); |
| 164 | aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CONFIG_ADDR, |
| 165 | AAT1290_MOVIE_MODE_ON); |
| 166 | } |
| 167 | |
| 168 | mutex_unlock(&led->lock); |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int aat1290_led_flash_strobe_set(struct led_classdev_flash *fled_cdev, |
| 174 | bool state) |
| 175 | |
| 176 | { |
| 177 | struct aat1290_led *led = fled_cdev_to_led(fled_cdev); |
| 178 | struct led_classdev *led_cdev = &fled_cdev->led_cdev; |
| 179 | struct led_flash_setting *timeout = &fled_cdev->timeout; |
| 180 | |
| 181 | mutex_lock(&led->lock); |
| 182 | |
| 183 | if (state) { |
| 184 | aat1290_set_flash_safety_timer(led, timeout->val); |
| 185 | gpiod_direction_output(led->gpio_fl_en, 1); |
| 186 | } else { |
| 187 | gpiod_direction_output(led->gpio_fl_en, 0); |
| 188 | gpiod_direction_output(led->gpio_en_set, 0); |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * To reenter movie mode after a flash event the part must be cycled |
| 193 | * off and back on to reset the movie mode and reprogrammed via the |
| 194 | * AS2Cwire. Therefore the brightness and movie_mode properties needs |
| 195 | * to be updated here to reflect the actual state. |
| 196 | */ |
| 197 | led_cdev->brightness = 0; |
| 198 | led->movie_mode = false; |
| 199 | |
| 200 | mutex_unlock(&led->lock); |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int aat1290_led_flash_timeout_set(struct led_classdev_flash *fled_cdev, |
| 206 | u32 timeout) |
| 207 | { |
| 208 | /* |
| 209 | * Don't do anything - flash timeout is cached in the led-class-flash |
| 210 | * core and will be applied in the strobe_set op, as writing the |
| 211 | * safety timer register spuriously turns the torch mode on. |
| 212 | */ |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int aat1290_led_parse_dt(struct aat1290_led *led, |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 218 | struct aat1290_led_config_data *cfg, |
| 219 | struct device_node **sub_node) |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 220 | { |
| 221 | struct led_classdev *led_cdev = &led->fled_cdev.led_cdev; |
| 222 | struct device *dev = &led->pdev->dev; |
| 223 | struct device_node *child_node; |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 224 | #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) |
| 225 | struct pinctrl *pinctrl; |
| 226 | #endif |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 227 | int ret = 0; |
| 228 | |
Uwe Kleine-König | 8a68771 | 2015-06-12 00:35:43 -0700 | [diff] [blame] | 229 | led->gpio_fl_en = devm_gpiod_get(dev, "flen", GPIOD_ASIS); |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 230 | if (IS_ERR(led->gpio_fl_en)) { |
| 231 | ret = PTR_ERR(led->gpio_fl_en); |
| 232 | dev_err(dev, "Unable to claim gpio \"flen\".\n"); |
| 233 | return ret; |
| 234 | } |
| 235 | |
Uwe Kleine-König | 8a68771 | 2015-06-12 00:35:43 -0700 | [diff] [blame] | 236 | led->gpio_en_set = devm_gpiod_get(dev, "enset", GPIOD_ASIS); |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 237 | if (IS_ERR(led->gpio_en_set)) { |
| 238 | ret = PTR_ERR(led->gpio_en_set); |
| 239 | dev_err(dev, "Unable to claim gpio \"enset\".\n"); |
| 240 | return ret; |
| 241 | } |
| 242 | |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 243 | #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) |
| 244 | pinctrl = devm_pinctrl_get_select_default(&led->pdev->dev); |
| 245 | if (IS_ERR(pinctrl)) { |
| 246 | cfg->has_external_strobe = false; |
| 247 | dev_info(dev, |
| 248 | "No support for external strobe detected.\n"); |
| 249 | } else { |
| 250 | cfg->has_external_strobe = true; |
| 251 | } |
| 252 | #endif |
| 253 | |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 254 | child_node = of_get_next_available_child(dev->of_node, NULL); |
| 255 | if (!child_node) { |
| 256 | dev_err(dev, "No DT child node found for connected LED.\n"); |
| 257 | return -EINVAL; |
| 258 | } |
| 259 | |
| 260 | led_cdev->name = of_get_property(child_node, "label", NULL) ? : |
| 261 | child_node->name; |
| 262 | |
| 263 | ret = of_property_read_u32(child_node, "led-max-microamp", |
| 264 | &cfg->max_mm_current); |
| 265 | /* |
| 266 | * led-max-microamp will default to 1/20 of flash-max-microamp |
| 267 | * in case it is missing. |
| 268 | */ |
| 269 | if (ret < 0) |
| 270 | dev_warn(dev, |
| 271 | "led-max-microamp DT property missing\n"); |
| 272 | |
| 273 | ret = of_property_read_u32(child_node, "flash-max-microamp", |
| 274 | &cfg->max_flash_current); |
| 275 | if (ret < 0) { |
| 276 | dev_err(dev, |
| 277 | "flash-max-microamp DT property missing\n"); |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | ret = of_property_read_u32(child_node, "flash-max-timeout-us", |
| 282 | &cfg->max_flash_tm); |
| 283 | if (ret < 0) { |
| 284 | dev_err(dev, |
| 285 | "flash-max-timeout-us DT property missing\n"); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | of_node_put(child_node); |
| 290 | |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 291 | *sub_node = child_node; |
| 292 | |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | static void aat1290_led_validate_mm_current(struct aat1290_led *led, |
| 297 | struct aat1290_led_config_data *cfg) |
| 298 | { |
| 299 | int i, b = 0, e = AAT1290_MM_CURRENT_SCALE_SIZE; |
| 300 | |
| 301 | while (e - b > 1) { |
| 302 | i = b + (e - b) / 2; |
| 303 | if (cfg->max_mm_current < led->mm_current_scale[i]) |
| 304 | e = i; |
| 305 | else |
| 306 | b = i; |
| 307 | } |
| 308 | |
| 309 | cfg->max_mm_current = led->mm_current_scale[b]; |
| 310 | cfg->max_brightness = b + 1; |
| 311 | } |
| 312 | |
Jacek Anaszewski | 305c324 | 2015-09-04 12:27:09 +0200 | [diff] [blame] | 313 | static int init_mm_current_scale(struct aat1290_led *led, |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 314 | struct aat1290_led_config_data *cfg) |
| 315 | { |
| 316 | int max_mm_current_percent[] = { 20, 22, 25, 28, 32, 36, 40, 45, 50, 56, |
| 317 | 63, 71, 79, 89, 100 }; |
| 318 | int i, max_mm_current = |
| 319 | AAT1290_MAX_MM_CURRENT(cfg->max_flash_current); |
| 320 | |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 321 | led->mm_current_scale = devm_kzalloc(&led->pdev->dev, |
| 322 | sizeof(max_mm_current_percent), |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 323 | GFP_KERNEL); |
| 324 | if (!led->mm_current_scale) |
| 325 | return -ENOMEM; |
| 326 | |
| 327 | for (i = 0; i < AAT1290_MM_CURRENT_SCALE_SIZE; ++i) |
| 328 | led->mm_current_scale[i] = max_mm_current * |
| 329 | max_mm_current_percent[i] / 100; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int aat1290_led_get_configuration(struct aat1290_led *led, |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 335 | struct aat1290_led_config_data *cfg, |
| 336 | struct device_node **sub_node) |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 337 | { |
| 338 | int ret; |
| 339 | |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 340 | ret = aat1290_led_parse_dt(led, cfg, sub_node); |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 341 | if (ret < 0) |
| 342 | return ret; |
| 343 | /* |
| 344 | * Init non-linear movie mode current scale basing |
| 345 | * on the max flash current from led configuration. |
| 346 | */ |
| 347 | ret = init_mm_current_scale(led, cfg); |
| 348 | if (ret < 0) |
| 349 | return ret; |
| 350 | |
| 351 | aat1290_led_validate_mm_current(led, cfg); |
| 352 | |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 353 | #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) |
| 354 | #else |
| 355 | devm_kfree(&led->pdev->dev, led->mm_current_scale); |
| 356 | #endif |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static void aat1290_init_flash_timeout(struct aat1290_led *led, |
| 362 | struct aat1290_led_config_data *cfg) |
| 363 | { |
| 364 | struct led_classdev_flash *fled_cdev = &led->fled_cdev; |
| 365 | struct led_flash_setting *setting; |
| 366 | |
| 367 | /* Init flash timeout setting */ |
| 368 | setting = &fled_cdev->timeout; |
| 369 | setting->min = cfg->max_flash_tm / AAT1290_FLASH_TM_NUM_LEVELS; |
| 370 | setting->max = cfg->max_flash_tm; |
| 371 | setting->step = setting->min; |
| 372 | setting->val = setting->max; |
| 373 | } |
| 374 | |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 375 | #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) |
| 376 | static enum led_brightness aat1290_intensity_to_brightness( |
| 377 | struct v4l2_flash *v4l2_flash, |
| 378 | s32 intensity) |
| 379 | { |
| 380 | struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; |
| 381 | struct aat1290_led *led = fled_cdev_to_led(fled_cdev); |
| 382 | int i; |
| 383 | |
| 384 | for (i = AAT1290_MM_CURRENT_SCALE_SIZE - 1; i >= 0; --i) |
| 385 | if (intensity >= led->mm_current_scale[i]) |
| 386 | return i + 1; |
| 387 | |
| 388 | return 1; |
| 389 | } |
| 390 | |
| 391 | static s32 aat1290_brightness_to_intensity(struct v4l2_flash *v4l2_flash, |
| 392 | enum led_brightness brightness) |
| 393 | { |
| 394 | struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; |
| 395 | struct aat1290_led *led = fled_cdev_to_led(fled_cdev); |
| 396 | |
| 397 | return led->mm_current_scale[brightness - 1]; |
| 398 | } |
| 399 | |
| 400 | static int aat1290_led_external_strobe_set(struct v4l2_flash *v4l2_flash, |
| 401 | bool enable) |
| 402 | { |
| 403 | struct aat1290_led *led = fled_cdev_to_led(v4l2_flash->fled_cdev); |
| 404 | struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; |
| 405 | struct led_classdev *led_cdev = &fled_cdev->led_cdev; |
| 406 | struct pinctrl *pinctrl; |
| 407 | |
| 408 | gpiod_direction_output(led->gpio_fl_en, 0); |
| 409 | gpiod_direction_output(led->gpio_en_set, 0); |
| 410 | |
| 411 | led->movie_mode = false; |
| 412 | led_cdev->brightness = 0; |
| 413 | |
| 414 | pinctrl = devm_pinctrl_get_select(&led->pdev->dev, |
| 415 | enable ? "isp" : "host"); |
| 416 | if (IS_ERR(pinctrl)) { |
| 417 | dev_warn(&led->pdev->dev, "Unable to switch strobe source.\n"); |
| 418 | return PTR_ERR(pinctrl); |
| 419 | } |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static void aat1290_init_v4l2_flash_config(struct aat1290_led *led, |
| 425 | struct aat1290_led_config_data *led_cfg, |
| 426 | struct v4l2_flash_config *v4l2_sd_cfg) |
| 427 | { |
| 428 | struct led_classdev *led_cdev = &led->fled_cdev.led_cdev; |
| 429 | struct led_flash_setting *s; |
| 430 | |
| 431 | strlcpy(v4l2_sd_cfg->dev_name, led_cdev->name, |
| 432 | sizeof(v4l2_sd_cfg->dev_name)); |
| 433 | |
| 434 | s = &v4l2_sd_cfg->torch_intensity; |
| 435 | s->min = led->mm_current_scale[0]; |
| 436 | s->max = led_cfg->max_mm_current; |
| 437 | s->step = 1; |
| 438 | s->val = s->max; |
| 439 | |
| 440 | v4l2_sd_cfg->has_external_strobe = led_cfg->has_external_strobe; |
| 441 | } |
| 442 | |
| 443 | static const struct v4l2_flash_ops v4l2_flash_ops = { |
| 444 | .external_strobe_set = aat1290_led_external_strobe_set, |
| 445 | .intensity_to_led_brightness = aat1290_intensity_to_brightness, |
| 446 | .led_brightness_to_intensity = aat1290_brightness_to_intensity, |
| 447 | }; |
| 448 | #else |
| 449 | static inline void aat1290_init_v4l2_flash_config(struct aat1290_led *led, |
| 450 | struct aat1290_led_config_data *led_cfg, |
| 451 | struct v4l2_flash_config *v4l2_sd_cfg) |
| 452 | { |
| 453 | } |
| 454 | static const struct v4l2_flash_ops v4l2_flash_ops; |
| 455 | #endif |
| 456 | |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 457 | static const struct led_flash_ops flash_ops = { |
| 458 | .strobe_set = aat1290_led_flash_strobe_set, |
| 459 | .timeout_set = aat1290_led_flash_timeout_set, |
| 460 | }; |
| 461 | |
| 462 | static int aat1290_led_probe(struct platform_device *pdev) |
| 463 | { |
| 464 | struct device *dev = &pdev->dev; |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 465 | struct device_node *sub_node = NULL; |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 466 | struct aat1290_led *led; |
| 467 | struct led_classdev *led_cdev; |
| 468 | struct led_classdev_flash *fled_cdev; |
| 469 | struct aat1290_led_config_data led_cfg = {}; |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 470 | struct v4l2_flash_config v4l2_sd_cfg = {}; |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 471 | int ret; |
| 472 | |
| 473 | led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL); |
| 474 | if (!led) |
| 475 | return -ENOMEM; |
| 476 | |
| 477 | led->pdev = pdev; |
| 478 | platform_set_drvdata(pdev, led); |
| 479 | |
| 480 | fled_cdev = &led->fled_cdev; |
| 481 | fled_cdev->ops = &flash_ops; |
| 482 | led_cdev = &fled_cdev->led_cdev; |
| 483 | |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 484 | ret = aat1290_led_get_configuration(led, &led_cfg, &sub_node); |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 485 | if (ret < 0) |
| 486 | return ret; |
| 487 | |
| 488 | mutex_init(&led->lock); |
| 489 | |
| 490 | /* Initialize LED Flash class device */ |
Jacek Anaszewski | 269e92d | 2015-10-07 11:10:46 +0200 | [diff] [blame^] | 491 | led_cdev->brightness_set_blocking = aat1290_led_brightness_set; |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 492 | led_cdev->max_brightness = led_cfg.max_brightness; |
| 493 | led_cdev->flags |= LED_DEV_CAP_FLASH; |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 494 | |
| 495 | aat1290_init_flash_timeout(led, &led_cfg); |
| 496 | |
| 497 | /* Register LED Flash class device */ |
| 498 | ret = led_classdev_flash_register(&pdev->dev, fled_cdev); |
| 499 | if (ret < 0) |
| 500 | goto err_flash_register; |
| 501 | |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 502 | aat1290_init_v4l2_flash_config(led, &led_cfg, &v4l2_sd_cfg); |
| 503 | |
| 504 | /* Create V4L2 Flash subdev. */ |
| 505 | led->v4l2_flash = v4l2_flash_init(dev, sub_node, fled_cdev, NULL, |
| 506 | &v4l2_flash_ops, &v4l2_sd_cfg); |
| 507 | if (IS_ERR(led->v4l2_flash)) { |
| 508 | ret = PTR_ERR(led->v4l2_flash); |
| 509 | goto error_v4l2_flash_init; |
| 510 | } |
| 511 | |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 512 | return 0; |
| 513 | |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 514 | error_v4l2_flash_init: |
| 515 | led_classdev_flash_unregister(fled_cdev); |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 516 | err_flash_register: |
| 517 | mutex_destroy(&led->lock); |
| 518 | |
| 519 | return ret; |
| 520 | } |
| 521 | |
| 522 | static int aat1290_led_remove(struct platform_device *pdev) |
| 523 | { |
| 524 | struct aat1290_led *led = platform_get_drvdata(pdev); |
| 525 | |
Jacek Anaszewski | ac69b90 | 2015-06-19 00:33:22 -0700 | [diff] [blame] | 526 | v4l2_flash_release(led->v4l2_flash); |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 527 | led_classdev_flash_unregister(&led->fled_cdev); |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 528 | |
| 529 | mutex_destroy(&led->lock); |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static const struct of_device_id aat1290_led_dt_match[] = { |
| 535 | { .compatible = "skyworks,aat1290" }, |
| 536 | {}, |
| 537 | }; |
Luis de Bethencourt | 93a51aa | 2015-09-01 23:35:07 +0200 | [diff] [blame] | 538 | MODULE_DEVICE_TABLE(of, aat1290_led_dt_match); |
Jacek Anaszewski | 49c34b8 | 2015-04-30 03:34:57 -0700 | [diff] [blame] | 539 | |
| 540 | static struct platform_driver aat1290_led_driver = { |
| 541 | .probe = aat1290_led_probe, |
| 542 | .remove = aat1290_led_remove, |
| 543 | .driver = { |
| 544 | .name = "aat1290", |
| 545 | .of_match_table = aat1290_led_dt_match, |
| 546 | }, |
| 547 | }; |
| 548 | |
| 549 | module_platform_driver(aat1290_led_driver); |
| 550 | |
| 551 | MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>"); |
| 552 | MODULE_DESCRIPTION("Skyworks Current Regulator for Flash LEDs"); |
| 553 | MODULE_LICENSE("GPL v2"); |