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) // Copyright (c) 2018 Fuzhou Rockchip Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/rk-camera-module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <media/v4l2-ctrls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <media/v4l2-device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "imx258_eeprom_head.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #define DEVICE_NAME			"imx258_eeprom"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) static inline struct imx258_eeprom_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	*sd_to_imx258_eeprom(struct v4l2_subdev *subdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	return container_of(subdev, struct imx258_eeprom_device, sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /* Read registers up to 4 at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static int imx258_read_reg_otp(struct i2c_client *client, u16 reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	unsigned int len, u32 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct i2c_msg msgs[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	u8 *data_be_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	__be32 data_be = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	__be16 reg_addr_be = cpu_to_be16(reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	if (len > 4 || !len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	data_be_p = (u8 *)&data_be;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	/* Write register address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	msgs[0].addr = client->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	msgs[0].flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	msgs[0].len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	msgs[0].buf = (u8 *)&reg_addr_be;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	/* Read data from register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	msgs[1].addr = client->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	msgs[1].flags = I2C_M_RD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	msgs[1].len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	msgs[1].buf = &data_be_p[4 - len];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (ret != ARRAY_SIZE(msgs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	*val = be32_to_cpu(data_be);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static u8 get_vendor_flag(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	u8 vendor_flag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (client->addr == SLAVE_ADDRESS_GZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		vendor_flag |= 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return vendor_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static int imx258_otp_read_gz(struct imx258_eeprom_device *imx258_eeprom_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct i2c_client *client = imx258_eeprom_dev->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	int otp_flag, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct imx258_otp_info *otp_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct device *dev = &imx258_eeprom_dev->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	u32 r_value, gr_value, gb_value, b_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	u32 temp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	u32 checksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	otp_ptr = kzalloc(sizeof(*otp_ptr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (!otp_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	otp_flag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	/* OTP base information*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	ret = imx258_read_reg_otp(client, GZ_INFO_FLAG_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		1, &otp_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (otp_flag == 0x01) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		otp_ptr->flag = 0x80; /* valid INFO in OTP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		ret |= imx258_read_reg_otp(client, GZ_ID_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			1, &otp_ptr->module_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		ret |= imx258_read_reg_otp(client, GZ_LENS_ID_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			1, &otp_ptr->lens_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		ret |= imx258_read_reg_otp(client, GZ_PRODUCT_YEAR_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			1, &otp_ptr->year);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		ret |= imx258_read_reg_otp(client, GZ_PRODUCT_MONTH_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			1, &otp_ptr->month);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		ret |= imx258_read_reg_otp(client, GZ_PRODUCT_DAY_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			1, &otp_ptr->day);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		dev_dbg(dev, "fac info: module(0x%x) lens(0x%x) time(%d_%d_%d)!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			otp_ptr->module_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			otp_ptr->lens_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			otp_ptr->year,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			otp_ptr->month,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			otp_ptr->day);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	/* OTP WB calibration data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	ret = imx258_read_reg_otp(client, GZ_AWB_FLAG_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		1, &otp_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (otp_flag == 0x01) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		otp_ptr->flag |= 0x40; /* valid AWB in OTP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		ret |= imx258_read_reg_otp(client, GZ_CUR_R_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			1, &r_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		checksum += r_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		ret |= imx258_read_reg_otp(client, GZ_CUR_GR_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			1, &gr_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		checksum += gr_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		ret |= imx258_read_reg_otp(client, GZ_CUR_GB_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			1, &gb_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		checksum += gb_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		ret |= imx258_read_reg_otp(client, GZ_CUR_B_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			1, &b_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		checksum += b_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		otp_ptr->rg_ratio =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			r_value * 1024 / ((gr_value + gb_value) / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		otp_ptr->bg_ratio =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			b_value * 1024 / ((gr_value + gb_value) / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		ret |= imx258_read_reg_otp(client, GZ_GOLDEN_R_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			1, &r_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		checksum += r_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		ret |= imx258_read_reg_otp(client, GZ_GOLDEN_GR_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			1, &gr_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		checksum += gr_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		ret |= imx258_read_reg_otp(client, GZ_GOLDEN_GB_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			1, &gb_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		checksum += gb_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		ret |= imx258_read_reg_otp(client, GZ_GOLDEN_B_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			1, &b_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		checksum += b_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		otp_ptr->rg_golden =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			r_value * 1024 / ((gr_value + gb_value) / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		otp_ptr->bg_golden =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			b_value * 1024 / ((gr_value + gb_value) / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		ret |= imx258_read_reg_otp(client, GZ_AWB_CHECKSUM_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			1, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		if (ret != 0 || (checksum % 0xff) != temp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			dev_err(dev, "otp awb info: check sum (%d,%d),ret = %d !\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 				checksum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 				temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 				ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		dev_dbg(dev, "awb cur:(rg 0x%x, bg 0x%x,)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			otp_ptr->rg_ratio, otp_ptr->bg_ratio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		dev_dbg(dev, "awb gol:(rg 0x%x, bg 0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			otp_ptr->rg_golden, otp_ptr->bg_golden);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	checksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	/* OTP LSC calibration data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	ret = imx258_read_reg_otp(client, GZ_LSC_FLAG_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		1, &otp_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (otp_flag == 0x01) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		otp_ptr->flag |= 0x10; /* valid LSC in OTP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		for (i = 0; i < 504; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			ret |= imx258_read_reg_otp(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 				GZ_LSC_DATA_START_REG + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 				1, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			otp_ptr->lenc[i] = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			checksum += temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			dev_dbg(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				"otp read lsc addr = 0x%04x, lenc[%d] = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 				GZ_LSC_DATA_START_REG + i, i, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		ret |= imx258_read_reg_otp(client, GZ_LSC_CHECKSUM_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			1, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		if (ret != 0 || (checksum % 0xff) != temp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 				"otp lsc info: check sum (%d,%d),ret = %d !\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 				checksum, temp, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			goto err;
^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) 	checksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	/* OTP VCM calibration data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	ret = imx258_read_reg_otp(client, GZ_VCM_FLAG_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		1, &otp_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (otp_flag == 0x01) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		otp_ptr->flag |= 0x20; /* valid VCM in OTP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		ret |= imx258_read_reg_otp(client, GZ_VCM_DIR_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			1, &otp_ptr->vcm_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		checksum += otp_ptr->vcm_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		ret |= imx258_read_reg_otp(client, GZ_VCM_START_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			1, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		checksum += temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		ret |= imx258_read_reg_otp(client, GZ_VCM_START_REG + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			1, &otp_ptr->vcm_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		checksum += otp_ptr->vcm_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		otp_ptr->vcm_start |= (temp << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		ret |= imx258_read_reg_otp(client, GZ_VCM_END_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			1, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		checksum += temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		ret |= imx258_read_reg_otp(client, GZ_VCM_END_REG + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			1, &otp_ptr->vcm_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		checksum += otp_ptr->vcm_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		otp_ptr->vcm_end |= (temp << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		ret |= imx258_read_reg_otp(client, GZ_VCM_CHECKSUM_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			1, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		if (ret != 0 || (checksum % 0xff) != temp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 				"otp VCM info: check sum (%d,%d),ret = %d !\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 				checksum, temp, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		dev_dbg(dev, "vcm_info: 0x%x, 0x%x, 0x%x!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			otp_ptr->vcm_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			otp_ptr->vcm_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			otp_ptr->vcm_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	checksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	/* OTP SPC calibration data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	ret = imx258_read_reg_otp(client, GZ_SPC_FLAG_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		1, &otp_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (otp_flag == 0x01) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		otp_ptr->flag |= 0x08; /* valid LSC in OTP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		for (i = 0; i < 126; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			ret |= imx258_read_reg_otp(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 				GZ_SPC_DATA_START_REG + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 				1, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			otp_ptr->spc[i] = (uint8_t)temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			checksum += temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			dev_dbg(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 				"otp read spc addr = 0x%04x, spc[%d] = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 				GZ_SPC_DATA_START_REG + i, i, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		ret |= imx258_read_reg_otp(client, GZ_SPC_CHECKSUM_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			1, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		if (ret != 0 || (checksum % 0xff) != temp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 				"otp spc info: check sum (%d,%d),ret = %d !\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 				checksum, temp, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (otp_ptr->flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		imx258_eeprom_dev->otp = otp_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		imx258_eeprom_dev->otp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		kfree(otp_ptr);
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	imx258_eeprom_dev->otp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	kfree(otp_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int imx258_otp_read(struct imx258_eeprom_device *imx258_eeprom_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	u8 vendor_flag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	struct i2c_client *client = imx258_eeprom_dev->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	vendor_flag = get_vendor_flag(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (vendor_flag == 0x80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		imx258_otp_read_gz(imx258_eeprom_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static long imx258_eeprom_ioctl(struct v4l2_subdev *sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	unsigned int cmd, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	struct imx258_eeprom_device *imx258_eeprom_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		sd_to_imx258_eeprom(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	imx258_otp_read(imx258_eeprom_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (arg && imx258_eeprom_dev->otp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		memcpy(arg, imx258_eeprom_dev->otp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			sizeof(struct imx258_otp_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static const struct v4l2_subdev_core_ops imx258_eeprom_core_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	.ioctl = imx258_eeprom_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static const struct v4l2_subdev_ops imx258_eeprom_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	.core = &imx258_eeprom_core_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static void imx258_eeprom_subdev_cleanup(struct imx258_eeprom_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	v4l2_device_unregister_subdev(&dev->sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	media_entity_cleanup(&dev->sd.entity);
^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 int imx258_eeprom_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	struct imx258_eeprom_device *imx258_eeprom_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	dev_info(&client->dev, "probing...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	imx258_eeprom_dev = devm_kzalloc(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		sizeof(*imx258_eeprom_dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (imx258_eeprom_dev == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		dev_err(&client->dev, "Probe failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	v4l2_i2c_subdev_init(&imx258_eeprom_dev->sd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		client, &imx258_eeprom_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	imx258_eeprom_dev->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	pm_runtime_set_active(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	pm_runtime_enable(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	pm_runtime_idle(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	dev_info(&client->dev, "probing successful\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int imx258_eeprom_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	struct imx258_eeprom_device *imx258_eeprom_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		sd_to_imx258_eeprom(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	kfree(imx258_eeprom_dev->otp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	pm_runtime_disable(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	imx258_eeprom_subdev_cleanup(imx258_eeprom_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static int __maybe_unused imx258_eeprom_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static int  __maybe_unused imx258_eeprom_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static const struct i2c_device_id imx258_eeprom_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	{ DEVICE_NAME, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	{ { 0 } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) MODULE_DEVICE_TABLE(i2c, imx258_eeprom_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static const struct of_device_id imx258_eeprom_of_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	{ .compatible = "sony,imx258_eeprom" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	{ { 0 } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) MODULE_DEVICE_TABLE(of, imx258_eeprom_of_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) static const struct dev_pm_ops imx258_eeprom_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	SET_SYSTEM_SLEEP_PM_OPS(imx258_eeprom_suspend, imx258_eeprom_resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	SET_RUNTIME_PM_OPS(imx258_eeprom_suspend, imx258_eeprom_resume, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static struct i2c_driver imx258_eeprom_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		.name = DEVICE_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		.pm = &imx258_eeprom_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		.of_match_table = imx258_eeprom_of_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	.probe = &imx258_eeprom_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	.remove = &imx258_eeprom_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	.id_table = imx258_eeprom_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) module_i2c_driver(imx258_eeprom_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) MODULE_DESCRIPTION("IMX258 OTP driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) MODULE_LICENSE("GPL v2");