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