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)  * lm83.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)  * Copyright (C) 2003-2009  Jean Delvare <jdelvare@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * a sensor chip made by National Semiconductor. It reports up to four
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * temperatures (its own plus up to three external ones) with a 1 deg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * from National's website at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *   http://www.national.com/pf/LM/LM83.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * Since the datasheet omits to give the chip stepping code, I give it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * here: 0x03 (at register 0xff).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * Also supports the LM82 temp sensor, which is basically a stripped down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * model of the LM83.  Datasheet is here:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * http://www.national.com/pf/LM/LM82.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/sysfs.h>
^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)  * Addresses to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * Address is selected using 2 three-level pins, resulting in 9 possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * addresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static const unsigned short normal_i2c[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) enum chips { lm83, lm82 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * The LM83 registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * Manufacturer ID is 0x01 for National Semiconductor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define LM83_REG_R_MAN_ID		0xFE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define LM83_REG_R_CHIP_ID		0xFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define LM83_REG_R_CONFIG		0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define LM83_REG_W_CONFIG		0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define LM83_REG_R_STATUS1		0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define LM83_REG_R_STATUS2		0x35
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define LM83_REG_R_LOCAL_TEMP		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define LM83_REG_R_LOCAL_HIGH		0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define LM83_REG_W_LOCAL_HIGH		0x0B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define LM83_REG_R_REMOTE1_TEMP		0x30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define LM83_REG_R_REMOTE1_HIGH		0x38
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #define LM83_REG_W_REMOTE1_HIGH		0x50
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define LM83_REG_R_REMOTE2_TEMP		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define LM83_REG_R_REMOTE2_HIGH		0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define LM83_REG_W_REMOTE2_HIGH		0x0D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #define LM83_REG_R_REMOTE3_TEMP		0x31
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define LM83_REG_R_REMOTE3_HIGH		0x3A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #define LM83_REG_W_REMOTE3_HIGH		0x52
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #define LM83_REG_R_TCRIT		0x42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #define LM83_REG_W_TCRIT		0x5A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * Conversions and various macros
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #define TEMP_FROM_REG(val)	((val) * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #define TEMP_TO_REG(val)	((val) <= -128000 ? -128 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 				 (val) >= 127000 ? 127 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 				 (val) < 0 ? ((val) - 500) / 1000 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 				 ((val) + 500) / 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static const u8 LM83_REG_R_TEMP[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	LM83_REG_R_LOCAL_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	LM83_REG_R_REMOTE1_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	LM83_REG_R_REMOTE2_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	LM83_REG_R_REMOTE3_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	LM83_REG_R_LOCAL_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	LM83_REG_R_REMOTE1_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	LM83_REG_R_REMOTE2_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	LM83_REG_R_REMOTE3_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	LM83_REG_R_TCRIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static const u8 LM83_REG_W_HIGH[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	LM83_REG_W_LOCAL_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	LM83_REG_W_REMOTE1_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	LM83_REG_W_REMOTE2_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	LM83_REG_W_REMOTE3_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	LM83_REG_W_TCRIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * Client data (each client gets its own)
^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) struct lm83_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	const struct attribute_group *groups[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	char valid; /* zero until following fields are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	unsigned long last_updated; /* in jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	/* registers values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	s8 temp[9];	/* 0..3: input 1-4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			   4..7: high limit 1-4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			   8   : critical limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	u16 alarms; /* bitvector, combined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static struct lm83_data *lm83_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	struct lm83_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		dev_dbg(&client->dev, "Updating lm83 data.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		for (nr = 0; nr < 9; nr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			data->temp[nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			    i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			    LM83_REG_R_TEMP[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		data->alarms =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		    i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		    + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		    << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		data->valid = 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) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)  * Sysfs stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	struct lm83_data *data = lm83_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static ssize_t temp_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			  struct device_attribute *devattr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			  size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	struct lm83_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	int nr = attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	data->temp[nr] = TEMP_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 				  data->temp[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static ssize_t alarms_show(struct device *dev, struct device_attribute *dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			   char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct lm83_data *data = lm83_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return sprintf(buf, "%d\n", data->alarms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static ssize_t alarm_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			  struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	struct lm83_data *data = lm83_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	int bitnr = attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
^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 SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static SENSOR_DEVICE_ATTR_RW(temp3_max, temp, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static SENSOR_DEVICE_ATTR_RW(temp4_max, temp, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static SENSOR_DEVICE_ATTR_RO(temp1_crit, temp, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static SENSOR_DEVICE_ATTR_RO(temp2_crit, temp, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static SENSOR_DEVICE_ATTR_RW(temp3_crit, temp, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static SENSOR_DEVICE_ATTR_RO(temp4_crit, temp, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /* Individual alarm files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm, alarm, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, alarm, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static SENSOR_DEVICE_ATTR_RO(temp4_crit_alarm, alarm, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static SENSOR_DEVICE_ATTR_RO(temp4_fault, alarm, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, alarm, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /* Raw alarm file for compatibility */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static DEVICE_ATTR_RO(alarms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static struct attribute *lm83_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	&sensor_dev_attr_temp3_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	&sensor_dev_attr_temp3_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	&dev_attr_alarms.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static const struct attribute_group lm83_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	.attrs = lm83_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static struct attribute *lm83_attributes_opt[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	&sensor_dev_attr_temp2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	&sensor_dev_attr_temp4_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	&sensor_dev_attr_temp2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	&sensor_dev_attr_temp4_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	&sensor_dev_attr_temp4_crit.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	&sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	&sensor_dev_attr_temp4_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static const struct attribute_group lm83_group_opt = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	.attrs = lm83_attributes_opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)  * Real code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /* Return 0 if detection is successful, -ENODEV otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static int lm83_detect(struct i2c_client *new_client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		       struct i2c_board_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	struct i2c_adapter *adapter = new_client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	u8 man_id, chip_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	/* Detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1) & 0xA8) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	    (i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2) & 0x48) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	    (i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) & 0x41)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		dev_dbg(&adapter->dev, "LM83 detection failed at 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			new_client->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	/* Identification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	man_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_MAN_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (man_id != 0x01)	/* National Semiconductor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	chip_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_CHIP_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	switch (chip_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	case 0x03:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		name = "lm83";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	case 0x01:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		name = "lm82";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		/* identification failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		dev_info(&adapter->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			 man_id, chip_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	strlcpy(info->type, name, I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static const struct i2c_device_id lm83_id[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static int lm83_probe(struct i2c_client *new_client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	struct lm83_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	data = devm_kzalloc(&new_client->dev, sizeof(struct lm83_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	data->client = new_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	 * Register sysfs hooks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	 * The LM82 can only monitor one external diode which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	 * at the same register as the LM83 temp3 entry - so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	 * declare 1 and 3 common, and then 2 and 4 only for the LM83.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	data->groups[0] = &lm83_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (i2c_match_id(lm83_id, new_client)->driver_data == lm83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		data->groups[1] = &lm83_group_opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 							   new_client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 							   data, data->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  * Driver data (common to all clients)
^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 i2c_device_id lm83_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	{ "lm83", lm83 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	{ "lm82", lm82 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) MODULE_DEVICE_TABLE(i2c, lm83_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static struct i2c_driver lm83_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	.class		= I2C_CLASS_HWMON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		.name	= "lm83",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	.probe_new	= lm83_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	.id_table	= lm83_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	.detect		= lm83_detect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	.address_list	= normal_i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) module_i2c_driver(lm83_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) MODULE_DESCRIPTION("LM83 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) MODULE_LICENSE("GPL");