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