Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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