Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / leds / leds-aat1290.c
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
5  *
6  *      Copyright (C) 2015, Samsung Electronics Co., Ltd.
7  *      Author: Jacek Anaszewski <j.anaszewski@samsung.com>
8  */
9
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>
21
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
25
26 #define AAT1290_FLASH_SAFETY_TIMER_ADDR 18
27
28 #define AAT1290_MOVIE_MODE_CONFIG_ADDR  19
29 #define AAT1290_MOVIE_MODE_OFF          1
30 #define AAT1290_MOVIE_MODE_ON           3
31
32 #define AAT1290_MM_CURRENT_RATIO_ADDR   20
33 #define AAT1290_MM_TO_FL_1_92           1
34
35 #define AAT1290_MM_TO_FL_RATIO          1000 / 1920
36 #define AAT1290_MAX_MM_CURRENT(fl_max)  (fl_max * AAT1290_MM_TO_FL_RATIO)
37
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
44
45
46 struct aat1290_led_config_data {
47         /* maximum LED current in movie mode */
48         u32 max_mm_current;
49         /* maximum LED current in flash mode */
50         u32 max_flash_current;
51         /* maximum flash timeout */
52         u32 max_flash_tm;
53         /* external strobe capability */
54         bool has_external_strobe;
55         /* max LED brightness level */
56         enum led_brightness max_brightness;
57 };
58
59 struct aat1290_led {
60         /* platform device data */
61         struct platform_device *pdev;
62         /* secures access to the device */
63         struct mutex lock;
64
65         /* corresponding LED Flash class device */
66         struct led_classdev_flash fled_cdev;
67         /* V4L2 Flash device */
68         struct v4l2_flash *v4l2_flash;
69
70         /* FLEN pin */
71         struct gpio_desc *gpio_fl_en;
72         /* EN|SET pin  */
73         struct gpio_desc *gpio_en_set;
74         /* movie mode current scale */
75         int *mm_current_scale;
76         /* device mode */
77         bool movie_mode;
78
79         /* brightness cache */
80         unsigned int torch_brightness;
81 };
82
83 static struct aat1290_led *fled_cdev_to_led(
84                                 struct led_classdev_flash *fled_cdev)
85 {
86         return container_of(fled_cdev, struct aat1290_led, fled_cdev);
87 }
88
89 static struct led_classdev_flash *led_cdev_to_fled_cdev(
90                                 struct led_classdev *led_cdev)
91 {
92         return container_of(led_cdev, struct led_classdev_flash, led_cdev);
93 }
94
95 static void aat1290_as2cwire_write(struct aat1290_led *led, int addr, int value)
96 {
97         int i;
98
99         gpiod_direction_output(led->gpio_fl_en, 0);
100         gpiod_direction_output(led->gpio_en_set, 0);
101
102         udelay(AAT1290_FLEN_OFF_DELAY_TIME_US);
103
104         /* write address */
105         for (i = 0; i < addr; ++i) {
106                 udelay(AAT1290_EN_SET_TICK_TIME_US);
107                 gpiod_direction_output(led->gpio_en_set, 0);
108                 udelay(AAT1290_EN_SET_TICK_TIME_US);
109                 gpiod_direction_output(led->gpio_en_set, 1);
110         }
111
112         usleep_range(AAT1290_LATCH_TIME_MIN_US, AAT1290_LATCH_TIME_MAX_US);
113
114         /* write data */
115         for (i = 0; i < value; ++i) {
116                 udelay(AAT1290_EN_SET_TICK_TIME_US);
117                 gpiod_direction_output(led->gpio_en_set, 0);
118                 udelay(AAT1290_EN_SET_TICK_TIME_US);
119                 gpiod_direction_output(led->gpio_en_set, 1);
120         }
121
122         usleep_range(AAT1290_LATCH_TIME_MIN_US, AAT1290_LATCH_TIME_MAX_US);
123 }
124
125 static void aat1290_set_flash_safety_timer(struct aat1290_led *led,
126                                         unsigned int micro_sec)
127 {
128         struct led_classdev_flash *fled_cdev = &led->fled_cdev;
129         struct led_flash_setting *flash_tm = &fled_cdev->timeout;
130         int flash_tm_reg = AAT1290_FLASH_TM_NUM_LEVELS -
131                                 (micro_sec / flash_tm->step) + 1;
132
133         aat1290_as2cwire_write(led, AAT1290_FLASH_SAFETY_TIMER_ADDR,
134                                                         flash_tm_reg);
135 }
136
137 /* LED subsystem callbacks */
138
139 static int aat1290_led_brightness_set(struct led_classdev *led_cdev,
140                                         enum led_brightness brightness)
141 {
142         struct led_classdev_flash *fled_cdev = led_cdev_to_fled_cdev(led_cdev);
143         struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
144
145         mutex_lock(&led->lock);
146
147         if (brightness == 0) {
148                 gpiod_direction_output(led->gpio_fl_en, 0);
149                 gpiod_direction_output(led->gpio_en_set, 0);
150                 led->movie_mode = false;
151         } else {
152                 if (!led->movie_mode) {
153                         aat1290_as2cwire_write(led,
154                                 AAT1290_MM_CURRENT_RATIO_ADDR,
155                                 AAT1290_MM_TO_FL_1_92);
156                         led->movie_mode = true;
157                 }
158
159                 aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CURRENT_ADDR,
160                                 AAT1290_MAX_MM_CURR_PERCENT_0 - brightness);
161                 aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CONFIG_ADDR,
162                                 AAT1290_MOVIE_MODE_ON);
163         }
164
165         mutex_unlock(&led->lock);
166
167         return 0;
168 }
169
170 static int aat1290_led_flash_strobe_set(struct led_classdev_flash *fled_cdev,
171                                          bool state)
172
173 {
174         struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
175         struct led_classdev *led_cdev = &fled_cdev->led_cdev;
176         struct led_flash_setting *timeout = &fled_cdev->timeout;
177
178         mutex_lock(&led->lock);
179
180         if (state) {
181                 aat1290_set_flash_safety_timer(led, timeout->val);
182                 gpiod_direction_output(led->gpio_fl_en, 1);
183         } else {
184                 gpiod_direction_output(led->gpio_fl_en, 0);
185                 gpiod_direction_output(led->gpio_en_set, 0);
186         }
187
188         /*
189          * To reenter movie mode after a flash event the part must be cycled
190          * off and back on to reset the movie mode and reprogrammed via the
191          * AS2Cwire. Therefore the brightness and movie_mode properties needs
192          * to be updated here to reflect the actual state.
193          */
194         led_cdev->brightness = 0;
195         led->movie_mode = false;
196
197         mutex_unlock(&led->lock);
198
199         return 0;
200 }
201
202 static int aat1290_led_flash_timeout_set(struct led_classdev_flash *fled_cdev,
203                                                 u32 timeout)
204 {
205         /*
206          * Don't do anything - flash timeout is cached in the led-class-flash
207          * core and will be applied in the strobe_set op, as writing the
208          * safety timer register spuriously turns the torch mode on.
209          */
210
211         return 0;
212 }
213
214 static int aat1290_led_parse_dt(struct aat1290_led *led,
215                         struct aat1290_led_config_data *cfg,
216                         struct device_node **sub_node)
217 {
218         struct led_classdev *led_cdev = &led->fled_cdev.led_cdev;
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;
225
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;
231         }
232
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;
238         }
239
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;
248         }
249 #endif
250
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;
255         }
256
257         led_cdev->name = of_get_property(child_node, "label", NULL) ? :
258                                                 child_node->name;
259
260         ret = of_property_read_u32(child_node, "led-max-microamp",
261                                 &cfg->max_mm_current);
262         /*
263          * led-max-microamp will default to 1/20 of flash-max-microamp
264          * in case it is missing.
265          */
266         if (ret < 0)
267                 dev_warn(dev,
268                         "led-max-microamp DT property missing\n");
269
270         ret = of_property_read_u32(child_node, "flash-max-microamp",
271                                 &cfg->max_flash_current);
272         if (ret < 0) {
273                 dev_err(dev,
274                         "flash-max-microamp DT property missing\n");
275                 goto err_parse_dt;
276         }
277
278         ret = of_property_read_u32(child_node, "flash-max-timeout-us",
279                                 &cfg->max_flash_tm);
280         if (ret < 0) {
281                 dev_err(dev,
282                         "flash-max-timeout-us DT property missing\n");
283                 goto err_parse_dt;
284         }
285
286         *sub_node = child_node;
287
288 err_parse_dt:
289         of_node_put(child_node);
290
291         return ret;
292 }
293
294 static void aat1290_led_validate_mm_current(struct aat1290_led *led,
295                                         struct aat1290_led_config_data *cfg)
296 {
297         int i, b = 0, e = AAT1290_MM_CURRENT_SCALE_SIZE;
298
299         while (e - b > 1) {
300                 i = b + (e - b) / 2;
301                 if (cfg->max_mm_current < led->mm_current_scale[i])
302                         e = i;
303                 else
304                         b = i;
305         }
306
307         cfg->max_mm_current = led->mm_current_scale[b];
308         cfg->max_brightness = b + 1;
309 }
310
311 static int init_mm_current_scale(struct aat1290_led *led,
312                         struct aat1290_led_config_data *cfg)
313 {
314         static const int max_mm_current_percent[] = {
315                 20, 22, 25, 28, 32, 36, 40, 45, 50, 56,
316                 63, 71, 79, 89, 100
317         };
318         int i, max_mm_current =
319                         AAT1290_MAX_MM_CURRENT(cfg->max_flash_current);
320
321         led->mm_current_scale = devm_kzalloc(&led->pdev->dev,
322                                         sizeof(max_mm_current_percent),
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,
335                                         struct aat1290_led_config_data *cfg,
336                                         struct device_node **sub_node)
337 {
338         int ret;
339
340         ret = aat1290_led_parse_dt(led, cfg, sub_node);
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
353 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
354 #else
355         devm_kfree(&led->pdev->dev, led->mm_current_scale);
356 #endif
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
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->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
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;
465         struct device_node *sub_node = NULL;
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 = {};
470         struct v4l2_flash_config v4l2_sd_cfg = {};
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
484         ret = aat1290_led_get_configuration(led, &led_cfg, &sub_node);
485         if (ret < 0)
486                 return ret;
487
488         mutex_init(&led->lock);
489
490         /* Initialize LED Flash class device */
491         led_cdev->brightness_set_blocking = aat1290_led_brightness_set;
492         led_cdev->max_brightness = led_cfg.max_brightness;
493         led_cdev->flags |= LED_DEV_CAP_FLASH;
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
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, of_fwnode_handle(sub_node),
506                                           fled_cdev, &v4l2_flash_ops,
507                                           &v4l2_sd_cfg);
508         if (IS_ERR(led->v4l2_flash)) {
509                 ret = PTR_ERR(led->v4l2_flash);
510                 goto error_v4l2_flash_init;
511         }
512
513         return 0;
514
515 error_v4l2_flash_init:
516         led_classdev_flash_unregister(fled_cdev);
517 err_flash_register:
518         mutex_destroy(&led->lock);
519
520         return ret;
521 }
522
523 static int aat1290_led_remove(struct platform_device *pdev)
524 {
525         struct aat1290_led *led = platform_get_drvdata(pdev);
526
527         v4l2_flash_release(led->v4l2_flash);
528         led_classdev_flash_unregister(&led->fled_cdev);
529
530         mutex_destroy(&led->lock);
531
532         return 0;
533 }
534
535 static const struct of_device_id aat1290_led_dt_match[] = {
536         { .compatible = "skyworks,aat1290" },
537         {},
538 };
539 MODULE_DEVICE_TABLE(of, aat1290_led_dt_match);
540
541 static struct platform_driver aat1290_led_driver = {
542         .probe          = aat1290_led_probe,
543         .remove         = aat1290_led_remove,
544         .driver         = {
545                 .name   = "aat1290",
546                 .of_match_table = aat1290_led_dt_match,
547         },
548 };
549
550 module_platform_driver(aat1290_led_driver);
551
552 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
553 MODULE_DESCRIPTION("Skyworks Current Regulator for Flash LEDs");
554 MODULE_LICENSE("GPL v2");