]>
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 | |
24 | * - add emc1404 support | |
25 | */ | |
26 | ||
27 | #include <linux/module.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/slab.h> | |
30 | #include <linux/i2c.h> | |
31 | #include <linux/hwmon.h> | |
32 | #include <linux/hwmon-sysfs.h> | |
33 | #include <linux/err.h> | |
34 | #include <linux/sysfs.h> | |
35 | #include <linux/mutex.h> | |
dcd8f392 | 36 | #include <linux/jiffies.h> |
dac6831e KT |
37 | |
38 | #define THERMAL_PID_REG 0xfd | |
39 | #define THERMAL_SMSC_ID_REG 0xfe | |
40 | #define THERMAL_REVISION_REG 0xff | |
41 | ||
42 | struct thermal_data { | |
454aee17 | 43 | struct i2c_client *client; |
dac6831e | 44 | struct mutex mutex; |
4bebced8 GR |
45 | /* |
46 | * Cache the hyst value so we don't keep re-reading it. In theory | |
47 | * we could cache it forever as nobody else should be writing it. | |
48 | */ | |
dac6831e KT |
49 | u8 cached_hyst; |
50 | unsigned long hyst_valid; | |
51 | }; | |
52 | ||
53 | static ssize_t show_temp(struct device *dev, | |
54 | struct device_attribute *attr, char *buf) | |
55 | { | |
dac6831e | 56 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
454aee17 GR |
57 | struct thermal_data *data = dev_get_drvdata(dev); |
58 | int retval; | |
dac6831e | 59 | |
454aee17 | 60 | retval = i2c_smbus_read_byte_data(data->client, sda->index); |
dac6831e KT |
61 | if (retval < 0) |
62 | return retval; | |
63 | return sprintf(buf, "%d000\n", retval); | |
64 | } | |
65 | ||
66 | static ssize_t show_bit(struct device *dev, | |
67 | struct device_attribute *attr, char *buf) | |
68 | { | |
dac6831e | 69 | struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); |
454aee17 GR |
70 | struct thermal_data *data = dev_get_drvdata(dev); |
71 | int retval; | |
dac6831e | 72 | |
454aee17 | 73 | retval = i2c_smbus_read_byte_data(data->client, sda->nr); |
dac6831e KT |
74 | if (retval < 0) |
75 | return retval; | |
454aee17 | 76 | return sprintf(buf, "%d\n", !!(retval & sda->index)); |
dac6831e KT |
77 | } |
78 | ||
79 | static ssize_t store_temp(struct device *dev, | |
80 | struct device_attribute *attr, const char *buf, size_t count) | |
81 | { | |
82 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); | |
454aee17 | 83 | struct thermal_data *data = dev_get_drvdata(dev); |
dac6831e KT |
84 | unsigned long val; |
85 | int retval; | |
86 | ||
179c4fdb | 87 | if (kstrtoul(buf, 10, &val)) |
dac6831e | 88 | return -EINVAL; |
454aee17 | 89 | retval = i2c_smbus_write_byte_data(data->client, sda->index, |
dac6831e KT |
90 | DIV_ROUND_CLOSEST(val, 1000)); |
91 | if (retval < 0) | |
92 | return retval; | |
93 | return count; | |
94 | } | |
95 | ||
960f12f4 AC |
96 | static ssize_t store_bit(struct device *dev, |
97 | struct device_attribute *attr, const char *buf, size_t count) | |
98 | { | |
960f12f4 | 99 | struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); |
454aee17 GR |
100 | struct thermal_data *data = dev_get_drvdata(dev); |
101 | struct i2c_client *client = data->client; | |
960f12f4 AC |
102 | unsigned long val; |
103 | int retval; | |
104 | ||
179c4fdb | 105 | if (kstrtoul(buf, 10, &val)) |
960f12f4 AC |
106 | return -EINVAL; |
107 | ||
108 | mutex_lock(&data->mutex); | |
109 | retval = i2c_smbus_read_byte_data(client, sda->nr); | |
110 | if (retval < 0) | |
111 | goto fail; | |
112 | ||
113 | retval &= ~sda->index; | |
114 | if (val) | |
115 | retval |= sda->index; | |
116 | ||
117 | retval = i2c_smbus_write_byte_data(client, sda->index, retval); | |
118 | if (retval == 0) | |
119 | retval = count; | |
120 | fail: | |
121 | mutex_unlock(&data->mutex); | |
122 | return retval; | |
123 | } | |
124 | ||
dac6831e KT |
125 | static ssize_t show_hyst(struct device *dev, |
126 | struct device_attribute *attr, char *buf) | |
127 | { | |
dac6831e | 128 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
454aee17 GR |
129 | struct thermal_data *data = dev_get_drvdata(dev); |
130 | struct i2c_client *client = data->client; | |
dac6831e KT |
131 | int retval; |
132 | int hyst; | |
133 | ||
134 | retval = i2c_smbus_read_byte_data(client, sda->index); | |
135 | if (retval < 0) | |
136 | return retval; | |
137 | ||
138 | if (time_after(jiffies, data->hyst_valid)) { | |
139 | hyst = i2c_smbus_read_byte_data(client, 0x21); | |
140 | if (hyst < 0) | |
141 | return retval; | |
142 | data->cached_hyst = hyst; | |
143 | data->hyst_valid = jiffies + HZ; | |
144 | } | |
145 | return sprintf(buf, "%d000\n", retval - data->cached_hyst); | |
146 | } | |
147 | ||
148 | static ssize_t store_hyst(struct device *dev, | |
149 | struct device_attribute *attr, const char *buf, size_t count) | |
150 | { | |
dac6831e | 151 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
454aee17 GR |
152 | struct thermal_data *data = dev_get_drvdata(dev); |
153 | struct i2c_client *client = data->client; | |
dac6831e KT |
154 | int retval; |
155 | int hyst; | |
156 | unsigned long val; | |
157 | ||
179c4fdb | 158 | if (kstrtoul(buf, 10, &val)) |
dac6831e KT |
159 | return -EINVAL; |
160 | ||
161 | mutex_lock(&data->mutex); | |
162 | retval = i2c_smbus_read_byte_data(client, sda->index); | |
163 | if (retval < 0) | |
164 | goto fail; | |
165 | ||
166 | hyst = val - retval * 1000; | |
167 | hyst = DIV_ROUND_CLOSEST(hyst, 1000); | |
168 | if (hyst < 0 || hyst > 255) { | |
169 | retval = -ERANGE; | |
170 | goto fail; | |
171 | } | |
172 | ||
173 | retval = i2c_smbus_write_byte_data(client, 0x21, hyst); | |
174 | if (retval == 0) { | |
175 | retval = count; | |
176 | data->cached_hyst = hyst; | |
177 | data->hyst_valid = jiffies + HZ; | |
178 | } | |
179 | fail: | |
180 | mutex_unlock(&data->mutex); | |
181 | return retval; | |
182 | } | |
183 | ||
184 | /* | |
185 | * Sensors. We pass the actual i2c register to the methods. | |
186 | */ | |
187 | ||
188 | static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, | |
189 | show_temp, store_temp, 0x06); | |
190 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, | |
191 | show_temp, store_temp, 0x05); | |
192 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR, | |
193 | show_temp, store_temp, 0x20); | |
194 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0x00); | |
195 | static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, | |
196 | show_bit, NULL, 0x36, 0x01); | |
197 | static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, | |
198 | show_bit, NULL, 0x35, 0x01); | |
199 | static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, | |
200 | show_bit, NULL, 0x37, 0x01); | |
201 | static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR, | |
202 | show_hyst, store_hyst, 0x20); | |
203 | ||
204 | static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, | |
205 | show_temp, store_temp, 0x08); | |
206 | static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, | |
207 | show_temp, store_temp, 0x07); | |
208 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR, | |
209 | show_temp, store_temp, 0x19); | |
210 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0x01); | |
211 | static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, | |
212 | show_bit, NULL, 0x36, 0x02); | |
213 | static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, | |
214 | show_bit, NULL, 0x35, 0x02); | |
215 | static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, | |
216 | show_bit, NULL, 0x37, 0x02); | |
217 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO | S_IWUSR, | |
218 | show_hyst, store_hyst, 0x19); | |
219 | ||
220 | static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR, | |
221 | show_temp, store_temp, 0x16); | |
222 | static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR, | |
223 | show_temp, store_temp, 0x15); | |
224 | static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO | S_IWUSR, | |
225 | show_temp, store_temp, 0x1A); | |
226 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 0x23); | |
227 | static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, | |
228 | show_bit, NULL, 0x36, 0x04); | |
229 | static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, | |
230 | show_bit, NULL, 0x35, 0x04); | |
231 | static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, | |
232 | show_bit, NULL, 0x37, 0x04); | |
233 | static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO | S_IWUSR, | |
234 | show_hyst, store_hyst, 0x1A); | |
235 | ||
960f12f4 AC |
236 | static SENSOR_DEVICE_ATTR_2(power_state, S_IRUGO | S_IWUSR, |
237 | show_bit, store_bit, 0x03, 0x40); | |
238 | ||
454aee17 | 239 | static struct attribute *emc1403_attrs[] = { |
dac6831e KT |
240 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
241 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
242 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | |
243 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
244 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, | |
245 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | |
246 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | |
247 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, | |
248 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
249 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
250 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | |
251 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
252 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, | |
253 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | |
254 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | |
255 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, | |
256 | &sensor_dev_attr_temp3_min.dev_attr.attr, | |
257 | &sensor_dev_attr_temp3_max.dev_attr.attr, | |
258 | &sensor_dev_attr_temp3_crit.dev_attr.attr, | |
259 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
260 | &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, | |
261 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, | |
262 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, | |
263 | &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, | |
960f12f4 | 264 | &sensor_dev_attr_power_state.dev_attr.attr, |
dac6831e KT |
265 | NULL |
266 | }; | |
454aee17 | 267 | ATTRIBUTE_GROUPS(emc1403); |
dac6831e KT |
268 | |
269 | static int emc1403_detect(struct i2c_client *client, | |
270 | struct i2c_board_info *info) | |
271 | { | |
272 | int id; | |
7a1b76f2 | 273 | /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */ |
dac6831e KT |
274 | |
275 | id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG); | |
276 | if (id != 0x5d) | |
277 | return -ENODEV; | |
278 | ||
7a1b76f2 JL |
279 | id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG); |
280 | switch (id) { | |
281 | case 0x21: | |
282 | strlcpy(info->type, "emc1403", I2C_NAME_SIZE); | |
283 | break; | |
284 | case 0x23: | |
285 | strlcpy(info->type, "emc1423", I2C_NAME_SIZE); | |
286 | break; | |
4bebced8 GR |
287 | /* |
288 | * Note: 0x25 is the 1404 which is very similar and this | |
289 | * driver could be extended | |
290 | */ | |
7a1b76f2 | 291 | default: |
dac6831e | 292 | return -ENODEV; |
7a1b76f2 | 293 | } |
dac6831e KT |
294 | |
295 | id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG); | |
296 | if (id != 0x01) | |
297 | return -ENODEV; | |
298 | ||
dac6831e KT |
299 | return 0; |
300 | } | |
301 | ||
302 | static int emc1403_probe(struct i2c_client *client, | |
303 | const struct i2c_device_id *id) | |
304 | { | |
dac6831e | 305 | struct thermal_data *data; |
454aee17 | 306 | struct device *hwmon_dev; |
dac6831e | 307 | |
7b52eefe GR |
308 | data = devm_kzalloc(&client->dev, sizeof(struct thermal_data), |
309 | GFP_KERNEL); | |
310 | if (data == NULL) | |
dac6831e | 311 | return -ENOMEM; |
dac6831e | 312 | |
454aee17 | 313 | data->client = client; |
dac6831e KT |
314 | mutex_init(&data->mutex); |
315 | data->hyst_valid = jiffies - 1; /* Expired */ | |
316 | ||
454aee17 GR |
317 | hwmon_dev = hwmon_device_register_with_groups(&client->dev, |
318 | client->name, data, | |
319 | emc1403_groups); | |
320 | if (IS_ERR(hwmon_dev)) | |
321 | return PTR_ERR(hwmon_dev); | |
dac6831e | 322 | |
454aee17 | 323 | dev_info(&client->dev, "EMC1403 Thermal chip found\n"); |
dac6831e KT |
324 | return 0; |
325 | } | |
326 | ||
327 | static const unsigned short emc1403_address_list[] = { | |
bcf721d1 | 328 | 0x18, 0x29, 0x4c, 0x4d, I2C_CLIENT_END |
dac6831e KT |
329 | }; |
330 | ||
331 | static const struct i2c_device_id emc1403_idtable[] = { | |
332 | { "emc1403", 0 }, | |
7a1b76f2 | 333 | { "emc1423", 0 }, |
dac6831e KT |
334 | { } |
335 | }; | |
336 | MODULE_DEVICE_TABLE(i2c, emc1403_idtable); | |
337 | ||
338 | static struct i2c_driver sensor_emc1403 = { | |
339 | .class = I2C_CLASS_HWMON, | |
340 | .driver = { | |
341 | .name = "emc1403", | |
342 | }, | |
343 | .detect = emc1403_detect, | |
344 | .probe = emc1403_probe, | |
dac6831e KT |
345 | .id_table = emc1403_idtable, |
346 | .address_list = emc1403_address_list, | |
347 | }; | |
348 | ||
f0967eea | 349 | module_i2c_driver(sensor_emc1403); |
dac6831e KT |
350 | |
351 | MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com"); | |
352 | MODULE_DESCRIPTION("emc1403 Thermal Driver"); | |
353 | MODULE_LICENSE("GPL v2"); |