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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * General device driver for awinic aw36518, FLASH LED Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2022 Fuzhou Rockchip Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * V0.0X01.0X00 init version.
^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/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/rk-camera-module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/rk-led-flash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/version.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <media/v4l2-ctrls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <media/v4l2-device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define DRIVER_VERSION			KERNEL_VERSION(0, 0x01, 0x0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define AW36518_NAME			"aw36518"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define AW36518_REG_ID			0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define AW36518_ID			0x30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define AW36518_REG_MODE		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define AW36518_REG_LED0_FLASH_CUR	0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define AW36518_REG_LED0_TORCH_CUR	0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define AW36518_REG_LED1_FLASH_CUR	0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define AW36518_REG_LED1_TORCH_CUR	0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define AW36518_REG_FAULT		0x0A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define AW36518_REG_FL_SC		0x70
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define AW36518_REG_FL_OT		BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define AW36518_REG_FL_TO		BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define AW36518_REG_FAULT2		0x0B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define AW36518_REG_FL_OVP		BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define AW36518_REG_ENABLE		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define LED_ON				0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define LED_OFF				0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) /*  FLASH Brightness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *	min 2940uA, step 5870uA, max 1500000uA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define AW36518_MIN_FLASH_INTENSITY	2940
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define AW36518_MAX_FLASH_INTENSITY	1500000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define AW36518_FLASH_INTENSITY_DEFAULT	748430
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define AW36518_FLASH_INTENSITY_STEP	5870
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define AW36518_FLASH_BRT_uA_TO_REG(a)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	((a) < AW36518_MIN_FLASH_INTENSITY ? 0 :	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	 (((a) - AW36518_MIN_FLASH_INTENSITY) / AW36518_FLASH_INTENSITY_STEP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define AW36518_FLASH_BRT_REG_TO_uA(a)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	((a) * AW36518_FLASH_INTENSITY_STEP + AW36518_MIN_FLASH_INTENSITY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) /*  TORCH BRT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  *	min 750uA, step 1510uA, max 3860000uA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define AW36518_MIN_TORCH_INTENSITY	750
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #define AW36518_MAX_TORCH_INTENSITY	386000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #define AW36518_TORCH_INTENSITY_DEFAULT	192000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #define AW36518_TORCH_INTENSITY_STEP	1510
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #define AW36518_TORCH_BRT_uA_TO_REG(a)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	((a) < AW36518_MIN_TORCH_INTENSITY ? 0 :	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	 (((a) - AW36518_MIN_TORCH_INTENSITY) / AW36518_TORCH_INTENSITY_STEP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define AW36518_TORCH_BRT_REG_TO_uA(a)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	((a) * AW36518_TORCH_INTENSITY_STEP + AW36518_MIN_TORCH_INTENSITY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) /*  FLASH TIMEOUT DURATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  *	min 40ms, step 40 or 200ms, max 1600ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) #define TIMEOUT_MAX			1600000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #define TIMEOUT_STEP			40000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #define TIMEOUT_MIN			40000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #define TIMEOUT_STEP2			200000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #define TIMEOUT_DEFAULT			600000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #define AW36518_FLASH_TOUT_ms_TO_REG(a)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	((a) < TIMEOUT_MIN ? 0 :	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	 (((a) - TIMEOUT_MIN) / TIMEOUT_STEP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) #define AW36518_FLASH_TOUT_REG_TO_ms(a)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	((a) * TIMEOUT_STEP + TIMEOUT_MIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static int debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) module_param(debug, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) MODULE_PARM_DESC(debug, "debug level (0-2)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) enum aw36518_led_id {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	LED0 = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	LED1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	LED_MAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct aw36518_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct v4l2_subdev sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	struct v4l2_ctrl_handler ctrls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct v4l2_ctrl *flash_brt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct v4l2_ctrl *torch_brt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct __kernel_old_timeval timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	u32 max_flash_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	u32 max_flash_intensity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	u32 max_torch_intensity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct aw36518_flash {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct aw36518_led leds[LED_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct gpio_desc *en_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct gpio_desc *torch_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct gpio_desc *strobe_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	struct gpio_desc *tx_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	u32 flash_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	enum v4l2_flash_led_mode led_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	u32 module_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	const char *module_facing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	bool			power_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #define to_led(sd) container_of(sd, struct aw36518_led, sd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int aw36518_i2c_write(struct aw36518_flash *flash, u8 reg, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct i2c_client *client = flash->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	ret = i2c_smbus_write_byte_data(client, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			 "%s: reg:0x%x val:0x%x failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			 __func__, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	v4l2_dbg(2, debug, &flash->leds[0].sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		 "%s: reg:0x%x val:0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		  __func__, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static int aw36518_i2c_read(struct aw36518_flash *flash, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct i2c_client *client = flash->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	ret = i2c_smbus_read_byte_data(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			 "%s: reg:0x%x failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			 __func__, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	dev_dbg(&client->dev, "%s: reg:0x%x val:0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		 __func__, reg, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return ret;
^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) static int aw36518_led_on(struct aw36518_flash *flash, bool on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct i2c_client *client = flash->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	int temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	u8 val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	v4l2_dbg(1, debug, &flash->leds[0].sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		 "%s: on:%d\n", __func__, on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	temp = aw36518_i2c_read(flash, AW36518_REG_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (temp < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			 "%s: read reg:0x%x failed.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			 __func__, AW36518_REG_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (flash->led_mode == V4L2_FLASH_LED_MODE_FLASH && on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		temp = temp | 0x0c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		temp = temp & 0xfc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	v4l2_dbg(1, debug, &flash->leds[0].sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		 "%s: temp:%d\n", __func__, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	val = on ? LED_ON : LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	ret = aw36518_i2c_write(flash, AW36518_REG_ENABLE, val | temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	flash->leds[0].timestamp = ns_to_kernel_old_timeval(ktime_get_ns());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	flash->leds[1].timestamp = flash->leds[0].timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static int aw36518_get_fault(struct aw36518_flash *flash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	int fault = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	int temp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	fault = aw36518_i2c_read(flash, AW36518_REG_FAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	v4l2_dbg(1, debug, &flash->leds[0].sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		 "%s: 0x%x\n", __func__, fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	fault = fault & 0xfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	temp = aw36518_i2c_read(flash, AW36518_REG_FAULT2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	fault = fault | (temp & 0x2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static int aw36518_timeout_cal(struct aw36518_flash *flash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	v4l2_dbg(1, debug, &flash->leds[0].sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		 "%s: timeout:%dUS\n", __func__, flash->flash_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (flash->flash_timeout < 400000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		val =  AW36518_FLASH_TOUT_ms_TO_REG(flash->flash_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		val =  (flash->flash_timeout - 0x400000) / TIMEOUT_STEP2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		val = val + 0x09;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static int aw36518_set_timeout(struct aw36518_flash *flash, u32 timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	v4l2_dbg(1, debug, &flash->leds[0].sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		 "%s: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		 __func__, flash->flash_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	flash->flash_timeout = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	ret = aw36518_i2c_read(flash, 0x08);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	val =  aw36518_timeout_cal(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return aw36518_i2c_write(flash, 0x08, val | (ret & 0xf0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static int aw36518_torch_brt(struct aw36518_flash *flash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			     enum aw36518_led_id id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	struct aw36518_led *led = &flash->leds[id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	u8 val, reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	v4l2_dbg(1, debug, &led->sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		 "%s: %d\n", __func__, led->torch_brt->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	val = AW36518_TORCH_BRT_uA_TO_REG(led->torch_brt->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	reg = id ? AW36518_REG_LED1_TORCH_CUR : AW36518_REG_LED0_TORCH_CUR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	return aw36518_i2c_write(flash, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static int aw36518_flash_brt(struct aw36518_flash *flash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			     enum aw36518_led_id id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	struct aw36518_led *led = &flash->leds[id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	u8 val, reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	v4l2_dbg(1, debug, &led->sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		 "%s: %d\n", __func__, led->flash_brt->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	val = AW36518_FLASH_BRT_uA_TO_REG(led->flash_brt->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	reg = id ? AW36518_REG_LED1_FLASH_CUR : AW36518_REG_LED0_FLASH_CUR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	return aw36518_i2c_write(flash, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static int aw36518_set_mode(struct aw36518_flash *flash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			    enum aw36518_led_id id, unsigned int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	v4l2_dbg(1, debug, &flash->leds[id].sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		 "%s: %d cur:%d\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		 mode, flash->led_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (flash->led_mode == mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	aw36518_led_on(flash, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	flash->led_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (mode == V4L2_FLASH_LED_MODE_FLASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		ret = aw36518_i2c_write(flash, 0x01, 0x0C);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		ret |= aw36518_flash_brt(flash, LED0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	} else if (mode == V4L2_FLASH_LED_MODE_TORCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		ret = aw36518_i2c_write(flash, 0x01, 0x08);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		ret |= aw36518_torch_brt(flash, LED0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		ret |= aw36518_led_on(flash, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		ret = aw36518_i2c_write(flash, 0x01, 0x00);
^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) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static int aw36518_strobe(struct aw36518_flash *flash, bool on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	v4l2_dbg(1, debug, &flash->leds[0].sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		 "%s: on %d\n", __func__, on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	ret = aw36518_led_on(flash, on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static int aw36518_get_ctrl(struct v4l2_ctrl *ctrl, enum aw36518_led_id id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	struct aw36518_led *led =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		container_of(ctrl->handler, struct aw36518_led, ctrls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	struct aw36518_flash *flash =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		container_of(led, struct aw36518_flash, leds[id]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	struct i2c_client *client = flash->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	v4l2_dbg(1, debug, &flash->leds[id].sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		 "%s: id 0x%x\n", __func__, ctrl->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	mutex_lock(&flash->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	switch (ctrl->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	case V4L2_CID_FLASH_FAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		ret = aw36518_get_fault(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		ctrl->val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		if (ret & AW36518_REG_FL_SC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			ctrl->val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		if (ret & AW36518_REG_FL_OT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			ctrl->val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		if (ret & AW36518_REG_FL_TO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			ctrl->val |= V4L2_FLASH_FAULT_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		if (ret & AW36518_REG_FL_OVP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			ctrl->val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			 "ctrl 0x%x not supported\n", ctrl->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	mutex_unlock(&flash->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static int aw36518_set_ctrl(struct v4l2_ctrl *ctrl, enum aw36518_led_id id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	struct aw36518_led *led =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		container_of(ctrl->handler, struct aw36518_led, ctrls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	struct aw36518_flash *flash =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		container_of(led, struct aw36518_flash, leds[id]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	struct i2c_client *client = flash->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	v4l2_dbg(1, debug, &led->sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		 "%s: id 0x%x val 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		 __func__, ctrl->id, ctrl->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	mutex_lock(&flash->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	ret = aw36518_get_fault(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	if ((ret & (AW36518_REG_FL_OVP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		     AW36518_REG_FL_OT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		     AW36518_REG_FL_SC)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	    (ctrl->id == V4L2_CID_FLASH_STROBE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	     ctrl->id == V4L2_CID_FLASH_TORCH_INTENSITY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	     ctrl->id == V4L2_CID_FLASH_LED_MODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	switch (ctrl->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	case V4L2_CID_FLASH_LED_MODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		ret = aw36518_set_mode(flash, id, ctrl->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	case V4L2_CID_FLASH_STROBE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		ret = aw36518_strobe(flash, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	case V4L2_CID_FLASH_STROBE_STOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		ret = aw36518_strobe(flash, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	case V4L2_CID_FLASH_TIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		ret = aw36518_set_timeout(flash, ctrl->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	case V4L2_CID_FLASH_INTENSITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		ret = aw36518_flash_brt(flash, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	case V4L2_CID_FLASH_TORCH_INTENSITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		ret = aw36518_torch_brt(flash, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			"ctrl 0x%x not supported\n", ctrl->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	mutex_unlock(&flash->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static int aw36518_led0_get_ctrl(struct v4l2_ctrl *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	return aw36518_get_ctrl(ctrl, LED0);
^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 int aw36518_led0_set_ctrl(struct v4l2_ctrl *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	return aw36518_set_ctrl(ctrl, LED0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static int aw36518_led1_get_ctrl(struct v4l2_ctrl *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	return aw36518_get_ctrl(ctrl, LED1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) static int aw36518_led1_set_ctrl(struct v4l2_ctrl *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	return aw36518_set_ctrl(ctrl, LED1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) static const struct v4l2_ctrl_ops aw36518_ctrl_ops[LED_MAX] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	[LED0] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		.g_volatile_ctrl = aw36518_led0_get_ctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		.s_ctrl = aw36518_led0_set_ctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	[LED1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		.g_volatile_ctrl = aw36518_led1_get_ctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		.s_ctrl = aw36518_led1_set_ctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) static int aw36518_init_controls(struct aw36518_flash *flash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 				 enum aw36518_led_id id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	struct v4l2_ctrl *fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	struct v4l2_ctrl_handler *hdl = &flash->leds[id].ctrls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	const struct v4l2_ctrl_ops *ops = &aw36518_ctrl_ops[id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	struct aw36518_led *led = &flash->leds[id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	v4l2_ctrl_handler_init(hdl, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	v4l2_ctrl_new_std_menu(hdl, ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			       V4L2_CID_FLASH_LED_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			       V4L2_FLASH_LED_MODE_TORCH, ~0x7, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	v4l2_ctrl_new_std_menu(hdl, ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 			       V4L2_CID_FLASH_STROBE_SOURCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			       V4L2_FLASH_STROBE_SOURCE_SOFTWARE, ~0x1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	v4l2_ctrl_new_std(hdl, ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			  V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	v4l2_ctrl_new_std(hdl, ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			  V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	v4l2_ctrl_new_std(hdl, ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			  V4L2_CID_FLASH_TIMEOUT, TIMEOUT_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 			  led->max_flash_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 			  TIMEOUT_STEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			  led->max_flash_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	flash->flash_timeout = led->max_flash_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	flash->leds[id].flash_brt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		v4l2_ctrl_new_std(hdl, ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 				  V4L2_CID_FLASH_INTENSITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 				  AW36518_MIN_FLASH_INTENSITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 				  led->max_flash_intensity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 				  AW36518_FLASH_INTENSITY_STEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 				  AW36518_FLASH_INTENSITY_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	flash->leds[id].torch_brt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		v4l2_ctrl_new_std(hdl, ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 				  V4L2_CID_FLASH_TORCH_INTENSITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 				  AW36518_MIN_TORCH_INTENSITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 				  led->max_torch_intensity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 				  AW36518_TORCH_INTENSITY_STEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 				  AW36518_TORCH_INTENSITY_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	fault = v4l2_ctrl_new_std(hdl, ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 				  V4L2_CID_FLASH_FAULT, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 				  V4L2_FLASH_FAULT_OVER_VOLTAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 				  | V4L2_FLASH_FAULT_TIMEOUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 				  | V4L2_FLASH_FAULT_OVER_TEMPERATURE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 				  | V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	if (hdl->error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		return hdl->error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	flash->leds[id].sd.ctrl_handler = hdl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) static void aw36518_get_time_info(struct v4l2_subdev *sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 				  struct old_timeval32 *compat_ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	struct aw36518_led *led =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		container_of(sd, struct aw36518_led, sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	memset(compat_ti, 0, sizeof(*compat_ti));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	compat_ti->tv_sec = led->timestamp.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	compat_ti->tv_usec = led->timestamp.tv_usec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) static long aw36518_ioctl(struct v4l2_subdev *sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 			 unsigned int cmd, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	long ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	case RK_VIDIOC_FLASH_TIMEINFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		aw36518_get_time_info(sd, (struct old_timeval32 *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		ret = -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) #define RK_VIDIOC_COMPAT_FLASH_TIMEINFO \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	_IOR('V', BASE_VIDIOC_PRIVATE + 0, struct old_timeval32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) static long aw36518_compat_ioctl32(struct v4l2_subdev *sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 				  unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 				  unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	void __user *up = compat_ptr(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	struct old_timeval32 *compat_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	case RK_VIDIOC_COMPAT_FLASH_TIMEINFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		compat_t = kzalloc(sizeof(*compat_t), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		if (!compat_t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		ret = aw36518_ioctl(sd, RK_VIDIOC_FLASH_TIMEINFO, compat_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 			ret = copy_to_user(up, compat_t, sizeof(*compat_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 				ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		kfree(compat_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		ret = -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) static int aw36518_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	int rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	rval = pm_runtime_get_sync(sd->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	if (rval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		pm_runtime_put_noidle(sd->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) static int aw36518_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	pm_runtime_put(sd->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) static int aw36518_s_power(struct v4l2_subdev *sd, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	struct aw36518_led *led = to_led(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	struct aw36518_flash *flash =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		container_of(led, struct aw36518_flash, leds[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	struct i2c_client *client = flash->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	dev_info(&client->dev, "%s on(%d)\n", __func__, on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	mutex_lock(&flash->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	/* If the power state is not modified - no work to do. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	if (flash->power_on == !!on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		goto unlock_and_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	if (on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		ret = pm_runtime_get_sync(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 			pm_runtime_put_noidle(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 			goto unlock_and_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		flash->power_on = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		pm_runtime_put(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		flash->power_on = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) unlock_and_return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	mutex_unlock(&flash->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) static const struct v4l2_subdev_core_ops aw36518_core_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	.s_power = aw36518_s_power,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	.ioctl = aw36518_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	.compat_ioctl32 = aw36518_compat_ioctl32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) static const struct v4l2_subdev_ops aw36518_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	.core = &aw36518_core_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) static const struct v4l2_subdev_internal_ops aw36518_internal_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	.open = aw36518_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	.close = aw36518_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) static int __aw36518_set_power(struct aw36518_flash *flash, bool on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	gpiod_direction_output(flash->en_gpio, on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) static int aw36518_check_id(struct aw36518_flash *flash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	__aw36518_set_power(flash, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	ret = aw36518_i2c_read(flash, AW36518_REG_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	if (ret != AW36518_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		dev_err(&flash->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 			"Read chip id error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	dev_info(&flash->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		 "Detected aw36518 flash id:0x%x\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) static int aw36518_of_init(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 			   struct aw36518_flash *flash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	struct device_node *node = client->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	struct aw36518_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 			"get device node failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	ret = of_property_read_u32(node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 				   RKMODULE_CAMERA_MODULE_INDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 				   &flash->module_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	ret |= of_property_read_string(node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 				       RKMODULE_CAMERA_MODULE_FACING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 				       &flash->module_facing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 			"could not get module information!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	flash->en_gpio = devm_gpiod_get(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 					"enable", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	if (IS_ERR(flash->en_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		dev_err(&client->dev, "get enable-gpio failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	flash->torch_gpio = devm_gpiod_get(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 					   "torch", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	if (IS_ERR(flash->torch_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 		flash->torch_gpio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		dev_warn(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 			 "get torch-gpio failed, using assist light mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	flash->strobe_gpio = devm_gpiod_get(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 					    "strobe", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	if (IS_ERR(flash->strobe_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 		flash->strobe_gpio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 		dev_warn(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 			 "get strobe-gpio failed, using assist light mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	flash->tx_gpio = devm_gpiod_get(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 					    "tx", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	if (IS_ERR(flash->strobe_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 		flash->tx_gpio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 		dev_warn(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 			 "get tx-gpio failed, using assist light mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	for_each_child_of_node(node, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 		u32 id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		of_property_read_u32(child, "reg", &id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 		if (id >= LED_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 			dev_err(&client->dev, "only support 2 leds\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		led = &flash->leds[id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 		led->sd.fwnode = of_fwnode_handle(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		if (of_property_read_u32(child, "flash-max-timeout-us",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 					 &led->max_flash_timeout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 			dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 				"get led%d flash-max-timeout-us fail\n", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 		if (led->max_flash_timeout > TIMEOUT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 			led->max_flash_timeout = TIMEOUT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 		if (of_property_read_u32(child, "flash-max-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 					 &led->max_flash_intensity)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 			dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 				"get led%d flash-max-microamp fail\n", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 		if (led->max_flash_intensity > AW36518_MAX_FLASH_INTENSITY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 			led->max_flash_intensity = AW36518_MAX_FLASH_INTENSITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 		if (of_property_read_u32(child, "led-max-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 					  &led->max_torch_intensity)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 			dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 				"get led%d led-max-microamp fail\n", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 		if (led->max_torch_intensity > AW36518_MAX_TORCH_INTENSITY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 			led->max_torch_intensity = AW36518_MAX_TORCH_INTENSITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 		v4l2_dbg(1, debug, &led->sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 			 "led%d max torch:%dUA flash:%dUA timeout:%dUS\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 			 id, led->max_torch_intensity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 			 led->max_flash_intensity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 			 led->max_flash_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) static int aw36518_init_device(struct aw36518_flash *flash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	unsigned int reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	struct i2c_client *client = flash->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 	/* output disable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	ret = aw36518_set_mode(flash, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	/* reset faults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	reg_val = aw36518_i2c_read(flash, AW36518_REG_FAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	dev_info(&client->dev, "%s: fault: 0x%x.\n", __func__, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	/* tx input default low */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	if (flash->tx_gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 		gpiod_direction_output(flash->tx_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) static int aw36518_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 			 const struct i2c_device_id *devid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	struct aw36518_flash *flash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	struct v4l2_subdev *sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	char facing[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	dev_info(&client->dev, "driver version: %02x.%02x.%02x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 		 DRIVER_VERSION >> 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 		 (DRIVER_VERSION & 0xff00) >> 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 		 DRIVER_VERSION & 0x00ff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	if (!flash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 	flash->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	ret = aw36518_of_init(client, flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	ret = aw36518_check_id(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 		goto err_power_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	for (i = 0; i < LED_MAX; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 		sd = &flash->leds[i].sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 		v4l2_i2c_subdev_init(sd, client, &aw36518_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 		sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 		sd->internal_ops = &aw36518_internal_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 		memset(facing, 0, sizeof(facing));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 		if (strcmp(flash->module_facing, "back") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 			facing[0] = 'b';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 			facing[0] = 'f';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 		/* NOTE: to distinguish between two led
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 		 * name: led0 meet the main led
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 		 * name: led1 meet the secondary led
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 		snprintf(sd->name, sizeof(sd->name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 			 "m%02d_%s_%s_led%d %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 			 flash->module_index, facing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 			 AW36518_NAME, i, dev_name(sd->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 		ret = aw36518_init_controls(flash, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 		ret = media_entity_pads_init(&sd->entity, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 			goto free_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 		sd->entity.function = MEDIA_ENT_F_FLASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 		ret = v4l2_async_register_subdev(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 			goto free_media;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 	ret = aw36518_init_device(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 		goto free_media;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 	i2c_set_clientdata(client, flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 	mutex_init(&flash->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 	pm_runtime_set_active(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 	pm_runtime_enable(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 	pm_runtime_idle(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	dev_info(&client->dev, "probing successful\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) free_media:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	media_entity_cleanup(&flash->leds[i].sd.entity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) free_ctl:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 	v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 	for (--i; i >= 0; --i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 		v4l2_device_unregister_subdev(&flash->leds[i].sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 		media_entity_cleanup(&flash->leds[i].sd.entity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 		v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) err_power_off:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 	__aw36518_set_power(flash, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) static int aw36518_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 	struct aw36518_flash *flash = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 	pm_runtime_disable(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	for (i = 0; i < LED_MAX; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 		v4l2_device_unregister_subdev(&flash->leds[i].sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 		v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 		media_entity_cleanup(&flash->leds[i].sd.entity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 	mutex_destroy(&flash->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) static int __maybe_unused aw36518_runtime_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 	struct aw36518_flash *flash = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 	return __aw36518_set_power(flash, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) static int __maybe_unused aw36518_runtime_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 	struct aw36518_flash *flash = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 	return __aw36518_set_power(flash, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) static const struct i2c_device_id aw36518_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 	{ AW36518_NAME, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 	{ { 0 } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) MODULE_DEVICE_TABLE(i2c, aw36518_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) static const struct of_device_id aw36518_of_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 	{ .compatible = "awinic,aw36518" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 	{ { 0 } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) static const struct dev_pm_ops aw36518_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 	SET_RUNTIME_PM_OPS(aw36518_runtime_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 			   aw36518_runtime_resume, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) static struct i2c_driver aw36518_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 	.driver	= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 		.name = AW36518_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 		.pm = &aw36518_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) 		.of_match_table = aw36518_of_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 	.probe = aw36518_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) 	.remove	= aw36518_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) 	.id_table = aw36518_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) module_i2c_driver(aw36518_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) MODULE_DESCRIPTION("AW36518 LED flash driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) MODULE_LICENSE("GPL");