summaryrefslogtreecommitdiff
path: root/drivers/hwmon/tc74.c
blob: ace55da97fc29f717dc2eef9055196d9a9f41e0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * An hwmon driver for the Microchip TC74
 *
 * Copyright 2015 Maciej Szmigiero <mail@maciej.szmigiero.name>
 *
 * Based on ad7414.c:
 *	Copyright 2006 Stefan Roese, DENX Software Engineering
 *	Copyright 2008 Sean MacLennan, PIKA Technologies
 *	Copyright 2008 Frank Edelhaeuser, Spansion Inc.
 */

#include <linux/bitops.h>
#include <linux/err.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/i2c.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/sysfs.h>

/* TC74 registers */
#define TC74_REG_TEMP		0x00
#define TC74_REG_CONFIG		0x01

struct tc74_data {
	struct i2c_client	*client;
	struct mutex		lock;	/* atomic read data updates */
	bool			valid;	/* validity of fields below */
	unsigned long		next_update;	/* In jiffies */
	s8			temp_input;	/* Temp value in dC */
};

static int tc74_update_device(struct device *dev)
{
	struct tc74_data *data = dev_get_drvdata(dev);
	struct i2c_client *client = data->client;
	int ret;

	ret = mutex_lock_interruptible(&data->lock);
	if (ret)
		return ret;

	if (time_after(jiffies, data->next_update) || !data->valid) {
		s32 value;

		value = i2c_smbus_read_byte_data(client, TC74_REG_CONFIG);
		if (value < 0) {
			dev_dbg(&client->dev, "TC74_REG_CONFIG read err %d\n",
				(int)value);

			ret = value;
			goto ret_unlock;
		}

		if (!(value & BIT(6))) {
			/* not ready yet */

			ret = -EAGAIN;
			goto ret_unlock;
		}

		value = i2c_smbus_read_byte_data(client, TC74_REG_TEMP);
		if (value < 0) {
			dev_dbg(&client->dev, "TC74_REG_TEMP read err %d\n",
				(int)value);

			ret = value;
			goto ret_unlock;
		}

		data->temp_input = value;
		data->next_update = jiffies + HZ / 4;
		data->valid = true;
	}

ret_unlock:
	mutex_unlock(&data->lock);

	return ret;
}

static ssize_t temp_input_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	struct tc74_data *data = dev_get_drvdata(dev);
	int ret;

	ret = tc74_update_device(dev);
	if (ret)
		return ret;

	return sprintf(buf, "%d\n", data->temp_input * 1000);
}
static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);

static struct attribute *tc74_attrs[] = {
	&sensor_dev_attr_temp1_input.dev_attr.attr,
	NULL
};

ATTRIBUTE_GROUPS(tc74);

static int tc74_probe(struct i2c_client *client)
{
	struct device *dev = &client->dev;
	struct tc74_data *data;
	struct device *hwmon_dev;
	s32 conf;

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
		return -EOPNOTSUPP;

	data = devm_kzalloc(dev, sizeof(struct tc74_data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	data->client = client;
	mutex_init(&data->lock);

	/* Make sure the chip is powered up. */
	conf = i2c_smbus_read_byte_data(client, TC74_REG_CONFIG);
	if (conf < 0) {
		dev_err(dev, "unable to read config register\n");

		return conf;
	}

	if (conf & 0x3f) {
		dev_err(dev, "invalid config register value\n");

		return -ENODEV;
	}

	if (conf & BIT(7)) {
		s32 ret;

		conf &= ~BIT(7);

		ret = i2c_smbus_write_byte_data(client, TC74_REG_CONFIG, conf);
		if (ret)
			dev_warn(dev, "unable to disable STANDBY\n");
	}

	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
							   client->name,
							   data, tc74_groups);
	return PTR_ERR_OR_ZERO(hwmon_dev);
}

static const struct i2c_device_id tc74_id[] = {
	{ "tc74", 0 },
	{}
};
MODULE_DEVICE_TABLE(i2c, tc74_id);

static struct i2c_driver tc74_driver = {
	.driver = {
		.name	= "tc74",
	},
	.probe_new = tc74_probe,
	.id_table = tc74_id,
};

module_i2c_driver(tc74_driver);

MODULE_AUTHOR("Maciej Szmigiero <mail@maciej.szmigiero.name>");

MODULE_DESCRIPTION("TC74 driver");
MODULE_LICENSE("GPL");