1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * An hwmon driver for the Microchip TC74
5 * Copyright 2015 Maciej Szmigiero <mail@maciej.szmigiero.name>
8 * Copyright 2006 Stefan Roese, DENX Software Engineering
9 * Copyright 2008 Sean MacLennan, PIKA Technologies
10 * Copyright 2008 Frank Edelhaeuser, Spansion Inc.
13 #include <linux/bitops.h>
14 #include <linux/err.h>
15 #include <linux/hwmon.h>
16 #include <linux/hwmon-sysfs.h>
17 #include <linux/i2c.h>
18 #include <linux/jiffies.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/sysfs.h>
25 #define TC74_REG_TEMP 0x00
26 #define TC74_REG_CONFIG 0x01
29 struct i2c_client *client;
30 struct mutex lock; /* atomic read data updates */
31 bool valid; /* validity of fields below */
32 unsigned long next_update; /* In jiffies */
33 s8 temp_input; /* Temp value in dC */
36 static int tc74_update_device(struct device *dev)
38 struct tc74_data *data = dev_get_drvdata(dev);
39 struct i2c_client *client = data->client;
42 ret = mutex_lock_interruptible(&data->lock);
46 if (time_after(jiffies, data->next_update) || !data->valid) {
49 value = i2c_smbus_read_byte_data(client, TC74_REG_CONFIG);
51 dev_dbg(&client->dev, "TC74_REG_CONFIG read err %d\n",
58 if (!(value & BIT(6))) {
65 value = i2c_smbus_read_byte_data(client, TC74_REG_TEMP);
67 dev_dbg(&client->dev, "TC74_REG_TEMP read err %d\n",
74 data->temp_input = value;
75 data->next_update = jiffies + HZ / 4;
80 mutex_unlock(&data->lock);
85 static ssize_t temp_input_show(struct device *dev,
86 struct device_attribute *attr, char *buf)
88 struct tc74_data *data = dev_get_drvdata(dev);
91 ret = tc74_update_device(dev);
95 return sprintf(buf, "%d\n", data->temp_input * 1000);
97 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
99 static struct attribute *tc74_attrs[] = {
100 &sensor_dev_attr_temp1_input.dev_attr.attr,
104 ATTRIBUTE_GROUPS(tc74);
106 static int tc74_probe(struct i2c_client *client)
108 struct device *dev = &client->dev;
109 struct tc74_data *data;
110 struct device *hwmon_dev;
113 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
116 data = devm_kzalloc(dev, sizeof(struct tc74_data), GFP_KERNEL);
120 data->client = client;
121 mutex_init(&data->lock);
123 /* Make sure the chip is powered up. */
124 conf = i2c_smbus_read_byte_data(client, TC74_REG_CONFIG);
126 dev_err(dev, "unable to read config register\n");
132 dev_err(dev, "invalid config register value\n");
142 ret = i2c_smbus_write_byte_data(client, TC74_REG_CONFIG, conf);
144 dev_warn(dev, "unable to disable STANDBY\n");
147 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
150 return PTR_ERR_OR_ZERO(hwmon_dev);
153 static const struct i2c_device_id tc74_id[] = {
157 MODULE_DEVICE_TABLE(i2c, tc74_id);
159 static struct i2c_driver tc74_driver = {
167 module_i2c_driver(tc74_driver);
169 MODULE_AUTHOR("Maciej Szmigiero <mail@maciej.szmigiero.name>");
171 MODULE_DESCRIPTION("TC74 driver");
172 MODULE_LICENSE("GPL");