blob: d50fbf93a7370784902266b2e3185a02f3fea872 [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 Delvare7c81c602014-01-29 20:40:08 +01004 * Copyright (C) 2004, 2007 Jean Delvare <jdelvare@suse.de>
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 = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100247 .name = "pc87360",
248 },
Jean Delvaref641b582007-06-09 10:11:16 -0400249 .probe = pc87360_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500250 .remove = pc87360_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251};
252
253/*
254 * Sysfs stuff
255 */
256
Guenter Roeck449a7a02012-01-14 21:49:53 -0800257static ssize_t show_fan_input(struct device *dev,
258 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200259{
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}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800265static ssize_t show_fan_min(struct device *dev,
266 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200267{
268 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
269 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200270 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
271 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200272}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800273static ssize_t show_fan_div(struct device *dev,
274 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200275{
276 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
277 struct pc87360_data *data = pc87360_update_device(dev);
278 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200279 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200280}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800281static ssize_t show_fan_status(struct device *dev,
282 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200283{
284 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
285 struct pc87360_data *data = pc87360_update_device(dev);
286 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200287 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200288}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800289static ssize_t set_fan_min(struct device *dev,
290 struct device_attribute *devattr, const char *buf,
Jim Cromief0986bd2005-09-02 22:52:43 +0200291 size_t count)
292{
293 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400294 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800295 long fan_min;
296 int err;
297
298 err = kstrtol(buf, 10, &fan_min);
299 if (err)
300 return err;
Jim Cromie11be27e2005-09-02 23:05:07 +0200301
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100302 mutex_lock(&data->update_lock);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800303 fan_min = FAN_TO_REG(fan_min,
304 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
Jim Cromie11be27e2005-09-02 23:05:07 +0200305
306 /* If it wouldn't fit, change clock divisor */
307 while (fan_min > 255
308 && (data->fan_status[attr->index] & 0x60) != 0x60) {
309 fan_min >>= 1;
310 data->fan[attr->index] >>= 1;
311 data->fan_status[attr->index] += 0x20;
312 }
313 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
Guenter Roeck449a7a02012-01-14 21:49:53 -0800314 pc87360_write_value(data, LD_FAN, NO_BANK,
315 PC87360_REG_FAN_MIN(attr->index),
Jim Cromie11be27e2005-09-02 23:05:07 +0200316 data->fan_min[attr->index]);
317
318 /* Write new divider, preserve alarm bits */
Guenter Roeck449a7a02012-01-14 21:49:53 -0800319 pc87360_write_value(data, LD_FAN, NO_BANK,
320 PC87360_REG_FAN_STATUS(attr->index),
Jim Cromie11be27e2005-09-02 23:05:07 +0200321 data->fan_status[attr->index] & 0xF9);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100322 mutex_unlock(&data->update_lock);
Jim Cromie11be27e2005-09-02 23:05:07 +0200323
324 return count;
Jim Cromief0986bd2005-09-02 22:52:43 +0200325}
326
Jim Cromiededc6a72006-01-09 23:24:41 +0100327static struct sensor_device_attribute fan_input[] = {
328 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
329 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
330 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
331};
332static struct sensor_device_attribute fan_status[] = {
333 SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
334 SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
335 SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
336};
337static struct sensor_device_attribute fan_div[] = {
338 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
339 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
340 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
341};
342static struct sensor_device_attribute fan_min[] = {
343 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
344 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
345 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
346};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Guenter Roeckbce27782012-01-19 20:50:57 -0800348#define FAN_UNIT_ATTRS(X) \
349{ &fan_input[X].dev_attr.attr, \
Jim Cromie941c5c02006-09-24 21:02:44 +0200350 &fan_status[X].dev_attr.attr, \
351 &fan_div[X].dev_attr.attr, \
Guenter Roeckbce27782012-01-19 20:50:57 -0800352 &fan_min[X].dev_attr.attr, \
353 NULL \
354}
Jim Cromie941c5c02006-09-24 21:02:44 +0200355
Guenter Roeck449a7a02012-01-14 21:49:53 -0800356static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
357 char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200358{
359 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
360 struct pc87360_data *data = pc87360_update_device(dev);
361 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200362 PWM_FROM_REG(data->pwm[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200363 FAN_CONFIG_INVERT(data->fan_conf,
Jim Cromie694fa052005-09-02 22:57:52 +0200364 attr->index)));
Jim Cromief0986bd2005-09-02 22:52:43 +0200365}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800366static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
367 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200368{
369 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400370 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800371 long val;
372 int err;
373
374 err = kstrtol(buf, 10, &val);
375 if (err)
376 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200377
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100378 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200379 data->pwm[attr->index] = PWM_TO_REG(val,
380 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
381 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
382 data->pwm[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100383 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200384 return count;
385}
386
Jim Cromiededc6a72006-01-09 23:24:41 +0100387static struct sensor_device_attribute pwm[] = {
388 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0),
389 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1),
390 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2),
391};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Guenter Roeckbce27782012-01-19 20:50:57 -0800393static struct attribute *pc8736x_fan_attr[][5] = {
Jim Cromie941c5c02006-09-24 21:02:44 +0200394 FAN_UNIT_ATTRS(0),
395 FAN_UNIT_ATTRS(1),
Guenter Roeckbce27782012-01-19 20:50:57 -0800396 FAN_UNIT_ATTRS(2)
Jim Cromie941c5c02006-09-24 21:02:44 +0200397};
Guenter Roeckbce27782012-01-19 20:50:57 -0800398
399static const struct attribute_group pc8736x_fan_attr_group[] = {
400 { .attrs = pc8736x_fan_attr[0], },
401 { .attrs = pc8736x_fan_attr[1], },
402 { .attrs = pc8736x_fan_attr[2], },
Jim Cromie941c5c02006-09-24 21:02:44 +0200403};
404
Guenter Roeck449a7a02012-01-14 21:49:53 -0800405static ssize_t show_in_input(struct device *dev,
406 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200407{
408 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
409 struct pc87360_data *data = pc87360_update_device(dev);
410 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
411 data->in_vref));
412}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800413static ssize_t show_in_min(struct device *dev,
414 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200415{
416 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
417 struct pc87360_data *data = pc87360_update_device(dev);
418 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
419 data->in_vref));
420}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800421static ssize_t show_in_max(struct device *dev,
422 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200423{
424 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
425 struct pc87360_data *data = pc87360_update_device(dev);
426 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
427 data->in_vref));
428}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800429static ssize_t show_in_status(struct device *dev,
430 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200431{
432 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
433 struct pc87360_data *data = pc87360_update_device(dev);
434 return sprintf(buf, "%u\n", data->in_status[attr->index]);
435}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800436static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr,
437 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200438{
439 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400440 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800441 long val;
442 int err;
443
444 err = kstrtol(buf, 10, &val);
445 if (err)
446 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200447
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100448 mutex_lock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200449 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
450 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
451 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100452 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200453 return count;
454}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800455static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr,
456 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200457{
458 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400459 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800460 long val;
461 int err;
462
463 err = kstrtol(buf, 10, &val);
464 if (err)
465 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200466
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100467 mutex_lock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200468 data->in_max[attr->index] = IN_TO_REG(val,
469 data->in_vref);
470 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
471 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100472 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200473 return count;
474}
475
Jim Cromiededc6a72006-01-09 23:24:41 +0100476static struct sensor_device_attribute in_input[] = {
477 SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
478 SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
479 SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
480 SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
481 SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
482 SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
483 SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
484 SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
485 SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
486 SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
487 SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
488};
489static struct sensor_device_attribute in_status[] = {
490 SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
491 SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
492 SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
493 SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
494 SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
495 SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
496 SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
497 SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
498 SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
499 SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
500 SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
501};
502static struct sensor_device_attribute in_min[] = {
503 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
504 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
505 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
506 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
507 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
508 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
509 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
510 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
511 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
512 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
513 SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
514};
515static struct sensor_device_attribute in_max[] = {
516 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
517 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
518 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
519 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
520 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
521 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
522 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
523 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
524 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
525 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
526 SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
527};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Jim Cromie28f74e72008-10-18 20:27:30 -0700529/* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
530#define CHAN_ALM_MIN 0x02 /* min limit crossed */
531#define CHAN_ALM_MAX 0x04 /* max limit exceeded */
532#define TEMP_ALM_CRIT 0x08 /* temp crit exceeded (temp only) */
533
Guenter Roeck3af28612012-01-19 11:02:22 -0800534/*
535 * show_in_min/max_alarm() reads data from the per-channel status
536 * register (sec 11.5.12), not the vin event status registers (sec
537 * 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms)
538 */
Jim Cromie492e9652008-10-18 20:27:32 -0700539
540static ssize_t show_in_min_alarm(struct device *dev,
541 struct device_attribute *devattr, char *buf)
542{
543 struct pc87360_data *data = pc87360_update_device(dev);
544 unsigned nr = to_sensor_dev_attr(devattr)->index;
545
546 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
547}
548static ssize_t show_in_max_alarm(struct device *dev,
549 struct device_attribute *devattr, char *buf)
550{
551 struct pc87360_data *data = pc87360_update_device(dev);
552 unsigned nr = to_sensor_dev_attr(devattr)->index;
553
554 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
555}
556
557static struct sensor_device_attribute in_min_alarm[] = {
558 SENSOR_ATTR(in0_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 0),
559 SENSOR_ATTR(in1_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 1),
560 SENSOR_ATTR(in2_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 2),
561 SENSOR_ATTR(in3_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 3),
562 SENSOR_ATTR(in4_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 4),
563 SENSOR_ATTR(in5_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 5),
564 SENSOR_ATTR(in6_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 6),
565 SENSOR_ATTR(in7_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 7),
566 SENSOR_ATTR(in8_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 8),
567 SENSOR_ATTR(in9_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 9),
568 SENSOR_ATTR(in10_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 10),
569};
570static struct sensor_device_attribute in_max_alarm[] = {
571 SENSOR_ATTR(in0_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 0),
572 SENSOR_ATTR(in1_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 1),
573 SENSOR_ATTR(in2_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 2),
574 SENSOR_ATTR(in3_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 3),
575 SENSOR_ATTR(in4_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 4),
576 SENSOR_ATTR(in5_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 5),
577 SENSOR_ATTR(in6_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 6),
578 SENSOR_ATTR(in7_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 7),
579 SENSOR_ATTR(in8_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 8),
580 SENSOR_ATTR(in9_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 9),
581 SENSOR_ATTR(in10_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 10),
582};
583
Jim Cromie941c5c02006-09-24 21:02:44 +0200584#define VIN_UNIT_ATTRS(X) \
585 &in_input[X].dev_attr.attr, \
586 &in_status[X].dev_attr.attr, \
587 &in_min[X].dev_attr.attr, \
Jim Cromie492e9652008-10-18 20:27:32 -0700588 &in_max[X].dev_attr.attr, \
589 &in_min_alarm[X].dev_attr.attr, \
590 &in_max_alarm[X].dev_attr.attr
Jim Cromie941c5c02006-09-24 21:02:44 +0200591
Guenter Roeck449a7a02012-01-14 21:49:53 -0800592static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
593 char *buf)
Jim Cromie44646c12006-09-24 21:01:56 +0200594{
595 struct pc87360_data *data = pc87360_update_device(dev);
596 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
597}
598static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
599
Guenter Roeck449a7a02012-01-14 21:49:53 -0800600static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
601 char *buf)
Jim Cromie44646c12006-09-24 21:01:56 +0200602{
Jean Delvare90d66192007-10-08 18:24:35 +0200603 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromie44646c12006-09-24 21:01:56 +0200604 return sprintf(buf, "%u\n", data->vrm);
605}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800606static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
607 const char *buf, size_t count)
Jim Cromie44646c12006-09-24 21:01:56 +0200608{
Jean Delvaref641b582007-06-09 10:11:16 -0400609 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800610 unsigned long val;
611 int err;
612
613 err = kstrtoul(buf, 10, &val);
614 if (err)
615 return err;
616
Axel Lin5e3b5612014-08-06 08:24:54 +0800617 if (val > 255)
618 return -EINVAL;
619
Guenter Roeck449a7a02012-01-14 21:49:53 -0800620 data->vrm = val;
Jim Cromie44646c12006-09-24 21:01:56 +0200621 return count;
622}
623static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
624
Guenter Roeck449a7a02012-01-14 21:49:53 -0800625static ssize_t show_in_alarms(struct device *dev,
626 struct device_attribute *attr, char *buf)
Jim Cromie44646c12006-09-24 21:01:56 +0200627{
628 struct pc87360_data *data = pc87360_update_device(dev);
629 return sprintf(buf, "%u\n", data->in_alarms);
630}
631static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
632
Jim Cromie941c5c02006-09-24 21:02:44 +0200633static struct attribute *pc8736x_vin_attr_array[] = {
634 VIN_UNIT_ATTRS(0),
635 VIN_UNIT_ATTRS(1),
636 VIN_UNIT_ATTRS(2),
637 VIN_UNIT_ATTRS(3),
638 VIN_UNIT_ATTRS(4),
639 VIN_UNIT_ATTRS(5),
640 VIN_UNIT_ATTRS(6),
641 VIN_UNIT_ATTRS(7),
642 VIN_UNIT_ATTRS(8),
643 VIN_UNIT_ATTRS(9),
644 VIN_UNIT_ATTRS(10),
645 &dev_attr_cpu0_vid.attr,
646 &dev_attr_vrm.attr,
647 &dev_attr_alarms_in.attr,
648 NULL
649};
650static const struct attribute_group pc8736x_vin_group = {
651 .attrs = pc8736x_vin_attr_array,
652};
653
Guenter Roeck449a7a02012-01-14 21:49:53 -0800654static ssize_t show_therm_input(struct device *dev,
655 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200656{
657 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
658 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200659 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200660 data->in_vref));
661}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800662static ssize_t show_therm_min(struct device *dev,
663 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200664{
665 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
666 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200667 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200668 data->in_vref));
669}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800670static ssize_t show_therm_max(struct device *dev,
671 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200672{
673 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
674 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200675 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200676 data->in_vref));
677}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800678static ssize_t show_therm_crit(struct device *dev,
679 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200680{
681 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
682 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200683 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
Jim Cromief0986bd2005-09-02 22:52:43 +0200684 data->in_vref));
685}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800686static ssize_t show_therm_status(struct device *dev,
687 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200688{
689 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
690 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200691 return sprintf(buf, "%u\n", data->in_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200692}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800693
694static ssize_t set_therm_min(struct device *dev,
695 struct device_attribute *devattr,
696 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200697{
698 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400699 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800700 long val;
701 int err;
702
703 err = kstrtol(buf, 10, &val);
704 if (err)
705 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200706
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100707 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200708 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
709 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
710 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100711 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200712 return count;
713}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800714
715static ssize_t set_therm_max(struct device *dev,
716 struct device_attribute *devattr,
717 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200718{
719 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400720 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800721 long val;
722 int err;
723
724 err = kstrtol(buf, 10, &val);
725 if (err)
726 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200727
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100728 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200729 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
730 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
731 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100732 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200733 return count;
734}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800735static ssize_t set_therm_crit(struct device *dev,
736 struct device_attribute *devattr,
737 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200738{
739 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400740 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800741 long val;
742 int err;
743
744 err = kstrtol(buf, 10, &val);
745 if (err)
746 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200747
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100748 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200749 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
750 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
751 data->in_crit[attr->index-11]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100752 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200753 return count;
754}
755
Guenter Roeck3af28612012-01-19 11:02:22 -0800756/*
757 * the +11 term below reflects the fact that VLM units 11,12,13 are
758 * used in the chip to measure voltage across the thermistors
759 */
Jim Cromiededc6a72006-01-09 23:24:41 +0100760static struct sensor_device_attribute therm_input[] = {
Guenter Roeck449a7a02012-01-14 21:49:53 -0800761 SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0 + 11),
762 SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1 + 11),
763 SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100764};
765static struct sensor_device_attribute therm_status[] = {
Guenter Roeck449a7a02012-01-14 21:49:53 -0800766 SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0 + 11),
767 SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1 + 11),
768 SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100769};
770static struct sensor_device_attribute therm_min[] = {
771 SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800772 show_therm_min, set_therm_min, 0 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100773 SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800774 show_therm_min, set_therm_min, 1 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100775 SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800776 show_therm_min, set_therm_min, 2 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100777};
778static struct sensor_device_attribute therm_max[] = {
779 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800780 show_therm_max, set_therm_max, 0 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100781 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800782 show_therm_max, set_therm_max, 1 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100783 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800784 show_therm_max, set_therm_max, 2 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100785};
786static struct sensor_device_attribute therm_crit[] = {
787 SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800788 show_therm_crit, set_therm_crit, 0 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100789 SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800790 show_therm_crit, set_therm_crit, 1 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100791 SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800792 show_therm_crit, set_therm_crit, 2 + 11),
Jim Cromiededc6a72006-01-09 23:24:41 +0100793};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Guenter Roeck3af28612012-01-19 11:02:22 -0800795/*
796 * show_therm_min/max_alarm() reads data from the per-channel voltage
797 * status register (sec 11.5.12)
798 */
Jim Cromie865c2952008-10-18 20:27:35 -0700799
800static ssize_t show_therm_min_alarm(struct device *dev,
801 struct device_attribute *devattr, char *buf)
802{
803 struct pc87360_data *data = pc87360_update_device(dev);
804 unsigned nr = to_sensor_dev_attr(devattr)->index;
805
806 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
807}
808static ssize_t show_therm_max_alarm(struct device *dev,
809 struct device_attribute *devattr, char *buf)
810{
811 struct pc87360_data *data = pc87360_update_device(dev);
812 unsigned nr = to_sensor_dev_attr(devattr)->index;
813
814 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
815}
816static ssize_t show_therm_crit_alarm(struct device *dev,
817 struct device_attribute *devattr, char *buf)
818{
819 struct pc87360_data *data = pc87360_update_device(dev);
820 unsigned nr = to_sensor_dev_attr(devattr)->index;
821
822 return sprintf(buf, "%u\n", !!(data->in_status[nr] & TEMP_ALM_CRIT));
823}
824
825static struct sensor_device_attribute therm_min_alarm[] = {
826 SENSOR_ATTR(temp4_min_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800827 show_therm_min_alarm, NULL, 0 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700828 SENSOR_ATTR(temp5_min_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800829 show_therm_min_alarm, NULL, 1 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700830 SENSOR_ATTR(temp6_min_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800831 show_therm_min_alarm, NULL, 2 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700832};
833static struct sensor_device_attribute therm_max_alarm[] = {
834 SENSOR_ATTR(temp4_max_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800835 show_therm_max_alarm, NULL, 0 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700836 SENSOR_ATTR(temp5_max_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800837 show_therm_max_alarm, NULL, 1 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700838 SENSOR_ATTR(temp6_max_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800839 show_therm_max_alarm, NULL, 2 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700840};
841static struct sensor_device_attribute therm_crit_alarm[] = {
842 SENSOR_ATTR(temp4_crit_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800843 show_therm_crit_alarm, NULL, 0 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700844 SENSOR_ATTR(temp5_crit_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800845 show_therm_crit_alarm, NULL, 1 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700846 SENSOR_ATTR(temp6_crit_alarm, S_IRUGO,
Guenter Roeck449a7a02012-01-14 21:49:53 -0800847 show_therm_crit_alarm, NULL, 2 + 11),
Jim Cromie865c2952008-10-18 20:27:35 -0700848};
849
Jim Cromie941c5c02006-09-24 21:02:44 +0200850#define THERM_UNIT_ATTRS(X) \
851 &therm_input[X].dev_attr.attr, \
852 &therm_status[X].dev_attr.attr, \
853 &therm_min[X].dev_attr.attr, \
854 &therm_max[X].dev_attr.attr, \
Jim Cromie865c2952008-10-18 20:27:35 -0700855 &therm_crit[X].dev_attr.attr, \
856 &therm_min_alarm[X].dev_attr.attr, \
857 &therm_max_alarm[X].dev_attr.attr, \
858 &therm_crit_alarm[X].dev_attr.attr
Jim Cromie941c5c02006-09-24 21:02:44 +0200859
Guenter Roeck449a7a02012-01-14 21:49:53 -0800860static struct attribute *pc8736x_therm_attr_array[] = {
Jim Cromie941c5c02006-09-24 21:02:44 +0200861 THERM_UNIT_ATTRS(0),
862 THERM_UNIT_ATTRS(1),
863 THERM_UNIT_ATTRS(2),
864 NULL
865};
866static const struct attribute_group pc8736x_therm_group = {
867 .attrs = pc8736x_therm_attr_array,
868};
869
Guenter Roeck449a7a02012-01-14 21:49:53 -0800870static ssize_t show_temp_input(struct device *dev,
871 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200872{
873 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
874 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200875 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200876}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800877
878static ssize_t show_temp_min(struct device *dev,
879 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200880{
881 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
882 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200883 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200884}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800885
886static ssize_t show_temp_max(struct device *dev,
887 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200888{
889 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
890 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200891 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200892}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800893
894static ssize_t show_temp_crit(struct device *dev,
895 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200896{
897 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
898 struct pc87360_data *data = pc87360_update_device(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800899 return sprintf(buf, "%d\n",
900 TEMP_FROM_REG(data->temp_crit[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200901}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800902
903static ssize_t show_temp_status(struct device *dev,
904 struct device_attribute *devattr, char *buf)
Jim Cromief0986bd2005-09-02 22:52:43 +0200905{
906 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
907 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200908 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200909}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800910
911static ssize_t set_temp_min(struct device *dev,
912 struct device_attribute *devattr,
913 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200914{
915 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400916 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800917 long val;
918 int err;
919
920 err = kstrtol(buf, 10, &val);
921 if (err)
922 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200923
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100924 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200925 data->temp_min[attr->index] = TEMP_TO_REG(val);
926 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
927 data->temp_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100928 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200929 return count;
930}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800931
932static ssize_t set_temp_max(struct device *dev,
933 struct device_attribute *devattr,
934 const char *buf, size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200935{
936 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400937 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800938 long val;
939 int err;
940
941 err = kstrtol(buf, 10, &val);
942 if (err)
943 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200944
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100945 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200946 data->temp_max[attr->index] = TEMP_TO_REG(val);
947 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
948 data->temp_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100949 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200950 return count;
951}
Guenter Roeck449a7a02012-01-14 21:49:53 -0800952
953static ssize_t set_temp_crit(struct device *dev,
954 struct device_attribute *devattr, const char *buf,
955 size_t count)
Jim Cromief0986bd2005-09-02 22:52:43 +0200956{
957 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400958 struct pc87360_data *data = dev_get_drvdata(dev);
Guenter Roeck449a7a02012-01-14 21:49:53 -0800959 long val;
960 int err;
961
962 err = kstrtol(buf, 10, &val);
963 if (err)
964 return err;
Jim Cromief0986bd2005-09-02 22:52:43 +0200965
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100966 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200967 data->temp_crit[attr->index] = TEMP_TO_REG(val);
968 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
969 data->temp_crit[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100970 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200971 return count;
972}
973
Jim Cromiededc6a72006-01-09 23:24:41 +0100974static struct sensor_device_attribute temp_input[] = {
975 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
976 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
977 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
978};
979static struct sensor_device_attribute temp_status[] = {
980 SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
981 SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
982 SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
983};
984static struct sensor_device_attribute temp_min[] = {
985 SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
986 show_temp_min, set_temp_min, 0),
987 SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
988 show_temp_min, set_temp_min, 1),
989 SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
990 show_temp_min, set_temp_min, 2),
991};
992static struct sensor_device_attribute temp_max[] = {
993 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
994 show_temp_max, set_temp_max, 0),
995 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
996 show_temp_max, set_temp_max, 1),
997 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
998 show_temp_max, set_temp_max, 2),
999};
1000static struct sensor_device_attribute temp_crit[] = {
1001 SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
1002 show_temp_crit, set_temp_crit, 0),
1003 SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
1004 show_temp_crit, set_temp_crit, 1),
1005 SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
1006 show_temp_crit, set_temp_crit, 2),
1007};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Guenter Roeck449a7a02012-01-14 21:49:53 -08001009static ssize_t show_temp_alarms(struct device *dev,
1010 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 struct pc87360_data *data = pc87360_update_device(dev);
1013 return sprintf(buf, "%u\n", data->temp_alarms);
1014}
Guenter Roeck449a7a02012-01-14 21:49:53 -08001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
1017
Guenter Roeck3af28612012-01-19 11:02:22 -08001018/*
1019 * show_temp_min/max_alarm() reads data from the per-channel status
1020 * register (sec 12.3.7), not the temp event status registers (sec
1021 * 12.3.2) that show_temp_alarm() reads (via data->temp_alarms)
1022 */
Jim Cromieb267e8c2008-10-18 20:27:32 -07001023
1024static ssize_t show_temp_min_alarm(struct device *dev,
1025 struct device_attribute *devattr, char *buf)
1026{
1027 struct pc87360_data *data = pc87360_update_device(dev);
1028 unsigned nr = to_sensor_dev_attr(devattr)->index;
1029
1030 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN));
1031}
Guenter Roeck449a7a02012-01-14 21:49:53 -08001032
Jim Cromieb267e8c2008-10-18 20:27:32 -07001033static ssize_t show_temp_max_alarm(struct device *dev,
1034 struct device_attribute *devattr, char *buf)
1035{
1036 struct pc87360_data *data = pc87360_update_device(dev);
1037 unsigned nr = to_sensor_dev_attr(devattr)->index;
1038
1039 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX));
1040}
Guenter Roeck449a7a02012-01-14 21:49:53 -08001041
Jim Cromieb267e8c2008-10-18 20:27:32 -07001042static ssize_t show_temp_crit_alarm(struct device *dev,
1043 struct device_attribute *devattr, char *buf)
1044{
1045 struct pc87360_data *data = pc87360_update_device(dev);
1046 unsigned nr = to_sensor_dev_attr(devattr)->index;
1047
1048 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT));
1049}
1050
1051static struct sensor_device_attribute temp_min_alarm[] = {
1052 SENSOR_ATTR(temp1_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 0),
1053 SENSOR_ATTR(temp2_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 1),
1054 SENSOR_ATTR(temp3_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 2),
1055};
Guenter Roeck449a7a02012-01-14 21:49:53 -08001056
Jim Cromieb267e8c2008-10-18 20:27:32 -07001057static struct sensor_device_attribute temp_max_alarm[] = {
1058 SENSOR_ATTR(temp1_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 0),
1059 SENSOR_ATTR(temp2_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 1),
1060 SENSOR_ATTR(temp3_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 2),
1061};
Guenter Roeck449a7a02012-01-14 21:49:53 -08001062
Jim Cromieb267e8c2008-10-18 20:27:32 -07001063static struct sensor_device_attribute temp_crit_alarm[] = {
1064 SENSOR_ATTR(temp1_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 0),
1065 SENSOR_ATTR(temp2_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 1),
1066 SENSOR_ATTR(temp3_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 2),
1067};
1068
1069#define TEMP_FAULT 0x40 /* open diode */
1070static ssize_t show_temp_fault(struct device *dev,
1071 struct device_attribute *devattr, char *buf)
1072{
1073 struct pc87360_data *data = pc87360_update_device(dev);
1074 unsigned nr = to_sensor_dev_attr(devattr)->index;
1075
1076 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT));
1077}
1078static struct sensor_device_attribute temp_fault[] = {
1079 SENSOR_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0),
1080 SENSOR_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1),
1081 SENSOR_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2),
1082};
1083
Guenter Roeckbce27782012-01-19 20:50:57 -08001084#define TEMP_UNIT_ATTRS(X) \
1085{ &temp_input[X].dev_attr.attr, \
1086 &temp_status[X].dev_attr.attr, \
1087 &temp_min[X].dev_attr.attr, \
1088 &temp_max[X].dev_attr.attr, \
1089 &temp_crit[X].dev_attr.attr, \
1090 &temp_min_alarm[X].dev_attr.attr, \
1091 &temp_max_alarm[X].dev_attr.attr, \
1092 &temp_crit_alarm[X].dev_attr.attr, \
1093 &temp_fault[X].dev_attr.attr, \
1094 NULL \
1095}
Jim Cromie941c5c02006-09-24 21:02:44 +02001096
Guenter Roeckbce27782012-01-19 20:50:57 -08001097static struct attribute *pc8736x_temp_attr[][10] = {
Jim Cromie941c5c02006-09-24 21:02:44 +02001098 TEMP_UNIT_ATTRS(0),
1099 TEMP_UNIT_ATTRS(1),
Guenter Roeckbce27782012-01-19 20:50:57 -08001100 TEMP_UNIT_ATTRS(2)
Jim Cromie941c5c02006-09-24 21:02:44 +02001101};
Guenter Roeck449a7a02012-01-14 21:49:53 -08001102
Guenter Roeckbce27782012-01-19 20:50:57 -08001103static const struct attribute_group pc8736x_temp_attr_group[] = {
1104 { .attrs = pc8736x_temp_attr[0] },
1105 { .attrs = pc8736x_temp_attr[1] },
1106 { .attrs = pc8736x_temp_attr[2] }
Jim Cromie941c5c02006-09-24 21:02:44 +02001107};
1108
Jim Cromieb267e8c2008-10-18 20:27:32 -07001109static ssize_t show_name(struct device *dev,
1110 struct device_attribute *devattr, char *buf)
Jean Delvaref641b582007-06-09 10:11:16 -04001111{
1112 struct pc87360_data *data = dev_get_drvdata(dev);
1113 return sprintf(buf, "%s\n", data->name);
1114}
Guenter Roeck449a7a02012-01-14 21:49:53 -08001115
Jean Delvaref641b582007-06-09 10:11:16 -04001116static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/*
1119 * Device detection, registration and update
1120 */
1121
Guenter Roeck449a7a02012-01-14 21:49:53 -08001122static int __init pc87360_find(int sioaddr, u8 *devid,
1123 unsigned short *addresses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 u16 val;
1126 int i;
1127 int nrdev; /* logical device count */
1128
1129 /* No superio_enter */
1130
1131 /* Identify device */
Jean Delvare67b671b2007-12-06 23:13:42 +01001132 val = force_id ? force_id : superio_inb(sioaddr, DEVID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 switch (val) {
1134 case 0xE1: /* PC87360 */
1135 case 0xE8: /* PC87363 */
1136 case 0xE4: /* PC87364 */
1137 nrdev = 1;
1138 break;
1139 case 0xE5: /* PC87365 */
1140 case 0xE9: /* PC87366 */
1141 nrdev = 3;
1142 break;
1143 default:
1144 superio_exit(sioaddr);
1145 return -ENODEV;
1146 }
1147 /* Remember the device id */
1148 *devid = val;
1149
1150 for (i = 0; i < nrdev; i++) {
1151 /* select logical device */
1152 superio_outb(sioaddr, DEV, logdev[i]);
1153
1154 val = superio_inb(sioaddr, ACT);
1155 if (!(val & 0x01)) {
Joe Perches9e991c62011-01-12 21:55:11 +01001156 pr_info("Device 0x%02x not activated\n", logdev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 continue;
1158 }
1159
1160 val = (superio_inb(sioaddr, BASE) << 8)
1161 | superio_inb(sioaddr, BASE + 1);
1162 if (!val) {
Joe Perches9e991c62011-01-12 21:55:11 +01001163 pr_info("Base address not set for device 0x%02x\n",
1164 logdev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 continue;
1166 }
1167
Jean Delvare2d8672c2005-07-19 23:56:35 +02001168 addresses[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Guenter Roeck449a7a02012-01-14 21:49:53 -08001170 if (i == 0) { /* Fans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 confreg[0] = superio_inb(sioaddr, 0xF0);
1172 confreg[1] = superio_inb(sioaddr, 0xF1);
1173
Joe Perches9e991c62011-01-12 21:55:11 +01001174 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 1,
1175 (confreg[0] >> 2) & 1, (confreg[0] >> 3) & 1,
1176 (confreg[0] >> 4) & 1);
1177 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 2,
1178 (confreg[0] >> 5) & 1, (confreg[0] >> 6) & 1,
1179 (confreg[0] >> 7) & 1);
1180 pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 3,
1181 confreg[1] & 1, (confreg[1] >> 1) & 1,
1182 (confreg[1] >> 2) & 1);
Guenter Roeck449a7a02012-01-14 21:49:53 -08001183 } else if (i == 1) { /* Voltages */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* Are we using thermistors? */
1185 if (*devid == 0xE9) { /* PC87366 */
Guenter Roeck3af28612012-01-19 11:02:22 -08001186 /*
1187 * These registers are not logical-device
1188 * specific, just that we won't need them if
1189 * we don't use the VLM device
1190 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 confreg[2] = superio_inb(sioaddr, 0x2B);
1192 confreg[3] = superio_inb(sioaddr, 0x25);
1193
1194 if (confreg[2] & 0x40) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08001195 pr_info("Using thermistors for temperature monitoring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197 if (confreg[3] & 0xE0) {
Joe Perches9e991c62011-01-12 21:55:11 +01001198 pr_info("VID inputs routed (mode %u)\n",
1199 confreg[3] >> 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201 }
1202 }
1203 }
1204
1205 superio_exit(sioaddr);
1206 return 0;
1207}
1208
Guenter Roeckbce27782012-01-19 20:50:57 -08001209static void pc87360_remove_files(struct device *dev)
1210{
1211 int i;
1212
1213 device_remove_file(dev, &dev_attr_name);
1214 device_remove_file(dev, &dev_attr_alarms_temp);
1215 for (i = 0; i < ARRAY_SIZE(pc8736x_temp_attr_group); i++)
1216 sysfs_remove_group(&dev->kobj, &pc8736x_temp_attr_group[i]);
1217 for (i = 0; i < ARRAY_SIZE(pc8736x_fan_attr_group); i++) {
1218 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_attr_group[i]);
1219 device_remove_file(dev, &pwm[i].dev_attr);
1220 }
1221 sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1222 sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
1223}
1224
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001225static int pc87360_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 struct pc87360_data *data;
1229 int err = 0;
Jean Delvare5b581572014-04-04 18:01:34 +02001230 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int use_thermistors = 0;
Jean Delvaref641b582007-06-09 10:11:16 -04001232 struct device *dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Guenter Roeck61d4d172012-06-02 11:20:19 -07001234 data = devm_kzalloc(dev, sizeof(struct pc87360_data), GFP_KERNEL);
Guenter Roeck449a7a02012-01-14 21:49:53 -08001235 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 switch (devid) {
Jean Delvare5b581572014-04-04 18:01:34 +02001239 default:
1240 name = "pc87360";
1241 data->fannr = 2;
1242 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 case 0xe8:
1244 name = "pc87363";
Jean Delvare5b581572014-04-04 18:01:34 +02001245 data->fannr = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 break;
1247 case 0xe4:
1248 name = "pc87364";
1249 data->fannr = 3;
1250 break;
1251 case 0xe5:
1252 name = "pc87365";
1253 data->fannr = extra_isa[0] ? 3 : 0;
1254 data->innr = extra_isa[1] ? 11 : 0;
1255 data->tempnr = extra_isa[2] ? 2 : 0;
1256 break;
1257 case 0xe9:
1258 name = "pc87366";
1259 data->fannr = extra_isa[0] ? 3 : 0;
1260 data->innr = extra_isa[1] ? 14 : 0;
1261 data->tempnr = extra_isa[2] ? 3 : 0;
1262 break;
1263 }
1264
Jean Delvaref641b582007-06-09 10:11:16 -04001265 data->name = name;
Jean Delvaref641b582007-06-09 10:11:16 -04001266 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001267 mutex_init(&data->update_lock);
Jean Delvaref641b582007-06-09 10:11:16 -04001268 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Jim Cromie2a32ec22008-10-18 20:27:33 -07001270 for (i = 0; i < LDNI_MAX; i++) {
Guenter Roeck449a7a02012-01-14 21:49:53 -08001271 data->address[i] = extra_isa[i];
1272 if (data->address[i]
Guenter Roeck61d4d172012-06-02 11:20:19 -07001273 && !devm_request_region(dev, extra_isa[i], PC87360_EXTENT,
1274 pc87360_driver.driver.name)) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08001275 dev_err(dev,
1276 "Region 0x%x-0x%x already in use!\n",
1277 extra_isa[i], extra_isa[i]+PC87360_EXTENT-1);
Guenter Roeck61d4d172012-06-02 11:20:19 -07001278 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280 }
1281
1282 /* Retrieve the fans configuration from Super-I/O space */
1283 if (data->fannr)
1284 data->fan_conf = confreg[0] | (confreg[1] << 8);
1285
Guenter Roeck3af28612012-01-19 11:02:22 -08001286 /*
1287 * Use the correct reference voltage
1288 * Unless both the VLM and the TMS logical devices agree to
1289 * use an external Vref, the internal one is used.
1290 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (data->innr) {
1292 i = pc87360_read_value(data, LD_IN, NO_BANK,
1293 PC87365_REG_IN_CONFIG);
1294 if (data->tempnr) {
1295 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
1296 PC87365_REG_TEMP_CONFIG);
1297 }
1298 data->in_vref = (i&0x02) ? 3025 : 2966;
Jean Delvaref641b582007-06-09 10:11:16 -04001299 dev_dbg(dev, "Using %s reference voltage\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 (i&0x02) ? "external" : "internal");
1301
1302 data->vid_conf = confreg[3];
Jim Cromie3d7f9a82006-12-12 18:18:28 +01001303 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305
1306 /* Fan clock dividers may be needed before any data is read */
1307 for (i = 0; i < data->fannr; i++) {
1308 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1309 data->fan_status[i] = pc87360_read_value(data,
1310 LD_FAN, NO_BANK,
1311 PC87360_REG_FAN_STATUS(i));
1312 }
1313
1314 if (init > 0) {
1315 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1316 use_thermistors = confreg[2] & 0x40;
1317
Jean Delvaref641b582007-06-09 10:11:16 -04001318 pc87360_init_device(pdev, use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
Jim Cromief3722d5b2006-09-24 21:03:25 +02001321 /* Register all-or-nothing sysfs groups */
1322
Guenter Roeck449a7a02012-01-14 21:49:53 -08001323 if (data->innr) {
1324 err = sysfs_create_group(&dev->kobj, &pc8736x_vin_group);
1325 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001326 goto error;
Guenter Roeck449a7a02012-01-14 21:49:53 -08001327 }
Jim Cromief3722d5b2006-09-24 21:03:25 +02001328
Guenter Roeck449a7a02012-01-14 21:49:53 -08001329 if (data->innr == 14) {
1330 err = sysfs_create_group(&dev->kobj, &pc8736x_therm_group);
1331 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001332 goto error;
Guenter Roeck449a7a02012-01-14 21:49:53 -08001333 }
Jim Cromief3722d5b2006-09-24 21:03:25 +02001334
1335 /* create device attr-files for varying sysfs groups */
1336
1337 if (data->tempnr) {
1338 for (i = 0; i < data->tempnr; i++) {
Guenter Roeckbce27782012-01-19 20:50:57 -08001339 err = sysfs_create_group(&dev->kobj,
1340 &pc8736x_temp_attr_group[i]);
1341 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001342 goto error;
Jim Cromief3722d5b2006-09-24 21:03:25 +02001343 }
Guenter Roeck449a7a02012-01-14 21:49:53 -08001344 err = device_create_file(dev, &dev_attr_alarms_temp);
1345 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001346 goto error;
Jim Cromief3722d5b2006-09-24 21:03:25 +02001347 }
1348
1349 for (i = 0; i < data->fannr; i++) {
Guenter Roeckbce27782012-01-19 20:50:57 -08001350 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1351 err = sysfs_create_group(&dev->kobj,
1352 &pc8736x_fan_attr_group[i]);
1353 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001354 goto error;
Guenter Roeckbce27782012-01-19 20:50:57 -08001355 }
Guenter Roeck449a7a02012-01-14 21:49:53 -08001356 if (FAN_CONFIG_CONTROL(data->fan_conf, i)) {
1357 err = device_create_file(dev, &pwm[i].dev_attr);
1358 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001359 goto error;
Guenter Roeck449a7a02012-01-14 21:49:53 -08001360 }
Jim Cromief3722d5b2006-09-24 21:03:25 +02001361 }
1362
Guenter Roeck449a7a02012-01-14 21:49:53 -08001363 err = device_create_file(dev, &dev_attr_name);
1364 if (err)
Guenter Roeck61d4d172012-06-02 11:20:19 -07001365 goto error;
Jean Delvaref641b582007-06-09 10:11:16 -04001366
Tony Jones1beeffe2007-08-20 13:46:20 -07001367 data->hwmon_dev = hwmon_device_register(dev);
1368 if (IS_ERR(data->hwmon_dev)) {
1369 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck61d4d172012-06-02 11:20:19 -07001370 goto error;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return 0;
1373
Guenter Roeck61d4d172012-06-02 11:20:19 -07001374error:
Guenter Roeckbce27782012-01-19 20:50:57 -08001375 pc87360_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return err;
1377}
1378
Bill Pemberton281dfd02012-11-19 13:25:51 -05001379static int pc87360_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Jean Delvaref641b582007-06-09 10:11:16 -04001381 struct pc87360_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Tony Jones1beeffe2007-08-20 13:46:20 -07001383 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckbce27782012-01-19 20:50:57 -08001384 pc87360_remove_files(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 return 0;
1387}
1388
Guenter Roeck3af28612012-01-19 11:02:22 -08001389/*
1390 * ldi is the logical device index
1391 * bank is for voltages and temperatures only
1392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1394 u8 reg)
1395{
1396 int res;
1397
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001398 mutex_lock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (bank != NO_BANK)
1400 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1401 res = inb_p(data->address[ldi] + reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001402 mutex_unlock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 return res;
1405}
1406
1407static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1408 u8 reg, u8 value)
1409{
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001410 mutex_lock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (bank != NO_BANK)
1412 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1413 outb_p(value, data->address[ldi] + reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001414 mutex_unlock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Jim Cromie28f74e72008-10-18 20:27:30 -07001417/* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
1418#define CHAN_CNVRTD 0x80 /* new data ready */
1419#define CHAN_ENA 0x01 /* enabled channel (temp or vin) */
1420#define CHAN_ALM_ENA 0x10 /* propagate to alarms-reg ?? (chk val!) */
1421#define CHAN_READY (CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
1422
Jim Cromieb267e8c2008-10-18 20:27:32 -07001423#define TEMP_OTS_OE 0x20 /* OTS Output Enable */
1424#define VIN_RW1C_MASK (CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN) /* 0x87 */
1425#define TEMP_RW1C_MASK (VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */
1426
Jean Delvaref641b582007-06-09 10:11:16 -04001427static void pc87360_init_device(struct platform_device *pdev,
1428 int use_thermistors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Jean Delvaref641b582007-06-09 10:11:16 -04001430 struct pc87360_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 int i, nr;
1432 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1433 const u8 init_temp[3] = { 2, 2, 1 };
1434 u8 reg;
1435
1436 if (init >= 2 && data->innr) {
1437 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1438 PC87365_REG_IN_CONVRATE);
Guenter Roeckb55f3752013-01-10 10:01:24 -08001439 dev_info(&pdev->dev,
1440 "VLM conversion set to 1s period, 160us delay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 pc87360_write_value(data, LD_IN, NO_BANK,
1442 PC87365_REG_IN_CONVRATE,
1443 (reg & 0xC0) | 0x11);
1444 }
1445
1446 nr = data->innr < 11 ? data->innr : 11;
Jim Cromiededc6a72006-01-09 23:24:41 +01001447 for (i = 0; i < nr; i++) {
Jim Cromie8ca13672008-10-18 20:27:34 -07001448 reg = pc87360_read_value(data, LD_IN, i,
1449 PC87365_REG_IN_STATUS);
1450 dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (init >= init_in[i]) {
1452 /* Forcibly enable voltage channel */
Jim Cromie28f74e72008-10-18 20:27:30 -07001453 if (!(reg & CHAN_ENA)) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08001454 dev_dbg(&pdev->dev, "Forcibly enabling in%d\n",
1455 i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 pc87360_write_value(data, LD_IN, i,
1457 PC87365_REG_IN_STATUS,
1458 (reg & 0x68) | 0x87);
1459 }
1460 }
1461 }
1462
Guenter Roeck3af28612012-01-19 11:02:22 -08001463 /*
1464 * We can't blindly trust the Super-I/O space configuration bit,
1465 * most BIOS won't set it properly
1466 */
Jim Cromie8ca13672008-10-18 20:27:34 -07001467 dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors);
Jim Cromiededc6a72006-01-09 23:24:41 +01001468 for (i = 11; i < data->innr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 reg = pc87360_read_value(data, LD_IN, i,
1470 PC87365_REG_TEMP_STATUS);
Jim Cromie28f74e72008-10-18 20:27:30 -07001471 use_thermistors = use_thermistors || (reg & CHAN_ENA);
Jim Cromie8ca13672008-10-18 20:27:34 -07001472 /* thermistors are temp[4-6], measured on vin[11-14] */
1473 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
Jim Cromie8ca13672008-10-18 20:27:34 -07001475 dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 i = use_thermistors ? 2 : 0;
Jim Cromiededc6a72006-01-09 23:24:41 +01001478 for (; i < data->tempnr; i++) {
Jim Cromie8ca13672008-10-18 20:27:34 -07001479 reg = pc87360_read_value(data, LD_TEMP, i,
1480 PC87365_REG_TEMP_STATUS);
Guenter Roeck449a7a02012-01-14 21:49:53 -08001481 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i + 1, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (init >= init_temp[i]) {
1483 /* Forcibly enable temperature channel */
Jim Cromie28f74e72008-10-18 20:27:30 -07001484 if (!(reg & CHAN_ENA)) {
Guenter Roeck449a7a02012-01-14 21:49:53 -08001485 dev_dbg(&pdev->dev,
1486 "Forcibly enabling temp%d\n", i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 pc87360_write_value(data, LD_TEMP, i,
1488 PC87365_REG_TEMP_STATUS,
1489 0xCF);
1490 }
1491 }
1492 }
1493
1494 if (use_thermistors) {
Jim Cromiededc6a72006-01-09 23:24:41 +01001495 for (i = 11; i < data->innr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (init >= init_in[i]) {
Guenter Roeck3af28612012-01-19 11:02:22 -08001497 /*
1498 * The pin may already be used by thermal
1499 * diodes
1500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 reg = pc87360_read_value(data, LD_TEMP,
Guenter Roeck449a7a02012-01-14 21:49:53 -08001502 (i - 11) / 2, PC87365_REG_TEMP_STATUS);
Jim Cromie28f74e72008-10-18 20:27:30 -07001503 if (reg & CHAN_ENA) {
Guenter Roeck449a7a02012-01-14 21:49:53 -08001504 dev_dbg(&pdev->dev,
1505 "Skipping temp%d, pin already in use by temp%d\n",
1506 i - 7, (i - 11) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 continue;
1508 }
1509
1510 /* Forcibly enable thermistor channel */
1511 reg = pc87360_read_value(data, LD_IN, i,
1512 PC87365_REG_IN_STATUS);
Jim Cromie28f74e72008-10-18 20:27:30 -07001513 if (!(reg & CHAN_ENA)) {
Guenter Roeck449a7a02012-01-14 21:49:53 -08001514 dev_dbg(&pdev->dev,
1515 "Forcibly enabling temp%d\n",
1516 i - 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 pc87360_write_value(data, LD_IN, i,
1518 PC87365_REG_TEMP_STATUS,
1519 (reg & 0x60) | 0x8F);
1520 }
1521 }
1522 }
1523 }
1524
1525 if (data->innr) {
1526 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1527 PC87365_REG_IN_CONFIG);
Jim Cromie8ca13672008-10-18 20:27:34 -07001528 dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg);
Jim Cromie28f74e72008-10-18 20:27:30 -07001529 if (reg & CHAN_ENA) {
Guenter Roeck449a7a02012-01-14 21:49:53 -08001530 dev_dbg(&pdev->dev,
1531 "Forcibly enabling monitoring (VLM)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 pc87360_write_value(data, LD_IN, NO_BANK,
1533 PC87365_REG_IN_CONFIG,
1534 reg & 0xFE);
1535 }
1536 }
1537
1538 if (data->tempnr) {
1539 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1540 PC87365_REG_TEMP_CONFIG);
Jim Cromie8ca13672008-10-18 20:27:34 -07001541 dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg);
Jim Cromie28f74e72008-10-18 20:27:30 -07001542 if (reg & CHAN_ENA) {
Guenter Roeck449a7a02012-01-14 21:49:53 -08001543 dev_dbg(&pdev->dev,
1544 "Forcibly enabling monitoring (TMS)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 pc87360_write_value(data, LD_TEMP, NO_BANK,
1546 PC87365_REG_TEMP_CONFIG,
1547 reg & 0xFE);
1548 }
1549
1550 if (init >= 2) {
1551 /* Chip config as documented by National Semi. */
1552 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
Guenter Roeck3af28612012-01-19 11:02:22 -08001553 /*
1554 * We voluntarily omit the bank here, in case the
1555 * sequence itself matters. It shouldn't be a problem,
1556 * since nobody else is supposed to access the
1557 * device at that point.
1558 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1560 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1561 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1562 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1563 }
1564 }
1565}
1566
Jean Delvaref641b582007-06-09 10:11:16 -04001567static void pc87360_autodiv(struct device *dev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Jean Delvaref641b582007-06-09 10:11:16 -04001569 struct pc87360_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 u8 old_min = data->fan_min[nr];
1571
1572 /* Increase clock divider if needed and possible */
1573 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1574 || (data->fan[nr] >= 224)) { /* next to overflow */
1575 if ((data->fan_status[nr] & 0x60) != 0x60) {
1576 data->fan_status[nr] += 0x20;
1577 data->fan_min[nr] >>= 1;
1578 data->fan[nr] >>= 1;
Guenter Roeckb55f3752013-01-10 10:01:24 -08001579 dev_dbg(dev,
1580 "Increasing clock divider to %d for fan %d\n",
Guenter Roeck449a7a02012-01-14 21:49:53 -08001581 FAN_DIV_FROM_REG(data->fan_status[nr]), nr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583 } else {
1584 /* Decrease clock divider if possible */
1585 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1586 && data->fan[nr] < 85 /* bad accuracy */
1587 && (data->fan_status[nr] & 0x60) != 0x00) {
1588 data->fan_status[nr] -= 0x20;
1589 data->fan_min[nr] <<= 1;
1590 data->fan[nr] <<= 1;
Guenter Roeckb55f3752013-01-10 10:01:24 -08001591 dev_dbg(dev,
1592 "Decreasing clock divider to %d for fan %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 FAN_DIV_FROM_REG(data->fan_status[nr]),
Guenter Roeck449a7a02012-01-14 21:49:53 -08001594 nr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
1596 }
1597
1598 /* Write new fan min if it changed */
1599 if (old_min != data->fan_min[nr]) {
1600 pc87360_write_value(data, LD_FAN, NO_BANK,
1601 PC87360_REG_FAN_MIN(nr),
1602 data->fan_min[nr]);
1603 }
1604}
1605
1606static struct pc87360_data *pc87360_update_device(struct device *dev)
1607{
Jean Delvaref641b582007-06-09 10:11:16 -04001608 struct pc87360_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 u8 i;
1610
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001611 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
Jean Delvaref641b582007-06-09 10:11:16 -04001614 dev_dbg(dev, "Data update\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 /* Fans */
1617 for (i = 0; i < data->fannr; i++) {
1618 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1619 data->fan_status[i] =
1620 pc87360_read_value(data, LD_FAN,
1621 NO_BANK, PC87360_REG_FAN_STATUS(i));
1622 data->fan[i] = pc87360_read_value(data, LD_FAN,
1623 NO_BANK, PC87360_REG_FAN(i));
1624 data->fan_min[i] = pc87360_read_value(data,
1625 LD_FAN, NO_BANK,
1626 PC87360_REG_FAN_MIN(i));
1627 /* Change clock divider if needed */
Jean Delvaref641b582007-06-09 10:11:16 -04001628 pc87360_autodiv(dev, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 /* Clear bits and write new divider */
1630 pc87360_write_value(data, LD_FAN, NO_BANK,
1631 PC87360_REG_FAN_STATUS(i),
1632 data->fan_status[i]);
1633 }
1634 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1635 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1636 NO_BANK, PC87360_REG_PWM(i));
1637 }
1638
1639 /* Voltages */
1640 for (i = 0; i < data->innr; i++) {
1641 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1642 PC87365_REG_IN_STATUS);
1643 /* Clear bits */
1644 pc87360_write_value(data, LD_IN, i,
1645 PC87365_REG_IN_STATUS,
1646 data->in_status[i]);
Jim Cromie28f74e72008-10-18 20:27:30 -07001647 if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 data->in[i] = pc87360_read_value(data, LD_IN,
1649 i, PC87365_REG_IN);
1650 }
Jim Cromie28f74e72008-10-18 20:27:30 -07001651 if (data->in_status[i] & CHAN_ENA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 data->in_min[i] = pc87360_read_value(data,
1653 LD_IN, i,
1654 PC87365_REG_IN_MIN);
1655 data->in_max[i] = pc87360_read_value(data,
1656 LD_IN, i,
1657 PC87365_REG_IN_MAX);
1658 if (i >= 11)
1659 data->in_crit[i-11] =
1660 pc87360_read_value(data, LD_IN,
1661 i, PC87365_REG_TEMP_CRIT);
1662 }
1663 }
1664 if (data->innr) {
1665 data->in_alarms = pc87360_read_value(data, LD_IN,
1666 NO_BANK, PC87365_REG_IN_ALARMS1)
1667 | ((pc87360_read_value(data, LD_IN,
1668 NO_BANK, PC87365_REG_IN_ALARMS2)
1669 & 0x07) << 8);
1670 data->vid = (data->vid_conf & 0xE0) ?
1671 pc87360_read_value(data, LD_IN,
1672 NO_BANK, PC87365_REG_VID) : 0x1F;
1673 }
1674
1675 /* Temperatures */
1676 for (i = 0; i < data->tempnr; i++) {
1677 data->temp_status[i] = pc87360_read_value(data,
1678 LD_TEMP, i,
1679 PC87365_REG_TEMP_STATUS);
1680 /* Clear bits */
1681 pc87360_write_value(data, LD_TEMP, i,
1682 PC87365_REG_TEMP_STATUS,
1683 data->temp_status[i]);
Jim Cromie28f74e72008-10-18 20:27:30 -07001684 if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 data->temp[i] = pc87360_read_value(data,
1686 LD_TEMP, i,
1687 PC87365_REG_TEMP);
1688 }
Jim Cromie28f74e72008-10-18 20:27:30 -07001689 if (data->temp_status[i] & CHAN_ENA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 data->temp_min[i] = pc87360_read_value(data,
1691 LD_TEMP, i,
1692 PC87365_REG_TEMP_MIN);
1693 data->temp_max[i] = pc87360_read_value(data,
1694 LD_TEMP, i,
1695 PC87365_REG_TEMP_MAX);
1696 data->temp_crit[i] = pc87360_read_value(data,
1697 LD_TEMP, i,
1698 PC87365_REG_TEMP_CRIT);
1699 }
1700 }
1701 if (data->tempnr) {
1702 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1703 NO_BANK, PC87365_REG_TEMP_ALARMS)
1704 & 0x3F;
1705 }
1706
1707 data->last_updated = jiffies;
1708 data->valid = 1;
1709 }
1710
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001711 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 return data;
1714}
1715
Jean Delvaref641b582007-06-09 10:11:16 -04001716static int __init pc87360_device_add(unsigned short address)
1717{
Jean Delvareb9783dc2010-08-14 21:08:48 +02001718 struct resource res[3];
1719 int err, i, res_count;
Jean Delvaref641b582007-06-09 10:11:16 -04001720
1721 pdev = platform_device_alloc("pc87360", address);
1722 if (!pdev) {
1723 err = -ENOMEM;
Joe Perches9e991c62011-01-12 21:55:11 +01001724 pr_err("Device allocation failed\n");
Jean Delvaref641b582007-06-09 10:11:16 -04001725 goto exit;
1726 }
1727
Jean Delvareb9783dc2010-08-14 21:08:48 +02001728 memset(res, 0, 3 * sizeof(struct resource));
1729 res_count = 0;
Jean Delvaref641b582007-06-09 10:11:16 -04001730 for (i = 0; i < 3; i++) {
1731 if (!extra_isa[i])
1732 continue;
Jean Delvareb9783dc2010-08-14 21:08:48 +02001733 res[res_count].start = extra_isa[i];
1734 res[res_count].end = extra_isa[i] + PC87360_EXTENT - 1;
1735 res[res_count].name = "pc87360",
1736 res[res_count].flags = IORESOURCE_IO,
Jean Delvareb9acb642009-01-07 16:37:35 +01001737
Jean Delvareb9783dc2010-08-14 21:08:48 +02001738 err = acpi_check_resource_conflict(&res[res_count]);
Jean Delvareb9acb642009-01-07 16:37:35 +01001739 if (err)
1740 goto exit_device_put;
1741
Jean Delvareb9783dc2010-08-14 21:08:48 +02001742 res_count++;
1743 }
1744
1745 err = platform_device_add_resources(pdev, res, res_count);
1746 if (err) {
Joe Perches9e991c62011-01-12 21:55:11 +01001747 pr_err("Device resources addition failed (%d)\n", err);
Jean Delvareb9783dc2010-08-14 21:08:48 +02001748 goto exit_device_put;
Jean Delvaref641b582007-06-09 10:11:16 -04001749 }
1750
1751 err = platform_device_add(pdev);
1752 if (err) {
Joe Perches9e991c62011-01-12 21:55:11 +01001753 pr_err("Device addition failed (%d)\n", err);
Jean Delvaref641b582007-06-09 10:11:16 -04001754 goto exit_device_put;
1755 }
1756
1757 return 0;
1758
1759exit_device_put:
1760 platform_device_put(pdev);
1761exit:
1762 return err;
1763}
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765static int __init pc87360_init(void)
1766{
Jean Delvaref641b582007-06-09 10:11:16 -04001767 int err, i;
1768 unsigned short address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 if (pc87360_find(0x2e, &devid, extra_isa)
1771 && pc87360_find(0x4e, &devid, extra_isa)) {
Joe Perches9e991c62011-01-12 21:55:11 +01001772 pr_warn("PC8736x not detected, module not inserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return -ENODEV;
1774 }
1775
1776 /* Arbitrarily pick one of the addresses */
1777 for (i = 0; i < 3; i++) {
1778 if (extra_isa[i] != 0x0000) {
Jean Delvare2d8672c2005-07-19 23:56:35 +02001779 address = extra_isa[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 break;
1781 }
1782 }
1783
Jean Delvare2d8672c2005-07-19 23:56:35 +02001784 if (address == 0x0000) {
Joe Perches9e991c62011-01-12 21:55:11 +01001785 pr_warn("No active logical device, module not inserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return -ENODEV;
1787 }
1788
Jean Delvaref641b582007-06-09 10:11:16 -04001789 err = platform_driver_register(&pc87360_driver);
1790 if (err)
1791 goto exit;
1792
1793 /* Sets global pdev as a side effect */
1794 err = pc87360_device_add(address);
1795 if (err)
1796 goto exit_driver;
1797
1798 return 0;
1799
1800 exit_driver:
1801 platform_driver_unregister(&pc87360_driver);
1802 exit:
1803 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
1806static void __exit pc87360_exit(void)
1807{
Jean Delvaref641b582007-06-09 10:11:16 -04001808 platform_device_unregister(pdev);
1809 platform_driver_unregister(&pc87360_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
1812
Jean Delvare7c81c602014-01-29 20:40:08 +01001813MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814MODULE_DESCRIPTION("PC8736x hardware monitor");
1815MODULE_LICENSE("GPL");
1816
1817module_init(pc87360_init);
1818module_exit(pc87360_exit);