xref: /linux/drivers/hwmon/ftsteutates.c (revision 95d002e0a34cb0f238abb39987f9980f325d8332)
1 /*
2  * Support for the FTS Systemmonitoring Chip "Teutates"
3  *
4  * Copyright (C) 2016 Fujitsu Technology Solutions GmbH,
5  *		  Thilo Cestonaro <thilo.cestonaro@ts.fujitsu.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  */
18 #include <linux/err.h>
19 #include <linux/fs.h>
20 #include <linux/hwmon.h>
21 #include <linux/hwmon-sysfs.h>
22 #include <linux/i2c.h>
23 #include <linux/init.h>
24 #include <linux/jiffies.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/slab.h>
28 #include <linux/sysfs.h>
29 #include <linux/uaccess.h>
30 #include <linux/watchdog.h>
31 
32 #define FTS_DEVICE_ID_REG		0x0000
33 #define FTS_DEVICE_REVISION_REG		0x0001
34 #define FTS_DEVICE_STATUS_REG		0x0004
35 #define FTS_SATELLITE_STATUS_REG	0x0005
36 #define FTS_EVENT_STATUS_REG		0x0006
37 #define FTS_GLOBAL_CONTROL_REG		0x0007
38 
39 #define FTS_DEVICE_DETECT_REG_1		0x0C
40 #define FTS_DEVICE_DETECT_REG_2		0x0D
41 #define FTS_DEVICE_DETECT_REG_3		0x0E
42 
43 #define FTS_SENSOR_EVENT_REG		0x0010
44 
45 #define FTS_FAN_EVENT_REG		0x0014
46 #define FTS_FAN_PRESENT_REG		0x0015
47 
48 #define FTS_POWER_ON_TIME_COUNTER_A	0x007A
49 #define FTS_POWER_ON_TIME_COUNTER_B	0x007B
50 #define FTS_POWER_ON_TIME_COUNTER_C	0x007C
51 
52 #define FTS_PAGE_SELECT_REG		0x007F
53 
54 #define FTS_WATCHDOG_TIME_PRESET	0x000B
55 #define FTS_WATCHDOG_CONTROL		0x5081
56 
57 #define FTS_NO_FAN_SENSORS		0x08
58 #define FTS_NO_TEMP_SENSORS		0x10
59 #define FTS_NO_VOLT_SENSORS		0x04
60 
61 static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
62 
63 static const struct i2c_device_id fts_id[] = {
64 	{ "ftsteutates", 0 },
65 	{ }
66 };
67 MODULE_DEVICE_TABLE(i2c, fts_id);
68 
69 enum WATCHDOG_RESOLUTION {
70 	seconds = 1,
71 	minutes = 60
72 };
73 
74 struct fts_data {
75 	struct i2c_client *client;
76 	/* update sensor data lock */
77 	struct mutex update_lock;
78 	/* read/write register lock */
79 	struct mutex access_lock;
80 	unsigned long last_updated; /* in jiffies */
81 	struct watchdog_device wdd;
82 	enum WATCHDOG_RESOLUTION resolution;
83 	bool valid; /* false until following fields are valid */
84 
85 	u8 volt[FTS_NO_VOLT_SENSORS];
86 
87 	u8 temp_input[FTS_NO_TEMP_SENSORS];
88 	u8 temp_alarm;
89 
90 	u8 fan_present;
91 	u8 fan_input[FTS_NO_FAN_SENSORS]; /* in rps */
92 	u8 fan_source[FTS_NO_FAN_SENSORS];
93 	u8 fan_alarm;
94 };
95 
96 #define FTS_REG_FAN_INPUT(idx) ((idx) + 0x20)
97 #define FTS_REG_FAN_SOURCE(idx) ((idx) + 0x30)
98 #define FTS_REG_FAN_CONTROL(idx) (((idx) << 16) + 0x4881)
99 
100 #define FTS_REG_TEMP_INPUT(idx) ((idx) + 0x40)
101 #define FTS_REG_TEMP_CONTROL(idx) (((idx) << 16) + 0x0681)
102 
103 #define FTS_REG_VOLT(idx) ((idx) + 0x18)
104 
105 /*****************************************************************************/
106 /* I2C Helper functions							     */
107 /*****************************************************************************/
108 static int fts_read_byte(struct i2c_client *client, unsigned short reg)
109 {
110 	int ret;
111 	unsigned char page = reg >> 8;
112 	struct fts_data *data = dev_get_drvdata(&client->dev);
113 
114 	mutex_lock(&data->access_lock);
115 
116 	dev_dbg(&client->dev, "page select - page: 0x%.02x\n", page);
117 	ret = i2c_smbus_write_byte_data(client, FTS_PAGE_SELECT_REG, page);
118 	if (ret < 0)
119 		goto error;
120 
121 	reg &= 0xFF;
122 	ret = i2c_smbus_read_byte_data(client, reg);
123 	dev_dbg(&client->dev, "read - reg: 0x%.02x: val: 0x%.02x\n", reg, ret);
124 
125 error:
126 	mutex_unlock(&data->access_lock);
127 	return ret;
128 }
129 
130 static int fts_write_byte(struct i2c_client *client, unsigned short reg,
131 			  unsigned char value)
132 {
133 	int ret;
134 	unsigned char page = reg >> 8;
135 	struct fts_data *data = dev_get_drvdata(&client->dev);
136 
137 	mutex_lock(&data->access_lock);
138 
139 	dev_dbg(&client->dev, "page select - page: 0x%.02x\n", page);
140 	ret = i2c_smbus_write_byte_data(client, FTS_PAGE_SELECT_REG, page);
141 	if (ret < 0)
142 		goto error;
143 
144 	reg &= 0xFF;
145 	dev_dbg(&client->dev,
146 		"write - reg: 0x%.02x: val: 0x%.02x\n", reg, value);
147 	ret = i2c_smbus_write_byte_data(client, reg, value);
148 
149 error:
150 	mutex_unlock(&data->access_lock);
151 	return ret;
152 }
153 
154 /*****************************************************************************/
155 /* Data Updater Helper function						     */
156 /*****************************************************************************/
157 static int fts_update_device(struct fts_data *data)
158 {
159 	int i;
160 	int err = 0;
161 
162 	mutex_lock(&data->update_lock);
163 	if (!time_after(jiffies, data->last_updated + 2 * HZ) && data->valid)
164 		goto exit;
165 
166 	err = fts_read_byte(data->client, FTS_DEVICE_STATUS_REG);
167 	if (err < 0)
168 		goto exit;
169 
170 	data->valid = !!(err & 0x02); /* Data not ready yet */
171 	if (unlikely(!data->valid)) {
172 		err = -EAGAIN;
173 		goto exit;
174 	}
175 
176 	err = fts_read_byte(data->client, FTS_FAN_PRESENT_REG);
177 	if (err < 0)
178 		goto exit;
179 	data->fan_present = err;
180 
181 	err = fts_read_byte(data->client, FTS_FAN_EVENT_REG);
182 	if (err < 0)
183 		goto exit;
184 	data->fan_alarm = err;
185 
186 	for (i = 0; i < FTS_NO_FAN_SENSORS; i++) {
187 		if (data->fan_present & BIT(i)) {
188 			err = fts_read_byte(data->client, FTS_REG_FAN_INPUT(i));
189 			if (err < 0)
190 				goto exit;
191 			data->fan_input[i] = err;
192 
193 			err = fts_read_byte(data->client,
194 					    FTS_REG_FAN_SOURCE(i));
195 			if (err < 0)
196 				goto exit;
197 			data->fan_source[i] = err;
198 		} else {
199 			data->fan_input[i] = 0;
200 			data->fan_source[i] = 0;
201 		}
202 	}
203 
204 	err = fts_read_byte(data->client, FTS_SENSOR_EVENT_REG);
205 	if (err < 0)
206 		goto exit;
207 	data->temp_alarm = err;
208 
209 	for (i = 0; i < FTS_NO_TEMP_SENSORS; i++) {
210 		err = fts_read_byte(data->client, FTS_REG_TEMP_INPUT(i));
211 		if (err < 0)
212 			goto exit;
213 		data->temp_input[i] = err;
214 	}
215 
216 	for (i = 0; i < FTS_NO_VOLT_SENSORS; i++) {
217 		err = fts_read_byte(data->client, FTS_REG_VOLT(i));
218 		if (err < 0)
219 			goto exit;
220 		data->volt[i] = err;
221 	}
222 	data->last_updated = jiffies;
223 	err = 0;
224 exit:
225 	mutex_unlock(&data->update_lock);
226 	return err;
227 }
228 
229 /*****************************************************************************/
230 /* Watchdog functions							     */
231 /*****************************************************************************/
232 static int fts_wd_set_resolution(struct fts_data *data,
233 				 enum WATCHDOG_RESOLUTION resolution)
234 {
235 	int ret;
236 
237 	if (data->resolution == resolution)
238 		return 0;
239 
240 	ret = fts_read_byte(data->client, FTS_WATCHDOG_CONTROL);
241 	if (ret < 0)
242 		return ret;
243 
244 	if ((resolution == seconds && ret & BIT(1)) ||
245 	    (resolution == minutes && (ret & BIT(1)) == 0)) {
246 		data->resolution = resolution;
247 		return 0;
248 	}
249 
250 	if (resolution == seconds)
251 		ret |= BIT(1);
252 	else
253 		ret &= ~BIT(1);
254 
255 	ret = fts_write_byte(data->client, FTS_WATCHDOG_CONTROL, ret);
256 	if (ret < 0)
257 		return ret;
258 
259 	data->resolution = resolution;
260 	return ret;
261 }
262 
263 static int fts_wd_set_timeout(struct watchdog_device *wdd, unsigned int timeout)
264 {
265 	struct fts_data *data;
266 	enum WATCHDOG_RESOLUTION resolution = seconds;
267 	int ret;
268 
269 	data = watchdog_get_drvdata(wdd);
270 	/* switch watchdog resolution to minutes if timeout does not fit
271 	 * into a byte
272 	 */
273 	if (timeout > 0xFF) {
274 		timeout = DIV_ROUND_UP(timeout, 60) * 60;
275 		resolution = minutes;
276 	}
277 
278 	ret = fts_wd_set_resolution(data, resolution);
279 	if (ret < 0)
280 		return ret;
281 
282 	wdd->timeout = timeout;
283 	return 0;
284 }
285 
286 static int fts_wd_start(struct watchdog_device *wdd)
287 {
288 	struct fts_data *data = watchdog_get_drvdata(wdd);
289 
290 	return fts_write_byte(data->client, FTS_WATCHDOG_TIME_PRESET,
291 			      wdd->timeout / (u8)data->resolution);
292 }
293 
294 static int fts_wd_stop(struct watchdog_device *wdd)
295 {
296 	struct fts_data *data;
297 
298 	data = watchdog_get_drvdata(wdd);
299 	return fts_write_byte(data->client, FTS_WATCHDOG_TIME_PRESET, 0);
300 }
301 
302 static const struct watchdog_info fts_wd_info = {
303 	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
304 	.identity = "FTS Teutates Hardware Watchdog",
305 };
306 
307 static const struct watchdog_ops fts_wd_ops = {
308 	.owner = THIS_MODULE,
309 	.start = fts_wd_start,
310 	.stop = fts_wd_stop,
311 	.set_timeout = fts_wd_set_timeout,
312 };
313 
314 static int fts_watchdog_init(struct fts_data *data)
315 {
316 	int timeout, ret;
317 
318 	watchdog_set_drvdata(&data->wdd, data);
319 
320 	timeout = fts_read_byte(data->client, FTS_WATCHDOG_TIME_PRESET);
321 	if (timeout < 0)
322 		return timeout;
323 
324 	/* watchdog not running, set timeout to a default of 60 sec. */
325 	if (timeout == 0) {
326 		ret = fts_wd_set_resolution(data, seconds);
327 		if (ret < 0)
328 			return ret;
329 		data->wdd.timeout = 60;
330 	} else {
331 		ret = fts_read_byte(data->client, FTS_WATCHDOG_CONTROL);
332 		if (ret < 0)
333 			return ret;
334 
335 		data->resolution = ret & BIT(1) ? seconds : minutes;
336 		data->wdd.timeout = timeout * (u8)data->resolution;
337 		set_bit(WDOG_HW_RUNNING, &data->wdd.status);
338 	}
339 
340 	/* Register our watchdog part */
341 	data->wdd.info = &fts_wd_info;
342 	data->wdd.ops = &fts_wd_ops;
343 	data->wdd.parent = &data->client->dev;
344 	data->wdd.min_timeout = 1;
345 
346 	/* max timeout 255 minutes. */
347 	data->wdd.max_hw_heartbeat_ms = 0xFF * 60 * MSEC_PER_SEC;
348 
349 	return watchdog_register_device(&data->wdd);
350 }
351 
352 /*****************************************************************************/
353 /* SysFS handler functions						     */
354 /*****************************************************************************/
355 static ssize_t in_value_show(struct device *dev,
356 			     struct device_attribute *devattr, char *buf)
357 {
358 	struct fts_data *data = dev_get_drvdata(dev);
359 	int index = to_sensor_dev_attr(devattr)->index;
360 	int err;
361 
362 	err = fts_update_device(data);
363 	if (err < 0)
364 		return err;
365 
366 	return sprintf(buf, "%u\n", data->volt[index]);
367 }
368 
369 static ssize_t temp_value_show(struct device *dev,
370 			       struct device_attribute *devattr, char *buf)
371 {
372 	struct fts_data *data = dev_get_drvdata(dev);
373 	int index = to_sensor_dev_attr(devattr)->index;
374 	int err;
375 
376 	err = fts_update_device(data);
377 	if (err < 0)
378 		return err;
379 
380 	return sprintf(buf, "%u\n", data->temp_input[index]);
381 }
382 
383 static ssize_t temp_fault_show(struct device *dev,
384 			       struct device_attribute *devattr, char *buf)
385 {
386 	struct fts_data *data = dev_get_drvdata(dev);
387 	int index = to_sensor_dev_attr(devattr)->index;
388 	int err;
389 
390 	err = fts_update_device(data);
391 	if (err < 0)
392 		return err;
393 
394 	/* 00h Temperature = Sensor Error */
395 	return sprintf(buf, "%d\n", data->temp_input[index] == 0);
396 }
397 
398 static ssize_t temp_alarm_show(struct device *dev,
399 			       struct device_attribute *devattr, char *buf)
400 {
401 	struct fts_data *data = dev_get_drvdata(dev);
402 	int index = to_sensor_dev_attr(devattr)->index;
403 	int err;
404 
405 	err = fts_update_device(data);
406 	if (err < 0)
407 		return err;
408 
409 	return sprintf(buf, "%u\n", !!(data->temp_alarm & BIT(index)));
410 }
411 
412 static ssize_t
413 temp_alarm_store(struct device *dev, struct device_attribute *devattr,
414 		 const char *buf, size_t count)
415 {
416 	struct fts_data *data = dev_get_drvdata(dev);
417 	int index = to_sensor_dev_attr(devattr)->index;
418 	long ret;
419 
420 	ret = fts_update_device(data);
421 	if (ret < 0)
422 		return ret;
423 
424 	if (kstrtoul(buf, 10, &ret) || ret != 0)
425 		return -EINVAL;
426 
427 	mutex_lock(&data->update_lock);
428 	ret = fts_read_byte(data->client, FTS_REG_TEMP_CONTROL(index));
429 	if (ret < 0)
430 		goto error;
431 
432 	ret = fts_write_byte(data->client, FTS_REG_TEMP_CONTROL(index),
433 			     ret | 0x1);
434 	if (ret < 0)
435 		goto error;
436 
437 	data->valid = false;
438 	ret = count;
439 error:
440 	mutex_unlock(&data->update_lock);
441 	return ret;
442 }
443 
444 static ssize_t fan_value_show(struct device *dev,
445 			      struct device_attribute *devattr, char *buf)
446 {
447 	struct fts_data *data = dev_get_drvdata(dev);
448 	int index = to_sensor_dev_attr(devattr)->index;
449 	int err;
450 
451 	err = fts_update_device(data);
452 	if (err < 0)
453 		return err;
454 
455 	return sprintf(buf, "%u\n", data->fan_input[index]);
456 }
457 
458 static ssize_t fan_source_show(struct device *dev,
459 			       struct device_attribute *devattr, char *buf)
460 {
461 	struct fts_data *data = dev_get_drvdata(dev);
462 	int index = to_sensor_dev_attr(devattr)->index;
463 	int err;
464 
465 	err = fts_update_device(data);
466 	if (err < 0)
467 		return err;
468 
469 	return sprintf(buf, "%u\n", data->fan_source[index]);
470 }
471 
472 static ssize_t fan_alarm_show(struct device *dev,
473 			      struct device_attribute *devattr, char *buf)
474 {
475 	struct fts_data *data = dev_get_drvdata(dev);
476 	int index = to_sensor_dev_attr(devattr)->index;
477 	int err;
478 
479 	err = fts_update_device(data);
480 	if (err < 0)
481 		return err;
482 
483 	return sprintf(buf, "%d\n", !!(data->fan_alarm & BIT(index)));
484 }
485 
486 static ssize_t
487 fan_alarm_store(struct device *dev, struct device_attribute *devattr,
488 		const char *buf, size_t count)
489 {
490 	struct fts_data *data = dev_get_drvdata(dev);
491 	int index = to_sensor_dev_attr(devattr)->index;
492 	long ret;
493 
494 	ret = fts_update_device(data);
495 	if (ret < 0)
496 		return ret;
497 
498 	if (kstrtoul(buf, 10, &ret) || ret != 0)
499 		return -EINVAL;
500 
501 	mutex_lock(&data->update_lock);
502 	ret = fts_read_byte(data->client, FTS_REG_FAN_CONTROL(index));
503 	if (ret < 0)
504 		goto error;
505 
506 	ret = fts_write_byte(data->client, FTS_REG_FAN_CONTROL(index),
507 			     ret | 0x1);
508 	if (ret < 0)
509 		goto error;
510 
511 	data->valid = false;
512 	ret = count;
513 error:
514 	mutex_unlock(&data->update_lock);
515 	return ret;
516 }
517 
518 /*****************************************************************************/
519 /* SysFS structs							     */
520 /*****************************************************************************/
521 
522 /* Temprature sensors */
523 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_value, 0);
524 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_value, 1);
525 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp_value, 2);
526 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp_value, 3);
527 static SENSOR_DEVICE_ATTR_RO(temp5_input, temp_value, 4);
528 static SENSOR_DEVICE_ATTR_RO(temp6_input, temp_value, 5);
529 static SENSOR_DEVICE_ATTR_RO(temp7_input, temp_value, 6);
530 static SENSOR_DEVICE_ATTR_RO(temp8_input, temp_value, 7);
531 static SENSOR_DEVICE_ATTR_RO(temp9_input, temp_value, 8);
532 static SENSOR_DEVICE_ATTR_RO(temp10_input, temp_value, 9);
533 static SENSOR_DEVICE_ATTR_RO(temp11_input, temp_value, 10);
534 static SENSOR_DEVICE_ATTR_RO(temp12_input, temp_value, 11);
535 static SENSOR_DEVICE_ATTR_RO(temp13_input, temp_value, 12);
536 static SENSOR_DEVICE_ATTR_RO(temp14_input, temp_value, 13);
537 static SENSOR_DEVICE_ATTR_RO(temp15_input, temp_value, 14);
538 static SENSOR_DEVICE_ATTR_RO(temp16_input, temp_value, 15);
539 
540 static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0);
541 static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1);
542 static SENSOR_DEVICE_ATTR_RO(temp3_fault, temp_fault, 2);
543 static SENSOR_DEVICE_ATTR_RO(temp4_fault, temp_fault, 3);
544 static SENSOR_DEVICE_ATTR_RO(temp5_fault, temp_fault, 4);
545 static SENSOR_DEVICE_ATTR_RO(temp6_fault, temp_fault, 5);
546 static SENSOR_DEVICE_ATTR_RO(temp7_fault, temp_fault, 6);
547 static SENSOR_DEVICE_ATTR_RO(temp8_fault, temp_fault, 7);
548 static SENSOR_DEVICE_ATTR_RO(temp9_fault, temp_fault, 8);
549 static SENSOR_DEVICE_ATTR_RO(temp10_fault, temp_fault, 9);
550 static SENSOR_DEVICE_ATTR_RO(temp11_fault, temp_fault, 10);
551 static SENSOR_DEVICE_ATTR_RO(temp12_fault, temp_fault, 11);
552 static SENSOR_DEVICE_ATTR_RO(temp13_fault, temp_fault, 12);
553 static SENSOR_DEVICE_ATTR_RO(temp14_fault, temp_fault, 13);
554 static SENSOR_DEVICE_ATTR_RO(temp15_fault, temp_fault, 14);
555 static SENSOR_DEVICE_ATTR_RO(temp16_fault, temp_fault, 15);
556 
557 static SENSOR_DEVICE_ATTR_RW(temp1_alarm, temp_alarm, 0);
558 static SENSOR_DEVICE_ATTR_RW(temp2_alarm, temp_alarm, 1);
559 static SENSOR_DEVICE_ATTR_RW(temp3_alarm, temp_alarm, 2);
560 static SENSOR_DEVICE_ATTR_RW(temp4_alarm, temp_alarm, 3);
561 static SENSOR_DEVICE_ATTR_RW(temp5_alarm, temp_alarm, 4);
562 static SENSOR_DEVICE_ATTR_RW(temp6_alarm, temp_alarm, 5);
563 static SENSOR_DEVICE_ATTR_RW(temp7_alarm, temp_alarm, 6);
564 static SENSOR_DEVICE_ATTR_RW(temp8_alarm, temp_alarm, 7);
565 static SENSOR_DEVICE_ATTR_RW(temp9_alarm, temp_alarm, 8);
566 static SENSOR_DEVICE_ATTR_RW(temp10_alarm, temp_alarm, 9);
567 static SENSOR_DEVICE_ATTR_RW(temp11_alarm, temp_alarm, 10);
568 static SENSOR_DEVICE_ATTR_RW(temp12_alarm, temp_alarm, 11);
569 static SENSOR_DEVICE_ATTR_RW(temp13_alarm, temp_alarm, 12);
570 static SENSOR_DEVICE_ATTR_RW(temp14_alarm, temp_alarm, 13);
571 static SENSOR_DEVICE_ATTR_RW(temp15_alarm, temp_alarm, 14);
572 static SENSOR_DEVICE_ATTR_RW(temp16_alarm, temp_alarm, 15);
573 
574 static struct attribute *fts_temp_attrs[] = {
575 	&sensor_dev_attr_temp1_input.dev_attr.attr,
576 	&sensor_dev_attr_temp2_input.dev_attr.attr,
577 	&sensor_dev_attr_temp3_input.dev_attr.attr,
578 	&sensor_dev_attr_temp4_input.dev_attr.attr,
579 	&sensor_dev_attr_temp5_input.dev_attr.attr,
580 	&sensor_dev_attr_temp6_input.dev_attr.attr,
581 	&sensor_dev_attr_temp7_input.dev_attr.attr,
582 	&sensor_dev_attr_temp8_input.dev_attr.attr,
583 	&sensor_dev_attr_temp9_input.dev_attr.attr,
584 	&sensor_dev_attr_temp10_input.dev_attr.attr,
585 	&sensor_dev_attr_temp11_input.dev_attr.attr,
586 	&sensor_dev_attr_temp12_input.dev_attr.attr,
587 	&sensor_dev_attr_temp13_input.dev_attr.attr,
588 	&sensor_dev_attr_temp14_input.dev_attr.attr,
589 	&sensor_dev_attr_temp15_input.dev_attr.attr,
590 	&sensor_dev_attr_temp16_input.dev_attr.attr,
591 
592 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
593 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
594 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
595 	&sensor_dev_attr_temp4_fault.dev_attr.attr,
596 	&sensor_dev_attr_temp5_fault.dev_attr.attr,
597 	&sensor_dev_attr_temp6_fault.dev_attr.attr,
598 	&sensor_dev_attr_temp7_fault.dev_attr.attr,
599 	&sensor_dev_attr_temp8_fault.dev_attr.attr,
600 	&sensor_dev_attr_temp9_fault.dev_attr.attr,
601 	&sensor_dev_attr_temp10_fault.dev_attr.attr,
602 	&sensor_dev_attr_temp11_fault.dev_attr.attr,
603 	&sensor_dev_attr_temp12_fault.dev_attr.attr,
604 	&sensor_dev_attr_temp13_fault.dev_attr.attr,
605 	&sensor_dev_attr_temp14_fault.dev_attr.attr,
606 	&sensor_dev_attr_temp15_fault.dev_attr.attr,
607 	&sensor_dev_attr_temp16_fault.dev_attr.attr,
608 
609 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
610 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
611 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
612 	&sensor_dev_attr_temp4_alarm.dev_attr.attr,
613 	&sensor_dev_attr_temp5_alarm.dev_attr.attr,
614 	&sensor_dev_attr_temp6_alarm.dev_attr.attr,
615 	&sensor_dev_attr_temp7_alarm.dev_attr.attr,
616 	&sensor_dev_attr_temp8_alarm.dev_attr.attr,
617 	&sensor_dev_attr_temp9_alarm.dev_attr.attr,
618 	&sensor_dev_attr_temp10_alarm.dev_attr.attr,
619 	&sensor_dev_attr_temp11_alarm.dev_attr.attr,
620 	&sensor_dev_attr_temp12_alarm.dev_attr.attr,
621 	&sensor_dev_attr_temp13_alarm.dev_attr.attr,
622 	&sensor_dev_attr_temp14_alarm.dev_attr.attr,
623 	&sensor_dev_attr_temp15_alarm.dev_attr.attr,
624 	&sensor_dev_attr_temp16_alarm.dev_attr.attr,
625 	NULL
626 };
627 
628 /* Fans */
629 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_value, 0);
630 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_value, 1);
631 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan_value, 2);
632 static SENSOR_DEVICE_ATTR_RO(fan4_input, fan_value, 3);
633 static SENSOR_DEVICE_ATTR_RO(fan5_input, fan_value, 4);
634 static SENSOR_DEVICE_ATTR_RO(fan6_input, fan_value, 5);
635 static SENSOR_DEVICE_ATTR_RO(fan7_input, fan_value, 6);
636 static SENSOR_DEVICE_ATTR_RO(fan8_input, fan_value, 7);
637 
638 static SENSOR_DEVICE_ATTR_RO(fan1_source, fan_source, 0);
639 static SENSOR_DEVICE_ATTR_RO(fan2_source, fan_source, 1);
640 static SENSOR_DEVICE_ATTR_RO(fan3_source, fan_source, 2);
641 static SENSOR_DEVICE_ATTR_RO(fan4_source, fan_source, 3);
642 static SENSOR_DEVICE_ATTR_RO(fan5_source, fan_source, 4);
643 static SENSOR_DEVICE_ATTR_RO(fan6_source, fan_source, 5);
644 static SENSOR_DEVICE_ATTR_RO(fan7_source, fan_source, 6);
645 static SENSOR_DEVICE_ATTR_RO(fan8_source, fan_source, 7);
646 
647 static SENSOR_DEVICE_ATTR_RW(fan1_alarm, fan_alarm, 0);
648 static SENSOR_DEVICE_ATTR_RW(fan2_alarm, fan_alarm, 1);
649 static SENSOR_DEVICE_ATTR_RW(fan3_alarm, fan_alarm, 2);
650 static SENSOR_DEVICE_ATTR_RW(fan4_alarm, fan_alarm, 3);
651 static SENSOR_DEVICE_ATTR_RW(fan5_alarm, fan_alarm, 4);
652 static SENSOR_DEVICE_ATTR_RW(fan6_alarm, fan_alarm, 5);
653 static SENSOR_DEVICE_ATTR_RW(fan7_alarm, fan_alarm, 6);
654 static SENSOR_DEVICE_ATTR_RW(fan8_alarm, fan_alarm, 7);
655 
656 static struct attribute *fts_fan_attrs[] = {
657 	&sensor_dev_attr_fan1_input.dev_attr.attr,
658 	&sensor_dev_attr_fan2_input.dev_attr.attr,
659 	&sensor_dev_attr_fan3_input.dev_attr.attr,
660 	&sensor_dev_attr_fan4_input.dev_attr.attr,
661 	&sensor_dev_attr_fan5_input.dev_attr.attr,
662 	&sensor_dev_attr_fan6_input.dev_attr.attr,
663 	&sensor_dev_attr_fan7_input.dev_attr.attr,
664 	&sensor_dev_attr_fan8_input.dev_attr.attr,
665 
666 	&sensor_dev_attr_fan1_source.dev_attr.attr,
667 	&sensor_dev_attr_fan2_source.dev_attr.attr,
668 	&sensor_dev_attr_fan3_source.dev_attr.attr,
669 	&sensor_dev_attr_fan4_source.dev_attr.attr,
670 	&sensor_dev_attr_fan5_source.dev_attr.attr,
671 	&sensor_dev_attr_fan6_source.dev_attr.attr,
672 	&sensor_dev_attr_fan7_source.dev_attr.attr,
673 	&sensor_dev_attr_fan8_source.dev_attr.attr,
674 
675 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
676 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
677 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
678 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
679 	&sensor_dev_attr_fan5_alarm.dev_attr.attr,
680 	&sensor_dev_attr_fan6_alarm.dev_attr.attr,
681 	&sensor_dev_attr_fan7_alarm.dev_attr.attr,
682 	&sensor_dev_attr_fan8_alarm.dev_attr.attr,
683 	NULL
684 };
685 
686 /* Voltages */
687 static SENSOR_DEVICE_ATTR_RO(in1_input, in_value, 0);
688 static SENSOR_DEVICE_ATTR_RO(in2_input, in_value, 1);
689 static SENSOR_DEVICE_ATTR_RO(in3_input, in_value, 2);
690 static SENSOR_DEVICE_ATTR_RO(in4_input, in_value, 3);
691 static struct attribute *fts_voltage_attrs[] = {
692 	&sensor_dev_attr_in1_input.dev_attr.attr,
693 	&sensor_dev_attr_in2_input.dev_attr.attr,
694 	&sensor_dev_attr_in3_input.dev_attr.attr,
695 	&sensor_dev_attr_in4_input.dev_attr.attr,
696 	NULL
697 };
698 
699 static const struct attribute_group fts_voltage_attr_group = {
700 	.attrs = fts_voltage_attrs
701 };
702 
703 static const struct attribute_group fts_temp_attr_group = {
704 	.attrs = fts_temp_attrs
705 };
706 
707 static const struct attribute_group fts_fan_attr_group = {
708 	.attrs = fts_fan_attrs
709 };
710 
711 static const struct attribute_group *fts_attr_groups[] = {
712 	&fts_voltage_attr_group,
713 	&fts_temp_attr_group,
714 	&fts_fan_attr_group,
715 	NULL
716 };
717 
718 /*****************************************************************************/
719 /* Module initialization / remove functions				     */
720 /*****************************************************************************/
721 static int fts_detect(struct i2c_client *client,
722 		      struct i2c_board_info *info)
723 {
724 	int val;
725 
726 	/* detection works with revsion greater or equal to 0x2b */
727 	val = i2c_smbus_read_byte_data(client, FTS_DEVICE_REVISION_REG);
728 	if (val < 0x2b)
729 		return -ENODEV;
730 
731 	/* Device Detect Regs must have 0x17 0x34 and 0x54 */
732 	val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_1);
733 	if (val != 0x17)
734 		return -ENODEV;
735 
736 	val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_2);
737 	if (val != 0x34)
738 		return -ENODEV;
739 
740 	val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_3);
741 	if (val != 0x54)
742 		return -ENODEV;
743 
744 	/*
745 	 * 0x10 == Baseboard Management Controller, 0x01 == Teutates
746 	 * Device ID Reg needs to be 0x11
747 	 */
748 	val = i2c_smbus_read_byte_data(client, FTS_DEVICE_ID_REG);
749 	if (val != 0x11)
750 		return -ENODEV;
751 
752 	strlcpy(info->type, fts_id[0].name, I2C_NAME_SIZE);
753 	info->flags = 0;
754 	return 0;
755 }
756 
757 static int fts_remove(struct i2c_client *client)
758 {
759 	struct fts_data *data = dev_get_drvdata(&client->dev);
760 
761 	watchdog_unregister_device(&data->wdd);
762 	return 0;
763 }
764 
765 static int fts_probe(struct i2c_client *client, const struct i2c_device_id *id)
766 {
767 	u8 revision;
768 	struct fts_data *data;
769 	int err;
770 	s8 deviceid;
771 	struct device *hwmon_dev;
772 
773 	if (client->addr != 0x73)
774 		return -ENODEV;
775 
776 	/* Baseboard Management Controller check */
777 	deviceid = i2c_smbus_read_byte_data(client, FTS_DEVICE_ID_REG);
778 	if (deviceid > 0 && (deviceid & 0xF0) == 0x10) {
779 		switch (deviceid & 0x0F) {
780 		case 0x01:
781 			break;
782 		default:
783 			dev_dbg(&client->dev,
784 				"No Baseboard Management Controller\n");
785 			return -ENODEV;
786 		}
787 	} else {
788 		dev_dbg(&client->dev, "No fujitsu board\n");
789 		return -ENODEV;
790 	}
791 
792 	data = devm_kzalloc(&client->dev, sizeof(struct fts_data),
793 			    GFP_KERNEL);
794 	if (!data)
795 		return -ENOMEM;
796 
797 	mutex_init(&data->update_lock);
798 	mutex_init(&data->access_lock);
799 	data->client = client;
800 	dev_set_drvdata(&client->dev, data);
801 
802 	err = i2c_smbus_read_byte_data(client, FTS_DEVICE_REVISION_REG);
803 	if (err < 0)
804 		return err;
805 	revision = err;
806 
807 	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
808 							   "ftsteutates",
809 							   data,
810 							   fts_attr_groups);
811 	if (IS_ERR(hwmon_dev))
812 		return PTR_ERR(hwmon_dev);
813 
814 	err = fts_watchdog_init(data);
815 	if (err)
816 		return err;
817 
818 	dev_info(&client->dev, "Detected FTS Teutates chip, revision: %d.%d\n",
819 		 (revision & 0xF0) >> 4, revision & 0x0F);
820 	return 0;
821 }
822 
823 /*****************************************************************************/
824 /* Module Details							     */
825 /*****************************************************************************/
826 static struct i2c_driver fts_driver = {
827 	.class = I2C_CLASS_HWMON,
828 	.driver = {
829 		.name = "ftsteutates",
830 	},
831 	.id_table = fts_id,
832 	.probe = fts_probe,
833 	.remove = fts_remove,
834 	.detect = fts_detect,
835 	.address_list = normal_i2c,
836 };
837 
838 module_i2c_driver(fts_driver);
839 
840 MODULE_AUTHOR("Thilo Cestonaro <thilo.cestonaro@ts.fujitsu.com>");
841 MODULE_DESCRIPTION("FTS Teutates driver");
842 MODULE_LICENSE("GPL");
843