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/media/i2c/ad5820.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * AD5820 DAC driver for camera voice coil focus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2008 Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Copyright (C) 2007 Texas Instruments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright (C) 2016 Pavel Machek <pavel@ucw.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * Contact: Tuukka Toivonen <tuukkat76@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *	    Sakari Ailus <sakari.ailus@iki.fi>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Based on af_d88.c by Texas Instruments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <media/v4l2-ctrls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <media/v4l2-device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <media/v4l2-subdev.h>
^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) #define AD5820_POWER_DOWN		(1 << 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define AD5820_DAC_SHIFT		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define AD5820_RAMP_MODE_LINEAR		(0 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define AD5820_RAMP_MODE_64_16		(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define CODE_TO_RAMP_US(s)	((s) == 0 ? 0 : (1 << ((s) - 1)) * 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define RAMP_US_TO_CODE(c)	fls(((c) + ((c)>>1)) / 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define to_ad5820_device(sd)	container_of(sd, struct ad5820_device, subdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) struct ad5820_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct v4l2_subdev subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct ad5820_platform_data *platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct regulator *vana;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct v4l2_ctrl_handler ctrls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	u32 focus_absolute;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	u32 focus_ramp_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	u32 focus_ramp_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct gpio_desc *enable_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct mutex power_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	int power_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	bool standby;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static int ad5820_write(struct ad5820_device *coil, u16 data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	struct i2c_client *client = v4l2_get_subdevdata(&coil->subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct i2c_msg msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	__be16 be_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (!client->adapter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	be_data = cpu_to_be16(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	msg.addr  = client->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	msg.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	msg.len   = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	msg.buf   = (u8 *)&be_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	r = i2c_transfer(client->adapter, &msg, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		dev_err(&client->dev, "write failed, error %d\n", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * Calculate status word and write it to the device based on current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * values of V4L2 controls. It is assumed that the stored V4L2 control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * values are properly limited and rounded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static int ad5820_update_hw(struct ad5820_device *coil)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	u16 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	status = RAMP_US_TO_CODE(coil->focus_ramp_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	status |= coil->focus_ramp_mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		? AD5820_RAMP_MODE_64_16 : AD5820_RAMP_MODE_LINEAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	status |= coil->focus_absolute << AD5820_DAC_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (coil->standby)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		status |= AD5820_POWER_DOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	return ad5820_write(coil, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * Power handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static int ad5820_power_off(struct ad5820_device *coil, bool standby)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	int ret = 0, ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	 * Go to standby first as real power off my be denied by the hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	 * (single power line control for both coil and sensor).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (standby) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		coil->standby = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		ret = ad5820_update_hw(coil);
^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) 	gpiod_set_value_cansleep(coil->enable_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	ret2 = regulator_disable(coil->vana);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	return ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static int ad5820_power_on(struct ad5820_device *coil, bool restore)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	ret = regulator_enable(coil->vana);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	gpiod_set_value_cansleep(coil->enable_gpio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (restore) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		/* Restore the hardware settings. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		coil->standby = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		ret = ad5820_update_hw(coil);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	gpiod_set_value_cansleep(coil->enable_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	coil->standby = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	regulator_disable(coil->vana);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  * V4L2 controls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static int ad5820_set_ctrl(struct v4l2_ctrl *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	struct ad5820_device *coil =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		container_of(ctrl->handler, struct ad5820_device, ctrls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	switch (ctrl->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	case V4L2_CID_FOCUS_ABSOLUTE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		coil->focus_absolute = ctrl->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		return ad5820_update_hw(coil);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static const struct v4l2_ctrl_ops ad5820_ctrl_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	.s_ctrl = ad5820_set_ctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static int ad5820_init_controls(struct ad5820_device *coil)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	v4l2_ctrl_handler_init(&coil->ctrls, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	 * V4L2_CID_FOCUS_ABSOLUTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	 * Minimum current is 0 mA, maximum is 100 mA. Thus, 1 code is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	 * equivalent to 100/1023 = 0.0978 mA. Nevertheless, we do not use [mA]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	 * for focus position, because it is meaningless for user. Meaningful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	 * would be to use focus distance or even its inverse, but since the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	 * driver doesn't have sufficiently knowledge to do the conversion, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	 * will just use abstract codes here. In any case, smaller value = focus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	 * position farther from camera. The default zero value means focus at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	 * infinity, and also least current consumption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	v4l2_ctrl_new_std(&coil->ctrls, &ad5820_ctrl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			  V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (coil->ctrls.error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		return coil->ctrls.error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	coil->focus_absolute = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	coil->focus_ramp_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	coil->focus_ramp_mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	coil->subdev.ctrl_handler = &coil->ctrls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  * V4L2 subdev operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static int ad5820_registered(struct v4l2_subdev *subdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	struct ad5820_device *coil = to_ad5820_device(subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	return ad5820_init_controls(coil);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) ad5820_set_power(struct v4l2_subdev *subdev, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	struct ad5820_device *coil = to_ad5820_device(subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	mutex_lock(&coil->power_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	 * If the power count is modified from 0 to != 0 or from != 0 to 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	 * update the power state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (coil->power_count == !on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		ret = on ? ad5820_power_on(coil, true) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			ad5820_power_off(coil, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	/* Update the power count. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	coil->power_count += on ? 1 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	WARN_ON(coil->power_count < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	mutex_unlock(&coil->power_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static int ad5820_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return ad5820_set_power(sd, 1);
^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 ad5820_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	return ad5820_set_power(sd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static const struct v4l2_subdev_core_ops ad5820_core_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	.s_power = ad5820_set_power,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static const struct v4l2_subdev_ops ad5820_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	.core = &ad5820_core_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static const struct v4l2_subdev_internal_ops ad5820_internal_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	.registered = ad5820_registered,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	.open = ad5820_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	.close = ad5820_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)  * I2C driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int __maybe_unused ad5820_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	struct ad5820_device *coil = to_ad5820_device(subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (!coil->power_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	return ad5820_power_off(coil, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static int __maybe_unused ad5820_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	struct ad5820_device *coil = to_ad5820_device(subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	if (!coil->power_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	return ad5820_power_on(coil, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static int ad5820_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			const struct i2c_device_id *devid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	struct ad5820_device *coil;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (!coil)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	coil->vana = devm_regulator_get(&client->dev, "VANA");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (IS_ERR(coil->vana)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		ret = PTR_ERR(coil->vana);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		if (ret != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			dev_err(&client->dev, "could not get regulator for vana\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	coil->enable_gpio = devm_gpiod_get_optional(&client->dev, "enable",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 						    GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (IS_ERR(coil->enable_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		ret = PTR_ERR(coil->enable_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		if (ret != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			dev_err(&client->dev, "could not get enable gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	mutex_init(&coil->power_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	v4l2_i2c_subdev_init(&coil->subdev, client, &ad5820_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	coil->subdev.internal_ops = &ad5820_internal_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	coil->subdev.entity.function = MEDIA_ENT_F_LENS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	strscpy(coil->subdev.name, "ad5820 focus", sizeof(coil->subdev.name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		goto cleanup2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	ret = v4l2_async_register_subdev(&coil->subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) cleanup2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	mutex_destroy(&coil->power_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	media_entity_cleanup(&coil->subdev.entity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static int ad5820_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	struct ad5820_device *coil = to_ad5820_device(subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	v4l2_async_unregister_subdev(&coil->subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	v4l2_ctrl_handler_free(&coil->ctrls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	media_entity_cleanup(&coil->subdev.entity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	mutex_destroy(&coil->power_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	return 0;
^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 const struct i2c_device_id ad5820_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	{ "ad5820", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	{ "ad5821", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	{ "ad5823", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) MODULE_DEVICE_TABLE(i2c, ad5820_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static const struct of_device_id ad5820_of_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	{ .compatible = "adi,ad5820" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	{ .compatible = "adi,ad5821" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	{ .compatible = "adi,ad5823" },
^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) MODULE_DEVICE_TABLE(of, ad5820_of_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static SIMPLE_DEV_PM_OPS(ad5820_pm, ad5820_suspend, ad5820_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static struct i2c_driver ad5820_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		.name	= "ad5820",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		.pm	= &ad5820_pm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		.of_match_table = ad5820_of_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	.probe		= ad5820_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	.remove		= ad5820_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	.id_table	= ad5820_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) module_i2c_driver(ad5820_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) MODULE_AUTHOR("Tuukka Toivonen");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) MODULE_DESCRIPTION("AD5820 camera lens driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) MODULE_LICENSE("GPL");