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
|
#include <linux/i2c.h>
#include <linux/export.h>
//#include <linux/mutex.h>
#include "sensor.h"
//DEFINE_MUTEX(mutex_client);
//static struct i2c_client *sensor_client=NULL;
struct i2c_client *sensor_i2c_register_device(int bus_no, int client_addr, const char *client_name)
{
struct i2c_adapter *adapter = NULL;
struct i2c_client *sensor_client=NULL;
struct i2c_board_info sensor_i2c_board_info = {
.type = "unused",
.flags = 0x00,
.addr = 0xff,
.platform_data = NULL,
.archdata = NULL,
.irq = -1,
};
if ((bus_no<0) || (client_addr>0x7f) || (client_addr<0)|| (!client_name))
{
printk(KERN_ERR "%s param error! pls check out!\n", __FUNCTION__);
return NULL;
}
printk(KERN_INFO "%s busno %d client_addr 0x%x client_name %s \n", __FUNCTION__, \
bus_no, client_addr, client_name);
sensor_i2c_board_info.addr = client_addr;
//sensor_i2c_board_info.type = client_name;
strcpy(sensor_i2c_board_info.type, client_name);
adapter = i2c_get_adapter(bus_no);/*in bus NR*/
if (NULL == adapter) {
printk("can not get i2c adapter, client address error\n");
return NULL;
}
//mutex_lock(&mutex_client);
sensor_client = i2c_new_device(adapter, &sensor_i2c_board_info);
if (sensor_client == NULL) {
printk("allocate i2c client failed\n");
//mutex_unlock(&mutex_client);
return NULL;
}
i2c_put_adapter(adapter);
//mutex_unlock(&mutex_client);
return sensor_client;
}
EXPORT_SYMBOL(sensor_i2c_register_device);
struct i2c_client *sensor_i2c_register_device2(int bus_no, int client_addr, const char *client_name,void *pdata)
{
struct i2c_adapter *adapter = NULL;
struct i2c_client *sensor_client=NULL;
struct i2c_board_info sensor_i2c_board_info = {
.type = "unused",
.flags = 0x00,
.addr = 0xff,
.platform_data = NULL,
.archdata = NULL,
.irq = -1,
};
if ((bus_no<0) || (client_addr>0x7f) || (client_addr<0)|| (!client_name))
{
printk(KERN_ERR "%s param error! pls check out!\n", __FUNCTION__);
return NULL;
}
printk(KERN_INFO "%s busno %d client_addr 0x%x client_name %s \n", __FUNCTION__, \
bus_no, client_addr, client_name);
sensor_i2c_board_info.addr = client_addr;
sensor_i2c_board_info.platform_data = pdata;
//sensor_i2c_board_info.type = client_name;
strcpy(sensor_i2c_board_info.type, client_name);
adapter = i2c_get_adapter(bus_no);/*in bus NR*/
if (NULL == adapter) {
printk("can not get i2c adapter, client address error\n");
return NULL;
}
//mutex_lock(&mutex_client);
sensor_client = i2c_new_device(adapter, &sensor_i2c_board_info);
if (sensor_client == NULL) {
printk("allocate i2c client failed\n");
//mutex_unlock(&mutex_client);
return NULL;
}
i2c_put_adapter(adapter);
//mutex_unlock(&mutex_client);
return sensor_client;
}
EXPORT_SYMBOL(sensor_i2c_register_device2);
void sensor_i2c_unregister_device(struct i2c_client *client)
{
if (client != NULL)
{
i2c_unregister_device(client);
}
}
EXPORT_SYMBOL(sensor_i2c_unregister_device);
|