blob: b54637efc879174edb89a623f204c2e7f14cc84f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * pc87360.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
Jean Delvaref641b582007-06-09 10:11:16 -04004 * Copyright (C) 2004, 2007 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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
Joe Perches9e991c62011-01-12 21:55:11 +010036#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/slab.h>
41#include <linux/jiffies.h>
Jean Delvaref641b582007-06-09 10:11:16 -040042#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040043#include <linux/hwmon.h>
Jim Cromief0986bd2005-09-02 22:52:43 +020044#include <linux/hwmon-sysfs.h>
Jean Delvare303760b2005-07-31 21:52:01 +020045#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040046#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010047#include <linux/mutex.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010048#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020049#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static u8 devid;
Jean Delvaref641b582007-06-09 10:11:16 -040052static struct platform_device *pdev;
Jean Delvare2d8672c2005-07-19 23:56:35 +020053static unsigned short extra_isa[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static u8 confreg[4];
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static 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
Jean Delvare67b671b2007-12-06 23:13:42 +010065static unsigned short force_id;
66module_param(force_id, ushort, 0);
67MODULE_PARM_DESC(force_id, "Override the detected device ID");
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
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 */
Jim Cromie2a32ec22008-10-18 20:27:33 -070081#define LDNI_MAX 3
82static const u8 logdev[LDNI_MAX] = { FSCM, VLM, TMS };
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
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/*
Jean Delvaref641b582007-06-09 10:11:16 -0400186 * Device data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
188
189struct pc87360_data {
Jean Delvaref641b582007-06-09 10:11:16 -0400190 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700191 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100192 struct mutex lock;
193 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 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
Jean Delvaref641b582007-06-09 10:11:16 -0400230static int pc87360_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200231static int __devexit pc87360_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
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);
Jean Delvaref641b582007-06-09 10:11:16 -0400237static void pc87360_init_device(struct platform_device *pdev,
238 int use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static struct pc87360_data *pc87360_update_device(struct device *dev);
240
241/*
Jean Delvaref641b582007-06-09 10:11:16 -0400242 * Driver data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
244
Jean Delvaref641b582007-06-09 10:11:16 -0400245static struct platform_driver pc87360_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100246 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200247 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100248 .name = "pc87360",
249 },
Jean Delvaref641b582007-06-09 10:11:16 -0400250 .probe = pc87360_probe,
251 .remove = __devexit_p(pc87360_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252};
253
254/*
255 * Sysfs stuff
256 */
257
Jim Cromief0986bd2005-09-02 22:52:43 +0200258static ssize_t show_fan_input(struct device *dev, 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);
Jim Cromie694fa052005-09-02 22:57:52 +0200262 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
263 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200264}
265static ssize_t show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
266{
267 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
268 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200269 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
270 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200271}
272static ssize_t show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
273{
274 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
275 struct pc87360_data *data = pc87360_update_device(dev);
276 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200277 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200278}
279static ssize_t show_fan_status(struct device *dev, struct device_attribute *devattr, char *buf)
280{
281 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
282 struct pc87360_data *data = pc87360_update_device(dev);
283 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200284 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200285}
286static ssize_t set_fan_min(struct device *dev, struct device_attribute *devattr, const char *buf,
287 size_t count)
288{
289 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400290 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromie11be27e2005-09-02 23:05:07 +0200291 long fan_min = simple_strtol(buf, NULL, 10);
292
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100293 mutex_lock(&data->update_lock);
Jim Cromie11be27e2005-09-02 23:05:07 +0200294 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[attr->index]));
295
296 /* If it wouldn't fit, change clock divisor */
297 while (fan_min > 255
298 && (data->fan_status[attr->index] & 0x60) != 0x60) {
299 fan_min >>= 1;
300 data->fan[attr->index] >>= 1;
301 data->fan_status[attr->index] += 0x20;
302 }
303 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
304 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(attr->index),
305 data->fan_min[attr->index]);
306
307 /* Write new divider, preserve alarm bits */
308 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(attr->index),
309 data->fan_status[attr->index] & 0xF9);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100310 mutex_unlock(&data->update_lock);
Jim Cromie11be27e2005-09-02 23:05:07 +0200311
312 return count;
Jim Cromief0986bd2005-09-02 22:52:43 +0200313}
314
Jim Cromiededc6a72006-01-09 23:24:41 +0100315static struct sensor_device_attribute fan_input[] = {
316 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
317 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
318 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
319};
320static struct sensor_device_attribute fan_status[] = {
321 SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
322 SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
323 SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
324};
325static struct sensor_device_attribute fan_div[] = {
326 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
327 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
328 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
329};
330static struct sensor_device_attribute fan_min[] = {
331 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
332 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
333 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
334};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Jim Cromie941c5c02006-09-24 21:02:44 +0200336#define FAN_UNIT_ATTRS(X) \
337 &fan_input[X].dev_attr.attr, \
338 &fan_status[X].dev_attr.attr, \
339 &fan_div[X].dev_attr.attr, \
340 &fan_min[X].dev_attr.attr
341
Jim Cromief0986bd2005-09-02 22:52:43 +0200342static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
343{
344 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
345 struct pc87360_data *data = pc87360_update_device(dev);
346 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200347 PWM_FROM_REG(data->pwm[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200348 FAN_CONFIG_INVERT(data->fan_conf,
Jim Cromie694fa052005-09-02 22:57:52 +0200349 attr->index)));
Jim Cromief0986bd2005-09-02 22:52:43 +0200350}
351static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf,
352 size_t count)
353{
354 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400355 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200356 long val = simple_strtol(buf, NULL, 10);
357
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100358 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200359 data->pwm[attr->index] = PWM_TO_REG(val,
360 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
361 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
362 data->pwm[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100363 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200364 return count;
365}
366
Jim Cromiededc6a72006-01-09 23:24:41 +0100367static struct sensor_device_attribute pwm[] = {
368 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0),
369 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1),
370 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2),
371};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Jim Cromie941c5c02006-09-24 21:02:44 +0200373static struct attribute * pc8736x_fan_attr_array[] = {
374 FAN_UNIT_ATTRS(0),
375 FAN_UNIT_ATTRS(1),
376 FAN_UNIT_ATTRS(2),
377 &pwm[0].dev_attr.attr,
378 &pwm[1].dev_attr.attr,
379 &pwm[2].dev_attr.attr,
380 NULL
381};
382static const struct attribute_group pc8736x_fan_group = {
383 .attrs = pc8736x_fan_attr_array,
384};
385
Jim Cromief0986bd2005-09-02 22:52:43 +0200386static ssize_t show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
387{
388 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
389 struct pc87360_data *data = pc87360_update_device(dev);
390 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
391 data->in_vref));
392}
393static ssize_t show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
394{
395 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
396 struct pc87360_data *data = pc87360_update_device(dev);
397 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
398 data->in_vref));
399}
400static ssize_t show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
401{
402 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
403 struct pc87360_data *data = pc87360_update_device(dev);
404 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
405 data->in_vref));
406}
407static ssize_t show_in_status(struct device *dev, 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", data->in_status[attr->index]);
412}
413static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr, const char *buf,
414 size_t count)
415{
416 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400417 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200418 long val = simple_strtol(buf, NULL, 10);
419
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100420 mutex_lock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200421 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
422 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
423 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100424 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200425 return count;
426}
427static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr, const char *buf,
428 size_t count)
429{
430 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400431 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200432 long val = simple_strtol(buf, NULL, 10);
433
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100434 mutex_lock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200435 data->in_max[attr->index] = IN_TO_REG(val,
436 data->in_vref);
437 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
438 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100439 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200440 return count;
441}
442
Jim Cromiededc6a72006-01-09 23:24:41 +0100443static struct sensor_device_attribute in_input[] = {
444 SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
445 SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
446 SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
447 SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
448 SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
449 SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
450 SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
451 SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
452 SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
453 SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
454 SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
455};
456static struct sensor_device_attribute in_status[] = {
457 SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
458 SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
459 SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
460 SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
461 SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
462 SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
463 SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
464 SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
465 SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
466 SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
467 SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
468};
469static struct sensor_device_attribute in_min[] = {
470 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
471 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
472 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
473 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
474 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
475 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
476 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
477 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
478 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
479 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
480 SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
481};
482static struct sensor_device_attribute in_max[] = {
483 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
484 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
485 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
486 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
487 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
488 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
489 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
490 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
491 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
492 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
493 SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
494};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Jim Cromie28f74e72008-10-18 20:27:30 -0700496/* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
497#define CHAN_ALM_MIN 0x02 /* min limit crossed */
498#define CHAN_ALM_MAX 0x04 /* max limit exceeded */
499#define TEMP_ALM_CRIT 0x08 /* temp crit exceeded (temp only) */
500
Guenter Roeck3af28612012-01-19 11:02:22 -0800501/*
502 * show_in_min/max_alarm() reads data from the per-channel status
503 * register (sec 11.5.12), not the vin event status registers (sec
504 * 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms)
505 */
Jim Cromie492e9652008-10-18 20:27:32 -0700506
507static ssize_t show_in_min_alarm(struct device *dev,
508 struct device_attribute *devattr, char *buf)
509{
510 struct pc87360_data *data = pc87360_update_device(dev);
511 unsigned nr = to_sensor_dev_attr(devattr)->index;
512
513 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
514}
515static ssize_t show_in_max_alarm(struct device *dev,
516 struct device_attribute *devattr, char *buf)
517{
518 struct pc87360_data *data = pc87360_update_device(dev);
519 unsigned nr = to_sensor_dev_attr(devattr)->index;
520
521 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
522}
523
524static struct sensor_device_attribute in_min_alarm[] = {
525 SENSOR_ATTR(in0_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 0),
526 SENSOR_ATTR(in1_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 1),
527 SENSOR_ATTR(in2_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 2),
528 SENSOR_ATTR(in3_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 3),
529 SENSOR_ATTR(in4_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 4),
530 SENSOR_ATTR(in5_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 5),
531 SENSOR_ATTR(in6_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 6),
532 SENSOR_ATTR(in7_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 7),
533 SENSOR_ATTR(in8_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 8),
534 SENSOR_ATTR(in9_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 9),
535 SENSOR_ATTR(in10_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 10),
536};
537static struct sensor_device_attribute in_max_alarm[] = {
538 SENSOR_ATTR(in0_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 0),
539 SENSOR_ATTR(in1_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 1),
540 SENSOR_ATTR(in2_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 2),
541 SENSOR_ATTR(in3_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 3),
542 SENSOR_ATTR(in4_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 4),
543 SENSOR_ATTR(in5_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 5),
544 SENSOR_ATTR(in6_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 6),
545 SENSOR_ATTR(in7_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 7),
546 SENSOR_ATTR(in8_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 8),
547 SENSOR_ATTR(in9_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 9),
548 SENSOR_ATTR(in10_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 10),
549};
550
Jim Cromie941c5c02006-09-24 21:02:44 +0200551#define VIN_UNIT_ATTRS(X) \
552 &in_input[X].dev_attr.attr, \
553 &in_status[X].dev_attr.attr, \
554 &in_min[X].dev_attr.attr, \
Jim Cromie492e9652008-10-18 20:27:32 -0700555 &in_max[X].dev_attr.attr, \
556 &in_min_alarm[X].dev_attr.attr, \
557 &in_max_alarm[X].dev_attr.attr
Jim Cromie941c5c02006-09-24 21:02:44 +0200558
Jim Cromie44646c12006-09-24 21:01:56 +0200559static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
560{
561 struct pc87360_data *data = pc87360_update_device(dev);
562 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
563}
564static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
565
566static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
567{
Jean Delvare90d66192007-10-08 18:24:35 +0200568 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromie44646c12006-09-24 21:01:56 +0200569 return sprintf(buf, "%u\n", data->vrm);
570}
571static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
572{
Jean Delvaref641b582007-06-09 10:11:16 -0400573 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromie44646c12006-09-24 21:01:56 +0200574 data->vrm = simple_strtoul(buf, NULL, 10);
575 return count;
576}
577static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
578
579static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
580{
581 struct pc87360_data *data = pc87360_update_device(dev);
582 return sprintf(buf, "%u\n", data->in_alarms);
583}
584static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
585
Jim Cromie941c5c02006-09-24 21:02:44 +0200586static struct attribute *pc8736x_vin_attr_array[] = {
587 VIN_UNIT_ATTRS(0),
588 VIN_UNIT_ATTRS(1),
589 VIN_UNIT_ATTRS(2),
590 VIN_UNIT_ATTRS(3),
591 VIN_UNIT_ATTRS(4),
592 VIN_UNIT_ATTRS(5),
593 VIN_UNIT_ATTRS(6),
594 VIN_UNIT_ATTRS(7),
595 VIN_UNIT_ATTRS(8),
596 VIN_UNIT_ATTRS(9),
597 VIN_UNIT_ATTRS(10),
598 &dev_attr_cpu0_vid.attr,
599 &dev_attr_vrm.attr,
600 &dev_attr_alarms_in.attr,
601 NULL
602};
603static const struct attribute_group pc8736x_vin_group = {
604 .attrs = pc8736x_vin_attr_array,
605};
606
Jim Cromief0986bd2005-09-02 22:52:43 +0200607static ssize_t show_therm_input(struct device *dev, struct device_attribute *devattr, char *buf)
608{
609 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
610 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200611 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200612 data->in_vref));
613}
614static ssize_t show_therm_min(struct device *dev, struct device_attribute *devattr, char *buf)
615{
616 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
617 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200618 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200619 data->in_vref));
620}
621static ssize_t show_therm_max(struct device *dev, struct device_attribute *devattr, char *buf)
622{
623 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
624 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200625 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200626 data->in_vref));
627}
628static ssize_t show_therm_crit(struct device *dev, struct device_attribute *devattr, char *buf)
629{
630 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
631 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200632 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
Jim Cromief0986bd2005-09-02 22:52:43 +0200633 data->in_vref));
634}
635static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
636{
637 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
638 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200639 return sprintf(buf, "%u\n", data->in_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200640}
641static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
642 size_t count)
643{
644 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400645 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200646 long val = simple_strtol(buf, NULL, 10);
647
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100648 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200649 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
650 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
651 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100652 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200653 return count;
654}
655static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
656 size_t count)
657{
658 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400659 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200660 long val = simple_strtol(buf, NULL, 10);
661
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100662 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200663 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
664 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
665 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100666 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200667 return count;
668}
669static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
670 size_t count)
671{
672 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400673 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200674 long val = simple_strtol(buf, NULL, 10);
675
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100676 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200677 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
678 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
679 data->in_crit[attr->index-11]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100680 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200681 return count;
682}
683
Guenter Roeck3af28612012-01-19 11:02:22 -0800684/*
685 * the +11 term below reflects the fact that VLM units 11,12,13 are
686 * used in the chip to measure voltage across the thermistors
687 */
Jim Cromiededc6a72006-01-09 23:24:41 +0100688static struct sensor_device_attribute therm_input[] = {
689 SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0+11),
690 SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1+11),
691 SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2+11),
692};
693static struct sensor_device_attribute therm_status[] = {
694 SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0+11),
695 SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1+11),
696 SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2+11),
697};
698static struct sensor_device_attribute therm_min[] = {
699 SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
700 show_therm_min, set_therm_min, 0+11),
701 SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
702 show_therm_min, set_therm_min, 1+11),
703 SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
704 show_therm_min, set_therm_min, 2+11),
705};
706static struct sensor_device_attribute therm_max[] = {
707 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
708 show_therm_max, set_therm_max, 0+11),
709 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
710 show_therm_max, set_therm_max, 1+11),
711 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
712 show_therm_max, set_therm_max, 2+11),
713};
714static struct sensor_device_attribute therm_crit[] = {
715 SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
716 show_therm_crit, set_therm_crit, 0+11),
717 SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
718 show_therm_crit, set_therm_crit, 1+11),
719 SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
720 show_therm_crit, set_therm_crit, 2+11),
721};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Guenter Roeck3af28612012-01-19 11:02:22 -0800723/*
724 * show_therm_min/max_alarm() reads data from the per-channel voltage
725 * status register (sec 11.5.12)
726 */
Jim Cromie865c2952008-10-18 20:27:35 -0700727
728static ssize_t show_therm_min_alarm(struct device *dev,
729 struct device_attribute *devattr, char *buf)
730{
731 struct pc87360_data *data = pc87360_update_device(dev);
732 unsigned nr = to_sensor_dev_attr(devattr)->index;
733
734 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
735}
736static ssize_t show_therm_max_alarm(struct device *dev,
737 struct device_attribute *devattr, char *buf)
738{
739 struct pc87360_data *data = pc87360_update_device(dev);
740 unsigned nr = to_sensor_dev_attr(devattr)->index;
741
742 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
743}
744static ssize_t show_therm_crit_alarm(struct device *dev,
745 struct device_attribute *devattr, char *buf)
746{
747 struct pc87360_data *data = pc87360_update_device(dev);
748 unsigned nr = to_sensor_dev_attr(devattr)->index;
749
750 return sprintf(buf, "%u\n", !!(data->in_status[nr] & TEMP_ALM_CRIT));
751}
752
753static struct sensor_device_attribute therm_min_alarm[] = {
754 SENSOR_ATTR(temp4_min_alarm, S_IRUGO,
755 show_therm_min_alarm, NULL, 0+11),
756 SENSOR_ATTR(temp5_min_alarm, S_IRUGO,
757 show_therm_min_alarm, NULL, 1+11),
758 SENSOR_ATTR(temp6_min_alarm, S_IRUGO,
759 show_therm_min_alarm, NULL, 2+11),
760};
761static struct sensor_device_attribute therm_max_alarm[] = {
762 SENSOR_ATTR(temp4_max_alarm, S_IRUGO,
763 show_therm_max_alarm, NULL, 0+11),
764 SENSOR_ATTR(temp5_max_alarm, S_IRUGO,
765 show_therm_max_alarm, NULL, 1+11),
766 SENSOR_ATTR(temp6_max_alarm, S_IRUGO,
767 show_therm_max_alarm, NULL, 2+11),
768};
769static struct sensor_device_attribute therm_crit_alarm[] = {
770 SENSOR_ATTR(temp4_crit_alarm, S_IRUGO,
771 show_therm_crit_alarm, NULL, 0+11),
772 SENSOR_ATTR(temp5_crit_alarm, S_IRUGO,
773 show_therm_crit_alarm, NULL, 1+11),
774 SENSOR_ATTR(temp6_crit_alarm, S_IRUGO,
775 show_therm_crit_alarm, NULL, 2+11),
776};
777
Jim Cromie941c5c02006-09-24 21:02:44 +0200778#define THERM_UNIT_ATTRS(X) \
779 &therm_input[X].dev_attr.attr, \
780 &therm_status[X].dev_attr.attr, \
781 &therm_min[X].dev_attr.attr, \
782 &therm_max[X].dev_attr.attr, \
Jim Cromie865c2952008-10-18 20:27:35 -0700783 &therm_crit[X].dev_attr.attr, \
784 &therm_min_alarm[X].dev_attr.attr, \
785 &therm_max_alarm[X].dev_attr.attr, \
786 &therm_crit_alarm[X].dev_attr.attr
Jim Cromie941c5c02006-09-24 21:02:44 +0200787
788static struct attribute * pc8736x_therm_attr_array[] = {
789 THERM_UNIT_ATTRS(0),
790 THERM_UNIT_ATTRS(1),
791 THERM_UNIT_ATTRS(2),
792 NULL
793};
794static const struct attribute_group pc8736x_therm_group = {
795 .attrs = pc8736x_therm_attr_array,
796};
797
Jim Cromief0986bd2005-09-02 22:52:43 +0200798static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
799{
800 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
801 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200802 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200803}
804static ssize_t show_temp_min(struct device *dev, struct device_attribute *devattr, char *buf)
805{
806 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
807 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200808 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200809}
810static ssize_t show_temp_max(struct device *dev, struct device_attribute *devattr, char *buf)
811{
812 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
813 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200814 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200815}
816static ssize_t show_temp_crit(struct device *dev, struct device_attribute *devattr, char *buf)
817{
818 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
819 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200820 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200821}
822static ssize_t show_temp_status(struct device *dev, struct device_attribute *devattr, char *buf)
823{
824 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
825 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200826 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200827}
828static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
829 size_t count)
830{
831 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400832 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200833 long val = simple_strtol(buf, NULL, 10);
834
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100835 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200836 data->temp_min[attr->index] = TEMP_TO_REG(val);
837 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
838 data->temp_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100839 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200840 return count;
841}
842static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
843 size_t count)
844{
845 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400846 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200847 long val = simple_strtol(buf, NULL, 10);
848
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100849 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200850 data->temp_max[attr->index] = TEMP_TO_REG(val);
851 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
852 data->temp_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100853 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200854 return count;
855}
856static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
857 size_t count)
858{
859 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400860 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200861 long val = simple_strtol(buf, NULL, 10);
862
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100863 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200864 data->temp_crit[attr->index] = TEMP_TO_REG(val);
865 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
866 data->temp_crit[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100867 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200868 return count;
869}
870
Jim Cromiededc6a72006-01-09 23:24:41 +0100871static struct sensor_device_attribute temp_input[] = {
872 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
873 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
874 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
875};
876static struct sensor_device_attribute temp_status[] = {
877 SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
878 SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
879 SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
880};
881static struct sensor_device_attribute temp_min[] = {
882 SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
883 show_temp_min, set_temp_min, 0),
884 SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
885 show_temp_min, set_temp_min, 1),
886 SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
887 show_temp_min, set_temp_min, 2),
888};
889static struct sensor_device_attribute temp_max[] = {
890 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
891 show_temp_max, set_temp_max, 0),
892 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
893 show_temp_max, set_temp_max, 1),
894 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
895 show_temp_max, set_temp_max, 2),
896};
897static struct sensor_device_attribute temp_crit[] = {
898 SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
899 show_temp_crit, set_temp_crit, 0),
900 SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
901 show_temp_crit, set_temp_crit, 1),
902 SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
903 show_temp_crit, set_temp_crit, 2),
904};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400906static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 struct pc87360_data *data = pc87360_update_device(dev);
909 return sprintf(buf, "%u\n", data->temp_alarms);
910}
911static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
912
Guenter Roeck3af28612012-01-19 11:02:22 -0800913/*
914 * show_temp_min/max_alarm() reads data from the per-channel status
915 * register (sec 12.3.7), not the temp event status registers (sec
916 * 12.3.2) that show_temp_alarm() reads (via data->temp_alarms)
917 */
Jim Cromieb267e8c2008-10-18 20:27:32 -0700918
919static ssize_t show_temp_min_alarm(struct device *dev,
920 struct device_attribute *devattr, char *buf)
921{
922 struct pc87360_data *data = pc87360_update_device(dev);
923 unsigned nr = to_sensor_dev_attr(devattr)->index;
924
925 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN));
926}
927static ssize_t show_temp_max_alarm(struct device *dev,
928 struct device_attribute *devattr, char *buf)
929{
930 struct pc87360_data *data = pc87360_update_device(dev);
931 unsigned nr = to_sensor_dev_attr(devattr)->index;
932
933 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX));
934}
935static ssize_t show_temp_crit_alarm(struct device *dev,
936 struct device_attribute *devattr, char *buf)
937{
938 struct pc87360_data *data = pc87360_update_device(dev);
939 unsigned nr = to_sensor_dev_attr(devattr)->index;
940
941 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT));
942}
943
944static struct sensor_device_attribute temp_min_alarm[] = {
945 SENSOR_ATTR(temp1_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 0),
946 SENSOR_ATTR(temp2_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 1),
947 SENSOR_ATTR(temp3_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 2),
948};
949static struct sensor_device_attribute temp_max_alarm[] = {
950 SENSOR_ATTR(temp1_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 0),
951 SENSOR_ATTR(temp2_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 1),
952 SENSOR_ATTR(temp3_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 2),
953};
954static struct sensor_device_attribute temp_crit_alarm[] = {
955 SENSOR_ATTR(temp1_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 0),
956 SENSOR_ATTR(temp2_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 1),
957 SENSOR_ATTR(temp3_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 2),
958};
959
960#define TEMP_FAULT 0x40 /* open diode */
961static ssize_t show_temp_fault(struct device *dev,
962 struct device_attribute *devattr, char *buf)
963{
964 struct pc87360_data *data = pc87360_update_device(dev);
965 unsigned nr = to_sensor_dev_attr(devattr)->index;
966
967 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT));
968}
969static struct sensor_device_attribute temp_fault[] = {
970 SENSOR_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0),
971 SENSOR_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1),
972 SENSOR_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2),
973};
974
Jim Cromie941c5c02006-09-24 21:02:44 +0200975#define TEMP_UNIT_ATTRS(X) \
976 &temp_input[X].dev_attr.attr, \
977 &temp_status[X].dev_attr.attr, \
978 &temp_min[X].dev_attr.attr, \
979 &temp_max[X].dev_attr.attr, \
Jim Cromieb267e8c2008-10-18 20:27:32 -0700980 &temp_crit[X].dev_attr.attr, \
981 &temp_min_alarm[X].dev_attr.attr, \
982 &temp_max_alarm[X].dev_attr.attr, \
983 &temp_crit_alarm[X].dev_attr.attr, \
984 &temp_fault[X].dev_attr.attr
Jim Cromie941c5c02006-09-24 21:02:44 +0200985
986static struct attribute * pc8736x_temp_attr_array[] = {
987 TEMP_UNIT_ATTRS(0),
988 TEMP_UNIT_ATTRS(1),
989 TEMP_UNIT_ATTRS(2),
990 /* include the few miscellaneous atts here */
991 &dev_attr_alarms_temp.attr,
992 NULL
993};
994static const struct attribute_group pc8736x_temp_group = {
995 .attrs = pc8736x_temp_attr_array,
996};
997
Jim Cromieb267e8c2008-10-18 20:27:32 -0700998static ssize_t show_name(struct device *dev,
999 struct device_attribute *devattr, char *buf)
Jean Delvaref641b582007-06-09 10:11:16 -04001000{
1001 struct pc87360_data *data = dev_get_drvdata(dev);
1002 return sprintf(buf, "%s\n", data->name);
1003}
1004static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006/*
1007 * Device detection, registration and update
1008 */
1009
Jean Delvaree6cfb3a2005-07-27 21:32:02 +02001010static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 u16 val;
1013 int i;
1014 int nrdev; /* logical device count */
1015
1016 /* No superio_enter */
1017
1018 /* Identify device */
Jean Delvare67b671b2007-12-06 23:13:42 +01001019 val = force_id ? force_id : superio_inb(sioaddr, DEVID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 switch (val) {
1021 case 0xE1: /* PC87360 */
1022 case 0xE8: /* PC87363 */
1023 case 0xE4: /* PC87364 */
1024 nrdev = 1;
1025 break;
1026 case 0xE5: /* PC87365 */
1027 case 0xE9: /* PC87366 */
1028 nrdev = 3;
1029 break;
1030 default:
1031 superio_exit(sioaddr);
1032 return -ENODEV;
1033 }
1034 /* Remember the device id */
1035 *devid = val;
1036
1037 for (i = 0; i < nrdev; i++) {
1038 /* select logical device */
1039 superio_outb(sioaddr, DEV, logdev[i]);
1040
1041 val = superio_inb(sioaddr, ACT);
1042 if (!(val & 0x01)) {
Joe Perches9e991c62011-01-12 21:55:11 +01001043 pr_info("Device 0x%02x not activated\n", logdev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 continue;
1045 }
1046
1047 val = (superio_inb(sioaddr, BASE) << 8)
1048 | superio_inb(sioaddr, BASE + 1);
1049 if (!val) {
Joe Perches9e991c62011-01-12 21:55:11 +01001050 pr_info("Base address not set for device 0x%02x\n",
1051 logdev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 continue;
1053 }
1054
Jean Delvare2d8672c2005-07-19 23:56:35 +02001055 addresses[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (i==0) { /* Fans */
1058 confreg[0] = superio_inb(sioaddr, 0xF0);
1059 confreg[1] = superio_inb(sioaddr, 0xF1);
1060
Joe Perches9e991c62011-01-12 21:55:11 +01001061 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 1,
1062 (confreg[0] >> 2) & 1, (confreg[0] >> 3) & 1,
1063 (confreg[0] >> 4) & 1);
1064 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 2,
1065 (confreg[0] >> 5) & 1, (confreg[0] >> 6) & 1,
1066 (confreg[0] >> 7) & 1);
1067 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 3,
1068 confreg[1] & 1, (confreg[1] >> 1) & 1,
1069 (confreg[1] >> 2) & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 } else if (i==1) { /* Voltages */
1071 /* Are we using thermistors? */
1072 if (*devid == 0xE9) { /* PC87366 */
Guenter Roeck3af28612012-01-19 11:02:22 -08001073 /*
1074 * These registers are not logical-device
1075 * specific, just that we won't need them if
1076 * we don't use the VLM device
1077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 confreg[2] = superio_inb(sioaddr, 0x2B);
1079 confreg[3] = superio_inb(sioaddr, 0x25);
1080
1081 if (confreg[2] & 0x40) {
Joe Perches9e991c62011-01-12 21:55:11 +01001082 pr_info("Using thermistors for "
1083 "temperature monitoring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085 if (confreg[3] & 0xE0) {
Joe Perches9e991c62011-01-12 21:55:11 +01001086 pr_info("VID inputs routed (mode %u)\n",
1087 confreg[3] >> 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089 }
1090 }
1091 }
1092
1093 superio_exit(sioaddr);
1094 return 0;
1095}
1096
Jean Delvaref641b582007-06-09 10:11:16 -04001097static int __devinit pc87360_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
1099 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 struct pc87360_data *data;
1101 int err = 0;
1102 const char *name = "pc87360";
1103 int use_thermistors = 0;
Jean Delvaref641b582007-06-09 10:11:16 -04001104 struct device *dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001106 if (!(data = kzalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 data->fannr = 2;
1110 data->innr = 0;
1111 data->tempnr = 0;
1112
1113 switch (devid) {
1114 case 0xe8:
1115 name = "pc87363";
1116 break;
1117 case 0xe4:
1118 name = "pc87364";
1119 data->fannr = 3;
1120 break;
1121 case 0xe5:
1122 name = "pc87365";
1123 data->fannr = extra_isa[0] ? 3 : 0;
1124 data->innr = extra_isa[1] ? 11 : 0;
1125 data->tempnr = extra_isa[2] ? 2 : 0;
1126 break;
1127 case 0xe9:
1128 name = "pc87366";
1129 data->fannr = extra_isa[0] ? 3 : 0;
1130 data->innr = extra_isa[1] ? 14 : 0;
1131 data->tempnr = extra_isa[2] ? 3 : 0;
1132 break;
1133 }
1134
Jean Delvaref641b582007-06-09 10:11:16 -04001135 data->name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 data->valid = 0;
Jean Delvaref641b582007-06-09 10:11:16 -04001137 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001138 mutex_init(&data->update_lock);
Jean Delvaref641b582007-06-09 10:11:16 -04001139 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Jim Cromie2a32ec22008-10-18 20:27:33 -07001141 for (i = 0; i < LDNI_MAX; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (((data->address[i] = extra_isa[i]))
1143 && !request_region(extra_isa[i], PC87360_EXTENT,
Laurent Riffardcdaf7932005-11-26 20:37:41 +01001144 pc87360_driver.driver.name)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001145 dev_err(dev, "Region 0x%x-0x%x already "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 "in use!\n", extra_isa[i],
1147 extra_isa[i]+PC87360_EXTENT-1);
1148 for (i--; i >= 0; i--)
1149 release_region(extra_isa[i], PC87360_EXTENT);
1150 err = -EBUSY;
1151 goto ERROR1;
1152 }
1153 }
1154
1155 /* Retrieve the fans configuration from Super-I/O space */
1156 if (data->fannr)
1157 data->fan_conf = confreg[0] | (confreg[1] << 8);
1158
Guenter Roeck3af28612012-01-19 11:02:22 -08001159 /*
1160 * Use the correct reference voltage
1161 * Unless both the VLM and the TMS logical devices agree to
1162 * use an external Vref, the internal one is used.
1163 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (data->innr) {
1165 i = pc87360_read_value(data, LD_IN, NO_BANK,
1166 PC87365_REG_IN_CONFIG);
1167 if (data->tempnr) {
1168 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
1169 PC87365_REG_TEMP_CONFIG);
1170 }
1171 data->in_vref = (i&0x02) ? 3025 : 2966;
Jean Delvaref641b582007-06-09 10:11:16 -04001172 dev_dbg(dev, "Using %s reference voltage\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 (i&0x02) ? "external" : "internal");
1174
1175 data->vid_conf = confreg[3];
Jim Cromie3d7f9a82006-12-12 18:18:28 +01001176 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
1178
1179 /* Fan clock dividers may be needed before any data is read */
1180 for (i = 0; i < data->fannr; i++) {
1181 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1182 data->fan_status[i] = pc87360_read_value(data,
1183 LD_FAN, NO_BANK,
1184 PC87360_REG_FAN_STATUS(i));
1185 }
1186
1187 if (init > 0) {
1188 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1189 use_thermistors = confreg[2] & 0x40;
1190
Jean Delvaref641b582007-06-09 10:11:16 -04001191 pc87360_init_device(pdev, use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
Jim Cromief3722d5b2006-09-24 21:03:25 +02001194 /* Register all-or-nothing sysfs groups */
1195
1196 if (data->innr &&
Jean Delvaref641b582007-06-09 10:11:16 -04001197 (err = sysfs_create_group(&dev->kobj,
Jim Cromief3722d5b2006-09-24 21:03:25 +02001198 &pc8736x_vin_group)))
1199 goto ERROR3;
1200
1201 if (data->innr == 14 &&
Jean Delvaref641b582007-06-09 10:11:16 -04001202 (err = sysfs_create_group(&dev->kobj,
Jim Cromief3722d5b2006-09-24 21:03:25 +02001203 &pc8736x_therm_group)))
1204 goto ERROR3;
1205
1206 /* create device attr-files for varying sysfs groups */
1207
1208 if (data->tempnr) {
1209 for (i = 0; i < data->tempnr; i++) {
1210 if ((err = device_create_file(dev,
1211 &temp_input[i].dev_attr))
1212 || (err = device_create_file(dev,
1213 &temp_min[i].dev_attr))
1214 || (err = device_create_file(dev,
1215 &temp_max[i].dev_attr))
1216 || (err = device_create_file(dev,
1217 &temp_crit[i].dev_attr))
1218 || (err = device_create_file(dev,
Jim Cromieb267e8c2008-10-18 20:27:32 -07001219 &temp_status[i].dev_attr))
1220 || (err = device_create_file(dev,
1221 &temp_min_alarm[i].dev_attr))
1222 || (err = device_create_file(dev,
1223 &temp_max_alarm[i].dev_attr))
1224 || (err = device_create_file(dev,
1225 &temp_crit_alarm[i].dev_attr))
1226 || (err = device_create_file(dev,
1227 &temp_fault[i].dev_attr)))
Jim Cromief3722d5b2006-09-24 21:03:25 +02001228 goto ERROR3;
1229 }
1230 if ((err = device_create_file(dev, &dev_attr_alarms_temp)))
1231 goto ERROR3;
1232 }
1233
1234 for (i = 0; i < data->fannr; i++) {
1235 if (FAN_CONFIG_MONITOR(data->fan_conf, i)
1236 && ((err = device_create_file(dev,
1237 &fan_input[i].dev_attr))
1238 || (err = device_create_file(dev,
1239 &fan_min[i].dev_attr))
1240 || (err = device_create_file(dev,
1241 &fan_div[i].dev_attr))
1242 || (err = device_create_file(dev,
1243 &fan_status[i].dev_attr))))
1244 goto ERROR3;
1245
1246 if (FAN_CONFIG_CONTROL(data->fan_conf, i)
1247 && (err = device_create_file(dev, &pwm[i].dev_attr)))
1248 goto ERROR3;
1249 }
1250
Jean Delvaref641b582007-06-09 10:11:16 -04001251 if ((err = device_create_file(dev, &dev_attr_name)))
1252 goto ERROR3;
1253
Tony Jones1beeffe2007-08-20 13:46:20 -07001254 data->hwmon_dev = hwmon_device_register(dev);
1255 if (IS_ERR(data->hwmon_dev)) {
1256 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001257 goto ERROR3;
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return 0;
1260
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001261ERROR3:
Jean Delvaref641b582007-06-09 10:11:16 -04001262 device_remove_file(dev, &dev_attr_name);
Jim Cromief3722d5b2006-09-24 21:03:25 +02001263 /* can still remove groups whose members were added individually */
Jean Delvaref641b582007-06-09 10:11:16 -04001264 sysfs_remove_group(&dev->kobj, &pc8736x_temp_group);
1265 sysfs_remove_group(&dev->kobj, &pc8736x_fan_group);
1266 sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1267 sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 for (i = 0; i < 3; i++) {
1269 if (data->address[i]) {
1270 release_region(data->address[i], PC87360_EXTENT);
1271 }
1272 }
1273ERROR1:
1274 kfree(data);
1275 return err;
1276}
1277
Jean Delvaref641b582007-06-09 10:11:16 -04001278static int __devexit pc87360_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Jean Delvaref641b582007-06-09 10:11:16 -04001280 struct pc87360_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int i;
1282
Tony Jones1beeffe2007-08-20 13:46:20 -07001283 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001284
Jean Delvaref641b582007-06-09 10:11:16 -04001285 device_remove_file(&pdev->dev, &dev_attr_name);
1286 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_temp_group);
1287 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_group);
1288 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_therm_group);
1289 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_vin_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 for (i = 0; i < 3; i++) {
1292 if (data->address[i]) {
1293 release_region(data->address[i], PC87360_EXTENT);
1294 }
1295 }
1296 kfree(data);
1297
1298 return 0;
1299}
1300
Guenter Roeck3af28612012-01-19 11:02:22 -08001301/*
1302 * ldi is the logical device index
1303 * bank is for voltages and temperatures only
1304 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1306 u8 reg)
1307{
1308 int res;
1309
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001310 mutex_lock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (bank != NO_BANK)
1312 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1313 res = inb_p(data->address[ldi] + reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001314 mutex_unlock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 return res;
1317}
1318
1319static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1320 u8 reg, u8 value)
1321{
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001322 mutex_lock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (bank != NO_BANK)
1324 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1325 outb_p(value, data->address[ldi] + reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001326 mutex_unlock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Jim Cromie28f74e72008-10-18 20:27:30 -07001329/* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
1330#define CHAN_CNVRTD 0x80 /* new data ready */
1331#define CHAN_ENA 0x01 /* enabled channel (temp or vin) */
1332#define CHAN_ALM_ENA 0x10 /* propagate to alarms-reg ?? (chk val!) */
1333#define CHAN_READY (CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
1334
Jim Cromieb267e8c2008-10-18 20:27:32 -07001335#define TEMP_OTS_OE 0x20 /* OTS Output Enable */
1336#define VIN_RW1C_MASK (CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN) /* 0x87 */
1337#define TEMP_RW1C_MASK (VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */
1338
Jean Delvaref641b582007-06-09 10:11:16 -04001339static void pc87360_init_device(struct platform_device *pdev,
1340 int use_thermistors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Jean Delvaref641b582007-06-09 10:11:16 -04001342 struct pc87360_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 int i, nr;
1344 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1345 const u8 init_temp[3] = { 2, 2, 1 };
1346 u8 reg;
1347
1348 if (init >= 2 && data->innr) {
1349 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1350 PC87365_REG_IN_CONVRATE);
Jean Delvaref641b582007-06-09 10:11:16 -04001351 dev_info(&pdev->dev, "VLM conversion set to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 "1s period, 160us delay\n");
1353 pc87360_write_value(data, LD_IN, NO_BANK,
1354 PC87365_REG_IN_CONVRATE,
1355 (reg & 0xC0) | 0x11);
1356 }
1357
1358 nr = data->innr < 11 ? data->innr : 11;
Jim Cromiededc6a72006-01-09 23:24:41 +01001359 for (i = 0; i < nr; i++) {
Jim Cromie8ca13672008-10-18 20:27:34 -07001360 reg = pc87360_read_value(data, LD_IN, i,
1361 PC87365_REG_IN_STATUS);
1362 dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (init >= init_in[i]) {
1364 /* Forcibly enable voltage channel */
Jim Cromie28f74e72008-10-18 20:27:30 -07001365 if (!(reg & CHAN_ENA)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001366 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 "enabling in%d\n", i);
1368 pc87360_write_value(data, LD_IN, i,
1369 PC87365_REG_IN_STATUS,
1370 (reg & 0x68) | 0x87);
1371 }
1372 }
1373 }
1374
Guenter Roeck3af28612012-01-19 11:02:22 -08001375 /*
1376 * We can't blindly trust the Super-I/O space configuration bit,
1377 * most BIOS won't set it properly
1378 */
Jim Cromie8ca13672008-10-18 20:27:34 -07001379 dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors);
Jim Cromiededc6a72006-01-09 23:24:41 +01001380 for (i = 11; i < data->innr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 reg = pc87360_read_value(data, LD_IN, i,
1382 PC87365_REG_TEMP_STATUS);
Jim Cromie28f74e72008-10-18 20:27:30 -07001383 use_thermistors = use_thermistors || (reg & CHAN_ENA);
Jim Cromie8ca13672008-10-18 20:27:34 -07001384 /* thermistors are temp[4-6], measured on vin[11-14] */
1385 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Jim Cromie8ca13672008-10-18 20:27:34 -07001387 dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 i = use_thermistors ? 2 : 0;
Jim Cromiededc6a72006-01-09 23:24:41 +01001390 for (; i < data->tempnr; i++) {
Jim Cromie8ca13672008-10-18 20:27:34 -07001391 reg = pc87360_read_value(data, LD_TEMP, i,
1392 PC87365_REG_TEMP_STATUS);
1393 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i+1, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (init >= init_temp[i]) {
1395 /* Forcibly enable temperature channel */
Jim Cromie28f74e72008-10-18 20:27:30 -07001396 if (!(reg & CHAN_ENA)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001397 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 "enabling temp%d\n", i+1);
1399 pc87360_write_value(data, LD_TEMP, i,
1400 PC87365_REG_TEMP_STATUS,
1401 0xCF);
1402 }
1403 }
1404 }
1405
1406 if (use_thermistors) {
Jim Cromiededc6a72006-01-09 23:24:41 +01001407 for (i = 11; i < data->innr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (init >= init_in[i]) {
Guenter Roeck3af28612012-01-19 11:02:22 -08001409 /*
1410 * The pin may already be used by thermal
1411 * diodes
1412 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 reg = pc87360_read_value(data, LD_TEMP,
1414 (i-11)/2, PC87365_REG_TEMP_STATUS);
Jim Cromie28f74e72008-10-18 20:27:30 -07001415 if (reg & CHAN_ENA) {
Jean Delvaref641b582007-06-09 10:11:16 -04001416 dev_dbg(&pdev->dev, "Skipping "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 "temp%d, pin already in use "
1418 "by temp%d\n", i-7, (i-11)/2);
1419 continue;
1420 }
1421
1422 /* Forcibly enable thermistor channel */
1423 reg = pc87360_read_value(data, LD_IN, i,
1424 PC87365_REG_IN_STATUS);
Jim Cromie28f74e72008-10-18 20:27:30 -07001425 if (!(reg & CHAN_ENA)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001426 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 "enabling temp%d\n", i-7);
1428 pc87360_write_value(data, LD_IN, i,
1429 PC87365_REG_TEMP_STATUS,
1430 (reg & 0x60) | 0x8F);
1431 }
1432 }
1433 }
1434 }
1435
1436 if (data->innr) {
1437 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1438 PC87365_REG_IN_CONFIG);
Jim Cromie8ca13672008-10-18 20:27:34 -07001439 dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg);
Jim Cromie28f74e72008-10-18 20:27:30 -07001440 if (reg & CHAN_ENA) {
Jean Delvaref641b582007-06-09 10:11:16 -04001441 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 "enabling monitoring (VLM)\n");
1443 pc87360_write_value(data, LD_IN, NO_BANK,
1444 PC87365_REG_IN_CONFIG,
1445 reg & 0xFE);
1446 }
1447 }
1448
1449 if (data->tempnr) {
1450 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1451 PC87365_REG_TEMP_CONFIG);
Jim Cromie8ca13672008-10-18 20:27:34 -07001452 dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg);
Jim Cromie28f74e72008-10-18 20:27:30 -07001453 if (reg & CHAN_ENA) {
Jean Delvaref641b582007-06-09 10:11:16 -04001454 dev_dbg(&pdev->dev, "Forcibly enabling "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 "monitoring (TMS)\n");
1456 pc87360_write_value(data, LD_TEMP, NO_BANK,
1457 PC87365_REG_TEMP_CONFIG,
1458 reg & 0xFE);
1459 }
1460
1461 if (init >= 2) {
1462 /* Chip config as documented by National Semi. */
1463 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
Guenter Roeck3af28612012-01-19 11:02:22 -08001464 /*
1465 * We voluntarily omit the bank here, in case the
1466 * sequence itself matters. It shouldn't be a problem,
1467 * since nobody else is supposed to access the
1468 * device at that point.
1469 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1471 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1472 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1473 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1474 }
1475 }
1476}
1477
Jean Delvaref641b582007-06-09 10:11:16 -04001478static void pc87360_autodiv(struct device *dev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Jean Delvaref641b582007-06-09 10:11:16 -04001480 struct pc87360_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 u8 old_min = data->fan_min[nr];
1482
1483 /* Increase clock divider if needed and possible */
1484 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1485 || (data->fan[nr] >= 224)) { /* next to overflow */
1486 if ((data->fan_status[nr] & 0x60) != 0x60) {
1487 data->fan_status[nr] += 0x20;
1488 data->fan_min[nr] >>= 1;
1489 data->fan[nr] >>= 1;
Jean Delvaref641b582007-06-09 10:11:16 -04001490 dev_dbg(dev, "Increasing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 "clock divider to %d for fan %d\n",
1492 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1493 }
1494 } else {
1495 /* Decrease clock divider if possible */
1496 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1497 && data->fan[nr] < 85 /* bad accuracy */
1498 && (data->fan_status[nr] & 0x60) != 0x00) {
1499 data->fan_status[nr] -= 0x20;
1500 data->fan_min[nr] <<= 1;
1501 data->fan[nr] <<= 1;
Jean Delvaref641b582007-06-09 10:11:16 -04001502 dev_dbg(dev, "Decreasing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 "clock divider to %d for fan %d\n",
1504 FAN_DIV_FROM_REG(data->fan_status[nr]),
1505 nr+1);
1506 }
1507 }
1508
1509 /* Write new fan min if it changed */
1510 if (old_min != data->fan_min[nr]) {
1511 pc87360_write_value(data, LD_FAN, NO_BANK,
1512 PC87360_REG_FAN_MIN(nr),
1513 data->fan_min[nr]);
1514 }
1515}
1516
1517static struct pc87360_data *pc87360_update_device(struct device *dev)
1518{
Jean Delvaref641b582007-06-09 10:11:16 -04001519 struct pc87360_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 u8 i;
1521
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001522 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
Jean Delvaref641b582007-06-09 10:11:16 -04001525 dev_dbg(dev, "Data update\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 /* Fans */
1528 for (i = 0; i < data->fannr; i++) {
1529 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1530 data->fan_status[i] =
1531 pc87360_read_value(data, LD_FAN,
1532 NO_BANK, PC87360_REG_FAN_STATUS(i));
1533 data->fan[i] = pc87360_read_value(data, LD_FAN,
1534 NO_BANK, PC87360_REG_FAN(i));
1535 data->fan_min[i] = pc87360_read_value(data,
1536 LD_FAN, NO_BANK,
1537 PC87360_REG_FAN_MIN(i));
1538 /* Change clock divider if needed */
Jean Delvaref641b582007-06-09 10:11:16 -04001539 pc87360_autodiv(dev, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 /* Clear bits and write new divider */
1541 pc87360_write_value(data, LD_FAN, NO_BANK,
1542 PC87360_REG_FAN_STATUS(i),
1543 data->fan_status[i]);
1544 }
1545 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1546 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1547 NO_BANK, PC87360_REG_PWM(i));
1548 }
1549
1550 /* Voltages */
1551 for (i = 0; i < data->innr; i++) {
1552 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1553 PC87365_REG_IN_STATUS);
1554 /* Clear bits */
1555 pc87360_write_value(data, LD_IN, i,
1556 PC87365_REG_IN_STATUS,
1557 data->in_status[i]);
Jim Cromie28f74e72008-10-18 20:27:30 -07001558 if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 data->in[i] = pc87360_read_value(data, LD_IN,
1560 i, PC87365_REG_IN);
1561 }
Jim Cromie28f74e72008-10-18 20:27:30 -07001562 if (data->in_status[i] & CHAN_ENA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 data->in_min[i] = pc87360_read_value(data,
1564 LD_IN, i,
1565 PC87365_REG_IN_MIN);
1566 data->in_max[i] = pc87360_read_value(data,
1567 LD_IN, i,
1568 PC87365_REG_IN_MAX);
1569 if (i >= 11)
1570 data->in_crit[i-11] =
1571 pc87360_read_value(data, LD_IN,
1572 i, PC87365_REG_TEMP_CRIT);
1573 }
1574 }
1575 if (data->innr) {
1576 data->in_alarms = pc87360_read_value(data, LD_IN,
1577 NO_BANK, PC87365_REG_IN_ALARMS1)
1578 | ((pc87360_read_value(data, LD_IN,
1579 NO_BANK, PC87365_REG_IN_ALARMS2)
1580 & 0x07) << 8);
1581 data->vid = (data->vid_conf & 0xE0) ?
1582 pc87360_read_value(data, LD_IN,
1583 NO_BANK, PC87365_REG_VID) : 0x1F;
1584 }
1585
1586 /* Temperatures */
1587 for (i = 0; i < data->tempnr; i++) {
1588 data->temp_status[i] = pc87360_read_value(data,
1589 LD_TEMP, i,
1590 PC87365_REG_TEMP_STATUS);
1591 /* Clear bits */
1592 pc87360_write_value(data, LD_TEMP, i,
1593 PC87365_REG_TEMP_STATUS,
1594 data->temp_status[i]);
Jim Cromie28f74e72008-10-18 20:27:30 -07001595 if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 data->temp[i] = pc87360_read_value(data,
1597 LD_TEMP, i,
1598 PC87365_REG_TEMP);
1599 }
Jim Cromie28f74e72008-10-18 20:27:30 -07001600 if (data->temp_status[i] & CHAN_ENA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 data->temp_min[i] = pc87360_read_value(data,
1602 LD_TEMP, i,
1603 PC87365_REG_TEMP_MIN);
1604 data->temp_max[i] = pc87360_read_value(data,
1605 LD_TEMP, i,
1606 PC87365_REG_TEMP_MAX);
1607 data->temp_crit[i] = pc87360_read_value(data,
1608 LD_TEMP, i,
1609 PC87365_REG_TEMP_CRIT);
1610 }
1611 }
1612 if (data->tempnr) {
1613 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1614 NO_BANK, PC87365_REG_TEMP_ALARMS)
1615 & 0x3F;
1616 }
1617
1618 data->last_updated = jiffies;
1619 data->valid = 1;
1620 }
1621
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001622 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 return data;
1625}
1626
Jean Delvaref641b582007-06-09 10:11:16 -04001627static int __init pc87360_device_add(unsigned short address)
1628{
Jean Delvareb9783dc2010-08-14 21:08:48 +02001629 struct resource res[3];
1630 int err, i, res_count;
Jean Delvaref641b582007-06-09 10:11:16 -04001631
1632 pdev = platform_device_alloc("pc87360", address);
1633 if (!pdev) {
1634 err = -ENOMEM;
Joe Perches9e991c62011-01-12 21:55:11 +01001635 pr_err("Device allocation failed\n");
Jean Delvaref641b582007-06-09 10:11:16 -04001636 goto exit;
1637 }
1638
Jean Delvareb9783dc2010-08-14 21:08:48 +02001639 memset(res, 0, 3 * sizeof(struct resource));
1640 res_count = 0;
Jean Delvaref641b582007-06-09 10:11:16 -04001641 for (i = 0; i < 3; i++) {
1642 if (!extra_isa[i])
1643 continue;
Jean Delvareb9783dc2010-08-14 21:08:48 +02001644 res[res_count].start = extra_isa[i];
1645 res[res_count].end = extra_isa[i] + PC87360_EXTENT - 1;
1646 res[res_count].name = "pc87360",
1647 res[res_count].flags = IORESOURCE_IO,
Jean Delvareb9acb642009-01-07 16:37:35 +01001648
Jean Delvareb9783dc2010-08-14 21:08:48 +02001649 err = acpi_check_resource_conflict(&res[res_count]);
Jean Delvareb9acb642009-01-07 16:37:35 +01001650 if (err)
1651 goto exit_device_put;
1652
Jean Delvareb9783dc2010-08-14 21:08:48 +02001653 res_count++;
1654 }
1655
1656 err = platform_device_add_resources(pdev, res, res_count);
1657 if (err) {
Joe Perches9e991c62011-01-12 21:55:11 +01001658 pr_err("Device resources addition failed (%d)\n", err);
Jean Delvareb9783dc2010-08-14 21:08:48 +02001659 goto exit_device_put;
Jean Delvaref641b582007-06-09 10:11:16 -04001660 }
1661
1662 err = platform_device_add(pdev);
1663 if (err) {
Joe Perches9e991c62011-01-12 21:55:11 +01001664 pr_err("Device addition failed (%d)\n", err);
Jean Delvaref641b582007-06-09 10:11:16 -04001665 goto exit_device_put;
1666 }
1667
1668 return 0;
1669
1670exit_device_put:
1671 platform_device_put(pdev);
1672exit:
1673 return err;
1674}
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676static int __init pc87360_init(void)
1677{
Jean Delvaref641b582007-06-09 10:11:16 -04001678 int err, i;
1679 unsigned short address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 if (pc87360_find(0x2e, &devid, extra_isa)
1682 && pc87360_find(0x4e, &devid, extra_isa)) {
Joe Perches9e991c62011-01-12 21:55:11 +01001683 pr_warn("PC8736x not detected, module not inserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return -ENODEV;
1685 }
1686
1687 /* Arbitrarily pick one of the addresses */
1688 for (i = 0; i < 3; i++) {
1689 if (extra_isa[i] != 0x0000) {
Jean Delvare2d8672c2005-07-19 23:56:35 +02001690 address = extra_isa[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 break;
1692 }
1693 }
1694
Jean Delvare2d8672c2005-07-19 23:56:35 +02001695 if (address == 0x0000) {
Joe Perches9e991c62011-01-12 21:55:11 +01001696 pr_warn("No active logical device, module not inserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return -ENODEV;
1698 }
1699
Jean Delvaref641b582007-06-09 10:11:16 -04001700 err = platform_driver_register(&pc87360_driver);
1701 if (err)
1702 goto exit;
1703
1704 /* Sets global pdev as a side effect */
1705 err = pc87360_device_add(address);
1706 if (err)
1707 goto exit_driver;
1708
1709 return 0;
1710
1711 exit_driver:
1712 platform_driver_unregister(&pc87360_driver);
1713 exit:
1714 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
1717static void __exit pc87360_exit(void)
1718{
Jean Delvaref641b582007-06-09 10:11:16 -04001719 platform_device_unregister(pdev);
1720 platform_driver_unregister(&pc87360_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
1723
1724MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1725MODULE_DESCRIPTION("PC8736x hardware monitor");
1726MODULE_LICENSE("GPL");
1727
1728module_init(pc87360_init);
1729module_exit(pc87360_exit);