Linux 2.6.17.3
[linux/fpc-iii.git] / drivers / char / ds1620.c
blob62cda25724e30d6501870683c53849f5d8e273a5
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>
15 #include <asm/hardware.h>
16 #include <asm/mach-types.h>
17 #include <asm/uaccess.h>
18 #include <asm/therm.h>
20 #ifdef CONFIG_PROC_FS
21 /* define for /proc interface */
22 #define THERM_USE_PROC
23 #endif
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
36 #define CFG_CPU 2
37 #define CFG_1SHOT 1
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 spinlock_t gpio_lock;
49 extern unsigned int system_rev;
51 static inline void netwinder_ds1620_set_clk(int clk)
53 gpio_modify_op(GPIO_DSCLK, clk ? GPIO_DSCLK : 0);
56 static inline void netwinder_ds1620_set_data(int dat)
58 gpio_modify_op(GPIO_DATA, dat ? GPIO_DATA : 0);
61 static inline int netwinder_ds1620_get_data(void)
63 return gpio_read() & GPIO_DATA;
66 static inline void netwinder_ds1620_set_data_dir(int dir)
68 gpio_modify_io(GPIO_DATA, dir ? GPIO_DATA : 0);
71 static inline void netwinder_ds1620_reset(void)
73 cpld_modify(CPLD_DS_ENABLE, 0);
74 cpld_modify(CPLD_DS_ENABLE, CPLD_DS_ENABLE);
77 static inline void netwinder_lock(unsigned long *flags)
79 spin_lock_irqsave(&gpio_lock, *flags);
82 static inline void netwinder_unlock(unsigned long *flags)
84 spin_unlock_irqrestore(&gpio_lock, *flags);
87 static inline void netwinder_set_fan(int i)
89 unsigned long flags;
91 spin_lock_irqsave(&gpio_lock, flags);
92 gpio_modify_op(GPIO_FAN, i ? GPIO_FAN : 0);
93 spin_unlock_irqrestore(&gpio_lock, flags);
96 static inline int netwinder_get_fan(void)
98 if ((system_rev & 0xf000) == 0x4000)
99 return FAN_ALWAYS_ON;
101 return (gpio_read() & GPIO_FAN) ? FAN_ON : FAN_OFF;
105 * End of NetWinder specifics
108 static void ds1620_send_bits(int nr, int value)
110 int i;
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);
119 value >>= 1;
123 static unsigned int ds1620_recv_bits(int nr)
125 unsigned int value = 0, mask = 1;
126 int i;
128 netwinder_ds1620_set_data(0);
130 for (i = 0; i < nr; i++) {
131 netwinder_ds1620_set_clk(0);
132 udelay(1);
134 if (netwinder_ds1620_get_data())
135 value |= mask;
137 mask <<= 1;
139 netwinder_ds1620_set_clk(1);
140 udelay(1);
143 return value;
146 static void ds1620_out(int cmd, int bits, int value)
148 unsigned long flags;
150 netwinder_lock(&flags);
151 netwinder_ds1620_set_clk(1);
152 netwinder_ds1620_set_data_dir(0);
153 netwinder_ds1620_reset();
155 udelay(1);
157 ds1620_send_bits(8, cmd);
158 if (bits)
159 ds1620_send_bits(bits, value);
161 udelay(1);
163 netwinder_ds1620_reset();
164 netwinder_unlock(&flags);
166 msleep(20);
169 static unsigned int ds1620_in(int cmd, int bits)
171 unsigned long flags;
172 unsigned int value;
174 netwinder_lock(&flags);
175 netwinder_ds1620_set_clk(1);
176 netwinder_ds1620_set_data_dir(0);
177 netwinder_ds1620_reset();
179 udelay(1);
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);
189 return value;
192 static int cvt_9_to_int(unsigned int val)
194 if (val & 0x100)
195 val |= 0xfffffe00;
197 return 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));
214 static ssize_t
215 ds1620_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
217 signed int cur_temp;
218 signed char cur_temp_degF;
220 cur_temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)) >> 1;
222 /* convert to Fahrenheit, as per wdt.c */
223 cur_temp_degF = (cur_temp * 9) / 5 + 32;
225 if (copy_to_user(buf, &cur_temp_degF, 1))
226 return -EFAULT;
228 return 1;
231 static int
232 ds1620_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
234 struct therm therm;
235 union {
236 struct therm __user *therm;
237 int __user *i;
238 } uarg;
239 int i;
241 uarg.i = (int __user *)arg;
243 switch(cmd) {
244 case CMD_SET_THERMOSTATE:
245 case CMD_SET_THERMOSTATE2:
246 if (!capable(CAP_SYS_ADMIN))
247 return -EPERM;
249 if (cmd == CMD_SET_THERMOSTATE) {
250 if (get_user(therm.hi, uarg.i))
251 return -EFAULT;
252 therm.lo = therm.hi - 3;
253 } else {
254 if (copy_from_user(&therm, uarg.therm, sizeof(therm)))
255 return -EFAULT;
258 therm.lo <<= 1;
259 therm.hi <<= 1;
261 ds1620_write_state(&therm);
262 break;
264 case CMD_GET_THERMOSTATE:
265 case CMD_GET_THERMOSTATE2:
266 ds1620_read_state(&therm);
268 therm.lo >>= 1;
269 therm.hi >>= 1;
271 if (cmd == CMD_GET_THERMOSTATE) {
272 if (put_user(therm.hi, uarg.i))
273 return -EFAULT;
274 } else {
275 if (copy_to_user(uarg.therm, &therm, sizeof(therm)))
276 return -EFAULT;
278 break;
280 case CMD_GET_TEMPERATURE:
281 case CMD_GET_TEMPERATURE2:
282 i = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
284 if (cmd == CMD_GET_TEMPERATURE)
285 i >>= 1;
287 return put_user(i, uarg.i) ? -EFAULT : 0;
289 case CMD_GET_STATUS:
290 i = ds1620_in(THERM_READ_CONFIG, 8) & 0xe3;
292 return put_user(i, uarg.i) ? -EFAULT : 0;
294 case CMD_GET_FAN:
295 i = netwinder_get_fan();
297 return put_user(i, uarg.i) ? -EFAULT : 0;
299 case CMD_SET_FAN:
300 if (!capable(CAP_SYS_ADMIN))
301 return -EPERM;
303 if (get_user(i, uarg.i))
304 return -EFAULT;
306 netwinder_set_fan(i);
307 break;
309 default:
310 return -ENOIOCTLCMD;
313 return 0;
316 #ifdef THERM_USE_PROC
317 static int
318 proc_therm_ds1620_read(char *buf, char **start, off_t offset,
319 int len, int *eof, void *unused)
321 struct therm th;
322 int temp;
324 ds1620_read_state(&th);
325 temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
327 len = sprintf(buf, "Thermostat: HI %i.%i, LOW %i.%i; "
328 "temperature: %i.%i C, fan %s\n",
329 th.hi >> 1, th.hi & 1 ? 5 : 0,
330 th.lo >> 1, th.lo & 1 ? 5 : 0,
331 temp >> 1, temp & 1 ? 5 : 0,
332 fan_state[netwinder_get_fan()]);
334 return len;
337 static struct proc_dir_entry *proc_therm_ds1620;
338 #endif
340 static struct file_operations ds1620_fops = {
341 .owner = THIS_MODULE,
342 .open = nonseekable_open,
343 .read = ds1620_read,
344 .ioctl = ds1620_ioctl,
347 static struct miscdevice ds1620_miscdev = {
348 TEMP_MINOR,
349 "temp",
350 &ds1620_fops
353 static int __init ds1620_init(void)
355 int ret;
356 struct therm th, th_start;
358 if (!machine_is_netwinder())
359 return -ENODEV;
361 ds1620_out(THERM_RESET, 0, 0);
362 ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
363 ds1620_out(THERM_START_CONVERT, 0, 0);
366 * Trigger the fan to start by setting
367 * temperature high point low. This kicks
368 * the fan into action.
370 ds1620_read_state(&th);
371 th_start.lo = 0;
372 th_start.hi = 1;
373 ds1620_write_state(&th_start);
375 msleep(2000);
377 ds1620_write_state(&th);
379 ret = misc_register(&ds1620_miscdev);
380 if (ret < 0)
381 return ret;
383 #ifdef THERM_USE_PROC
384 proc_therm_ds1620 = create_proc_entry("therm", 0, NULL);
385 if (proc_therm_ds1620)
386 proc_therm_ds1620->read_proc = proc_therm_ds1620_read;
387 else
388 printk(KERN_ERR "therm: unable to register /proc/therm\n");
389 #endif
391 ds1620_read_state(&th);
392 ret = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
394 printk(KERN_INFO "Thermostat: high %i.%i, low %i.%i, "
395 "current %i.%i C, fan %s.\n",
396 th.hi >> 1, th.hi & 1 ? 5 : 0,
397 th.lo >> 1, th.lo & 1 ? 5 : 0,
398 ret >> 1, ret & 1 ? 5 : 0,
399 fan_state[netwinder_get_fan()]);
401 return 0;
404 static void __exit ds1620_exit(void)
406 #ifdef THERM_USE_PROC
407 remove_proc_entry("therm", NULL);
408 #endif
409 misc_deregister(&ds1620_miscdev);
412 module_init(ds1620_init);
413 module_exit(ds1620_exit);
415 MODULE_LICENSE("GPL");