Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/char/ds1620.c: Dallas Semiconductors DS1620 |
| 3 | * thermometer driver (as used in the Rebel.com NetWinder) |
| 4 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/miscdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/delay.h> |
| 8 | #include <linux/proc_fs.h> |
David Howells | 9fb8ca5 | 2013-04-10 16:48:39 +0100 | [diff] [blame] | 9 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/capability.h> |
| 11 | #include <linux/init.h> |
Arnd Bergmann | 613655f | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 12 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 14 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <asm/mach-types.h> |
| 16 | #include <asm/uaccess.h> |
| 17 | #include <asm/therm.h> |
| 18 | |
| 19 | #ifdef CONFIG_PROC_FS |
| 20 | /* define for /proc interface */ |
| 21 | #define THERM_USE_PROC |
| 22 | #endif |
| 23 | |
| 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 |
| 34 | |
| 35 | #define CFG_CPU 2 |
| 36 | #define CFG_1SHOT 1 |
| 37 | |
Arnd Bergmann | 613655f | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 38 | static DEFINE_MUTEX(ds1620_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | static const char *fan_state[] = { "off", "on", "on (hardwired)" }; |
| 40 | |
| 41 | /* |
| 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. |
| 47 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | extern unsigned int system_rev; |
| 49 | |
| 50 | static inline void netwinder_ds1620_set_clk(int clk) |
| 51 | { |
Russell King | 70d13e0 | 2008-12-06 08:25:16 +0000 | [diff] [blame] | 52 | nw_gpio_modify_op(GPIO_DSCLK, clk ? GPIO_DSCLK : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static inline void netwinder_ds1620_set_data(int dat) |
| 56 | { |
Russell King | 70d13e0 | 2008-12-06 08:25:16 +0000 | [diff] [blame] | 57 | nw_gpio_modify_op(GPIO_DATA, dat ? GPIO_DATA : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static inline int netwinder_ds1620_get_data(void) |
| 61 | { |
Russell King | 70d13e0 | 2008-12-06 08:25:16 +0000 | [diff] [blame] | 62 | return nw_gpio_read() & GPIO_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static inline void netwinder_ds1620_set_data_dir(int dir) |
| 66 | { |
Russell King | 70d13e0 | 2008-12-06 08:25:16 +0000 | [diff] [blame] | 67 | nw_gpio_modify_io(GPIO_DATA, dir ? GPIO_DATA : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static inline void netwinder_ds1620_reset(void) |
| 71 | { |
Russell King | 70d13e0 | 2008-12-06 08:25:16 +0000 | [diff] [blame] | 72 | nw_cpld_modify(CPLD_DS_ENABLE, 0); |
| 73 | nw_cpld_modify(CPLD_DS_ENABLE, CPLD_DS_ENABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static inline void netwinder_lock(unsigned long *flags) |
| 77 | { |
Arnd Bergmann | 45ef6ac | 2012-08-05 14:59:12 +0000 | [diff] [blame] | 78 | raw_spin_lock_irqsave(&nw_gpio_lock, *flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static inline void netwinder_unlock(unsigned long *flags) |
| 82 | { |
Arnd Bergmann | 45ef6ac | 2012-08-05 14:59:12 +0000 | [diff] [blame] | 83 | raw_spin_unlock_irqrestore(&nw_gpio_lock, *flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static inline void netwinder_set_fan(int i) |
| 87 | { |
| 88 | unsigned long flags; |
| 89 | |
Arnd Bergmann | 45ef6ac | 2012-08-05 14:59:12 +0000 | [diff] [blame] | 90 | raw_spin_lock_irqsave(&nw_gpio_lock, flags); |
Russell King | 70d13e0 | 2008-12-06 08:25:16 +0000 | [diff] [blame] | 91 | nw_gpio_modify_op(GPIO_FAN, i ? GPIO_FAN : 0); |
Arnd Bergmann | 45ef6ac | 2012-08-05 14:59:12 +0000 | [diff] [blame] | 92 | raw_spin_unlock_irqrestore(&nw_gpio_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static inline int netwinder_get_fan(void) |
| 96 | { |
| 97 | if ((system_rev & 0xf000) == 0x4000) |
| 98 | return FAN_ALWAYS_ON; |
| 99 | |
Russell King | 70d13e0 | 2008-12-06 08:25:16 +0000 | [diff] [blame] | 100 | return (nw_gpio_read() & GPIO_FAN) ? FAN_ON : FAN_OFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
| 104 | * End of NetWinder specifics |
| 105 | */ |
| 106 | |
| 107 | static void ds1620_send_bits(int nr, int value) |
| 108 | { |
| 109 | int i; |
| 110 | |
| 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); |
| 117 | |
| 118 | value >>= 1; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static unsigned int ds1620_recv_bits(int nr) |
| 123 | { |
| 124 | unsigned int value = 0, mask = 1; |
| 125 | int i; |
| 126 | |
| 127 | netwinder_ds1620_set_data(0); |
| 128 | |
| 129 | for (i = 0; i < nr; i++) { |
| 130 | netwinder_ds1620_set_clk(0); |
| 131 | udelay(1); |
| 132 | |
| 133 | if (netwinder_ds1620_get_data()) |
| 134 | value |= mask; |
| 135 | |
| 136 | mask <<= 1; |
| 137 | |
| 138 | netwinder_ds1620_set_clk(1); |
| 139 | udelay(1); |
| 140 | } |
| 141 | |
| 142 | return value; |
| 143 | } |
| 144 | |
| 145 | static void ds1620_out(int cmd, int bits, int value) |
| 146 | { |
| 147 | unsigned long flags; |
| 148 | |
| 149 | netwinder_lock(&flags); |
| 150 | netwinder_ds1620_set_clk(1); |
| 151 | netwinder_ds1620_set_data_dir(0); |
| 152 | netwinder_ds1620_reset(); |
| 153 | |
| 154 | udelay(1); |
| 155 | |
| 156 | ds1620_send_bits(8, cmd); |
| 157 | if (bits) |
| 158 | ds1620_send_bits(bits, value); |
| 159 | |
| 160 | udelay(1); |
| 161 | |
| 162 | netwinder_ds1620_reset(); |
| 163 | netwinder_unlock(&flags); |
| 164 | |
Domen Puncer | d8eddb6 | 2005-06-25 14:58:41 -0700 | [diff] [blame] | 165 | msleep(20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static unsigned int ds1620_in(int cmd, int bits) |
| 169 | { |
| 170 | unsigned long flags; |
| 171 | unsigned int value; |
| 172 | |
| 173 | netwinder_lock(&flags); |
| 174 | netwinder_ds1620_set_clk(1); |
| 175 | netwinder_ds1620_set_data_dir(0); |
| 176 | netwinder_ds1620_reset(); |
| 177 | |
| 178 | udelay(1); |
| 179 | |
| 180 | ds1620_send_bits(8, cmd); |
| 181 | |
| 182 | netwinder_ds1620_set_data_dir(1); |
| 183 | value = ds1620_recv_bits(bits); |
| 184 | |
| 185 | netwinder_ds1620_reset(); |
| 186 | netwinder_unlock(&flags); |
| 187 | |
| 188 | return value; |
| 189 | } |
| 190 | |
| 191 | static int cvt_9_to_int(unsigned int val) |
| 192 | { |
| 193 | if (val & 0x100) |
| 194 | val |= 0xfffffe00; |
| 195 | |
| 196 | return val; |
| 197 | } |
| 198 | |
| 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 | } |
| 206 | |
| 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 | } |
| 212 | |
Arnd Bergmann | 080c222 | 2008-05-20 19:15:50 +0200 | [diff] [blame] | 213 | static int ds1620_open(struct inode *inode, struct file *file) |
| 214 | { |
Arnd Bergmann | 080c222 | 2008-05-20 19:15:50 +0200 | [diff] [blame] | 215 | return nonseekable_open(inode, file); |
| 216 | } |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | static ssize_t |
| 219 | ds1620_read(struct file *file, char __user *buf, size_t count, loff_t *ptr) |
| 220 | { |
| 221 | signed int cur_temp; |
| 222 | signed char cur_temp_degF; |
| 223 | |
| 224 | cur_temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)) >> 1; |
| 225 | |
| 226 | /* convert to Fahrenheit, as per wdt.c */ |
| 227 | cur_temp_degF = (cur_temp * 9) / 5 + 32; |
| 228 | |
| 229 | if (copy_to_user(buf, &cur_temp_degF, 1)) |
| 230 | return -EFAULT; |
| 231 | |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | static int |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 236 | ds1620_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
| 238 | struct therm therm; |
| 239 | union { |
| 240 | struct therm __user *therm; |
| 241 | int __user *i; |
| 242 | } uarg; |
| 243 | int i; |
| 244 | |
| 245 | uarg.i = (int __user *)arg; |
| 246 | |
| 247 | switch(cmd) { |
| 248 | case CMD_SET_THERMOSTATE: |
| 249 | case CMD_SET_THERMOSTATE2: |
| 250 | if (!capable(CAP_SYS_ADMIN)) |
| 251 | return -EPERM; |
| 252 | |
| 253 | if (cmd == CMD_SET_THERMOSTATE) { |
| 254 | if (get_user(therm.hi, uarg.i)) |
| 255 | return -EFAULT; |
| 256 | therm.lo = therm.hi - 3; |
| 257 | } else { |
| 258 | if (copy_from_user(&therm, uarg.therm, sizeof(therm))) |
| 259 | return -EFAULT; |
| 260 | } |
| 261 | |
| 262 | therm.lo <<= 1; |
| 263 | therm.hi <<= 1; |
| 264 | |
| 265 | ds1620_write_state(&therm); |
| 266 | break; |
| 267 | |
| 268 | case CMD_GET_THERMOSTATE: |
| 269 | case CMD_GET_THERMOSTATE2: |
| 270 | ds1620_read_state(&therm); |
| 271 | |
| 272 | therm.lo >>= 1; |
| 273 | therm.hi >>= 1; |
| 274 | |
| 275 | if (cmd == CMD_GET_THERMOSTATE) { |
| 276 | if (put_user(therm.hi, uarg.i)) |
| 277 | return -EFAULT; |
| 278 | } else { |
| 279 | if (copy_to_user(uarg.therm, &therm, sizeof(therm))) |
| 280 | return -EFAULT; |
| 281 | } |
| 282 | break; |
| 283 | |
| 284 | case CMD_GET_TEMPERATURE: |
| 285 | case CMD_GET_TEMPERATURE2: |
| 286 | i = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)); |
| 287 | |
| 288 | if (cmd == CMD_GET_TEMPERATURE) |
| 289 | i >>= 1; |
| 290 | |
| 291 | return put_user(i, uarg.i) ? -EFAULT : 0; |
| 292 | |
| 293 | case CMD_GET_STATUS: |
| 294 | i = ds1620_in(THERM_READ_CONFIG, 8) & 0xe3; |
| 295 | |
| 296 | return put_user(i, uarg.i) ? -EFAULT : 0; |
| 297 | |
| 298 | case CMD_GET_FAN: |
| 299 | i = netwinder_get_fan(); |
| 300 | |
| 301 | return put_user(i, uarg.i) ? -EFAULT : 0; |
| 302 | |
| 303 | case CMD_SET_FAN: |
| 304 | if (!capable(CAP_SYS_ADMIN)) |
| 305 | return -EPERM; |
| 306 | |
| 307 | if (get_user(i, uarg.i)) |
| 308 | return -EFAULT; |
| 309 | |
| 310 | netwinder_set_fan(i); |
| 311 | break; |
| 312 | |
| 313 | default: |
| 314 | return -ENOIOCTLCMD; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 320 | static long |
| 321 | ds1620_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 322 | { |
| 323 | int ret; |
| 324 | |
Arnd Bergmann | 613655f | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 325 | mutex_lock(&ds1620_mutex); |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 326 | ret = ds1620_ioctl(file, cmd, arg); |
Arnd Bergmann | 613655f | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 327 | mutex_unlock(&ds1620_mutex); |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 328 | |
| 329 | return ret; |
| 330 | } |
| 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | #ifdef THERM_USE_PROC |
David Howells | 9fb8ca5 | 2013-04-10 16:48:39 +0100 | [diff] [blame] | 333 | static int ds1620_proc_therm_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
| 335 | struct therm th; |
| 336 | int temp; |
| 337 | |
| 338 | ds1620_read_state(&th); |
| 339 | temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)); |
| 340 | |
David Howells | 9fb8ca5 | 2013-04-10 16:48:39 +0100 | [diff] [blame] | 341 | seq_printf(m, "Thermostat: HI %i.%i, LOW %i.%i; temperature: %i.%i C, fan %s\n", |
| 342 | th.hi >> 1, th.hi & 1 ? 5 : 0, |
| 343 | th.lo >> 1, th.lo & 1 ? 5 : 0, |
| 344 | temp >> 1, temp & 1 ? 5 : 0, |
| 345 | fan_state[netwinder_get_fan()]); |
| 346 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | |
David Howells | 9fb8ca5 | 2013-04-10 16:48:39 +0100 | [diff] [blame] | 349 | static int ds1620_proc_therm_open(struct inode *inode, struct file *file) |
| 350 | { |
| 351 | return single_open(file, ds1620_proc_therm_show, NULL); |
| 352 | } |
| 353 | |
| 354 | static const struct file_operations ds1620_proc_therm_fops = { |
| 355 | .open = ds1620_proc_therm_open, |
| 356 | .read = seq_read, |
| 357 | .llseek = seq_lseek, |
Al Viro | 7485e16 | 2013-05-05 00:11:29 -0400 | [diff] [blame] | 358 | .release = single_release, |
David Howells | 9fb8ca5 | 2013-04-10 16:48:39 +0100 | [diff] [blame] | 359 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | #endif |
| 361 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 362 | static const struct file_operations ds1620_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | .owner = THIS_MODULE, |
Arnd Bergmann | 080c222 | 2008-05-20 19:15:50 +0200 | [diff] [blame] | 364 | .open = ds1620_open, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | .read = ds1620_read, |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 366 | .unlocked_ioctl = ds1620_unlocked_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 367 | .llseek = no_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | static struct miscdevice ds1620_miscdev = { |
| 371 | TEMP_MINOR, |
| 372 | "temp", |
| 373 | &ds1620_fops |
| 374 | }; |
| 375 | |
| 376 | static int __init ds1620_init(void) |
| 377 | { |
| 378 | int ret; |
| 379 | struct therm th, th_start; |
| 380 | |
| 381 | if (!machine_is_netwinder()) |
| 382 | return -ENODEV; |
| 383 | |
| 384 | ds1620_out(THERM_RESET, 0, 0); |
| 385 | ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU); |
| 386 | ds1620_out(THERM_START_CONVERT, 0, 0); |
| 387 | |
| 388 | /* |
| 389 | * Trigger the fan to start by setting |
| 390 | * temperature high point low. This kicks |
| 391 | * the fan into action. |
| 392 | */ |
| 393 | ds1620_read_state(&th); |
| 394 | th_start.lo = 0; |
| 395 | th_start.hi = 1; |
| 396 | ds1620_write_state(&th_start); |
| 397 | |
| 398 | msleep(2000); |
| 399 | |
| 400 | ds1620_write_state(&th); |
| 401 | |
| 402 | ret = misc_register(&ds1620_miscdev); |
| 403 | if (ret < 0) |
| 404 | return ret; |
| 405 | |
| 406 | #ifdef THERM_USE_PROC |
David Howells | 9fb8ca5 | 2013-04-10 16:48:39 +0100 | [diff] [blame] | 407 | if (!proc_create("therm", 0, NULL, &ds1620_proc_therm_fops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | printk(KERN_ERR "therm: unable to register /proc/therm\n"); |
| 409 | #endif |
| 410 | |
| 411 | ds1620_read_state(&th); |
| 412 | ret = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)); |
| 413 | |
| 414 | printk(KERN_INFO "Thermostat: high %i.%i, low %i.%i, " |
| 415 | "current %i.%i C, fan %s.\n", |
| 416 | th.hi >> 1, th.hi & 1 ? 5 : 0, |
| 417 | th.lo >> 1, th.lo & 1 ? 5 : 0, |
| 418 | ret >> 1, ret & 1 ? 5 : 0, |
| 419 | fan_state[netwinder_get_fan()]); |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static void __exit ds1620_exit(void) |
| 425 | { |
| 426 | #ifdef THERM_USE_PROC |
| 427 | remove_proc_entry("therm", NULL); |
| 428 | #endif |
| 429 | misc_deregister(&ds1620_miscdev); |
| 430 | } |
| 431 | |
| 432 | module_init(ds1620_init); |
| 433 | module_exit(ds1620_exit); |
| 434 | |
| 435 | MODULE_LICENSE("GPL"); |