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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * adt7x10.c - Part of lm_sensors, Linux kernel modules for hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *	 monitoring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * This driver handles the ADT7410 and compatible digital temperature sensors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Hartmut Knaack <knaack.h@gmx.de> 2012-07-22
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * based on lm75.c by Frodo Looijaard <frodol@dds.nl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * and adt7410.c from iio-staging by Sonic Zhang <sonic.zhang@analog.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.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/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "adt7x10.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * ADT7X10 status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define ADT7X10_STAT_T_LOW		(1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define ADT7X10_STAT_T_HIGH		(1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define ADT7X10_STAT_T_CRIT		(1 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define ADT7X10_STAT_NOT_RDY		(1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * ADT7X10 config
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define ADT7X10_FAULT_QUEUE_MASK	(1 << 0 | 1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define ADT7X10_CT_POLARITY		(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define ADT7X10_INT_POLARITY		(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define ADT7X10_EVENT_MODE		(1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define ADT7X10_MODE_MASK		(1 << 5 | 1 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define ADT7X10_FULL			(0 << 5 | 0 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define ADT7X10_PD			(1 << 5 | 1 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define ADT7X10_RESOLUTION		(1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * ADT7X10 masks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define ADT7X10_T13_VALUE_MASK		0xFFF8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define ADT7X10_T_HYST_MASK		0xF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) /* straight from the datasheet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define ADT7X10_TEMP_MIN (-55000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define ADT7X10_TEMP_MAX 150000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) /* Each client has this additional data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) struct adt7x10_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	const struct adt7x10_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	const char		*name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct device		*hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	struct mutex		update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	u8			config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	u8			oldconfig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	bool			valid;		/* true if registers valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	unsigned long		last_updated;	/* In jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	s16			temp[4];	/* Register values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 						   0 = input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 						   1 = high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 						   2 = low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 						   3 = critical */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	u8			hyst;		/* hysteresis offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static int adt7x10_read_byte(struct device *dev, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct adt7x10_data *d = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	return d->ops->read_byte(dev, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static int adt7x10_write_byte(struct device *dev, u8 reg, u8 data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct adt7x10_data *d = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	return d->ops->write_byte(dev, reg, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static int adt7x10_read_word(struct device *dev, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	struct adt7x10_data *d = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	return d->ops->read_word(dev, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static int adt7x10_write_word(struct device *dev, u8 reg, u16 data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct adt7x10_data *d = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	return d->ops->write_word(dev, reg, data);
^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 const u8 ADT7X10_REG_TEMP[4] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	ADT7X10_TEMPERATURE,		/* input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	ADT7X10_T_ALARM_HIGH,		/* high */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	ADT7X10_T_ALARM_LOW,		/* low */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	ADT7X10_T_CRIT,			/* critical */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static irqreturn_t adt7x10_irq_handler(int irq, void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct device *dev = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	status = adt7x10_read_byte(dev, ADT7X10_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (status & ADT7X10_STAT_T_HIGH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		sysfs_notify(&dev->kobj, NULL, "temp1_max_alarm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (status & ADT7X10_STAT_T_LOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		sysfs_notify(&dev->kobj, NULL, "temp1_min_alarm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (status & ADT7X10_STAT_T_CRIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		sysfs_notify(&dev->kobj, NULL, "temp1_crit_alarm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return IRQ_HANDLED;
^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) static int adt7x10_temp_ready(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	int i, status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	for (i = 0; i < 6; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		status = adt7x10_read_byte(dev, ADT7X10_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		if (!(status & ADT7X10_STAT_NOT_RDY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		msleep(60);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int adt7x10_update_temp(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	struct adt7x10_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	    || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		int temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		dev_dbg(dev, "Starting update\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		ret = adt7x10_temp_ready(dev); /* check for new value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		temp = adt7x10_read_word(dev, ADT7X10_REG_TEMP[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		if (temp < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			ret = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			dev_dbg(dev, "Failed to read value: reg %d, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				ADT7X10_REG_TEMP[0], ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		data->temp[0] = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		data->valid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) abort:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int adt7x10_fill_cache(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct adt7x10_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	for (i = 1; i < ARRAY_SIZE(data->temp); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		ret = adt7x10_read_word(dev, ADT7X10_REG_TEMP[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			dev_dbg(dev, "Failed to read value: reg %d, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 				ADT7X10_REG_TEMP[i], ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		data->temp[i] = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	ret = adt7x10_read_byte(dev, ADT7X10_T_HYST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		dev_dbg(dev, "Failed to read value: reg %d, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 				ADT7X10_T_HYST, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	data->hyst = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static s16 ADT7X10_TEMP_TO_REG(long temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return DIV_ROUND_CLOSEST(clamp_val(temp, ADT7X10_TEMP_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 					       ADT7X10_TEMP_MAX) * 128, 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static int ADT7X10_REG_TO_TEMP(struct adt7x10_data *data, s16 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	/* in 13 bit mode, bits 0-2 are status flags - mask them out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (!(data->config & ADT7X10_RESOLUTION))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		reg &= ADT7X10_T13_VALUE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * temperature is stored in twos complement format, in steps of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 * 1/128°C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	return DIV_ROUND_CLOSEST(reg * 1000, 128);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /*-----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /* sysfs attributes for hwmon */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static ssize_t adt7x10_temp_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 				 struct device_attribute *da, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	struct adt7x10_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (attr->index == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		ret = adt7x10_update_temp(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	return sprintf(buf, "%d\n", ADT7X10_REG_TO_TEMP(data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		       data->temp[attr->index]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static ssize_t adt7x10_temp_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 				  struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 				  const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	struct adt7x10_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	int nr = attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	long temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	ret = kstrtol(buf, 10, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	data->temp[nr] = ADT7X10_TEMP_TO_REG(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	ret = adt7x10_write_word(dev, ADT7X10_REG_TEMP[nr], data->temp[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		count = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static ssize_t adt7x10_t_hyst_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 				   struct device_attribute *da, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	struct adt7x10_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	int nr = attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	int hyst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	hyst = (data->hyst & ADT7X10_T_HYST_MASK) * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	 * hysteresis is stored as a 4 bit offset in the device, convert it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	 * to an absolute value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (nr == 2)	/* min has positive offset, others have negative */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		hyst = -hyst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		       ADT7X10_REG_TO_TEMP(data, data->temp[nr]) - hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static ssize_t adt7x10_t_hyst_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 				    struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 				    const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	struct adt7x10_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	int limit, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	long hyst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	ret = kstrtol(buf, 10, &hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	/* convert absolute hysteresis value to a 4 bit delta value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	limit = ADT7X10_REG_TO_TEMP(data, data->temp[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	hyst = clamp_val(hyst, ADT7X10_TEMP_MIN, ADT7X10_TEMP_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	data->hyst = clamp_val(DIV_ROUND_CLOSEST(limit - hyst, 1000),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 				   0, ADT7X10_T_HYST_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	ret = adt7x10_write_byte(dev, ADT7X10_T_HYST, data->hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static ssize_t adt7x10_alarm_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 				  struct device_attribute *da, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	ret = adt7x10_read_byte(dev, ADT7X10_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	return sprintf(buf, "%d\n", !!(ret & attr->index));
^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) static ssize_t name_show(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	struct adt7x10_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	return sprintf(buf, "%s\n", data->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static SENSOR_DEVICE_ATTR_RO(temp1_input, adt7x10_temp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static SENSOR_DEVICE_ATTR_RW(temp1_max, adt7x10_temp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static SENSOR_DEVICE_ATTR_RW(temp1_min, adt7x10_temp, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static SENSOR_DEVICE_ATTR_RW(temp1_crit, adt7x10_temp, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, adt7x10_t_hyst, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static SENSOR_DEVICE_ATTR_RO(temp1_min_hyst, adt7x10_t_hyst, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static SENSOR_DEVICE_ATTR_RO(temp1_crit_hyst, adt7x10_t_hyst, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, adt7x10_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			     ADT7X10_STAT_T_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, adt7x10_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			     ADT7X10_STAT_T_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, adt7x10_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			     ADT7X10_STAT_T_CRIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static DEVICE_ATTR_RO(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static struct attribute *adt7x10_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	&sensor_dev_attr_temp1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	&sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static const struct attribute_group adt7x10_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	.attrs = adt7x10_attributes,
^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) int adt7x10_probe(struct device *dev, const char *name, int irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		  const struct adt7x10_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	struct adt7x10_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	data->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	data->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	dev_set_drvdata(dev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	/* configure as specified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	ret = adt7x10_read_byte(dev, ADT7X10_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		dev_dbg(dev, "Can't read config? %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	data->oldconfig = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	 * Set to 16 bit resolution, continous conversion and comparator mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	data->config = data->oldconfig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	data->config &= ~(ADT7X10_MODE_MASK | ADT7X10_CT_POLARITY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			ADT7X10_INT_POLARITY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	data->config |= ADT7X10_FULL | ADT7X10_RESOLUTION | ADT7X10_EVENT_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	if (data->config != data->oldconfig) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		ret = adt7x10_write_byte(dev, ADT7X10_CONFIG, data->config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	dev_dbg(dev, "Config %02x\n", data->config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	ret = adt7x10_fill_cache(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		goto exit_restore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	/* Register sysfs hooks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	ret = sysfs_create_group(&dev->kobj, &adt7x10_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		goto exit_restore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	 * The I2C device will already have it's own 'name' attribute, but for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	 * the SPI device we need to register it. name will only be non NULL if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	 * the device doesn't register the 'name' attribute on its own.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		ret = device_create_file(dev, &dev_attr_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	data->hwmon_dev = hwmon_device_register(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	if (IS_ERR(data->hwmon_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		ret = PTR_ERR(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		goto exit_remove_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (irq > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		ret = request_threaded_irq(irq, NULL, adt7x10_irq_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 				IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 				dev_name(dev), dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 			goto exit_hwmon_device_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) exit_hwmon_device_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	hwmon_device_unregister(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) exit_remove_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		device_remove_file(dev, &dev_attr_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) exit_remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	sysfs_remove_group(&dev->kobj, &adt7x10_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) exit_restore:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	adt7x10_write_byte(dev, ADT7X10_CONFIG, data->oldconfig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) EXPORT_SYMBOL_GPL(adt7x10_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) int adt7x10_remove(struct device *dev, int irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	struct adt7x10_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	if (irq > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		free_irq(irq, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	hwmon_device_unregister(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	if (data->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		device_remove_file(dev, &dev_attr_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	sysfs_remove_group(&dev->kobj, &adt7x10_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	if (data->oldconfig != data->config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		adt7x10_write_byte(dev, ADT7X10_CONFIG, data->oldconfig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) EXPORT_SYMBOL_GPL(adt7x10_remove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) static int adt7x10_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	struct adt7x10_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	return adt7x10_write_byte(dev, ADT7X10_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		data->config | ADT7X10_PD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static int adt7x10_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	struct adt7x10_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	return adt7x10_write_byte(dev, ADT7X10_CONFIG, data->config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) SIMPLE_DEV_PM_OPS(adt7x10_dev_pm_ops, adt7x10_suspend, adt7x10_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) EXPORT_SYMBOL_GPL(adt7x10_dev_pm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) #endif /* CONFIG_PM_SLEEP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) MODULE_AUTHOR("Hartmut Knaack");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) MODULE_DESCRIPTION("ADT7410/ADT7420, ADT7310/ADT7320 common code");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) MODULE_LICENSE("GPL");