blob: e35856bb79b4d69096a452360e3f7973c7b9c1f3 [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,
Guenter Roeck449a7a02012-01-14 21:49:53 -080059"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");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
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);
Guenter Roeck449a7a02012-01-14 21:49:53 -080091 outb(val, sioaddr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94static inline int superio_inb(int sioaddr, int reg)
95{
96 outb(reg, sioaddr);
Guenter Roeck449a7a02012-01-14 21:49:53 -080097 return inb(sioaddr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100static inline void superio_exit(int sioaddr)
101{
102 outb(0x02, sioaddr);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800103 outb(0x02, sioaddr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
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
Guenter Roeck449a7a02012-01-14 21:49:53 -0800125#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))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
131
Guenter Roeck449a7a02012-01-14 21:49:53 -0800132#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Guenter Roeck449a7a02012-01-14 21:49:53 -0800136#define PWM_FROM_REG(val, inv) ((inv) ? 255 - (val) : (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static 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
Guenter Roeck449a7a02012-01-14 21:49:53 -0800162#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))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
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);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500231static int 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,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500251 .remove = pc87360_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252};
253
254/*
255 * Sysfs stuff
256 */
257
Guenter Roeck449a7a02012-01-14 21:49:53 -0800258static ssize_t show_fan_input(struct device *dev,
259 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200260{
261 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
262 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200263 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
264 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200265}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800266static ssize_t show_fan_min(struct device *dev,
267 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200268{
269 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
270 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200271 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
272 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200273}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800274static ssize_t show_fan_div(struct device *dev,
275 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200276{
277 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
278 struct pc87360_data *data = pc87360_update_device(dev);
279 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200280 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200281}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800282static ssize_t show_fan_status(struct device *dev,
283 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200284{
285 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
286 struct pc87360_data *data = pc87360_update_device(dev);
287 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200288 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200289}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800290static ssize_t set_fan_min(struct device *dev,
291 struct device_attribute *devattr, const char *buf,
Jim Cromief0986bd2005-09-02 22:52:43 +0200292 size_t count)
293{
294 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400295 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800296 long fan_min;
297 int err;
298
299 err = kstrtol(buf, 10, &fan_min);
300 if (err)
301 return err;
Jim Cromie11be27e2005-09-02 23:05:07 +0200302
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100303 mutex_lock(&data->update_lock);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800304 fan_min = FAN_TO_REG(fan_min,
305 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
Jim Cromie11be27e2005-09-02 23:05:07 +0200306
307 /* If it wouldn't fit, change clock divisor */
308 while (fan_min > 255
309 && (data->fan_status[attr->index] & 0x60) != 0x60) {
310 fan_min >>= 1;
311 data->fan[attr->index] >>= 1;
312 data->fan_status[attr->index] += 0x20;
313 }
314 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
Guenter Roeck449a7a02012-01-14 21:49:53 -0800315 pc87360_write_value(data, LD_FAN, NO_BANK,
316 PC87360_REG_FAN_MIN(attr->index),
Jim Cromie11be27e2005-09-02 23:05:07 +0200317 data->fan_min[attr->index]);
318
319 /* Write new divider, preserve alarm bits */
Guenter Roeck449a7a02012-01-14 21:49:53 -0800320 pc87360_write_value(data, LD_FAN, NO_BANK,
321 PC87360_REG_FAN_STATUS(attr->index),
Jim Cromie11be27e2005-09-02 23:05:07 +0200322 data->fan_status[attr->index] & 0xF9);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100323 mutex_unlock(&data->update_lock);
Jim Cromie11be27e2005-09-02 23:05:07 +0200324
325 return count;
Jim Cromief0986bd2005-09-02 22:52:43 +0200326}
327
Jim Cromiededc6a72006-01-09 23:24:41 +0100328static struct sensor_device_attribute fan_input[] = {
329 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
330 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
331 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
332};
333static struct sensor_device_attribute fan_status[] = {
334 SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
335 SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
336 SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
337};
338static struct sensor_device_attribute fan_div[] = {
339 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
340 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
341 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
342};
343static struct sensor_device_attribute fan_min[] = {
344 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
345 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
346 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
347};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Guenter Roeckbce27782012-01-19 20:50:57 -0800349#define FAN_UNIT_ATTRS(X) \
350{ &fan_input[X].dev_attr.attr, \
Jim Cromie941c5c02006-09-24 21:02:44 +0200351 &fan_status[X].dev_attr.attr, \
352 &fan_div[X].dev_attr.attr, \
Guenter Roeckbce27782012-01-19 20:50:57 -0800353 &fan_min[X].dev_attr.attr, \
354 NULL \
355}
Jim Cromie941c5c02006-09-24 21:02:44 +0200356
Guenter Roeck449a7a02012-01-14 21:49:53 -0800357static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
358 char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200359{
360 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
361 struct pc87360_data *data = pc87360_update_device(dev);
362 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200363 PWM_FROM_REG(data->pwm[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200364 FAN_CONFIG_INVERT(data->fan_conf,
Jim Cromie694fa052005-09-02 22:57:52 +0200365 attr->index)));
Jim Cromief0986bd2005-09-02 22:52:43 +0200366}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800367static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
368 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200369{
370 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400371 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800372 long val;
373 int err;
374
375 err = kstrtol(buf, 10, &val);
376 if (err)
377 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200378
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100379 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200380 data->pwm[attr->index] = PWM_TO_REG(val,
381 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
382 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
383 data->pwm[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100384 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200385 return count;
386}
387
Jim Cromiededc6a72006-01-09 23:24:41 +0100388static struct sensor_device_attribute pwm[] = {
389 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0),
390 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1),
391 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2),
392};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Guenter Roeckbce27782012-01-19 20:50:57 -0800394static struct attribute *pc8736x_fan_attr[][5] = {
Jim Cromie941c5c02006-09-24 21:02:44 +0200395 FAN_UNIT_ATTRS(0),
396 FAN_UNIT_ATTRS(1),
Guenter Roeckbce27782012-01-19 20:50:57 -0800397 FAN_UNIT_ATTRS(2)
Jim Cromie941c5c02006-09-24 21:02:44 +0200398};
Guenter Roeckbce27782012-01-19 20:50:57 -0800399
400static const struct attribute_group pc8736x_fan_attr_group[] = {
401 { .attrs = pc8736x_fan_attr[0], },
402 { .attrs = pc8736x_fan_attr[1], },
403 { .attrs = pc8736x_fan_attr[2], },
Jim Cromie941c5c02006-09-24 21:02:44 +0200404};
405
Guenter Roeck449a7a02012-01-14 21:49:53 -0800406static ssize_t show_in_input(struct device *dev,
407 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200408{
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}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800414static ssize_t show_in_min(struct device *dev,
415 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200416{
417 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
418 struct pc87360_data *data = pc87360_update_device(dev);
419 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
420 data->in_vref));
421}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800422static ssize_t show_in_max(struct device *dev,
423 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200424{
425 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
426 struct pc87360_data *data = pc87360_update_device(dev);
427 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
428 data->in_vref));
429}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800430static ssize_t show_in_status(struct device *dev,
431 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200432{
433 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
434 struct pc87360_data *data = pc87360_update_device(dev);
435 return sprintf(buf, "%u\n", data->in_status[attr->index]);
436}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800437static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr,
438 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200439{
440 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400441 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800442 long val;
443 int err;
444
445 err = kstrtol(buf, 10, &val);
446 if (err)
447 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200448
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100449 mutex_lock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200450 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
451 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
452 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100453 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200454 return count;
455}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800456static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr,
457 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200458{
459 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400460 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800461 long val;
462 int err;
463
464 err = kstrtol(buf, 10, &val);
465 if (err)
466 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200467
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100468 mutex_lock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200469 data->in_max[attr->index] = IN_TO_REG(val,
470 data->in_vref);
471 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
472 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100473 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200474 return count;
475}
476
Jim Cromiededc6a72006-01-09 23:24:41 +0100477static struct sensor_device_attribute in_input[] = {
478 SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
479 SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
480 SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
481 SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
482 SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
483 SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
484 SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
485 SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
486 SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
487 SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
488 SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
489};
490static struct sensor_device_attribute in_status[] = {
491 SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
492 SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
493 SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
494 SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
495 SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
496 SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
497 SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
498 SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
499 SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
500 SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
501 SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
502};
503static struct sensor_device_attribute in_min[] = {
504 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
505 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
506 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
507 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
508 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
509 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
510 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
511 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
512 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
513 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
514 SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
515};
516static struct sensor_device_attribute in_max[] = {
517 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
518 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
519 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
520 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
521 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
522 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
523 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
524 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
525 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
526 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
527 SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
528};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Jim Cromie28f74e72008-10-18 20:27:30 -0700530/* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
531#define CHAN_ALM_MIN 0x02 /* min limit crossed */
532#define CHAN_ALM_MAX 0x04 /* max limit exceeded */
533#define TEMP_ALM_CRIT 0x08 /* temp crit exceeded (temp only) */
534
Guenter Roeck3af28612012-01-19 11:02:22 -0800535/*
536 * show_in_min/max_alarm() reads data from the per-channel status
537 * register (sec 11.5.12), not the vin event status registers (sec
538 * 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms)
539 */
Jim Cromie492e9652008-10-18 20:27:32 -0700540
541static ssize_t show_in_min_alarm(struct device *dev,
542 struct device_attribute *devattr, char *buf)
543{
544 struct pc87360_data *data = pc87360_update_device(dev);
545 unsigned nr = to_sensor_dev_attr(devattr)->index;
546
547 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
548}
549static ssize_t show_in_max_alarm(struct device *dev,
550 struct device_attribute *devattr, char *buf)
551{
552 struct pc87360_data *data = pc87360_update_device(dev);
553 unsigned nr = to_sensor_dev_attr(devattr)->index;
554
555 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
556}
557
558static struct sensor_device_attribute in_min_alarm[] = {
559 SENSOR_ATTR(in0_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 0),
560 SENSOR_ATTR(in1_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 1),
561 SENSOR_ATTR(in2_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 2),
562 SENSOR_ATTR(in3_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 3),
563 SENSOR_ATTR(in4_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 4),
564 SENSOR_ATTR(in5_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 5),
565 SENSOR_ATTR(in6_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 6),
566 SENSOR_ATTR(in7_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 7),
567 SENSOR_ATTR(in8_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 8),
568 SENSOR_ATTR(in9_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 9),
569 SENSOR_ATTR(in10_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 10),
570};
571static struct sensor_device_attribute in_max_alarm[] = {
572 SENSOR_ATTR(in0_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 0),
573 SENSOR_ATTR(in1_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 1),
574 SENSOR_ATTR(in2_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 2),
575 SENSOR_ATTR(in3_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 3),
576 SENSOR_ATTR(in4_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 4),
577 SENSOR_ATTR(in5_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 5),
578 SENSOR_ATTR(in6_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 6),
579 SENSOR_ATTR(in7_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 7),
580 SENSOR_ATTR(in8_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 8),
581 SENSOR_ATTR(in9_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 9),
582 SENSOR_ATTR(in10_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 10),
583};
584
Jim Cromie941c5c02006-09-24 21:02:44 +0200585#define VIN_UNIT_ATTRS(X) \
586 &in_input[X].dev_attr.attr, \
587 &in_status[X].dev_attr.attr, \
588 &in_min[X].dev_attr.attr, \
Jim Cromie492e9652008-10-18 20:27:32 -0700589 &in_max[X].dev_attr.attr, \
590 &in_min_alarm[X].dev_attr.attr, \
591 &in_max_alarm[X].dev_attr.attr
Jim Cromie941c5c02006-09-24 21:02:44 +0200592
Guenter Roeck449a7a02012-01-14 21:49:53 -0800593static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
594 char *buf)
Jim Cromie44646c12006-09-24 21:01:56 +0200595{
596 struct pc87360_data *data = pc87360_update_device(dev);
597 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
598}
599static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
600
Guenter Roeck449a7a02012-01-14 21:49:53 -0800601static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
602 char *buf)
Jim Cromie44646c12006-09-24 21:01:56 +0200603{
Jean Delvare90d66192007-10-08 18:24:35 +0200604 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromie44646c12006-09-24 21:01:56 +0200605 return sprintf(buf, "%u\n", data->vrm);
606}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800607static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
608 const char *buf, size_t count)
Jim Cromie44646c12006-09-24 21:01:56 +0200609{
Jean Delvaref641b582007-06-09 10:11:16 -0400610 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800611 unsigned long val;
612 int err;
613
614 err = kstrtoul(buf, 10, &val);
615 if (err)
616 return err;
617
618 data->vrm = val;
Jim Cromie44646c12006-09-24 21:01:56 +0200619 return count;
620}
621static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
622
Guenter Roeck449a7a02012-01-14 21:49:53 -0800623static ssize_t show_in_alarms(struct device *dev,
624 struct device_attribute *attr, char *buf)
Jim Cromie44646c12006-09-24 21:01:56 +0200625{
626 struct pc87360_data *data = pc87360_update_device(dev);
627 return sprintf(buf, "%u\n", data->in_alarms);
628}
629static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
630
Jim Cromie941c5c02006-09-24 21:02:44 +0200631static struct attribute *pc8736x_vin_attr_array[] = {
632 VIN_UNIT_ATTRS(0),
633 VIN_UNIT_ATTRS(1),
634 VIN_UNIT_ATTRS(2),
635 VIN_UNIT_ATTRS(3),
636 VIN_UNIT_ATTRS(4),
637 VIN_UNIT_ATTRS(5),
638 VIN_UNIT_ATTRS(6),
639 VIN_UNIT_ATTRS(7),
640 VIN_UNIT_ATTRS(8),
641 VIN_UNIT_ATTRS(9),
642 VIN_UNIT_ATTRS(10),
643 &dev_attr_cpu0_vid.attr,
644 &dev_attr_vrm.attr,
645 &dev_attr_alarms_in.attr,
646 NULL
647};
648static const struct attribute_group pc8736x_vin_group = {
649 .attrs = pc8736x_vin_attr_array,
650};
651
Guenter Roeck449a7a02012-01-14 21:49:53 -0800652static ssize_t show_therm_input(struct device *dev,
653 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200654{
655 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
656 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200657 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200658 data->in_vref));
659}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800660static ssize_t show_therm_min(struct device *dev,
661 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200662{
663 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
664 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200665 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200666 data->in_vref));
667}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800668static ssize_t show_therm_max(struct device *dev,
669 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200670{
671 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
672 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200673 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200674 data->in_vref));
675}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800676static ssize_t show_therm_crit(struct device *dev,
677 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200678{
679 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
680 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200681 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
Jim Cromief0986bd2005-09-02 22:52:43 +0200682 data->in_vref));
683}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800684static ssize_t show_therm_status(struct device *dev,
685 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200686{
687 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
688 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200689 return sprintf(buf, "%u\n", data->in_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200690}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800691
692static ssize_t set_therm_min(struct device *dev,
693 struct device_attribute *devattr,
694 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200695{
696 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400697 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800698 long val;
699 int err;
700
701 err = kstrtol(buf, 10, &val);
702 if (err)
703 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200704
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100705 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200706 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
707 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
708 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100709 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200710 return count;
711}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800712
713static ssize_t set_therm_max(struct device *dev,
714 struct device_attribute *devattr,
715 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200716{
717 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400718 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800719 long val;
720 int err;
721
722 err = kstrtol(buf, 10, &val);
723 if (err)
724 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200725
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100726 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200727 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
728 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
729 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100730 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200731 return count;
732}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800733static ssize_t set_therm_crit(struct device *dev,
734 struct device_attribute *devattr,
735 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200736{
737 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400738 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800739 long val;
740 int err;
741
742 err = kstrtol(buf, 10, &val);
743 if (err)
744 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200745
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100746 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200747 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
748 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
749 data->in_crit[attr->index-11]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100750 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200751 return count;
752}
753
Guenter Roeck3af28612012-01-19 11:02:22 -0800754/*
755 * the +11 term below reflects the fact that VLM units 11,12,13 are
756 * used in the chip to measure voltage across the thermistors
757 */
Jim Cromiededc6a72006-01-09 23:24:41 +0100758static struct sensor_device_attribute therm_input[] = {
Guenter Roeck449a7a02012-01-14 21:49:53 -0800759 SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0 + 11),
760 SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1 + 11),
761 SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100762};
763static struct sensor_device_attribute therm_status[] = {
Guenter Roeck449a7a02012-01-14 21:49:53 -0800764 SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0 + 11),
765 SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1 + 11),
766 SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100767};
768static struct sensor_device_attribute therm_min[] = {
769 SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800770 show_therm_min, set_therm_min, 0 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100771 SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800772 show_therm_min, set_therm_min, 1 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100773 SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800774 show_therm_min, set_therm_min, 2 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100775};
776static struct sensor_device_attribute therm_max[] = {
777 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800778 show_therm_max, set_therm_max, 0 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100779 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800780 show_therm_max, set_therm_max, 1 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100781 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800782 show_therm_max, set_therm_max, 2 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100783};
784static struct sensor_device_attribute therm_crit[] = {
785 SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800786 show_therm_crit, set_therm_crit, 0 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100787 SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800788 show_therm_crit, set_therm_crit, 1 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100789 SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800790 show_therm_crit, set_therm_crit, 2 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100791};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Guenter Roeck3af28612012-01-19 11:02:22 -0800793/*
794 * show_therm_min/max_alarm() reads data from the per-channel voltage
795 * status register (sec 11.5.12)
796 */
Jim Cromie865c2952008-10-18 20:27:35 -0700797
798static ssize_t show_therm_min_alarm(struct device *dev,
799 struct device_attribute *devattr, char *buf)
800{
801 struct pc87360_data *data = pc87360_update_device(dev);
802 unsigned nr = to_sensor_dev_attr(devattr)->index;
803
804 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
805}
806static ssize_t show_therm_max_alarm(struct device *dev,
807 struct device_attribute *devattr, char *buf)
808{
809 struct pc87360_data *data = pc87360_update_device(dev);
810 unsigned nr = to_sensor_dev_attr(devattr)->index;
811
812 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
813}
814static ssize_t show_therm_crit_alarm(struct device *dev,
815 struct device_attribute *devattr, char *buf)
816{
817 struct pc87360_data *data = pc87360_update_device(dev);
818 unsigned nr = to_sensor_dev_attr(devattr)->index;
819
820 return sprintf(buf, "%u\n", !!(data->in_status[nr] & TEMP_ALM_CRIT));
821}
822
823static struct sensor_device_attribute therm_min_alarm[] = {
824 SENSOR_ATTR(temp4_min_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800825 show_therm_min_alarm, NULL, 0 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700826 SENSOR_ATTR(temp5_min_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800827 show_therm_min_alarm, NULL, 1 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700828 SENSOR_ATTR(temp6_min_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800829 show_therm_min_alarm, NULL, 2 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700830};
831static struct sensor_device_attribute therm_max_alarm[] = {
832 SENSOR_ATTR(temp4_max_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800833 show_therm_max_alarm, NULL, 0 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700834 SENSOR_ATTR(temp5_max_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800835 show_therm_max_alarm, NULL, 1 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700836 SENSOR_ATTR(temp6_max_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800837 show_therm_max_alarm, NULL, 2 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700838};
839static struct sensor_device_attribute therm_crit_alarm[] = {
840 SENSOR_ATTR(temp4_crit_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800841 show_therm_crit_alarm, NULL, 0 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700842 SENSOR_ATTR(temp5_crit_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800843 show_therm_crit_alarm, NULL, 1 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700844 SENSOR_ATTR(temp6_crit_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800845 show_therm_crit_alarm, NULL, 2 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700846};
847
Jim Cromie941c5c02006-09-24 21:02:44 +0200848#define THERM_UNIT_ATTRS(X) \
849 &therm_input[X].dev_attr.attr, \
850 &therm_status[X].dev_attr.attr, \
851 &therm_min[X].dev_attr.attr, \
852 &therm_max[X].dev_attr.attr, \
Jim Cromie865c2952008-10-18 20:27:35 -0700853 &therm_crit[X].dev_attr.attr, \
854 &therm_min_alarm[X].dev_attr.attr, \
855 &therm_max_alarm[X].dev_attr.attr, \
856 &therm_crit_alarm[X].dev_attr.attr
Jim Cromie941c5c02006-09-24 21:02:44 +0200857
Guenter Roeck449a7a02012-01-14 21:49:53 -0800858static struct attribute *pc8736x_therm_attr_array[] = {
Jim Cromie941c5c02006-09-24 21:02:44 +0200859 THERM_UNIT_ATTRS(0),
860 THERM_UNIT_ATTRS(1),
861 THERM_UNIT_ATTRS(2),
862 NULL
863};
864static const struct attribute_group pc8736x_therm_group = {
865 .attrs = pc8736x_therm_attr_array,
866};
867
Guenter Roeck449a7a02012-01-14 21:49:53 -0800868static ssize_t show_temp_input(struct device *dev,
869 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200870{
871 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
872 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200873 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200874}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800875
876static ssize_t show_temp_min(struct device *dev,
877 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200878{
879 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
880 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200881 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200882}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800883
884static ssize_t show_temp_max(struct device *dev,
885 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200886{
887 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
888 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200889 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200890}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800891
892static ssize_t show_temp_crit(struct device *dev,
893 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200894{
895 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
896 struct pc87360_data *data = pc87360_update_device(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800897 return sprintf(buf, "%d\n",
898 TEMP_FROM_REG(data->temp_crit[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200899}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800900
901static ssize_t show_temp_status(struct device *dev,
902 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200903{
904 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
905 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200906 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200907}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800908
909static ssize_t set_temp_min(struct device *dev,
910 struct device_attribute *devattr,
911 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200912{
913 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400914 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800915 long val;
916 int err;
917
918 err = kstrtol(buf, 10, &val);
919 if (err)
920 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200921
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100922 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200923 data->temp_min[attr->index] = TEMP_TO_REG(val);
924 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
925 data->temp_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100926 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200927 return count;
928}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800929
930static ssize_t set_temp_max(struct device *dev,
931 struct device_attribute *devattr,
932 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200933{
934 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400935 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800936 long val;
937 int err;
938
939 err = kstrtol(buf, 10, &val);
940 if (err)
941 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200942
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100943 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200944 data->temp_max[attr->index] = TEMP_TO_REG(val);
945 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
946 data->temp_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100947 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200948 return count;
949}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800950
951static ssize_t set_temp_crit(struct device *dev,
952 struct device_attribute *devattr, const char *buf,
953 size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200954{
955 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400956 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800957 long val;
958 int err;
959
960 err = kstrtol(buf, 10, &val);
961 if (err)
962 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200963
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100964 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200965 data->temp_crit[attr->index] = TEMP_TO_REG(val);
966 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
967 data->temp_crit[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100968 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200969 return count;
970}
971
Jim Cromiededc6a72006-01-09 23:24:41 +0100972static struct sensor_device_attribute temp_input[] = {
973 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
974 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
975 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
976};
977static struct sensor_device_attribute temp_status[] = {
978 SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
979 SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
980 SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
981};
982static struct sensor_device_attribute temp_min[] = {
983 SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
984 show_temp_min, set_temp_min, 0),
985 SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
986 show_temp_min, set_temp_min, 1),
987 SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
988 show_temp_min, set_temp_min, 2),
989};
990static struct sensor_device_attribute temp_max[] = {
991 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
992 show_temp_max, set_temp_max, 0),
993 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
994 show_temp_max, set_temp_max, 1),
995 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
996 show_temp_max, set_temp_max, 2),
997};
998static struct sensor_device_attribute temp_crit[] = {
999 SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
1000 show_temp_crit, set_temp_crit, 0),
1001 SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
1002 show_temp_crit, set_temp_crit, 1),
1003 SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
1004 show_temp_crit, set_temp_crit, 2),
1005};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Guenter Roeck449a7a02012-01-14 21:49:53 -08001007static ssize_t show_temp_alarms(struct device *dev,
1008 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 struct pc87360_data *data = pc87360_update_device(dev);
1011 return sprintf(buf, "%u\n", data->temp_alarms);
1012}
Guenter Roeck449a7a02012-01-14 21:49:53 -08001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
1015
Guenter Roeck3af28612012-01-19 11:02:22 -08001016/*
1017 * show_temp_min/max_alarm() reads data from the per-channel status
1018 * register (sec 12.3.7), not the temp event status registers (sec
1019 * 12.3.2) that show_temp_alarm() reads (via data->temp_alarms)
1020 */
Jim Cromieb267e8c2008-10-18 20:27:32 -07001021
1022static ssize_t show_temp_min_alarm(struct device *dev,
1023 struct device_attribute *devattr, char *buf)
1024{
1025 struct pc87360_data *data = pc87360_update_device(dev);
1026 unsigned nr = to_sensor_dev_attr(devattr)->index;
1027
1028 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN));
1029}
Guenter Roeck449a7a02012-01-14 21:49:53 -08001030
Jim Cromieb267e8c2008-10-18 20:27:32 -07001031static ssize_t show_temp_max_alarm(struct device *dev,
1032 struct device_attribute *devattr, char *buf)
1033{
1034 struct pc87360_data *data = pc87360_update_device(dev);
1035 unsigned nr = to_sensor_dev_attr(devattr)->index;
1036
1037 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX));
1038}
Guenter Roeck449a7a02012-01-14 21:49:53 -08001039
Jim Cromieb267e8c2008-10-18 20:27:32 -07001040static ssize_t show_temp_crit_alarm(struct device *dev,
1041 struct device_attribute *devattr, char *buf)
1042{
1043 struct pc87360_data *data = pc87360_update_device(dev);
1044 unsigned nr = to_sensor_dev_attr(devattr)->index;
1045
1046 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT));
1047}
1048
1049static struct sensor_device_attribute temp_min_alarm[] = {
1050 SENSOR_ATTR(temp1_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 0),
1051 SENSOR_ATTR(temp2_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 1),
1052 SENSOR_ATTR(temp3_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 2),
1053};
Guenter Roeck449a7a02012-01-14 21:49:53 -08001054
Jim Cromieb267e8c2008-10-18 20:27:32 -07001055static struct sensor_device_attribute temp_max_alarm[] = {
1056 SENSOR_ATTR(temp1_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 0),
1057 SENSOR_ATTR(temp2_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 1),
1058 SENSOR_ATTR(temp3_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 2),
1059};
Guenter Roeck449a7a02012-01-14 21:49:53 -08001060
Jim Cromieb267e8c2008-10-18 20:27:32 -07001061static struct sensor_device_attribute temp_crit_alarm[] = {
1062 SENSOR_ATTR(temp1_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 0),
1063 SENSOR_ATTR(temp2_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 1),
1064 SENSOR_ATTR(temp3_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 2),
1065};
1066
1067#define TEMP_FAULT 0x40 /* open diode */
1068static ssize_t show_temp_fault(struct device *dev,
1069 struct device_attribute *devattr, char *buf)
1070{
1071 struct pc87360_data *data = pc87360_update_device(dev);
1072 unsigned nr = to_sensor_dev_attr(devattr)->index;
1073
1074 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT));
1075}
1076static struct sensor_device_attribute temp_fault[] = {
1077 SENSOR_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0),
1078 SENSOR_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1),
1079 SENSOR_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2),
1080};
1081
Guenter Roeckbce27782012-01-19 20:50:57 -08001082#define TEMP_UNIT_ATTRS(X) \
1083{ &temp_input[X].dev_attr.attr, \
1084 &temp_status[X].dev_attr.attr, \
1085 &temp_min[X].dev_attr.attr, \
1086 &temp_max[X].dev_attr.attr, \
1087 &temp_crit[X].dev_attr.attr, \
1088 &temp_min_alarm[X].dev_attr.attr, \
1089 &temp_max_alarm[X].dev_attr.attr, \
1090 &temp_crit_alarm[X].dev_attr.attr, \
1091 &temp_fault[X].dev_attr.attr, \
1092 NULL \
1093}
Jim Cromie941c5c02006-09-24 21:02:44 +02001094
Guenter Roeckbce27782012-01-19 20:50:57 -08001095static struct attribute *pc8736x_temp_attr[][10] = {
Jim Cromie941c5c02006-09-24 21:02:44 +02001096 TEMP_UNIT_ATTRS(0),
1097 TEMP_UNIT_ATTRS(1),
Guenter Roeckbce27782012-01-19 20:50:57 -08001098 TEMP_UNIT_ATTRS(2)
Jim Cromie941c5c02006-09-24 21:02:44 +02001099};
Guenter Roeck449a7a02012-01-14 21:49:53 -08001100
Guenter Roeckbce27782012-01-19 20:50:57 -08001101static const struct attribute_group pc8736x_temp_attr_group[] = {
1102 { .attrs = pc8736x_temp_attr[0] },
1103 { .attrs = pc8736x_temp_attr[1] },
1104 { .attrs = pc8736x_temp_attr[2] }
Jim Cromie941c5c02006-09-24 21:02:44 +02001105};
1106
Jim Cromieb267e8c2008-10-18 20:27:32 -07001107static ssize_t show_name(struct device *dev,
1108 struct device_attribute *devattr, char *buf)
Jean Delvaref641b582007-06-09 10:11:16 -04001109{
1110 struct pc87360_data *data = dev_get_drvdata(dev);
1111 return sprintf(buf, "%s\n", data->name);
1112}
Guenter Roeck449a7a02012-01-14 21:49:53 -08001113
Jean Delvaref641b582007-06-09 10:11:16 -04001114static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/*
1117 * Device detection, registration and update
1118 */
1119
Guenter Roeck449a7a02012-01-14 21:49:53 -08001120static int __init pc87360_find(int sioaddr, u8 *devid,
1121 unsigned short *addresses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 u16 val;
1124 int i;
1125 int nrdev; /* logical device count */
1126
1127 /* No superio_enter */
1128
1129 /* Identify device */
Jean Delvare67b671b2007-12-06 23:13:42 +01001130 val = force_id ? force_id : superio_inb(sioaddr, DEVID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 switch (val) {
1132 case 0xE1: /* PC87360 */
1133 case 0xE8: /* PC87363 */
1134 case 0xE4: /* PC87364 */
1135 nrdev = 1;
1136 break;
1137 case 0xE5: /* PC87365 */
1138 case 0xE9: /* PC87366 */
1139 nrdev = 3;
1140 break;
1141 default:
1142 superio_exit(sioaddr);
1143 return -ENODEV;
1144 }
1145 /* Remember the device id */
1146 *devid = val;
1147
1148 for (i = 0; i < nrdev; i++) {
1149 /* select logical device */
1150 superio_outb(sioaddr, DEV, logdev[i]);
1151
1152 val = superio_inb(sioaddr, ACT);
1153 if (!(val & 0x01)) {
Joe Perches9e991c62011-01-12 21:55:11 +01001154 pr_info("Device 0x%02x not activated\n", logdev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 continue;
1156 }
1157
1158 val = (superio_inb(sioaddr, BASE) << 8)
1159 | superio_inb(sioaddr, BASE + 1);
1160 if (!val) {
Joe Perches9e991c62011-01-12 21:55:11 +01001161 pr_info("Base address not set for device 0x%02x\n",
1162 logdev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 continue;
1164 }
1165
Jean Delvare2d8672c2005-07-19 23:56:35 +02001166 addresses[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Guenter Roeck449a7a02012-01-14 21:49:53 -08001168 if (i == 0) { /* Fans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 confreg[0] = superio_inb(sioaddr, 0xF0);
1170 confreg[1] = superio_inb(sioaddr, 0xF1);
1171
Joe Perches9e991c62011-01-12 21:55:11 +01001172 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 1,
1173 (confreg[0] >> 2) & 1, (confreg[0] >> 3) & 1,
1174 (confreg[0] >> 4) & 1);
1175 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 2,
1176 (confreg[0] >> 5) & 1, (confreg[0] >> 6) & 1,
1177 (confreg[0] >> 7) & 1);
1178 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 3,
1179 confreg[1] & 1, (confreg[1] >> 1) & 1,
1180 (confreg[1] >> 2) & 1);
Guenter Roeck449a7a02012-01-14 21:49:53 -08001181 } else if (i == 1) { /* Voltages */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 /* Are we using thermistors? */
1183 if (*devid == 0xE9) { /* PC87366 */
Guenter Roeck3af28612012-01-19 11:02:22 -08001184 /*
1185 * These registers are not logical-device
1186 * specific, just that we won't need them if
1187 * we don't use the VLM device
1188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 confreg[2] = superio_inb(sioaddr, 0x2B);
1190 confreg[3] = superio_inb(sioaddr, 0x25);
1191
1192 if (confreg[2] & 0x40) {
Joe Perches9e991c62011-01-12 21:55:11 +01001193 pr_info("Using thermistors for "
1194 "temperature monitoring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196 if (confreg[3] & 0xE0) {
Joe Perches9e991c62011-01-12 21:55:11 +01001197 pr_info("VID inputs routed (mode %u)\n",
1198 confreg[3] >> 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200 }
1201 }
1202 }
1203
1204 superio_exit(sioaddr);
1205 return 0;
1206}
1207
Guenter Roeckbce27782012-01-19 20:50:57 -08001208static void pc87360_remove_files(struct device *dev)
1209{
1210 int i;
1211
1212 device_remove_file(dev, &dev_attr_name);
1213 device_remove_file(dev, &dev_attr_alarms_temp);
1214 for (i = 0; i < ARRAY_SIZE(pc8736x_temp_attr_group); i++)
1215 sysfs_remove_group(&dev->kobj, &pc8736x_temp_attr_group[i]);
1216 for (i = 0; i < ARRAY_SIZE(pc8736x_fan_attr_group); i++) {
1217 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_attr_group[i]);
1218 device_remove_file(dev, &pwm[i].dev_attr);
1219 }
1220 sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1221 sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
1222}
1223
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001224static int pc87360_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 struct pc87360_data *data;
1228 int err = 0;
1229 const char *name = "pc87360";
1230 int use_thermistors = 0;
Jean Delvaref641b582007-06-09 10:11:16 -04001231 struct device *dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Guenter Roeck61d4d172012-06-02 11:20:19 -07001233 data = devm_kzalloc(dev, sizeof(struct pc87360_data), GFP_KERNEL);
Guenter Roeck449a7a02012-01-14 21:49:53 -08001234 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 data->fannr = 2;
1238 data->innr = 0;
1239 data->tempnr = 0;
1240
1241 switch (devid) {
1242 case 0xe8:
1243 name = "pc87363";
1244 break;
1245 case 0xe4:
1246 name = "pc87364";
1247 data->fannr = 3;
1248 break;
1249 case 0xe5:
1250 name = "pc87365";
1251 data->fannr = extra_isa[0] ? 3 : 0;
1252 data->innr = extra_isa[1] ? 11 : 0;
1253 data->tempnr = extra_isa[2] ? 2 : 0;
1254 break;
1255 case 0xe9:
1256 name = "pc87366";
1257 data->fannr = extra_isa[0] ? 3 : 0;
1258 data->innr = extra_isa[1] ? 14 : 0;
1259 data->tempnr = extra_isa[2] ? 3 : 0;
1260 break;
1261 }
1262
Jean Delvaref641b582007-06-09 10:11:16 -04001263 data->name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 data->valid = 0;
Jean Delvaref641b582007-06-09 10:11:16 -04001265 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001266 mutex_init(&data->update_lock);
Jean Delvaref641b582007-06-09 10:11:16 -04001267 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Jim Cromie2a32ec22008-10-18 20:27:33 -07001269 for (i = 0; i < LDNI_MAX; i++) {
Guenter Roeck449a7a02012-01-14 21:49:53 -08001270 data->address[i] = extra_isa[i];
1271 if (data->address[i]
Guenter Roeck61d4d172012-06-02 11:20:19 -07001272 && !devm_request_region(dev, extra_isa[i], PC87360_EXTENT,
1273 pc87360_driver.driver.name)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001274 dev_err(dev, "Region 0x%x-0x%x already "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 "in use!\n", extra_isa[i],
1276 extra_isa[i]+PC87360_EXTENT-1);
Guenter Roeck61d4d172012-06-02 11:20:19 -07001277 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279 }
1280
1281 /* Retrieve the fans configuration from Super-I/O space */
1282 if (data->fannr)
1283 data->fan_conf = confreg[0] | (confreg[1] << 8);
1284
Guenter Roeck3af28612012-01-19 11:02:22 -08001285 /*
1286 * Use the correct reference voltage
1287 * Unless both the VLM and the TMS logical devices agree to
1288 * use an external Vref, the internal one is used.
1289 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (data->innr) {
1291 i = pc87360_read_value(data, LD_IN, NO_BANK,
1292 PC87365_REG_IN_CONFIG);
1293 if (data->tempnr) {
1294 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
1295 PC87365_REG_TEMP_CONFIG);
1296 }
1297 data->in_vref = (i&0x02) ? 3025 : 2966;
Jean Delvaref641b582007-06-09 10:11:16 -04001298 dev_dbg(dev, "Using %s reference voltage\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 (i&0x02) ? "external" : "internal");
1300
1301 data->vid_conf = confreg[3];
Jim Cromie3d7f9a82006-12-12 18:18:28 +01001302 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304
1305 /* Fan clock dividers may be needed before any data is read */
1306 for (i = 0; i < data->fannr; i++) {
1307 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1308 data->fan_status[i] = pc87360_read_value(data,
1309 LD_FAN, NO_BANK,
1310 PC87360_REG_FAN_STATUS(i));
1311 }
1312
1313 if (init > 0) {
1314 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1315 use_thermistors = confreg[2] & 0x40;
1316
Jean Delvaref641b582007-06-09 10:11:16 -04001317 pc87360_init_device(pdev, use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
Jim Cromief3722d5b2006-09-24 21:03:25 +02001320 /* Register all-or-nothing sysfs groups */
1321
Guenter Roeck449a7a02012-01-14 21:49:53 -08001322 if (data->innr) {
1323 err = sysfs_create_group(&dev->kobj, &pc8736x_vin_group);
1324 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001325 goto error;
Guenter Roeck449a7a02012-01-14 21:49:53 -08001326 }
Jim Cromief3722d5b2006-09-24 21:03:25 +02001327
Guenter Roeck449a7a02012-01-14 21:49:53 -08001328 if (data->innr == 14) {
1329 err = sysfs_create_group(&dev->kobj, &pc8736x_therm_group);
1330 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001331 goto error;
Guenter Roeck449a7a02012-01-14 21:49:53 -08001332 }
Jim Cromief3722d5b2006-09-24 21:03:25 +02001333
1334 /* create device attr-files for varying sysfs groups */
1335
1336 if (data->tempnr) {
1337 for (i = 0; i < data->tempnr; i++) {
Guenter Roeckbce27782012-01-19 20:50:57 -08001338 err = sysfs_create_group(&dev->kobj,
1339 &pc8736x_temp_attr_group[i]);
1340 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001341 goto error;
Jim Cromief3722d5b2006-09-24 21:03:25 +02001342 }
Guenter Roeck449a7a02012-01-14 21:49:53 -08001343 err = device_create_file(dev, &dev_attr_alarms_temp);
1344 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001345 goto error;
Jim Cromief3722d5b2006-09-24 21:03:25 +02001346 }
1347
1348 for (i = 0; i < data->fannr; i++) {
Guenter Roeckbce27782012-01-19 20:50:57 -08001349 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1350 err = sysfs_create_group(&dev->kobj,
1351 &pc8736x_fan_attr_group[i]);
1352 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001353 goto error;
Guenter Roeckbce27782012-01-19 20:50:57 -08001354 }
Guenter Roeck449a7a02012-01-14 21:49:53 -08001355 if (FAN_CONFIG_CONTROL(data->fan_conf, i)) {
1356 err = device_create_file(dev, &pwm[i].dev_attr);
1357 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001358 goto error;
Guenter Roeck449a7a02012-01-14 21:49:53 -08001359 }
Jim Cromief3722d5b2006-09-24 21:03:25 +02001360 }
1361
Guenter Roeck449a7a02012-01-14 21:49:53 -08001362 err = device_create_file(dev, &dev_attr_name);
1363 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001364 goto error;
Jean Delvaref641b582007-06-09 10:11:16 -04001365
Tony Jones1beeffe2007-08-20 13:46:20 -07001366 data->hwmon_dev = hwmon_device_register(dev);
1367 if (IS_ERR(data->hwmon_dev)) {
1368 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck61d4d172012-06-02 11:20:19 -07001369 goto error;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return 0;
1372
Guenter Roeck61d4d172012-06-02 11:20:19 -07001373error:
Guenter Roeckbce27782012-01-19 20:50:57 -08001374 pc87360_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return err;
1376}
1377
Bill Pemberton281dfd02012-11-19 13:25:51 -05001378static int pc87360_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Jean Delvaref641b582007-06-09 10:11:16 -04001380 struct pc87360_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Tony Jones1beeffe2007-08-20 13:46:20 -07001382 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckbce27782012-01-19 20:50:57 -08001383 pc87360_remove_files(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 return 0;
1386}
1387
Guenter Roeck3af28612012-01-19 11:02:22 -08001388/*
1389 * ldi is the logical device index
1390 * bank is for voltages and temperatures only
1391 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1393 u8 reg)
1394{
1395 int res;
1396
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001397 mutex_lock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (bank != NO_BANK)
1399 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1400 res = inb_p(data->address[ldi] + reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001401 mutex_unlock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 return res;
1404}
1405
1406static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1407 u8 reg, u8 value)
1408{
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001409 mutex_lock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (bank != NO_BANK)
1411 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1412 outb_p(value, data->address[ldi] + reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001413 mutex_unlock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Jim Cromie28f74e72008-10-18 20:27:30 -07001416/* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
1417#define CHAN_CNVRTD 0x80 /* new data ready */
1418#define CHAN_ENA 0x01 /* enabled channel (temp or vin) */
1419#define CHAN_ALM_ENA 0x10 /* propagate to alarms-reg ?? (chk val!) */
1420#define CHAN_READY (CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
1421
Jim Cromieb267e8c2008-10-18 20:27:32 -07001422#define TEMP_OTS_OE 0x20 /* OTS Output Enable */
1423#define VIN_RW1C_MASK (CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN) /* 0x87 */
1424#define TEMP_RW1C_MASK (VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */
1425
Jean Delvaref641b582007-06-09 10:11:16 -04001426static void pc87360_init_device(struct platform_device *pdev,
1427 int use_thermistors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Jean Delvaref641b582007-06-09 10:11:16 -04001429 struct pc87360_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 int i, nr;
1431 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1432 const u8 init_temp[3] = { 2, 2, 1 };
1433 u8 reg;
1434
1435 if (init >= 2 && data->innr) {
1436 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1437 PC87365_REG_IN_CONVRATE);
Jean Delvaref641b582007-06-09 10:11:16 -04001438 dev_info(&pdev->dev, "VLM conversion set to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 "1s period, 160us delay\n");
1440 pc87360_write_value(data, LD_IN, NO_BANK,
1441 PC87365_REG_IN_CONVRATE,
1442 (reg & 0xC0) | 0x11);
1443 }
1444
1445 nr = data->innr < 11 ? data->innr : 11;
Jim Cromiededc6a72006-01-09 23:24:41 +01001446 for (i = 0; i < nr; i++) {
Jim Cromie8ca13672008-10-18 20:27:34 -07001447 reg = pc87360_read_value(data, LD_IN, i,
1448 PC87365_REG_IN_STATUS);
1449 dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (init >= init_in[i]) {
1451 /* Forcibly enable voltage channel */
Jim Cromie28f74e72008-10-18 20:27:30 -07001452 if (!(reg & CHAN_ENA)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001453 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 "enabling in%d\n", i);
1455 pc87360_write_value(data, LD_IN, i,
1456 PC87365_REG_IN_STATUS,
1457 (reg & 0x68) | 0x87);
1458 }
1459 }
1460 }
1461
Guenter Roeck3af28612012-01-19 11:02:22 -08001462 /*
1463 * We can't blindly trust the Super-I/O space configuration bit,
1464 * most BIOS won't set it properly
1465 */
Jim Cromie8ca13672008-10-18 20:27:34 -07001466 dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors);
Jim Cromiededc6a72006-01-09 23:24:41 +01001467 for (i = 11; i < data->innr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 reg = pc87360_read_value(data, LD_IN, i,
1469 PC87365_REG_TEMP_STATUS);
Jim Cromie28f74e72008-10-18 20:27:30 -07001470 use_thermistors = use_thermistors || (reg & CHAN_ENA);
Jim Cromie8ca13672008-10-18 20:27:34 -07001471 /* thermistors are temp[4-6], measured on vin[11-14] */
1472 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
Jim Cromie8ca13672008-10-18 20:27:34 -07001474 dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 i = use_thermistors ? 2 : 0;
Jim Cromiededc6a72006-01-09 23:24:41 +01001477 for (; i < data->tempnr; i++) {
Jim Cromie8ca13672008-10-18 20:27:34 -07001478 reg = pc87360_read_value(data, LD_TEMP, i,
1479 PC87365_REG_TEMP_STATUS);
Guenter Roeck449a7a02012-01-14 21:49:53 -08001480 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i + 1, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (init >= init_temp[i]) {
1482 /* Forcibly enable temperature channel */
Jim Cromie28f74e72008-10-18 20:27:30 -07001483 if (!(reg & CHAN_ENA)) {
Guenter Roeck449a7a02012-01-14 21:49:53 -08001484 dev_dbg(&pdev->dev,
1485 "Forcibly enabling temp%d\n", i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 pc87360_write_value(data, LD_TEMP, i,
1487 PC87365_REG_TEMP_STATUS,
1488 0xCF);
1489 }
1490 }
1491 }
1492
1493 if (use_thermistors) {
Jim Cromiededc6a72006-01-09 23:24:41 +01001494 for (i = 11; i < data->innr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (init >= init_in[i]) {
Guenter Roeck3af28612012-01-19 11:02:22 -08001496 /*
1497 * The pin may already be used by thermal
1498 * diodes
1499 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 reg = pc87360_read_value(data, LD_TEMP,
Guenter Roeck449a7a02012-01-14 21:49:53 -08001501 (i - 11) / 2, PC87365_REG_TEMP_STATUS);
Jim Cromie28f74e72008-10-18 20:27:30 -07001502 if (reg & CHAN_ENA) {
Guenter Roeck449a7a02012-01-14 21:49:53 -08001503 dev_dbg(&pdev->dev,
1504 "Skipping temp%d, pin already in use by temp%d\n",
1505 i - 7, (i - 11) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 continue;
1507 }
1508
1509 /* Forcibly enable thermistor channel */
1510 reg = pc87360_read_value(data, LD_IN, i,
1511 PC87365_REG_IN_STATUS);
Jim Cromie28f74e72008-10-18 20:27:30 -07001512 if (!(reg & CHAN_ENA)) {
Guenter Roeck449a7a02012-01-14 21:49:53 -08001513 dev_dbg(&pdev->dev,
1514 "Forcibly enabling temp%d\n",
1515 i - 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 pc87360_write_value(data, LD_IN, i,
1517 PC87365_REG_TEMP_STATUS,
1518 (reg & 0x60) | 0x8F);
1519 }
1520 }
1521 }
1522 }
1523
1524 if (data->innr) {
1525 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1526 PC87365_REG_IN_CONFIG);
Jim Cromie8ca13672008-10-18 20:27:34 -07001527 dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg);
Jim Cromie28f74e72008-10-18 20:27:30 -07001528 if (reg & CHAN_ENA) {
Guenter Roeck449a7a02012-01-14 21:49:53 -08001529 dev_dbg(&pdev->dev,
1530 "Forcibly enabling monitoring (VLM)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 pc87360_write_value(data, LD_IN, NO_BANK,
1532 PC87365_REG_IN_CONFIG,
1533 reg & 0xFE);
1534 }
1535 }
1536
1537 if (data->tempnr) {
1538 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1539 PC87365_REG_TEMP_CONFIG);
Jim Cromie8ca13672008-10-18 20:27:34 -07001540 dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg);
Jim Cromie28f74e72008-10-18 20:27:30 -07001541 if (reg & CHAN_ENA) {
Guenter Roeck449a7a02012-01-14 21:49:53 -08001542 dev_dbg(&pdev->dev,
1543 "Forcibly enabling monitoring (TMS)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 pc87360_write_value(data, LD_TEMP, NO_BANK,
1545 PC87365_REG_TEMP_CONFIG,
1546 reg & 0xFE);
1547 }
1548
1549 if (init >= 2) {
1550 /* Chip config as documented by National Semi. */
1551 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
Guenter Roeck3af28612012-01-19 11:02:22 -08001552 /*
1553 * We voluntarily omit the bank here, in case the
1554 * sequence itself matters. It shouldn't be a problem,
1555 * since nobody else is supposed to access the
1556 * device at that point.
1557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1559 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1560 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1561 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1562 }
1563 }
1564}
1565
Jean Delvaref641b582007-06-09 10:11:16 -04001566static void pc87360_autodiv(struct device *dev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
Jean Delvaref641b582007-06-09 10:11:16 -04001568 struct pc87360_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 u8 old_min = data->fan_min[nr];
1570
1571 /* Increase clock divider if needed and possible */
1572 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1573 || (data->fan[nr] >= 224)) { /* next to overflow */
1574 if ((data->fan_status[nr] & 0x60) != 0x60) {
1575 data->fan_status[nr] += 0x20;
1576 data->fan_min[nr] >>= 1;
1577 data->fan[nr] >>= 1;
Jean Delvaref641b582007-06-09 10:11:16 -04001578 dev_dbg(dev, "Increasing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 "clock divider to %d for fan %d\n",
Guenter Roeck449a7a02012-01-14 21:49:53 -08001580 FAN_DIV_FROM_REG(data->fan_status[nr]), nr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582 } else {
1583 /* Decrease clock divider if possible */
1584 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1585 && data->fan[nr] < 85 /* bad accuracy */
1586 && (data->fan_status[nr] & 0x60) != 0x00) {
1587 data->fan_status[nr] -= 0x20;
1588 data->fan_min[nr] <<= 1;
1589 data->fan[nr] <<= 1;
Jean Delvaref641b582007-06-09 10:11:16 -04001590 dev_dbg(dev, "Decreasing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 "clock divider to %d for fan %d\n",
1592 FAN_DIV_FROM_REG(data->fan_status[nr]),
Guenter Roeck449a7a02012-01-14 21:49:53 -08001593 nr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595 }
1596
1597 /* Write new fan min if it changed */
1598 if (old_min != data->fan_min[nr]) {
1599 pc87360_write_value(data, LD_FAN, NO_BANK,
1600 PC87360_REG_FAN_MIN(nr),
1601 data->fan_min[nr]);
1602 }
1603}
1604
1605static struct pc87360_data *pc87360_update_device(struct device *dev)
1606{
Jean Delvaref641b582007-06-09 10:11:16 -04001607 struct pc87360_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 u8 i;
1609
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001610 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
Jean Delvaref641b582007-06-09 10:11:16 -04001613 dev_dbg(dev, "Data update\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 /* Fans */
1616 for (i = 0; i < data->fannr; i++) {
1617 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1618 data->fan_status[i] =
1619 pc87360_read_value(data, LD_FAN,
1620 NO_BANK, PC87360_REG_FAN_STATUS(i));
1621 data->fan[i] = pc87360_read_value(data, LD_FAN,
1622 NO_BANK, PC87360_REG_FAN(i));
1623 data->fan_min[i] = pc87360_read_value(data,
1624 LD_FAN, NO_BANK,
1625 PC87360_REG_FAN_MIN(i));
1626 /* Change clock divider if needed */
Jean Delvaref641b582007-06-09 10:11:16 -04001627 pc87360_autodiv(dev, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /* Clear bits and write new divider */
1629 pc87360_write_value(data, LD_FAN, NO_BANK,
1630 PC87360_REG_FAN_STATUS(i),
1631 data->fan_status[i]);
1632 }
1633 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1634 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1635 NO_BANK, PC87360_REG_PWM(i));
1636 }
1637
1638 /* Voltages */
1639 for (i = 0; i < data->innr; i++) {
1640 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1641 PC87365_REG_IN_STATUS);
1642 /* Clear bits */
1643 pc87360_write_value(data, LD_IN, i,
1644 PC87365_REG_IN_STATUS,
1645 data->in_status[i]);
Jim Cromie28f74e72008-10-18 20:27:30 -07001646 if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 data->in[i] = pc87360_read_value(data, LD_IN,
1648 i, PC87365_REG_IN);
1649 }
Jim Cromie28f74e72008-10-18 20:27:30 -07001650 if (data->in_status[i] & CHAN_ENA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 data->in_min[i] = pc87360_read_value(data,
1652 LD_IN, i,
1653 PC87365_REG_IN_MIN);
1654 data->in_max[i] = pc87360_read_value(data,
1655 LD_IN, i,
1656 PC87365_REG_IN_MAX);
1657 if (i >= 11)
1658 data->in_crit[i-11] =
1659 pc87360_read_value(data, LD_IN,
1660 i, PC87365_REG_TEMP_CRIT);
1661 }
1662 }
1663 if (data->innr) {
1664 data->in_alarms = pc87360_read_value(data, LD_IN,
1665 NO_BANK, PC87365_REG_IN_ALARMS1)
1666 | ((pc87360_read_value(data, LD_IN,
1667 NO_BANK, PC87365_REG_IN_ALARMS2)
1668 & 0x07) << 8);
1669 data->vid = (data->vid_conf & 0xE0) ?
1670 pc87360_read_value(data, LD_IN,
1671 NO_BANK, PC87365_REG_VID) : 0x1F;
1672 }
1673
1674 /* Temperatures */
1675 for (i = 0; i < data->tempnr; i++) {
1676 data->temp_status[i] = pc87360_read_value(data,
1677 LD_TEMP, i,
1678 PC87365_REG_TEMP_STATUS);
1679 /* Clear bits */
1680 pc87360_write_value(data, LD_TEMP, i,
1681 PC87365_REG_TEMP_STATUS,
1682 data->temp_status[i]);
Jim Cromie28f74e72008-10-18 20:27:30 -07001683 if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 data->temp[i] = pc87360_read_value(data,
1685 LD_TEMP, i,
1686 PC87365_REG_TEMP);
1687 }
Jim Cromie28f74e72008-10-18 20:27:30 -07001688 if (data->temp_status[i] & CHAN_ENA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 data->temp_min[i] = pc87360_read_value(data,
1690 LD_TEMP, i,
1691 PC87365_REG_TEMP_MIN);
1692 data->temp_max[i] = pc87360_read_value(data,
1693 LD_TEMP, i,
1694 PC87365_REG_TEMP_MAX);
1695 data->temp_crit[i] = pc87360_read_value(data,
1696 LD_TEMP, i,
1697 PC87365_REG_TEMP_CRIT);
1698 }
1699 }
1700 if (data->tempnr) {
1701 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1702 NO_BANK, PC87365_REG_TEMP_ALARMS)
1703 & 0x3F;
1704 }
1705
1706 data->last_updated = jiffies;
1707 data->valid = 1;
1708 }
1709
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001710 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 return data;
1713}
1714
Jean Delvaref641b582007-06-09 10:11:16 -04001715static int __init pc87360_device_add(unsigned short address)
1716{
Jean Delvareb9783dc2010-08-14 21:08:48 +02001717 struct resource res[3];
1718 int err, i, res_count;
Jean Delvaref641b582007-06-09 10:11:16 -04001719
1720 pdev = platform_device_alloc("pc87360", address);
1721 if (!pdev) {
1722 err = -ENOMEM;
Joe Perches9e991c62011-01-12 21:55:11 +01001723 pr_err("Device allocation failed\n");
Jean Delvaref641b582007-06-09 10:11:16 -04001724 goto exit;
1725 }
1726
Jean Delvareb9783dc2010-08-14 21:08:48 +02001727 memset(res, 0, 3 * sizeof(struct resource));
1728 res_count = 0;
Jean Delvaref641b582007-06-09 10:11:16 -04001729 for (i = 0; i < 3; i++) {
1730 if (!extra_isa[i])
1731 continue;
Jean Delvareb9783dc2010-08-14 21:08:48 +02001732 res[res_count].start = extra_isa[i];
1733 res[res_count].end = extra_isa[i] + PC87360_EXTENT - 1;
1734 res[res_count].name = "pc87360",
1735 res[res_count].flags = IORESOURCE_IO,
Jean Delvareb9acb642009-01-07 16:37:35 +01001736
Jean Delvareb9783dc2010-08-14 21:08:48 +02001737 err = acpi_check_resource_conflict(&res[res_count]);
Jean Delvareb9acb642009-01-07 16:37:35 +01001738 if (err)
1739 goto exit_device_put;
1740
Jean Delvareb9783dc2010-08-14 21:08:48 +02001741 res_count++;
1742 }
1743
1744 err = platform_device_add_resources(pdev, res, res_count);
1745 if (err) {
Joe Perches9e991c62011-01-12 21:55:11 +01001746 pr_err("Device resources addition failed (%d)\n", err);
Jean Delvareb9783dc2010-08-14 21:08:48 +02001747 goto exit_device_put;
Jean Delvaref641b582007-06-09 10:11:16 -04001748 }
1749
1750 err = platform_device_add(pdev);
1751 if (err) {
Joe Perches9e991c62011-01-12 21:55:11 +01001752 pr_err("Device addition failed (%d)\n", err);
Jean Delvaref641b582007-06-09 10:11:16 -04001753 goto exit_device_put;
1754 }
1755
1756 return 0;
1757
1758exit_device_put:
1759 platform_device_put(pdev);
1760exit:
1761 return err;
1762}
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764static int __init pc87360_init(void)
1765{
Jean Delvaref641b582007-06-09 10:11:16 -04001766 int err, i;
1767 unsigned short address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 if (pc87360_find(0x2e, &devid, extra_isa)
1770 && pc87360_find(0x4e, &devid, extra_isa)) {
Joe Perches9e991c62011-01-12 21:55:11 +01001771 pr_warn("PC8736x not detected, module not inserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return -ENODEV;
1773 }
1774
1775 /* Arbitrarily pick one of the addresses */
1776 for (i = 0; i < 3; i++) {
1777 if (extra_isa[i] != 0x0000) {
Jean Delvare2d8672c2005-07-19 23:56:35 +02001778 address = extra_isa[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 break;
1780 }
1781 }
1782
Jean Delvare2d8672c2005-07-19 23:56:35 +02001783 if (address == 0x0000) {
Joe Perches9e991c62011-01-12 21:55:11 +01001784 pr_warn("No active logical device, module not inserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return -ENODEV;
1786 }
1787
Jean Delvaref641b582007-06-09 10:11:16 -04001788 err = platform_driver_register(&pc87360_driver);
1789 if (err)
1790 goto exit;
1791
1792 /* Sets global pdev as a side effect */
1793 err = pc87360_device_add(address);
1794 if (err)
1795 goto exit_driver;
1796
1797 return 0;
1798
1799 exit_driver:
1800 platform_driver_unregister(&pc87360_driver);
1801 exit:
1802 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
1805static void __exit pc87360_exit(void)
1806{
Jean Delvaref641b582007-06-09 10:11:16 -04001807 platform_device_unregister(pdev);
1808 platform_driver_unregister(&pc87360_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
1811
1812MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1813MODULE_DESCRIPTION("PC8736x hardware monitor");
1814MODULE_LICENSE("GPL");
1815
1816module_init(pc87360_init);
1817module_exit(pc87360_exit);