blob: 5f92eab24c628582e7bf7622d92e24ddf05e8886 [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 Delvare7c81c60f2014-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 Roeckd6410402019-04-04 11:28:37 -070076static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -070077superio_enter(void)
78{
Guenter Roeckd6410402019-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 Roeckd6410402019-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 Roeckd6410402019-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
Guenter Roeck96c6f812019-01-22 15:13:02 -0800210static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
211 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
Guenter Roeck96c6f812019-01-22 15:13:02 -0800229static ssize_t fan_min_show(struct device *dev,
230 struct device_attribute *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
Guenter Roeck96c6f812019-01-22 15:13:02 -0800240static ssize_t fan_div_show(struct device *dev,
241 struct device_attribute *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
Guenter Roeck96c6f812019-01-22 15:13:02 -0800248static ssize_t fan_alarm_show(struct device *dev,
249 struct device_attribute *devattr, char *buf)
Jean Delvare1f08af72008-01-06 15:36:13 +0100250{
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
Guenter Roeck96c6f812019-01-22 15:13:02 -0800256static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
257 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
Guenter Roeck96c6f812019-01-22 15:13:02 -0800264static ssize_t pwm_en_show(struct device *dev,
265 struct device_attribute *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
Julia Lawall83aa233e5dc2016-12-22 13:05:06 +0100272static ssize_t alarms_show(struct device *dev,
273 struct device_attribute *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
Guenter Roeck96c6f812019-01-22 15:13:02 -0800279static ssize_t fan_min_store(struct device *dev,
280 struct device_attribute *devattr,
281 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200283 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200284 struct smsc47m1_data *data = dev_get_drvdata(dev);
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200285 int nr = attr->index;
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800286 long rpmdiv;
287 long val;
288 int err;
289
290 err = kstrtol(buf, 10, &val);
291 if (err)
292 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100294 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]);
296
297 if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100298 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return -EINVAL;
300 }
301
302 data->fan_preload[nr] = 192 - ((983040 + rpmdiv / 2) / rpmdiv);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200303 smsc47m1_write_value(data, SMSC47M1_REG_FAN_PRELOAD[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 data->fan_preload[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100305 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 return count;
308}
309
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800310/*
311 * Note: we save and restore the fan minimum here, because its value is
312 * determined in part by the fan clock divider. This follows the principle
313 * of least surprise; the user doesn't expect the fan minimum to change just
314 * because the divider changed.
315 */
Guenter Roeck96c6f812019-01-22 15:13:02 -0800316static ssize_t fan_div_store(struct device *dev,
317 struct device_attribute *devattr,
318 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200320 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200321 struct smsc47m1_data *data = dev_get_drvdata(dev);
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200322 int nr = attr->index;
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800323 long new_div;
324 int err;
325 long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u8 old_div = DIV_FROM_REG(data->fan_div[nr]);
327
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800328 err = kstrtol(buf, 10, &new_div);
329 if (err)
330 return err;
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (new_div == old_div) /* No change */
333 return count;
334
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100335 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 switch (new_div) {
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800337 case 1:
338 data->fan_div[nr] = 0;
339 break;
340 case 2:
341 data->fan_div[nr] = 1;
342 break;
343 case 4:
344 data->fan_div[nr] = 2;
345 break;
346 case 8:
347 data->fan_div[nr] = 3;
348 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 default:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100350 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -EINVAL;
352 }
353
Jean Delvare8eccbb62007-05-08 17:21:59 +0200354 switch (nr) {
355 case 0:
356 case 1:
Jean Delvare51f2cca2007-05-08 17:22:00 +0200357 tmp = smsc47m1_read_value(data, SMSC47M1_REG_FANDIV)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200358 & ~(0x03 << (4 + 2 * nr));
359 tmp |= data->fan_div[nr] << (4 + 2 * nr);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200360 smsc47m1_write_value(data, SMSC47M1_REG_FANDIV, tmp);
Jean Delvare8eccbb62007-05-08 17:21:59 +0200361 break;
362 case 2:
Jean Delvare51f2cca2007-05-08 17:22:00 +0200363 tmp = smsc47m1_read_value(data, SMSC47M2_REG_FANDIV3) & 0xCF;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200364 tmp |= data->fan_div[2] << 4;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200365 smsc47m1_write_value(data, SMSC47M2_REG_FANDIV3, tmp);
Jean Delvare8eccbb62007-05-08 17:21:59 +0200366 break;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /* Preserve fan min */
370 tmp = 192 - (old_div * (192 - data->fan_preload[nr])
371 + new_div / 2) / new_div;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800372 data->fan_preload[nr] = clamp_val(tmp, 0, 191);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200373 smsc47m1_write_value(data, SMSC47M1_REG_FAN_PRELOAD[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 data->fan_preload[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100375 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 return count;
378}
379
Guenter Roeck96c6f812019-01-22 15:13:02 -0800380static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
381 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200383 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200384 struct smsc47m1_data *data = dev_get_drvdata(dev);
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200385 int nr = attr->index;
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800386 long val;
387 int err;
388
389 err = kstrtol(buf, 10, &val);
390 if (err)
391 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (val < 0 || val > 255)
394 return -EINVAL;
395
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100396 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 data->pwm[nr] &= 0x81; /* Preserve additional bits */
398 data->pwm[nr] |= PWM_TO_REG(val);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200399 smsc47m1_write_value(data, SMSC47M1_REG_PWM[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100401 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 return count;
404}
405
Guenter Roeck96c6f812019-01-22 15:13:02 -0800406static ssize_t pwm_en_store(struct device *dev,
407 struct device_attribute *devattr, const char *buf,
408 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200410 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200411 struct smsc47m1_data *data = dev_get_drvdata(dev);
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200412 int nr = attr->index;
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800413 unsigned long val;
414 int err;
415
416 err = kstrtoul(buf, 10, &val);
417 if (err)
418 return err;
419
420 if (val > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return -EINVAL;
422
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100423 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 data->pwm[nr] &= 0xFE; /* preserve the other bits */
425 data->pwm[nr] |= !val;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200426 smsc47m1_write_value(data, SMSC47M1_REG_PWM[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100428 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 return count;
431}
432
Guenter Roeck96c6f812019-01-22 15:13:02 -0800433static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
434static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
435static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
436static SENSOR_DEVICE_ATTR_RO(fan1_alarm, fan_alarm, 0);
437static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
438static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_en, 0);
439static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
440static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
441static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
442static SENSOR_DEVICE_ATTR_RO(fan2_alarm, fan_alarm, 1);
443static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
444static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_en, 1);
445static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
446static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
447static SENSOR_DEVICE_ATTR_RW(fan3_div, fan_div, 2);
448static SENSOR_DEVICE_ATTR_RO(fan3_alarm, fan_alarm, 2);
449static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
450static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_en, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Julia Lawall83aa233e5dc2016-12-22 13:05:06 +0100452static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Julia Lawall83aa233e5dc2016-12-22 13:05:06 +0100454static ssize_t name_show(struct device *dev, struct device_attribute
Jean Delvare51f2cca2007-05-08 17:22:00 +0200455 *devattr, char *buf)
456{
457 struct smsc47m1_data *data = dev_get_drvdata(dev);
458
459 return sprintf(buf, "%s\n", data->name);
460}
Julia Lawall83aa233e5dc2016-12-22 13:05:06 +0100461static DEVICE_ATTR_RO(name);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200462
Guenter Roeck7e6126852012-01-16 17:15:02 -0800463static struct attribute *smsc47m1_attributes_fan1[] = {
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200464 &sensor_dev_attr_fan1_input.dev_attr.attr,
465 &sensor_dev_attr_fan1_min.dev_attr.attr,
466 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvare1f08af72008-01-06 15:36:13 +0100467 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Guenter Roeck7e6126852012-01-16 17:15:02 -0800468 NULL
469};
470
471static const struct attribute_group smsc47m1_group_fan1 = {
472 .attrs = smsc47m1_attributes_fan1,
473};
474
475static struct attribute *smsc47m1_attributes_fan2[] = {
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200476 &sensor_dev_attr_fan2_input.dev_attr.attr,
477 &sensor_dev_attr_fan2_min.dev_attr.attr,
478 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare1f08af72008-01-06 15:36:13 +0100479 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Guenter Roeck7e6126852012-01-16 17:15:02 -0800480 NULL
481};
482
483static const struct attribute_group smsc47m1_group_fan2 = {
484 .attrs = smsc47m1_attributes_fan2,
485};
486
487static struct attribute *smsc47m1_attributes_fan3[] = {
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200488 &sensor_dev_attr_fan3_input.dev_attr.attr,
489 &sensor_dev_attr_fan3_min.dev_attr.attr,
490 &sensor_dev_attr_fan3_div.dev_attr.attr,
Jean Delvare1f08af72008-01-06 15:36:13 +0100491 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Guenter Roeck7e6126852012-01-16 17:15:02 -0800492 NULL
493};
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200494
Guenter Roeck7e6126852012-01-16 17:15:02 -0800495static const struct attribute_group smsc47m1_group_fan3 = {
496 .attrs = smsc47m1_attributes_fan3,
497};
498
499static struct attribute *smsc47m1_attributes_pwm1[] = {
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200500 &sensor_dev_attr_pwm1.dev_attr.attr,
501 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Guenter Roeck7e6126852012-01-16 17:15:02 -0800502 NULL
503};
504
505static const struct attribute_group smsc47m1_group_pwm1 = {
506 .attrs = smsc47m1_attributes_pwm1,
507};
508
509static struct attribute *smsc47m1_attributes_pwm2[] = {
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200510 &sensor_dev_attr_pwm2.dev_attr.attr,
511 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
Guenter Roeck7e6126852012-01-16 17:15:02 -0800512 NULL
513};
514
515static const struct attribute_group smsc47m1_group_pwm2 = {
516 .attrs = smsc47m1_attributes_pwm2,
517};
518
519static struct attribute *smsc47m1_attributes_pwm3[] = {
Jean Delvaree84cfbc2007-05-08 17:22:00 +0200520 &sensor_dev_attr_pwm3.dev_attr.attr,
521 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Guenter Roeck7e6126852012-01-16 17:15:02 -0800522 NULL
523};
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200524
Guenter Roeck7e6126852012-01-16 17:15:02 -0800525static const struct attribute_group smsc47m1_group_pwm3 = {
526 .attrs = smsc47m1_attributes_pwm3,
527};
528
529static struct attribute *smsc47m1_attributes[] = {
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200530 &dev_attr_alarms.attr,
Jean Delvare51f2cca2007-05-08 17:22:00 +0200531 &dev_attr_name.attr,
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200532 NULL
533};
534
535static const struct attribute_group smsc47m1_group = {
536 .attrs = smsc47m1_attributes,
537};
538
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700539static int __init smsc47m1_find(struct smsc47m1_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 u8 val;
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700542 unsigned short addr;
Guenter Roeckd6410402019-04-04 11:28:37 -0700543 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Guenter Roeckd6410402019-04-04 11:28:37 -0700545 err = superio_enter();
546 if (err)
547 return err;
548
Jean Delvare67b671b2007-12-06 23:13:42 +0100549 val = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
Jean Delvare60917802006-10-08 22:00:44 +0200552 * SMSC LPC47M10x/LPC47M112/LPC47M13x (device id 0x59), LPC47M14x
553 * (device id 0x5F) and LPC47B27x (device id 0x51) have fan control.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * The LPC47M15x and LPC47M192 chips "with hardware monitoring block"
Jean Delvareec5ce552005-04-26 22:09:43 +0200555 * can do much more besides (device id 0x60).
Jean Delvareb890a072005-10-26 22:21:24 +0200556 * The LPC47M997 is undocumented, but seems to be compatible with
557 * the LPC47M192, and has the same device id.
Jean Delvare8eccbb62007-05-08 17:21:59 +0200558 * The LPC47M292 (device id 0x6B) is somewhat compatible, but it
559 * supports a 3rd fan, and the pin configuration registers are
560 * unfortunately different.
Jean Delvare1b54ab42009-07-28 16:31:39 +0200561 * The LPC47M233 has the same device id (0x6B) but is not compatible.
562 * We check the high bit of the device revision register to
563 * differentiate them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Jean Delvare51f2cca2007-05-08 17:22:00 +0200565 switch (val) {
Jean Delvare8eccbb62007-05-08 17:21:59 +0200566 case 0x51:
Joe Perches512504e2010-10-20 06:51:49 +0000567 pr_info("Found SMSC LPC47B27x\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200568 sio_data->type = smsc47m1;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200569 break;
570 case 0x59:
Joe Perches512504e2010-10-20 06:51:49 +0000571 pr_info("Found SMSC LPC47M10x/LPC47M112/LPC47M13x\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200572 sio_data->type = smsc47m1;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200573 break;
574 case 0x5F:
Joe Perches512504e2010-10-20 06:51:49 +0000575 pr_info("Found SMSC LPC47M14x\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200576 sio_data->type = smsc47m1;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200577 break;
578 case 0x60:
Joe Perches512504e2010-10-20 06:51:49 +0000579 pr_info("Found SMSC LPC47M15x/LPC47M192/LPC47M997\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200580 sio_data->type = smsc47m1;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200581 break;
582 case 0x6B:
Jean Delvare1b54ab42009-07-28 16:31:39 +0200583 if (superio_inb(SUPERIO_REG_DEVREV) & 0x80) {
Joe Perches512504e2010-10-20 06:51:49 +0000584 pr_debug("Found SMSC LPC47M233, unsupported\n");
Jean Delvare1b54ab42009-07-28 16:31:39 +0200585 superio_exit();
586 return -ENODEV;
587 }
588
Joe Perches512504e2010-10-20 06:51:49 +0000589 pr_info("Found SMSC LPC47M292\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200590 sio_data->type = smsc47m2;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200591 break;
592 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 superio_exit();
594 return -ENODEV;
595 }
596
597 superio_select();
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700598 addr = (superio_inb(SUPERIO_REG_BASE) << 8)
Jean Delvare2d8672c2005-07-19 23:56:35 +0200599 | superio_inb(SUPERIO_REG_BASE + 1);
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700600 if (addr == 0) {
Joe Perches512504e2010-10-20 06:51:49 +0000601 pr_info("Device address not set, will not use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 superio_exit();
603 return -ENODEV;
604 }
605
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800606 /*
607 * Enable only if address is set (needed at least on the
608 * Compaq Presario S4000NX)
609 */
Jean Delvarefa0bff02009-12-16 21:38:27 +0100610 sio_data->activate = superio_inb(SUPERIO_REG_ACT);
611 if ((sio_data->activate & 0x01) == 0) {
Joe Perches512504e2010-10-20 06:51:49 +0000612 pr_info("Enabling device\n");
Jean Delvarefa0bff02009-12-16 21:38:27 +0100613 superio_outb(SUPERIO_REG_ACT, sio_data->activate | 0x01);
614 }
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 superio_exit();
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700617 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Jean Delvarefa0bff02009-12-16 21:38:27 +0100620/* Restore device to its initial state */
Jeff Mahoneya00d6432010-01-25 15:00:48 +0100621static void smsc47m1_restore(const struct smsc47m1_sio_data *sio_data)
Jean Delvarefa0bff02009-12-16 21:38:27 +0100622{
623 if ((sio_data->activate & 0x01) == 0) {
Guenter Roeckd6410402019-04-04 11:28:37 -0700624 if (!superio_enter()) {
625 superio_select();
626 pr_info("Disabling device\n");
627 superio_outb(SUPERIO_REG_ACT, sio_data->activate);
628 superio_exit();
629 } else {
630 pr_warn("Failed to disable device\n");
631 }
Jean Delvarefa0bff02009-12-16 21:38:27 +0100632 }
633}
634
Jean Delvarea0e92d72009-12-16 21:38:26 +0100635#define CHECK 1
636#define REQUEST 2
Jean Delvarea0e92d72009-12-16 21:38:26 +0100637
638/*
639 * This function can be used to:
640 * - test for resource conflicts with ACPI
641 * - request the resources
Jean Delvarea0e92d72009-12-16 21:38:26 +0100642 * We only allocate the I/O ports we really need, to minimize the risk of
643 * conflicts with ACPI or with other drivers.
644 */
Guenter Roeck08ad7c92012-06-02 12:04:07 -0700645static int __init smsc47m1_handle_resources(unsigned short address,
646 enum chips type, int action,
647 struct device *dev)
Jean Delvarea0e92d72009-12-16 21:38:26 +0100648{
649 static const u8 ports_m1[] = {
650 /* register, region length */
651 0x04, 1,
652 0x33, 4,
653 0x56, 7,
654 };
655
656 static const u8 ports_m2[] = {
657 /* register, region length */
658 0x04, 1,
659 0x09, 1,
660 0x2c, 2,
661 0x35, 4,
662 0x56, 7,
663 0x69, 4,
664 };
665
666 int i, ports_size, err;
667 const u8 *ports;
668
669 switch (type) {
670 case smsc47m1:
671 default:
672 ports = ports_m1;
673 ports_size = ARRAY_SIZE(ports_m1);
674 break;
675 case smsc47m2:
676 ports = ports_m2;
677 ports_size = ARRAY_SIZE(ports_m2);
678 break;
679 }
680
681 for (i = 0; i + 1 < ports_size; i += 2) {
682 unsigned short start = address + ports[i];
683 unsigned short len = ports[i + 1];
684
685 switch (action) {
686 case CHECK:
687 /* Only check for conflicts */
688 err = acpi_check_region(start, len, DRVNAME);
689 if (err)
690 return err;
691 break;
692 case REQUEST:
693 /* Request the resources */
Guenter Roeck08ad7c92012-06-02 12:04:07 -0700694 if (!devm_request_region(dev, start, len, DRVNAME)) {
695 dev_err(dev,
696 "Region 0x%hx-0x%hx already in use!\n",
697 start, start + len);
Jean Delvarea0e92d72009-12-16 21:38:26 +0100698 return -EBUSY;
699 }
700 break;
Jean Delvarea0e92d72009-12-16 21:38:26 +0100701 }
702 }
703
704 return 0;
705}
706
Guenter Roeck7e6126852012-01-16 17:15:02 -0800707static void smsc47m1_remove_files(struct device *dev)
708{
709 sysfs_remove_group(&dev->kobj, &smsc47m1_group);
710 sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan1);
711 sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan2);
712 sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan3);
713 sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm1);
714 sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm2);
715 sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm3);
716}
717
Jean Delvare3ecf44b2009-12-16 21:38:26 +0100718static int __init smsc47m1_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Jean Delvare51f2cca2007-05-08 17:22:00 +0200720 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +0900721 struct smsc47m1_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct smsc47m1_data *data;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200723 struct resource *res;
Jean Delvarea0e92d72009-12-16 21:38:26 +0100724 int err;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200725 int fan1, fan2, fan3, pwm1, pwm2, pwm3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800727 static const char * const names[] = {
Jean Delvare51f2cca2007-05-08 17:22:00 +0200728 "smsc47m1",
729 "smsc47m2",
730 };
731
732 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Jean Delvarea0e92d72009-12-16 21:38:26 +0100733 err = smsc47m1_handle_resources(res->start, sio_data->type,
734 REQUEST, dev);
735 if (err < 0)
736 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Guenter Roeck08ad7c92012-06-02 12:04:07 -0700738 data = devm_kzalloc(dev, sizeof(struct smsc47m1_data), GFP_KERNEL);
739 if (!data)
740 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Jean Delvare51f2cca2007-05-08 17:22:00 +0200742 data->addr = res->start;
743 data->type = sio_data->type;
744 data->name = names[sio_data->type];
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100745 mutex_init(&data->update_lock);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200746 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800748 /*
749 * If no function is properly configured, there's no point in
750 * actually registering the chip.
751 */
Jean Delvare51f2cca2007-05-08 17:22:00 +0200752 pwm1 = (smsc47m1_read_value(data, SMSC47M1_REG_PPIN(0)) & 0x05)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 == 0x04;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200754 pwm2 = (smsc47m1_read_value(data, SMSC47M1_REG_PPIN(1)) & 0x05)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 == 0x04;
Jean Delvare8eccbb62007-05-08 17:21:59 +0200756 if (data->type == smsc47m2) {
Jean Delvare51f2cca2007-05-08 17:22:00 +0200757 fan1 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN1)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200758 & 0x0d) == 0x09;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200759 fan2 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN2)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200760 & 0x0d) == 0x09;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200761 fan3 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN3)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200762 & 0x0d) == 0x0d;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200763 pwm3 = (smsc47m1_read_value(data, SMSC47M2_REG_PPIN3)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200764 & 0x0d) == 0x08;
765 } else {
Jean Delvare51f2cca2007-05-08 17:22:00 +0200766 fan1 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(0))
Jean Delvare8eccbb62007-05-08 17:21:59 +0200767 & 0x05) == 0x05;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200768 fan2 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(1))
Jean Delvare8eccbb62007-05-08 17:21:59 +0200769 & 0x05) == 0x05;
770 fan3 = 0;
771 pwm3 = 0;
772 }
773 if (!(fan1 || fan2 || fan3 || pwm1 || pwm2 || pwm3)) {
Jean Delvare51f2cca2007-05-08 17:22:00 +0200774 dev_warn(dev, "Device not configured, will not use\n");
Guenter Roeck08ad7c92012-06-02 12:04:07 -0700775 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
Guenter Roeck85a0c0d2012-01-14 22:27:00 -0800778 /*
779 * Some values (fan min, clock dividers, pwm registers) may be
780 * needed before any update is triggered, so we better read them
781 * at least once here. We don't usually do it that way, but in
782 * this particular case, manually reading 5 registers out of 8
783 * doesn't make much sense and we're better using the existing
784 * function.
785 */
Jean Delvare51f2cca2007-05-08 17:22:00 +0200786 smsc47m1_update_device(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400788 /* Register sysfs hooks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (fan1) {
Guenter Roeck7e6126852012-01-16 17:15:02 -0800790 err = sysfs_create_group(&dev->kobj,
791 &smsc47m1_group_fan1);
792 if (err)
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200793 goto error_remove_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 } else
Jean Delvare51f2cca2007-05-08 17:22:00 +0200795 dev_dbg(dev, "Fan 1 not enabled by hardware, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 if (fan2) {
Guenter Roeck7e6126852012-01-16 17:15:02 -0800798 err = sysfs_create_group(&dev->kobj,
799 &smsc47m1_group_fan2);
800 if (err)
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200801 goto error_remove_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 } else
Jean Delvare51f2cca2007-05-08 17:22:00 +0200803 dev_dbg(dev, "Fan 2 not enabled by hardware, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Jean Delvare8eccbb62007-05-08 17:21:59 +0200805 if (fan3) {
Guenter Roeck7e6126852012-01-16 17:15:02 -0800806 err = sysfs_create_group(&dev->kobj,
807 &smsc47m1_group_fan3);
808 if (err)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200809 goto error_remove_files;
Jean Delvare8477d022007-08-16 14:33:37 +0200810 } else if (data->type == smsc47m2)
Jean Delvare51f2cca2007-05-08 17:22:00 +0200811 dev_dbg(dev, "Fan 3 not enabled by hardware, skipping\n");
Jean Delvare8eccbb62007-05-08 17:21:59 +0200812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (pwm1) {
Guenter Roeck7e6126852012-01-16 17:15:02 -0800814 err = sysfs_create_group(&dev->kobj,
815 &smsc47m1_group_pwm1);
816 if (err)
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200817 goto error_remove_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 } else
Jean Delvare51f2cca2007-05-08 17:22:00 +0200819 dev_dbg(dev, "PWM 1 not enabled by hardware, skipping\n");
Jean Delvare8eccbb62007-05-08 17:21:59 +0200820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (pwm2) {
Guenter Roeck7e6126852012-01-16 17:15:02 -0800822 err = sysfs_create_group(&dev->kobj,
823 &smsc47m1_group_pwm2);
824 if (err)
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200825 goto error_remove_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 } else
Jean Delvare51f2cca2007-05-08 17:22:00 +0200827 dev_dbg(dev, "PWM 2 not enabled by hardware, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Jean Delvare8eccbb62007-05-08 17:21:59 +0200829 if (pwm3) {
Guenter Roeck7e6126852012-01-16 17:15:02 -0800830 err = sysfs_create_group(&dev->kobj,
831 &smsc47m1_group_pwm3);
832 if (err)
Jean Delvare8eccbb62007-05-08 17:21:59 +0200833 goto error_remove_files;
Jean Delvare8477d022007-08-16 14:33:37 +0200834 } else if (data->type == smsc47m2)
Jean Delvare51f2cca2007-05-08 17:22:00 +0200835 dev_dbg(dev, "PWM 3 not enabled by hardware, skipping\n");
Jean Delvare8eccbb62007-05-08 17:21:59 +0200836
Guenter Roeck7e6126852012-01-16 17:15:02 -0800837 err = sysfs_create_group(&dev->kobj, &smsc47m1_group);
838 if (err)
Jean Delvare68a50b52007-08-12 13:58:50 +0200839 goto error_remove_files;
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200840
Tony Jones1beeffe2007-08-20 13:46:20 -0700841 data->hwmon_dev = hwmon_device_register(dev);
842 if (IS_ERR(data->hwmon_dev)) {
843 err = PTR_ERR(data->hwmon_dev);
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200844 goto error_remove_files;
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 return 0;
848
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200849error_remove_files:
Guenter Roeck7e6126852012-01-16 17:15:02 -0800850 smsc47m1_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return err;
852}
853
Jean Delvare3ecf44b2009-12-16 21:38:26 +0100854static int __exit smsc47m1_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Jean Delvare51f2cca2007-05-08 17:22:00 +0200856 struct smsc47m1_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Tony Jones1beeffe2007-08-20 13:46:20 -0700858 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeck7e6126852012-01-16 17:15:02 -0800859 smsc47m1_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return 0;
862}
863
Axel Line70198a2014-07-17 19:15:36 +0800864static struct platform_driver smsc47m1_driver = {
865 .driver = {
Axel Line70198a2014-07-17 19:15:36 +0800866 .name = DRVNAME,
867 },
868 .remove = __exit_p(smsc47m1_remove),
869};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Jean Delvare51f2cca2007-05-08 17:22:00 +0200871static int __init smsc47m1_device_add(unsigned short address,
872 const struct smsc47m1_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Jean Delvare51f2cca2007-05-08 17:22:00 +0200874 struct resource res = {
875 .start = address,
876 .end = address + SMSC_EXTENT - 1,
877 .name = DRVNAME,
878 .flags = IORESOURCE_IO,
879 };
880 int err;
881
Jean Delvarea0e92d72009-12-16 21:38:26 +0100882 err = smsc47m1_handle_resources(address, sio_data->type, CHECK, NULL);
Jean Delvareb9acb642009-01-07 16:37:35 +0100883 if (err)
884 goto exit;
885
Jean Delvare51f2cca2007-05-08 17:22:00 +0200886 pdev = platform_device_alloc(DRVNAME, address);
887 if (!pdev) {
888 err = -ENOMEM;
Joe Perches512504e2010-10-20 06:51:49 +0000889 pr_err("Device allocation failed\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200890 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892
Jean Delvare51f2cca2007-05-08 17:22:00 +0200893 err = platform_device_add_resources(pdev, &res, 1);
894 if (err) {
Joe Perches512504e2010-10-20 06:51:49 +0000895 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200896 goto exit_device_put;
897 }
898
Jean Delvare2df6d812007-06-09 10:11:16 -0400899 err = platform_device_add_data(pdev, sio_data,
900 sizeof(struct smsc47m1_sio_data));
901 if (err) {
Joe Perches512504e2010-10-20 06:51:49 +0000902 pr_err("Platform data allocation failed\n");
Jean Delvare51f2cca2007-05-08 17:22:00 +0200903 goto exit_device_put;
904 }
Jean Delvare51f2cca2007-05-08 17:22:00 +0200905
906 err = platform_device_add(pdev);
907 if (err) {
Joe Perches512504e2010-10-20 06:51:49 +0000908 pr_err("Device addition failed (%d)\n", err);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200909 goto exit_device_put;
910 }
911
912 return 0;
913
914exit_device_put:
915 platform_device_put(pdev);
916exit:
917 return err;
918}
919
920static int __init sm_smsc47m1_init(void)
921{
922 int err;
923 unsigned short address;
924 struct smsc47m1_sio_data sio_data;
925
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700926 err = smsc47m1_find(&sio_data);
927 if (err < 0)
928 return err;
929 address = err;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200930
Jean Delvare51f2cca2007-05-08 17:22:00 +0200931 /* Sets global pdev as a side effect */
932 err = smsc47m1_device_add(address, &sio_data);
933 if (err)
Guenter Roeck1d0045e2012-03-28 08:55:12 -0700934 return err;
Jean Delvare3ecf44b2009-12-16 21:38:26 +0100935
936 err = platform_driver_probe(&smsc47m1_driver, smsc47m1_probe);
937 if (err)
938 goto exit_device;
Jean Delvare51f2cca2007-05-08 17:22:00 +0200939
940 return 0;
941
Jean Delvare3ecf44b2009-12-16 21:38:26 +0100942exit_device:
943 platform_device_unregister(pdev);
Jean Delvarefa0bff02009-12-16 21:38:27 +0100944 smsc47m1_restore(&sio_data);
Jean Delvare51f2cca2007-05-08 17:22:00 +0200945 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
948static void __exit sm_smsc47m1_exit(void)
949{
Jean Delvare51f2cca2007-05-08 17:22:00 +0200950 platform_driver_unregister(&smsc47m1_driver);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +0900951 smsc47m1_restore(dev_get_platdata(&pdev->dev));
Jean Delvare3ecf44b2009-12-16 21:38:26 +0100952 platform_device_unregister(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
956MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver");
957MODULE_LICENSE("GPL");
958
959module_init(sm_smsc47m1_init);
960module_exit(sm_smsc47m1_exit);