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/errno.h>
23 #include <linux/init.h>
24 #include <linux/i2c.h>
26 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/types.h>
31 #include <media/v4l2-device.h>
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.
40 void *e2prom_otp_read(struct v4l2_subdev *sd, u8 dev_addr,
41 u32 start_addr, u32 size)
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;
47 u32 s_addr = start_addr & E2PROM_ADDR_MASK;
48 bool two_addr = (start_addr & E2PROM_2ADDR) >> 31;
51 buffer = devm_kzalloc(&client->dev, size, GFP_KERNEL);
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;
63 msg[0].addr = i2c_addr;
64 addr_buf = cpu_to_be16(addr & 0xFFFF);
66 msg[0].buf = (u8 *)&addr_buf;
68 i2c_addr |= (addr >> 8) & 0x7;
69 msg[0].addr = i2c_addr;
70 addr_buf = addr & 0xFF;
72 msg[0].buf = (u8 *)&addr_buf;
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];
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);