xref: /linux/drivers/hwmon/f75375s.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * f75375s.c - driver for the Fintek F75375/SP, F75373 and
4  *             F75387SG/RG hardware monitoring features
5  * Copyright (C) 2006-2007  Riku Voipio
6  *
7  * Datasheets available at:
8  *
9  * f75375:
10  * http://www.fintek.com.tw/files/productfiles/F75375_V026P.pdf
11  *
12  * f75373:
13  * http://www.fintek.com.tw/files/productfiles/F75373_V025P.pdf
14  *
15  * f75387:
16  * http://www.fintek.com.tw/files/productfiles/F75387_V027P.pdf
17  */
18 
19 #include <linux/module.h>
20 #include <linux/jiffies.h>
21 #include <linux/hwmon.h>
22 #include <linux/hwmon-sysfs.h>
23 #include <linux/i2c.h>
24 #include <linux/err.h>
25 #include <linux/mutex.h>
26 #include <linux/f75375s.h>
27 #include <linux/slab.h>
28 
29 /* Addresses to scan */
30 static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
31 
32 enum chips { f75373, f75375, f75387 };
33 
34 /* Fintek F75375 registers  */
35 #define F75375_REG_CONFIG0		0x0
36 #define F75375_REG_CONFIG1		0x1
37 #define F75375_REG_CONFIG2		0x2
38 #define F75375_REG_CONFIG3		0x3
39 #define F75375_REG_ADDR			0x4
40 #define F75375_REG_INTR			0x31
41 #define F75375_CHIP_ID			0x5A
42 #define F75375_REG_VERSION		0x5C
43 #define F75375_REG_VENDOR		0x5D
44 #define F75375_REG_FAN_TIMER		0x60
45 
46 #define F75375_REG_VOLT(nr)		(0x10 + (nr))
47 #define F75375_REG_VOLT_HIGH(nr)	(0x20 + (nr) * 2)
48 #define F75375_REG_VOLT_LOW(nr)		(0x21 + (nr) * 2)
49 
50 #define F75375_REG_TEMP(nr)		(0x14 + (nr))
51 #define F75387_REG_TEMP11_LSB(nr)	(0x1a + (nr))
52 #define F75375_REG_TEMP_HIGH(nr)	(0x28 + (nr) * 2)
53 #define F75375_REG_TEMP_HYST(nr)	(0x29 + (nr) * 2)
54 
55 #define F75375_REG_FAN(nr)		(0x16 + (nr) * 2)
56 #define F75375_REG_FAN_MIN(nr)		(0x2C + (nr) * 2)
57 #define F75375_REG_FAN_FULL(nr)		(0x70 + (nr) * 0x10)
58 #define F75375_REG_FAN_PWM_DUTY(nr)	(0x76 + (nr) * 0x10)
59 #define F75375_REG_FAN_PWM_CLOCK(nr)	(0x7D + (nr) * 0x10)
60 
61 #define F75375_REG_FAN_EXP(nr)		(0x74 + (nr) * 0x10)
62 #define F75375_REG_FAN_B_TEMP(nr, step)	((0xA0 + (nr) * 0x10) + (step))
63 #define F75375_REG_FAN_B_SPEED(nr, step) \
64 	((0xA5 + (nr) * 0x10) + (step) * 2)
65 
66 #define F75375_REG_PWM1_RAISE_DUTY	0x69
67 #define F75375_REG_PWM2_RAISE_DUTY	0x6A
68 #define F75375_REG_PWM1_DROP_DUTY	0x6B
69 #define F75375_REG_PWM2_DROP_DUTY	0x6C
70 
71 #define F75375_FAN_CTRL_LINEAR(nr)	(4 + nr)
72 #define F75387_FAN_CTRL_LINEAR(nr)	(1 + ((nr) * 4))
73 #define FAN_CTRL_MODE(nr)		(4 + ((nr) * 2))
74 #define F75387_FAN_DUTY_MODE(nr)	(2 + ((nr) * 4))
75 #define F75387_FAN_MANU_MODE(nr)	((nr) * 4)
76 
77 /*
78  * Data structures and manipulation thereof
79  */
80 
81 struct f75375_data {
82 	unsigned short addr;
83 	struct device *hwmon_dev;
84 
85 	const char *name;
86 	int kind;
87 	struct mutex update_lock; /* protect register access */
88 	bool valid;
89 	unsigned long last_updated;	/* In jiffies */
90 	unsigned long last_limits;	/* In jiffies */
91 
92 	/* Register values */
93 	u8 in[4];
94 	u8 in_max[4];
95 	u8 in_min[4];
96 	u16 fan[2];
97 	u16 fan_min[2];
98 	u16 fan_max[2];
99 	u16 fan_target[2];
100 	u8 fan_timer;
101 	u8 pwm[2];
102 	u8 pwm_mode[2];
103 	u8 pwm_enable[2];
104 	/*
105 	 * f75387: For remote temperature reading, it uses signed 11-bit
106 	 * values with LSB = 0.125 degree Celsius, left-justified in 16-bit
107 	 * registers. For original 8-bit temp readings, the LSB just is 0.
108 	 */
109 	s16 temp11[2];
110 	s8 temp_high[2];
111 	s8 temp_max_hyst[2];
112 };
113 
f75375_read8(struct i2c_client * client,u8 reg)114 static inline int f75375_read8(struct i2c_client *client, u8 reg)
115 {
116 	return i2c_smbus_read_byte_data(client, reg);
117 }
118 
119 /* in most cases, should be called while holding update_lock */
f75375_read16(struct i2c_client * client,u8 reg)120 static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
121 {
122 	return (i2c_smbus_read_byte_data(client, reg) << 8)
123 		| i2c_smbus_read_byte_data(client, reg + 1);
124 }
125 
f75375_write8(struct i2c_client * client,u8 reg,u8 value)126 static inline void f75375_write8(struct i2c_client *client, u8 reg,
127 		u8 value)
128 {
129 	i2c_smbus_write_byte_data(client, reg, value);
130 }
131 
f75375_write16(struct i2c_client * client,u8 reg,u16 value)132 static inline void f75375_write16(struct i2c_client *client, u8 reg,
133 		u16 value)
134 {
135 	int err = i2c_smbus_write_byte_data(client, reg, (value >> 8));
136 	if (err)
137 		return;
138 	i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
139 }
140 
f75375_write_pwm(struct i2c_client * client,int nr)141 static void f75375_write_pwm(struct i2c_client *client, int nr)
142 {
143 	struct f75375_data *data = i2c_get_clientdata(client);
144 	if (data->kind == f75387)
145 		f75375_write16(client, F75375_REG_FAN_EXP(nr), data->pwm[nr]);
146 	else
147 		f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
148 			      data->pwm[nr]);
149 }
150 
f75375_update_device(struct device * dev)151 static struct f75375_data *f75375_update_device(struct device *dev)
152 {
153 	struct i2c_client *client = to_i2c_client(dev);
154 	struct f75375_data *data = i2c_get_clientdata(client);
155 	int nr;
156 
157 	mutex_lock(&data->update_lock);
158 
159 	/* Limit registers cache is refreshed after 60 seconds */
160 	if (time_after(jiffies, data->last_limits + 60 * HZ)
161 		|| !data->valid) {
162 		for (nr = 0; nr < 2; nr++) {
163 			data->temp_high[nr] =
164 				f75375_read8(client, F75375_REG_TEMP_HIGH(nr));
165 			data->temp_max_hyst[nr] =
166 				f75375_read8(client, F75375_REG_TEMP_HYST(nr));
167 			data->fan_max[nr] =
168 				f75375_read16(client, F75375_REG_FAN_FULL(nr));
169 			data->fan_min[nr] =
170 				f75375_read16(client, F75375_REG_FAN_MIN(nr));
171 			data->fan_target[nr] =
172 				f75375_read16(client, F75375_REG_FAN_EXP(nr));
173 		}
174 		for (nr = 0; nr < 4; nr++) {
175 			data->in_max[nr] =
176 				f75375_read8(client, F75375_REG_VOLT_HIGH(nr));
177 			data->in_min[nr] =
178 				f75375_read8(client, F75375_REG_VOLT_LOW(nr));
179 		}
180 		data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
181 		data->last_limits = jiffies;
182 	}
183 
184 	/* Measurement registers cache is refreshed after 2 second */
185 	if (time_after(jiffies, data->last_updated + 2 * HZ)
186 		|| !data->valid) {
187 		for (nr = 0; nr < 2; nr++) {
188 			data->pwm[nr] =	f75375_read8(client,
189 				F75375_REG_FAN_PWM_DUTY(nr));
190 			/* assign MSB, therefore shift it by 8 bits */
191 			data->temp11[nr] =
192 				f75375_read8(client, F75375_REG_TEMP(nr)) << 8;
193 			if (data->kind == f75387)
194 				/* merge F75387's temperature LSB (11-bit) */
195 				data->temp11[nr] |=
196 					f75375_read8(client,
197 						     F75387_REG_TEMP11_LSB(nr));
198 			data->fan[nr] =
199 				f75375_read16(client, F75375_REG_FAN(nr));
200 		}
201 		for (nr = 0; nr < 4; nr++)
202 			data->in[nr] =
203 				f75375_read8(client, F75375_REG_VOLT(nr));
204 
205 		data->last_updated = jiffies;
206 		data->valid = true;
207 	}
208 
209 	mutex_unlock(&data->update_lock);
210 	return data;
211 }
212 
rpm_from_reg(u16 reg)213 static inline u16 rpm_from_reg(u16 reg)
214 {
215 	if (reg == 0 || reg == 0xffff)
216 		return 0;
217 	return 1500000 / reg;
218 }
219 
rpm_to_reg(int rpm)220 static inline u16 rpm_to_reg(int rpm)
221 {
222 	if (rpm < 367 || rpm > 0xffff)
223 		return 0xffff;
224 	return 1500000 / rpm;
225 }
226 
duty_mode_enabled(u8 pwm_enable)227 static bool duty_mode_enabled(u8 pwm_enable)
228 {
229 	switch (pwm_enable) {
230 	case 0: /* Manual, duty mode (full speed) */
231 	case 1: /* Manual, duty mode */
232 	case 4: /* Auto, duty mode */
233 		return true;
234 	case 2: /* Auto, speed mode */
235 	case 3: /* Manual, speed mode */
236 		return false;
237 	default:
238 		WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable);
239 		return true;
240 	}
241 }
242 
auto_mode_enabled(u8 pwm_enable)243 static bool auto_mode_enabled(u8 pwm_enable)
244 {
245 	switch (pwm_enable) {
246 	case 0: /* Manual, duty mode (full speed) */
247 	case 1: /* Manual, duty mode */
248 	case 3: /* Manual, speed mode */
249 		return false;
250 	case 2: /* Auto, speed mode */
251 	case 4: /* Auto, duty mode */
252 		return true;
253 	default:
254 		WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable);
255 		return false;
256 	}
257 }
258 
set_fan_min(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)259 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
260 		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);
265 	unsigned long val;
266 	int err;
267 
268 	err = kstrtoul(buf, 10, &val);
269 	if (err < 0)
270 		return err;
271 
272 	mutex_lock(&data->update_lock);
273 	data->fan_min[nr] = rpm_to_reg(val);
274 	f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
275 	mutex_unlock(&data->update_lock);
276 	return count;
277 }
278 
set_fan_target(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)279 static ssize_t set_fan_target(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);
285 	unsigned long val;
286 	int err;
287 
288 	err = kstrtoul(buf, 10, &val);
289 	if (err < 0)
290 		return err;
291 
292 	if (auto_mode_enabled(data->pwm_enable[nr]))
293 		return -EINVAL;
294 	if (data->kind == f75387 && duty_mode_enabled(data->pwm_enable[nr]))
295 		return -EINVAL;
296 
297 	mutex_lock(&data->update_lock);
298 	data->fan_target[nr] = rpm_to_reg(val);
299 	f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_target[nr]);
300 	mutex_unlock(&data->update_lock);
301 	return count;
302 }
303 
set_pwm(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)304 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
305 		const char *buf, size_t count)
306 {
307 	int nr = to_sensor_dev_attr(attr)->index;
308 	struct i2c_client *client = to_i2c_client(dev);
309 	struct f75375_data *data = i2c_get_clientdata(client);
310 	unsigned long val;
311 	int err;
312 
313 	err = kstrtoul(buf, 10, &val);
314 	if (err < 0)
315 		return err;
316 
317 	if (auto_mode_enabled(data->pwm_enable[nr]) ||
318 	    !duty_mode_enabled(data->pwm_enable[nr]))
319 		return -EINVAL;
320 
321 	mutex_lock(&data->update_lock);
322 	data->pwm[nr] = clamp_val(val, 0, 255);
323 	f75375_write_pwm(client, nr);
324 	mutex_unlock(&data->update_lock);
325 	return count;
326 }
327 
show_pwm_enable(struct device * dev,struct device_attribute * attr,char * buf)328 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
329 		*attr, char *buf)
330 {
331 	int nr = to_sensor_dev_attr(attr)->index;
332 	struct f75375_data *data = f75375_update_device(dev);
333 	return sprintf(buf, "%d\n", data->pwm_enable[nr]);
334 }
335 
set_pwm_enable_direct(struct i2c_client * client,int nr,int val)336 static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
337 {
338 	struct f75375_data *data = i2c_get_clientdata(client);
339 	u8 fanmode;
340 
341 	if (val < 0 || val > 4)
342 		return -EINVAL;
343 
344 	fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
345 	if (data->kind == f75387) {
346 		/* For now, deny dangerous toggling of duty mode */
347 		if (duty_mode_enabled(data->pwm_enable[nr]) !=
348 				duty_mode_enabled(val))
349 			return -EOPNOTSUPP;
350 		/* clear each fanX_mode bit before setting them properly */
351 		fanmode &= ~(1 << F75387_FAN_DUTY_MODE(nr));
352 		fanmode &= ~(1 << F75387_FAN_MANU_MODE(nr));
353 		switch (val) {
354 		case 0: /* full speed */
355 			fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
356 			fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
357 			data->pwm[nr] = 255;
358 			break;
359 		case 1: /* PWM */
360 			fanmode  |= (1 << F75387_FAN_MANU_MODE(nr));
361 			fanmode  |= (1 << F75387_FAN_DUTY_MODE(nr));
362 			break;
363 		case 2: /* Automatic, speed mode */
364 			break;
365 		case 3: /* fan speed */
366 			fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
367 			break;
368 		case 4: /* Automatic, pwm */
369 			fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
370 			break;
371 		}
372 	} else {
373 		/* clear each fanX_mode bit before setting them properly */
374 		fanmode &= ~(3 << FAN_CTRL_MODE(nr));
375 		switch (val) {
376 		case 0: /* full speed */
377 			fanmode  |= (3 << FAN_CTRL_MODE(nr));
378 			data->pwm[nr] = 255;
379 			break;
380 		case 1: /* PWM */
381 			fanmode  |= (3 << FAN_CTRL_MODE(nr));
382 			break;
383 		case 2: /* AUTOMATIC*/
384 			fanmode  |= (1 << FAN_CTRL_MODE(nr));
385 			break;
386 		case 3: /* fan speed */
387 			break;
388 		case 4: /* Automatic pwm */
389 			return -EINVAL;
390 		}
391 	}
392 
393 	f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
394 	data->pwm_enable[nr] = val;
395 	if (val == 0)
396 		f75375_write_pwm(client, nr);
397 	return 0;
398 }
399 
set_pwm_enable(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)400 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
401 		const char *buf, size_t count)
402 {
403 	int nr = to_sensor_dev_attr(attr)->index;
404 	struct i2c_client *client = to_i2c_client(dev);
405 	struct f75375_data *data = i2c_get_clientdata(client);
406 	unsigned long val;
407 	int err;
408 
409 	err = kstrtoul(buf, 10, &val);
410 	if (err < 0)
411 		return err;
412 
413 	mutex_lock(&data->update_lock);
414 	err = set_pwm_enable_direct(client, nr, val);
415 	mutex_unlock(&data->update_lock);
416 	return err ? err : count;
417 }
418 
set_pwm_mode(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)419 static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
420 		const char *buf, size_t count)
421 {
422 	int nr = to_sensor_dev_attr(attr)->index;
423 	struct i2c_client *client = to_i2c_client(dev);
424 	struct f75375_data *data = i2c_get_clientdata(client);
425 	unsigned long val;
426 	int err;
427 	u8 conf;
428 	char reg, ctrl;
429 
430 	err = kstrtoul(buf, 10, &val);
431 	if (err < 0)
432 		return err;
433 
434 	if (!(val == 0 || val == 1))
435 		return -EINVAL;
436 
437 	/* F75373 does not support DC (linear voltage) fan control mode */
438 	if (data->kind == f75373 && val == 0)
439 		return -EINVAL;
440 
441 	/* take care for different registers */
442 	if (data->kind == f75387) {
443 		reg = F75375_REG_FAN_TIMER;
444 		ctrl = F75387_FAN_CTRL_LINEAR(nr);
445 	} else {
446 		reg = F75375_REG_CONFIG1;
447 		ctrl = F75375_FAN_CTRL_LINEAR(nr);
448 	}
449 
450 	mutex_lock(&data->update_lock);
451 	conf = f75375_read8(client, reg);
452 	conf &= ~(1 << ctrl);
453 
454 	if (val == 0)
455 		conf |= (1 << ctrl);
456 
457 	f75375_write8(client, reg, conf);
458 	data->pwm_mode[nr] = val;
459 	mutex_unlock(&data->update_lock);
460 	return count;
461 }
462 
show_pwm(struct device * dev,struct device_attribute * attr,char * buf)463 static ssize_t show_pwm(struct device *dev, struct device_attribute
464 		*attr, char *buf)
465 {
466 	int nr = to_sensor_dev_attr(attr)->index;
467 	struct f75375_data *data = f75375_update_device(dev);
468 	return sprintf(buf, "%d\n", data->pwm[nr]);
469 }
470 
show_pwm_mode(struct device * dev,struct device_attribute * attr,char * buf)471 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
472 		*attr, char *buf)
473 {
474 	int nr = to_sensor_dev_attr(attr)->index;
475 	struct f75375_data *data = f75375_update_device(dev);
476 	return sprintf(buf, "%d\n", data->pwm_mode[nr]);
477 }
478 
479 #define VOLT_FROM_REG(val) ((val) * 8)
480 #define VOLT_TO_REG(val) ((val) / 8)
481 
show_in(struct device * dev,struct device_attribute * attr,char * buf)482 static ssize_t show_in(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[nr]));
488 }
489 
show_in_max(struct device * dev,struct device_attribute * attr,char * buf)490 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
491 		char *buf)
492 {
493 	int nr = to_sensor_dev_attr(attr)->index;
494 	struct f75375_data *data = f75375_update_device(dev);
495 	return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
496 }
497 
show_in_min(struct device * dev,struct device_attribute * attr,char * buf)498 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
499 		char *buf)
500 {
501 	int nr = to_sensor_dev_attr(attr)->index;
502 	struct f75375_data *data = f75375_update_device(dev);
503 	return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
504 }
505 
set_in_max(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)506 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
507 		const char *buf, size_t count)
508 {
509 	int nr = to_sensor_dev_attr(attr)->index;
510 	struct i2c_client *client = to_i2c_client(dev);
511 	struct f75375_data *data = i2c_get_clientdata(client);
512 	unsigned long val;
513 	int err;
514 
515 	err = kstrtoul(buf, 10, &val);
516 	if (err < 0)
517 		return err;
518 
519 	val = clamp_val(VOLT_TO_REG(val), 0, 0xff);
520 	mutex_lock(&data->update_lock);
521 	data->in_max[nr] = val;
522 	f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
523 	mutex_unlock(&data->update_lock);
524 	return count;
525 }
526 
set_in_min(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)527 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
528 		const char *buf, size_t count)
529 {
530 	int nr = to_sensor_dev_attr(attr)->index;
531 	struct i2c_client *client = to_i2c_client(dev);
532 	struct f75375_data *data = i2c_get_clientdata(client);
533 	unsigned long val;
534 	int err;
535 
536 	err = kstrtoul(buf, 10, &val);
537 	if (err < 0)
538 		return err;
539 
540 	val = clamp_val(VOLT_TO_REG(val), 0, 0xff);
541 	mutex_lock(&data->update_lock);
542 	data->in_min[nr] = val;
543 	f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
544 	mutex_unlock(&data->update_lock);
545 	return count;
546 }
547 #define TEMP_FROM_REG(val) ((val) * 1000)
548 #define TEMP_TO_REG(val) ((val) / 1000)
549 #define TEMP11_FROM_REG(reg)	((reg) / 32 * 125)
550 
show_temp11(struct device * dev,struct device_attribute * attr,char * buf)551 static ssize_t show_temp11(struct device *dev, struct device_attribute *attr,
552 		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", TEMP11_FROM_REG(data->temp11[nr]));
557 }
558 
show_temp_max(struct device * dev,struct device_attribute * attr,char * buf)559 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
560 		char *buf)
561 {
562 	int nr = to_sensor_dev_attr(attr)->index;
563 	struct f75375_data *data = f75375_update_device(dev);
564 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
565 }
566 
show_temp_max_hyst(struct device * dev,struct device_attribute * attr,char * buf)567 static ssize_t show_temp_max_hyst(struct device *dev,
568 		struct device_attribute *attr, char *buf)
569 {
570 	int nr = to_sensor_dev_attr(attr)->index;
571 	struct f75375_data *data = f75375_update_device(dev);
572 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
573 }
574 
set_temp_max(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)575 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
576 		const char *buf, size_t count)
577 {
578 	int nr = to_sensor_dev_attr(attr)->index;
579 	struct i2c_client *client = to_i2c_client(dev);
580 	struct f75375_data *data = i2c_get_clientdata(client);
581 	unsigned long val;
582 	int err;
583 
584 	err = kstrtoul(buf, 10, &val);
585 	if (err < 0)
586 		return err;
587 
588 	val = clamp_val(TEMP_TO_REG(val), 0, 127);
589 	mutex_lock(&data->update_lock);
590 	data->temp_high[nr] = val;
591 	f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
592 	mutex_unlock(&data->update_lock);
593 	return count;
594 }
595 
set_temp_max_hyst(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)596 static ssize_t set_temp_max_hyst(struct device *dev,
597 	struct device_attribute *attr, const char *buf, size_t count)
598 {
599 	int nr = to_sensor_dev_attr(attr)->index;
600 	struct i2c_client *client = to_i2c_client(dev);
601 	struct f75375_data *data = i2c_get_clientdata(client);
602 	unsigned long val;
603 	int err;
604 
605 	err = kstrtoul(buf, 10, &val);
606 	if (err < 0)
607 		return err;
608 
609 	val = clamp_val(TEMP_TO_REG(val), 0, 127);
610 	mutex_lock(&data->update_lock);
611 	data->temp_max_hyst[nr] = val;
612 	f75375_write8(client, F75375_REG_TEMP_HYST(nr),
613 		data->temp_max_hyst[nr]);
614 	mutex_unlock(&data->update_lock);
615 	return count;
616 }
617 
618 #define show_fan(thing) \
619 static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
620 			char *buf)\
621 {\
622 	int nr = to_sensor_dev_attr(attr)->index;\
623 	struct f75375_data *data = f75375_update_device(dev); \
624 	return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
625 }
626 
627 show_fan(fan);
628 show_fan(fan_min);
629 show_fan(fan_max);
630 show_fan(fan_target);
631 
632 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
633 static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
634 	show_in_max, set_in_max, 0);
635 static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
636 	show_in_min, set_in_min, 0);
637 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
638 static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
639 	show_in_max, set_in_max, 1);
640 static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
641 	show_in_min, set_in_min, 1);
642 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
643 static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
644 	show_in_max, set_in_max, 2);
645 static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
646 	show_in_min, set_in_min, 2);
647 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
648 static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
649 	show_in_max, set_in_max, 3);
650 static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
651 	show_in_min, set_in_min, 3);
652 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 0);
653 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
654 	show_temp_max_hyst, set_temp_max_hyst, 0);
655 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
656 	show_temp_max, set_temp_max, 0);
657 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 1);
658 static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
659 	show_temp_max_hyst, set_temp_max_hyst, 1);
660 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
661 	show_temp_max, set_temp_max, 1);
662 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
663 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, show_fan_max, NULL, 0);
664 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
665 	show_fan_min, set_fan_min, 0);
666 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO|S_IWUSR,
667 	show_fan_target, set_fan_target, 0);
668 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
669 static SENSOR_DEVICE_ATTR(fan2_max, S_IRUGO, show_fan_max, NULL, 1);
670 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
671 	show_fan_min, set_fan_min, 1);
672 static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO|S_IWUSR,
673 	show_fan_target, set_fan_target, 1);
674 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
675 	show_pwm, set_pwm, 0);
676 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
677 	show_pwm_enable, set_pwm_enable, 0);
678 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
679 	show_pwm_mode, set_pwm_mode, 0);
680 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
681 	show_pwm, set_pwm, 1);
682 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
683 	show_pwm_enable, set_pwm_enable, 1);
684 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
685 	show_pwm_mode, set_pwm_mode, 1);
686 
687 static struct attribute *f75375_attributes[] = {
688 	&sensor_dev_attr_temp1_input.dev_attr.attr,
689 	&sensor_dev_attr_temp1_max.dev_attr.attr,
690 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
691 	&sensor_dev_attr_temp2_input.dev_attr.attr,
692 	&sensor_dev_attr_temp2_max.dev_attr.attr,
693 	&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
694 	&sensor_dev_attr_fan1_input.dev_attr.attr,
695 	&sensor_dev_attr_fan1_max.dev_attr.attr,
696 	&sensor_dev_attr_fan1_min.dev_attr.attr,
697 	&sensor_dev_attr_fan1_target.dev_attr.attr,
698 	&sensor_dev_attr_fan2_input.dev_attr.attr,
699 	&sensor_dev_attr_fan2_max.dev_attr.attr,
700 	&sensor_dev_attr_fan2_min.dev_attr.attr,
701 	&sensor_dev_attr_fan2_target.dev_attr.attr,
702 	&sensor_dev_attr_pwm1.dev_attr.attr,
703 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
704 	&sensor_dev_attr_pwm1_mode.dev_attr.attr,
705 	&sensor_dev_attr_pwm2.dev_attr.attr,
706 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
707 	&sensor_dev_attr_pwm2_mode.dev_attr.attr,
708 	&sensor_dev_attr_in0_input.dev_attr.attr,
709 	&sensor_dev_attr_in0_max.dev_attr.attr,
710 	&sensor_dev_attr_in0_min.dev_attr.attr,
711 	&sensor_dev_attr_in1_input.dev_attr.attr,
712 	&sensor_dev_attr_in1_max.dev_attr.attr,
713 	&sensor_dev_attr_in1_min.dev_attr.attr,
714 	&sensor_dev_attr_in2_input.dev_attr.attr,
715 	&sensor_dev_attr_in2_max.dev_attr.attr,
716 	&sensor_dev_attr_in2_min.dev_attr.attr,
717 	&sensor_dev_attr_in3_input.dev_attr.attr,
718 	&sensor_dev_attr_in3_max.dev_attr.attr,
719 	&sensor_dev_attr_in3_min.dev_attr.attr,
720 	NULL
721 };
722 
723 static const struct attribute_group f75375_group = {
724 	.attrs = f75375_attributes,
725 };
726 
f75375_init(struct i2c_client * client,struct f75375_data * data,struct f75375s_platform_data * f75375s_pdata)727 static void f75375_init(struct i2c_client *client, struct f75375_data *data,
728 		struct f75375s_platform_data *f75375s_pdata)
729 {
730 	int nr;
731 
732 	if (!f75375s_pdata) {
733 		u8 conf, mode;
734 		int nr;
735 
736 		conf = f75375_read8(client, F75375_REG_CONFIG1);
737 		mode = f75375_read8(client, F75375_REG_FAN_TIMER);
738 		for (nr = 0; nr < 2; nr++) {
739 			if (data->kind == f75387) {
740 				bool manu, duty;
741 
742 				if (!(mode & (1 << F75387_FAN_CTRL_LINEAR(nr))))
743 					data->pwm_mode[nr] = 1;
744 
745 				manu = ((mode >> F75387_FAN_MANU_MODE(nr)) & 1);
746 				duty = ((mode >> F75387_FAN_DUTY_MODE(nr)) & 1);
747 				if (!manu && duty)
748 					/* auto, pwm */
749 					data->pwm_enable[nr] = 4;
750 				else if (manu && !duty)
751 					/* manual, speed */
752 					data->pwm_enable[nr] = 3;
753 				else if (!manu && !duty)
754 					/* automatic, speed */
755 					data->pwm_enable[nr] = 2;
756 				else
757 					/* manual, pwm */
758 					data->pwm_enable[nr] = 1;
759 			} else {
760 				if (!(conf & (1 << F75375_FAN_CTRL_LINEAR(nr))))
761 					data->pwm_mode[nr] = 1;
762 
763 				switch ((mode >> FAN_CTRL_MODE(nr)) & 3) {
764 				case 0:		/* speed */
765 					data->pwm_enable[nr] = 3;
766 					break;
767 				case 1:		/* automatic */
768 					data->pwm_enable[nr] = 2;
769 					break;
770 				default:	/* manual */
771 					data->pwm_enable[nr] = 1;
772 					break;
773 				}
774 			}
775 		}
776 		return;
777 	}
778 
779 	set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
780 	set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
781 	for (nr = 0; nr < 2; nr++) {
782 		if (auto_mode_enabled(f75375s_pdata->pwm_enable[nr]) ||
783 		    !duty_mode_enabled(f75375s_pdata->pwm_enable[nr]))
784 			continue;
785 		data->pwm[nr] = clamp_val(f75375s_pdata->pwm[nr], 0, 255);
786 		f75375_write_pwm(client, nr);
787 	}
788 
789 }
790 
f75375_probe(struct i2c_client * client)791 static int f75375_probe(struct i2c_client *client)
792 {
793 	struct f75375_data *data;
794 	struct f75375s_platform_data *f75375s_pdata =
795 			dev_get_platdata(&client->dev);
796 	int err;
797 
798 	if (!i2c_check_functionality(client->adapter,
799 				I2C_FUNC_SMBUS_BYTE_DATA))
800 		return -EIO;
801 	data = devm_kzalloc(&client->dev, sizeof(struct f75375_data),
802 			    GFP_KERNEL);
803 	if (!data)
804 		return -ENOMEM;
805 
806 	i2c_set_clientdata(client, data);
807 	mutex_init(&data->update_lock);
808 	data->kind = (uintptr_t)i2c_get_match_data(client);
809 
810 	err = sysfs_create_group(&client->dev.kobj, &f75375_group);
811 	if (err)
812 		return err;
813 
814 	if (data->kind != f75373) {
815 		err = sysfs_chmod_file(&client->dev.kobj,
816 			&sensor_dev_attr_pwm1_mode.dev_attr.attr,
817 			S_IRUGO | S_IWUSR);
818 		if (err)
819 			goto exit_remove;
820 		err = sysfs_chmod_file(&client->dev.kobj,
821 			&sensor_dev_attr_pwm2_mode.dev_attr.attr,
822 			S_IRUGO | S_IWUSR);
823 		if (err)
824 			goto exit_remove;
825 	}
826 
827 	data->hwmon_dev = hwmon_device_register(&client->dev);
828 	if (IS_ERR(data->hwmon_dev)) {
829 		err = PTR_ERR(data->hwmon_dev);
830 		goto exit_remove;
831 	}
832 
833 	f75375_init(client, data, f75375s_pdata);
834 
835 	return 0;
836 
837 exit_remove:
838 	sysfs_remove_group(&client->dev.kobj, &f75375_group);
839 	return err;
840 }
841 
f75375_remove(struct i2c_client * client)842 static void f75375_remove(struct i2c_client *client)
843 {
844 	struct f75375_data *data = i2c_get_clientdata(client);
845 	hwmon_device_unregister(data->hwmon_dev);
846 	sysfs_remove_group(&client->dev.kobj, &f75375_group);
847 }
848 
849 /* Return 0 if detection is successful, -ENODEV otherwise */
f75375_detect(struct i2c_client * client,struct i2c_board_info * info)850 static int f75375_detect(struct i2c_client *client,
851 			 struct i2c_board_info *info)
852 {
853 	struct i2c_adapter *adapter = client->adapter;
854 	u16 vendid, chipid;
855 	u8 version;
856 	const char *name;
857 
858 	vendid = f75375_read16(client, F75375_REG_VENDOR);
859 	chipid = f75375_read16(client, F75375_CHIP_ID);
860 	if (vendid != 0x1934)
861 		return -ENODEV;
862 
863 	if (chipid == 0x0306)
864 		name = "f75375";
865 	else if (chipid == 0x0204)
866 		name = "f75373";
867 	else if (chipid == 0x0410)
868 		name = "f75387";
869 	else
870 		return -ENODEV;
871 
872 	version = f75375_read8(client, F75375_REG_VERSION);
873 	dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
874 	strscpy(info->type, name, I2C_NAME_SIZE);
875 
876 	return 0;
877 }
878 
879 static const struct i2c_device_id f75375_id[] = {
880 	{ "f75373", f75373 },
881 	{ "f75375", f75375 },
882 	{ "f75387", f75387 },
883 	{ }
884 };
885 MODULE_DEVICE_TABLE(i2c, f75375_id);
886 
887 static struct i2c_driver f75375_driver = {
888 	.class = I2C_CLASS_HWMON,
889 	.driver = {
890 		.name = "f75375",
891 	},
892 	.probe = f75375_probe,
893 	.remove = f75375_remove,
894 	.id_table = f75375_id,
895 	.detect = f75375_detect,
896 	.address_list = normal_i2c,
897 };
898 module_i2c_driver(f75375_driver);
899 
900 MODULE_AUTHOR("Riku Voipio");
901 MODULE_LICENSE("GPL");
902 MODULE_DESCRIPTION("F75373/F75375/F75387 hardware monitoring driver");
903