forked from moko365/ProgramAssignment-LinuxDeviceDriver
-
Notifications
You must be signed in to change notification settings - Fork 15
/
omap34xx_sht7x.c
510 lines (422 loc) · 10.6 KB
/
omap34xx_sht7x.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* omap34xx_sht7x.c - Linux kernel module for SHT7x hardware monitoring
*
* Copyright (C) 2011 Moko365 Inc.
*
* Written by Jollen Chen <jollen@moko365.com>
*
*
* This file is subject to the terms and conditions of the GNU General
* Public License. See the file "COPYING" in the main directory of this
* archive for more details.
*
* 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/hrtimer.h>
#include <linux/module.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <linux/slab.h>
struct sht7x_data {
struct device *hwmon_dev; // Linux hardware minotoring
struct mutex lock; // Semphoare variable
const char *name;
char valid;
unsigned long last_updated;
u32 temperature; // Temperature
u32 humidity; // Humidity
u16 valueT;
u16 valueH;
};
static struct platform_device omap34xx_sht7x_device = {
.name = "omap34xx_sht7x",
.id = -1,
};
/*********************** internal ******************************/
void set_gpio_184_output(void);
void set_gpio_185_output(void);
void set_gpio_184_input(void);
void set_gpio_185_input(void);
void set_gpio_184_output(void)
{
}
void set_gpio_185_output(void)
{
}
void set_gpio_184_input(void)
{
}
void set_gpio_185_input(void)
{
}
static void gpio_184_high(void);
static void gpio_185_high(void);
static void gpio_184_low(void);
static void gpio_185_low(void);
/* GPIO High/Low */
static void gpio_184_high(void)
{
}
static void gpio_185_high(void)
{
}
static void gpio_184_low(void)
{
}
static void gpio_185_low(void)
{
}
/* GPIO 185 Data In */
static u32 gpio_185_read(void);
static u32 gpio_185_read()
{
return 0;
}
//#define SHT_SCL (1<<17)
//#define SHT_SDA (1<<16)
#define SHT_PORT_IN gpio_185_read()
#define SHT_SCL_HIGH() do{gpio_184_high();} while(0)
#define SHT_SCL_LOW() do{gpio_184_low();} while(0)
#define SHT_SDA_HIGH() do{gpio_185_high();} while(0)
#define SHT_SDA_LOW() do{gpio_185_low();} while(0)
/*Delay Function*/
/* Delay time = (13 + 4*value) * 1000us -> 3.82us*value */
#define delay_sht() udelay(7)
static inline void pulse_sht(void)
{
SHT_SCL_HIGH(); //CLOCK pulse -> approx 1.67us;
delay_sht();
SHT_SCL_LOW();
delay_sht();
}
typedef enum {
SHT_MEASURE_TEMP = 0x03,
SHT_MEASURE_HUMI = 0x05,
SHT_SOFT_RESET = 0x1e,
} SHTXX_CMD_e;
static void shtxx_Initial(void)
{
u8 i;
// Connection reset sequence
SHT_SDA_HIGH();
delay_sht();
SHT_SCL_LOW();
delay_sht();
for(i = 0; i < 9; i++){
SHT_SCL_HIGH();
delay_sht();
SHT_SCL_LOW();
delay_sht();
//delay 1ms
udelay(1000);
}
}
static void shtxx_start_transmission(void);
static void shtxx_start_transmission(void)
{
delay_sht();
SHT_SDA_HIGH();
delay_sht();
SHT_SCL_LOW(); //Initial state
delay_sht();
SHT_SCL_HIGH();
delay_sht();
SHT_SDA_LOW();
delay_sht();
SHT_SCL_LOW();
delay_sht();
SHT_SCL_HIGH();
delay_sht();
SHT_SDA_HIGH();
delay_sht();
SHT_SCL_LOW();
delay_sht();
//delay 1ms
#if 0
// use HR timer
#else
udelay(1000);
#endif
}
void sht_sync(void);
void sht_sync(void)
{
//spin_lock();
//omap34xx_sht7x->sync = 1;
//spin_unlock();
//wake_up();
}
u8 shtxx_write_byte(unsigned char output);
u8 shtxx_write_byte(unsigned char output)
{
u32 sWait;
u8 i;
for(i = 0; i < 8; i++)
{
if(output & 0x80)
SHT_SDA_HIGH();
else
SHT_SDA_LOW();
delay_sht();
output <<= 1;
pulse_sht();
delay_sht();
}
SHT_SDA_HIGH(); //release DATA-line
delay_sht();
pulse_sht();
delay_sht();
SHT_SDA_HIGH();
delay_sht();
sWait = 4000000; //Avoid infinity loop
while(sWait-- > 0){
if(!(SHT_PORT_IN)){ //!!! Wait for sensing finish
return true;
}
}
return false;
}
u8 shtxx_write_byte_no_wait(unsigned char output)
{
u8 i;
for(i = 0; i < 8; i++)
{
if(output & 0x80)
SHT_SDA_HIGH();
else
SHT_SDA_LOW();
delay_sht();
output <<= 1;
pulse_sht();
delay_sht();
}
SHT_SDA_HIGH(); //release DATA-line
delay_sht();
pulse_sht();
delay_sht();
SHT_SDA_HIGH();
delay_sht();
return true;
}
int shtxx_read_word(void)
{
unsigned int input = 0;
u8 i;
SHT_SDA_HIGH();
for(i = 0; i < 16; i++)
{
delay_sht();
if (SHT_PORT_IN)
input |= 0x0001;
if (i != 15)
input <<= 1;
if (i == 7)
{
pulse_sht();
delay_sht();
SHT_SDA_LOW();
delay_sht();
pulse_sht();
delay_sht();
SHT_SDA_HIGH(); //release the dataline
delay_sht();
}
else
{
pulse_sht();
delay_sht();
}
}
SHT_SDA_HIGH(); //ACK ...........no CRC
delay_sht();
pulse_sht();
delay_sht();
return input;
}
static void shtxx_read_TH(struct sht7x_data *pResTH)
{
u8 resT, resH;
/* 1. Measure Temperature */
shtxx_start_transmission(); //Start Transmission
resT = shtxx_write_byte(SHT_MEASURE_TEMP); //Write commmand
if(resT == true){
pResTH->valueT = shtxx_read_word(); //Read Temperature
}
/* 2. Measure Humidity */
shtxx_start_transmission(); //Start transmission with sensor
resH = shtxx_write_byte(SHT_MEASURE_HUMI); //Write commmand
if(resH == true){
pResTH->valueH = shtxx_read_word(); //Read Humidity
}
/* 3. Convert result */
//¨ìap¼h¦b°µ¹Bºâ¡A¦]¬°Driver¤£¤ä´©¯BÂI¼Æ
//shtxx_convert_TH(resT, resH, valueT, valueH, pResTH);
if(resT && resH){
pResTH->temperature = 0;
pResTH->humidity = 0;
}
else{
pResTH->valueT = 0;
pResTH->valueH = 0;
pResTH->temperature = 0xFFFF;
pResTH->humidity = 0xFFFF;
}
}
/***************************************************************/
static void sht7x_update(struct sht7x_data *sht7x)
{
mutex_lock(&sht7x->lock);
if (time_after(jiffies, sht7x->last_updated + HZ / 2) || !sht7x->valid) {
shtxx_read_TH(sht7x);
sht7x->last_updated = jiffies;
sht7x->valid = 1;
}
mutex_unlock(&sht7x->lock);
}
/**
* show_name -
* @dev:
* @devattr:
* @buf:
*
* Returns 0 on success, else negative errno.
*/
static ssize_t show_name(struct device *dev,
struct device_attribute *devattr, char *buf)
{
struct sht7x_data *data = dev_get_drvdata(dev);
return sprintf(buf, "%s\n", data->name);
}
/**
* show_temp -
* @dev:
* @devattr:
* @buf:
*
* Returns 0 on success, else negative errno.
*/
static ssize_t show_temp(struct device *dev,
struct device_attribute *devattr, char *buf)
{
struct sht7x_data *data = dev_get_drvdata(dev);
sht7x_update(data);
return sprintf(buf, "%d\n", data->temperature);
}
/**
* show_humidity -
* @dev:
* @devattr:
* @buf:
*
* Returns 0 on success, else negative errno.
*/
static ssize_t show_humidity(struct device *dev,
struct device_attribute *devattr, char *buf)
{
struct sht7x_data *data = dev_get_drvdata(dev);
sht7x_update(data);
//return sprintf(buf, "%d\n", data->humidity);
return sprintf(buf, "100\n");
}
static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
static SENSOR_DEVICE_ATTR_2(humidity1_input, S_IRUGO, show_humidity, NULL, 0, 0);
static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
static struct attribute *sht7x_attributes[] = {
&sensor_dev_attr_temp1_input.dev_attr,
&sensor_dev_attr_humidity1_input.dev_attr,
NULL
};
static const struct attribute_group sht7x_attr_group = {
.attrs = sht7x_attributes,
};
static int omap34xx_sht7x_probe(struct platform_device *pdev);
static int omap34xx_sht7x_probe(struct platform_device *pdev)
{
int err;
struct sht7x_data *data;
data = kzalloc(sizeof(struct sht7x_data), GFP_KERNEL);
if (!data) {
err = -ENOMEM;
goto exit_platform;
}
/* first initialized */
data->last_updated = jiffies;
shtxx_Initial();
dev_set_drvdata(&omap34xx_sht7x_device.dev, data);
mutex_init(&data->lock);
data->name = "omap34xx_sht7x";
err = sysfs_create_group(&omap34xx_sht7x_device.dev.kobj, &sht7x_attr_group);
if (err) {
printk(KERN_ALERT "could not create sysfs files\n");
goto exit_free;
}
err = device_create_file(&omap34xx_sht7x_device.dev, &dev_attr_name);
if (err)
goto exit_remove_all;
data->hwmon_dev = hwmon_device_register(&omap34xx_sht7x_device.dev);
if (IS_ERR(data->hwmon_dev)) {
err = PTR_ERR(data->hwmon_dev);
goto exit_remove_all;
}
printk(KERN_INFO "omap34xx_sht7x: initialized.\n");
return 0;
exit_remove_all:
device_remove_file(&omap34xx_sht7x_device.dev,
&dev_attr_name);
exit_free:
kfree(data);
exit_platform:
return err;
}
static int omap34xx_sht7x_remove(struct platform_device *pdev);
static int omap34xx_sht7x_remove(struct platform_device *pdev)
{
struct sht7x_data *data;
data = dev_get_drvdata(&omap34xx_sht7x_device.dev);
hwmon_device_unregister(data->hwmon_dev);
sysfs_remove_group(&omap34xx_sht7x_device.dev.kobj, &sht7x_attr_group);
kfree(data);
return 0;
}
static struct platform_driver omap34xx_sht7x_driver = {
.probe = omap34xx_sht7x_probe,
.remove = omap34xx_sht7x_remove, // __devexit_p()
.driver = {
.name = "omap34xx_sht7x",
.owner = THIS_MODULE,
},
};
static int __init omap34xx_sht7x_init(void)
{
int err;
err = platform_device_register(&omap34xx_sht7x_device);
if (err) {
printk(KERN_ERR
"Unable to register omap34xx SHT7x humidity and temperature sensor\n");
goto exit_platform;
}
return platform_driver_register(&omap34xx_sht7x_driver);
exit_platform:
return err;
}
static void __exit omap34xx_sht7x_exit(void)
{
platform_driver_unregister(&omap34xx_sht7x_driver);
platform_device_unregister(&omap34xx_sht7x_device);
}
MODULE_AUTHOR("Jollen Chen");
MODULE_DESCRIPTION("SHT7x humidity and temperature sensor for OMAP34xx");
MODULE_LICENSE("GPL");
module_init(omap34xx_sht7x_init)
module_exit(omap34xx_sht7x_exit)