Linux-2.6.12-rc2
[linux-2.6.git] / drivers / char / ds1620.c
1 /*
2  * linux/drivers/char/ds1620.c: Dallas Semiconductors DS1620
3  *   thermometer driver (as used in the Rebel.com NetWinder)
4  */
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>
14
15 #include <asm/hardware.h>
16 #include <asm/mach-types.h>
17 #include <asm/uaccess.h>
18 #include <asm/therm.h>
19
20 #ifdef CONFIG_PROC_FS
21 /* define for /proc interface */
22 #define THERM_USE_PROC
23 #endif
24
25 /* Definitions for DS1620 chip */
26 #define THERM_START_CONVERT     0xee
27 #define THERM_RESET             0xaf
28 #define THERM_READ_CONFIG       0xac
29 #define THERM_READ_TEMP         0xaa
30 #define THERM_READ_TL           0xa2
31 #define THERM_READ_TH           0xa1
32 #define THERM_WRITE_CONFIG      0x0c
33 #define THERM_WRITE_TL          0x02
34 #define THERM_WRITE_TH          0x01
35
36 #define CFG_CPU                 2
37 #define CFG_1SHOT               1
38
39 static const char *fan_state[] = { "off", "on", "on (hardwired)" };
40
41 /*
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.
47  */
48 extern spinlock_t gpio_lock;
49 extern unsigned int system_rev;
50
51 static inline void netwinder_ds1620_set_clk(int clk)
52 {
53         gpio_modify_op(GPIO_DSCLK, clk ? GPIO_DSCLK : 0);
54 }
55
56 static inline void netwinder_ds1620_set_data(int dat)
57 {
58         gpio_modify_op(GPIO_DATA, dat ? GPIO_DATA : 0);
59 }
60
61 static inline int netwinder_ds1620_get_data(void)
62 {
63         return gpio_read() & GPIO_DATA;
64 }
65
66 static inline void netwinder_ds1620_set_data_dir(int dir)
67 {
68         gpio_modify_io(GPIO_DATA, dir ? GPIO_DATA : 0);
69 }
70
71 static inline void netwinder_ds1620_reset(void)
72 {
73         cpld_modify(CPLD_DS_ENABLE, 0);
74         cpld_modify(CPLD_DS_ENABLE, CPLD_DS_ENABLE);
75 }
76
77 static inline void netwinder_lock(unsigned long *flags)
78 {
79         spin_lock_irqsave(&gpio_lock, *flags);
80 }
81
82 static inline void netwinder_unlock(unsigned long *flags)
83 {
84         spin_unlock_irqrestore(&gpio_lock, *flags);
85 }
86
87 static inline void netwinder_set_fan(int i)
88 {
89         unsigned long flags;
90
91         spin_lock_irqsave(&gpio_lock, flags);
92         gpio_modify_op(GPIO_FAN, i ? GPIO_FAN : 0);
93         spin_unlock_irqrestore(&gpio_lock, flags);
94 }
95
96 static inline int netwinder_get_fan(void)
97 {
98         if ((system_rev & 0xf000) == 0x4000)
99                 return FAN_ALWAYS_ON;
100
101         return (gpio_read() & GPIO_FAN) ? FAN_ON : FAN_OFF;
102 }
103
104 /*
105  * End of NetWinder specifics
106  */
107
108 static void ds1620_send_bits(int nr, int value)
109 {
110         int i;
111
112         for (i = 0; i < nr; i++) {
113                 netwinder_ds1620_set_data(value & 1);
114                 netwinder_ds1620_set_clk(0);
115                 udelay(1);
116                 netwinder_ds1620_set_clk(1);
117                 udelay(1);
118
119                 value >>= 1;
120         }
121 }
122
123 static unsigned int ds1620_recv_bits(int nr)
124 {
125         unsigned int value = 0, mask = 1;
126         int i;
127
128         netwinder_ds1620_set_data(0);
129
130         for (i = 0; i < nr; i++) {
131                 netwinder_ds1620_set_clk(0);
132                 udelay(1);
133
134                 if (netwinder_ds1620_get_data())
135                         value |= mask;
136
137                 mask <<= 1;
138
139                 netwinder_ds1620_set_clk(1);
140                 udelay(1);
141         }
142
143         return value;
144 }
145
146 static void ds1620_out(int cmd, int bits, int value)
147 {
148         unsigned long flags;
149
150         netwinder_lock(&flags);
151         netwinder_ds1620_set_clk(1);
152         netwinder_ds1620_set_data_dir(0);
153         netwinder_ds1620_reset();
154
155         udelay(1);
156
157         ds1620_send_bits(8, cmd);
158         if (bits)
159                 ds1620_send_bits(bits, value);
160
161         udelay(1);
162
163         netwinder_ds1620_reset();
164         netwinder_unlock(&flags);
165
166         set_current_state(TASK_INTERRUPTIBLE);
167         schedule_timeout(2);
168 }
169
170 static unsigned int ds1620_in(int cmd, int bits)
171 {
172         unsigned long flags;
173         unsigned int value;
174
175         netwinder_lock(&flags);
176         netwinder_ds1620_set_clk(1);
177         netwinder_ds1620_set_data_dir(0);
178         netwinder_ds1620_reset();
179
180         udelay(1);
181
182         ds1620_send_bits(8, cmd);
183
184         netwinder_ds1620_set_data_dir(1);
185         value = ds1620_recv_bits(bits);
186
187         netwinder_ds1620_reset();
188         netwinder_unlock(&flags);
189
190         return value;
191 }
192
193 static int cvt_9_to_int(unsigned int val)
194 {
195         if (val & 0x100)
196                 val |= 0xfffffe00;
197
198         return val;
199 }
200
201 static void ds1620_write_state(struct therm *therm)
202 {
203         ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
204         ds1620_out(THERM_WRITE_TL, 9, therm->lo);
205         ds1620_out(THERM_WRITE_TH, 9, therm->hi);
206         ds1620_out(THERM_START_CONVERT, 0, 0);
207 }
208
209 static void ds1620_read_state(struct therm *therm)
210 {
211         therm->lo = cvt_9_to_int(ds1620_in(THERM_READ_TL, 9));
212         therm->hi = cvt_9_to_int(ds1620_in(THERM_READ_TH, 9));
213 }
214
215 static ssize_t
216 ds1620_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
217 {
218         signed int cur_temp;
219         signed char cur_temp_degF;
220
221         cur_temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)) >> 1;
222
223         /* convert to Fahrenheit, as per wdt.c */
224         cur_temp_degF = (cur_temp * 9) / 5 + 32;
225
226         if (copy_to_user(buf, &cur_temp_degF, 1))
227                 return -EFAULT;
228
229         return 1;
230 }
231
232 static int
233 ds1620_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
234 {
235         struct therm therm;
236         union {
237                 struct therm __user *therm;
238                 int __user *i;
239         } uarg;
240         int i;
241
242         uarg.i = (int __user *)arg;
243
244         switch(cmd) {
245         case CMD_SET_THERMOSTATE:
246         case CMD_SET_THERMOSTATE2:
247                 if (!capable(CAP_SYS_ADMIN))
248                         return -EPERM;
249
250                 if (cmd == CMD_SET_THERMOSTATE) {
251                         if (get_user(therm.hi, uarg.i))
252                                 return -EFAULT;
253                         therm.lo = therm.hi - 3;
254                 } else {
255                         if (copy_from_user(&therm, uarg.therm, sizeof(therm)))
256                                 return -EFAULT;
257                 }
258
259                 therm.lo <<= 1;
260                 therm.hi <<= 1;
261
262                 ds1620_write_state(&therm);
263                 break;
264
265         case CMD_GET_THERMOSTATE:
266         case CMD_GET_THERMOSTATE2:
267                 ds1620_read_state(&therm);
268
269                 therm.lo >>= 1;
270                 therm.hi >>= 1;
271
272                 if (cmd == CMD_GET_THERMOSTATE) {
273                         if (put_user(therm.hi, uarg.i))
274                                 return -EFAULT;
275                 } else {
276                         if (copy_to_user(uarg.therm, &therm, sizeof(therm)))
277                                 return -EFAULT;
278                 }
279                 break;
280
281         case CMD_GET_TEMPERATURE:
282         case CMD_GET_TEMPERATURE2:
283                 i = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
284
285                 if (cmd == CMD_GET_TEMPERATURE)
286                         i >>= 1;
287
288                 return put_user(i, uarg.i) ? -EFAULT : 0;
289
290         case CMD_GET_STATUS:
291                 i = ds1620_in(THERM_READ_CONFIG, 8) & 0xe3;
292
293                 return put_user(i, uarg.i) ? -EFAULT : 0;
294
295         case CMD_GET_FAN:
296                 i = netwinder_get_fan();
297
298                 return put_user(i, uarg.i) ? -EFAULT : 0;
299
300         case CMD_SET_FAN:
301                 if (!capable(CAP_SYS_ADMIN))
302                         return -EPERM;
303
304                 if (get_user(i, uarg.i))
305                         return -EFAULT;
306
307                 netwinder_set_fan(i);
308                 break;
309                 
310         default:
311                 return -ENOIOCTLCMD;
312         }
313
314         return 0;
315 }
316
317 #ifdef THERM_USE_PROC
318 static int
319 proc_therm_ds1620_read(char *buf, char **start, off_t offset,
320                        int len, int *eof, void *unused)
321 {
322         struct therm th;
323         int temp;
324
325         ds1620_read_state(&th);
326         temp =  cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
327
328         len = sprintf(buf, "Thermostat: HI %i.%i, LOW %i.%i; "
329                       "temperature: %i.%i C, fan %s\n",
330                       th.hi >> 1, th.hi & 1 ? 5 : 0,
331                       th.lo >> 1, th.lo & 1 ? 5 : 0,
332                       temp  >> 1, temp  & 1 ? 5 : 0,
333                       fan_state[netwinder_get_fan()]);
334
335         return len;
336 }
337
338 static struct proc_dir_entry *proc_therm_ds1620;
339 #endif
340
341 static struct file_operations ds1620_fops = {
342         .owner          = THIS_MODULE,
343         .open           = nonseekable_open,
344         .read           = ds1620_read,
345         .ioctl          = ds1620_ioctl,
346 };
347
348 static struct miscdevice ds1620_miscdev = {
349         TEMP_MINOR,
350         "temp",
351         &ds1620_fops
352 };
353
354 static int __init ds1620_init(void)
355 {
356         int ret;
357         struct therm th, th_start;
358
359         if (!machine_is_netwinder())
360                 return -ENODEV;
361
362         ds1620_out(THERM_RESET, 0, 0);
363         ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
364         ds1620_out(THERM_START_CONVERT, 0, 0);
365
366         /*
367          * Trigger the fan to start by setting
368          * temperature high point low.  This kicks
369          * the fan into action.
370          */
371         ds1620_read_state(&th);
372         th_start.lo = 0;
373         th_start.hi = 1;
374         ds1620_write_state(&th_start);
375
376         msleep(2000);
377
378         ds1620_write_state(&th);
379
380         ret = misc_register(&ds1620_miscdev);
381         if (ret < 0)
382                 return ret;
383
384 #ifdef THERM_USE_PROC
385         proc_therm_ds1620 = create_proc_entry("therm", 0, NULL);
386         if (proc_therm_ds1620)
387                 proc_therm_ds1620->read_proc = proc_therm_ds1620_read;
388         else
389                 printk(KERN_ERR "therm: unable to register /proc/therm\n");
390 #endif
391
392         ds1620_read_state(&th);
393         ret = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
394
395         printk(KERN_INFO "Thermostat: high %i.%i, low %i.%i, "
396                "current %i.%i C, fan %s.\n",
397                th.hi >> 1, th.hi & 1 ? 5 : 0,
398                th.lo >> 1, th.lo & 1 ? 5 : 0,
399                ret   >> 1, ret   & 1 ? 5 : 0,
400                fan_state[netwinder_get_fan()]);
401
402         return 0;
403 }
404
405 static void __exit ds1620_exit(void)
406 {
407 #ifdef THERM_USE_PROC
408         remove_proc_entry("therm", NULL);
409 #endif
410         misc_deregister(&ds1620_miscdev);
411 }
412
413 module_init(ds1620_init);
414 module_exit(ds1620_exit);
415
416 MODULE_LICENSE("GPL");