xref: /openbmc/linux/drivers/hwmon/amc6821.c (revision 2fa49589)
1 /*
2  * amc6821.c - Part of lm_sensors, Linux kernel modules for hardware
3  *	       monitoring
4  * Copyright (C) 2009 T. Mertelj <tomaz.mertelj@guest.arnes.si>
5  *
6  * Based on max6650.c:
7  * Copyright (C) 2007 Hans J. Koch <hjk@hansjkoch.de>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23 
24 #include <linux/kernel.h>	/* Needed for KERN_INFO */
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/i2c.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
34 
35 /*
36  * Addresses to scan.
37  */
38 
39 static const unsigned short normal_i2c[] = {0x18, 0x19, 0x1a, 0x2c, 0x2d, 0x2e,
40 	0x4c, 0x4d, 0x4e, I2C_CLIENT_END};
41 
42 /*
43  * Insmod parameters
44  */
45 
46 static int pwminv;	/*Inverted PWM output. */
47 module_param(pwminv, int, 0444);
48 
49 static int init = 1; /*Power-on initialization.*/
50 module_param(init, int, 0444);
51 
52 enum chips { amc6821 };
53 
54 #define AMC6821_REG_DEV_ID 0x3D
55 #define AMC6821_REG_COMP_ID 0x3E
56 #define AMC6821_REG_CONF1 0x00
57 #define AMC6821_REG_CONF2 0x01
58 #define AMC6821_REG_CONF3 0x3F
59 #define AMC6821_REG_CONF4 0x04
60 #define AMC6821_REG_STAT1 0x02
61 #define AMC6821_REG_STAT2 0x03
62 #define AMC6821_REG_TDATA_LOW 0x08
63 #define AMC6821_REG_TDATA_HI 0x09
64 #define AMC6821_REG_LTEMP_HI 0x0A
65 #define AMC6821_REG_RTEMP_HI 0x0B
66 #define AMC6821_REG_LTEMP_LIMIT_MIN 0x15
67 #define AMC6821_REG_LTEMP_LIMIT_MAX 0x14
68 #define AMC6821_REG_RTEMP_LIMIT_MIN 0x19
69 #define AMC6821_REG_RTEMP_LIMIT_MAX 0x18
70 #define AMC6821_REG_LTEMP_CRIT 0x1B
71 #define AMC6821_REG_RTEMP_CRIT 0x1D
72 #define AMC6821_REG_PSV_TEMP 0x1C
73 #define AMC6821_REG_DCY 0x22
74 #define AMC6821_REG_LTEMP_FAN_CTRL 0x24
75 #define AMC6821_REG_RTEMP_FAN_CTRL 0x25
76 #define AMC6821_REG_DCY_LOW_TEMP 0x21
77 
78 #define AMC6821_REG_TACH_LLIMITL 0x10
79 #define AMC6821_REG_TACH_LLIMITH 0x11
80 #define AMC6821_REG_TACH_HLIMITL 0x12
81 #define AMC6821_REG_TACH_HLIMITH 0x13
82 
83 #define AMC6821_CONF1_START 0x01
84 #define AMC6821_CONF1_FAN_INT_EN 0x02
85 #define AMC6821_CONF1_FANIE 0x04
86 #define AMC6821_CONF1_PWMINV 0x08
87 #define AMC6821_CONF1_FAN_FAULT_EN 0x10
88 #define AMC6821_CONF1_FDRC0 0x20
89 #define AMC6821_CONF1_FDRC1 0x40
90 #define AMC6821_CONF1_THERMOVIE 0x80
91 
92 #define AMC6821_CONF2_PWM_EN 0x01
93 #define AMC6821_CONF2_TACH_MODE 0x02
94 #define AMC6821_CONF2_TACH_EN 0x04
95 #define AMC6821_CONF2_RTFIE 0x08
96 #define AMC6821_CONF2_LTOIE 0x10
97 #define AMC6821_CONF2_RTOIE 0x20
98 #define AMC6821_CONF2_PSVIE 0x40
99 #define AMC6821_CONF2_RST 0x80
100 
101 #define AMC6821_CONF3_THERM_FAN_EN 0x80
102 #define AMC6821_CONF3_REV_MASK 0x0F
103 
104 #define AMC6821_CONF4_OVREN 0x10
105 #define AMC6821_CONF4_TACH_FAST 0x20
106 #define AMC6821_CONF4_PSPR 0x40
107 #define AMC6821_CONF4_MODE 0x80
108 
109 #define AMC6821_STAT1_RPM_ALARM 0x01
110 #define AMC6821_STAT1_FANS 0x02
111 #define AMC6821_STAT1_RTH 0x04
112 #define AMC6821_STAT1_RTL 0x08
113 #define AMC6821_STAT1_R_THERM 0x10
114 #define AMC6821_STAT1_RTF 0x20
115 #define AMC6821_STAT1_LTH 0x40
116 #define AMC6821_STAT1_LTL 0x80
117 
118 #define AMC6821_STAT2_RTC 0x08
119 #define AMC6821_STAT2_LTC 0x10
120 #define AMC6821_STAT2_LPSV 0x20
121 #define AMC6821_STAT2_L_THERM 0x40
122 #define AMC6821_STAT2_THERM_IN 0x80
123 
124 enum {IDX_TEMP1_INPUT = 0, IDX_TEMP1_MIN, IDX_TEMP1_MAX,
125 	IDX_TEMP1_CRIT, IDX_TEMP2_INPUT, IDX_TEMP2_MIN,
126 	IDX_TEMP2_MAX, IDX_TEMP2_CRIT,
127 	TEMP_IDX_LEN, };
128 
129 static const u8 temp_reg[] = {AMC6821_REG_LTEMP_HI,
130 			AMC6821_REG_LTEMP_LIMIT_MIN,
131 			AMC6821_REG_LTEMP_LIMIT_MAX,
132 			AMC6821_REG_LTEMP_CRIT,
133 			AMC6821_REG_RTEMP_HI,
134 			AMC6821_REG_RTEMP_LIMIT_MIN,
135 			AMC6821_REG_RTEMP_LIMIT_MAX,
136 			AMC6821_REG_RTEMP_CRIT, };
137 
138 enum {IDX_FAN1_INPUT = 0, IDX_FAN1_MIN, IDX_FAN1_MAX,
139 	FAN1_IDX_LEN, };
140 
141 static const u8 fan_reg_low[] = {AMC6821_REG_TDATA_LOW,
142 			AMC6821_REG_TACH_LLIMITL,
143 			AMC6821_REG_TACH_HLIMITL, };
144 
145 
146 static const u8 fan_reg_hi[] = {AMC6821_REG_TDATA_HI,
147 			AMC6821_REG_TACH_LLIMITH,
148 			AMC6821_REG_TACH_HLIMITH, };
149 
150 /*
151  * Client data (each client gets its own)
152  */
153 
154 struct amc6821_data {
155 	struct i2c_client *client;
156 	struct mutex update_lock;
157 	char valid; /* zero until following fields are valid */
158 	unsigned long last_updated; /* in jiffies */
159 
160 	/* register values */
161 	int temp[TEMP_IDX_LEN];
162 
163 	u16 fan[FAN1_IDX_LEN];
164 	u8 fan1_div;
165 
166 	u8 pwm1;
167 	u8 temp1_auto_point_temp[3];
168 	u8 temp2_auto_point_temp[3];
169 	u8 pwm1_auto_point_pwm[3];
170 	u8 pwm1_enable;
171 	u8 pwm1_auto_channels_temp;
172 
173 	u8 stat1;
174 	u8 stat2;
175 };
176 
177 static struct amc6821_data *amc6821_update_device(struct device *dev)
178 {
179 	struct amc6821_data *data = dev_get_drvdata(dev);
180 	struct i2c_client *client = data->client;
181 	int timeout = HZ;
182 	u8 reg;
183 	int i;
184 
185 	mutex_lock(&data->update_lock);
186 
187 	if (time_after(jiffies, data->last_updated + timeout) ||
188 			!data->valid) {
189 
190 		for (i = 0; i < TEMP_IDX_LEN; i++)
191 			data->temp[i] = (int8_t)i2c_smbus_read_byte_data(
192 				client, temp_reg[i]);
193 
194 		data->stat1 = i2c_smbus_read_byte_data(client,
195 			AMC6821_REG_STAT1);
196 		data->stat2 = i2c_smbus_read_byte_data(client,
197 			AMC6821_REG_STAT2);
198 
199 		data->pwm1 = i2c_smbus_read_byte_data(client,
200 			AMC6821_REG_DCY);
201 		for (i = 0; i < FAN1_IDX_LEN; i++) {
202 			data->fan[i] = i2c_smbus_read_byte_data(
203 					client,
204 					fan_reg_low[i]);
205 			data->fan[i] += i2c_smbus_read_byte_data(
206 					client,
207 					fan_reg_hi[i]) << 8;
208 		}
209 		data->fan1_div = i2c_smbus_read_byte_data(client,
210 			AMC6821_REG_CONF4);
211 		data->fan1_div = data->fan1_div & AMC6821_CONF4_PSPR ? 4 : 2;
212 
213 		data->pwm1_auto_point_pwm[0] = 0;
214 		data->pwm1_auto_point_pwm[2] = 255;
215 		data->pwm1_auto_point_pwm[1] = i2c_smbus_read_byte_data(client,
216 			AMC6821_REG_DCY_LOW_TEMP);
217 
218 		data->temp1_auto_point_temp[0] =
219 			i2c_smbus_read_byte_data(client,
220 					AMC6821_REG_PSV_TEMP);
221 		data->temp2_auto_point_temp[0] =
222 				data->temp1_auto_point_temp[0];
223 		reg = i2c_smbus_read_byte_data(client,
224 			AMC6821_REG_LTEMP_FAN_CTRL);
225 		data->temp1_auto_point_temp[1] = (reg & 0xF8) >> 1;
226 		reg &= 0x07;
227 		reg = 0x20 >> reg;
228 		if (reg > 0)
229 			data->temp1_auto_point_temp[2] =
230 				data->temp1_auto_point_temp[1] +
231 				(data->pwm1_auto_point_pwm[2] -
232 				data->pwm1_auto_point_pwm[1]) / reg;
233 		else
234 			data->temp1_auto_point_temp[2] = 255;
235 
236 		reg = i2c_smbus_read_byte_data(client,
237 			AMC6821_REG_RTEMP_FAN_CTRL);
238 		data->temp2_auto_point_temp[1] = (reg & 0xF8) >> 1;
239 		reg &= 0x07;
240 		reg = 0x20 >> reg;
241 		if (reg > 0)
242 			data->temp2_auto_point_temp[2] =
243 				data->temp2_auto_point_temp[1] +
244 				(data->pwm1_auto_point_pwm[2] -
245 				data->pwm1_auto_point_pwm[1]) / reg;
246 		else
247 			data->temp2_auto_point_temp[2] = 255;
248 
249 		reg = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
250 		reg = (reg >> 5) & 0x3;
251 		switch (reg) {
252 		case 0: /*open loop: software sets pwm1*/
253 			data->pwm1_auto_channels_temp = 0;
254 			data->pwm1_enable = 1;
255 			break;
256 		case 2: /*closed loop: remote T (temp2)*/
257 			data->pwm1_auto_channels_temp = 2;
258 			data->pwm1_enable = 2;
259 			break;
260 		case 3: /*closed loop: local and remote T (temp2)*/
261 			data->pwm1_auto_channels_temp = 3;
262 			data->pwm1_enable = 3;
263 			break;
264 		case 1: /*
265 			 * semi-open loop: software sets rpm, chip controls
266 			 * pwm1, currently not implemented
267 			 */
268 			data->pwm1_auto_channels_temp = 0;
269 			data->pwm1_enable = 0;
270 			break;
271 		}
272 
273 		data->last_updated = jiffies;
274 		data->valid = 1;
275 	}
276 	mutex_unlock(&data->update_lock);
277 	return data;
278 }
279 
280 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
281 			 char *buf)
282 {
283 	struct amc6821_data *data = amc6821_update_device(dev);
284 	int ix = to_sensor_dev_attr(devattr)->index;
285 
286 	return sprintf(buf, "%d\n", data->temp[ix] * 1000);
287 }
288 
289 static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
290 			  const char *buf, size_t count)
291 {
292 	struct amc6821_data *data = dev_get_drvdata(dev);
293 	struct i2c_client *client = data->client;
294 	int ix = to_sensor_dev_attr(attr)->index;
295 	long val;
296 
297 	int ret = kstrtol(buf, 10, &val);
298 	if (ret)
299 		return ret;
300 	val = clamp_val(val / 1000, -128, 127);
301 
302 	mutex_lock(&data->update_lock);
303 	data->temp[ix] = val;
304 	if (i2c_smbus_write_byte_data(client, temp_reg[ix], data->temp[ix])) {
305 		dev_err(&client->dev, "Register write error, aborting.\n");
306 		count = -EIO;
307 	}
308 	mutex_unlock(&data->update_lock);
309 	return count;
310 }
311 
312 static ssize_t temp_alarm_show(struct device *dev,
313 			       struct device_attribute *devattr, char *buf)
314 {
315 	struct amc6821_data *data = amc6821_update_device(dev);
316 	int ix = to_sensor_dev_attr(devattr)->index;
317 	u8 flag;
318 
319 	switch (ix) {
320 	case IDX_TEMP1_MIN:
321 		flag = data->stat1 & AMC6821_STAT1_LTL;
322 		break;
323 	case IDX_TEMP1_MAX:
324 		flag = data->stat1 & AMC6821_STAT1_LTH;
325 		break;
326 	case IDX_TEMP1_CRIT:
327 		flag = data->stat2 & AMC6821_STAT2_LTC;
328 		break;
329 	case IDX_TEMP2_MIN:
330 		flag = data->stat1 & AMC6821_STAT1_RTL;
331 		break;
332 	case IDX_TEMP2_MAX:
333 		flag = data->stat1 & AMC6821_STAT1_RTH;
334 		break;
335 	case IDX_TEMP2_CRIT:
336 		flag = data->stat2 & AMC6821_STAT2_RTC;
337 		break;
338 	default:
339 		dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
340 		return -EINVAL;
341 	}
342 	if (flag)
343 		return sprintf(buf, "1");
344 	else
345 		return sprintf(buf, "0");
346 }
347 
348 static ssize_t temp2_fault_show(struct device *dev,
349 				struct device_attribute *devattr, char *buf)
350 {
351 	struct amc6821_data *data = amc6821_update_device(dev);
352 	if (data->stat1 & AMC6821_STAT1_RTF)
353 		return sprintf(buf, "1");
354 	else
355 		return sprintf(buf, "0");
356 }
357 
358 static ssize_t pwm1_show(struct device *dev, struct device_attribute *devattr,
359 			 char *buf)
360 {
361 	struct amc6821_data *data = amc6821_update_device(dev);
362 	return sprintf(buf, "%d\n", data->pwm1);
363 }
364 
365 static ssize_t pwm1_store(struct device *dev,
366 			  struct device_attribute *devattr, const char *buf,
367 			  size_t count)
368 {
369 	struct amc6821_data *data = dev_get_drvdata(dev);
370 	struct i2c_client *client = data->client;
371 	long val;
372 	int ret = kstrtol(buf, 10, &val);
373 	if (ret)
374 		return ret;
375 
376 	mutex_lock(&data->update_lock);
377 	data->pwm1 = clamp_val(val , 0, 255);
378 	i2c_smbus_write_byte_data(client, AMC6821_REG_DCY, data->pwm1);
379 	mutex_unlock(&data->update_lock);
380 	return count;
381 }
382 
383 static ssize_t pwm1_enable_show(struct device *dev,
384 				struct device_attribute *devattr, char *buf)
385 {
386 	struct amc6821_data *data = amc6821_update_device(dev);
387 	return sprintf(buf, "%d\n", data->pwm1_enable);
388 }
389 
390 static ssize_t pwm1_enable_store(struct device *dev,
391 				 struct device_attribute *attr,
392 				 const char *buf, size_t count)
393 {
394 	struct amc6821_data *data = dev_get_drvdata(dev);
395 	struct i2c_client *client = data->client;
396 	long val;
397 	int config = kstrtol(buf, 10, &val);
398 	if (config)
399 		return config;
400 
401 	mutex_lock(&data->update_lock);
402 	config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
403 	if (config < 0) {
404 			dev_err(&client->dev,
405 			"Error reading configuration register, aborting.\n");
406 			count = config;
407 			goto unlock;
408 	}
409 
410 	switch (val) {
411 	case 1:
412 		config &= ~AMC6821_CONF1_FDRC0;
413 		config &= ~AMC6821_CONF1_FDRC1;
414 		break;
415 	case 2:
416 		config &= ~AMC6821_CONF1_FDRC0;
417 		config |= AMC6821_CONF1_FDRC1;
418 		break;
419 	case 3:
420 		config |= AMC6821_CONF1_FDRC0;
421 		config |= AMC6821_CONF1_FDRC1;
422 		break;
423 	default:
424 		count = -EINVAL;
425 		goto unlock;
426 	}
427 	if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF1, config)) {
428 			dev_err(&client->dev,
429 			"Configuration register write error, aborting.\n");
430 			count = -EIO;
431 	}
432 unlock:
433 	mutex_unlock(&data->update_lock);
434 	return count;
435 }
436 
437 static ssize_t pwm1_auto_channels_temp_show(struct device *dev,
438 					    struct device_attribute *devattr,
439 					    char *buf)
440 {
441 	struct amc6821_data *data = amc6821_update_device(dev);
442 	return sprintf(buf, "%d\n", data->pwm1_auto_channels_temp);
443 }
444 
445 static ssize_t temp_auto_point_temp_show(struct device *dev,
446 					 struct device_attribute *devattr,
447 					 char *buf)
448 {
449 	int ix = to_sensor_dev_attr_2(devattr)->index;
450 	int nr = to_sensor_dev_attr_2(devattr)->nr;
451 	struct amc6821_data *data = amc6821_update_device(dev);
452 	switch (nr) {
453 	case 1:
454 		return sprintf(buf, "%d\n",
455 			data->temp1_auto_point_temp[ix] * 1000);
456 	case 2:
457 		return sprintf(buf, "%d\n",
458 			data->temp2_auto_point_temp[ix] * 1000);
459 	default:
460 		dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
461 		return -EINVAL;
462 	}
463 }
464 
465 static ssize_t pwm1_auto_point_pwm_show(struct device *dev,
466 					struct device_attribute *devattr,
467 					char *buf)
468 {
469 	int ix = to_sensor_dev_attr(devattr)->index;
470 	struct amc6821_data *data = amc6821_update_device(dev);
471 	return sprintf(buf, "%d\n", data->pwm1_auto_point_pwm[ix]);
472 }
473 
474 static inline ssize_t set_slope_register(struct i2c_client *client,
475 		u8 reg,
476 		u8 dpwm,
477 		u8 *ptemp)
478 {
479 	int dt;
480 	u8 tmp;
481 
482 	dt = ptemp[2]-ptemp[1];
483 	for (tmp = 4; tmp > 0; tmp--) {
484 		if (dt * (0x20 >> tmp) >= dpwm)
485 			break;
486 	}
487 	tmp |= (ptemp[1] & 0x7C) << 1;
488 	if (i2c_smbus_write_byte_data(client,
489 			reg, tmp)) {
490 		dev_err(&client->dev, "Register write error, aborting.\n");
491 		return -EIO;
492 	}
493 	return 0;
494 }
495 
496 static ssize_t temp_auto_point_temp_store(struct device *dev,
497 					  struct device_attribute *attr,
498 					  const char *buf, size_t count)
499 {
500 	struct amc6821_data *data = amc6821_update_device(dev);
501 	struct i2c_client *client = data->client;
502 	int ix = to_sensor_dev_attr_2(attr)->index;
503 	int nr = to_sensor_dev_attr_2(attr)->nr;
504 	u8 *ptemp;
505 	u8 reg;
506 	int dpwm;
507 	long val;
508 	int ret = kstrtol(buf, 10, &val);
509 	if (ret)
510 		return ret;
511 
512 	switch (nr) {
513 	case 1:
514 		ptemp = data->temp1_auto_point_temp;
515 		reg = AMC6821_REG_LTEMP_FAN_CTRL;
516 		break;
517 	case 2:
518 		ptemp = data->temp2_auto_point_temp;
519 		reg = AMC6821_REG_RTEMP_FAN_CTRL;
520 		break;
521 	default:
522 		dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
523 		return -EINVAL;
524 	}
525 
526 	mutex_lock(&data->update_lock);
527 	data->valid = 0;
528 
529 	switch (ix) {
530 	case 0:
531 		ptemp[0] = clamp_val(val / 1000, 0,
532 				     data->temp1_auto_point_temp[1]);
533 		ptemp[0] = clamp_val(ptemp[0], 0,
534 				     data->temp2_auto_point_temp[1]);
535 		ptemp[0] = clamp_val(ptemp[0], 0, 63);
536 		if (i2c_smbus_write_byte_data(
537 					client,
538 					AMC6821_REG_PSV_TEMP,
539 					ptemp[0])) {
540 				dev_err(&client->dev,
541 					"Register write error, aborting.\n");
542 				count = -EIO;
543 		}
544 		goto EXIT;
545 	case 1:
546 		ptemp[1] = clamp_val(val / 1000, (ptemp[0] & 0x7C) + 4, 124);
547 		ptemp[1] &= 0x7C;
548 		ptemp[2] = clamp_val(ptemp[2], ptemp[1] + 1, 255);
549 		break;
550 	case 2:
551 		ptemp[2] = clamp_val(val / 1000, ptemp[1]+1, 255);
552 		break;
553 	default:
554 		dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
555 		count = -EINVAL;
556 		goto EXIT;
557 	}
558 	dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
559 	if (set_slope_register(client, reg, dpwm, ptemp))
560 		count = -EIO;
561 
562 EXIT:
563 	mutex_unlock(&data->update_lock);
564 	return count;
565 }
566 
567 static ssize_t pwm1_auto_point_pwm_store(struct device *dev,
568 					 struct device_attribute *attr,
569 					 const char *buf, size_t count)
570 {
571 	struct amc6821_data *data = dev_get_drvdata(dev);
572 	struct i2c_client *client = data->client;
573 	int dpwm;
574 	long val;
575 	int ret = kstrtol(buf, 10, &val);
576 	if (ret)
577 		return ret;
578 
579 	mutex_lock(&data->update_lock);
580 	data->pwm1_auto_point_pwm[1] = clamp_val(val, 0, 254);
581 	if (i2c_smbus_write_byte_data(client, AMC6821_REG_DCY_LOW_TEMP,
582 			data->pwm1_auto_point_pwm[1])) {
583 		dev_err(&client->dev, "Register write error, aborting.\n");
584 		count = -EIO;
585 		goto EXIT;
586 	}
587 	dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
588 	if (set_slope_register(client, AMC6821_REG_LTEMP_FAN_CTRL, dpwm,
589 			data->temp1_auto_point_temp)) {
590 		count = -EIO;
591 		goto EXIT;
592 	}
593 	if (set_slope_register(client, AMC6821_REG_RTEMP_FAN_CTRL, dpwm,
594 			data->temp2_auto_point_temp)) {
595 		count = -EIO;
596 		goto EXIT;
597 	}
598 
599 EXIT:
600 	data->valid = 0;
601 	mutex_unlock(&data->update_lock);
602 	return count;
603 }
604 
605 static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
606 			char *buf)
607 {
608 	struct amc6821_data *data = amc6821_update_device(dev);
609 	int ix = to_sensor_dev_attr(devattr)->index;
610 	if (0 == data->fan[ix])
611 		return sprintf(buf, "0");
612 	return sprintf(buf, "%d\n", (int)(6000000 / data->fan[ix]));
613 }
614 
615 static ssize_t fan1_fault_show(struct device *dev,
616 			       struct device_attribute *devattr, char *buf)
617 {
618 	struct amc6821_data *data = amc6821_update_device(dev);
619 	if (data->stat1 & AMC6821_STAT1_FANS)
620 		return sprintf(buf, "1");
621 	else
622 		return sprintf(buf, "0");
623 }
624 
625 static ssize_t fan_store(struct device *dev, struct device_attribute *attr,
626 			 const char *buf, size_t count)
627 {
628 	struct amc6821_data *data = dev_get_drvdata(dev);
629 	struct i2c_client *client = data->client;
630 	long val;
631 	int ix = to_sensor_dev_attr(attr)->index;
632 	int ret = kstrtol(buf, 10, &val);
633 	if (ret)
634 		return ret;
635 	val = 1 > val ? 0xFFFF : 6000000/val;
636 
637 	mutex_lock(&data->update_lock);
638 	data->fan[ix] = (u16) clamp_val(val, 1, 0xFFFF);
639 	if (i2c_smbus_write_byte_data(client, fan_reg_low[ix],
640 			data->fan[ix] & 0xFF)) {
641 		dev_err(&client->dev, "Register write error, aborting.\n");
642 		count = -EIO;
643 		goto EXIT;
644 	}
645 	if (i2c_smbus_write_byte_data(client,
646 			fan_reg_hi[ix], data->fan[ix] >> 8)) {
647 		dev_err(&client->dev, "Register write error, aborting.\n");
648 		count = -EIO;
649 	}
650 EXIT:
651 	mutex_unlock(&data->update_lock);
652 	return count;
653 }
654 
655 static ssize_t fan1_div_show(struct device *dev,
656 			     struct device_attribute *devattr, char *buf)
657 {
658 	struct amc6821_data *data = amc6821_update_device(dev);
659 	return sprintf(buf, "%d\n", data->fan1_div);
660 }
661 
662 static ssize_t fan1_div_store(struct device *dev,
663 			      struct device_attribute *attr, const char *buf,
664 			      size_t count)
665 {
666 	struct amc6821_data *data = dev_get_drvdata(dev);
667 	struct i2c_client *client = data->client;
668 	long val;
669 	int config = kstrtol(buf, 10, &val);
670 	if (config)
671 		return config;
672 
673 	mutex_lock(&data->update_lock);
674 	config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
675 	if (config < 0) {
676 		dev_err(&client->dev,
677 			"Error reading configuration register, aborting.\n");
678 		count = config;
679 		goto EXIT;
680 	}
681 	switch (val) {
682 	case 2:
683 		config &= ~AMC6821_CONF4_PSPR;
684 		data->fan1_div = 2;
685 		break;
686 	case 4:
687 		config |= AMC6821_CONF4_PSPR;
688 		data->fan1_div = 4;
689 		break;
690 	default:
691 		count = -EINVAL;
692 		goto EXIT;
693 	}
694 	if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4, config)) {
695 		dev_err(&client->dev,
696 			"Configuration register write error, aborting.\n");
697 		count = -EIO;
698 	}
699 EXIT:
700 	mutex_unlock(&data->update_lock);
701 	return count;
702 }
703 
704 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, IDX_TEMP1_INPUT);
705 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, IDX_TEMP1_MIN);
706 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, IDX_TEMP1_MAX);
707 static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, IDX_TEMP1_CRIT);
708 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, temp_alarm, IDX_TEMP1_MIN);
709 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, temp_alarm, IDX_TEMP1_MAX);
710 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, temp_alarm, IDX_TEMP1_CRIT);
711 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, IDX_TEMP2_INPUT);
712 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp, IDX_TEMP2_MIN);
713 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, IDX_TEMP2_MAX);
714 static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, IDX_TEMP2_CRIT);
715 static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp2_fault, 0);
716 static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, temp_alarm, IDX_TEMP2_MIN);
717 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, temp_alarm, IDX_TEMP2_MAX);
718 static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, temp_alarm, IDX_TEMP2_CRIT);
719 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, IDX_FAN1_INPUT);
720 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan, IDX_FAN1_MIN);
721 static SENSOR_DEVICE_ATTR_RW(fan1_max, fan, IDX_FAN1_MAX);
722 static SENSOR_DEVICE_ATTR_RO(fan1_fault, fan1_fault, 0);
723 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan1_div, 0);
724 
725 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm1, 0);
726 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm1_enable, 0);
727 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point1_pwm, pwm1_auto_point_pwm, 0);
728 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm1_auto_point_pwm, 1);
729 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point3_pwm, pwm1_auto_point_pwm, 2);
730 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_channels_temp, pwm1_auto_channels_temp,
731 			     0);
732 static SENSOR_DEVICE_ATTR_2_RO(temp1_auto_point1_temp, temp_auto_point_temp,
733 			       1, 0);
734 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point2_temp, temp_auto_point_temp,
735 			       1, 1);
736 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point3_temp, temp_auto_point_temp,
737 			       1, 2);
738 
739 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point1_temp, temp_auto_point_temp,
740 			       2, 0);
741 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point2_temp, temp_auto_point_temp,
742 			       2, 1);
743 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point3_temp, temp_auto_point_temp,
744 			       2, 2);
745 
746 static struct attribute *amc6821_attrs[] = {
747 	&sensor_dev_attr_temp1_input.dev_attr.attr,
748 	&sensor_dev_attr_temp1_min.dev_attr.attr,
749 	&sensor_dev_attr_temp1_max.dev_attr.attr,
750 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
751 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
752 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
753 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
754 	&sensor_dev_attr_temp2_input.dev_attr.attr,
755 	&sensor_dev_attr_temp2_min.dev_attr.attr,
756 	&sensor_dev_attr_temp2_max.dev_attr.attr,
757 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
758 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
759 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
760 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
761 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
762 	&sensor_dev_attr_fan1_input.dev_attr.attr,
763 	&sensor_dev_attr_fan1_min.dev_attr.attr,
764 	&sensor_dev_attr_fan1_max.dev_attr.attr,
765 	&sensor_dev_attr_fan1_fault.dev_attr.attr,
766 	&sensor_dev_attr_fan1_div.dev_attr.attr,
767 	&sensor_dev_attr_pwm1.dev_attr.attr,
768 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
769 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
770 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
771 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
772 	&sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
773 	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
774 	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
775 	&sensor_dev_attr_temp1_auto_point3_temp.dev_attr.attr,
776 	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
777 	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
778 	&sensor_dev_attr_temp2_auto_point3_temp.dev_attr.attr,
779 	NULL
780 };
781 
782 ATTRIBUTE_GROUPS(amc6821);
783 
784 /* Return 0 if detection is successful, -ENODEV otherwise */
785 static int amc6821_detect(
786 		struct i2c_client *client,
787 		struct i2c_board_info *info)
788 {
789 	struct i2c_adapter *adapter = client->adapter;
790 	int address = client->addr;
791 	int dev_id, comp_id;
792 
793 	dev_dbg(&adapter->dev, "amc6821_detect called.\n");
794 
795 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
796 		dev_dbg(&adapter->dev,
797 			"amc6821: I2C bus doesn't support byte mode, "
798 			"skipping.\n");
799 		return -ENODEV;
800 	}
801 
802 	dev_id = i2c_smbus_read_byte_data(client, AMC6821_REG_DEV_ID);
803 	comp_id = i2c_smbus_read_byte_data(client, AMC6821_REG_COMP_ID);
804 	if (dev_id != 0x21 || comp_id != 0x49) {
805 		dev_dbg(&adapter->dev,
806 			"amc6821: detection failed at 0x%02x.\n",
807 			address);
808 		return -ENODEV;
809 	}
810 
811 	/*
812 	 * Bit 7 of the address register is ignored, so we can check the
813 	 * ID registers again
814 	 */
815 	dev_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_DEV_ID);
816 	comp_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_COMP_ID);
817 	if (dev_id != 0x21 || comp_id != 0x49) {
818 		dev_dbg(&adapter->dev,
819 			"amc6821: detection failed at 0x%02x.\n",
820 			address);
821 		return -ENODEV;
822 	}
823 
824 	dev_info(&adapter->dev, "amc6821: chip found at 0x%02x.\n", address);
825 	strlcpy(info->type, "amc6821", I2C_NAME_SIZE);
826 
827 	return 0;
828 }
829 
830 static int amc6821_init_client(struct i2c_client *client)
831 {
832 	int config;
833 	int err = -EIO;
834 
835 	if (init) {
836 		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
837 
838 		if (config < 0) {
839 				dev_err(&client->dev,
840 			"Error reading configuration register, aborting.\n");
841 				return err;
842 		}
843 
844 		config |= AMC6821_CONF4_MODE;
845 
846 		if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4,
847 				config)) {
848 			dev_err(&client->dev,
849 			"Configuration register write error, aborting.\n");
850 			return err;
851 		}
852 
853 		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF3);
854 
855 		if (config < 0) {
856 			dev_err(&client->dev,
857 			"Error reading configuration register, aborting.\n");
858 			return err;
859 		}
860 
861 		dev_info(&client->dev, "Revision %d\n", config & 0x0f);
862 
863 		config &= ~AMC6821_CONF3_THERM_FAN_EN;
864 
865 		if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF3,
866 				config)) {
867 			dev_err(&client->dev,
868 			"Configuration register write error, aborting.\n");
869 			return err;
870 		}
871 
872 		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF2);
873 
874 		if (config < 0) {
875 			dev_err(&client->dev,
876 			"Error reading configuration register, aborting.\n");
877 			return err;
878 		}
879 
880 		config &= ~AMC6821_CONF2_RTFIE;
881 		config &= ~AMC6821_CONF2_LTOIE;
882 		config &= ~AMC6821_CONF2_RTOIE;
883 		if (i2c_smbus_write_byte_data(client,
884 				AMC6821_REG_CONF2, config)) {
885 			dev_err(&client->dev,
886 			"Configuration register write error, aborting.\n");
887 			return err;
888 		}
889 
890 		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
891 
892 		if (config < 0) {
893 			dev_err(&client->dev,
894 			"Error reading configuration register, aborting.\n");
895 			return err;
896 		}
897 
898 		config &= ~AMC6821_CONF1_THERMOVIE;
899 		config &= ~AMC6821_CONF1_FANIE;
900 		config |= AMC6821_CONF1_START;
901 		if (pwminv)
902 			config |= AMC6821_CONF1_PWMINV;
903 		else
904 			config &= ~AMC6821_CONF1_PWMINV;
905 
906 		if (i2c_smbus_write_byte_data(
907 				client, AMC6821_REG_CONF1, config)) {
908 			dev_err(&client->dev,
909 			"Configuration register write error, aborting.\n");
910 			return err;
911 		}
912 	}
913 	return 0;
914 }
915 
916 static int amc6821_probe(struct i2c_client *client,
917 			 const struct i2c_device_id *id)
918 {
919 	struct device *dev = &client->dev;
920 	struct amc6821_data *data;
921 	struct device *hwmon_dev;
922 	int err;
923 
924 	data = devm_kzalloc(dev, sizeof(struct amc6821_data), GFP_KERNEL);
925 	if (!data)
926 		return -ENOMEM;
927 
928 	data->client = client;
929 	mutex_init(&data->update_lock);
930 
931 	/*
932 	 * Initialize the amc6821 chip
933 	 */
934 	err = amc6821_init_client(client);
935 	if (err)
936 		return err;
937 
938 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
939 							   data,
940 							   amc6821_groups);
941 	return PTR_ERR_OR_ZERO(hwmon_dev);
942 }
943 
944 static const struct i2c_device_id amc6821_id[] = {
945 	{ "amc6821", amc6821 },
946 	{ }
947 };
948 
949 MODULE_DEVICE_TABLE(i2c, amc6821_id);
950 
951 static struct i2c_driver amc6821_driver = {
952 	.class = I2C_CLASS_HWMON,
953 	.driver = {
954 		.name	= "amc6821",
955 	},
956 	.probe = amc6821_probe,
957 	.id_table = amc6821_id,
958 	.detect = amc6821_detect,
959 	.address_list = normal_i2c,
960 };
961 
962 module_i2c_driver(amc6821_driver);
963 
964 MODULE_LICENSE("GPL");
965 MODULE_AUTHOR("T. Mertelj <tomaz.mertelj@guest.arnes.si>");
966 MODULE_DESCRIPTION("Texas Instruments amc6821 hwmon driver");
967