sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / hwmon / ftsteutates.c
blob0f0277e7aae5140015ed179cab6ef419800cd4c7
1 /*
2 * Support for the FTS Systemmonitoring Chip "Teutates"
4 * Copyright (C) 2016 Fujitsu Technology Solutions GmbH,
5 * Thilo Cestonaro <thilo.cestonaro@ts.fujitsu.com>
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.
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.
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>
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
39 #define FTS_DEVICE_DETECT_REG_1 0x0C
40 #define FTS_DEVICE_DETECT_REG_2 0x0D
41 #define FTS_DEVICE_DETECT_REG_3 0x0E
43 #define FTS_SENSOR_EVENT_REG 0x0010
45 #define FTS_FAN_EVENT_REG 0x0014
46 #define FTS_FAN_PRESENT_REG 0x0015
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
52 #define FTS_PAGE_SELECT_REG 0x007F
54 #define FTS_WATCHDOG_TIME_PRESET 0x000B
55 #define FTS_WATCHDOG_CONTROL 0x5081
57 #define FTS_NO_FAN_SENSORS 0x08
58 #define FTS_NO_TEMP_SENSORS 0x10
59 #define FTS_NO_VOLT_SENSORS 0x04
61 static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
63 static struct i2c_device_id fts_id[] = {
64 { "ftsteutates", 0 },
65 { }
67 MODULE_DEVICE_TABLE(i2c, fts_id);
69 enum WATCHDOG_RESOLUTION {
70 seconds = 1,
71 minutes = 60
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 */
85 u8 volt[FTS_NO_VOLT_SENSORS];
87 u8 temp_input[FTS_NO_TEMP_SENSORS];
88 u8 temp_alarm;
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;
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)
100 #define FTS_REG_TEMP_INPUT(idx) ((idx) + 0x40)
101 #define FTS_REG_TEMP_CONTROL(idx) (((idx) << 16) + 0x0681)
103 #define FTS_REG_VOLT(idx) ((idx) + 0x18)
105 /*****************************************************************************/
106 /* I2C Helper functions */
107 /*****************************************************************************/
108 static int fts_read_byte(struct i2c_client *client, unsigned short reg)
110 int ret;
111 unsigned char page = reg >> 8;
112 struct fts_data *data = dev_get_drvdata(&client->dev);
114 mutex_lock(&data->access_lock);
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;
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);
125 error:
126 mutex_unlock(&data->access_lock);
127 return ret;
130 static int fts_write_byte(struct i2c_client *client, unsigned short reg,
131 unsigned char value)
133 int ret;
134 unsigned char page = reg >> 8;
135 struct fts_data *data = dev_get_drvdata(&client->dev);
137 mutex_lock(&data->access_lock);
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;
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);
149 error:
150 mutex_unlock(&data->access_lock);
151 return ret;
154 /*****************************************************************************/
155 /* Data Updater Helper function */
156 /*****************************************************************************/
157 static int fts_update_device(struct fts_data *data)
159 int i;
160 int err = 0;
162 mutex_lock(&data->update_lock);
163 if (!time_after(jiffies, data->last_updated + 2 * HZ) && data->valid)
164 goto exit;
166 err = fts_read_byte(data->client, FTS_DEVICE_STATUS_REG);
167 if (err < 0)
168 goto exit;
170 data->valid = !!(err & 0x02); /* Data not ready yet */
171 if (unlikely(!data->valid)) {
172 err = -EAGAIN;
173 goto exit;
176 err = fts_read_byte(data->client, FTS_FAN_PRESENT_REG);
177 if (err < 0)
178 goto exit;
179 data->fan_present = err;
181 err = fts_read_byte(data->client, FTS_FAN_EVENT_REG);
182 if (err < 0)
183 goto exit;
184 data->fan_alarm = err;
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;
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;
204 err = fts_read_byte(data->client, FTS_SENSOR_EVENT_REG);
205 if (err < 0)
206 goto exit;
207 data->temp_alarm = err;
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;
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;
222 data->last_updated = jiffies;
223 err = 0;
224 exit:
225 mutex_unlock(&data->update_lock);
226 return err;
229 /*****************************************************************************/
230 /* Watchdog functions */
231 /*****************************************************************************/
232 static int fts_wd_set_resolution(struct fts_data *data,
233 enum WATCHDOG_RESOLUTION resolution)
235 int ret;
237 if (data->resolution == resolution)
238 return 0;
240 ret = fts_read_byte(data->client, FTS_WATCHDOG_CONTROL);
241 if (ret < 0)
242 return ret;
244 if ((resolution == seconds && ret & BIT(1)) ||
245 (resolution == minutes && (ret & BIT(1)) == 0)) {
246 data->resolution = resolution;
247 return 0;
250 if (resolution == seconds)
251 ret |= BIT(1);
252 else
253 ret &= ~BIT(1);
255 ret = fts_write_byte(data->client, FTS_WATCHDOG_CONTROL, ret);
256 if (ret < 0)
257 return ret;
259 data->resolution = resolution;
260 return ret;
263 static int fts_wd_set_timeout(struct watchdog_device *wdd, unsigned int timeout)
265 struct fts_data *data;
266 enum WATCHDOG_RESOLUTION resolution = seconds;
267 int ret;
269 data = watchdog_get_drvdata(wdd);
270 /* switch watchdog resolution to minutes if timeout does not fit
271 * into a byte
273 if (timeout > 0xFF) {
274 timeout = DIV_ROUND_UP(timeout, 60) * 60;
275 resolution = minutes;
278 ret = fts_wd_set_resolution(data, resolution);
279 if (ret < 0)
280 return ret;
282 wdd->timeout = timeout;
283 return 0;
286 static int fts_wd_start(struct watchdog_device *wdd)
288 struct fts_data *data = watchdog_get_drvdata(wdd);
290 return fts_write_byte(data->client, FTS_WATCHDOG_TIME_PRESET,
291 wdd->timeout / (u8)data->resolution);
294 static int fts_wd_stop(struct watchdog_device *wdd)
296 struct fts_data *data;
298 data = watchdog_get_drvdata(wdd);
299 return fts_write_byte(data->client, FTS_WATCHDOG_TIME_PRESET, 0);
302 static const struct watchdog_info fts_wd_info = {
303 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
304 .identity = "FTS Teutates Hardware Watchdog",
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,
314 static int fts_watchdog_init(struct fts_data *data)
316 int timeout, ret;
318 watchdog_set_drvdata(&data->wdd, data);
320 timeout = fts_read_byte(data->client, FTS_WATCHDOG_TIME_PRESET);
321 if (timeout < 0)
322 return timeout;
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;
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);
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;
346 /* max timeout 255 minutes. */
347 data->wdd.max_hw_heartbeat_ms = 0xFF * 60 * MSEC_PER_SEC;
349 return watchdog_register_device(&data->wdd);
352 /*****************************************************************************/
353 /* SysFS handler functions */
354 /*****************************************************************************/
355 static ssize_t show_in_value(struct device *dev,
356 struct device_attribute *devattr, char *buf)
358 struct fts_data *data = dev_get_drvdata(dev);
359 int index = to_sensor_dev_attr(devattr)->index;
360 int err;
362 err = fts_update_device(data);
363 if (err < 0)
364 return err;
366 return sprintf(buf, "%u\n", data->volt[index]);
369 static ssize_t show_temp_value(struct device *dev,
370 struct device_attribute *devattr, char *buf)
372 struct fts_data *data = dev_get_drvdata(dev);
373 int index = to_sensor_dev_attr(devattr)->index;
374 int err;
376 err = fts_update_device(data);
377 if (err < 0)
378 return err;
380 return sprintf(buf, "%u\n", data->temp_input[index]);
383 static ssize_t show_temp_fault(struct device *dev,
384 struct device_attribute *devattr, char *buf)
386 struct fts_data *data = dev_get_drvdata(dev);
387 int index = to_sensor_dev_attr(devattr)->index;
388 int err;
390 err = fts_update_device(data);
391 if (err < 0)
392 return err;
394 /* 00h Temperature = Sensor Error */
395 return sprintf(buf, "%d\n", data->temp_input[index] == 0);
398 static ssize_t show_temp_alarm(struct device *dev,
399 struct device_attribute *devattr, char *buf)
401 struct fts_data *data = dev_get_drvdata(dev);
402 int index = to_sensor_dev_attr(devattr)->index;
403 int err;
405 err = fts_update_device(data);
406 if (err < 0)
407 return err;
409 return sprintf(buf, "%u\n", !!(data->temp_alarm & BIT(index)));
412 static ssize_t
413 clear_temp_alarm(struct device *dev, struct device_attribute *devattr,
414 const char *buf, size_t count)
416 struct fts_data *data = dev_get_drvdata(dev);
417 int index = to_sensor_dev_attr(devattr)->index;
418 long ret;
420 ret = fts_update_device(data);
421 if (ret < 0)
422 return ret;
424 if (kstrtoul(buf, 10, &ret) || ret != 0)
425 return -EINVAL;
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;
432 ret = fts_write_byte(data->client, FTS_REG_TEMP_CONTROL(index),
433 ret | 0x1);
434 if (ret < 0)
435 goto error;
437 data->valid = false;
438 error:
439 mutex_unlock(&data->update_lock);
440 return ret;
443 static ssize_t show_fan_value(struct device *dev,
444 struct device_attribute *devattr, char *buf)
446 struct fts_data *data = dev_get_drvdata(dev);
447 int index = to_sensor_dev_attr(devattr)->index;
448 int err;
450 err = fts_update_device(data);
451 if (err < 0)
452 return err;
454 return sprintf(buf, "%u\n", data->fan_input[index]);
457 static ssize_t show_fan_source(struct device *dev,
458 struct device_attribute *devattr, char *buf)
460 struct fts_data *data = dev_get_drvdata(dev);
461 int index = to_sensor_dev_attr(devattr)->index;
462 int err;
464 err = fts_update_device(data);
465 if (err < 0)
466 return err;
468 return sprintf(buf, "%u\n", data->fan_source[index]);
471 static ssize_t show_fan_alarm(struct device *dev,
472 struct device_attribute *devattr, char *buf)
474 struct fts_data *data = dev_get_drvdata(dev);
475 int index = to_sensor_dev_attr(devattr)->index;
476 int err;
478 err = fts_update_device(data);
479 if (err < 0)
480 return err;
482 return sprintf(buf, "%d\n", !!(data->fan_alarm & BIT(index)));
485 static ssize_t
486 clear_fan_alarm(struct device *dev, struct device_attribute *devattr,
487 const char *buf, size_t count)
489 struct fts_data *data = dev_get_drvdata(dev);
490 int index = to_sensor_dev_attr(devattr)->index;
491 long ret;
493 ret = fts_update_device(data);
494 if (ret < 0)
495 return ret;
497 if (kstrtoul(buf, 10, &ret) || ret != 0)
498 return -EINVAL;
500 mutex_lock(&data->update_lock);
501 ret = fts_read_byte(data->client, FTS_REG_FAN_CONTROL(index));
502 if (ret < 0)
503 goto error;
505 ret = fts_write_byte(data->client, FTS_REG_FAN_CONTROL(index),
506 ret | 0x1);
507 if (ret < 0)
508 goto error;
510 data->valid = false;
511 error:
512 mutex_unlock(&data->update_lock);
513 return ret;
516 /*****************************************************************************/
517 /* SysFS structs */
518 /*****************************************************************************/
520 /* Temprature sensors */
521 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_value, NULL, 0);
522 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_value, NULL, 1);
523 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_value, NULL, 2);
524 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_value, NULL, 3);
525 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp_value, NULL, 4);
526 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp_value, NULL, 5);
527 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp_value, NULL, 6);
528 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp_value, NULL, 7);
529 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp_value, NULL, 8);
530 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp_value, NULL, 9);
531 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO, show_temp_value, NULL, 10);
532 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO, show_temp_value, NULL, 11);
533 static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO, show_temp_value, NULL, 12);
534 static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO, show_temp_value, NULL, 13);
535 static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO, show_temp_value, NULL, 14);
536 static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO, show_temp_value, NULL, 15);
538 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
539 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
540 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2);
541 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3);
542 static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_temp_fault, NULL, 4);
543 static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_temp_fault, NULL, 5);
544 static SENSOR_DEVICE_ATTR(temp7_fault, S_IRUGO, show_temp_fault, NULL, 6);
545 static SENSOR_DEVICE_ATTR(temp8_fault, S_IRUGO, show_temp_fault, NULL, 7);
546 static SENSOR_DEVICE_ATTR(temp9_fault, S_IRUGO, show_temp_fault, NULL, 8);
547 static SENSOR_DEVICE_ATTR(temp10_fault, S_IRUGO, show_temp_fault, NULL, 9);
548 static SENSOR_DEVICE_ATTR(temp11_fault, S_IRUGO, show_temp_fault, NULL, 10);
549 static SENSOR_DEVICE_ATTR(temp12_fault, S_IRUGO, show_temp_fault, NULL, 11);
550 static SENSOR_DEVICE_ATTR(temp13_fault, S_IRUGO, show_temp_fault, NULL, 12);
551 static SENSOR_DEVICE_ATTR(temp14_fault, S_IRUGO, show_temp_fault, NULL, 13);
552 static SENSOR_DEVICE_ATTR(temp15_fault, S_IRUGO, show_temp_fault, NULL, 14);
553 static SENSOR_DEVICE_ATTR(temp16_fault, S_IRUGO, show_temp_fault, NULL, 15);
555 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
556 clear_temp_alarm, 0);
557 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
558 clear_temp_alarm, 1);
559 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
560 clear_temp_alarm, 2);
561 static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
562 clear_temp_alarm, 3);
563 static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
564 clear_temp_alarm, 4);
565 static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
566 clear_temp_alarm, 5);
567 static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
568 clear_temp_alarm, 6);
569 static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
570 clear_temp_alarm, 7);
571 static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
572 clear_temp_alarm, 8);
573 static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
574 clear_temp_alarm, 9);
575 static SENSOR_DEVICE_ATTR(temp11_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
576 clear_temp_alarm, 10);
577 static SENSOR_DEVICE_ATTR(temp12_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
578 clear_temp_alarm, 11);
579 static SENSOR_DEVICE_ATTR(temp13_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
580 clear_temp_alarm, 12);
581 static SENSOR_DEVICE_ATTR(temp14_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
582 clear_temp_alarm, 13);
583 static SENSOR_DEVICE_ATTR(temp15_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
584 clear_temp_alarm, 14);
585 static SENSOR_DEVICE_ATTR(temp16_alarm, S_IRUGO | S_IWUSR, show_temp_alarm,
586 clear_temp_alarm, 15);
588 static struct attribute *fts_temp_attrs[] = {
589 &sensor_dev_attr_temp1_input.dev_attr.attr,
590 &sensor_dev_attr_temp2_input.dev_attr.attr,
591 &sensor_dev_attr_temp3_input.dev_attr.attr,
592 &sensor_dev_attr_temp4_input.dev_attr.attr,
593 &sensor_dev_attr_temp5_input.dev_attr.attr,
594 &sensor_dev_attr_temp6_input.dev_attr.attr,
595 &sensor_dev_attr_temp7_input.dev_attr.attr,
596 &sensor_dev_attr_temp8_input.dev_attr.attr,
597 &sensor_dev_attr_temp9_input.dev_attr.attr,
598 &sensor_dev_attr_temp10_input.dev_attr.attr,
599 &sensor_dev_attr_temp11_input.dev_attr.attr,
600 &sensor_dev_attr_temp12_input.dev_attr.attr,
601 &sensor_dev_attr_temp13_input.dev_attr.attr,
602 &sensor_dev_attr_temp14_input.dev_attr.attr,
603 &sensor_dev_attr_temp15_input.dev_attr.attr,
604 &sensor_dev_attr_temp16_input.dev_attr.attr,
606 &sensor_dev_attr_temp1_fault.dev_attr.attr,
607 &sensor_dev_attr_temp2_fault.dev_attr.attr,
608 &sensor_dev_attr_temp3_fault.dev_attr.attr,
609 &sensor_dev_attr_temp4_fault.dev_attr.attr,
610 &sensor_dev_attr_temp5_fault.dev_attr.attr,
611 &sensor_dev_attr_temp6_fault.dev_attr.attr,
612 &sensor_dev_attr_temp7_fault.dev_attr.attr,
613 &sensor_dev_attr_temp8_fault.dev_attr.attr,
614 &sensor_dev_attr_temp9_fault.dev_attr.attr,
615 &sensor_dev_attr_temp10_fault.dev_attr.attr,
616 &sensor_dev_attr_temp11_fault.dev_attr.attr,
617 &sensor_dev_attr_temp12_fault.dev_attr.attr,
618 &sensor_dev_attr_temp13_fault.dev_attr.attr,
619 &sensor_dev_attr_temp14_fault.dev_attr.attr,
620 &sensor_dev_attr_temp15_fault.dev_attr.attr,
621 &sensor_dev_attr_temp16_fault.dev_attr.attr,
623 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
624 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
625 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
626 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
627 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
628 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
629 &sensor_dev_attr_temp7_alarm.dev_attr.attr,
630 &sensor_dev_attr_temp8_alarm.dev_attr.attr,
631 &sensor_dev_attr_temp9_alarm.dev_attr.attr,
632 &sensor_dev_attr_temp10_alarm.dev_attr.attr,
633 &sensor_dev_attr_temp11_alarm.dev_attr.attr,
634 &sensor_dev_attr_temp12_alarm.dev_attr.attr,
635 &sensor_dev_attr_temp13_alarm.dev_attr.attr,
636 &sensor_dev_attr_temp14_alarm.dev_attr.attr,
637 &sensor_dev_attr_temp15_alarm.dev_attr.attr,
638 &sensor_dev_attr_temp16_alarm.dev_attr.attr,
639 NULL
642 /* Fans */
643 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_value, NULL, 0);
644 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_value, NULL, 1);
645 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan_value, NULL, 2);
646 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan_value, NULL, 3);
647 static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, show_fan_value, NULL, 4);
648 static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, show_fan_value, NULL, 5);
649 static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, show_fan_value, NULL, 6);
650 static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, show_fan_value, NULL, 7);
652 static SENSOR_DEVICE_ATTR(fan1_source, S_IRUGO, show_fan_source, NULL, 0);
653 static SENSOR_DEVICE_ATTR(fan2_source, S_IRUGO, show_fan_source, NULL, 1);
654 static SENSOR_DEVICE_ATTR(fan3_source, S_IRUGO, show_fan_source, NULL, 2);
655 static SENSOR_DEVICE_ATTR(fan4_source, S_IRUGO, show_fan_source, NULL, 3);
656 static SENSOR_DEVICE_ATTR(fan5_source, S_IRUGO, show_fan_source, NULL, 4);
657 static SENSOR_DEVICE_ATTR(fan6_source, S_IRUGO, show_fan_source, NULL, 5);
658 static SENSOR_DEVICE_ATTR(fan7_source, S_IRUGO, show_fan_source, NULL, 6);
659 static SENSOR_DEVICE_ATTR(fan8_source, S_IRUGO, show_fan_source, NULL, 7);
661 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO | S_IWUSR,
662 show_fan_alarm, clear_fan_alarm, 0);
663 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO | S_IWUSR,
664 show_fan_alarm, clear_fan_alarm, 1);
665 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO | S_IWUSR,
666 show_fan_alarm, clear_fan_alarm, 2);
667 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO | S_IWUSR,
668 show_fan_alarm, clear_fan_alarm, 3);
669 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO | S_IWUSR,
670 show_fan_alarm, clear_fan_alarm, 4);
671 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO | S_IWUSR,
672 show_fan_alarm, clear_fan_alarm, 5);
673 static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO | S_IWUSR,
674 show_fan_alarm, clear_fan_alarm, 6);
675 static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO | S_IWUSR,
676 show_fan_alarm, clear_fan_alarm, 7);
678 static struct attribute *fts_fan_attrs[] = {
679 &sensor_dev_attr_fan1_input.dev_attr.attr,
680 &sensor_dev_attr_fan2_input.dev_attr.attr,
681 &sensor_dev_attr_fan3_input.dev_attr.attr,
682 &sensor_dev_attr_fan4_input.dev_attr.attr,
683 &sensor_dev_attr_fan5_input.dev_attr.attr,
684 &sensor_dev_attr_fan6_input.dev_attr.attr,
685 &sensor_dev_attr_fan7_input.dev_attr.attr,
686 &sensor_dev_attr_fan8_input.dev_attr.attr,
688 &sensor_dev_attr_fan1_source.dev_attr.attr,
689 &sensor_dev_attr_fan2_source.dev_attr.attr,
690 &sensor_dev_attr_fan3_source.dev_attr.attr,
691 &sensor_dev_attr_fan4_source.dev_attr.attr,
692 &sensor_dev_attr_fan5_source.dev_attr.attr,
693 &sensor_dev_attr_fan6_source.dev_attr.attr,
694 &sensor_dev_attr_fan7_source.dev_attr.attr,
695 &sensor_dev_attr_fan8_source.dev_attr.attr,
697 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
698 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
699 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
700 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
701 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
702 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
703 &sensor_dev_attr_fan7_alarm.dev_attr.attr,
704 &sensor_dev_attr_fan8_alarm.dev_attr.attr,
705 NULL
708 /* Voltages */
709 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_value, NULL, 0);
710 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_value, NULL, 1);
711 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_value, NULL, 2);
712 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_value, NULL, 3);
713 static struct attribute *fts_voltage_attrs[] = {
714 &sensor_dev_attr_in1_input.dev_attr.attr,
715 &sensor_dev_attr_in2_input.dev_attr.attr,
716 &sensor_dev_attr_in3_input.dev_attr.attr,
717 &sensor_dev_attr_in4_input.dev_attr.attr,
718 NULL
721 static const struct attribute_group fts_voltage_attr_group = {
722 .attrs = fts_voltage_attrs
725 static const struct attribute_group fts_temp_attr_group = {
726 .attrs = fts_temp_attrs
729 static const struct attribute_group fts_fan_attr_group = {
730 .attrs = fts_fan_attrs
733 static const struct attribute_group *fts_attr_groups[] = {
734 &fts_voltage_attr_group,
735 &fts_temp_attr_group,
736 &fts_fan_attr_group,
737 NULL
740 /*****************************************************************************/
741 /* Module initialization / remove functions */
742 /*****************************************************************************/
743 static int fts_detect(struct i2c_client *client,
744 struct i2c_board_info *info)
746 int val;
748 /* detection works with revsion greater or equal to 0x2b */
749 val = i2c_smbus_read_byte_data(client, FTS_DEVICE_REVISION_REG);
750 if (val < 0x2b)
751 return -ENODEV;
753 /* Device Detect Regs must have 0x17 0x34 and 0x54 */
754 val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_1);
755 if (val != 0x17)
756 return -ENODEV;
758 val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_2);
759 if (val != 0x34)
760 return -ENODEV;
762 val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_3);
763 if (val != 0x54)
764 return -ENODEV;
767 * 0x10 == Baseboard Management Controller, 0x01 == Teutates
768 * Device ID Reg needs to be 0x11
770 val = i2c_smbus_read_byte_data(client, FTS_DEVICE_ID_REG);
771 if (val != 0x11)
772 return -ENODEV;
774 strlcpy(info->type, fts_id[0].name, I2C_NAME_SIZE);
775 info->flags = 0;
776 return 0;
779 static int fts_remove(struct i2c_client *client)
781 struct fts_data *data = dev_get_drvdata(&client->dev);
783 watchdog_unregister_device(&data->wdd);
784 return 0;
787 static int fts_probe(struct i2c_client *client, const struct i2c_device_id *id)
789 u8 revision;
790 struct fts_data *data;
791 int err;
792 s8 deviceid;
793 struct device *hwmon_dev;
795 if (client->addr != 0x73)
796 return -ENODEV;
798 /* Baseboard Management Controller check */
799 deviceid = i2c_smbus_read_byte_data(client, FTS_DEVICE_ID_REG);
800 if (deviceid > 0 && (deviceid & 0xF0) == 0x10) {
801 switch (deviceid & 0x0F) {
802 case 0x01:
803 break;
804 default:
805 dev_dbg(&client->dev,
806 "No Baseboard Management Controller\n");
807 return -ENODEV;
809 } else {
810 dev_dbg(&client->dev, "No fujitsu board\n");
811 return -ENODEV;
814 data = devm_kzalloc(&client->dev, sizeof(struct fts_data),
815 GFP_KERNEL);
816 if (!data)
817 return -ENOMEM;
819 mutex_init(&data->update_lock);
820 mutex_init(&data->access_lock);
821 data->client = client;
822 dev_set_drvdata(&client->dev, data);
824 err = i2c_smbus_read_byte_data(client, FTS_DEVICE_REVISION_REG);
825 if (err < 0)
826 return err;
827 revision = err;
829 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
830 "ftsteutates",
831 data,
832 fts_attr_groups);
833 if (IS_ERR(hwmon_dev))
834 return PTR_ERR(hwmon_dev);
836 err = fts_watchdog_init(data);
837 if (err)
838 return err;
840 dev_info(&client->dev, "Detected FTS Teutates chip, revision: %d.%d\n",
841 (revision & 0xF0) >> 4, revision & 0x0F);
842 return 0;
845 /*****************************************************************************/
846 /* Module Details */
847 /*****************************************************************************/
848 static struct i2c_driver fts_driver = {
849 .class = I2C_CLASS_HWMON,
850 .driver = {
851 .name = "ftsteutates",
853 .id_table = fts_id,
854 .probe = fts_probe,
855 .remove = fts_remove,
856 .detect = fts_detect,
857 .address_list = normal_i2c,
860 module_i2c_driver(fts_driver);
862 MODULE_AUTHOR("Thilo Cestonaro <thilo.cestonaro@ts.fujitsu.com>");
863 MODULE_DESCRIPTION("FTS Teutates driver");
864 MODULE_LICENSE("GPL");