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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/* *
* Copyright 2011 Pixtree Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* @file pix_i2c.c
* @brief
* @author hun chan Yu(kingmst@pixtree.com)
* @version 1.0.1
* @history
* 2011.11.14 : Create
*/
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/fcntl.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/miscdevice.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/mach-types.h>
#include <mach/gpio.h>
#include <mach/regs-gpio.h>
#include <plat/gpio-cfg.h>
#include <mach/map.h>
#include <asm/atomic.h>
#include "pix_i2c.h"
#if defined(CONFIG_BOARD_ODROID_A)
#error
#elif defined(CONFIG_BOARD_ODROID_A4)
#define PIN_SDA EXYNOS4_GPX0(5)
#define PIN_SCL EXYNOS4_GPX0(2)
#elif defined(CONFIG_BOARD_ODROID_PC)
#define PIN_SDA EXYNOS4_GPC0(0)
#define PIN_SCL EXYNOS4_GPC0(3)
#elif defined(CONFIG_BOARD_ETRI_RTU)
#define PIN_SDA EXYNOS4_GPX0(5)
#define PIN_SCL EXYNOS4_GPX0(0)
#elif defined(CONFIG_BOARD_ODROID_Q)
#define PIN_SDA EXYNOS4_GPD0(2)
#define PIN_SCL EXYNOS4_GPD0(3)
#else
#error
#endif
#define PIX_I2C_MINOR (249)
#define SDA_IN gpio_direction_input(PIN_SDA)
#define SDA_OUT gpio_direction_input(PIN_SDA)
#define SDA_LOW gpio_direction_output(PIN_SDA,0)
#define SDA_HIGH gpio_direction_input(PIN_SDA)
#define SDA_DETECT gpio_get_value(PIN_SDA)
#define SCL_LOW gpio_direction_output(PIN_SCL,0)
#define SCL_HIGH gpio_direction_input(PIN_SCL)
#define SCL_OUT gpio_direction_input(PIN_SCL)
#define I2C_DELAY udelay(2)
#define I2C_DELAY_LONG udelay(50)
static atomic_t scull_available = ATOMIC_INIT(1);
int InitGPIO(void)
{
gpio_request(PIN_SDA,"SDA");
gpio_request(PIN_SCL,"SCL");
SDA_HIGH;
SCL_HIGH;
return 0;
}
void ReleaseGPIO(void)
{
gpio_free(PIN_SDA);
gpio_free(PIN_SCL);
return;
}
void I2c_start(void)
{
SDA_HIGH;
I2C_DELAY;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
SDA_LOW;
I2C_DELAY;
SCL_LOW;
I2C_DELAY;
}
void I2c_stop(void)
{
SDA_LOW;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
SDA_HIGH;
I2C_DELAY_LONG;
}
unsigned char I2c_ack_detect(void)
{
SDA_IN; // SDA Input Mode
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
if (SDA_DETECT)
{
SDA_OUT;
return ERROR_CODE_FALSE; // false
}
I2C_DELAY;
SCL_LOW;
SDA_OUT;
return ERROR_CODE_TRUE; // true
}
void I2c_ack_send(void)
{
SDA_OUT;
SDA_LOW;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
SCL_LOW;
I2C_DELAY;
}
unsigned char I2c_write_byte(unsigned char data)
{
int i;
for(i = 0; i< 8; i++)
{
if( (data << i) & 0x80) SDA_HIGH;
else SDA_LOW;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
SCL_LOW;
I2C_DELAY;
}
if(I2c_ack_detect()!=ERROR_CODE_TRUE) {
return ERROR_CODE_FALSE;
}
return ERROR_CODE_TRUE;
}
unsigned char I2c_read_byte(void)
{
int i;
unsigned char data;
data = 0;
SDA_IN;
for(i = 0; i< 8; i++){
data <<= 1;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
if (SDA_DETECT) data |= 0x01;
SCL_LOW;
I2C_DELAY;
}
I2c_ack_send();
return data;
}
unsigned char I2c_write(unsigned char device_addr, unsigned char sub_addr, unsigned char *buff, int ByteNo)
{
int i;
I2c_start();
I2C_DELAY;
if(I2c_write_byte(device_addr)) {
I2c_stop();
return ERROR_CODE_WRITE_ADDR;
}
if(I2c_write_byte(sub_addr)) {
I2c_stop();
return ERROR_CODE_WRITE_ADDR;
}
for(i = 0; i<ByteNo; i++) {
if(I2c_write_byte(buff[i])) {
I2c_stop();
return ERROR_CODE_WRITE_DATA;
}
}
I2C_DELAY;
I2c_stop();
I2C_DELAY_LONG;
return ERROR_CODE_TRUE;
}
unsigned char I2c_read(unsigned char device_addr, unsigned char sub_addr, unsigned char *buff, int ByteNo)
{
int i;
I2c_start();
I2C_DELAY;
if(I2c_write_byte(device_addr)) {
I2c_stop();
DPRINTK("Fail to write device addr\n");
return ERROR_CODE_READ_ADDR;
}
if(I2c_write_byte(sub_addr)) {
I2c_stop();
DPRINTK("Fail to write sub addr\n");
return ERROR_CODE_READ_ADDR;
}
I2c_start();
I2C_DELAY;
if(I2c_write_byte(device_addr+1)) {
I2c_stop();
DPRINTK("Fail to write device addr+1\n");
return ERROR_CODE_READ_ADDR;
}
for(i = 0; i<ByteNo; i++) buff[i] = I2c_read_byte();
I2C_DELAY;
I2C_DELAY_LONG;
I2c_stop();
I2C_DELAY_LONG;
return ERROR_CODE_TRUE;
}
int PixI2cWrite(unsigned char device_addr, unsigned char sub_addr, void *ptr_data, unsigned int ui_data_length)
{
unsigned char uc_return;
if((uc_return = I2c_write(device_addr, sub_addr, (unsigned char*)ptr_data, ui_data_length))!=ERROR_CODE_TRUE)
{
DPRINTK("[Write]error code = %d\n",uc_return);
return -1;
}
return 0;
}
int PixI2cRead(unsigned char device_addr, unsigned char sub_addr, void *ptr_data, unsigned int ui_data_length)
{
unsigned char uc_return;
if((uc_return=I2c_read(device_addr, sub_addr, (unsigned char*)ptr_data, ui_data_length))!=ERROR_CODE_TRUE)
{
DPRINTK("[Read]error code = %d\n",uc_return);
return -1;
}
return 0;
}
//static int PixI2c_Ioctl(struct inode *inode, struct file *fp, unsigned int cmd, unsigned long arg)
static int PixI2c_Ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
{
PPIX_IIC_DATA ptr_user_data;
unsigned int ui_data_length;
unsigned char uc_device_addr;
unsigned char uc_sub_addr;
void *ptr_data=NULL;
int i_return=0;
switch(cmd)
{
case IOCTL_PIX_I2C_INIT:
InitGPIO();
break;
case IOCTL_PIX_I2C_TERMINATE:
ReleaseGPIO();
break;
case IOCTL_PIX_I2C_READ:
ptr_user_data = (PPIX_IIC_DATA)arg;
if((i_return=get_user(uc_device_addr, &ptr_user_data->device_addr))<0)
{
goto ERROR_IOCTL;
}
if((i_return=get_user(uc_sub_addr, &ptr_user_data->sub_addr))<0)
{
goto ERROR_IOCTL;
}
if((i_return=get_user(ui_data_length, &ptr_user_data->ui_data_length))<0)
{
goto ERROR_IOCTL;
}
ptr_data = kmalloc(ui_data_length,GFP_KERNEL);
if((i_return=PixI2cRead(uc_device_addr, uc_sub_addr, ptr_data, ui_data_length))<0)
goto ERROR_IOCTL;
if((i_return=copy_to_user((void*)ptr_user_data->ptr_data, ptr_data, ui_data_length))<0)
goto ERROR_IOCTL;
break;
case IOCTL_PIX_I2C_WRITE:
ptr_user_data = (PPIX_IIC_DATA)arg;
if((i_return=get_user(uc_device_addr, &ptr_user_data->device_addr))<0)
goto ERROR_IOCTL;
if((i_return=get_user(uc_sub_addr, &ptr_user_data->sub_addr))<0)
goto ERROR_IOCTL;
if((i_return=get_user(ui_data_length, &ptr_user_data->ui_data_length))<0)
goto ERROR_IOCTL;
ptr_data = kmalloc(ui_data_length,GFP_KERNEL);
if((i_return=copy_from_user(ptr_data, (const void*)ptr_user_data->ptr_data, ui_data_length))<0)
goto ERROR_IOCTL;
if((i_return=PixI2cWrite(uc_device_addr, uc_sub_addr, ptr_data, ui_data_length))<0)
goto ERROR_IOCTL;
break;
default: i_return=-EINVAL; break;
}
ERROR_IOCTL:
KERNEL_FREE(ptr_data);
return i_return;
}
static int PixI2c_Open(struct inode *pInode, struct file *pFile)
{
int i_return = -ENODEV;
int iNum= MINOR(pInode->i_rdev);
if(!atomic_dec_and_test(&scull_available))
{
atomic_inc(&scull_available);
return -EBUSY;
}
if(!try_module_get(THIS_MODULE))
{
DPRINTK("Failed iic driver for pixtree\n");
goto OUT;
}
i_return = 0;
DPRINTK("open iic driver for pixtree minor : %d\n",iNum);
OUT:
return i_return;
}
static int PixI2c_Release(struct inode *inode, struct file *file)
{
module_put(THIS_MODULE);
DPRINTK("release iic driver for pixtree\n");
atomic_inc(&scull_available);
return 0;
}
static const struct file_operations pix_iic_fops=
{
.owner = THIS_MODULE,
.open = PixI2c_Open,
.unlocked_ioctl = PixI2c_Ioctl,
.release = PixI2c_Release,
};
static struct miscdevice pix_i2c_dev={
.minor = PIX_I2C_MINOR,
.name = "pix_i2c",
.fops = &pix_iic_fops,
};
int __init PixI2c_Init(void)
{
int nResult;
nResult = misc_register(&pix_i2c_dev);
if(nResult)
{
DPRINTK("[init PixI2c_Init]fail to regist dev...................!!!!!!!!!!!!!!!!!\n");
return nResult;
}
InitGPIO();
s5p_gpio_set_pd_cfg(PIN_SDA, S3C_GPIO_SPECIAL(2));
s5p_gpio_set_pd_cfg(PIN_SCL, S3C_GPIO_SPECIAL(2));
s5p_gpio_set_pd_pull(PIN_SDA, S3C_GPIO_PULL_NONE);
s5p_gpio_set_pd_pull(PIN_SCL, S3C_GPIO_PULL_NONE);
return 0;
}
void __exit PixI2c_Exit(void)
{
misc_deregister(&pix_i2c_dev);
}
#ifndef MODULE
__initcall(PixI2c_Init);
#else
module_init(PixI2c_Init);
module_exit(PixI2c_Exit);
#endif
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Pixtree, Inc.");
MODULE_DESCRIPTION("for IIC communication");
|