blob: 67b47de8263afb930cb30407c1fff3ad2af9d2fd [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jean Delvaree53004e2006-01-09 23:26:14 +01002/*
Jean Delvare51c997d2006-12-12 18:18:29 +01003 * f71805f.c - driver for the Fintek F71805F/FG and F71872F/FG Super-I/O
4 * chips integrated hardware monitoring features
Jean Delvare7c81c60f2014-01-29 20:40:08 +01005 * Copyright (C) 2005-2006 Jean Delvare <jdelvare@suse.de>
Jean Delvaree53004e2006-01-09 23:26:14 +01006 *
7 * The F71805F/FG is a LPC Super-I/O chip made by Fintek. It integrates
8 * complete hardware monitoring features: voltage, fan and temperature
9 * sensors, and manual and automatic fan speed control.
10 *
Jean Delvare51c997d2006-12-12 18:18:29 +010011 * The F71872F/FG is almost the same, with two more voltages monitored,
12 * and 6 VID inputs.
13 *
Jean Delvare9cab0212007-07-15 10:36:06 +020014 * The F71806F/FG is essentially the same as the F71872F/FG. It even has
15 * the same chip ID, so the driver can't differentiate between.
Jean Delvaree53004e2006-01-09 23:26:14 +010016 */
17
Joe Perchese54c5ad2010-10-20 06:51:33 +000018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Jean Delvaree53004e2006-01-09 23:26:14 +010020#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/slab.h>
23#include <linux/jiffies.h>
24#include <linux/platform_device.h>
25#include <linux/hwmon.h>
26#include <linux/hwmon-sysfs.h>
27#include <linux/err.h>
Jean Delvaref0819182006-01-18 23:20:53 +010028#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020029#include <linux/sysfs.h>
Jean Delvarece7ee4e2007-05-08 17:21:59 +020030#include <linux/ioport.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010031#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020032#include <linux/io.h>
Jean Delvaree53004e2006-01-09 23:26:14 +010033
Jean Delvare67b671b2007-12-06 23:13:42 +010034static unsigned short force_id;
35module_param(force_id, ushort, 0);
36MODULE_PARM_DESC(force_id, "Override the detected device ID");
37
Jean Delvaree53004e2006-01-09 23:26:14 +010038static struct platform_device *pdev;
39
40#define DRVNAME "f71805f"
Jean Delvare51c997d2006-12-12 18:18:29 +010041enum kinds { f71805f, f71872f };
Jean Delvaree53004e2006-01-09 23:26:14 +010042
43/*
44 * Super-I/O constants and functions
45 */
46
47#define F71805F_LD_HWM 0x04
48
49#define SIO_REG_LDSEL 0x07 /* Logical device select */
50#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
51#define SIO_REG_DEVREV 0x22 /* Device revision */
52#define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
Jean Delvare51c997d2006-12-12 18:18:29 +010053#define SIO_REG_FNSEL1 0x29 /* Multi Function Select 1 (F71872F) */
Jean Delvaree53004e2006-01-09 23:26:14 +010054#define SIO_REG_ENABLE 0x30 /* Logical device enable */
55#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
56
57#define SIO_FINTEK_ID 0x1934
58#define SIO_F71805F_ID 0x0406
Jean Delvare51c997d2006-12-12 18:18:29 +010059#define SIO_F71872F_ID 0x0341
Jean Delvaree53004e2006-01-09 23:26:14 +010060
61static inline int
62superio_inb(int base, int reg)
63{
64 outb(reg, base);
65 return inb(base + 1);
66}
67
68static int
69superio_inw(int base, int reg)
70{
71 int val;
72 outb(reg++, base);
73 val = inb(base + 1) << 8;
74 outb(reg, base);
75 val |= inb(base + 1);
76 return val;
77}
78
79static inline void
80superio_select(int base, int ld)
81{
82 outb(SIO_REG_LDSEL, base);
83 outb(ld, base + 1);
84}
85
Guenter Roeck73e6ff72019-04-04 10:52:43 -070086static inline int
Jean Delvaree53004e2006-01-09 23:26:14 +010087superio_enter(int base)
88{
Guenter Roeck73e6ff72019-04-04 10:52:43 -070089 if (!request_muxed_region(base, 2, DRVNAME))
90 return -EBUSY;
91
Jean Delvaree53004e2006-01-09 23:26:14 +010092 outb(0x87, base);
93 outb(0x87, base);
Guenter Roeck73e6ff72019-04-04 10:52:43 -070094
95 return 0;
Jean Delvaree53004e2006-01-09 23:26:14 +010096}
97
98static inline void
99superio_exit(int base)
100{
101 outb(0xaa, base);
Guenter Roeck73e6ff72019-04-04 10:52:43 -0700102 release_region(base, 2);
Jean Delvaree53004e2006-01-09 23:26:14 +0100103}
104
105/*
106 * ISA constants
107 */
108
Jean Delvare75c99022006-12-12 18:18:29 +0100109#define REGION_LENGTH 8
110#define ADDR_REG_OFFSET 5
111#define DATA_REG_OFFSET 6
Jean Delvaree53004e2006-01-09 23:26:14 +0100112
Jean Delvaree53004e2006-01-09 23:26:14 +0100113/*
114 * Registers
115 */
116
Jean Delvare51c997d2006-12-12 18:18:29 +0100117/* in nr from 0 to 10 (8-bit values) */
Jean Delvaree53004e2006-01-09 23:26:14 +0100118#define F71805F_REG_IN(nr) (0x10 + (nr))
Jean Delvare51c997d2006-12-12 18:18:29 +0100119#define F71805F_REG_IN_HIGH(nr) ((nr) < 10 ? 0x40 + 2 * (nr) : 0x2E)
120#define F71805F_REG_IN_LOW(nr) ((nr) < 10 ? 0x41 + 2 * (nr) : 0x2F)
Jean Delvaree53004e2006-01-09 23:26:14 +0100121/* fan nr from 0 to 2 (12-bit values, two registers) */
122#define F71805F_REG_FAN(nr) (0x20 + 2 * (nr))
123#define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr))
Jean Delvare315c7112006-12-12 18:18:27 +0100124#define F71805F_REG_FAN_TARGET(nr) (0x69 + 16 * (nr))
Jean Delvaree53004e2006-01-09 23:26:14 +0100125#define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr))
Jean Delvare6e2bc172006-12-12 18:18:27 +0100126#define F71805F_REG_PWM_FREQ(nr) (0x63 + 16 * (nr))
Jean Delvare95e35312006-12-12 18:18:26 +0100127#define F71805F_REG_PWM_DUTY(nr) (0x6B + 16 * (nr))
Jean Delvaree53004e2006-01-09 23:26:14 +0100128/* temp nr from 0 to 2 (8-bit values) */
129#define F71805F_REG_TEMP(nr) (0x1B + (nr))
130#define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr))
131#define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr))
132#define F71805F_REG_TEMP_MODE 0x01
Phil Endecottaba50732007-06-29 09:19:14 +0200133/* pwm/fan pwmnr from 0 to 2, auto point apnr from 0 to 2 */
134/* map Fintek numbers to our numbers as follows: 9->0, 5->1, 1->2 */
135#define F71805F_REG_PWM_AUTO_POINT_TEMP(pwmnr, apnr) \
136 (0xA0 + 0x10 * (pwmnr) + (2 - (apnr)))
137#define F71805F_REG_PWM_AUTO_POINT_FAN(pwmnr, apnr) \
138 (0xA4 + 0x10 * (pwmnr) + \
139 2 * (2 - (apnr)))
Jean Delvaree53004e2006-01-09 23:26:14 +0100140
141#define F71805F_REG_START 0x00
142/* status nr from 0 to 2 */
143#define F71805F_REG_STATUS(nr) (0x36 + (nr))
144
Jean Delvare6b14a542006-12-12 18:18:26 +0100145/* individual register bits */
Jean Delvaree1967832006-12-12 18:18:27 +0100146#define FAN_CTRL_DC_MODE 0x10
Jean Delvare315c7112006-12-12 18:18:27 +0100147#define FAN_CTRL_LATCH_FULL 0x08
Jean Delvare95e35312006-12-12 18:18:26 +0100148#define FAN_CTRL_MODE_MASK 0x03
149#define FAN_CTRL_MODE_SPEED 0x00
150#define FAN_CTRL_MODE_TEMPERATURE 0x01
151#define FAN_CTRL_MODE_MANUAL 0x02
Jean Delvare6b14a542006-12-12 18:18:26 +0100152
Jean Delvaree53004e2006-01-09 23:26:14 +0100153/*
154 * Data structures and manipulation thereof
155 */
156
Phil Endecottaba50732007-06-29 09:19:14 +0200157struct f71805f_auto_point {
158 u8 temp[3];
159 u16 fan[3];
160};
161
Jean Delvaree53004e2006-01-09 23:26:14 +0100162struct f71805f_data {
163 unsigned short addr;
164 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700165 struct device *hwmon_dev;
Jean Delvaree53004e2006-01-09 23:26:14 +0100166
Jean Delvaref0819182006-01-18 23:20:53 +0100167 struct mutex update_lock;
Jean Delvaree53004e2006-01-09 23:26:14 +0100168 char valid; /* !=0 if following fields are valid */
169 unsigned long last_updated; /* In jiffies */
170 unsigned long last_limits; /* In jiffies */
171
172 /* Register values */
Jean Delvare51c997d2006-12-12 18:18:29 +0100173 u8 in[11];
174 u8 in_high[11];
175 u8 in_low[11];
176 u16 has_in;
Jean Delvaree53004e2006-01-09 23:26:14 +0100177 u16 fan[3];
178 u16 fan_low[3];
Jean Delvare315c7112006-12-12 18:18:27 +0100179 u16 fan_target[3];
Jean Delvare6b14a542006-12-12 18:18:26 +0100180 u8 fan_ctrl[3];
Jean Delvare95e35312006-12-12 18:18:26 +0100181 u8 pwm[3];
Jean Delvare6e2bc172006-12-12 18:18:27 +0100182 u8 pwm_freq[3];
Jean Delvaree53004e2006-01-09 23:26:14 +0100183 u8 temp[3];
184 u8 temp_high[3];
185 u8 temp_hyst[3];
186 u8 temp_mode;
Jean Delvare2d457712006-09-24 20:52:15 +0200187 unsigned long alarms;
Phil Endecottaba50732007-06-29 09:19:14 +0200188 struct f71805f_auto_point auto_points[3];
Jean Delvaree53004e2006-01-09 23:26:14 +0100189};
190
Jean Delvare51c997d2006-12-12 18:18:29 +0100191struct f71805f_sio_data {
192 enum kinds kind;
193 u8 fnsel1;
194};
195
Jean Delvaree53004e2006-01-09 23:26:14 +0100196static inline long in_from_reg(u8 reg)
197{
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100198 return reg * 8;
Jean Delvaree53004e2006-01-09 23:26:14 +0100199}
200
201/* The 2 least significant bits are not used */
202static inline u8 in_to_reg(long val)
203{
204 if (val <= 0)
205 return 0;
206 if (val >= 2016)
207 return 0xfc;
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100208 return ((val + 16) / 32) << 2;
Jean Delvaree53004e2006-01-09 23:26:14 +0100209}
210
211/* in0 is downscaled by a factor 2 internally */
212static inline long in0_from_reg(u8 reg)
213{
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100214 return reg * 16;
Jean Delvaree53004e2006-01-09 23:26:14 +0100215}
216
217static inline u8 in0_to_reg(long val)
218{
219 if (val <= 0)
220 return 0;
221 if (val >= 4032)
222 return 0xfc;
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100223 return ((val + 32) / 64) << 2;
Jean Delvaree53004e2006-01-09 23:26:14 +0100224}
225
226/* The 4 most significant bits are not used */
227static inline long fan_from_reg(u16 reg)
228{
229 reg &= 0xfff;
230 if (!reg || reg == 0xfff)
231 return 0;
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100232 return 1500000 / reg;
Jean Delvaree53004e2006-01-09 23:26:14 +0100233}
234
235static inline u16 fan_to_reg(long rpm)
236{
Guenter Roeck2fff0842012-01-19 11:02:17 -0800237 /*
238 * If the low limit is set below what the chip can measure,
239 * store the largest possible 12-bit value in the registers,
240 * so that no alarm will ever trigger.
241 */
Jean Delvaree53004e2006-01-09 23:26:14 +0100242 if (rpm < 367)
243 return 0xfff;
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100244 return 1500000 / rpm;
Jean Delvaree53004e2006-01-09 23:26:14 +0100245}
246
Jean Delvare6e2bc172006-12-12 18:18:27 +0100247static inline unsigned long pwm_freq_from_reg(u8 reg)
248{
249 unsigned long clock = (reg & 0x80) ? 48000000UL : 1000000UL;
250
251 reg &= 0x7f;
252 if (reg == 0)
253 reg++;
254 return clock / (reg << 8);
255}
256
257static inline u8 pwm_freq_to_reg(unsigned long val)
258{
259 if (val >= 187500) /* The highest we can do */
260 return 0x80;
261 if (val >= 1475) /* Use 48 MHz clock */
262 return 0x80 | (48000000UL / (val << 8));
263 if (val < 31) /* The lowest we can do */
264 return 0x7f;
265 else /* Use 1 MHz clock */
266 return 1000000UL / (val << 8);
267}
268
Jean Delvaree1967832006-12-12 18:18:27 +0100269static inline int pwm_mode_from_reg(u8 reg)
270{
271 return !(reg & FAN_CTRL_DC_MODE);
272}
273
Jean Delvaree53004e2006-01-09 23:26:14 +0100274static inline long temp_from_reg(u8 reg)
275{
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100276 return reg * 1000;
Jean Delvaree53004e2006-01-09 23:26:14 +0100277}
278
279static inline u8 temp_to_reg(long val)
280{
Jean Delvare86b2bbf2012-01-20 10:09:23 -0500281 if (val <= 0)
282 return 0;
283 if (val >= 1000 * 0xff)
284 return 0xff;
285 return (val + 500) / 1000;
Jean Delvaree53004e2006-01-09 23:26:14 +0100286}
287
288/*
289 * Device I/O access
290 */
291
Jean Delvare7f999aa2007-02-14 21:15:03 +0100292/* Must be called with data->update_lock held, except during initialization */
Jean Delvaree53004e2006-01-09 23:26:14 +0100293static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
294{
Jean Delvaree53004e2006-01-09 23:26:14 +0100295 outb(reg, data->addr + ADDR_REG_OFFSET);
Jean Delvare7f999aa2007-02-14 21:15:03 +0100296 return inb(data->addr + DATA_REG_OFFSET);
Jean Delvaree53004e2006-01-09 23:26:14 +0100297}
298
Jean Delvare7f999aa2007-02-14 21:15:03 +0100299/* Must be called with data->update_lock held, except during initialization */
Jean Delvaree53004e2006-01-09 23:26:14 +0100300static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
301{
Jean Delvaree53004e2006-01-09 23:26:14 +0100302 outb(reg, data->addr + ADDR_REG_OFFSET);
303 outb(val, data->addr + DATA_REG_OFFSET);
Jean Delvaree53004e2006-01-09 23:26:14 +0100304}
305
Guenter Roeck2fff0842012-01-19 11:02:17 -0800306/*
307 * It is important to read the MSB first, because doing so latches the
308 * value of the LSB, so we are sure both bytes belong to the same value.
309 * Must be called with data->update_lock held, except during initialization
310 */
Jean Delvaree53004e2006-01-09 23:26:14 +0100311static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
312{
313 u16 val;
314
Jean Delvaree53004e2006-01-09 23:26:14 +0100315 outb(reg, data->addr + ADDR_REG_OFFSET);
316 val = inb(data->addr + DATA_REG_OFFSET) << 8;
317 outb(++reg, data->addr + ADDR_REG_OFFSET);
318 val |= inb(data->addr + DATA_REG_OFFSET);
Jean Delvaree53004e2006-01-09 23:26:14 +0100319
320 return val;
321}
322
Jean Delvare7f999aa2007-02-14 21:15:03 +0100323/* Must be called with data->update_lock held, except during initialization */
Jean Delvaree53004e2006-01-09 23:26:14 +0100324static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
325{
Jean Delvaree53004e2006-01-09 23:26:14 +0100326 outb(reg, data->addr + ADDR_REG_OFFSET);
327 outb(val >> 8, data->addr + DATA_REG_OFFSET);
328 outb(++reg, data->addr + ADDR_REG_OFFSET);
329 outb(val & 0xff, data->addr + DATA_REG_OFFSET);
Jean Delvaree53004e2006-01-09 23:26:14 +0100330}
331
332static struct f71805f_data *f71805f_update_device(struct device *dev)
333{
334 struct f71805f_data *data = dev_get_drvdata(dev);
Phil Endecottaba50732007-06-29 09:19:14 +0200335 int nr, apnr;
Jean Delvaree53004e2006-01-09 23:26:14 +0100336
Jean Delvaref0819182006-01-18 23:20:53 +0100337 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100338
339 /* Limit registers cache is refreshed after 60 seconds */
340 if (time_after(jiffies, data->last_updated + 60 * HZ)
341 || !data->valid) {
Jean Delvare51c997d2006-12-12 18:18:29 +0100342 for (nr = 0; nr < 11; nr++) {
343 if (!(data->has_in & (1 << nr)))
344 continue;
Jean Delvaree53004e2006-01-09 23:26:14 +0100345 data->in_high[nr] = f71805f_read8(data,
346 F71805F_REG_IN_HIGH(nr));
347 data->in_low[nr] = f71805f_read8(data,
348 F71805F_REG_IN_LOW(nr));
349 }
350 for (nr = 0; nr < 3; nr++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100351 data->fan_low[nr] = f71805f_read16(data,
352 F71805F_REG_FAN_LOW(nr));
Jean Delvare315c7112006-12-12 18:18:27 +0100353 data->fan_target[nr] = f71805f_read16(data,
354 F71805F_REG_FAN_TARGET(nr));
Jean Delvare6e2bc172006-12-12 18:18:27 +0100355 data->pwm_freq[nr] = f71805f_read8(data,
356 F71805F_REG_PWM_FREQ(nr));
Jean Delvaree53004e2006-01-09 23:26:14 +0100357 }
358 for (nr = 0; nr < 3; nr++) {
359 data->temp_high[nr] = f71805f_read8(data,
360 F71805F_REG_TEMP_HIGH(nr));
361 data->temp_hyst[nr] = f71805f_read8(data,
362 F71805F_REG_TEMP_HYST(nr));
363 }
364 data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
Phil Endecottaba50732007-06-29 09:19:14 +0200365 for (nr = 0; nr < 3; nr++) {
366 for (apnr = 0; apnr < 3; apnr++) {
367 data->auto_points[nr].temp[apnr] =
368 f71805f_read8(data,
369 F71805F_REG_PWM_AUTO_POINT_TEMP(nr,
370 apnr));
371 data->auto_points[nr].fan[apnr] =
372 f71805f_read16(data,
373 F71805F_REG_PWM_AUTO_POINT_FAN(nr,
374 apnr));
375 }
376 }
Jean Delvaree53004e2006-01-09 23:26:14 +0100377
378 data->last_limits = jiffies;
379 }
380
381 /* Measurement registers cache is refreshed after 1 second */
382 if (time_after(jiffies, data->last_updated + HZ)
383 || !data->valid) {
Jean Delvare51c997d2006-12-12 18:18:29 +0100384 for (nr = 0; nr < 11; nr++) {
385 if (!(data->has_in & (1 << nr)))
386 continue;
Jean Delvaree53004e2006-01-09 23:26:14 +0100387 data->in[nr] = f71805f_read8(data,
388 F71805F_REG_IN(nr));
389 }
390 for (nr = 0; nr < 3; nr++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100391 data->fan[nr] = f71805f_read16(data,
392 F71805F_REG_FAN(nr));
Jean Delvare95e35312006-12-12 18:18:26 +0100393 data->fan_ctrl[nr] = f71805f_read8(data,
394 F71805F_REG_FAN_CTRL(nr));
395 data->pwm[nr] = f71805f_read8(data,
396 F71805F_REG_PWM_DUTY(nr));
Jean Delvaree53004e2006-01-09 23:26:14 +0100397 }
398 for (nr = 0; nr < 3; nr++) {
399 data->temp[nr] = f71805f_read8(data,
400 F71805F_REG_TEMP(nr));
401 }
Jean Delvare2d457712006-09-24 20:52:15 +0200402 data->alarms = f71805f_read8(data, F71805F_REG_STATUS(0))
403 + (f71805f_read8(data, F71805F_REG_STATUS(1)) << 8)
404 + (f71805f_read8(data, F71805F_REG_STATUS(2)) << 16);
Jean Delvaree53004e2006-01-09 23:26:14 +0100405
406 data->last_updated = jiffies;
407 data->valid = 1;
408 }
409
Jean Delvaref0819182006-01-18 23:20:53 +0100410 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100411
412 return data;
413}
414
415/*
416 * Sysfs interface
417 */
418
419static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
420 char *buf)
421{
422 struct f71805f_data *data = f71805f_update_device(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100423 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
424 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100425
Jean Delvare51c997d2006-12-12 18:18:29 +0100426 return sprintf(buf, "%ld\n", in0_from_reg(data->in[nr]));
Jean Delvaree53004e2006-01-09 23:26:14 +0100427}
428
429static ssize_t show_in0_max(struct device *dev, struct device_attribute
430 *devattr, char *buf)
431{
432 struct f71805f_data *data = f71805f_update_device(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100433 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
434 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100435
Jean Delvare51c997d2006-12-12 18:18:29 +0100436 return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[nr]));
Jean Delvaree53004e2006-01-09 23:26:14 +0100437}
438
439static ssize_t show_in0_min(struct device *dev, struct device_attribute
440 *devattr, char *buf)
441{
442 struct f71805f_data *data = f71805f_update_device(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100443 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
444 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100445
Jean Delvare51c997d2006-12-12 18:18:29 +0100446 return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[nr]));
Jean Delvaree53004e2006-01-09 23:26:14 +0100447}
448
449static ssize_t set_in0_max(struct device *dev, struct device_attribute
450 *devattr, const char *buf, size_t count)
451{
452 struct f71805f_data *data = dev_get_drvdata(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100453 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
454 int nr = attr->index;
Guenter Roeck2fff0842012-01-19 11:02:17 -0800455 long val;
456 int err;
457
458 err = kstrtol(buf, 10, &val);
459 if (err)
460 return err;
Jean Delvaree53004e2006-01-09 23:26:14 +0100461
Jean Delvaref0819182006-01-18 23:20:53 +0100462 mutex_lock(&data->update_lock);
Jean Delvare51c997d2006-12-12 18:18:29 +0100463 data->in_high[nr] = in0_to_reg(val);
464 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100465 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100466
467 return count;
468}
469
470static ssize_t set_in0_min(struct device *dev, struct device_attribute
471 *devattr, const char *buf, size_t count)
472{
473 struct f71805f_data *data = dev_get_drvdata(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100474 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
475 int nr = attr->index;
Guenter Roeck2fff0842012-01-19 11:02:17 -0800476 long val;
477 int err;
478
479 err = kstrtol(buf, 10, &val);
480 if (err)
481 return err;
Jean Delvaree53004e2006-01-09 23:26:14 +0100482
Jean Delvaref0819182006-01-18 23:20:53 +0100483 mutex_lock(&data->update_lock);
Jean Delvare51c997d2006-12-12 18:18:29 +0100484 data->in_low[nr] = in0_to_reg(val);
485 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100486 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100487
488 return count;
489}
490
Jean Delvaree53004e2006-01-09 23:26:14 +0100491static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
492 char *buf)
493{
494 struct f71805f_data *data = f71805f_update_device(dev);
495 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
496 int nr = attr->index;
497
498 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
499}
500
501static ssize_t show_in_max(struct device *dev, struct device_attribute
502 *devattr, char *buf)
503{
504 struct f71805f_data *data = f71805f_update_device(dev);
505 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
506 int nr = attr->index;
507
508 return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
509}
510
511static ssize_t show_in_min(struct device *dev, struct device_attribute
512 *devattr, char *buf)
513{
514 struct f71805f_data *data = f71805f_update_device(dev);
515 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
516 int nr = attr->index;
517
518 return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr]));
519}
520
521static ssize_t set_in_max(struct device *dev, struct device_attribute
522 *devattr, const char *buf, size_t count)
523{
524 struct f71805f_data *data = dev_get_drvdata(dev);
525 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
526 int nr = attr->index;
Guenter Roeck2fff0842012-01-19 11:02:17 -0800527 long val;
528 int err;
529
530 err = kstrtol(buf, 10, &val);
531 if (err)
532 return err;
Jean Delvaree53004e2006-01-09 23:26:14 +0100533
Jean Delvaref0819182006-01-18 23:20:53 +0100534 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100535 data->in_high[nr] = in_to_reg(val);
536 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100537 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100538
539 return count;
540}
541
542static ssize_t set_in_min(struct device *dev, struct device_attribute
543 *devattr, const char *buf, size_t count)
544{
545 struct f71805f_data *data = dev_get_drvdata(dev);
546 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
547 int nr = attr->index;
Guenter Roeck2fff0842012-01-19 11:02:17 -0800548 long val;
549 int err;
550
551 err = kstrtol(buf, 10, &val);
552 if (err)
553 return err;
Jean Delvaree53004e2006-01-09 23:26:14 +0100554
Jean Delvaref0819182006-01-18 23:20:53 +0100555 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100556 data->in_low[nr] = in_to_reg(val);
557 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100558 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100559
560 return count;
561}
562
Jean Delvaree53004e2006-01-09 23:26:14 +0100563static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
564 char *buf)
565{
566 struct f71805f_data *data = f71805f_update_device(dev);
567 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
568 int nr = attr->index;
569
570 return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
571}
572
573static ssize_t show_fan_min(struct device *dev, struct device_attribute
574 *devattr, char *buf)
575{
576 struct f71805f_data *data = f71805f_update_device(dev);
577 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
578 int nr = attr->index;
579
580 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
581}
582
Jean Delvare315c7112006-12-12 18:18:27 +0100583static ssize_t show_fan_target(struct device *dev, struct device_attribute
584 *devattr, char *buf)
585{
586 struct f71805f_data *data = f71805f_update_device(dev);
587 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
588 int nr = attr->index;
589
590 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_target[nr]));
591}
592
Jean Delvaree53004e2006-01-09 23:26:14 +0100593static ssize_t set_fan_min(struct device *dev, struct device_attribute
594 *devattr, const char *buf, size_t count)
595{
596 struct f71805f_data *data = dev_get_drvdata(dev);
597 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
598 int nr = attr->index;
Guenter Roeck2fff0842012-01-19 11:02:17 -0800599 long val;
600 int err;
601
602 err = kstrtol(buf, 10, &val);
603 if (err)
604 return err;
Jean Delvaree53004e2006-01-09 23:26:14 +0100605
Jean Delvaref0819182006-01-18 23:20:53 +0100606 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100607 data->fan_low[nr] = fan_to_reg(val);
608 f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100609 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100610
611 return count;
612}
613
Jean Delvare315c7112006-12-12 18:18:27 +0100614static ssize_t set_fan_target(struct device *dev, struct device_attribute
615 *devattr, const char *buf, size_t count)
616{
617 struct f71805f_data *data = dev_get_drvdata(dev);
618 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
619 int nr = attr->index;
Guenter Roeck2fff0842012-01-19 11:02:17 -0800620 long val;
621 int err;
622
623 err = kstrtol(buf, 10, &val);
624 if (err)
625 return err;
Jean Delvare315c7112006-12-12 18:18:27 +0100626
627 mutex_lock(&data->update_lock);
628 data->fan_target[nr] = fan_to_reg(val);
629 f71805f_write16(data, F71805F_REG_FAN_TARGET(nr),
630 data->fan_target[nr]);
631 mutex_unlock(&data->update_lock);
632
633 return count;
634}
635
Jean Delvare95e35312006-12-12 18:18:26 +0100636static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
637 char *buf)
638{
639 struct f71805f_data *data = f71805f_update_device(dev);
640 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
641 int nr = attr->index;
642
643 return sprintf(buf, "%d\n", (int)data->pwm[nr]);
644}
645
646static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
647 *devattr, char *buf)
648{
649 struct f71805f_data *data = f71805f_update_device(dev);
650 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
651 int nr = attr->index;
652 int mode;
653
654 switch (data->fan_ctrl[nr] & FAN_CTRL_MODE_MASK) {
655 case FAN_CTRL_MODE_SPEED:
656 mode = 3;
657 break;
658 case FAN_CTRL_MODE_TEMPERATURE:
659 mode = 2;
660 break;
661 default: /* MANUAL */
662 mode = 1;
663 }
664
665 return sprintf(buf, "%d\n", mode);
666}
667
Jean Delvare6e2bc172006-12-12 18:18:27 +0100668static ssize_t show_pwm_freq(struct device *dev, struct device_attribute
669 *devattr, char *buf)
670{
671 struct f71805f_data *data = f71805f_update_device(dev);
672 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
673 int nr = attr->index;
674
675 return sprintf(buf, "%lu\n", pwm_freq_from_reg(data->pwm_freq[nr]));
676}
677
Jean Delvaree1967832006-12-12 18:18:27 +0100678static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
679 *devattr, char *buf)
680{
681 struct f71805f_data *data = f71805f_update_device(dev);
682 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
683 int nr = attr->index;
684
685 return sprintf(buf, "%d\n", pwm_mode_from_reg(data->fan_ctrl[nr]));
686}
687
Jean Delvare95e35312006-12-12 18:18:26 +0100688static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
689 const char *buf, size_t count)
690{
691 struct f71805f_data *data = dev_get_drvdata(dev);
692 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
693 int nr = attr->index;
Guenter Roeck2fff0842012-01-19 11:02:17 -0800694 unsigned long val;
695 int err;
696
697 err = kstrtoul(buf, 10, &val);
698 if (err)
699 return err;
Jean Delvare95e35312006-12-12 18:18:26 +0100700
701 if (val > 255)
702 return -EINVAL;
703
704 mutex_lock(&data->update_lock);
705 data->pwm[nr] = val;
706 f71805f_write8(data, F71805F_REG_PWM_DUTY(nr), data->pwm[nr]);
707 mutex_unlock(&data->update_lock);
708
709 return count;
710}
711
712static struct attribute *f71805f_attr_pwm[];
713
714static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
715 *devattr, const char *buf, size_t count)
716{
717 struct f71805f_data *data = dev_get_drvdata(dev);
718 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
719 int nr = attr->index;
Jean Delvare95e35312006-12-12 18:18:26 +0100720 u8 reg;
Guenter Roeck2fff0842012-01-19 11:02:17 -0800721 unsigned long val;
722 int err;
723
724 err = kstrtoul(buf, 10, &val);
725 if (err)
726 return err;
Jean Delvare95e35312006-12-12 18:18:26 +0100727
728 if (val < 1 || val > 3)
729 return -EINVAL;
730
731 if (val > 1) { /* Automatic mode, user can't set PWM value */
732 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
733 S_IRUGO))
734 dev_dbg(dev, "chmod -w pwm%d failed\n", nr + 1);
735 }
736
737 mutex_lock(&data->update_lock);
738 reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(nr))
739 & ~FAN_CTRL_MODE_MASK;
740 switch (val) {
741 case 1:
742 reg |= FAN_CTRL_MODE_MANUAL;
743 break;
744 case 2:
745 reg |= FAN_CTRL_MODE_TEMPERATURE;
746 break;
747 case 3:
748 reg |= FAN_CTRL_MODE_SPEED;
749 break;
750 }
751 data->fan_ctrl[nr] = reg;
752 f71805f_write8(data, F71805F_REG_FAN_CTRL(nr), reg);
753 mutex_unlock(&data->update_lock);
754
755 if (val == 1) { /* Manual mode, user can set PWM value */
756 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
757 S_IRUGO | S_IWUSR))
758 dev_dbg(dev, "chmod +w pwm%d failed\n", nr + 1);
759 }
760
761 return count;
762}
763
Jean Delvare6e2bc172006-12-12 18:18:27 +0100764static ssize_t set_pwm_freq(struct device *dev, struct device_attribute
765 *devattr, const char *buf, size_t count)
766{
767 struct f71805f_data *data = dev_get_drvdata(dev);
768 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
769 int nr = attr->index;
Guenter Roeck2fff0842012-01-19 11:02:17 -0800770 unsigned long val;
771 int err;
772
773 err = kstrtoul(buf, 10, &val);
774 if (err)
775 return err;
Jean Delvare6e2bc172006-12-12 18:18:27 +0100776
777 mutex_lock(&data->update_lock);
778 data->pwm_freq[nr] = pwm_freq_to_reg(val);
779 f71805f_write8(data, F71805F_REG_PWM_FREQ(nr), data->pwm_freq[nr]);
780 mutex_unlock(&data->update_lock);
781
782 return count;
783}
784
Phil Endecottaba50732007-06-29 09:19:14 +0200785static ssize_t show_pwm_auto_point_temp(struct device *dev,
786 struct device_attribute *devattr,
Guenter Roeck2fff0842012-01-19 11:02:17 -0800787 char *buf)
Phil Endecottaba50732007-06-29 09:19:14 +0200788{
789 struct f71805f_data *data = dev_get_drvdata(dev);
790 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
791 int pwmnr = attr->nr;
792 int apnr = attr->index;
793
794 return sprintf(buf, "%ld\n",
795 temp_from_reg(data->auto_points[pwmnr].temp[apnr]));
796}
797
798static ssize_t set_pwm_auto_point_temp(struct device *dev,
799 struct device_attribute *devattr,
Guenter Roeck2fff0842012-01-19 11:02:17 -0800800 const char *buf, size_t count)
Phil Endecottaba50732007-06-29 09:19:14 +0200801{
802 struct f71805f_data *data = dev_get_drvdata(dev);
803 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
804 int pwmnr = attr->nr;
805 int apnr = attr->index;
Guenter Roeck2fff0842012-01-19 11:02:17 -0800806 unsigned long val;
807 int err;
808
809 err = kstrtoul(buf, 10, &val);
810 if (err)
811 return err;
Phil Endecottaba50732007-06-29 09:19:14 +0200812
813 mutex_lock(&data->update_lock);
814 data->auto_points[pwmnr].temp[apnr] = temp_to_reg(val);
815 f71805f_write8(data, F71805F_REG_PWM_AUTO_POINT_TEMP(pwmnr, apnr),
816 data->auto_points[pwmnr].temp[apnr]);
817 mutex_unlock(&data->update_lock);
818
819 return count;
820}
821
822static ssize_t show_pwm_auto_point_fan(struct device *dev,
823 struct device_attribute *devattr,
Guenter Roeck2fff0842012-01-19 11:02:17 -0800824 char *buf)
Phil Endecottaba50732007-06-29 09:19:14 +0200825{
826 struct f71805f_data *data = dev_get_drvdata(dev);
827 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
828 int pwmnr = attr->nr;
829 int apnr = attr->index;
830
831 return sprintf(buf, "%ld\n",
832 fan_from_reg(data->auto_points[pwmnr].fan[apnr]));
833}
834
835static ssize_t set_pwm_auto_point_fan(struct device *dev,
836 struct device_attribute *devattr,
Guenter Roeck2fff0842012-01-19 11:02:17 -0800837 const char *buf, size_t count)
Phil Endecottaba50732007-06-29 09:19:14 +0200838{
839 struct f71805f_data *data = dev_get_drvdata(dev);
840 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
841 int pwmnr = attr->nr;
842 int apnr = attr->index;
Guenter Roeck2fff0842012-01-19 11:02:17 -0800843 unsigned long val;
844 int err;
845
846 err = kstrtoul(buf, 10, &val);
847 if (err)
848 return err;
Phil Endecottaba50732007-06-29 09:19:14 +0200849
850 mutex_lock(&data->update_lock);
851 data->auto_points[pwmnr].fan[apnr] = fan_to_reg(val);
852 f71805f_write16(data, F71805F_REG_PWM_AUTO_POINT_FAN(pwmnr, apnr),
Guenter Roeck2fff0842012-01-19 11:02:17 -0800853 data->auto_points[pwmnr].fan[apnr]);
Phil Endecottaba50732007-06-29 09:19:14 +0200854 mutex_unlock(&data->update_lock);
855
856 return count;
857}
858
Jean Delvaree53004e2006-01-09 23:26:14 +0100859static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
860 char *buf)
861{
862 struct f71805f_data *data = f71805f_update_device(dev);
863 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
864 int nr = attr->index;
865
866 return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
867}
868
869static ssize_t show_temp_max(struct device *dev, struct device_attribute
870 *devattr, char *buf)
871{
872 struct f71805f_data *data = f71805f_update_device(dev);
873 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
874 int nr = attr->index;
875
876 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
877}
878
879static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
880 *devattr, char *buf)
881{
882 struct f71805f_data *data = f71805f_update_device(dev);
883 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
884 int nr = attr->index;
885
886 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
887}
888
889static ssize_t show_temp_type(struct device *dev, struct device_attribute
890 *devattr, char *buf)
891{
892 struct f71805f_data *data = f71805f_update_device(dev);
893 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
894 int nr = attr->index;
895
896 /* 3 is diode, 4 is thermistor */
897 return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
898}
899
900static ssize_t set_temp_max(struct device *dev, struct device_attribute
901 *devattr, const char *buf, size_t count)
902{
903 struct f71805f_data *data = dev_get_drvdata(dev);
904 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
905 int nr = attr->index;
Guenter Roeck2fff0842012-01-19 11:02:17 -0800906 long val;
907 int err;
908
909 err = kstrtol(buf, 10, &val);
910 if (err)
911 return err;
Jean Delvaree53004e2006-01-09 23:26:14 +0100912
Jean Delvaref0819182006-01-18 23:20:53 +0100913 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100914 data->temp_high[nr] = temp_to_reg(val);
915 f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100916 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100917
918 return count;
919}
920
921static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
922 *devattr, const char *buf, size_t count)
923{
924 struct f71805f_data *data = dev_get_drvdata(dev);
925 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
926 int nr = attr->index;
Guenter Roeck2fff0842012-01-19 11:02:17 -0800927 long val;
928 int err;
929
930 err = kstrtol(buf, 10, &val);
931 if (err)
932 return err;
Jean Delvaree53004e2006-01-09 23:26:14 +0100933
Jean Delvaref0819182006-01-18 23:20:53 +0100934 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100935 data->temp_hyst[nr] = temp_to_reg(val);
936 f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100937 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100938
939 return count;
940}
941
Julia Lawall8258e492016-12-22 13:05:16 +0100942static ssize_t alarms_in_show(struct device *dev, struct device_attribute
Jean Delvaree53004e2006-01-09 23:26:14 +0100943 *devattr, char *buf)
944{
945 struct f71805f_data *data = f71805f_update_device(dev);
946
Jean Delvare51c997d2006-12-12 18:18:29 +0100947 return sprintf(buf, "%lu\n", data->alarms & 0x7ff);
Jean Delvaree53004e2006-01-09 23:26:14 +0100948}
949
Julia Lawall8258e492016-12-22 13:05:16 +0100950static ssize_t alarms_fan_show(struct device *dev, struct device_attribute
Jean Delvaree53004e2006-01-09 23:26:14 +0100951 *devattr, char *buf)
952{
953 struct f71805f_data *data = f71805f_update_device(dev);
954
Jean Delvare2d457712006-09-24 20:52:15 +0200955 return sprintf(buf, "%lu\n", (data->alarms >> 16) & 0x07);
Jean Delvaree53004e2006-01-09 23:26:14 +0100956}
957
Julia Lawall8258e492016-12-22 13:05:16 +0100958static ssize_t alarms_temp_show(struct device *dev, struct device_attribute
Jean Delvaree53004e2006-01-09 23:26:14 +0100959 *devattr, char *buf)
960{
961 struct f71805f_data *data = f71805f_update_device(dev);
962
Jean Delvare2d457712006-09-24 20:52:15 +0200963 return sprintf(buf, "%lu\n", (data->alarms >> 11) & 0x07);
964}
965
966static ssize_t show_alarm(struct device *dev, struct device_attribute
967 *devattr, char *buf)
968{
969 struct f71805f_data *data = f71805f_update_device(dev);
970 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
971 int bitnr = attr->index;
972
973 return sprintf(buf, "%lu\n", (data->alarms >> bitnr) & 1);
Jean Delvaree53004e2006-01-09 23:26:14 +0100974}
975
Julia Lawall8258e492016-12-22 13:05:16 +0100976static ssize_t name_show(struct device *dev, struct device_attribute
Jean Delvaree53004e2006-01-09 23:26:14 +0100977 *devattr, char *buf)
978{
979 struct f71805f_data *data = dev_get_drvdata(dev);
980
981 return sprintf(buf, "%s\n", data->name);
982}
983
Jean Delvare51c997d2006-12-12 18:18:29 +0100984static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in0, NULL, 0);
Guenter Roeck2fff0842012-01-19 11:02:17 -0800985static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
Jean Delvare51c997d2006-12-12 18:18:29 +0100986 show_in0_max, set_in0_max, 0);
Guenter Roeck2fff0842012-01-19 11:02:17 -0800987static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
Jean Delvare51c997d2006-12-12 18:18:29 +0100988 show_in0_min, set_in0_min, 0);
Jean Delvare0e39e012006-09-24 21:16:40 +0200989static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
990static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
991 show_in_max, set_in_max, 1);
992static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
993 show_in_min, set_in_min, 1);
994static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
995static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
996 show_in_max, set_in_max, 2);
997static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
998 show_in_min, set_in_min, 2);
999static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
1000static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
1001 show_in_max, set_in_max, 3);
1002static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
1003 show_in_min, set_in_min, 3);
1004static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
1005static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
1006 show_in_max, set_in_max, 4);
1007static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
1008 show_in_min, set_in_min, 4);
1009static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
1010static SENSOR_DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR,
1011 show_in_max, set_in_max, 5);
1012static SENSOR_DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR,
1013 show_in_min, set_in_min, 5);
1014static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
1015static SENSOR_DEVICE_ATTR(in6_max, S_IRUGO | S_IWUSR,
1016 show_in_max, set_in_max, 6);
1017static SENSOR_DEVICE_ATTR(in6_min, S_IRUGO | S_IWUSR,
1018 show_in_min, set_in_min, 6);
1019static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
1020static SENSOR_DEVICE_ATTR(in7_max, S_IRUGO | S_IWUSR,
1021 show_in_max, set_in_max, 7);
1022static SENSOR_DEVICE_ATTR(in7_min, S_IRUGO | S_IWUSR,
1023 show_in_min, set_in_min, 7);
1024static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
1025static SENSOR_DEVICE_ATTR(in8_max, S_IRUGO | S_IWUSR,
1026 show_in_max, set_in_max, 8);
1027static SENSOR_DEVICE_ATTR(in8_min, S_IRUGO | S_IWUSR,
1028 show_in_min, set_in_min, 8);
Jean Delvare51c997d2006-12-12 18:18:29 +01001029static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in0, NULL, 9);
1030static SENSOR_DEVICE_ATTR(in9_max, S_IRUGO | S_IWUSR,
1031 show_in0_max, set_in0_max, 9);
1032static SENSOR_DEVICE_ATTR(in9_min, S_IRUGO | S_IWUSR,
1033 show_in0_min, set_in0_min, 9);
1034static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in0, NULL, 10);
1035static SENSOR_DEVICE_ATTR(in10_max, S_IRUGO | S_IWUSR,
1036 show_in0_max, set_in0_max, 10);
1037static SENSOR_DEVICE_ATTR(in10_min, S_IRUGO | S_IWUSR,
1038 show_in0_min, set_in0_min, 10);
Jean Delvare0e39e012006-09-24 21:16:40 +02001039
1040static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
1041static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
1042 show_fan_min, set_fan_min, 0);
Jean Delvare315c7112006-12-12 18:18:27 +01001043static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR,
1044 show_fan_target, set_fan_target, 0);
Jean Delvare0e39e012006-09-24 21:16:40 +02001045static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
1046static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
1047 show_fan_min, set_fan_min, 1);
Jean Delvare315c7112006-12-12 18:18:27 +01001048static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO | S_IWUSR,
1049 show_fan_target, set_fan_target, 1);
Jean Delvare0e39e012006-09-24 21:16:40 +02001050static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
1051static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
1052 show_fan_min, set_fan_min, 2);
Jean Delvare315c7112006-12-12 18:18:27 +01001053static SENSOR_DEVICE_ATTR(fan3_target, S_IRUGO | S_IWUSR,
1054 show_fan_target, set_fan_target, 2);
Jean Delvare0e39e012006-09-24 21:16:40 +02001055
1056static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
1057static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
1058 show_temp_max, set_temp_max, 0);
1059static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
1060 show_temp_hyst, set_temp_hyst, 0);
1061static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0);
1062static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
1063static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
1064 show_temp_max, set_temp_max, 1);
1065static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
1066 show_temp_hyst, set_temp_hyst, 1);
1067static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1);
1068static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
1069static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
1070 show_temp_max, set_temp_max, 2);
1071static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR,
1072 show_temp_hyst, set_temp_hyst, 2);
1073static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2);
1074
Guenter Roeck2fff0842012-01-19 11:02:17 -08001075/*
1076 * pwm (value) files are created read-only, write permission is
1077 * then added or removed dynamically as needed
1078 */
Jean Delvare95e35312006-12-12 18:18:26 +01001079static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO, show_pwm, set_pwm, 0);
1080static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1081 show_pwm_enable, set_pwm_enable, 0);
Jean Delvare6e2bc172006-12-12 18:18:27 +01001082static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR,
1083 show_pwm_freq, set_pwm_freq, 0);
Jean Delvaree1967832006-12-12 18:18:27 +01001084static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
Jean Delvare95e35312006-12-12 18:18:26 +01001085static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO, show_pwm, set_pwm, 1);
1086static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1087 show_pwm_enable, set_pwm_enable, 1);
Jean Delvare6e2bc172006-12-12 18:18:27 +01001088static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO | S_IWUSR,
1089 show_pwm_freq, set_pwm_freq, 1);
Jean Delvaree1967832006-12-12 18:18:27 +01001090static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO, show_pwm_mode, NULL, 1);
Jean Delvare95e35312006-12-12 18:18:26 +01001091static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO, show_pwm, set_pwm, 2);
1092static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1093 show_pwm_enable, set_pwm_enable, 2);
Jean Delvare6e2bc172006-12-12 18:18:27 +01001094static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO | S_IWUSR,
1095 show_pwm_freq, set_pwm_freq, 2);
Jean Delvaree1967832006-12-12 18:18:27 +01001096static SENSOR_DEVICE_ATTR(pwm3_mode, S_IRUGO, show_pwm_mode, NULL, 2);
Jean Delvare95e35312006-12-12 18:18:26 +01001097
Phil Endecottaba50732007-06-29 09:19:14 +02001098static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1099 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1100 0, 0);
1101static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_fan, S_IRUGO | S_IWUSR,
1102 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1103 0, 0);
1104static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1105 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1106 0, 1);
1107static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_fan, S_IRUGO | S_IWUSR,
1108 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1109 0, 1);
1110static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1111 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1112 0, 2);
1113static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_fan, S_IRUGO | S_IWUSR,
1114 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1115 0, 2);
1116
1117static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1118 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1119 1, 0);
1120static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_fan, S_IRUGO | S_IWUSR,
1121 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1122 1, 0);
1123static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1124 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1125 1, 1);
1126static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_fan, S_IRUGO | S_IWUSR,
1127 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1128 1, 1);
1129static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1130 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1131 1, 2);
1132static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_fan, S_IRUGO | S_IWUSR,
1133 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1134 1, 2);
1135
1136static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1137 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1138 2, 0);
1139static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_fan, S_IRUGO | S_IWUSR,
1140 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1141 2, 0);
1142static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1143 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1144 2, 1);
1145static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_fan, S_IRUGO | S_IWUSR,
1146 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1147 2, 1);
1148static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1149 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1150 2, 2);
1151static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_fan, S_IRUGO | S_IWUSR,
1152 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1153 2, 2);
1154
Jean Delvare0e39e012006-09-24 21:16:40 +02001155static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1156static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1157static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1158static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1159static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1160static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1161static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1162static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1163static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
Jean Delvare51c997d2006-12-12 18:18:29 +01001164static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1165static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
Jean Delvare0e39e012006-09-24 21:16:40 +02001166static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 11);
1167static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 12);
1168static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
1169static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
1170static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
1171static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
Julia Lawall8258e492016-12-22 13:05:16 +01001172static DEVICE_ATTR_RO(alarms_in);
1173static DEVICE_ATTR_RO(alarms_fan);
1174static DEVICE_ATTR_RO(alarms_temp);
Jean Delvare0e39e012006-09-24 21:16:40 +02001175
Julia Lawall8258e492016-12-22 13:05:16 +01001176static DEVICE_ATTR_RO(name);
Jean Delvare0e39e012006-09-24 21:16:40 +02001177
1178static struct attribute *f71805f_attributes[] = {
Jean Delvare51c997d2006-12-12 18:18:29 +01001179 &sensor_dev_attr_in0_input.dev_attr.attr,
1180 &sensor_dev_attr_in0_max.dev_attr.attr,
1181 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001182 &sensor_dev_attr_in1_input.dev_attr.attr,
1183 &sensor_dev_attr_in1_max.dev_attr.attr,
1184 &sensor_dev_attr_in1_min.dev_attr.attr,
1185 &sensor_dev_attr_in2_input.dev_attr.attr,
1186 &sensor_dev_attr_in2_max.dev_attr.attr,
1187 &sensor_dev_attr_in2_min.dev_attr.attr,
1188 &sensor_dev_attr_in3_input.dev_attr.attr,
1189 &sensor_dev_attr_in3_max.dev_attr.attr,
1190 &sensor_dev_attr_in3_min.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001191 &sensor_dev_attr_in5_input.dev_attr.attr,
1192 &sensor_dev_attr_in5_max.dev_attr.attr,
1193 &sensor_dev_attr_in5_min.dev_attr.attr,
1194 &sensor_dev_attr_in6_input.dev_attr.attr,
1195 &sensor_dev_attr_in6_max.dev_attr.attr,
1196 &sensor_dev_attr_in6_min.dev_attr.attr,
1197 &sensor_dev_attr_in7_input.dev_attr.attr,
1198 &sensor_dev_attr_in7_max.dev_attr.attr,
1199 &sensor_dev_attr_in7_min.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001200
Jean Delvarec7176cb2006-12-12 18:18:29 +01001201 &sensor_dev_attr_fan1_input.dev_attr.attr,
1202 &sensor_dev_attr_fan1_min.dev_attr.attr,
1203 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1204 &sensor_dev_attr_fan1_target.dev_attr.attr,
1205 &sensor_dev_attr_fan2_input.dev_attr.attr,
1206 &sensor_dev_attr_fan2_min.dev_attr.attr,
1207 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1208 &sensor_dev_attr_fan2_target.dev_attr.attr,
1209 &sensor_dev_attr_fan3_input.dev_attr.attr,
1210 &sensor_dev_attr_fan3_min.dev_attr.attr,
1211 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1212 &sensor_dev_attr_fan3_target.dev_attr.attr,
1213
1214 &sensor_dev_attr_pwm1.dev_attr.attr,
1215 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1216 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
1217 &sensor_dev_attr_pwm2.dev_attr.attr,
1218 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1219 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
1220 &sensor_dev_attr_pwm3.dev_attr.attr,
1221 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1222 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
1223
Jean Delvare0e39e012006-09-24 21:16:40 +02001224 &sensor_dev_attr_temp1_input.dev_attr.attr,
1225 &sensor_dev_attr_temp1_max.dev_attr.attr,
1226 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
1227 &sensor_dev_attr_temp1_type.dev_attr.attr,
1228 &sensor_dev_attr_temp2_input.dev_attr.attr,
1229 &sensor_dev_attr_temp2_max.dev_attr.attr,
1230 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
1231 &sensor_dev_attr_temp2_type.dev_attr.attr,
1232 &sensor_dev_attr_temp3_input.dev_attr.attr,
1233 &sensor_dev_attr_temp3_max.dev_attr.attr,
1234 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
1235 &sensor_dev_attr_temp3_type.dev_attr.attr,
1236
Phil Endecottaba50732007-06-29 09:19:14 +02001237 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1238 &sensor_dev_attr_pwm1_auto_point1_fan.dev_attr.attr,
1239 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1240 &sensor_dev_attr_pwm1_auto_point2_fan.dev_attr.attr,
1241 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1242 &sensor_dev_attr_pwm1_auto_point3_fan.dev_attr.attr,
1243 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1244 &sensor_dev_attr_pwm2_auto_point1_fan.dev_attr.attr,
1245 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1246 &sensor_dev_attr_pwm2_auto_point2_fan.dev_attr.attr,
1247 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1248 &sensor_dev_attr_pwm2_auto_point3_fan.dev_attr.attr,
1249 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1250 &sensor_dev_attr_pwm3_auto_point1_fan.dev_attr.attr,
1251 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1252 &sensor_dev_attr_pwm3_auto_point2_fan.dev_attr.attr,
1253 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1254 &sensor_dev_attr_pwm3_auto_point3_fan.dev_attr.attr,
1255
Jean Delvare0e39e012006-09-24 21:16:40 +02001256 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1257 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1258 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1259 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001260 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1261 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1262 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001263 &dev_attr_alarms_in.attr,
1264 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1265 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1266 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1267 &dev_attr_alarms_temp.attr,
1268 &dev_attr_alarms_fan.attr,
1269
1270 &dev_attr_name.attr,
1271 NULL
Jean Delvare2488a392006-01-09 23:29:11 +01001272};
1273
Jean Delvare0e39e012006-09-24 21:16:40 +02001274static const struct attribute_group f71805f_group = {
1275 .attrs = f71805f_attributes,
Jean Delvare2488a392006-01-09 23:29:11 +01001276};
1277
Jean Delvare51c997d2006-12-12 18:18:29 +01001278static struct attribute *f71805f_attributes_optin[4][5] = {
1279 {
1280 &sensor_dev_attr_in4_input.dev_attr.attr,
1281 &sensor_dev_attr_in4_max.dev_attr.attr,
1282 &sensor_dev_attr_in4_min.dev_attr.attr,
1283 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1284 NULL
1285 }, {
1286 &sensor_dev_attr_in8_input.dev_attr.attr,
1287 &sensor_dev_attr_in8_max.dev_attr.attr,
1288 &sensor_dev_attr_in8_min.dev_attr.attr,
1289 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1290 NULL
1291 }, {
1292 &sensor_dev_attr_in9_input.dev_attr.attr,
1293 &sensor_dev_attr_in9_max.dev_attr.attr,
1294 &sensor_dev_attr_in9_min.dev_attr.attr,
1295 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1296 NULL
1297 }, {
1298 &sensor_dev_attr_in10_input.dev_attr.attr,
1299 &sensor_dev_attr_in10_max.dev_attr.attr,
1300 &sensor_dev_attr_in10_min.dev_attr.attr,
1301 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1302 NULL
1303 }
1304};
1305
1306static const struct attribute_group f71805f_group_optin[4] = {
1307 { .attrs = f71805f_attributes_optin[0] },
1308 { .attrs = f71805f_attributes_optin[1] },
1309 { .attrs = f71805f_attributes_optin[2] },
1310 { .attrs = f71805f_attributes_optin[3] },
1311};
1312
Guenter Roeck2fff0842012-01-19 11:02:17 -08001313/*
1314 * We don't include pwm_freq files in the arrays above, because they must be
1315 * created conditionally (only if pwm_mode is 1 == PWM)
1316 */
Jean Delvaree1967832006-12-12 18:18:27 +01001317static struct attribute *f71805f_attributes_pwm_freq[] = {
1318 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1319 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1320 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1321 NULL
1322};
1323
1324static const struct attribute_group f71805f_group_pwm_freq = {
1325 .attrs = f71805f_attributes_pwm_freq,
1326};
1327
Jean Delvare95e35312006-12-12 18:18:26 +01001328/* We also need an indexed access to pwmN files to toggle writability */
1329static struct attribute *f71805f_attr_pwm[] = {
1330 &sensor_dev_attr_pwm1.dev_attr.attr,
1331 &sensor_dev_attr_pwm2.dev_attr.attr,
1332 &sensor_dev_attr_pwm3.dev_attr.attr,
1333};
1334
Jean Delvaree53004e2006-01-09 23:26:14 +01001335/*
1336 * Device registration and initialization
1337 */
1338
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001339static void f71805f_init_device(struct f71805f_data *data)
Jean Delvaree53004e2006-01-09 23:26:14 +01001340{
1341 u8 reg;
1342 int i;
1343
1344 reg = f71805f_read8(data, F71805F_REG_START);
1345 if ((reg & 0x41) != 0x01) {
Guenter Roeck692fe502013-01-10 05:49:55 -08001346 pr_debug("Starting monitoring operations\n");
Jean Delvaree53004e2006-01-09 23:26:14 +01001347 f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
1348 }
1349
Guenter Roeck2fff0842012-01-19 11:02:17 -08001350 /*
1351 * Fan monitoring can be disabled. If it is, we won't be polling
1352 * the register values, and won't create the related sysfs files.
1353 */
Jean Delvaree53004e2006-01-09 23:26:14 +01001354 for (i = 0; i < 3; i++) {
Jean Delvare6b14a542006-12-12 18:18:26 +01001355 data->fan_ctrl[i] = f71805f_read8(data,
1356 F71805F_REG_FAN_CTRL(i));
Guenter Roeck2fff0842012-01-19 11:02:17 -08001357 /*
1358 * Clear latch full bit, else "speed mode" fan speed control
1359 * doesn't work
1360 */
Jean Delvare315c7112006-12-12 18:18:27 +01001361 if (data->fan_ctrl[i] & FAN_CTRL_LATCH_FULL) {
1362 data->fan_ctrl[i] &= ~FAN_CTRL_LATCH_FULL;
1363 f71805f_write8(data, F71805F_REG_FAN_CTRL(i),
1364 data->fan_ctrl[i]);
1365 }
Jean Delvaree53004e2006-01-09 23:26:14 +01001366 }
1367}
1368
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001369static int f71805f_probe(struct platform_device *pdev)
Jean Delvaree53004e2006-01-09 23:26:14 +01001370{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09001371 struct f71805f_sio_data *sio_data = dev_get_platdata(&pdev->dev);
Jean Delvaree53004e2006-01-09 23:26:14 +01001372 struct f71805f_data *data;
1373 struct resource *res;
Jean Delvare2488a392006-01-09 23:29:11 +01001374 int i, err;
Jean Delvaree53004e2006-01-09 23:26:14 +01001375
Guenter Roeck2fff0842012-01-19 11:02:17 -08001376 static const char * const names[] = {
Jean Delvare51c997d2006-12-12 18:18:29 +01001377 "f71805f",
1378 "f71872f",
1379 };
1380
Guenter Roeckadc14132012-06-02 09:58:05 -07001381 data = devm_kzalloc(&pdev->dev, sizeof(struct f71805f_data),
1382 GFP_KERNEL);
Jingoo Hanb83207f2014-04-29 17:06:59 +09001383 if (!data)
Guenter Roeckadc14132012-06-02 09:58:05 -07001384 return -ENOMEM;
Jean Delvaree53004e2006-01-09 23:26:14 +01001385
1386 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeckadc14132012-06-02 09:58:05 -07001387 if (!devm_request_region(&pdev->dev, res->start + ADDR_REG_OFFSET, 2,
1388 DRVNAME)) {
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001389 dev_err(&pdev->dev, "Failed to request region 0x%lx-0x%lx\n",
1390 (unsigned long)(res->start + ADDR_REG_OFFSET),
1391 (unsigned long)(res->start + ADDR_REG_OFFSET + 1));
Guenter Roeckadc14132012-06-02 09:58:05 -07001392 return -EBUSY;
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001393 }
Jean Delvaree53004e2006-01-09 23:26:14 +01001394 data->addr = res->start;
Jean Delvare51c997d2006-12-12 18:18:29 +01001395 data->name = names[sio_data->kind];
Jean Delvaref0819182006-01-18 23:20:53 +01001396 mutex_init(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +01001397
1398 platform_set_drvdata(pdev, data);
1399
Jean Delvare51c997d2006-12-12 18:18:29 +01001400 /* Some voltage inputs depend on chip model and configuration */
1401 switch (sio_data->kind) {
1402 case f71805f:
1403 data->has_in = 0x1ff;
1404 break;
1405 case f71872f:
1406 data->has_in = 0x6ef;
1407 if (sio_data->fnsel1 & 0x01)
1408 data->has_in |= (1 << 4); /* in4 */
1409 if (sio_data->fnsel1 & 0x02)
1410 data->has_in |= (1 << 8); /* in8 */
1411 break;
1412 }
1413
Jean Delvaree53004e2006-01-09 23:26:14 +01001414 /* Initialize the F71805F chip */
1415 f71805f_init_device(data);
1416
1417 /* Register sysfs interface files */
Guenter Roeck2fff0842012-01-19 11:02:17 -08001418 err = sysfs_create_group(&pdev->dev.kobj, &f71805f_group);
1419 if (err)
Guenter Roeckadc14132012-06-02 09:58:05 -07001420 return err;
Jean Delvare51c997d2006-12-12 18:18:29 +01001421 if (data->has_in & (1 << 4)) { /* in4 */
Guenter Roeck2fff0842012-01-19 11:02:17 -08001422 err = sysfs_create_group(&pdev->dev.kobj,
1423 &f71805f_group_optin[0]);
1424 if (err)
Jean Delvare51c997d2006-12-12 18:18:29 +01001425 goto exit_remove_files;
1426 }
1427 if (data->has_in & (1 << 8)) { /* in8 */
Guenter Roeck2fff0842012-01-19 11:02:17 -08001428 err = sysfs_create_group(&pdev->dev.kobj,
1429 &f71805f_group_optin[1]);
1430 if (err)
Jean Delvare51c997d2006-12-12 18:18:29 +01001431 goto exit_remove_files;
1432 }
1433 if (data->has_in & (1 << 9)) { /* in9 (F71872F/FG only) */
Guenter Roeck2fff0842012-01-19 11:02:17 -08001434 err = sysfs_create_group(&pdev->dev.kobj,
1435 &f71805f_group_optin[2]);
1436 if (err)
Jean Delvare51c997d2006-12-12 18:18:29 +01001437 goto exit_remove_files;
1438 }
1439 if (data->has_in & (1 << 10)) { /* in9 (F71872F/FG only) */
Guenter Roeck2fff0842012-01-19 11:02:17 -08001440 err = sysfs_create_group(&pdev->dev.kobj,
1441 &f71805f_group_optin[3]);
1442 if (err)
Jean Delvare51c997d2006-12-12 18:18:29 +01001443 goto exit_remove_files;
1444 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001445 for (i = 0; i < 3; i++) {
Jean Delvaree1967832006-12-12 18:18:27 +01001446 /* If control mode is PWM, create pwm_freq file */
1447 if (!(data->fan_ctrl[i] & FAN_CTRL_DC_MODE)) {
Guenter Roeck2fff0842012-01-19 11:02:17 -08001448 err = sysfs_create_file(&pdev->dev.kobj,
1449 f71805f_attributes_pwm_freq[i]);
1450 if (err)
Jean Delvaree1967832006-12-12 18:18:27 +01001451 goto exit_remove_files;
1452 }
Jean Delvare95e35312006-12-12 18:18:26 +01001453 /* If PWM is in manual mode, add write permission */
1454 if (data->fan_ctrl[i] & FAN_CTRL_MODE_MANUAL) {
Guenter Roeck2fff0842012-01-19 11:02:17 -08001455 err = sysfs_chmod_file(&pdev->dev.kobj,
1456 f71805f_attr_pwm[i],
1457 S_IRUGO | S_IWUSR);
1458 if (err) {
Jean Delvare95e35312006-12-12 18:18:26 +01001459 dev_err(&pdev->dev, "chmod +w pwm%d failed\n",
1460 i + 1);
1461 goto exit_remove_files;
1462 }
1463 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001464 }
1465
Tony Jones1beeffe2007-08-20 13:46:20 -07001466 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1467 if (IS_ERR(data->hwmon_dev)) {
1468 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001469 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
1470 goto exit_remove_files;
Jean Delvaree53004e2006-01-09 23:26:14 +01001471 }
Jean Delvaree53004e2006-01-09 23:26:14 +01001472
1473 return 0;
1474
Jean Delvare0e39e012006-09-24 21:16:40 +02001475exit_remove_files:
1476 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
Jean Delvare51c997d2006-12-12 18:18:29 +01001477 for (i = 0; i < 4; i++)
1478 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_optin[i]);
Jean Delvaree1967832006-12-12 18:18:27 +01001479 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_pwm_freq);
Jean Delvaree53004e2006-01-09 23:26:14 +01001480 return err;
1481}
1482
Bill Pemberton281dfd02012-11-19 13:25:51 -05001483static int f71805f_remove(struct platform_device *pdev)
Jean Delvaree53004e2006-01-09 23:26:14 +01001484{
1485 struct f71805f_data *data = platform_get_drvdata(pdev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001486 int i;
Jean Delvaree53004e2006-01-09 23:26:14 +01001487
Tony Jones1beeffe2007-08-20 13:46:20 -07001488 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001489 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
Jean Delvare51c997d2006-12-12 18:18:29 +01001490 for (i = 0; i < 4; i++)
1491 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_optin[i]);
Jean Delvaree1967832006-12-12 18:18:27 +01001492 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_pwm_freq);
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001493
Jean Delvaree53004e2006-01-09 23:26:14 +01001494 return 0;
1495}
1496
1497static struct platform_driver f71805f_driver = {
1498 .driver = {
Jean Delvaree53004e2006-01-09 23:26:14 +01001499 .name = DRVNAME,
1500 },
1501 .probe = f71805f_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -05001502 .remove = f71805f_remove,
Jean Delvaree53004e2006-01-09 23:26:14 +01001503};
1504
Jean Delvare51c997d2006-12-12 18:18:29 +01001505static int __init f71805f_device_add(unsigned short address,
1506 const struct f71805f_sio_data *sio_data)
Jean Delvaree53004e2006-01-09 23:26:14 +01001507{
Jean Delvare568825c2006-03-23 16:40:23 +01001508 struct resource res = {
1509 .start = address,
1510 .end = address + REGION_LENGTH - 1,
1511 .flags = IORESOURCE_IO,
1512 };
Jean Delvaree53004e2006-01-09 23:26:14 +01001513 int err;
1514
1515 pdev = platform_device_alloc(DRVNAME, address);
1516 if (!pdev) {
1517 err = -ENOMEM;
Joe Perchese54c5ad2010-10-20 06:51:33 +00001518 pr_err("Device allocation failed\n");
Jean Delvaree53004e2006-01-09 23:26:14 +01001519 goto exit;
1520 }
1521
Jean Delvare568825c2006-03-23 16:40:23 +01001522 res.name = pdev->name;
Jean Delvareb9acb642009-01-07 16:37:35 +01001523 err = acpi_check_resource_conflict(&res);
1524 if (err)
1525 goto exit_device_put;
1526
Jean Delvare568825c2006-03-23 16:40:23 +01001527 err = platform_device_add_resources(pdev, &res, 1);
Jean Delvaree53004e2006-01-09 23:26:14 +01001528 if (err) {
Joe Perchese54c5ad2010-10-20 06:51:33 +00001529 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvaree53004e2006-01-09 23:26:14 +01001530 goto exit_device_put;
1531 }
1532
Jean Delvare2df6d812007-06-09 10:11:16 -04001533 err = platform_device_add_data(pdev, sio_data,
1534 sizeof(struct f71805f_sio_data));
1535 if (err) {
Joe Perchese54c5ad2010-10-20 06:51:33 +00001536 pr_err("Platform data allocation failed\n");
Jean Delvare51c997d2006-12-12 18:18:29 +01001537 goto exit_device_put;
1538 }
Jean Delvare51c997d2006-12-12 18:18:29 +01001539
Jean Delvaree53004e2006-01-09 23:26:14 +01001540 err = platform_device_add(pdev);
1541 if (err) {
Joe Perchese54c5ad2010-10-20 06:51:33 +00001542 pr_err("Device addition failed (%d)\n", err);
Jean Delvarea117ddd2007-02-14 21:15:05 +01001543 goto exit_device_put;
Jean Delvaree53004e2006-01-09 23:26:14 +01001544 }
1545
1546 return 0;
1547
1548exit_device_put:
1549 platform_device_put(pdev);
1550exit:
1551 return err;
1552}
1553
Jean Delvare51c997d2006-12-12 18:18:29 +01001554static int __init f71805f_find(int sioaddr, unsigned short *address,
1555 struct f71805f_sio_data *sio_data)
Jean Delvaree53004e2006-01-09 23:26:14 +01001556{
Guenter Roeck73e6ff72019-04-04 10:52:43 -07001557 int err;
Jean Delvaree53004e2006-01-09 23:26:14 +01001558 u16 devid;
1559
Guenter Roeck2fff0842012-01-19 11:02:17 -08001560 static const char * const names[] = {
Jean Delvare51c997d2006-12-12 18:18:29 +01001561 "F71805F/FG",
Jean Delvare9cab0212007-07-15 10:36:06 +02001562 "F71872F/FG or F71806F/FG",
Jean Delvare51c997d2006-12-12 18:18:29 +01001563 };
1564
Guenter Roeck73e6ff72019-04-04 10:52:43 -07001565 err = superio_enter(sioaddr);
1566 if (err)
1567 return err;
Jean Delvaree53004e2006-01-09 23:26:14 +01001568
Guenter Roeck73e6ff72019-04-04 10:52:43 -07001569 err = -ENODEV;
Jean Delvaree53004e2006-01-09 23:26:14 +01001570 devid = superio_inw(sioaddr, SIO_REG_MANID);
1571 if (devid != SIO_FINTEK_ID)
1572 goto exit;
1573
Jean Delvare67b671b2007-12-06 23:13:42 +01001574 devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
Jean Delvare51c997d2006-12-12 18:18:29 +01001575 switch (devid) {
1576 case SIO_F71805F_ID:
1577 sio_data->kind = f71805f;
1578 break;
1579 case SIO_F71872F_ID:
1580 sio_data->kind = f71872f;
1581 sio_data->fnsel1 = superio_inb(sioaddr, SIO_REG_FNSEL1);
1582 break;
1583 default:
Joe Perchese54c5ad2010-10-20 06:51:33 +00001584 pr_info("Unsupported Fintek device, skipping\n");
Jean Delvaree53004e2006-01-09 23:26:14 +01001585 goto exit;
1586 }
1587
1588 superio_select(sioaddr, F71805F_LD_HWM);
1589 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
Joe Perchese54c5ad2010-10-20 06:51:33 +00001590 pr_warn("Device not activated, skipping\n");
Jean Delvaree53004e2006-01-09 23:26:14 +01001591 goto exit;
1592 }
1593
1594 *address = superio_inw(sioaddr, SIO_REG_ADDR);
1595 if (*address == 0) {
Joe Perchese54c5ad2010-10-20 06:51:33 +00001596 pr_warn("Base address not set, skipping\n");
Jean Delvaree53004e2006-01-09 23:26:14 +01001597 goto exit;
1598 }
Jean Delvare75c99022006-12-12 18:18:29 +01001599 *address &= ~(REGION_LENGTH - 1); /* Ignore 3 LSB */
Jean Delvaree53004e2006-01-09 23:26:14 +01001600
1601 err = 0;
Joe Perchese54c5ad2010-10-20 06:51:33 +00001602 pr_info("Found %s chip at %#x, revision %u\n",
1603 names[sio_data->kind], *address,
1604 superio_inb(sioaddr, SIO_REG_DEVREV));
Jean Delvaree53004e2006-01-09 23:26:14 +01001605
1606exit:
1607 superio_exit(sioaddr);
1608 return err;
1609}
1610
1611static int __init f71805f_init(void)
1612{
1613 int err;
1614 unsigned short address;
Jean Delvare51c997d2006-12-12 18:18:29 +01001615 struct f71805f_sio_data sio_data;
Jean Delvaree53004e2006-01-09 23:26:14 +01001616
Jean Delvare51c997d2006-12-12 18:18:29 +01001617 if (f71805f_find(0x2e, &address, &sio_data)
1618 && f71805f_find(0x4e, &address, &sio_data))
Jean Delvaree53004e2006-01-09 23:26:14 +01001619 return -ENODEV;
1620
1621 err = platform_driver_register(&f71805f_driver);
1622 if (err)
1623 goto exit;
1624
1625 /* Sets global pdev as a side effect */
Jean Delvare51c997d2006-12-12 18:18:29 +01001626 err = f71805f_device_add(address, &sio_data);
Jean Delvaree53004e2006-01-09 23:26:14 +01001627 if (err)
1628 goto exit_driver;
1629
1630 return 0;
1631
1632exit_driver:
1633 platform_driver_unregister(&f71805f_driver);
1634exit:
1635 return err;
1636}
1637
1638static void __exit f71805f_exit(void)
1639{
1640 platform_device_unregister(pdev);
1641 platform_driver_unregister(&f71805f_driver);
1642}
1643
Jean Delvare964f9452014-04-04 18:01:32 +02001644MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
Jean Delvaree53004e2006-01-09 23:26:14 +01001645MODULE_LICENSE("GPL");
Jean Delvare51c997d2006-12-12 18:18:29 +01001646MODULE_DESCRIPTION("F71805F/F71872F hardware monitoring driver");
Jean Delvaree53004e2006-01-09 23:26:14 +01001647
1648module_init(f71805f_init);
1649module_exit(f71805f_exit);