1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/i2c.h>
21#include <linux/hwmon.h>
22#include <linux/hwmon-sysfs.h>
23#include <linux/err.h>
24
25
26
27static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c,
28 0x4d, 0x4e, I2C_CLIENT_END };
29
30
31#define LM73_REG_INPUT 0x00
32#define LM73_REG_CONF 0x01
33#define LM73_REG_MAX 0x02
34#define LM73_REG_MIN 0x03
35#define LM73_REG_CTRL 0x04
36#define LM73_REG_ID 0x07
37
38#define LM73_ID 0x9001
39#define DRVNAME "lm73"
40#define LM73_TEMP_MIN (-256000 / 250)
41#define LM73_TEMP_MAX (255750 / 250)
42
43#define LM73_CTRL_RES_SHIFT 5
44#define LM73_CTRL_RES_MASK (BIT(5) | BIT(6))
45#define LM73_CTRL_TO_MASK BIT(7)
46
47#define LM73_CTRL_HI_SHIFT 2
48#define LM73_CTRL_LO_SHIFT 1
49
50static const unsigned short lm73_convrates[] = {
51 14,
52 28,
53 56,
54 112,
55};
56
57struct lm73_data {
58 struct i2c_client *client;
59 struct mutex lock;
60 u8 ctrl;
61};
62
63
64
65static ssize_t set_temp(struct device *dev, struct device_attribute *da,
66 const char *buf, size_t count)
67{
68 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
69 struct lm73_data *data = dev_get_drvdata(dev);
70 long temp;
71 short value;
72 s32 err;
73
74 int status = kstrtol(buf, 10, &temp);
75 if (status < 0)
76 return status;
77
78
79 value = clamp_val(temp / 250, LM73_TEMP_MIN, LM73_TEMP_MAX) << 5;
80 err = i2c_smbus_write_word_swapped(data->client, attr->index, value);
81 return (err < 0) ? err : count;
82}
83
84static ssize_t show_temp(struct device *dev, struct device_attribute *da,
85 char *buf)
86{
87 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
88 struct lm73_data *data = dev_get_drvdata(dev);
89 int temp;
90
91 s32 err = i2c_smbus_read_word_swapped(data->client, attr->index);
92 if (err < 0)
93 return err;
94
95
96
97 temp = (((s16) err) * 250) / 32;
98 return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
99}
100
101static ssize_t set_convrate(struct device *dev, struct device_attribute *da,
102 const char *buf, size_t count)
103{
104 struct lm73_data *data = dev_get_drvdata(dev);
105 unsigned long convrate;
106 s32 err;
107 int res = 0;
108
109 err = kstrtoul(buf, 10, &convrate);
110 if (err < 0)
111 return err;
112
113
114
115
116
117
118
119 while (res < (ARRAY_SIZE(lm73_convrates) - 1) &&
120 convrate > lm73_convrates[res])
121 res++;
122
123 mutex_lock(&data->lock);
124 data->ctrl &= LM73_CTRL_TO_MASK;
125 data->ctrl |= res << LM73_CTRL_RES_SHIFT;
126 err = i2c_smbus_write_byte_data(data->client, LM73_REG_CTRL,
127 data->ctrl);
128 mutex_unlock(&data->lock);
129
130 if (err < 0)
131 return err;
132
133 return count;
134}
135
136static ssize_t show_convrate(struct device *dev, struct device_attribute *da,
137 char *buf)
138{
139 struct lm73_data *data = dev_get_drvdata(dev);
140 int res;
141
142 res = (data->ctrl & LM73_CTRL_RES_MASK) >> LM73_CTRL_RES_SHIFT;
143 return scnprintf(buf, PAGE_SIZE, "%hu\n", lm73_convrates[res]);
144}
145
146static ssize_t show_maxmin_alarm(struct device *dev,
147 struct device_attribute *da, char *buf)
148{
149 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
150 struct lm73_data *data = dev_get_drvdata(dev);
151 s32 ctrl;
152
153 mutex_lock(&data->lock);
154 ctrl = i2c_smbus_read_byte_data(data->client, LM73_REG_CTRL);
155 if (ctrl < 0)
156 goto abort;
157 data->ctrl = ctrl;
158 mutex_unlock(&data->lock);
159
160 return scnprintf(buf, PAGE_SIZE, "%d\n", (ctrl >> attr->index) & 1);
161
162abort:
163 mutex_unlock(&data->lock);
164 return ctrl;
165}
166
167
168
169
170
171static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
172 show_temp, set_temp, LM73_REG_MAX);
173static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
174 show_temp, set_temp, LM73_REG_MIN);
175static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
176 show_temp, NULL, LM73_REG_INPUT);
177static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO,
178 show_convrate, set_convrate, 0);
179static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
180 show_maxmin_alarm, NULL, LM73_CTRL_HI_SHIFT);
181static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO,
182 show_maxmin_alarm, NULL, LM73_CTRL_LO_SHIFT);
183
184static struct attribute *lm73_attrs[] = {
185 &sensor_dev_attr_temp1_input.dev_attr.attr,
186 &sensor_dev_attr_temp1_max.dev_attr.attr,
187 &sensor_dev_attr_temp1_min.dev_attr.attr,
188 &sensor_dev_attr_update_interval.dev_attr.attr,
189 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
190 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
191 NULL
192};
193ATTRIBUTE_GROUPS(lm73);
194
195
196
197
198
199static int
200lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
201{
202 struct device *dev = &client->dev;
203 struct device *hwmon_dev;
204 struct lm73_data *data;
205 int ctrl;
206
207 data = devm_kzalloc(dev, sizeof(struct lm73_data), GFP_KERNEL);
208 if (!data)
209 return -ENOMEM;
210
211 data->client = client;
212 mutex_init(&data->lock);
213
214 ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL);
215 if (ctrl < 0)
216 return ctrl;
217 data->ctrl = ctrl;
218
219 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
220 data, lm73_groups);
221 if (IS_ERR(hwmon_dev))
222 return PTR_ERR(hwmon_dev);
223
224 dev_info(dev, "sensor '%s'\n", client->name);
225
226 return 0;
227}
228
229static const struct i2c_device_id lm73_ids[] = {
230 { "lm73", 0 },
231 { }
232};
233MODULE_DEVICE_TABLE(i2c, lm73_ids);
234
235
236static int lm73_detect(struct i2c_client *new_client,
237 struct i2c_board_info *info)
238{
239 struct i2c_adapter *adapter = new_client->adapter;
240 int id, ctrl, conf;
241
242 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
243 I2C_FUNC_SMBUS_WORD_DATA))
244 return -ENODEV;
245
246
247
248
249
250 ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
251 if (ctrl < 0 || (ctrl & 0x10))
252 return -ENODEV;
253
254 conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF);
255 if (conf < 0 || (conf & 0x0c))
256 return -ENODEV;
257
258 id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID);
259 if (id < 0 || id != (LM73_ID & 0xff))
260 return -ENODEV;
261
262
263 id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
264 if (id < 0 || id != LM73_ID)
265 return -ENODEV;
266
267 strlcpy(info->type, "lm73", I2C_NAME_SIZE);
268
269 return 0;
270}
271
272static struct i2c_driver lm73_driver = {
273 .class = I2C_CLASS_HWMON,
274 .driver = {
275 .name = "lm73",
276 },
277 .probe = lm73_probe,
278 .id_table = lm73_ids,
279 .detect = lm73_detect,
280 .address_list = normal_i2c,
281};
282
283module_i2c_driver(lm73_driver);
284
285MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
286MODULE_DESCRIPTION("LM73 driver");
287MODULE_LICENSE("GPL");
288