Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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