1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  * netup-eeprom.c
5  *
6  * 24LC02 EEPROM driver in conjunction with NetUP Dual DVB-S2 CI card
7  *
8  * Copyright (C) 2009 NetUP Inc.
9  * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru>
10  */
11 
12 #
13 #include "cx23885.h"
14 #include "netup-eeprom.h"
15 
16 #define EEPROM_I2C_ADDR 0x50
17 
netup_eeprom_read(struct i2c_adapter * i2c_adap,u8 addr)18 int netup_eeprom_read(struct i2c_adapter *i2c_adap, u8 addr)
19 {
20 	int ret;
21 	unsigned char buf[2];
22 
23 	/* Read from EEPROM */
24 	struct i2c_msg msg[] = {
25 		{
26 			.addr	= EEPROM_I2C_ADDR,
27 			.flags	= 0,
28 			.buf	= &buf[0],
29 			.len	= 1
30 		}, {
31 			.addr	= EEPROM_I2C_ADDR,
32 			.flags	= I2C_M_RD,
33 			.buf	= &buf[1],
34 			.len	= 1
35 		}
36 
37 	};
38 
39 	buf[0] = addr;
40 	buf[1] = 0x0;
41 
42 	ret = i2c_transfer(i2c_adap, msg, 2);
43 
44 	if (ret != 2) {
45 		pr_err("eeprom i2c read error, status=%d\n", ret);
46 		return -1;
47 	}
48 
49 	return buf[1];
50 };
51 
netup_eeprom_write(struct i2c_adapter * i2c_adap,u8 addr,u8 data)52 int netup_eeprom_write(struct i2c_adapter *i2c_adap, u8 addr, u8 data)
53 {
54 	int ret;
55 	unsigned char bufw[2];
56 
57 	/* Write into EEPROM */
58 	struct i2c_msg msg[] = {
59 		{
60 			.addr	= EEPROM_I2C_ADDR,
61 			.flags	= 0,
62 			.buf	= &bufw[0],
63 			.len	= 2
64 		}
65 	};
66 
67 	bufw[0] = addr;
68 	bufw[1] = data;
69 
70 	ret = i2c_transfer(i2c_adap, msg, 1);
71 
72 	if (ret != 1) {
73 		pr_err("eeprom i2c write error, status=%d\n", ret);
74 		return -1;
75 	}
76 
77 	mdelay(10); /* prophylactic delay, datasheet write cycle time = 5 ms */
78 	return 0;
79 };
80 
netup_get_card_info(struct i2c_adapter * i2c_adap,struct netup_card_info * cinfo)81 void netup_get_card_info(struct i2c_adapter *i2c_adap,
82 				struct netup_card_info *cinfo)
83 {
84 	int i, j;
85 
86 	cinfo->rev =  netup_eeprom_read(i2c_adap, 63);
87 
88 	for (i = 64, j = 0; i < 70; i++, j++)
89 		cinfo->port[0].mac[j] =  netup_eeprom_read(i2c_adap, i);
90 
91 	for (i = 70, j = 0; i < 76; i++, j++)
92 		cinfo->port[1].mac[j] =  netup_eeprom_read(i2c_adap, i);
93 };
94