Blame view

drivers/char/ds1620.c 8.53 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
  /*
   * linux/drivers/char/ds1620.c: Dallas Semiconductors DS1620
   *   thermometer driver (as used in the Rebel.com NetWinder)
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
  #include <linux/miscdevice.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
9
10
  #include <linux/delay.h>
  #include <linux/proc_fs.h>
  #include <linux/capability.h>
  #include <linux/init.h>
613655fa3   Arnd Bergmann   drivers: autoconv...
11
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12

a09e64fbc   Russell King   [ARM] Move includ...
13
  #include <mach/hardware.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <asm/mach-types.h>
  #include <asm/uaccess.h>
  #include <asm/therm.h>
  
  #ifdef CONFIG_PROC_FS
  /* define for /proc interface */
  #define THERM_USE_PROC
  #endif
  
  /* Definitions for DS1620 chip */
  #define THERM_START_CONVERT	0xee
  #define THERM_RESET		0xaf
  #define THERM_READ_CONFIG	0xac
  #define THERM_READ_TEMP		0xaa
  #define THERM_READ_TL		0xa2
  #define THERM_READ_TH		0xa1
  #define THERM_WRITE_CONFIG	0x0c
  #define THERM_WRITE_TL		0x02
  #define THERM_WRITE_TH		0x01
  
  #define CFG_CPU			2
  #define CFG_1SHOT		1
613655fa3   Arnd Bergmann   drivers: autoconv...
36
  static DEFINE_MUTEX(ds1620_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
39
40
41
42
43
44
45
  static const char *fan_state[] = { "off", "on", "on (hardwired)" };
  
  /*
   * Start of NetWinder specifics
   *  Note!  We have to hold the gpio lock with IRQs disabled over the
   *  whole of our transaction to the Dallas chip, since there is a
   *  chance that the WaveArtist driver could touch these bits to
   *  enable or disable the speaker.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
48
49
  extern unsigned int system_rev;
  
  static inline void netwinder_ds1620_set_clk(int clk)
  {
70d13e083   Russell King   [ARM] netwinder: ...
50
  	nw_gpio_modify_op(GPIO_DSCLK, clk ? GPIO_DSCLK : 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
54
  }
  
  static inline void netwinder_ds1620_set_data(int dat)
  {
70d13e083   Russell King   [ARM] netwinder: ...
55
  	nw_gpio_modify_op(GPIO_DATA, dat ? GPIO_DATA : 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
59
  }
  
  static inline int netwinder_ds1620_get_data(void)
  {
70d13e083   Russell King   [ARM] netwinder: ...
60
  	return nw_gpio_read() & GPIO_DATA;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
62
63
64
  }
  
  static inline void netwinder_ds1620_set_data_dir(int dir)
  {
70d13e083   Russell King   [ARM] netwinder: ...
65
  	nw_gpio_modify_io(GPIO_DATA, dir ? GPIO_DATA : 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
67
68
69
  }
  
  static inline void netwinder_ds1620_reset(void)
  {
70d13e083   Russell King   [ARM] netwinder: ...
70
71
  	nw_cpld_modify(CPLD_DS_ENABLE, 0);
  	nw_cpld_modify(CPLD_DS_ENABLE, CPLD_DS_ENABLE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
73
74
75
  }
  
  static inline void netwinder_lock(unsigned long *flags)
  {
70d13e083   Russell King   [ARM] netwinder: ...
76
  	spin_lock_irqsave(&nw_gpio_lock, *flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
79
80
  }
  
  static inline void netwinder_unlock(unsigned long *flags)
  {
70d13e083   Russell King   [ARM] netwinder: ...
81
  	spin_unlock_irqrestore(&nw_gpio_lock, *flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
84
85
86
  }
  
  static inline void netwinder_set_fan(int i)
  {
  	unsigned long flags;
70d13e083   Russell King   [ARM] netwinder: ...
87
88
89
  	spin_lock_irqsave(&nw_gpio_lock, flags);
  	nw_gpio_modify_op(GPIO_FAN, i ? GPIO_FAN : 0);
  	spin_unlock_irqrestore(&nw_gpio_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
92
93
94
95
  }
  
  static inline int netwinder_get_fan(void)
  {
  	if ((system_rev & 0xf000) == 0x4000)
  		return FAN_ALWAYS_ON;
70d13e083   Russell King   [ARM] netwinder: ...
96
  	return (nw_gpio_read() & GPIO_FAN) ? FAN_ON : FAN_OFF;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  }
  
  /*
   * End of NetWinder specifics
   */
  
  static void ds1620_send_bits(int nr, int value)
  {
  	int i;
  
  	for (i = 0; i < nr; i++) {
  		netwinder_ds1620_set_data(value & 1);
  		netwinder_ds1620_set_clk(0);
  		udelay(1);
  		netwinder_ds1620_set_clk(1);
  		udelay(1);
  
  		value >>= 1;
  	}
  }
  
  static unsigned int ds1620_recv_bits(int nr)
  {
  	unsigned int value = 0, mask = 1;
  	int i;
  
  	netwinder_ds1620_set_data(0);
  
  	for (i = 0; i < nr; i++) {
  		netwinder_ds1620_set_clk(0);
  		udelay(1);
  
  		if (netwinder_ds1620_get_data())
  			value |= mask;
  
  		mask <<= 1;
  
  		netwinder_ds1620_set_clk(1);
  		udelay(1);
  	}
  
  	return value;
  }
  
  static void ds1620_out(int cmd, int bits, int value)
  {
  	unsigned long flags;
  
  	netwinder_lock(&flags);
  	netwinder_ds1620_set_clk(1);
  	netwinder_ds1620_set_data_dir(0);
  	netwinder_ds1620_reset();
  
  	udelay(1);
  
  	ds1620_send_bits(8, cmd);
  	if (bits)
  		ds1620_send_bits(bits, value);
  
  	udelay(1);
  
  	netwinder_ds1620_reset();
  	netwinder_unlock(&flags);
d8eddb620   Domen Puncer   [PATCH] char/ds16...
160
  	msleep(20);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  }
  
  static unsigned int ds1620_in(int cmd, int bits)
  {
  	unsigned long flags;
  	unsigned int value;
  
  	netwinder_lock(&flags);
  	netwinder_ds1620_set_clk(1);
  	netwinder_ds1620_set_data_dir(0);
  	netwinder_ds1620_reset();
  
  	udelay(1);
  
  	ds1620_send_bits(8, cmd);
  
  	netwinder_ds1620_set_data_dir(1);
  	value = ds1620_recv_bits(bits);
  
  	netwinder_ds1620_reset();
  	netwinder_unlock(&flags);
  
  	return value;
  }
  
  static int cvt_9_to_int(unsigned int val)
  {
  	if (val & 0x100)
  		val |= 0xfffffe00;
  
  	return val;
  }
  
  static void ds1620_write_state(struct therm *therm)
  {
  	ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
  	ds1620_out(THERM_WRITE_TL, 9, therm->lo);
  	ds1620_out(THERM_WRITE_TH, 9, therm->hi);
  	ds1620_out(THERM_START_CONVERT, 0, 0);
  }
  
  static void ds1620_read_state(struct therm *therm)
  {
  	therm->lo = cvt_9_to_int(ds1620_in(THERM_READ_TL, 9));
  	therm->hi = cvt_9_to_int(ds1620_in(THERM_READ_TH, 9));
  }
080c22264   Arnd Bergmann   ds1620: BKL pushdown
207
208
  static int ds1620_open(struct inode *inode, struct file *file)
  {
080c22264   Arnd Bergmann   ds1620: BKL pushdown
209
210
  	return nonseekable_open(inode, file);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  static ssize_t
  ds1620_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
  {
  	signed int cur_temp;
  	signed char cur_temp_degF;
  
  	cur_temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)) >> 1;
  
  	/* convert to Fahrenheit, as per wdt.c */
  	cur_temp_degF = (cur_temp * 9) / 5 + 32;
  
  	if (copy_to_user(buf, &cur_temp_degF, 1))
  		return -EFAULT;
  
  	return 1;
  }
  
  static int
