blob: cf89a9631107dbd54561c222003033d49c3bd41c [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/drivers/char/ds1620.c: Dallas Semiconductors DS1620
4 * thermometer driver (as used in the Rebel.com NetWinder)
5 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/miscdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/delay.h>
9#include <linux/proc_fs.h>
David Howells9fb8ca52013-04-10 16:48:39 +010010#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/capability.h>
12#include <linux/init.h>
Arnd Bergmann613655f2010-06-02 14:28:52 +020013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Russell Kinga09e64f2008-08-05 16:14:15 +010015#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/mach-types.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080017#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/therm.h>
19
20#ifdef CONFIG_PROC_FS
21/* define for /proc interface */
22#define THERM_USE_PROC
23#endif
24
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
35
36#define CFG_CPU 2
37#define CFG_1SHOT 1
38
Arnd Bergmann613655f2010-06-02 14:28:52 +020039static DEFINE_MUTEX(ds1620_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static const char *fan_state[] = { "off", "on", "on (hardwired)" };
41
42/*
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.
48 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049extern unsigned int system_rev;
50
51static inline void netwinder_ds1620_set_clk(int clk)
52{
Russell King70d13e02008-12-06 08:25:16 +000053 nw_gpio_modify_op(GPIO_DSCLK, clk ? GPIO_DSCLK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
56static inline void netwinder_ds1620_set_data(int dat)
57{
Russell King70d13e02008-12-06 08:25:16 +000058 nw_gpio_modify_op(GPIO_DATA, dat ? GPIO_DATA : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61static inline int netwinder_ds1620_get_data(void)
62{
Russell King70d13e02008-12-06 08:25:16 +000063 return nw_gpio_read() & GPIO_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
66static inline void netwinder_ds1620_set_data_dir(int dir)
67{
Russell King70d13e02008-12-06 08:25:16 +000068 nw_gpio_modify_io(GPIO_DATA, dir ? GPIO_DATA : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71static inline void netwinder_ds1620_reset(void)
72{
Russell King70d13e02008-12-06 08:25:16 +000073 nw_cpld_modify(CPLD_DS_ENABLE, 0);
74 nw_cpld_modify(CPLD_DS_ENABLE, CPLD_DS_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77static inline void netwinder_lock(unsigned long *flags)
78{
Arnd Bergmann45ef6ac2012-08-05 14:59:12 +000079 raw_spin_lock_irqsave(&nw_gpio_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82static inline void netwinder_unlock(unsigned long *flags)
83{
Arnd Bergmann45ef6ac2012-08-05 14:59:12 +000084 raw_spin_unlock_irqrestore(&nw_gpio_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87static inline void netwinder_set_fan(int i)
88{
89 unsigned long flags;
90
Arnd Bergmann45ef6ac2012-08-05 14:59:12 +000091 raw_spin_lock_irqsave(&nw_gpio_lock, flags);
Russell King70d13e02008-12-06 08:25:16 +000092 nw_gpio_modify_op(GPIO_FAN, i ? GPIO_FAN : 0);
Arnd Bergmann45ef6ac2012-08-05 14:59:12 +000093 raw_spin_unlock_irqrestore(&nw_gpio_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96static inline int netwinder_get_fan(void)
97{
98 if ((system_rev & 0xf000) == 0x4000)
99 return FAN_ALWAYS_ON;
100
Russell King70d13e02008-12-06 08:25:16 +0000101 return (nw_gpio_read() & GPIO_FAN) ? FAN_ON : FAN_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104/*
105 * End of NetWinder specifics
106 */
107
108static void ds1620_send_bits(int nr, int value)
109{
110 int i;
111
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);
118
119 value >>= 1;
120 }
121}
122
123static unsigned int ds1620_recv_bits(int nr)
124{
125 unsigned int value = 0, mask = 1;
126 int i;
127
128 netwinder_ds1620_set_data(0);
129
130 for (i = 0; i < nr; i++) {
131 netwinder_ds1620_set_clk(0);
132 udelay(1);
133
134 if (netwinder_ds1620_get_data())
135 value |= mask;
136
137 mask <<= 1;
138
139 netwinder_ds1620_set_clk(1);
140 udelay(1);
141 }
142
143 return value;
144}
145
146static void ds1620_out(int cmd, int bits, int value)
147{
148 unsigned long flags;
149
150 netwinder_lock(&flags);
151 netwinder_ds1620_set_clk(1);
152 netwinder_ds1620_set_data_dir(0);
153 netwinder_ds1620_reset();
154
155 udelay(1);
156
157 ds1620_send_bits(8, cmd);
158 if (bits)
159 ds1620_send_bits(bits, value);
160
161 udelay(1);
162
163 netwinder_ds1620_reset();
164 netwinder_unlock(&flags);
165
Domen Puncerd8eddb62005-06-25 14:58:41 -0700166 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169static unsigned int ds1620_in(int cmd, int bits)
170{
171 unsigned long flags;
172 unsigned int value;
173
174 netwinder_lock(&flags);
175 netwinder_ds1620_set_clk(1);
176 netwinder_ds1620_set_data_dir(0);
177 netwinder_ds1620_reset();
178
179 udelay(1);
180
181 ds1620_send_bits(8, cmd);
182
183 netwinder_ds1620_set_data_dir(1);
184 value = ds1620_recv_bits(bits);
185
186 netwinder_ds1620_reset();
187 netwinder_unlock(&flags);
188
189 return value;
190}
191
192static int cvt_9_to_int(unsigned int val)
193{
194 if (val & 0x100)
195 val |= 0xfffffe00;
196
197 return val;
198}
199
200static void ds1620_write_state(struct therm *therm)
201{
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);
206}
207
208static void ds1620_read_state(struct therm *therm)
209{
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));
212}
213
Arnd Bergmann080c2222008-05-20 19:15:50 +0200214static int ds1620_open(struct inode *inode, struct file *file)
215{
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300216 return stream_open(inode, file);
Arnd Bergmann080c2222008-05-20 19:15:50 +0200217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static ssize_t
220ds1620_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
221{
222 signed int cur_temp;
223 signed char cur_temp_degF;
224
225 cur_temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)) >> 1;
226
227 /* convert to Fahrenheit, as per wdt.c */
228 cur_temp_degF = (cur_temp * 9) / 5 + 32;
229
230 if (copy_to_user(buf, &cur_temp_degF, 1))
231 return -EFAULT;
232
233 return 1;
234}
235
236static int
Arnd Bergmann55929332010-04-27 00:24:05 +0200237ds1620_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 struct therm therm;
240 union {
241 struct therm __user *therm;
242 int __user *i;
243 } uarg;
244 int i;
245
246 uarg.i = (int __user *)arg;
247
248 switch(cmd) {
249 case CMD_SET_THERMOSTATE:
250 case CMD_SET_THERMOSTATE2:
251 if (!capable(CAP_SYS_ADMIN))
252 return -EPERM;
253
254 if (cmd == CMD_SET_THERMOSTATE) {
255 if (get_user(therm.hi, uarg.i))
256 return -EFAULT;
257 therm.lo = therm.hi - 3;
258 } else {
259 if (copy_from_user(&therm, uarg.therm, sizeof(therm)))
260 return -EFAULT;
261 }
262
263 therm.lo <<= 1;
264 therm.hi <<= 1;
265
266 ds1620_write_state(&therm);
267 break;
268
269 case CMD_GET_THERMOSTATE:
270 case CMD_GET_THERMOSTATE2:
271 ds1620_read_state(&therm);
272
273 therm.lo >>= 1;
274 therm.hi >>= 1;
275
276 if (cmd == CMD_GET_THERMOSTATE) {
277 if (put_user(therm.hi, uarg.i))
278 return -EFAULT;
279 } else {
280 if (copy_to_user(uarg.therm, &therm, sizeof(therm)))
281 return -EFAULT;
282 }
283 break;
284
285 case CMD_GET_TEMPERATURE:
286 case CMD_GET_TEMPERATURE2:
287 i = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
288
289 if (cmd == CMD_GET_TEMPERATURE)
290 i >>= 1;
291
292 return put_user(i, uarg.i) ? -EFAULT : 0;
293
294 case CMD_GET_STATUS:
295 i = ds1620_in(THERM_READ_CONFIG, 8) & 0xe3;
296
297 return put_user(i, uarg.i) ? -EFAULT : 0;
298
299 case CMD_GET_FAN:
300 i = netwinder_get_fan();
301
302 return put_user(i, uarg.i) ? -EFAULT : 0;
303
304 case CMD_SET_FAN:
305 if (!capable(CAP_SYS_ADMIN))
306 return -EPERM;
307
308 if (get_user(i, uarg.i))
309 return -EFAULT;
310
311 netwinder_set_fan(i);
312 break;
313
314 default:
315 return -ENOIOCTLCMD;
316 }
317
318 return 0;
319}
320
Arnd Bergmann55929332010-04-27 00:24:05 +0200321static long
322ds1620_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
323{
324 int ret;
325
Arnd Bergmann613655f2010-06-02 14:28:52 +0200326 mutex_lock(&ds1620_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200327 ret = ds1620_ioctl(file, cmd, arg);
Arnd Bergmann613655f2010-06-02 14:28:52 +0200328 mutex_unlock(&ds1620_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200329
330 return ret;
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#ifdef THERM_USE_PROC
David Howells9fb8ca52013-04-10 16:48:39 +0100334static int ds1620_proc_therm_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct therm th;
337 int temp;
338
339 ds1620_read_state(&th);
340 temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
341
David Howells9fb8ca52013-04-10 16:48:39 +0100342 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()]);
347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349#endif
350
Arjan van de Ven62322d22006-07-03 00:24:21 -0700351static const struct file_operations ds1620_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 .owner = THIS_MODULE,
Arnd Bergmann080c2222008-05-20 19:15:50 +0200353 .open = ds1620_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 .read = ds1620_read,
Arnd Bergmann55929332010-04-27 00:24:05 +0200355 .unlocked_ioctl = ds1620_unlocked_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200356 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357};
358
359static struct miscdevice ds1620_miscdev = {
360 TEMP_MINOR,
361 "temp",
362 &ds1620_fops
363};
364
365static int __init ds1620_init(void)
366{
367 int ret;
368 struct therm th, th_start;
369
370 if (!machine_is_netwinder())
371 return -ENODEV;
372
373 ds1620_out(THERM_RESET, 0, 0);
374 ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
375 ds1620_out(THERM_START_CONVERT, 0, 0);
376
377 /*
378 * Trigger the fan to start by setting
379 * temperature high point low. This kicks
380 * the fan into action.
381 */
382 ds1620_read_state(&th);
383 th_start.lo = 0;
384 th_start.hi = 1;
385 ds1620_write_state(&th_start);
386
387 msleep(2000);
388
389 ds1620_write_state(&th);
390
391 ret = misc_register(&ds1620_miscdev);
392 if (ret < 0)
393 return ret;
394
395#ifdef THERM_USE_PROC
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200396 if (!proc_create_single("therm", 0, NULL, ds1620_proc_therm_show))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 printk(KERN_ERR "therm: unable to register /proc/therm\n");
398#endif
399
400 ds1620_read_state(&th);
401 ret = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
402
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()]);
409
410 return 0;
411}
412
413static void __exit ds1620_exit(void)
414{
415#ifdef THERM_USE_PROC
416 remove_proc_entry("therm", NULL);
417#endif
418 misc_deregister(&ds1620_miscdev);
419}
420
421module_init(ds1620_init);
422module_exit(ds1620_exit);
423
424MODULE_LICENSE("GPL");