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