55929332c   Arnd Bergmann   drivers: Push dow...
229
  ds1620_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  {
  	struct therm therm;
  	union {
  		struct therm __user *therm;
  		int __user *i;
  	} uarg;
  	int i;
  
  	uarg.i = (int __user *)arg;
  
  	switch(cmd) {
  	case CMD_SET_THERMOSTATE:
  	case CMD_SET_THERMOSTATE2:
  		if (!capable(CAP_SYS_ADMIN))
  			return -EPERM;
  
  		if (cmd == CMD_SET_THERMOSTATE) {
  			if (get_user(therm.hi, uarg.i))
  				return -EFAULT;
  			therm.lo = therm.hi - 3;
  		} else {
  			if (copy_from_user(&therm, uarg.therm, sizeof(therm)))
  				return -EFAULT;
  		}
  
  		therm.lo <<= 1;
  		therm.hi <<= 1;
  
  		ds1620_write_state(&therm);
  		break;
  
  	case CMD_GET_THERMOSTATE:
  	case CMD_GET_THERMOSTATE2:
  		ds1620_read_state(&therm);
  
  		therm.lo >>= 1;
  		therm.hi >>= 1;
  
  		if (cmd == CMD_GET_THERMOSTATE) {
  			if (put_user(therm.hi, uarg.i))
  				return -EFAULT;
  		} else {
  			if (copy_to_user(uarg.therm, &therm, sizeof(therm)))
  				return -EFAULT;
  		}
  		break;
  
  	case CMD_GET_TEMPERATURE:
  	case CMD_GET_TEMPERATURE2:
  		i = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  
  		if (cmd == CMD_GET_TEMPERATURE)
  			i >>= 1;
  
  		return put_user(i, uarg.i) ? -EFAULT : 0;
  
  	case CMD_GET_STATUS:
  		i = ds1620_in(THERM_READ_CONFIG, 8) & 0xe3;
  
  		return put_user(i, uarg.i) ? -EFAULT : 0;
  
  	case CMD_GET_FAN:
  		i = netwinder_get_fan();
  
  		return put_user(i, uarg.i) ? -EFAULT : 0;
  
  	case CMD_SET_FAN:
  		if (!capable(CAP_SYS_ADMIN))
  			return -EPERM;
  
  		if (get_user(i, uarg.i))
  			return -EFAULT;
  
  		netwinder_set_fan(i);
  		break;
  		
  	default:
  		return -ENOIOCTLCMD;
  	}
  
  	return 0;
  }
