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)  * A hwmon driver for the Intel 5000 series chipset FB-DIMM AMB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * temperature sensors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2007 IBM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Darrick J. Wong <darrick.wong@oracle.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/log2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define DRVNAME "i5k_amb"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define I5K_REG_AMB_BASE_ADDR		0x48
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define I5K_REG_AMB_LEN_ADDR		0x50
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define I5K_REG_CHAN0_PRESENCE_ADDR	0x64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define I5K_REG_CHAN1_PRESENCE_ADDR	0x66
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define AMB_REG_TEMP_MIN_ADDR		0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define AMB_REG_TEMP_MID_ADDR		0x81
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define AMB_REG_TEMP_MAX_ADDR		0x82
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define AMB_REG_TEMP_STATUS_ADDR	0x84
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define AMB_REG_TEMP_ADDR		0x85
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define AMB_CONFIG_SIZE			2048
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define AMB_FUNC_3_OFFSET		768
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static unsigned long amb_reg_temp_status(unsigned int amb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_STATUS_ADDR +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	       AMB_CONFIG_SIZE * amb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static unsigned long amb_reg_temp_min(unsigned int amb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MIN_ADDR +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	       AMB_CONFIG_SIZE * amb;
^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) static unsigned long amb_reg_temp_mid(unsigned int amb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MID_ADDR +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	       AMB_CONFIG_SIZE * amb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static unsigned long amb_reg_temp_max(unsigned int amb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MAX_ADDR +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	       AMB_CONFIG_SIZE * amb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static unsigned long amb_reg_temp(unsigned int amb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_ADDR +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	       AMB_CONFIG_SIZE * amb;
^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) #define MAX_MEM_CHANNELS		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #define MAX_AMBS_PER_CHANNEL		16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #define MAX_AMBS			(MAX_MEM_CHANNELS * \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 					 MAX_AMBS_PER_CHANNEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #define CHANNEL_SHIFT			4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define DIMM_MASK			0xF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * Ugly hack: For some reason the highest bit is set if there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * are _any_ DIMMs in the channel.  Attempting to read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * this "high-order" AMB results in a memory bus error, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * for now we'll just ignore that top bit, even though that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * might prevent us from seeing the 16th DIMM in the channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #define REAL_MAX_AMBS_PER_CHANNEL	15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #define KNOBS_PER_AMB			6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static unsigned long amb_num_from_reg(unsigned int byte_num, unsigned int bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	return byte_num * MAX_AMBS_PER_CHANNEL + bit;
^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) #define AMB_SYSFS_NAME_LEN		16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) struct i5k_device_attribute {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct sensor_device_attribute s_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	char name[AMB_SYSFS_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) struct i5k_amb_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	unsigned long amb_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	unsigned long amb_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	u16 amb_present[MAX_MEM_CHANNELS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	void __iomem *amb_mmio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct i5k_device_attribute *attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	unsigned int num_attrs;
^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) static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return sprintf(buf, "%s\n", DRVNAME);
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static DEVICE_ATTR_RO(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static struct platform_device *amb_pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static u8 amb_read_byte(struct i5k_amb_data *data, unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	return ioread8(data->amb_mmio + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static void amb_write_byte(struct i5k_amb_data *data, unsigned long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			   u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	iowrite8(val, data->amb_mmio + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static ssize_t show_amb_alarm(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			     struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct i5k_amb_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (!(amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x20) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	     (amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return sprintf(buf, "1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return sprintf(buf, "0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static ssize_t store_amb_min(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			     struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			     const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			     size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	struct i5k_amb_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	unsigned long temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	int ret = kstrtoul(buf, 10, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	temp = temp / 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (temp > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		temp = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	amb_write_byte(data, amb_reg_temp_min(attr->index), temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	return count;
^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 store_amb_mid(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			     struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			     const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			     size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	struct i5k_amb_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	unsigned long temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	int ret = kstrtoul(buf, 10, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	temp = temp / 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (temp > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		temp = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	amb_write_byte(data, amb_reg_temp_mid(attr->index), temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static ssize_t store_amb_max(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			     struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			     const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			     size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct i5k_amb_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	unsigned long temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	int ret = kstrtoul(buf, 10, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	temp = temp / 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (temp > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		temp = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	amb_write_byte(data, amb_reg_temp_max(attr->index), temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return count;
^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 ssize_t show_amb_min(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			     struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	struct i5k_amb_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		500 * amb_read_byte(data, amb_reg_temp_min(attr->index)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static ssize_t show_amb_mid(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			     struct device_attribute *devattr,
^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 sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	struct i5k_amb_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		500 * amb_read_byte(data, amb_reg_temp_mid(attr->index)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static ssize_t show_amb_max(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			     struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	struct i5k_amb_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		500 * amb_read_byte(data, amb_reg_temp_max(attr->index)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static ssize_t show_amb_temp(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			     struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	struct i5k_amb_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		500 * amb_read_byte(data, amb_reg_temp(attr->index)));
^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) static ssize_t show_label(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			  struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			  char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return sprintf(buf, "Ch. %d DIMM %d\n", attr->index >> CHANNEL_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		       attr->index & DIMM_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static int i5k_amb_hwmon_init(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	int i, j, k, d = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	u16 c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	int num_ambs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct i5k_amb_data *data = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	/* Count the number of AMBs found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	/* ignore the high-order bit, see "Ugly hack" comment above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	for (i = 0; i < MAX_MEM_CHANNELS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		num_ambs += hweight16(data->amb_present[i] & 0x7fff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	/* Set up sysfs stuff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	data->attrs = kzalloc(array3_size(num_ambs, KNOBS_PER_AMB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 					  sizeof(*data->attrs)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (!data->attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	data->num_attrs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	for (i = 0; i < MAX_MEM_CHANNELS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		c = data->amb_present[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		for (j = 0; j < REAL_MAX_AMBS_PER_CHANNEL; j++, c >>= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			struct i5k_device_attribute *iattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			k = amb_num_from_reg(i, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			if (!(c & 0x1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			d++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			/* sysfs label */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			iattr = data->attrs + data->num_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 				 "temp%d_label", d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			iattr->s_attr.dev_attr.attr.name = iattr->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			iattr->s_attr.dev_attr.attr.mode = 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			iattr->s_attr.dev_attr.show = show_label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			iattr->s_attr.index = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			res = device_create_file(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 						 &iattr->s_attr.dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 				goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			data->num_attrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			/* Temperature sysfs knob */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			iattr = data->attrs + data->num_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 				 "temp%d_input", d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			iattr->s_attr.dev_attr.attr.name = iattr->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			iattr->s_attr.dev_attr.attr.mode = 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			iattr->s_attr.dev_attr.show = show_amb_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			iattr->s_attr.index = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			res = device_create_file(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 						 &iattr->s_attr.dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 				goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			data->num_attrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			/* Temperature min sysfs knob */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			iattr = data->attrs + data->num_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 				 "temp%d_min", d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			iattr->s_attr.dev_attr.attr.name = iattr->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			iattr->s_attr.dev_attr.attr.mode = 0644;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			iattr->s_attr.dev_attr.show = show_amb_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			iattr->s_attr.dev_attr.store = store_amb_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			iattr->s_attr.index = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			res = device_create_file(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 						 &iattr->s_attr.dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 				goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			data->num_attrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			/* Temperature mid sysfs knob */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			iattr = data->attrs + data->num_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 				 "temp%d_mid", d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			iattr->s_attr.dev_attr.attr.name = iattr->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			iattr->s_attr.dev_attr.attr.mode = 0644;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			iattr->s_attr.dev_attr.show = show_amb_mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			iattr->s_attr.dev_attr.store = store_amb_mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			iattr->s_attr.index = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			res = device_create_file(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 						 &iattr->s_attr.dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 				goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			data->num_attrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			/* Temperature max sysfs knob */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			iattr = data->attrs + data->num_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 				 "temp%d_max", d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			iattr->s_attr.dev_attr.attr.name = iattr->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			iattr->s_attr.dev_attr.attr.mode = 0644;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			iattr->s_attr.dev_attr.show = show_amb_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			iattr->s_attr.dev_attr.store = store_amb_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			iattr->s_attr.index = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			res = device_create_file(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 						 &iattr->s_attr.dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 				goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			data->num_attrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			/* Temperature alarm sysfs knob */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			iattr = data->attrs + data->num_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 				 "temp%d_alarm", d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			iattr->s_attr.dev_attr.attr.name = iattr->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			iattr->s_attr.dev_attr.attr.mode = 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			iattr->s_attr.dev_attr.show = show_amb_alarm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			iattr->s_attr.index = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			res = device_create_file(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 						 &iattr->s_attr.dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 				goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			data->num_attrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	res = device_create_file(&pdev->dev, &dev_attr_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	data->hwmon_dev = hwmon_device_register(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	if (IS_ERR(data->hwmon_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		res = PTR_ERR(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) exit_remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	device_remove_file(&pdev->dev, &dev_attr_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	for (i = 0; i < data->num_attrs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	kfree(data->attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static int i5k_amb_add(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	/* only ever going to be one of these */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	amb_pdev = platform_device_alloc(DRVNAME, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	if (!amb_pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	res = platform_device_add(amb_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	platform_device_put(amb_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static int i5k_find_amb_registers(struct i5k_amb_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 					    unsigned long devid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	struct pci_dev *pcidev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	u32 val32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	int res = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	/* Find AMB register memory space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 				devid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 				NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	if (!pcidev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	pci_read_config_dword(pcidev, I5K_REG_AMB_BASE_ADDR, &val32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	if (val32 == (u32)~0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	data->amb_base = val32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	pci_read_config_dword(pcidev, I5K_REG_AMB_LEN_ADDR, &val32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	if (val32 == (u32)~0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	data->amb_len = val32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	/* Is it big enough? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	if (data->amb_len < AMB_CONFIG_SIZE * MAX_AMBS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		dev_err(&pcidev->dev, "AMB region too small!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	pci_dev_put(pcidev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) static int i5k_channel_probe(u16 *amb_present, unsigned long dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	struct pci_dev *pcidev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	u16 val16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	int res = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	/* Copy the DIMM presence map for these two channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	if (!pcidev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	pci_read_config_word(pcidev, I5K_REG_CHAN0_PRESENCE_ADDR, &val16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (val16 == (u16)~0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	amb_present[0] = val16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	pci_read_config_word(pcidev, I5K_REG_CHAN1_PRESENCE_ADDR, &val16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if (val16 == (u16)~0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	amb_present[1] = val16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	pci_dev_put(pcidev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	unsigned long err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	unsigned long fbd0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) } chipset_ids[]  = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	{ PCI_DEVICE_ID_INTEL_5000_ERR, PCI_DEVICE_ID_INTEL_5000_FBD0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	{ PCI_DEVICE_ID_INTEL_5400_ERR, PCI_DEVICE_ID_INTEL_5400_FBD0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	{ 0, 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) #ifdef MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) static const struct pci_device_id i5k_amb_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5000_ERR) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5400_ERR) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	{ 0, }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) MODULE_DEVICE_TABLE(pci, i5k_amb_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) static int i5k_amb_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	struct i5k_amb_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	struct resource *reso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	int i, res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	data = kzalloc(sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	/* Figure out where the AMB registers live */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		res = i5k_find_amb_registers(data, chipset_ids[i].err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		if (res == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	} while (chipset_ids[i].err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	/* Copy the DIMM presence map for the first two channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	res = i5k_channel_probe(&data->amb_present[0], chipset_ids[i].fbd0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	/* Copy the DIMM presence map for the optional second two channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	i5k_channel_probe(&data->amb_present[2], chipset_ids[i].fbd0 + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	/* Set up resource regions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	reso = request_mem_region(data->amb_base, data->amb_len, DRVNAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	if (!reso) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		res = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	data->amb_mmio = ioremap(data->amb_base, data->amb_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	if (!data->amb_mmio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		res = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		goto err_map_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	platform_set_drvdata(pdev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	res = i5k_amb_hwmon_init(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		goto err_init_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) err_init_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	iounmap(data->amb_mmio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) err_map_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	release_mem_region(data->amb_base, data->amb_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) static int i5k_amb_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	struct i5k_amb_data *data = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	hwmon_device_unregister(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	device_remove_file(&pdev->dev, &dev_attr_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	for (i = 0; i < data->num_attrs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	kfree(data->attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	iounmap(data->amb_mmio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	release_mem_region(data->amb_base, data->amb_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static struct platform_driver i5k_amb_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		.name = DRVNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	.probe = i5k_amb_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	.remove = i5k_amb_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) static int __init i5k_amb_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	res = platform_driver_register(&i5k_amb_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	res = i5k_amb_add();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		platform_driver_unregister(&i5k_amb_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) static void __exit i5k_amb_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	platform_device_unregister(amb_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	platform_driver_unregister(&i5k_amb_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) module_init(i5k_amb_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) module_exit(i5k_amb_exit);