| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 1 | /* | 
 | 2 |  * max6639.c - Support for Maxim MAX6639 | 
 | 3 |  * | 
 | 4 |  * 2-Channel Temperature Monitor with Dual PWM Fan-Speed Controller | 
 | 5 |  * | 
 | 6 |  * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de> | 
 | 7 |  * | 
 | 8 |  * based on the initial MAX6639 support from semptian.net | 
 | 9 |  * by He Changqing <hechangqing@semptian.com> | 
 | 10 |  * | 
 | 11 |  * This program is free software; you can redistribute it and/or modify | 
 | 12 |  * it under the terms of the GNU General Public License as published by | 
 | 13 |  * the Free Software Foundation; either version 2 of the License, or | 
 | 14 |  * (at your option) any later version. | 
 | 15 |  * | 
 | 16 |  * This program is distributed in the hope that it will be useful, | 
 | 17 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 18 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 19 |  * GNU General Public License for more details. | 
 | 20 |  * | 
 | 21 |  * You should have received a copy of the GNU General Public License | 
 | 22 |  * along with this program; if not, write to the Free Software | 
 | 23 |  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
 | 24 |  */ | 
 | 25 |  | 
 | 26 | #include <linux/module.h> | 
 | 27 | #include <linux/init.h> | 
 | 28 | #include <linux/slab.h> | 
 | 29 | #include <linux/jiffies.h> | 
 | 30 | #include <linux/i2c.h> | 
 | 31 | #include <linux/hwmon.h> | 
 | 32 | #include <linux/hwmon-sysfs.h> | 
 | 33 | #include <linux/err.h> | 
 | 34 | #include <linux/mutex.h> | 
 | 35 | #include <linux/i2c/max6639.h> | 
 | 36 |  | 
 | 37 | /* Addresses to scan */ | 
 | 38 | static unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END }; | 
 | 39 |  | 
 | 40 | /* The MAX6639 registers, valid channel numbers: 0, 1 */ | 
 | 41 | #define MAX6639_REG_TEMP(ch)			(0x00 + (ch)) | 
 | 42 | #define MAX6639_REG_STATUS			0x02 | 
 | 43 | #define MAX6639_REG_OUTPUT_MASK			0x03 | 
 | 44 | #define MAX6639_REG_GCONFIG			0x04 | 
 | 45 | #define MAX6639_REG_TEMP_EXT(ch)		(0x05 + (ch)) | 
 | 46 | #define MAX6639_REG_ALERT_LIMIT(ch)		(0x08 + (ch)) | 
 | 47 | #define MAX6639_REG_OT_LIMIT(ch)		(0x0A + (ch)) | 
 | 48 | #define MAX6639_REG_THERM_LIMIT(ch)		(0x0C + (ch)) | 
 | 49 | #define MAX6639_REG_FAN_CONFIG1(ch)		(0x10 + (ch) * 4) | 
 | 50 | #define MAX6639_REG_FAN_CONFIG2a(ch)		(0x11 + (ch) * 4) | 
 | 51 | #define MAX6639_REG_FAN_CONFIG2b(ch)		(0x12 + (ch) * 4) | 
 | 52 | #define MAX6639_REG_FAN_CONFIG3(ch)		(0x13 + (ch) * 4) | 
 | 53 | #define MAX6639_REG_FAN_CNT(ch)			(0x20 + (ch)) | 
 | 54 | #define MAX6639_REG_TARGET_CNT(ch)		(0x22 + (ch)) | 
 | 55 | #define MAX6639_REG_FAN_PPR(ch)			(0x24 + (ch)) | 
 | 56 | #define MAX6639_REG_TARGTDUTY(ch)		(0x26 + (ch)) | 
 | 57 | #define MAX6639_REG_FAN_START_TEMP(ch)		(0x28 + (ch)) | 
 | 58 | #define MAX6639_REG_DEVID			0x3D | 
 | 59 | #define MAX6639_REG_MANUID			0x3E | 
 | 60 | #define MAX6639_REG_DEVREV			0x3F | 
 | 61 |  | 
 | 62 | /* Register bits */ | 
 | 63 | #define MAX6639_GCONFIG_STANDBY			0x80 | 
 | 64 | #define MAX6639_GCONFIG_POR			0x40 | 
 | 65 | #define MAX6639_GCONFIG_DISABLE_TIMEOUT		0x20 | 
 | 66 | #define MAX6639_GCONFIG_CH2_LOCAL		0x10 | 
