blob: 35b570e56ff0d29bf2c6ff7f3e35b148dc01d666 [file] [log] [blame]
Riku Voipio84f1e442007-08-24 13:03:09 +03001/*
2 * f75375s.c - driver for the Fintek F75375/SP and F75373
3 * 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 *
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 *
28 */
29
30#include <linux/module.h>
31#include <linux/jiffies.h>
32#include <linux/hwmon.h>
33#include <linux/hwmon-sysfs.h>
34#include <linux/i2c.h>
35#include <linux/err.h>
36#include <linux/mutex.h>
Riku Voipioff312d02007-09-26 13:14:40 +030037#include <linux/f75375s.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Riku Voipio84f1e442007-08-24 13:03:09 +030039
40/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050041static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
Riku Voipio84f1e442007-08-24 13:03:09 +030042
Jean Delvaree5e9f442009-12-14 21:17:27 +010043enum chips { f75373, f75375 };
Riku Voipio84f1e442007-08-24 13:03:09 +030044
45/* Fintek F75375 registers */
46#define F75375_REG_CONFIG0 0x0
47#define F75375_REG_CONFIG1 0x1
48#define F75375_REG_CONFIG2 0x2
49#define F75375_REG_CONFIG3 0x3
50#define F75375_REG_ADDR 0x4
51#define F75375_REG_INTR 0x31
52#define F75375_CHIP_ID 0x5A
53#define F75375_REG_VERSION 0x5C
54#define F75375_REG_VENDOR 0x5D
55#define F75375_REG_FAN_TIMER 0x60
56
57#define F75375_REG_VOLT(nr) (0x10 + (nr))
58#define F75375_REG_VOLT_HIGH(nr) (0x20 + (nr) * 2)
59#define F75375_REG_VOLT_LOW(nr) (0x21 + (nr) * 2)
60
61#define F75375_REG_TEMP(nr) (0x14 + (nr))
62#define F75375_REG_TEMP_HIGH(nr) (0x28 + (nr) * 2)
63#define F75375_REG_TEMP_HYST(nr) (0x29 + (nr) * 2)
64
65#define F75375_REG_FAN(nr) (0x16 + (nr) * 2)
66#define F75375_REG_FAN_MIN(nr) (0x2C + (nr) * 2)
67#define F75375_REG_FAN_FULL(nr) (0x70 + (nr) * 0x10)
68#define F75375_REG_FAN_PWM_DUTY(nr) (0x76 + (nr) * 0x10)
69#define F75375_REG_FAN_PWM_CLOCK(nr) (0x7D + (nr) * 0x10)
70
71#define F75375_REG_FAN_EXP(nr) (0x74 + (nr) * 0x10)
72#define F75375_REG_FAN_B_TEMP(nr, step) ((0xA0 + (nr) * 0x10) + (step))
73#define F75375_REG_FAN_B_SPEED(nr, step) \
74 ((0xA5 + (nr) * 0x10) + (step) * 2)
75
76#define F75375_REG_PWM1_RAISE_DUTY 0x69
77#define F75375_REG_PWM2_RAISE_DUTY 0x6A
78#define F75375_REG_PWM1_DROP_DUTY 0x6B
79#define F75375_REG_PWM2_DROP_DUTY 0x6C
80
81#define FAN_CTRL_LINEAR(nr) (4 + nr)
Guillem Jover96f36402010-09-17 17:24:11 +020082#define FAN_CTRL_MODE(nr) (4 + ((nr) * 2))
Riku Voipio84f1e442007-08-24 13:03:09 +030083
84/*
85 * Data structures and manipulation thereof
86 */
87
88struct f75375_data {
89 unsigned short addr;
Tony Jones1beeffe2007-08-20 13:46:20 -070090 struct device *hwmon_dev;
Riku Voipio84f1e442007-08-24 13:03:09 +030091
92 const char *name;
93 int kind;
94 struct mutex update_lock; /* protect register access */
95 char valid;
96 unsigned long last_updated; /* In jiffies */
97 unsigned long last_limits; /* In jiffies */
98
99 /* Register values */
100 u8 in[4];
101 u8 in_max[4];
102 u8 in_min[4];
103 u16 fan[2];
104 u16 fan_min[2];
Guenter Roeck740f6be32011-12-08 09:59:25 -0800105 u16 fan_max[2];
106 u16 fan_target[2];
Riku Voipio84f1e442007-08-24 13:03:09 +0300107 u8 fan_timer;
108 u8 pwm[2];
109 u8 pwm_mode[2];
110 u8 pwm_enable[2];
111 s8 temp[2];
112 s8 temp_high[2];
113 s8 temp_max_hyst[2];
114};
115
Jean Delvare310ec792009-12-14 21:17:23 +0100116static int f75375_detect(struct i2c_client *client,
Jean Delvare935ada82008-07-16 19:30:11 +0200117 struct i2c_board_info *info);
Jean Delvared2653e92008-04-29 23:11:39 +0200118static int f75375_probe(struct i2c_client *client,
119 const struct i2c_device_id *id);
Riku Voipio620c1422007-10-16 12:46:49 +0300120static int f75375_remove(struct i2c_client *client);
121
Jean Delvare3760f732008-04-29 23:11:40 +0200122static const struct i2c_device_id f75375_id[] = {
123 { "f75373", f75373 },
124 { "f75375", f75375 },
125 { }
126};
127MODULE_DEVICE_TABLE(i2c, f75375_id);
128
Riku Voipio84f1e442007-08-24 13:03:09 +0300129static struct i2c_driver f75375_driver = {
Jean Delvare935ada82008-07-16 19:30:11 +0200130 .class = I2C_CLASS_HWMON,
Riku Voipio84f1e442007-08-24 13:03:09 +0300131 .driver = {
132 .name = "f75375",
133 },
Riku Voipio620c1422007-10-16 12:46:49 +0300134 .probe = f75375_probe,
135 .remove = f75375_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200136 .id_table = f75375_id,
Jean Delvare935ada82008-07-16 19:30:11 +0200137 .detect = f75375_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100138 .address_list = normal_i2c,
Riku Voipio84f1e442007-08-24 13:03:09 +0300139};
140
141static inline int f75375_read8(struct i2c_client *client, u8 reg)
142{
143 return i2c_smbus_read_byte_data(client, reg);
144}
145
146/* in most cases, should be called while holding update_lock */
147static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
148{
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800149 return (i2c_smbus_read_byte_data(client, reg) << 8)
150 | i2c_smbus_read_byte_data(client, reg + 1);
Riku Voipio84f1e442007-08-24 13:03:09 +0300151}
152
153static inline void f75375_write8(struct i2c_client *client, u8 reg,
154 u8 value)
155{
156 i2c_smbus_write_byte_data(client, reg, value);
157}
158
159static inline void f75375_write16(struct i2c_client *client, u8 reg,
160 u16 value)
161{
162 int err = i2c_smbus_write_byte_data(client, reg, (value << 8));
163 if (err)
164 return;
165 i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
166}
167
168static struct f75375_data *f75375_update_device(struct device *dev)
169{
170 struct i2c_client *client = to_i2c_client(dev);
171 struct f75375_data *data = i2c_get_clientdata(client);
172 int nr;
173
174 mutex_lock(&data->update_lock);
175
176 /* Limit registers cache is refreshed after 60 seconds */
177 if (time_after(jiffies, data->last_limits + 60 * HZ)
178 || !data->valid) {
179 for (nr = 0; nr < 2; nr++) {
180 data->temp_high[nr] =
181 f75375_read8(client, F75375_REG_TEMP_HIGH(nr));
182 data->temp_max_hyst[nr] =
183 f75375_read8(client, F75375_REG_TEMP_HYST(nr));
Guenter Roeck740f6be32011-12-08 09:59:25 -0800184 data->fan_max[nr] =
Riku Voipio84f1e442007-08-24 13:03:09 +0300185 f75375_read16(client, F75375_REG_FAN_FULL(nr));
186 data->fan_min[nr] =
187 f75375_read16(client, F75375_REG_FAN_MIN(nr));
Guenter Roeck740f6be32011-12-08 09:59:25 -0800188 data->fan_target[nr] =
Riku Voipio84f1e442007-08-24 13:03:09 +0300189 f75375_read16(client, F75375_REG_FAN_EXP(nr));
190 data->pwm[nr] = f75375_read8(client,
191 F75375_REG_FAN_PWM_DUTY(nr));
192
193 }
194 for (nr = 0; nr < 4; nr++) {
195 data->in_max[nr] =
196 f75375_read8(client, F75375_REG_VOLT_HIGH(nr));
197 data->in_min[nr] =
198 f75375_read8(client, F75375_REG_VOLT_LOW(nr));
199 }
200 data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
201 data->last_limits = jiffies;
202 }
203
204 /* Measurement registers cache is refreshed after 2 second */
205 if (time_after(jiffies, data->last_updated + 2 * HZ)
206 || !data->valid) {
207 for (nr = 0; nr < 2; nr++) {
208 data->temp[nr] =
209 f75375_read8(client, F75375_REG_TEMP(nr));
210 data->fan[nr] =
211 f75375_read16(client, F75375_REG_FAN(nr));
212 }
213 for (nr = 0; nr < 4; nr++)
214 data->in[nr] =
215 f75375_read8(client, F75375_REG_VOLT(nr));
216
217 data->last_updated = jiffies;
218 data->valid = 1;
219 }
220
221 mutex_unlock(&data->update_lock);
222 return data;
223}
224
225static inline u16 rpm_from_reg(u16 reg)
226{
227 if (reg == 0 || reg == 0xffff)
228 return 0;
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800229 return 1500000 / reg;
Riku Voipio84f1e442007-08-24 13:03:09 +0300230}
231
232static inline u16 rpm_to_reg(int rpm)
233{
234 if (rpm < 367 || rpm > 0xffff)
235 return 0xffff;
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800236 return 1500000 / rpm;
Riku Voipio84f1e442007-08-24 13:03:09 +0300237}
238
239static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
240 const char *buf, size_t count)
241{
242 int nr = to_sensor_dev_attr(attr)->index;
243 struct i2c_client *client = to_i2c_client(dev);
244 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800245 unsigned long val;
246 int err;
247
248 err = kstrtoul(buf, 10, &val);
249 if (err < 0)
250 return err;
Riku Voipio84f1e442007-08-24 13:03:09 +0300251
252 mutex_lock(&data->update_lock);
253 data->fan_min[nr] = rpm_to_reg(val);
254 f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
255 mutex_unlock(&data->update_lock);
256 return count;
257}
258
Guenter Roeck740f6be32011-12-08 09:59:25 -0800259static ssize_t set_fan_target(struct device *dev, struct device_attribute *attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300260 const char *buf, size_t count)
261{
262 int nr = to_sensor_dev_attr(attr)->index;
263 struct i2c_client *client = to_i2c_client(dev);
264 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800265 unsigned long val;
266 int err;
267
268 err = kstrtoul(buf, 10, &val);
269 if (err < 0)
270 return err;
Riku Voipio84f1e442007-08-24 13:03:09 +0300271
272 mutex_lock(&data->update_lock);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800273 data->fan_target[nr] = rpm_to_reg(val);
274 f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_target[nr]);
Riku Voipio84f1e442007-08-24 13:03:09 +0300275 mutex_unlock(&data->update_lock);
276 return count;
277}
278
279static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
280 const char *buf, size_t count)
281{
282 int nr = to_sensor_dev_attr(attr)->index;
283 struct i2c_client *client = to_i2c_client(dev);
284 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800285 unsigned long val;
286 int err;
287
288 err = kstrtoul(buf, 10, &val);
289 if (err < 0)
290 return err;
Riku Voipio84f1e442007-08-24 13:03:09 +0300291
292 mutex_lock(&data->update_lock);
293 data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
294 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr), data->pwm[nr]);
295 mutex_unlock(&data->update_lock);
296 return count;
297}
298
299static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
300 *attr, char *buf)
301{
302 int nr = to_sensor_dev_attr(attr)->index;
303 struct f75375_data *data = f75375_update_device(dev);
304 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
305}
306
Riku Voipioff312d02007-09-26 13:14:40 +0300307static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
Riku Voipio84f1e442007-08-24 13:03:09 +0300308{
Riku Voipio84f1e442007-08-24 13:03:09 +0300309 struct f75375_data *data = i2c_get_clientdata(client);
Riku Voipio84f1e442007-08-24 13:03:09 +0300310 u8 fanmode;
311
Guenter Roeck33106002011-12-08 10:01:25 -0800312 if (val < 0 || val > 3)
Riku Voipio84f1e442007-08-24 13:03:09 +0300313 return -EINVAL;
314
Riku Voipio84f1e442007-08-24 13:03:09 +0300315 fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
Guillem Joverc3b327d2010-09-17 17:24:12 +0200316 fanmode &= ~(3 << FAN_CTRL_MODE(nr));
Riku Voipio84f1e442007-08-24 13:03:09 +0300317
318 switch (val) {
319 case 0: /* Full speed */
320 fanmode |= (3 << FAN_CTRL_MODE(nr));
321 data->pwm[nr] = 255;
322 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
323 data->pwm[nr]);
324 break;
325 case 1: /* PWM */
326 fanmode |= (3 << FAN_CTRL_MODE(nr));
327 break;
328 case 2: /* AUTOMATIC*/
329 fanmode |= (2 << FAN_CTRL_MODE(nr));
330 break;
331 case 3: /* fan speed */
332 break;
333 }
334 f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
335 data->pwm_enable[nr] = val;
Riku Voipioff312d02007-09-26 13:14:40 +0300336 return 0;
337}
338
339static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
340 const char *buf, size_t count)
341{
342 int nr = to_sensor_dev_attr(attr)->index;
343 struct i2c_client *client = to_i2c_client(dev);
344 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800345 unsigned long val;
346 int err;
347
348 err = kstrtoul(buf, 10, &val);
349 if (err < 0)
350 return err;
Riku Voipioff312d02007-09-26 13:14:40 +0300351
352 mutex_lock(&data->update_lock);
353 err = set_pwm_enable_direct(client, nr, val);
Riku Voipio84f1e442007-08-24 13:03:09 +0300354 mutex_unlock(&data->update_lock);
Riku Voipioff312d02007-09-26 13:14:40 +0300355 return err ? err : count;
Riku Voipio84f1e442007-08-24 13:03:09 +0300356}
357
358static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
359 const char *buf, size_t count)
360{
361 int nr = to_sensor_dev_attr(attr)->index;
362 struct i2c_client *client = to_i2c_client(dev);
363 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800364 unsigned long val;
365 int err;
366 u8 conf;
367
368 err = kstrtoul(buf, 10, &val);
369 if (err < 0)
370 return err;
Riku Voipio84f1e442007-08-24 13:03:09 +0300371
Riku Voipio76e83be2007-10-26 14:14:23 +0300372 if (!(val == 0 || val == 1))
Riku Voipio84f1e442007-08-24 13:03:09 +0300373 return -EINVAL;
374
Guenter Roeck5cd32222011-12-08 10:55:16 -0800375 /* F75373 does not support DC (linear voltage) fan control mode */
376 if (data->kind == f75373 && val == 0)
377 return -EINVAL;
378
Riku Voipio84f1e442007-08-24 13:03:09 +0300379 mutex_lock(&data->update_lock);
380 conf = f75375_read8(client, F75375_REG_CONFIG1);
Guillem Joverc3b327d2010-09-17 17:24:12 +0200381 conf &= ~(1 << FAN_CTRL_LINEAR(nr));
Riku Voipio84f1e442007-08-24 13:03:09 +0300382
383 if (val == 0)
384 conf |= (1 << FAN_CTRL_LINEAR(nr)) ;
385
386 f75375_write8(client, F75375_REG_CONFIG1, conf);
387 data->pwm_mode[nr] = val;
388 mutex_unlock(&data->update_lock);
389 return count;
390}
391
392static ssize_t show_pwm(struct device *dev, struct device_attribute
393 *attr, char *buf)
394{
395 int nr = to_sensor_dev_attr(attr)->index;
396 struct f75375_data *data = f75375_update_device(dev);
397 return sprintf(buf, "%d\n", data->pwm[nr]);
398}
399
400static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
401 *attr, char *buf)
402{
403 int nr = to_sensor_dev_attr(attr)->index;
404 struct f75375_data *data = f75375_update_device(dev);
405 return sprintf(buf, "%d\n", data->pwm_mode[nr]);
406}
407
408#define VOLT_FROM_REG(val) ((val) * 8)
409#define VOLT_TO_REG(val) ((val) / 8)
410
411static ssize_t show_in(struct device *dev, struct device_attribute *attr,
412 char *buf)
413{
414 int nr = to_sensor_dev_attr(attr)->index;
415 struct f75375_data *data = f75375_update_device(dev);
416 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
417}
418
419static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
420 char *buf)
421{
422 int nr = to_sensor_dev_attr(attr)->index;
423 struct f75375_data *data = f75375_update_device(dev);
424 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
425}
426
427static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
428 char *buf)
429{
430 int nr = to_sensor_dev_attr(attr)->index;
431 struct f75375_data *data = f75375_update_device(dev);
432 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
433}
434
435static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
436 const char *buf, size_t count)
437{
438 int nr = to_sensor_dev_attr(attr)->index;
439 struct i2c_client *client = to_i2c_client(dev);
440 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800441 unsigned long val;
442 int err;
443
444 err = kstrtoul(buf, 10, &val);
445 if (err < 0)
446 return err;
447
Riku Voipio84f1e442007-08-24 13:03:09 +0300448 val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
449 mutex_lock(&data->update_lock);
450 data->in_max[nr] = val;
451 f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
452 mutex_unlock(&data->update_lock);
453 return count;
454}
455
456static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
457 const char *buf, size_t count)
458{
459 int nr = to_sensor_dev_attr(attr)->index;
460 struct i2c_client *client = to_i2c_client(dev);
461 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800462 unsigned long val;
463 int err;
464
465 err = kstrtoul(buf, 10, &val);
466 if (err < 0)
467 return err;
468
Riku Voipio84f1e442007-08-24 13:03:09 +0300469 val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
470 mutex_lock(&data->update_lock);
471 data->in_min[nr] = val;
472 f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
473 mutex_unlock(&data->update_lock);
474 return count;
475}
476#define TEMP_FROM_REG(val) ((val) * 1000)
477#define TEMP_TO_REG(val) ((val) / 1000)
478
479static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
480 char *buf)
481{
482 int nr = to_sensor_dev_attr(attr)->index;
483 struct f75375_data *data = f75375_update_device(dev);
484 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
485}
486
487static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
488 char *buf)
489{
490 int nr = to_sensor_dev_attr(attr)->index;
491 struct f75375_data *data = f75375_update_device(dev);
492 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
493}
494
495static ssize_t show_temp_max_hyst(struct device *dev,
496 struct device_attribute *attr, char *buf)
497{
498 int nr = to_sensor_dev_attr(attr)->index;
499 struct f75375_data *data = f75375_update_device(dev);
500 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
501}
502
503static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
504 const char *buf, size_t count)
505{
506 int nr = to_sensor_dev_attr(attr)->index;
507 struct i2c_client *client = to_i2c_client(dev);
508 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800509 unsigned long val;
510 int err;
511
512 err = kstrtoul(buf, 10, &val);
513 if (err < 0)
514 return err;
515
Riku Voipio84f1e442007-08-24 13:03:09 +0300516 val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
517 mutex_lock(&data->update_lock);
518 data->temp_high[nr] = val;
519 f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
520 mutex_unlock(&data->update_lock);
521 return count;
522}
523
524static ssize_t set_temp_max_hyst(struct device *dev,
525 struct device_attribute *attr, const char *buf, size_t count)
526{
527 int nr = to_sensor_dev_attr(attr)->index;
528 struct i2c_client *client = to_i2c_client(dev);
529 struct f75375_data *data = i2c_get_clientdata(client);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800530 unsigned long val;
531 int err;
532
533 err = kstrtoul(buf, 10, &val);
534 if (err < 0)
535 return err;
536
Riku Voipio84f1e442007-08-24 13:03:09 +0300537 val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
538 mutex_lock(&data->update_lock);
539 data->temp_max_hyst[nr] = val;
540 f75375_write8(client, F75375_REG_TEMP_HYST(nr),
541 data->temp_max_hyst[nr]);
542 mutex_unlock(&data->update_lock);
543 return count;
544}
545
546#define show_fan(thing) \
547static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
548 char *buf)\
549{\
550 int nr = to_sensor_dev_attr(attr)->index;\
551 struct f75375_data *data = f75375_update_device(dev); \
552 return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
553}
554
555show_fan(fan);
556show_fan(fan_min);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800557show_fan(fan_max);
558show_fan(fan_target);
Riku Voipio84f1e442007-08-24 13:03:09 +0300559
560static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
561static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
562 show_in_max, set_in_max, 0);
563static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
564 show_in_min, set_in_min, 0);
565static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
566static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
567 show_in_max, set_in_max, 1);
568static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
569 show_in_min, set_in_min, 1);
570static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
571static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
572 show_in_max, set_in_max, 2);
573static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
574 show_in_min, set_in_min, 2);
575static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
576static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
577 show_in_max, set_in_max, 3);
578static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
579 show_in_min, set_in_min, 3);
580static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
581static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
582 show_temp_max_hyst, set_temp_max_hyst, 0);
583static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
584 show_temp_max, set_temp_max, 0);
585static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
586static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
587 show_temp_max_hyst, set_temp_max_hyst, 1);
588static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
589 show_temp_max, set_temp_max, 1);
590static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800591static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, show_fan_max, NULL, 0);
Riku Voipio84f1e442007-08-24 13:03:09 +0300592static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
593 show_fan_min, set_fan_min, 0);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800594static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO|S_IWUSR,
595 show_fan_target, set_fan_target, 0);
Riku Voipio84f1e442007-08-24 13:03:09 +0300596static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800597static SENSOR_DEVICE_ATTR(fan2_max, S_IRUGO, show_fan_max, NULL, 1);
Riku Voipio84f1e442007-08-24 13:03:09 +0300598static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
599 show_fan_min, set_fan_min, 1);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800600static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO|S_IWUSR,
601 show_fan_target, set_fan_target, 1);
Riku Voipio84f1e442007-08-24 13:03:09 +0300602static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
603 show_pwm, set_pwm, 0);
604static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
605 show_pwm_enable, set_pwm_enable, 0);
Riku Voipio76e83be2007-10-26 14:14:23 +0300606static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
Riku Voipio84f1e442007-08-24 13:03:09 +0300607 show_pwm_mode, set_pwm_mode, 0);
608static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
609 show_pwm, set_pwm, 1);
610static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
611 show_pwm_enable, set_pwm_enable, 1);
Riku Voipio76e83be2007-10-26 14:14:23 +0300612static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
Riku Voipio84f1e442007-08-24 13:03:09 +0300613 show_pwm_mode, set_pwm_mode, 1);
614
615static struct attribute *f75375_attributes[] = {
616 &sensor_dev_attr_temp1_input.dev_attr.attr,
617 &sensor_dev_attr_temp1_max.dev_attr.attr,
618 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
619 &sensor_dev_attr_temp2_input.dev_attr.attr,
620 &sensor_dev_attr_temp2_max.dev_attr.attr,
621 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
622 &sensor_dev_attr_fan1_input.dev_attr.attr,
Guenter Roeck740f6be32011-12-08 09:59:25 -0800623 &sensor_dev_attr_fan1_max.dev_attr.attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300624 &sensor_dev_attr_fan1_min.dev_attr.attr,
Guenter Roeck740f6be32011-12-08 09:59:25 -0800625 &sensor_dev_attr_fan1_target.dev_attr.attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300626 &sensor_dev_attr_fan2_input.dev_attr.attr,
Guenter Roeck740f6be32011-12-08 09:59:25 -0800627 &sensor_dev_attr_fan2_max.dev_attr.attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300628 &sensor_dev_attr_fan2_min.dev_attr.attr,
Guenter Roeck740f6be32011-12-08 09:59:25 -0800629 &sensor_dev_attr_fan2_target.dev_attr.attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300630 &sensor_dev_attr_pwm1.dev_attr.attr,
631 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
632 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
633 &sensor_dev_attr_pwm2.dev_attr.attr,
634 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
635 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
636 &sensor_dev_attr_in0_input.dev_attr.attr,
637 &sensor_dev_attr_in0_max.dev_attr.attr,
638 &sensor_dev_attr_in0_min.dev_attr.attr,
639 &sensor_dev_attr_in1_input.dev_attr.attr,
640 &sensor_dev_attr_in1_max.dev_attr.attr,
641 &sensor_dev_attr_in1_min.dev_attr.attr,
642 &sensor_dev_attr_in2_input.dev_attr.attr,
643 &sensor_dev_attr_in2_max.dev_attr.attr,
644 &sensor_dev_attr_in2_min.dev_attr.attr,
645 &sensor_dev_attr_in3_input.dev_attr.attr,
646 &sensor_dev_attr_in3_max.dev_attr.attr,
647 &sensor_dev_attr_in3_min.dev_attr.attr,
648 NULL
649};
650
651static const struct attribute_group f75375_group = {
652 .attrs = f75375_attributes,
653};
654
Riku Voipioff312d02007-09-26 13:14:40 +0300655static void f75375_init(struct i2c_client *client, struct f75375_data *data,
656 struct f75375s_platform_data *f75375s_pdata)
657{
658 int nr;
Guenter Roeckb1b561a2011-12-08 10:15:51 -0800659
660 if (!f75375s_pdata) {
661 u8 conf, mode;
662 int nr;
663
664 conf = f75375_read8(client, F75375_REG_CONFIG1);
665 mode = f75375_read8(client, F75375_REG_FAN_TIMER);
666 for (nr = 0; nr < 2; nr++) {
667 if (!(conf & (1 << FAN_CTRL_LINEAR(nr))))
668 data->pwm_mode[nr] = 1;
669 switch ((mode >> FAN_CTRL_MODE(nr)) & 3) {
670 case 0: /* speed */
671 data->pwm_enable[nr] = 3;
672 break;
673 case 1: /* automatic */
674 data->pwm_enable[nr] = 2;
675 break;
676 default: /* manual */
677 data->pwm_enable[nr] = 1;
678 break;
679 }
680 }
681 return;
682 }
683
Riku Voipioff312d02007-09-26 13:14:40 +0300684 set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
685 set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
686 for (nr = 0; nr < 2; nr++) {
687 data->pwm[nr] = SENSORS_LIMIT(f75375s_pdata->pwm[nr], 0, 255);
688 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
689 data->pwm[nr]);
690 }
691
692}
693
Jean Delvared2653e92008-04-29 23:11:39 +0200694static int f75375_probe(struct i2c_client *client,
695 const struct i2c_device_id *id)
Riku Voipio620c1422007-10-16 12:46:49 +0300696{
Andrew Klossner51b3e272009-03-12 13:36:39 +0100697 struct f75375_data *data;
Riku Voipioff312d02007-09-26 13:14:40 +0300698 struct f75375s_platform_data *f75375s_pdata = client->dev.platform_data;
Riku Voipio620c1422007-10-16 12:46:49 +0300699 int err;
700
701 if (!i2c_check_functionality(client->adapter,
702 I2C_FUNC_SMBUS_BYTE_DATA))
703 return -EIO;
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800704 data = kzalloc(sizeof(struct f75375_data), GFP_KERNEL);
705 if (!data)
Riku Voipio620c1422007-10-16 12:46:49 +0300706 return -ENOMEM;
707
708 i2c_set_clientdata(client, data);
Riku Voipio620c1422007-10-16 12:46:49 +0300709 mutex_init(&data->update_lock);
Jean Delvare3760f732008-04-29 23:11:40 +0200710 data->kind = id->driver_data;
Riku Voipio620c1422007-10-16 12:46:49 +0300711
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800712 err = sysfs_create_group(&client->dev.kobj, &f75375_group);
713 if (err)
Riku Voipio620c1422007-10-16 12:46:49 +0300714 goto exit_free;
715
Riku Voipio76e83be2007-10-26 14:14:23 +0300716 if (data->kind == f75375) {
717 err = sysfs_chmod_file(&client->dev.kobj,
718 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
719 S_IRUGO | S_IWUSR);
720 if (err)
721 goto exit_remove;
722 err = sysfs_chmod_file(&client->dev.kobj,
723 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
724 S_IRUGO | S_IWUSR);
725 if (err)
726 goto exit_remove;
727 }
728
Riku Voipio620c1422007-10-16 12:46:49 +0300729 data->hwmon_dev = hwmon_device_register(&client->dev);
730 if (IS_ERR(data->hwmon_dev)) {
731 err = PTR_ERR(data->hwmon_dev);
732 goto exit_remove;
733 }
734
Guenter Roeckb1b561a2011-12-08 10:15:51 -0800735 f75375_init(client, data, f75375s_pdata);
Riku Voipioff312d02007-09-26 13:14:40 +0300736
Riku Voipio620c1422007-10-16 12:46:49 +0300737 return 0;
738
739exit_remove:
740 sysfs_remove_group(&client->dev.kobj, &f75375_group);
741exit_free:
Riku Voipio84f1e442007-08-24 13:03:09 +0300742 kfree(data);
Riku Voipio620c1422007-10-16 12:46:49 +0300743 return err;
744}
745
746static int f75375_remove(struct i2c_client *client)
747{
748 struct f75375_data *data = i2c_get_clientdata(client);
749 hwmon_device_unregister(data->hwmon_dev);
750 sysfs_remove_group(&client->dev.kobj, &f75375_group);
751 kfree(data);
Riku Voipio84f1e442007-08-24 13:03:09 +0300752 return 0;
753}
754
Jean Delvare935ada82008-07-16 19:30:11 +0200755/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100756static int f75375_detect(struct i2c_client *client,
Jean Delvare935ada82008-07-16 19:30:11 +0200757 struct i2c_board_info *info)
Riku Voipio84f1e442007-08-24 13:03:09 +0300758{
Jean Delvare935ada82008-07-16 19:30:11 +0200759 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100760 u16 vendid, chipid;
761 u8 version;
762 const char *name;
Riku Voipio84f1e442007-08-24 13:03:09 +0300763
Jean Delvare52df6442009-12-09 20:35:57 +0100764 vendid = f75375_read16(client, F75375_REG_VENDOR);
765 chipid = f75375_read16(client, F75375_CHIP_ID);
766 if (chipid == 0x0306 && vendid == 0x1934)
Riku Voipio84f1e442007-08-24 13:03:09 +0300767 name = "f75375";
Jean Delvare52df6442009-12-09 20:35:57 +0100768 else if (chipid == 0x0204 && vendid == 0x1934)
Riku Voipio84f1e442007-08-24 13:03:09 +0300769 name = "f75373";
Jean Delvare52df6442009-12-09 20:35:57 +0100770 else
771 return -ENODEV;
772
773 version = f75375_read8(client, F75375_REG_VERSION);
Riku Voipio84f1e442007-08-24 13:03:09 +0300774 dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
Jean Delvare935ada82008-07-16 19:30:11 +0200775 strlcpy(info->type, name, I2C_NAME_SIZE);
Riku Voipio84f1e442007-08-24 13:03:09 +0300776
Riku Voipio84f1e442007-08-24 13:03:09 +0300777 return 0;
Riku Voipio84f1e442007-08-24 13:03:09 +0300778}
779
780static int __init sensors_f75375_init(void)
781{
Jean Delvare935ada82008-07-16 19:30:11 +0200782 return i2c_add_driver(&f75375_driver);
Riku Voipio84f1e442007-08-24 13:03:09 +0300783}
784
785static void __exit sensors_f75375_exit(void)
786{
Riku Voipio84f1e442007-08-24 13:03:09 +0300787 i2c_del_driver(&f75375_driver);
788}
789
Riku Voipiob26e0ed2009-03-03 21:37:17 +0200790MODULE_AUTHOR("Riku Voipio");
Riku Voipio84f1e442007-08-24 13:03:09 +0300791MODULE_LICENSE("GPL");
792MODULE_DESCRIPTION("F75373/F75375 hardware monitoring driver");
793
794module_init(sensors_f75375_init);
795module_exit(sensors_f75375_exit);