]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/hwmon/sch5627.c
hwmon: (sch5627) Add sch5627_send_cmd function
[mirror_ubuntu-zesty-kernel.git] / drivers / hwmon / sch5627.c
CommitLineData
a98d506c
HG
1/***************************************************************************
2 * Copyright (C) 2010-2011 Hans de Goede <hdegoede@redhat.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/jiffies.h>
26#include <linux/platform_device.h>
27#include <linux/hwmon.h>
28#include <linux/hwmon-sysfs.h>
29#include <linux/err.h>
30#include <linux/mutex.h>
31#include <linux/io.h>
32#include <linux/acpi.h>
33#include <linux/delay.h>
34
35#define DRVNAME "sch5627"
36#define DEVNAME DRVNAME /* We only support one model */
37
38#define SIO_SCH5627_EM_LD 0x0C /* Embedded Microcontroller LD */
39#define SIO_UNLOCK_KEY 0x55 /* Key to enable Super-I/O */
40#define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */
41
42#define SIO_REG_LDSEL 0x07 /* Logical device select */
43#define SIO_REG_DEVID 0x20 /* Device ID */
44#define SIO_REG_ENABLE 0x30 /* Logical device enable */
45#define SIO_REG_ADDR 0x66 /* Logical device address (2 bytes) */
46
47#define SIO_SCH5627_ID 0xC6 /* Chipset ID */
48
49#define REGION_LENGTH 9
50
51#define SCH5627_HWMON_ID 0xa5
52#define SCH5627_COMPANY_ID 0x5c
53#define SCH5627_PRIMARY_ID 0xa0
54
709046a6
HG
55#define SCH5627_CMD_READ 0x02
56#define SCH5627_CMD_WRITE 0x03
57
a98d506c
HG
58#define SCH5627_REG_BUILD_CODE 0x39
59#define SCH5627_REG_BUILD_ID 0x3a
60#define SCH5627_REG_HWMON_ID 0x3c
61#define SCH5627_REG_HWMON_REV 0x3d
62#define SCH5627_REG_COMPANY_ID 0x3e
63#define SCH5627_REG_PRIMARY_ID 0x3f
64#define SCH5627_REG_CTRL 0x40
65
66#define SCH5627_NO_TEMPS 8
67#define SCH5627_NO_FANS 4
68#define SCH5627_NO_IN 5
69
70static const u16 SCH5627_REG_TEMP_MSB[SCH5627_NO_TEMPS] = {
71 0x2B, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x180, 0x181 };
72static const u16 SCH5627_REG_TEMP_LSN[SCH5627_NO_TEMPS] = {
73 0xE2, 0xE1, 0xE1, 0xE5, 0xE5, 0xE6, 0x182, 0x182 };
74static const u16 SCH5627_REG_TEMP_HIGH_NIBBLE[SCH5627_NO_TEMPS] = {
75 0, 0, 1, 1, 0, 0, 0, 1 };
76static const u16 SCH5627_REG_TEMP_HIGH[SCH5627_NO_TEMPS] = {
77 0x61, 0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x184, 0x186 };
78static const u16 SCH5627_REG_TEMP_ABS[SCH5627_NO_TEMPS] = {
79 0x9B, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x1A8, 0x1A9 };
80
81static const u16 SCH5627_REG_FAN[SCH5627_NO_FANS] = {
82 0x2C, 0x2E, 0x30, 0x32 };
83static const u16 SCH5627_REG_FAN_MIN[SCH5627_NO_FANS] = {
84 0x62, 0x64, 0x66, 0x68 };
85
86static const u16 SCH5627_REG_IN_MSB[SCH5627_NO_IN] = {
87 0x22, 0x23, 0x24, 0x25, 0x189 };
88static const u16 SCH5627_REG_IN_LSN[SCH5627_NO_IN] = {
89 0xE4, 0xE4, 0xE3, 0xE3, 0x18A };
90static const u16 SCH5627_REG_IN_HIGH_NIBBLE[SCH5627_NO_IN] = {
91 1, 0, 1, 0, 1 };
92static const u16 SCH5627_REG_IN_FACTOR[SCH5627_NO_IN] = {
93 10745, 3660, 9765, 10745, 3660 };
94static const char * const SCH5627_IN_LABELS[SCH5627_NO_IN] = {
95 "VCC", "VTT", "VBAT", "VTR", "V_IN" };
96
97struct sch5627_data {
98 unsigned short addr;
99 struct device *hwmon_dev;
100 u8 temp_max[SCH5627_NO_TEMPS];
101 u8 temp_crit[SCH5627_NO_TEMPS];
102 u16 fan_min[SCH5627_NO_FANS];
103
104 struct mutex update_lock;
105 char valid; /* !=0 if following fields are valid */
106 unsigned long last_updated; /* In jiffies */
107 u16 temp[SCH5627_NO_TEMPS];
108 u16 fan[SCH5627_NO_FANS];
109 u16 in[SCH5627_NO_IN];
110};
111
112static struct platform_device *sch5627_pdev;
113
114/* Super I/O functions */
115static inline int superio_inb(int base, int reg)
116{
117 outb(reg, base);
118 return inb(base + 1);
119}
120
121static inline int superio_enter(int base)
122{
123 /* Don't step on other drivers' I/O space by accident */
124 if (!request_muxed_region(base, 2, DRVNAME)) {
125 pr_err("I/O address 0x%04x already in use\n", base);
126 return -EBUSY;
127 }
128
129 outb(SIO_UNLOCK_KEY, base);
130
131 return 0;
132}
133
134static inline void superio_select(int base, int ld)
135{
136 outb(SIO_REG_LDSEL, base);
137 outb(ld, base + 1);
138}
139
140static inline void superio_exit(int base)
141{
142 outb(SIO_LOCK_KEY, base);
143 release_region(base, 2);
144}
145
709046a6 146static int sch5627_send_cmd(struct sch5627_data *data, u8 cmd, u16 reg, u8 v)
a98d506c
HG
147{
148 u8 val;
149 int i;
150 /*
151 * According to SMSC for the commands we use the maximum time for
152 * the EM to respond is 15 ms, but testing shows in practice it
153 * responds within 15-32 reads, so we first busy poll, and if
154 * that fails sleep a bit and try again until we are way past
155 * the 15 ms maximum response time.
156 */
157 const int max_busy_polls = 64;
158 const int max_lazy_polls = 32;
159
160 /* (Optional) Write-Clear the EC to Host Mailbox Register */
161 val = inb(data->addr + 1);
162 outb(val, data->addr + 1);
163
164 /* Set Mailbox Address Pointer to first location in Region 1 */
165 outb(0x00, data->addr + 2);
166 outb(0x80, data->addr + 3);
167
168 /* Write Request Packet Header */
709046a6 169 outb(cmd, data->addr + 4); /* VREG Access Type read:0x02 write:0x03 */
a98d506c
HG
170 outb(0x01, data->addr + 5); /* # of Entries: 1 Byte (8-bit) */
171 outb(0x04, data->addr + 2); /* Mailbox AP to first data entry loc. */
172
709046a6
HG
173 /* Write Value field */
174 if (cmd == SCH5627_CMD_WRITE)
175 outb(v, data->addr + 4);
176
a98d506c
HG
177 /* Write Address field */
178 outb(reg & 0xff, data->addr + 6);
179 outb(reg >> 8, data->addr + 7);
180
181 /* Execute the Random Access Command */
182 outb(0x01, data->addr); /* Write 01h to the Host-to-EC register */
183
184 /* EM Interface Polling "Algorithm" */
185 for (i = 0; i < max_busy_polls + max_lazy_polls; i++) {
186 if (i >= max_busy_polls)
187 msleep(1);
188 /* Read Interrupt source Register */
189 val = inb(data->addr + 8);
190 /* Write Clear the interrupt source bits */
191 if (val)
192 outb(val, data->addr + 8);
193 /* Command Completed ? */
194 if (val & 0x01)
195 break;
196 }
197 if (i == max_busy_polls + max_lazy_polls) {
198 pr_err("Max retries exceeded reading virtual "
199 "register 0x%04hx (%d)\n", reg, 1);
200 return -EIO;
201 }
202
203 /*
204 * According to SMSC we may need to retry this, but sofar I've always
205 * seen this succeed in 1 try.
206 */
207 for (i = 0; i < max_busy_polls; i++) {
208 /* Read EC-to-Host Register */
209 val = inb(data->addr + 1);
210 /* Command Completed ? */
211 if (val == 0x01)
212 break;
213
214 if (i == 0)
215 pr_warn("EC reports: 0x%02x reading virtual register "
216 "0x%04hx\n", (unsigned int)val, reg);
217 }
218 if (i == max_busy_polls) {
219 pr_err("Max retries exceeded reading virtual "
220 "register 0x%04hx (%d)\n", reg, 2);
221 return -EIO;
222 }
223
224 /*
225 * According to the SMSC app note we should now do:
226 *
227 * Set Mailbox Address Pointer to first location in Region 1 *
228 * outb(0x00, data->addr + 2);
229 * outb(0x80, data->addr + 3);
230 *
231 * But if we do that things don't work, so let's not.
232 */
233
709046a6
HG
234 /* Read Value field */
235 if (cmd == SCH5627_CMD_READ)
236 return inb(data->addr + 4);
237
238 return 0;
239}
240
241static int sch5627_read_virtual_reg(struct sch5627_data *data, u16 reg)
242{
243 return sch5627_send_cmd(data, SCH5627_CMD_READ, reg, 0);
a98d506c
HG
244}
245
246static int sch5627_read_virtual_reg16(struct sch5627_data *data, u16 reg)
247{
248 int lsb, msb;
249
250 /* Read LSB first, this will cause the matching MSB to be latched */
251 lsb = sch5627_read_virtual_reg(data, reg);
252 if (lsb < 0)
253 return lsb;
254
255 msb = sch5627_read_virtual_reg(data, reg + 1);
256 if (msb < 0)
257 return msb;
258
259 return lsb | (msb << 8);
260}
261
262static int sch5627_read_virtual_reg12(struct sch5627_data *data, u16 msb_reg,
263 u16 lsn_reg, int high_nibble)
264{
265 int msb, lsn;
266
267 /* Read MSB first, this will cause the matching LSN to be latched */
268 msb = sch5627_read_virtual_reg(data, msb_reg);
269 if (msb < 0)
270 return msb;
271
272 lsn = sch5627_read_virtual_reg(data, lsn_reg);
273 if (lsn < 0)
274 return lsn;
275
276 if (high_nibble)
277 return (msb << 4) | (lsn >> 4);
278 else
279 return (msb << 4) | (lsn & 0x0f);
280}
281
282static struct sch5627_data *sch5627_update_device(struct device *dev)
283{
284 struct sch5627_data *data = dev_get_drvdata(dev);
285 struct sch5627_data *ret = data;
286 int i, val;
287
288 mutex_lock(&data->update_lock);
289
290 /* Cache the values for 1 second */
291 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
292 for (i = 0; i < SCH5627_NO_TEMPS; i++) {
293 val = sch5627_read_virtual_reg12(data,
294 SCH5627_REG_TEMP_MSB[i],
295 SCH5627_REG_TEMP_LSN[i],
296 SCH5627_REG_TEMP_HIGH_NIBBLE[i]);
297 if (unlikely(val < 0)) {
298 ret = ERR_PTR(val);
299 goto abort;
300 }
301 data->temp[i] = val;
302 }
303
304 for (i = 0; i < SCH5627_NO_FANS; i++) {
305 val = sch5627_read_virtual_reg16(data,
306 SCH5627_REG_FAN[i]);
307 if (unlikely(val < 0)) {
308 ret = ERR_PTR(val);
309 goto abort;
310 }
311 data->fan[i] = val;
312 }
313
314 for (i = 0; i < SCH5627_NO_IN; i++) {
315 val = sch5627_read_virtual_reg12(data,
316 SCH5627_REG_IN_MSB[i],
317 SCH5627_REG_IN_LSN[i],
318 SCH5627_REG_IN_HIGH_NIBBLE[i]);
319 if (unlikely(val < 0)) {
320 ret = ERR_PTR(val);
321 goto abort;
322 }
323 data->in[i] = val;
324 }
325
326 data->last_updated = jiffies;
327 data->valid = 1;
328 }
329abort:
330 mutex_unlock(&data->update_lock);
331 return ret;
332}
333
334static int __devinit sch5627_read_limits(struct sch5627_data *data)
335{
336 int i, val;
337
338 for (i = 0; i < SCH5627_NO_TEMPS; i++) {
339 /*
340 * Note what SMSC calls ABS, is what lm_sensors calls max
341 * (aka high), and HIGH is what lm_sensors calls crit.
342 */
343 val = sch5627_read_virtual_reg(data, SCH5627_REG_TEMP_ABS[i]);
344 if (val < 0)
345 return val;
346 data->temp_max[i] = val;
347
348 val = sch5627_read_virtual_reg(data, SCH5627_REG_TEMP_HIGH[i]);
349 if (val < 0)
350 return val;
351 data->temp_crit[i] = val;
352 }
353 for (i = 0; i < SCH5627_NO_FANS; i++) {
354 val = sch5627_read_virtual_reg16(data, SCH5627_REG_FAN_MIN[i]);
355 if (val < 0)
356 return val;
357 data->fan_min[i] = val;
358 }
359
360 return 0;
361}
362
363static int reg_to_temp(u16 reg)
364{
365 return (reg * 625) / 10 - 64000;
366}
367
368static int reg_to_temp_limit(u8 reg)
369{
370 return (reg - 64) * 1000;
371}
372
373static int reg_to_rpm(u16 reg)
374{
375 if (reg == 0)
376 return -EIO;
377 if (reg == 0xffff)
378 return 0;
379
380 return 5400540 / reg;
381}
382
383static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
384 char *buf)
385{
386 return snprintf(buf, PAGE_SIZE, "%s\n", DEVNAME);
387}
388
389static ssize_t show_temp(struct device *dev, struct device_attribute
390 *devattr, char *buf)
391{
392 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
393 struct sch5627_data *data = sch5627_update_device(dev);
394 int val;
395
396 if (IS_ERR(data))
397 return PTR_ERR(data);
398
399 val = reg_to_temp(data->temp[attr->index]);
400 return snprintf(buf, PAGE_SIZE, "%d\n", val);
401}
402
403static ssize_t show_temp_fault(struct device *dev, struct device_attribute
404 *devattr, char *buf)
405{
406 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
407 struct sch5627_data *data = sch5627_update_device(dev);
408
409 if (IS_ERR(data))
410 return PTR_ERR(data);
411
412 return snprintf(buf, PAGE_SIZE, "%d\n", data->temp[attr->index] == 0);
413}
414
415static ssize_t show_temp_max(struct device *dev, struct device_attribute
416 *devattr, char *buf)
417{
418 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
419 struct sch5627_data *data = dev_get_drvdata(dev);
420 int val;
421
422 val = reg_to_temp_limit(data->temp_max[attr->index]);
423 return snprintf(buf, PAGE_SIZE, "%d\n", val);
424}
425
426static ssize_t show_temp_crit(struct device *dev, struct device_attribute
427 *devattr, char *buf)
428{
429 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
430 struct sch5627_data *data = dev_get_drvdata(dev);
431 int val;
432
433 val = reg_to_temp_limit(data->temp_crit[attr->index]);
434 return snprintf(buf, PAGE_SIZE, "%d\n", val);
435}
436
437static ssize_t show_fan(struct device *dev, struct device_attribute
438 *devattr, char *buf)
439{
440 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
441 struct sch5627_data *data = sch5627_update_device(dev);
442 int val;
443
444 if (IS_ERR(data))
445 return PTR_ERR(data);
446
447 val = reg_to_rpm(data->fan[attr->index]);
448 if (val < 0)
449 return val;
450
451 return snprintf(buf, PAGE_SIZE, "%d\n", val);
452}
453
454static ssize_t show_fan_fault(struct device *dev, struct device_attribute
455 *devattr, char *buf)
456{
457 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
458 struct sch5627_data *data = sch5627_update_device(dev);
459
460 if (IS_ERR(data))
461 return PTR_ERR(data);
462
463 return snprintf(buf, PAGE_SIZE, "%d\n",
464 data->fan[attr->index] == 0xffff);
465}
466
467static ssize_t show_fan_min(struct device *dev, struct device_attribute
468 *devattr, char *buf)
469{
470 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
471 struct sch5627_data *data = dev_get_drvdata(dev);
472 int val = reg_to_rpm(data->fan_min[attr->index]);
473 if (val < 0)
474 return val;
475
476 return snprintf(buf, PAGE_SIZE, "%d\n", val);
477}
478
479static ssize_t show_in(struct device *dev, struct device_attribute
480 *devattr, char *buf)
481{
482 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
483 struct sch5627_data *data = sch5627_update_device(dev);
484 int val;
485
486 if (IS_ERR(data))
487 return PTR_ERR(data);
488
489 val = DIV_ROUND_CLOSEST(
490 data->in[attr->index] * SCH5627_REG_IN_FACTOR[attr->index],
491 10000);
492 return snprintf(buf, PAGE_SIZE, "%d\n", val);
493}
494
495static ssize_t show_in_label(struct device *dev, struct device_attribute
496 *devattr, char *buf)
497{
498 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
499
500 return snprintf(buf, PAGE_SIZE, "%s\n",
501 SCH5627_IN_LABELS[attr->index]);
502}
503
504static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
505static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
506static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
507static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
508static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
509static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
510static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
511static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
512static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
513static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
514static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
515static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2);
516static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3);
517static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_temp_fault, NULL, 4);
518static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_temp_fault, NULL, 5);
519static SENSOR_DEVICE_ATTR(temp7_fault, S_IRUGO, show_temp_fault, NULL, 6);
520static SENSOR_DEVICE_ATTR(temp8_fault, S_IRUGO, show_temp_fault, NULL, 7);
521static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, NULL, 0);
522static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max, NULL, 1);
523static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max, NULL, 2);
524static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max, NULL, 3);
525static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max, NULL, 4);
526static SENSOR_DEVICE_ATTR(temp6_max, S_IRUGO, show_temp_max, NULL, 5);
527static SENSOR_DEVICE_ATTR(temp7_max, S_IRUGO, show_temp_max, NULL, 6);
528static SENSOR_DEVICE_ATTR(temp8_max, S_IRUGO, show_temp_max, NULL, 7);
529static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL, 0);
530static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit, NULL, 1);
531static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit, NULL, 2);
532static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp_crit, NULL, 3);
533static SENSOR_DEVICE_ATTR(temp5_crit, S_IRUGO, show_temp_crit, NULL, 4);
534static SENSOR_DEVICE_ATTR(temp6_crit, S_IRUGO, show_temp_crit, NULL, 5);
535static SENSOR_DEVICE_ATTR(temp7_crit, S_IRUGO, show_temp_crit, NULL, 6);
536static SENSOR_DEVICE_ATTR(temp8_crit, S_IRUGO, show_temp_crit, NULL, 7);
537
538static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
539static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
540static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
541static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
542static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL, 0);
543static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_fan_fault, NULL, 1);
544static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, show_fan_fault, NULL, 2);
545static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, show_fan_fault, NULL, 3);
546static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan_min, NULL, 0);
547static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan_min, NULL, 1);
548static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO, show_fan_min, NULL, 2);
549static SENSOR_DEVICE_ATTR(fan4_min, S_IRUGO, show_fan_min, NULL, 3);
550
551static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
552static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
553static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
554static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
555static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
556static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_in_label, NULL, 0);
557static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, show_in_label, NULL, 1);
558static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, show_in_label, NULL, 2);
559static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_in_label, NULL, 3);
560
561static struct attribute *sch5627_attributes[] = {
562 &dev_attr_name.attr,
563
564 &sensor_dev_attr_temp1_input.dev_attr.attr,
565 &sensor_dev_attr_temp2_input.dev_attr.attr,
566 &sensor_dev_attr_temp3_input.dev_attr.attr,
567 &sensor_dev_attr_temp4_input.dev_attr.attr,
568 &sensor_dev_attr_temp5_input.dev_attr.attr,
569 &sensor_dev_attr_temp6_input.dev_attr.attr,
570 &sensor_dev_attr_temp7_input.dev_attr.attr,
571 &sensor_dev_attr_temp8_input.dev_attr.attr,
572 &sensor_dev_attr_temp1_fault.dev_attr.attr,
573 &sensor_dev_attr_temp2_fault.dev_attr.attr,
574 &sensor_dev_attr_temp3_fault.dev_attr.attr,
575 &sensor_dev_attr_temp4_fault.dev_attr.attr,
576 &sensor_dev_attr_temp5_fault.dev_attr.attr,
577 &sensor_dev_attr_temp6_fault.dev_attr.attr,
578 &sensor_dev_attr_temp7_fault.dev_attr.attr,
579 &sensor_dev_attr_temp8_fault.dev_attr.attr,
580 &sensor_dev_attr_temp1_max.dev_attr.attr,
581 &sensor_dev_attr_temp2_max.dev_attr.attr,
582 &sensor_dev_attr_temp3_max.dev_attr.attr,
583 &sensor_dev_attr_temp4_max.dev_attr.attr,
584 &sensor_dev_attr_temp5_max.dev_attr.attr,
585 &sensor_dev_attr_temp6_max.dev_attr.attr,
586 &sensor_dev_attr_temp7_max.dev_attr.attr,
587 &sensor_dev_attr_temp8_max.dev_attr.attr,
588 &sensor_dev_attr_temp1_crit.dev_attr.attr,
589 &sensor_dev_attr_temp2_crit.dev_attr.attr,
590 &sensor_dev_attr_temp3_crit.dev_attr.attr,
591 &sensor_dev_attr_temp4_crit.dev_attr.attr,
592 &sensor_dev_attr_temp5_crit.dev_attr.attr,
593 &sensor_dev_attr_temp6_crit.dev_attr.attr,
594 &sensor_dev_attr_temp7_crit.dev_attr.attr,
595 &sensor_dev_attr_temp8_crit.dev_attr.attr,
596
597 &sensor_dev_attr_fan1_input.dev_attr.attr,
598 &sensor_dev_attr_fan2_input.dev_attr.attr,
599 &sensor_dev_attr_fan3_input.dev_attr.attr,
600 &sensor_dev_attr_fan4_input.dev_attr.attr,
601 &sensor_dev_attr_fan1_fault.dev_attr.attr,
602 &sensor_dev_attr_fan2_fault.dev_attr.attr,
603 &sensor_dev_attr_fan3_fault.dev_attr.attr,
604 &sensor_dev_attr_fan4_fault.dev_attr.attr,
605 &sensor_dev_attr_fan1_min.dev_attr.attr,
606 &sensor_dev_attr_fan2_min.dev_attr.attr,
607 &sensor_dev_attr_fan3_min.dev_attr.attr,
608 &sensor_dev_attr_fan4_min.dev_attr.attr,
609
610 &sensor_dev_attr_in0_input.dev_attr.attr,
611 &sensor_dev_attr_in1_input.dev_attr.attr,
612 &sensor_dev_attr_in2_input.dev_attr.attr,
613 &sensor_dev_attr_in3_input.dev_attr.attr,
614 &sensor_dev_attr_in4_input.dev_attr.attr,
615 &sensor_dev_attr_in0_label.dev_attr.attr,
616 &sensor_dev_attr_in1_label.dev_attr.attr,
617 &sensor_dev_attr_in2_label.dev_attr.attr,
618 &sensor_dev_attr_in3_label.dev_attr.attr,
619 /* No in4_label as in4 is a generic input pin */
620
621 NULL
622};
623
624static const struct attribute_group sch5627_group = {
625 .attrs = sch5627_attributes,
626};
627
628static int sch5627_remove(struct platform_device *pdev)
629{
630 struct sch5627_data *data = platform_get_drvdata(pdev);
631
632 if (data->hwmon_dev)
633 hwmon_device_unregister(data->hwmon_dev);
634
635 sysfs_remove_group(&pdev->dev.kobj, &sch5627_group);
636 platform_set_drvdata(pdev, NULL);
637 kfree(data);
638
639 return 0;
640}
641
642static int __devinit sch5627_probe(struct platform_device *pdev)
643{
644 struct sch5627_data *data;
645 int err, build_code, build_id, hwmon_rev, val;
646
647 data = kzalloc(sizeof(struct sch5627_data), GFP_KERNEL);
648 if (!data)
649 return -ENOMEM;
650
651 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
652 mutex_init(&data->update_lock);
653 platform_set_drvdata(pdev, data);
654
655 val = sch5627_read_virtual_reg(data, SCH5627_REG_HWMON_ID);
656 if (val < 0) {
657 err = val;
658 goto error;
659 }
660 if (val != SCH5627_HWMON_ID) {
661 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "hwmon",
662 val, SCH5627_HWMON_ID);
663 err = -ENODEV;
664 goto error;
665 }
666
667 val = sch5627_read_virtual_reg(data, SCH5627_REG_COMPANY_ID);
668 if (val < 0) {
669 err = val;
670 goto error;
671 }
672 if (val != SCH5627_COMPANY_ID) {
673 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "company",
674 val, SCH5627_COMPANY_ID);
675 err = -ENODEV;
676 goto error;
677 }
678
679 val = sch5627_read_virtual_reg(data, SCH5627_REG_PRIMARY_ID);
680 if (val < 0) {
681 err = val;
682 goto error;
683 }
684 if (val != SCH5627_PRIMARY_ID) {
685 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "primary",
686 val, SCH5627_PRIMARY_ID);
687 err = -ENODEV;
688 goto error;
689 }
690
691 build_code = sch5627_read_virtual_reg(data, SCH5627_REG_BUILD_CODE);
692 if (build_code < 0) {
693 err = build_code;
694 goto error;
695 }
696
697 build_id = sch5627_read_virtual_reg16(data, SCH5627_REG_BUILD_ID);
698 if (build_id < 0) {
699 err = build_id;
700 goto error;
701 }
702
703 hwmon_rev = sch5627_read_virtual_reg(data, SCH5627_REG_HWMON_REV);
704 if (hwmon_rev < 0) {
705 err = hwmon_rev;
706 goto error;
707 }
708
709 val = sch5627_read_virtual_reg(data, SCH5627_REG_CTRL);
710 if (val < 0) {
711 err = val;
712 goto error;
713 }
714 if (!(val & 0x01)) {
715 pr_err("hardware monitoring not enabled\n");
716 err = -ENODEV;
717 goto error;
718 }
719
720 /*
721 * Read limits, we do this only once as reading a register on
722 * the sch5627 is quite expensive (and they don't change).
723 */
724 err = sch5627_read_limits(data);
725 if (err)
726 goto error;
727
728 pr_info("firmware build: code 0x%02X, id 0x%04X, hwmon: rev 0x%02X\n",
729 build_code, build_id, hwmon_rev);
730
731 /* Register sysfs interface files */
732 err = sysfs_create_group(&pdev->dev.kobj, &sch5627_group);
733 if (err)
734 goto error;
735
736 data->hwmon_dev = hwmon_device_register(&pdev->dev);
737 if (IS_ERR(data->hwmon_dev)) {
738 err = PTR_ERR(data->hwmon_dev);
739 data->hwmon_dev = NULL;
740 goto error;
741 }
742
743 return 0;
744
745error:
746 sch5627_remove(pdev);
747 return err;
748}
749
750static int __init sch5627_find(int sioaddr, unsigned short *address)
751{
752 u8 devid;
753 int err = superio_enter(sioaddr);
754 if (err)
755 return err;
756
757 devid = superio_inb(sioaddr, SIO_REG_DEVID);
758 if (devid != SIO_SCH5627_ID) {
759 pr_debug("Unsupported device id: 0x%02x\n",
760 (unsigned int)devid);
761 err = -ENODEV;
762 goto exit;
763 }
764
765 superio_select(sioaddr, SIO_SCH5627_EM_LD);
766
767 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
768 pr_warn("Device not activated\n");
769 err = -ENODEV;
770 goto exit;
771 }
772
773 /*
774 * Warning the order of the low / high byte is the other way around
775 * as on most other superio devices!!
776 */
777 *address = superio_inb(sioaddr, SIO_REG_ADDR) |
778 superio_inb(sioaddr, SIO_REG_ADDR + 1) << 8;
779 if (*address == 0) {
780 pr_warn("Base address not set\n");
781 err = -ENODEV;
782 goto exit;
783 }
784
785 pr_info("Found %s chip at %#hx\n", DEVNAME, *address);
786exit:
787 superio_exit(sioaddr);
788 return err;
789}
790
791static int __init sch5627_device_add(unsigned short address)
792{
793 struct resource res = {
794 .start = address,
795 .end = address + REGION_LENGTH - 1,
796 .flags = IORESOURCE_IO,
797 };
798 int err;
799
800 sch5627_pdev = platform_device_alloc(DRVNAME, address);
801 if (!sch5627_pdev)
802 return -ENOMEM;
803
804 res.name = sch5627_pdev->name;
805 err = acpi_check_resource_conflict(&res);
806 if (err)
807 goto exit_device_put;
808
809 err = platform_device_add_resources(sch5627_pdev, &res, 1);
810 if (err) {
811 pr_err("Device resource addition failed\n");
812 goto exit_device_put;
813 }
814
815 err = platform_device_add(sch5627_pdev);
816 if (err) {
817 pr_err("Device addition failed\n");
818 goto exit_device_put;
819 }
820
821 return 0;
822
823exit_device_put:
824 platform_device_put(sch5627_pdev);
825
826 return err;
827}
828
829static struct platform_driver sch5627_driver = {
830 .driver = {
831 .owner = THIS_MODULE,
832 .name = DRVNAME,
833 },
834 .probe = sch5627_probe,
835 .remove = sch5627_remove,
836};
837
838static int __init sch5627_init(void)
839{
840 int err = -ENODEV;
841 unsigned short address;
842
843 if (sch5627_find(0x4e, &address) && sch5627_find(0x2e, &address))
844 goto exit;
845
846 err = platform_driver_register(&sch5627_driver);
847 if (err)
848 goto exit;
849
850 err = sch5627_device_add(address);
851 if (err)
852 goto exit_driver;
853
854 return 0;
855
856exit_driver:
857 platform_driver_unregister(&sch5627_driver);
858exit:
859 return err;
860}
861
862static void __exit sch5627_exit(void)
863{
864 platform_device_unregister(sch5627_pdev);
865 platform_driver_unregister(&sch5627_driver);
866}
867
868MODULE_DESCRIPTION("SMSC SCH5627 Hardware Monitoring Driver");
869MODULE_AUTHOR("Hans de Goede (hdegoede@redhat.com)");
870MODULE_LICENSE("GPL");
871
872module_init(sch5627_init);
873module_exit(sch5627_exit);