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)  * drivers/leds/leds-as3645a.c - AS3645A and LM3555 flash controllers driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2008-2011 Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2011, 2017 Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Based on drivers/media/i2c/as3645a.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Contact: Sakari Ailus <sakari.ailus@iki.fi>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/led-class-flash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <media/v4l2-flash-led-class.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define AS_TIMER_US_TO_CODE(t)			(((t) / 1000 - 100) / 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define AS_TIMER_CODE_TO_US(c)			((50 * (c) + 100) * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) /* Register definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /* Read-only Design info register: Reset state: xxxx 0001 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define AS_DESIGN_INFO_REG			0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define AS_DESIGN_INFO_FACTORY(x)		(((x) >> 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define AS_DESIGN_INFO_MODEL(x)			((x) & 0x0f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) /* Read-only Version control register: Reset state: 0000 0000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * for first engineering samples
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define AS_VERSION_CONTROL_REG			0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define AS_VERSION_CONTROL_RFU(x)		(((x) >> 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define AS_VERSION_CONTROL_VERSION(x)		((x) & 0x0f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) /* Read / Write	(Indicator and timer register): Reset state: 0000 1111 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define AS_INDICATOR_AND_TIMER_REG		0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define AS_INDICATOR_AND_TIMER_VREF_SHIFT	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT	6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) /* Read / Write	(Current set register): Reset state: 0110 1001 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define AS_CURRENT_SET_REG			0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define AS_CURRENT_ASSIST_LIGHT_SHIFT		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define AS_CURRENT_LED_DET_ON			(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define AS_CURRENT_FLASH_CURRENT_SHIFT		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) /* Read / Write	(Control register): Reset state: 1011 0100 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define AS_CONTROL_REG				0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define AS_CONTROL_MODE_SETTING_SHIFT		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define AS_CONTROL_STROBE_ON			(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define AS_CONTROL_OUT_ON			(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #define AS_CONTROL_EXT_TORCH_ON			(1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define AS_CONTROL_STROBE_TYPE_EDGE		(0 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define AS_CONTROL_STROBE_TYPE_LEVEL		(1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define AS_CONTROL_COIL_PEAK_SHIFT		6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) /* Read only (D3 is read / write) (Fault and info): Reset state: 0000 x000 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #define AS_FAULT_INFO_REG			0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #define AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT	(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #define AS_FAULT_INFO_INDICATOR_LED		(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #define AS_FAULT_INFO_LED_AMOUNT		(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #define AS_FAULT_INFO_TIMEOUT			(1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #define AS_FAULT_INFO_OVER_TEMPERATURE		(1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define AS_FAULT_INFO_SHORT_CIRCUIT		(1 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #define AS_FAULT_INFO_OVER_VOLTAGE		(1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) /* Boost register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #define AS_BOOST_REG				0x0d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #define AS_BOOST_CURRENT_DISABLE		(0 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #define AS_BOOST_CURRENT_ENABLE			(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) /* Password register is used to unlock boost register writing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #define AS_PASSWORD_REG				0x0f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #define AS_PASSWORD_UNLOCK_VALUE		0x55
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) #define AS_NAME					"as3645a"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #define AS_I2C_ADDR				(0x60 >> 1) /* W:0x60, R:0x61 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) #define AS_FLASH_TIMEOUT_MIN			100000	/* us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) #define AS_FLASH_TIMEOUT_MAX			850000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) #define AS_FLASH_TIMEOUT_STEP			50000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) #define AS_FLASH_INTENSITY_MIN			200000	/* uA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) #define AS_FLASH_INTENSITY_MAX_1LED		500000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) #define AS_FLASH_INTENSITY_MAX_2LEDS		400000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) #define AS_FLASH_INTENSITY_STEP			20000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) #define AS_TORCH_INTENSITY_MIN			20000	/* uA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) #define AS_TORCH_INTENSITY_MAX			160000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) #define AS_TORCH_INTENSITY_STEP			20000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) #define AS_INDICATOR_INTENSITY_MIN		0	/* uA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #define AS_INDICATOR_INTENSITY_MAX		10000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define AS_INDICATOR_INTENSITY_STEP		2500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define AS_PEAK_mA_MAX				2000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define AS_PEAK_mA_TO_REG(a) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	((min_t(u32, AS_PEAK_mA_MAX, a) - 1250) / 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* LED numbers for Devicetree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #define AS_LED_FLASH				0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #define AS_LED_INDICATOR			1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) enum as_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	AS_MODE_EXT_TORCH = 0 << AS_CONTROL_MODE_SETTING_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	AS_MODE_INDICATOR = 1 << AS_CONTROL_MODE_SETTING_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	AS_MODE_ASSIST = 2 << AS_CONTROL_MODE_SETTING_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	AS_MODE_FLASH = 3 << AS_CONTROL_MODE_SETTING_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct as3645a_config {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	u32 flash_timeout_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	u32 flash_max_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	u32 assist_max_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	u32 indicator_max_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	u32 voltage_reference;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	u32 peak;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct as3645a {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	struct led_classdev_flash fled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	struct led_classdev iled_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	struct v4l2_flash *vf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	struct v4l2_flash *vfind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	struct fwnode_handle *flash_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	struct fwnode_handle *indicator_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	struct as3645a_config cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	enum as_mode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	unsigned int timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	unsigned int flash_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	unsigned int assist_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	unsigned int indicator_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	enum v4l2_flash_strobe_source strobe_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #define fled_to_as3645a(__fled) container_of(__fled, struct as3645a, fled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #define iled_cdev_to_as3645a(__iled_cdev) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	container_of(__iled_cdev, struct as3645a, iled_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* Return negative errno else zero on success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static int as3645a_write(struct as3645a *flash, u8 addr, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	struct i2c_client *client = flash->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	int rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	rval = i2c_smbus_write_byte_data(client, addr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	dev_dbg(&client->dev, "Write Addr:%02X Val:%02X %s\n", addr, val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		rval < 0 ? "fail" : "ok");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* Return negative errno else a data byte received from the device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static int as3645a_read(struct as3645a *flash, u8 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct i2c_client *client = flash->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	int rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	rval = i2c_smbus_read_byte_data(client, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	dev_dbg(&client->dev, "Read Addr:%02X Val:%02X %s\n", addr, rval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		rval < 0 ? "fail" : "ok");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* -----------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * Hardware configuration and trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  * as3645a_set_config - Set flash configuration registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  * @flash: The flash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  * Configure the hardware with flash, assist and indicator currents, as well as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  * flash timeout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  * Return 0 on success, or a negative error code if an I2C communication error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  * occurred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int as3645a_set_current(struct as3645a *flash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	val = (flash->flash_current << AS_CURRENT_FLASH_CURRENT_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	    | (flash->assist_current << AS_CURRENT_ASSIST_LIGHT_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	    | AS_CURRENT_LED_DET_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	return as3645a_write(flash, AS_CURRENT_SET_REG, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static int as3645a_set_timeout(struct as3645a *flash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	val = flash->timeout << AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	val |= (flash->cfg.voltage_reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		<< AS_INDICATOR_AND_TIMER_VREF_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	    |  ((flash->indicator_current ? flash->indicator_current - 1 : 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		 << AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	return as3645a_write(flash, AS_INDICATOR_AND_TIMER_REG, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * as3645a_set_control - Set flash control register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  * @flash: The flash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  * @mode: Desired output mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  * @on: Desired output state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  * Configure the hardware with output mode and state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  * Return 0 on success, or a negative error code if an I2C communication error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  * occurred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) as3645a_set_control(struct as3645a *flash, enum as_mode mode, bool on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	/* Configure output parameters and operation mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	reg = (flash->cfg.peak << AS_CONTROL_COIL_PEAK_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	    | (on ? AS_CONTROL_OUT_ON : 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	    | mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (mode == AS_MODE_FLASH &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	    flash->strobe_source == V4L2_FLASH_STROBE_SOURCE_EXTERNAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		reg |= AS_CONTROL_STROBE_TYPE_LEVEL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		    |  AS_CONTROL_STROBE_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	return as3645a_write(flash, AS_CONTROL_REG, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static int as3645a_get_fault(struct led_classdev_flash *fled, u32 *fault)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	struct as3645a *flash = fled_to_as3645a(fled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	int rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	/* NOTE: reading register clears fault status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	rval = as3645a_read(flash, AS_FAULT_INFO_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (rval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (rval & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		*fault |= LED_FAULT_OVER_CURRENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (rval & AS_FAULT_INFO_INDICATOR_LED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		*fault |= LED_FAULT_INDICATOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	dev_dbg(&flash->client->dev, "%u connected LEDs\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		rval & AS_FAULT_INFO_LED_AMOUNT ? 2 : 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (rval & AS_FAULT_INFO_TIMEOUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		*fault |= LED_FAULT_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (rval & AS_FAULT_INFO_OVER_TEMPERATURE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		*fault |= LED_FAULT_OVER_TEMPERATURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (rval & AS_FAULT_INFO_SHORT_CIRCUIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		*fault |= LED_FAULT_OVER_CURRENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (rval & AS_FAULT_INFO_OVER_VOLTAGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		*fault |= LED_FAULT_INPUT_VOLTAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static unsigned int __as3645a_current_to_reg(unsigned int min, unsigned int max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 					     unsigned int step,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 					     unsigned int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	if (val < min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		val = min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (val > max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		val = max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	return (val - min) / step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static unsigned int as3645a_current_to_reg(struct as3645a *flash, bool is_flash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 					   unsigned int ua)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (is_flash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		return __as3645a_current_to_reg(AS_TORCH_INTENSITY_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 						flash->cfg.assist_max_ua,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 						AS_TORCH_INTENSITY_STEP, ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		return __as3645a_current_to_reg(AS_FLASH_INTENSITY_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 						flash->cfg.flash_max_ua,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 						AS_FLASH_INTENSITY_STEP, ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static int as3645a_set_indicator_brightness(struct led_classdev *iled_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 					    enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	struct as3645a *flash = iled_cdev_to_as3645a(iled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	int rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	flash->indicator_current = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	rval = as3645a_set_timeout(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (rval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	return as3645a_set_control(flash, AS_MODE_INDICATOR, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int as3645a_set_assist_brightness(struct led_classdev *fled_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 					 enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	struct led_classdev_flash *fled = lcdev_to_flcdev(fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	struct as3645a *flash = fled_to_as3645a(fled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	int rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (brightness) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		/* Register value 0 is 20 mA. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		flash->assist_current = brightness - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		rval = as3645a_set_current(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		if (rval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	return as3645a_set_control(flash, AS_MODE_ASSIST, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static int as3645a_set_flash_brightness(struct led_classdev_flash *fled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 					u32 brightness_ua)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	struct as3645a *flash = fled_to_as3645a(fled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	flash->flash_current = as3645a_current_to_reg(flash, true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 						      brightness_ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return as3645a_set_current(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static int as3645a_set_flash_timeout(struct led_classdev_flash *fled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 				     u32 timeout_us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	struct as3645a *flash = fled_to_as3645a(fled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	flash->timeout = AS_TIMER_US_TO_CODE(timeout_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	return as3645a_set_timeout(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int as3645a_set_strobe(struct led_classdev_flash *fled, bool state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	struct as3645a *flash = fled_to_as3645a(fled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	return as3645a_set_control(flash, AS_MODE_FLASH, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static const struct led_flash_ops as3645a_led_flash_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	.flash_brightness_set = as3645a_set_flash_brightness,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	.timeout_set = as3645a_set_flash_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	.strobe_set = as3645a_set_strobe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	.fault_get = as3645a_get_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static int as3645a_setup(struct as3645a *flash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	struct device *dev = &flash->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	u32 fault = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	int rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	/* clear errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	rval = as3645a_read(flash, AS_FAULT_INFO_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	if (rval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	dev_dbg(dev, "Fault info: %02x\n", rval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	rval = as3645a_set_current(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	if (rval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	rval = as3645a_set_timeout(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	if (rval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	rval = as3645a_set_control(flash, AS_MODE_INDICATOR, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (rval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	/* read status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	rval = as3645a_get_fault(&flash->fled, &fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	if (rval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	dev_dbg(dev, "AS_INDICATOR_AND_TIMER_REG: %02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		as3645a_read(flash, AS_INDICATOR_AND_TIMER_REG));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	dev_dbg(dev, "AS_CURRENT_SET_REG: %02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		as3645a_read(flash, AS_CURRENT_SET_REG));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	dev_dbg(dev, "AS_CONTROL_REG: %02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		as3645a_read(flash, AS_CONTROL_REG));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	return rval & ~AS_FAULT_INFO_LED_AMOUNT ? -EIO : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static int as3645a_detect(struct as3645a *flash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	struct device *dev = &flash->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	int rval, man, model, rfu, version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	const char *vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	rval = as3645a_read(flash, AS_DESIGN_INFO_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	if (rval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		dev_err(dev, "can't read design info reg\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	man = AS_DESIGN_INFO_FACTORY(rval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	model = AS_DESIGN_INFO_MODEL(rval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	rval = as3645a_read(flash, AS_VERSION_CONTROL_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	if (rval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		dev_err(dev, "can't read version control reg\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	rfu = AS_VERSION_CONTROL_RFU(rval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	version = AS_VERSION_CONTROL_VERSION(rval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	/* Verify the chip model and version. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (model != 0x01 || rfu != 0x00) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		dev_err(dev, "AS3645A not detected (model %d rfu %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			model, rfu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		return -ENODEV;
^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) 	switch (man) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		vendor = "AMS, Austria Micro Systems";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		vendor = "ADI, Analog Devices Inc.";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	case 3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		vendor = "NSC, National Semiconductor";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		vendor = "NXP";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	case 5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		vendor = "TI, Texas Instrument";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		vendor = "Unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	dev_info(dev, "Chip vendor: %s (%d) Version: %d\n", vendor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		 man, version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	rval = as3645a_write(flash, AS_PASSWORD_REG, AS_PASSWORD_UNLOCK_VALUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	if (rval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	return as3645a_write(flash, AS_BOOST_REG, AS_BOOST_CURRENT_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) static int as3645a_parse_node(struct as3645a *flash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			      struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	struct as3645a_config *cfg = &flash->cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	struct fwnode_handle *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	int rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	fwnode_for_each_child_node(fwnode, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		u32 id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		fwnode_property_read_u32(child, "reg", &id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		switch (id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		case AS_LED_FLASH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			flash->flash_node = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			fwnode_handle_get(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		case AS_LED_INDICATOR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			flash->indicator_node = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			fwnode_handle_get(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 			dev_warn(&flash->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 				 "unknown LED %u encountered, ignoring\n", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	if (!flash->flash_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		dev_err(&flash->client->dev, "can't find flash node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	rval = fwnode_property_read_u32(flash->flash_node, "flash-timeout-us",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 					&cfg->flash_timeout_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	if (rval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		dev_err(&flash->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			"can't read flash-timeout-us property for flash\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		goto out_err;
^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) 	rval = fwnode_property_read_u32(flash->flash_node, "flash-max-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 					&cfg->flash_max_ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (rval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		dev_err(&flash->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			"can't read flash-max-microamp property for flash\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	rval = fwnode_property_read_u32(flash->flash_node, "led-max-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 					&cfg->assist_max_ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	if (rval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		dev_err(&flash->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 			"can't read led-max-microamp property for flash\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	fwnode_property_read_u32(flash->flash_node, "voltage-reference",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 				 &cfg->voltage_reference);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	fwnode_property_read_u32(flash->flash_node, "ams,input-max-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 				 &cfg->peak);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	cfg->peak = AS_PEAK_mA_TO_REG(cfg->peak);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	if (!flash->indicator_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		dev_warn(&flash->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 			 "can't find indicator node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		rval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		goto out_err;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	rval = fwnode_property_read_u32(flash->indicator_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 					"led-max-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 					&cfg->indicator_max_ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	if (rval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		dev_err(&flash->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			"can't read led-max-microamp property for indicator\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	fwnode_handle_put(flash->flash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	fwnode_handle_put(flash->indicator_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) static int as3645a_led_class_setup(struct as3645a *flash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	struct led_classdev *fled_cdev = &flash->fled.led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	struct led_classdev *iled_cdev = &flash->iled_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	struct led_init_data init_data = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	struct led_flash_setting *cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	int rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	iled_cdev->brightness_set_blocking = as3645a_set_indicator_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	iled_cdev->max_brightness =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		flash->cfg.indicator_max_ua / AS_INDICATOR_INTENSITY_STEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	iled_cdev->flags = LED_CORE_SUSPENDRESUME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	init_data.fwnode = flash->indicator_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	init_data.devicename = AS_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	init_data.default_label = "indicator";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	rval = led_classdev_register_ext(&flash->client->dev, iled_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 					 &init_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	if (rval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	cfg = &flash->fled.brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	cfg->min = AS_FLASH_INTENSITY_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	cfg->max = flash->cfg.flash_max_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	cfg->step = AS_FLASH_INTENSITY_STEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	cfg->val = flash->cfg.flash_max_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	cfg = &flash->fled.timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	cfg->min = AS_FLASH_TIMEOUT_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	cfg->max = flash->cfg.flash_timeout_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	cfg->step = AS_FLASH_TIMEOUT_STEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	cfg->val = flash->cfg.flash_timeout_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	flash->fled.ops = &as3645a_led_flash_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	fled_cdev->brightness_set_blocking = as3645a_set_assist_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	/* Value 0 is off in LED class. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	fled_cdev->max_brightness =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		as3645a_current_to_reg(flash, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 				       flash->cfg.assist_max_ua) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	fled_cdev->flags = LED_DEV_CAP_FLASH | LED_CORE_SUSPENDRESUME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	init_data.fwnode = flash->flash_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	init_data.devicename = AS_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	init_data.default_label = "flash";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	rval = led_classdev_flash_register_ext(&flash->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 					       &flash->fled, &init_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	if (rval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	led_classdev_unregister(iled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	dev_err(&flash->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		"led_classdev_flash_register() failed, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 		rval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) static int as3645a_v4l2_setup(struct as3645a *flash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	struct led_classdev_flash *fled = &flash->fled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	struct led_classdev *led = &fled->led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	struct v4l2_flash_config cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		.intensity = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 			.min = AS_TORCH_INTENSITY_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 			.max = flash->cfg.assist_max_ua,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 			.step = AS_TORCH_INTENSITY_STEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 			.val = flash->cfg.assist_max_ua,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	struct v4l2_flash_config cfgind = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		.intensity = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 			.min = AS_INDICATOR_INTENSITY_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 			.max = flash->cfg.indicator_max_ua,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 			.step = AS_INDICATOR_INTENSITY_STEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 			.val = flash->cfg.indicator_max_ua,
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	strlcpy(cfg.dev_name, led->dev->kobj.name, sizeof(cfg.dev_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	strlcpy(cfgind.dev_name, flash->iled_cdev.dev->kobj.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		sizeof(cfgind.dev_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	flash->vf = v4l2_flash_init(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		&flash->client->dev, flash->flash_node, &flash->fled, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		&cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	if (IS_ERR(flash->vf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		return PTR_ERR(flash->vf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	flash->vfind = v4l2_flash_indicator_init(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		&flash->client->dev, flash->indicator_node, &flash->iled_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		&cfgind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	if (IS_ERR(flash->vfind)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		v4l2_flash_release(flash->vf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		return PTR_ERR(flash->vfind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) static int as3645a_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	struct as3645a *flash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	int rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	if (!dev_fwnode(&client->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	if (flash == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	flash->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	rval = as3645a_parse_node(flash, dev_fwnode(&client->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	if (rval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 		return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	rval = as3645a_detect(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	if (rval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 		goto out_put_nodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	mutex_init(&flash->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	i2c_set_clientdata(client, flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	rval = as3645a_setup(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	if (rval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		goto out_mutex_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	rval = as3645a_led_class_setup(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	if (rval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 		goto out_mutex_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	rval = as3645a_v4l2_setup(flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	if (rval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 		goto out_led_classdev_flash_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) out_led_classdev_flash_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	led_classdev_flash_unregister(&flash->fled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) out_mutex_destroy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	mutex_destroy(&flash->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) out_put_nodes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	fwnode_handle_put(flash->flash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	fwnode_handle_put(flash->indicator_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	return rval;
^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) static int as3645a_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	struct as3645a *flash = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	as3645a_set_control(flash, AS_MODE_EXT_TORCH, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	v4l2_flash_release(flash->vf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	v4l2_flash_release(flash->vfind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	led_classdev_flash_unregister(&flash->fled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	led_classdev_unregister(&flash->iled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	mutex_destroy(&flash->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	fwnode_handle_put(flash->flash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	fwnode_handle_put(flash->indicator_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) static const struct i2c_device_id as3645a_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	{ AS_NAME, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) MODULE_DEVICE_TABLE(i2c, as3645a_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) static const struct of_device_id as3645a_of_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	{ .compatible = "ams,as3645a" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) MODULE_DEVICE_TABLE(of, as3645a_of_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) static struct i2c_driver as3645a_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	.driver	= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 		.of_match_table = as3645a_of_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 		.name = AS_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	.probe_new	= as3645a_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	.remove	= as3645a_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	.id_table = as3645a_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) module_i2c_driver(as3645a_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) MODULE_DESCRIPTION("LED flash driver for AS3645A, LM3555 and their clones");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) MODULE_LICENSE("GPL v2");