2 * linux/drivers/char/ds1620.c: Dallas Semiconductors DS1620
3 * thermometer driver (as used in the Rebel.com NetWinder)
5 #include <linux/config.h>
6 #include <linux/module.h>
7 #include <linux/sched.h>
8 #include <linux/miscdevice.h>
9 #include <linux/smp_lock.h>
10 #include <linux/delay.h>
11 #include <linux/proc_fs.h>
12 #include <linux/capability.h>
13 #include <linux/init.h>
15 #include <asm/hardware.h>
16 #include <asm/uaccess.h>
17 #include <asm/therm.h>
20 /* define for /proc interface */
21 #define THERM_USE_PROC
24 /* Definitions for DS1620 chip */
25 #define THERM_START_CONVERT 0xee
26 #define THERM_RESET 0xaf
27 #define THERM_READ_CONFIG 0xac
28 #define THERM_READ_TEMP 0xaa
29 #define THERM_READ_TL 0xa2
30 #define THERM_READ_TH 0xa1
31 #define THERM_WRITE_CONFIG 0x0c
32 #define THERM_WRITE_TL 0x02
33 #define THERM_WRITE_TH 0x01
38 static const char *fan_state
[] = { "off", "on", "on (hardwired)" };
41 * Start of NetWinder specifics
42 * Note! We have to hold the gpio lock with IRQs disabled over the
43 * whole of our transaction to the Dallas chip, since there is a
44 * chance that the WaveArtist driver could touch these bits to
45 * enable or disable the speaker.
47 extern spinlock_t gpio_lock
;
48 extern unsigned int system_rev
;
50 static inline void netwinder_ds1620_set_clk(int clk
)
52 gpio_modify_op(GPIO_DSCLK
, clk
? GPIO_DSCLK
: 0);
55 static inline void netwinder_ds1620_set_data(int dat
)
57 gpio_modify_op(GPIO_DATA
, dat
? GPIO_DATA
: 0);
60 static inline int netwinder_ds1620_get_data(void)
62 return gpio_read() & GPIO_DATA
;
65 static inline void netwinder_ds1620_set_data_dir(int dir
)
67 gpio_modify_io(GPIO_DATA
, dir
? GPIO_DATA
: 0);
70 static inline void netwinder_ds1620_reset(void)
72 cpld_modify(CPLD_DS_ENABLE
, 0);
73 cpld_modify(CPLD_DS_ENABLE
, CPLD_DS_ENABLE
);
76 static inline void netwinder_lock(unsigned long *flags
)
78 spin_lock_irqsave(&gpio_lock
, *flags
);
81 static inline void netwinder_unlock(unsigned long *flags
)
83 spin_unlock_irqrestore(&gpio_lock
, *flags
);
86 static inline void netwinder_set_fan(int i
)
90 spin_lock_irqsave(&gpio_lock
, flags
);
91 gpio_modify_op(GPIO_FAN
, i
? GPIO_FAN
: 0);
92 spin_unlock_irqrestore(&gpio_lock
, flags
);
95 static inline int netwinder_get_fan(void)
97 if ((system_rev
& 0xf000) == 0x4000)
100 return (gpio_read() & GPIO_FAN
) ? FAN_ON
: FAN_OFF
;
104 * End of NetWinder specifics
107 static void ds1620_send_bits(int nr
, int value
)
111 for (i
= 0; i
< nr
; i
++) {
112 netwinder_ds1620_set_data(value
& 1);
113 netwinder_ds1620_set_clk(0);
115 netwinder_ds1620_set_clk(1);
122 static unsigned int ds1620_recv_bits(int nr
)
124 unsigned int value
= 0, mask
= 1;
127 netwinder_ds1620_set_data(0);
129 for (i
= 0; i
< nr
; i
++) {
130 netwinder_ds1620_set_clk(0);
133 if (netwinder_ds1620_get_data())
138 netwinder_ds1620_set_clk(1);
145 static void ds1620_out(int cmd
, int bits
, int value
)
149 netwinder_lock(&flags
);
150 netwinder_ds1620_set_clk(1);
151 netwinder_ds1620_set_data_dir(0);
152 netwinder_ds1620_reset();
156 ds1620_send_bits(8, cmd
);
158 ds1620_send_bits(bits
, value
);
162 netwinder_ds1620_reset();
163 netwinder_unlock(&flags
);
165 set_current_state(TASK_INTERRUPTIBLE
);
169 static unsigned int ds1620_in(int cmd
, int bits
)
174 netwinder_lock(&flags
);
175 netwinder_ds1620_set_clk(1);
176 netwinder_ds1620_set_data_dir(0);
177 netwinder_ds1620_reset();
181 ds1620_send_bits(8, cmd
);
183 netwinder_ds1620_set_data_dir(1);
184 value
= ds1620_recv_bits(bits
);
186 netwinder_ds1620_reset();
187 netwinder_unlock(&flags
);
192 static int cvt_9_to_int(unsigned int val
)
200 static void ds1620_write_state(struct therm
*therm
)
202 ds1620_out(THERM_WRITE_CONFIG
, 8, CFG_CPU
);
203 ds1620_out(THERM_WRITE_TL
, 9, therm
->lo
);
204 ds1620_out(THERM_WRITE_TH
, 9, therm
->hi
);
205 ds1620_out(THERM_START_CONVERT
, 0, 0);
208 static void ds1620_read_state(struct therm
*therm
)
210 therm
->lo
= cvt_9_to_int(ds1620_in(THERM_READ_TL
, 9));
211 therm
->hi
= cvt_9_to_int(ds1620_in(THERM_READ_TH
, 9));
215 ds1620_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ptr
)
218 signed char cur_temp_degF
;
220 /* Can't seek (pread) on this device */
221 if (ptr
!= &file
->f_pos
)
224 cur_temp
= cvt_9_to_int(ds1620_in(THERM_READ_TEMP
, 9)) >> 1;
226 /* convert to Fahrenheit, as per wdt.c */
227 cur_temp_degF
= (cur_temp
* 9) / 5 + 32;
229 if (copy_to_user(buf
, &cur_temp_degF
, 1))
236 ds1620_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
242 case CMD_SET_THERMOSTATE
:
243 case CMD_SET_THERMOSTATE2
:
244 if (!capable(CAP_SYS_ADMIN
))
247 if (cmd
== CMD_SET_THERMOSTATE
) {
248 if (get_user(therm
.hi
, (int *)arg
))
250 therm
.lo
= therm
.hi
- 3;
252 if (copy_from_user(&therm
, (void *)arg
, sizeof(therm
)))
259 ds1620_write_state(&therm
);
262 case CMD_GET_THERMOSTATE
:
263 case CMD_GET_THERMOSTATE2
:
264 ds1620_read_state(&therm
);
269 if (cmd
== CMD_GET_THERMOSTATE
) {
270 if (put_user(therm
.hi
, (int *)arg
))
273 if (copy_to_user((void *)arg
, &therm
, sizeof(therm
)))
278 case CMD_GET_TEMPERATURE
:
279 case CMD_GET_TEMPERATURE2
:
280 i
= cvt_9_to_int(ds1620_in(THERM_READ_TEMP
, 9));
282 if (cmd
== CMD_GET_TEMPERATURE
)
285 return put_user(i
, (int *)arg
) ? -EFAULT
: 0;
288 i
= ds1620_in(THERM_READ_CONFIG
, 8) & 0xe3;
290 return put_user(i
, (int *)arg
) ? -EFAULT
: 0;
293 i
= netwinder_get_fan();
295 return put_user(i
, (int *)arg
) ? -EFAULT
: 0;
298 if (!capable(CAP_SYS_ADMIN
))
301 if (get_user(i
, (int *)arg
))
304 netwinder_set_fan(i
);
315 ds1620_open(struct inode
*inode
, struct file
*file
)
323 ds1620_release(struct inode
*inode
, struct file
*file
)
330 #ifdef THERM_USE_PROC
332 proc_therm_ds1620_read(char *buf
, char **start
, off_t offset
,
333 int len
, int *eof
, void *unused
)
338 ds1620_read_state(&th
);
339 temp
= cvt_9_to_int(ds1620_in(THERM_READ_TEMP
, 9));
341 len
= sprintf(buf
, "Thermostat: HI %i.%i, LOW %i.%i; "
342 "temperature: %i.%i C, fan %s\n",
343 th
.hi
>> 1, th
.hi
& 1 ? 5 : 0,
344 th
.lo
>> 1, th
.lo
& 1 ? 5 : 0,
345 temp
>> 1, temp
& 1 ? 5 : 0,
346 fan_state
[netwinder_get_fan()]);
351 static struct proc_dir_entry
*proc_therm_ds1620
;
354 static struct file_operations ds1620_fops
= {
358 release
: ds1620_release
,
361 static struct miscdevice ds1620_miscdev
= {
367 int __init
ds1620_init(void)
370 struct therm th
, th_start
;
372 if (!machine_is_netwinder())
375 ds1620_out(THERM_RESET
, 0, 0);
376 ds1620_out(THERM_WRITE_CONFIG
, 8, CFG_CPU
);
377 ds1620_out(THERM_START_CONVERT
, 0, 0);
380 * Trigger the fan to start by setting
381 * temperature high point low. This kicks
382 * the fan into action.
384 ds1620_read_state(&th
);
387 ds1620_write_state(&th_start
);
389 set_current_state(TASK_INTERRUPTIBLE
);
390 schedule_timeout(2*HZ
);
392 ds1620_write_state(&th
);
394 ret
= misc_register(&ds1620_miscdev
);
398 #ifdef THERM_USE_PROC
399 proc_therm_ds1620
= create_proc_entry("therm", 0, 0);
400 if (proc_therm_ds1620
)
401 proc_therm_ds1620
->read_proc
= proc_therm_ds1620_read
;
403 printk(KERN_ERR
"therm: unable to register /proc/therm\n");
406 ds1620_read_state(&th
);
407 ret
= cvt_9_to_int(ds1620_in(THERM_READ_TEMP
, 9));
409 printk(KERN_INFO
"Thermostat: high %i.%i, low %i.%i, "
410 "current %i.%i C, fan %s.\n",
411 th
.hi
>> 1, th
.hi
& 1 ? 5 : 0,
412 th
.lo
>> 1, th
.lo
& 1 ? 5 : 0,
413 ret
>> 1, ret
& 1 ? 5 : 0,
414 fan_state
[netwinder_get_fan()]);
419 void __exit
ds1620_exit(void)
421 #ifdef THERM_USE_PROC
422 remove_proc_entry("therm", NULL
);
424 misc_deregister(&ds1620_miscdev
);
427 module_init(ds1620_init
);
428 module_exit(ds1620_exit
);