]>
Commit | Line | Data |
---|---|---|
dac6831e KT |
1 | /* |
2 | * emc1403.c - SMSC Thermal Driver | |
3 | * | |
4 | * Copyright (C) 2008 Intel Corp | |
5 | * | |
6 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; version 2 of the License. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along | |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
20 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
21 | * | |
22 | * TODO | |
23 | * - cache alarm and critical limit registers | |
dac6831e KT |
24 | */ |
25 | ||
26 | #include <linux/module.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/i2c.h> | |
30 | #include <linux/hwmon.h> | |
31 | #include <linux/hwmon-sysfs.h> | |
32 | #include <linux/err.h> | |
33 | #include <linux/sysfs.h> | |
34 | #include <linux/mutex.h> | |
dcd8f392 | 35 | #include <linux/jiffies.h> |
dac6831e KT |
36 | |
37 | #define THERMAL_PID_REG 0xfd | |
38 | #define THERMAL_SMSC_ID_REG 0xfe | |
39 | #define THERMAL_REVISION_REG 0xff | |
40 | ||
be7f5c4d JG |
41 | enum emc1403_chip { emc1402, emc1403, emc1404 }; |
42 | ||
dac6831e | 43 | struct thermal_data { |
454aee17 | 44 | struct i2c_client *client; |
be7f5c4d | 45 | const struct attribute_group *groups[4]; |
dac6831e | 46 | struct mutex mutex; |
4bebced8 GR |
47 | /* |
48 | * Cache the hyst value so we don't keep re-reading it. In theory | |
49 | * we could cache it forever as nobody else should be writing it. | |
50 | */ | |
dac6831e KT |
51 | u8 cached_hyst; |
52 | unsigned long hyst_valid; | |
53 | }; | |
54 | ||
55 | static ssize_t show_temp(struct device *dev, | |
56 | struct device_attribute *attr, char *buf) | |
57 | { | |
dac6831e | 58 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
454aee17 GR |
59 | struct thermal_data *data = dev_get_drvdata(dev); |
60 | int retval; | |
dac6831e | 61 | |
454aee17 | 62 | retval = i2c_smbus_read_byte_data(data->client, sda->index); |
dac6831e KT |
63 | if (retval < 0) |
64 | return retval; | |
65 | return sprintf(buf, "%d000\n", retval); | |
66 | } | |
67 | ||
68 | static ssize_t show_bit(struct device *dev, | |
69 | struct device_attribute *attr, char *buf) | |
70 | { | |
dac6831e | 71 | struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); |
454aee17 GR |
72 | struct thermal_data *data = dev_get_drvdata(dev); |
73 | int retval; | |
dac6831e | 74 | |
454aee17 | 75 | retval = i2c_smbus_read_byte_data(data->client, sda->nr); |
dac6831e KT |
76 | if (retval < 0) |
77 | return retval; | |
454aee17 | 78 | return sprintf(buf, "%d\n", !!(retval & sda->index)); |
dac6831e KT |
79 | } |
80 | ||
81 | static ssize_t store_temp(struct device *dev, | |
82 | struct device_attribute *attr, const char *buf, size_t count) | |
83 | { | |
84 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); | |
454aee17 | 85 | struct thermal_data *data = dev_get_drvdata(dev); |
dac6831e KT |
86 | unsigned long val; |
87 | int retval; | |
88 | ||
179c4fdb | 89 | if (kstrtoul(buf, 10, &val)) |
dac6831e | 90 | return -EINVAL; |
454aee17 | 91 | retval = i2c_smbus_write_byte_data(data->client, sda->index, |
dac6831e KT |
92 | DIV_ROUND_CLOSEST(val, 1000)); |
93 | if (retval < 0) | |
94 | return retval; | |
95 | return count; | |
96 | } | |
97 | ||
960f12f4 AC |
98 | static ssize_t store_bit(struct device *dev, |
99 | struct device_attribute *attr, const char *buf, size_t count) | |
100 | { | |
960f12f4 | 101 | struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); |
454aee17 GR |
102 | struct thermal_data *data = dev_get_drvdata(dev); |
103 | struct i2c_client *client = data->client; | |
960f12f4 AC |
104 | unsigned long val; |
105 | int retval; | |
106 | ||
179c4fdb | 107 | if (kstrtoul(buf, 10, &val)) |
960f12f4 AC |
108 | return -EINVAL; |
109 | ||
110 | mutex_lock(&data->mutex); | |
111 | retval = i2c_smbus_read_byte_data(client, sda->nr); | |
112 | if (retval < 0) | |
113 | goto fail; | |
114 | ||
115 | retval &= ~sda->index; | |
116 | if (val) | |
117 | retval |= sda->index; | |
118 | ||
119 | retval = i2c_smbus_write_byte_data(client, sda->index, retval); | |
120 | if (retval == 0) | |
121 | retval = count; | |
122 | fail: | |
123 | mutex_unlock(&data->mutex); | |
124 | return retval; | |
125 | } | |
126 | ||
dac6831e KT |
127 | static ssize_t show_hyst(struct device *dev, |
128 | struct device_attribute *attr, char *buf) | |
129 | { | |
dac6831e | 130 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
454aee17 GR |
131 | struct thermal_data *data = dev_get_drvdata(dev); |
132 | struct i2c_client *client = data->client; | |
dac6831e KT |
133 | int retval; |
134 | int hyst; | |
135 | ||
136 | retval = i2c_smbus_read_byte_data(client, sda->index); | |
137 | if (retval < 0) | |
138 | return retval; | |
139 | ||
140 | if (time_after(jiffies, data->hyst_valid)) { | |
141 | hyst = i2c_smbus_read_byte_data(client, 0x21); | |
142 | if (hyst < 0) | |
143 | return retval; | |
144 | data->cached_hyst = hyst; | |
145 | data->hyst_valid = jiffies + HZ; | |
146 | } | |
147 | return sprintf(buf, "%d000\n", retval - data->cached_hyst); | |
148 | } | |
149 | ||
150 | static ssize_t store_hyst(struct device *dev, | |
151 | struct device_attribute *attr, const char *buf, size_t count) | |
152 | { | |
dac6831e | 153 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
454aee17 GR |
154 | struct thermal_data *data = dev_get_drvdata(dev); |
155 | struct i2c_client *client = data->client; | |
dac6831e KT |
156 | int retval; |
157 | int hyst; | |
158 | unsigned long val; | |
159 | ||
179c4fdb | 160 | if (kstrtoul(buf, 10, &val)) |
dac6831e KT |
161 | return -EINVAL; |
162 | ||
163 | mutex_lock(&data->mutex); | |
164 | retval = i2c_smbus_read_byte_data(client, sda->index); | |
165 | if (retval < 0) | |
166 | goto fail; | |
167 | ||
17c048fc | 168 | hyst = retval * 1000 - val; |
dac6831e KT |
169 | hyst = DIV_ROUND_CLOSEST(hyst, 1000); |
170 | if (hyst < 0 || hyst > 255) { | |
171 | retval = -ERANGE; | |
172 | goto fail; | |
173 | } | |
174 | ||
175 | retval = i2c_smbus_write_byte_data(client, 0x21, hyst); | |
176 | if (retval == 0) { | |
177 | retval = count; | |
178 | data->cached_hyst = hyst; | |
179 | data->hyst_valid = jiffies + HZ; | |
180 | } | |
181 | fail: | |
182 | mutex_unlock(&data->mutex); | |
183 | return retval; | |
184 | } | |
185 | ||
186 | /* | |
187 | * Sensors. We pass the actual i2c register to the methods. | |
188 | */ | |
189 | ||
190 | static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, | |
191 | show_temp, store_temp, 0x06); | |
192 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, | |
193 | show_temp, store_temp, 0x05); | |
194 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR, | |
195 | show_temp, store_temp, 0x20); | |
196 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0x00); | |
197 | static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, | |
198 | show_bit, NULL, 0x36, 0x01); | |
199 | static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, | |
200 | show_bit, NULL, 0x35, 0x01); | |
201 | static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, | |
202 | show_bit, NULL, 0x37, 0x01); | |
203 | static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR, | |
204 | show_hyst, store_hyst, 0x20); | |
205 | ||
206 | static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, | |
207 | show_temp, store_temp, 0x08); | |
208 | static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, | |
209 | show_temp, store_temp, 0x07); | |
210 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR, | |
211 | show_temp, store_temp, 0x19); | |
212 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0x01); | |
213 | static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, | |
214 | show_bit, NULL, 0x36, 0x02); | |
215 | static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, | |
216 | show_bit, NULL, 0x35, 0x02); | |
217 | static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, | |
218 | show_bit, NULL, 0x37, 0x02); | |
219 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO | S_IWUSR, | |
220 | show_hyst, store_hyst, 0x19); | |
221 | ||
222 | static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR, | |
223 | show_temp, store_temp, 0x16); | |
224 | static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR, | |
225 | show_temp, store_temp, 0x15); | |
226 | static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO | S_IWUSR, | |
227 | show_temp, store_temp, 0x1A); | |
228 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 0x23); | |
229 | static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, | |
230 | show_bit, NULL, 0x36, 0x04); | |
231 | static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, | |
232 | show_bit, NULL, 0x35, 0x04); | |
233 | static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, | |
234 | show_bit, NULL, 0x37, 0x04); | |
235 | static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO | S_IWUSR, | |
236 | show_hyst, store_hyst, 0x1A); | |
237 | ||
0011ddfe GR |
238 | static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO | S_IWUSR, |
239 | show_temp, store_temp, 0x2D); | |
240 | static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO | S_IWUSR, | |
241 | show_temp, store_temp, 0x2C); | |
242 | static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO | S_IWUSR, | |
243 | show_temp, store_temp, 0x30); | |
244 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 0x2A); | |
245 | static SENSOR_DEVICE_ATTR_2(temp4_min_alarm, S_IRUGO, | |
246 | show_bit, NULL, 0x36, 0x08); | |
247 | static SENSOR_DEVICE_ATTR_2(temp4_max_alarm, S_IRUGO, | |
248 | show_bit, NULL, 0x35, 0x08); | |
249 | static SENSOR_DEVICE_ATTR_2(temp4_crit_alarm, S_IRUGO, | |
250 | show_bit, NULL, 0x37, 0x08); | |
251 | static SENSOR_DEVICE_ATTR(temp4_crit_hyst, S_IRUGO | S_IWUSR, | |
252 | show_hyst, store_hyst, 0x30); | |
253 | ||
960f12f4 AC |
254 | static SENSOR_DEVICE_ATTR_2(power_state, S_IRUGO | S_IWUSR, |
255 | show_bit, store_bit, 0x03, 0x40); | |
256 | ||
be7f5c4d | 257 | static struct attribute *emc1402_attrs[] = { |
dac6831e KT |
258 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
259 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
260 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | |
261 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
dac6831e | 262 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, |
be7f5c4d | 263 | |
dac6831e KT |
264 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
265 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
266 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | |
267 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
be7f5c4d JG |
268 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, |
269 | ||
270 | &sensor_dev_attr_power_state.dev_attr.attr, | |
271 | NULL | |
272 | }; | |
273 | ||
274 | static const struct attribute_group emc1402_group = { | |
275 | .attrs = emc1402_attrs, | |
276 | }; | |
277 | ||
278 | static struct attribute *emc1403_attrs[] = { | |
279 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, | |
280 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | |
281 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | |
282 | ||
dac6831e KT |
283 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
284 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | |
285 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | |
be7f5c4d | 286 | |
dac6831e KT |
287 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
288 | &sensor_dev_attr_temp3_max.dev_attr.attr, | |
289 | &sensor_dev_attr_temp3_crit.dev_attr.attr, | |
290 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
291 | &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, | |
292 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, | |
293 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, | |
294 | &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, | |
295 | NULL | |
296 | }; | |
0011ddfe GR |
297 | |
298 | static const struct attribute_group emc1403_group = { | |
299 | .attrs = emc1403_attrs, | |
300 | }; | |
301 | ||
302 | static struct attribute *emc1404_attrs[] = { | |
303 | &sensor_dev_attr_temp4_min.dev_attr.attr, | |
304 | &sensor_dev_attr_temp4_max.dev_attr.attr, | |
305 | &sensor_dev_attr_temp4_crit.dev_attr.attr, | |
306 | &sensor_dev_attr_temp4_input.dev_attr.attr, | |
307 | &sensor_dev_attr_temp4_min_alarm.dev_attr.attr, | |
308 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, | |
309 | &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, | |
310 | &sensor_dev_attr_temp4_crit_hyst.dev_attr.attr, | |
311 | NULL | |
312 | }; | |
313 | ||
314 | static const struct attribute_group emc1404_group = { | |
315 | .attrs = emc1404_attrs, | |
316 | }; | |
dac6831e KT |
317 | |
318 | static int emc1403_detect(struct i2c_client *client, | |
319 | struct i2c_board_info *info) | |
320 | { | |
321 | int id; | |
7a1b76f2 | 322 | /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */ |
dac6831e KT |
323 | |
324 | id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG); | |
325 | if (id != 0x5d) | |
326 | return -ENODEV; | |
327 | ||
7a1b76f2 JL |
328 | id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG); |
329 | switch (id) { | |
be7f5c4d JG |
330 | case 0x20: |
331 | strlcpy(info->type, "emc1402", I2C_NAME_SIZE); | |
332 | break; | |
7a1b76f2 JL |
333 | case 0x21: |
334 | strlcpy(info->type, "emc1403", I2C_NAME_SIZE); | |
335 | break; | |
be7f5c4d JG |
336 | case 0x22: |
337 | strlcpy(info->type, "emc1422", I2C_NAME_SIZE); | |
338 | break; | |
7a1b76f2 JL |
339 | case 0x23: |
340 | strlcpy(info->type, "emc1423", I2C_NAME_SIZE); | |
341 | break; | |
0011ddfe GR |
342 | case 0x25: |
343 | strlcpy(info->type, "emc1404", I2C_NAME_SIZE); | |
344 | break; | |
345 | case 0x27: | |
346 | strlcpy(info->type, "emc1424", I2C_NAME_SIZE); | |
347 | break; | |
7a1b76f2 | 348 | default: |
dac6831e | 349 | return -ENODEV; |
7a1b76f2 | 350 | } |
dac6831e KT |
351 | |
352 | id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG); | |
3a18e139 | 353 | if (id < 0x01 || id > 0x04) |
dac6831e KT |
354 | return -ENODEV; |
355 | ||
dac6831e KT |
356 | return 0; |
357 | } | |
358 | ||
359 | static int emc1403_probe(struct i2c_client *client, | |
360 | const struct i2c_device_id *id) | |
361 | { | |
dac6831e | 362 | struct thermal_data *data; |
454aee17 | 363 | struct device *hwmon_dev; |
dac6831e | 364 | |
7b52eefe GR |
365 | data = devm_kzalloc(&client->dev, sizeof(struct thermal_data), |
366 | GFP_KERNEL); | |
367 | if (data == NULL) | |
dac6831e | 368 | return -ENOMEM; |
dac6831e | 369 | |
454aee17 | 370 | data->client = client; |
dac6831e KT |
371 | mutex_init(&data->mutex); |
372 | data->hyst_valid = jiffies - 1; /* Expired */ | |
373 | ||
be7f5c4d JG |
374 | switch (id->driver_data) { |
375 | case emc1404: | |
376 | data->groups[2] = &emc1404_group; | |
377 | case emc1403: | |
378 | data->groups[1] = &emc1403_group; | |
379 | case emc1402: | |
380 | data->groups[0] = &emc1402_group; | |
381 | } | |
0011ddfe | 382 | |
8759f904 JD |
383 | hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev, |
384 | client->name, data, | |
385 | data->groups); | |
454aee17 GR |
386 | if (IS_ERR(hwmon_dev)) |
387 | return PTR_ERR(hwmon_dev); | |
dac6831e | 388 | |
0011ddfe | 389 | dev_info(&client->dev, "%s Thermal chip found\n", id->name); |
dac6831e KT |
390 | return 0; |
391 | } | |
392 | ||
393 | static const unsigned short emc1403_address_list[] = { | |
be7f5c4d | 394 | 0x18, 0x1c, 0x29, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END |
dac6831e KT |
395 | }; |
396 | ||
be7f5c4d | 397 | /* Last digit of chip name indicates number of channels */ |
dac6831e | 398 | static const struct i2c_device_id emc1403_idtable[] = { |
be7f5c4d JG |
399 | { "emc1402", emc1402 }, |
400 | { "emc1403", emc1403 }, | |
401 | { "emc1404", emc1404 }, | |
402 | { "emc1422", emc1402 }, | |
403 | { "emc1423", emc1403 }, | |
404 | { "emc1424", emc1404 }, | |
dac6831e KT |
405 | { } |
406 | }; | |
407 | MODULE_DEVICE_TABLE(i2c, emc1403_idtable); | |
408 | ||
409 | static struct i2c_driver sensor_emc1403 = { | |
410 | .class = I2C_CLASS_HWMON, | |
411 | .driver = { | |
412 | .name = "emc1403", | |
413 | }, | |
414 | .detect = emc1403_detect, | |
415 | .probe = emc1403_probe, | |
dac6831e KT |
416 | .id_table = emc1403_idtable, |
417 | .address_list = emc1403_address_list, | |
418 | }; | |
419 | ||
f0967eea | 420 | module_i2c_driver(sensor_emc1403); |
dac6831e KT |
421 | |
422 | MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com"); | |
423 | MODULE_DESCRIPTION("emc1403 Thermal Driver"); | |
424 | MODULE_LICENSE("GPL v2"); |