blob: bad2cf3ef4a448aa76a72033b527647c6434f9d3 [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:
9 * http://www.fintek.com.tw/files/productfiles/2005111152950.pdf
10 *
11 * f75373:
12 * http://www.fintek.com.tw/files/productfiles/2005111153128.pdf
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 *
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)
82#define FAN_CTRL_MODE(nr) (5 + ((nr) * 2))
83
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];
105 u16 fan_full[2];
106 u16 fan_exp[2];
107 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{
149 return ((i2c_smbus_read_byte_data(client, reg) << 8)
150 | i2c_smbus_read_byte_data(client, reg + 1));
151}
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));
184 data->fan_full[nr] =
185 f75375_read16(client, F75375_REG_FAN_FULL(nr));
186 data->fan_min[nr] =
187 f75375_read16(client, F75375_REG_FAN_MIN(nr));
188 data->fan_exp[nr] =
189 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;
229 return (1500000 / reg);
230}
231
232static inline u16 rpm_to_reg(int rpm)
233{
234 if (rpm < 367 || rpm > 0xffff)
235 return 0xffff;
236 return (1500000 / rpm);
237}
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);
245 int val = simple_strtoul(buf, NULL, 10);
246
247 mutex_lock(&data->update_lock);
248 data->fan_min[nr] = rpm_to_reg(val);
249 f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
250 mutex_unlock(&data->update_lock);
251 return count;
252}
253
254static ssize_t set_fan_exp(struct device *dev, struct device_attribute *attr,
255 const char *buf, size_t count)
256{
257 int nr = to_sensor_dev_attr(attr)->index;
258 struct i2c_client *client = to_i2c_client(dev);
259 struct f75375_data *data = i2c_get_clientdata(client);
260 int val = simple_strtoul(buf, NULL, 10);
261
262 mutex_lock(&data->update_lock);
263 data->fan_exp[nr] = rpm_to_reg(val);
264 f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_exp[nr]);
265 mutex_unlock(&data->update_lock);
266 return count;
267}
268
269static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
270 const char *buf, size_t count)
271{
272 int nr = to_sensor_dev_attr(attr)->index;
273 struct i2c_client *client = to_i2c_client(dev);
274 struct f75375_data *data = i2c_get_clientdata(client);
275 int val = simple_strtoul(buf, NULL, 10);
276
277 mutex_lock(&data->update_lock);
278 data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
279 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr), data->pwm[nr]);
280 mutex_unlock(&data->update_lock);
281 return count;
282}
283
284static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
285 *attr, char *buf)
286{
287 int nr = to_sensor_dev_attr(attr)->index;
288 struct f75375_data *data = f75375_update_device(dev);
289 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
290}
291
Riku Voipioff312d02007-09-26 13:14:40 +0300292static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
Riku Voipio84f1e442007-08-24 13:03:09 +0300293{
Riku Voipio84f1e442007-08-24 13:03:09 +0300294 struct f75375_data *data = i2c_get_clientdata(client);
Riku Voipio84f1e442007-08-24 13:03:09 +0300295 u8 fanmode;
296
297 if (val < 0 || val > 4)
298 return -EINVAL;
299
Riku Voipio84f1e442007-08-24 13:03:09 +0300300 fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
301 fanmode = ~(3 << FAN_CTRL_MODE(nr));
302
303 switch (val) {
304 case 0: /* Full speed */
305 fanmode |= (3 << FAN_CTRL_MODE(nr));
306 data->pwm[nr] = 255;
307 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
308 data->pwm[nr]);
309 break;
310 case 1: /* PWM */
311 fanmode |= (3 << FAN_CTRL_MODE(nr));
312 break;
313 case 2: /* AUTOMATIC*/
314 fanmode |= (2 << FAN_CTRL_MODE(nr));
315 break;
316 case 3: /* fan speed */
317 break;
318 }
319 f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
320 data->pwm_enable[nr] = val;
Riku Voipioff312d02007-09-26 13:14:40 +0300321 return 0;
322}
323
324static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
325 const char *buf, size_t count)
326{
327 int nr = to_sensor_dev_attr(attr)->index;
328 struct i2c_client *client = to_i2c_client(dev);
329 struct f75375_data *data = i2c_get_clientdata(client);
330 int val = simple_strtoul(buf, NULL, 10);
331 int err = 0;
332
333 mutex_lock(&data->update_lock);
334 err = set_pwm_enable_direct(client, nr, val);
Riku Voipio84f1e442007-08-24 13:03:09 +0300335 mutex_unlock(&data->update_lock);
Riku Voipioff312d02007-09-26 13:14:40 +0300336 return err ? err : count;
Riku Voipio84f1e442007-08-24 13:03:09 +0300337}
338
339static ssize_t set_pwm_mode(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);
345 int val = simple_strtoul(buf, NULL, 10);
346 u8 conf = 0;
347
Riku Voipio76e83be2007-10-26 14:14:23 +0300348 if (!(val == 0 || val == 1))
Riku Voipio84f1e442007-08-24 13:03:09 +0300349 return -EINVAL;
350
351 mutex_lock(&data->update_lock);
352 conf = f75375_read8(client, F75375_REG_CONFIG1);
353 conf = ~(1 << FAN_CTRL_LINEAR(nr));
354
355 if (val == 0)
356 conf |= (1 << FAN_CTRL_LINEAR(nr)) ;
357
358 f75375_write8(client, F75375_REG_CONFIG1, conf);
359 data->pwm_mode[nr] = val;
360 mutex_unlock(&data->update_lock);
361 return count;
362}
363
364static ssize_t show_pwm(struct device *dev, struct device_attribute
365 *attr, char *buf)
366{
367 int nr = to_sensor_dev_attr(attr)->index;
368 struct f75375_data *data = f75375_update_device(dev);
369 return sprintf(buf, "%d\n", data->pwm[nr]);
370}
371
372static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
373 *attr, char *buf)
374{
375 int nr = to_sensor_dev_attr(attr)->index;
376 struct f75375_data *data = f75375_update_device(dev);
377 return sprintf(buf, "%d\n", data->pwm_mode[nr]);
378}
379
380#define VOLT_FROM_REG(val) ((val) * 8)
381#define VOLT_TO_REG(val) ((val) / 8)
382
383static ssize_t show_in(struct device *dev, struct device_attribute *attr,
384 char *buf)
385{
386 int nr = to_sensor_dev_attr(attr)->index;
387 struct f75375_data *data = f75375_update_device(dev);
388 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
389}
390
391static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
392 char *buf)
393{
394 int nr = to_sensor_dev_attr(attr)->index;
395 struct f75375_data *data = f75375_update_device(dev);
396 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
397}
398
399static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
400 char *buf)
401{
402 int nr = to_sensor_dev_attr(attr)->index;
403 struct f75375_data *data = f75375_update_device(dev);
404 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
405}
406
407static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
408 const char *buf, size_t count)
409{
410 int nr = to_sensor_dev_attr(attr)->index;
411 struct i2c_client *client = to_i2c_client(dev);
412 struct f75375_data *data = i2c_get_clientdata(client);
413 int val = simple_strtoul(buf, NULL, 10);
414 val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
415 mutex_lock(&data->update_lock);
416 data->in_max[nr] = val;
417 f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
418 mutex_unlock(&data->update_lock);
419 return count;
420}
421
422static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
423 const char *buf, size_t count)
424{
425 int nr = to_sensor_dev_attr(attr)->index;
426 struct i2c_client *client = to_i2c_client(dev);
427 struct f75375_data *data = i2c_get_clientdata(client);
428 int val = simple_strtoul(buf, NULL, 10);
429 val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
430 mutex_lock(&data->update_lock);
431 data->in_min[nr] = val;
432 f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
433 mutex_unlock(&data->update_lock);
434 return count;
435}
436#define TEMP_FROM_REG(val) ((val) * 1000)
437#define TEMP_TO_REG(val) ((val) / 1000)
438
439static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
440 char *buf)
441{
442 int nr = to_sensor_dev_attr(attr)->index;
443 struct f75375_data *data = f75375_update_device(dev);
444 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
445}
446
447static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
448 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", TEMP_FROM_REG(data->temp_high[nr]));
453}
454
455static ssize_t show_temp_max_hyst(struct device *dev,
456 struct device_attribute *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", TEMP_FROM_REG(data->temp_max_hyst[nr]));
461}
462
463static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
464 const char *buf, size_t count)
465{
466 int nr = to_sensor_dev_attr(attr)->index;
467 struct i2c_client *client = to_i2c_client(dev);
468 struct f75375_data *data = i2c_get_clientdata(client);
469 int val = simple_strtol(buf, NULL, 10);
470 val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
471 mutex_lock(&data->update_lock);
472 data->temp_high[nr] = val;
473 f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
474 mutex_unlock(&data->update_lock);
475 return count;
476}
477
478static ssize_t set_temp_max_hyst(struct device *dev,
479 struct device_attribute *attr, const char *buf, size_t count)
480{
481 int nr = to_sensor_dev_attr(attr)->index;
482 struct i2c_client *client = to_i2c_client(dev);
483 struct f75375_data *data = i2c_get_clientdata(client);
484 int val = simple_strtol(buf, NULL, 10);
485 val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
486 mutex_lock(&data->update_lock);
487 data->temp_max_hyst[nr] = val;
488 f75375_write8(client, F75375_REG_TEMP_HYST(nr),
489 data->temp_max_hyst[nr]);
490 mutex_unlock(&data->update_lock);
491 return count;
492}
493
494#define show_fan(thing) \
495static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
496 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", rpm_from_reg(data->thing[nr])); \
501}
502
503show_fan(fan);
504show_fan(fan_min);
505show_fan(fan_full);
506show_fan(fan_exp);
507
508static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
509static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
510 show_in_max, set_in_max, 0);
511static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
512 show_in_min, set_in_min, 0);
513static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
514static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
515 show_in_max, set_in_max, 1);
516static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
517 show_in_min, set_in_min, 1);
518static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
519static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
520 show_in_max, set_in_max, 2);
521static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
522 show_in_min, set_in_min, 2);
523static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
524static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
525 show_in_max, set_in_max, 3);
526static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
527 show_in_min, set_in_min, 3);
528static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
529static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
530 show_temp_max_hyst, set_temp_max_hyst, 0);
531static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
532 show_temp_max, set_temp_max, 0);
533static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
534static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
535 show_temp_max_hyst, set_temp_max_hyst, 1);
536static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
537 show_temp_max, set_temp_max, 1);
538static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
539static SENSOR_DEVICE_ATTR(fan1_full, S_IRUGO, show_fan_full, NULL, 0);
540static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
541 show_fan_min, set_fan_min, 0);
542static SENSOR_DEVICE_ATTR(fan1_exp, S_IRUGO|S_IWUSR,
543 show_fan_exp, set_fan_exp, 0);
544static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
545static SENSOR_DEVICE_ATTR(fan2_full, S_IRUGO, show_fan_full, NULL, 1);
546static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
547 show_fan_min, set_fan_min, 1);
548static SENSOR_DEVICE_ATTR(fan2_exp, S_IRUGO|S_IWUSR,
549 show_fan_exp, set_fan_exp, 1);
550static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
551 show_pwm, set_pwm, 0);
552static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
553 show_pwm_enable, set_pwm_enable, 0);
Riku Voipio76e83be2007-10-26 14:14:23 +0300554static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
Riku Voipio84f1e442007-08-24 13:03:09 +0300555 show_pwm_mode, set_pwm_mode, 0);
556static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
557 show_pwm, set_pwm, 1);
558static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
559 show_pwm_enable, set_pwm_enable, 1);
Riku Voipio76e83be2007-10-26 14:14:23 +0300560static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
Riku Voipio84f1e442007-08-24 13:03:09 +0300561 show_pwm_mode, set_pwm_mode, 1);
562
563static struct attribute *f75375_attributes[] = {
564 &sensor_dev_attr_temp1_input.dev_attr.attr,
565 &sensor_dev_attr_temp1_max.dev_attr.attr,
566 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
567 &sensor_dev_attr_temp2_input.dev_attr.attr,
568 &sensor_dev_attr_temp2_max.dev_attr.attr,
569 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
570 &sensor_dev_attr_fan1_input.dev_attr.attr,
571 &sensor_dev_attr_fan1_full.dev_attr.attr,
572 &sensor_dev_attr_fan1_min.dev_attr.attr,
573 &sensor_dev_attr_fan1_exp.dev_attr.attr,
574 &sensor_dev_attr_fan2_input.dev_attr.attr,
575 &sensor_dev_attr_fan2_full.dev_attr.attr,
576 &sensor_dev_attr_fan2_min.dev_attr.attr,
577 &sensor_dev_attr_fan2_exp.dev_attr.attr,
578 &sensor_dev_attr_pwm1.dev_attr.attr,
579 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
580 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
581 &sensor_dev_attr_pwm2.dev_attr.attr,
582 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
583 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
584 &sensor_dev_attr_in0_input.dev_attr.attr,
585 &sensor_dev_attr_in0_max.dev_attr.attr,
586 &sensor_dev_attr_in0_min.dev_attr.attr,
587 &sensor_dev_attr_in1_input.dev_attr.attr,
588 &sensor_dev_attr_in1_max.dev_attr.attr,
589 &sensor_dev_attr_in1_min.dev_attr.attr,
590 &sensor_dev_attr_in2_input.dev_attr.attr,
591 &sensor_dev_attr_in2_max.dev_attr.attr,
592 &sensor_dev_attr_in2_min.dev_attr.attr,
593 &sensor_dev_attr_in3_input.dev_attr.attr,
594 &sensor_dev_attr_in3_max.dev_attr.attr,
595 &sensor_dev_attr_in3_min.dev_attr.attr,
596 NULL
597};
598
599static const struct attribute_group f75375_group = {
600 .attrs = f75375_attributes,
601};
602
Riku Voipioff312d02007-09-26 13:14:40 +0300603static void f75375_init(struct i2c_client *client, struct f75375_data *data,
604 struct f75375s_platform_data *f75375s_pdata)
605{
606 int nr;
607 set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
608 set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
609 for (nr = 0; nr < 2; nr++) {
610 data->pwm[nr] = SENSORS_LIMIT(f75375s_pdata->pwm[nr], 0, 255);
611 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
612 data->pwm[nr]);
613 }
614
615}
616
Jean Delvared2653e92008-04-29 23:11:39 +0200617static int f75375_probe(struct i2c_client *client,
618 const struct i2c_device_id *id)
Riku Voipio620c1422007-10-16 12:46:49 +0300619{
Andrew Klossner51b3e272009-03-12 13:36:39 +0100620 struct f75375_data *data;
Riku Voipioff312d02007-09-26 13:14:40 +0300621 struct f75375s_platform_data *f75375s_pdata = client->dev.platform_data;
Riku Voipio620c1422007-10-16 12:46:49 +0300622 int err;
623
624 if (!i2c_check_functionality(client->adapter,
625 I2C_FUNC_SMBUS_BYTE_DATA))
626 return -EIO;
627 if (!(data = kzalloc(sizeof(struct f75375_data), GFP_KERNEL)))
628 return -ENOMEM;
629
630 i2c_set_clientdata(client, data);
Riku Voipio620c1422007-10-16 12:46:49 +0300631 mutex_init(&data->update_lock);
Jean Delvare3760f732008-04-29 23:11:40 +0200632 data->kind = id->driver_data;
Riku Voipio620c1422007-10-16 12:46:49 +0300633
634 if ((err = sysfs_create_group(&client->dev.kobj, &f75375_group)))
635 goto exit_free;
636
Riku Voipio76e83be2007-10-26 14:14:23 +0300637 if (data->kind == f75375) {
638 err = sysfs_chmod_file(&client->dev.kobj,
639 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
640 S_IRUGO | S_IWUSR);
641 if (err)
642 goto exit_remove;
643 err = sysfs_chmod_file(&client->dev.kobj,
644 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
645 S_IRUGO | S_IWUSR);
646 if (err)
647 goto exit_remove;
648 }
649
Riku Voipio620c1422007-10-16 12:46:49 +0300650 data->hwmon_dev = hwmon_device_register(&client->dev);
651 if (IS_ERR(data->hwmon_dev)) {
652 err = PTR_ERR(data->hwmon_dev);
653 goto exit_remove;
654 }
655
Riku Voipioff312d02007-09-26 13:14:40 +0300656 if (f75375s_pdata != NULL)
657 f75375_init(client, data, f75375s_pdata);
658
Riku Voipio620c1422007-10-16 12:46:49 +0300659 return 0;
660
661exit_remove:
662 sysfs_remove_group(&client->dev.kobj, &f75375_group);
663exit_free:
Riku Voipio84f1e442007-08-24 13:03:09 +0300664 kfree(data);
Riku Voipio620c1422007-10-16 12:46:49 +0300665 i2c_set_clientdata(client, NULL);
666 return err;
667}
668
669static int f75375_remove(struct i2c_client *client)
670{
671 struct f75375_data *data = i2c_get_clientdata(client);
672 hwmon_device_unregister(data->hwmon_dev);
673 sysfs_remove_group(&client->dev.kobj, &f75375_group);
674 kfree(data);
675 i2c_set_clientdata(client, NULL);
Riku Voipio84f1e442007-08-24 13:03:09 +0300676 return 0;
677}
678
Jean Delvare935ada82008-07-16 19:30:11 +0200679/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100680static int f75375_detect(struct i2c_client *client,
Jean Delvare935ada82008-07-16 19:30:11 +0200681 struct i2c_board_info *info)
Riku Voipio84f1e442007-08-24 13:03:09 +0300682{
Jean Delvare935ada82008-07-16 19:30:11 +0200683 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100684 u16 vendid, chipid;
685 u8 version;
686 const char *name;
Riku Voipio84f1e442007-08-24 13:03:09 +0300687
Jean Delvare52df6442009-12-09 20:35:57 +0100688 vendid = f75375_read16(client, F75375_REG_VENDOR);
689 chipid = f75375_read16(client, F75375_CHIP_ID);
690 if (chipid == 0x0306 && vendid == 0x1934)
Riku Voipio84f1e442007-08-24 13:03:09 +0300691 name = "f75375";
Jean Delvare52df6442009-12-09 20:35:57 +0100692 else if (chipid == 0x0204 && vendid == 0x1934)
Riku Voipio84f1e442007-08-24 13:03:09 +0300693 name = "f75373";
Jean Delvare52df6442009-12-09 20:35:57 +0100694 else
695 return -ENODEV;
696
697 version = f75375_read8(client, F75375_REG_VERSION);
Riku Voipio84f1e442007-08-24 13:03:09 +0300698 dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
Jean Delvare935ada82008-07-16 19:30:11 +0200699 strlcpy(info->type, name, I2C_NAME_SIZE);
Riku Voipio84f1e442007-08-24 13:03:09 +0300700
Riku Voipio84f1e442007-08-24 13:03:09 +0300701 return 0;
Riku Voipio84f1e442007-08-24 13:03:09 +0300702}
703
704static int __init sensors_f75375_init(void)
705{
Jean Delvare935ada82008-07-16 19:30:11 +0200706 return i2c_add_driver(&f75375_driver);
Riku Voipio84f1e442007-08-24 13:03:09 +0300707}
708
709static void __exit sensors_f75375_exit(void)
710{
Riku Voipio84f1e442007-08-24 13:03:09 +0300711 i2c_del_driver(&f75375_driver);
712}
713
Riku Voipiob26e0ed2009-03-03 21:37:17 +0200714MODULE_AUTHOR("Riku Voipio");
Riku Voipio84f1e442007-08-24 13:03:09 +0300715MODULE_LICENSE("GPL");
716MODULE_DESCRIPTION("F75373/F75375 hardware monitoring driver");
717
718module_init(sensors_f75375_init);
719module_exit(sensors_f75375_exit);