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