blob: eedf574ab539c9108c7dec18e16e57994f9cee12 [file] [log] [blame]
Riku Voipio84f1e442007-08-24 13:03:09 +03001/*
Björn Gerhartf58c44e2011-12-13 13:19:58 -05002 * f75375s.c - driver for the Fintek F75375/SP, F75373 and
3 * F75387SG/RG hardware monitoring features
Riku Voipiob26e0ed2009-03-03 21:37:17 +02004 * Copyright (C) 2006-2007 Riku Voipio
Riku Voipio84f1e442007-08-24 13:03:09 +03005 *
6 * Datasheets available at:
7 *
8 * f75375:
Guenter Roeck4fd826e2011-12-08 09:54:18 -08009 * http://www.fintek.com.tw/files/productfiles/F75375_V026P.pdf
Riku Voipio84f1e442007-08-24 13:03:09 +030010 *
11 * f75373:
Justin P. Mattock631dd1a2010-10-18 11:03:14 +020012 * http://www.fintek.com.tw/files/productfiles/F75373_V025P.pdf
Riku Voipio84f1e442007-08-24 13:03:09 +030013 *
Björn Gerhartf58c44e2011-12-13 13:19:58 -050014 * f75387:
15 * http://www.fintek.com.tw/files/productfiles/F75387_V027P.pdf
16 *
Riku Voipio84f1e442007-08-24 13:03:09 +030017 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 *
31 */
32
33#include <linux/module.h>
34#include <linux/jiffies.h>
35#include <linux/hwmon.h>
36#include <linux/hwmon-sysfs.h>
37#include <linux/i2c.h>
38#include <linux/err.h>
39#include <linux/mutex.h>
Riku Voipioff312d02007-09-26 13:14:40 +030040#include <linux/f75375s.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Riku Voipio84f1e442007-08-24 13:03:09 +030042
43/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050044static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
Riku Voipio84f1e442007-08-24 13:03:09 +030045
Björn Gerhartf58c44e2011-12-13 13:19:58 -050046enum chips { f75373, f75375, f75387 };
Riku Voipio84f1e442007-08-24 13:03:09 +030047
48/* Fintek F75375 registers */
49#define F75375_REG_CONFIG0 0x0
50#define F75375_REG_CONFIG1 0x1
51#define F75375_REG_CONFIG2 0x2
52#define F75375_REG_CONFIG3 0x3
53#define F75375_REG_ADDR 0x4
54#define F75375_REG_INTR 0x31
55#define F75375_CHIP_ID 0x5A
56#define F75375_REG_VERSION 0x5C
57#define F75375_REG_VENDOR 0x5D
58#define F75375_REG_FAN_TIMER 0x60
59
60#define F75375_REG_VOLT(nr) (0x10 + (nr))
61#define F75375_REG_VOLT_HIGH(nr) (0x20 + (nr) * 2)
62#define F75375_REG_VOLT_LOW(nr) (0x21 + (nr) * 2)
63
64#define F75375_REG_TEMP(nr) (0x14 + (nr))
Björn Gerhartf58c44e2011-12-13 13:19:58 -050065#define F75387_REG_TEMP11_LSB(nr) (0x1a + (nr))
Riku Voipio84f1e442007-08-24 13:03:09 +030066#define F75375_REG_TEMP_HIGH(nr) (0x28 + (nr) * 2)
67#define F75375_REG_TEMP_HYST(nr) (0x29 + (nr) * 2)
68
69#define F75375_REG_FAN(nr) (0x16 + (nr) * 2)
70#define F75375_REG_FAN_MIN(nr) (0x2C + (nr) * 2)
71#define F75375_REG_FAN_FULL(nr) (0x70 + (nr) * 0x10)
72#define F75375_REG_FAN_PWM_DUTY(nr) (0x76 + (nr) * 0x10)
73#define F75375_REG_FAN_PWM_CLOCK(nr) (0x7D + (nr) * 0x10)
74
75#define F75375_REG_FAN_EXP(nr) (0x74 + (nr) * 0x10)
76#define F75375_REG_FAN_B_TEMP(nr, step) ((0xA0 + (nr) * 0x10) + (step))
77#define F75375_REG_FAN_B_SPEED(nr, step) \
78 ((0xA5 + (nr) * 0x10) + (step) * 2)
79
80#define F75375_REG_PWM1_RAISE_DUTY 0x69
81#define F75375_REG_PWM2_RAISE_DUTY 0x6A
82#define F75375_REG_PWM1_DROP_DUTY 0x6B
83#define F75375_REG_PWM2_DROP_DUTY 0x6C
84
Björn Gerhartf58c44e2011-12-13 13:19:58 -050085#define F75375_FAN_CTRL_LINEAR(nr) (4 + nr)
86#define F75387_FAN_CTRL_LINEAR(nr) (1 + ((nr) * 4))
Guillem Jover96f36402010-09-17 17:24:11 +020087#define FAN_CTRL_MODE(nr) (4 + ((nr) * 2))
Björn Gerhartf58c44e2011-12-13 13:19:58 -050088#define F75387_FAN_DUTY_MODE(nr) (2 + ((nr) * 4))
89#define F75387_FAN_MANU_MODE(nr) ((nr) * 4)
Riku Voipio84f1e442007-08-24 13:03:09 +030090
91/*
92 * Data structures and manipulation thereof
93 */
94
95struct f75375_data {
96 unsigned short addr;
Tony Jones1beeffe2007-08-20 13:46:20 -070097 struct device *hwmon_dev;
Riku Voipio84f1e442007-08-24 13:03:09 +030098
99 const char *name;
100 int kind;
101 struct mutex update_lock; /* protect register access */
102 char valid;
103 unsigned long last_updated; /* In jiffies */
104 unsigned long last_limits; /* In jiffies */
105
106 /* Register values */
107 u8 in[4];
108 u8 in_max[4];
109 u8 in_min[4];
110 u16 fan[2];
111 u16 fan_min[2];
Guenter Roeck740f6be32011-12-08 09:59:25 -0800112 u16 fan_max[2];
113 u16 fan_target[2];
Riku Voipio84f1e442007-08-24 13:03:09 +0300114 u8 fan_timer;
115 u8 pwm[2];
116 u8 pwm_mode[2];
117 u8 pwm_enable[2];
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500118 /*
119 * f75387: For remote temperature reading, it uses signed 11-bit
120 * values with LSB = 0.125 degree Celsius, left-justified in 16-bit
121 * registers. For original 8-bit temp readings, the LSB just is 0.
122 */
123 s16 temp11[2];
Riku Voipio84f1e442007-08-24 13:03:09 +0300124 s8 temp_high[2];
125 s8 temp_max_hyst[2];
126};
127
Jean Delvare310ec792009-12-14 21:17:23 +0100128static int f75375_detect(struct i2c_client *client,
Jean Delvare935ada82008-07-16 19:30:11 +0200129 struct i2c_board_info *info);
Jean Delvared2653e92008-04-29 23:11:39 +0200130static int f75375_probe(struct i2c_client *client,
131 const struct i2c_device_id *id);
Riku Voipio620c1422007-10-16 12:46:49 +0300132static int f75375_remove(struct i2c_client *client);
133
Jean Delvare3760f732008-04-29 23:11:40 +0200134static const struct i2c_device_id f75375_id[] = {
135 { "f75373", f75373 },
136 { "f75375", f75375 },
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500137 { "f75387", f75387 },
Jean Delvare3760f732008-04-29 23:11:40 +0200138 { }
139};
140MODULE_DEVICE_TABLE(i2c, f75375_id);
141
Riku Voipio84f1e442007-08-24 13:03:09 +0300142static struct i2c_driver f75375_driver = {
Jean Delvare935ada82008-07-16 19:30:11 +0200143 .class = I2C_CLASS_HWMON,
Riku Voipio84f1e442007-08-24 13:03:09 +0300144 .driver = {
145 .name = "f75375",
146 },
Riku Voipio620c1422007-10-16 12:46:49 +0300147 .probe = f75375_probe,
148 .remove = f75375_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200149 .id_table = f75375_id,
Jean Delvare935ada82008-07-16 19:30:11 +0200150 .detect = f75375_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100151 .address_list = normal_i2c,
Riku Voipio84f1e442007-08-24 13:03:09 +0300152};
153
154static inline int f75375_read8(struct i2c_client *client, u8 reg)
155{
156 return i2c_smbus_read_byte_data(client, reg);
157}
158
159/* in most cases, should be called while holding update_lock */
160static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
161{
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800162 return (i2c_smbus_read_byte_data(client, reg) << 8)
163 | i2c_smbus_read_byte_data(client, reg + 1);
Riku Voipio84f1e442007-08-24 13:03:09 +0300164}
165
166static inline void f75375_write8(struct i2c_client *client, u8 reg,
167 u8 value)
168{
169 i2c_smbus_write_byte_data(client, reg, value);
170}
171
172static inline void f75375_write16(struct i2c_client *client, u8 reg,
173 u16 value)
174{
175 int err = i2c_smbus_write_byte_data(client, reg, (value << 8));
176 if (err)
177 return;
178 i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
179}
180
181static struct f75375_data *f75375_update_device(struct device *dev)
182{
183 struct i2c_client *client = to_i2c_client(dev);
184 struct f75375_data *data = i2c_get_clientdata(client);
185 int nr;
186
187 mutex_lock(&data->update_lock);
188
189 /* Limit registers cache is refreshed after 60 seconds */
190 if (time_after(jiffies, data->last_limits + 60 * HZ)
191 || !data->valid) {
192 for (nr = 0; nr < 2; nr++) {
193 data->temp_high[nr] =
194 f75375_read8(client, F75375_REG_TEMP_HIGH(nr));
195 data->temp_max_hyst[nr] =
196 f75375_read8(client, F75375_REG_TEMP_HYST(nr));
Guenter Roeck740f6be32011-12-08 09:59:25 -0800197 data->fan_max[nr] =
Riku Voipio84f1e442007-08-24 13:03:09 +0300198 f75375_read16(client, F75375_REG_FAN_FULL(nr));
199 data->fan_min[nr] =
200 f75375_read16(client, F75375_REG_FAN_MIN(nr));
Guenter Roeck740f6be32011-12-08 09:59:25 -0800201 data->fan_target[nr] =
Riku Voipio84f1e442007-08-24 13:03:09 +0300202 f75375_read16(client, F75375_REG_FAN_EXP(nr));
203 data->pwm[nr] = f75375_read8(client,
204 F75375_REG_FAN_PWM_DUTY(nr));
205
206 }
207 for (nr = 0; nr < 4; nr++) {
208 data->in_max[nr] =
209 f75375_read8(client, F75375_REG_VOLT_HIGH(nr));
210 data->in_min[nr] =
211 f75375_read8(client, F75375_REG_VOLT_LOW(nr));
212 }
213 data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
214 data->last_limits = jiffies;
215 }
216
217 /* Measurement registers cache is refreshed after 2 second */
218 if (time_after(jiffies, data->last_updated + 2 * HZ)
219 || !data->valid) {
220 for (nr = 0; nr < 2; nr++) {
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500221 /* assign MSB, therefore shift it by 8 bits */
222 data->temp11[nr] =
223 f75375_read8(client, F75375_REG_TEMP(nr)) << 8;
224 if (data->kind == f75387)
225 /* merge F75387's temperature LSB (11-bit) */
226 data->temp11[nr] |=
227 f75375_read8(client,
228 F75387_REG_TEMP11_LSB(nr));
Riku Voipio84f1e442007-08-24 13:03:09 +0300229 data->fan[nr] =
230 f75375_read16(client, F75375_REG_FAN(nr));
231 }
232 for (nr = 0; nr < 4; nr++)
233 data->in[nr] =
234 f75375_read8(client, F75375_REG_VOLT(nr));
235
236 data->last_updated = jiffies;
237 data->valid = 1;
238 }
239
240 mutex_unlock(&data->update_lock);
241 return data;
242}
243
244static inline u16 rpm_from_reg(u16 reg)
245{
246 if (reg == 0 || reg == 0xffff)
247 return 0;
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800248 return 1500000 / reg;
Riku Voipio84f1e442007-08-24 13:03:09 +0300249}
250
251static inline u16 rpm_to_reg(int rpm)
252{
253 if (rpm < 367 || rpm > 0xffff)
254 return 0xffff;
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800255 return 1500000 / rpm;
Riku Voipio84f1e442007-08-24 13:03:09 +0300256}
257
258static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
259 const char *buf, size_t count)
260{
261 int nr = to_sensor_dev_attr(attr)->index;
262 struct i2c_client *client = to_i2c_client(dev);
263 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800264 unsigned long val;
265 int err;
266
267 err = kstrtoul(buf, 10, &val);
268 if (err < 0)
269 return err;
Riku Voipio84f1e442007-08-24 13:03:09 +0300270
271 mutex_lock(&data->update_lock);
272 data->fan_min[nr] = rpm_to_reg(val);
273 f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
274 mutex_unlock(&data->update_lock);
275 return count;
276}
277
Guenter Roeck740f6be32011-12-08 09:59:25 -0800278static ssize_t set_fan_target(struct device *dev, struct device_attribute *attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300279 const char *buf, size_t count)
280{
281 int nr = to_sensor_dev_attr(attr)->index;
282 struct i2c_client *client = to_i2c_client(dev);
283 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800284 unsigned long val;
285 int err;
286
287 err = kstrtoul(buf, 10, &val);
288 if (err < 0)
289 return err;
Riku Voipio84f1e442007-08-24 13:03:09 +0300290
291 mutex_lock(&data->update_lock);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800292 data->fan_target[nr] = rpm_to_reg(val);
293 f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_target[nr]);
Riku Voipio84f1e442007-08-24 13:03:09 +0300294 mutex_unlock(&data->update_lock);
295 return count;
296}
297
298static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
299 const char *buf, size_t count)
300{
301 int nr = to_sensor_dev_attr(attr)->index;
302 struct i2c_client *client = to_i2c_client(dev);
303 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800304 unsigned long val;
305 int err;
306
307 err = kstrtoul(buf, 10, &val);
308 if (err < 0)
309 return err;
Riku Voipio84f1e442007-08-24 13:03:09 +0300310
311 mutex_lock(&data->update_lock);
312 data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
313 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr), data->pwm[nr]);
314 mutex_unlock(&data->update_lock);
315 return count;
316}
317
318static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
319 *attr, char *buf)
320{
321 int nr = to_sensor_dev_attr(attr)->index;
322 struct f75375_data *data = f75375_update_device(dev);
323 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
324}
325
Riku Voipioff312d02007-09-26 13:14:40 +0300326static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
Riku Voipio84f1e442007-08-24 13:03:09 +0300327{
Riku Voipio84f1e442007-08-24 13:03:09 +0300328 struct f75375_data *data = i2c_get_clientdata(client);
Riku Voipio84f1e442007-08-24 13:03:09 +0300329 u8 fanmode;
330
Guenter Roeck33106002011-12-08 10:01:25 -0800331 if (val < 0 || val > 3)
Riku Voipio84f1e442007-08-24 13:03:09 +0300332 return -EINVAL;
333
Riku Voipio84f1e442007-08-24 13:03:09 +0300334 fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500335 if (data->kind == f75387) {
336 /* clear each fanX_mode bit before setting them properly */
337 fanmode &= ~(1 << F75387_FAN_DUTY_MODE(nr));
338 fanmode &= ~(1 << F75387_FAN_MANU_MODE(nr));
339 switch (val) {
340 case 0: /* full speed */
341 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
342 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
343 data->pwm[nr] = 255;
344 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
345 data->pwm[nr]);
346 break;
347 case 1: /* PWM */
348 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
349 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
350 break;
351 case 2: /* AUTOMATIC*/
352 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
353 break;
354 case 3: /* fan speed */
355 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
356 break;
357 }
358 } else {
359 /* clear each fanX_mode bit before setting them properly */
360 fanmode &= ~(3 << FAN_CTRL_MODE(nr));
361 switch (val) {
362 case 0: /* full speed */
363 fanmode |= (3 << FAN_CTRL_MODE(nr));
364 data->pwm[nr] = 255;
365 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
366 data->pwm[nr]);
367 break;
368 case 1: /* PWM */
369 fanmode |= (3 << FAN_CTRL_MODE(nr));
370 break;
371 case 2: /* AUTOMATIC*/
372 fanmode |= (2 << FAN_CTRL_MODE(nr));
373 break;
374 case 3: /* fan speed */
375 break;
376 }
Riku Voipio84f1e442007-08-24 13:03:09 +0300377 }
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500378
Riku Voipio84f1e442007-08-24 13:03:09 +0300379 f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
380 data->pwm_enable[nr] = val;
Riku Voipioff312d02007-09-26 13:14:40 +0300381 return 0;
382}
383
384static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
385 const char *buf, size_t count)
386{
387 int nr = to_sensor_dev_attr(attr)->index;
388 struct i2c_client *client = to_i2c_client(dev);
389 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800390 unsigned long val;
391 int err;
392
393 err = kstrtoul(buf, 10, &val);
394 if (err < 0)
395 return err;
Riku Voipioff312d02007-09-26 13:14:40 +0300396
397 mutex_lock(&data->update_lock);
398 err = set_pwm_enable_direct(client, nr, val);
Riku Voipio84f1e442007-08-24 13:03:09 +0300399 mutex_unlock(&data->update_lock);
Riku Voipioff312d02007-09-26 13:14:40 +0300400 return err ? err : count;
Riku Voipio84f1e442007-08-24 13:03:09 +0300401}
402
403static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
404 const char *buf, size_t count)
405{
406 int nr = to_sensor_dev_attr(attr)->index;
407 struct i2c_client *client = to_i2c_client(dev);
408 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800409 unsigned long val;
410 int err;
411 u8 conf;
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500412 char reg, ctrl;
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800413
414 err = kstrtoul(buf, 10, &val);
415 if (err < 0)
416 return err;
Riku Voipio84f1e442007-08-24 13:03:09 +0300417
Riku Voipio76e83be2007-10-26 14:14:23 +0300418 if (!(val == 0 || val == 1))
Riku Voipio84f1e442007-08-24 13:03:09 +0300419 return -EINVAL;
420
Guenter Roeck5cd32222011-12-08 10:55:16 -0800421 /* F75373 does not support DC (linear voltage) fan control mode */
422 if (data->kind == f75373 && val == 0)
423 return -EINVAL;
424
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500425 /* take care for different registers */
426 if (data->kind == f75387) {
427 reg = F75375_REG_FAN_TIMER;
428 ctrl = F75387_FAN_CTRL_LINEAR(nr);
429 } else {
430 reg = F75375_REG_CONFIG1;
431 ctrl = F75375_FAN_CTRL_LINEAR(nr);
432 }
433
Riku Voipio84f1e442007-08-24 13:03:09 +0300434 mutex_lock(&data->update_lock);
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500435 conf = f75375_read8(client, reg);
436 conf &= ~(1 << ctrl);
Riku Voipio84f1e442007-08-24 13:03:09 +0300437
438 if (val == 0)
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500439 conf |= (1 << ctrl);
Riku Voipio84f1e442007-08-24 13:03:09 +0300440
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500441 f75375_write8(client, reg, conf);
Riku Voipio84f1e442007-08-24 13:03:09 +0300442 data->pwm_mode[nr] = val;
443 mutex_unlock(&data->update_lock);
444 return count;
445}
446
447static ssize_t show_pwm(struct device *dev, struct device_attribute
448 *attr, char *buf)
449{
450 int nr = to_sensor_dev_attr(attr)->index;
451 struct f75375_data *data = f75375_update_device(dev);
452 return sprintf(buf, "%d\n", data->pwm[nr]);
453}
454
455static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
456 *attr, char *buf)
457{
458 int nr = to_sensor_dev_attr(attr)->index;
459 struct f75375_data *data = f75375_update_device(dev);
460 return sprintf(buf, "%d\n", data->pwm_mode[nr]);
461}
462
463#define VOLT_FROM_REG(val) ((val) * 8)
464#define VOLT_TO_REG(val) ((val) / 8)
465
466static ssize_t show_in(struct device *dev, struct device_attribute *attr,
467 char *buf)
468{
469 int nr = to_sensor_dev_attr(attr)->index;
470 struct f75375_data *data = f75375_update_device(dev);
471 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
472}
473
474static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
475 char *buf)
476{
477 int nr = to_sensor_dev_attr(attr)->index;
478 struct f75375_data *data = f75375_update_device(dev);
479 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
480}
481
482static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
483 char *buf)
484{
485 int nr = to_sensor_dev_attr(attr)->index;
486 struct f75375_data *data = f75375_update_device(dev);
487 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
488}
489
490static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
491 const char *buf, size_t count)
492{
493 int nr = to_sensor_dev_attr(attr)->index;
494 struct i2c_client *client = to_i2c_client(dev);
495 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800496 unsigned long val;
497 int err;
498
499 err = kstrtoul(buf, 10, &val);
500 if (err < 0)
501 return err;
502
Riku Voipio84f1e442007-08-24 13:03:09 +0300503 val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
504 mutex_lock(&data->update_lock);
505 data->in_max[nr] = val;
506 f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
507 mutex_unlock(&data->update_lock);
508 return count;
509}
510
511static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
512 const char *buf, size_t count)
513{
514 int nr = to_sensor_dev_attr(attr)->index;
515 struct i2c_client *client = to_i2c_client(dev);
516 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800517 unsigned long val;
518 int err;
519
520 err = kstrtoul(buf, 10, &val);
521 if (err < 0)
522 return err;
523
Riku Voipio84f1e442007-08-24 13:03:09 +0300524 val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
525 mutex_lock(&data->update_lock);
526 data->in_min[nr] = val;
527 f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
528 mutex_unlock(&data->update_lock);
529 return count;
530}
531#define TEMP_FROM_REG(val) ((val) * 1000)
532#define TEMP_TO_REG(val) ((val) / 1000)
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500533#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
Riku Voipio84f1e442007-08-24 13:03:09 +0300534
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500535static ssize_t show_temp11(struct device *dev, struct device_attribute *attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300536 char *buf)
537{
538 int nr = to_sensor_dev_attr(attr)->index;
539 struct f75375_data *data = f75375_update_device(dev);
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500540 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[nr]));
Riku Voipio84f1e442007-08-24 13:03:09 +0300541}
542
543static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
544 char *buf)
545{
546 int nr = to_sensor_dev_attr(attr)->index;
547 struct f75375_data *data = f75375_update_device(dev);
548 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
549}
550
551static ssize_t show_temp_max_hyst(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
554 int nr = to_sensor_dev_attr(attr)->index;
555 struct f75375_data *data = f75375_update_device(dev);
556 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
557}
558
559static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
560 const char *buf, size_t count)
561{
562 int nr = to_sensor_dev_attr(attr)->index;
563 struct i2c_client *client = to_i2c_client(dev);
564 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800565 unsigned long val;
566 int err;
567
568 err = kstrtoul(buf, 10, &val);
569 if (err < 0)
570 return err;
571
Riku Voipio84f1e442007-08-24 13:03:09 +0300572 val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
573 mutex_lock(&data->update_lock);
574 data->temp_high[nr] = val;
575 f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
576 mutex_unlock(&data->update_lock);
577 return count;
578}
579
580static ssize_t set_temp_max_hyst(struct device *dev,
581 struct device_attribute *attr, const char *buf, size_t count)
582{
583 int nr = to_sensor_dev_attr(attr)->index;
584 struct i2c_client *client = to_i2c_client(dev);
585 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800586 unsigned long val;
587 int err;
588
589 err = kstrtoul(buf, 10, &val);
590 if (err < 0)
591 return err;
592
Riku Voipio84f1e442007-08-24 13:03:09 +0300593 val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
594 mutex_lock(&data->update_lock);
595 data->temp_max_hyst[nr] = val;
596 f75375_write8(client, F75375_REG_TEMP_HYST(nr),
597 data->temp_max_hyst[nr]);
598 mutex_unlock(&data->update_lock);
599 return count;
600}
601
602#define show_fan(thing) \
603static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
604 char *buf)\
605{\
606 int nr = to_sensor_dev_attr(attr)->index;\
607 struct f75375_data *data = f75375_update_device(dev); \
608 return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
609}
610
611show_fan(fan);
612show_fan(fan_min);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800613show_fan(fan_max);
614show_fan(fan_target);
Riku Voipio84f1e442007-08-24 13:03:09 +0300615
616static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
617static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
618 show_in_max, set_in_max, 0);
619static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
620 show_in_min, set_in_min, 0);
621static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
622static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
623 show_in_max, set_in_max, 1);
624static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
625 show_in_min, set_in_min, 1);
626static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
627static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
628 show_in_max, set_in_max, 2);
629static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
630 show_in_min, set_in_min, 2);
631static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
632static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
633 show_in_max, set_in_max, 3);
634static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
635 show_in_min, set_in_min, 3);
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500636static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 0);
Riku Voipio84f1e442007-08-24 13:03:09 +0300637static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
638 show_temp_max_hyst, set_temp_max_hyst, 0);
639static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
640 show_temp_max, set_temp_max, 0);
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500641static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 1);
Riku Voipio84f1e442007-08-24 13:03:09 +0300642static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
643 show_temp_max_hyst, set_temp_max_hyst, 1);
644static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
645 show_temp_max, set_temp_max, 1);
646static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800647static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, show_fan_max, NULL, 0);
Riku Voipio84f1e442007-08-24 13:03:09 +0300648static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
649 show_fan_min, set_fan_min, 0);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800650static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO|S_IWUSR,
651 show_fan_target, set_fan_target, 0);
Riku Voipio84f1e442007-08-24 13:03:09 +0300652static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800653static SENSOR_DEVICE_ATTR(fan2_max, S_IRUGO, show_fan_max, NULL, 1);
Riku Voipio84f1e442007-08-24 13:03:09 +0300654static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
655 show_fan_min, set_fan_min, 1);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800656static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO|S_IWUSR,
657 show_fan_target, set_fan_target, 1);
Riku Voipio84f1e442007-08-24 13:03:09 +0300658static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
659 show_pwm, set_pwm, 0);
660static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
661 show_pwm_enable, set_pwm_enable, 0);
Riku Voipio76e83be2007-10-26 14:14:23 +0300662static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
Riku Voipio84f1e442007-08-24 13:03:09 +0300663 show_pwm_mode, set_pwm_mode, 0);
664static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
665 show_pwm, set_pwm, 1);
666static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
667 show_pwm_enable, set_pwm_enable, 1);
Riku Voipio76e83be2007-10-26 14:14:23 +0300668static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
Riku Voipio84f1e442007-08-24 13:03:09 +0300669 show_pwm_mode, set_pwm_mode, 1);
670
671static struct attribute *f75375_attributes[] = {
672 &sensor_dev_attr_temp1_input.dev_attr.attr,
673 &sensor_dev_attr_temp1_max.dev_attr.attr,
674 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
675 &sensor_dev_attr_temp2_input.dev_attr.attr,
676 &sensor_dev_attr_temp2_max.dev_attr.attr,
677 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
678 &sensor_dev_attr_fan1_input.dev_attr.attr,
Guenter Roeck740f6be32011-12-08 09:59:25 -0800679 &sensor_dev_attr_fan1_max.dev_attr.attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300680 &sensor_dev_attr_fan1_min.dev_attr.attr,
Guenter Roeck740f6be32011-12-08 09:59:25 -0800681 &sensor_dev_attr_fan1_target.dev_attr.attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300682 &sensor_dev_attr_fan2_input.dev_attr.attr,
Guenter Roeck740f6be32011-12-08 09:59:25 -0800683 &sensor_dev_attr_fan2_max.dev_attr.attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300684 &sensor_dev_attr_fan2_min.dev_attr.attr,
Guenter Roeck740f6be32011-12-08 09:59:25 -0800685 &sensor_dev_attr_fan2_target.dev_attr.attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300686 &sensor_dev_attr_pwm1.dev_attr.attr,
687 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
688 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
689 &sensor_dev_attr_pwm2.dev_attr.attr,
690 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
691 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
692 &sensor_dev_attr_in0_input.dev_attr.attr,
693 &sensor_dev_attr_in0_max.dev_attr.attr,
694 &sensor_dev_attr_in0_min.dev_attr.attr,
695 &sensor_dev_attr_in1_input.dev_attr.attr,
696 &sensor_dev_attr_in1_max.dev_attr.attr,
697 &sensor_dev_attr_in1_min.dev_attr.attr,
698 &sensor_dev_attr_in2_input.dev_attr.attr,
699 &sensor_dev_attr_in2_max.dev_attr.attr,
700 &sensor_dev_attr_in2_min.dev_attr.attr,
701 &sensor_dev_attr_in3_input.dev_attr.attr,
702 &sensor_dev_attr_in3_max.dev_attr.attr,
703 &sensor_dev_attr_in3_min.dev_attr.attr,
704 NULL
705};
706
707static const struct attribute_group f75375_group = {
708 .attrs = f75375_attributes,
709};
710
Riku Voipioff312d02007-09-26 13:14:40 +0300711static void f75375_init(struct i2c_client *client, struct f75375_data *data,
712 struct f75375s_platform_data *f75375s_pdata)
713{
714 int nr;
Guenter Roeckb1b561a2011-12-08 10:15:51 -0800715
716 if (!f75375s_pdata) {
717 u8 conf, mode;
718 int nr;
719
720 conf = f75375_read8(client, F75375_REG_CONFIG1);
721 mode = f75375_read8(client, F75375_REG_FAN_TIMER);
722 for (nr = 0; nr < 2; nr++) {
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500723 if (data->kind == f75387) {
724 bool manu, duty;
725
726 if (!(conf & (1 << F75387_FAN_CTRL_LINEAR(nr))))
727 data->pwm_mode[nr] = 1;
728
729 manu = ((mode >> F75387_FAN_MANU_MODE(nr)) & 1);
730 duty = ((mode >> F75387_FAN_DUTY_MODE(nr)) & 1);
731 if (manu && duty)
732 /* speed */
733 data->pwm_enable[nr] = 3;
734 else if (!manu && duty)
735 /* automatic */
736 data->pwm_enable[nr] = 2;
737 else
738 /* manual */
739 data->pwm_enable[nr] = 1;
740 } else {
741 if (!(conf & (1 << F75375_FAN_CTRL_LINEAR(nr))))
742 data->pwm_mode[nr] = 1;
743
744 switch ((mode >> FAN_CTRL_MODE(nr)) & 3) {
745 case 0: /* speed */
746 data->pwm_enable[nr] = 3;
747 break;
748 case 1: /* automatic */
749 data->pwm_enable[nr] = 2;
750 break;
751 default: /* manual */
752 data->pwm_enable[nr] = 1;
753 break;
754 }
Guenter Roeckb1b561a2011-12-08 10:15:51 -0800755 }
756 }
757 return;
758 }
759
Riku Voipioff312d02007-09-26 13:14:40 +0300760 set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
761 set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
762 for (nr = 0; nr < 2; nr++) {
763 data->pwm[nr] = SENSORS_LIMIT(f75375s_pdata->pwm[nr], 0, 255);
764 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
765 data->pwm[nr]);
766 }
767
768}
769
Jean Delvared2653e92008-04-29 23:11:39 +0200770static int f75375_probe(struct i2c_client *client,
771 const struct i2c_device_id *id)
Riku Voipio620c1422007-10-16 12:46:49 +0300772{
Andrew Klossner51b3e272009-03-12 13:36:39 +0100773 struct f75375_data *data;
Riku Voipioff312d02007-09-26 13:14:40 +0300774 struct f75375s_platform_data *f75375s_pdata = client->dev.platform_data;
Riku Voipio620c1422007-10-16 12:46:49 +0300775 int err;
776
777 if (!i2c_check_functionality(client->adapter,
778 I2C_FUNC_SMBUS_BYTE_DATA))
779 return -EIO;
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800780 data = kzalloc(sizeof(struct f75375_data), GFP_KERNEL);
781 if (!data)
Riku Voipio620c1422007-10-16 12:46:49 +0300782 return -ENOMEM;
783
784 i2c_set_clientdata(client, data);
Riku Voipio620c1422007-10-16 12:46:49 +0300785 mutex_init(&data->update_lock);
Jean Delvare3760f732008-04-29 23:11:40 +0200786 data->kind = id->driver_data;
Riku Voipio620c1422007-10-16 12:46:49 +0300787
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800788 err = sysfs_create_group(&client->dev.kobj, &f75375_group);
789 if (err)
Riku Voipio620c1422007-10-16 12:46:49 +0300790 goto exit_free;
791
Riku Voipio76e83be2007-10-26 14:14:23 +0300792 if (data->kind == f75375) {
793 err = sysfs_chmod_file(&client->dev.kobj,
794 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
795 S_IRUGO | S_IWUSR);
796 if (err)
797 goto exit_remove;
798 err = sysfs_chmod_file(&client->dev.kobj,
799 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
800 S_IRUGO | S_IWUSR);
801 if (err)
802 goto exit_remove;
803 }
804
Riku Voipio620c1422007-10-16 12:46:49 +0300805 data->hwmon_dev = hwmon_device_register(&client->dev);
806 if (IS_ERR(data->hwmon_dev)) {
807 err = PTR_ERR(data->hwmon_dev);
808 goto exit_remove;
809 }
810
Guenter Roeckb1b561a2011-12-08 10:15:51 -0800811 f75375_init(client, data, f75375s_pdata);
Riku Voipioff312d02007-09-26 13:14:40 +0300812
Riku Voipio620c1422007-10-16 12:46:49 +0300813 return 0;
814
815exit_remove:
816 sysfs_remove_group(&client->dev.kobj, &f75375_group);
817exit_free:
Riku Voipio84f1e442007-08-24 13:03:09 +0300818 kfree(data);
Riku Voipio620c1422007-10-16 12:46:49 +0300819 return err;
820}
821
822static int f75375_remove(struct i2c_client *client)
823{
824 struct f75375_data *data = i2c_get_clientdata(client);
825 hwmon_device_unregister(data->hwmon_dev);
826 sysfs_remove_group(&client->dev.kobj, &f75375_group);
827 kfree(data);
Riku Voipio84f1e442007-08-24 13:03:09 +0300828 return 0;
829}
830
Jean Delvare935ada82008-07-16 19:30:11 +0200831/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100832static int f75375_detect(struct i2c_client *client,
Jean Delvare935ada82008-07-16 19:30:11 +0200833 struct i2c_board_info *info)
Riku Voipio84f1e442007-08-24 13:03:09 +0300834{
Jean Delvare935ada82008-07-16 19:30:11 +0200835 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100836 u16 vendid, chipid;
837 u8 version;
838 const char *name;
Riku Voipio84f1e442007-08-24 13:03:09 +0300839
Jean Delvare52df6442009-12-09 20:35:57 +0100840 vendid = f75375_read16(client, F75375_REG_VENDOR);
841 chipid = f75375_read16(client, F75375_CHIP_ID);
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500842 if (vendid != 0x1934)
843 return -ENODEV;
844
845 if (chipid == 0x0306)
Riku Voipio84f1e442007-08-24 13:03:09 +0300846 name = "f75375";
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500847 else if (chipid == 0x0204)
Riku Voipio84f1e442007-08-24 13:03:09 +0300848 name = "f75373";
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500849 else if (chipid == 0x0410)
850 name = "f75387";
Jean Delvare52df6442009-12-09 20:35:57 +0100851 else
852 return -ENODEV;
853
854 version = f75375_read8(client, F75375_REG_VERSION);
Riku Voipio84f1e442007-08-24 13:03:09 +0300855 dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
Jean Delvare935ada82008-07-16 19:30:11 +0200856 strlcpy(info->type, name, I2C_NAME_SIZE);
Riku Voipio84f1e442007-08-24 13:03:09 +0300857
Riku Voipio84f1e442007-08-24 13:03:09 +0300858 return 0;
Riku Voipio84f1e442007-08-24 13:03:09 +0300859}
860
861static int __init sensors_f75375_init(void)
862{
Jean Delvare935ada82008-07-16 19:30:11 +0200863 return i2c_add_driver(&f75375_driver);
Riku Voipio84f1e442007-08-24 13:03:09 +0300864}
865
866static void __exit sensors_f75375_exit(void)
867{
Riku Voipio84f1e442007-08-24 13:03:09 +0300868 i2c_del_driver(&f75375_driver);
869}
870
Riku Voipiob26e0ed2009-03-03 21:37:17 +0200871MODULE_AUTHOR("Riku Voipio");
Riku Voipio84f1e442007-08-24 13:03:09 +0300872MODULE_LICENSE("GPL");
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500873MODULE_DESCRIPTION("F75373/F75375/F75387 hardware monitoring driver");
Riku Voipio84f1e442007-08-24 13:03:09 +0300874
875module_init(sensors_f75375_init);
876module_exit(sensors_f75375_exit);