xref: /linux/drivers/hwmon/emc2103.c (revision 2355375efdf10f43680d420023baa97796bfcdff)
19df7305bSSteve Glendinning /*
2bf0f3a04SGuenter Roeck  * emc2103.c - Support for SMSC EMC2103
3bf0f3a04SGuenter Roeck  * Copyright (c) 2010 SMSC
4bf0f3a04SGuenter Roeck  *
5bf0f3a04SGuenter Roeck  * This program is free software; you can redistribute it and/or modify
6bf0f3a04SGuenter Roeck  * it under the terms of the GNU General Public License as published by
7bf0f3a04SGuenter Roeck  * the Free Software Foundation; either version 2 of the License, or
8bf0f3a04SGuenter Roeck  * (at your option) any later version.
9bf0f3a04SGuenter Roeck  *
10bf0f3a04SGuenter Roeck  * This program is distributed in the hope that it will be useful,
11bf0f3a04SGuenter Roeck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12bf0f3a04SGuenter Roeck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13bf0f3a04SGuenter Roeck  * GNU General Public License for more details.
14bf0f3a04SGuenter Roeck  *
15bf0f3a04SGuenter Roeck  * You should have received a copy of the GNU General Public License
16bf0f3a04SGuenter Roeck  * along with this program; if not, write to the Free Software
17bf0f3a04SGuenter Roeck  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
189df7305bSSteve Glendinning  */
199df7305bSSteve Glendinning 
209df7305bSSteve Glendinning #include <linux/module.h>
219df7305bSSteve Glendinning #include <linux/init.h>
229df7305bSSteve Glendinning #include <linux/slab.h>
239df7305bSSteve Glendinning #include <linux/jiffies.h>
249df7305bSSteve Glendinning #include <linux/i2c.h>
259df7305bSSteve Glendinning #include <linux/hwmon.h>
269df7305bSSteve Glendinning #include <linux/hwmon-sysfs.h>
279df7305bSSteve Glendinning #include <linux/err.h>
289df7305bSSteve Glendinning #include <linux/mutex.h>
299df7305bSSteve Glendinning 
309df7305bSSteve Glendinning /* Addresses scanned */
319df7305bSSteve Glendinning static const unsigned short normal_i2c[] = { 0x2E, I2C_CLIENT_END };
329df7305bSSteve Glendinning 
339df7305bSSteve Glendinning static const u8 REG_TEMP[4] = { 0x00, 0x02, 0x04, 0x06 };
349df7305bSSteve Glendinning static const u8 REG_TEMP_MIN[4] = { 0x3c, 0x38, 0x39, 0x3a };
359df7305bSSteve Glendinning static const u8 REG_TEMP_MAX[4] = { 0x34, 0x30, 0x31, 0x32 };
369df7305bSSteve Glendinning 
379df7305bSSteve Glendinning #define REG_CONF1		0x20
389df7305bSSteve Glendinning #define REG_TEMP_MAX_ALARM	0x24
399df7305bSSteve Glendinning #define REG_TEMP_MIN_ALARM	0x25
409df7305bSSteve Glendinning #define REG_FAN_CONF1		0x42
419df7305bSSteve Glendinning #define REG_FAN_TARGET_LO	0x4c
429df7305bSSteve Glendinning #define REG_FAN_TARGET_HI	0x4d
439df7305bSSteve Glendinning #define REG_FAN_TACH_HI		0x4e
449df7305bSSteve Glendinning #define REG_FAN_TACH_LO		0x4f
459df7305bSSteve Glendinning #define REG_PRODUCT_ID		0xfd
469df7305bSSteve Glendinning #define REG_MFG_ID		0xfe
479df7305bSSteve Glendinning 
489df7305bSSteve Glendinning /* equation 4 from datasheet: rpm = (3932160 * multipler) / count */
499df7305bSSteve Glendinning #define FAN_RPM_FACTOR		3932160
509df7305bSSteve Glendinning 
51bf0f3a04SGuenter Roeck /*
52bf0f3a04SGuenter Roeck  * 2103-2 and 2103-4's 3rd temperature sensor can be connected to two diodes
539df7305bSSteve Glendinning  * in anti-parallel mode, and in this configuration both can be read
549df7305bSSteve Glendinning  * independently (so we have 4 temperature inputs).  The device can't
559df7305bSSteve Glendinning  * detect if it's connected in this mode, so we have to manually enable
569df7305bSSteve Glendinning  * it.  Default is to leave the device in the state it's already in (-1).
57bf0f3a04SGuenter Roeck  * This parameter allows APD mode to be optionally forced on or off
58bf0f3a04SGuenter Roeck  */
599df7305bSSteve Glendinning static int apd = -1;
6069116f27SRusty Russell module_param(apd, bint, 0);
619df7305bSSteve Glendinning MODULE_PARM_DESC(init, "Set to zero to disable anti-parallel diode mode");
629df7305bSSteve Glendinning 
639df7305bSSteve Glendinning struct temperature {
649df7305bSSteve Glendinning 	s8	degrees;
659df7305bSSteve Glendinning 	u8	fraction;	/* 0-7 multiples of 0.125 */
669df7305bSSteve Glendinning };
679df7305bSSteve Glendinning 
689df7305bSSteve Glendinning struct emc2103_data {
699df7305bSSteve Glendinning 	struct device		*hwmon_dev;
709df7305bSSteve Glendinning 	struct mutex		update_lock;
719df7305bSSteve Glendinning 	bool			valid;		/* registers are valid */
729df7305bSSteve Glendinning 	bool			fan_rpm_control;
739df7305bSSteve Glendinning 	int			temp_count;	/* num of temp sensors */
749df7305bSSteve Glendinning 	unsigned long		last_updated;	/* in jiffies */
759df7305bSSteve Glendinning 	struct temperature	temp[4];	/* internal + 3 external */
769df7305bSSteve Glendinning 	s8			temp_min[4];	/* no fractional part */
779df7305bSSteve Glendinning 	s8			temp_max[4];    /* no fractional part */
789df7305bSSteve Glendinning 	u8			temp_min_alarm;
799df7305bSSteve Glendinning 	u8			temp_max_alarm;
809df7305bSSteve Glendinning 	u8			fan_multiplier;
819df7305bSSteve Glendinning 	u16			fan_tach;
829df7305bSSteve Glendinning 	u16			fan_target;
839df7305bSSteve Glendinning };
849df7305bSSteve Glendinning 
859df7305bSSteve Glendinning static int read_u8_from_i2c(struct i2c_client *client, u8 i2c_reg, u8 *output)
869df7305bSSteve Glendinning {
879df7305bSSteve Glendinning 	int status = i2c_smbus_read_byte_data(client, i2c_reg);
889df7305bSSteve Glendinning 	if (status < 0) {
899df7305bSSteve Glendinning 		dev_warn(&client->dev, "reg 0x%02x, err %d\n",
909df7305bSSteve Glendinning 			i2c_reg, status);
919df7305bSSteve Glendinning 	} else {
929df7305bSSteve Glendinning 		*output = status;
939df7305bSSteve Glendinning 	}
949df7305bSSteve Glendinning 	return status;
959df7305bSSteve Glendinning }
969df7305bSSteve Glendinning 
979df7305bSSteve Glendinning static void read_temp_from_i2c(struct i2c_client *client, u8 i2c_reg,
989df7305bSSteve Glendinning 			       struct temperature *temp)
999df7305bSSteve Glendinning {
1009df7305bSSteve Glendinning 	u8 degrees, fractional;
1019df7305bSSteve Glendinning 
1029df7305bSSteve Glendinning 	if (read_u8_from_i2c(client, i2c_reg, &degrees) < 0)
1039df7305bSSteve Glendinning 		return;
1049df7305bSSteve Glendinning 
1059df7305bSSteve Glendinning 	if (read_u8_from_i2c(client, i2c_reg + 1, &fractional) < 0)
1069df7305bSSteve Glendinning 		return;
1079df7305bSSteve Glendinning 
1089df7305bSSteve Glendinning 	temp->degrees = degrees;
1099df7305bSSteve Glendinning 	temp->fraction = (fractional & 0xe0) >> 5;
1109df7305bSSteve Glendinning }
1119df7305bSSteve Glendinning 
1129df7305bSSteve Glendinning static void read_fan_from_i2c(struct i2c_client *client, u16 *output,
1139df7305bSSteve Glendinning 			      u8 hi_addr, u8 lo_addr)
1149df7305bSSteve Glendinning {
1159df7305bSSteve Glendinning 	u8 high_byte, lo_byte;
1169df7305bSSteve Glendinning 
1179df7305bSSteve Glendinning 	if (read_u8_from_i2c(client, hi_addr, &high_byte) < 0)
1189df7305bSSteve Glendinning 		return;
1199df7305bSSteve Glendinning 
1209df7305bSSteve Glendinning 	if (read_u8_from_i2c(client, lo_addr, &lo_byte) < 0)
1219df7305bSSteve Glendinning 		return;
1229df7305bSSteve Glendinning 
1239df7305bSSteve Glendinning 	*output = ((u16)high_byte << 5) | (lo_byte >> 3);
1249df7305bSSteve Glendinning }
1259df7305bSSteve Glendinning 
1269df7305bSSteve Glendinning static void write_fan_target_to_i2c(struct i2c_client *client, u16 new_target)
1279df7305bSSteve Glendinning {
1289df7305bSSteve Glendinning 	u8 high_byte = (new_target & 0x1fe0) >> 5;
1299df7305bSSteve Glendinning 	u8 low_byte = (new_target & 0x001f) << 3;
1309df7305bSSteve Glendinning 	i2c_smbus_write_byte_data(client, REG_FAN_TARGET_LO, low_byte);
1319df7305bSSteve Glendinning 	i2c_smbus_write_byte_data(client, REG_FAN_TARGET_HI, high_byte);
1329df7305bSSteve Glendinning }
1339df7305bSSteve Glendinning 
1349df7305bSSteve Glendinning static void read_fan_config_from_i2c(struct i2c_client *client)
1359df7305bSSteve Glendinning 
1369df7305bSSteve Glendinning {
1379df7305bSSteve Glendinning 	struct emc2103_data *data = i2c_get_clientdata(client);
1389df7305bSSteve Glendinning 	u8 conf1;
1399df7305bSSteve Glendinning 
1409df7305bSSteve Glendinning 	if (read_u8_from_i2c(client, REG_FAN_CONF1, &conf1) < 0)
1419df7305bSSteve Glendinning 		return;
1429df7305bSSteve Glendinning 
1439df7305bSSteve Glendinning 	data->fan_multiplier = 1 << ((conf1 & 0x60) >> 5);
1449df7305bSSteve Glendinning 	data->fan_rpm_control = (conf1 & 0x80) != 0;
1459df7305bSSteve Glendinning }
1469df7305bSSteve Glendinning 
1479df7305bSSteve Glendinning static struct emc2103_data *emc2103_update_device(struct device *dev)
1489df7305bSSteve Glendinning {
1499df7305bSSteve Glendinning 	struct i2c_client *client = to_i2c_client(dev);
1509df7305bSSteve Glendinning 	struct emc2103_data *data = i2c_get_clientdata(client);
1519df7305bSSteve Glendinning 
1529df7305bSSteve Glendinning 	mutex_lock(&data->update_lock);
1539df7305bSSteve Glendinning 
1549df7305bSSteve Glendinning 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1559df7305bSSteve Glendinning 	    || !data->valid) {
1569df7305bSSteve Glendinning 		int i;
1579df7305bSSteve Glendinning 
1589df7305bSSteve Glendinning 		for (i = 0; i < data->temp_count; i++) {
1599df7305bSSteve Glendinning 			read_temp_from_i2c(client, REG_TEMP[i], &data->temp[i]);
1609df7305bSSteve Glendinning 			read_u8_from_i2c(client, REG_TEMP_MIN[i],
1619df7305bSSteve Glendinning 				&data->temp_min[i]);
1629df7305bSSteve Glendinning 			read_u8_from_i2c(client, REG_TEMP_MAX[i],
1639df7305bSSteve Glendinning 				&data->temp_max[i]);
1649df7305bSSteve Glendinning 		}
1659df7305bSSteve Glendinning 
1669df7305bSSteve Glendinning 		read_u8_from_i2c(client, REG_TEMP_MIN_ALARM,
1679df7305bSSteve Glendinning 			&data->temp_min_alarm);
1689df7305bSSteve Glendinning 		read_u8_from_i2c(client, REG_TEMP_MAX_ALARM,
1699df7305bSSteve Glendinning 			&data->temp_max_alarm);
1709df7305bSSteve Glendinning 
1719df7305bSSteve Glendinning 		read_fan_from_i2c(client, &data->fan_tach,
1729df7305bSSteve Glendinning 			REG_FAN_TACH_HI, REG_FAN_TACH_LO);
1739df7305bSSteve Glendinning 		read_fan_from_i2c(client, &data->fan_target,
1749df7305bSSteve Glendinning 			REG_FAN_TARGET_HI, REG_FAN_TARGET_LO);
1759df7305bSSteve Glendinning 		read_fan_config_from_i2c(client);
1769df7305bSSteve Glendinning 
1779df7305bSSteve Glendinning 		data->last_updated = jiffies;
1789df7305bSSteve Glendinning 		data->valid = true;
1799df7305bSSteve Glendinning 	}
1809df7305bSSteve Glendinning 
1819df7305bSSteve Glendinning 	mutex_unlock(&data->update_lock);
1829df7305bSSteve Glendinning 
1839df7305bSSteve Glendinning 	return data;
1849df7305bSSteve Glendinning }
1859df7305bSSteve Glendinning 
1869df7305bSSteve Glendinning static ssize_t
1879df7305bSSteve Glendinning show_temp(struct device *dev, struct device_attribute *da, char *buf)
1889df7305bSSteve Glendinning {
1899df7305bSSteve Glendinning 	int nr = to_sensor_dev_attr(da)->index;
1909df7305bSSteve Glendinning 	struct emc2103_data *data = emc2103_update_device(dev);
1919df7305bSSteve Glendinning 	int millidegrees = data->temp[nr].degrees * 1000
1929df7305bSSteve Glendinning 		+ data->temp[nr].fraction * 125;
1939df7305bSSteve Glendinning 	return sprintf(buf, "%d\n", millidegrees);
1949df7305bSSteve Glendinning }
1959df7305bSSteve Glendinning 
1969df7305bSSteve Glendinning static ssize_t
1979df7305bSSteve Glendinning show_temp_min(struct device *dev, struct device_attribute *da, char *buf)
1989df7305bSSteve Glendinning {
1999df7305bSSteve Glendinning 	int nr = to_sensor_dev_attr(da)->index;
2009df7305bSSteve Glendinning 	struct emc2103_data *data = emc2103_update_device(dev);
2019df7305bSSteve Glendinning 	int millidegrees = data->temp_min[nr] * 1000;
2029df7305bSSteve Glendinning 	return sprintf(buf, "%d\n", millidegrees);
2039df7305bSSteve Glendinning }
2049df7305bSSteve Glendinning 
2059df7305bSSteve Glendinning static ssize_t
2069df7305bSSteve Glendinning show_temp_max(struct device *dev, struct device_attribute *da, char *buf)
2079df7305bSSteve Glendinning {
2089df7305bSSteve Glendinning 	int nr = to_sensor_dev_attr(da)->index;
2099df7305bSSteve Glendinning 	struct emc2103_data *data = emc2103_update_device(dev);
2109df7305bSSteve Glendinning 	int millidegrees = data->temp_max[nr] * 1000;
2119df7305bSSteve Glendinning 	return sprintf(buf, "%d\n", millidegrees);
2129df7305bSSteve Glendinning }
2139df7305bSSteve Glendinning 
2149df7305bSSteve Glendinning static ssize_t
2159df7305bSSteve Glendinning show_temp_fault(struct device *dev, struct device_attribute *da, char *buf)
2169df7305bSSteve Glendinning {
2179df7305bSSteve Glendinning 	int nr = to_sensor_dev_attr(da)->index;
2189df7305bSSteve Glendinning 	struct emc2103_data *data = emc2103_update_device(dev);
2199df7305bSSteve Glendinning 	bool fault = (data->temp[nr].degrees == -128);
2209df7305bSSteve Glendinning 	return sprintf(buf, "%d\n", fault ? 1 : 0);
2219df7305bSSteve Glendinning }
2229df7305bSSteve Glendinning 
2239df7305bSSteve Glendinning static ssize_t
2249df7305bSSteve Glendinning show_temp_min_alarm(struct device *dev, struct device_attribute *da, char *buf)
2259df7305bSSteve Glendinning {
2269df7305bSSteve Glendinning 	int nr = to_sensor_dev_attr(da)->index;
2279df7305bSSteve Glendinning 	struct emc2103_data *data = emc2103_update_device(dev);
2289df7305bSSteve Glendinning 	bool alarm = data->temp_min_alarm & (1 << nr);
2299df7305bSSteve Glendinning 	return sprintf(buf, "%d\n", alarm ? 1 : 0);
2309df7305bSSteve Glendinning }
2319df7305bSSteve Glendinning 
2329df7305bSSteve Glendinning static ssize_t
2339df7305bSSteve Glendinning show_temp_max_alarm(struct device *dev, struct device_attribute *da, char *buf)
2349df7305bSSteve Glendinning {
2359df7305bSSteve Glendinning 	int nr = to_sensor_dev_attr(da)->index;
2369df7305bSSteve Glendinning 	struct emc2103_data *data = emc2103_update_device(dev);
2379df7305bSSteve Glendinning 	bool alarm = data->temp_max_alarm & (1 << nr);
2389df7305bSSteve Glendinning 	return sprintf(buf, "%d\n", alarm ? 1 : 0);
2399df7305bSSteve Glendinning }
2409df7305bSSteve Glendinning 
2419df7305bSSteve Glendinning static ssize_t set_temp_min(struct device *dev, struct device_attribute *da,
2429df7305bSSteve Glendinning 			    const char *buf, size_t count)
2439df7305bSSteve Glendinning {
2449df7305bSSteve Glendinning 	int nr = to_sensor_dev_attr(da)->index;
2459df7305bSSteve Glendinning 	struct i2c_client *client = to_i2c_client(dev);
2469df7305bSSteve Glendinning 	struct emc2103_data *data = i2c_get_clientdata(client);
2479df7305bSSteve Glendinning 	long val;
2489df7305bSSteve Glendinning 
249179c4fdbSFrans Meulenbroeks 	int result = kstrtol(buf, 10, &val);
2509df7305bSSteve Glendinning 	if (result < 0)
2519df7305bSSteve Glendinning 		return -EINVAL;
2529df7305bSSteve Glendinning 
2539df7305bSSteve Glendinning 	val = DIV_ROUND_CLOSEST(val, 1000);
2549df7305bSSteve Glendinning 	if ((val < -63) || (val > 127))
2559df7305bSSteve Glendinning 		return -EINVAL;
2569df7305bSSteve Glendinning 
2579df7305bSSteve Glendinning 	mutex_lock(&data->update_lock);
2589df7305bSSteve Glendinning 	data->temp_min[nr] = val;
2599df7305bSSteve Glendinning 	i2c_smbus_write_byte_data(client, REG_TEMP_MIN[nr], val);
2609df7305bSSteve Glendinning 	mutex_unlock(&data->update_lock);
2619df7305bSSteve Glendinning 
2629df7305bSSteve Glendinning 	return count;
2639df7305bSSteve Glendinning }
2649df7305bSSteve Glendinning 
2659df7305bSSteve Glendinning static ssize_t set_temp_max(struct device *dev, struct device_attribute *da,
2669df7305bSSteve Glendinning 			    const char *buf, size_t count)
2679df7305bSSteve Glendinning {
2689df7305bSSteve Glendinning 	int nr = to_sensor_dev_attr(da)->index;
2699df7305bSSteve Glendinning 	struct i2c_client *client = to_i2c_client(dev);
2709df7305bSSteve Glendinning 	struct emc2103_data *data = i2c_get_clientdata(client);
2719df7305bSSteve Glendinning 	long val;
2729df7305bSSteve Glendinning 
273179c4fdbSFrans Meulenbroeks 	int result = kstrtol(buf, 10, &val);
2749df7305bSSteve Glendinning 	if (result < 0)
2759df7305bSSteve Glendinning 		return -EINVAL;
2769df7305bSSteve Glendinning 
2779df7305bSSteve Glendinning 	val = DIV_ROUND_CLOSEST(val, 1000);
2789df7305bSSteve Glendinning 	if ((val < -63) || (val > 127))
2799df7305bSSteve Glendinning 		return -EINVAL;
2809df7305bSSteve Glendinning 
2819df7305bSSteve Glendinning 	mutex_lock(&data->update_lock);
2829df7305bSSteve Glendinning 	data->temp_max[nr] = val;
2839df7305bSSteve Glendinning 	i2c_smbus_write_byte_data(client, REG_TEMP_MAX[nr], val);
2849df7305bSSteve Glendinning 	mutex_unlock(&data->update_lock);
2859df7305bSSteve Glendinning 
2869df7305bSSteve Glendinning 	return count;
2879df7305bSSteve Glendinning }
2889df7305bSSteve Glendinning 
2899df7305bSSteve Glendinning static ssize_t
2909df7305bSSteve Glendinning show_fan(struct device *dev, struct device_attribute *da, char *buf)
2919df7305bSSteve Glendinning {
2929df7305bSSteve Glendinning 	struct emc2103_data *data = emc2103_update_device(dev);
2939df7305bSSteve Glendinning 	int rpm = 0;
2949df7305bSSteve Glendinning 	if (data->fan_tach != 0)
2959df7305bSSteve Glendinning 		rpm = (FAN_RPM_FACTOR * data->fan_multiplier) / data->fan_tach;
2969df7305bSSteve Glendinning 	return sprintf(buf, "%d\n", rpm);
2979df7305bSSteve Glendinning }
2989df7305bSSteve Glendinning 
2999df7305bSSteve Glendinning static ssize_t
3009df7305bSSteve Glendinning show_fan_div(struct device *dev, struct device_attribute *da, char *buf)
3019df7305bSSteve Glendinning {
3029df7305bSSteve Glendinning 	struct emc2103_data *data = emc2103_update_device(dev);
3039df7305bSSteve Glendinning 	int fan_div = 8 / data->fan_multiplier;
3049df7305bSSteve Glendinning 	return sprintf(buf, "%d\n", fan_div);
3059df7305bSSteve Glendinning }
3069df7305bSSteve Glendinning 
307bf0f3a04SGuenter Roeck /*
308bf0f3a04SGuenter Roeck  * Note: we also update the fan target here, because its value is
309bf0f3a04SGuenter Roeck  * determined in part by the fan clock divider.  This follows the principle
310bf0f3a04SGuenter Roeck  * of least surprise; the user doesn't expect the fan target to change just
311bf0f3a04SGuenter Roeck  * because the divider changed.
312bf0f3a04SGuenter Roeck  */
3139df7305bSSteve Glendinning static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
3149df7305bSSteve Glendinning 			   const char *buf, size_t count)
3159df7305bSSteve Glendinning {
3169df7305bSSteve Glendinning 	struct emc2103_data *data = emc2103_update_device(dev);
3179df7305bSSteve Glendinning 	struct i2c_client *client = to_i2c_client(dev);
3189df7305bSSteve Glendinning 	int new_range_bits, old_div = 8 / data->fan_multiplier;
3199df7305bSSteve Glendinning 	long new_div;
3209df7305bSSteve Glendinning 
321179c4fdbSFrans Meulenbroeks 	int status = kstrtol(buf, 10, &new_div);
3229df7305bSSteve Glendinning 	if (status < 0)
3239df7305bSSteve Glendinning 		return -EINVAL;
3249df7305bSSteve Glendinning 
3259df7305bSSteve Glendinning 	if (new_div == old_div) /* No change */
3269df7305bSSteve Glendinning 		return count;
3279df7305bSSteve Glendinning 
3289df7305bSSteve Glendinning 	switch (new_div) {
3299df7305bSSteve Glendinning 	case 1:
3309df7305bSSteve Glendinning 		new_range_bits = 3;
3319df7305bSSteve Glendinning 		break;
3329df7305bSSteve Glendinning 	case 2:
3339df7305bSSteve Glendinning 		new_range_bits = 2;
3349df7305bSSteve Glendinning 		break;
3359df7305bSSteve Glendinning 	case 4:
3369df7305bSSteve Glendinning 		new_range_bits = 1;
3379df7305bSSteve Glendinning 		break;
3389df7305bSSteve Glendinning 	case 8:
3399df7305bSSteve Glendinning 		new_range_bits = 0;
3409df7305bSSteve Glendinning 		break;
3419df7305bSSteve Glendinning 	default:
3429df7305bSSteve Glendinning 		return -EINVAL;
3439df7305bSSteve Glendinning 	}
3449df7305bSSteve Glendinning 
3459df7305bSSteve Glendinning 	mutex_lock(&data->update_lock);
3469df7305bSSteve Glendinning 
3479df7305bSSteve Glendinning 	status = i2c_smbus_read_byte_data(client, REG_FAN_CONF1);
3489df7305bSSteve Glendinning 	if (status < 0) {
3499df7305bSSteve Glendinning 		dev_dbg(&client->dev, "reg 0x%02x, err %d\n",
3509df7305bSSteve Glendinning 			REG_FAN_CONF1, status);
3519df7305bSSteve Glendinning 		mutex_unlock(&data->update_lock);
3529df7305bSSteve Glendinning 		return -EIO;
3539df7305bSSteve Glendinning 	}
3549df7305bSSteve Glendinning 	status &= 0x9F;
3559df7305bSSteve Glendinning 	status |= (new_range_bits << 5);
3569df7305bSSteve Glendinning 	i2c_smbus_write_byte_data(client, REG_FAN_CONF1, status);
3579df7305bSSteve Glendinning 
3589df7305bSSteve Glendinning 	data->fan_multiplier = 8 / new_div;
3599df7305bSSteve Glendinning 
3609df7305bSSteve Glendinning 	/* update fan target if high byte is not disabled */
3619df7305bSSteve Glendinning 	if ((data->fan_target & 0x1fe0) != 0x1fe0) {
3629df7305bSSteve Glendinning 		u16 new_target = (data->fan_target * old_div) / new_div;
3639df7305bSSteve Glendinning 		data->fan_target = min(new_target, (u16)0x1fff);
3649df7305bSSteve Glendinning 		write_fan_target_to_i2c(client, data->fan_target);
3659df7305bSSteve Glendinning 	}
3669df7305bSSteve Glendinning 
3679df7305bSSteve Glendinning 	/* invalidate data to force re-read from hardware */
3689df7305bSSteve Glendinning 	data->valid = false;
3699df7305bSSteve Glendinning 
3709df7305bSSteve Glendinning 	mutex_unlock(&data->update_lock);
3719df7305bSSteve Glendinning 	return count;
3729df7305bSSteve Glendinning }
3739df7305bSSteve Glendinning 
3749df7305bSSteve Glendinning static ssize_t
3759df7305bSSteve Glendinning show_fan_target(struct device *dev, struct device_attribute *da, char *buf)
3769df7305bSSteve Glendinning {
3779df7305bSSteve Glendinning 	struct emc2103_data *data = emc2103_update_device(dev);
3789df7305bSSteve Glendinning 	int rpm = 0;
3799df7305bSSteve Glendinning 
3809df7305bSSteve Glendinning 	/* high byte of 0xff indicates disabled so return 0 */
3819df7305bSSteve Glendinning 	if ((data->fan_target != 0) && ((data->fan_target & 0x1fe0) != 0x1fe0))
3829df7305bSSteve Glendinning 		rpm = (FAN_RPM_FACTOR * data->fan_multiplier)
3839df7305bSSteve Glendinning 			/ data->fan_target;
3849df7305bSSteve Glendinning 
3859df7305bSSteve Glendinning 	return sprintf(buf, "%d\n", rpm);
3869df7305bSSteve Glendinning }
3879df7305bSSteve Glendinning 
3889df7305bSSteve Glendinning static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
3899df7305bSSteve Glendinning 			      const char *buf, size_t count)
3909df7305bSSteve Glendinning {
3919df7305bSSteve Glendinning 	struct emc2103_data *data = emc2103_update_device(dev);
3929df7305bSSteve Glendinning 	struct i2c_client *client = to_i2c_client(dev);
3939df7305bSSteve Glendinning 	long rpm_target;
3949df7305bSSteve Glendinning 
395179c4fdbSFrans Meulenbroeks 	int result = kstrtol(buf, 10, &rpm_target);
3969df7305bSSteve Glendinning 	if (result < 0)
3979df7305bSSteve Glendinning 		return -EINVAL;
3989df7305bSSteve Glendinning 
3999df7305bSSteve Glendinning 	/* Datasheet states 16384 as maximum RPM target (table 3.2) */
4009df7305bSSteve Glendinning 	if ((rpm_target < 0) || (rpm_target > 16384))
4019df7305bSSteve Glendinning 		return -EINVAL;
4029df7305bSSteve Glendinning 
4039df7305bSSteve Glendinning 	mutex_lock(&data->update_lock);
4049df7305bSSteve Glendinning 
4059df7305bSSteve Glendinning 	if (rpm_target == 0)
4069df7305bSSteve Glendinning 		data->fan_target = 0x1fff;
4079df7305bSSteve Glendinning 	else
4089df7305bSSteve Glendinning 		data->fan_target = SENSORS_LIMIT(
4099df7305bSSteve Glendinning 			(FAN_RPM_FACTOR * data->fan_multiplier) / rpm_target,
4109df7305bSSteve Glendinning 			0, 0x1fff);
4119df7305bSSteve Glendinning 
4129df7305bSSteve Glendinning 	write_fan_target_to_i2c(client, data->fan_target);
4139df7305bSSteve Glendinning 
4149df7305bSSteve Glendinning 	mutex_unlock(&data->update_lock);
4159df7305bSSteve Glendinning 	return count;
4169df7305bSSteve Glendinning }
4179df7305bSSteve Glendinning 
4189df7305bSSteve Glendinning static ssize_t
4199df7305bSSteve Glendinning show_fan_fault(struct device *dev, struct device_attribute *da, char *buf)
4209df7305bSSteve Glendinning {
4219df7305bSSteve Glendinning 	struct emc2103_data *data = emc2103_update_device(dev);
4229df7305bSSteve Glendinning 	bool fault = ((data->fan_tach & 0x1fe0) == 0x1fe0);
4239df7305bSSteve Glendinning 	return sprintf(buf, "%d\n", fault ? 1 : 0);
4249df7305bSSteve Glendinning }
4259df7305bSSteve Glendinning 
4269df7305bSSteve Glendinning static ssize_t
4279df7305bSSteve Glendinning show_pwm_enable(struct device *dev, struct device_attribute *da, char *buf)
4289df7305bSSteve Glendinning {
4299df7305bSSteve Glendinning 	struct emc2103_data *data = emc2103_update_device(dev);
4309df7305bSSteve Glendinning 	return sprintf(buf, "%d\n", data->fan_rpm_control ? 3 : 0);
4319df7305bSSteve Glendinning }
4329df7305bSSteve Glendinning 
4339df7305bSSteve Glendinning static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da,
4349df7305bSSteve Glendinning 			      const char *buf, size_t count)
4359df7305bSSteve Glendinning {
4369df7305bSSteve Glendinning 	struct i2c_client *client = to_i2c_client(dev);
4379df7305bSSteve Glendinning 	struct emc2103_data *data = i2c_get_clientdata(client);
4389df7305bSSteve Glendinning 	long new_value;
4399df7305bSSteve Glendinning 	u8 conf_reg;
4409df7305bSSteve Glendinning 
441179c4fdbSFrans Meulenbroeks 	int result = kstrtol(buf, 10, &new_value);
4429df7305bSSteve Glendinning 	if (result < 0)
4439df7305bSSteve Glendinning 		return -EINVAL;
4449df7305bSSteve Glendinning 
4459df7305bSSteve Glendinning 	mutex_lock(&data->update_lock);
4469df7305bSSteve Glendinning 	switch (new_value) {
4479df7305bSSteve Glendinning 	case 0:
4489df7305bSSteve Glendinning 		data->fan_rpm_control = false;
4499df7305bSSteve Glendinning 		break;
4509df7305bSSteve Glendinning 	case 3:
4519df7305bSSteve Glendinning 		data->fan_rpm_control = true;
4529df7305bSSteve Glendinning 		break;
4539df7305bSSteve Glendinning 	default:
454*2355375eSGuenter Roeck 		count = -EINVAL;
455*2355375eSGuenter Roeck 		goto err;
4569df7305bSSteve Glendinning 	}
4579df7305bSSteve Glendinning 
458*2355375eSGuenter Roeck 	result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
459*2355375eSGuenter Roeck 	if (result) {
460*2355375eSGuenter Roeck 		count = result;
461*2355375eSGuenter Roeck 		goto err;
462*2355375eSGuenter Roeck 	}
4639df7305bSSteve Glendinning 
4649df7305bSSteve Glendinning 	if (data->fan_rpm_control)
4659df7305bSSteve Glendinning 		conf_reg |= 0x80;
4669df7305bSSteve Glendinning 	else
4679df7305bSSteve Glendinning 		conf_reg &= ~0x80;
4689df7305bSSteve Glendinning 
4699df7305bSSteve Glendinning 	i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg);
470*2355375eSGuenter Roeck err:
4719df7305bSSteve Glendinning 	mutex_unlock(&data->update_lock);
4729df7305bSSteve Glendinning 	return count;
4739df7305bSSteve Glendinning }
4749df7305bSSteve Glendinning 
4759df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
4769df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, show_temp_min,
4779df7305bSSteve Glendinning 	set_temp_min, 0);
4789df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
4799df7305bSSteve Glendinning 	set_temp_max, 0);
4809df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
4819df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_temp_min_alarm,
4829df7305bSSteve Glendinning 	NULL, 0);
4839df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_temp_max_alarm,
4849df7305bSSteve Glendinning 	NULL, 0);
4859df7305bSSteve Glendinning 
4869df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
4879df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, show_temp_min,
4889df7305bSSteve Glendinning 	set_temp_min, 1);
4899df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
4909df7305bSSteve Glendinning 	set_temp_max, 1);
4919df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
4929df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_temp_min_alarm,
4939df7305bSSteve Glendinning 	NULL, 1);
4949df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_temp_max_alarm,
4959df7305bSSteve Glendinning 	NULL, 1);
4969df7305bSSteve Glendinning 
4979df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
4989df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR, show_temp_min,
4999df7305bSSteve Glendinning 	set_temp_min, 2);
5009df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
5019df7305bSSteve Glendinning 	set_temp_max, 2);
5029df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2);
5039df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_temp_min_alarm,
5049df7305bSSteve Glendinning 	NULL, 2);
5059df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_temp_max_alarm,
5069df7305bSSteve Glendinning 	NULL, 2);
5079df7305bSSteve Glendinning 
5089df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
5099df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO | S_IWUSR, show_temp_min,
5109df7305bSSteve Glendinning 	set_temp_min, 3);
5119df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
5129df7305bSSteve Glendinning 	set_temp_max, 3);
5139df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3);
5149df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_temp_min_alarm,
5159df7305bSSteve Glendinning 	NULL, 3);
5169df7305bSSteve Glendinning static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_temp_max_alarm,
5179df7305bSSteve Glendinning 	NULL, 3);
5189df7305bSSteve Glendinning 
5199df7305bSSteve Glendinning static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL);
5209df7305bSSteve Glendinning static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div, set_fan_div);
5219df7305bSSteve Glendinning static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, show_fan_target,
5229df7305bSSteve Glendinning 	set_fan_target);
5239df7305bSSteve Glendinning static DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL);
5249df7305bSSteve Glendinning 
5259df7305bSSteve Glendinning static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
5269df7305bSSteve Glendinning 	set_pwm_enable);
5279df7305bSSteve Glendinning 
5289df7305bSSteve Glendinning /* sensors present on all models */
5299df7305bSSteve Glendinning static struct attribute *emc2103_attributes[] = {
5309df7305bSSteve Glendinning 	&sensor_dev_attr_temp1_input.dev_attr.attr,
5319df7305bSSteve Glendinning 	&sensor_dev_attr_temp1_min.dev_attr.attr,
5329df7305bSSteve Glendinning 	&sensor_dev_attr_temp1_max.dev_attr.attr,
5339df7305bSSteve Glendinning 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
5349df7305bSSteve Glendinning 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
5359df7305bSSteve Glendinning 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
5369df7305bSSteve Glendinning 	&sensor_dev_attr_temp2_input.dev_attr.attr,
5379df7305bSSteve Glendinning 	&sensor_dev_attr_temp2_min.dev_attr.attr,
5389df7305bSSteve Glendinning 	&sensor_dev_attr_temp2_max.dev_attr.attr,
5399df7305bSSteve Glendinning 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
5409df7305bSSteve Glendinning 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
5419df7305bSSteve Glendinning 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
5429df7305bSSteve Glendinning 	&dev_attr_fan1_input.attr,
5439df7305bSSteve Glendinning 	&dev_attr_fan1_div.attr,
5449df7305bSSteve Glendinning 	&dev_attr_fan1_target.attr,
5459df7305bSSteve Glendinning 	&dev_attr_fan1_fault.attr,
5469df7305bSSteve Glendinning 	&dev_attr_pwm1_enable.attr,
5479df7305bSSteve Glendinning 	NULL
5489df7305bSSteve Glendinning };
5499df7305bSSteve Glendinning 
5509df7305bSSteve Glendinning /* extra temperature sensors only present on 2103-2 and 2103-4 */
5519df7305bSSteve Glendinning static struct attribute *emc2103_attributes_temp3[] = {
5529df7305bSSteve Glendinning 	&sensor_dev_attr_temp3_input.dev_attr.attr,
5539df7305bSSteve Glendinning 	&sensor_dev_attr_temp3_min.dev_attr.attr,
5549df7305bSSteve Glendinning 	&sensor_dev_attr_temp3_max.dev_attr.attr,
5559df7305bSSteve Glendinning 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
5569df7305bSSteve Glendinning 	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
5579df7305bSSteve Glendinning 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
5589df7305bSSteve Glendinning 	NULL
5599df7305bSSteve Glendinning };
5609df7305bSSteve Glendinning 
5619df7305bSSteve Glendinning /* extra temperature sensors only present on 2103-2 and 2103-4 in APD mode */
5629df7305bSSteve Glendinning static struct attribute *emc2103_attributes_temp4[] = {
5639df7305bSSteve Glendinning 	&sensor_dev_attr_temp4_input.dev_attr.attr,
5649df7305bSSteve Glendinning 	&sensor_dev_attr_temp4_min.dev_attr.attr,
5659df7305bSSteve Glendinning 	&sensor_dev_attr_temp4_max.dev_attr.attr,
5669df7305bSSteve Glendinning 	&sensor_dev_attr_temp4_fault.dev_attr.attr,
5679df7305bSSteve Glendinning 	&sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
5689df7305bSSteve Glendinning 	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
5699df7305bSSteve Glendinning 	NULL
5709df7305bSSteve Glendinning };
5719df7305bSSteve Glendinning 
5729df7305bSSteve Glendinning static const struct attribute_group emc2103_group = {
5739df7305bSSteve Glendinning 	.attrs = emc2103_attributes,
5749df7305bSSteve Glendinning };
5759df7305bSSteve Glendinning 
5769df7305bSSteve Glendinning static const struct attribute_group emc2103_temp3_group = {
5779df7305bSSteve Glendinning 	.attrs = emc2103_attributes_temp3,
5789df7305bSSteve Glendinning };
5799df7305bSSteve Glendinning 
5809df7305bSSteve Glendinning static const struct attribute_group emc2103_temp4_group = {
5819df7305bSSteve Glendinning 	.attrs = emc2103_attributes_temp4,
5829df7305bSSteve Glendinning };
5839df7305bSSteve Glendinning 
5849df7305bSSteve Glendinning static int
5859df7305bSSteve Glendinning emc2103_probe(struct i2c_client *client, const struct i2c_device_id *id)
5869df7305bSSteve Glendinning {
5879df7305bSSteve Glendinning 	struct emc2103_data *data;
5889df7305bSSteve Glendinning 	int status;
5899df7305bSSteve Glendinning 
5909df7305bSSteve Glendinning 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
5919df7305bSSteve Glendinning 		return -EIO;
5929df7305bSSteve Glendinning 
5939df7305bSSteve Glendinning 	data = kzalloc(sizeof(struct emc2103_data), GFP_KERNEL);
5949df7305bSSteve Glendinning 	if (!data)
5959df7305bSSteve Glendinning 		return -ENOMEM;
5969df7305bSSteve Glendinning 
5979df7305bSSteve Glendinning 	i2c_set_clientdata(client, data);
5989df7305bSSteve Glendinning 	mutex_init(&data->update_lock);
5999df7305bSSteve Glendinning 
6009df7305bSSteve Glendinning 	/* 2103-2 and 2103-4 have 3 external diodes, 2103-1 has 1 */
6019df7305bSSteve Glendinning 	status = i2c_smbus_read_byte_data(client, REG_PRODUCT_ID);
6029df7305bSSteve Glendinning 	if (status == 0x24) {
6039df7305bSSteve Glendinning 		/* 2103-1 only has 1 external diode */
6049df7305bSSteve Glendinning 		data->temp_count = 2;
6059df7305bSSteve Glendinning 	} else {
6069df7305bSSteve Glendinning 		/* 2103-2 and 2103-4 have 3 or 4 external diodes */
6079df7305bSSteve Glendinning 		status = i2c_smbus_read_byte_data(client, REG_CONF1);
6089df7305bSSteve Glendinning 		if (status < 0) {
6099df7305bSSteve Glendinning 			dev_dbg(&client->dev, "reg 0x%02x, err %d\n", REG_CONF1,
6109df7305bSSteve Glendinning 				status);
6119df7305bSSteve Glendinning 			goto exit_free;
6129df7305bSSteve Glendinning 		}
6139df7305bSSteve Glendinning 
6149df7305bSSteve Glendinning 		/* detect current state of hardware */
6159df7305bSSteve Glendinning 		data->temp_count = (status & 0x01) ? 4 : 3;
6169df7305bSSteve Glendinning 
6179df7305bSSteve Glendinning 		/* force APD state if module parameter is set */
6189df7305bSSteve Glendinning 		if (apd == 0) {
6199df7305bSSteve Glendinning 			/* force APD mode off */
6209df7305bSSteve Glendinning 			data->temp_count = 3;
6219df7305bSSteve Glendinning 			status &= ~(0x01);
6229df7305bSSteve Glendinning 			i2c_smbus_write_byte_data(client, REG_CONF1, status);
6239df7305bSSteve Glendinning 		} else if (apd == 1) {
6249df7305bSSteve Glendinning 			/* force APD mode on */
6259df7305bSSteve Glendinning 			data->temp_count = 4;
6269df7305bSSteve Glendinning 			status |= 0x01;
6279df7305bSSteve Glendinning 			i2c_smbus_write_byte_data(client, REG_CONF1, status);
6289df7305bSSteve Glendinning 		}
6299df7305bSSteve Glendinning 	}
6309df7305bSSteve Glendinning 
6319df7305bSSteve Glendinning 	/* Register sysfs hooks */
6329df7305bSSteve Glendinning 	status = sysfs_create_group(&client->dev.kobj, &emc2103_group);
6339df7305bSSteve Glendinning 	if (status)
6349df7305bSSteve Glendinning 		goto exit_free;
6359df7305bSSteve Glendinning 
6369df7305bSSteve Glendinning 	if (data->temp_count >= 3) {
6379df7305bSSteve Glendinning 		status = sysfs_create_group(&client->dev.kobj,
6389df7305bSSteve Glendinning 			&emc2103_temp3_group);
6399df7305bSSteve Glendinning 		if (status)
6409df7305bSSteve Glendinning 			goto exit_remove;
6419df7305bSSteve Glendinning 	}
6429df7305bSSteve Glendinning 
6439df7305bSSteve Glendinning 	if (data->temp_count == 4) {
6449df7305bSSteve Glendinning 		status = sysfs_create_group(&client->dev.kobj,
6459df7305bSSteve Glendinning 			&emc2103_temp4_group);
6469df7305bSSteve Glendinning 		if (status)
6479df7305bSSteve Glendinning 			goto exit_remove_temp3;
6489df7305bSSteve Glendinning 	}
6499df7305bSSteve Glendinning 
6509df7305bSSteve Glendinning 	data->hwmon_dev = hwmon_device_register(&client->dev);
6519df7305bSSteve Glendinning 	if (IS_ERR(data->hwmon_dev)) {
6529df7305bSSteve Glendinning 		status = PTR_ERR(data->hwmon_dev);
6539df7305bSSteve Glendinning 		goto exit_remove_temp4;
6549df7305bSSteve Glendinning 	}
6559df7305bSSteve Glendinning 
6569df7305bSSteve Glendinning 	dev_info(&client->dev, "%s: sensor '%s'\n",
6579df7305bSSteve Glendinning 		 dev_name(data->hwmon_dev), client->name);
6589df7305bSSteve Glendinning 
6599df7305bSSteve Glendinning 	return 0;
6609df7305bSSteve Glendinning 
6619df7305bSSteve Glendinning exit_remove_temp4:
6629df7305bSSteve Glendinning 	if (data->temp_count == 4)
6639df7305bSSteve Glendinning 		sysfs_remove_group(&client->dev.kobj, &emc2103_temp4_group);
6649df7305bSSteve Glendinning exit_remove_temp3:
6659df7305bSSteve Glendinning 	if (data->temp_count >= 3)
6669df7305bSSteve Glendinning 		sysfs_remove_group(&client->dev.kobj, &emc2103_temp3_group);
6679df7305bSSteve Glendinning exit_remove:
6689df7305bSSteve Glendinning 	sysfs_remove_group(&client->dev.kobj, &emc2103_group);
6699df7305bSSteve Glendinning exit_free:
6709df7305bSSteve Glendinning 	kfree(data);
6719df7305bSSteve Glendinning 	return status;
6729df7305bSSteve Glendinning }
6739df7305bSSteve Glendinning 
6749df7305bSSteve Glendinning static int emc2103_remove(struct i2c_client *client)
6759df7305bSSteve Glendinning {
6769df7305bSSteve Glendinning 	struct emc2103_data *data = i2c_get_clientdata(client);
6779df7305bSSteve Glendinning 
6789df7305bSSteve Glendinning 	hwmon_device_unregister(data->hwmon_dev);
6799df7305bSSteve Glendinning 
6809df7305bSSteve Glendinning 	if (data->temp_count == 4)
6819df7305bSSteve Glendinning 		sysfs_remove_group(&client->dev.kobj, &emc2103_temp4_group);
6829df7305bSSteve Glendinning 
6839df7305bSSteve Glendinning 	if (data->temp_count >= 3)
6849df7305bSSteve Glendinning 		sysfs_remove_group(&client->dev.kobj, &emc2103_temp3_group);
6859df7305bSSteve Glendinning 
6869df7305bSSteve Glendinning 	sysfs_remove_group(&client->dev.kobj, &emc2103_group);
6879df7305bSSteve Glendinning 
6889df7305bSSteve Glendinning 	kfree(data);
6899df7305bSSteve Glendinning 	return 0;
6909df7305bSSteve Glendinning }
6919df7305bSSteve Glendinning 
6929df7305bSSteve Glendinning static const struct i2c_device_id emc2103_ids[] = {
6939df7305bSSteve Glendinning 	{ "emc2103", 0, },
6949df7305bSSteve Glendinning 	{ /* LIST END */ }
6959df7305bSSteve Glendinning };
6969df7305bSSteve Glendinning MODULE_DEVICE_TABLE(i2c, emc2103_ids);
6979df7305bSSteve Glendinning 
6989df7305bSSteve Glendinning /* Return 0 if detection is successful, -ENODEV otherwise */
6999df7305bSSteve Glendinning static int
7009df7305bSSteve Glendinning emc2103_detect(struct i2c_client *new_client, struct i2c_board_info *info)
7019df7305bSSteve Glendinning {
7029df7305bSSteve Glendinning 	struct i2c_adapter *adapter = new_client->adapter;
7039df7305bSSteve Glendinning 	int manufacturer, product;
7049df7305bSSteve Glendinning 
7059df7305bSSteve Glendinning 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
7069df7305bSSteve Glendinning 		return -ENODEV;
7079df7305bSSteve Glendinning 
7089df7305bSSteve Glendinning 	manufacturer = i2c_smbus_read_byte_data(new_client, REG_MFG_ID);
7099df7305bSSteve Glendinning 	if (manufacturer != 0x5D)
7109df7305bSSteve Glendinning 		return -ENODEV;
7119df7305bSSteve Glendinning 
7129df7305bSSteve Glendinning 	product = i2c_smbus_read_byte_data(new_client, REG_PRODUCT_ID);
7139df7305bSSteve Glendinning 	if ((product != 0x24) && (product != 0x26))
7149df7305bSSteve Glendinning 		return -ENODEV;
7159df7305bSSteve Glendinning 
7169df7305bSSteve Glendinning 	strlcpy(info->type, "emc2103", I2C_NAME_SIZE);
7179df7305bSSteve Glendinning 
7189df7305bSSteve Glendinning 	return 0;
7199df7305bSSteve Glendinning }
7209df7305bSSteve Glendinning 
7219df7305bSSteve Glendinning static struct i2c_driver emc2103_driver = {
7229df7305bSSteve Glendinning 	.class		= I2C_CLASS_HWMON,
7239df7305bSSteve Glendinning 	.driver = {
7249df7305bSSteve Glendinning 		.name	= "emc2103",
7259df7305bSSteve Glendinning 	},
7269df7305bSSteve Glendinning 	.probe		= emc2103_probe,
7279df7305bSSteve Glendinning 	.remove		= emc2103_remove,
7289df7305bSSteve Glendinning 	.id_table	= emc2103_ids,
7299df7305bSSteve Glendinning 	.detect		= emc2103_detect,
7309df7305bSSteve Glendinning 	.address_list	= normal_i2c,
7319df7305bSSteve Glendinning };
7329df7305bSSteve Glendinning 
733f0967eeaSAxel Lin module_i2c_driver(emc2103_driver);
7349df7305bSSteve Glendinning 
7359df7305bSSteve Glendinning MODULE_AUTHOR("Steve Glendinning <steve.glendinning@smsc.com>");
7369df7305bSSteve Glendinning MODULE_DESCRIPTION("SMSC EMC2103 hwmon driver");
7379df7305bSSteve Glendinning MODULE_LICENSE("GPL");
738