GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / media / atomisp / i2c / imx / otp_e2prom.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/errno.h>
22 #include <linux/fs.h>
23 #include <linux/init.h>
24 #include <linux/i2c.h>
25 #include <linux/io.h>
26 #include <linux/kernel.h>
27 #include <linux/mm.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/types.h>
31 #include <media/v4l2-device.h>
32 #include "common.h"
33
34 /*
35  * Read EEPROM data from the gerneral e2prom chip(eg.
36  * CAT24C08, CAT24C128, le24l042cs, and store
37  * it into a kmalloced buffer. On error return NULL.
38  * @size: set to the size of the returned EEPROM data.
39  */
40 void *e2prom_otp_read(struct v4l2_subdev *sd, u8 dev_addr,
41         u32 start_addr, u32 size)
42 {
43         struct i2c_client *client = v4l2_get_subdevdata(sd);
44         unsigned int e2prom_i2c_addr = dev_addr >> 1;
45         static const unsigned int max_read_size = 30;
46         int addr;
47         u32 s_addr = start_addr & E2PROM_ADDR_MASK;
48         bool two_addr = (start_addr & E2PROM_2ADDR) >> 31;
49         char *buffer;
50
51         buffer = devm_kzalloc(&client->dev, size, GFP_KERNEL);
52         if (!buffer)
53                 return NULL;
54
55         for (addr = s_addr; addr < size; addr += max_read_size) {
56                 struct i2c_msg msg[2];
57                 unsigned int i2c_addr = e2prom_i2c_addr;
58                 u16 addr_buf;
59                 int r;
60
61                 msg[0].flags = 0;
62                 if (two_addr) {
63                         msg[0].addr = i2c_addr;
64                         addr_buf = cpu_to_be16(addr & 0xFFFF);
65                         msg[0].len = 2;
66                         msg[0].buf = (u8 *)&addr_buf;
67                 } else {
68                         i2c_addr |= (addr >> 8) & 0x7;
69                         msg[0].addr = i2c_addr;
70                         addr_buf = addr & 0xFF;
71                         msg[0].len = 1;
72                         msg[0].buf = (u8 *)&addr_buf;
73                 }
74
75                 msg[1].addr = i2c_addr;
76                 msg[1].flags = I2C_M_RD;
77                 msg[1].len = min(max_read_size, size - addr);
78                 msg[1].buf = &buffer[addr];
79
80                 r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
81                 if (r != ARRAY_SIZE(msg)) {
82                         dev_err(&client->dev, "read failed at 0x%03x\n", addr);
83                         return NULL;
84                 }
85         }
86         return buffer;
87 }
88
89