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