1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Support for the FTS Systemmonitoring Chip "Teutates"
5 * Copyright (C) 2016 Fujitsu Technology Solutions GmbH,
6 * Thilo Cestonaro <thilo.cestonaro@ts.fujitsu.com>
9 #include <linux/hwmon.h>
10 #include <linux/i2c.h>
11 #include <linux/init.h>
12 #include <linux/jiffies.h>
13 #include <linux/math.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/slab.h>
17 #include <linux/watchdog.h>
19 #define FTS_DEVICE_ID_REG 0x0000
20 #define FTS_DEVICE_REVISION_REG 0x0001
21 #define FTS_DEVICE_STATUS_REG 0x0004
22 #define FTS_SATELLITE_STATUS_REG 0x0005
23 #define FTS_EVENT_STATUS_REG 0x0006
24 #define FTS_GLOBAL_CONTROL_REG 0x0007
26 #define FTS_DEVICE_DETECT_REG_1 0x0C
27 #define FTS_DEVICE_DETECT_REG_2 0x0D
28 #define FTS_DEVICE_DETECT_REG_3 0x0E
30 #define FTS_SENSOR_EVENT_REG 0x0010
32 #define FTS_FAN_EVENT_REG 0x0014
33 #define FTS_FAN_PRESENT_REG 0x0015
35 #define FTS_POWER_ON_TIME_COUNTER_A 0x007A
36 #define FTS_POWER_ON_TIME_COUNTER_B 0x007B
37 #define FTS_POWER_ON_TIME_COUNTER_C 0x007C
39 #define FTS_PAGE_SELECT_REG 0x007F
41 #define FTS_WATCHDOG_TIME_PRESET 0x000B
42 #define FTS_WATCHDOG_CONTROL 0x5081
44 #define FTS_NO_FAN_SENSORS 0x08
45 #define FTS_NO_TEMP_SENSORS 0x10
46 #define FTS_NO_VOLT_SENSORS 0x04
48 #define FTS_FAN_SOURCE_INVALID 0xff
50 static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
52 static const struct i2c_device_id fts_id[] = {
56 MODULE_DEVICE_TABLE(i2c, fts_id);
58 enum WATCHDOG_RESOLUTION {
64 struct i2c_client *client;
65 /* update sensor data lock */
66 struct mutex update_lock;
67 /* read/write register lock */
68 struct mutex access_lock;
69 unsigned long last_updated; /* in jiffies */
70 struct watchdog_device wdd;
71 enum WATCHDOG_RESOLUTION resolution;
72 bool valid; /* false until following fields are valid */
74 u8 volt[FTS_NO_VOLT_SENSORS];
76 u8 temp_input[FTS_NO_TEMP_SENSORS];
80 u8 fan_input[FTS_NO_FAN_SENSORS]; /* in rps */
81 u8 fan_source[FTS_NO_FAN_SENSORS];
85 #define FTS_REG_FAN_INPUT(idx) ((idx) + 0x20)
86 #define FTS_REG_FAN_SOURCE(idx) ((idx) + 0x30)
87 #define FTS_REG_FAN_CONTROL(idx) (((idx) << 16) + 0x4881)
89 #define FTS_REG_TEMP_INPUT(idx) ((idx) + 0x40)
90 #define FTS_REG_TEMP_CONTROL(idx) (((idx) << 16) + 0x0681)
92 #define FTS_REG_VOLT(idx) ((idx) + 0x18)
94 /*****************************************************************************/
95 /* I2C Helper functions */
96 /*****************************************************************************/
97 static int fts_read_byte(struct i2c_client *client, unsigned short reg)
100 unsigned char page = reg >> 8;
101 struct fts_data *data = dev_get_drvdata(&client->dev);
103 mutex_lock(&data->access_lock);
105 dev_dbg(&client->dev, "page select - page: 0x%.02x\n", page);
106 ret = i2c_smbus_write_byte_data(client, FTS_PAGE_SELECT_REG, page);
111 ret = i2c_smbus_read_byte_data(client, reg);
112 dev_dbg(&client->dev, "read - reg: 0x%.02x: val: 0x%.02x\n", reg, ret);
115 mutex_unlock(&data->access_lock);
119 static int fts_write_byte(struct i2c_client *client, unsigned short reg,
123 unsigned char page = reg >> 8;
124 struct fts_data *data = dev_get_drvdata(&client->dev);
126 mutex_lock(&data->access_lock);
128 dev_dbg(&client->dev, "page select - page: 0x%.02x\n", page);
129 ret = i2c_smbus_write_byte_data(client, FTS_PAGE_SELECT_REG, page);
134 dev_dbg(&client->dev,
135 "write - reg: 0x%.02x: val: 0x%.02x\n", reg, value);
136 ret = i2c_smbus_write_byte_data(client, reg, value);
139 mutex_unlock(&data->access_lock);
143 /*****************************************************************************/
144 /* Data Updater Helper function */
145 /*****************************************************************************/
146 static int fts_update_device(struct fts_data *data)
151 mutex_lock(&data->update_lock);
152 if (!time_after(jiffies, data->last_updated + 2 * HZ) && data->valid)
155 err = fts_read_byte(data->client, FTS_DEVICE_STATUS_REG);
159 data->valid = !!(err & 0x02); /* Data not ready yet */
160 if (unlikely(!data->valid)) {
165 err = fts_read_byte(data->client, FTS_FAN_PRESENT_REG);
168 data->fan_present = err;
170 err = fts_read_byte(data->client, FTS_FAN_EVENT_REG);
173 data->fan_alarm = err;
175 for (i = 0; i < FTS_NO_FAN_SENSORS; i++) {
176 if (data->fan_present & BIT(i)) {
177 err = fts_read_byte(data->client, FTS_REG_FAN_INPUT(i));
180 data->fan_input[i] = err;
182 err = fts_read_byte(data->client,
183 FTS_REG_FAN_SOURCE(i));
186 data->fan_source[i] = err;
188 data->fan_input[i] = 0;
189 data->fan_source[i] = FTS_FAN_SOURCE_INVALID;
193 err = fts_read_byte(data->client, FTS_SENSOR_EVENT_REG);
196 data->temp_alarm = err;
198 for (i = 0; i < FTS_NO_TEMP_SENSORS; i++) {
199 err = fts_read_byte(data->client, FTS_REG_TEMP_INPUT(i));
202 data->temp_input[i] = err;
205 for (i = 0; i < FTS_NO_VOLT_SENSORS; i++) {
206 err = fts_read_byte(data->client, FTS_REG_VOLT(i));
211 data->last_updated = jiffies;
214 mutex_unlock(&data->update_lock);
218 /*****************************************************************************/
219 /* Watchdog functions */
220 /*****************************************************************************/
221 static int fts_wd_set_resolution(struct fts_data *data,
222 enum WATCHDOG_RESOLUTION resolution)
226 if (data->resolution == resolution)
229 ret = fts_read_byte(data->client, FTS_WATCHDOG_CONTROL);
233 if ((resolution == seconds && ret & BIT(1)) ||
234 (resolution == minutes && (ret & BIT(1)) == 0)) {
235 data->resolution = resolution;
239 if (resolution == seconds)
244 ret = fts_write_byte(data->client, FTS_WATCHDOG_CONTROL, ret);
248 data->resolution = resolution;
252 static int fts_wd_set_timeout(struct watchdog_device *wdd, unsigned int timeout)
254 struct fts_data *data;
255 enum WATCHDOG_RESOLUTION resolution = seconds;
258 data = watchdog_get_drvdata(wdd);
259 /* switch watchdog resolution to minutes if timeout does not fit
262 if (timeout > 0xFF) {
263 timeout = DIV_ROUND_UP(timeout, 60) * 60;
264 resolution = minutes;
267 ret = fts_wd_set_resolution(data, resolution);
271 wdd->timeout = timeout;
275 static int fts_wd_start(struct watchdog_device *wdd)
277 struct fts_data *data = watchdog_get_drvdata(wdd);
279 return fts_write_byte(data->client, FTS_WATCHDOG_TIME_PRESET,
280 wdd->timeout / (u8)data->resolution);
283 static int fts_wd_stop(struct watchdog_device *wdd)
285 struct fts_data *data;
287 data = watchdog_get_drvdata(wdd);
288 return fts_write_byte(data->client, FTS_WATCHDOG_TIME_PRESET, 0);
291 static const struct watchdog_info fts_wd_info = {
292 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
293 .identity = "FTS Teutates Hardware Watchdog",
296 static const struct watchdog_ops fts_wd_ops = {
297 .owner = THIS_MODULE,
298 .start = fts_wd_start,
300 .set_timeout = fts_wd_set_timeout,
303 static int fts_watchdog_init(struct fts_data *data)
307 watchdog_set_drvdata(&data->wdd, data);
309 timeout = fts_read_byte(data->client, FTS_WATCHDOG_TIME_PRESET);
313 /* watchdog not running, set timeout to a default of 60 sec. */
315 ret = fts_wd_set_resolution(data, seconds);
318 data->wdd.timeout = 60;
320 ret = fts_read_byte(data->client, FTS_WATCHDOG_CONTROL);
324 data->resolution = ret & BIT(1) ? seconds : minutes;
325 data->wdd.timeout = timeout * (u8)data->resolution;
326 set_bit(WDOG_HW_RUNNING, &data->wdd.status);
329 /* Register our watchdog part */
330 data->wdd.info = &fts_wd_info;
331 data->wdd.ops = &fts_wd_ops;
332 data->wdd.parent = &data->client->dev;
333 data->wdd.min_timeout = 1;
335 /* max timeout 255 minutes. */
336 data->wdd.max_hw_heartbeat_ms = 0xFF * 60 * MSEC_PER_SEC;
338 return devm_watchdog_register_device(&data->client->dev, &data->wdd);
341 static umode_t fts_is_visible(const void *devdata, enum hwmon_sensor_types type, u32 attr,
347 case hwmon_temp_input:
348 case hwmon_temp_fault:
350 case hwmon_temp_alarm:
358 case hwmon_fan_input:
359 case hwmon_fan_fault:
361 case hwmon_fan_alarm:
377 static int fts_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
380 struct fts_data *data = dev_get_drvdata(dev);
381 int ret = fts_update_device(data);
389 case hwmon_temp_input:
390 *val = (data->temp_input[channel] - 64) * 1000;
393 case hwmon_temp_alarm:
394 *val = !!(data->temp_alarm & BIT(channel));
397 case hwmon_temp_fault:
398 /* 00h Temperature = Sensor Error */;
399 *val = (data->temp_input[channel] == 0);
408 case hwmon_fan_input:
409 *val = data->fan_input[channel] * 60;
412 case hwmon_fan_alarm:
413 *val = !!(data->fan_alarm & BIT(channel));
416 case hwmon_fan_fault:
417 *val = !(data->fan_present & BIT(channel));
426 case hwmon_pwm_auto_channels_temp:
427 if (data->fan_source[channel] == FTS_FAN_SOURCE_INVALID)
430 *val = BIT(data->fan_source[channel]);
440 *val = DIV_ROUND_CLOSEST(data->volt[channel] * 3300, 255);
454 static int fts_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
457 struct fts_data *data = dev_get_drvdata(dev);
458 int ret = fts_update_device(data);
466 case hwmon_temp_alarm:
470 mutex_lock(&data->update_lock);
471 ret = fts_read_byte(data->client, FTS_REG_TEMP_CONTROL(channel));
473 ret = fts_write_byte(data->client, FTS_REG_TEMP_CONTROL(channel),
478 mutex_unlock(&data->update_lock);
489 case hwmon_fan_alarm:
493 mutex_lock(&data->update_lock);
494 ret = fts_read_byte(data->client, FTS_REG_FAN_CONTROL(channel));
496 ret = fts_write_byte(data->client, FTS_REG_FAN_CONTROL(channel),
501 mutex_unlock(&data->update_lock);
517 static const struct hwmon_ops fts_ops = {
518 .is_visible = fts_is_visible,
523 static const struct hwmon_channel_info *fts_info[] = {
524 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
525 HWMON_CHANNEL_INFO(temp,
526 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
527 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
528 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
529 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
530 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
531 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
532 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
533 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
534 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
535 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
536 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
537 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
538 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
539 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
540 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT,
541 HWMON_T_INPUT | HWMON_T_ALARM | HWMON_T_FAULT
543 HWMON_CHANNEL_INFO(fan,
544 HWMON_F_INPUT | HWMON_F_ALARM | HWMON_F_FAULT,
545 HWMON_F_INPUT | HWMON_F_ALARM | HWMON_F_FAULT,
546 HWMON_F_INPUT | HWMON_F_ALARM | HWMON_F_FAULT,
547 HWMON_F_INPUT | HWMON_F_ALARM | HWMON_F_FAULT,
548 HWMON_F_INPUT | HWMON_F_ALARM | HWMON_F_FAULT,
549 HWMON_F_INPUT | HWMON_F_ALARM | HWMON_F_FAULT,
550 HWMON_F_INPUT | HWMON_F_ALARM | HWMON_F_FAULT,
551 HWMON_F_INPUT | HWMON_F_ALARM | HWMON_F_FAULT
553 HWMON_CHANNEL_INFO(pwm,
554 HWMON_PWM_AUTO_CHANNELS_TEMP,
555 HWMON_PWM_AUTO_CHANNELS_TEMP,
556 HWMON_PWM_AUTO_CHANNELS_TEMP,
557 HWMON_PWM_AUTO_CHANNELS_TEMP,
558 HWMON_PWM_AUTO_CHANNELS_TEMP,
559 HWMON_PWM_AUTO_CHANNELS_TEMP,
560 HWMON_PWM_AUTO_CHANNELS_TEMP,
561 HWMON_PWM_AUTO_CHANNELS_TEMP
563 HWMON_CHANNEL_INFO(in,
572 static const struct hwmon_chip_info fts_chip_info = {
577 /*****************************************************************************/
578 /* Module initialization / remove functions */
579 /*****************************************************************************/
580 static int fts_detect(struct i2c_client *client,
581 struct i2c_board_info *info)
585 /* detection works with revision greater or equal to 0x2b */
586 val = i2c_smbus_read_byte_data(client, FTS_DEVICE_REVISION_REG);
590 /* Device Detect Regs must have 0x17 0x34 and 0x54 */
591 val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_1);
595 val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_2);
599 val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_3);
604 * 0x10 == Baseboard Management Controller, 0x01 == Teutates
605 * Device ID Reg needs to be 0x11
607 val = i2c_smbus_read_byte_data(client, FTS_DEVICE_ID_REG);
611 strscpy(info->type, fts_id[0].name, I2C_NAME_SIZE);
616 static int fts_probe(struct i2c_client *client)
619 struct fts_data *data;
622 struct device *hwmon_dev;
624 if (client->addr != 0x73)
627 /* Baseboard Management Controller check */
628 deviceid = i2c_smbus_read_byte_data(client, FTS_DEVICE_ID_REG);
629 if (deviceid > 0 && (deviceid & 0xF0) == 0x10) {
630 switch (deviceid & 0x0F) {
634 dev_dbg(&client->dev,
635 "No Baseboard Management Controller\n");
639 dev_dbg(&client->dev, "No fujitsu board\n");
643 data = devm_kzalloc(&client->dev, sizeof(struct fts_data),
648 mutex_init(&data->update_lock);
649 mutex_init(&data->access_lock);
650 data->client = client;
651 dev_set_drvdata(&client->dev, data);
653 err = i2c_smbus_read_byte_data(client, FTS_DEVICE_REVISION_REG);
658 hwmon_dev = devm_hwmon_device_register_with_info(&client->dev, "ftsteutates", data,
659 &fts_chip_info, NULL);
660 if (IS_ERR(hwmon_dev))
661 return PTR_ERR(hwmon_dev);
663 err = fts_watchdog_init(data);
667 dev_info(&client->dev, "Detected FTS Teutates chip, revision: %d.%d\n",
668 (revision & 0xF0) >> 4, revision & 0x0F);
672 /*****************************************************************************/
674 /*****************************************************************************/
675 static struct i2c_driver fts_driver = {
676 .class = I2C_CLASS_HWMON,
678 .name = "ftsteutates",
681 .probe_new = fts_probe,
682 .detect = fts_detect,
683 .address_list = normal_i2c,
686 module_i2c_driver(fts_driver);
688 MODULE_AUTHOR("Thilo Cestonaro <thilo.cestonaro@ts.fujitsu.com>");
689 MODULE_DESCRIPTION("FTS Teutates driver");
690 MODULE_LICENSE("GPL");