^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) * lm77.c - Part of lm_sensors, Linux kernel modules for hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * monitoring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2004 Andras BALI <drewie@freemail.hu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Heavily based on lm75.c by Frodo Looijaard <frodol@dds.nl>. The LM77
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * is a temperature sensor and thermal window comparator with 0.5 deg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * resolution made by National Semiconductor. Complete datasheet can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * obtained at their site:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * http://www.national.com/pf/LM/LM77.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.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/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* Addresses to scan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* The LM77 registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define LM77_REG_TEMP 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define LM77_REG_CONF 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define LM77_REG_TEMP_HYST 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define LM77_REG_TEMP_CRIT 0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define LM77_REG_TEMP_MIN 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define LM77_REG_TEMP_MAX 0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) enum temp_index {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) t_input = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) t_crit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) t_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) t_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) t_hyst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) t_num_temp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static const u8 temp_regs[t_num_temp] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) [t_input] = LM77_REG_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) [t_min] = LM77_REG_TEMP_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) [t_max] = LM77_REG_TEMP_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) [t_crit] = LM77_REG_TEMP_CRIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) [t_hyst] = LM77_REG_TEMP_HYST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* Each client has this additional data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct lm77_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) char valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) unsigned long last_updated; /* In jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int temp[t_num_temp]; /* index using temp_index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) u8 alarms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* straight from the datasheet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define LM77_TEMP_MIN (-55000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define LM77_TEMP_MAX 125000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * In the temperature registers, the low 3 bits are not part of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * temperature values; they are the status bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static inline s16 LM77_TEMP_TO_REG(int temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return (temp / 500) * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static inline int LM77_TEMP_FROM_REG(s16 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return (reg / 8) * 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * All registers are word-sized, except for the configuration register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * The LM77 uses the high-byte first convention.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static u16 lm77_read_value(struct i2c_client *client, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (reg == LM77_REG_CONF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return i2c_smbus_read_byte_data(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return i2c_smbus_read_word_swapped(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (reg == LM77_REG_CONF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return i2c_smbus_write_byte_data(client, reg, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return i2c_smbus_write_word_swapped(client, reg, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static struct lm77_data *lm77_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct lm77_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) dev_dbg(&client->dev, "Starting lm77 update\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) for (i = 0; i < t_num_temp; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) data->temp[i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) LM77_TEMP_FROM_REG(lm77_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) temp_regs[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) data->alarms =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /* sysfs stuff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct lm77_data *data = lm77_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return sprintf(buf, "%d\n", data->temp[attr->index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static ssize_t temp_hyst_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct lm77_data *data = lm77_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) int nr = attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) temp = nr == t_min ? data->temp[nr] + data->temp[t_hyst] :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) data->temp[nr] - data->temp[t_hyst];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return sprintf(buf, "%d\n", temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static ssize_t temp_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct device_attribute *devattr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct lm77_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int nr = attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) val = clamp_val(val, LM77_TEMP_MIN, LM77_TEMP_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) data->temp[nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) lm77_write_value(client, temp_regs[nr], LM77_TEMP_TO_REG(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * hysteresis is stored as a relative value on the chip, so it has to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * converted first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static ssize_t temp_hyst_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct lm77_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) val = clamp_val(data->temp[t_crit] - val, LM77_TEMP_MIN, LM77_TEMP_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) data->temp[t_hyst] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) lm77_write_value(client, LM77_REG_TEMP_HYST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) LM77_TEMP_TO_REG(data->temp[t_hyst]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int bitnr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct lm77_data *data = lm77_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, t_crit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, t_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, t_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp_hyst, t_crit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static SENSOR_DEVICE_ATTR_RO(temp1_min_hyst, temp_hyst, t_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, temp_hyst, t_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static struct attribute *lm77_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) &sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) &sensor_dev_attr_temp1_crit.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) &sensor_dev_attr_temp1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) &sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ATTRIBUTE_GROUPS(lm77);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* Return 0 if detection is successful, -ENODEV otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static int lm77_detect(struct i2c_client *client, struct i2c_board_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int i, cur, conf, hyst, crit, min, max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) I2C_FUNC_SMBUS_WORD_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * Here comes the remaining detection. Since the LM77 has no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * register dedicated to identification, we have to rely on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * following tricks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * 1. the high 4 bits represent the sign and thus they should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * always be the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * 2. the high 3 bits are unused in the configuration register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * 3. addresses 0x06 and 0x07 return the last read value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * 4. registers cycling over 8-address boundaries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * Word-sized registers are high-byte first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* addresses cycling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) cur = i2c_smbus_read_word_data(client, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) conf = i2c_smbus_read_byte_data(client, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) hyst = i2c_smbus_read_word_data(client, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) crit = i2c_smbus_read_word_data(client, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) min = i2c_smbus_read_word_data(client, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) max = i2c_smbus_read_word_data(client, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) for (i = 8; i <= 0xff; i += 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (i2c_smbus_read_byte_data(client, i + 1) != conf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) || i2c_smbus_read_word_data(client, i + 2) != hyst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) || i2c_smbus_read_word_data(client, i + 3) != crit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) || i2c_smbus_read_word_data(client, i + 4) != min
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) || i2c_smbus_read_word_data(client, i + 5) != max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /* sign bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /* unused bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (conf & 0xe0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /* 0x06 and 0x07 return the last read value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) cur = i2c_smbus_read_word_data(client, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (i2c_smbus_read_word_data(client, 6) != cur
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) || i2c_smbus_read_word_data(client, 7) != cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) hyst = i2c_smbus_read_word_data(client, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (i2c_smbus_read_word_data(client, 6) != hyst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) || i2c_smbus_read_word_data(client, 7) != hyst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) min = i2c_smbus_read_word_data(client, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (i2c_smbus_read_word_data(client, 6) != min
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) || i2c_smbus_read_word_data(client, 7) != min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) strlcpy(info->type, "lm77", I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static void lm77_init_client(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) /* Initialize the LM77 chip - turn off shutdown mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) int conf = lm77_read_value(client, LM77_REG_CONF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (conf & 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static int lm77_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct lm77_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) data = devm_kzalloc(dev, sizeof(struct lm77_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) /* Initialize the LM77 chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) lm77_init_client(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) data, lm77_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static const struct i2c_device_id lm77_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) { "lm77", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) MODULE_DEVICE_TABLE(i2c, lm77_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /* This is the driver that will be inserted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static struct i2c_driver lm77_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) .class = I2C_CLASS_HWMON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) .name = "lm77",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) .probe_new = lm77_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) .id_table = lm77_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) .detect = lm77_detect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) .address_list = normal_i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) module_i2c_driver(lm77_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) MODULE_DESCRIPTION("LM77 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) MODULE_LICENSE("GPL");