55929332c   Arnd Bergmann   drivers: Push dow...
312
313
314
315
  static long
  ds1620_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  {
  	int ret;
613655fa3   Arnd Bergmann   drivers: autoconv...
316
  	mutex_lock(&ds1620_mutex);
55929332c   Arnd Bergmann   drivers: Push dow...
317
  	ret = ds1620_ioctl(file, cmd, arg);
613655fa3   Arnd Bergmann   drivers: autoconv...
318
  	mutex_unlock(&ds1620_mutex);
55929332c   Arnd Bergmann   drivers: Push dow...
319
320
321
  
  	return ret;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
  #ifdef THERM_USE_PROC
  static int
  proc_therm_ds1620_read(char *buf, char **start, off_t offset,
  		       int len, int *eof, void *unused)
  {
  	struct therm th;
  	int temp;
  
  	ds1620_read_state(&th);
  	temp =  cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  
  	len = sprintf(buf, "Thermostat: HI %i.%i, LOW %i.%i; "
  		      "temperature: %i.%i C, fan %s
  ",
  		      th.hi >> 1, th.hi & 1 ? 5 : 0,
  		      th.lo >> 1, th.lo & 1 ? 5 : 0,
  		      temp  >> 1, temp  & 1 ? 5 : 0,
  		      fan_state[netwinder_get_fan()]);
  
  	return len;
  }
  
  static struct proc_dir_entry *proc_therm_ds1620;
  #endif
62322d255   Arjan van de Ven   [PATCH] make more...
346
  static const struct file_operations ds1620_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
347
  	.owner		= THIS_MODULE,
080c22264   Arnd Bergmann   ds1620: BKL pushdown
348
  	.open		= ds1620_open,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
349
  	.read		= ds1620_read,
55929332c   Arnd Bergmann   drivers: Push dow...
350
  	.unlocked_ioctl	= ds1620_unlocked_ioctl,
6038f373a   Arnd Bergmann   llseek: automatic...
351
  	.llseek		= no_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  };
  
  static struct miscdevice ds1620_miscdev = {
  	TEMP_MINOR,
  	"temp",
  	&ds1620_fops
  };
  
  static int __init ds1620_init(void)
  {
  	int ret;
  	struct therm th, th_start;
  
  	if (!machine_is_netwinder())
  		return -ENODEV;
  
  	ds1620_out(THERM_RESET, 0, 0);
  	ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
  	ds1620_out(THERM_START_CONVERT, 0, 0);
  
  	/*
  	 * Trigger the fan to start by setting
  	 * temperature high point low.  This kicks
  	 * the fan into action.
  	 */
  	ds1620_read_state(&th);
  	th_start.lo = 0;
  	th_start.hi = 1;
  	ds1620_write_state(&th_start);
  
  	msleep(2000);
  
  	ds1620_write_state(&th);
  
  	ret = misc_register(&ds1620_miscdev);
  	if (ret < 0)
  		return ret;
  
  #ifdef THERM_USE_PROC
  	proc_therm_ds1620 = create_proc_entry("therm", 0, NULL);
  	if (proc_therm_ds1620)
  		proc_therm_ds1620->read_proc = proc_therm_ds1620_read;
  	else
  		printk(KERN_ERR "therm: unable to register /proc/therm
  ");
  #endif
  
  	ds1620_read_state(&th);
  	ret = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  
  	printk(KERN_INFO "Thermostat: high %i.%i, low %i.%i, "
  	       "current %i.%i C, fan %s.
  ",
  	       th.hi >> 1, th.hi & 1 ? 5 : 0,
  	       th.lo >> 1, th.lo & 1 ? 5 : 0,
  	       ret   >> 1, ret   & 1 ? 5 : 0,
  	       fan_state[netwinder_get_fan()]);
  
  	return 0;
  }
  
  static void __exit ds1620_exit(void)
  {
  #ifdef THERM_USE_PROC
  	remove_proc_entry("therm", NULL);
  #endif
  	misc_deregister(&ds1620_miscdev);
  }
  
  module_init(ds1620_init);
  module_exit(ds1620_exit);
  
  MODULE_LICENSE("GPL");