blob: a30f34cf512c2b96a2064f2fca92581fae4a01b3 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07002/*
3 * A hwmon driver for the Analog Devices ADT7470
4 * Copyright (C) 2007 IBM
5 *
Darrick J. Wong5407e0512013-08-26 15:42:27 -07006 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07007 */
8
Joe Perches2e991202010-10-20 06:51:27 +00009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070011#include <linux/module.h>
12#include <linux/jiffies.h>
13#include <linux/i2c.h>
14#include <linux/hwmon.h>
15#include <linux/hwmon-sysfs.h>
16#include <linux/err.h>
17#include <linux/mutex.h>
18#include <linux/delay.h>
19#include <linux/log2.h>
Darrick J. Wong89fac112009-01-06 14:41:34 -080020#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Joshua Scottaa18cc92016-08-08 13:35:45 +120022#include <linux/util_macros.h>
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070023
24/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050025static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070026
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070027/* ADT7470 registers */
28#define ADT7470_REG_BASE_ADDR 0x20
29#define ADT7470_REG_TEMP_BASE_ADDR 0x20
30#define ADT7470_REG_TEMP_MAX_ADDR 0x29
31#define ADT7470_REG_FAN_BASE_ADDR 0x2A
32#define ADT7470_REG_FAN_MAX_ADDR 0x31
33#define ADT7470_REG_PWM_BASE_ADDR 0x32
34#define ADT7470_REG_PWM_MAX_ADDR 0x35
35#define ADT7470_REG_PWM_MAX_BASE_ADDR 0x38
36#define ADT7470_REG_PWM_MAX_MAX_ADDR 0x3B
37#define ADT7470_REG_CFG 0x40
38#define ADT7470_FSPD_MASK 0x04
39#define ADT7470_REG_ALARM1 0x41
Darrick J. Wongfe03f282007-12-19 14:11:25 -080040#define ADT7470_R1T_ALARM 0x01
41#define ADT7470_R2T_ALARM 0x02
42#define ADT7470_R3T_ALARM 0x04
43#define ADT7470_R4T_ALARM 0x08
44#define ADT7470_R5T_ALARM 0x10
45#define ADT7470_R6T_ALARM 0x20
46#define ADT7470_R7T_ALARM 0x40
47#define ADT7470_OOL_ALARM 0x80
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070048#define ADT7470_REG_ALARM2 0x42
Darrick J. Wongfe03f282007-12-19 14:11:25 -080049#define ADT7470_R8T_ALARM 0x01
50#define ADT7470_R9T_ALARM 0x02
51#define ADT7470_R10T_ALARM 0x04
52#define ADT7470_FAN1_ALARM 0x10
53#define ADT7470_FAN2_ALARM 0x20
54#define ADT7470_FAN3_ALARM 0x40
55#define ADT7470_FAN4_ALARM 0x80
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070056#define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44
57#define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57
58#define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58
59#define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F
60#define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60
61#define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67
62#define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68
63#define ADT7470_REG_PWM12_CFG 0x68
64#define ADT7470_PWM2_AUTO_MASK 0x40
65#define ADT7470_PWM1_AUTO_MASK 0x80
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -080066#define ADT7470_PWM_AUTO_MASK 0xC0
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070067#define ADT7470_REG_PWM34_CFG 0x69
68#define ADT7470_PWM3_AUTO_MASK 0x40
69#define ADT7470_PWM4_AUTO_MASK 0x80
70#define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A
71#define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D
72#define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E
73#define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71
Joshua Scottaa18cc92016-08-08 13:35:45 +120074#define ADT7470_REG_CFG_2 0x74
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070075#define ADT7470_REG_ACOUSTICS12 0x75
76#define ADT7470_REG_ACOUSTICS34 0x76
77#define ADT7470_REG_DEVICE 0x3D
78#define ADT7470_REG_VENDOR 0x3E
79#define ADT7470_REG_REVISION 0x3F
80#define ADT7470_REG_ALARM1_MASK 0x72
81#define ADT7470_REG_ALARM2_MASK 0x73
82#define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C
83#define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D
84#define ADT7470_REG_MAX_ADDR 0x81
85
86#define ADT7470_TEMP_COUNT 10
87#define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x))
88#define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
89#define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
90 ((x) * 2) + 1)
91
92#define ADT7470_FAN_COUNT 4
93#define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
94#define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
95#define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
96
97#define ADT7470_PWM_COUNT 4
98#define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x))
99#define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
100#define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
101#define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
102#define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
103#define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
104 ((x) / 2))
105
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800106#define ALARM2(x) ((x) << 8)
107
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700108#define ADT7470_VENDOR 0x41
109#define ADT7470_DEVICE 0x70
110/* datasheet only mentions a revision 2 */
111#define ADT7470_REVISION 0x02
112
113/* "all temps" according to hwmon sysfs interface spec */
114#define ADT7470_PWM_ALL_TEMPS 0x3FF
115
116/* How often do we reread sensors values? (In jiffies) */
117#define SENSOR_REFRESH_INTERVAL (5 * HZ)
118
119/* How often do we reread sensor limit values? (In jiffies) */
120#define LIMIT_REFRESH_INTERVAL (60 * HZ)
121
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800122/* Wait at least 200ms per sensor for 10 sensors */
123#define TEMP_COLLECTION_TIME 2000
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700124
Darrick J. Wong89fac112009-01-06 14:41:34 -0800125/* auto update thing won't fire more than every 2s */
126#define AUTO_UPDATE_INTERVAL 2000
127
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700128/* datasheet says to divide this number by the fan reading to get fan rpm */
129#define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
130#define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
131#define FAN_PERIOD_INVALID 65535
132#define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
133
Joshua Scottaa18cc92016-08-08 13:35:45 +1200134/* Config registers 1 and 2 include fields for selecting the PWM frequency */
135#define ADT7470_CFG_LF 0x40
136#define ADT7470_FREQ_MASK 0x70
137#define ADT7470_FREQ_SHIFT 4
138
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700139struct adt7470_data {
Axel Lin30485772014-07-16 23:12:54 +0800140 struct i2c_client *client;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700141 struct mutex lock;
142 char sensors_valid;
143 char limits_valid;
144 unsigned long sensors_last_updated; /* In jiffies */
145 unsigned long limits_last_updated; /* In jiffies */
146
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800147 int num_temp_sensors; /* -1 = probe */
Darrick J. Wong89fac112009-01-06 14:41:34 -0800148 int temperatures_probed;
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800149
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700150 s8 temp[ADT7470_TEMP_COUNT];
151 s8 temp_min[ADT7470_TEMP_COUNT];
152 s8 temp_max[ADT7470_TEMP_COUNT];
153 u16 fan[ADT7470_FAN_COUNT];
154 u16 fan_min[ADT7470_FAN_COUNT];
155 u16 fan_max[ADT7470_FAN_COUNT];
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800156 u16 alarm;
157 u16 alarms_mask;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700158 u8 force_pwm_max;
159 u8 pwm[ADT7470_PWM_COUNT];
160 u8 pwm_max[ADT7470_PWM_COUNT];
161 u8 pwm_automatic[ADT7470_PWM_COUNT];
162 u8 pwm_min[ADT7470_PWM_COUNT];
163 s8 pwm_tmin[ADT7470_PWM_COUNT];
164 u8 pwm_auto_temp[ADT7470_PWM_COUNT];
Darrick J. Wong89fac112009-01-06 14:41:34 -0800165
166 struct task_struct *auto_update;
Darrick J. Wong89fac112009-01-06 14:41:34 -0800167 unsigned int auto_update_interval;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700168};
169
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700170/*
171 * 16-bit registers on the ADT7470 are low-byte first. The data sheet says
172 * that the low byte must be read before the high byte.
173 */
174static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
175{
176 u16 foo;
177 foo = i2c_smbus_read_byte_data(client, reg);
178 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
179 return foo;
180}
181
182static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
183 u16 value)
184{
185 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
Curt Brune93d783b2013-08-08 12:11:03 -0700186 || i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700187}
188
Darrick J. Wong89fac112009-01-06 14:41:34 -0800189/* Probe for temperature sensors. Assumes lock is held */
190static int adt7470_read_temperatures(struct i2c_client *client,
191 struct adt7470_data *data)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700192{
Darrick J. Wong89fac112009-01-06 14:41:34 -0800193 unsigned long res;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700194 int i;
Darrick J. Wong89fac112009-01-06 14:41:34 -0800195 u8 cfg, pwm[4], pwm_cfg[2];
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700196
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -0800197 /* save pwm[1-4] config register */
198 pwm_cfg[0] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(0));
199 pwm_cfg[1] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(2));
200
201 /* set manual pwm to whatever it is set to now */
202 for (i = 0; i < ADT7470_FAN_COUNT; i++)
203 pwm[i] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM(i));
204
205 /* put pwm in manual mode */
206 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0),
207 pwm_cfg[0] & ~(ADT7470_PWM_AUTO_MASK));
208 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2),
209 pwm_cfg[1] & ~(ADT7470_PWM_AUTO_MASK));
210
211 /* write pwm control to whatever it was */
212 for (i = 0; i < ADT7470_FAN_COUNT; i++)
213 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(i), pwm[i]);
214
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700215 /* start reading temperature sensors */
216 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
217 cfg |= 0x80;
218 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
219
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800220 /* Delay is 200ms * number of temp sensors. */
Darrick J. Wong89fac112009-01-06 14:41:34 -0800221 res = msleep_interruptible((data->num_temp_sensors >= 0 ?
222 data->num_temp_sensors * 200 :
223 TEMP_COLLECTION_TIME));
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700224
225 /* done reading temperature sensors */
226 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
227 cfg &= ~0x80;
228 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
229
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -0800230 /* restore pwm[1-4] config registers */
231 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
232 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
233
Darrick J. Wong89fac112009-01-06 14:41:34 -0800234 if (res) {
Joe Perches2e991202010-10-20 06:51:27 +0000235 pr_err("ha ha, interrupted\n");
Darrick J. Wong89fac112009-01-06 14:41:34 -0800236 return -EAGAIN;
237 }
238
239 /* Only count fans if we have to */
240 if (data->num_temp_sensors >= 0)
241 return 0;
242
243 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700244 data->temp[i] = i2c_smbus_read_byte_data(client,
245 ADT7470_TEMP_REG(i));
Darrick J. Wong89fac112009-01-06 14:41:34 -0800246 if (data->temp[i])
247 data->num_temp_sensors = i + 1;
248 }
249 data->temperatures_probed = 1;
250 return 0;
251}
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700252
Darrick J. Wong89fac112009-01-06 14:41:34 -0800253static int adt7470_update_thread(void *p)
254{
255 struct i2c_client *client = p;
256 struct adt7470_data *data = i2c_get_clientdata(client);
257
258 while (!kthread_should_stop()) {
259 mutex_lock(&data->lock);
260 adt7470_read_temperatures(client, data);
261 mutex_unlock(&data->lock);
Joshua Scott93cacfd2016-09-09 17:19:26 +1200262
263 set_current_state(TASK_INTERRUPTIBLE);
Darrick J. Wong89fac112009-01-06 14:41:34 -0800264 if (kthread_should_stop())
265 break;
Joshua Scott93cacfd2016-09-09 17:19:26 +1200266
267 schedule_timeout(msecs_to_jiffies(data->auto_update_interval));
Darrick J. Wong89fac112009-01-06 14:41:34 -0800268 }
269
Darrick J. Wong89fac112009-01-06 14:41:34 -0800270 return 0;
271}
272
273static struct adt7470_data *adt7470_update_device(struct device *dev)
274{
Axel Lin30485772014-07-16 23:12:54 +0800275 struct adt7470_data *data = dev_get_drvdata(dev);
276 struct i2c_client *client = data->client;
Darrick J. Wong89fac112009-01-06 14:41:34 -0800277 unsigned long local_jiffies = jiffies;
278 u8 cfg;
279 int i;
280 int need_sensors = 1;
281 int need_limits = 1;
282
283 /*
284 * Figure out if we need to update the shadow registers.
285 * Lockless means that we may occasionally report out of
286 * date data.
287 */
288 if (time_before(local_jiffies, data->sensors_last_updated +
289 SENSOR_REFRESH_INTERVAL) &&
290 data->sensors_valid)
291 need_sensors = 0;
292
293 if (time_before(local_jiffies, data->limits_last_updated +
294 LIMIT_REFRESH_INTERVAL) &&
295 data->limits_valid)
296 need_limits = 0;
297
298 if (!need_sensors && !need_limits)
299 return data;
300
301 mutex_lock(&data->lock);
302 if (!need_sensors)
303 goto no_sensor_update;
304
305 if (!data->temperatures_probed)
306 adt7470_read_temperatures(client, data);
307 else
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800308 for (i = 0; i < ADT7470_TEMP_COUNT; i++)
Darrick J. Wong89fac112009-01-06 14:41:34 -0800309 data->temp[i] = i2c_smbus_read_byte_data(client,
310 ADT7470_TEMP_REG(i));
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800311
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700312 for (i = 0; i < ADT7470_FAN_COUNT; i++)
313 data->fan[i] = adt7470_read_word_data(client,
314 ADT7470_REG_FAN(i));
315
316 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
317 int reg;
318 int reg_mask;
319
320 data->pwm[i] = i2c_smbus_read_byte_data(client,
321 ADT7470_REG_PWM(i));
322
323 if (i % 2)
324 reg_mask = ADT7470_PWM2_AUTO_MASK;
325 else
326 reg_mask = ADT7470_PWM1_AUTO_MASK;
327
328 reg = ADT7470_REG_PWM_CFG(i);
329 if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
330 data->pwm_automatic[i] = 1;
331 else
332 data->pwm_automatic[i] = 0;
333
334 reg = ADT7470_REG_PWM_AUTO_TEMP(i);
335 cfg = i2c_smbus_read_byte_data(client, reg);
336 if (!(i % 2))
337 data->pwm_auto_temp[i] = cfg >> 4;
338 else
339 data->pwm_auto_temp[i] = cfg & 0xF;
340 }
341
342 if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
343 ADT7470_FSPD_MASK)
344 data->force_pwm_max = 1;
345 else
346 data->force_pwm_max = 0;
347
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800348 data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
349 if (data->alarm & ADT7470_OOL_ALARM)
350 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
351 ADT7470_REG_ALARM2));
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700352 data->alarms_mask = adt7470_read_word_data(client,
353 ADT7470_REG_ALARM1_MASK);
354
355 data->sensors_last_updated = local_jiffies;
356 data->sensors_valid = 1;
357
358no_sensor_update:
Darrick J. Wong89fac112009-01-06 14:41:34 -0800359 if (!need_limits)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700360 goto out;
361
362 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
363 data->temp_min[i] = i2c_smbus_read_byte_data(client,
364 ADT7470_TEMP_MIN_REG(i));
365 data->temp_max[i] = i2c_smbus_read_byte_data(client,
366 ADT7470_TEMP_MAX_REG(i));
367 }
368
369 for (i = 0; i < ADT7470_FAN_COUNT; i++) {
370 data->fan_min[i] = adt7470_read_word_data(client,
371 ADT7470_REG_FAN_MIN(i));
372 data->fan_max[i] = adt7470_read_word_data(client,
373 ADT7470_REG_FAN_MAX(i));
374 }
375
376 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
377 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
378 ADT7470_REG_PWM_MAX(i));
379 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
380 ADT7470_REG_PWM_MIN(i));
381 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
382 ADT7470_REG_PWM_TMIN(i));
383 }
384
385 data->limits_last_updated = local_jiffies;
386 data->limits_valid = 1;
387
388out:
389 mutex_unlock(&data->lock);
390 return data;
391}
392
Julia Lawall808fc6c2016-12-22 13:04:34 +0100393static ssize_t auto_update_interval_show(struct device *dev,
Darrick J. Wong89fac112009-01-06 14:41:34 -0800394 struct device_attribute *devattr,
395 char *buf)
396{
397 struct adt7470_data *data = adt7470_update_device(dev);
398 return sprintf(buf, "%d\n", data->auto_update_interval);
399}
400
Julia Lawall808fc6c2016-12-22 13:04:34 +0100401static ssize_t auto_update_interval_store(struct device *dev,
402 struct device_attribute *devattr,
403 const char *buf, size_t count)
Darrick J. Wong89fac112009-01-06 14:41:34 -0800404{
Axel Lin30485772014-07-16 23:12:54 +0800405 struct adt7470_data *data = dev_get_drvdata(dev);
Darrick J. Wong89fac112009-01-06 14:41:34 -0800406 long temp;
407
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100408 if (kstrtol(buf, 10, &temp))
Darrick J. Wong89fac112009-01-06 14:41:34 -0800409 return -EINVAL;
410
Guenter Roeck2a844c12013-01-09 08:09:34 -0800411 temp = clamp_val(temp, 0, 60000);
Darrick J. Wong89fac112009-01-06 14:41:34 -0800412
413 mutex_lock(&data->lock);
414 data->auto_update_interval = temp;
415 mutex_unlock(&data->lock);
416
417 return count;
418}
419
Julia Lawall808fc6c2016-12-22 13:04:34 +0100420static ssize_t num_temp_sensors_show(struct device *dev,
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800421 struct device_attribute *devattr,
422 char *buf)
423{
424 struct adt7470_data *data = adt7470_update_device(dev);
425 return sprintf(buf, "%d\n", data->num_temp_sensors);
426}
427
Julia Lawall808fc6c2016-12-22 13:04:34 +0100428static ssize_t num_temp_sensors_store(struct device *dev,
429 struct device_attribute *devattr,
430 const char *buf, size_t count)
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800431{
Axel Lin30485772014-07-16 23:12:54 +0800432 struct adt7470_data *data = dev_get_drvdata(dev);
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800433 long temp;
434
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100435 if (kstrtol(buf, 10, &temp))
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800436 return -EINVAL;
437
Guenter Roeck2a844c12013-01-09 08:09:34 -0800438 temp = clamp_val(temp, -1, 10);
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800439
440 mutex_lock(&data->lock);
441 data->num_temp_sensors = temp;
Darrick J. Wong89fac112009-01-06 14:41:34 -0800442 if (temp < 0)
443 data->temperatures_probed = 0;
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800444 mutex_unlock(&data->lock);
445
446 return count;
447}
448
Guenter Roeck42291a52018-12-10 14:02:02 -0800449static ssize_t temp_min_show(struct device *dev,
450 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700451{
452 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
453 struct adt7470_data *data = adt7470_update_device(dev);
454 return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
455}
456
Guenter Roeck42291a52018-12-10 14:02:02 -0800457static ssize_t temp_min_store(struct device *dev,
458 struct device_attribute *devattr,
459 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700460{
461 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800462 struct adt7470_data *data = dev_get_drvdata(dev);
463 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800464 long temp;
465
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100466 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800467 return -EINVAL;
468
Guenter Roeck64bd7082016-12-03 11:10:34 -0800469 temp = clamp_val(temp, -128000, 127000);
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800470 temp = DIV_ROUND_CLOSEST(temp, 1000);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700471
472 mutex_lock(&data->lock);
473 data->temp_min[attr->index] = temp;
474 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
475 temp);
476 mutex_unlock(&data->lock);
477
478 return count;
479}
480
Guenter Roeck42291a52018-12-10 14:02:02 -0800481static ssize_t temp_max_show(struct device *dev,
482 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700483{
484 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
485 struct adt7470_data *data = adt7470_update_device(dev);
486 return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
487}
488
Guenter Roeck42291a52018-12-10 14:02:02 -0800489static ssize_t temp_max_store(struct device *dev,
490 struct device_attribute *devattr,
491 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700492{
493 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800494 struct adt7470_data *data = dev_get_drvdata(dev);
495 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800496 long temp;
497
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100498 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800499 return -EINVAL;
500
Guenter Roeck64bd7082016-12-03 11:10:34 -0800501 temp = clamp_val(temp, -128000, 127000);
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800502 temp = DIV_ROUND_CLOSEST(temp, 1000);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700503
504 mutex_lock(&data->lock);
505 data->temp_max[attr->index] = temp;
506 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
507 temp);
508 mutex_unlock(&data->lock);
509
510 return count;
511}
512
Guenter Roeck42291a52018-12-10 14:02:02 -0800513static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700514 char *buf)
515{
516 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
517 struct adt7470_data *data = adt7470_update_device(dev);
518 return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
519}
520
Julia Lawall808fc6c2016-12-22 13:04:34 +0100521static ssize_t alarm_mask_show(struct device *dev,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700522 struct device_attribute *devattr,
523 char *buf)
524{
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700525 struct adt7470_data *data = adt7470_update_device(dev);
526
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800527 return sprintf(buf, "%x\n", data->alarms_mask);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700528}
529
Julia Lawall808fc6c2016-12-22 13:04:34 +0100530static ssize_t alarm_mask_store(struct device *dev,
531 struct device_attribute *devattr,
532 const char *buf, size_t count)
Joshua Scottfeca3132016-09-09 17:19:25 +1200533{
534 struct adt7470_data *data = dev_get_drvdata(dev);
535 long mask;
536
537 if (kstrtoul(buf, 0, &mask))
538 return -EINVAL;
539
540 if (mask & ~0xffff)
541 return -EINVAL;
542
543 mutex_lock(&data->lock);
544 data->alarms_mask = mask;
545 adt7470_write_word_data(data->client, ADT7470_REG_ALARM1_MASK, mask);
546 mutex_unlock(&data->lock);
547
548 return count;
549}
550
Guenter Roeck42291a52018-12-10 14:02:02 -0800551static ssize_t fan_max_show(struct device *dev,
552 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700553{
554 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
555 struct adt7470_data *data = adt7470_update_device(dev);
556
557 if (FAN_DATA_VALID(data->fan_max[attr->index]))
558 return sprintf(buf, "%d\n",
559 FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
560 else
561 return sprintf(buf, "0\n");
562}
563
Guenter Roeck42291a52018-12-10 14:02:02 -0800564static ssize_t fan_max_store(struct device *dev,
565 struct device_attribute *devattr,
566 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700567{
568 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800569 struct adt7470_data *data = dev_get_drvdata(dev);
570 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800571 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700572
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100573 if (kstrtol(buf, 10, &temp) || !temp)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700574 return -EINVAL;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800575
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700576 temp = FAN_RPM_TO_PERIOD(temp);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800577 temp = clamp_val(temp, 1, 65534);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700578
579 mutex_lock(&data->lock);
580 data->fan_max[attr->index] = temp;
581 adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
582 mutex_unlock(&data->lock);
583
584 return count;
585}
586
Guenter Roeck42291a52018-12-10 14:02:02 -0800587static ssize_t fan_min_show(struct device *dev,
588 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700589{
590 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
591 struct adt7470_data *data = adt7470_update_device(dev);
592
593 if (FAN_DATA_VALID(data->fan_min[attr->index]))
594 return sprintf(buf, "%d\n",
595 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
596 else
597 return sprintf(buf, "0\n");
598}
599
Guenter Roeck42291a52018-12-10 14:02:02 -0800600static ssize_t fan_min_store(struct device *dev,
601 struct device_attribute *devattr,
602 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700603{
604 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800605 struct adt7470_data *data = dev_get_drvdata(dev);
606 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800607 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700608
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100609 if (kstrtol(buf, 10, &temp) || !temp)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700610 return -EINVAL;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800611
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700612 temp = FAN_RPM_TO_PERIOD(temp);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800613 temp = clamp_val(temp, 1, 65534);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700614
615 mutex_lock(&data->lock);
616 data->fan_min[attr->index] = temp;
617 adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
618 mutex_unlock(&data->lock);
619
620 return count;
621}
622
Guenter Roeck42291a52018-12-10 14:02:02 -0800623static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700624 char *buf)
625{
626 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
627 struct adt7470_data *data = adt7470_update_device(dev);
628
629 if (FAN_DATA_VALID(data->fan[attr->index]))
630 return sprintf(buf, "%d\n",
631 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
632 else
633 return sprintf(buf, "0\n");
634}
635
Guenter Roeck42291a52018-12-10 14:02:02 -0800636static ssize_t force_pwm_max_show(struct device *dev,
637 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700638{
639 struct adt7470_data *data = adt7470_update_device(dev);
640 return sprintf(buf, "%d\n", data->force_pwm_max);
641}
642
Guenter Roeck42291a52018-12-10 14:02:02 -0800643static ssize_t force_pwm_max_store(struct device *dev,
644 struct device_attribute *devattr,
645 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700646{
Axel Lin30485772014-07-16 23:12:54 +0800647 struct adt7470_data *data = dev_get_drvdata(dev);
648 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800649 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700650 u8 reg;
651
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100652 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800653 return -EINVAL;
654
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700655 mutex_lock(&data->lock);
656 data->force_pwm_max = temp;
657 reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
658 if (temp)
659 reg |= ADT7470_FSPD_MASK;
660 else
661 reg &= ~ADT7470_FSPD_MASK;
662 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
663 mutex_unlock(&data->lock);
664
665 return count;
666}
667
Guenter Roeck42291a52018-12-10 14:02:02 -0800668static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700669 char *buf)
670{
671 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
672 struct adt7470_data *data = adt7470_update_device(dev);
673 return sprintf(buf, "%d\n", data->pwm[attr->index]);
674}
675
Guenter Roeck42291a52018-12-10 14:02:02 -0800676static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
677 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700678{
679 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800680 struct adt7470_data *data = dev_get_drvdata(dev);
681 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800682 long temp;
683
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100684 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800685 return -EINVAL;
686
Guenter Roeck2a844c12013-01-09 08:09:34 -0800687 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700688
689 mutex_lock(&data->lock);
690 data->pwm[attr->index] = temp;
691 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
692 mutex_unlock(&data->lock);
693
694 return count;
695}
696
Joshua Scottaa18cc92016-08-08 13:35:45 +1200697/* These are the valid PWM frequencies to the nearest Hz */
698static const int adt7470_freq_map[] = {
699 11, 15, 22, 29, 35, 44, 59, 88, 1400, 22500
700};
701
Julia Lawall808fc6c2016-12-22 13:04:34 +0100702static ssize_t pwm1_freq_show(struct device *dev,
703 struct device_attribute *devattr, char *buf)
Joshua Scottaa18cc92016-08-08 13:35:45 +1200704{
705 struct adt7470_data *data = adt7470_update_device(dev);
706 unsigned char cfg_reg_1;
707 unsigned char cfg_reg_2;
708 int index;
709
710 mutex_lock(&data->lock);
711 cfg_reg_1 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG);
712 cfg_reg_2 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG_2);
713 mutex_unlock(&data->lock);
714
715 index = (cfg_reg_2 & ADT7470_FREQ_MASK) >> ADT7470_FREQ_SHIFT;
716 if (!(cfg_reg_1 & ADT7470_CFG_LF))
717 index += 8;
718 if (index >= ARRAY_SIZE(adt7470_freq_map))
719 index = ARRAY_SIZE(adt7470_freq_map) - 1;
720
721 return scnprintf(buf, PAGE_SIZE, "%d\n", adt7470_freq_map[index]);
722}
723
Julia Lawall808fc6c2016-12-22 13:04:34 +0100724static ssize_t pwm1_freq_store(struct device *dev,
725 struct device_attribute *devattr,
726 const char *buf, size_t count)
Joshua Scottaa18cc92016-08-08 13:35:45 +1200727{
728 struct adt7470_data *data = dev_get_drvdata(dev);
729 struct i2c_client *client = data->client;
730 long freq;
731 int index;
732 int low_freq = ADT7470_CFG_LF;
733 unsigned char val;
734
735 if (kstrtol(buf, 10, &freq))
736 return -EINVAL;
737
738 /* Round the user value given to the closest available frequency */
739 index = find_closest(freq, adt7470_freq_map,
740 ARRAY_SIZE(adt7470_freq_map));
741
742 if (index >= 8) {
743 index -= 8;
744 low_freq = 0;
745 }
746
747 mutex_lock(&data->lock);
748 /* Configuration Register 1 */
749 val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
750 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG,
751 (val & ~ADT7470_CFG_LF) | low_freq);
752 /* Configuration Register 2 */
753 val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG_2);
754 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG_2,
755 (val & ~ADT7470_FREQ_MASK) | (index << ADT7470_FREQ_SHIFT));
756 mutex_unlock(&data->lock);
757
758 return count;
759}
760
Guenter Roeck42291a52018-12-10 14:02:02 -0800761static ssize_t pwm_max_show(struct device *dev,
762 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700763{
764 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
765 struct adt7470_data *data = adt7470_update_device(dev);
766 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
767}
768
Guenter Roeck42291a52018-12-10 14:02:02 -0800769static ssize_t pwm_max_store(struct device *dev,
770 struct device_attribute *devattr,
771 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700772{
773 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800774 struct adt7470_data *data = dev_get_drvdata(dev);
775 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800776 long temp;
777
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100778 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800779 return -EINVAL;
780
Guenter Roeck2a844c12013-01-09 08:09:34 -0800781 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700782
783 mutex_lock(&data->lock);
784 data->pwm_max[attr->index] = temp;
785 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
786 temp);
787 mutex_unlock(&data->lock);
788
789 return count;
790}
791
Guenter Roeck42291a52018-12-10 14:02:02 -0800792static ssize_t pwm_min_show(struct device *dev,
793 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700794{
795 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
796 struct adt7470_data *data = adt7470_update_device(dev);
797 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
798}
799
Guenter Roeck42291a52018-12-10 14:02:02 -0800800static ssize_t pwm_min_store(struct device *dev,
801 struct device_attribute *devattr,
802 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700803{
804 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800805 struct adt7470_data *data = dev_get_drvdata(dev);
806 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800807 long temp;
808
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100809 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800810 return -EINVAL;
811
Guenter Roeck2a844c12013-01-09 08:09:34 -0800812 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700813
814 mutex_lock(&data->lock);
815 data->pwm_min[attr->index] = temp;
816 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
817 temp);
818 mutex_unlock(&data->lock);
819
820 return count;
821}
822
Guenter Roeck42291a52018-12-10 14:02:02 -0800823static ssize_t pwm_tmax_show(struct device *dev,
824 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700825{
826 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
827 struct adt7470_data *data = adt7470_update_device(dev);
828 /* the datasheet says that tmax = tmin + 20C */
829 return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
830}
831
Guenter Roeck42291a52018-12-10 14:02:02 -0800832static ssize_t pwm_tmin_show(struct device *dev,
833 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700834{
835 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
836 struct adt7470_data *data = adt7470_update_device(dev);
837 return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
838}
839
Guenter Roeck42291a52018-12-10 14:02:02 -0800840static ssize_t pwm_tmin_store(struct device *dev,
841 struct device_attribute *devattr,
842 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700843{
844 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800845 struct adt7470_data *data = dev_get_drvdata(dev);
846 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800847 long temp;
848
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100849 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800850 return -EINVAL;
851
Guenter Roeck64bd7082016-12-03 11:10:34 -0800852 temp = clamp_val(temp, -128000, 127000);
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800853 temp = DIV_ROUND_CLOSEST(temp, 1000);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700854
855 mutex_lock(&data->lock);
856 data->pwm_tmin[attr->index] = temp;
857 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
858 temp);
859 mutex_unlock(&data->lock);
860
861 return count;
862}
863
Guenter Roeck42291a52018-12-10 14:02:02 -0800864static ssize_t pwm_auto_show(struct device *dev,
865 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700866{
867 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
868 struct adt7470_data *data = adt7470_update_device(dev);
869 return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
870}
871
Guenter Roeck42291a52018-12-10 14:02:02 -0800872static ssize_t pwm_auto_store(struct device *dev,
873 struct device_attribute *devattr,
874 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700875{
876 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800877 struct adt7470_data *data = dev_get_drvdata(dev);
878 struct i2c_client *client = data->client;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700879 int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
880 int pwm_auto_reg_mask;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800881 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700882 u8 reg;
883
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100884 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800885 return -EINVAL;
886
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700887 if (attr->index % 2)
888 pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
889 else
890 pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
891
892 if (temp != 2 && temp != 1)
893 return -EINVAL;
894 temp--;
895
896 mutex_lock(&data->lock);
897 data->pwm_automatic[attr->index] = temp;
898 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
899 if (temp)
900 reg |= pwm_auto_reg_mask;
901 else
902 reg &= ~pwm_auto_reg_mask;
903 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
904 mutex_unlock(&data->lock);
905
906 return count;
907}
908
Guenter Roeck42291a52018-12-10 14:02:02 -0800909static ssize_t pwm_auto_temp_show(struct device *dev,
910 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700911{
912 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
913 struct adt7470_data *data = adt7470_update_device(dev);
914 u8 ctrl = data->pwm_auto_temp[attr->index];
915
916 if (ctrl)
917 return sprintf(buf, "%d\n", 1 << (ctrl - 1));
918 else
919 return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
920}
921
922static int cvt_auto_temp(int input)
923{
924 if (input == ADT7470_PWM_ALL_TEMPS)
925 return 0;
Robert P. J. Dayce9c2f42007-11-06 03:21:42 -0500926 if (input < 1 || !is_power_of_2(input))
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700927 return -EINVAL;
928 return ilog2(input) + 1;
929}
930
Guenter Roeck42291a52018-12-10 14:02:02 -0800931static ssize_t pwm_auto_temp_store(struct device *dev,
932 struct device_attribute *devattr,
933 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700934{
935 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800936 struct adt7470_data *data = dev_get_drvdata(dev);
937 struct i2c_client *client = data->client;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700938 int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800939 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700940 u8 reg;
941
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100942 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800943 return -EINVAL;
944
945 temp = cvt_auto_temp(temp);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700946 if (temp < 0)
947 return temp;
948
949 mutex_lock(&data->lock);
950 data->pwm_automatic[attr->index] = temp;
951 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
952
953 if (!(attr->index % 2)) {
954 reg &= 0xF;
955 reg |= (temp << 4) & 0xF0;
956 } else {
957 reg &= 0xF0;
958 reg |= temp & 0xF;
959 }
960
961 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
962 mutex_unlock(&data->lock);
963
964 return count;
965}
966
Guenter Roeck42291a52018-12-10 14:02:02 -0800967static ssize_t alarm_show(struct device *dev,
968 struct device_attribute *devattr, char *buf)
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800969{
970 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
971 struct adt7470_data *data = adt7470_update_device(dev);
972
973 if (data->alarm & attr->index)
974 return sprintf(buf, "1\n");
975 else
976 return sprintf(buf, "0\n");
977}
978
Julia Lawall808fc6c2016-12-22 13:04:34 +0100979static DEVICE_ATTR_RW(alarm_mask);
980static DEVICE_ATTR_RW(num_temp_sensors);
981static DEVICE_ATTR_RW(auto_update_interval);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700982
Guenter Roeck42291a52018-12-10 14:02:02 -0800983static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
984static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
985static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
986static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
987static SENSOR_DEVICE_ATTR_RW(temp5_max, temp_max, 4);
988static SENSOR_DEVICE_ATTR_RW(temp6_max, temp_max, 5);
989static SENSOR_DEVICE_ATTR_RW(temp7_max, temp_max, 6);
990static SENSOR_DEVICE_ATTR_RW(temp8_max, temp_max, 7);
991static SENSOR_DEVICE_ATTR_RW(temp9_max, temp_max, 8);
992static SENSOR_DEVICE_ATTR_RW(temp10_max, temp_max, 9);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700993
Guenter Roeck42291a52018-12-10 14:02:02 -0800994static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
995static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
996static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
997static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3);
998static SENSOR_DEVICE_ATTR_RW(temp5_min, temp_min, 4);
999static SENSOR_DEVICE_ATTR_RW(temp6_min, temp_min, 5);
1000static SENSOR_DEVICE_ATTR_RW(temp7_min, temp_min, 6);
1001static SENSOR_DEVICE_ATTR_RW(temp8_min, temp_min, 7);
1002static SENSOR_DEVICE_ATTR_RW(temp9_min, temp_min, 8);
1003static SENSOR_DEVICE_ATTR_RW(temp10_min, temp_min, 9);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001004
Guenter Roeck42291a52018-12-10 14:02:02 -08001005static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
1006static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
1007static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
1008static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
1009static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
1010static SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5);
1011static SENSOR_DEVICE_ATTR_RO(temp7_input, temp, 6);
1012static SENSOR_DEVICE_ATTR_RO(temp8_input, temp, 7);
1013static SENSOR_DEVICE_ATTR_RO(temp9_input, temp, 8);
1014static SENSOR_DEVICE_ATTR_RO(temp10_input, temp, 9);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001015
Guenter Roeck42291a52018-12-10 14:02:02 -08001016static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, ADT7470_R1T_ALARM);
1017static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, ADT7470_R2T_ALARM);
1018static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, ADT7470_R3T_ALARM);
1019static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm, ADT7470_R4T_ALARM);
1020static SENSOR_DEVICE_ATTR_RO(temp5_alarm, alarm, ADT7470_R5T_ALARM);
1021static SENSOR_DEVICE_ATTR_RO(temp6_alarm, alarm, ADT7470_R6T_ALARM);
1022static SENSOR_DEVICE_ATTR_RO(temp7_alarm, alarm, ADT7470_R7T_ALARM);
1023static SENSOR_DEVICE_ATTR_RO(temp8_alarm, alarm, ALARM2(ADT7470_R8T_ALARM));
1024static SENSOR_DEVICE_ATTR_RO(temp9_alarm, alarm, ALARM2(ADT7470_R9T_ALARM));
1025static SENSOR_DEVICE_ATTR_RO(temp10_alarm, alarm, ALARM2(ADT7470_R10T_ALARM));
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001026
Guenter Roeck42291a52018-12-10 14:02:02 -08001027static SENSOR_DEVICE_ATTR_RW(fan1_max, fan_max, 0);
1028static SENSOR_DEVICE_ATTR_RW(fan2_max, fan_max, 1);
1029static SENSOR_DEVICE_ATTR_RW(fan3_max, fan_max, 2);
1030static SENSOR_DEVICE_ATTR_RW(fan4_max, fan_max, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001031
Guenter Roeck42291a52018-12-10 14:02:02 -08001032static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
1033static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
1034static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
1035static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001036
Guenter Roeck42291a52018-12-10 14:02:02 -08001037static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
1038static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
1039static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
1040static SENSOR_DEVICE_ATTR_RO(fan4_input, fan, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001041
Guenter Roeck42291a52018-12-10 14:02:02 -08001042static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, ALARM2(ADT7470_FAN1_ALARM));
1043static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, ALARM2(ADT7470_FAN2_ALARM));
1044static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, ALARM2(ADT7470_FAN3_ALARM));
1045static SENSOR_DEVICE_ATTR_RO(fan4_alarm, alarm, ALARM2(ADT7470_FAN4_ALARM));
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001046
Guenter Roeck42291a52018-12-10 14:02:02 -08001047static SENSOR_DEVICE_ATTR_RW(force_pwm_max, force_pwm_max, 0);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001048
Guenter Roeck42291a52018-12-10 14:02:02 -08001049static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
1050static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
1051static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
1052static SENSOR_DEVICE_ATTR_RW(pwm4, pwm, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001053
Julia Lawall808fc6c2016-12-22 13:04:34 +01001054static DEVICE_ATTR_RW(pwm1_freq);
Joshua Scottaa18cc92016-08-08 13:35:45 +12001055
Guenter Roeck42291a52018-12-10 14:02:02 -08001056static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm_min, 0);
1057static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm_min, 1);
1058static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm_min, 2);
1059static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_pwm, pwm_min, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001060
Guenter Roeck42291a52018-12-10 14:02:02 -08001061static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm_max, 0);
1062static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm_max, 1);
1063static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm_max, 2);
1064static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point2_pwm, pwm_max, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001065
Guenter Roeck42291a52018-12-10 14:02:02 -08001066static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_tmin, 0);
1067static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_temp, pwm_tmin, 1);
1068static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_temp, pwm_tmin, 2);
1069static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_temp, pwm_tmin, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001070
Guenter Roeck42291a52018-12-10 14:02:02 -08001071static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_temp, pwm_tmax, 0);
1072static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point2_temp, pwm_tmax, 1);
1073static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point2_temp, pwm_tmax, 2);
1074static SENSOR_DEVICE_ATTR_RO(pwm4_auto_point2_temp, pwm_tmax, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001075
Guenter Roeck42291a52018-12-10 14:02:02 -08001076static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_auto, 0);
1077static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_auto, 1);
1078static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_auto, 2);
1079static SENSOR_DEVICE_ATTR_RW(pwm4_enable, pwm_auto, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001080
Guenter Roeck42291a52018-12-10 14:02:02 -08001081static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels_temp, pwm_auto_temp, 0);
1082static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels_temp, pwm_auto_temp, 1);
1083static SENSOR_DEVICE_ATTR_RW(pwm3_auto_channels_temp, pwm_auto_temp, 2);
1084static SENSOR_DEVICE_ATTR_RW(pwm4_auto_channels_temp, pwm_auto_temp, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001085
Axel Lin30485772014-07-16 23:12:54 +08001086static struct attribute *adt7470_attrs[] = {
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001087 &dev_attr_alarm_mask.attr,
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001088 &dev_attr_num_temp_sensors.attr,
Darrick J. Wong89fac112009-01-06 14:41:34 -08001089 &dev_attr_auto_update_interval.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001090 &sensor_dev_attr_temp1_max.dev_attr.attr,
1091 &sensor_dev_attr_temp2_max.dev_attr.attr,
1092 &sensor_dev_attr_temp3_max.dev_attr.attr,
1093 &sensor_dev_attr_temp4_max.dev_attr.attr,
1094 &sensor_dev_attr_temp5_max.dev_attr.attr,
1095 &sensor_dev_attr_temp6_max.dev_attr.attr,
1096 &sensor_dev_attr_temp7_max.dev_attr.attr,
1097 &sensor_dev_attr_temp8_max.dev_attr.attr,
1098 &sensor_dev_attr_temp9_max.dev_attr.attr,
1099 &sensor_dev_attr_temp10_max.dev_attr.attr,
1100 &sensor_dev_attr_temp1_min.dev_attr.attr,
1101 &sensor_dev_attr_temp2_min.dev_attr.attr,
1102 &sensor_dev_attr_temp3_min.dev_attr.attr,
1103 &sensor_dev_attr_temp4_min.dev_attr.attr,
1104 &sensor_dev_attr_temp5_min.dev_attr.attr,
1105 &sensor_dev_attr_temp6_min.dev_attr.attr,
1106 &sensor_dev_attr_temp7_min.dev_attr.attr,
1107 &sensor_dev_attr_temp8_min.dev_attr.attr,
1108 &sensor_dev_attr_temp9_min.dev_attr.attr,
1109 &sensor_dev_attr_temp10_min.dev_attr.attr,
1110 &sensor_dev_attr_temp1_input.dev_attr.attr,
1111 &sensor_dev_attr_temp2_input.dev_attr.attr,
1112 &sensor_dev_attr_temp3_input.dev_attr.attr,
1113 &sensor_dev_attr_temp4_input.dev_attr.attr,
1114 &sensor_dev_attr_temp5_input.dev_attr.attr,
1115 &sensor_dev_attr_temp6_input.dev_attr.attr,
1116 &sensor_dev_attr_temp7_input.dev_attr.attr,
1117 &sensor_dev_attr_temp8_input.dev_attr.attr,
1118 &sensor_dev_attr_temp9_input.dev_attr.attr,
1119 &sensor_dev_attr_temp10_input.dev_attr.attr,
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001120 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1121 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1122 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1123 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
1124 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
1125 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
1126 &sensor_dev_attr_temp7_alarm.dev_attr.attr,
1127 &sensor_dev_attr_temp8_alarm.dev_attr.attr,
1128 &sensor_dev_attr_temp9_alarm.dev_attr.attr,
1129 &sensor_dev_attr_temp10_alarm.dev_attr.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001130 &sensor_dev_attr_fan1_max.dev_attr.attr,
1131 &sensor_dev_attr_fan2_max.dev_attr.attr,
1132 &sensor_dev_attr_fan3_max.dev_attr.attr,
1133 &sensor_dev_attr_fan4_max.dev_attr.attr,
1134 &sensor_dev_attr_fan1_min.dev_attr.attr,
1135 &sensor_dev_attr_fan2_min.dev_attr.attr,
1136 &sensor_dev_attr_fan3_min.dev_attr.attr,
1137 &sensor_dev_attr_fan4_min.dev_attr.attr,
1138 &sensor_dev_attr_fan1_input.dev_attr.attr,
1139 &sensor_dev_attr_fan2_input.dev_attr.attr,
1140 &sensor_dev_attr_fan3_input.dev_attr.attr,
1141 &sensor_dev_attr_fan4_input.dev_attr.attr,
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001142 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1143 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1144 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1145 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001146 &sensor_dev_attr_force_pwm_max.dev_attr.attr,
1147 &sensor_dev_attr_pwm1.dev_attr.attr,
Joshua Scottaa18cc92016-08-08 13:35:45 +12001148 &dev_attr_pwm1_freq.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001149 &sensor_dev_attr_pwm2.dev_attr.attr,
1150 &sensor_dev_attr_pwm3.dev_attr.attr,
1151 &sensor_dev_attr_pwm4.dev_attr.attr,
1152 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1153 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1154 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1155 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1156 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1157 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1158 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1159 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1160 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1161 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1162 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1163 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
1164 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1165 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1166 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1167 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
1168 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1169 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1170 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1171 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1172 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1173 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1174 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1175 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1176 NULL
1177};
1178
Axel Lin30485772014-07-16 23:12:54 +08001179ATTRIBUTE_GROUPS(adt7470);
1180
Jean Delvare008f1ca2008-07-16 19:30:10 +02001181/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001182static int adt7470_detect(struct i2c_client *client,
Jean Delvare008f1ca2008-07-16 19:30:10 +02001183 struct i2c_board_info *info)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001184{
Jean Delvare008f1ca2008-07-16 19:30:10 +02001185 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +01001186 int vendor, device, revision;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001187
1188 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare008f1ca2008-07-16 19:30:10 +02001189 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001190
Jean Delvare52df6442009-12-09 20:35:57 +01001191 vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1192 if (vendor != ADT7470_VENDOR)
1193 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001194
Jean Delvare52df6442009-12-09 20:35:57 +01001195 device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1196 if (device != ADT7470_DEVICE)
1197 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001198
Jean Delvare52df6442009-12-09 20:35:57 +01001199 revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION);
1200 if (revision != ADT7470_REVISION)
1201 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001202
Jean Delvare008f1ca2008-07-16 19:30:10 +02001203 strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001204
Jean Delvare008f1ca2008-07-16 19:30:10 +02001205 return 0;
1206}
1207
Axel Lin9027d932014-07-16 23:12:05 +08001208static void adt7470_init_client(struct i2c_client *client)
1209{
1210 int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
1211
1212 if (reg < 0) {
1213 dev_err(&client->dev, "cannot read configuration register\n");
1214 } else {
1215 /* start monitoring (and do a self-test) */
1216 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
1217 }
1218}
1219
Jean Delvare008f1ca2008-07-16 19:30:10 +02001220static int adt7470_probe(struct i2c_client *client,
1221 const struct i2c_device_id *id)
1222{
Axel Lin30485772014-07-16 23:12:54 +08001223 struct device *dev = &client->dev;
Jean Delvare008f1ca2008-07-16 19:30:10 +02001224 struct adt7470_data *data;
Axel Lin30485772014-07-16 23:12:54 +08001225 struct device *hwmon_dev;
Jean Delvare008f1ca2008-07-16 19:30:10 +02001226
Axel Lin30485772014-07-16 23:12:54 +08001227 data = devm_kzalloc(dev, sizeof(struct adt7470_data), GFP_KERNEL);
Guenter Roeck9cc7dcc2012-06-02 09:58:01 -07001228 if (!data)
1229 return -ENOMEM;
Jean Delvare008f1ca2008-07-16 19:30:10 +02001230
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001231 data->num_temp_sensors = -1;
Darrick J. Wong89fac112009-01-06 14:41:34 -08001232 data->auto_update_interval = AUTO_UPDATE_INTERVAL;
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001233
Jean Delvare008f1ca2008-07-16 19:30:10 +02001234 i2c_set_clientdata(client, data);
Axel Lin30485772014-07-16 23:12:54 +08001235 data->client = client;
Jean Delvare008f1ca2008-07-16 19:30:10 +02001236 mutex_init(&data->lock);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001237
1238 dev_info(&client->dev, "%s chip found\n", client->name);
1239
1240 /* Initialize the ADT7470 chip */
1241 adt7470_init_client(client);
1242
1243 /* Register sysfs hooks */
Axel Lin30485772014-07-16 23:12:54 +08001244 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1245 data,
1246 adt7470_groups);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001247
Axel Lin30485772014-07-16 23:12:54 +08001248 if (IS_ERR(hwmon_dev))
1249 return PTR_ERR(hwmon_dev);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001250
Kees Cookf1701682013-07-03 15:04:58 -07001251 data->auto_update = kthread_run(adt7470_update_thread, client, "%s",
Axel Lin30485772014-07-16 23:12:54 +08001252 dev_name(hwmon_dev));
Axel Linf7334b42010-11-08 00:11:33 -05001253 if (IS_ERR(data->auto_update)) {
Axel Lin30485772014-07-16 23:12:54 +08001254 return PTR_ERR(data->auto_update);
Axel Linf7334b42010-11-08 00:11:33 -05001255 }
Darrick J. Wong89fac112009-01-06 14:41:34 -08001256
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001257 return 0;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001258}
1259
Jean Delvare008f1ca2008-07-16 19:30:10 +02001260static int adt7470_remove(struct i2c_client *client)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001261{
1262 struct adt7470_data *data = i2c_get_clientdata(client);
1263
Darrick J. Wong89fac112009-01-06 14:41:34 -08001264 kthread_stop(data->auto_update);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001265 return 0;
1266}
1267
Axel Lin9027d932014-07-16 23:12:05 +08001268static const struct i2c_device_id adt7470_id[] = {
1269 { "adt7470", 0 },
1270 { }
1271};
1272MODULE_DEVICE_TABLE(i2c, adt7470_id);
1273
1274static struct i2c_driver adt7470_driver = {
1275 .class = I2C_CLASS_HWMON,
1276 .driver = {
1277 .name = "adt7470",
1278 },
1279 .probe = adt7470_probe,
1280 .remove = adt7470_remove,
1281 .id_table = adt7470_id,
1282 .detect = adt7470_detect,
1283 .address_list = normal_i2c,
1284};
1285
Axel Linf0967ee2012-01-20 15:38:18 +08001286module_i2c_driver(adt7470_driver);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001287
Darrick J. Wong5407e0512013-08-26 15:42:27 -07001288MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001289MODULE_DESCRIPTION("ADT7470 driver");
1290MODULE_LICENSE("GPL");