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