2 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version
6 * 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #include <linux/bitops.h>
20 #include <linux/device.h>
21 #include <linux/delay.h>
22 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/i2c.h>
27 #include <linux/kernel.h>
29 #include <linux/string.h>
30 #include <linux/slab.h>
31 #include <linux/types.h>
32 #include <media/v4l2-device.h>
33 #include <asm/intel-mid.h>
36 /* Defines for OTP Data Registers */
37 #define IMX_OTP_START_ADDR 0x3B04
38 #define IMX_OTP_PAGE_SIZE 64
39 #define IMX_OTP_READY_REG 0x3B01
40 #define IMX_OTP_PAGE_REG 0x3B02
41 #define IMX_OTP_MODE_REG 0x3B00
42 #define IMX_OTP_PAGE_MAX 20
43 #define IMX_OTP_READY_REG_DONE 1
44 #define IMX_OTP_READ_ONETIME 32
45 #define IMX_OTP_MODE_READ 1
46 #define IMX227_OTP_START_ADDR 0x0A04
47 #define IMX227_OTP_ENABLE_REG 0x0A00
48 #define IMX227_OTP_READY_REG 0x0A01
49 #define IMX227_OTP_PAGE_REG 0x0A02
50 #define IMX227_OTP_READY_REG_DONE 1
51 #define IMX227_OTP_MODE_READ 1
54 imx_read_otp_data(struct i2c_client *client, u16 len, u16 reg, void *val)
56 struct i2c_msg msg[2];
57 u16 data[IMX_SHORT_MAX] = { 0 };
60 if (len > IMX_BYTE_MAX) {
61 dev_err(&client->dev, "%s error, invalid data length\n",
66 memset(msg, 0 , sizeof(msg));
67 memset(data, 0 , sizeof(data));
69 msg[0].addr = client->addr;
71 msg[0].len = I2C_MSG_LENGTH;
72 msg[0].buf = (u8 *)data;
73 /* high byte goes first */
74 data[0] = cpu_to_be16(reg);
76 msg[1].addr = client->addr;
78 msg[1].flags = I2C_M_RD;
79 msg[1].buf = (u8 *)data;
81 err = i2c_transfer(client->adapter, msg, 2);
88 memcpy(val, data, len);
92 dev_err(&client->dev, "read from offset 0x%x error %d", reg, err);
96 static int imx_read_otp_reg_array(struct i2c_client *client, u16 size, u16 addr,
102 for (index = 0; index + IMX_OTP_READ_ONETIME <= size;
103 index += IMX_OTP_READ_ONETIME) {
104 ret = imx_read_otp_data(client, IMX_OTP_READ_ONETIME,
105 addr + index, &buf[index]);
112 void *imx_otp_read(struct v4l2_subdev *sd, u8 dev_addr,
113 u32 start_addr, u32 size)
115 struct i2c_client *client = v4l2_get_subdevdata(sd);
120 buf = devm_kzalloc(&client->dev, size, GFP_KERNEL);
122 return ERR_PTR(-ENOMEM);
124 for (i = 0; i < IMX_OTP_PAGE_MAX; i++) {
127 ret = imx_write_reg(client, IMX_8BIT,
128 IMX_OTP_PAGE_REG, i & 0xff);
133 ret = imx_write_reg(client, IMX_8BIT,
134 IMX_OTP_MODE_REG, IMX_OTP_MODE_READ);
138 /* Reading the OTP data array */
139 ret = imx_read_otp_reg_array(client, IMX_OTP_PAGE_SIZE,
140 IMX_OTP_START_ADDR, buf + i * IMX_OTP_PAGE_SIZE);
147 /* Driver has failed to find valid data */
148 dev_err(&client->dev, "sensor found no valid OTP data\n");
152 void *imx227_otp_read(struct v4l2_subdev *sd, u8 dev_addr,
153 u32 start_addr, u32 size)
155 struct i2c_client *client = v4l2_get_subdevdata(sd);
160 buf = devm_kzalloc(&client->dev, size, GFP_KERNEL);
162 return ERR_PTR(-ENOMEM);
164 for (i = 0; i < IMX_OTP_PAGE_MAX; i++) {
167 ret = imx_write_reg(client, IMX_8BIT,
168 IMX227_OTP_PAGE_REG, i & 0xff);
173 ret = imx_write_reg(client, IMX_8BIT,
174 IMX227_OTP_ENABLE_REG, IMX227_OTP_MODE_READ);
178 /* Reading the OTP data array */
179 ret = imx_read_otp_reg_array(client, IMX_OTP_PAGE_SIZE,
180 IMX227_OTP_START_ADDR, buf + i * IMX_OTP_PAGE_SIZE);
187 /* Driver has failed to find valid data */
188 dev_err(&client->dev, "sensor found no valid OTP data\n");