summaryrefstree
path: root/kernel/drivers/ccic/s2mm005-i2c.c
blob: 819bb286c3abb5908b8887a226f9e4f3c34976c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * driver/ccic/s2mm005-i2c.c - S2MM005 USBPD I2C device driver
 *
 * Copyright (C) 2017 Samsung Electronics
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */
#include <linux/ccic/s2mm005.h>

int s2mm005_read_byte(const struct i2c_client *i2c, u16 reg, u8 *val, u16 size)
{
	int ret, i2c_retry; u8 wbuf[2];
	struct i2c_msg msg[2];
	struct s2mm005_data *usbpd_data = i2c_get_clientdata(i2c);

	mutex_lock(&usbpd_data->i2c_mutex);
	i2c_retry = 0;
	msg[0].addr = i2c->addr;
	msg[0].flags = i2c->flags;
	msg[0].len = 2;
	msg[0].buf = wbuf;
	msg[1].addr = i2c->addr;
	msg[1].flags = I2C_M_RD;
	msg[1].len = size;
	msg[1].buf = val;

	wbuf[0] = (reg & 0xFF00) >> 8;
	wbuf[1] = (reg & 0xFF);

	do {
		ret = i2c_transfer(i2c->adapter, msg, ARRAY_SIZE(msg));
	} while (ret < 0 &&  i2c_retry++ < 5);

	if (ret < 0)
		dev_err(&i2c->dev, "i2c read16 fail reg:0x%x error %d\n", reg, ret);

	mutex_unlock(&usbpd_data->i2c_mutex);

	return ret;
}

int s2mm005_write_byte(const struct i2c_client *i2c, u16 reg, u8 *val, u16 size)
{
	int ret, i2c_retry; u8 buf[258] = {0,};
	struct i2c_msg msg[1];
	struct s2mm005_data *usbpd_data = i2c_get_clientdata(i2c);

	if (size > 256) {
		pr_err("I2C error, over the size %d", size);
		return -EIO;
	}

	mutex_lock(&usbpd_data->i2c_mutex);
	i2c_retry = 0;
	msg[0].addr = i2c->addr;
	msg[0].flags = 0;
	msg[0].len = size+2;
	msg[0].buf = buf;

	buf[0] = (reg & 0xFF00) >> 8;
	buf[1] = (reg & 0xFF);
	memcpy(&buf[2], val, size);

	do {
		ret = i2c_transfer(i2c->adapter, msg, 1);
	} while (ret < 0 &&  i2c_retry++ < 5);

	if (ret < 0)
		dev_err(&i2c->dev, "i2c write fail reg:0x%x error %d\n", reg, ret);

	mutex_unlock(&usbpd_data->i2c_mutex);

	return ret;
}

void s2mm005_sel_write(struct i2c_client *i2c, u16 reg, u8 *buf, i2c_slv_cmd_t cmd)
{
	u8 W_DATA[8] = {0,};

	switch (cmd) {
	case SEL_WRITE_BYTE:
	case SEL_WRITE_WORD:
	case SEL_WRITE_LONG:
		break;
	default:
		dev_err(&i2c->dev, "%s, cmd is worng cmd(%d)\n", __func__, cmd);
		return;
	}

	W_DATA[0] = 0x02;
	W_DATA[1] = cmd;
	W_DATA[2] = reg & 0xFF;
	W_DATA[3] = (reg & 0xFF00) >> 8;
	memcpy(&W_DATA[4], buf, cmd);

	s2mm005_write_byte(i2c, REG_I2C_SLV_CMD, W_DATA, cmd + 4);
}

void s2mm005_sel_read(struct i2c_client *i2c, u16 reg, u8 *buf, i2c_slv_cmd_t cmd)
{
	u8 W_DATA[4] = {0,};
	u8 size;

	switch (cmd) {
	case SEL_READ_BYTE:
	case SEL_READ_WORD:
	case SEL_READ_LONG:
		size = cmd >> 8;
		break;
	default:
		dev_err(&i2c->dev, "%s, cmd is worng cmd(%d)\n", __func__, cmd);
		return;
	}

	W_DATA[0] = 0x02;
	W_DATA[1] = cmd;
	W_DATA[2] = reg & 0xFF;
	W_DATA[3] = (reg & 0xFF00) >> 8;
	s2mm005_write_byte(i2c, S2MM005_REG_I2C_CMD, W_DATA, 4);

	s2mm005_read_byte(i2c, S2MM005_REG_I2C_RESPONSE, buf, size);
}

void s2mm005_int_clear(struct s2mm005_data *usbpd_data)
{
	struct i2c_client *i2c = usbpd_data->i2c;
	u8 val = 0;

	dev_info(usbpd_data->dev, "%s : -- clear clear -- \n", __func__);
	val = 0x01;
	s2mm005_write_byte(i2c, S2MM005_REG_I2C_CMD, &val, 1);
}

void s2mm005_system_reset(struct s2mm005_data *usbpd_data)
{
	struct i2c_client *i2c = usbpd_data->i2c;
	u8 R_DATA[2];
	u8 W_DATA[2];

	s2mm005_sel_read(i2c, 0x1064, R_DATA, SEL_READ_WORD);

	/* SYSTEM RESET */
	W_DATA[0] = R_DATA[0];
	W_DATA[1] = R_DATA[1];

	s2mm005_sel_write(i2c, 0x1068, W_DATA, SEL_WRITE_WORD);
}