0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/bitops.h>
0014 #include <linux/err.h>
0015 #include <linux/hwmon.h>
0016 #include <linux/hwmon-sysfs.h>
0017 #include <linux/i2c.h>
0018 #include <linux/jiffies.h>
0019 #include <linux/module.h>
0020 #include <linux/mutex.h>
0021 #include <linux/slab.h>
0022 #include <linux/sysfs.h>
0023
0024
0025 #define TC74_REG_TEMP 0x00
0026 #define TC74_REG_CONFIG 0x01
0027
0028 struct tc74_data {
0029 struct i2c_client *client;
0030 struct mutex lock;
0031 bool valid;
0032 unsigned long next_update;
0033 s8 temp_input;
0034 };
0035
0036 static int tc74_update_device(struct device *dev)
0037 {
0038 struct tc74_data *data = dev_get_drvdata(dev);
0039 struct i2c_client *client = data->client;
0040 int ret;
0041
0042 ret = mutex_lock_interruptible(&data->lock);
0043 if (ret)
0044 return ret;
0045
0046 if (time_after(jiffies, data->next_update) || !data->valid) {
0047 s32 value;
0048
0049 value = i2c_smbus_read_byte_data(client, TC74_REG_CONFIG);
0050 if (value < 0) {
0051 dev_dbg(&client->dev, "TC74_REG_CONFIG read err %d\n",
0052 (int)value);
0053
0054 ret = value;
0055 goto ret_unlock;
0056 }
0057
0058 if (!(value & BIT(6))) {
0059
0060
0061 ret = -EAGAIN;
0062 goto ret_unlock;
0063 }
0064
0065 value = i2c_smbus_read_byte_data(client, TC74_REG_TEMP);
0066 if (value < 0) {
0067 dev_dbg(&client->dev, "TC74_REG_TEMP read err %d\n",
0068 (int)value);
0069
0070 ret = value;
0071 goto ret_unlock;
0072 }
0073
0074 data->temp_input = value;
0075 data->next_update = jiffies + HZ / 4;
0076 data->valid = true;
0077 }
0078
0079 ret_unlock:
0080 mutex_unlock(&data->lock);
0081
0082 return ret;
0083 }
0084
0085 static ssize_t temp_input_show(struct device *dev,
0086 struct device_attribute *attr, char *buf)
0087 {
0088 struct tc74_data *data = dev_get_drvdata(dev);
0089 int ret;
0090
0091 ret = tc74_update_device(dev);
0092 if (ret)
0093 return ret;
0094
0095 return sprintf(buf, "%d\n", data->temp_input * 1000);
0096 }
0097 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
0098
0099 static struct attribute *tc74_attrs[] = {
0100 &sensor_dev_attr_temp1_input.dev_attr.attr,
0101 NULL
0102 };
0103
0104 ATTRIBUTE_GROUPS(tc74);
0105
0106 static int tc74_probe(struct i2c_client *client)
0107 {
0108 struct device *dev = &client->dev;
0109 struct tc74_data *data;
0110 struct device *hwmon_dev;
0111 s32 conf;
0112
0113 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0114 return -EOPNOTSUPP;
0115
0116 data = devm_kzalloc(dev, sizeof(struct tc74_data), GFP_KERNEL);
0117 if (!data)
0118 return -ENOMEM;
0119
0120 data->client = client;
0121 mutex_init(&data->lock);
0122
0123
0124 conf = i2c_smbus_read_byte_data(client, TC74_REG_CONFIG);
0125 if (conf < 0) {
0126 dev_err(dev, "unable to read config register\n");
0127
0128 return conf;
0129 }
0130
0131 if (conf & 0x3f) {
0132 dev_err(dev, "invalid config register value\n");
0133
0134 return -ENODEV;
0135 }
0136
0137 if (conf & BIT(7)) {
0138 s32 ret;
0139
0140 conf &= ~BIT(7);
0141
0142 ret = i2c_smbus_write_byte_data(client, TC74_REG_CONFIG, conf);
0143 if (ret)
0144 dev_warn(dev, "unable to disable STANDBY\n");
0145 }
0146
0147 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
0148 client->name,
0149 data, tc74_groups);
0150 return PTR_ERR_OR_ZERO(hwmon_dev);
0151 }
0152
0153 static const struct i2c_device_id tc74_id[] = {
0154 { "tc74", 0 },
0155 {}
0156 };
0157 MODULE_DEVICE_TABLE(i2c, tc74_id);
0158
0159 static struct i2c_driver tc74_driver = {
0160 .driver = {
0161 .name = "tc74",
0162 },
0163 .probe_new = tc74_probe,
0164 .id_table = tc74_id,
0165 };
0166
0167 module_i2c_driver(tc74_driver);
0168
0169 MODULE_AUTHOR("Maciej Szmigiero <mail@maciej.szmigiero.name>");
0170
0171 MODULE_DESCRIPTION("TC74 driver");
0172 MODULE_LICENSE("GPL");