blob: d24df0c50bea43d0afc3926dde18fc5dd8091024 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeck85a0c0d2012-01-14 22:27:00 -08002 * smsc47m1.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 *
5 * Supports the SMSC LPC47B27x, LPC47M10x, LPC47M112, LPC47M13x,
6 * LPC47M14x, LPC47M15x, LPC47M192, LPC47M292 and LPC47M997
7 * Super-I/O chips.
8 *
9 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
Jean Delvare7c81c602014-01-29 20:40:08 +010010 * Copyright (C) 2004-2007 Jean Delvare <jdelvare@suse.de>
Guenter Roeck85a0c0d2012-01-14 22:27:00 -080011 * Ported to Linux 2.6 by Gabriele Gorla <gorlik@yahoo.com>
12 * and Jean Delvare
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Joe Perches512504e2010-10-20 06:51:49 +000029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/ioport.h>
34#include <linux/jiffies.h>
Jean Delvare51f2cca2007-05-08 17:22:00 +020035#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040036#include <linux/hwmon.h>
Jean Delvaree84cfbc2007-05-08 17:22:00 +020037#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040038#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010040#include <linux/mutex.h>
Jean Delvarece8c6ce12006-09-24 21:25:12 +020041#include <linux/sysfs.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010042#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020043#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Jean Delvare67b671b2007-12-06 23:13:42 +010045static unsigned short force_id;
46module_param(force_id, ushort, 0);
47MODULE_PARM_DESC(force_id, "Override the detected device ID");
48
Jean Delvare51f2cca2007-05-08 17:22:00 +020049static struct platform_device *pdev;
50
51#define DRVNAME "smsc47m1"
Jean Delvare8eccbb62007-05-08 17:21:59 +020052enum chips { smsc47m1, smsc47m2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/* Super-I/0 registers and commands */
55
Guenter Roeck85a0c0d2012-01-14 22:27:00 -080056#define REG 0x2e /* The register to read/write */
57#define VAL 0x2f /* The value to read/write */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59static inline void
60superio_outb(int reg, int val)
61{
62 outb(reg, REG);
63 outb(val, VAL);
64}
65
66static inline int
67superio_inb(int reg)
68{
69 outb(reg, REG);
70 return inb(VAL);
71}
72
73/* logical device for fans is 0x0A */
74#define superio_select() superio_outb(0x07, 0x0A)
75
Guenter Roeckac713a62019-04-04 11:28:37 -070076static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -070077superio_enter(void)
78{
Guenter Roeckac713a62019-04-04 11:28:37 -070079 if (!request_muxed_region(REG, 2, DRVNAME))
80 return -EBUSY;
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 outb(0x55, REG);
Guenter Roeckac713a62019-04-04 11:28:37 -070083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86static inline void
87superio_exit(void)
88{
89 outb(0xAA, REG);
Guenter Roeckac713a62019-04-04 11:28:37 -070090 release_region(REG, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93#define SUPERIO_REG_ACT 0x30
94#define SUPERIO_REG_BASE 0x60
95#define SUPERIO_REG_DEVID 0x20
Jean Delvare1b54ab42009-07-28 16:31:39 +020096#define SUPERIO_REG_DEVREV 0x21
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/* Logical device registers */
99
100#define SMSC_EXTENT 0x80
101
102/* nr is 0 or 1 in the macros below */
103#define SMSC47M1_REG_ALARM 0x04
104#define SMSC47M1_REG_TPIN(nr) (0x34 - (nr))
105#define SMSC47M1_REG_PPIN(nr) (0x36 - (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define SMSC47M1_REG_FANDIV 0x58
Jean Delvare8eccbb62007-05-08 17:21:59 +0200107
108static const u8 SMSC47M1_REG_FAN[3] = { 0x59, 0x5a, 0x6b };
109static const u8 SMSC47M1_REG_FAN_PRELOAD[3] = { 0x5b, 0x5c, 0x6c };
110static const u8 SMSC47M1_REG_PWM[3] = { 0x56, 0x57, 0x69 };
111
112#define SMSC47M2_REG_ALARM6 0x09
113#define SMSC47M2_REG_TPIN1 0x38
114#define SMSC47M2_REG_TPIN2 0x37
115#define SMSC47M2_REG_TPIN3 0x2d
116#define SMSC47M2_REG_PPIN3 0x2c
117#define SMSC47M2_REG_FANDIV3 0x6a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800119#define MIN_FROM_REG(reg, div) ((reg) >= 192 ? 0 : \
120 983040 / ((192 - (reg)) * (div)))
121#define FAN_FROM_REG(reg, div, preload) ((reg) <= (preload) || (reg) == 255 ? \
122 0 : \
123 983040 / (((reg) - (preload)) * (div)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define DIV_FROM_REG(reg) (1 << (reg))
125#define PWM_FROM_REG(reg) (((reg) & 0x7E) << 1)
126#define PWM_EN_FROM_REG(reg) ((~(reg)) & 0x01)
127#define PWM_TO_REG(reg) (((reg) >> 1) & 0x7E)
128
129struct smsc47m1_data {
Jean Delvare51f2cca2007-05-08 17:22:00 +0200130 unsigned short addr;
131 const char *name;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200132 enum chips type;
Tony Jones1beeffe2007-08-20 13:46:20 -0700133 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100135 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned long last_updated; /* In jiffies */
137
Jean Delvare8eccbb62007-05-08 17:21:59 +0200138 u8 fan[3]; /* Register value */
139 u8 fan_preload[3]; /* Register value */
140 u8 fan_div[3]; /* Register encoding, shifted right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 u8 alarms; /* Register encoding */
Jean Delvare8eccbb62007-05-08 17:21:59 +0200142 u8 pwm[3]; /* Register value (bit 0 is disable) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
Jean Delvare51f2cca2007-05-08 17:22:00 +0200145struct smsc47m1_sio_data {
146 enum chips type;
Jean Delvarefa0bff02009-12-16 21:38:27 +0100147 u8 activate; /* Remember initial device state */
Jean Delvare51f2cca2007-05-08 17:22:00 +0200148};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Jean Delvare51f2cca2007-05-08 17:22:00 +0200150static inline int smsc47m1_read_value(struct smsc47m1_data *data, u8 reg)
Jean Delvare94e183f2007-05-08 17:21:59 +0200151{
Jean Delvare51f2cca2007-05-08 17:22:00 +0200152 return inb_p(data->addr + reg);
Jean Delvare94e183f2007-05-08 17:21:59 +0200153}
154
Jean Delvare51f2cca2007-05-08 17:22:00 +0200155static inline void smsc47m1_write_value(struct smsc47m1_data *data, u8 reg,
Jean Delvare94e183f2007-05-08 17:21:59 +0200156 u8 value)
157{
Jean Delvare51f2cca2007-05-08 17:22:00 +0200158 outb_p(value, data->addr + reg);
Jean Delvare94e183f2007-05-08 17:21:59 +0200159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Axel Line70198a2014-07-17 19:15:36 +0800161static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
162 int init)
163{
164 struct smsc47m1_data *data = dev_get_drvdata(dev);
165
166 mutex_lock(&data->update_lock);
167
168 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) || init) {
169 int i, fan_nr;
170 fan_nr = data->type == smsc47m2 ? 3 : 2;
171
172 for (i = 0; i < fan_nr; i++) {
173 data->fan[i] = smsc47m1_read_value(data,
174 SMSC47M1_REG_FAN[i]);
175 data->fan_preload[i] = smsc47m1_read_value(data,
176 SMSC47M1_REG_FAN_PRELOAD[i]);
177 data->pwm[i] = smsc47m1_read_value(data,
178 SMSC47M1_REG_PWM[i]);
179 }
180
181 i = smsc47m1_read_value(data, SMSC47M1_REG_FANDIV);
182 data->fan_div[0] = (i >> 4) & 0x03;
183 data->fan_div[1] = i >> 6;
184
185 data->alarms = smsc47m1_read_value(data,
186 SMSC47M1_REG_ALARM) >> 6;
187 /* Clear alarms if needed */
188 if (data->alarms)
189 smsc47m1_write_value(data, SMSC47M1_REG_ALARM, 0xC0);
190
191 if (fan_nr >= 3) {
192 data->fan_div[2] = (smsc47m1_read_value(data,
193 SMSC47M2_REG_FANDIV3) >> 4) & 0x03;
194 data->alarms |= (smsc47m1_read_value(data,
195 SMSC47M2_REG_ALARM6) & 0x40) >> 4;
196 /* Clear alarm if needed */
197 if (data->alarms & 0x04)
198 smsc47m1_write_value(data,
199 SMSC47M2_REG_ALARM6,
200 0x40);
201 }
202
203 data->last_updated = jiffies;
204 }
205
206 mutex_unlock(&data->update_lock);
207 return data;
208}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200210static ssize_t get_fan(struct device *dev, struct device_attribute
211 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200213 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200215 int nr = attr->index;
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800216 /*
217 * This chip (stupidly) stops monitoring fan speed if PWM is
218 * enabled and duty cycle is 0%. This is fine if the monitoring
219 * and control concern the same fan, but troublesome if they are
220 * not (which could as well happen).
221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int rpm = (data->pwm[nr] & 0x7F) == 0x00 ? 0 :
223 FAN_FROM_REG(data->fan[nr],
224 DIV_FROM_REG(data->fan_div[nr]),
225 data->fan_preload[nr]);
226 return sprintf(buf, "%d\n", rpm);
227}
228
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200229static ssize_t get_fan_min(struct device *dev, struct device_attribute
230 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200232 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200234 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 int rpm = MIN_FROM_REG(data->fan_preload[nr],
236 DIV_FROM_REG(data->fan_div[nr]));
237 return sprintf(buf, "%d\n", rpm);
238}
239
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200240static ssize_t get_fan_div(struct device *dev, struct device_attribute
241 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200243 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200245 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Jean Delvare1f08af72008-01-06 15:36:13 +0100248static ssize_t get_fan_alarm(struct device *dev, struct device_attribute
249 *devattr, char *buf)
250{
251 int bitnr = to_sensor_dev_attr(devattr)->index;
252 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
253 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
254}
255
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200256static ssize_t get_pwm(struct device *dev, struct device_attribute
257 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200259 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200261 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200264static ssize_t get_pwm_en(struct device *dev, struct device_attribute
265 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200267 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200269 return sprintf(buf, "%d\n", PWM_EN_FROM_REG(data->pwm[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200272static ssize_t get_alarms(struct device *dev, struct device_attribute
273 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
276 return sprintf(buf, "%d\n", data->alarms);
277}
278
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200279static ssize_t set_fan_min(struct device *dev, struct device_attribute
280 *devattr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200282 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200283 struct smsc47m1_data *data = dev_get_drvdata(dev);
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200284 int nr = attr->index;
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800285 long rpmdiv;
286 long val;
287 int err;
288
289 err = kstrtol(buf, 10, &val);
290 if (err)
291 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100293 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]);
295
296 if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100297 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return -EINVAL;
299 }
300
301 data->fan_preload[nr] = 192 - ((983040 + rpmdiv / 2) / rpmdiv);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200302 smsc47m1_write_value(data, SMSC47M1_REG_FAN_PRELOAD[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 data->fan_preload[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100304 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 return count;
307}
308
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800309/*
310 * Note: we save and restore the fan minimum here, because its value is
311 * determined in part by the fan clock divider. This follows the principle
312 * of least surprise; the user doesn't expect the fan minimum to change just
313 * because the divider changed.
314 */
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200315static ssize_t set_fan_div(struct device *dev, struct device_attribute
316 *devattr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200318 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200319 struct smsc47m1_data *data = dev_get_drvdata(dev);
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200320 int nr = attr->index;
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800321 long new_div;
322 int err;
323 long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 u8 old_div = DIV_FROM_REG(data->fan_div[nr]);
325
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800326 err = kstrtol(buf, 10, &new_div);
327 if (err)
328 return err;
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (new_div == old_div) /* No change */
331 return count;
332
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100333 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 switch (new_div) {
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800335 case 1:
336 data->fan_div[nr] = 0;
337 break;
338 case 2:
339 data->fan_div[nr] = 1;
340 break;
341 case 4:
342 data->fan_div[nr] = 2;
343 break;
344 case 8:
345 data->fan_div[nr] = 3;
346 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 default:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100348 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return -EINVAL;
350 }
351
Jean Delvare8eccbb62007-05-08 17:21:59 +0200352 switch (nr) {
353 case 0:
354 case 1:
Jean Delvare51f2cca2007-05-08 17:22:00 +0200355 tmp = smsc47m1_read_value(data, SMSC47M1_REG_FANDIV)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200356 & ~(0x03 << (4 + 2 * nr));
357 tmp |= data->fan_div[nr] << (4 + 2 * nr);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200358 smsc47m1_write_value(data, SMSC47M1_REG_FANDIV, tmp);
Jean Delvare8eccbb62007-05-08 17:21:59 +0200359 break;
360 case 2:
Jean Delvare51f2cca2007-05-08 17:22:00 +0200361 tmp = smsc47m1_read_value(data, SMSC47M2_REG_FANDIV3) & 0xCF;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200362 tmp |= data->fan_div[2] << 4;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200363 smsc47m1_write_value(data, SMSC47M2_REG_FANDIV3, tmp);
Jean Delvare8eccbb62007-05-08 17:21:59 +0200364 break;
365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* Preserve fan min */
368 tmp = 192 - (old_div * (192 - data->fan_preload[nr])
369 + new_div / 2) / new_div;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800370 data->fan_preload[nr] = clamp_val(tmp, 0, 191);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200371 smsc47m1_write_value(data, SMSC47M1_REG_FAN_PRELOAD[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 data->fan_preload[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100373 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 return count;
376}
377
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200378static ssize_t set_pwm(struct device *dev, struct device_attribute
379 *devattr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200381 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200382 struct smsc47m1_data *data = dev_get_drvdata(dev);
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200383 int nr = attr->index;
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800384 long val;
385 int err;
386
387 err = kstrtol(buf, 10, &val);
388 if (err)
389 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (val < 0 || val > 255)
392 return -EINVAL;
393
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100394 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 data->pwm[nr] &= 0x81; /* Preserve additional bits */
396 data->pwm[nr] |= PWM_TO_REG(val);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200397 smsc47m1_write_value(data, SMSC47M1_REG_PWM[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100399 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 return count;
402}
403
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200404static ssize_t set_pwm_en(struct device *dev, struct device_attribute
405 *devattr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200407 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200408 struct smsc47m1_data *data = dev_get_drvdata(dev);
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200409 int nr = attr->index;
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800410 unsigned long val;
411 int err;
412
413 err = kstrtoul(buf, 10, &val);
414 if (err)
415 return err;
416
417 if (val > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return -EINVAL;
419
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100420 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 data->pwm[nr] &= 0xFE; /* preserve the other bits */
422 data->pwm[nr] |= !val;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200423 smsc47m1_write_value(data, SMSC47M1_REG_PWM[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100425 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 return count;
428}
429
430#define fan_present(offset) \
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200431static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, get_fan, \
432 NULL, offset - 1); \
433static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
434 get_fan_min, set_fan_min, offset - 1); \
435static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
436 get_fan_div, set_fan_div, offset - 1); \
Jean Delvare1f08af72008-01-06 15:36:13 +0100437static SENSOR_DEVICE_ATTR(fan##offset##_alarm, S_IRUGO, get_fan_alarm, \
438 NULL, offset - 1); \
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200439static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
440 get_pwm, set_pwm, offset - 1); \
441static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
442 get_pwm_en, set_pwm_en, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444fan_present(1);
445fan_present(2);
Jean Delvare8eccbb62007-05-08 17:21:59 +0200446fan_present(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL);
449
Jean Delvare51f2cca2007-05-08 17:22:00 +0200450static ssize_t show_name(struct device *dev, struct device_attribute
451 *devattr, char *buf)
452{
453 struct smsc47m1_data *data = dev_get_drvdata(dev);
454
455 return sprintf(buf, "%s\n", data->name);
456}
457static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
458
Guenter Roeck7e612682012-01-16 17:15:02 -0800459static struct attribute *smsc47m1_attributes_fan1[] = {
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200460 &sensor_dev_attr_fan1_input.dev_attr.attr,
461 &sensor_dev_attr_fan1_min.dev_attr.attr,
462 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvare1f08af72008-01-06 15:36:13 +0100463 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Guenter Roeck7e612682012-01-16 17:15:02 -0800464 NULL
465};
466
467static const struct attribute_group smsc47m1_group_fan1 = {
468 .attrs = smsc47m1_attributes_fan1,
469};
470
471static struct attribute *smsc47m1_attributes_fan2[] = {
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200472 &sensor_dev_attr_fan2_input.dev_attr.attr,
473 &sensor_dev_attr_fan2_min.dev_attr.attr,
474 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare1f08af72008-01-06 15:36:13 +0100475 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Guenter Roeck7e612682012-01-16 17:15:02 -0800476 NULL
477};
478
479static const struct attribute_group smsc47m1_group_fan2 = {
480 .attrs = smsc47m1_attributes_fan2,
481};
482
483static struct attribute *smsc47m1_attributes_fan3[] = {
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200484 &sensor_dev_attr_fan3_input.dev_attr.attr,
485 &sensor_dev_attr_fan3_min.dev_attr.attr,
486 &sensor_dev_attr_fan3_div.dev_attr.attr,
Jean Delvare1f08af72008-01-06 15:36:13 +0100487 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Guenter Roeck7e612682012-01-16 17:15:02 -0800488 NULL
489};
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200490
Guenter Roeck7e612682012-01-16 17:15:02 -0800491static const struct attribute_group smsc47m1_group_fan3 = {
492 .attrs = smsc47m1_attributes_fan3,
493};
494
495static struct attribute *smsc47m1_attributes_pwm1[] = {
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200496 &sensor_dev_attr_pwm1.dev_attr.attr,
497 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Guenter Roeck7e612682012-01-16 17:15:02 -0800498 NULL
499};
500
501static const struct attribute_group smsc47m1_group_pwm1 = {
502 .attrs = smsc47m1_attributes_pwm1,
503};
504
505static struct attribute *smsc47m1_attributes_pwm2[] = {
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200506 &sensor_dev_attr_pwm2.dev_attr.attr,
507 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
Guenter Roeck7e612682012-01-16 17:15:02 -0800508 NULL
509};
510
511static const struct attribute_group smsc47m1_group_pwm2 = {
512 .attrs = smsc47m1_attributes_pwm2,
513};
514
515static struct attribute *smsc47m1_attributes_pwm3[] = {
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200516 &sensor_dev_attr_pwm3.dev_attr.attr,
517 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Guenter Roeck7e612682012-01-16 17:15:02 -0800518 NULL
519};
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200520
Guenter Roeck7e612682012-01-16 17:15:02 -0800521static const struct attribute_group smsc47m1_group_pwm3 = {
522 .attrs = smsc47m1_attributes_pwm3,
523};
524
525static struct attribute *smsc47m1_attributes[] = {
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200526 &dev_attr_alarms.attr,
Jean Delvare51f2cca2007-05-08 17:22:00 +0200527 &dev_attr_name.attr,
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200528 NULL
529};
530
531static const struct attribute_group smsc47m1_group = {
532 .attrs = smsc47m1_attributes,
533};
534
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700535static int __init smsc47m1_find(struct smsc47m1_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 u8 val;
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700538 unsigned short addr;
Guenter Roeckac713a62019-04-04 11:28:37 -0700539 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Guenter Roeckac713a62019-04-04 11:28:37 -0700541 err = superio_enter();
542 if (err)
543 return err;
544
Jean Delvare67b671b2007-12-06 23:13:42 +0100545 val = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /*
Jean Delvare60917802006-10-08 22:00:44 +0200548 * SMSC LPC47M10x/LPC47M112/LPC47M13x (device id 0x59), LPC47M14x
549 * (device id 0x5F) and LPC47B27x (device id 0x51) have fan control.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * The LPC47M15x and LPC47M192 chips "with hardware monitoring block"
Jean Delvareec5ce552005-04-26 22:09:43 +0200551 * can do much more besides (device id 0x60).
Jean Delvareb890a072005-10-26 22:21:24 +0200552 * The LPC47M997 is undocumented, but seems to be compatible with
553 * the LPC47M192, and has the same device id.
Jean Delvare8eccbb62007-05-08 17:21:59 +0200554 * The LPC47M292 (device id 0x6B) is somewhat compatible, but it
555 * supports a 3rd fan, and the pin configuration registers are
556 * unfortunately different.
Jean Delvare1b54ab42009-07-28 16:31:39 +0200557 * The LPC47M233 has the same device id (0x6B) but is not compatible.
558 * We check the high bit of the device revision register to
559 * differentiate them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
Jean Delvare51f2cca2007-05-08 17:22:00 +0200561 switch (val) {
Jean Delvare8eccbb62007-05-08 17:21:59 +0200562 case 0x51:
Joe Perches512504e2010-10-20 06:51:49 +0000563 pr_info("Found SMSC LPC47B27x\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200564 sio_data->type = smsc47m1;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200565 break;
566 case 0x59:
Joe Perches512504e2010-10-20 06:51:49 +0000567 pr_info("Found SMSC LPC47M10x/LPC47M112/LPC47M13x\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200568 sio_data->type = smsc47m1;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200569 break;
570 case 0x5F:
Joe Perches512504e2010-10-20 06:51:49 +0000571 pr_info("Found SMSC LPC47M14x\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200572 sio_data->type = smsc47m1;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200573 break;
574 case 0x60:
Joe Perches512504e2010-10-20 06:51:49 +0000575 pr_info("Found SMSC LPC47M15x/LPC47M192/LPC47M997\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200576 sio_data->type = smsc47m1;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200577 break;
578 case 0x6B:
Jean Delvare1b54ab42009-07-28 16:31:39 +0200579 if (superio_inb(SUPERIO_REG_DEVREV) & 0x80) {
Joe Perches512504e2010-10-20 06:51:49 +0000580 pr_debug("Found SMSC LPC47M233, unsupported\n");
Jean Delvare1b54ab42009-07-28 16:31:39 +0200581 superio_exit();
582 return -ENODEV;
583 }
584
Joe Perches512504e2010-10-20 06:51:49 +0000585 pr_info("Found SMSC LPC47M292\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200586 sio_data->type = smsc47m2;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200587 break;
588 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 superio_exit();
590 return -ENODEV;
591 }
592
593 superio_select();
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700594 addr = (superio_inb(SUPERIO_REG_BASE) << 8)
Jean Delvare2d8672c2005-07-19 23:56:35 +0200595 | superio_inb(SUPERIO_REG_BASE + 1);
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700596 if (addr == 0) {
Joe Perches512504e2010-10-20 06:51:49 +0000597 pr_info("Device address not set, will not use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 superio_exit();
599 return -ENODEV;
600 }
601
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800602 /*
603 * Enable only if address is set (needed at least on the
604 * Compaq Presario S4000NX)
605 */
Jean Delvarefa0bff02009-12-16 21:38:27 +0100606 sio_data->activate = superio_inb(SUPERIO_REG_ACT);
607 if ((sio_data->activate & 0x01) == 0) {
Joe Perches512504e2010-10-20 06:51:49 +0000608 pr_info("Enabling device\n");
Jean Delvarefa0bff02009-12-16 21:38:27 +0100609 superio_outb(SUPERIO_REG_ACT, sio_data->activate | 0x01);
610 }
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 superio_exit();
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700613 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Jean Delvarefa0bff02009-12-16 21:38:27 +0100616/* Restore device to its initial state */
Jeff Mahoneya00d6432010-01-25 15:00:48 +0100617static void smsc47m1_restore(const struct smsc47m1_sio_data *sio_data)
Jean Delvarefa0bff02009-12-16 21:38:27 +0100618{
619 if ((sio_data->activate & 0x01) == 0) {
Guenter Roeckac713a62019-04-04 11:28:37 -0700620 if (!superio_enter()) {
621 superio_select();
622 pr_info("Disabling device\n");
623 superio_outb(SUPERIO_REG_ACT, sio_data->activate);
624 superio_exit();
625 } else {
626 pr_warn("Failed to disable device\n");
627 }
Jean Delvarefa0bff02009-12-16 21:38:27 +0100628 }
629}
630
Jean Delvarea0e92d72009-12-16 21:38:26 +0100631#define CHECK 1
632#define REQUEST 2
Jean Delvarea0e92d72009-12-16 21:38:26 +0100633
634/*
635 * This function can be used to:
636 * - test for resource conflicts with ACPI
637 * - request the resources
Jean Delvarea0e92d72009-12-16 21:38:26 +0100638 * We only allocate the I/O ports we really need, to minimize the risk of
639 * conflicts with ACPI or with other drivers.
640 */
Guenter Roeck08ad7c92012-06-02 12:04:07 -0700641static int __init smsc47m1_handle_resources(unsigned short address,
642 enum chips type, int action,
643 struct device *dev)
Jean Delvarea0e92d72009-12-16 21:38:26 +0100644{
645 static const u8 ports_m1[] = {
646 /* register, region length */
647 0x04, 1,
648 0x33, 4,
649 0x56, 7,
650 };
651
652 static const u8 ports_m2[] = {
653 /* register, region length */
654 0x04, 1,
655 0x09, 1,
656 0x2c, 2,
657 0x35, 4,
658 0x56, 7,
659 0x69, 4,
660 };
661
662 int i, ports_size, err;
663 const u8 *ports;
664
665 switch (type) {
666 case smsc47m1:
667 default:
668 ports = ports_m1;
669 ports_size = ARRAY_SIZE(ports_m1);
670 break;
671 case smsc47m2:
672 ports = ports_m2;
673 ports_size = ARRAY_SIZE(ports_m2);
674 break;
675 }
676
677 for (i = 0; i + 1 < ports_size; i += 2) {
678 unsigned short start = address + ports[i];
679 unsigned short len = ports[i + 1];
680
681 switch (action) {
682 case CHECK:
683 /* Only check for conflicts */
684 err = acpi_check_region(start, len, DRVNAME);
685 if (err)
686 return err;
687 break;
688 case REQUEST:
689 /* Request the resources */
Guenter Roeck08ad7c92012-06-02 12:04:07 -0700690 if (!devm_request_region(dev, start, len, DRVNAME)) {
691 dev_err(dev,
692 "Region 0x%hx-0x%hx already in use!\n",
693 start, start + len);
Jean Delvarea0e92d72009-12-16 21:38:26 +0100694 return -EBUSY;
695 }
696 break;
Jean Delvarea0e92d72009-12-16 21:38:26 +0100697 }
698 }
699
700 return 0;
701}
702
Guenter Roeck7e612682012-01-16 17:15:02 -0800703static void smsc47m1_remove_files(struct device *dev)
704{
705 sysfs_remove_group(&dev->kobj, &smsc47m1_group);
706 sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan1);
707 sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan2);
708 sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan3);
709 sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm1);
710 sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm2);
711 sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm3);
712}
713
Jean Delvare3ecf44b2009-12-16 21:38:26 +0100714static int __init smsc47m1_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Jean Delvare51f2cca2007-05-08 17:22:00 +0200716 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +0900717 struct smsc47m1_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct smsc47m1_data *data;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200719 struct resource *res;
Jean Delvarea0e92d72009-12-16 21:38:26 +0100720 int err;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200721 int fan1, fan2, fan3, pwm1, pwm2, pwm3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800723 static const char * const names[] = {
Jean Delvare51f2cca2007-05-08 17:22:00 +0200724 "smsc47m1",
725 "smsc47m2",
726 };
727
728 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Jean Delvarea0e92d72009-12-16 21:38:26 +0100729 err = smsc47m1_handle_resources(res->start, sio_data->type,
730 REQUEST, dev);
731 if (err < 0)
732 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Guenter Roeck08ad7c92012-06-02 12:04:07 -0700734 data = devm_kzalloc(dev, sizeof(struct smsc47m1_data), GFP_KERNEL);
735 if (!data)
736 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Jean Delvare51f2cca2007-05-08 17:22:00 +0200738 data->addr = res->start;
739 data->type = sio_data->type;
740 data->name = names[sio_data->type];
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100741 mutex_init(&data->update_lock);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200742 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800744 /*
745 * If no function is properly configured, there's no point in
746 * actually registering the chip.
747 */
Jean Delvare51f2cca2007-05-08 17:22:00 +0200748 pwm1 = (smsc47m1_read_value(data, SMSC47M1_REG_PPIN(0)) & 0x05)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 == 0x04;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200750 pwm2 = (smsc47m1_read_value(data, SMSC47M1_REG_PPIN(1)) & 0x05)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 == 0x04;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200752 if (data->type == smsc47m2) {
Jean Delvare51f2cca2007-05-08 17:22:00 +0200753 fan1 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN1)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200754 & 0x0d) == 0x09;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200755 fan2 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN2)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200756 & 0x0d) == 0x09;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200757 fan3 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN3)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200758 & 0x0d) == 0x0d;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200759 pwm3 = (smsc47m1_read_value(data, SMSC47M2_REG_PPIN3)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200760 & 0x0d) == 0x08;
761 } else {
Jean Delvare51f2cca2007-05-08 17:22:00 +0200762 fan1 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(0))
Jean Delvare8eccbb62007-05-08 17:21:59 +0200763 & 0x05) == 0x05;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200764 fan2 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(1))
Jean Delvare8eccbb62007-05-08 17:21:59 +0200765 & 0x05) == 0x05;
766 fan3 = 0;
767 pwm3 = 0;
768 }
769 if (!(fan1 || fan2 || fan3 || pwm1 || pwm2 || pwm3)) {
Jean Delvare51f2cca2007-05-08 17:22:00 +0200770 dev_warn(dev, "Device not configured, will not use\n");
Guenter Roeck08ad7c92012-06-02 12:04:07 -0700771 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800774 /*
775 * Some values (fan min, clock dividers, pwm registers) may be
776 * needed before any update is triggered, so we better read them
777 * at least once here. We don't usually do it that way, but in
778 * this particular case, manually reading 5 registers out of 8
779 * doesn't make much sense and we're better using the existing
780 * function.
781 */
Jean Delvare51f2cca2007-05-08 17:22:00 +0200782 smsc47m1_update_device(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400784 /* Register sysfs hooks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (fan1) {
Guenter Roeck7e612682012-01-16 17:15:02 -0800786 err = sysfs_create_group(&dev->kobj,
787 &smsc47m1_group_fan1);
788 if (err)
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200789 goto error_remove_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 } else
Jean Delvare51f2cca2007-05-08 17:22:00 +0200791 dev_dbg(dev, "Fan 1 not enabled by hardware, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (fan2) {
Guenter Roeck7e612682012-01-16 17:15:02 -0800794 err = sysfs_create_group(&dev->kobj,
795 &smsc47m1_group_fan2);
796 if (err)
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200797 goto error_remove_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 } else
Jean Delvare51f2cca2007-05-08 17:22:00 +0200799 dev_dbg(dev, "Fan 2 not enabled by hardware, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Jean Delvare8eccbb62007-05-08 17:21:59 +0200801 if (fan3) {
Guenter Roeck7e612682012-01-16 17:15:02 -0800802 err = sysfs_create_group(&dev->kobj,
803 &smsc47m1_group_fan3);
804 if (err)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200805 goto error_remove_files;
Jean Delvare8477d022007-08-16 14:33:37 +0200806 } else if (data->type == smsc47m2)
Jean Delvare51f2cca2007-05-08 17:22:00 +0200807 dev_dbg(dev, "Fan 3 not enabled by hardware, skipping\n");
Jean Delvare8eccbb62007-05-08 17:21:59 +0200808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (pwm1) {
Guenter Roeck7e612682012-01-16 17:15:02 -0800810 err = sysfs_create_group(&dev->kobj,
811 &smsc47m1_group_pwm1);
812 if (err)
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200813 goto error_remove_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 } else
Jean Delvare51f2cca2007-05-08 17:22:00 +0200815 dev_dbg(dev, "PWM 1 not enabled by hardware, skipping\n");
Jean Delvare8eccbb62007-05-08 17:21:59 +0200816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (pwm2) {
Guenter Roeck7e612682012-01-16 17:15:02 -0800818 err = sysfs_create_group(&dev->kobj,
819 &smsc47m1_group_pwm2);
820 if (err)
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200821 goto error_remove_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 } else
Jean Delvare51f2cca2007-05-08 17:22:00 +0200823 dev_dbg(dev, "PWM 2 not enabled by hardware, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Jean Delvare8eccbb62007-05-08 17:21:59 +0200825 if (pwm3) {
Guenter Roeck7e612682012-01-16 17:15:02 -0800826 err = sysfs_create_group(&dev->kobj,
827 &smsc47m1_group_pwm3);
828 if (err)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200829 goto error_remove_files;
Jean Delvare8477d022007-08-16 14:33:37 +0200830 } else if (data->type == smsc47m2)
Jean Delvare51f2cca2007-05-08 17:22:00 +0200831 dev_dbg(dev, "PWM 3 not enabled by hardware, skipping\n");
Jean Delvare8eccbb62007-05-08 17:21:59 +0200832
Guenter Roeck7e612682012-01-16 17:15:02 -0800833 err = sysfs_create_group(&dev->kobj, &smsc47m1_group);
834 if (err)
Jean Delvare68a50b52007-08-12 13:58:50 +0200835 goto error_remove_files;
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200836
Tony Jones1beeffe2007-08-20 13:46:20 -0700837 data->hwmon_dev = hwmon_device_register(dev);
838 if (IS_ERR(data->hwmon_dev)) {
839 err = PTR_ERR(data->hwmon_dev);
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200840 goto error_remove_files;
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 return 0;
844
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200845error_remove_files:
Guenter Roeck7e612682012-01-16 17:15:02 -0800846 smsc47m1_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return err;
848}
849
Jean Delvare3ecf44b2009-12-16 21:38:26 +0100850static int __exit smsc47m1_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Jean Delvare51f2cca2007-05-08 17:22:00 +0200852 struct smsc47m1_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Tony Jones1beeffe2007-08-20 13:46:20 -0700854 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeck7e612682012-01-16 17:15:02 -0800855 smsc47m1_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return 0;
858}
859
Axel Line70198a2014-07-17 19:15:36 +0800860static struct platform_driver smsc47m1_driver = {
861 .driver = {
Axel Line70198a2014-07-17 19:15:36 +0800862 .name = DRVNAME,
863 },
864 .remove = __exit_p(smsc47m1_remove),
865};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Jean Delvare51f2cca2007-05-08 17:22:00 +0200867static int __init smsc47m1_device_add(unsigned short address,
868 const struct smsc47m1_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Jean Delvare51f2cca2007-05-08 17:22:00 +0200870 struct resource res = {
871 .start = address,
872 .end = address + SMSC_EXTENT - 1,
873 .name = DRVNAME,
874 .flags = IORESOURCE_IO,
875 };
876 int err;
877
Jean Delvarea0e92d72009-12-16 21:38:26 +0100878 err = smsc47m1_handle_resources(address, sio_data->type, CHECK, NULL);
Jean Delvareb9acb642009-01-07 16:37:35 +0100879 if (err)
880 goto exit;
881
Jean Delvare51f2cca2007-05-08 17:22:00 +0200882 pdev = platform_device_alloc(DRVNAME, address);
883 if (!pdev) {
884 err = -ENOMEM;
Joe Perches512504e2010-10-20 06:51:49 +0000885 pr_err("Device allocation failed\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200886 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
Jean Delvare51f2cca2007-05-08 17:22:00 +0200889 err = platform_device_add_resources(pdev, &res, 1);
890 if (err) {
Joe Perches512504e2010-10-20 06:51:49 +0000891 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200892 goto exit_device_put;
893 }
894
Jean Delvare2df6d812007-06-09 10:11:16 -0400895 err = platform_device_add_data(pdev, sio_data,
896 sizeof(struct smsc47m1_sio_data));
897 if (err) {
Joe Perches512504e2010-10-20 06:51:49 +0000898 pr_err("Platform data allocation failed\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200899 goto exit_device_put;
900 }
Jean Delvare51f2cca2007-05-08 17:22:00 +0200901
902 err = platform_device_add(pdev);
903 if (err) {
Joe Perches512504e2010-10-20 06:51:49 +0000904 pr_err("Device addition failed (%d)\n", err);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200905 goto exit_device_put;
906 }
907
908 return 0;
909
910exit_device_put:
911 platform_device_put(pdev);
912exit:
913 return err;
914}
915
916static int __init sm_smsc47m1_init(void)
917{
918 int err;
919 unsigned short address;
920 struct smsc47m1_sio_data sio_data;
921
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700922 err = smsc47m1_find(&sio_data);
923 if (err < 0)
924 return err;
925 address = err;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200926
Jean Delvare51f2cca2007-05-08 17:22:00 +0200927 /* Sets global pdev as a side effect */
928 err = smsc47m1_device_add(address, &sio_data);
929 if (err)
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700930 return err;
Jean Delvare3ecf44b2009-12-16 21:38:26 +0100931
932 err = platform_driver_probe(&smsc47m1_driver, smsc47m1_probe);
933 if (err)
934 goto exit_device;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200935
936 return 0;
937
Jean Delvare3ecf44b2009-12-16 21:38:26 +0100938exit_device:
939 platform_device_unregister(pdev);
Jean Delvarefa0bff02009-12-16 21:38:27 +0100940 smsc47m1_restore(&sio_data);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200941 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944static void __exit sm_smsc47m1_exit(void)
945{
Jean Delvare51f2cca2007-05-08 17:22:00 +0200946 platform_driver_unregister(&smsc47m1_driver);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +0900947 smsc47m1_restore(dev_get_platdata(&pdev->dev));
Jean Delvare3ecf44b2009-12-16 21:38:26 +0100948 platform_device_unregister(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
952MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver");
953MODULE_LICENSE("GPL");
954
955module_init(sm_smsc47m1_init);
956module_exit(sm_smsc47m1_exit);