ASoC: Intel: bytcht_es8316: Fix Irbis NB41 netbook quirk
[linux/fpc-iii.git] / drivers / leds / leds-aat1290.c
blob5a0fe7b7b8bc7ed03bd519b60970e9386ea728f6
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * LED Flash class driver for the AAT1290
4 * 1.5A Step-Up Current Regulator for Flash LEDs
6 * Copyright (C) 2015, Samsung Electronics Co., Ltd.
7 * Author: Jacek Anaszewski <j.anaszewski@samsung.com>
8 */
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/led-class-flash.h>
13 #include <linux/leds.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/of.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <media/v4l2-flash-led-class.h>
22 #define AAT1290_MOVIE_MODE_CURRENT_ADDR 17
23 #define AAT1290_MAX_MM_CURR_PERCENT_0 16
24 #define AAT1290_MAX_MM_CURR_PERCENT_100 1
26 #define AAT1290_FLASH_SAFETY_TIMER_ADDR 18
28 #define AAT1290_MOVIE_MODE_CONFIG_ADDR 19
29 #define AAT1290_MOVIE_MODE_OFF 1
30 #define AAT1290_MOVIE_MODE_ON 3
32 #define AAT1290_MM_CURRENT_RATIO_ADDR 20
33 #define AAT1290_MM_TO_FL_1_92 1
35 #define AAT1290_MM_TO_FL_RATIO 1000 / 1920
36 #define AAT1290_MAX_MM_CURRENT(fl_max) (fl_max * AAT1290_MM_TO_FL_RATIO)
38 #define AAT1290_LATCH_TIME_MIN_US 500
39 #define AAT1290_LATCH_TIME_MAX_US 1000
40 #define AAT1290_EN_SET_TICK_TIME_US 1
41 #define AAT1290_FLEN_OFF_DELAY_TIME_US 10
42 #define AAT1290_FLASH_TM_NUM_LEVELS 16
43 #define AAT1290_MM_CURRENT_SCALE_SIZE 15
45 #define AAT1290_NAME "aat1290"
48 struct aat1290_led_config_data {
49 /* maximum LED current in movie mode */
50 u32 max_mm_current;
51 /* maximum LED current in flash mode */
52 u32 max_flash_current;
53 /* maximum flash timeout */
54 u32 max_flash_tm;
55 /* external strobe capability */
56 bool has_external_strobe;
57 /* max LED brightness level */
58 enum led_brightness max_brightness;
61 struct aat1290_led {
62 /* platform device data */
63 struct platform_device *pdev;
64 /* secures access to the device */
65 struct mutex lock;
67 /* corresponding LED Flash class device */
68 struct led_classdev_flash fled_cdev;
69 /* V4L2 Flash device */
70 struct v4l2_flash *v4l2_flash;
72 /* FLEN pin */
73 struct gpio_desc *gpio_fl_en;
74 /* EN|SET pin */
75 struct gpio_desc *gpio_en_set;
76 /* movie mode current scale */
77 int *mm_current_scale;
78 /* device mode */
79 bool movie_mode;
80 /* brightness cache */
81 unsigned int torch_brightness;
84 static struct aat1290_led *fled_cdev_to_led(
85 struct led_classdev_flash *fled_cdev)
87 return container_of(fled_cdev, struct aat1290_led, fled_cdev);
90 static struct led_classdev_flash *led_cdev_to_fled_cdev(
91 struct led_classdev *led_cdev)
93 return container_of(led_cdev, struct led_classdev_flash, led_cdev);
96 static void aat1290_as2cwire_write(struct aat1290_led *led, int addr, int value)
98 int i;
100 gpiod_direction_output(led->gpio_fl_en, 0);
101 gpiod_direction_output(led->gpio_en_set, 0);
103 udelay(AAT1290_FLEN_OFF_DELAY_TIME_US);
105 /* write address */
106 for (i = 0; i < addr; ++i) {
107 udelay(AAT1290_EN_SET_TICK_TIME_US);
108 gpiod_direction_output(led->gpio_en_set, 0);
109 udelay(AAT1290_EN_SET_TICK_TIME_US);
110 gpiod_direction_output(led->gpio_en_set, 1);
113 usleep_range(AAT1290_LATCH_TIME_MIN_US, AAT1290_LATCH_TIME_MAX_US);
115 /* write data */
116 for (i = 0; i < value; ++i) {
117 udelay(AAT1290_EN_SET_TICK_TIME_US);
118 gpiod_direction_output(led->gpio_en_set, 0);
119 udelay(AAT1290_EN_SET_TICK_TIME_US);
120 gpiod_direction_output(led->gpio_en_set, 1);
123 usleep_range(AAT1290_LATCH_TIME_MIN_US, AAT1290_LATCH_TIME_MAX_US);
126 static void aat1290_set_flash_safety_timer(struct aat1290_led *led,
127 unsigned int micro_sec)
129 struct led_classdev_flash *fled_cdev = &led->fled_cdev;
130 struct led_flash_setting *flash_tm = &fled_cdev->timeout;
131 int flash_tm_reg = AAT1290_FLASH_TM_NUM_LEVELS -
132 (micro_sec / flash_tm->step) + 1;
134 aat1290_as2cwire_write(led, AAT1290_FLASH_SAFETY_TIMER_ADDR,
135 flash_tm_reg);
138 /* LED subsystem callbacks */
140 static int aat1290_led_brightness_set(struct led_classdev *led_cdev,
141 enum led_brightness brightness)
143 struct led_classdev_flash *fled_cdev = led_cdev_to_fled_cdev(led_cdev);
144 struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
146 mutex_lock(&led->lock);
148 if (brightness == 0) {
149 gpiod_direction_output(led->gpio_fl_en, 0);
150 gpiod_direction_output(led->gpio_en_set, 0);
151 led->movie_mode = false;
152 } else {
153 if (!led->movie_mode) {
154 aat1290_as2cwire_write(led,
155 AAT1290_MM_CURRENT_RATIO_ADDR,
156 AAT1290_MM_TO_FL_1_92);
157 led->movie_mode = true;
160 aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CURRENT_ADDR,
161 AAT1290_MAX_MM_CURR_PERCENT_0 - brightness);
162 aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CONFIG_ADDR,
163 AAT1290_MOVIE_MODE_ON);
166 mutex_unlock(&led->lock);
168 return 0;
171 static int aat1290_led_flash_strobe_set(struct led_classdev_flash *fled_cdev,
172 bool state)
175 struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
176 struct led_classdev *led_cdev = &fled_cdev->led_cdev;
177 struct led_flash_setting *timeout = &fled_cdev->timeout;
179 mutex_lock(&led->lock);
181 if (state) {
182 aat1290_set_flash_safety_timer(led, timeout->val);
183 gpiod_direction_output(led->gpio_fl_en, 1);
184 } else {
185 gpiod_direction_output(led->gpio_fl_en, 0);
186 gpiod_direction_output(led->gpio_en_set, 0);
190 * To reenter movie mode after a flash event the part must be cycled
191 * off and back on to reset the movie mode and reprogrammed via the
192 * AS2Cwire. Therefore the brightness and movie_mode properties needs
193 * to be updated here to reflect the actual state.
195 led_cdev->brightness = 0;
196 led->movie_mode = false;
198 mutex_unlock(&led->lock);
200 return 0;
203 static int aat1290_led_flash_timeout_set(struct led_classdev_flash *fled_cdev,
204 u32 timeout)
207 * Don't do anything - flash timeout is cached in the led-class-flash
208 * core and will be applied in the strobe_set op, as writing the
209 * safety timer register spuriously turns the torch mode on.
212 return 0;
215 static int aat1290_led_parse_dt(struct aat1290_led *led,
216 struct aat1290_led_config_data *cfg,
217 struct device_node **sub_node)
219 struct device *dev = &led->pdev->dev;
220 struct device_node *child_node;
221 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
222 struct pinctrl *pinctrl;
223 #endif
224 int ret = 0;
226 led->gpio_fl_en = devm_gpiod_get(dev, "flen", GPIOD_ASIS);
227 if (IS_ERR(led->gpio_fl_en)) {
228 ret = PTR_ERR(led->gpio_fl_en);
229 dev_err(dev, "Unable to claim gpio \"flen\".\n");
230 return ret;
233 led->gpio_en_set = devm_gpiod_get(dev, "enset", GPIOD_ASIS);
234 if (IS_ERR(led->gpio_en_set)) {
235 ret = PTR_ERR(led->gpio_en_set);
236 dev_err(dev, "Unable to claim gpio \"enset\".\n");
237 return ret;
240 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
241 pinctrl = devm_pinctrl_get_select_default(&led->pdev->dev);
242 if (IS_ERR(pinctrl)) {
243 cfg->has_external_strobe = false;
244 dev_info(dev,
245 "No support for external strobe detected.\n");
246 } else {
247 cfg->has_external_strobe = true;
249 #endif
251 child_node = of_get_next_available_child(dev->of_node, NULL);
252 if (!child_node) {
253 dev_err(dev, "No DT child node found for connected LED.\n");
254 return -EINVAL;
257 ret = of_property_read_u32(child_node, "led-max-microamp",
258 &cfg->max_mm_current);
260 * led-max-microamp will default to 1/20 of flash-max-microamp
261 * in case it is missing.
263 if (ret < 0)
264 dev_warn(dev,
265 "led-max-microamp DT property missing\n");
267 ret = of_property_read_u32(child_node, "flash-max-microamp",
268 &cfg->max_flash_current);
269 if (ret < 0) {
270 dev_err(dev,
271 "flash-max-microamp DT property missing\n");
272 goto err_parse_dt;
275 ret = of_property_read_u32(child_node, "flash-max-timeout-us",
276 &cfg->max_flash_tm);
277 if (ret < 0) {
278 dev_err(dev,
279 "flash-max-timeout-us DT property missing\n");
280 goto err_parse_dt;
283 *sub_node = child_node;
285 err_parse_dt:
286 of_node_put(child_node);
288 return ret;
291 static void aat1290_led_validate_mm_current(struct aat1290_led *led,
292 struct aat1290_led_config_data *cfg)
294 int i, b = 0, e = AAT1290_MM_CURRENT_SCALE_SIZE;
296 while (e - b > 1) {
297 i = b + (e - b) / 2;
298 if (cfg->max_mm_current < led->mm_current_scale[i])
299 e = i;
300 else
301 b = i;
304 cfg->max_mm_current = led->mm_current_scale[b];
305 cfg->max_brightness = b + 1;
308 static int init_mm_current_scale(struct aat1290_led *led,
309 struct aat1290_led_config_data *cfg)
311 static const int max_mm_current_percent[] = {
312 20, 22, 25, 28, 32, 36, 40, 45, 50, 56,
313 63, 71, 79, 89, 100
315 int i, max_mm_current =
316 AAT1290_MAX_MM_CURRENT(cfg->max_flash_current);
318 led->mm_current_scale = devm_kzalloc(&led->pdev->dev,
319 sizeof(max_mm_current_percent),
320 GFP_KERNEL);
321 if (!led->mm_current_scale)
322 return -ENOMEM;
324 for (i = 0; i < AAT1290_MM_CURRENT_SCALE_SIZE; ++i)
325 led->mm_current_scale[i] = max_mm_current *
326 max_mm_current_percent[i] / 100;
328 return 0;
331 static int aat1290_led_get_configuration(struct aat1290_led *led,
332 struct aat1290_led_config_data *cfg,
333 struct device_node **sub_node)
335 int ret;
337 ret = aat1290_led_parse_dt(led, cfg, sub_node);
338 if (ret < 0)
339 return ret;
341 * Init non-linear movie mode current scale basing
342 * on the max flash current from led configuration.
344 ret = init_mm_current_scale(led, cfg);
345 if (ret < 0)
346 return ret;
348 aat1290_led_validate_mm_current(led, cfg);
350 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
351 #else
352 devm_kfree(&led->pdev->dev, led->mm_current_scale);
353 #endif
355 return 0;
358 static void aat1290_init_flash_timeout(struct aat1290_led *led,
359 struct aat1290_led_config_data *cfg)
361 struct led_classdev_flash *fled_cdev = &led->fled_cdev;
362 struct led_flash_setting *setting;
364 /* Init flash timeout setting */
365 setting = &fled_cdev->timeout;
366 setting->min = cfg->max_flash_tm / AAT1290_FLASH_TM_NUM_LEVELS;
367 setting->max = cfg->max_flash_tm;
368 setting->step = setting->min;
369 setting->val = setting->max;
372 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
373 static enum led_brightness aat1290_intensity_to_brightness(
374 struct v4l2_flash *v4l2_flash,
375 s32 intensity)
377 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
378 struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
379 int i;
381 for (i = AAT1290_MM_CURRENT_SCALE_SIZE - 1; i >= 0; --i)
382 if (intensity >= led->mm_current_scale[i])
383 return i + 1;
385 return 1;
388 static s32 aat1290_brightness_to_intensity(struct v4l2_flash *v4l2_flash,
389 enum led_brightness brightness)
391 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
392 struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
394 return led->mm_current_scale[brightness - 1];
397 static int aat1290_led_external_strobe_set(struct v4l2_flash *v4l2_flash,
398 bool enable)
400 struct aat1290_led *led = fled_cdev_to_led(v4l2_flash->fled_cdev);
401 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
402 struct led_classdev *led_cdev = &fled_cdev->led_cdev;
403 struct pinctrl *pinctrl;
405 gpiod_direction_output(led->gpio_fl_en, 0);
406 gpiod_direction_output(led->gpio_en_set, 0);
408 led->movie_mode = false;
409 led_cdev->brightness = 0;
411 pinctrl = devm_pinctrl_get_select(&led->pdev->dev,
412 enable ? "isp" : "host");
413 if (IS_ERR(pinctrl)) {
414 dev_warn(&led->pdev->dev, "Unable to switch strobe source.\n");
415 return PTR_ERR(pinctrl);
418 return 0;
421 static void aat1290_init_v4l2_flash_config(struct aat1290_led *led,
422 struct aat1290_led_config_data *led_cfg,
423 struct v4l2_flash_config *v4l2_sd_cfg)
425 struct led_classdev *led_cdev = &led->fled_cdev.led_cdev;
426 struct led_flash_setting *s;
428 strlcpy(v4l2_sd_cfg->dev_name, led_cdev->dev->kobj.name,
429 sizeof(v4l2_sd_cfg->dev_name));
431 s = &v4l2_sd_cfg->intensity;
432 s->min = led->mm_current_scale[0];
433 s->max = led_cfg->max_mm_current;
434 s->step = 1;
435 s->val = s->max;
437 v4l2_sd_cfg->has_external_strobe = led_cfg->has_external_strobe;
440 static const struct v4l2_flash_ops v4l2_flash_ops = {
441 .external_strobe_set = aat1290_led_external_strobe_set,
442 .intensity_to_led_brightness = aat1290_intensity_to_brightness,
443 .led_brightness_to_intensity = aat1290_brightness_to_intensity,
445 #else
446 static inline void aat1290_init_v4l2_flash_config(struct aat1290_led *led,
447 struct aat1290_led_config_data *led_cfg,
448 struct v4l2_flash_config *v4l2_sd_cfg)
451 static const struct v4l2_flash_ops v4l2_flash_ops;
452 #endif
454 static const struct led_flash_ops flash_ops = {
455 .strobe_set = aat1290_led_flash_strobe_set,
456 .timeout_set = aat1290_led_flash_timeout_set,
459 static int aat1290_led_probe(struct platform_device *pdev)
461 struct device *dev = &pdev->dev;
462 struct device_node *sub_node = NULL;
463 struct aat1290_led *led;
464 struct led_classdev *led_cdev;
465 struct led_classdev_flash *fled_cdev;
466 struct led_init_data init_data = {};
467 struct aat1290_led_config_data led_cfg = {};
468 struct v4l2_flash_config v4l2_sd_cfg = {};
469 int ret;
471 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
472 if (!led)
473 return -ENOMEM;
475 led->pdev = pdev;
476 platform_set_drvdata(pdev, led);
478 fled_cdev = &led->fled_cdev;
479 fled_cdev->ops = &flash_ops;
480 led_cdev = &fled_cdev->led_cdev;
482 ret = aat1290_led_get_configuration(led, &led_cfg, &sub_node);
483 if (ret < 0)
484 return ret;
486 mutex_init(&led->lock);
488 /* Initialize LED Flash class device */
489 led_cdev->brightness_set_blocking = aat1290_led_brightness_set;
490 led_cdev->max_brightness = led_cfg.max_brightness;
491 led_cdev->flags |= LED_DEV_CAP_FLASH;
493 aat1290_init_flash_timeout(led, &led_cfg);
495 init_data.fwnode = of_fwnode_handle(sub_node);
496 init_data.devicename = AAT1290_NAME;
498 /* Register LED Flash class device */
499 ret = led_classdev_flash_register_ext(&pdev->dev, fled_cdev,
500 &init_data);
501 if (ret < 0)
502 goto err_flash_register;
504 aat1290_init_v4l2_flash_config(led, &led_cfg, &v4l2_sd_cfg);
506 /* Create V4L2 Flash subdev. */
507 led->v4l2_flash = v4l2_flash_init(dev, of_fwnode_handle(sub_node),
508 fled_cdev, &v4l2_flash_ops,
509 &v4l2_sd_cfg);
510 if (IS_ERR(led->v4l2_flash)) {
511 ret = PTR_ERR(led->v4l2_flash);
512 goto error_v4l2_flash_init;
515 return 0;
517 error_v4l2_flash_init:
518 led_classdev_flash_unregister(fled_cdev);
519 err_flash_register:
520 mutex_destroy(&led->lock);
522 return ret;
525 static int aat1290_led_remove(struct platform_device *pdev)
527 struct aat1290_led *led = platform_get_drvdata(pdev);
529 v4l2_flash_release(led->v4l2_flash);
530 led_classdev_flash_unregister(&led->fled_cdev);
532 mutex_destroy(&led->lock);
534 return 0;
537 static const struct of_device_id aat1290_led_dt_match[] = {
538 { .compatible = "skyworks,aat1290" },
541 MODULE_DEVICE_TABLE(of, aat1290_led_dt_match);
543 static struct platform_driver aat1290_led_driver = {
544 .probe = aat1290_led_probe,
545 .remove = aat1290_led_remove,
546 .driver = {
547 .name = "aat1290",
548 .of_match_table = aat1290_led_dt_match,
552 module_platform_driver(aat1290_led_driver);
554 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
555 MODULE_DESCRIPTION("Skyworks Current Regulator for Flash LEDs");
556 MODULE_LICENSE("GPL v2");