2 * linux/drivers/char/ds1620.c: Dallas Semiconductors DS1620
3 * thermometer driver (as used in the Rebel.com NetWinder)
5 #include <linux/module.h>
6 #include <linux/miscdevice.h>
7 #include <linux/delay.h>
8 #include <linux/proc_fs.h>
9 #include <linux/seq_file.h>
10 #include <linux/capability.h>
11 #include <linux/init.h>
12 #include <linux/mutex.h>
14 #include <mach/hardware.h>
15 #include <asm/mach-types.h>
16 #include <linux/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 DEFINE_MUTEX(ds1620_mutex
);
39 static const char *fan_state
[] = { "off", "on", "on (hardwired)" };
42 * Start of NetWinder specifics
43 * Note! We have to hold the gpio lock with IRQs disabled over the
44 * whole of our transaction to the Dallas chip, since there is a
45 * chance that the WaveArtist driver could touch these bits to
46 * enable or disable the speaker.
48 extern unsigned int system_rev
;
50 static inline void netwinder_ds1620_set_clk(int clk
)
52 nw_gpio_modify_op(GPIO_DSCLK
, clk
? GPIO_DSCLK
: 0);
55 static inline void netwinder_ds1620_set_data(int dat
)
57 nw_gpio_modify_op(GPIO_DATA
, dat
? GPIO_DATA
: 0);
60 static inline int netwinder_ds1620_get_data(void)
62 return nw_gpio_read() & GPIO_DATA
;
65 static inline void netwinder_ds1620_set_data_dir(int dir
)
67 nw_gpio_modify_io(GPIO_DATA
, dir
? GPIO_DATA
: 0);
70 static inline void netwinder_ds1620_reset(void)
72 nw_cpld_modify(CPLD_DS_ENABLE
, 0);
73 nw_cpld_modify(CPLD_DS_ENABLE
, CPLD_DS_ENABLE
);
76 static inline void netwinder_lock(unsigned long *flags
)
78 raw_spin_lock_irqsave(&nw_gpio_lock
, *flags
);
81 static inline void netwinder_unlock(unsigned long *flags
)
83 raw_spin_unlock_irqrestore(&nw_gpio_lock
, *flags
);
86 static inline void netwinder_set_fan(int i
)
90 raw_spin_lock_irqsave(&nw_gpio_lock
, flags
);
91 nw_gpio_modify_op(GPIO_FAN
, i
? GPIO_FAN
: 0);
92 raw_spin_unlock_irqrestore(&nw_gpio_lock
, flags
);
95 static inline int netwinder_get_fan(void)
97 if ((system_rev
& 0xf000) == 0x4000)
100 return (nw_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
);
168 static unsigned int ds1620_in(int cmd
, int bits
)
173 netwinder_lock(&flags
);
174 netwinder_ds1620_set_clk(1);
175 netwinder_ds1620_set_data_dir(0);
176 netwinder_ds1620_reset();
180 ds1620_send_bits(8, cmd
);
182 netwinder_ds1620_set_data_dir(1);
183 value
= ds1620_recv_bits(bits
);
185 netwinder_ds1620_reset();
186 netwinder_unlock(&flags
);
191 static int cvt_9_to_int(unsigned int val
)
199 static void ds1620_write_state(struct therm
*therm
)
201 ds1620_out(THERM_WRITE_CONFIG
, 8, CFG_CPU
);
202 ds1620_out(THERM_WRITE_TL
, 9, therm
->lo
);
203 ds1620_out(THERM_WRITE_TH
, 9, therm
->hi
);
204 ds1620_out(THERM_START_CONVERT
, 0, 0);
207 static void ds1620_read_state(struct therm
*therm
)
209 therm
->lo
= cvt_9_to_int(ds1620_in(THERM_READ_TL
, 9));
210 therm
->hi
= cvt_9_to_int(ds1620_in(THERM_READ_TH
, 9));
213 static int ds1620_open(struct inode
*inode
, struct file
*file
)
215 return nonseekable_open(inode
, file
);
219 ds1620_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ptr
)
222 signed char cur_temp_degF
;
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 file
*file
, unsigned int cmd
, unsigned long arg
)
240 struct therm __user
*therm
;
245 uarg
.i
= (int __user
*)arg
;
248 case CMD_SET_THERMOSTATE
:
249 case CMD_SET_THERMOSTATE2
:
250 if (!capable(CAP_SYS_ADMIN
))
253 if (cmd
== CMD_SET_THERMOSTATE
) {
254 if (get_user(therm
.hi
, uarg
.i
))
256 therm
.lo
= therm
.hi
- 3;
258 if (copy_from_user(&therm
, uarg
.therm
, sizeof(therm
)))
265 ds1620_write_state(&therm
);
268 case CMD_GET_THERMOSTATE
:
269 case CMD_GET_THERMOSTATE2
:
270 ds1620_read_state(&therm
);
275 if (cmd
== CMD_GET_THERMOSTATE
) {
276 if (put_user(therm
.hi
, uarg
.i
))
279 if (copy_to_user(uarg
.therm
, &therm
, sizeof(therm
)))
284 case CMD_GET_TEMPERATURE
:
285 case CMD_GET_TEMPERATURE2
:
286 i
= cvt_9_to_int(ds1620_in(THERM_READ_TEMP
, 9));
288 if (cmd
== CMD_GET_TEMPERATURE
)
291 return put_user(i
, uarg
.i
) ? -EFAULT
: 0;
294 i
= ds1620_in(THERM_READ_CONFIG
, 8) & 0xe3;
296 return put_user(i
, uarg
.i
) ? -EFAULT
: 0;
299 i
= netwinder_get_fan();
301 return put_user(i
, uarg
.i
) ? -EFAULT
: 0;
304 if (!capable(CAP_SYS_ADMIN
))
307 if (get_user(i
, uarg
.i
))
310 netwinder_set_fan(i
);
321 ds1620_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
325 mutex_lock(&ds1620_mutex
);
326 ret
= ds1620_ioctl(file
, cmd
, arg
);
327 mutex_unlock(&ds1620_mutex
);
332 #ifdef THERM_USE_PROC
333 static int ds1620_proc_therm_show(struct seq_file
*m
, void *v
)
338 ds1620_read_state(&th
);
339 temp
= cvt_9_to_int(ds1620_in(THERM_READ_TEMP
, 9));
341 seq_printf(m
, "Thermostat: HI %i.%i, LOW %i.%i; temperature: %i.%i C, fan %s\n",
342 th
.hi
>> 1, th
.hi
& 1 ? 5 : 0,
343 th
.lo
>> 1, th
.lo
& 1 ? 5 : 0,
344 temp
>> 1, temp
& 1 ? 5 : 0,
345 fan_state
[netwinder_get_fan()]);
349 static int ds1620_proc_therm_open(struct inode
*inode
, struct file
*file
)
351 return single_open(file
, ds1620_proc_therm_show
, NULL
);
354 static const struct file_operations ds1620_proc_therm_fops
= {
355 .open
= ds1620_proc_therm_open
,
358 .release
= single_release
,
362 static const struct file_operations ds1620_fops
= {
363 .owner
= THIS_MODULE
,
366 .unlocked_ioctl
= ds1620_unlocked_ioctl
,
370 static struct miscdevice ds1620_miscdev
= {
376 static int __init
ds1620_init(void)
379 struct therm th
, th_start
;
381 if (!machine_is_netwinder())
384 ds1620_out(THERM_RESET
, 0, 0);
385 ds1620_out(THERM_WRITE_CONFIG
, 8, CFG_CPU
);
386 ds1620_out(THERM_START_CONVERT
, 0, 0);
389 * Trigger the fan to start by setting
390 * temperature high point low. This kicks
391 * the fan into action.
393 ds1620_read_state(&th
);
396 ds1620_write_state(&th_start
);
400 ds1620_write_state(&th
);
402 ret
= misc_register(&ds1620_miscdev
);
406 #ifdef THERM_USE_PROC
407 if (!proc_create("therm", 0, NULL
, &ds1620_proc_therm_fops
))
408 printk(KERN_ERR
"therm: unable to register /proc/therm\n");
411 ds1620_read_state(&th
);
412 ret
= cvt_9_to_int(ds1620_in(THERM_READ_TEMP
, 9));
414 printk(KERN_INFO
"Thermostat: high %i.%i, low %i.%i, "
415 "current %i.%i C, fan %s.\n",
416 th
.hi
>> 1, th
.hi
& 1 ? 5 : 0,
417 th
.lo
>> 1, th
.lo
& 1 ? 5 : 0,
418 ret
>> 1, ret
& 1 ? 5 : 0,
419 fan_state
[netwinder_get_fan()]);
424 static void __exit
ds1620_exit(void)
426 #ifdef THERM_USE_PROC
427 remove_proc_entry("therm", NULL
);
429 misc_deregister(&ds1620_miscdev
);
432 module_init(ds1620_init
);
433 module_exit(ds1620_exit
);
435 MODULE_LICENSE("GPL");