-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcp9808.c
256 lines (205 loc) · 5.74 KB
/
mcp9808.c
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
/*
*
* @Description: MCP9808 I2C sensor's linux device driver
* @Author: BenAli Samar
* @Date: Aug/2022
*
*/
#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/cdev.h>
#include<linux/slab.h>
#include<linux/kernel.h>
#include<linux/version.h>
#include<linux/errno.h>
#include<linux/device.h>
#include<linux/mutex.h>
#include<linux/i2c.h>
#include<linux/delay.h>
#include<linux/uaccess.h>
#define DEVICE_NAME "mcp9808"
static unsigned int mcp_major;
static unsigned int mcp_minor;
/* Sensor's data struct*/
struct mcp_data {
unsigned char *data;
struct i2c_client *client;
u8 readtmp;
struct cdev dev;
};
/*creating device class */
struct class *mcp_class = NULL;
/* open methode for kernel access to the device file*/
int mcp_open(struct inode *inode,struct file *filp)
{
struct mcp_data *mcp = NULL;
mcp = container_of(inode->i_cdev,struct mcp_data,dev);
if(mcp == NULL){
pr_err("Container_of did not found valid data\n");
return -ENODEV;
}
filp->private_data = mcp;
mcp->data = kmalloc(2,GFP_KERNEL);
if(mcp->data == NULL){
pr_err("Error allocating memory\n");
return -ENOMEM;
}
return 0;
}
/* release methode for freeeing the memory after acceding to the file*/
int mcp_release(struct inode *inode,struct file *filp)
{
struct mcp_data *mcp = filp->private_data;
if(mcp->data != NULL){
kfree(mcp->data);
mcp->data = NULL;
}
return 0;
}
/*Read data function*/
ssize_t mcp_read(struct file *filp,char __user *buf,size_t count,loff_t *f_pos)
{
u8 reg_addr;
struct i2c_msg msg[2];
struct mcp_data *mcp_data = filp->private_data;
u8 rawtmp[2];
/* Temperature raw value register address */
reg_addr = 0x05;
msg[0].addr = mcp_data->client->addr;
msg[0].flags = 0;
msg[0].len = 1;
msg[0].buf = ®_addr;
msg[1].addr = mcp_data->client->addr;
msg[1].flags = I2C_M_RD;
msg[1].len = 2;
msg[1].buf = rawtmp;
if(i2c_transfer(mcp_data->client->adapter,msg,2) < 0){
printk("i2c_transfer fail \n");
}
/* conversion tmp */
mcp_data->data[0] = ((rawtmp[0]>> 4)& 0x0f);
mcp_data->data[1] = (rawtmp[1]& 0x0f);
printk("Temprature value = %d%d Celsius degree\n",mcp_data->data[0],mcp_data->data[1]);
/* copy from kernel to user space */
if(copy_to_user(buf,mcp_data->data,2) != 0){
pr_err("copy_to_user fail\n");
return -EIO;
};
return 2;
}
/* file operation */
static struct file_operations mcp_fops = {
.owner = THIS_MODULE,
.open = mcp_open,
.release = mcp_release,
.read = mcp_read,
};
/*Probe function*/
static int mcp9808_probe(struct i2c_client *client, const struct i2c_device_id *id) {
struct mcp_data *mcp_data;
struct i2c_msg msg[2];
struct device *mcp_device=NULL;
unsigned char reg_addr = 0x06;
u8 data[2];
int err;
dev_t dev_no;
/*check functionnality*/
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
printk( "i2c_check_functionality failed ! \n");
return -ENODEV;
}
printk("Functinallity checked succefully\n");
/* check id*/
msg[0].addr =client->addr;
msg[0].flags=0;
msg[0].len =1;
msg[0].buf =®_addr;
msg[1].addr =client -> addr;
msg[1].flags = I2C_M_RD;
msg[1].len =2;
msg[1].buf =data;
if (i2c_transfer(client->adapter,msg,2)<0){
printk("i2c_transfer fail \n MCP9808 not found\n");
}
printk("MCP9808 Manufacture ID = %x\n", data [0]<<8| data [1]);
/* Allocate the major and minor number */
err = alloc_chrdev_region(&dev_no,0,1,DEVICE_NAME);
if(err < 0){
printk("alloc_chrdev_region fail\n");
return err;
}
mcp_major = MAJOR(dev_no);
mcp_minor = MINOR(dev_no);
printk("%s major num = %d -- minor num = %d\n",DEVICE_NAME,mcp_major,mcp_minor);
/* Create the sysfs class */
mcp_class = class_create(THIS_MODULE,DEVICE_NAME);
if(IS_ERR(mcp_class)){
err = PTR_ERR(mcp_class);
}
/* allocating the memory for device data */
mcp_data = kzalloc(sizeof (struct mcp_data),GFP_KERNEL);
if (mcp_data== NULL) {
printk("Memory allocation failed! \n");
return -ENOMEM;
}
printk("Memory allocated succefully\n");
mcp_data->data = NULL;
mcp_data->client = client;
printk ("Starting the registration of char device \n");
/* Initilize and register character device with kernel */
cdev_init(&mcp_data->dev,&mcp_fops);
mcp_data->dev.owner = THIS_MODULE;
err = cdev_add(&mcp_data->dev,dev_no,1);
if(err){
printk("Adding cdev fail\n");
}
printk("Device registred \n");
/* create the device so that user can access the slave device from user space */
mcp_device = device_create(mcp_class,
NULL,
dev_no,
NULL,
DEVICE_NAME);
if(IS_ERR(mcp_device)) {
err = PTR_ERR(mcp_device);
printk("Device creation fail\n");
cdev_del(&mcp_data->dev);
}
printk ("Device created succeffully\n");
i2c_set_clientdata(client,mcp_data);
return 0;
return 0;
};
/*Remove function*/
static int mcp9808_remove(struct i2c_client *client)
{
struct mcp_data *mcp_data = i2c_get_clientdata(client);
kfree(mcp_data) ;
unregister_chrdev_region(MKDEV(mcp_major,mcp_minor),1);
printk("MCP9808's module removed successfully\n");
return 0;
}
/* for the device tree stuff plus the compatible one ..*/
static const struct of_device_id mcp98xx_of[] = {
{.compatible = "microchip,mcp9808","brcm,bcm2708",},
{ },
};
static const struct i2c_device_id mcp98xx_id[] = {
{"mcp9808",0},
{},
};
MODULE_DEVICE_TABLE(i2c, mcp98xx_id);
static struct i2c_driver mcp9808_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "mcp9808",
.of_match_table = of_match_ptr(mcp98xx_of),
},
.probe = mcp9808_probe,
.remove = mcp9808_remove,
.id_table = mcp98xx_id,
};
module_i2c_driver(mcp9808_driver);
MODULE_AUTHOR("SAMAR_BENALI");
MODULE_LICENSE ("GPL");