xref: /openbmc/linux/drivers/hwmon/f75375s.c (revision a09d2831)
1 /*
2  * f75375s.c - driver for the Fintek F75375/SP and F75373
3  *             hardware monitoring features
4  * Copyright (C) 2006-2007  Riku Voipio
5  *
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>
37 #include <linux/f75375s.h>
38 
39 /* Addresses to scan */
40 static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
41 
42 enum chips { f75373, f75375 };
43 
44 /* Fintek F75375 registers  */
45 #define F75375_REG_CONFIG0		0x0
46 #define F75375_REG_CONFIG1		0x1
47 #define F75375_REG_CONFIG2		0x2
48 #define F75375_REG_CONFIG3		0x3
49 #define F75375_REG_ADDR			0x4
50 #define F75375_REG_INTR			0x31
51 #define F75375_CHIP_ID			0x5A
52 #define F75375_REG_VERSION		0x5C
53 #define F75375_REG_VENDOR		0x5D
54 #define F75375_REG_FAN_TIMER		0x60
55 
56 #define F75375_REG_VOLT(nr)		(0x10 + (nr))
57 #define F75375_REG_VOLT_HIGH(nr)	(0x20 + (nr) * 2)
58 #define F75375_REG_VOLT_LOW(nr)		(0x21 + (nr) * 2)
59 
60 #define F75375_REG_TEMP(nr)		(0x14 + (nr))
61 #define F75375_REG_TEMP_HIGH(nr)	(0x28 + (nr) * 2)
62 #define F75375_REG_TEMP_HYST(nr)	(0x29 + (nr) * 2)
63 
64 #define F75375_REG_FAN(nr)		(0x16 + (nr) * 2)
65 #define F75375_REG_FAN_MIN(nr)		(0x2C + (nr) * 2)
66 #define F75375_REG_FAN_FULL(nr)		(0x70 + (nr) * 0x10)
67 #define F75375_REG_FAN_PWM_DUTY(nr)	(0x76 + (nr) * 0x10)
68 #define F75375_REG_FAN_PWM_CLOCK(nr)	(0x7D + (nr) * 0x10)
69 
70 #define F75375_REG_FAN_EXP(nr)		(0x74 + (nr) * 0x10)
71 #define F75375_REG_FAN_B_TEMP(nr, step)	((0xA0 + (nr) * 0x10) + (step))
72 #define F75375_REG_FAN_B_SPEED(nr, step) \
73 	((0xA5 + (nr) * 0x10) + (step) * 2)
74 
75 #define F75375_REG_PWM1_RAISE_DUTY	0x69
76 #define F75375_REG_PWM2_RAISE_DUTY	0x6A
77 #define F75375_REG_PWM1_DROP_DUTY	0x6B
78 #define F75375_REG_PWM2_DROP_DUTY	0x6C
79 
80 #define FAN_CTRL_LINEAR(nr)		(4 + nr)
81 #define FAN_CTRL_MODE(nr)		(5 + ((nr) * 2))
82 
83 /*
84  * Data structures and manipulation thereof
85  */
86 
87 struct f75375_data {
88 	unsigned short addr;
89 	struct device *hwmon_dev;
90 
91 	const char *name;
92 	int kind;
93 	struct mutex update_lock; /* protect register access */
94 	char valid;
95 	unsigned long last_updated;	/* In jiffies */
96 	unsigned long last_limits;	/* In jiffies */
97 
98 	/* Register values */
99 	u8 in[4];
100 	u8 in_max[4];
101 	u8 in_min[4];
102 	u16 fan[2];
103 	u16 fan_min[2];
104 	u16 fan_full[2];
105 	u16 fan_exp[2];
106 	u8 fan_timer;
107 	u8 pwm[2];
108 	u8 pwm_mode[2];
109 	u8 pwm_enable[2];
110 	s8 temp[2];
111 	s8 temp_high[2];
112 	s8 temp_max_hyst[2];
113 };
114 
115 static int f75375_detect(struct i2c_client *client,
116 			 struct i2c_board_info *info);
117 static int f75375_probe(struct i2c_client *client,
118 			const struct i2c_device_id *id);
119 static int f75375_remove(struct i2c_client *client);
120 
121 static const struct i2c_device_id f75375_id[] = {
122 	{ "f75373", f75373 },
123 	{ "f75375", f75375 },
124 	{ }
125 };
126 MODULE_DEVICE_TABLE(i2c, f75375_id);
127 
128 static struct i2c_driver f75375_driver = {
129 	.class = I2C_CLASS_HWMON,
130 	.driver = {
131 		.name = "f75375",
132 	},
133 	.probe = f75375_probe,
134 	.remove = f75375_remove,
135 	.id_table = f75375_id,
136 	.detect = f75375_detect,
137 	.address_list = normal_i2c,
138 };
139 
140 static inline int f75375_read8(struct i2c_client *client, u8 reg)
141 {
142 	return i2c_smbus_read_byte_data(client, reg);
143 }
144 
145 /* in most cases, should be called while holding update_lock */
146 static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
147 {
148 	return ((i2c_smbus_read_byte_data(client, reg) << 8)
149 		| i2c_smbus_read_byte_data(client, reg + 1));
150 }
151 
152 static inline void f75375_write8(struct i2c_client *client, u8 reg,
153 		u8 value)
154 {
155 	i2c_smbus_write_byte_data(client, reg, value);
156 }
157 
158 static inline void f75375_write16(struct i2c_client *client, u8 reg,
159 		u16 value)
160 {
161 	int err = i2c_smbus_write_byte_data(client, reg, (value << 8));
162 	if (err)
163 		return;
164 	i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
165 }
166 
167 static struct f75375_data *f75375_update_device(struct device *dev)
168 {
169 	struct i2c_client *client = to_i2c_client(dev);
170 	struct f75375_data *data = i2c_get_clientdata(client);
171 	int nr;
172 
173 	mutex_lock(&data->update_lock);
174 
175 	/* Limit registers cache is refreshed after 60 seconds */
176 	if (time_after(jiffies, data->last_limits + 60 * HZ)
177 		|| !data->valid) {
178 		for (nr = 0; nr < 2; nr++) {
179 			data->temp_high[nr] =
180 				f75375_read8(client, F75375_REG_TEMP_HIGH(nr));
181 			data->temp_max_hyst[nr] =
182 				f75375_read8(client, F75375_REG_TEMP_HYST(nr));
183 			data->fan_full[nr] =
184 				f75375_read16(client, F75375_REG_FAN_FULL(nr));
185 			data->fan_min[nr] =
186 				f75375_read16(client, F75375_REG_FAN_MIN(nr));
187 			data->fan_exp[nr] =
188 				f75375_read16(client, F75375_REG_FAN_EXP(nr));
189 			data->pwm[nr] =	f75375_read8(client,
190 				F75375_REG_FAN_PWM_DUTY(nr));
191 
192 		}
193 		for (nr = 0; nr < 4; nr++) {
194 			data->in_max[nr] =
195 				f75375_read8(client, F75375_REG_VOLT_HIGH(nr));
196 			data->in_min[nr] =
197 				f75375_read8(client, F75375_REG_VOLT_LOW(nr));
198 		}
199 		data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
200 		data->last_limits = jiffies;
201 	}
202 
203 	/* Measurement registers cache is refreshed after 2 second */
204 	if (time_after(jiffies, data->last_updated + 2 * HZ)
205 		|| !data->valid) {
206 		for (nr = 0; nr < 2; nr++) {
207 			data->temp[nr] =
208 				f75375_read8(client, F75375_REG_TEMP(nr));
209 			data->fan[nr] =
210 				f75375_read16(client, F75375_REG_FAN(nr));
211 		}
212 		for (nr = 0; nr < 4; nr++)
213 			data->in[nr] =
214 				f75375_read8(client, F75375_REG_VOLT(nr));
215 
216 		data->last_updated = jiffies;
217 		data->valid = 1;
218 	}
219 
220 	mutex_unlock(&data->update_lock);
221 	return data;
222 }
223 
224 static inline u16 rpm_from_reg(u16 reg)
225 {
226 	if (reg == 0 || reg == 0xffff)
227 		return 0;
228 	return (1500000 / reg);
229 }
230 
231 static inline u16 rpm_to_reg(int rpm)
232 {
233 	if (rpm < 367 || rpm > 0xffff)
234 		return 0xffff;
235 	return (1500000 / rpm);
236 }
237 
238 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
239 		const char *buf, size_t count)
240 {
241 	int nr = to_sensor_dev_attr(attr)->index;
242 	struct i2c_client *client = to_i2c_client(dev);
243 	struct f75375_data *data = i2c_get_clientdata(client);
244 	int val = simple_strtoul(buf, NULL, 10);
245 
246 	mutex_lock(&data->update_lock);
247 	data->fan_min[nr] = rpm_to_reg(val);
248 	f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
249 	mutex_unlock(&data->update_lock);
250 	return count;
251 }
252 
253 static ssize_t set_fan_exp(struct device *dev, struct device_attribute *attr,
254 		const char *buf, size_t count)
255 {
256 	int nr = to_sensor_dev_attr(attr)->index;
257 	struct i2c_client *client = to_i2c_client(dev);
258 	struct f75375_data *data = i2c_get_clientdata(client);
259 	int val = simple_strtoul(buf, NULL, 10);
260 
261 	mutex_lock(&data->update_lock);
262 	data->fan_exp[nr] = rpm_to_reg(val);
263 	f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_exp[nr]);
264 	mutex_unlock(&data->update_lock);
265 	return count;
266 }
267 
268 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
269 		const char *buf, size_t count)
270 {
271 	int nr = to_sensor_dev_attr(attr)->index;
272 	struct i2c_client *client = to_i2c_client(dev);
273 	struct f75375_data *data = i2c_get_clientdata(client);
274 	int val = simple_strtoul(buf, NULL, 10);
275 
276 	mutex_lock(&data->update_lock);
277 	data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
278 	f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr), data->pwm[nr]);
279 	mutex_unlock(&data->update_lock);
280 	return count;
281 }
282 
283 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
284 		*attr, char *buf)
285 {
286 	int nr = to_sensor_dev_attr(attr)->index;
287 	struct f75375_data *data = f75375_update_device(dev);
288 	return sprintf(buf, "%d\n", data->pwm_enable[nr]);
289 }
290 
291 static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
292 {
293 	struct f75375_data *data = i2c_get_clientdata(client);
294 	u8 fanmode;
295 
296 	if (val < 0 || val > 4)
297 		return -EINVAL;
298 
299 	fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
300 	fanmode = ~(3 << FAN_CTRL_MODE(nr));
301 
302 	switch (val) {
303 	case 0: /* Full speed */
304 		fanmode  |= (3 << FAN_CTRL_MODE(nr));
305 		data->pwm[nr] = 255;
306 		f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
307 				data->pwm[nr]);
308 		break;
309 	case 1: /* PWM */
310 		fanmode  |= (3 << FAN_CTRL_MODE(nr));
311 		break;
312 	case 2: /* AUTOMATIC*/
313 		fanmode  |= (2 << FAN_CTRL_MODE(nr));
314 		break;
315 	case 3: /* fan speed */
316 		break;
317 	}
318 	f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
319 	data->pwm_enable[nr] = val;
320 	return 0;
321 }
322 
323 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
324 		const char *buf, size_t count)
325 {
326 	int nr = to_sensor_dev_attr(attr)->index;
327 	struct i2c_client *client = to_i2c_client(dev);
328 	struct f75375_data *data = i2c_get_clientdata(client);
329 	int val = simple_strtoul(buf, NULL, 10);
330 	int err = 0;
331 
332 	mutex_lock(&data->update_lock);
333 	err = set_pwm_enable_direct(client, nr, val);
334 	mutex_unlock(&data->update_lock);
335 	return err ? err : count;
336 }
337 
338 static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
339 		const char *buf, size_t count)
340 {
341 	int nr = to_sensor_dev_attr(attr)->index;
342 	struct i2c_client *client = to_i2c_client(dev);
343 	struct f75375_data *data = i2c_get_clientdata(client);
344 	int val = simple_strtoul(buf, NULL, 10);
345 	u8 conf = 0;
346 
347 	if (!(val == 0 || val == 1))
348 		return -EINVAL;
349 
350 	mutex_lock(&data->update_lock);
351 	conf = f75375_read8(client, F75375_REG_CONFIG1);
352 	conf = ~(1 << FAN_CTRL_LINEAR(nr));
353 
354 	if (val == 0)
355 		conf |= (1 << FAN_CTRL_LINEAR(nr)) ;
356 
357 	f75375_write8(client, F75375_REG_CONFIG1, conf);
358 	data->pwm_mode[nr] = val;
359 	mutex_unlock(&data->update_lock);
360 	return count;
361 }
362 
363 static ssize_t show_pwm(struct device *dev, struct device_attribute
364 		*attr, char *buf)
365 {
366 	int nr = to_sensor_dev_attr(attr)->index;
367 	struct f75375_data *data = f75375_update_device(dev);
368 	return sprintf(buf, "%d\n", data->pwm[nr]);
369 }
370 
371 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
372 		*attr, char *buf)
373 {
374 	int nr = to_sensor_dev_attr(attr)->index;
375 	struct f75375_data *data = f75375_update_device(dev);
376 	return sprintf(buf, "%d\n", data->pwm_mode[nr]);
377 }
378 
379 #define VOLT_FROM_REG(val) ((val) * 8)
380 #define VOLT_TO_REG(val) ((val) / 8)
381 
382 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
383 		char *buf)
384 {
385 	int nr = to_sensor_dev_attr(attr)->index;
386 	struct f75375_data *data = f75375_update_device(dev);
387 	return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
388 }
389 
390 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
391 		char *buf)
392 {
393 	int nr = to_sensor_dev_attr(attr)->index;
394 	struct f75375_data *data = f75375_update_device(dev);
395 	return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
396 }
397 
398 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
399 		char *buf)
400 {
401 	int nr = to_sensor_dev_attr(attr)->index;
402 	struct f75375_data *data = f75375_update_device(dev);
403 	return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
404 }
405 
406 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
407 		const char *buf, size_t count)
408 {
409 	int nr = to_sensor_dev_attr(attr)->index;
410 	struct i2c_client *client = to_i2c_client(dev);
411 	struct f75375_data *data = i2c_get_clientdata(client);
412 	int val = simple_strtoul(buf, NULL, 10);
413 	val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
414 	mutex_lock(&data->update_lock);
415 	data->in_max[nr] = val;
416 	f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
417 	mutex_unlock(&data->update_lock);
418 	return count;
419 }
420 
421 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
422 		const char *buf, size_t count)
423 {
424 	int nr = to_sensor_dev_attr(attr)->index;
425 	struct i2c_client *client = to_i2c_client(dev);
426 	struct f75375_data *data = i2c_get_clientdata(client);
427 	int val = simple_strtoul(buf, NULL, 10);
428 	val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
429 	mutex_lock(&data->update_lock);
430 	data->in_min[nr] = val;
431 	f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
432 	mutex_unlock(&data->update_lock);
433 	return count;
434 }
435 #define TEMP_FROM_REG(val) ((val) * 1000)
436 #define TEMP_TO_REG(val) ((val) / 1000)
437 
438 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
439 		char *buf)
440 {
441 	int nr = to_sensor_dev_attr(attr)->index;
442 	struct f75375_data *data = f75375_update_device(dev);
443 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
444 }
445 
446 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
447 		char *buf)
448 {
449 	int nr = to_sensor_dev_attr(attr)->index;
450 	struct f75375_data *data = f75375_update_device(dev);
451 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
452 }
453 
454 static ssize_t show_temp_max_hyst(struct device *dev,
455 		struct device_attribute *attr, char *buf)
456 {
457 	int nr = to_sensor_dev_attr(attr)->index;
458 	struct f75375_data *data = f75375_update_device(dev);
459 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
460 }
461 
462 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
463 		const char *buf, size_t count)
464 {
465 	int nr = to_sensor_dev_attr(attr)->index;
466 	struct i2c_client *client = to_i2c_client(dev);
467 	struct f75375_data *data = i2c_get_clientdata(client);
468 	int val = simple_strtol(buf, NULL, 10);
469 	val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
470 	mutex_lock(&data->update_lock);
471 	data->temp_high[nr] = val;
472 	f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
473 	mutex_unlock(&data->update_lock);
474 	return count;
475 }
476 
477 static ssize_t set_temp_max_hyst(struct device *dev,
478 	struct device_attribute *attr, const char *buf, size_t count)
479 {
480 	int nr = to_sensor_dev_attr(attr)->index;
481 	struct i2c_client *client = to_i2c_client(dev);
482 	struct f75375_data *data = i2c_get_clientdata(client);
483 	int val = simple_strtol(buf, NULL, 10);
484 	val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
485 	mutex_lock(&data->update_lock);
486 	data->temp_max_hyst[nr] = val;
487 	f75375_write8(client, F75375_REG_TEMP_HYST(nr),
488 		data->temp_max_hyst[nr]);
489 	mutex_unlock(&data->update_lock);
490 	return count;
491 }
492 
493 #define show_fan(thing) \
494 static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
495 			char *buf)\
496 {\
497 	int nr = to_sensor_dev_attr(attr)->index;\
498 	struct f75375_data *data = f75375_update_device(dev); \
499 	return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
500 }
501 
502 show_fan(fan);
503 show_fan(fan_min);
504 show_fan(fan_full);
505 show_fan(fan_exp);
506 
507 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
508 static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
509 	show_in_max, set_in_max, 0);
510 static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
511 	show_in_min, set_in_min, 0);
512 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
513 static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
514 	show_in_max, set_in_max, 1);
515 static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
516 	show_in_min, set_in_min, 1);
517 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
518 static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
519 	show_in_max, set_in_max, 2);
520 static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
521 	show_in_min, set_in_min, 2);
522 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
523 static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
524 	show_in_max, set_in_max, 3);
525 static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
526 	show_in_min, set_in_min, 3);
527 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
528 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
529 	show_temp_max_hyst, set_temp_max_hyst, 0);
530 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
531 	show_temp_max, set_temp_max, 0);
532 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
533 static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
534 	show_temp_max_hyst, set_temp_max_hyst, 1);
535 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
536 	show_temp_max, set_temp_max, 1);
537 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
538 static SENSOR_DEVICE_ATTR(fan1_full, S_IRUGO, show_fan_full, NULL, 0);
539 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
540 	show_fan_min, set_fan_min, 0);
541 static SENSOR_DEVICE_ATTR(fan1_exp, S_IRUGO|S_IWUSR,
542 	show_fan_exp, set_fan_exp, 0);
543 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
544 static SENSOR_DEVICE_ATTR(fan2_full, S_IRUGO, show_fan_full, NULL, 1);
545 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
546 	show_fan_min, set_fan_min, 1);
547 static SENSOR_DEVICE_ATTR(fan2_exp, S_IRUGO|S_IWUSR,
548 	show_fan_exp, set_fan_exp, 1);
549 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
550 	show_pwm, set_pwm, 0);
551 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
552 	show_pwm_enable, set_pwm_enable, 0);
553 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
554 	show_pwm_mode, set_pwm_mode, 0);
555 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
556 	show_pwm, set_pwm, 1);
557 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
558 	show_pwm_enable, set_pwm_enable, 1);
559 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
560 	show_pwm_mode, set_pwm_mode, 1);
561 
562 static struct attribute *f75375_attributes[] = {
563 	&sensor_dev_attr_temp1_input.dev_attr.attr,
564 	&sensor_dev_attr_temp1_max.dev_attr.attr,
565 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
566 	&sensor_dev_attr_temp2_input.dev_attr.attr,
567 	&sensor_dev_attr_temp2_max.dev_attr.attr,
568 	&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
569 	&sensor_dev_attr_fan1_input.dev_attr.attr,
570 	&sensor_dev_attr_fan1_full.dev_attr.attr,
571 	&sensor_dev_attr_fan1_min.dev_attr.attr,
572 	&sensor_dev_attr_fan1_exp.dev_attr.attr,
573 	&sensor_dev_attr_fan2_input.dev_attr.attr,
574 	&sensor_dev_attr_fan2_full.dev_attr.attr,
575 	&sensor_dev_attr_fan2_min.dev_attr.attr,
576 	&sensor_dev_attr_fan2_exp.dev_attr.attr,
577 	&sensor_dev_attr_pwm1.dev_attr.attr,
578 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
579 	&sensor_dev_attr_pwm1_mode.dev_attr.attr,
580 	&sensor_dev_attr_pwm2.dev_attr.attr,
581 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
582 	&sensor_dev_attr_pwm2_mode.dev_attr.attr,
583 	&sensor_dev_attr_in0_input.dev_attr.attr,
584 	&sensor_dev_attr_in0_max.dev_attr.attr,
585 	&sensor_dev_attr_in0_min.dev_attr.attr,
586 	&sensor_dev_attr_in1_input.dev_attr.attr,
587 	&sensor_dev_attr_in1_max.dev_attr.attr,
588 	&sensor_dev_attr_in1_min.dev_attr.attr,
589 	&sensor_dev_attr_in2_input.dev_attr.attr,
590 	&sensor_dev_attr_in2_max.dev_attr.attr,
591 	&sensor_dev_attr_in2_min.dev_attr.attr,
592 	&sensor_dev_attr_in3_input.dev_attr.attr,
593 	&sensor_dev_attr_in3_max.dev_attr.attr,
594 	&sensor_dev_attr_in3_min.dev_attr.attr,
595 	NULL
596 };
597 
598 static const struct attribute_group f75375_group = {
599 	.attrs = f75375_attributes,
600 };
601 
602 static void f75375_init(struct i2c_client *client, struct f75375_data *data,
603 		struct f75375s_platform_data *f75375s_pdata)
604 {
605 	int nr;
606 	set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
607 	set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
608 	for (nr = 0; nr < 2; nr++) {
609 		data->pwm[nr] = SENSORS_LIMIT(f75375s_pdata->pwm[nr], 0, 255);
610 		f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
611 			data->pwm[nr]);
612 	}
613 
614 }
615 
616 static int f75375_probe(struct i2c_client *client,
617 		const struct i2c_device_id *id)
618 {
619 	struct f75375_data *data;
620 	struct f75375s_platform_data *f75375s_pdata = client->dev.platform_data;
621 	int err;
622 
623 	if (!i2c_check_functionality(client->adapter,
624 				I2C_FUNC_SMBUS_BYTE_DATA))
625 		return -EIO;
626 	if (!(data = kzalloc(sizeof(struct f75375_data), GFP_KERNEL)))
627 		return -ENOMEM;
628 
629 	i2c_set_clientdata(client, data);
630 	mutex_init(&data->update_lock);
631 	data->kind = id->driver_data;
632 
633 	if ((err = sysfs_create_group(&client->dev.kobj, &f75375_group)))
634 		goto exit_free;
635 
636 	if (data->kind == f75375) {
637 		err = sysfs_chmod_file(&client->dev.kobj,
638 			&sensor_dev_attr_pwm1_mode.dev_attr.attr,
639 			S_IRUGO | S_IWUSR);
640 		if (err)
641 			goto exit_remove;
642 		err = sysfs_chmod_file(&client->dev.kobj,
643 			&sensor_dev_attr_pwm2_mode.dev_attr.attr,
644 			S_IRUGO | S_IWUSR);
645 		if (err)
646 			goto exit_remove;
647 	}
648 
649 	data->hwmon_dev = hwmon_device_register(&client->dev);
650 	if (IS_ERR(data->hwmon_dev)) {
651 		err = PTR_ERR(data->hwmon_dev);
652 		goto exit_remove;
653 	}
654 
655 	if (f75375s_pdata != NULL)
656 		f75375_init(client, data, f75375s_pdata);
657 
658 	return 0;
659 
660 exit_remove:
661 	sysfs_remove_group(&client->dev.kobj, &f75375_group);
662 exit_free:
663 	kfree(data);
664 	i2c_set_clientdata(client, NULL);
665 	return err;
666 }
667 
668 static int f75375_remove(struct i2c_client *client)
669 {
670 	struct f75375_data *data = i2c_get_clientdata(client);
671 	hwmon_device_unregister(data->hwmon_dev);
672 	sysfs_remove_group(&client->dev.kobj, &f75375_group);
673 	kfree(data);
674 	i2c_set_clientdata(client, NULL);
675 	return 0;
676 }
677 
678 /* Return 0 if detection is successful, -ENODEV otherwise */
679 static int f75375_detect(struct i2c_client *client,
680 			 struct i2c_board_info *info)
681 {
682 	struct i2c_adapter *adapter = client->adapter;
683 	u16 vendid, chipid;
684 	u8 version;
685 	const char *name;
686 
687 	vendid = f75375_read16(client, F75375_REG_VENDOR);
688 	chipid = f75375_read16(client, F75375_CHIP_ID);
689 	if (chipid == 0x0306 && vendid == 0x1934)
690 		name = "f75375";
691 	else if (chipid == 0x0204 && vendid == 0x1934)
692 		name = "f75373";
693 	else
694 		return -ENODEV;
695 
696 	version = f75375_read8(client, F75375_REG_VERSION);
697 	dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
698 	strlcpy(info->type, name, I2C_NAME_SIZE);
699 
700 	return 0;
701 }
702 
703 static int __init sensors_f75375_init(void)
704 {
705 	return i2c_add_driver(&f75375_driver);
706 }
707 
708 static void __exit sensors_f75375_exit(void)
709 {
710 	i2c_del_driver(&f75375_driver);
711 }
712 
713 MODULE_AUTHOR("Riku Voipio");
714 MODULE_LICENSE("GPL");
715 MODULE_DESCRIPTION("F75373/F75375 hardware monitoring driver");
716 
717 module_init(sensors_f75375_init);
718 module_exit(sensors_f75375_exit);
719