^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * An hwmon driver for the Microchip TC74
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2015 Maciej Szmigiero <mail@maciej.szmigiero.name>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Based on ad7414.c:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright 2006 Stefan Roese, DENX Software Engineering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright 2008 Sean MacLennan, PIKA Technologies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright 2008 Frank Edelhaeuser, Spansion Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* TC74 registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define TC74_REG_TEMP 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define TC74_REG_CONFIG 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct tc74_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct mutex lock; /* atomic read data updates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) bool valid; /* validity of fields below */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned long next_update; /* In jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) s8 temp_input; /* Temp value in dC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static int tc74_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct tc74_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ret = mutex_lock_interruptible(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (time_after(jiffies, data->next_update) || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) s32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) value = i2c_smbus_read_byte_data(client, TC74_REG_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (value < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) dev_dbg(&client->dev, "TC74_REG_CONFIG read err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) (int)value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ret = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) goto ret_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!(value & BIT(6))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* not ready yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) goto ret_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) value = i2c_smbus_read_byte_data(client, TC74_REG_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (value < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) dev_dbg(&client->dev, "TC74_REG_TEMP read err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) (int)value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ret = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) goto ret_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) data->temp_input = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) data->next_update = jiffies + HZ / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) data->valid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ret_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static ssize_t temp_input_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct tc74_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ret = tc74_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return sprintf(buf, "%d\n", data->temp_input * 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static struct attribute *tc74_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) &sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ATTRIBUTE_GROUPS(tc74);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static int tc74_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct tc74_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) s32 conf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) data = devm_kzalloc(dev, sizeof(struct tc74_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) mutex_init(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* Make sure the chip is powered up. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) conf = i2c_smbus_read_byte_data(client, TC74_REG_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (conf < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) dev_err(dev, "unable to read config register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return conf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (conf & 0x3f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) dev_err(dev, "invalid config register value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (conf & BIT(7)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) s32 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) conf &= ~BIT(7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ret = i2c_smbus_write_byte_data(client, TC74_REG_CONFIG, conf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) dev_warn(dev, "unable to disable STANDBY\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) hwmon_dev = devm_hwmon_device_register_with_groups(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) data, tc74_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static const struct i2c_device_id tc74_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) { "tc74", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) MODULE_DEVICE_TABLE(i2c, tc74_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static struct i2c_driver tc74_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .name = "tc74",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .probe_new = tc74_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .id_table = tc74_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) module_i2c_driver(tc74_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) MODULE_AUTHOR("Maciej Szmigiero <mail@maciej.szmigiero.name>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) MODULE_DESCRIPTION("TC74 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) MODULE_LICENSE("GPL");