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)  * lm92 - Hardware monitoring driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2005-2008  Jean Delvare <jdelvare@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Based on the lm90 driver, with some ideas taken from the lm_sensors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * lm92 driver as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * The LM92 is a sensor chip made by National Semiconductor. It reports
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * its own temperature with a 0.0625 deg resolution and a 0.33 deg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * accuracy. Complete datasheet can be obtained from National's website
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *   http://www.national.com/pf/LM/LM92.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * This driver also supports the MAX6635 sensor chip made by Maxim.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * This chip is compatible with the LM92, but has a lesser accuracy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * (1.0 deg). Complete datasheet can be obtained from Maxim's website
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *   http://www.maxim-ic.com/quick_view2.cfm/qv_pk/3074
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * Since the LM92 was the first chipset supported by this driver, most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * comments will refer to this chipset, but are actually general and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * concern all supported chipsets, unless mentioned otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * Support could easily be added for the National Semiconductor LM76
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * and Maxim MAX6633 and MAX6634 chips, which are mostly compatible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * with the LM92.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * The LM92 and MAX6635 have 2 two-state pins for address selection,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * resulting in 4 possible addresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 						I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) enum chips { lm92, max6635 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) /* The LM92 registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define LM92_REG_CONFIG			0x01 /* 8-bit, RW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define LM92_REG_TEMP			0x00 /* 16-bit, RO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define LM92_REG_TEMP_HYST		0x02 /* 16-bit, RW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define LM92_REG_TEMP_CRIT		0x03 /* 16-bit, RW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define LM92_REG_TEMP_LOW		0x04 /* 16-bit, RW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define LM92_REG_TEMP_HIGH		0x05 /* 16-bit, RW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define LM92_REG_MAN_ID			0x07 /* 16-bit, RO, LM92 only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * The LM92 uses signed 13-bit values with LSB = 0.0625 degree Celsius,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * left-justified in 16-bit registers. No rounding is done, with such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * a resolution it's just not worth it. Note that the MAX6635 doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * make use of the 4 lower bits for limits (i.e. effective resolution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * for limits is 1 degree Celsius).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static inline int TEMP_FROM_REG(s16 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	return reg / 8 * 625 / 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static inline s16 TEMP_TO_REG(long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	val = clamp_val(val, -60000, 160000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	return val * 10 / 625 * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) /* Alarm flags are stored in the 3 LSB of the temperature register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static inline u8 ALARMS_FROM_REG(s16 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	return reg & 0x0007;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) enum temp_index {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	t_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	t_crit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	t_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	t_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	t_hyst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	t_num_regs
^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 const u8 regs[t_num_regs] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	[t_input] = LM92_REG_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	[t_crit] = LM92_REG_TEMP_CRIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	[t_min] = LM92_REG_TEMP_LOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	[t_max] = LM92_REG_TEMP_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	[t_hyst] = LM92_REG_TEMP_HYST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) /* Client data (each client gets its own) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) struct lm92_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	char valid; /* zero until following fields are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	unsigned long last_updated; /* in jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	/* registers values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	s16 temp[t_num_regs];	/* index with enum temp_index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * Sysfs attributes and callback functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static struct lm92_data *lm92_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct lm92_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (time_after(jiffies, data->last_updated + HZ) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	    !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		dev_dbg(&client->dev, "Updating lm92 data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		for (i = 0; i < t_num_regs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			data->temp[i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 				i2c_smbus_read_word_swapped(client, regs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return data;
^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 ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	struct lm92_data *data = lm92_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
^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 ssize_t temp_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			  struct device_attribute *devattr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			  size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct lm92_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	int nr = attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	data->temp[nr] = TEMP_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	i2c_smbus_write_word_swapped(client, regs[nr], data->temp[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static ssize_t temp_hyst_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			      struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct lm92_data *data = lm92_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		       - TEMP_FROM_REG(data->temp[t_hyst]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static ssize_t temp1_min_hyst_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 				   struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	struct lm92_data *data = lm92_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[t_min])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		       + TEMP_FROM_REG(data->temp[t_hyst]));
^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) static ssize_t temp_hyst_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			       struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			       const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	struct lm92_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	val = clamp_val(val, -120000, 220000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	data->temp[t_hyst] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		TEMP_TO_REG(TEMP_FROM_REG(data->temp[attr->index]) - val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	i2c_smbus_write_word_swapped(client, LM92_REG_TEMP_HYST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 				     data->temp[t_hyst]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			   char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	struct lm92_data *data = lm92_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->temp[t_input]));
^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 alarm_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			  char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	int bitnr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	struct lm92_data *data = lm92_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	return sprintf(buf, "%d\n", (data->temp[t_input] >> bitnr) & 1);
^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) static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, t_crit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp_hyst, t_crit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, t_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static DEVICE_ATTR_RO(temp1_min_hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, t_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, temp_hyst, t_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static DEVICE_ATTR_RO(alarms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  * Detection and registration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static void lm92_init_client(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	u8 config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	/* Start the conversions if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	config = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (config & 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		i2c_smbus_write_byte_data(client, LM92_REG_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 					  config & 0xFE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static struct attribute *lm92_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	&sensor_dev_attr_temp1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	&dev_attr_temp1_min_hyst.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	&dev_attr_alarms.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) ATTRIBUTE_GROUPS(lm92);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /* Return 0 if detection is successful, -ENODEV otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int lm92_detect(struct i2c_client *new_client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		       struct i2c_board_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	struct i2c_adapter *adapter = new_client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	u8 config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	u16 man_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 					    | I2C_FUNC_SMBUS_WORD_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	config = i2c_smbus_read_byte_data(new_client, LM92_REG_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	man_id = i2c_smbus_read_word_data(new_client, LM92_REG_MAN_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if ((config & 0xe0) == 0x00 && man_id == 0x0180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		pr_info("lm92: Found National Semiconductor LM92 chip\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	strlcpy(info->type, "lm92", I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static int lm92_probe(struct i2c_client *new_client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	struct lm92_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	data = devm_kzalloc(&new_client->dev, sizeof(struct lm92_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	data->client = new_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	/* Initialize the chipset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	lm92_init_client(new_client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 							   new_client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 							   data, lm92_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^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)  * Module and driver stuff
^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 const struct i2c_device_id lm92_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	{ "lm92", lm92 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	{ "max6635", max6635 },
^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) MODULE_DEVICE_TABLE(i2c, lm92_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static struct i2c_driver lm92_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	.class		= I2C_CLASS_HWMON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		.name	= "lm92",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	.probe_new	= lm92_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	.id_table	= lm92_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	.detect		= lm92_detect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	.address_list	= normal_i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) module_i2c_driver(lm92_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) MODULE_DESCRIPTION("LM92/MAX6635 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) MODULE_LICENSE("GPL");