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