ia64/linux-2.6.18-xen.hg

view drivers/char/ds1620.c @ 893:f994bfe9b93b

linux/blktap2: reduce TLB flush scope

c/s 885 added very coarse TLB flushing. Since these flushes always
follow single page updates, single page flushes (when available) are
sufficient.

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jun 04 10:32:57 2009 +0100 (2009-06-04)
parents 831230e53067
children
line source
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/sched.h>
7 #include <linux/miscdevice.h>
8 #include <linux/smp_lock.h>
9 #include <linux/delay.h>
10 #include <linux/proc_fs.h>
11 #include <linux/capability.h>
12 #include <linux/init.h>
14 #include <asm/hardware.h>
15 #include <asm/mach-types.h>
16 #include <asm/uaccess.h>
17 #include <asm/therm.h>
19 #ifdef CONFIG_PROC_FS
20 /* define for /proc interface */
21 #define THERM_USE_PROC
22 #endif
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
35 #define CFG_CPU 2
36 #define CFG_1SHOT 1
38 static const char *fan_state[] = { "off", "on", "on (hardwired)" };
40 /*
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.
46 */
47 extern spinlock_t gpio_lock;
48 extern unsigned int system_rev;
50 static inline void netwinder_ds1620_set_clk(int clk)
51 {
52 gpio_modify_op(GPIO_DSCLK, clk ? GPIO_DSCLK : 0);
53 }
55 static inline void netwinder_ds1620_set_data(int dat)
56 {
57 gpio_modify_op(GPIO_DATA, dat ? GPIO_DATA : 0);
58 }
60 static inline int netwinder_ds1620_get_data(void)
61 {
62 return gpio_read() & GPIO_DATA;
63 }
65 static inline void netwinder_ds1620_set_data_dir(int dir)
66 {
67 gpio_modify_io(GPIO_DATA, dir ? GPIO_DATA : 0);
68 }
70 static inline void netwinder_ds1620_reset(void)
71 {
72 cpld_modify(CPLD_DS_ENABLE, 0);
73 cpld_modify(CPLD_DS_ENABLE, CPLD_DS_ENABLE);
74 }
76 static inline void netwinder_lock(unsigned long *flags)
77 {
78 spin_lock_irqsave(&gpio_lock, *flags);
79 }
81 static inline void netwinder_unlock(unsigned long *flags)
82 {
83 spin_unlock_irqrestore(&gpio_lock, *flags);
84 }
86 static inline void netwinder_set_fan(int i)
87 {
88 unsigned long flags;
90 spin_lock_irqsave(&gpio_lock, flags);
91 gpio_modify_op(GPIO_FAN, i ? GPIO_FAN : 0);
92 spin_unlock_irqrestore(&gpio_lock, flags);
93 }
95 static inline int netwinder_get_fan(void)
96 {
97 if ((system_rev & 0xf000) == 0x4000)
98 return FAN_ALWAYS_ON;
100 return (gpio_read() & GPIO_FAN) ? FAN_ON : FAN_OFF;
101 }
103 /*
104 * End of NetWinder specifics
105 */
107 static void ds1620_send_bits(int nr, int value)
108 {
109 int i;
111 for (i = 0; i < nr; i++) {
112 netwinder_ds1620_set_data(value & 1);
113 netwinder_ds1620_set_clk(0);
114 udelay(1);
115 netwinder_ds1620_set_clk(1);
116 udelay(1);
118 value >>= 1;
119 }
120 }
122 static unsigned int ds1620_recv_bits(int nr)
123 {
124 unsigned int value = 0, mask = 1;
125 int i;
127 netwinder_ds1620_set_data(0);
129 for (i = 0; i < nr; i++) {
130 netwinder_ds1620_set_clk(0);
131 udelay(1);
133 if (netwinder_ds1620_get_data())
134 value |= mask;
136 mask <<= 1;
138 netwinder_ds1620_set_clk(1);
139 udelay(1);
140 }
142 return value;
143 }
145 static void ds1620_out(int cmd, int bits, int value)
146 {
147 unsigned long flags;
149 netwinder_lock(&flags);
150 netwinder_ds1620_set_clk(1);
151 netwinder_ds1620_set_data_dir(0);
152 netwinder_ds1620_reset();
154 udelay(1);
156 ds1620_send_bits(8, cmd);
157 if (bits)
158 ds1620_send_bits(bits, value);
160 udelay(1);
162 netwinder_ds1620_reset();
163 netwinder_unlock(&flags);
165 msleep(20);
166 }
168 static unsigned int ds1620_in(int cmd, int bits)
169 {
170 unsigned long flags;
171 unsigned int value;
173 netwinder_lock(&flags);
174 netwinder_ds1620_set_clk(1);
175 netwinder_ds1620_set_data_dir(0);
176 netwinder_ds1620_reset();
178 udelay(1);
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);
188 return value;
189 }
191 static int cvt_9_to_int(unsigned int val)
192 {
193 if (val & 0x100)
194 val |= 0xfffffe00;
196 return val;
197 }
199 static void ds1620_write_state(struct therm *therm)
200 {
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);
205 }
207 static void ds1620_read_state(struct therm *therm)
208 {
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));
211 }
213 static ssize_t
214 ds1620_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
215 {
216 signed int cur_temp;
217 signed char cur_temp_degF;
219 cur_temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)) >> 1;
221 /* convert to Fahrenheit, as per wdt.c */
222 cur_temp_degF = (cur_temp * 9) / 5 + 32;
224 if (copy_to_user(buf, &cur_temp_degF, 1))
225 return -EFAULT;
227 return 1;
228 }
230 static int
231 ds1620_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
232 {
233 struct therm therm;
234 union {
235 struct therm __user *therm;
236 int __user *i;
237 } uarg;
238 int i;
240 uarg.i = (int __user *)arg;
242 switch(cmd) {
243 case CMD_SET_THERMOSTATE:
244 case CMD_SET_THERMOSTATE2:
245 if (!capable(CAP_SYS_ADMIN))
246 return -EPERM;
248 if (cmd == CMD_SET_THERMOSTATE) {
249 if (get_user(therm.hi, uarg.i))
250 return -EFAULT;
251 therm.lo = therm.hi - 3;
252 } else {
253 if (copy_from_user(&therm, uarg.therm, sizeof(therm)))
254 return -EFAULT;
255 }
257 therm.lo <<= 1;
258 therm.hi <<= 1;
260 ds1620_write_state(&therm);
261 break;
263 case CMD_GET_THERMOSTATE:
264 case CMD_GET_THERMOSTATE2:
265 ds1620_read_state(&therm);
267 therm.lo >>= 1;
268 therm.hi >>= 1;
270 if (cmd == CMD_GET_THERMOSTATE) {
271 if (put_user(therm.hi, uarg.i))
272 return -EFAULT;
273 } else {
274 if (copy_to_user(uarg.therm, &therm, sizeof(therm)))
275 return -EFAULT;
276 }
277 break;
279 case CMD_GET_TEMPERATURE:
280 case CMD_GET_TEMPERATURE2:
281 i = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
283 if (cmd == CMD_GET_TEMPERATURE)
284 i >>= 1;
286 return put_user(i, uarg.i) ? -EFAULT : 0;
288 case CMD_GET_STATUS:
289 i = ds1620_in(THERM_READ_CONFIG, 8) & 0xe3;
291 return put_user(i, uarg.i) ? -EFAULT : 0;
293 case CMD_GET_FAN:
294 i = netwinder_get_fan();
296 return put_user(i, uarg.i) ? -EFAULT : 0;
298 case CMD_SET_FAN:
299 if (!capable(CAP_SYS_ADMIN))
300 return -EPERM;
302 if (get_user(i, uarg.i))
303 return -EFAULT;
305 netwinder_set_fan(i);
306 break;
308 default:
309 return -ENOIOCTLCMD;
310 }
312 return 0;
313 }
315 #ifdef THERM_USE_PROC
316 static int
317 proc_therm_ds1620_read(char *buf, char **start, off_t offset,
318 int len, int *eof, void *unused)
319 {
320 struct therm th;
321 int temp;
323 ds1620_read_state(&th);
324 temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
326 len = sprintf(buf, "Thermostat: HI %i.%i, LOW %i.%i; "
327 "temperature: %i.%i C, fan %s\n",
328 th.hi >> 1, th.hi & 1 ? 5 : 0,
329 th.lo >> 1, th.lo & 1 ? 5 : 0,
330 temp >> 1, temp & 1 ? 5 : 0,
331 fan_state[netwinder_get_fan()]);
333 return len;
334 }
336 static struct proc_dir_entry *proc_therm_ds1620;
337 #endif
339 static const struct file_operations ds1620_fops = {
340 .owner = THIS_MODULE,
341 .open = nonseekable_open,
342 .read = ds1620_read,
343 .ioctl = ds1620_ioctl,
344 };
346 static struct miscdevice ds1620_miscdev = {
347 TEMP_MINOR,
348 "temp",
349 &ds1620_fops
350 };
352 static int __init ds1620_init(void)
353 {
354 int ret;
355 struct therm th, th_start;
357 if (!machine_is_netwinder())
358 return -ENODEV;
360 ds1620_out(THERM_RESET, 0, 0);
361 ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
362 ds1620_out(THERM_START_CONVERT, 0, 0);
364 /*
365 * Trigger the fan to start by setting
366 * temperature high point low. This kicks
367 * the fan into action.
368 */
369 ds1620_read_state(&th);
370 th_start.lo = 0;
371 th_start.hi = 1;
372 ds1620_write_state(&th_start);
374 msleep(2000);
376 ds1620_write_state(&th);
378 ret = misc_register(&ds1620_miscdev);
379 if (ret < 0)
380 return ret;
382 #ifdef THERM_USE_PROC
383 proc_therm_ds1620 = create_proc_entry("therm", 0, NULL);
384 if (proc_therm_ds1620)
385 proc_therm_ds1620->read_proc = proc_therm_ds1620_read;
386 else
387 printk(KERN_ERR "therm: unable to register /proc/therm\n");
388 #endif
390 ds1620_read_state(&th);
391 ret = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
393 printk(KERN_INFO "Thermostat: high %i.%i, low %i.%i, "
394 "current %i.%i C, fan %s.\n",
395 th.hi >> 1, th.hi & 1 ? 5 : 0,
396 th.lo >> 1, th.lo & 1 ? 5 : 0,
397 ret >> 1, ret & 1 ? 5 : 0,
398 fan_state[netwinder_get_fan()]);
400 return 0;
401 }
403 static void __exit ds1620_exit(void)
404 {
405 #ifdef THERM_USE_PROC
406 remove_proc_entry("therm", NULL);
407 #endif
408 misc_deregister(&ds1620_miscdev);
409 }
411 module_init(ds1620_init);
412 module_exit(ds1620_exit);
414 MODULE_LICENSE("GPL");