slob: fix page order calculation on not 4KB page
[linux-2.6/x86.git] / drivers / char / ds1620.c
blob3d7efc26aad61338818a1416ff4d344ad4227a76
1 /*
2 * linux/drivers/char/ds1620.c: Dallas Semiconductors DS1620
3 * thermometer driver (as used in the Rebel.com NetWinder)
4 */
5 #include <linux/module.h>
6 #include <linux/miscdevice.h>
7 #include <linux/smp_lock.h>
8 #include <linux/delay.h>
9 #include <linux/proc_fs.h>
10 #include <linux/capability.h>
11 #include <linux/init.h>
13 #include <asm/hardware.h>
14 #include <asm/mach-types.h>
15 #include <asm/uaccess.h>
16 #include <asm/therm.h>
18 #ifdef CONFIG_PROC_FS
19 /* define for /proc interface */
20 #define THERM_USE_PROC
21 #endif
23 /* Definitions for DS1620 chip */
24 #define THERM_START_CONVERT 0xee
25 #define THERM_RESET 0xaf
26 #define THERM_READ_CONFIG 0xac
27 #define THERM_READ_TEMP 0xaa
28 #define THERM_READ_TL 0xa2
29 #define THERM_READ_TH 0xa1
30 #define THERM_WRITE_CONFIG 0x0c
31 #define THERM_WRITE_TL 0x02
32 #define THERM_WRITE_TH 0x01
34 #define CFG_CPU 2
35 #define CFG_1SHOT 1
37 static const char *fan_state[] = { "off", "on", "on (hardwired)" };
40 * Start of NetWinder specifics
41 * Note! We have to hold the gpio lock with IRQs disabled over the
42 * whole of our transaction to the Dallas chip, since there is a
43 * chance that the WaveArtist driver could touch these bits to
44 * enable or disable the speaker.
46 extern spinlock_t gpio_lock;
47 extern unsigned int system_rev;
49 static inline void netwinder_ds1620_set_clk(int clk)
51 gpio_modify_op(GPIO_DSCLK, clk ? GPIO_DSCLK : 0);
54 static inline void netwinder_ds1620_set_data(int dat)
56 gpio_modify_op(GPIO_DATA, dat ? GPIO_DATA : 0);
59 static inline int netwinder_ds1620_get_data(void)
61 return gpio_read() & GPIO_DATA;
64 static inline void netwinder_ds1620_set_data_dir(int dir)
66 gpio_modify_io(GPIO_DATA, dir ? GPIO_DATA : 0);
69 static inline void netwinder_ds1620_reset(void)
71 cpld_modify(CPLD_DS_ENABLE, 0);
72 cpld_modify(CPLD_DS_ENABLE, CPLD_DS_ENABLE);
75 static inline void netwinder_lock(unsigned long *flags)
77 spin_lock_irqsave(&gpio_lock, *flags);
80 static inline void netwinder_unlock(unsigned long *flags)
82 spin_unlock_irqrestore(&gpio_lock, *flags);
85 static inline void netwinder_set_fan(int i)
87 unsigned long flags;
89 spin_lock_irqsave(&gpio_lock, flags);
90 gpio_modify_op(GPIO_FAN, i ? GPIO_FAN : 0);
91 spin_unlock_irqrestore(&gpio_lock, flags);
94 static inline int netwinder_get_fan(void)
96 if ((system_rev & 0xf000) == 0x4000)
97 return FAN_ALWAYS_ON;
99 return (gpio_read() & GPIO_FAN) ? FAN_ON : FAN_OFF;
103 * End of NetWinder specifics
106 static void ds1620_send_bits(int nr, int value)
108 int i;
110 for (i = 0; i < nr; i++) {
111 netwinder_ds1620_set_data(value & 1);
112 netwinder_ds1620_set_clk(0);
113 udelay(1);
114 netwinder_ds1620_set_clk(1);
115 udelay(1);
117 value >>= 1;
121 static unsigned int ds1620_recv_bits(int nr)
123 unsigned int value = 0, mask = 1;
124 int i;
126 netwinder_ds1620_set_data(0);
128 for (i = 0; i < nr; i++) {
129 netwinder_ds1620_set_clk(0);
130 udelay(1);
132 if (netwinder_ds1620_get_data())
133 value |= mask;
135 mask <<= 1;
137 netwinder_ds1620_set_clk(1);
138 udelay(1);
141 return value;
144 static void ds1620_out(int cmd, int bits, int value)
146 unsigned long flags;
148 netwinder_lock(&flags);
149 netwinder_ds1620_set_clk(1);
150 netwinder_ds1620_set_data_dir(0);
151 netwinder_ds1620_reset();
153 udelay(1);
155 ds1620_send_bits(8, cmd);
156 if (bits)
157 ds1620_send_bits(bits, value);
159 udelay(1);
161 netwinder_ds1620_reset();
162 netwinder_unlock(&flags);
164 msleep(20);
167 static unsigned int ds1620_in(int cmd, int bits)
169 unsigned long flags;
170 unsigned int value;
172 netwinder_lock(&flags);
173 netwinder_ds1620_set_clk(1);
174 netwinder_ds1620_set_data_dir(0);
175 netwinder_ds1620_reset();
177 udelay(1);
179 ds1620_send_bits(8, cmd);
181 netwinder_ds1620_set_data_dir(1);
182 value = ds1620_recv_bits(bits);
184 netwinder_ds1620_reset();
185 netwinder_unlock(&flags);
187 return value;
190 static int cvt_9_to_int(unsigned int val)
192 if (val & 0x100)
193 val |= 0xfffffe00;
195 return val;
198 static void ds1620_write_state(struct therm *therm)
200 ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
201 ds1620_out(THERM_WRITE_TL, 9, therm->lo);
202 ds1620_out(THERM_WRITE_TH, 9, therm->hi);
203 ds1620_out(THERM_START_CONVERT, 0, 0);
206 static void ds1620_read_state(struct therm *therm)
208 therm->lo = cvt_9_to_int(ds1620_in(THERM_READ_TL, 9));
209 therm->hi = cvt_9_to_int(ds1620_in(THERM_READ_TH, 9));
212 static ssize_t
213 ds1620_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
215 signed int cur_temp;
216 signed char cur_temp_degF;
218 cur_temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)) >> 1;
220 /* convert to Fahrenheit, as per wdt.c */
221 cur_temp_degF = (cur_temp * 9) / 5 + 32;
223 if (copy_to_user(buf, &cur_temp_degF, 1))
224 return -EFAULT;
226 return 1;
229 static int
230 ds1620_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
232 struct therm therm;
233 union {
234 struct therm __user *therm;
235 int __user *i;
236 } uarg;
237 int i;
239 uarg.i = (int __user *)arg;
241 switch(cmd) {
242 case CMD_SET_THERMOSTATE:
243 case CMD_SET_THERMOSTATE2:
244 if (!capable(CAP_SYS_ADMIN))
245 return -EPERM;
247 if (cmd == CMD_SET_THERMOSTATE) {
248 if (get_user(therm.hi, uarg.i))
249 return -EFAULT;
250 therm.lo = therm.hi - 3;
251 } else {
252 if (copy_from_user(&therm, uarg.therm, sizeof(therm)))
253 return -EFAULT;
256 therm.lo <<= 1;
257 therm.hi <<= 1;
259 ds1620_write_state(&therm);
260 break;
262 case CMD_GET_THERMOSTATE:
263 case CMD_GET_THERMOSTATE2:
264 ds1620_read_state(&therm);
266 therm.lo >>= 1;
267 therm.hi >>= 1;
269 if (cmd == CMD_GET_THERMOSTATE) {
270 if (put_user(therm.hi, uarg.i))
271 return -EFAULT;
272 } else {
273 if (copy_to_user(uarg.therm, &therm, sizeof(therm)))
274 return -EFAULT;
276 break;
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)
283 i >>= 1;
285 return put_user(i, uarg.i) ? -EFAULT : 0;
287 case CMD_GET_STATUS:
288 i = ds1620_in(THERM_READ_CONFIG, 8) & 0xe3;
290 return put_user(i, uarg.i) ? -EFAULT : 0;
292 case CMD_GET_FAN:
293 i = netwinder_get_fan();
295 return put_user(i, uarg.i) ? -EFAULT : 0;
297 case CMD_SET_FAN:
298 if (!capable(CAP_SYS_ADMIN))
299 return -EPERM;
301 if (get_user(i, uarg.i))
302 return -EFAULT;
304 netwinder_set_fan(i);
305 break;
307 default:
308 return -ENOIOCTLCMD;
311 return 0;
314 #ifdef THERM_USE_PROC
315 static int
316 proc_therm_ds1620_read(char *buf, char **start, off_t offset,
317 int len, int *eof, void *unused)
319 struct therm th;
320 int temp;
322 ds1620_read_state(&th);
323 temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
325 len = sprintf(buf, "Thermostat: HI %i.%i, LOW %i.%i; "
326 "temperature: %i.%i C, fan %s\n",
327 th.hi >> 1, th.hi & 1 ? 5 : 0,
328 th.lo >> 1, th.lo & 1 ? 5 : 0,
329 temp >> 1, temp & 1 ? 5 : 0,
330 fan_state[netwinder_get_fan()]);
332 return len;
335 static struct proc_dir_entry *proc_therm_ds1620;
336 #endif
338 static const struct file_operations ds1620_fops = {
339 .owner = THIS_MODULE,
340 .open = nonseekable_open,
341 .read = ds1620_read,
342 .ioctl = ds1620_ioctl,
345 static struct miscdevice ds1620_miscdev = {
346 TEMP_MINOR,
347 "temp",
348 &ds1620_fops
351 static int __init ds1620_init(void)
353 int ret;
354 struct therm th, th_start;
356 if (!machine_is_netwinder())
357 return -ENODEV;
359 ds1620_out(THERM_RESET, 0, 0);
360 ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
361 ds1620_out(THERM_START_CONVERT, 0, 0);
364 * Trigger the fan to start by setting
365 * temperature high point low. This kicks
366 * the fan into action.
368 ds1620_read_state(&th);
369 th_start.lo = 0;
370 th_start.hi = 1;
371 ds1620_write_state(&th_start);
373 msleep(2000);
375 ds1620_write_state(&th);
377 ret = misc_register(&ds1620_miscdev);
378 if (ret < 0)
379 return ret;
381 #ifdef THERM_USE_PROC
382 proc_therm_ds1620 = create_proc_entry("therm", 0, NULL);
383 if (proc_therm_ds1620)
384 proc_therm_ds1620->read_proc = proc_therm_ds1620_read;
385 else
386 printk(KERN_ERR "therm: unable to register /proc/therm\n");
387 #endif
389 ds1620_read_state(&th);
390 ret = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
392 printk(KERN_INFO "Thermostat: high %i.%i, low %i.%i, "
393 "current %i.%i C, fan %s.\n",
394 th.hi >> 1, th.hi & 1 ? 5 : 0,
395 th.lo >> 1, th.lo & 1 ? 5 : 0,
396 ret >> 1, ret & 1 ? 5 : 0,
397 fan_state[netwinder_get_fan()]);
399 return 0;
402 static void __exit ds1620_exit(void)
404 #ifdef THERM_USE_PROC
405 remove_proc_entry("therm", NULL);
406 #endif
407 misc_deregister(&ds1620_miscdev);
410 module_init(ds1620_init);
411 module_exit(ds1620_exit);
413 MODULE_LICENSE("GPL");