| stigge@antcom.de | 177f3b9 | 2011-01-29 17:04:01 +0100 | [diff] [blame] | 67 | #define MAX6639_GCONFIG_PWM_FREQ_HI		0x08 | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 68 |  | 
 | 69 | #define MAX6639_FAN_CONFIG1_PWM			0x80 | 
 | 70 |  | 
| stigge@antcom.de | 177f3b9 | 2011-01-29 17:04:01 +0100 | [diff] [blame] | 71 | #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED	0x40 | 
 | 72 |  | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 73 | static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 }; | 
 | 74 |  | 
| Chris D Schimp | b63d97a | 2012-02-20 16:59:24 -0500 | [diff] [blame] | 75 | #define FAN_FROM_REG(val, rpm_range)	((val) == 0 || (val) == 255 ? \ | 
 | 76 | 				0 : (rpm_ranges[rpm_range] * 30) / (val)) | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 77 | #define TEMP_LIMIT_TO_REG(val)	SENSORS_LIMIT((val) / 1000, 0, 255) | 
 | 78 |  | 
 | 79 | /* | 
 | 80 |  * Client data (each client gets its own) | 
 | 81 |  */ | 
 | 82 | struct max6639_data { | 
 | 83 | 	struct device *hwmon_dev; | 
 | 84 | 	struct mutex update_lock; | 
 | 85 | 	char valid;		/* !=0 if following fields are valid */ | 
 | 86 | 	unsigned long last_updated;	/* In jiffies */ | 
 | 87 |  | 
 | 88 | 	/* Register values sampled regularly */ | 
 | 89 | 	u16 temp[2];		/* Temperature, in 1/8 C, 0..255 C */ | 
 | 90 | 	bool temp_fault[2];	/* Detected temperature diode failure */ | 
 | 91 | 	u8 fan[2];		/* Register value: TACH count for fans >=30 */ | 
 | 92 | 	u8 status;		/* Detected channel alarms and fan failures */ | 
 | 93 |  | 
 | 94 | 	/* Register values only written to */ | 
 | 95 | 	u8 pwm[2];		/* Register value: Duty cycle 0..120 */ | 
 | 96 | 	u8 temp_therm[2];	/* THERM Temperature, 0..255 C (->_max) */ | 
 | 97 | 	u8 temp_alert[2];	/* ALERT Temperature, 0..255 C (->_crit) */ | 
 | 98 | 	u8 temp_ot[2];		/* OT Temperature, 0..255 C (->_emergency) */ | 
 | 99 |  | 
 | 100 | 	/* Register values initialized only once */ | 
 | 101 | 	u8 ppr;			/* Pulses per rotation 0..3 for 1..4 ppr */ | 
 | 102 | 	u8 rpm_range;		/* Index in above rpm_ranges table */ | 
 | 103 | }; | 
 | 104 |  | 
 | 105 | static struct max6639_data *max6639_update_device(struct device *dev) | 
 | 106 | { | 
 | 107 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 108 | 	struct max6639_data *data = i2c_get_clientdata(client); | 
 | 109 | 	struct max6639_data *ret = data; | 
 | 110 | 	int i; | 
 | 111 | 	int status_reg; | 
 | 112 |  | 
 | 113 | 	mutex_lock(&data->update_lock); | 
 | 114 |  | 
 | 115 | 	if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { | 
 | 116 | 		int res; | 
 | 117 |  | 
 | 118 | 		dev_dbg(&client->dev, "Starting max6639 update\n"); | 
 | 119 |  | 
 | 120 | 		status_reg = i2c_smbus_read_byte_data(client, | 
 | 121 | 						      MAX6639_REG_STATUS); | 
 | 122 | 		if (status_reg < 0) { | 
 | 123 | 			ret = ERR_PTR(status_reg); | 
 | 124 | 			goto abort; | 
 | 125 | 		} | 
 | 126 |  | 
 | 127 | 		data->status = status_reg; | 
 | 128 |  | 
 | 129 | 		for (i = 0; i < 2; i++) { | 
 | 130 | 			res = i2c_smbus_read_byte_data(client, | 
 | 131 | 					MAX6639_REG_FAN_CNT(i)); | 
 | 132 | 			if (res < 0) { | 
 | 133 | 				ret = ERR_PTR(res); | 
 | 134 | 				goto abort; | 
 | 135 | 			} | 
 | 136 | 			data->fan[i] = res; | 
 | 137 |  | 
 | 138 | 			res = i2c_smbus_read_byte_data(client, | 
 | 139 | 					MAX6639_REG_TEMP_EXT(i)); | 
 | 140 | 			if (res < 0) { | 
 | 141 | 				ret = ERR_PTR(res); | 
 | 142 | 				goto abort; | 
 | 143 | 			} | 
 | 144 | 			data->temp[i] = res >> 5; | 
 | 145 | 			data->temp_fault[i] = res & 0x01; | 
 | 146 |  | 
 | 147 | 			res = i2c_smbus_read_byte_data(client, | 
 | 148 | 					MAX6639_REG_TEMP(i)); | 
 | 149 | 			if (res < 0) { | 
 | 150 | 				ret = ERR_PTR(res); | 
 | 151 | 				goto abort; | 
 | 152 | 			} | 
 | 153 | 			data->temp[i] |= res << 3; | 
 | 154 | 		} | 
 | 155 |  | 
 | 156 | 		data->last_updated = jiffies; | 
 | 157 | 		data->valid = 1; | 
 | 158 | 	} | 
 | 159 | abort: | 
 | 160 | 	mutex_unlock(&data->update_lock); | 
 | 161 |  | 
 | 162 | 	return ret; | 
 | 163 | } | 
 | 164 |  | 
 | 165 | static ssize_t show_temp_input(struct device *dev, | 
 | 166 | 			       struct device_attribute *dev_attr, char *buf) | 
 | 167 | { | 
 | 168 | 	long temp; | 
 | 169 | 	struct max6639_data *data = max6639_update_device(dev); | 
 | 170 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 
 | 171 |  | 
 | 172 | 	if (IS_ERR(data)) | 
 | 173 | 		return PTR_ERR(data); | 
 | 174 |  | 
 | 175 | 	temp = data->temp[attr->index] * 125; | 
 | 176 | 	return sprintf(buf, "%ld\n", temp); | 
 | 177 | } | 
 | 178 |  | 
 | 179 | static ssize_t show_temp_fault(struct device *dev, | 
 | 180 | 			       struct device_attribute *dev_attr, char *buf) | 
 | 181 | { | 
 | 182 | 	struct max6639_data *data = max6639_update_device(dev); | 
 | 183 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 
 | 184 |  | 
 | 185 | 	if (IS_ERR(data)) | 
 | 186 | 		return PTR_ERR(data); | 
 | 187 |  | 
 | 188 | 	return sprintf(buf, "%d\n", data->temp_fault[attr->index]); | 
 | 189 | } | 
 | 190 |  | 
 | 191 | static ssize_t show_temp_max(struct device *dev, | 
 | 192 | 			     struct device_attribute *dev_attr, char *buf) | 
 | 193 | { | 
 | 194 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 195 | 	struct max6639_data *data = i2c_get_clientdata(client); | 
 | 196 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 
 | 197 |  | 
 | 198 | 	return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000)); | 
 | 199 | } | 
 | 200 |  | 
 | 201 | static ssize_t set_temp_max(struct device *dev, | 
 | 202 | 			    struct device_attribute *dev_attr, | 
 | 203 | 			    const char *buf, size_t count) | 
 | 204 | { | 
 | 205 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 206 | 	struct max6639_data *data = i2c_get_clientdata(client); | 
 | 207 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 
 | 208 | 	unsigned long val; | 
 | 209 | 	int res; | 
 | 210 |  | 
| Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 211 | 	res = kstrtoul(buf, 10, &val); | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 212 | 	if (res) | 
 | 213 | 		return res; | 
 | 214 |  | 
 | 215 | 	mutex_lock(&data->update_lock); | 
 | 216 | 	data->temp_therm[attr->index] = TEMP_LIMIT_TO_REG(val); | 
 | 217 | 	i2c_smbus_write_byte_data(client, | 
 | 218 | 				  MAX6639_REG_THERM_LIMIT(attr->index), | 
 | 219 | 				  data->temp_therm[attr->index]); | 
 | 220 | 	mutex_unlock(&data->update_lock); | 
 | 221 | 	return count; | 
 | 222 | } | 
 | 223 |  | 
 | 224 | static ssize_t show_temp_crit(struct device *dev, | 
 | 225 | 			      struct device_attribute *dev_attr, char *buf) | 
 | 226 | { | 
 | 227 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 228 | 	struct max6639_data *data = i2c_get_clientdata(client); | 
 | 229 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 
 | 230 |  | 
 | 231 | 	return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000)); | 
 | 232 | } | 
 | 233 |  | 
 | 234 | static ssize_t set_temp_crit(struct device *dev, | 
 | 235 | 			     struct device_attribute *dev_attr, | 
 | 236 | 			     const char *buf, size_t count) | 
 | 237 | { | 
 | 238 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 239 | 	struct max6639_data *data = i2c_get_clientdata(client); | 
 | 240 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 
 | 241 | 	unsigned long val; | 
 | 242 | 	int res; | 
 | 243 |  | 
| Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 244 | 	res = kstrtoul(buf, 10, &val); | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 245 | 	if (res) | 
 | 246 | 		return res; | 
 | 247 |  | 
 | 248 | 	mutex_lock(&data->update_lock); | 
 | 249 | 	data->temp_alert[attr->index] = TEMP_LIMIT_TO_REG(val); | 
 | 250 | 	i2c_smbus_write_byte_data(client, | 
 | 251 | 				  MAX6639_REG_ALERT_LIMIT(attr->index), | 
 | 252 | 				  data->temp_alert[attr->index]); | 
 | 253 | 	mutex_unlock(&data->update_lock); | 
 | 254 | 	return count; | 
 | 255 | } | 
 | 256 |  | 
 | 257 | static ssize_t show_temp_emergency(struct device *dev, | 
 | 258 | 				   struct device_attribute *dev_attr, | 
 | 259 | 				   char *buf) | 
 | 260 | { | 
 | 261 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 262 | 	struct max6639_data *data = i2c_get_clientdata(client); | 
 | 263 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 
 | 264 |  | 
 | 265 | 	return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000)); | 
 | 266 | } | 
 | 267 |  | 
 | 268 | static ssize_t set_temp_emergency(struct device *dev, | 
 | 269 | 				  struct device_attribute *dev_attr, | 
 | 270 | 				  const char *buf, size_t count) | 
 | 271 | { | 
 | 272 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 273 | 	struct max6639_data *data = i2c_get_clientdata(client); | 
 | 274 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 
 | 275 | 	unsigned long val; | 
 | 276 | 	int res; | 
 | 277 |  | 
| Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 278 | 	res = kstrtoul(buf, 10, &val); | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 279 | 	if (res) | 
 | 280 | 		return res; | 
 | 281 |  | 
 | 282 | 	mutex_lock(&data->update_lock); | 
 | 283 | 	data->temp_ot[attr->index] = TEMP_LIMIT_TO_REG(val); | 
 | 284 | 	i2c_smbus_write_byte_data(client, | 
 | 285 | 				  MAX6639_REG_OT_LIMIT(attr->index), | 
 | 286 | 				  data->temp_ot[attr->index]); | 
 | 287 | 	mutex_unlock(&data->update_lock); | 
 | 288 | 	return count; | 
 | 289 | } | 
 | 290 |  | 
 | 291 | static ssize_t show_pwm(struct device *dev, | 
 | 292 | 			struct device_attribute *dev_attr, char *buf) | 
 | 293 | { | 
 | 294 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 295 | 	struct max6639_data *data = i2c_get_clientdata(client); | 
 | 296 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 
 | 297 |  | 
 | 298 | 	return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120); | 
 | 299 | } | 
 | 300 |  | 
 | 301 | static ssize_t set_pwm(struct device *dev, | 
 | 302 | 		       struct device_attribute *dev_attr, | 
 | 303 | 		       const char *buf, size_t count) | 
 | 304 | { | 
 | 305 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 306 | 	struct max6639_data *data = i2c_get_clientdata(client); | 
 | 307 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 
 | 308 | 	unsigned long val; | 
 | 309 | 	int res; | 
 | 310 |  | 
| Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 311 | 	res = kstrtoul(buf, 10, &val); | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 312 | 	if (res) | 
 | 313 | 		return res; | 
 | 314 |  | 
 | 315 | 	val = SENSORS_LIMIT(val, 0, 255); | 
 | 316 |  | 
 | 317 | 	mutex_lock(&data->update_lock); | 
 | 318 | 	data->pwm[attr->index] = (u8)(val * 120 / 255); | 
 | 319 | 	i2c_smbus_write_byte_data(client, | 
 | 320 | 				  MAX6639_REG_TARGTDUTY(attr->index), | 
 | 321 | 				  data->pwm[attr->index]); | 
 | 322 | 	mutex_unlock(&data->update_lock); | 
 | 323 | 	return count; | 
 | 324 | } | 
 | 325 |  | 
 | 326 | static ssize_t show_fan_input(struct device *dev, | 
 | 327 | 			      struct device_attribute *dev_attr, char *buf) | 
 | 328 | { | 
 | 329 | 	struct max6639_data *data = max6639_update_device(dev); | 
 | 330 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 
 | 331 |  | 
 | 332 | 	if (IS_ERR(data)) | 
 | 333 | 		return PTR_ERR(data); | 
 | 334 |  | 
 | 335 | 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index], | 
