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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/*
* Copyright (C) 2012 Spreadtrum Communications Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* 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.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include "sprdfb.h"
#include "sprdfb_panel.h"
#define SPRDFB_I2C_NAME "sprdfb_i2c"
#define SPRDFB_I2C_TRY_NUM (4)
static struct i2c_client *this_client = NULL;
static struct panel_spec *i2c_panel_info = NULL;
static struct i2c_device_id panel_i2c_id[] = {
{SPRDFB_I2C_NAME, 0},
{}
};
static unsigned short panel_i2c_default_addr_list[] =
{ I2C_CLIENT_END, I2C_CLIENT_END };
static int sprdfb_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int res = 0;
// struct sprdfb_device *dev = (struct sprdfb_device *)id->driver_data;;
printk(KERN_INFO "sprdfb: [%s], addr = %d\n", __FUNCTION__, client->addr);
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
printk(KERN_INFO "sprdfb: [%s]: functionality check failed\n",
__FUNCTION__);
res = -ENODEV;
goto out;
}
this_client = client;
if (i2c_panel_info->info.rgb->bus_info.i2c->i2c_addr != (this_client->addr & (~0xFF))) {
this_client->addr =
(this_client->addr & (~0xFF)) |
(i2c_panel_info->info.rgb->bus_info.i2c->i2c_addr & 0xFF);
}
printk(KERN_INFO "sordfb: [%s]:this_client->addr =0x%x\n", __FUNCTION__,
this_client->addr);
msleep(20);
return 0;
out:
return res;
}
static int sprdfb_i2c_remove(struct i2c_client *client)
{
return 0;
}
static int sprdfb_i2c_detect(struct i2c_client *client, struct i2c_board_info *info)
{
printk("SENSOR_DRV: detect!");
strcpy(info->type, client->name);
return 0;
}
static struct i2c_driver sprdfb_i2c_driver = {
.driver = {
.owner = THIS_MODULE,
},
.probe = sprdfb_i2c_probe,
.remove = sprdfb_i2c_remove,
.detect = sprdfb_i2c_detect,
};
bool sprdfb_i2c_init(struct sprdfb_device *dev)
{
/* panel_i2c_id[0].driver_data = dev;*/
i2c_panel_info = dev->panel;
panel_i2c_default_addr_list[0] = dev->panel->info.rgb->bus_info.i2c->i2c_addr;
sprdfb_i2c_driver.driver.name = SPRDFB_I2C_NAME;
sprdfb_i2c_driver.id_table = panel_i2c_id;
sprdfb_i2c_driver.address_list = &panel_i2c_default_addr_list[0];
if (i2c_add_driver(&sprdfb_i2c_driver)) {
printk("sprdfb: [%s] i2c_add_driver fail\n", __FUNCTION__);
return false;
}else{
printk("sprdfb: [%s] i2c_add_driver ok\n", __FUNCTION__);
}
return true;
}
bool sprdfb_i2c_uninit(struct sprdfb_device *dev)
{
/* panel_i2c_id[0].driver_data = dev;*/
panel_i2c_default_addr_list[0] = dev->panel->info.rgb->bus_info.i2c->i2c_addr;
sprdfb_i2c_driver.driver.name = SPRDFB_I2C_NAME;
sprdfb_i2c_driver.id_table = panel_i2c_id;
sprdfb_i2c_driver.address_list = &panel_i2c_default_addr_list[0];
i2c_panel_info = NULL;
i2c_del_driver(&sprdfb_i2c_driver);
return true;
}
/*write i2c, reg is 8 bit, val is 8 bit*/
static int32_t sprdfb_i2c_write_8bits(uint8_t reg, uint8_t val)
{
uint8_t buf_w[2];
uint32_t i = 0;
int32_t ret = -1;
struct i2c_msg msg_w;
if (0xFF == reg) {
mdelay(val);
printk("sprdfb: [%s], sleep %d\n", __FUNCTION__, val);
return 0;
}
buf_w[0] = (uint8_t) reg;
buf_w[1] = val;
msg_w.addr = this_client->addr;
msg_w.flags = 0;
msg_w.buf = buf_w;
msg_w.len = 2;
for (i = 0; i < SPRDFB_I2C_TRY_NUM; i++) {
ret = i2c_transfer(this_client->adapter, &msg_w, 1);
if (ret != 1) {
printk("sprdfb: [%s] write panel reg fai, ret : %d, I2C w addr: 0x%x, \n",
__FUNCTION__, ret, this_client->addr);
ret = -1;
msleep(20);
}else{
ret = 0;
break;
}
}
return ret;
}
/*read i2c, reg is 8 bit, val is 8 bit*/
static int32_t sprdfb_i2c_read_8bits(uint8_t reg, uint8_t *val)
{
uint8_t buf_w[1];
uint8_t buf_r;
uint32_t i = 0;
int32_t ret = -1;
struct i2c_msg msg_r[2];
buf_w[0] = reg;
msg_r[0].addr = this_client->addr;
msg_r[0].flags = 0;
msg_r[0].buf = buf_w;
msg_r[0].len = 1;
msg_r[1].addr = this_client->addr;
msg_r[1].flags = I2C_M_RD;
msg_r[1].buf = &buf_r;
msg_r[1].len = 1;
for (i = 0; i < SPRDFB_I2C_TRY_NUM; i++) {
ret = i2c_transfer(this_client->adapter, msg_r, 2);
if (ret != 2) {
printk("sprdfb: [%s]: read i2c reg fail, ret: %d, I2C r addr: 0x%x \n",
__FUNCTION__, ret, this_client->addr);
*val = 0xff;
ret = -1;
msleep(20);
}else{
*val = buf_r;
ret = 0;
break;
}
}
return ret;
}
/*write i2c, reg is 8 or 16 bit, val is 8 or 16bit*/
static int32_t sprdfb_i2c_write_16bits(uint16_t reg, bool reg_is_8bit, uint16_t val, bool val_is_8bit)
{
uint8_t cmd[4] = { 0 };
uint32_t i = 0;
uint32_t cmd_num = 0;
struct i2c_msg msg_w;
int32_t ret = -1;
if ((reg_is_8bit && (0xff == reg))||(!reg_is_8bit &&(0xffff == reg))) {
mdelay(val);
printk("sprdfb: [%s], sleep %d\n", __FUNCTION__, val);
return 0;
}
if (!reg_is_8bit) {
cmd[cmd_num++] = (uint8_t) ((reg >> 8) & 0xff);
cmd[cmd_num++] = (uint8_t) (reg & 0xff);
} else {
cmd[cmd_num++] = (uint8_t) reg;
}
if (!val_is_8bit) {
cmd[cmd_num++] = (uint8_t) ((val >> 8) & 0xff);
cmd[cmd_num++] = (uint8_t) (val & 0xff);
} else {
cmd[cmd_num++] = (uint8_t) val;
}
msg_w.addr = this_client->addr;
msg_w.flags = 0;
msg_w.buf = cmd;
msg_w.len = cmd_num;
for (i = 0; i < SPRDFB_I2C_TRY_NUM; i++) {
ret = i2c_transfer(this_client->adapter, &msg_w, 1);
if (ret != 1) {
printk("srpdfb [%s]: write sensor reg fai, ret : %d, I2C w addr: 0x%x, \n",
__FUNCTION__, ret, this_client->addr);
ret = -1;
msleep(20);
} else {
ret = 0;
break;
}
}
return ret;
}
/*read i2c, reg is 8 or 16 bit, val is 8 or 16bit*/
static int32_t sprdfb_i2c_read_16bits(uint16_t reg, bool reg_is_8bit, uint16_t *val, bool val_is_8bit)
{
uint32_t i = 0;
uint8_t cmd[2] = { 0 };
uint16_t w_cmd_num = 0;
uint16_t r_cmd_num = 0;
uint8_t buf_r[2] = { 0 };
int32_t ret = -1;
struct i2c_msg msg_r[2];
if (!reg_is_8bit) {
cmd[w_cmd_num++] = (uint8_t) ((reg >> 8) & 0xff);
cmd[w_cmd_num++] = (uint8_t) (reg & 0xff);
} else {
cmd[w_cmd_num++] = (uint8_t) reg;
}
if (!val_is_8bit) {
r_cmd_num = 2;
} else {
r_cmd_num = 1;
}
msg_r[0].addr = this_client->addr;
msg_r[0].flags = 0;
msg_r[0].buf = cmd;
msg_r[0].len = w_cmd_num;
msg_r[1].addr = this_client->addr;
msg_r[1].flags = I2C_M_RD;
msg_r[1].buf = buf_r;
msg_r[1].len = r_cmd_num;
for (i = 0; i < SPRDFB_I2C_TRY_NUM; i++) {
ret = i2c_transfer(this_client->adapter, msg_r, 2);
if (ret != 2) {
printk("srpdfb [%s]: read panel reg fai, ret : %d, I2C w addr: 0x%x, \n",
__FUNCTION__,ret, this_client->addr);
*val = 0xffff;
ret = -1;
msleep(20);
} else {
*val = (r_cmd_num == 1) ? (uint16_t) buf_r[0]
: (uint16_t) ((buf_r[0] << 8) + buf_r[1]);
ret = 0;
break;
}
}
return ret;
}
/*write i2c with burst mode*/
static int32_t sprdfb_i2c_write_burst(uint8_t* buf, int num)
{
uint32_t i = 0;
struct i2c_msg msg_w;
int32_t ret = -1;
for (i = 0; i < SPRDFB_I2C_TRY_NUM; i++) {
msg_w.addr = this_client->addr;
msg_w.flags = 0;
msg_w.buf = buf;
msg_w.len = num;
ret = i2c_transfer(this_client->adapter, &msg_w, 1);
if (ret != 1) {
printk("srpdfb [%s]: write i2c reg fai, ret : %d, I2C w addr: 0x%x, \n",
__FUNCTION__, ret, this_client->addr);
ret = -1;
msleep(20);
} else {
ret = 0;
break;
}
}
return ret;
}
struct ops_i2c sprdfb_i2c_ops = {
.i2c_write_8bits = sprdfb_i2c_write_8bits,
.i2c_read_8bits = sprdfb_i2c_read_8bits,
.i2c_write_16bits = sprdfb_i2c_write_16bits,
.i2c_read_16bits = sprdfb_i2c_read_16bits,
.i2c_write_burst = sprdfb_i2c_write_burst,
};
|