xref: /openbmc/linux/drivers/hwmon/amc6821.c (revision eb3fcf00)
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, S_IRUGO);
48 
49 static int init = 1; /*Power-on initialization.*/
50 module_param(init, int, S_IRUGO);
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] = i2c_smbus_read_byte_data(client,
192 				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 get_temp(
281 		struct device *dev,
282 		struct device_attribute *devattr,
283 		char *buf)
284 {
285 	struct amc6821_data *data = amc6821_update_device(dev);
286 	int ix = to_sensor_dev_attr(devattr)->index;
287 
288 	return sprintf(buf, "%d\n", data->temp[ix] * 1000);
289 }
290 
291 static ssize_t set_temp(
292 		struct device *dev,
293 		struct device_attribute *attr,
294 		const char *buf,
295 		size_t count)
296 {
297 	struct amc6821_data *data = dev_get_drvdata(dev);
298 	struct i2c_client *client = data->client;
299 	int ix = to_sensor_dev_attr(attr)->index;
300 	long val;
301 
302 	int ret = kstrtol(buf, 10, &val);
303 	if (ret)
304 		return ret;
305 	val = clamp_val(val / 1000, -128, 127);
306 
307 	mutex_lock(&data->update_lock);
308 	data->temp[ix] = val;
309 	if (i2c_smbus_write_byte_data(client, temp_reg[ix], data->temp[ix])) {
310 		dev_err(&client->dev, "Register write error, aborting.\n");
311 		count = -EIO;
312 	}
313 	mutex_unlock(&data->update_lock);
314 	return count;
315 }
316 
317 static ssize_t get_temp_alarm(
318 	struct device *dev,
319 	struct device_attribute *devattr,
320 	char *buf)
321 {
322 	struct amc6821_data *data = amc6821_update_device(dev);
323 	int ix = to_sensor_dev_attr(devattr)->index;
324 	u8 flag;
325 
326 	switch (ix) {
327 	case IDX_TEMP1_MIN:
328 		flag = data->stat1 & AMC6821_STAT1_LTL;
329 		break;
330 	case IDX_TEMP1_MAX:
331 		flag = data->stat1 & AMC6821_STAT1_LTH;
332 		break;
333 	case IDX_TEMP1_CRIT:
334 		flag = data->stat2 & AMC6821_STAT2_LTC;
335 		break;
336 	case IDX_TEMP2_MIN:
337 		flag = data->stat1 & AMC6821_STAT1_RTL;
338 		break;
339 	case IDX_TEMP2_MAX:
340 		flag = data->stat1 & AMC6821_STAT1_RTH;
341 		break;
342 	case IDX_TEMP2_CRIT:
343 		flag = data->stat2 & AMC6821_STAT2_RTC;
344 		break;
345 	default:
346 		dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
347 		return -EINVAL;
348 	}
349 	if (flag)
350 		return sprintf(buf, "1");
351 	else
352 		return sprintf(buf, "0");
353 }
354 
355 static ssize_t get_temp2_fault(
356 		struct device *dev,
357 		struct device_attribute *devattr,
358 		char *buf)
359 {
360 	struct amc6821_data *data = amc6821_update_device(dev);
361 	if (data->stat1 & AMC6821_STAT1_RTF)
362 		return sprintf(buf, "1");
363 	else
364 		return sprintf(buf, "0");
365 }
366 
367 static ssize_t get_pwm1(
368 		struct device *dev,
369 		struct device_attribute *devattr,
370 		char *buf)
371 {
372 	struct amc6821_data *data = amc6821_update_device(dev);
373 	return sprintf(buf, "%d\n", data->pwm1);
374 }
375 
376 static ssize_t set_pwm1(
377 		struct device *dev,
378 		struct device_attribute *devattr,
379 		const char *buf,
380 		size_t count)
381 {
382 	struct amc6821_data *data = dev_get_drvdata(dev);
383 	struct i2c_client *client = data->client;
384 	long val;
385 	int ret = kstrtol(buf, 10, &val);
386 	if (ret)
387 		return ret;
388 
389 	mutex_lock(&data->update_lock);
390 	data->pwm1 = clamp_val(val , 0, 255);
391 	i2c_smbus_write_byte_data(client, AMC6821_REG_DCY, data->pwm1);
392 	mutex_unlock(&data->update_lock);
393 	return count;
394 }
395 
396 static ssize_t get_pwm1_enable(
397 		struct device *dev,
398 		struct device_attribute *devattr,
399 		char *buf)
400 {
401 	struct amc6821_data *data = amc6821_update_device(dev);
402 	return sprintf(buf, "%d\n", data->pwm1_enable);
403 }
404 
405 static ssize_t set_pwm1_enable(
406 		struct device *dev,
407 		struct device_attribute *attr,
408 		const char *buf,
409 		size_t count)
410 {
411 	struct amc6821_data *data = dev_get_drvdata(dev);
412 	struct i2c_client *client = data->client;
413 	long val;
414 	int config = kstrtol(buf, 10, &val);
415 	if (config)
416 		return config;
417 
418 	mutex_lock(&data->update_lock);
419 	config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
420 	if (config < 0) {
421 			dev_err(&client->dev,
422 			"Error reading configuration register, aborting.\n");
423 			count = config;
424 			goto unlock;
425 	}
426 
427 	switch (val) {
428 	case 1:
429 		config &= ~AMC6821_CONF1_FDRC0;
430 		config &= ~AMC6821_CONF1_FDRC1;
431 		break;
432 	case 2:
433 		config &= ~AMC6821_CONF1_FDRC0;
434 		config |= AMC6821_CONF1_FDRC1;
435 		break;
436 	case 3:
437 		config |= AMC6821_CONF1_FDRC0;
438 		config |= AMC6821_CONF1_FDRC1;
439 		break;
440 	default:
441 		count = -EINVAL;
442 		goto unlock;
443 	}
444 	if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF1, config)) {
445 			dev_err(&client->dev,
446 			"Configuration register write error, aborting.\n");
447 			count = -EIO;
448 	}
449 unlock:
450 	mutex_unlock(&data->update_lock);
451 	return count;
452 }
453 
454 static ssize_t get_pwm1_auto_channels_temp(
455 		struct device *dev,
456 		struct device_attribute *devattr,
457 		char *buf)
458 {
459 	struct amc6821_data *data = amc6821_update_device(dev);
460 	return sprintf(buf, "%d\n", data->pwm1_auto_channels_temp);
461 }
462 
463 static ssize_t get_temp_auto_point_temp(
464 		struct device *dev,
465 		struct device_attribute *devattr,
466 		char *buf)
467 {
468 	int ix = to_sensor_dev_attr_2(devattr)->index;
469 	int nr = to_sensor_dev_attr_2(devattr)->nr;
470 	struct amc6821_data *data = amc6821_update_device(dev);
471 	switch (nr) {
472 	case 1:
473 		return sprintf(buf, "%d\n",
474 			data->temp1_auto_point_temp[ix] * 1000);
475 	case 2:
476 		return sprintf(buf, "%d\n",
477 			data->temp2_auto_point_temp[ix] * 1000);
478 	default:
479 		dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
480 		return -EINVAL;
481 	}
482 }
483 
484 static ssize_t get_pwm1_auto_point_pwm(
485 		struct device *dev,
486 		struct device_attribute *devattr,
487 		char *buf)
488 {
489 	int ix = to_sensor_dev_attr(devattr)->index;
490 	struct amc6821_data *data = amc6821_update_device(dev);
491 	return sprintf(buf, "%d\n", data->pwm1_auto_point_pwm[ix]);
492 }
493 
494 static inline ssize_t set_slope_register(struct i2c_client *client,
495 		u8 reg,
496 		u8 dpwm,
497 		u8 *ptemp)
498 {
499 	int dt;
500 	u8 tmp;
501 
502 	dt = ptemp[2]-ptemp[1];
503 	for (tmp = 4; tmp > 0; tmp--) {
504 		if (dt * (0x20 >> tmp) >= dpwm)
505 			break;
506 	}
507 	tmp |= (ptemp[1] & 0x7C) << 1;
508 	if (i2c_smbus_write_byte_data(client,
509 			reg, tmp)) {
510 		dev_err(&client->dev, "Register write error, aborting.\n");
511 		return -EIO;
512 	}
513 	return 0;
514 }
515 
516 static ssize_t set_temp_auto_point_temp(
517 		struct device *dev,
518 		struct device_attribute *attr,
519 		const char *buf,
520 		size_t count)
521 {
522 	struct amc6821_data *data = amc6821_update_device(dev);
523 	struct i2c_client *client = data->client;
524 	int ix = to_sensor_dev_attr_2(attr)->index;
525 	int nr = to_sensor_dev_attr_2(attr)->nr;
526 	u8 *ptemp;
527 	u8 reg;
528 	int dpwm;
529 	long val;
530 	int ret = kstrtol(buf, 10, &val);
531 	if (ret)
532 		return ret;
533 
534 	switch (nr) {
535 	case 1:
536 		ptemp = data->temp1_auto_point_temp;
537 		reg = AMC6821_REG_LTEMP_FAN_CTRL;
538 		break;
539 	case 2:
540 		ptemp = data->temp2_auto_point_temp;
541 		reg = AMC6821_REG_RTEMP_FAN_CTRL;
542 		break;
543 	default:
544 		dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
545 		return -EINVAL;
546 	}
547 
548 	mutex_lock(&data->update_lock);
549 	data->valid = 0;
550 
551 	switch (ix) {
552 	case 0:
553 		ptemp[0] = clamp_val(val / 1000, 0,
554 				     data->temp1_auto_point_temp[1]);
555 		ptemp[0] = clamp_val(ptemp[0], 0,
556 				     data->temp2_auto_point_temp[1]);
557 		ptemp[0] = clamp_val(ptemp[0], 0, 63);
558 		if (i2c_smbus_write_byte_data(
559 					client,
560 					AMC6821_REG_PSV_TEMP,
561 					ptemp[0])) {
562 				dev_err(&client->dev,
563 					"Register write error, aborting.\n");
564 				count = -EIO;
565 		}
566 		goto EXIT;
567 	case 1:
568 		ptemp[1] = clamp_val(val / 1000, (ptemp[0] & 0x7C) + 4, 124);
569 		ptemp[1] &= 0x7C;
570 		ptemp[2] = clamp_val(ptemp[2], ptemp[1] + 1, 255);
571 		break;
572 	case 2:
573 		ptemp[2] = clamp_val(val / 1000, ptemp[1]+1, 255);
574 		break;
575 	default:
576 		dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
577 		count = -EINVAL;
578 		goto EXIT;
579 	}
580 	dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
581 	if (set_slope_register(client, reg, dpwm, ptemp))
582 		count = -EIO;
583 
584 EXIT:
585 	mutex_unlock(&data->update_lock);
586 	return count;
587 }
588 
589 static ssize_t set_pwm1_auto_point_pwm(
590 		struct device *dev,
591 		struct device_attribute *attr,
592 		const char *buf,
593 		size_t count)
594 {
595 	struct amc6821_data *data = dev_get_drvdata(dev);
596 	struct i2c_client *client = data->client;
597 	int dpwm;
598 	long val;
599 	int ret = kstrtol(buf, 10, &val);
600 	if (ret)
601 		return ret;
602 
603 	mutex_lock(&data->update_lock);
604 	data->pwm1_auto_point_pwm[1] = clamp_val(val, 0, 254);
605 	if (i2c_smbus_write_byte_data(client, AMC6821_REG_DCY_LOW_TEMP,
606 			data->pwm1_auto_point_pwm[1])) {
607 		dev_err(&client->dev, "Register write error, aborting.\n");
608 		count = -EIO;
609 		goto EXIT;
610 	}
611 	dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
612 	if (set_slope_register(client, AMC6821_REG_LTEMP_FAN_CTRL, dpwm,
613 			data->temp1_auto_point_temp)) {
614 		count = -EIO;
615 		goto EXIT;
616 	}
617 	if (set_slope_register(client, AMC6821_REG_RTEMP_FAN_CTRL, dpwm,
618 			data->temp2_auto_point_temp)) {
619 		count = -EIO;
620 		goto EXIT;
621 	}
622 
623 EXIT:
624 	data->valid = 0;
625 	mutex_unlock(&data->update_lock);
626 	return count;
627 }
628 
629 static ssize_t get_fan(
630 		struct device *dev,
631 		struct device_attribute *devattr,
632 		char *buf)
633 {
634 	struct amc6821_data *data = amc6821_update_device(dev);
635 	int ix = to_sensor_dev_attr(devattr)->index;
636 	if (0 == data->fan[ix])
637 		return sprintf(buf, "0");
638 	return sprintf(buf, "%d\n", (int)(6000000 / data->fan[ix]));
639 }
640 
641 static ssize_t get_fan1_fault(
642 		struct device *dev,
643 		struct device_attribute *devattr,
644 		char *buf)
645 {
646 	struct amc6821_data *data = amc6821_update_device(dev);
647 	if (data->stat1 & AMC6821_STAT1_FANS)
648 		return sprintf(buf, "1");
649 	else
650 		return sprintf(buf, "0");
651 }
652 
653 static ssize_t set_fan(
654 		struct device *dev,
655 		struct device_attribute *attr,
656 		const char *buf, size_t count)
657 {
658 	struct amc6821_data *data = dev_get_drvdata(dev);
659 	struct i2c_client *client = data->client;
660 	long val;
661 	int ix = to_sensor_dev_attr(attr)->index;
662 	int ret = kstrtol(buf, 10, &val);
663 	if (ret)
664 		return ret;
665 	val = 1 > val ? 0xFFFF : 6000000/val;
666 
667 	mutex_lock(&data->update_lock);
668 	data->fan[ix] = (u16) clamp_val(val, 1, 0xFFFF);
669 	if (i2c_smbus_write_byte_data(client, fan_reg_low[ix],
670 			data->fan[ix] & 0xFF)) {
671 		dev_err(&client->dev, "Register write error, aborting.\n");
672 		count = -EIO;
673 		goto EXIT;
674 	}
675 	if (i2c_smbus_write_byte_data(client,
676 			fan_reg_hi[ix], data->fan[ix] >> 8)) {
677 		dev_err(&client->dev, "Register write error, aborting.\n");
678 		count = -EIO;
679 	}
680 EXIT:
681 	mutex_unlock(&data->update_lock);
682 	return count;
683 }
684 
685 static ssize_t get_fan1_div(
686 		struct device *dev,
687 		struct device_attribute *devattr,
688 		char *buf)
689 {
690 	struct amc6821_data *data = amc6821_update_device(dev);
691 	return sprintf(buf, "%d\n", data->fan1_div);
692 }
693 
694 static ssize_t set_fan1_div(
695 		struct device *dev,
696 		struct device_attribute *attr,
697 		const char *buf, size_t count)
698 {
699 	struct amc6821_data *data = dev_get_drvdata(dev);
700 	struct i2c_client *client = data->client;
701 	long val;
702 	int config = kstrtol(buf, 10, &val);
703 	if (config)
704 		return config;
705 
706 	mutex_lock(&data->update_lock);
707 	config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
708 	if (config < 0) {
709 		dev_err(&client->dev,
710 			"Error reading configuration register, aborting.\n");
711 		count = config;
712 		goto EXIT;
713 	}
714 	switch (val) {
715 	case 2:
716 		config &= ~AMC6821_CONF4_PSPR;
717 		data->fan1_div = 2;
718 		break;
719 	case 4:
720 		config |= AMC6821_CONF4_PSPR;
721 		data->fan1_div = 4;
722 		break;
723 	default:
724 		count = -EINVAL;
725 		goto EXIT;
726 	}
727 	if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4, config)) {
728 		dev_err(&client->dev,
729 			"Configuration register write error, aborting.\n");
730 		count = -EIO;
731 	}
732 EXIT:
733 	mutex_unlock(&data->update_lock);
734 	return count;
735 }
736 
737 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
738 	get_temp, NULL, IDX_TEMP1_INPUT);
739 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, get_temp,
740 	set_temp, IDX_TEMP1_MIN);
741 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, get_temp,
742 	set_temp, IDX_TEMP1_MAX);
743 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR, get_temp,
744 	set_temp, IDX_TEMP1_CRIT);
745 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO,
746 	get_temp_alarm, NULL, IDX_TEMP1_MIN);
747 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
748 	get_temp_alarm, NULL, IDX_TEMP1_MAX);
749 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO,
750 	get_temp_alarm, NULL, IDX_TEMP1_CRIT);
751 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
752 	get_temp, NULL, IDX_TEMP2_INPUT);
753 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, get_temp,
754 	set_temp, IDX_TEMP2_MIN);
755 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, get_temp,
756 	set_temp, IDX_TEMP2_MAX);
757 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR, get_temp,
758 	set_temp, IDX_TEMP2_CRIT);
759 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO,
760 	get_temp2_fault, NULL, 0);
761 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO,
762 	get_temp_alarm, NULL, IDX_TEMP2_MIN);
763 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO,
764 	get_temp_alarm, NULL, IDX_TEMP2_MAX);
765 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO,
766 	get_temp_alarm, NULL, IDX_TEMP2_CRIT);
767 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, IDX_FAN1_INPUT);
768 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
769 	get_fan, set_fan, IDX_FAN1_MIN);
770 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO | S_IWUSR,
771 	get_fan, set_fan, IDX_FAN1_MAX);
772 static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan1_fault, NULL, 0);
773 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
774 	get_fan1_div, set_fan1_div, 0);
775 
776 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm1, set_pwm1, 0);
777 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
778 	get_pwm1_enable, set_pwm1_enable, 0);
779 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO,
780 	get_pwm1_auto_point_pwm, NULL, 0);
781 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
782 	get_pwm1_auto_point_pwm, set_pwm1_auto_point_pwm, 1);
783 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO,
784 	get_pwm1_auto_point_pwm, NULL, 2);
785 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
786 	get_pwm1_auto_channels_temp, NULL, 0);
787 static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO,
788 	get_temp_auto_point_temp, NULL, 1, 0);
789 static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
790 	get_temp_auto_point_temp, set_temp_auto_point_temp, 1, 1);
791 static SENSOR_DEVICE_ATTR_2(temp1_auto_point3_temp, S_IWUSR | S_IRUGO,
792 	get_temp_auto_point_temp, set_temp_auto_point_temp, 1, 2);
793 
794 static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
795 	get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 0);
796 static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
797 	get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 1);
798 static SENSOR_DEVICE_ATTR_2(temp2_auto_point3_temp, S_IWUSR | S_IRUGO,
799 	get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 2);
800 
801 static struct attribute *amc6821_attrs[] = {
802 	&sensor_dev_attr_temp1_input.dev_attr.attr,
803 	&sensor_dev_attr_temp1_min.dev_attr.attr,
804 	&sensor_dev_attr_temp1_max.dev_attr.attr,
805 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
806 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
807 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
808 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
809 	&sensor_dev_attr_temp2_input.dev_attr.attr,
810 	&sensor_dev_attr_temp2_min.dev_attr.attr,
811 	&sensor_dev_attr_temp2_max.dev_attr.attr,
812 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
813 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
814 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
815 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
816 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
817 	&sensor_dev_attr_fan1_input.dev_attr.attr,
818 	&sensor_dev_attr_fan1_min.dev_attr.attr,
819 	&sensor_dev_attr_fan1_max.dev_attr.attr,
820 	&sensor_dev_attr_fan1_fault.dev_attr.attr,
821 	&sensor_dev_attr_fan1_div.dev_attr.attr,
822 	&sensor_dev_attr_pwm1.dev_attr.attr,
823 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
824 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
825 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
826 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
827 	&sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
828 	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
829 	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
830 	&sensor_dev_attr_temp1_auto_point3_temp.dev_attr.attr,
831 	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
832 	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
833 	&sensor_dev_attr_temp2_auto_point3_temp.dev_attr.attr,
834 	NULL
835 };
836 
837 ATTRIBUTE_GROUPS(amc6821);
838 
839 /* Return 0 if detection is successful, -ENODEV otherwise */
840 static int amc6821_detect(
841 		struct i2c_client *client,
842 		struct i2c_board_info *info)
843 {
844 	struct i2c_adapter *adapter = client->adapter;
845 	int address = client->addr;
846 	int dev_id, comp_id;
847 
848 	dev_dbg(&adapter->dev, "amc6821_detect called.\n");
849 
850 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
851 		dev_dbg(&adapter->dev,
852 			"amc6821: I2C bus doesn't support byte mode, "
853 			"skipping.\n");
854 		return -ENODEV;
855 	}
856 
857 	dev_id = i2c_smbus_read_byte_data(client, AMC6821_REG_DEV_ID);
858 	comp_id = i2c_smbus_read_byte_data(client, AMC6821_REG_COMP_ID);
859 	if (dev_id != 0x21 || comp_id != 0x49) {
860 		dev_dbg(&adapter->dev,
861 			"amc6821: detection failed at 0x%02x.\n",
862 			address);
863 		return -ENODEV;
864 	}
865 
866 	/*
867 	 * Bit 7 of the address register is ignored, so we can check the
868 	 * ID registers again
869 	 */
870 	dev_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_DEV_ID);
871 	comp_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_COMP_ID);
872 	if (dev_id != 0x21 || comp_id != 0x49) {
873 		dev_dbg(&adapter->dev,
874 			"amc6821: detection failed at 0x%02x.\n",
875 			address);
876 		return -ENODEV;
877 	}
878 
879 	dev_info(&adapter->dev, "amc6821: chip found at 0x%02x.\n", address);
880 	strlcpy(info->type, "amc6821", I2C_NAME_SIZE);
881 
882 	return 0;
883 }
884 
885 static int amc6821_init_client(struct i2c_client *client)
886 {
887 	int config;
888 	int err = -EIO;
889 
890 	if (init) {
891 		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
892 
893 		if (config < 0) {
894 				dev_err(&client->dev,
895 			"Error reading configuration register, aborting.\n");
896 				return err;
897 		}
898 
899 		config |= AMC6821_CONF4_MODE;
900 
901 		if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4,
902 				config)) {
903 			dev_err(&client->dev,
904 			"Configuration register write error, aborting.\n");
905 			return err;
906 		}
907 
908 		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF3);
909 
910 		if (config < 0) {
911 			dev_err(&client->dev,
912 			"Error reading configuration register, aborting.\n");
913 			return err;
914 		}
915 
916 		dev_info(&client->dev, "Revision %d\n", config & 0x0f);
917 
918 		config &= ~AMC6821_CONF3_THERM_FAN_EN;
919 
920 		if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF3,
921 				config)) {
922 			dev_err(&client->dev,
923 			"Configuration register write error, aborting.\n");
924 			return err;
925 		}
926 
927 		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF2);
928 
929 		if (config < 0) {
930 			dev_err(&client->dev,
931 			"Error reading configuration register, aborting.\n");
932 			return err;
933 		}
934 
935 		config &= ~AMC6821_CONF2_RTFIE;
936 		config &= ~AMC6821_CONF2_LTOIE;
937 		config &= ~AMC6821_CONF2_RTOIE;
938 		if (i2c_smbus_write_byte_data(client,
939 				AMC6821_REG_CONF2, config)) {
940 			dev_err(&client->dev,
941 			"Configuration register write error, aborting.\n");
942 			return err;
943 		}
944 
945 		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
946 
947 		if (config < 0) {
948 			dev_err(&client->dev,
949 			"Error reading configuration register, aborting.\n");
950 			return err;
951 		}
952 
953 		config &= ~AMC6821_CONF1_THERMOVIE;
954 		config &= ~AMC6821_CONF1_FANIE;
955 		config |= AMC6821_CONF1_START;
956 		if (pwminv)
957 			config |= AMC6821_CONF1_PWMINV;
958 		else
959 			config &= ~AMC6821_CONF1_PWMINV;
960 
961 		if (i2c_smbus_write_byte_data(
962 				client, AMC6821_REG_CONF1, config)) {
963 			dev_err(&client->dev,
964 			"Configuration register write error, aborting.\n");
965 			return err;
966 		}
967 	}
968 	return 0;
969 }
970 
971 static int amc6821_probe(struct i2c_client *client,
972 			 const struct i2c_device_id *id)
973 {
974 	struct device *dev = &client->dev;
975 	struct amc6821_data *data;
976 	struct device *hwmon_dev;
977 	int err;
978 
979 	data = devm_kzalloc(dev, sizeof(struct amc6821_data), GFP_KERNEL);
980 	if (!data)
981 		return -ENOMEM;
982 
983 	data->client = client;
984 	mutex_init(&data->update_lock);
985 
986 	/*
987 	 * Initialize the amc6821 chip
988 	 */
989 	err = amc6821_init_client(client);
990 	if (err)
991 		return err;
992 
993 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
994 							   data,
995 							   amc6821_groups);
996 	return PTR_ERR_OR_ZERO(hwmon_dev);
997 }
998 
999 static const struct i2c_device_id amc6821_id[] = {
1000 	{ "amc6821", amc6821 },
1001 	{ }
1002 };
1003 
1004 MODULE_DEVICE_TABLE(i2c, amc6821_id);
1005 
1006 static struct i2c_driver amc6821_driver = {
1007 	.class = I2C_CLASS_HWMON,
1008 	.driver = {
1009 		.name	= "amc6821",
1010 	},
1011 	.probe = amc6821_probe,
1012 	.id_table = amc6821_id,
1013 	.detect = amc6821_detect,
1014 	.address_list = normal_i2c,
1015 };
1016 
1017 module_i2c_driver(amc6821_driver);
1018 
1019 MODULE_LICENSE("GPL");
1020 MODULE_AUTHOR("T. Mertelj <tomaz.mertelj@guest.arnes.si>");
1021 MODULE_DESCRIPTION("Texas Instruments amc6821 hwmon driver");
1022