| Chris D Schimp | b63d97a | 2012-02-20 16:59:24 -0500 | [diff] [blame] | 336 | 		       data->rpm_range)); | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 337 | } | 
 | 338 |  | 
 | 339 | static ssize_t show_alarm(struct device *dev, | 
 | 340 | 			  struct device_attribute *dev_attr, char *buf) | 
 | 341 | { | 
 | 342 | 	struct max6639_data *data = max6639_update_device(dev); | 
 | 343 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 
 | 344 |  | 
 | 345 | 	if (IS_ERR(data)) | 
 | 346 | 		return PTR_ERR(data); | 
 | 347 |  | 
 | 348 | 	return sprintf(buf, "%d\n", !!(data->status & (1 << attr->index))); | 
 | 349 | } | 
 | 350 |  | 
 | 351 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0); | 
 | 352 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1); | 
 | 353 | static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0); | 
 | 354 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1); | 
 | 355 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, | 
 | 356 | 		set_temp_max, 0); | 
 | 357 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max, | 
 | 358 | 		set_temp_max, 1); | 
 | 359 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_crit, | 
 | 360 | 		set_temp_crit, 0); | 
 | 361 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit, | 
 | 362 | 		set_temp_crit, 1); | 
 | 363 | static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, | 
 | 364 | 		show_temp_emergency, set_temp_emergency, 0); | 
 | 365 | static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, | 
 | 366 | 		show_temp_emergency, set_temp_emergency, 1); | 
 | 367 | static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0); | 
 | 368 | static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1); | 
 | 369 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0); | 
 | 370 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1); | 
 | 371 | static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1); | 
 | 372 | static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 0); | 
 | 373 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 3); | 
 | 374 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2); | 
 | 375 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 7); | 
 | 376 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 6); | 
 | 377 | static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 5); | 
 | 378 | static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 4); | 
 | 379 |  | 
 | 380 |  | 
 | 381 | static struct attribute *max6639_attributes[] = { | 
 | 382 | 	&sensor_dev_attr_temp1_input.dev_attr.attr, | 
 | 383 | 	&sensor_dev_attr_temp2_input.dev_attr.attr, | 
 | 384 | 	&sensor_dev_attr_temp1_fault.dev_attr.attr, | 
 | 385 | 	&sensor_dev_attr_temp2_fault.dev_attr.attr, | 
 | 386 | 	&sensor_dev_attr_temp1_max.dev_attr.attr, | 
 | 387 | 	&sensor_dev_attr_temp2_max.dev_attr.attr, | 
 | 388 | 	&sensor_dev_attr_temp1_crit.dev_attr.attr, | 
 | 389 | 	&sensor_dev_attr_temp2_crit.dev_attr.attr, | 
 | 390 | 	&sensor_dev_attr_temp1_emergency.dev_attr.attr, | 
 | 391 | 	&sensor_dev_attr_temp2_emergency.dev_attr.attr, | 
 | 392 | 	&sensor_dev_attr_pwm1.dev_attr.attr, | 
 | 393 | 	&sensor_dev_attr_pwm2.dev_attr.attr, | 
 | 394 | 	&sensor_dev_attr_fan1_input.dev_attr.attr, | 
 | 395 | 	&sensor_dev_attr_fan2_input.dev_attr.attr, | 
 | 396 | 	&sensor_dev_attr_fan1_fault.dev_attr.attr, | 
 | 397 | 	&sensor_dev_attr_fan2_fault.dev_attr.attr, | 
 | 398 | 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | 
 | 399 | 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | 
 | 400 | 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | 
 | 401 | 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | 
 | 402 | 	&sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr, | 
 | 403 | 	&sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr, | 
 | 404 | 	NULL | 
 | 405 | }; | 
 | 406 |  | 
 | 407 | static const struct attribute_group max6639_group = { | 
 | 408 | 	.attrs = max6639_attributes, | 
 | 409 | }; | 
 | 410 |  | 
 | 411 | /* | 
 | 412 |  *  returns respective index in rpm_ranges table | 
 | 413 |  *  1 by default on invalid range | 
 | 414 |  */ | 
 | 415 | static int rpm_range_to_reg(int range) | 
 | 416 | { | 
 | 417 | 	int i; | 
 | 418 |  | 
 | 419 | 	for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) { | 
 | 420 | 		if (rpm_ranges[i] == range) | 
 | 421 | 			return i; | 
 | 422 | 	} | 
 | 423 |  | 
 | 424 | 	return 1; /* default: 4000 RPM */ | 
 | 425 | } | 
 | 426 |  | 
 | 427 | static int max6639_init_client(struct i2c_client *client) | 
 | 428 | { | 
 | 429 | 	struct max6639_data *data = i2c_get_clientdata(client); | 
 | 430 | 	struct max6639_platform_data *max6639_info = | 
 | 431 | 		client->dev.platform_data; | 
| Chris D Schimp | 2f2da1a | 2012-02-20 17:44:59 -0500 | [diff] [blame] | 432 | 	int i; | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 433 | 	int rpm_range = 1; /* default: 4000 RPM */ | 
| Chris D Schimp | 2f2da1a | 2012-02-20 17:44:59 -0500 | [diff] [blame] | 434 | 	int err; | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 435 |  | 
| stigge@antcom.de | 177f3b9 | 2011-01-29 17:04:01 +0100 | [diff] [blame] | 436 | 	/* Reset chip to default values, see below for GCONFIG setup */ | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 437 | 	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG, | 
 | 438 | 				  MAX6639_GCONFIG_POR); | 
 | 439 | 	if (err) | 
 | 440 | 		goto exit; | 
 | 441 |  | 
 | 442 | 	/* Fans pulse per revolution is 2 by default */ | 
 | 443 | 	if (max6639_info && max6639_info->ppr > 0 && | 
 | 444 | 			max6639_info->ppr < 5) | 
 | 445 | 		data->ppr = max6639_info->ppr; | 
 | 446 | 	else | 
 | 447 | 		data->ppr = 2; | 
 | 448 | 	data->ppr -= 1; | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 449 |  | 
 | 450 | 	if (max6639_info) | 
 | 451 | 		rpm_range = rpm_range_to_reg(max6639_info->rpm_range); | 
 | 452 | 	data->rpm_range = rpm_range; | 
 | 453 |  | 
 | 454 | 	for (i = 0; i < 2; i++) { | 
 | 455 |  | 
| Chris D Schimp | 2f2da1a | 2012-02-20 17:44:59 -0500 | [diff] [blame] | 456 | 		/* Set Fan pulse per revolution */ | 
 | 457 | 		err = i2c_smbus_write_byte_data(client, | 
 | 458 | 				MAX6639_REG_FAN_PPR(i), | 
 | 459 | 				data->ppr << 6); | 
 | 460 | 		if (err) | 
 | 461 | 			goto exit; | 
 | 462 |  | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 463 | 		/* Fans config PWM, RPM */ | 
 | 464 | 		err = i2c_smbus_write_byte_data(client, | 
 | 465 | 			MAX6639_REG_FAN_CONFIG1(i), | 
 | 466 | 			MAX6639_FAN_CONFIG1_PWM | rpm_range); | 
 | 467 | 		if (err) | 
 | 468 | 			goto exit; | 
 | 469 |  | 
 | 470 | 		/* Fans PWM polarity high by default */ | 
 | 471 | 		if (max6639_info && max6639_info->pwm_polarity == 0) | 
 | 472 | 			err = i2c_smbus_write_byte_data(client, | 
 | 473 | 				MAX6639_REG_FAN_CONFIG2a(i), 0x00); | 
 | 474 | 		else | 
 | 475 | 			err = i2c_smbus_write_byte_data(client, | 
 | 476 | 				MAX6639_REG_FAN_CONFIG2a(i), 0x02); | 
 | 477 | 		if (err) | 
 | 478 | 			goto exit; | 
 | 479 |  | 
| stigge@antcom.de | 177f3b9 | 2011-01-29 17:04:01 +0100 | [diff] [blame] | 480 | 		/* | 
 | 481 | 		 * /THERM full speed enable, | 
 | 482 | 		 * PWM frequency 25kHz, see also GCONFIG below | 
 | 483 | 		 */ | 
 | 484 | 		err = i2c_smbus_write_byte_data(client, | 
 | 485 | 			MAX6639_REG_FAN_CONFIG3(i), | 
 | 486 | 			MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03); | 
 | 487 | 		if (err) | 
 | 488 | 			goto exit; | 
 | 489 |  | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 490 | 		/* Max. temp. 80C/90C/100C */ | 
 | 491 | 		data->temp_therm[i] = 80; | 
 | 492 | 		data->temp_alert[i] = 90; | 
 | 493 | 		data->temp_ot[i] = 100; | 
 | 494 | 		err = i2c_smbus_write_byte_data(client, | 
 | 495 | 				MAX6639_REG_THERM_LIMIT(i), | 
 | 496 | 				data->temp_therm[i]); | 
 | 497 | 		if (err) | 
 | 498 | 			goto exit; | 
 | 499 | 		err = i2c_smbus_write_byte_data(client, | 
 | 500 | 				MAX6639_REG_ALERT_LIMIT(i), | 
 | 501 | 				data->temp_alert[i]); | 
 | 502 | 		if (err) | 
 | 503 | 			goto exit; | 
 | 504 | 		err = i2c_smbus_write_byte_data(client, | 
 | 505 | 				MAX6639_REG_OT_LIMIT(i), data->temp_ot[i]); | 
 | 506 | 		if (err) | 
 | 507 | 			goto exit; | 
 | 508 |  | 
 | 509 | 		/* PWM 120/120 (i.e. 100%) */ | 
 | 510 | 		data->pwm[i] = 120; | 
 | 511 | 		err = i2c_smbus_write_byte_data(client, | 
 | 512 | 				MAX6639_REG_TARGTDUTY(i), data->pwm[i]); | 
 | 513 | 		if (err) | 
 | 514 | 			goto exit; | 
 | 515 | 	} | 
 | 516 | 	/* Start monitoring */ | 
 | 517 | 	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG, | 
