Blame view
drivers/hwmon/lm73.c
7.35 KB
067760774
|
1 |
// SPDX-License-Identifier: GPL-2.0-only |
4e233cbed
|
2 3 4 5 6 7 8 9 10 11 |
/* * LM73 Sensor driver * Based on LM75 * * Copyright (C) 2007, CenoSYS (www.cenosys.com). * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu). * * Guillaume Ligneul <guillaume.ligneul@gmail.com> * Adrien Demarez <adrien.demarez@bolloretelecom.eu> * Jeremy Laine <jeremy.laine@bolloretelecom.eu> |
2bf9233a1
|
12 |
* Chris Verges <kg4ysn@gmail.com> |
4e233cbed
|
13 14 15 16 |
*/ #include <linux/module.h> #include <linux/init.h> |
4e233cbed
|
17 18 19 20 21 22 23 24 25 |
#include <linux/i2c.h> #include <linux/hwmon.h> #include <linux/hwmon-sysfs.h> #include <linux/err.h> /* Addresses scanned */ static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; |
4e233cbed
|
26 27 28 29 30 31 32 |
/* LM73 registers */ #define LM73_REG_INPUT 0x00 #define LM73_REG_CONF 0x01 #define LM73_REG_MAX 0x02 #define LM73_REG_MIN 0x03 #define LM73_REG_CTRL 0x04 #define LM73_REG_ID 0x07 |
90f4102ce
|
33 |
#define LM73_ID 0x9001 /* 0x0190, byte-swapped */ |
4e233cbed
|
34 |
#define DRVNAME "lm73" |
91bba6880
|
35 36 |
#define LM73_TEMP_MIN (-256000 / 250) #define LM73_TEMP_MAX (255750 / 250) |
4e233cbed
|
37 |
|
8c14d126a
|
38 39 40 |
#define LM73_CTRL_RES_SHIFT 5 #define LM73_CTRL_RES_MASK (BIT(5) | BIT(6)) #define LM73_CTRL_TO_MASK BIT(7) |
2bf9233a1
|
41 42 |
#define LM73_CTRL_HI_SHIFT 2 #define LM73_CTRL_LO_SHIFT 1 |
8c14d126a
|
43 44 45 46 47 48 49 50 |
static const unsigned short lm73_convrates[] = { 14, /* 11-bits (0.25000 C/LSB): RES1 Bit = 0, RES0 Bit = 0 */ 28, /* 12-bits (0.12500 C/LSB): RES1 Bit = 0, RES0 Bit = 1 */ 56, /* 13-bits (0.06250 C/LSB): RES1 Bit = 1, RES0 Bit = 0 */ 112, /* 14-bits (0.03125 C/LSB): RES1 Bit = 1, RES0 Bit = 1 */ }; struct lm73_data { |
3710263f1
|
51 |
struct i2c_client *client; |
8c14d126a
|
52 53 54 |
struct mutex lock; u8 ctrl; /* control register value */ }; |
4e233cbed
|
55 |
|
8c14d126a
|
56 |
/*-----------------------------------------------------------------------*/ |
4e233cbed
|
57 |
|
0f875acc9
|
58 59 |
static ssize_t temp_store(struct device *dev, struct device_attribute *da, const char *buf, size_t count) |
4e233cbed
|
60 61 |
{ struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
3710263f1
|
62 |
struct lm73_data *data = dev_get_drvdata(dev); |
4e233cbed
|
63 64 |
long temp; short value; |
0602934f3
|
65 |
s32 err; |
4e233cbed
|
66 |
|
179c4fdb5
|
67 |
int status = kstrtol(buf, 10, &temp); |
4e233cbed
|
68 69 70 71 |
if (status < 0) return status; /* Write value */ |
91bba6880
|
72 |
value = clamp_val(temp / 250, LM73_TEMP_MIN, LM73_TEMP_MAX) << 5; |
3710263f1
|
73 |
err = i2c_smbus_write_word_swapped(data->client, attr->index, value); |
0602934f3
|
74 |
return (err < 0) ? err : count; |
4e233cbed
|
75 |
} |
0f875acc9
|
76 |
static ssize_t temp_show(struct device *dev, struct device_attribute *da, |
4e233cbed
|
77 78 79 |
char *buf) { struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
3710263f1
|
80 |
struct lm73_data *data = dev_get_drvdata(dev); |
0602934f3
|
81 |
int temp; |
3710263f1
|
82 |
s32 err = i2c_smbus_read_word_swapped(data->client, attr->index); |
0602934f3
|
83 84 |
if (err < 0) return err; |
4e233cbed
|
85 86 |
/* use integer division instead of equivalent right shift to guarantee arithmetic shift and preserve the sign */ |
0602934f3
|
87 88 89 |
temp = (((s16) err) * 250) / 32; return scnprintf(buf, PAGE_SIZE, "%d ", temp); |
4e233cbed
|
90 |
} |
0f875acc9
|
91 92 |
static ssize_t convrate_store(struct device *dev, struct device_attribute *da, const char *buf, size_t count) |
8c14d126a
|
93 |
{ |
3710263f1
|
94 |
struct lm73_data *data = dev_get_drvdata(dev); |
8c14d126a
|
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
unsigned long convrate; s32 err; int res = 0; err = kstrtoul(buf, 10, &convrate); if (err < 0) return err; /* * Convert the desired conversion rate into register bits. * res is already initialized, and everything past the second-to-last * value in the array is treated as belonging to the last value * in the array. */ while (res < (ARRAY_SIZE(lm73_convrates) - 1) && convrate > lm73_convrates[res]) res++; mutex_lock(&data->lock); data->ctrl &= LM73_CTRL_TO_MASK; data->ctrl |= res << LM73_CTRL_RES_SHIFT; |
3710263f1
|
116 117 |
err = i2c_smbus_write_byte_data(data->client, LM73_REG_CTRL, data->ctrl); |
8c14d126a
|
118 119 120 121 122 123 124 |
mutex_unlock(&data->lock); if (err < 0) return err; return count; } |
0f875acc9
|
125 |
static ssize_t convrate_show(struct device *dev, struct device_attribute *da, |
8c14d126a
|
126 127 |
char *buf) { |
3710263f1
|
128 |
struct lm73_data *data = dev_get_drvdata(dev); |
8c14d126a
|
129 130 131 132 133 134 |
int res; res = (data->ctrl & LM73_CTRL_RES_MASK) >> LM73_CTRL_RES_SHIFT; return scnprintf(buf, PAGE_SIZE, "%hu ", lm73_convrates[res]); } |
4e233cbed
|
135 |
|
0f875acc9
|
136 |
static ssize_t maxmin_alarm_show(struct device *dev, |
2bf9233a1
|
137 138 |
struct device_attribute *da, char *buf) { |
2bf9233a1
|
139 |
struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
3710263f1
|
140 |
struct lm73_data *data = dev_get_drvdata(dev); |
2bf9233a1
|
141 142 143 |
s32 ctrl; mutex_lock(&data->lock); |
3710263f1
|
144 |
ctrl = i2c_smbus_read_byte_data(data->client, LM73_REG_CTRL); |
2bf9233a1
|
145 146 147 148 149 150 151 152 153 154 155 156 |
if (ctrl < 0) goto abort; data->ctrl = ctrl; mutex_unlock(&data->lock); return scnprintf(buf, PAGE_SIZE, "%d ", (ctrl >> attr->index) & 1); abort: mutex_unlock(&data->lock); return ctrl; } |
4e233cbed
|
157 158 159 |
/*-----------------------------------------------------------------------*/ /* sysfs attributes for hwmon */ |
0f875acc9
|
160 161 162 163 164 165 166 167 |
static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, LM73_REG_MAX); static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, LM73_REG_MIN); static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, LM73_REG_INPUT); static SENSOR_DEVICE_ATTR_RW(update_interval, convrate, 0); static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, maxmin_alarm, LM73_CTRL_HI_SHIFT); static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, maxmin_alarm, LM73_CTRL_LO_SHIFT); |
4e233cbed
|
168 |
|
3710263f1
|
169 |
static struct attribute *lm73_attrs[] = { |
4e233cbed
|
170 171 172 |
&sensor_dev_attr_temp1_input.dev_attr.attr, &sensor_dev_attr_temp1_max.dev_attr.attr, &sensor_dev_attr_temp1_min.dev_attr.attr, |
8c14d126a
|
173 |
&sensor_dev_attr_update_interval.dev_attr.attr, |
2bf9233a1
|
174 175 |
&sensor_dev_attr_temp1_max_alarm.dev_attr.attr, &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
4e233cbed
|
176 177 |
NULL }; |
3710263f1
|
178 |
ATTRIBUTE_GROUPS(lm73); |
4e233cbed
|
179 180 181 182 183 184 185 186 |
/*-----------------------------------------------------------------------*/ /* device probe and removal */ static int lm73_probe(struct i2c_client *client, const struct i2c_device_id *id) { |
3710263f1
|
187 188 |
struct device *dev = &client->dev; struct device *hwmon_dev; |
8c14d126a
|
189 190 |
struct lm73_data *data; int ctrl; |
3710263f1
|
191 |
data = devm_kzalloc(dev, sizeof(struct lm73_data), GFP_KERNEL); |
8c14d126a
|
192 193 |
if (!data) return -ENOMEM; |
3710263f1
|
194 |
data->client = client; |
8c14d126a
|
195 196 197 198 199 200 |
mutex_init(&data->lock); ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL); if (ctrl < 0) return ctrl; data->ctrl = ctrl; |
4e233cbed
|
201 |
|
3710263f1
|
202 203 204 205 |
hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, data, lm73_groups); if (IS_ERR(hwmon_dev)) return PTR_ERR(hwmon_dev); |
4e233cbed
|
206 |
|
3710263f1
|
207 208 |
dev_info(dev, "sensor '%s' ", client->name); |
4e233cbed
|
209 |
|
4e233cbed
|
210 211 212 213 |
return 0; } static const struct i2c_device_id lm73_ids[] = { |
1f86df49d
|
214 |
{ "lm73", 0 }, |
4e233cbed
|
215 216 217 218 219 |
{ /* LIST END */ } }; MODULE_DEVICE_TABLE(i2c, lm73_ids); /* Return 0 if detection is successful, -ENODEV otherwise */ |
310ec7921
|
220 |
static int lm73_detect(struct i2c_client *new_client, |
4e233cbed
|
221 222 223 |
struct i2c_board_info *info) { struct i2c_adapter *adapter = new_client->adapter; |
24d6e2a89
|
224 |
int id, ctrl, conf; |
4e233cbed
|
225 226 227 228 |
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA)) return -ENODEV; |
24d6e2a89
|
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
/* * Do as much detection as possible with byte reads first, as word * reads can confuse other devices. */ ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL); if (ctrl < 0 || (ctrl & 0x10)) return -ENODEV; conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF); if (conf < 0 || (conf & 0x0c)) return -ENODEV; id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID); if (id < 0 || id != (LM73_ID & 0xff)) return -ENODEV; |
4e233cbed
|
244 245 |
/* Check device ID */ id = i2c_smbus_read_word_data(new_client, LM73_REG_ID); |
24d6e2a89
|
246 |
if (id < 0 || id != LM73_ID) |
4e233cbed
|
247 248 249 250 251 252 253 254 255 256 257 258 259 |
return -ENODEV; strlcpy(info->type, "lm73", I2C_NAME_SIZE); return 0; } static struct i2c_driver lm73_driver = { .class = I2C_CLASS_HWMON, .driver = { .name = "lm73", }, .probe = lm73_probe, |
4e233cbed
|
260 261 |
.id_table = lm73_ids, .detect = lm73_detect, |
c3813d6af
|
262 |
.address_list = normal_i2c, |
4e233cbed
|
263 |
}; |
f0967eea8
|
264 |
module_i2c_driver(lm73_driver); |
4e233cbed
|
265 266 267 268 |
MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>"); MODULE_DESCRIPTION("LM73 driver"); MODULE_LICENSE("GPL"); |