blob: 3e567be60fb158e9d2763468235ae2410337cbc3 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Riku Voipio84f1e442007-08-24 13:03:09 +03002/*
Björn Gerhartf58c44e2011-12-13 13:19:58 -05003 * f75375s.c - driver for the Fintek F75375/SP, F75373 and
4 * F75387SG/RG hardware monitoring features
Riku Voipiob26e0ed2009-03-03 21:37:17 +02005 * Copyright (C) 2006-2007 Riku Voipio
Riku Voipio84f1e442007-08-24 13:03:09 +03006 *
7 * Datasheets available at:
8 *
9 * f75375:
Guenter Roeck4fd826e2011-12-08 09:54:18 -080010 * http://www.fintek.com.tw/files/productfiles/F75375_V026P.pdf
Riku Voipio84f1e442007-08-24 13:03:09 +030011 *
12 * f75373:
Justin P. Mattock631dd1a2010-10-18 11:03:14 +020013 * http://www.fintek.com.tw/files/productfiles/F75373_V025P.pdf
Riku Voipio84f1e442007-08-24 13:03:09 +030014 *
Björn Gerhartf58c44e2011-12-13 13:19:58 -050015 * f75387:
16 * http://www.fintek.com.tw/files/productfiles/F75387_V027P.pdf
Riku Voipio84f1e442007-08-24 13:03:09 +030017 */
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>
Riku Voipioff312d02007-09-26 13:14:40 +030026#include <linux/f75375s.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Riku Voipio84f1e442007-08-24 13:03:09 +030028
29/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050030static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
Riku Voipio84f1e442007-08-24 13:03:09 +030031
Björn Gerhartf58c44e2011-12-13 13:19:58 -050032enum chips { f75373, f75375, f75387 };
Riku Voipio84f1e442007-08-24 13:03:09 +030033
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))
Björn Gerhartf58c44e2011-12-13 13:19:58 -050051#define F75387_REG_TEMP11_LSB(nr) (0x1a + (nr))
Riku Voipio84f1e442007-08-24 13:03:09 +030052#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
Björn Gerhartf58c44e2011-12-13 13:19:58 -050071#define F75375_FAN_CTRL_LINEAR(nr) (4 + nr)
72#define F75387_FAN_CTRL_LINEAR(nr) (1 + ((nr) * 4))
Guillem Jover96f36402010-09-17 17:24:11 +020073#define FAN_CTRL_MODE(nr) (4 + ((nr) * 2))
Björn Gerhartf58c44e2011-12-13 13:19:58 -050074#define F75387_FAN_DUTY_MODE(nr) (2 + ((nr) * 4))
75#define F75387_FAN_MANU_MODE(nr) ((nr) * 4)
Riku Voipio84f1e442007-08-24 13:03:09 +030076
77/*
78 * Data structures and manipulation thereof
79 */
80
81struct f75375_data {
82 unsigned short addr;
Tony Jones1beeffe2007-08-20 13:46:20 -070083 struct device *hwmon_dev;
Riku Voipio84f1e442007-08-24 13:03:09 +030084
85 const char *name;
86 int kind;
87 struct mutex update_lock; /* protect register access */
88 char 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];
Guenter Roeck740f6be32011-12-08 09:59:25 -080098 u16 fan_max[2];
99 u16 fan_target[2];
Riku Voipio84f1e442007-08-24 13:03:09 +0300100 u8 fan_timer;
101 u8 pwm[2];
102 u8 pwm_mode[2];
103 u8 pwm_enable[2];
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500104 /*
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];
Riku Voipio84f1e442007-08-24 13:03:09 +0300110 s8 temp_high[2];
111 s8 temp_max_hyst[2];
112};
113
Jean Delvare310ec792009-12-14 21:17:23 +0100114static int f75375_detect(struct i2c_client *client,
Jean Delvare935ada82008-07-16 19:30:11 +0200115 struct i2c_board_info *info);
Stephen Kittf7bd7b52020-08-21 18:01:59 +0200116static int f75375_probe(struct i2c_client *client);
Riku Voipio620c1422007-10-16 12:46:49 +0300117static int f75375_remove(struct i2c_client *client);
118
Jean Delvare3760f732008-04-29 23:11:40 +0200119static const struct i2c_device_id f75375_id[] = {
120 { "f75373", f75373 },
121 { "f75375", f75375 },
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500122 { "f75387", f75387 },
Jean Delvare3760f732008-04-29 23:11:40 +0200123 { }
124};
125MODULE_DEVICE_TABLE(i2c, f75375_id);
126
Riku Voipio84f1e442007-08-24 13:03:09 +0300127static struct i2c_driver f75375_driver = {
Jean Delvare935ada82008-07-16 19:30:11 +0200128 .class = I2C_CLASS_HWMON,
Riku Voipio84f1e442007-08-24 13:03:09 +0300129 .driver = {
130 .name = "f75375",
131 },
Stephen Kittf7bd7b52020-08-21 18:01:59 +0200132 .probe_new = f75375_probe,
Riku Voipio620c1422007-10-16 12:46:49 +0300133 .remove = f75375_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200134 .id_table = f75375_id,
Jean Delvare935ada82008-07-16 19:30:11 +0200135 .detect = f75375_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100136 .address_list = normal_i2c,
Riku Voipio84f1e442007-08-24 13:03:09 +0300137};
138
139static 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 */
145static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
146{
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800147 return (i2c_smbus_read_byte_data(client, reg) << 8)
148 | i2c_smbus_read_byte_data(client, reg + 1);
Riku Voipio84f1e442007-08-24 13:03:09 +0300149}
150
151static 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
157static inline void f75375_write16(struct i2c_client *client, u8 reg,
158 u16 value)
159{
Nikolaus Schulzeb2f2552012-02-08 18:56:10 +0100160 int err = i2c_smbus_write_byte_data(client, reg, (value >> 8));
Riku Voipio84f1e442007-08-24 13:03:09 +0300161 if (err)
162 return;
163 i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
164}
165
Nikolaus Schulz331255d2012-02-28 22:15:51 +0100166static 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
Riku Voipio84f1e442007-08-24 13:03:09 +0300176static 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));
Guenter Roeck740f6be32011-12-08 09:59:25 -0800192 data->fan_max[nr] =
Riku Voipio84f1e442007-08-24 13:03:09 +0300193 f75375_read16(client, F75375_REG_FAN_FULL(nr));
194 data->fan_min[nr] =
195 f75375_read16(client, F75375_REG_FAN_MIN(nr));
Guenter Roeck740f6be32011-12-08 09:59:25 -0800196 data->fan_target[nr] =
Riku Voipio84f1e442007-08-24 13:03:09 +0300197 f75375_read16(client, F75375_REG_FAN_EXP(nr));
Riku Voipio84f1e442007-08-24 13:03:09 +0300198 }
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++) {
Nikolaus Schulza1c1baf2012-02-08 18:56:11 +0100213 data->pwm[nr] = f75375_read8(client,
214 F75375_REG_FAN_PWM_DUTY(nr));
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500215 /* 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));
Riku Voipio84f1e442007-08-24 13:03:09 +0300223 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 = 1;
232 }
233
234 mutex_unlock(&data->update_lock);
235 return data;
236}
237
238static inline u16 rpm_from_reg(u16 reg)
239{
240 if (reg == 0 || reg == 0xffff)
241 return 0;
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800242 return 1500000 / reg;
Riku Voipio84f1e442007-08-24 13:03:09 +0300243}
244
245static inline u16 rpm_to_reg(int rpm)
246{
247 if (rpm < 367 || rpm > 0xffff)
248 return 0xffff;
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800249 return 1500000 / rpm;
Riku Voipio84f1e442007-08-24 13:03:09 +0300250}
251
Nikolaus Schulzb17d6562012-02-28 16:15:53 -0500252static 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:
Guenter Roeck39b103b2013-09-14 00:43:12 -0700263 WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable);
Guenter Roeck63940112012-03-24 08:38:21 -0700264 return true;
Nikolaus Schulzb17d6562012-02-28 16:15:53 -0500265 }
266}
267
Nikolaus Schulz15d1ad02012-02-28 16:15:54 -0500268static 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:
Guenter Roeck39b103b2013-09-14 00:43:12 -0700279 WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable);
Guenter Roeck63940112012-03-24 08:38:21 -0700280 return false;
Nikolaus Schulz15d1ad02012-02-28 16:15:54 -0500281 }
282}
283
Riku Voipio84f1e442007-08-24 13:03:09 +0300284static 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);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800290 unsigned long val;
291 int err;
292
293 err = kstrtoul(buf, 10, &val);
294 if (err < 0)
295 return err;
Riku Voipio84f1e442007-08-24 13:03:09 +0300296
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
Guenter Roeck740f6be32011-12-08 09:59:25 -0800304static ssize_t set_fan_target(struct device *dev, struct device_attribute *attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300305 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);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800310 unsigned long val;
311 int err;
312
313 err = kstrtoul(buf, 10, &val);
314 if (err < 0)
315 return err;
Riku Voipio84f1e442007-08-24 13:03:09 +0300316
Nikolaus Schulz15d1ad02012-02-28 16:15:54 -0500317 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
Riku Voipio84f1e442007-08-24 13:03:09 +0300322 mutex_lock(&data->update_lock);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800323 data->fan_target[nr] = rpm_to_reg(val);
324 f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_target[nr]);
Riku Voipio84f1e442007-08-24 13:03:09 +0300325 mutex_unlock(&data->update_lock);
326 return count;
327}
328
329static 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);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800335 unsigned long val;
336 int err;
337
338 err = kstrtoul(buf, 10, &val);
339 if (err < 0)
340 return err;
Riku Voipio84f1e442007-08-24 13:03:09 +0300341
Nikolaus Schulz15d1ad02012-02-28 16:15:54 -0500342 if (auto_mode_enabled(data->pwm_enable[nr]) ||
343 !duty_mode_enabled(data->pwm_enable[nr]))
344 return -EINVAL;
345
Riku Voipio84f1e442007-08-24 13:03:09 +0300346 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800347 data->pwm[nr] = clamp_val(val, 0, 255);
Nikolaus Schulz331255d2012-02-28 22:15:51 +0100348 f75375_write_pwm(client, nr);
Riku Voipio84f1e442007-08-24 13:03:09 +0300349 mutex_unlock(&data->update_lock);
350 return count;
351}
352
353static 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
Riku Voipioff312d02007-09-26 13:14:40 +0300361static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
Riku Voipio84f1e442007-08-24 13:03:09 +0300362{
Riku Voipio84f1e442007-08-24 13:03:09 +0300363 struct f75375_data *data = i2c_get_clientdata(client);
Riku Voipio84f1e442007-08-24 13:03:09 +0300364 u8 fanmode;
365
Nikolaus Schulzb17d6562012-02-28 16:15:53 -0500366 if (val < 0 || val > 4)
Riku Voipio84f1e442007-08-24 13:03:09 +0300367 return -EINVAL;
368
Riku Voipio84f1e442007-08-24 13:03:09 +0300369 fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500370 if (data->kind == f75387) {
Nikolaus Schulzb17d6562012-02-28 16:15:53 -0500371 /* 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;
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500375 /* 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;
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500383 break;
384 case 1: /* PWM */
385 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
386 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
387 break;
Nikolaus Schulzb17d6562012-02-28 16:15:53 -0500388 case 2: /* Automatic, speed mode */
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500389 break;
390 case 3: /* fan speed */
391 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
392 break;
Nikolaus Schulzb17d6562012-02-28 16:15:53 -0500393 case 4: /* Automatic, pwm */
394 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
395 break;
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500396 }
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;
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500404 break;
405 case 1: /* PWM */
406 fanmode |= (3 << FAN_CTRL_MODE(nr));
407 break;
408 case 2: /* AUTOMATIC*/
Nikolaus Schulz09e87e52012-02-08 18:56:08 +0100409 fanmode |= (1 << FAN_CTRL_MODE(nr));
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500410 break;
411 case 3: /* fan speed */
412 break;
Nikolaus Schulzb17d6562012-02-28 16:15:53 -0500413 case 4: /* Automatic pwm */
414 return -EINVAL;
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500415 }
Riku Voipio84f1e442007-08-24 13:03:09 +0300416 }
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500417
Riku Voipio84f1e442007-08-24 13:03:09 +0300418 f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
419 data->pwm_enable[nr] = val;
Nikolaus Schulzc1c1a3d2012-02-22 23:18:44 +0100420 if (val == 0)
Nikolaus Schulz331255d2012-02-28 22:15:51 +0100421 f75375_write_pwm(client, nr);
Riku Voipioff312d02007-09-26 13:14:40 +0300422 return 0;
423}
424
425static 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);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800431 unsigned long val;
432 int err;
433
434 err = kstrtoul(buf, 10, &val);
435 if (err < 0)
436 return err;
Riku Voipioff312d02007-09-26 13:14:40 +0300437
438 mutex_lock(&data->update_lock);
439 err = set_pwm_enable_direct(client, nr, val);
Riku Voipio84f1e442007-08-24 13:03:09 +0300440 mutex_unlock(&data->update_lock);
Riku Voipioff312d02007-09-26 13:14:40 +0300441 return err ? err : count;
Riku Voipio84f1e442007-08-24 13:03:09 +0300442}
443
444static 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);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800450 unsigned long val;
451 int err;
452 u8 conf;
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500453 char reg, ctrl;
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800454
455 err = kstrtoul(buf, 10, &val);
456 if (err < 0)
457 return err;
Riku Voipio84f1e442007-08-24 13:03:09 +0300458
Riku Voipio76e83be2007-10-26 14:14:23 +0300459 if (!(val == 0 || val == 1))
Riku Voipio84f1e442007-08-24 13:03:09 +0300460 return -EINVAL;
461
Guenter Roeck5cd32222011-12-08 10:55:16 -0800462 /* F75373 does not support DC (linear voltage) fan control mode */
463 if (data->kind == f75373 && val == 0)
464 return -EINVAL;
465
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500466 /* 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
Riku Voipio84f1e442007-08-24 13:03:09 +0300475 mutex_lock(&data->update_lock);
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500476 conf = f75375_read8(client, reg);
477 conf &= ~(1 << ctrl);
Riku Voipio84f1e442007-08-24 13:03:09 +0300478
479 if (val == 0)
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500480 conf |= (1 << ctrl);
Riku Voipio84f1e442007-08-24 13:03:09 +0300481
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500482 f75375_write8(client, reg, conf);
Riku Voipio84f1e442007-08-24 13:03:09 +0300483 data->pwm_mode[nr] = val;
484 mutex_unlock(&data->update_lock);
485 return count;
486}
487
488static 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
496static 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
507static 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
515static 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
523static 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
531static 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);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800537 unsigned long val;
538 int err;
539
540 err = kstrtoul(buf, 10, &val);
541 if (err < 0)
542 return err;
543
Guenter Roeck2a844c12013-01-09 08:09:34 -0800544 val = clamp_val(VOLT_TO_REG(val), 0, 0xff);
Riku Voipio84f1e442007-08-24 13:03:09 +0300545 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
552static 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);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800558 unsigned long val;
559 int err;
560
561 err = kstrtoul(buf, 10, &val);
562 if (err < 0)
563 return err;
564
Guenter Roeck2a844c12013-01-09 08:09:34 -0800565 val = clamp_val(VOLT_TO_REG(val), 0, 0xff);
Riku Voipio84f1e442007-08-24 13:03:09 +0300566 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)
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500574#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
Riku Voipio84f1e442007-08-24 13:03:09 +0300575
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500576static ssize_t show_temp11(struct device *dev, struct device_attribute *attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300577 char *buf)
578{
579 int nr = to_sensor_dev_attr(attr)->index;
580 struct f75375_data *data = f75375_update_device(dev);
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500581 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[nr]));
Riku Voipio84f1e442007-08-24 13:03:09 +0300582}
583
584static 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
592static 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
600static 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);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800606 unsigned long val;
607 int err;
608
609 err = kstrtoul(buf, 10, &val);
610 if (err < 0)
611 return err;
612
Guenter Roeck2a844c12013-01-09 08:09:34 -0800613 val = clamp_val(TEMP_TO_REG(val), 0, 127);
Riku Voipio84f1e442007-08-24 13:03:09 +0300614 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
621static 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);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800627 unsigned long val;
628 int err;
629
630 err = kstrtoul(buf, 10, &val);
631 if (err < 0)
632 return err;
633
Guenter Roeck2a844c12013-01-09 08:09:34 -0800634 val = clamp_val(TEMP_TO_REG(val), 0, 127);
Riku Voipio84f1e442007-08-24 13:03:09 +0300635 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) \
644static 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
652show_fan(fan);
653show_fan(fan_min);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800654show_fan(fan_max);
655show_fan(fan_target);
Riku Voipio84f1e442007-08-24 13:03:09 +0300656
657static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
658static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
659 show_in_max, set_in_max, 0);
660static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
661 show_in_min, set_in_min, 0);
662static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
663static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
664 show_in_max, set_in_max, 1);
665static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
666 show_in_min, set_in_min, 1);
667static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
668static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
669 show_in_max, set_in_max, 2);
670static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
671 show_in_min, set_in_min, 2);
672static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
673static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
674 show_in_max, set_in_max, 3);
675static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
676 show_in_min, set_in_min, 3);
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500677static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 0);
Riku Voipio84f1e442007-08-24 13:03:09 +0300678static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
679 show_temp_max_hyst, set_temp_max_hyst, 0);
680static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
681 show_temp_max, set_temp_max, 0);
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500682static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 1);
Riku Voipio84f1e442007-08-24 13:03:09 +0300683static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
684 show_temp_max_hyst, set_temp_max_hyst, 1);
685static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
686 show_temp_max, set_temp_max, 1);
687static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800688static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, show_fan_max, NULL, 0);
Riku Voipio84f1e442007-08-24 13:03:09 +0300689static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
690 show_fan_min, set_fan_min, 0);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800691static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO|S_IWUSR,
692 show_fan_target, set_fan_target, 0);
Riku Voipio84f1e442007-08-24 13:03:09 +0300693static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800694static SENSOR_DEVICE_ATTR(fan2_max, S_IRUGO, show_fan_max, NULL, 1);
Riku Voipio84f1e442007-08-24 13:03:09 +0300695static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
696 show_fan_min, set_fan_min, 1);
Guenter Roeck740f6be32011-12-08 09:59:25 -0800697static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO|S_IWUSR,
698 show_fan_target, set_fan_target, 1);
Riku Voipio84f1e442007-08-24 13:03:09 +0300699static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
700 show_pwm, set_pwm, 0);
701static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
702 show_pwm_enable, set_pwm_enable, 0);
Riku Voipio76e83be2007-10-26 14:14:23 +0300703static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
Riku Voipio84f1e442007-08-24 13:03:09 +0300704 show_pwm_mode, set_pwm_mode, 0);
705static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
706 show_pwm, set_pwm, 1);
707static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
708 show_pwm_enable, set_pwm_enable, 1);
Riku Voipio76e83be2007-10-26 14:14:23 +0300709static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
Riku Voipio84f1e442007-08-24 13:03:09 +0300710 show_pwm_mode, set_pwm_mode, 1);
711
712static 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,
Guenter Roeck740f6be32011-12-08 09:59:25 -0800720 &sensor_dev_attr_fan1_max.dev_attr.attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300721 &sensor_dev_attr_fan1_min.dev_attr.attr,
Guenter Roeck740f6be32011-12-08 09:59:25 -0800722 &sensor_dev_attr_fan1_target.dev_attr.attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300723 &sensor_dev_attr_fan2_input.dev_attr.attr,
Guenter Roeck740f6be32011-12-08 09:59:25 -0800724 &sensor_dev_attr_fan2_max.dev_attr.attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300725 &sensor_dev_attr_fan2_min.dev_attr.attr,
Guenter Roeck740f6be32011-12-08 09:59:25 -0800726 &sensor_dev_attr_fan2_target.dev_attr.attr,
Riku Voipio84f1e442007-08-24 13:03:09 +0300727 &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
748static const struct attribute_group f75375_group = {
749 .attrs = f75375_attributes,
750};
751
Riku Voipioff312d02007-09-26 13:14:40 +0300752static void f75375_init(struct i2c_client *client, struct f75375_data *data,
753 struct f75375s_platform_data *f75375s_pdata)
754{
755 int nr;
Guenter Roeckb1b561a2011-12-08 10:15:51 -0800756
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++) {
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500764 if (data->kind == f75387) {
765 bool manu, duty;
766
Nikolaus Schulza367a1e2012-02-08 18:56:09 +0100767 if (!(mode & (1 << F75387_FAN_CTRL_LINEAR(nr))))
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500768 data->pwm_mode[nr] = 1;
769
770 manu = ((mode >> F75387_FAN_MANU_MODE(nr)) & 1);
771 duty = ((mode >> F75387_FAN_DUTY_MODE(nr)) & 1);
Nikolaus Schulzb17d6562012-02-28 16:15:53 -0500772 if (!manu && duty)
773 /* auto, pwm */
774 data->pwm_enable[nr] = 4;
775 else if (manu && !duty)
776 /* manual, speed */
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500777 data->pwm_enable[nr] = 3;
Nikolaus Schulzb17d6562012-02-28 16:15:53 -0500778 else if (!manu && !duty)
779 /* automatic, speed */
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500780 data->pwm_enable[nr] = 2;
781 else
Nikolaus Schulzb17d6562012-02-28 16:15:53 -0500782 /* manual, pwm */
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500783 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 }
Guenter Roeckb1b561a2011-12-08 10:15:51 -0800799 }
800 }
801 return;
802 }
803
Riku Voipioff312d02007-09-26 13:14:40 +0300804 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++) {
Nikolaus Schulz15d1ad02012-02-28 16:15:54 -0500807 if (auto_mode_enabled(f75375s_pdata->pwm_enable[nr]) ||
808 !duty_mode_enabled(f75375s_pdata->pwm_enable[nr]))
809 continue;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800810 data->pwm[nr] = clamp_val(f75375s_pdata->pwm[nr], 0, 255);
Nikolaus Schulz331255d2012-02-28 22:15:51 +0100811 f75375_write_pwm(client, nr);
Riku Voipioff312d02007-09-26 13:14:40 +0300812 }
813
814}
815
Stephen Kittf7bd7b52020-08-21 18:01:59 +0200816static int f75375_probe(struct i2c_client *client)
Riku Voipio620c1422007-10-16 12:46:49 +0300817{
Andrew Klossner51b3e272009-03-12 13:36:39 +0100818 struct f75375_data *data;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +0900819 struct f75375s_platform_data *f75375s_pdata =
820 dev_get_platdata(&client->dev);
Riku Voipio620c1422007-10-16 12:46:49 +0300821 int err;
822
823 if (!i2c_check_functionality(client->adapter,
824 I2C_FUNC_SMBUS_BYTE_DATA))
825 return -EIO;
Guenter Roeck05639bc2012-06-02 09:58:05 -0700826 data = devm_kzalloc(&client->dev, sizeof(struct f75375_data),
827 GFP_KERNEL);
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800828 if (!data)
Riku Voipio620c1422007-10-16 12:46:49 +0300829 return -ENOMEM;
830
831 i2c_set_clientdata(client, data);
Riku Voipio620c1422007-10-16 12:46:49 +0300832 mutex_init(&data->update_lock);
Stephen Kittf7bd7b52020-08-21 18:01:59 +0200833 data->kind = i2c_match_id(f75375_id, client)->driver_data;
Riku Voipio620c1422007-10-16 12:46:49 +0300834
Guenter Roeck4fd826e2011-12-08 09:54:18 -0800835 err = sysfs_create_group(&client->dev.kobj, &f75375_group);
836 if (err)
Guenter Roeck05639bc2012-06-02 09:58:05 -0700837 return err;
Riku Voipio620c1422007-10-16 12:46:49 +0300838
Nikolaus Schulzedeea102012-02-28 22:15:52 +0100839 if (data->kind != f75373) {
Riku Voipio76e83be2007-10-26 14:14:23 +0300840 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
Riku Voipio620c1422007-10-16 12:46:49 +0300852 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
Guenter Roeckb1b561a2011-12-08 10:15:51 -0800858 f75375_init(client, data, f75375s_pdata);
Riku Voipioff312d02007-09-26 13:14:40 +0300859
Riku Voipio620c1422007-10-16 12:46:49 +0300860 return 0;
861
862exit_remove:
863 sysfs_remove_group(&client->dev.kobj, &f75375_group);
Riku Voipio620c1422007-10-16 12:46:49 +0300864 return err;
865}
866
867static int 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);
Riku Voipio84f1e442007-08-24 13:03:09 +0300872 return 0;
873}
874
Jean Delvare935ada82008-07-16 19:30:11 +0200875/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100876static int f75375_detect(struct i2c_client *client,
Jean Delvare935ada82008-07-16 19:30:11 +0200877 struct i2c_board_info *info)
Riku Voipio84f1e442007-08-24 13:03:09 +0300878{
Jean Delvare935ada82008-07-16 19:30:11 +0200879 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100880 u16 vendid, chipid;
881 u8 version;
882 const char *name;
Riku Voipio84f1e442007-08-24 13:03:09 +0300883
Jean Delvare52df6442009-12-09 20:35:57 +0100884 vendid = f75375_read16(client, F75375_REG_VENDOR);
885 chipid = f75375_read16(client, F75375_CHIP_ID);
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500886 if (vendid != 0x1934)
887 return -ENODEV;
888
889 if (chipid == 0x0306)
Riku Voipio84f1e442007-08-24 13:03:09 +0300890 name = "f75375";
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500891 else if (chipid == 0x0204)
Riku Voipio84f1e442007-08-24 13:03:09 +0300892 name = "f75373";
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500893 else if (chipid == 0x0410)
894 name = "f75387";
Jean Delvare52df6442009-12-09 20:35:57 +0100895 else
896 return -ENODEV;
897
898 version = f75375_read8(client, F75375_REG_VERSION);
Riku Voipio84f1e442007-08-24 13:03:09 +0300899 dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
Jean Delvare935ada82008-07-16 19:30:11 +0200900 strlcpy(info->type, name, I2C_NAME_SIZE);
Riku Voipio84f1e442007-08-24 13:03:09 +0300901
Riku Voipio84f1e442007-08-24 13:03:09 +0300902 return 0;
Riku Voipio84f1e442007-08-24 13:03:09 +0300903}
904
Axel Linf0967ee2012-01-20 15:38:18 +0800905module_i2c_driver(f75375_driver);
Riku Voipio84f1e442007-08-24 13:03:09 +0300906
Riku Voipiob26e0ed2009-03-03 21:37:17 +0200907MODULE_AUTHOR("Riku Voipio");
Riku Voipio84f1e442007-08-24 13:03:09 +0300908MODULE_LICENSE("GPL");
Björn Gerhartf58c44e2011-12-13 13:19:58 -0500909MODULE_DESCRIPTION("F75373/F75375/F75387 hardware monitoring driver");