| stigge@antcom.de | 177f3b9 | 2011-01-29 17:04:01 +0100 | [diff] [blame] | 518 | 		MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL | | 
 | 519 | 		MAX6639_GCONFIG_PWM_FREQ_HI); | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 520 | exit: | 
 | 521 | 	return err; | 
 | 522 | } | 
 | 523 |  | 
 | 524 | /* Return 0 if detection is successful, -ENODEV otherwise */ | 
 | 525 | static int max6639_detect(struct i2c_client *client, | 
 | 526 | 			  struct i2c_board_info *info) | 
 | 527 | { | 
 | 528 | 	struct i2c_adapter *adapter = client->adapter; | 
 | 529 | 	int dev_id, manu_id; | 
 | 530 |  | 
 | 531 | 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | 
 | 532 | 		return -ENODEV; | 
 | 533 |  | 
 | 534 | 	/* Actual detection via device and manufacturer ID */ | 
 | 535 | 	dev_id = i2c_smbus_read_byte_data(client, MAX6639_REG_DEVID); | 
 | 536 | 	manu_id = i2c_smbus_read_byte_data(client, MAX6639_REG_MANUID); | 
 | 537 | 	if (dev_id != 0x58 || manu_id != 0x4D) | 
 | 538 | 		return -ENODEV; | 
 | 539 |  | 
 | 540 | 	strlcpy(info->type, "max6639", I2C_NAME_SIZE); | 
 | 541 |  | 
 | 542 | 	return 0; | 
 | 543 | } | 
 | 544 |  | 
 | 545 | static int max6639_probe(struct i2c_client *client, | 
 | 546 | 			 const struct i2c_device_id *id) | 
 | 547 | { | 
 | 548 | 	struct max6639_data *data; | 
 | 549 | 	int err; | 
 | 550 |  | 
 | 551 | 	data = kzalloc(sizeof(struct max6639_data), GFP_KERNEL); | 
 | 552 | 	if (!data) { | 
 | 553 | 		err = -ENOMEM; | 
 | 554 | 		goto exit; | 
 | 555 | 	} | 
 | 556 |  | 
 | 557 | 	i2c_set_clientdata(client, data); | 
 | 558 | 	mutex_init(&data->update_lock); | 
 | 559 |  | 
 | 560 | 	/* Initialize the max6639 chip */ | 
 | 561 | 	err = max6639_init_client(client); | 
 | 562 | 	if (err < 0) | 
 | 563 | 		goto error_free; | 
 | 564 |  | 
 | 565 | 	/* Register sysfs hooks */ | 
 | 566 | 	err = sysfs_create_group(&client->dev.kobj, &max6639_group); | 
 | 567 | 	if (err) | 
 | 568 | 		goto error_free; | 
 | 569 |  | 
 | 570 | 	data->hwmon_dev = hwmon_device_register(&client->dev); | 
 | 571 | 	if (IS_ERR(data->hwmon_dev)) { | 
 | 572 | 		err = PTR_ERR(data->hwmon_dev); | 
 | 573 | 		goto error_remove; | 
 | 574 | 	} | 
 | 575 |  | 
 | 576 | 	dev_info(&client->dev, "temperature sensor and fan control found\n"); | 
 | 577 |  | 
 | 578 | 	return 0; | 
 | 579 |  | 
 | 580 | error_remove: | 
 | 581 | 	sysfs_remove_group(&client->dev.kobj, &max6639_group); | 
 | 582 | error_free: | 
 | 583 | 	kfree(data); | 
 | 584 | exit: | 
 | 585 | 	return err; | 
 | 586 | } | 
 | 587 |  | 
 | 588 | static int max6639_remove(struct i2c_client *client) | 
 | 589 | { | 
 | 590 | 	struct max6639_data *data = i2c_get_clientdata(client); | 
 | 591 |  | 
 | 592 | 	hwmon_device_unregister(data->hwmon_dev); | 
 | 593 | 	sysfs_remove_group(&client->dev.kobj, &max6639_group); | 
 | 594 |  | 
 | 595 | 	kfree(data); | 
 | 596 | 	return 0; | 
 | 597 | } | 
 | 598 |  | 
| Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 599 | #ifdef CONFIG_PM_SLEEP | 
 | 600 | static int max6639_suspend(struct device *dev) | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 601 | { | 
| Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 602 | 	struct i2c_client *client = to_i2c_client(dev); | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 603 | 	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG); | 
 | 604 | 	if (data < 0) | 
 | 605 | 		return data; | 
 | 606 |  | 
 | 607 | 	return i2c_smbus_write_byte_data(client, | 
 | 608 | 			MAX6639_REG_GCONFIG, data | MAX6639_GCONFIG_STANDBY); | 
 | 609 | } | 
 | 610 |  | 
| Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 611 | static int max6639_resume(struct device *dev) | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 612 | { | 
| Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 613 | 	struct i2c_client *client = to_i2c_client(dev); | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 614 | 	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG); | 
 | 615 | 	if (data < 0) | 
 | 616 | 		return data; | 
 | 617 |  | 
 | 618 | 	return i2c_smbus_write_byte_data(client, | 
 | 619 | 			MAX6639_REG_GCONFIG, data & ~MAX6639_GCONFIG_STANDBY); | 
 | 620 | } | 
| Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 621 | #endif /* CONFIG_PM_SLEEP */ | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 622 |  | 
 | 623 | static const struct i2c_device_id max6639_id[] = { | 
 | 624 | 	{"max6639", 0}, | 
 | 625 | 	{ } | 
 | 626 | }; | 
 | 627 |  | 
 | 628 | MODULE_DEVICE_TABLE(i2c, max6639_id); | 
 | 629 |  | 
| Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 630 | static const struct dev_pm_ops max6639_pm_ops = { | 
 | 631 | 	SET_SYSTEM_SLEEP_PM_OPS(max6639_suspend, max6639_resume) | 
 | 632 | }; | 
 | 633 |  | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 634 | static struct i2c_driver max6639_driver = { | 
 | 635 | 	.class = I2C_CLASS_HWMON, | 
 | 636 | 	.driver = { | 
 | 637 | 		   .name = "max6639", | 
| Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 638 | 		   .pm = &max6639_pm_ops, | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 639 | 		   }, | 
 | 640 | 	.probe = max6639_probe, | 
 | 641 | 	.remove = max6639_remove, | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 642 | 	.id_table = max6639_id, | 
 | 643 | 	.detect = max6639_detect, | 
 | 644 | 	.address_list = normal_i2c, | 
 | 645 | }; | 
 | 646 |  | 
| Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 647 | module_i2c_driver(max6639_driver); | 
| stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 648 |  | 
 | 649 | MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>"); | 
 | 650 | MODULE_DESCRIPTION("max6639 driver"); | 
 | 651 | MODULE_LICENSE("GPL"); |