GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / media / atomisp / i2c / imx / otp_imx.c
1 /*
2  * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
3  *
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.
7  *
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.
12  *
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
16  * 02110-1301, USA.
17  *
18  */
19 #include <linux/bitops.h>
20 #include <linux/device.h>
21 #include <linux/delay.h>
22 #include <linux/errno.h>
23 #include <linux/fs.h>
24 #include <linux/init.h>
25 #include <linux/i2c.h>
26 #include <linux/io.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.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>
34 #include "common.h"
35
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
52
53 static int
54 imx_read_otp_data(struct i2c_client *client, u16 len, u16 reg, void *val)
55 {
56         struct i2c_msg msg[2];
57         u16 data[IMX_SHORT_MAX] = { 0 };
58         int err;
59
60         if (len > IMX_BYTE_MAX) {
61                 dev_err(&client->dev, "%s error, invalid data length\n",
62                         __func__);
63                 return -EINVAL;
64         }
65
66         memset(msg, 0 , sizeof(msg));
67         memset(data, 0 , sizeof(data));
68
69         msg[0].addr = client->addr;
70         msg[0].flags = 0;
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);
75
76         msg[1].addr = client->addr;
77         msg[1].len = len;
78         msg[1].flags = I2C_M_RD;
79         msg[1].buf = (u8 *)data;
80
81         err = i2c_transfer(client->adapter, msg, 2);
82         if (err != 2) {
83                 if (err >= 0)
84                         err = -EIO;
85                 goto error;
86         }
87
88         memcpy(val, data, len);
89         return 0;
90
91 error:
92         dev_err(&client->dev, "read from offset 0x%x error %d", reg, err);
93         return err;
94 }
95
96 static int imx_read_otp_reg_array(struct i2c_client *client, u16 size, u16 addr,
97                                   u8 *buf)
98 {
99         u16 index;
100         int ret;
101
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]);
106                 if (ret)
107                         return ret;
108         }
109         return 0;
110 }
111
112 void *imx_otp_read(struct v4l2_subdev *sd, u8 dev_addr,
113         u32 start_addr, u32 size)
114 {
115         struct i2c_client *client = v4l2_get_subdevdata(sd);
116         u8 *buf;
117         int ret;
118         int i;
119
120         buf = devm_kzalloc(&client->dev, size, GFP_KERNEL);
121         if (!buf)
122                 return ERR_PTR(-ENOMEM);
123
124         for (i = 0; i < IMX_OTP_PAGE_MAX; i++) {
125
126                 /*set page NO.*/
127                 ret = imx_write_reg(client, IMX_8BIT,
128                                IMX_OTP_PAGE_REG, i & 0xff);
129                 if (ret)
130                         goto fail;
131
132                 /*set read mode*/
133                 ret = imx_write_reg(client, IMX_8BIT,
134                                IMX_OTP_MODE_REG, IMX_OTP_MODE_READ);
135                 if (ret)
136                         goto fail;
137
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);
141                 if (ret)
142                         goto fail;
143         }
144
145         return buf;
146 fail:
147         /* Driver has failed to find valid data */
148         dev_err(&client->dev, "sensor found no valid OTP data\n");
149         return ERR_PTR(ret);
150 }
151
152 void *imx227_otp_read(struct v4l2_subdev *sd, u8 dev_addr,
153         u32 start_addr, u32 size)
154 {
155         struct i2c_client *client = v4l2_get_subdevdata(sd);
156         u8 *buf;
157         int ret;
158         int i;
159
160         buf = devm_kzalloc(&client->dev, size, GFP_KERNEL);
161         if (!buf)
162                 return ERR_PTR(-ENOMEM);
163
164         for (i = 0; i < IMX_OTP_PAGE_MAX; i++) {
165
166                 /*set page NO.*/
167                 ret = imx_write_reg(client, IMX_8BIT,
168                                IMX227_OTP_PAGE_REG, i & 0xff);
169                 if (ret)
170                         goto fail;
171
172                 /*set read mode*/
173                 ret = imx_write_reg(client, IMX_8BIT,
174                                IMX227_OTP_ENABLE_REG, IMX227_OTP_MODE_READ);
175                 if (ret)
176                         goto fail;
177
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);
181                 if (ret)
182                         goto fail;
183         }
184
185         return buf;
186 fail:
187         /* Driver has failed to find valid data */
188         dev_err(&client->dev, "sensor found no valid OTP data\n");
189         return ERR_PTR(ret);
190 }
191