xref: /linux/drivers/hwmon/ftsteutates.c (revision 23bc3caf42bd0f140182ba60cc68d7872e81aeea)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
208426edaSThilo Cestonaro /*
308426edaSThilo Cestonaro  * Support for the FTS Systemmonitoring Chip "Teutates"
408426edaSThilo Cestonaro  *
508426edaSThilo Cestonaro  * Copyright (C) 2016 Fujitsu Technology Solutions GmbH,
608426edaSThilo Cestonaro  *		  Thilo Cestonaro <thilo.cestonaro@ts.fujitsu.com>
708426edaSThilo Cestonaro  */
808426edaSThilo Cestonaro #include <linux/err.h>
908426edaSThilo Cestonaro #include <linux/fs.h>
1008426edaSThilo Cestonaro #include <linux/hwmon.h>
1108426edaSThilo Cestonaro #include <linux/hwmon-sysfs.h>
1208426edaSThilo Cestonaro #include <linux/i2c.h>
1308426edaSThilo Cestonaro #include <linux/init.h>
1408426edaSThilo Cestonaro #include <linux/jiffies.h>
1508426edaSThilo Cestonaro #include <linux/module.h>
1608426edaSThilo Cestonaro #include <linux/mutex.h>
1708426edaSThilo Cestonaro #include <linux/slab.h>
1808426edaSThilo Cestonaro #include <linux/sysfs.h>
1908426edaSThilo Cestonaro #include <linux/uaccess.h>
2008426edaSThilo Cestonaro #include <linux/watchdog.h>
2108426edaSThilo Cestonaro 
2208426edaSThilo Cestonaro #define FTS_DEVICE_ID_REG		0x0000
2308426edaSThilo Cestonaro #define FTS_DEVICE_REVISION_REG		0x0001
2408426edaSThilo Cestonaro #define FTS_DEVICE_STATUS_REG		0x0004
2508426edaSThilo Cestonaro #define FTS_SATELLITE_STATUS_REG	0x0005
2608426edaSThilo Cestonaro #define FTS_EVENT_STATUS_REG		0x0006
2708426edaSThilo Cestonaro #define FTS_GLOBAL_CONTROL_REG		0x0007
2808426edaSThilo Cestonaro 
292d5aee43SThilo Cestonaro #define FTS_DEVICE_DETECT_REG_1		0x0C
302d5aee43SThilo Cestonaro #define FTS_DEVICE_DETECT_REG_2		0x0D
312d5aee43SThilo Cestonaro #define FTS_DEVICE_DETECT_REG_3		0x0E
322d5aee43SThilo Cestonaro 
3308426edaSThilo Cestonaro #define FTS_SENSOR_EVENT_REG		0x0010
3408426edaSThilo Cestonaro 
3508426edaSThilo Cestonaro #define FTS_FAN_EVENT_REG		0x0014
3608426edaSThilo Cestonaro #define FTS_FAN_PRESENT_REG		0x0015
3708426edaSThilo Cestonaro 
3808426edaSThilo Cestonaro #define FTS_POWER_ON_TIME_COUNTER_A	0x007A
3908426edaSThilo Cestonaro #define FTS_POWER_ON_TIME_COUNTER_B	0x007B
4008426edaSThilo Cestonaro #define FTS_POWER_ON_TIME_COUNTER_C	0x007C
4108426edaSThilo Cestonaro 
4208426edaSThilo Cestonaro #define FTS_PAGE_SELECT_REG		0x007F
4308426edaSThilo Cestonaro 
4408426edaSThilo Cestonaro #define FTS_WATCHDOG_TIME_PRESET	0x000B
4508426edaSThilo Cestonaro #define FTS_WATCHDOG_CONTROL		0x5081
4608426edaSThilo Cestonaro 
4708426edaSThilo Cestonaro #define FTS_NO_FAN_SENSORS		0x08
4808426edaSThilo Cestonaro #define FTS_NO_TEMP_SENSORS		0x10
4908426edaSThilo Cestonaro #define FTS_NO_VOLT_SENSORS		0x04
5008426edaSThilo Cestonaro 
512d5aee43SThilo Cestonaro static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
522d5aee43SThilo Cestonaro 
531dad2e95SArvind Yadav static const struct i2c_device_id fts_id[] = {
5408426edaSThilo Cestonaro 	{ "ftsteutates", 0 },
5508426edaSThilo Cestonaro 	{ }
5608426edaSThilo Cestonaro };
5708426edaSThilo Cestonaro MODULE_DEVICE_TABLE(i2c, fts_id);
5808426edaSThilo Cestonaro 
5908426edaSThilo Cestonaro enum WATCHDOG_RESOLUTION {
6008426edaSThilo Cestonaro 	seconds = 1,
6108426edaSThilo Cestonaro 	minutes = 60
6208426edaSThilo Cestonaro };
6308426edaSThilo Cestonaro 
6408426edaSThilo Cestonaro struct fts_data {
6508426edaSThilo Cestonaro 	struct i2c_client *client;
6608426edaSThilo Cestonaro 	/* update sensor data lock */
6708426edaSThilo Cestonaro 	struct mutex update_lock;
6808426edaSThilo Cestonaro 	/* read/write register lock */
6908426edaSThilo Cestonaro 	struct mutex access_lock;
7008426edaSThilo Cestonaro 	unsigned long last_updated; /* in jiffies */
7108426edaSThilo Cestonaro 	struct watchdog_device wdd;
7208426edaSThilo Cestonaro 	enum WATCHDOG_RESOLUTION resolution;
7308426edaSThilo Cestonaro 	bool valid; /* false until following fields are valid */
7408426edaSThilo Cestonaro 
7508426edaSThilo Cestonaro 	u8 volt[FTS_NO_VOLT_SENSORS];
7608426edaSThilo Cestonaro 
7708426edaSThilo Cestonaro 	u8 temp_input[FTS_NO_TEMP_SENSORS];
7808426edaSThilo Cestonaro 	u8 temp_alarm;
7908426edaSThilo Cestonaro 
8008426edaSThilo Cestonaro 	u8 fan_present;
8108426edaSThilo Cestonaro 	u8 fan_input[FTS_NO_FAN_SENSORS]; /* in rps */
8208426edaSThilo Cestonaro 	u8 fan_source[FTS_NO_FAN_SENSORS];
8308426edaSThilo Cestonaro 	u8 fan_alarm;
8408426edaSThilo Cestonaro };
8508426edaSThilo Cestonaro 
8608426edaSThilo Cestonaro #define FTS_REG_FAN_INPUT(idx) ((idx) + 0x20)
8708426edaSThilo Cestonaro #define FTS_REG_FAN_SOURCE(idx) ((idx) + 0x30)
8808426edaSThilo Cestonaro #define FTS_REG_FAN_CONTROL(idx) (((idx) << 16) + 0x4881)
8908426edaSThilo Cestonaro 
9008426edaSThilo Cestonaro #define FTS_REG_TEMP_INPUT(idx) ((idx) + 0x40)
9108426edaSThilo Cestonaro #define FTS_REG_TEMP_CONTROL(idx) (((idx) << 16) + 0x0681)
9208426edaSThilo Cestonaro 
9308426edaSThilo Cestonaro #define FTS_REG_VOLT(idx) ((idx) + 0x18)
9408426edaSThilo Cestonaro 
9508426edaSThilo Cestonaro /*****************************************************************************/
9608426edaSThilo Cestonaro /* I2C Helper functions							     */
9708426edaSThilo Cestonaro /*****************************************************************************/
9808426edaSThilo Cestonaro static int fts_read_byte(struct i2c_client *client, unsigned short reg)
9908426edaSThilo Cestonaro {
10008426edaSThilo Cestonaro 	int ret;
10108426edaSThilo Cestonaro 	unsigned char page = reg >> 8;
10208426edaSThilo Cestonaro 	struct fts_data *data = dev_get_drvdata(&client->dev);
10308426edaSThilo Cestonaro 
10408426edaSThilo Cestonaro 	mutex_lock(&data->access_lock);
10508426edaSThilo Cestonaro 
10608426edaSThilo Cestonaro 	dev_dbg(&client->dev, "page select - page: 0x%.02x\n", page);
10708426edaSThilo Cestonaro 	ret = i2c_smbus_write_byte_data(client, FTS_PAGE_SELECT_REG, page);
10808426edaSThilo Cestonaro 	if (ret < 0)
10908426edaSThilo Cestonaro 		goto error;
11008426edaSThilo Cestonaro 
11108426edaSThilo Cestonaro 	reg &= 0xFF;
11208426edaSThilo Cestonaro 	ret = i2c_smbus_read_byte_data(client, reg);
11308426edaSThilo Cestonaro 	dev_dbg(&client->dev, "read - reg: 0x%.02x: val: 0x%.02x\n", reg, ret);
11408426edaSThilo Cestonaro 
11508426edaSThilo Cestonaro error:
11608426edaSThilo Cestonaro 	mutex_unlock(&data->access_lock);
11708426edaSThilo Cestonaro 	return ret;
11808426edaSThilo Cestonaro }
11908426edaSThilo Cestonaro 
12008426edaSThilo Cestonaro static int fts_write_byte(struct i2c_client *client, unsigned short reg,
12108426edaSThilo Cestonaro 			  unsigned char value)
12208426edaSThilo Cestonaro {
12308426edaSThilo Cestonaro 	int ret;
12408426edaSThilo Cestonaro 	unsigned char page = reg >> 8;
12508426edaSThilo Cestonaro 	struct fts_data *data = dev_get_drvdata(&client->dev);
12608426edaSThilo Cestonaro 
12708426edaSThilo Cestonaro 	mutex_lock(&data->access_lock);
12808426edaSThilo Cestonaro 
12908426edaSThilo Cestonaro 	dev_dbg(&client->dev, "page select - page: 0x%.02x\n", page);
13008426edaSThilo Cestonaro 	ret = i2c_smbus_write_byte_data(client, FTS_PAGE_SELECT_REG, page);
13108426edaSThilo Cestonaro 	if (ret < 0)
13208426edaSThilo Cestonaro 		goto error;
13308426edaSThilo Cestonaro 
13408426edaSThilo Cestonaro 	reg &= 0xFF;
13508426edaSThilo Cestonaro 	dev_dbg(&client->dev,
13608426edaSThilo Cestonaro 		"write - reg: 0x%.02x: val: 0x%.02x\n", reg, value);
13708426edaSThilo Cestonaro 	ret = i2c_smbus_write_byte_data(client, reg, value);
13808426edaSThilo Cestonaro 
13908426edaSThilo Cestonaro error:
14008426edaSThilo Cestonaro 	mutex_unlock(&data->access_lock);
14108426edaSThilo Cestonaro 	return ret;
14208426edaSThilo Cestonaro }
14308426edaSThilo Cestonaro 
14408426edaSThilo Cestonaro /*****************************************************************************/
14508426edaSThilo Cestonaro /* Data Updater Helper function						     */
14608426edaSThilo Cestonaro /*****************************************************************************/
14708426edaSThilo Cestonaro static int fts_update_device(struct fts_data *data)
14808426edaSThilo Cestonaro {
14908426edaSThilo Cestonaro 	int i;
15008426edaSThilo Cestonaro 	int err = 0;
15108426edaSThilo Cestonaro 
15208426edaSThilo Cestonaro 	mutex_lock(&data->update_lock);
15308426edaSThilo Cestonaro 	if (!time_after(jiffies, data->last_updated + 2 * HZ) && data->valid)
15408426edaSThilo Cestonaro 		goto exit;
15508426edaSThilo Cestonaro 
15608426edaSThilo Cestonaro 	err = fts_read_byte(data->client, FTS_DEVICE_STATUS_REG);
15708426edaSThilo Cestonaro 	if (err < 0)
15808426edaSThilo Cestonaro 		goto exit;
15908426edaSThilo Cestonaro 
16008426edaSThilo Cestonaro 	data->valid = !!(err & 0x02); /* Data not ready yet */
16108426edaSThilo Cestonaro 	if (unlikely(!data->valid)) {
16208426edaSThilo Cestonaro 		err = -EAGAIN;
16308426edaSThilo Cestonaro 		goto exit;
16408426edaSThilo Cestonaro 	}
16508426edaSThilo Cestonaro 
16608426edaSThilo Cestonaro 	err = fts_read_byte(data->client, FTS_FAN_PRESENT_REG);
16708426edaSThilo Cestonaro 	if (err < 0)
16808426edaSThilo Cestonaro 		goto exit;
16908426edaSThilo Cestonaro 	data->fan_present = err;
17008426edaSThilo Cestonaro 
17108426edaSThilo Cestonaro 	err = fts_read_byte(data->client, FTS_FAN_EVENT_REG);
17208426edaSThilo Cestonaro 	if (err < 0)
17308426edaSThilo Cestonaro 		goto exit;
17408426edaSThilo Cestonaro 	data->fan_alarm = err;
17508426edaSThilo Cestonaro 
17608426edaSThilo Cestonaro 	for (i = 0; i < FTS_NO_FAN_SENSORS; i++) {
17708426edaSThilo Cestonaro 		if (data->fan_present & BIT(i)) {
17808426edaSThilo Cestonaro 			err = fts_read_byte(data->client, FTS_REG_FAN_INPUT(i));
17908426edaSThilo Cestonaro 			if (err < 0)
18008426edaSThilo Cestonaro 				goto exit;
18108426edaSThilo Cestonaro 			data->fan_input[i] = err;
18208426edaSThilo Cestonaro 
18308426edaSThilo Cestonaro 			err = fts_read_byte(data->client,
18408426edaSThilo Cestonaro 					    FTS_REG_FAN_SOURCE(i));
18508426edaSThilo Cestonaro 			if (err < 0)
18608426edaSThilo Cestonaro 				goto exit;
18708426edaSThilo Cestonaro 			data->fan_source[i] = err;
18808426edaSThilo Cestonaro 		} else {
18908426edaSThilo Cestonaro 			data->fan_input[i] = 0;
19008426edaSThilo Cestonaro 			data->fan_source[i] = 0;
19108426edaSThilo Cestonaro 		}
19208426edaSThilo Cestonaro 	}
19308426edaSThilo Cestonaro 
19408426edaSThilo Cestonaro 	err = fts_read_byte(data->client, FTS_SENSOR_EVENT_REG);
19508426edaSThilo Cestonaro 	if (err < 0)
19608426edaSThilo Cestonaro 		goto exit;
19708426edaSThilo Cestonaro 	data->temp_alarm = err;
19808426edaSThilo Cestonaro 
19908426edaSThilo Cestonaro 	for (i = 0; i < FTS_NO_TEMP_SENSORS; i++) {
20008426edaSThilo Cestonaro 		err = fts_read_byte(data->client, FTS_REG_TEMP_INPUT(i));
20108426edaSThilo Cestonaro 		if (err < 0)
20208426edaSThilo Cestonaro 			goto exit;
20308426edaSThilo Cestonaro 		data->temp_input[i] = err;
20408426edaSThilo Cestonaro 	}
20508426edaSThilo Cestonaro 
20608426edaSThilo Cestonaro 	for (i = 0; i < FTS_NO_VOLT_SENSORS; i++) {
20708426edaSThilo Cestonaro 		err = fts_read_byte(data->client, FTS_REG_VOLT(i));
20808426edaSThilo Cestonaro 		if (err < 0)
20908426edaSThilo Cestonaro 			goto exit;
21008426edaSThilo Cestonaro 		data->volt[i] = err;
21108426edaSThilo Cestonaro 	}
21208426edaSThilo Cestonaro 	data->last_updated = jiffies;
21308426edaSThilo Cestonaro 	err = 0;
21408426edaSThilo Cestonaro exit:
21508426edaSThilo Cestonaro 	mutex_unlock(&data->update_lock);
21608426edaSThilo Cestonaro 	return err;
21708426edaSThilo Cestonaro }
21808426edaSThilo Cestonaro 
21908426edaSThilo Cestonaro /*****************************************************************************/
22008426edaSThilo Cestonaro /* Watchdog functions							     */
22108426edaSThilo Cestonaro /*****************************************************************************/
22208426edaSThilo Cestonaro static int fts_wd_set_resolution(struct fts_data *data,
22308426edaSThilo Cestonaro 				 enum WATCHDOG_RESOLUTION resolution)
22408426edaSThilo Cestonaro {
22508426edaSThilo Cestonaro 	int ret;
22608426edaSThilo Cestonaro 
22708426edaSThilo Cestonaro 	if (data->resolution == resolution)
22808426edaSThilo Cestonaro 		return 0;
22908426edaSThilo Cestonaro 
23008426edaSThilo Cestonaro 	ret = fts_read_byte(data->client, FTS_WATCHDOG_CONTROL);
23108426edaSThilo Cestonaro 	if (ret < 0)
23208426edaSThilo Cestonaro 		return ret;
23308426edaSThilo Cestonaro 
23408426edaSThilo Cestonaro 	if ((resolution == seconds && ret & BIT(1)) ||
23508426edaSThilo Cestonaro 	    (resolution == minutes && (ret & BIT(1)) == 0)) {
23608426edaSThilo Cestonaro 		data->resolution = resolution;
23708426edaSThilo Cestonaro 		return 0;
23808426edaSThilo Cestonaro 	}
23908426edaSThilo Cestonaro 
24008426edaSThilo Cestonaro 	if (resolution == seconds)
2414c8702b3SGuenter Roeck 		ret |= BIT(1);
24208426edaSThilo Cestonaro 	else
24308426edaSThilo Cestonaro 		ret &= ~BIT(1);
24408426edaSThilo Cestonaro 
24508426edaSThilo Cestonaro 	ret = fts_write_byte(data->client, FTS_WATCHDOG_CONTROL, ret);
24608426edaSThilo Cestonaro 	if (ret < 0)
24708426edaSThilo Cestonaro 		return ret;
24808426edaSThilo Cestonaro 
24908426edaSThilo Cestonaro 	data->resolution = resolution;
25008426edaSThilo Cestonaro 	return ret;
25108426edaSThilo Cestonaro }
25208426edaSThilo Cestonaro 
25308426edaSThilo Cestonaro static int fts_wd_set_timeout(struct watchdog_device *wdd, unsigned int timeout)
25408426edaSThilo Cestonaro {
25508426edaSThilo Cestonaro 	struct fts_data *data;
25608426edaSThilo Cestonaro 	enum WATCHDOG_RESOLUTION resolution = seconds;
25708426edaSThilo Cestonaro 	int ret;
25808426edaSThilo Cestonaro 
25908426edaSThilo Cestonaro 	data = watchdog_get_drvdata(wdd);
26008426edaSThilo Cestonaro 	/* switch watchdog resolution to minutes if timeout does not fit
26108426edaSThilo Cestonaro 	 * into a byte
26208426edaSThilo Cestonaro 	 */
26308426edaSThilo Cestonaro 	if (timeout > 0xFF) {
26408426edaSThilo Cestonaro 		timeout = DIV_ROUND_UP(timeout, 60) * 60;
26508426edaSThilo Cestonaro 		resolution = minutes;
26608426edaSThilo Cestonaro 	}
26708426edaSThilo Cestonaro 
26808426edaSThilo Cestonaro 	ret = fts_wd_set_resolution(data, resolution);
26908426edaSThilo Cestonaro 	if (ret < 0)
27008426edaSThilo Cestonaro 		return ret;
27108426edaSThilo Cestonaro 
27208426edaSThilo Cestonaro 	wdd->timeout = timeout;
27308426edaSThilo Cestonaro 	return 0;
27408426edaSThilo Cestonaro }
27508426edaSThilo Cestonaro 
27608426edaSThilo Cestonaro static int fts_wd_start(struct watchdog_device *wdd)
27708426edaSThilo Cestonaro {
27808426edaSThilo Cestonaro 	struct fts_data *data = watchdog_get_drvdata(wdd);
27908426edaSThilo Cestonaro 
28008426edaSThilo Cestonaro 	return fts_write_byte(data->client, FTS_WATCHDOG_TIME_PRESET,
28108426edaSThilo Cestonaro 			      wdd->timeout / (u8)data->resolution);
28208426edaSThilo Cestonaro }
28308426edaSThilo Cestonaro 
28408426edaSThilo Cestonaro static int fts_wd_stop(struct watchdog_device *wdd)
28508426edaSThilo Cestonaro {
28608426edaSThilo Cestonaro 	struct fts_data *data;
28708426edaSThilo Cestonaro 
28808426edaSThilo Cestonaro 	data = watchdog_get_drvdata(wdd);
28908426edaSThilo Cestonaro 	return fts_write_byte(data->client, FTS_WATCHDOG_TIME_PRESET, 0);
29008426edaSThilo Cestonaro }
29108426edaSThilo Cestonaro 
29208426edaSThilo Cestonaro static const struct watchdog_info fts_wd_info = {
29308426edaSThilo Cestonaro 	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
29408426edaSThilo Cestonaro 	.identity = "FTS Teutates Hardware Watchdog",
29508426edaSThilo Cestonaro };
29608426edaSThilo Cestonaro 
29708426edaSThilo Cestonaro static const struct watchdog_ops fts_wd_ops = {
29808426edaSThilo Cestonaro 	.owner = THIS_MODULE,
29908426edaSThilo Cestonaro 	.start = fts_wd_start,
30008426edaSThilo Cestonaro 	.stop = fts_wd_stop,
30108426edaSThilo Cestonaro 	.set_timeout = fts_wd_set_timeout,
30208426edaSThilo Cestonaro };
30308426edaSThilo Cestonaro 
30408426edaSThilo Cestonaro static int fts_watchdog_init(struct fts_data *data)
30508426edaSThilo Cestonaro {
30608426edaSThilo Cestonaro 	int timeout, ret;
30708426edaSThilo Cestonaro 
30808426edaSThilo Cestonaro 	watchdog_set_drvdata(&data->wdd, data);
30908426edaSThilo Cestonaro 
31008426edaSThilo Cestonaro 	timeout = fts_read_byte(data->client, FTS_WATCHDOG_TIME_PRESET);
31108426edaSThilo Cestonaro 	if (timeout < 0)
31208426edaSThilo Cestonaro 		return timeout;
31308426edaSThilo Cestonaro 
31408426edaSThilo Cestonaro 	/* watchdog not running, set timeout to a default of 60 sec. */
31508426edaSThilo Cestonaro 	if (timeout == 0) {
31608426edaSThilo Cestonaro 		ret = fts_wd_set_resolution(data, seconds);
31708426edaSThilo Cestonaro 		if (ret < 0)
31808426edaSThilo Cestonaro 			return ret;
31908426edaSThilo Cestonaro 		data->wdd.timeout = 60;
32008426edaSThilo Cestonaro 	} else {
32108426edaSThilo Cestonaro 		ret = fts_read_byte(data->client, FTS_WATCHDOG_CONTROL);
32208426edaSThilo Cestonaro 		if (ret < 0)
32308426edaSThilo Cestonaro 			return ret;
32408426edaSThilo Cestonaro 
32508426edaSThilo Cestonaro 		data->resolution = ret & BIT(1) ? seconds : minutes;
32608426edaSThilo Cestonaro 		data->wdd.timeout = timeout * (u8)data->resolution;
32708426edaSThilo Cestonaro 		set_bit(WDOG_HW_RUNNING, &data->wdd.status);
32808426edaSThilo Cestonaro 	}
32908426edaSThilo Cestonaro 
33008426edaSThilo Cestonaro 	/* Register our watchdog part */
33108426edaSThilo Cestonaro 	data->wdd.info = &fts_wd_info;
33208426edaSThilo Cestonaro 	data->wdd.ops = &fts_wd_ops;
33308426edaSThilo Cestonaro 	data->wdd.parent = &data->client->dev;
33408426edaSThilo Cestonaro 	data->wdd.min_timeout = 1;
33508426edaSThilo Cestonaro 
33608426edaSThilo Cestonaro 	/* max timeout 255 minutes. */
33708426edaSThilo Cestonaro 	data->wdd.max_hw_heartbeat_ms = 0xFF * 60 * MSEC_PER_SEC;
33808426edaSThilo Cestonaro 
33908426edaSThilo Cestonaro 	return watchdog_register_device(&data->wdd);
34008426edaSThilo Cestonaro }
34108426edaSThilo Cestonaro 
34208426edaSThilo Cestonaro /*****************************************************************************/
34308426edaSThilo Cestonaro /* SysFS handler functions						     */
34408426edaSThilo Cestonaro /*****************************************************************************/
345626f5eaeSGuenter Roeck static ssize_t in_value_show(struct device *dev,
34608426edaSThilo Cestonaro 			     struct device_attribute *devattr, char *buf)
34708426edaSThilo Cestonaro {
34808426edaSThilo Cestonaro 	struct fts_data *data = dev_get_drvdata(dev);
34908426edaSThilo Cestonaro 	int index = to_sensor_dev_attr(devattr)->index;
35008426edaSThilo Cestonaro 	int err;
35108426edaSThilo Cestonaro 
35208426edaSThilo Cestonaro 	err = fts_update_device(data);
35308426edaSThilo Cestonaro 	if (err < 0)
35408426edaSThilo Cestonaro 		return err;
35508426edaSThilo Cestonaro 
35608426edaSThilo Cestonaro 	return sprintf(buf, "%u\n", data->volt[index]);
35708426edaSThilo Cestonaro }
35808426edaSThilo Cestonaro 
359626f5eaeSGuenter Roeck static ssize_t temp_value_show(struct device *dev,
36008426edaSThilo Cestonaro 			       struct device_attribute *devattr, char *buf)
36108426edaSThilo Cestonaro {
36208426edaSThilo Cestonaro 	struct fts_data *data = dev_get_drvdata(dev);
36308426edaSThilo Cestonaro 	int index = to_sensor_dev_attr(devattr)->index;
36408426edaSThilo Cestonaro 	int err;
36508426edaSThilo Cestonaro 
36608426edaSThilo Cestonaro 	err = fts_update_device(data);
36708426edaSThilo Cestonaro 	if (err < 0)
36808426edaSThilo Cestonaro 		return err;
36908426edaSThilo Cestonaro 
37008426edaSThilo Cestonaro 	return sprintf(buf, "%u\n", data->temp_input[index]);
37108426edaSThilo Cestonaro }
37208426edaSThilo Cestonaro 
373626f5eaeSGuenter Roeck static ssize_t temp_fault_show(struct device *dev,
37408426edaSThilo Cestonaro 			       struct device_attribute *devattr, char *buf)
37508426edaSThilo Cestonaro {
37608426edaSThilo Cestonaro 	struct fts_data *data = dev_get_drvdata(dev);
37708426edaSThilo Cestonaro 	int index = to_sensor_dev_attr(devattr)->index;
37808426edaSThilo Cestonaro 	int err;
37908426edaSThilo Cestonaro 
38008426edaSThilo Cestonaro 	err = fts_update_device(data);
38108426edaSThilo Cestonaro 	if (err < 0)
38208426edaSThilo Cestonaro 		return err;
38308426edaSThilo Cestonaro 
38408426edaSThilo Cestonaro 	/* 00h Temperature = Sensor Error */
38508426edaSThilo Cestonaro 	return sprintf(buf, "%d\n", data->temp_input[index] == 0);
38608426edaSThilo Cestonaro }
38708426edaSThilo Cestonaro 
388626f5eaeSGuenter Roeck static ssize_t temp_alarm_show(struct device *dev,
38908426edaSThilo Cestonaro 			       struct device_attribute *devattr, char *buf)
39008426edaSThilo Cestonaro {
39108426edaSThilo Cestonaro 	struct fts_data *data = dev_get_drvdata(dev);
39208426edaSThilo Cestonaro 	int index = to_sensor_dev_attr(devattr)->index;
39308426edaSThilo Cestonaro 	int err;
39408426edaSThilo Cestonaro 
39508426edaSThilo Cestonaro 	err = fts_update_device(data);
39608426edaSThilo Cestonaro 	if (err < 0)
39708426edaSThilo Cestonaro 		return err;
39808426edaSThilo Cestonaro 
39908426edaSThilo Cestonaro 	return sprintf(buf, "%u\n", !!(data->temp_alarm & BIT(index)));
40008426edaSThilo Cestonaro }
40108426edaSThilo Cestonaro 
40208426edaSThilo Cestonaro static ssize_t
403626f5eaeSGuenter Roeck temp_alarm_store(struct device *dev, struct device_attribute *devattr,
40408426edaSThilo Cestonaro 		 const char *buf, size_t count)
40508426edaSThilo Cestonaro {
40608426edaSThilo Cestonaro 	struct fts_data *data = dev_get_drvdata(dev);
40708426edaSThilo Cestonaro 	int index = to_sensor_dev_attr(devattr)->index;
40808426edaSThilo Cestonaro 	long ret;
40908426edaSThilo Cestonaro 
41008426edaSThilo Cestonaro 	ret = fts_update_device(data);
41108426edaSThilo Cestonaro 	if (ret < 0)
41208426edaSThilo Cestonaro 		return ret;
41308426edaSThilo Cestonaro 
41408426edaSThilo Cestonaro 	if (kstrtoul(buf, 10, &ret) || ret != 0)
41508426edaSThilo Cestonaro 		return -EINVAL;
41608426edaSThilo Cestonaro 
41708426edaSThilo Cestonaro 	mutex_lock(&data->update_lock);
41808426edaSThilo Cestonaro 	ret = fts_read_byte(data->client, FTS_REG_TEMP_CONTROL(index));
41908426edaSThilo Cestonaro 	if (ret < 0)
42008426edaSThilo Cestonaro 		goto error;
42108426edaSThilo Cestonaro 
42208426edaSThilo Cestonaro 	ret = fts_write_byte(data->client, FTS_REG_TEMP_CONTROL(index),
42308426edaSThilo Cestonaro 			     ret | 0x1);
42408426edaSThilo Cestonaro 	if (ret < 0)
42508426edaSThilo Cestonaro 		goto error;
42608426edaSThilo Cestonaro 
42708426edaSThilo Cestonaro 	data->valid = false;
4287576750fSThilo Cestonaro 	ret = count;
42908426edaSThilo Cestonaro error:
43008426edaSThilo Cestonaro 	mutex_unlock(&data->update_lock);
43108426edaSThilo Cestonaro 	return ret;
43208426edaSThilo Cestonaro }
43308426edaSThilo Cestonaro 
434626f5eaeSGuenter Roeck static ssize_t fan_value_show(struct device *dev,
43508426edaSThilo Cestonaro 			      struct device_attribute *devattr, char *buf)
43608426edaSThilo Cestonaro {
43708426edaSThilo Cestonaro 	struct fts_data *data = dev_get_drvdata(dev);
43808426edaSThilo Cestonaro 	int index = to_sensor_dev_attr(devattr)->index;
43908426edaSThilo Cestonaro 	int err;
44008426edaSThilo Cestonaro 
44108426edaSThilo Cestonaro 	err = fts_update_device(data);
44208426edaSThilo Cestonaro 	if (err < 0)
44308426edaSThilo Cestonaro 		return err;
44408426edaSThilo Cestonaro 
44508426edaSThilo Cestonaro 	return sprintf(buf, "%u\n", data->fan_input[index]);
44608426edaSThilo Cestonaro }
44708426edaSThilo Cestonaro 
448626f5eaeSGuenter Roeck static ssize_t fan_source_show(struct device *dev,
44908426edaSThilo Cestonaro 			       struct device_attribute *devattr, char *buf)
45008426edaSThilo Cestonaro {
45108426edaSThilo Cestonaro 	struct fts_data *data = dev_get_drvdata(dev);
45208426edaSThilo Cestonaro 	int index = to_sensor_dev_attr(devattr)->index;
45308426edaSThilo Cestonaro 	int err;
45408426edaSThilo Cestonaro 
45508426edaSThilo Cestonaro 	err = fts_update_device(data);
45608426edaSThilo Cestonaro 	if (err < 0)
45708426edaSThilo Cestonaro 		return err;
45808426edaSThilo Cestonaro 
45908426edaSThilo Cestonaro 	return sprintf(buf, "%u\n", data->fan_source[index]);
46008426edaSThilo Cestonaro }
46108426edaSThilo Cestonaro 
462626f5eaeSGuenter Roeck static ssize_t fan_alarm_show(struct device *dev,
46308426edaSThilo Cestonaro 			      struct device_attribute *devattr, char *buf)
46408426edaSThilo Cestonaro {
46508426edaSThilo Cestonaro 	struct fts_data *data = dev_get_drvdata(dev);
46608426edaSThilo Cestonaro 	int index = to_sensor_dev_attr(devattr)->index;
46708426edaSThilo Cestonaro 	int err;
46808426edaSThilo Cestonaro 
46908426edaSThilo Cestonaro 	err = fts_update_device(data);
47008426edaSThilo Cestonaro 	if (err < 0)
47108426edaSThilo Cestonaro 		return err;
47208426edaSThilo Cestonaro 
47308426edaSThilo Cestonaro 	return sprintf(buf, "%d\n", !!(data->fan_alarm & BIT(index)));
47408426edaSThilo Cestonaro }
47508426edaSThilo Cestonaro 
47608426edaSThilo Cestonaro static ssize_t
477626f5eaeSGuenter Roeck fan_alarm_store(struct device *dev, struct device_attribute *devattr,
47808426edaSThilo Cestonaro 		const char *buf, size_t count)
47908426edaSThilo Cestonaro {
48008426edaSThilo Cestonaro 	struct fts_data *data = dev_get_drvdata(dev);
48108426edaSThilo Cestonaro 	int index = to_sensor_dev_attr(devattr)->index;
48208426edaSThilo Cestonaro 	long ret;
48308426edaSThilo Cestonaro 
48408426edaSThilo Cestonaro 	ret = fts_update_device(data);
48508426edaSThilo Cestonaro 	if (ret < 0)
48608426edaSThilo Cestonaro 		return ret;
48708426edaSThilo Cestonaro 
48808426edaSThilo Cestonaro 	if (kstrtoul(buf, 10, &ret) || ret != 0)
48908426edaSThilo Cestonaro 		return -EINVAL;
49008426edaSThilo Cestonaro 
49108426edaSThilo Cestonaro 	mutex_lock(&data->update_lock);
49208426edaSThilo Cestonaro 	ret = fts_read_byte(data->client, FTS_REG_FAN_CONTROL(index));
49308426edaSThilo Cestonaro 	if (ret < 0)
49408426edaSThilo Cestonaro 		goto error;
49508426edaSThilo Cestonaro 
49608426edaSThilo Cestonaro 	ret = fts_write_byte(data->client, FTS_REG_FAN_CONTROL(index),
49708426edaSThilo Cestonaro 			     ret | 0x1);
49808426edaSThilo Cestonaro 	if (ret < 0)
49908426edaSThilo Cestonaro 		goto error;
50008426edaSThilo Cestonaro 
50108426edaSThilo Cestonaro 	data->valid = false;
5027576750fSThilo Cestonaro 	ret = count;
50308426edaSThilo Cestonaro error:
50408426edaSThilo Cestonaro 	mutex_unlock(&data->update_lock);
50508426edaSThilo Cestonaro 	return ret;
50608426edaSThilo Cestonaro }
50708426edaSThilo Cestonaro 
50808426edaSThilo Cestonaro /*****************************************************************************/
50908426edaSThilo Cestonaro /* SysFS structs							     */
51008426edaSThilo Cestonaro /*****************************************************************************/
51108426edaSThilo Cestonaro 
51208426edaSThilo Cestonaro /* Temprature sensors */
513626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_value, 0);
514626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_value, 1);
515626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp3_input, temp_value, 2);
516626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp4_input, temp_value, 3);
517626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp5_input, temp_value, 4);
518626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp6_input, temp_value, 5);
519626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp7_input, temp_value, 6);
520626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp8_input, temp_value, 7);
521626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp9_input, temp_value, 8);
522626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp10_input, temp_value, 9);
523626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp11_input, temp_value, 10);
524626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp12_input, temp_value, 11);
525626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp13_input, temp_value, 12);
526626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp14_input, temp_value, 13);
527626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp15_input, temp_value, 14);
528626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp16_input, temp_value, 15);
52908426edaSThilo Cestonaro 
530626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0);
531626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1);
532626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp3_fault, temp_fault, 2);
533626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp4_fault, temp_fault, 3);
534626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp5_fault, temp_fault, 4);
535626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp6_fault, temp_fault, 5);
536626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp7_fault, temp_fault, 6);
537626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp8_fault, temp_fault, 7);
538626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp9_fault, temp_fault, 8);
539626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp10_fault, temp_fault, 9);
540626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp11_fault, temp_fault, 10);
541626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp12_fault, temp_fault, 11);
542626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp13_fault, temp_fault, 12);
543626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp14_fault, temp_fault, 13);
544626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp15_fault, temp_fault, 14);
545626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp16_fault, temp_fault, 15);
54608426edaSThilo Cestonaro 
547626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_alarm, temp_alarm, 0);
548626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_alarm, temp_alarm, 1);
549626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp3_alarm, temp_alarm, 2);
550626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp4_alarm, temp_alarm, 3);
551626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp5_alarm, temp_alarm, 4);
552626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp6_alarm, temp_alarm, 5);
553626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp7_alarm, temp_alarm, 6);
554626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp8_alarm, temp_alarm, 7);
555626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp9_alarm, temp_alarm, 8);
556626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp10_alarm, temp_alarm, 9);
557626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp11_alarm, temp_alarm, 10);
558626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp12_alarm, temp_alarm, 11);
559626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp13_alarm, temp_alarm, 12);
560626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp14_alarm, temp_alarm, 13);
561626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp15_alarm, temp_alarm, 14);
562626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp16_alarm, temp_alarm, 15);
56308426edaSThilo Cestonaro 
56408426edaSThilo Cestonaro static struct attribute *fts_temp_attrs[] = {
56508426edaSThilo Cestonaro 	&sensor_dev_attr_temp1_input.dev_attr.attr,
56608426edaSThilo Cestonaro 	&sensor_dev_attr_temp2_input.dev_attr.attr,
56708426edaSThilo Cestonaro 	&sensor_dev_attr_temp3_input.dev_attr.attr,
56808426edaSThilo Cestonaro 	&sensor_dev_attr_temp4_input.dev_attr.attr,
56908426edaSThilo Cestonaro 	&sensor_dev_attr_temp5_input.dev_attr.attr,
57008426edaSThilo Cestonaro 	&sensor_dev_attr_temp6_input.dev_attr.attr,
57108426edaSThilo Cestonaro 	&sensor_dev_attr_temp7_input.dev_attr.attr,
57208426edaSThilo Cestonaro 	&sensor_dev_attr_temp8_input.dev_attr.attr,
57308426edaSThilo Cestonaro 	&sensor_dev_attr_temp9_input.dev_attr.attr,
57408426edaSThilo Cestonaro 	&sensor_dev_attr_temp10_input.dev_attr.attr,
57508426edaSThilo Cestonaro 	&sensor_dev_attr_temp11_input.dev_attr.attr,
57608426edaSThilo Cestonaro 	&sensor_dev_attr_temp12_input.dev_attr.attr,
57708426edaSThilo Cestonaro 	&sensor_dev_attr_temp13_input.dev_attr.attr,
57808426edaSThilo Cestonaro 	&sensor_dev_attr_temp14_input.dev_attr.attr,
57908426edaSThilo Cestonaro 	&sensor_dev_attr_temp15_input.dev_attr.attr,
58008426edaSThilo Cestonaro 	&sensor_dev_attr_temp16_input.dev_attr.attr,
58108426edaSThilo Cestonaro 
58208426edaSThilo Cestonaro 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
58308426edaSThilo Cestonaro 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
58408426edaSThilo Cestonaro 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
58508426edaSThilo Cestonaro 	&sensor_dev_attr_temp4_fault.dev_attr.attr,
58608426edaSThilo Cestonaro 	&sensor_dev_attr_temp5_fault.dev_attr.attr,
58708426edaSThilo Cestonaro 	&sensor_dev_attr_temp6_fault.dev_attr.attr,
58808426edaSThilo Cestonaro 	&sensor_dev_attr_temp7_fault.dev_attr.attr,
58908426edaSThilo Cestonaro 	&sensor_dev_attr_temp8_fault.dev_attr.attr,
59008426edaSThilo Cestonaro 	&sensor_dev_attr_temp9_fault.dev_attr.attr,
59108426edaSThilo Cestonaro 	&sensor_dev_attr_temp10_fault.dev_attr.attr,
59208426edaSThilo Cestonaro 	&sensor_dev_attr_temp11_fault.dev_attr.attr,
59308426edaSThilo Cestonaro 	&sensor_dev_attr_temp12_fault.dev_attr.attr,
59408426edaSThilo Cestonaro 	&sensor_dev_attr_temp13_fault.dev_attr.attr,
59508426edaSThilo Cestonaro 	&sensor_dev_attr_temp14_fault.dev_attr.attr,
59608426edaSThilo Cestonaro 	&sensor_dev_attr_temp15_fault.dev_attr.attr,
59708426edaSThilo Cestonaro 	&sensor_dev_attr_temp16_fault.dev_attr.attr,
59808426edaSThilo Cestonaro 
59908426edaSThilo Cestonaro 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
60008426edaSThilo Cestonaro 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
60108426edaSThilo Cestonaro 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
60208426edaSThilo Cestonaro 	&sensor_dev_attr_temp4_alarm.dev_attr.attr,
60308426edaSThilo Cestonaro 	&sensor_dev_attr_temp5_alarm.dev_attr.attr,
60408426edaSThilo Cestonaro 	&sensor_dev_attr_temp6_alarm.dev_attr.attr,
60508426edaSThilo Cestonaro 	&sensor_dev_attr_temp7_alarm.dev_attr.attr,
60608426edaSThilo Cestonaro 	&sensor_dev_attr_temp8_alarm.dev_attr.attr,
60708426edaSThilo Cestonaro 	&sensor_dev_attr_temp9_alarm.dev_attr.attr,
60808426edaSThilo Cestonaro 	&sensor_dev_attr_temp10_alarm.dev_attr.attr,
60908426edaSThilo Cestonaro 	&sensor_dev_attr_temp11_alarm.dev_attr.attr,
61008426edaSThilo Cestonaro 	&sensor_dev_attr_temp12_alarm.dev_attr.attr,
61108426edaSThilo Cestonaro 	&sensor_dev_attr_temp13_alarm.dev_attr.attr,
61208426edaSThilo Cestonaro 	&sensor_dev_attr_temp14_alarm.dev_attr.attr,
61308426edaSThilo Cestonaro 	&sensor_dev_attr_temp15_alarm.dev_attr.attr,
61408426edaSThilo Cestonaro 	&sensor_dev_attr_temp16_alarm.dev_attr.attr,
61508426edaSThilo Cestonaro 	NULL
61608426edaSThilo Cestonaro };
61708426edaSThilo Cestonaro 
61808426edaSThilo Cestonaro /* Fans */
619626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_value, 0);
620626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_value, 1);
621626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan3_input, fan_value, 2);
622626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan4_input, fan_value, 3);
623626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan5_input, fan_value, 4);
624626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan6_input, fan_value, 5);
625626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan7_input, fan_value, 6);
626626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan8_input, fan_value, 7);
62708426edaSThilo Cestonaro 
628626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_source, fan_source, 0);
629626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_source, fan_source, 1);
630626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan3_source, fan_source, 2);
631626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan4_source, fan_source, 3);
632626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan5_source, fan_source, 4);
633626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan6_source, fan_source, 5);
634626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan7_source, fan_source, 6);
635626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan8_source, fan_source, 7);
63608426edaSThilo Cestonaro 
637626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan1_alarm, fan_alarm, 0);
638626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan2_alarm, fan_alarm, 1);
639626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan3_alarm, fan_alarm, 2);
640626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan4_alarm, fan_alarm, 3);
641626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan5_alarm, fan_alarm, 4);
642626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan6_alarm, fan_alarm, 5);
643626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan7_alarm, fan_alarm, 6);
644626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan8_alarm, fan_alarm, 7);
64508426edaSThilo Cestonaro 
64608426edaSThilo Cestonaro static struct attribute *fts_fan_attrs[] = {
64708426edaSThilo Cestonaro 	&sensor_dev_attr_fan1_input.dev_attr.attr,
64808426edaSThilo Cestonaro 	&sensor_dev_attr_fan2_input.dev_attr.attr,
64908426edaSThilo Cestonaro 	&sensor_dev_attr_fan3_input.dev_attr.attr,
65008426edaSThilo Cestonaro 	&sensor_dev_attr_fan4_input.dev_attr.attr,
65108426edaSThilo Cestonaro 	&sensor_dev_attr_fan5_input.dev_attr.attr,
65208426edaSThilo Cestonaro 	&sensor_dev_attr_fan6_input.dev_attr.attr,
65308426edaSThilo Cestonaro 	&sensor_dev_attr_fan7_input.dev_attr.attr,
65408426edaSThilo Cestonaro 	&sensor_dev_attr_fan8_input.dev_attr.attr,
65508426edaSThilo Cestonaro 
65608426edaSThilo Cestonaro 	&sensor_dev_attr_fan1_source.dev_attr.attr,
65708426edaSThilo Cestonaro 	&sensor_dev_attr_fan2_source.dev_attr.attr,
65808426edaSThilo Cestonaro 	&sensor_dev_attr_fan3_source.dev_attr.attr,
65908426edaSThilo Cestonaro 	&sensor_dev_attr_fan4_source.dev_attr.attr,
66008426edaSThilo Cestonaro 	&sensor_dev_attr_fan5_source.dev_attr.attr,
66108426edaSThilo Cestonaro 	&sensor_dev_attr_fan6_source.dev_attr.attr,
66208426edaSThilo Cestonaro 	&sensor_dev_attr_fan7_source.dev_attr.attr,
66308426edaSThilo Cestonaro 	&sensor_dev_attr_fan8_source.dev_attr.attr,
66408426edaSThilo Cestonaro 
66508426edaSThilo Cestonaro 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
66608426edaSThilo Cestonaro 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
66708426edaSThilo Cestonaro 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
66808426edaSThilo Cestonaro 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
66908426edaSThilo Cestonaro 	&sensor_dev_attr_fan5_alarm.dev_attr.attr,
67008426edaSThilo Cestonaro 	&sensor_dev_attr_fan6_alarm.dev_attr.attr,
67108426edaSThilo Cestonaro 	&sensor_dev_attr_fan7_alarm.dev_attr.attr,
67208426edaSThilo Cestonaro 	&sensor_dev_attr_fan8_alarm.dev_attr.attr,
67308426edaSThilo Cestonaro 	NULL
67408426edaSThilo Cestonaro };
67508426edaSThilo Cestonaro 
67608426edaSThilo Cestonaro /* Voltages */
677626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_input, in_value, 0);
678626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in2_input, in_value, 1);
679626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in3_input, in_value, 2);
680626f5eaeSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in4_input, in_value, 3);
68108426edaSThilo Cestonaro static struct attribute *fts_voltage_attrs[] = {
68208426edaSThilo Cestonaro 	&sensor_dev_attr_in1_input.dev_attr.attr,
68308426edaSThilo Cestonaro 	&sensor_dev_attr_in2_input.dev_attr.attr,
68408426edaSThilo Cestonaro 	&sensor_dev_attr_in3_input.dev_attr.attr,
68508426edaSThilo Cestonaro 	&sensor_dev_attr_in4_input.dev_attr.attr,
68608426edaSThilo Cestonaro 	NULL
68708426edaSThilo Cestonaro };
68808426edaSThilo Cestonaro 
68908426edaSThilo Cestonaro static const struct attribute_group fts_voltage_attr_group = {
69008426edaSThilo Cestonaro 	.attrs = fts_voltage_attrs
69108426edaSThilo Cestonaro };
69208426edaSThilo Cestonaro 
69308426edaSThilo Cestonaro static const struct attribute_group fts_temp_attr_group = {
69408426edaSThilo Cestonaro 	.attrs = fts_temp_attrs
69508426edaSThilo Cestonaro };
69608426edaSThilo Cestonaro 
69708426edaSThilo Cestonaro static const struct attribute_group fts_fan_attr_group = {
69808426edaSThilo Cestonaro 	.attrs = fts_fan_attrs
69908426edaSThilo Cestonaro };
70008426edaSThilo Cestonaro 
70108426edaSThilo Cestonaro static const struct attribute_group *fts_attr_groups[] = {
70208426edaSThilo Cestonaro 	&fts_voltage_attr_group,
70308426edaSThilo Cestonaro 	&fts_temp_attr_group,
70408426edaSThilo Cestonaro 	&fts_fan_attr_group,
70508426edaSThilo Cestonaro 	NULL
70608426edaSThilo Cestonaro };
70708426edaSThilo Cestonaro 
70808426edaSThilo Cestonaro /*****************************************************************************/
70908426edaSThilo Cestonaro /* Module initialization / remove functions				     */
71008426edaSThilo Cestonaro /*****************************************************************************/
7112d5aee43SThilo Cestonaro static int fts_detect(struct i2c_client *client,
7122d5aee43SThilo Cestonaro 		      struct i2c_board_info *info)
7132d5aee43SThilo Cestonaro {
7142d5aee43SThilo Cestonaro 	int val;
7152d5aee43SThilo Cestonaro 
716*23bc3cafSzuoqilin 	/* detection works with revision greater or equal to 0x2b */
7172d5aee43SThilo Cestonaro 	val = i2c_smbus_read_byte_data(client, FTS_DEVICE_REVISION_REG);
7182d5aee43SThilo Cestonaro 	if (val < 0x2b)
7192d5aee43SThilo Cestonaro 		return -ENODEV;
7202d5aee43SThilo Cestonaro 
7212d5aee43SThilo Cestonaro 	/* Device Detect Regs must have 0x17 0x34 and 0x54 */
7222d5aee43SThilo Cestonaro 	val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_1);
7232d5aee43SThilo Cestonaro 	if (val != 0x17)
7242d5aee43SThilo Cestonaro 		return -ENODEV;
7252d5aee43SThilo Cestonaro 
7262d5aee43SThilo Cestonaro 	val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_2);
7272d5aee43SThilo Cestonaro 	if (val != 0x34)
7282d5aee43SThilo Cestonaro 		return -ENODEV;
7292d5aee43SThilo Cestonaro 
7302d5aee43SThilo Cestonaro 	val = i2c_smbus_read_byte_data(client, FTS_DEVICE_DETECT_REG_3);
7312d5aee43SThilo Cestonaro 	if (val != 0x54)
7322d5aee43SThilo Cestonaro 		return -ENODEV;
7332d5aee43SThilo Cestonaro 
7342d5aee43SThilo Cestonaro 	/*
7352d5aee43SThilo Cestonaro 	 * 0x10 == Baseboard Management Controller, 0x01 == Teutates
7362d5aee43SThilo Cestonaro 	 * Device ID Reg needs to be 0x11
7372d5aee43SThilo Cestonaro 	 */
7382d5aee43SThilo Cestonaro 	val = i2c_smbus_read_byte_data(client, FTS_DEVICE_ID_REG);
7392d5aee43SThilo Cestonaro 	if (val != 0x11)
7402d5aee43SThilo Cestonaro 		return -ENODEV;
7412d5aee43SThilo Cestonaro 
7422d5aee43SThilo Cestonaro 	strlcpy(info->type, fts_id[0].name, I2C_NAME_SIZE);
7432d5aee43SThilo Cestonaro 	info->flags = 0;
7442d5aee43SThilo Cestonaro 	return 0;
7452d5aee43SThilo Cestonaro }
7462d5aee43SThilo Cestonaro 
74708426edaSThilo Cestonaro static int fts_remove(struct i2c_client *client)
74808426edaSThilo Cestonaro {
74908426edaSThilo Cestonaro 	struct fts_data *data = dev_get_drvdata(&client->dev);
75008426edaSThilo Cestonaro 
75108426edaSThilo Cestonaro 	watchdog_unregister_device(&data->wdd);
75208426edaSThilo Cestonaro 	return 0;
75308426edaSThilo Cestonaro }
75408426edaSThilo Cestonaro 
75567487038SStephen Kitt static int fts_probe(struct i2c_client *client)
75608426edaSThilo Cestonaro {
75708426edaSThilo Cestonaro 	u8 revision;
75808426edaSThilo Cestonaro 	struct fts_data *data;
75908426edaSThilo Cestonaro 	int err;
76008426edaSThilo Cestonaro 	s8 deviceid;
76108426edaSThilo Cestonaro 	struct device *hwmon_dev;
76208426edaSThilo Cestonaro 
76308426edaSThilo Cestonaro 	if (client->addr != 0x73)
76408426edaSThilo Cestonaro 		return -ENODEV;
76508426edaSThilo Cestonaro 
76608426edaSThilo Cestonaro 	/* Baseboard Management Controller check */
76708426edaSThilo Cestonaro 	deviceid = i2c_smbus_read_byte_data(client, FTS_DEVICE_ID_REG);
76808426edaSThilo Cestonaro 	if (deviceid > 0 && (deviceid & 0xF0) == 0x10) {
76908426edaSThilo Cestonaro 		switch (deviceid & 0x0F) {
77008426edaSThilo Cestonaro 		case 0x01:
77108426edaSThilo Cestonaro 			break;
77208426edaSThilo Cestonaro 		default:
77308426edaSThilo Cestonaro 			dev_dbg(&client->dev,
77408426edaSThilo Cestonaro 				"No Baseboard Management Controller\n");
77508426edaSThilo Cestonaro 			return -ENODEV;
77608426edaSThilo Cestonaro 		}
77708426edaSThilo Cestonaro 	} else {
77808426edaSThilo Cestonaro 		dev_dbg(&client->dev, "No fujitsu board\n");
77908426edaSThilo Cestonaro 		return -ENODEV;
78008426edaSThilo Cestonaro 	}
78108426edaSThilo Cestonaro 
78208426edaSThilo Cestonaro 	data = devm_kzalloc(&client->dev, sizeof(struct fts_data),
78308426edaSThilo Cestonaro 			    GFP_KERNEL);
78408426edaSThilo Cestonaro 	if (!data)
78508426edaSThilo Cestonaro 		return -ENOMEM;
78608426edaSThilo Cestonaro 
78708426edaSThilo Cestonaro 	mutex_init(&data->update_lock);
78808426edaSThilo Cestonaro 	mutex_init(&data->access_lock);
78908426edaSThilo Cestonaro 	data->client = client;
79008426edaSThilo Cestonaro 	dev_set_drvdata(&client->dev, data);
79108426edaSThilo Cestonaro 
79208426edaSThilo Cestonaro 	err = i2c_smbus_read_byte_data(client, FTS_DEVICE_REVISION_REG);
79308426edaSThilo Cestonaro 	if (err < 0)
79408426edaSThilo Cestonaro 		return err;
79508426edaSThilo Cestonaro 	revision = err;
79608426edaSThilo Cestonaro 
79708426edaSThilo Cestonaro 	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
79808426edaSThilo Cestonaro 							   "ftsteutates",
79908426edaSThilo Cestonaro 							   data,
80008426edaSThilo Cestonaro 							   fts_attr_groups);
80108426edaSThilo Cestonaro 	if (IS_ERR(hwmon_dev))
80208426edaSThilo Cestonaro 		return PTR_ERR(hwmon_dev);
80308426edaSThilo Cestonaro 
80408426edaSThilo Cestonaro 	err = fts_watchdog_init(data);
80508426edaSThilo Cestonaro 	if (err)
80608426edaSThilo Cestonaro 		return err;
80708426edaSThilo Cestonaro 
80808426edaSThilo Cestonaro 	dev_info(&client->dev, "Detected FTS Teutates chip, revision: %d.%d\n",
80908426edaSThilo Cestonaro 		 (revision & 0xF0) >> 4, revision & 0x0F);
81008426edaSThilo Cestonaro 	return 0;
81108426edaSThilo Cestonaro }
81208426edaSThilo Cestonaro 
81308426edaSThilo Cestonaro /*****************************************************************************/
81408426edaSThilo Cestonaro /* Module Details							     */
81508426edaSThilo Cestonaro /*****************************************************************************/
81608426edaSThilo Cestonaro static struct i2c_driver fts_driver = {
8172d5aee43SThilo Cestonaro 	.class = I2C_CLASS_HWMON,
81808426edaSThilo Cestonaro 	.driver = {
81908426edaSThilo Cestonaro 		.name = "ftsteutates",
82008426edaSThilo Cestonaro 	},
82108426edaSThilo Cestonaro 	.id_table = fts_id,
82267487038SStephen Kitt 	.probe_new = fts_probe,
82308426edaSThilo Cestonaro 	.remove = fts_remove,
8242d5aee43SThilo Cestonaro 	.detect = fts_detect,
8252d5aee43SThilo Cestonaro 	.address_list = normal_i2c,
82608426edaSThilo Cestonaro };
82708426edaSThilo Cestonaro 
82808426edaSThilo Cestonaro module_i2c_driver(fts_driver);
82908426edaSThilo Cestonaro 
83008426edaSThilo Cestonaro MODULE_AUTHOR("Thilo Cestonaro <thilo.cestonaro@ts.fujitsu.com>");
83108426edaSThilo Cestonaro MODULE_DESCRIPTION("FTS Teutates driver");
83208426edaSThilo Cestonaro MODULE_LICENSE("GPL");
833