1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/char/ds1620.c: Dallas Semiconductors DS1620
4 * thermometer driver (as used in the Rebel.com NetWinder)
6 #include <linux/module.h>
7 #include <linux/miscdevice.h>
8 #include <linux/delay.h>
9 #include <linux/proc_fs.h>
10 #include <linux/seq_file.h>
11 #include <linux/capability.h>
12 #include <linux/init.h>
13 #include <linux/mutex.h>
15 #include <mach/hardware.h>
16 #include <asm/mach-types.h>
17 #include <linux/uaccess.h>
18 #include <asm/therm.h>
21 /* define for /proc interface */
22 #define THERM_USE_PROC
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
39 static DEFINE_MUTEX(ds1620_mutex);
40 static const char *fan_state[] = { "off", "on", "on (hardwired)" };
43 * Start of NetWinder specifics
44 * Note! We have to hold the gpio lock with IRQs disabled over the
45 * whole of our transaction to the Dallas chip, since there is a
46 * chance that the WaveArtist driver could touch these bits to
47 * enable or disable the speaker.
49 extern unsigned int system_rev;
51 static inline void netwinder_ds1620_set_clk(int clk)
53 nw_gpio_modify_op(GPIO_DSCLK, clk ? GPIO_DSCLK : 0);
56 static inline void netwinder_ds1620_set_data(int dat)
58 nw_gpio_modify_op(GPIO_DATA, dat ? GPIO_DATA : 0);
61 static inline int netwinder_ds1620_get_data(void)
63 return nw_gpio_read() & GPIO_DATA;
66 static inline void netwinder_ds1620_set_data_dir(int dir)
68 nw_gpio_modify_io(GPIO_DATA, dir ? GPIO_DATA : 0);
71 static inline void netwinder_ds1620_reset(void)
73 nw_cpld_modify(CPLD_DS_ENABLE, 0);
74 nw_cpld_modify(CPLD_DS_ENABLE, CPLD_DS_ENABLE);
77 static inline void netwinder_lock(unsigned long *flags)
79 raw_spin_lock_irqsave(&nw_gpio_lock, *flags);
82 static inline void netwinder_unlock(unsigned long *flags)
84 raw_spin_unlock_irqrestore(&nw_gpio_lock, *flags);
87 static inline void netwinder_set_fan(int i)
91 raw_spin_lock_irqsave(&nw_gpio_lock, flags);
92 nw_gpio_modify_op(GPIO_FAN, i ? GPIO_FAN : 0);
93 raw_spin_unlock_irqrestore(&nw_gpio_lock, flags);
96 static inline int netwinder_get_fan(void)
98 if ((system_rev & 0xf000) == 0x4000)
101 return (nw_gpio_read() & GPIO_FAN) ? FAN_ON : FAN_OFF;
105 * End of NetWinder specifics
108 static void ds1620_send_bits(int nr, int value)
112 for (i = 0; i < nr; i++) {
113 netwinder_ds1620_set_data(value & 1);
114 netwinder_ds1620_set_clk(0);
116 netwinder_ds1620_set_clk(1);
123 static unsigned int ds1620_recv_bits(int nr)
125 unsigned int value = 0, mask = 1;
128 netwinder_ds1620_set_data(0);
130 for (i = 0; i < nr; i++) {
131 netwinder_ds1620_set_clk(0);
134 if (netwinder_ds1620_get_data())
139 netwinder_ds1620_set_clk(1);
146 static void ds1620_out(int cmd, int bits, int value)
150 netwinder_lock(&flags);
151 netwinder_ds1620_set_clk(1);
152 netwinder_ds1620_set_data_dir(0);
153 netwinder_ds1620_reset();
157 ds1620_send_bits(8, cmd);
159 ds1620_send_bits(bits, value);
163 netwinder_ds1620_reset();
164 netwinder_unlock(&flags);
169 static unsigned int ds1620_in(int cmd, int bits)
174 netwinder_lock(&flags);
175 netwinder_ds1620_set_clk(1);
176 netwinder_ds1620_set_data_dir(0);
177 netwinder_ds1620_reset();
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);
192 static int cvt_9_to_int(unsigned int 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 int ds1620_open(struct inode *inode, struct file *file)
216 return stream_open(inode, file);
220 ds1620_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
223 signed char cur_temp_degF;
225 cur_temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)) >> 1;
227 /* convert to Fahrenheit, as per wdt.c */
228 cur_temp_degF = (cur_temp * 9) / 5 + 32;
230 if (copy_to_user(buf, &cur_temp_degF, 1))
237 ds1620_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
241 struct therm __user *therm;
246 uarg.i = (int __user *)arg;
249 case CMD_SET_THERMOSTATE:
250 case CMD_SET_THERMOSTATE2:
251 if (!capable(CAP_SYS_ADMIN))
254 if (cmd == CMD_SET_THERMOSTATE) {
255 if (get_user(therm.hi, uarg.i))
257 therm.lo = therm.hi - 3;
259 if (copy_from_user(&therm, uarg.therm, sizeof(therm)))
266 ds1620_write_state(&therm);
269 case CMD_GET_THERMOSTATE:
270 case CMD_GET_THERMOSTATE2:
271 ds1620_read_state(&therm);
276 if (cmd == CMD_GET_THERMOSTATE) {
277 if (put_user(therm.hi, uarg.i))
280 if (copy_to_user(uarg.therm, &therm, sizeof(therm)))
285 case CMD_GET_TEMPERATURE:
286 case CMD_GET_TEMPERATURE2:
287 i = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
289 if (cmd == CMD_GET_TEMPERATURE)
292 return put_user(i, uarg.i) ? -EFAULT : 0;
295 i = ds1620_in(THERM_READ_CONFIG, 8) & 0xe3;
297 return put_user(i, uarg.i) ? -EFAULT : 0;
300 i = netwinder_get_fan();
302 return put_user(i, uarg.i) ? -EFAULT : 0;
305 if (!capable(CAP_SYS_ADMIN))
308 if (get_user(i, uarg.i))
311 netwinder_set_fan(i);
322 ds1620_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
326 mutex_lock(&ds1620_mutex);
327 ret = ds1620_ioctl(file, cmd, arg);
328 mutex_unlock(&ds1620_mutex);
333 #ifdef THERM_USE_PROC
334 static int ds1620_proc_therm_show(struct seq_file *m, void *v)
339 ds1620_read_state(&th);
340 temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
342 seq_printf(m, "Thermostat: HI %i.%i, LOW %i.%i; temperature: %i.%i C, fan %s\n",
343 th.hi >> 1, th.hi & 1 ? 5 : 0,
344 th.lo >> 1, th.lo & 1 ? 5 : 0,
345 temp >> 1, temp & 1 ? 5 : 0,
346 fan_state[netwinder_get_fan()]);
351 static const struct file_operations ds1620_fops = {
352 .owner = THIS_MODULE,
355 .unlocked_ioctl = ds1620_unlocked_ioctl,
359 static struct miscdevice ds1620_miscdev = {
365 static int __init ds1620_init(void)
368 struct therm th, th_start;
370 if (!machine_is_netwinder())
373 ds1620_out(THERM_RESET, 0, 0);
374 ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
375 ds1620_out(THERM_START_CONVERT, 0, 0);
378 * Trigger the fan to start by setting
379 * temperature high point low. This kicks
380 * the fan into action.
382 ds1620_read_state(&th);
385 ds1620_write_state(&th_start);
389 ds1620_write_state(&th);
391 ret = misc_register(&ds1620_miscdev);
395 #ifdef THERM_USE_PROC
396 if (!proc_create_single("therm", 0, NULL, ds1620_proc_therm_show))
397 printk(KERN_ERR "therm: unable to register /proc/therm\n");
400 ds1620_read_state(&th);
401 ret = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
403 printk(KERN_INFO "Thermostat: high %i.%i, low %i.%i, "
404 "current %i.%i C, fan %s.\n",
405 th.hi >> 1, th.hi & 1 ? 5 : 0,
406 th.lo >> 1, th.lo & 1 ? 5 : 0,
407 ret >> 1, ret & 1 ? 5 : 0,
408 fan_state[netwinder_get_fan()]);
413 static void __exit ds1620_exit(void)
415 #ifdef THERM_USE_PROC
416 remove_proc_entry("therm", NULL);
418 misc_deregister(&ds1620_miscdev);
421 module_init(ds1620_init);
422 module_exit(ds1620_exit);
424 MODULE_LICENSE("GPL");