Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * pc87360.c - Part of lm_sensors, Linux kernel modules
4 * for hardware monitoring
5 * Copyright (C) 2004, 2007 Jean Delvare <jdelvare@suse.de>
6 *
7 * Copied from smsc47m1.c:
8 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
9 *
10 * Supports the following chips:
11 *
12 * Chip #vin #fan #pwm #temp devid
13 * PC87360 - 2 2 - 0xE1
14 * PC87363 - 2 2 - 0xE8
15 * PC87364 - 3 3 - 0xE4
16 * PC87365 11 3 3 2 0xE5
17 * PC87366 11 3 3 3-4 0xE9
18 *
19 * This driver assumes that no more than one chip is present, and one of
20 * the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
21 */
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/jiffies.h>
29#include <linux/platform_device.h>
30#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
32#include <linux/hwmon-vid.h>
33#include <linux/err.h>
34#include <linux/mutex.h>
35#include <linux/acpi.h>
36#include <linux/io.h>
37
38static u8 devid;
39static struct platform_device *pdev;
40static unsigned short extra_isa[3];
41static u8 confreg[4];
42
43static int init = 1;
44module_param(init, int, 0);
45MODULE_PARM_DESC(init,
46"Chip initialization level:\n"
47" 0: None\n"
48"*1: Forcibly enable internal voltage and temperature channels, except in9\n"
49" 2: Forcibly enable all voltage and temperature channels, except in9\n"
50" 3: Forcibly enable all voltage and temperature channels, including in9");
51
52static unsigned short force_id;
53module_param(force_id, ushort, 0);
54MODULE_PARM_DESC(force_id, "Override the detected device ID");
55
56/*
57 * Super-I/O registers and operations
58 */
59
60#define DEV 0x07 /* Register: Logical device select */
61#define DEVID 0x20 /* Register: Device ID */
62#define ACT 0x30 /* Register: Device activation */
63#define BASE 0x60 /* Register: Base address */
64
65#define FSCM 0x09 /* Logical device: fans */
66#define VLM 0x0d /* Logical device: voltages */
67#define TMS 0x0e /* Logical device: temperatures */
68#define LDNI_MAX 3
69static const u8 logdev[LDNI_MAX] = { FSCM, VLM, TMS };
70
71#define LD_FAN 0
72#define LD_IN 1
73#define LD_TEMP 2
74
75static inline void superio_outb(int sioaddr, int reg, int val)
76{
77 outb(reg, sioaddr);
78 outb(val, sioaddr + 1);
79}
80
81static inline int superio_inb(int sioaddr, int reg)
82{
83 outb(reg, sioaddr);
84 return inb(sioaddr + 1);
85}
86
87static inline void superio_exit(int sioaddr)
88{
89 outb(0x02, sioaddr);
90 outb(0x02, sioaddr + 1);
91}
92
93/*
94 * Logical devices
95 */
96
97#define PC87360_EXTENT 0x10
98#define PC87365_REG_BANK 0x09
99#define NO_BANK 0xff
100
101/*
102 * Fan registers and conversions
103 */
104
105/* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
106#define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
107#define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
108#define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
109#define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
110#define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
111
112#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : \
113 480000 / ((val) * (div)))
114#define FAN_TO_REG(val, div) ((val) <= 100 ? 0 : \
115 480000 / ((val) * (div)))
116#define FAN_DIV_FROM_REG(val) (1 << (((val) >> 5) & 0x03))
117#define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
118
119#define FAN_CONFIG_MONITOR(val, nr) (((val) >> (2 + (nr) * 3)) & 1)
120#define FAN_CONFIG_CONTROL(val, nr) (((val) >> (3 + (nr) * 3)) & 1)
121#define FAN_CONFIG_INVERT(val, nr) (((val) >> (4 + (nr) * 3)) & 1)
122
123#define PWM_FROM_REG(val, inv) ((inv) ? 255 - (val) : (val))
124static inline u8 PWM_TO_REG(int val, int inv)
125{
126 if (inv)
127 val = 255 - val;
128 if (val < 0)
129 return 0;
130 if (val > 255)
131 return 255;
132 return val;
133}
134
135/*
136 * Voltage registers and conversions
137 */
138
139#define PC87365_REG_IN_CONVRATE 0x07
140#define PC87365_REG_IN_CONFIG 0x08
141#define PC87365_REG_IN 0x0B
142#define PC87365_REG_IN_MIN 0x0D
143#define PC87365_REG_IN_MAX 0x0C
144#define PC87365_REG_IN_STATUS 0x0A
145#define PC87365_REG_IN_ALARMS1 0x00
146#define PC87365_REG_IN_ALARMS2 0x01
147#define PC87365_REG_VID 0x06
148
149#define IN_FROM_REG(val, ref) (((val) * (ref) + 128) / 256)
150#define IN_TO_REG(val, ref) ((val) < 0 ? 0 : \
151 (val) * 256 >= (ref) * 255 ? 255 : \
152 ((val) * 256 + (ref) / 2) / (ref))
153
154/*
155 * Temperature registers and conversions
156 */
157
158#define PC87365_REG_TEMP_CONFIG 0x08
159#define PC87365_REG_TEMP 0x0B
160#define PC87365_REG_TEMP_MIN 0x0D
161#define PC87365_REG_TEMP_MAX 0x0C
162#define PC87365_REG_TEMP_CRIT 0x0E
163#define PC87365_REG_TEMP_STATUS 0x0A
164#define PC87365_REG_TEMP_ALARMS 0x00
165
166#define TEMP_FROM_REG(val) ((val) * 1000)
167#define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
168 (val) > 127000 ? 127 : \
169 (val) < 0 ? ((val) - 500) / 1000 : \
170 ((val) + 500) / 1000)
171
172/*
173 * Device data
174 */
175
176struct pc87360_data {
177 const char *name;
178 struct device *hwmon_dev;
179 struct mutex lock;
180 struct mutex update_lock;
181 char valid; /* !=0 if following fields are valid */
182 unsigned long last_updated; /* In jiffies */
183
184 int address[3];
185
186 u8 fannr, innr, tempnr;
187
188 u8 fan[3]; /* Register value */
189 u8 fan_min[3]; /* Register value */
190 u8 fan_status[3]; /* Register value */
191 u8 pwm[3]; /* Register value */
192 u16 fan_conf; /* Configuration register values, combined */
193
194 u16 in_vref; /* 1 mV/bit */
195 u8 in[14]; /* Register value */
196 u8 in_min[14]; /* Register value */
197 u8 in_max[14]; /* Register value */
198 u8 in_crit[3]; /* Register value */
199 u8 in_status[14]; /* Register value */
200 u16 in_alarms; /* Register values, combined, masked */
201 u8 vid_conf; /* Configuration register value */
202 u8 vrm;
203 u8 vid; /* Register value */
204
205 s8 temp[3]; /* Register value */
206 s8 temp_min[3]; /* Register value */
207 s8 temp_max[3]; /* Register value */
208 s8 temp_crit[3]; /* Register value */
209 u8 temp_status[3]; /* Register value */
210 u8 temp_alarms; /* Register value, masked */
211};
212
213/*
214 * Functions declaration
215 */
216
217static int pc87360_probe(struct platform_device *pdev);
218static int pc87360_remove(struct platform_device *pdev);
219
220static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
221 u8 reg);
222static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
223 u8 reg, u8 value);
224static void pc87360_init_device(struct platform_device *pdev,
225 int use_thermistors);
226static struct pc87360_data *pc87360_update_device(struct device *dev);
227
228/*
229 * Driver data
230 */
231
232static struct platform_driver pc87360_driver = {
233 .driver = {
234 .name = "pc87360",
235 },
236 .probe = pc87360_probe,
237 .remove = pc87360_remove,
238};
239
240/*
241 * Sysfs stuff
242 */
243
244static ssize_t fan_input_show(struct device *dev,
245 struct device_attribute *devattr, char *buf)
246{
247 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
248 struct pc87360_data *data = pc87360_update_device(dev);
249 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
250 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
251}
252static ssize_t fan_min_show(struct device *dev,
253 struct device_attribute *devattr, char *buf)
254{
255 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
256 struct pc87360_data *data = pc87360_update_device(dev);
257 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
258 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
259}
260static ssize_t fan_div_show(struct device *dev,
261 struct device_attribute *devattr, char *buf)
262{
263 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
264 struct pc87360_data *data = pc87360_update_device(dev);
265 return sprintf(buf, "%u\n",
266 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
267}
268static ssize_t fan_status_show(struct device *dev,
269 struct device_attribute *devattr, char *buf)
270{
271 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
272 struct pc87360_data *data = pc87360_update_device(dev);
273 return sprintf(buf, "%u\n",
274 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
275}
276static ssize_t fan_min_store(struct device *dev,
277 struct device_attribute *devattr,
278 const char *buf, size_t count)
279{
280 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
281 struct pc87360_data *data = dev_get_drvdata(dev);
282 long fan_min;
283 int err;
284
285 err = kstrtol(buf, 10, &fan_min);
286 if (err)
287 return err;
288
289 mutex_lock(&data->update_lock);
290 fan_min = FAN_TO_REG(fan_min,
291 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
292
293 /* If it wouldn't fit, change clock divisor */
294 while (fan_min > 255
295 && (data->fan_status[attr->index] & 0x60) != 0x60) {
296 fan_min >>= 1;
297 data->fan[attr->index] >>= 1;
298 data->fan_status[attr->index] += 0x20;
299 }
300 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
301 pc87360_write_value(data, LD_FAN, NO_BANK,
302 PC87360_REG_FAN_MIN(attr->index),
303 data->fan_min[attr->index]);
304
305 /* Write new divider, preserve alarm bits */
306 pc87360_write_value(data, LD_FAN, NO_BANK,
307 PC87360_REG_FAN_STATUS(attr->index),
308 data->fan_status[attr->index] & 0xF9);
309 mutex_unlock(&data->update_lock);
310
311 return count;
312}
313
314static struct sensor_device_attribute fan_input[] = {
315 SENSOR_ATTR_RO(fan1_input, fan_input, 0),
316 SENSOR_ATTR_RO(fan2_input, fan_input, 1),
317 SENSOR_ATTR_RO(fan3_input, fan_input, 2),
318};
319static struct sensor_device_attribute fan_status[] = {
320 SENSOR_ATTR_RO(fan1_status, fan_status, 0),
321 SENSOR_ATTR_RO(fan2_status, fan_status, 1),
322 SENSOR_ATTR_RO(fan3_status, fan_status, 2),
323};
324static struct sensor_device_attribute fan_div[] = {
325 SENSOR_ATTR_RO(fan1_div, fan_div, 0),
326 SENSOR_ATTR_RO(fan2_div, fan_div, 1),
327 SENSOR_ATTR_RO(fan3_div, fan_div, 2),
328};
329static struct sensor_device_attribute fan_min[] = {
330 SENSOR_ATTR_RW(fan1_min, fan_min, 0),
331 SENSOR_ATTR_RW(fan2_min, fan_min, 1),
332 SENSOR_ATTR_RW(fan3_min, fan_min, 2),
333};
334
335#define FAN_UNIT_ATTRS(X) \
336{ &fan_input[X].dev_attr.attr, \
337 &fan_status[X].dev_attr.attr, \
338 &fan_div[X].dev_attr.attr, \
339 &fan_min[X].dev_attr.attr, \
340 NULL \
341}
342
343static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
344 char *buf)
345{
346 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
347 struct pc87360_data *data = pc87360_update_device(dev);
348 return sprintf(buf, "%u\n",
349 PWM_FROM_REG(data->pwm[attr->index],
350 FAN_CONFIG_INVERT(data->fan_conf,
351 attr->index)));
352}
353static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
354 const char *buf, size_t count)
355{
356 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
357 struct pc87360_data *data = dev_get_drvdata(dev);
358 long val;
359 int err;
360
361 err = kstrtol(buf, 10, &val);
362 if (err)
363 return err;
364
365 mutex_lock(&data->update_lock);
366 data->pwm[attr->index] = PWM_TO_REG(val,
367 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
368 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
369 data->pwm[attr->index]);
370 mutex_unlock(&data->update_lock);
371 return count;
372}
373
374static struct sensor_device_attribute pwm[] = {
375 SENSOR_ATTR_RW(pwm1, pwm, 0),
376 SENSOR_ATTR_RW(pwm2, pwm, 1),
377 SENSOR_ATTR_RW(pwm3, pwm, 2),
378};
379
380static struct attribute *pc8736x_fan_attr[][5] = {
381 FAN_UNIT_ATTRS(0),
382 FAN_UNIT_ATTRS(1),
383 FAN_UNIT_ATTRS(2)
384};
385
386static const struct attribute_group pc8736x_fan_attr_group[] = {
387 { .attrs = pc8736x_fan_attr[0], },
388 { .attrs = pc8736x_fan_attr[1], },
389 { .attrs = pc8736x_fan_attr[2], },
390};
391
392static ssize_t in_input_show(struct device *dev,
393 struct device_attribute *devattr, char *buf)
394{
395 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
396 struct pc87360_data *data = pc87360_update_device(dev);
397 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
398 data->in_vref));
399}
400static ssize_t in_min_show(struct device *dev,
401 struct device_attribute *devattr, char *buf)
402{
403 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
404 struct pc87360_data *data = pc87360_update_device(dev);
405 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
406 data->in_vref));
407}
408static ssize_t in_max_show(struct device *dev,
409 struct device_attribute *devattr, char *buf)
410{
411 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
412 struct pc87360_data *data = pc87360_update_device(dev);
413 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
414 data->in_vref));
415}
416static ssize_t in_status_show(struct device *dev,
417 struct device_attribute *devattr, char *buf)
418{
419 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
420 struct pc87360_data *data = pc87360_update_device(dev);
421 return sprintf(buf, "%u\n", data->in_status[attr->index]);
422}
423static ssize_t in_min_store(struct device *dev,
424 struct device_attribute *devattr, const char *buf,
425 size_t count)
426{
427 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
428 struct pc87360_data *data = dev_get_drvdata(dev);
429 long val;
430 int err;
431
432 err = kstrtol(buf, 10, &val);
433 if (err)
434 return err;
435
436 mutex_lock(&data->update_lock);
437 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
438 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
439 data->in_min[attr->index]);
440 mutex_unlock(&data->update_lock);
441 return count;
442}
443static ssize_t in_max_store(struct device *dev,
444 struct device_attribute *devattr, const char *buf,
445 size_t count)
446{
447 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
448 struct pc87360_data *data = dev_get_drvdata(dev);
449 long val;
450 int err;
451
452 err = kstrtol(buf, 10, &val);
453 if (err)
454 return err;
455
456 mutex_lock(&data->update_lock);
457 data->in_max[attr->index] = IN_TO_REG(val,
458 data->in_vref);
459 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
460 data->in_max[attr->index]);
461 mutex_unlock(&data->update_lock);
462 return count;
463}
464
465static struct sensor_device_attribute in_input[] = {
466 SENSOR_ATTR_RO(in0_input, in_input, 0),
467 SENSOR_ATTR_RO(in1_input, in_input, 1),
468 SENSOR_ATTR_RO(in2_input, in_input, 2),
469 SENSOR_ATTR_RO(in3_input, in_input, 3),
470 SENSOR_ATTR_RO(in4_input, in_input, 4),
471 SENSOR_ATTR_RO(in5_input, in_input, 5),
472 SENSOR_ATTR_RO(in6_input, in_input, 6),
473 SENSOR_ATTR_RO(in7_input, in_input, 7),
474 SENSOR_ATTR_RO(in8_input, in_input, 8),
475 SENSOR_ATTR_RO(in9_input, in_input, 9),
476 SENSOR_ATTR_RO(in10_input, in_input, 10),
477};
478static struct sensor_device_attribute in_status[] = {
479 SENSOR_ATTR_RO(in0_status, in_status, 0),
480 SENSOR_ATTR_RO(in1_status, in_status, 1),
481 SENSOR_ATTR_RO(in2_status, in_status, 2),
482 SENSOR_ATTR_RO(in3_status, in_status, 3),
483 SENSOR_ATTR_RO(in4_status, in_status, 4),
484 SENSOR_ATTR_RO(in5_status, in_status, 5),
485 SENSOR_ATTR_RO(in6_status, in_status, 6),
486 SENSOR_ATTR_RO(in7_status, in_status, 7),
487 SENSOR_ATTR_RO(in8_status, in_status, 8),
488 SENSOR_ATTR_RO(in9_status, in_status, 9),
489 SENSOR_ATTR_RO(in10_status, in_status, 10),
490};
491static struct sensor_device_attribute in_min[] = {
492 SENSOR_ATTR_RW(in0_min, in_min, 0),
493 SENSOR_ATTR_RW(in1_min, in_min, 1),
494 SENSOR_ATTR_RW(in2_min, in_min, 2),
495 SENSOR_ATTR_RW(in3_min, in_min, 3),
496 SENSOR_ATTR_RW(in4_min, in_min, 4),
497 SENSOR_ATTR_RW(in5_min, in_min, 5),
498 SENSOR_ATTR_RW(in6_min, in_min, 6),
499 SENSOR_ATTR_RW(in7_min, in_min, 7),
500 SENSOR_ATTR_RW(in8_min, in_min, 8),
501 SENSOR_ATTR_RW(in9_min, in_min, 9),
502 SENSOR_ATTR_RW(in10_min, in_min, 10),
503};
504static struct sensor_device_attribute in_max[] = {
505 SENSOR_ATTR_RW(in0_max, in_max, 0),
506 SENSOR_ATTR_RW(in1_max, in_max, 1),
507 SENSOR_ATTR_RW(in2_max, in_max, 2),
508 SENSOR_ATTR_RW(in3_max, in_max, 3),
509 SENSOR_ATTR_RW(in4_max, in_max, 4),
510 SENSOR_ATTR_RW(in5_max, in_max, 5),
511 SENSOR_ATTR_RW(in6_max, in_max, 6),
512 SENSOR_ATTR_RW(in7_max, in_max, 7),
513 SENSOR_ATTR_RW(in8_max, in_max, 8),
514 SENSOR_ATTR_RW(in9_max, in_max, 9),
515 SENSOR_ATTR_RW(in10_max, in_max, 10),
516};
517
518/* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
519#define CHAN_ALM_MIN 0x02 /* min limit crossed */
520#define CHAN_ALM_MAX 0x04 /* max limit exceeded */
521#define TEMP_ALM_CRIT 0x08 /* temp crit exceeded (temp only) */
522
523/*
524 * show_in_min/max_alarm() reads data from the per-channel status
525 * register (sec 11.5.12), not the vin event status registers (sec
526 * 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms)
527 */
528
529static ssize_t in_min_alarm_show(struct device *dev,
530 struct device_attribute *devattr, char *buf)
531{
532 struct pc87360_data *data = pc87360_update_device(dev);
533 unsigned nr = to_sensor_dev_attr(devattr)->index;
534
535 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
536}
537static ssize_t in_max_alarm_show(struct device *dev,
538 struct device_attribute *devattr, char *buf)
539{
540 struct pc87360_data *data = pc87360_update_device(dev);
541 unsigned nr = to_sensor_dev_attr(devattr)->index;
542
543 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
544}
545
546static struct sensor_device_attribute in_min_alarm[] = {
547 SENSOR_ATTR_RO(in0_min_alarm, in_min_alarm, 0),
548 SENSOR_ATTR_RO(in1_min_alarm, in_min_alarm, 1),
549 SENSOR_ATTR_RO(in2_min_alarm, in_min_alarm, 2),
550 SENSOR_ATTR_RO(in3_min_alarm, in_min_alarm, 3),
551 SENSOR_ATTR_RO(in4_min_alarm, in_min_alarm, 4),
552 SENSOR_ATTR_RO(in5_min_alarm, in_min_alarm, 5),
553 SENSOR_ATTR_RO(in6_min_alarm, in_min_alarm, 6),
554 SENSOR_ATTR_RO(in7_min_alarm, in_min_alarm, 7),
555 SENSOR_ATTR_RO(in8_min_alarm, in_min_alarm, 8),
556 SENSOR_ATTR_RO(in9_min_alarm, in_min_alarm, 9),
557 SENSOR_ATTR_RO(in10_min_alarm, in_min_alarm, 10),
558};
559static struct sensor_device_attribute in_max_alarm[] = {
560 SENSOR_ATTR_RO(in0_max_alarm, in_max_alarm, 0),
561 SENSOR_ATTR_RO(in1_max_alarm, in_max_alarm, 1),
562 SENSOR_ATTR_RO(in2_max_alarm, in_max_alarm, 2),
563 SENSOR_ATTR_RO(in3_max_alarm, in_max_alarm, 3),
564 SENSOR_ATTR_RO(in4_max_alarm, in_max_alarm, 4),
565 SENSOR_ATTR_RO(in5_max_alarm, in_max_alarm, 5),
566 SENSOR_ATTR_RO(in6_max_alarm, in_max_alarm, 6),
567 SENSOR_ATTR_RO(in7_max_alarm, in_max_alarm, 7),
568 SENSOR_ATTR_RO(in8_max_alarm, in_max_alarm, 8),
569 SENSOR_ATTR_RO(in9_max_alarm, in_max_alarm, 9),
570 SENSOR_ATTR_RO(in10_max_alarm, in_max_alarm, 10),
571};
572
573#define VIN_UNIT_ATTRS(X) \
574 &in_input[X].dev_attr.attr, \
575 &in_status[X].dev_attr.attr, \
576 &in_min[X].dev_attr.attr, \
577 &in_max[X].dev_attr.attr, \
578 &in_min_alarm[X].dev_attr.attr, \
579 &in_max_alarm[X].dev_attr.attr
580
581static ssize_t cpu0_vid_show(struct device *dev,
582 struct device_attribute *attr, char *buf)
583{
584 struct pc87360_data *data = pc87360_update_device(dev);
585 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
586}
587static DEVICE_ATTR_RO(cpu0_vid);
588
589static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
590 char *buf)
591{
592 struct pc87360_data *data = dev_get_drvdata(dev);
593 return sprintf(buf, "%u\n", data->vrm);
594}
595static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
596 const char *buf, size_t count)
597{
598 struct pc87360_data *data = dev_get_drvdata(dev);
599 unsigned long val;
600 int err;
601
602 err = kstrtoul(buf, 10, &val);
603 if (err)
604 return err;
605
606 if (val > 255)
607 return -EINVAL;
608
609 data->vrm = val;
610 return count;
611}
612static DEVICE_ATTR_RW(vrm);
613
614static ssize_t alarms_in_show(struct device *dev,
615 struct device_attribute *attr, char *buf)
616{
617 struct pc87360_data *data = pc87360_update_device(dev);
618 return sprintf(buf, "%u\n", data->in_alarms);
619}
620static DEVICE_ATTR_RO(alarms_in);
621
622static struct attribute *pc8736x_vin_attr_array[] = {
623 VIN_UNIT_ATTRS(0),
624 VIN_UNIT_ATTRS(1),
625 VIN_UNIT_ATTRS(2),
626 VIN_UNIT_ATTRS(3),
627 VIN_UNIT_ATTRS(4),
628 VIN_UNIT_ATTRS(5),
629 VIN_UNIT_ATTRS(6),
630 VIN_UNIT_ATTRS(7),
631 VIN_UNIT_ATTRS(8),
632 VIN_UNIT_ATTRS(9),
633 VIN_UNIT_ATTRS(10),
634 &dev_attr_cpu0_vid.attr,
635 &dev_attr_vrm.attr,
636 &dev_attr_alarms_in.attr,
637 NULL
638};
639static const struct attribute_group pc8736x_vin_group = {
640 .attrs = pc8736x_vin_attr_array,
641};
642
643static ssize_t therm_input_show(struct device *dev,
644 struct device_attribute *devattr, char *buf)
645{
646 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
647 struct pc87360_data *data = pc87360_update_device(dev);
648 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
649 data->in_vref));
650}
651static ssize_t therm_min_show(struct device *dev,
652 struct device_attribute *devattr, char *buf)
653{
654 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
655 struct pc87360_data *data = pc87360_update_device(dev);
656 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
657 data->in_vref));
658}
659static ssize_t therm_max_show(struct device *dev,
660 struct device_attribute *devattr, char *buf)
661{
662 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
663 struct pc87360_data *data = pc87360_update_device(dev);
664 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
665 data->in_vref));
666}
667static ssize_t therm_crit_show(struct device *dev,
668 struct device_attribute *devattr, char *buf)
669{
670 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
671 struct pc87360_data *data = pc87360_update_device(dev);
672 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
673 data->in_vref));
674}
675static ssize_t therm_status_show(struct device *dev,
676 struct device_attribute *devattr, char *buf)
677{
678 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
679 struct pc87360_data *data = pc87360_update_device(dev);
680 return sprintf(buf, "%u\n", data->in_status[attr->index]);
681}
682
683static ssize_t therm_min_store(struct device *dev,
684 struct device_attribute *devattr,
685 const char *buf, size_t count)
686{
687 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
688 struct pc87360_data *data = dev_get_drvdata(dev);
689 long val;
690 int err;
691
692 err = kstrtol(buf, 10, &val);
693 if (err)
694 return err;
695
696 mutex_lock(&data->update_lock);
697 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
698 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
699 data->in_min[attr->index]);
700 mutex_unlock(&data->update_lock);
701 return count;
702}
703
704static ssize_t therm_max_store(struct device *dev,
705 struct device_attribute *devattr,
706 const char *buf, size_t count)
707{
708 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
709 struct pc87360_data *data = dev_get_drvdata(dev);
710 long val;
711 int err;
712
713 err = kstrtol(buf, 10, &val);
714 if (err)
715 return err;
716
717 mutex_lock(&data->update_lock);
718 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
719 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
720 data->in_max[attr->index]);
721 mutex_unlock(&data->update_lock);
722 return count;
723}
724static ssize_t therm_crit_store(struct device *dev,
725 struct device_attribute *devattr,
726 const char *buf, size_t count)
727{
728 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
729 struct pc87360_data *data = dev_get_drvdata(dev);
730 long val;
731 int err;
732
733 err = kstrtol(buf, 10, &val);
734 if (err)
735 return err;
736
737 mutex_lock(&data->update_lock);
738 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
739 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
740 data->in_crit[attr->index-11]);
741 mutex_unlock(&data->update_lock);
742 return count;
743}
744
745/*
746 * the +11 term below reflects the fact that VLM units 11,12,13 are
747 * used in the chip to measure voltage across the thermistors
748 */
749static struct sensor_device_attribute therm_input[] = {
750 SENSOR_ATTR_RO(temp4_input, therm_input, 0 + 11),
751 SENSOR_ATTR_RO(temp5_input, therm_input, 1 + 11),
752 SENSOR_ATTR_RO(temp6_input, therm_input, 2 + 11),
753};
754static struct sensor_device_attribute therm_status[] = {
755 SENSOR_ATTR_RO(temp4_status, therm_status, 0 + 11),
756 SENSOR_ATTR_RO(temp5_status, therm_status, 1 + 11),
757 SENSOR_ATTR_RO(temp6_status, therm_status, 2 + 11),
758};
759static struct sensor_device_attribute therm_min[] = {
760 SENSOR_ATTR_RW(temp4_min, therm_min, 0 + 11),
761 SENSOR_ATTR_RW(temp5_min, therm_min, 1 + 11),
762 SENSOR_ATTR_RW(temp6_min, therm_min, 2 + 11),
763};
764static struct sensor_device_attribute therm_max[] = {
765 SENSOR_ATTR_RW(temp4_max, therm_max, 0 + 11),
766 SENSOR_ATTR_RW(temp5_max, therm_max, 1 + 11),
767 SENSOR_ATTR_RW(temp6_max, therm_max, 2 + 11),
768};
769static struct sensor_device_attribute therm_crit[] = {
770 SENSOR_ATTR_RW(temp4_crit, therm_crit, 0 + 11),
771 SENSOR_ATTR_RW(temp5_crit, therm_crit, 1 + 11),
772 SENSOR_ATTR_RW(temp6_crit, therm_crit, 2 + 11),
773};
774
775/*
776 * show_therm_min/max_alarm() reads data from the per-channel voltage
777 * status register (sec 11.5.12)
778 */
779
780static ssize_t therm_min_alarm_show(struct device *dev,
781 struct device_attribute *devattr,
782 char *buf)
783{
784 struct pc87360_data *data = pc87360_update_device(dev);
785 unsigned nr = to_sensor_dev_attr(devattr)->index;
786
787 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
788}
789static ssize_t therm_max_alarm_show(struct device *dev,
790 struct device_attribute *devattr,
791 char *buf)
792{
793 struct pc87360_data *data = pc87360_update_device(dev);
794 unsigned nr = to_sensor_dev_attr(devattr)->index;
795
796 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
797}
798static ssize_t therm_crit_alarm_show(struct device *dev,
799 struct device_attribute *devattr,
800 char *buf)
801{
802 struct pc87360_data *data = pc87360_update_device(dev);
803 unsigned nr = to_sensor_dev_attr(devattr)->index;
804
805 return sprintf(buf, "%u\n", !!(data->in_status[nr] & TEMP_ALM_CRIT));
806}
807
808static struct sensor_device_attribute therm_min_alarm[] = {
809 SENSOR_ATTR_RO(temp4_min_alarm, therm_min_alarm, 0 + 11),
810 SENSOR_ATTR_RO(temp5_min_alarm, therm_min_alarm, 1 + 11),
811 SENSOR_ATTR_RO(temp6_min_alarm, therm_min_alarm, 2 + 11),
812};
813static struct sensor_device_attribute therm_max_alarm[] = {
814 SENSOR_ATTR_RO(temp4_max_alarm, therm_max_alarm, 0 + 11),
815 SENSOR_ATTR_RO(temp5_max_alarm, therm_max_alarm, 1 + 11),
816 SENSOR_ATTR_RO(temp6_max_alarm, therm_max_alarm, 2 + 11),
817};
818static struct sensor_device_attribute therm_crit_alarm[] = {
819 SENSOR_ATTR_RO(temp4_crit_alarm, therm_crit_alarm, 0 + 11),
820 SENSOR_ATTR_RO(temp5_crit_alarm, therm_crit_alarm, 1 + 11),
821 SENSOR_ATTR_RO(temp6_crit_alarm, therm_crit_alarm, 2 + 11),
822};
823
824#define THERM_UNIT_ATTRS(X) \
825 &therm_input[X].dev_attr.attr, \
826 &therm_status[X].dev_attr.attr, \
827 &therm_min[X].dev_attr.attr, \
828 &therm_max[X].dev_attr.attr, \
829 &therm_crit[X].dev_attr.attr, \
830 &therm_min_alarm[X].dev_attr.attr, \
831 &therm_max_alarm[X].dev_attr.attr, \
832 &therm_crit_alarm[X].dev_attr.attr
833
834static struct attribute *pc8736x_therm_attr_array[] = {
835 THERM_UNIT_ATTRS(0),
836 THERM_UNIT_ATTRS(1),
837 THERM_UNIT_ATTRS(2),
838 NULL
839};
840static const struct attribute_group pc8736x_therm_group = {
841 .attrs = pc8736x_therm_attr_array,
842};
843
844static ssize_t temp_input_show(struct device *dev,
845 struct device_attribute *devattr, char *buf)
846{
847 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
848 struct pc87360_data *data = pc87360_update_device(dev);
849 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
850}
851
852static ssize_t temp_min_show(struct device *dev,
853 struct device_attribute *devattr, char *buf)
854{
855 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
856 struct pc87360_data *data = pc87360_update_device(dev);
857 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
858}
859
860static ssize_t temp_max_show(struct device *dev,
861 struct device_attribute *devattr, char *buf)
862{
863 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
864 struct pc87360_data *data = pc87360_update_device(dev);
865 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
866}
867
868static ssize_t temp_crit_show(struct device *dev,
869 struct device_attribute *devattr, char *buf)
870{
871 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
872 struct pc87360_data *data = pc87360_update_device(dev);
873 return sprintf(buf, "%d\n",
874 TEMP_FROM_REG(data->temp_crit[attr->index]));
875}
876
877static ssize_t temp_status_show(struct device *dev,
878 struct device_attribute *devattr, char *buf)
879{
880 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
881 struct pc87360_data *data = pc87360_update_device(dev);
882 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
883}
884
885static ssize_t temp_min_store(struct device *dev,
886 struct device_attribute *devattr,
887 const char *buf, size_t count)
888{
889 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
890 struct pc87360_data *data = dev_get_drvdata(dev);
891 long val;
892 int err;
893
894 err = kstrtol(buf, 10, &val);
895 if (err)
896 return err;
897
898 mutex_lock(&data->update_lock);
899 data->temp_min[attr->index] = TEMP_TO_REG(val);
900 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
901 data->temp_min[attr->index]);
902 mutex_unlock(&data->update_lock);
903 return count;
904}
905
906static ssize_t temp_max_store(struct device *dev,
907 struct device_attribute *devattr,
908 const char *buf, size_t count)
909{
910 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
911 struct pc87360_data *data = dev_get_drvdata(dev);
912 long val;
913 int err;
914
915 err = kstrtol(buf, 10, &val);
916 if (err)
917 return err;
918
919 mutex_lock(&data->update_lock);
920 data->temp_max[attr->index] = TEMP_TO_REG(val);
921 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
922 data->temp_max[attr->index]);
923 mutex_unlock(&data->update_lock);
924 return count;
925}
926
927static ssize_t temp_crit_store(struct device *dev,
928 struct device_attribute *devattr,
929 const char *buf, size_t count)
930{
931 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
932 struct pc87360_data *data = dev_get_drvdata(dev);
933 long val;
934 int err;
935
936 err = kstrtol(buf, 10, &val);
937 if (err)
938 return err;
939
940 mutex_lock(&data->update_lock);
941 data->temp_crit[attr->index] = TEMP_TO_REG(val);
942 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
943 data->temp_crit[attr->index]);
944 mutex_unlock(&data->update_lock);
945 return count;
946}
947
948static struct sensor_device_attribute temp_input[] = {
949 SENSOR_ATTR_RO(temp1_input, temp_input, 0),
950 SENSOR_ATTR_RO(temp2_input, temp_input, 1),
951 SENSOR_ATTR_RO(temp3_input, temp_input, 2),
952};
953static struct sensor_device_attribute temp_status[] = {
954 SENSOR_ATTR_RO(temp1_status, temp_status, 0),
955 SENSOR_ATTR_RO(temp2_status, temp_status, 1),
956 SENSOR_ATTR_RO(temp3_status, temp_status, 2),
957};
958static struct sensor_device_attribute temp_min[] = {
959 SENSOR_ATTR_RW(temp1_min, temp_min, 0),
960 SENSOR_ATTR_RW(temp2_min, temp_min, 1),
961 SENSOR_ATTR_RW(temp3_min, temp_min, 2),
962};
963static struct sensor_device_attribute temp_max[] = {
964 SENSOR_ATTR_RW(temp1_max, temp_max, 0),
965 SENSOR_ATTR_RW(temp2_max, temp_max, 1),
966 SENSOR_ATTR_RW(temp3_max, temp_max, 2),
967};
968static struct sensor_device_attribute temp_crit[] = {
969 SENSOR_ATTR_RW(temp1_crit, temp_crit, 0),
970 SENSOR_ATTR_RW(temp2_crit, temp_crit, 1),
971 SENSOR_ATTR_RW(temp3_crit, temp_crit, 2),
972};
973
974static ssize_t alarms_temp_show(struct device *dev,
975 struct device_attribute *attr, char *buf)
976{
977 struct pc87360_data *data = pc87360_update_device(dev);
978 return sprintf(buf, "%u\n", data->temp_alarms);
979}
980
981static DEVICE_ATTR_RO(alarms_temp);
982
983/*
984 * show_temp_min/max_alarm() reads data from the per-channel status
985 * register (sec 12.3.7), not the temp event status registers (sec
986 * 12.3.2) that show_temp_alarm() reads (via data->temp_alarms)
987 */
988
989static ssize_t temp_min_alarm_show(struct device *dev,
990 struct device_attribute *devattr,
991 char *buf)
992{
993 struct pc87360_data *data = pc87360_update_device(dev);
994 unsigned nr = to_sensor_dev_attr(devattr)->index;
995
996 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN));
997}
998
999static ssize_t temp_max_alarm_show(struct device *dev,
1000 struct device_attribute *devattr,
1001 char *buf)
1002{
1003 struct pc87360_data *data = pc87360_update_device(dev);
1004 unsigned nr = to_sensor_dev_attr(devattr)->index;
1005
1006 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX));
1007}
1008
1009static ssize_t temp_crit_alarm_show(struct device *dev,
1010 struct device_attribute *devattr,
1011 char *buf)
1012{
1013 struct pc87360_data *data = pc87360_update_device(dev);
1014 unsigned nr = to_sensor_dev_attr(devattr)->index;
1015
1016 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT));
1017}
1018
1019static struct sensor_device_attribute temp_min_alarm[] = {
1020 SENSOR_ATTR_RO(temp1_min_alarm, temp_min_alarm, 0),
1021 SENSOR_ATTR_RO(temp2_min_alarm, temp_min_alarm, 1),
1022 SENSOR_ATTR_RO(temp3_min_alarm, temp_min_alarm, 2),
1023};
1024
1025static struct sensor_device_attribute temp_max_alarm[] = {
1026 SENSOR_ATTR_RO(temp1_max_alarm, temp_max_alarm, 0),
1027 SENSOR_ATTR_RO(temp2_max_alarm, temp_max_alarm, 1),
1028 SENSOR_ATTR_RO(temp3_max_alarm, temp_max_alarm, 2),
1029};
1030
1031static struct sensor_device_attribute temp_crit_alarm[] = {
1032 SENSOR_ATTR_RO(temp1_crit_alarm, temp_crit_alarm, 0),
1033 SENSOR_ATTR_RO(temp2_crit_alarm, temp_crit_alarm, 1),
1034 SENSOR_ATTR_RO(temp3_crit_alarm, temp_crit_alarm, 2),
1035};
1036
1037#define TEMP_FAULT 0x40 /* open diode */
1038static ssize_t temp_fault_show(struct device *dev,
1039 struct device_attribute *devattr, char *buf)
1040{
1041 struct pc87360_data *data = pc87360_update_device(dev);
1042 unsigned nr = to_sensor_dev_attr(devattr)->index;
1043
1044 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT));
1045}
1046static struct sensor_device_attribute temp_fault[] = {
1047 SENSOR_ATTR_RO(temp1_fault, temp_fault, 0),
1048 SENSOR_ATTR_RO(temp2_fault, temp_fault, 1),
1049 SENSOR_ATTR_RO(temp3_fault, temp_fault, 2),
1050};
1051
1052#define TEMP_UNIT_ATTRS(X) \
1053{ &temp_input[X].dev_attr.attr, \
1054 &temp_status[X].dev_attr.attr, \
1055 &temp_min[X].dev_attr.attr, \
1056 &temp_max[X].dev_attr.attr, \
1057 &temp_crit[X].dev_attr.attr, \
1058 &temp_min_alarm[X].dev_attr.attr, \
1059 &temp_max_alarm[X].dev_attr.attr, \
1060 &temp_crit_alarm[X].dev_attr.attr, \
1061 &temp_fault[X].dev_attr.attr, \
1062 NULL \
1063}
1064
1065static struct attribute *pc8736x_temp_attr[][10] = {
1066 TEMP_UNIT_ATTRS(0),
1067 TEMP_UNIT_ATTRS(1),
1068 TEMP_UNIT_ATTRS(2)
1069};
1070
1071static const struct attribute_group pc8736x_temp_attr_group[] = {
1072 { .attrs = pc8736x_temp_attr[0] },
1073 { .attrs = pc8736x_temp_attr[1] },
1074 { .attrs = pc8736x_temp_attr[2] }
1075};
1076
1077static ssize_t name_show(struct device *dev,
1078 struct device_attribute *devattr, char *buf)
1079{
1080 struct pc87360_data *data = dev_get_drvdata(dev);
1081 return sprintf(buf, "%s\n", data->name);
1082}
1083
1084static DEVICE_ATTR_RO(name);
1085
1086/*
1087 * Device detection, registration and update
1088 */
1089
1090static int __init pc87360_find(int sioaddr, u8 *devid,
1091 unsigned short *addresses)
1092{
1093 u16 val;
1094 int i;
1095 int nrdev; /* logical device count */
1096
1097 /* No superio_enter */
1098
1099 /* Identify device */
1100 val = force_id ? force_id : superio_inb(sioaddr, DEVID);
1101 switch (val) {
1102 case 0xE1: /* PC87360 */
1103 case 0xE8: /* PC87363 */
1104 case 0xE4: /* PC87364 */
1105 nrdev = 1;
1106 break;
1107 case 0xE5: /* PC87365 */
1108 case 0xE9: /* PC87366 */
1109 nrdev = 3;
1110 break;
1111 default:
1112 superio_exit(sioaddr);
1113 return -ENODEV;
1114 }
1115 /* Remember the device id */
1116 *devid = val;
1117
1118 for (i = 0; i < nrdev; i++) {
1119 /* select logical device */
1120 superio_outb(sioaddr, DEV, logdev[i]);
1121
1122 val = superio_inb(sioaddr, ACT);
1123 if (!(val & 0x01)) {
1124 pr_info("Device 0x%02x not activated\n", logdev[i]);
1125 continue;
1126 }
1127
1128 val = (superio_inb(sioaddr, BASE) << 8)
1129 | superio_inb(sioaddr, BASE + 1);
1130 if (!val) {
1131 pr_info("Base address not set for device 0x%02x\n",
1132 logdev[i]);
1133 continue;
1134 }
1135
1136 addresses[i] = val;
1137
1138 if (i == 0) { /* Fans */
1139 confreg[0] = superio_inb(sioaddr, 0xF0);
1140 confreg[1] = superio_inb(sioaddr, 0xF1);
1141
1142 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 1,
1143 (confreg[0] >> 2) & 1, (confreg[0] >> 3) & 1,
1144 (confreg[0] >> 4) & 1);
1145 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 2,
1146 (confreg[0] >> 5) & 1, (confreg[0] >> 6) & 1,
1147 (confreg[0] >> 7) & 1);
1148 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 3,
1149 confreg[1] & 1, (confreg[1] >> 1) & 1,
1150 (confreg[1] >> 2) & 1);
1151 } else if (i == 1) { /* Voltages */
1152 /* Are we using thermistors? */
1153 if (*devid == 0xE9) { /* PC87366 */
1154 /*
1155 * These registers are not logical-device
1156 * specific, just that we won't need them if
1157 * we don't use the VLM device
1158 */
1159 confreg[2] = superio_inb(sioaddr, 0x2B);
1160 confreg[3] = superio_inb(sioaddr, 0x25);
1161
1162 if (confreg[2] & 0x40) {
1163 pr_info("Using thermistors for temperature monitoring\n");
1164 }
1165 if (confreg[3] & 0xE0) {
1166 pr_info("VID inputs routed (mode %u)\n",
1167 confreg[3] >> 5);
1168 }
1169 }
1170 }
1171 }
1172
1173 superio_exit(sioaddr);
1174 return 0;
1175}
1176
1177static void pc87360_remove_files(struct device *dev)
1178{
1179 int i;
1180
1181 device_remove_file(dev, &dev_attr_name);
1182 device_remove_file(dev, &dev_attr_alarms_temp);
1183 for (i = 0; i < ARRAY_SIZE(pc8736x_temp_attr_group); i++)
1184 sysfs_remove_group(&dev->kobj, &pc8736x_temp_attr_group[i]);
1185 for (i = 0; i < ARRAY_SIZE(pc8736x_fan_attr_group); i++) {
1186 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_attr_group[i]);
1187 device_remove_file(dev, &pwm[i].dev_attr);
1188 }
1189 sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1190 sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
1191}
1192
1193static int pc87360_probe(struct platform_device *pdev)
1194{
1195 int i;
1196 struct pc87360_data *data;
1197 int err = 0;
1198 const char *name;
1199 int use_thermistors = 0;
1200 struct device *dev = &pdev->dev;
1201
1202 data = devm_kzalloc(dev, sizeof(struct pc87360_data), GFP_KERNEL);
1203 if (!data)
1204 return -ENOMEM;
1205
1206 switch (devid) {
1207 default:
1208 name = "pc87360";
1209 data->fannr = 2;
1210 break;
1211 case 0xe8:
1212 name = "pc87363";
1213 data->fannr = 2;
1214 break;
1215 case 0xe4:
1216 name = "pc87364";
1217 data->fannr = 3;
1218 break;
1219 case 0xe5:
1220 name = "pc87365";
1221 data->fannr = extra_isa[0] ? 3 : 0;
1222 data->innr = extra_isa[1] ? 11 : 0;
1223 data->tempnr = extra_isa[2] ? 2 : 0;
1224 break;
1225 case 0xe9:
1226 name = "pc87366";
1227 data->fannr = extra_isa[0] ? 3 : 0;
1228 data->innr = extra_isa[1] ? 14 : 0;
1229 data->tempnr = extra_isa[2] ? 3 : 0;
1230 break;
1231 }
1232
1233 data->name = name;
1234 mutex_init(&data->lock);
1235 mutex_init(&data->update_lock);
1236 platform_set_drvdata(pdev, data);
1237
1238 for (i = 0; i < LDNI_MAX; i++) {
1239 data->address[i] = extra_isa[i];
1240 if (data->address[i]
1241 && !devm_request_region(dev, extra_isa[i], PC87360_EXTENT,
1242 pc87360_driver.driver.name)) {
1243 dev_err(dev,
1244 "Region 0x%x-0x%x already in use!\n",
1245 extra_isa[i], extra_isa[i]+PC87360_EXTENT-1);
1246 return -EBUSY;
1247 }
1248 }
1249
1250 /* Retrieve the fans configuration from Super-I/O space */
1251 if (data->fannr)
1252 data->fan_conf = confreg[0] | (confreg[1] << 8);
1253
1254 /*
1255 * Use the correct reference voltage
1256 * Unless both the VLM and the TMS logical devices agree to
1257 * use an external Vref, the internal one is used.
1258 */
1259 if (data->innr) {
1260 i = pc87360_read_value(data, LD_IN, NO_BANK,
1261 PC87365_REG_IN_CONFIG);
1262 if (data->tempnr) {
1263 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
1264 PC87365_REG_TEMP_CONFIG);
1265 }
1266 data->in_vref = (i&0x02) ? 3025 : 2966;
1267 dev_dbg(dev, "Using %s reference voltage\n",
1268 (i&0x02) ? "external" : "internal");
1269
1270 data->vid_conf = confreg[3];
1271 data->vrm = vid_which_vrm();
1272 }
1273
1274 /* Fan clock dividers may be needed before any data is read */
1275 for (i = 0; i < data->fannr; i++) {
1276 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1277 data->fan_status[i] = pc87360_read_value(data,
1278 LD_FAN, NO_BANK,
1279 PC87360_REG_FAN_STATUS(i));
1280 }
1281
1282 if (init > 0) {
1283 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1284 use_thermistors = confreg[2] & 0x40;
1285
1286 pc87360_init_device(pdev, use_thermistors);
1287 }
1288
1289 /* Register all-or-nothing sysfs groups */
1290
1291 if (data->innr) {
1292 err = sysfs_create_group(&dev->kobj, &pc8736x_vin_group);
1293 if (err)
1294 goto error;
1295 }
1296
1297 if (data->innr == 14) {
1298 err = sysfs_create_group(&dev->kobj, &pc8736x_therm_group);
1299 if (err)
1300 goto error;
1301 }
1302
1303 /* create device attr-files for varying sysfs groups */
1304
1305 if (data->tempnr) {
1306 for (i = 0; i < data->tempnr; i++) {
1307 err = sysfs_create_group(&dev->kobj,
1308 &pc8736x_temp_attr_group[i]);
1309 if (err)
1310 goto error;
1311 }
1312 err = device_create_file(dev, &dev_attr_alarms_temp);
1313 if (err)
1314 goto error;
1315 }
1316
1317 for (i = 0; i < data->fannr; i++) {
1318 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1319 err = sysfs_create_group(&dev->kobj,
1320 &pc8736x_fan_attr_group[i]);
1321 if (err)
1322 goto error;
1323 }
1324 if (FAN_CONFIG_CONTROL(data->fan_conf, i)) {
1325 err = device_create_file(dev, &pwm[i].dev_attr);
1326 if (err)
1327 goto error;
1328 }
1329 }
1330
1331 err = device_create_file(dev, &dev_attr_name);
1332 if (err)
1333 goto error;
1334
1335 data->hwmon_dev = hwmon_device_register(dev);
1336 if (IS_ERR(data->hwmon_dev)) {
1337 err = PTR_ERR(data->hwmon_dev);
1338 goto error;
1339 }
1340 return 0;
1341
1342error:
1343 pc87360_remove_files(dev);
1344 return err;
1345}
1346
1347static int pc87360_remove(struct platform_device *pdev)
1348{
1349 struct pc87360_data *data = platform_get_drvdata(pdev);
1350
1351 hwmon_device_unregister(data->hwmon_dev);
1352 pc87360_remove_files(&pdev->dev);
1353
1354 return 0;
1355}
1356
1357/*
1358 * ldi is the logical device index
1359 * bank is for voltages and temperatures only
1360 */
1361static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1362 u8 reg)
1363{
1364 int res;
1365
1366 mutex_lock(&(data->lock));
1367 if (bank != NO_BANK)
1368 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1369 res = inb_p(data->address[ldi] + reg);
1370 mutex_unlock(&(data->lock));
1371
1372 return res;
1373}
1374
1375static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1376 u8 reg, u8 value)
1377{
1378 mutex_lock(&(data->lock));
1379 if (bank != NO_BANK)
1380 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1381 outb_p(value, data->address[ldi] + reg);
1382 mutex_unlock(&(data->lock));
1383}
1384
1385/* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
1386#define CHAN_CNVRTD 0x80 /* new data ready */
1387#define CHAN_ENA 0x01 /* enabled channel (temp or vin) */
1388#define CHAN_ALM_ENA 0x10 /* propagate to alarms-reg ?? (chk val!) */
1389#define CHAN_READY (CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
1390
1391#define TEMP_OTS_OE 0x20 /* OTS Output Enable */
1392#define VIN_RW1C_MASK (CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN) /* 0x87 */
1393#define TEMP_RW1C_MASK (VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */
1394
1395static void pc87360_init_device(struct platform_device *pdev,
1396 int use_thermistors)
1397{
1398 struct pc87360_data *data = platform_get_drvdata(pdev);
1399 int i, nr;
1400 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1401 const u8 init_temp[3] = { 2, 2, 1 };
1402 u8 reg;
1403
1404 if (init >= 2 && data->innr) {
1405 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1406 PC87365_REG_IN_CONVRATE);
1407 dev_info(&pdev->dev,
1408 "VLM conversion set to 1s period, 160us delay\n");
1409 pc87360_write_value(data, LD_IN, NO_BANK,
1410 PC87365_REG_IN_CONVRATE,
1411 (reg & 0xC0) | 0x11);
1412 }
1413
1414 nr = data->innr < 11 ? data->innr : 11;
1415 for (i = 0; i < nr; i++) {
1416 reg = pc87360_read_value(data, LD_IN, i,
1417 PC87365_REG_IN_STATUS);
1418 dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg);
1419 if (init >= init_in[i]) {
1420 /* Forcibly enable voltage channel */
1421 if (!(reg & CHAN_ENA)) {
1422 dev_dbg(&pdev->dev, "Forcibly enabling in%d\n",
1423 i);
1424 pc87360_write_value(data, LD_IN, i,
1425 PC87365_REG_IN_STATUS,
1426 (reg & 0x68) | 0x87);
1427 }
1428 }
1429 }
1430
1431 /*
1432 * We can't blindly trust the Super-I/O space configuration bit,
1433 * most BIOS won't set it properly
1434 */
1435 dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors);
1436 for (i = 11; i < data->innr; i++) {
1437 reg = pc87360_read_value(data, LD_IN, i,
1438 PC87365_REG_TEMP_STATUS);
1439 use_thermistors = use_thermistors || (reg & CHAN_ENA);
1440 /* thermistors are temp[4-6], measured on vin[11-14] */
1441 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg);
1442 }
1443 dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors);
1444
1445 i = use_thermistors ? 2 : 0;
1446 for (; i < data->tempnr; i++) {
1447 reg = pc87360_read_value(data, LD_TEMP, i,
1448 PC87365_REG_TEMP_STATUS);
1449 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i + 1, reg);
1450 if (init >= init_temp[i]) {
1451 /* Forcibly enable temperature channel */
1452 if (!(reg & CHAN_ENA)) {
1453 dev_dbg(&pdev->dev,
1454 "Forcibly enabling temp%d\n", i + 1);
1455 pc87360_write_value(data, LD_TEMP, i,
1456 PC87365_REG_TEMP_STATUS,
1457 0xCF);
1458 }
1459 }
1460 }
1461
1462 if (use_thermistors) {
1463 for (i = 11; i < data->innr; i++) {
1464 if (init >= init_in[i]) {
1465 /*
1466 * The pin may already be used by thermal
1467 * diodes
1468 */
1469 reg = pc87360_read_value(data, LD_TEMP,
1470 (i - 11) / 2, PC87365_REG_TEMP_STATUS);
1471 if (reg & CHAN_ENA) {
1472 dev_dbg(&pdev->dev,
1473 "Skipping temp%d, pin already in use by temp%d\n",
1474 i - 7, (i - 11) / 2);
1475 continue;
1476 }
1477
1478 /* Forcibly enable thermistor channel */
1479 reg = pc87360_read_value(data, LD_IN, i,
1480 PC87365_REG_IN_STATUS);
1481 if (!(reg & CHAN_ENA)) {
1482 dev_dbg(&pdev->dev,
1483 "Forcibly enabling temp%d\n",
1484 i - 7);
1485 pc87360_write_value(data, LD_IN, i,
1486 PC87365_REG_TEMP_STATUS,
1487 (reg & 0x60) | 0x8F);
1488 }
1489 }
1490 }
1491 }
1492
1493 if (data->innr) {
1494 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1495 PC87365_REG_IN_CONFIG);
1496 dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg);
1497 if (reg & CHAN_ENA) {
1498 dev_dbg(&pdev->dev,
1499 "Forcibly enabling monitoring (VLM)\n");
1500 pc87360_write_value(data, LD_IN, NO_BANK,
1501 PC87365_REG_IN_CONFIG,
1502 reg & 0xFE);
1503 }
1504 }
1505
1506 if (data->tempnr) {
1507 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1508 PC87365_REG_TEMP_CONFIG);
1509 dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg);
1510 if (reg & CHAN_ENA) {
1511 dev_dbg(&pdev->dev,
1512 "Forcibly enabling monitoring (TMS)\n");
1513 pc87360_write_value(data, LD_TEMP, NO_BANK,
1514 PC87365_REG_TEMP_CONFIG,
1515 reg & 0xFE);
1516 }
1517
1518 if (init >= 2) {
1519 /* Chip config as documented by National Semi. */
1520 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1521 /*
1522 * We voluntarily omit the bank here, in case the
1523 * sequence itself matters. It shouldn't be a problem,
1524 * since nobody else is supposed to access the
1525 * device at that point.
1526 */
1527 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1528 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1529 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1530 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1531 }
1532 }
1533}
1534
1535static void pc87360_autodiv(struct device *dev, int nr)
1536{
1537 struct pc87360_data *data = dev_get_drvdata(dev);
1538 u8 old_min = data->fan_min[nr];
1539
1540 /* Increase clock divider if needed and possible */
1541 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1542 || (data->fan[nr] >= 224)) { /* next to overflow */
1543 if ((data->fan_status[nr] & 0x60) != 0x60) {
1544 data->fan_status[nr] += 0x20;
1545 data->fan_min[nr] >>= 1;
1546 data->fan[nr] >>= 1;
1547 dev_dbg(dev,
1548 "Increasing clock divider to %d for fan %d\n",
1549 FAN_DIV_FROM_REG(data->fan_status[nr]), nr + 1);
1550 }
1551 } else {
1552 /* Decrease clock divider if possible */
1553 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1554 && data->fan[nr] < 85 /* bad accuracy */
1555 && (data->fan_status[nr] & 0x60) != 0x00) {
1556 data->fan_status[nr] -= 0x20;
1557 data->fan_min[nr] <<= 1;
1558 data->fan[nr] <<= 1;
1559 dev_dbg(dev,
1560 "Decreasing clock divider to %d for fan %d\n",
1561 FAN_DIV_FROM_REG(data->fan_status[nr]),
1562 nr + 1);
1563 }
1564 }
1565
1566 /* Write new fan min if it changed */
1567 if (old_min != data->fan_min[nr]) {
1568 pc87360_write_value(data, LD_FAN, NO_BANK,
1569 PC87360_REG_FAN_MIN(nr),
1570 data->fan_min[nr]);
1571 }
1572}
1573
1574static struct pc87360_data *pc87360_update_device(struct device *dev)
1575{
1576 struct pc87360_data *data = dev_get_drvdata(dev);
1577 u8 i;
1578
1579 mutex_lock(&data->update_lock);
1580
1581 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1582 dev_dbg(dev, "Data update\n");
1583
1584 /* Fans */
1585 for (i = 0; i < data->fannr; i++) {
1586 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1587 data->fan_status[i] =
1588 pc87360_read_value(data, LD_FAN,
1589 NO_BANK, PC87360_REG_FAN_STATUS(i));
1590 data->fan[i] = pc87360_read_value(data, LD_FAN,
1591 NO_BANK, PC87360_REG_FAN(i));
1592 data->fan_min[i] = pc87360_read_value(data,
1593 LD_FAN, NO_BANK,
1594 PC87360_REG_FAN_MIN(i));
1595 /* Change clock divider if needed */
1596 pc87360_autodiv(dev, i);
1597 /* Clear bits and write new divider */
1598 pc87360_write_value(data, LD_FAN, NO_BANK,
1599 PC87360_REG_FAN_STATUS(i),
1600 data->fan_status[i]);
1601 }
1602 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1603 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1604 NO_BANK, PC87360_REG_PWM(i));
1605 }
1606
1607 /* Voltages */
1608 for (i = 0; i < data->innr; i++) {
1609 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1610 PC87365_REG_IN_STATUS);
1611 /* Clear bits */
1612 pc87360_write_value(data, LD_IN, i,
1613 PC87365_REG_IN_STATUS,
1614 data->in_status[i]);
1615 if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
1616 data->in[i] = pc87360_read_value(data, LD_IN,
1617 i, PC87365_REG_IN);
1618 }
1619 if (data->in_status[i] & CHAN_ENA) {
1620 data->in_min[i] = pc87360_read_value(data,
1621 LD_IN, i,
1622 PC87365_REG_IN_MIN);
1623 data->in_max[i] = pc87360_read_value(data,
1624 LD_IN, i,
1625 PC87365_REG_IN_MAX);
1626 if (i >= 11)
1627 data->in_crit[i-11] =
1628 pc87360_read_value(data, LD_IN,
1629 i, PC87365_REG_TEMP_CRIT);
1630 }
1631 }
1632 if (data->innr) {
1633 data->in_alarms = pc87360_read_value(data, LD_IN,
1634 NO_BANK, PC87365_REG_IN_ALARMS1)
1635 | ((pc87360_read_value(data, LD_IN,
1636 NO_BANK, PC87365_REG_IN_ALARMS2)
1637 & 0x07) << 8);
1638 data->vid = (data->vid_conf & 0xE0) ?
1639 pc87360_read_value(data, LD_IN,
1640 NO_BANK, PC87365_REG_VID) : 0x1F;
1641 }
1642
1643 /* Temperatures */
1644 for (i = 0; i < data->tempnr; i++) {
1645 data->temp_status[i] = pc87360_read_value(data,
1646 LD_TEMP, i,
1647 PC87365_REG_TEMP_STATUS);
1648 /* Clear bits */
1649 pc87360_write_value(data, LD_TEMP, i,
1650 PC87365_REG_TEMP_STATUS,
1651 data->temp_status[i]);
1652 if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
1653 data->temp[i] = pc87360_read_value(data,
1654 LD_TEMP, i,
1655 PC87365_REG_TEMP);
1656 }
1657 if (data->temp_status[i] & CHAN_ENA) {
1658 data->temp_min[i] = pc87360_read_value(data,
1659 LD_TEMP, i,
1660 PC87365_REG_TEMP_MIN);
1661 data->temp_max[i] = pc87360_read_value(data,
1662 LD_TEMP, i,
1663 PC87365_REG_TEMP_MAX);
1664 data->temp_crit[i] = pc87360_read_value(data,
1665 LD_TEMP, i,
1666 PC87365_REG_TEMP_CRIT);
1667 }
1668 }
1669 if (data->tempnr) {
1670 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1671 NO_BANK, PC87365_REG_TEMP_ALARMS)
1672 & 0x3F;
1673 }
1674
1675 data->last_updated = jiffies;
1676 data->valid = 1;
1677 }
1678
1679 mutex_unlock(&data->update_lock);
1680
1681 return data;
1682}
1683
1684static int __init pc87360_device_add(unsigned short address)
1685{
1686 struct resource res[3];
1687 int err, i, res_count;
1688
1689 pdev = platform_device_alloc("pc87360", address);
1690 if (!pdev) {
1691 err = -ENOMEM;
1692 pr_err("Device allocation failed\n");
1693 goto exit;
1694 }
1695
1696 memset(res, 0, 3 * sizeof(struct resource));
1697 res_count = 0;
1698 for (i = 0; i < 3; i++) {
1699 if (!extra_isa[i])
1700 continue;
1701 res[res_count].start = extra_isa[i];
1702 res[res_count].end = extra_isa[i] + PC87360_EXTENT - 1;
1703 res[res_count].name = "pc87360",
1704 res[res_count].flags = IORESOURCE_IO,
1705
1706 err = acpi_check_resource_conflict(&res[res_count]);
1707 if (err)
1708 goto exit_device_put;
1709
1710 res_count++;
1711 }
1712
1713 err = platform_device_add_resources(pdev, res, res_count);
1714 if (err) {
1715 pr_err("Device resources addition failed (%d)\n", err);
1716 goto exit_device_put;
1717 }
1718
1719 err = platform_device_add(pdev);
1720 if (err) {
1721 pr_err("Device addition failed (%d)\n", err);
1722 goto exit_device_put;
1723 }
1724
1725 return 0;
1726
1727exit_device_put:
1728 platform_device_put(pdev);
1729exit:
1730 return err;
1731}
1732
1733static int __init pc87360_init(void)
1734{
1735 int err, i;
1736 unsigned short address = 0;
1737
1738 if (pc87360_find(0x2e, &devid, extra_isa)
1739 && pc87360_find(0x4e, &devid, extra_isa)) {
1740 pr_warn("PC8736x not detected, module not inserted\n");
1741 return -ENODEV;
1742 }
1743
1744 /* Arbitrarily pick one of the addresses */
1745 for (i = 0; i < 3; i++) {
1746 if (extra_isa[i] != 0x0000) {
1747 address = extra_isa[i];
1748 break;
1749 }
1750 }
1751
1752 if (address == 0x0000) {
1753 pr_warn("No active logical device, module not inserted\n");
1754 return -ENODEV;
1755 }
1756
1757 err = platform_driver_register(&pc87360_driver);
1758 if (err)
1759 goto exit;
1760
1761 /* Sets global pdev as a side effect */
1762 err = pc87360_device_add(address);
1763 if (err)
1764 goto exit_driver;
1765
1766 return 0;
1767
1768 exit_driver:
1769 platform_driver_unregister(&pc87360_driver);
1770 exit:
1771 return err;
1772}
1773
1774static void __exit pc87360_exit(void)
1775{
1776 platform_device_unregister(pdev);
1777 platform_driver_unregister(&pc87360_driver);
1778}
1779
1780
1781MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1782MODULE_DESCRIPTION("PC8736x hardware monitor");
1783MODULE_LICENSE("GPL");
1784
1785module_init(pc87360_init);
1786module_exit(pc87360_exit);
1/*
2 * pc87360.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 * Copyright (C) 2004, 2007 Jean Delvare <jdelvare@suse.de>
5 *
6 * Copied from smsc47m1.c:
7 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Supports the following chips:
24 *
25 * Chip #vin #fan #pwm #temp devid
26 * PC87360 - 2 2 - 0xE1
27 * PC87363 - 2 2 - 0xE8
28 * PC87364 - 3 3 - 0xE4
29 * PC87365 11 3 3 2 0xE5
30 * PC87366 11 3 3 3-4 0xE9
31 *
32 * This driver assumes that no more than one chip is present, and one of
33 * the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
34 */
35
36#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/slab.h>
41#include <linux/jiffies.h>
42#include <linux/platform_device.h>
43#include <linux/hwmon.h>
44#include <linux/hwmon-sysfs.h>
45#include <linux/hwmon-vid.h>
46#include <linux/err.h>
47#include <linux/mutex.h>
48#include <linux/acpi.h>
49#include <linux/io.h>
50
51static u8 devid;
52static struct platform_device *pdev;
53static unsigned short extra_isa[3];
54static u8 confreg[4];
55
56static int init = 1;
57module_param(init, int, 0);
58MODULE_PARM_DESC(init,
59"Chip initialization level:\n"
60" 0: None\n"
61"*1: Forcibly enable internal voltage and temperature channels, except in9\n"
62" 2: Forcibly enable all voltage and temperature channels, except in9\n"
63" 3: Forcibly enable all voltage and temperature channels, including in9");
64
65static unsigned short force_id;
66module_param(force_id, ushort, 0);
67MODULE_PARM_DESC(force_id, "Override the detected device ID");
68
69/*
70 * Super-I/O registers and operations
71 */
72
73#define DEV 0x07 /* Register: Logical device select */
74#define DEVID 0x20 /* Register: Device ID */
75#define ACT 0x30 /* Register: Device activation */
76#define BASE 0x60 /* Register: Base address */
77
78#define FSCM 0x09 /* Logical device: fans */
79#define VLM 0x0d /* Logical device: voltages */
80#define TMS 0x0e /* Logical device: temperatures */
81#define LDNI_MAX 3
82static const u8 logdev[LDNI_MAX] = { FSCM, VLM, TMS };
83
84#define LD_FAN 0
85#define LD_IN 1
86#define LD_TEMP 2
87
88static inline void superio_outb(int sioaddr, int reg, int val)
89{
90 outb(reg, sioaddr);
91 outb(val, sioaddr + 1);
92}
93
94static inline int superio_inb(int sioaddr, int reg)
95{
96 outb(reg, sioaddr);
97 return inb(sioaddr + 1);
98}
99
100static inline void superio_exit(int sioaddr)
101{
102 outb(0x02, sioaddr);
103 outb(0x02, sioaddr + 1);
104}
105
106/*
107 * Logical devices
108 */
109
110#define PC87360_EXTENT 0x10
111#define PC87365_REG_BANK 0x09
112#define NO_BANK 0xff
113
114/*
115 * Fan registers and conversions
116 */
117
118/* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
119#define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
120#define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
121#define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
122#define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
123#define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
124
125#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : \
126 480000 / ((val) * (div)))
127#define FAN_TO_REG(val, div) ((val) <= 100 ? 0 : \
128 480000 / ((val) * (div)))
129#define FAN_DIV_FROM_REG(val) (1 << (((val) >> 5) & 0x03))
130#define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
131
132#define FAN_CONFIG_MONITOR(val, nr) (((val) >> (2 + (nr) * 3)) & 1)
133#define FAN_CONFIG_CONTROL(val, nr) (((val) >> (3 + (nr) * 3)) & 1)
134#define FAN_CONFIG_INVERT(val, nr) (((val) >> (4 + (nr) * 3)) & 1)
135
136#define PWM_FROM_REG(val, inv) ((inv) ? 255 - (val) : (val))
137static inline u8 PWM_TO_REG(int val, int inv)
138{
139 if (inv)
140 val = 255 - val;
141 if (val < 0)
142 return 0;
143 if (val > 255)
144 return 255;
145 return val;
146}
147
148/*
149 * Voltage registers and conversions
150 */
151
152#define PC87365_REG_IN_CONVRATE 0x07
153#define PC87365_REG_IN_CONFIG 0x08
154#define PC87365_REG_IN 0x0B
155#define PC87365_REG_IN_MIN 0x0D
156#define PC87365_REG_IN_MAX 0x0C
157#define PC87365_REG_IN_STATUS 0x0A
158#define PC87365_REG_IN_ALARMS1 0x00
159#define PC87365_REG_IN_ALARMS2 0x01
160#define PC87365_REG_VID 0x06
161
162#define IN_FROM_REG(val, ref) (((val) * (ref) + 128) / 256)
163#define IN_TO_REG(val, ref) ((val) < 0 ? 0 : \
164 (val) * 256 >= (ref) * 255 ? 255 : \
165 ((val) * 256 + (ref) / 2) / (ref))
166
167/*
168 * Temperature registers and conversions
169 */
170
171#define PC87365_REG_TEMP_CONFIG 0x08
172#define PC87365_REG_TEMP 0x0B
173#define PC87365_REG_TEMP_MIN 0x0D
174#define PC87365_REG_TEMP_MAX 0x0C
175#define PC87365_REG_TEMP_CRIT 0x0E
176#define PC87365_REG_TEMP_STATUS 0x0A
177#define PC87365_REG_TEMP_ALARMS 0x00
178
179#define TEMP_FROM_REG(val) ((val) * 1000)
180#define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
181 (val) > 127000 ? 127 : \
182 (val) < 0 ? ((val) - 500) / 1000 : \
183 ((val) + 500) / 1000)
184
185/*
186 * Device data
187 */
188
189struct pc87360_data {
190 const char *name;
191 struct device *hwmon_dev;
192 struct mutex lock;
193 struct mutex update_lock;
194 char valid; /* !=0 if following fields are valid */
195 unsigned long last_updated; /* In jiffies */
196
197 int address[3];
198
199 u8 fannr, innr, tempnr;
200
201 u8 fan[3]; /* Register value */
202 u8 fan_min[3]; /* Register value */
203 u8 fan_status[3]; /* Register value */
204 u8 pwm[3]; /* Register value */
205 u16 fan_conf; /* Configuration register values, combined */
206
207 u16 in_vref; /* 1 mV/bit */
208 u8 in[14]; /* Register value */
209 u8 in_min[14]; /* Register value */
210 u8 in_max[14]; /* Register value */
211 u8 in_crit[3]; /* Register value */
212 u8 in_status[14]; /* Register value */
213 u16 in_alarms; /* Register values, combined, masked */
214 u8 vid_conf; /* Configuration register value */
215 u8 vrm;
216 u8 vid; /* Register value */
217
218 s8 temp[3]; /* Register value */
219 s8 temp_min[3]; /* Register value */
220 s8 temp_max[3]; /* Register value */
221 s8 temp_crit[3]; /* Register value */
222 u8 temp_status[3]; /* Register value */
223 u8 temp_alarms; /* Register value, masked */
224};
225
226/*
227 * Functions declaration
228 */
229
230static int pc87360_probe(struct platform_device *pdev);
231static int pc87360_remove(struct platform_device *pdev);
232
233static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
234 u8 reg);
235static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
236 u8 reg, u8 value);
237static void pc87360_init_device(struct platform_device *pdev,
238 int use_thermistors);
239static struct pc87360_data *pc87360_update_device(struct device *dev);
240
241/*
242 * Driver data
243 */
244
245static struct platform_driver pc87360_driver = {
246 .driver = {
247 .owner = THIS_MODULE,
248 .name = "pc87360",
249 },
250 .probe = pc87360_probe,
251 .remove = pc87360_remove,
252};
253
254/*
255 * Sysfs stuff
256 */
257
258static ssize_t show_fan_input(struct device *dev,
259 struct device_attribute *devattr, char *buf)
260{
261 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
262 struct pc87360_data *data = pc87360_update_device(dev);
263 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
264 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
265}
266static ssize_t show_fan_min(struct device *dev,
267 struct device_attribute *devattr, char *buf)
268{
269 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
270 struct pc87360_data *data = pc87360_update_device(dev);
271 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
272 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
273}
274static ssize_t show_fan_div(struct device *dev,
275 struct device_attribute *devattr, char *buf)
276{
277 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
278 struct pc87360_data *data = pc87360_update_device(dev);
279 return sprintf(buf, "%u\n",
280 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
281}
282static ssize_t show_fan_status(struct device *dev,
283 struct device_attribute *devattr, char *buf)
284{
285 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
286 struct pc87360_data *data = pc87360_update_device(dev);
287 return sprintf(buf, "%u\n",
288 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
289}
290static ssize_t set_fan_min(struct device *dev,
291 struct device_attribute *devattr, const char *buf,
292 size_t count)
293{
294 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
295 struct pc87360_data *data = dev_get_drvdata(dev);
296 long fan_min;
297 int err;
298
299 err = kstrtol(buf, 10, &fan_min);
300 if (err)
301 return err;
302
303 mutex_lock(&data->update_lock);
304 fan_min = FAN_TO_REG(fan_min,
305 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
306
307 /* If it wouldn't fit, change clock divisor */
308 while (fan_min > 255
309 && (data->fan_status[attr->index] & 0x60) != 0x60) {
310 fan_min >>= 1;
311 data->fan[attr->index] >>= 1;
312 data->fan_status[attr->index] += 0x20;
313 }
314 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
315 pc87360_write_value(data, LD_FAN, NO_BANK,
316 PC87360_REG_FAN_MIN(attr->index),
317 data->fan_min[attr->index]);
318
319 /* Write new divider, preserve alarm bits */
320 pc87360_write_value(data, LD_FAN, NO_BANK,
321 PC87360_REG_FAN_STATUS(attr->index),
322 data->fan_status[attr->index] & 0xF9);
323 mutex_unlock(&data->update_lock);
324
325 return count;
326}
327
328static struct sensor_device_attribute fan_input[] = {
329 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
330 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
331 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
332};
333static struct sensor_device_attribute fan_status[] = {
334 SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
335 SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
336 SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
337};
338static struct sensor_device_attribute fan_div[] = {
339 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
340 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
341 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
342};
343static struct sensor_device_attribute fan_min[] = {
344 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
345 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
346 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
347};
348
349#define FAN_UNIT_ATTRS(X) \
350{ &fan_input[X].dev_attr.attr, \
351 &fan_status[X].dev_attr.attr, \
352 &fan_div[X].dev_attr.attr, \
353 &fan_min[X].dev_attr.attr, \
354 NULL \
355}
356
357static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
358 char *buf)
359{
360 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
361 struct pc87360_data *data = pc87360_update_device(dev);
362 return sprintf(buf, "%u\n",
363 PWM_FROM_REG(data->pwm[attr->index],
364 FAN_CONFIG_INVERT(data->fan_conf,
365 attr->index)));
366}
367static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
368 const char *buf, size_t count)
369{
370 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
371 struct pc87360_data *data = dev_get_drvdata(dev);
372 long val;
373 int err;
374
375 err = kstrtol(buf, 10, &val);
376 if (err)
377 return err;
378
379 mutex_lock(&data->update_lock);
380 data->pwm[attr->index] = PWM_TO_REG(val,
381 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
382 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
383 data->pwm[attr->index]);
384 mutex_unlock(&data->update_lock);
385 return count;
386}
387
388static struct sensor_device_attribute pwm[] = {
389 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0),
390 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1),
391 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2),
392};
393
394static struct attribute *pc8736x_fan_attr[][5] = {
395 FAN_UNIT_ATTRS(0),
396 FAN_UNIT_ATTRS(1),
397 FAN_UNIT_ATTRS(2)
398};
399
400static const struct attribute_group pc8736x_fan_attr_group[] = {
401 { .attrs = pc8736x_fan_attr[0], },
402 { .attrs = pc8736x_fan_attr[1], },
403 { .attrs = pc8736x_fan_attr[2], },
404};
405
406static ssize_t show_in_input(struct device *dev,
407 struct device_attribute *devattr, char *buf)
408{
409 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
410 struct pc87360_data *data = pc87360_update_device(dev);
411 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
412 data->in_vref));
413}
414static ssize_t show_in_min(struct device *dev,
415 struct device_attribute *devattr, char *buf)
416{
417 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
418 struct pc87360_data *data = pc87360_update_device(dev);
419 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
420 data->in_vref));
421}
422static ssize_t show_in_max(struct device *dev,
423 struct device_attribute *devattr, char *buf)
424{
425 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
426 struct pc87360_data *data = pc87360_update_device(dev);
427 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
428 data->in_vref));
429}
430static ssize_t show_in_status(struct device *dev,
431 struct device_attribute *devattr, char *buf)
432{
433 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
434 struct pc87360_data *data = pc87360_update_device(dev);
435 return sprintf(buf, "%u\n", data->in_status[attr->index]);
436}
437static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr,
438 const char *buf, size_t count)
439{
440 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
441 struct pc87360_data *data = dev_get_drvdata(dev);
442 long val;
443 int err;
444
445 err = kstrtol(buf, 10, &val);
446 if (err)
447 return err;
448
449 mutex_lock(&data->update_lock);
450 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
451 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
452 data->in_min[attr->index]);
453 mutex_unlock(&data->update_lock);
454 return count;
455}
456static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr,
457 const char *buf, size_t count)
458{
459 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
460 struct pc87360_data *data = dev_get_drvdata(dev);
461 long val;
462 int err;
463
464 err = kstrtol(buf, 10, &val);
465 if (err)
466 return err;
467
468 mutex_lock(&data->update_lock);
469 data->in_max[attr->index] = IN_TO_REG(val,
470 data->in_vref);
471 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
472 data->in_max[attr->index]);
473 mutex_unlock(&data->update_lock);
474 return count;
475}
476
477static struct sensor_device_attribute in_input[] = {
478 SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
479 SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
480 SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
481 SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
482 SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
483 SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
484 SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
485 SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
486 SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
487 SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
488 SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
489};
490static struct sensor_device_attribute in_status[] = {
491 SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
492 SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
493 SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
494 SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
495 SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
496 SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
497 SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
498 SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
499 SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
500 SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
501 SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
502};
503static struct sensor_device_attribute in_min[] = {
504 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
505 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
506 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
507 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
508 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
509 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
510 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
511 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
512 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
513 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
514 SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
515};
516static struct sensor_device_attribute in_max[] = {
517 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
518 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
519 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
520 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
521 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
522 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
523 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
524 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
525 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
526 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
527 SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
528};
529
530/* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
531#define CHAN_ALM_MIN 0x02 /* min limit crossed */
532#define CHAN_ALM_MAX 0x04 /* max limit exceeded */
533#define TEMP_ALM_CRIT 0x08 /* temp crit exceeded (temp only) */
534
535/*
536 * show_in_min/max_alarm() reads data from the per-channel status
537 * register (sec 11.5.12), not the vin event status registers (sec
538 * 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms)
539 */
540
541static ssize_t show_in_min_alarm(struct device *dev,
542 struct device_attribute *devattr, char *buf)
543{
544 struct pc87360_data *data = pc87360_update_device(dev);
545 unsigned nr = to_sensor_dev_attr(devattr)->index;
546
547 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
548}
549static ssize_t show_in_max_alarm(struct device *dev,
550 struct device_attribute *devattr, char *buf)
551{
552 struct pc87360_data *data = pc87360_update_device(dev);
553 unsigned nr = to_sensor_dev_attr(devattr)->index;
554
555 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
556}
557
558static struct sensor_device_attribute in_min_alarm[] = {
559 SENSOR_ATTR(in0_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 0),
560 SENSOR_ATTR(in1_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 1),
561 SENSOR_ATTR(in2_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 2),
562 SENSOR_ATTR(in3_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 3),
563 SENSOR_ATTR(in4_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 4),
564 SENSOR_ATTR(in5_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 5),
565 SENSOR_ATTR(in6_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 6),
566 SENSOR_ATTR(in7_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 7),
567 SENSOR_ATTR(in8_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 8),
568 SENSOR_ATTR(in9_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 9),
569 SENSOR_ATTR(in10_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 10),
570};
571static struct sensor_device_attribute in_max_alarm[] = {
572 SENSOR_ATTR(in0_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 0),
573 SENSOR_ATTR(in1_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 1),
574 SENSOR_ATTR(in2_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 2),
575 SENSOR_ATTR(in3_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 3),
576 SENSOR_ATTR(in4_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 4),
577 SENSOR_ATTR(in5_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 5),
578 SENSOR_ATTR(in6_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 6),
579 SENSOR_ATTR(in7_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 7),
580 SENSOR_ATTR(in8_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 8),
581 SENSOR_ATTR(in9_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 9),
582 SENSOR_ATTR(in10_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 10),
583};
584
585#define VIN_UNIT_ATTRS(X) \
586 &in_input[X].dev_attr.attr, \
587 &in_status[X].dev_attr.attr, \
588 &in_min[X].dev_attr.attr, \
589 &in_max[X].dev_attr.attr, \
590 &in_min_alarm[X].dev_attr.attr, \
591 &in_max_alarm[X].dev_attr.attr
592
593static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
594 char *buf)
595{
596 struct pc87360_data *data = pc87360_update_device(dev);
597 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
598}
599static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
600
601static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
602 char *buf)
603{
604 struct pc87360_data *data = dev_get_drvdata(dev);
605 return sprintf(buf, "%u\n", data->vrm);
606}
607static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
608 const char *buf, size_t count)
609{
610 struct pc87360_data *data = dev_get_drvdata(dev);
611 unsigned long val;
612 int err;
613
614 err = kstrtoul(buf, 10, &val);
615 if (err)
616 return err;
617
618 data->vrm = val;
619 return count;
620}
621static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
622
623static ssize_t show_in_alarms(struct device *dev,
624 struct device_attribute *attr, char *buf)
625{
626 struct pc87360_data *data = pc87360_update_device(dev);
627 return sprintf(buf, "%u\n", data->in_alarms);
628}
629static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
630
631static struct attribute *pc8736x_vin_attr_array[] = {
632 VIN_UNIT_ATTRS(0),
633 VIN_UNIT_ATTRS(1),
634 VIN_UNIT_ATTRS(2),
635 VIN_UNIT_ATTRS(3),
636 VIN_UNIT_ATTRS(4),
637 VIN_UNIT_ATTRS(5),
638 VIN_UNIT_ATTRS(6),
639 VIN_UNIT_ATTRS(7),
640 VIN_UNIT_ATTRS(8),
641 VIN_UNIT_ATTRS(9),
642 VIN_UNIT_ATTRS(10),
643 &dev_attr_cpu0_vid.attr,
644 &dev_attr_vrm.attr,
645 &dev_attr_alarms_in.attr,
646 NULL
647};
648static const struct attribute_group pc8736x_vin_group = {
649 .attrs = pc8736x_vin_attr_array,
650};
651
652static ssize_t show_therm_input(struct device *dev,
653 struct device_attribute *devattr, char *buf)
654{
655 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
656 struct pc87360_data *data = pc87360_update_device(dev);
657 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
658 data->in_vref));
659}
660static ssize_t show_therm_min(struct device *dev,
661 struct device_attribute *devattr, char *buf)
662{
663 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
664 struct pc87360_data *data = pc87360_update_device(dev);
665 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
666 data->in_vref));
667}
668static ssize_t show_therm_max(struct device *dev,
669 struct device_attribute *devattr, char *buf)
670{
671 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
672 struct pc87360_data *data = pc87360_update_device(dev);
673 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
674 data->in_vref));
675}
676static ssize_t show_therm_crit(struct device *dev,
677 struct device_attribute *devattr, char *buf)
678{
679 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
680 struct pc87360_data *data = pc87360_update_device(dev);
681 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
682 data->in_vref));
683}
684static ssize_t show_therm_status(struct device *dev,
685 struct device_attribute *devattr, char *buf)
686{
687 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
688 struct pc87360_data *data = pc87360_update_device(dev);
689 return sprintf(buf, "%u\n", data->in_status[attr->index]);
690}
691
692static ssize_t set_therm_min(struct device *dev,
693 struct device_attribute *devattr,
694 const char *buf, size_t count)
695{
696 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
697 struct pc87360_data *data = dev_get_drvdata(dev);
698 long val;
699 int err;
700
701 err = kstrtol(buf, 10, &val);
702 if (err)
703 return err;
704
705 mutex_lock(&data->update_lock);
706 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
707 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
708 data->in_min[attr->index]);
709 mutex_unlock(&data->update_lock);
710 return count;
711}
712
713static ssize_t set_therm_max(struct device *dev,
714 struct device_attribute *devattr,
715 const char *buf, size_t count)
716{
717 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
718 struct pc87360_data *data = dev_get_drvdata(dev);
719 long val;
720 int err;
721
722 err = kstrtol(buf, 10, &val);
723 if (err)
724 return err;
725
726 mutex_lock(&data->update_lock);
727 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
728 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
729 data->in_max[attr->index]);
730 mutex_unlock(&data->update_lock);
731 return count;
732}
733static ssize_t set_therm_crit(struct device *dev,
734 struct device_attribute *devattr,
735 const char *buf, size_t count)
736{
737 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
738 struct pc87360_data *data = dev_get_drvdata(dev);
739 long val;
740 int err;
741
742 err = kstrtol(buf, 10, &val);
743 if (err)
744 return err;
745
746 mutex_lock(&data->update_lock);
747 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
748 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
749 data->in_crit[attr->index-11]);
750 mutex_unlock(&data->update_lock);
751 return count;
752}
753
754/*
755 * the +11 term below reflects the fact that VLM units 11,12,13 are
756 * used in the chip to measure voltage across the thermistors
757 */
758static struct sensor_device_attribute therm_input[] = {
759 SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0 + 11),
760 SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1 + 11),
761 SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2 + 11),
762};
763static struct sensor_device_attribute therm_status[] = {
764 SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0 + 11),
765 SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1 + 11),
766 SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2 + 11),
767};
768static struct sensor_device_attribute therm_min[] = {
769 SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
770 show_therm_min, set_therm_min, 0 + 11),
771 SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
772 show_therm_min, set_therm_min, 1 + 11),
773 SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
774 show_therm_min, set_therm_min, 2 + 11),
775};
776static struct sensor_device_attribute therm_max[] = {
777 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
778 show_therm_max, set_therm_max, 0 + 11),
779 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
780 show_therm_max, set_therm_max, 1 + 11),
781 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
782 show_therm_max, set_therm_max, 2 + 11),
783};
784static struct sensor_device_attribute therm_crit[] = {
785 SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
786 show_therm_crit, set_therm_crit, 0 + 11),
787 SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
788 show_therm_crit, set_therm_crit, 1 + 11),
789 SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
790 show_therm_crit, set_therm_crit, 2 + 11),
791};
792
793/*
794 * show_therm_min/max_alarm() reads data from the per-channel voltage
795 * status register (sec 11.5.12)
796 */
797
798static ssize_t show_therm_min_alarm(struct device *dev,
799 struct device_attribute *devattr, char *buf)
800{
801 struct pc87360_data *data = pc87360_update_device(dev);
802 unsigned nr = to_sensor_dev_attr(devattr)->index;
803
804 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
805}
806static ssize_t show_therm_max_alarm(struct device *dev,
807 struct device_attribute *devattr, char *buf)
808{
809 struct pc87360_data *data = pc87360_update_device(dev);
810 unsigned nr = to_sensor_dev_attr(devattr)->index;
811
812 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
813}
814static ssize_t show_therm_crit_alarm(struct device *dev,
815 struct device_attribute *devattr, char *buf)
816{
817 struct pc87360_data *data = pc87360_update_device(dev);
818 unsigned nr = to_sensor_dev_attr(devattr)->index;
819
820 return sprintf(buf, "%u\n", !!(data->in_status[nr] & TEMP_ALM_CRIT));
821}
822
823static struct sensor_device_attribute therm_min_alarm[] = {
824 SENSOR_ATTR(temp4_min_alarm, S_IRUGO,
825 show_therm_min_alarm, NULL, 0 + 11),
826 SENSOR_ATTR(temp5_min_alarm, S_IRUGO,
827 show_therm_min_alarm, NULL, 1 + 11),
828 SENSOR_ATTR(temp6_min_alarm, S_IRUGO,
829 show_therm_min_alarm, NULL, 2 + 11),
830};
831static struct sensor_device_attribute therm_max_alarm[] = {
832 SENSOR_ATTR(temp4_max_alarm, S_IRUGO,
833 show_therm_max_alarm, NULL, 0 + 11),
834 SENSOR_ATTR(temp5_max_alarm, S_IRUGO,
835 show_therm_max_alarm, NULL, 1 + 11),
836 SENSOR_ATTR(temp6_max_alarm, S_IRUGO,
837 show_therm_max_alarm, NULL, 2 + 11),
838};
839static struct sensor_device_attribute therm_crit_alarm[] = {
840 SENSOR_ATTR(temp4_crit_alarm, S_IRUGO,
841 show_therm_crit_alarm, NULL, 0 + 11),
842 SENSOR_ATTR(temp5_crit_alarm, S_IRUGO,
843 show_therm_crit_alarm, NULL, 1 + 11),
844 SENSOR_ATTR(temp6_crit_alarm, S_IRUGO,
845 show_therm_crit_alarm, NULL, 2 + 11),
846};
847
848#define THERM_UNIT_ATTRS(X) \
849 &therm_input[X].dev_attr.attr, \
850 &therm_status[X].dev_attr.attr, \
851 &therm_min[X].dev_attr.attr, \
852 &therm_max[X].dev_attr.attr, \
853 &therm_crit[X].dev_attr.attr, \
854 &therm_min_alarm[X].dev_attr.attr, \
855 &therm_max_alarm[X].dev_attr.attr, \
856 &therm_crit_alarm[X].dev_attr.attr
857
858static struct attribute *pc8736x_therm_attr_array[] = {
859 THERM_UNIT_ATTRS(0),
860 THERM_UNIT_ATTRS(1),
861 THERM_UNIT_ATTRS(2),
862 NULL
863};
864static const struct attribute_group pc8736x_therm_group = {
865 .attrs = pc8736x_therm_attr_array,
866};
867
868static ssize_t show_temp_input(struct device *dev,
869 struct device_attribute *devattr, char *buf)
870{
871 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
872 struct pc87360_data *data = pc87360_update_device(dev);
873 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
874}
875
876static ssize_t show_temp_min(struct device *dev,
877 struct device_attribute *devattr, char *buf)
878{
879 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
880 struct pc87360_data *data = pc87360_update_device(dev);
881 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
882}
883
884static ssize_t show_temp_max(struct device *dev,
885 struct device_attribute *devattr, char *buf)
886{
887 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
888 struct pc87360_data *data = pc87360_update_device(dev);
889 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
890}
891
892static ssize_t show_temp_crit(struct device *dev,
893 struct device_attribute *devattr, char *buf)
894{
895 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
896 struct pc87360_data *data = pc87360_update_device(dev);
897 return sprintf(buf, "%d\n",
898 TEMP_FROM_REG(data->temp_crit[attr->index]));
899}
900
901static ssize_t show_temp_status(struct device *dev,
902 struct device_attribute *devattr, char *buf)
903{
904 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
905 struct pc87360_data *data = pc87360_update_device(dev);
906 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
907}
908
909static ssize_t set_temp_min(struct device *dev,
910 struct device_attribute *devattr,
911 const char *buf, size_t count)
912{
913 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
914 struct pc87360_data *data = dev_get_drvdata(dev);
915 long val;
916 int err;
917
918 err = kstrtol(buf, 10, &val);
919 if (err)
920 return err;
921
922 mutex_lock(&data->update_lock);
923 data->temp_min[attr->index] = TEMP_TO_REG(val);
924 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
925 data->temp_min[attr->index]);
926 mutex_unlock(&data->update_lock);
927 return count;
928}
929
930static ssize_t set_temp_max(struct device *dev,
931 struct device_attribute *devattr,
932 const char *buf, size_t count)
933{
934 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
935 struct pc87360_data *data = dev_get_drvdata(dev);
936 long val;
937 int err;
938
939 err = kstrtol(buf, 10, &val);
940 if (err)
941 return err;
942
943 mutex_lock(&data->update_lock);
944 data->temp_max[attr->index] = TEMP_TO_REG(val);
945 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
946 data->temp_max[attr->index]);
947 mutex_unlock(&data->update_lock);
948 return count;
949}
950
951static ssize_t set_temp_crit(struct device *dev,
952 struct device_attribute *devattr, const char *buf,
953 size_t count)
954{
955 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
956 struct pc87360_data *data = dev_get_drvdata(dev);
957 long val;
958 int err;
959
960 err = kstrtol(buf, 10, &val);
961 if (err)
962 return err;
963
964 mutex_lock(&data->update_lock);
965 data->temp_crit[attr->index] = TEMP_TO_REG(val);
966 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
967 data->temp_crit[attr->index]);
968 mutex_unlock(&data->update_lock);
969 return count;
970}
971
972static struct sensor_device_attribute temp_input[] = {
973 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
974 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
975 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
976};
977static struct sensor_device_attribute temp_status[] = {
978 SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
979 SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
980 SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
981};
982static struct sensor_device_attribute temp_min[] = {
983 SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
984 show_temp_min, set_temp_min, 0),
985 SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
986 show_temp_min, set_temp_min, 1),
987 SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
988 show_temp_min, set_temp_min, 2),
989};
990static struct sensor_device_attribute temp_max[] = {
991 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
992 show_temp_max, set_temp_max, 0),
993 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
994 show_temp_max, set_temp_max, 1),
995 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
996 show_temp_max, set_temp_max, 2),
997};
998static struct sensor_device_attribute temp_crit[] = {
999 SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
1000 show_temp_crit, set_temp_crit, 0),
1001 SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
1002 show_temp_crit, set_temp_crit, 1),
1003 SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
1004 show_temp_crit, set_temp_crit, 2),
1005};
1006
1007static ssize_t show_temp_alarms(struct device *dev,
1008 struct device_attribute *attr, char *buf)
1009{
1010 struct pc87360_data *data = pc87360_update_device(dev);
1011 return sprintf(buf, "%u\n", data->temp_alarms);
1012}
1013
1014static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
1015
1016/*
1017 * show_temp_min/max_alarm() reads data from the per-channel status
1018 * register (sec 12.3.7), not the temp event status registers (sec
1019 * 12.3.2) that show_temp_alarm() reads (via data->temp_alarms)
1020 */
1021
1022static ssize_t show_temp_min_alarm(struct device *dev,
1023 struct device_attribute *devattr, char *buf)
1024{
1025 struct pc87360_data *data = pc87360_update_device(dev);
1026 unsigned nr = to_sensor_dev_attr(devattr)->index;
1027
1028 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN));
1029}
1030
1031static ssize_t show_temp_max_alarm(struct device *dev,
1032 struct device_attribute *devattr, char *buf)
1033{
1034 struct pc87360_data *data = pc87360_update_device(dev);
1035 unsigned nr = to_sensor_dev_attr(devattr)->index;
1036
1037 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX));
1038}
1039
1040static ssize_t show_temp_crit_alarm(struct device *dev,
1041 struct device_attribute *devattr, char *buf)
1042{
1043 struct pc87360_data *data = pc87360_update_device(dev);
1044 unsigned nr = to_sensor_dev_attr(devattr)->index;
1045
1046 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT));
1047}
1048
1049static struct sensor_device_attribute temp_min_alarm[] = {
1050 SENSOR_ATTR(temp1_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 0),
1051 SENSOR_ATTR(temp2_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 1),
1052 SENSOR_ATTR(temp3_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 2),
1053};
1054
1055static struct sensor_device_attribute temp_max_alarm[] = {
1056 SENSOR_ATTR(temp1_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 0),
1057 SENSOR_ATTR(temp2_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 1),
1058 SENSOR_ATTR(temp3_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 2),
1059};
1060
1061static struct sensor_device_attribute temp_crit_alarm[] = {
1062 SENSOR_ATTR(temp1_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 0),
1063 SENSOR_ATTR(temp2_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 1),
1064 SENSOR_ATTR(temp3_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 2),
1065};
1066
1067#define TEMP_FAULT 0x40 /* open diode */
1068static ssize_t show_temp_fault(struct device *dev,
1069 struct device_attribute *devattr, char *buf)
1070{
1071 struct pc87360_data *data = pc87360_update_device(dev);
1072 unsigned nr = to_sensor_dev_attr(devattr)->index;
1073
1074 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT));
1075}
1076static struct sensor_device_attribute temp_fault[] = {
1077 SENSOR_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0),
1078 SENSOR_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1),
1079 SENSOR_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2),
1080};
1081
1082#define TEMP_UNIT_ATTRS(X) \
1083{ &temp_input[X].dev_attr.attr, \
1084 &temp_status[X].dev_attr.attr, \
1085 &temp_min[X].dev_attr.attr, \
1086 &temp_max[X].dev_attr.attr, \
1087 &temp_crit[X].dev_attr.attr, \
1088 &temp_min_alarm[X].dev_attr.attr, \
1089 &temp_max_alarm[X].dev_attr.attr, \
1090 &temp_crit_alarm[X].dev_attr.attr, \
1091 &temp_fault[X].dev_attr.attr, \
1092 NULL \
1093}
1094
1095static struct attribute *pc8736x_temp_attr[][10] = {
1096 TEMP_UNIT_ATTRS(0),
1097 TEMP_UNIT_ATTRS(1),
1098 TEMP_UNIT_ATTRS(2)
1099};
1100
1101static const struct attribute_group pc8736x_temp_attr_group[] = {
1102 { .attrs = pc8736x_temp_attr[0] },
1103 { .attrs = pc8736x_temp_attr[1] },
1104 { .attrs = pc8736x_temp_attr[2] }
1105};
1106
1107static ssize_t show_name(struct device *dev,
1108 struct device_attribute *devattr, char *buf)
1109{
1110 struct pc87360_data *data = dev_get_drvdata(dev);
1111 return sprintf(buf, "%s\n", data->name);
1112}
1113
1114static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1115
1116/*
1117 * Device detection, registration and update
1118 */
1119
1120static int __init pc87360_find(int sioaddr, u8 *devid,
1121 unsigned short *addresses)
1122{
1123 u16 val;
1124 int i;
1125 int nrdev; /* logical device count */
1126
1127 /* No superio_enter */
1128
1129 /* Identify device */
1130 val = force_id ? force_id : superio_inb(sioaddr, DEVID);
1131 switch (val) {
1132 case 0xE1: /* PC87360 */
1133 case 0xE8: /* PC87363 */
1134 case 0xE4: /* PC87364 */
1135 nrdev = 1;
1136 break;
1137 case 0xE5: /* PC87365 */
1138 case 0xE9: /* PC87366 */
1139 nrdev = 3;
1140 break;
1141 default:
1142 superio_exit(sioaddr);
1143 return -ENODEV;
1144 }
1145 /* Remember the device id */
1146 *devid = val;
1147
1148 for (i = 0; i < nrdev; i++) {
1149 /* select logical device */
1150 superio_outb(sioaddr, DEV, logdev[i]);
1151
1152 val = superio_inb(sioaddr, ACT);
1153 if (!(val & 0x01)) {
1154 pr_info("Device 0x%02x not activated\n", logdev[i]);
1155 continue;
1156 }
1157
1158 val = (superio_inb(sioaddr, BASE) << 8)
1159 | superio_inb(sioaddr, BASE + 1);
1160 if (!val) {
1161 pr_info("Base address not set for device 0x%02x\n",
1162 logdev[i]);
1163 continue;
1164 }
1165
1166 addresses[i] = val;
1167
1168 if (i == 0) { /* Fans */
1169 confreg[0] = superio_inb(sioaddr, 0xF0);
1170 confreg[1] = superio_inb(sioaddr, 0xF1);
1171
1172 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 1,
1173 (confreg[0] >> 2) & 1, (confreg[0] >> 3) & 1,
1174 (confreg[0] >> 4) & 1);
1175 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 2,
1176 (confreg[0] >> 5) & 1, (confreg[0] >> 6) & 1,
1177 (confreg[0] >> 7) & 1);
1178 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 3,
1179 confreg[1] & 1, (confreg[1] >> 1) & 1,
1180 (confreg[1] >> 2) & 1);
1181 } else if (i == 1) { /* Voltages */
1182 /* Are we using thermistors? */
1183 if (*devid == 0xE9) { /* PC87366 */
1184 /*
1185 * These registers are not logical-device
1186 * specific, just that we won't need them if
1187 * we don't use the VLM device
1188 */
1189 confreg[2] = superio_inb(sioaddr, 0x2B);
1190 confreg[3] = superio_inb(sioaddr, 0x25);
1191
1192 if (confreg[2] & 0x40) {
1193 pr_info("Using thermistors for temperature monitoring\n");
1194 }
1195 if (confreg[3] & 0xE0) {
1196 pr_info("VID inputs routed (mode %u)\n",
1197 confreg[3] >> 5);
1198 }
1199 }
1200 }
1201 }
1202
1203 superio_exit(sioaddr);
1204 return 0;
1205}
1206
1207static void pc87360_remove_files(struct device *dev)
1208{
1209 int i;
1210
1211 device_remove_file(dev, &dev_attr_name);
1212 device_remove_file(dev, &dev_attr_alarms_temp);
1213 for (i = 0; i < ARRAY_SIZE(pc8736x_temp_attr_group); i++)
1214 sysfs_remove_group(&dev->kobj, &pc8736x_temp_attr_group[i]);
1215 for (i = 0; i < ARRAY_SIZE(pc8736x_fan_attr_group); i++) {
1216 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_attr_group[i]);
1217 device_remove_file(dev, &pwm[i].dev_attr);
1218 }
1219 sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1220 sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
1221}
1222
1223static int pc87360_probe(struct platform_device *pdev)
1224{
1225 int i;
1226 struct pc87360_data *data;
1227 int err = 0;
1228 const char *name;
1229 int use_thermistors = 0;
1230 struct device *dev = &pdev->dev;
1231
1232 data = devm_kzalloc(dev, sizeof(struct pc87360_data), GFP_KERNEL);
1233 if (!data)
1234 return -ENOMEM;
1235
1236 switch (devid) {
1237 default:
1238 name = "pc87360";
1239 data->fannr = 2;
1240 break;
1241 case 0xe8:
1242 name = "pc87363";
1243 data->fannr = 2;
1244 break;
1245 case 0xe4:
1246 name = "pc87364";
1247 data->fannr = 3;
1248 break;
1249 case 0xe5:
1250 name = "pc87365";
1251 data->fannr = extra_isa[0] ? 3 : 0;
1252 data->innr = extra_isa[1] ? 11 : 0;
1253 data->tempnr = extra_isa[2] ? 2 : 0;
1254 break;
1255 case 0xe9:
1256 name = "pc87366";
1257 data->fannr = extra_isa[0] ? 3 : 0;
1258 data->innr = extra_isa[1] ? 14 : 0;
1259 data->tempnr = extra_isa[2] ? 3 : 0;
1260 break;
1261 }
1262
1263 data->name = name;
1264 mutex_init(&data->lock);
1265 mutex_init(&data->update_lock);
1266 platform_set_drvdata(pdev, data);
1267
1268 for (i = 0; i < LDNI_MAX; i++) {
1269 data->address[i] = extra_isa[i];
1270 if (data->address[i]
1271 && !devm_request_region(dev, extra_isa[i], PC87360_EXTENT,
1272 pc87360_driver.driver.name)) {
1273 dev_err(dev,
1274 "Region 0x%x-0x%x already in use!\n",
1275 extra_isa[i], extra_isa[i]+PC87360_EXTENT-1);
1276 return -EBUSY;
1277 }
1278 }
1279
1280 /* Retrieve the fans configuration from Super-I/O space */
1281 if (data->fannr)
1282 data->fan_conf = confreg[0] | (confreg[1] << 8);
1283
1284 /*
1285 * Use the correct reference voltage
1286 * Unless both the VLM and the TMS logical devices agree to
1287 * use an external Vref, the internal one is used.
1288 */
1289 if (data->innr) {
1290 i = pc87360_read_value(data, LD_IN, NO_BANK,
1291 PC87365_REG_IN_CONFIG);
1292 if (data->tempnr) {
1293 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
1294 PC87365_REG_TEMP_CONFIG);
1295 }
1296 data->in_vref = (i&0x02) ? 3025 : 2966;
1297 dev_dbg(dev, "Using %s reference voltage\n",
1298 (i&0x02) ? "external" : "internal");
1299
1300 data->vid_conf = confreg[3];
1301 data->vrm = vid_which_vrm();
1302 }
1303
1304 /* Fan clock dividers may be needed before any data is read */
1305 for (i = 0; i < data->fannr; i++) {
1306 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1307 data->fan_status[i] = pc87360_read_value(data,
1308 LD_FAN, NO_BANK,
1309 PC87360_REG_FAN_STATUS(i));
1310 }
1311
1312 if (init > 0) {
1313 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1314 use_thermistors = confreg[2] & 0x40;
1315
1316 pc87360_init_device(pdev, use_thermistors);
1317 }
1318
1319 /* Register all-or-nothing sysfs groups */
1320
1321 if (data->innr) {
1322 err = sysfs_create_group(&dev->kobj, &pc8736x_vin_group);
1323 if (err)
1324 goto error;
1325 }
1326
1327 if (data->innr == 14) {
1328 err = sysfs_create_group(&dev->kobj, &pc8736x_therm_group);
1329 if (err)
1330 goto error;
1331 }
1332
1333 /* create device attr-files for varying sysfs groups */
1334
1335 if (data->tempnr) {
1336 for (i = 0; i < data->tempnr; i++) {
1337 err = sysfs_create_group(&dev->kobj,
1338 &pc8736x_temp_attr_group[i]);
1339 if (err)
1340 goto error;
1341 }
1342 err = device_create_file(dev, &dev_attr_alarms_temp);
1343 if (err)
1344 goto error;
1345 }
1346
1347 for (i = 0; i < data->fannr; i++) {
1348 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1349 err = sysfs_create_group(&dev->kobj,
1350 &pc8736x_fan_attr_group[i]);
1351 if (err)
1352 goto error;
1353 }
1354 if (FAN_CONFIG_CONTROL(data->fan_conf, i)) {
1355 err = device_create_file(dev, &pwm[i].dev_attr);
1356 if (err)
1357 goto error;
1358 }
1359 }
1360
1361 err = device_create_file(dev, &dev_attr_name);
1362 if (err)
1363 goto error;
1364
1365 data->hwmon_dev = hwmon_device_register(dev);
1366 if (IS_ERR(data->hwmon_dev)) {
1367 err = PTR_ERR(data->hwmon_dev);
1368 goto error;
1369 }
1370 return 0;
1371
1372error:
1373 pc87360_remove_files(dev);
1374 return err;
1375}
1376
1377static int pc87360_remove(struct platform_device *pdev)
1378{
1379 struct pc87360_data *data = platform_get_drvdata(pdev);
1380
1381 hwmon_device_unregister(data->hwmon_dev);
1382 pc87360_remove_files(&pdev->dev);
1383
1384 return 0;
1385}
1386
1387/*
1388 * ldi is the logical device index
1389 * bank is for voltages and temperatures only
1390 */
1391static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1392 u8 reg)
1393{
1394 int res;
1395
1396 mutex_lock(&(data->lock));
1397 if (bank != NO_BANK)
1398 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1399 res = inb_p(data->address[ldi] + reg);
1400 mutex_unlock(&(data->lock));
1401
1402 return res;
1403}
1404
1405static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1406 u8 reg, u8 value)
1407{
1408 mutex_lock(&(data->lock));
1409 if (bank != NO_BANK)
1410 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1411 outb_p(value, data->address[ldi] + reg);
1412 mutex_unlock(&(data->lock));
1413}
1414
1415/* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
1416#define CHAN_CNVRTD 0x80 /* new data ready */
1417#define CHAN_ENA 0x01 /* enabled channel (temp or vin) */
1418#define CHAN_ALM_ENA 0x10 /* propagate to alarms-reg ?? (chk val!) */
1419#define CHAN_READY (CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
1420
1421#define TEMP_OTS_OE 0x20 /* OTS Output Enable */
1422#define VIN_RW1C_MASK (CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN) /* 0x87 */
1423#define TEMP_RW1C_MASK (VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */
1424
1425static void pc87360_init_device(struct platform_device *pdev,
1426 int use_thermistors)
1427{
1428 struct pc87360_data *data = platform_get_drvdata(pdev);
1429 int i, nr;
1430 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1431 const u8 init_temp[3] = { 2, 2, 1 };
1432 u8 reg;
1433
1434 if (init >= 2 && data->innr) {
1435 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1436 PC87365_REG_IN_CONVRATE);
1437 dev_info(&pdev->dev,
1438 "VLM conversion set to 1s period, 160us delay\n");
1439 pc87360_write_value(data, LD_IN, NO_BANK,
1440 PC87365_REG_IN_CONVRATE,
1441 (reg & 0xC0) | 0x11);
1442 }
1443
1444 nr = data->innr < 11 ? data->innr : 11;
1445 for (i = 0; i < nr; i++) {
1446 reg = pc87360_read_value(data, LD_IN, i,
1447 PC87365_REG_IN_STATUS);
1448 dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg);
1449 if (init >= init_in[i]) {
1450 /* Forcibly enable voltage channel */
1451 if (!(reg & CHAN_ENA)) {
1452 dev_dbg(&pdev->dev, "Forcibly enabling in%d\n",
1453 i);
1454 pc87360_write_value(data, LD_IN, i,
1455 PC87365_REG_IN_STATUS,
1456 (reg & 0x68) | 0x87);
1457 }
1458 }
1459 }
1460
1461 /*
1462 * We can't blindly trust the Super-I/O space configuration bit,
1463 * most BIOS won't set it properly
1464 */
1465 dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors);
1466 for (i = 11; i < data->innr; i++) {
1467 reg = pc87360_read_value(data, LD_IN, i,
1468 PC87365_REG_TEMP_STATUS);
1469 use_thermistors = use_thermistors || (reg & CHAN_ENA);
1470 /* thermistors are temp[4-6], measured on vin[11-14] */
1471 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg);
1472 }
1473 dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors);
1474
1475 i = use_thermistors ? 2 : 0;
1476 for (; i < data->tempnr; i++) {
1477 reg = pc87360_read_value(data, LD_TEMP, i,
1478 PC87365_REG_TEMP_STATUS);
1479 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i + 1, reg);
1480 if (init >= init_temp[i]) {
1481 /* Forcibly enable temperature channel */
1482 if (!(reg & CHAN_ENA)) {
1483 dev_dbg(&pdev->dev,
1484 "Forcibly enabling temp%d\n", i + 1);
1485 pc87360_write_value(data, LD_TEMP, i,
1486 PC87365_REG_TEMP_STATUS,
1487 0xCF);
1488 }
1489 }
1490 }
1491
1492 if (use_thermistors) {
1493 for (i = 11; i < data->innr; i++) {
1494 if (init >= init_in[i]) {
1495 /*
1496 * The pin may already be used by thermal
1497 * diodes
1498 */
1499 reg = pc87360_read_value(data, LD_TEMP,
1500 (i - 11) / 2, PC87365_REG_TEMP_STATUS);
1501 if (reg & CHAN_ENA) {
1502 dev_dbg(&pdev->dev,
1503 "Skipping temp%d, pin already in use by temp%d\n",
1504 i - 7, (i - 11) / 2);
1505 continue;
1506 }
1507
1508 /* Forcibly enable thermistor channel */
1509 reg = pc87360_read_value(data, LD_IN, i,
1510 PC87365_REG_IN_STATUS);
1511 if (!(reg & CHAN_ENA)) {
1512 dev_dbg(&pdev->dev,
1513 "Forcibly enabling temp%d\n",
1514 i - 7);
1515 pc87360_write_value(data, LD_IN, i,
1516 PC87365_REG_TEMP_STATUS,
1517 (reg & 0x60) | 0x8F);
1518 }
1519 }
1520 }
1521 }
1522
1523 if (data->innr) {
1524 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1525 PC87365_REG_IN_CONFIG);
1526 dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg);
1527 if (reg & CHAN_ENA) {
1528 dev_dbg(&pdev->dev,
1529 "Forcibly enabling monitoring (VLM)\n");
1530 pc87360_write_value(data, LD_IN, NO_BANK,
1531 PC87365_REG_IN_CONFIG,
1532 reg & 0xFE);
1533 }
1534 }
1535
1536 if (data->tempnr) {
1537 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1538 PC87365_REG_TEMP_CONFIG);
1539 dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg);
1540 if (reg & CHAN_ENA) {
1541 dev_dbg(&pdev->dev,
1542 "Forcibly enabling monitoring (TMS)\n");
1543 pc87360_write_value(data, LD_TEMP, NO_BANK,
1544 PC87365_REG_TEMP_CONFIG,
1545 reg & 0xFE);
1546 }
1547
1548 if (init >= 2) {
1549 /* Chip config as documented by National Semi. */
1550 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1551 /*
1552 * We voluntarily omit the bank here, in case the
1553 * sequence itself matters. It shouldn't be a problem,
1554 * since nobody else is supposed to access the
1555 * device at that point.
1556 */
1557 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1558 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1559 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1560 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1561 }
1562 }
1563}
1564
1565static void pc87360_autodiv(struct device *dev, int nr)
1566{
1567 struct pc87360_data *data = dev_get_drvdata(dev);
1568 u8 old_min = data->fan_min[nr];
1569
1570 /* Increase clock divider if needed and possible */
1571 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1572 || (data->fan[nr] >= 224)) { /* next to overflow */
1573 if ((data->fan_status[nr] & 0x60) != 0x60) {
1574 data->fan_status[nr] += 0x20;
1575 data->fan_min[nr] >>= 1;
1576 data->fan[nr] >>= 1;
1577 dev_dbg(dev,
1578 "Increasing clock divider to %d for fan %d\n",
1579 FAN_DIV_FROM_REG(data->fan_status[nr]), nr + 1);
1580 }
1581 } else {
1582 /* Decrease clock divider if possible */
1583 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1584 && data->fan[nr] < 85 /* bad accuracy */
1585 && (data->fan_status[nr] & 0x60) != 0x00) {
1586 data->fan_status[nr] -= 0x20;
1587 data->fan_min[nr] <<= 1;
1588 data->fan[nr] <<= 1;
1589 dev_dbg(dev,
1590 "Decreasing clock divider to %d for fan %d\n",
1591 FAN_DIV_FROM_REG(data->fan_status[nr]),
1592 nr + 1);
1593 }
1594 }
1595
1596 /* Write new fan min if it changed */
1597 if (old_min != data->fan_min[nr]) {
1598 pc87360_write_value(data, LD_FAN, NO_BANK,
1599 PC87360_REG_FAN_MIN(nr),
1600 data->fan_min[nr]);
1601 }
1602}
1603
1604static struct pc87360_data *pc87360_update_device(struct device *dev)
1605{
1606 struct pc87360_data *data = dev_get_drvdata(dev);
1607 u8 i;
1608
1609 mutex_lock(&data->update_lock);
1610
1611 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1612 dev_dbg(dev, "Data update\n");
1613
1614 /* Fans */
1615 for (i = 0; i < data->fannr; i++) {
1616 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1617 data->fan_status[i] =
1618 pc87360_read_value(data, LD_FAN,
1619 NO_BANK, PC87360_REG_FAN_STATUS(i));
1620 data->fan[i] = pc87360_read_value(data, LD_FAN,
1621 NO_BANK, PC87360_REG_FAN(i));
1622 data->fan_min[i] = pc87360_read_value(data,
1623 LD_FAN, NO_BANK,
1624 PC87360_REG_FAN_MIN(i));
1625 /* Change clock divider if needed */
1626 pc87360_autodiv(dev, i);
1627 /* Clear bits and write new divider */
1628 pc87360_write_value(data, LD_FAN, NO_BANK,
1629 PC87360_REG_FAN_STATUS(i),
1630 data->fan_status[i]);
1631 }
1632 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1633 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1634 NO_BANK, PC87360_REG_PWM(i));
1635 }
1636
1637 /* Voltages */
1638 for (i = 0; i < data->innr; i++) {
1639 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1640 PC87365_REG_IN_STATUS);
1641 /* Clear bits */
1642 pc87360_write_value(data, LD_IN, i,
1643 PC87365_REG_IN_STATUS,
1644 data->in_status[i]);
1645 if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
1646 data->in[i] = pc87360_read_value(data, LD_IN,
1647 i, PC87365_REG_IN);
1648 }
1649 if (data->in_status[i] & CHAN_ENA) {
1650 data->in_min[i] = pc87360_read_value(data,
1651 LD_IN, i,
1652 PC87365_REG_IN_MIN);
1653 data->in_max[i] = pc87360_read_value(data,
1654 LD_IN, i,
1655 PC87365_REG_IN_MAX);
1656 if (i >= 11)
1657 data->in_crit[i-11] =
1658 pc87360_read_value(data, LD_IN,
1659 i, PC87365_REG_TEMP_CRIT);
1660 }
1661 }
1662 if (data->innr) {
1663 data->in_alarms = pc87360_read_value(data, LD_IN,
1664 NO_BANK, PC87365_REG_IN_ALARMS1)
1665 | ((pc87360_read_value(data, LD_IN,
1666 NO_BANK, PC87365_REG_IN_ALARMS2)
1667 & 0x07) << 8);
1668 data->vid = (data->vid_conf & 0xE0) ?
1669 pc87360_read_value(data, LD_IN,
1670 NO_BANK, PC87365_REG_VID) : 0x1F;
1671 }
1672
1673 /* Temperatures */
1674 for (i = 0; i < data->tempnr; i++) {
1675 data->temp_status[i] = pc87360_read_value(data,
1676 LD_TEMP, i,
1677 PC87365_REG_TEMP_STATUS);
1678 /* Clear bits */
1679 pc87360_write_value(data, LD_TEMP, i,
1680 PC87365_REG_TEMP_STATUS,
1681 data->temp_status[i]);
1682 if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
1683 data->temp[i] = pc87360_read_value(data,
1684 LD_TEMP, i,
1685 PC87365_REG_TEMP);
1686 }
1687 if (data->temp_status[i] & CHAN_ENA) {
1688 data->temp_min[i] = pc87360_read_value(data,
1689 LD_TEMP, i,
1690 PC87365_REG_TEMP_MIN);
1691 data->temp_max[i] = pc87360_read_value(data,
1692 LD_TEMP, i,
1693 PC87365_REG_TEMP_MAX);
1694 data->temp_crit[i] = pc87360_read_value(data,
1695 LD_TEMP, i,
1696 PC87365_REG_TEMP_CRIT);
1697 }
1698 }
1699 if (data->tempnr) {
1700 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1701 NO_BANK, PC87365_REG_TEMP_ALARMS)
1702 & 0x3F;
1703 }
1704
1705 data->last_updated = jiffies;
1706 data->valid = 1;
1707 }
1708
1709 mutex_unlock(&data->update_lock);
1710
1711 return data;
1712}
1713
1714static int __init pc87360_device_add(unsigned short address)
1715{
1716 struct resource res[3];
1717 int err, i, res_count;
1718
1719 pdev = platform_device_alloc("pc87360", address);
1720 if (!pdev) {
1721 err = -ENOMEM;
1722 pr_err("Device allocation failed\n");
1723 goto exit;
1724 }
1725
1726 memset(res, 0, 3 * sizeof(struct resource));
1727 res_count = 0;
1728 for (i = 0; i < 3; i++) {
1729 if (!extra_isa[i])
1730 continue;
1731 res[res_count].start = extra_isa[i];
1732 res[res_count].end = extra_isa[i] + PC87360_EXTENT - 1;
1733 res[res_count].name = "pc87360",
1734 res[res_count].flags = IORESOURCE_IO,
1735
1736 err = acpi_check_resource_conflict(&res[res_count]);
1737 if (err)
1738 goto exit_device_put;
1739
1740 res_count++;
1741 }
1742
1743 err = platform_device_add_resources(pdev, res, res_count);
1744 if (err) {
1745 pr_err("Device resources addition failed (%d)\n", err);
1746 goto exit_device_put;
1747 }
1748
1749 err = platform_device_add(pdev);
1750 if (err) {
1751 pr_err("Device addition failed (%d)\n", err);
1752 goto exit_device_put;
1753 }
1754
1755 return 0;
1756
1757exit_device_put:
1758 platform_device_put(pdev);
1759exit:
1760 return err;
1761}
1762
1763static int __init pc87360_init(void)
1764{
1765 int err, i;
1766 unsigned short address = 0;
1767
1768 if (pc87360_find(0x2e, &devid, extra_isa)
1769 && pc87360_find(0x4e, &devid, extra_isa)) {
1770 pr_warn("PC8736x not detected, module not inserted\n");
1771 return -ENODEV;
1772 }
1773
1774 /* Arbitrarily pick one of the addresses */
1775 for (i = 0; i < 3; i++) {
1776 if (extra_isa[i] != 0x0000) {
1777 address = extra_isa[i];
1778 break;
1779 }
1780 }
1781
1782 if (address == 0x0000) {
1783 pr_warn("No active logical device, module not inserted\n");
1784 return -ENODEV;
1785 }
1786
1787 err = platform_driver_register(&pc87360_driver);
1788 if (err)
1789 goto exit;
1790
1791 /* Sets global pdev as a side effect */
1792 err = pc87360_device_add(address);
1793 if (err)
1794 goto exit_driver;
1795
1796 return 0;
1797
1798 exit_driver:
1799 platform_driver_unregister(&pc87360_driver);
1800 exit:
1801 return err;
1802}
1803
1804static void __exit pc87360_exit(void)
1805{
1806 platform_device_unregister(pdev);
1807 platform_driver_unregister(&pc87360_driver);
1808}
1809
1810
1811MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1812MODULE_DESCRIPTION("PC8736x hardware monitor");
1813MODULE_LICENSE("GPL");
1814
1815module_init(pc87360_init);
1816module_exit(pc87360_exit);