blob: 6d87daf18809c541b6604c724acfd945f5e068f7 [file] [log] [blame]
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001/*
2 * A hwmon driver for the Analog Devices ADT7470
3 * Copyright (C) 2007 IBM
4 *
Darrick J. Wong5407e0512013-08-26 15:42:27 -07005 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Joe Perches2e991202010-10-20 06:51:27 +000022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070024#include <linux/module.h>
25#include <linux/jiffies.h>
26#include <linux/i2c.h>
27#include <linux/hwmon.h>
28#include <linux/hwmon-sysfs.h>
29#include <linux/err.h>
30#include <linux/mutex.h>
31#include <linux/delay.h>
32#include <linux/log2.h>
Darrick J. Wong89fac112009-01-06 14:41:34 -080033#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Joshua Scottaa18cc92016-08-08 13:35:45 +120035#include <linux/util_macros.h>
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070036
37/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050038static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070039
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070040/* ADT7470 registers */
41#define ADT7470_REG_BASE_ADDR 0x20
42#define ADT7470_REG_TEMP_BASE_ADDR 0x20
43#define ADT7470_REG_TEMP_MAX_ADDR 0x29
44#define ADT7470_REG_FAN_BASE_ADDR 0x2A
45#define ADT7470_REG_FAN_MAX_ADDR 0x31
46#define ADT7470_REG_PWM_BASE_ADDR 0x32
47#define ADT7470_REG_PWM_MAX_ADDR 0x35
48#define ADT7470_REG_PWM_MAX_BASE_ADDR 0x38
49#define ADT7470_REG_PWM_MAX_MAX_ADDR 0x3B
50#define ADT7470_REG_CFG 0x40
51#define ADT7470_FSPD_MASK 0x04
52#define ADT7470_REG_ALARM1 0x41
Darrick J. Wongfe03f282007-12-19 14:11:25 -080053#define ADT7470_R1T_ALARM 0x01
54#define ADT7470_R2T_ALARM 0x02
55#define ADT7470_R3T_ALARM 0x04
56#define ADT7470_R4T_ALARM 0x08
57#define ADT7470_R5T_ALARM 0x10
58#define ADT7470_R6T_ALARM 0x20
59#define ADT7470_R7T_ALARM 0x40
60#define ADT7470_OOL_ALARM 0x80
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070061#define ADT7470_REG_ALARM2 0x42
Darrick J. Wongfe03f282007-12-19 14:11:25 -080062#define ADT7470_R8T_ALARM 0x01
63#define ADT7470_R9T_ALARM 0x02
64#define ADT7470_R10T_ALARM 0x04
65#define ADT7470_FAN1_ALARM 0x10
66#define ADT7470_FAN2_ALARM 0x20
67#define ADT7470_FAN3_ALARM 0x40
68#define ADT7470_FAN4_ALARM 0x80
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070069#define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44
70#define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57
71#define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58
72#define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F
73#define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60
74#define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67
75#define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68
76#define ADT7470_REG_PWM12_CFG 0x68
77#define ADT7470_PWM2_AUTO_MASK 0x40
78#define ADT7470_PWM1_AUTO_MASK 0x80
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -080079#define ADT7470_PWM_AUTO_MASK 0xC0
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070080#define ADT7470_REG_PWM34_CFG 0x69
81#define ADT7470_PWM3_AUTO_MASK 0x40
82#define ADT7470_PWM4_AUTO_MASK 0x80
83#define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A
84#define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D
85#define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E
86#define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71
Joshua Scottaa18cc92016-08-08 13:35:45 +120087#define ADT7470_REG_CFG_2 0x74
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070088#define ADT7470_REG_ACOUSTICS12 0x75
89#define ADT7470_REG_ACOUSTICS34 0x76
90#define ADT7470_REG_DEVICE 0x3D
91#define ADT7470_REG_VENDOR 0x3E
92#define ADT7470_REG_REVISION 0x3F
93#define ADT7470_REG_ALARM1_MASK 0x72
94#define ADT7470_REG_ALARM2_MASK 0x73
95#define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C
96#define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D
97#define ADT7470_REG_MAX_ADDR 0x81
98
99#define ADT7470_TEMP_COUNT 10
100#define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x))
101#define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
102#define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
103 ((x) * 2) + 1)
104
105#define ADT7470_FAN_COUNT 4
106#define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
107#define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
108#define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
109
110#define ADT7470_PWM_COUNT 4
111#define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x))
112#define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
113#define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
114#define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
115#define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
116#define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
117 ((x) / 2))
118
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800119#define ALARM2(x) ((x) << 8)
120
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700121#define ADT7470_VENDOR 0x41
122#define ADT7470_DEVICE 0x70
123/* datasheet only mentions a revision 2 */
124#define ADT7470_REVISION 0x02
125
126/* "all temps" according to hwmon sysfs interface spec */
127#define ADT7470_PWM_ALL_TEMPS 0x3FF
128
129/* How often do we reread sensors values? (In jiffies) */
130#define SENSOR_REFRESH_INTERVAL (5 * HZ)
131
132/* How often do we reread sensor limit values? (In jiffies) */
133#define LIMIT_REFRESH_INTERVAL (60 * HZ)
134
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800135/* Wait at least 200ms per sensor for 10 sensors */
136#define TEMP_COLLECTION_TIME 2000
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700137
Darrick J. Wong89fac112009-01-06 14:41:34 -0800138/* auto update thing won't fire more than every 2s */
139#define AUTO_UPDATE_INTERVAL 2000
140
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700141/* datasheet says to divide this number by the fan reading to get fan rpm */
142#define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
143#define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
144#define FAN_PERIOD_INVALID 65535
145#define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
146
Joshua Scottaa18cc92016-08-08 13:35:45 +1200147/* Config registers 1 and 2 include fields for selecting the PWM frequency */
148#define ADT7470_CFG_LF 0x40
149#define ADT7470_FREQ_MASK 0x70
150#define ADT7470_FREQ_SHIFT 4
151
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700152struct adt7470_data {
Axel Lin30485772014-07-16 23:12:54 +0800153 struct i2c_client *client;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700154 struct mutex lock;
155 char sensors_valid;
156 char limits_valid;
157 unsigned long sensors_last_updated; /* In jiffies */
158 unsigned long limits_last_updated; /* In jiffies */
159
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800160 int num_temp_sensors; /* -1 = probe */
Darrick J. Wong89fac112009-01-06 14:41:34 -0800161 int temperatures_probed;
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800162
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700163 s8 temp[ADT7470_TEMP_COUNT];
164 s8 temp_min[ADT7470_TEMP_COUNT];
165 s8 temp_max[ADT7470_TEMP_COUNT];
166 u16 fan[ADT7470_FAN_COUNT];
167 u16 fan_min[ADT7470_FAN_COUNT];
168 u16 fan_max[ADT7470_FAN_COUNT];
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800169 u16 alarm;
170 u16 alarms_mask;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700171 u8 force_pwm_max;
172 u8 pwm[ADT7470_PWM_COUNT];
173 u8 pwm_max[ADT7470_PWM_COUNT];
174 u8 pwm_automatic[ADT7470_PWM_COUNT];
175 u8 pwm_min[ADT7470_PWM_COUNT];
176 s8 pwm_tmin[ADT7470_PWM_COUNT];
177 u8 pwm_auto_temp[ADT7470_PWM_COUNT];
Darrick J. Wong89fac112009-01-06 14:41:34 -0800178
179 struct task_struct *auto_update;
Darrick J. Wong89fac112009-01-06 14:41:34 -0800180 unsigned int auto_update_interval;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700181};
182
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700183/*
184 * 16-bit registers on the ADT7470 are low-byte first. The data sheet says
185 * that the low byte must be read before the high byte.
186 */
187static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
188{
189 u16 foo;
190 foo = i2c_smbus_read_byte_data(client, reg);
191 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
192 return foo;
193}
194
195static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
196 u16 value)
197{
198 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
Curt Brune93d783b2013-08-08 12:11:03 -0700199 || i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700200}
201
Darrick J. Wong89fac112009-01-06 14:41:34 -0800202/* Probe for temperature sensors. Assumes lock is held */
203static int adt7470_read_temperatures(struct i2c_client *client,
204 struct adt7470_data *data)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700205{
Darrick J. Wong89fac112009-01-06 14:41:34 -0800206 unsigned long res;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700207 int i;
Darrick J. Wong89fac112009-01-06 14:41:34 -0800208 u8 cfg, pwm[4], pwm_cfg[2];
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700209
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -0800210 /* save pwm[1-4] config register */
211 pwm_cfg[0] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(0));
212 pwm_cfg[1] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(2));
213
214 /* set manual pwm to whatever it is set to now */
215 for (i = 0; i < ADT7470_FAN_COUNT; i++)
216 pwm[i] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM(i));
217
218 /* put pwm in manual mode */
219 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0),
220 pwm_cfg[0] & ~(ADT7470_PWM_AUTO_MASK));
221 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2),
222 pwm_cfg[1] & ~(ADT7470_PWM_AUTO_MASK));
223
224 /* write pwm control to whatever it was */
225 for (i = 0; i < ADT7470_FAN_COUNT; i++)
226 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(i), pwm[i]);
227
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700228 /* start reading temperature sensors */
229 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
230 cfg |= 0x80;
231 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
232
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800233 /* Delay is 200ms * number of temp sensors. */
Darrick J. Wong89fac112009-01-06 14:41:34 -0800234 res = msleep_interruptible((data->num_temp_sensors >= 0 ?
235 data->num_temp_sensors * 200 :
236 TEMP_COLLECTION_TIME));
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700237
238 /* done reading temperature sensors */
239 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
240 cfg &= ~0x80;
241 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
242
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -0800243 /* restore pwm[1-4] config registers */
244 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
245 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
246
Darrick J. Wong89fac112009-01-06 14:41:34 -0800247 if (res) {
Joe Perches2e991202010-10-20 06:51:27 +0000248 pr_err("ha ha, interrupted\n");
Darrick J. Wong89fac112009-01-06 14:41:34 -0800249 return -EAGAIN;
250 }
251
252 /* Only count fans if we have to */
253 if (data->num_temp_sensors >= 0)
254 return 0;
255
256 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700257 data->temp[i] = i2c_smbus_read_byte_data(client,
258 ADT7470_TEMP_REG(i));
Darrick J. Wong89fac112009-01-06 14:41:34 -0800259 if (data->temp[i])
260 data->num_temp_sensors = i + 1;
261 }
262 data->temperatures_probed = 1;
263 return 0;
264}
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700265
Darrick J. Wong89fac112009-01-06 14:41:34 -0800266static int adt7470_update_thread(void *p)
267{
268 struct i2c_client *client = p;
269 struct adt7470_data *data = i2c_get_clientdata(client);
270
271 while (!kthread_should_stop()) {
272 mutex_lock(&data->lock);
273 adt7470_read_temperatures(client, data);
274 mutex_unlock(&data->lock);
Joshua Scott93cacfd2016-09-09 17:19:26 +1200275
276 set_current_state(TASK_INTERRUPTIBLE);
Darrick J. Wong89fac112009-01-06 14:41:34 -0800277 if (kthread_should_stop())
278 break;
Joshua Scott93cacfd2016-09-09 17:19:26 +1200279
280 schedule_timeout(msecs_to_jiffies(data->auto_update_interval));
Darrick J. Wong89fac112009-01-06 14:41:34 -0800281 }
282
Darrick J. Wong89fac112009-01-06 14:41:34 -0800283 return 0;
284}
285
286static struct adt7470_data *adt7470_update_device(struct device *dev)
287{
Axel Lin30485772014-07-16 23:12:54 +0800288 struct adt7470_data *data = dev_get_drvdata(dev);
289 struct i2c_client *client = data->client;
Darrick J. Wong89fac112009-01-06 14:41:34 -0800290 unsigned long local_jiffies = jiffies;
291 u8 cfg;
292 int i;
293 int need_sensors = 1;
294 int need_limits = 1;
295
296 /*
297 * Figure out if we need to update the shadow registers.
298 * Lockless means that we may occasionally report out of
299 * date data.
300 */
301 if (time_before(local_jiffies, data->sensors_last_updated +
302 SENSOR_REFRESH_INTERVAL) &&
303 data->sensors_valid)
304 need_sensors = 0;
305
306 if (time_before(local_jiffies, data->limits_last_updated +
307 LIMIT_REFRESH_INTERVAL) &&
308 data->limits_valid)
309 need_limits = 0;
310
311 if (!need_sensors && !need_limits)
312 return data;
313
314 mutex_lock(&data->lock);
315 if (!need_sensors)
316 goto no_sensor_update;
317
318 if (!data->temperatures_probed)
319 adt7470_read_temperatures(client, data);
320 else
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800321 for (i = 0; i < ADT7470_TEMP_COUNT; i++)
Darrick J. Wong89fac112009-01-06 14:41:34 -0800322 data->temp[i] = i2c_smbus_read_byte_data(client,
323 ADT7470_TEMP_REG(i));
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800324
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700325 for (i = 0; i < ADT7470_FAN_COUNT; i++)
326 data->fan[i] = adt7470_read_word_data(client,
327 ADT7470_REG_FAN(i));
328
329 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
330 int reg;
331 int reg_mask;
332
333 data->pwm[i] = i2c_smbus_read_byte_data(client,
334 ADT7470_REG_PWM(i));
335
336 if (i % 2)
337 reg_mask = ADT7470_PWM2_AUTO_MASK;
338 else
339 reg_mask = ADT7470_PWM1_AUTO_MASK;
340
341 reg = ADT7470_REG_PWM_CFG(i);
342 if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
343 data->pwm_automatic[i] = 1;
344 else
345 data->pwm_automatic[i] = 0;
346
347 reg = ADT7470_REG_PWM_AUTO_TEMP(i);
348 cfg = i2c_smbus_read_byte_data(client, reg);
349 if (!(i % 2))
350 data->pwm_auto_temp[i] = cfg >> 4;
351 else
352 data->pwm_auto_temp[i] = cfg & 0xF;
353 }
354
355 if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
356 ADT7470_FSPD_MASK)
357 data->force_pwm_max = 1;
358 else
359 data->force_pwm_max = 0;
360
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800361 data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
362 if (data->alarm & ADT7470_OOL_ALARM)
363 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
364 ADT7470_REG_ALARM2));
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700365 data->alarms_mask = adt7470_read_word_data(client,
366 ADT7470_REG_ALARM1_MASK);
367
368 data->sensors_last_updated = local_jiffies;
369 data->sensors_valid = 1;
370
371no_sensor_update:
Darrick J. Wong89fac112009-01-06 14:41:34 -0800372 if (!need_limits)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700373 goto out;
374
375 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
376 data->temp_min[i] = i2c_smbus_read_byte_data(client,
377 ADT7470_TEMP_MIN_REG(i));
378 data->temp_max[i] = i2c_smbus_read_byte_data(client,
379 ADT7470_TEMP_MAX_REG(i));
380 }
381
382 for (i = 0; i < ADT7470_FAN_COUNT; i++) {
383 data->fan_min[i] = adt7470_read_word_data(client,
384 ADT7470_REG_FAN_MIN(i));
385 data->fan_max[i] = adt7470_read_word_data(client,
386 ADT7470_REG_FAN_MAX(i));
387 }
388
389 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
390 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
391 ADT7470_REG_PWM_MAX(i));
392 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
393 ADT7470_REG_PWM_MIN(i));
394 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
395 ADT7470_REG_PWM_TMIN(i));
396 }
397
398 data->limits_last_updated = local_jiffies;
399 data->limits_valid = 1;
400
401out:
402 mutex_unlock(&data->lock);
403 return data;
404}
405
Julia Lawall808fc6c2016-12-22 13:04:34 +0100406static ssize_t auto_update_interval_show(struct device *dev,
Darrick J. Wong89fac112009-01-06 14:41:34 -0800407 struct device_attribute *devattr,
408 char *buf)
409{
410 struct adt7470_data *data = adt7470_update_device(dev);
411 return sprintf(buf, "%d\n", data->auto_update_interval);
412}
413
Julia Lawall808fc6c2016-12-22 13:04:34 +0100414static ssize_t auto_update_interval_store(struct device *dev,
415 struct device_attribute *devattr,
416 const char *buf, size_t count)
Darrick J. Wong89fac112009-01-06 14:41:34 -0800417{
Axel Lin30485772014-07-16 23:12:54 +0800418 struct adt7470_data *data = dev_get_drvdata(dev);
Darrick J. Wong89fac112009-01-06 14:41:34 -0800419 long temp;
420
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100421 if (kstrtol(buf, 10, &temp))
Darrick J. Wong89fac112009-01-06 14:41:34 -0800422 return -EINVAL;
423
Guenter Roeck2a844c12013-01-09 08:09:34 -0800424 temp = clamp_val(temp, 0, 60000);
Darrick J. Wong89fac112009-01-06 14:41:34 -0800425
426 mutex_lock(&data->lock);
427 data->auto_update_interval = temp;
428 mutex_unlock(&data->lock);
429
430 return count;
431}
432
Julia Lawall808fc6c2016-12-22 13:04:34 +0100433static ssize_t num_temp_sensors_show(struct device *dev,
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800434 struct device_attribute *devattr,
435 char *buf)
436{
437 struct adt7470_data *data = adt7470_update_device(dev);
438 return sprintf(buf, "%d\n", data->num_temp_sensors);
439}
440
Julia Lawall808fc6c2016-12-22 13:04:34 +0100441static ssize_t num_temp_sensors_store(struct device *dev,
442 struct device_attribute *devattr,
443 const char *buf, size_t count)
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800444{
Axel Lin30485772014-07-16 23:12:54 +0800445 struct adt7470_data *data = dev_get_drvdata(dev);
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800446 long temp;
447
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100448 if (kstrtol(buf, 10, &temp))
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800449 return -EINVAL;
450
Guenter Roeck2a844c12013-01-09 08:09:34 -0800451 temp = clamp_val(temp, -1, 10);
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800452
453 mutex_lock(&data->lock);
454 data->num_temp_sensors = temp;
Darrick J. Wong89fac112009-01-06 14:41:34 -0800455 if (temp < 0)
456 data->temperatures_probed = 0;
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800457 mutex_unlock(&data->lock);
458
459 return count;
460}
461
Guenter Roeck42291a52018-12-10 14:02:02 -0800462static ssize_t temp_min_show(struct device *dev,
463 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700464{
465 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
466 struct adt7470_data *data = adt7470_update_device(dev);
467 return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
468}
469
Guenter Roeck42291a52018-12-10 14:02:02 -0800470static ssize_t temp_min_store(struct device *dev,
471 struct device_attribute *devattr,
472 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700473{
474 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800475 struct adt7470_data *data = dev_get_drvdata(dev);
476 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800477 long temp;
478
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100479 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800480 return -EINVAL;
481
Guenter Roeck64bd7082016-12-03 11:10:34 -0800482 temp = clamp_val(temp, -128000, 127000);
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800483 temp = DIV_ROUND_CLOSEST(temp, 1000);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700484
485 mutex_lock(&data->lock);
486 data->temp_min[attr->index] = temp;
487 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
488 temp);
489 mutex_unlock(&data->lock);
490
491 return count;
492}
493
Guenter Roeck42291a52018-12-10 14:02:02 -0800494static ssize_t temp_max_show(struct device *dev,
495 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700496{
497 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
498 struct adt7470_data *data = adt7470_update_device(dev);
499 return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
500}
501
Guenter Roeck42291a52018-12-10 14:02:02 -0800502static ssize_t temp_max_store(struct device *dev,
503 struct device_attribute *devattr,
504 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700505{
506 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800507 struct adt7470_data *data = dev_get_drvdata(dev);
508 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800509 long temp;
510
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100511 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800512 return -EINVAL;
513
Guenter Roeck64bd7082016-12-03 11:10:34 -0800514 temp = clamp_val(temp, -128000, 127000);
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800515 temp = DIV_ROUND_CLOSEST(temp, 1000);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700516
517 mutex_lock(&data->lock);
518 data->temp_max[attr->index] = temp;
519 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
520 temp);
521 mutex_unlock(&data->lock);
522
523 return count;
524}
525
Guenter Roeck42291a52018-12-10 14:02:02 -0800526static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700527 char *buf)
528{
529 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
530 struct adt7470_data *data = adt7470_update_device(dev);
531 return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
532}
533
Julia Lawall808fc6c2016-12-22 13:04:34 +0100534static ssize_t alarm_mask_show(struct device *dev,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700535 struct device_attribute *devattr,
536 char *buf)
537{
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700538 struct adt7470_data *data = adt7470_update_device(dev);
539
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800540 return sprintf(buf, "%x\n", data->alarms_mask);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700541}
542
Julia Lawall808fc6c2016-12-22 13:04:34 +0100543static ssize_t alarm_mask_store(struct device *dev,
544 struct device_attribute *devattr,
545 const char *buf, size_t count)
Joshua Scottfeca3132016-09-09 17:19:25 +1200546{
547 struct adt7470_data *data = dev_get_drvdata(dev);
548 long mask;
549
550 if (kstrtoul(buf, 0, &mask))
551 return -EINVAL;
552
553 if (mask & ~0xffff)
554 return -EINVAL;
555
556 mutex_lock(&data->lock);
557 data->alarms_mask = mask;
558 adt7470_write_word_data(data->client, ADT7470_REG_ALARM1_MASK, mask);
559 mutex_unlock(&data->lock);
560
561 return count;
562}
563
Guenter Roeck42291a52018-12-10 14:02:02 -0800564static ssize_t fan_max_show(struct device *dev,
565 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700566{
567 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
568 struct adt7470_data *data = adt7470_update_device(dev);
569
570 if (FAN_DATA_VALID(data->fan_max[attr->index]))
571 return sprintf(buf, "%d\n",
572 FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
573 else
574 return sprintf(buf, "0\n");
575}
576
Guenter Roeck42291a52018-12-10 14:02:02 -0800577static ssize_t fan_max_store(struct device *dev,
578 struct device_attribute *devattr,
579 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700580{
581 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800582 struct adt7470_data *data = dev_get_drvdata(dev);
583 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800584 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700585
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100586 if (kstrtol(buf, 10, &temp) || !temp)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700587 return -EINVAL;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800588
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700589 temp = FAN_RPM_TO_PERIOD(temp);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800590 temp = clamp_val(temp, 1, 65534);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700591
592 mutex_lock(&data->lock);
593 data->fan_max[attr->index] = temp;
594 adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
595 mutex_unlock(&data->lock);
596
597 return count;
598}
599
Guenter Roeck42291a52018-12-10 14:02:02 -0800600static ssize_t fan_min_show(struct device *dev,
601 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700602{
603 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
604 struct adt7470_data *data = adt7470_update_device(dev);
605
606 if (FAN_DATA_VALID(data->fan_min[attr->index]))
607 return sprintf(buf, "%d\n",
608 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
609 else
610 return sprintf(buf, "0\n");
611}
612
Guenter Roeck42291a52018-12-10 14:02:02 -0800613static ssize_t fan_min_store(struct device *dev,
614 struct device_attribute *devattr,
615 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700616{
617 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800618 struct adt7470_data *data = dev_get_drvdata(dev);
619 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800620 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700621
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100622 if (kstrtol(buf, 10, &temp) || !temp)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700623 return -EINVAL;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800624
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700625 temp = FAN_RPM_TO_PERIOD(temp);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800626 temp = clamp_val(temp, 1, 65534);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700627
628 mutex_lock(&data->lock);
629 data->fan_min[attr->index] = temp;
630 adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
631 mutex_unlock(&data->lock);
632
633 return count;
634}
635
Guenter Roeck42291a52018-12-10 14:02:02 -0800636static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700637 char *buf)
638{
639 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
640 struct adt7470_data *data = adt7470_update_device(dev);
641
642 if (FAN_DATA_VALID(data->fan[attr->index]))
643 return sprintf(buf, "%d\n",
644 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
645 else
646 return sprintf(buf, "0\n");
647}
648
Guenter Roeck42291a52018-12-10 14:02:02 -0800649static ssize_t force_pwm_max_show(struct device *dev,
650 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700651{
652 struct adt7470_data *data = adt7470_update_device(dev);
653 return sprintf(buf, "%d\n", data->force_pwm_max);
654}
655
Guenter Roeck42291a52018-12-10 14:02:02 -0800656static ssize_t force_pwm_max_store(struct device *dev,
657 struct device_attribute *devattr,
658 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700659{
Axel Lin30485772014-07-16 23:12:54 +0800660 struct adt7470_data *data = dev_get_drvdata(dev);
661 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800662 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700663 u8 reg;
664
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100665 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800666 return -EINVAL;
667
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700668 mutex_lock(&data->lock);
669 data->force_pwm_max = temp;
670 reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
671 if (temp)
672 reg |= ADT7470_FSPD_MASK;
673 else
674 reg &= ~ADT7470_FSPD_MASK;
675 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
676 mutex_unlock(&data->lock);
677
678 return count;
679}
680
Guenter Roeck42291a52018-12-10 14:02:02 -0800681static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700682 char *buf)
683{
684 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
685 struct adt7470_data *data = adt7470_update_device(dev);
686 return sprintf(buf, "%d\n", data->pwm[attr->index]);
687}
688
Guenter Roeck42291a52018-12-10 14:02:02 -0800689static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
690 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700691{
692 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800693 struct adt7470_data *data = dev_get_drvdata(dev);
694 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800695 long temp;
696
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100697 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800698 return -EINVAL;
699
Guenter Roeck2a844c12013-01-09 08:09:34 -0800700 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700701
702 mutex_lock(&data->lock);
703 data->pwm[attr->index] = temp;
704 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
705 mutex_unlock(&data->lock);
706
707 return count;
708}
709
Joshua Scottaa18cc92016-08-08 13:35:45 +1200710/* These are the valid PWM frequencies to the nearest Hz */
711static const int adt7470_freq_map[] = {
712 11, 15, 22, 29, 35, 44, 59, 88, 1400, 22500
713};
714
Julia Lawall808fc6c2016-12-22 13:04:34 +0100715static ssize_t pwm1_freq_show(struct device *dev,
716 struct device_attribute *devattr, char *buf)
Joshua Scottaa18cc92016-08-08 13:35:45 +1200717{
718 struct adt7470_data *data = adt7470_update_device(dev);
719 unsigned char cfg_reg_1;
720 unsigned char cfg_reg_2;
721 int index;
722
723 mutex_lock(&data->lock);
724 cfg_reg_1 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG);
725 cfg_reg_2 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG_2);
726 mutex_unlock(&data->lock);
727
728 index = (cfg_reg_2 & ADT7470_FREQ_MASK) >> ADT7470_FREQ_SHIFT;
729 if (!(cfg_reg_1 & ADT7470_CFG_LF))
730 index += 8;
731 if (index >= ARRAY_SIZE(adt7470_freq_map))
732 index = ARRAY_SIZE(adt7470_freq_map) - 1;
733
734 return scnprintf(buf, PAGE_SIZE, "%d\n", adt7470_freq_map[index]);
735}
736
Julia Lawall808fc6c2016-12-22 13:04:34 +0100737static ssize_t pwm1_freq_store(struct device *dev,
738 struct device_attribute *devattr,
739 const char *buf, size_t count)
Joshua Scottaa18cc92016-08-08 13:35:45 +1200740{
741 struct adt7470_data *data = dev_get_drvdata(dev);
742 struct i2c_client *client = data->client;
743 long freq;
744 int index;
745 int low_freq = ADT7470_CFG_LF;
746 unsigned char val;
747
748 if (kstrtol(buf, 10, &freq))
749 return -EINVAL;
750
751 /* Round the user value given to the closest available frequency */
752 index = find_closest(freq, adt7470_freq_map,
753 ARRAY_SIZE(adt7470_freq_map));
754
755 if (index >= 8) {
756 index -= 8;
757 low_freq = 0;
758 }
759
760 mutex_lock(&data->lock);
761 /* Configuration Register 1 */
762 val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
763 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG,
764 (val & ~ADT7470_CFG_LF) | low_freq);
765 /* Configuration Register 2 */
766 val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG_2);
767 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG_2,
768 (val & ~ADT7470_FREQ_MASK) | (index << ADT7470_FREQ_SHIFT));
769 mutex_unlock(&data->lock);
770
771 return count;
772}
773
Guenter Roeck42291a52018-12-10 14:02:02 -0800774static ssize_t pwm_max_show(struct device *dev,
775 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700776{
777 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
778 struct adt7470_data *data = adt7470_update_device(dev);
779 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
780}
781
Guenter Roeck42291a52018-12-10 14:02:02 -0800782static ssize_t pwm_max_store(struct device *dev,
783 struct device_attribute *devattr,
784 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700785{
786 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800787 struct adt7470_data *data = dev_get_drvdata(dev);
788 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800789 long temp;
790
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100791 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800792 return -EINVAL;
793
Guenter Roeck2a844c12013-01-09 08:09:34 -0800794 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700795
796 mutex_lock(&data->lock);
797 data->pwm_max[attr->index] = temp;
798 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
799 temp);
800 mutex_unlock(&data->lock);
801
802 return count;
803}
804
Guenter Roeck42291a52018-12-10 14:02:02 -0800805static ssize_t pwm_min_show(struct device *dev,
806 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700807{
808 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
809 struct adt7470_data *data = adt7470_update_device(dev);
810 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
811}
812
Guenter Roeck42291a52018-12-10 14:02:02 -0800813static ssize_t pwm_min_store(struct device *dev,
814 struct device_attribute *devattr,
815 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700816{
817 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800818 struct adt7470_data *data = dev_get_drvdata(dev);
819 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800820 long temp;
821
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100822 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800823 return -EINVAL;
824
Guenter Roeck2a844c12013-01-09 08:09:34 -0800825 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700826
827 mutex_lock(&data->lock);
828 data->pwm_min[attr->index] = temp;
829 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
830 temp);
831 mutex_unlock(&data->lock);
832
833 return count;
834}
835
Guenter Roeck42291a52018-12-10 14:02:02 -0800836static ssize_t pwm_tmax_show(struct device *dev,
837 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700838{
839 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
840 struct adt7470_data *data = adt7470_update_device(dev);
841 /* the datasheet says that tmax = tmin + 20C */
842 return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
843}
844
Guenter Roeck42291a52018-12-10 14:02:02 -0800845static ssize_t pwm_tmin_show(struct device *dev,
846 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700847{
848 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
849 struct adt7470_data *data = adt7470_update_device(dev);
850 return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
851}
852
Guenter Roeck42291a52018-12-10 14:02:02 -0800853static ssize_t pwm_tmin_store(struct device *dev,
854 struct device_attribute *devattr,
855 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700856{
857 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800858 struct adt7470_data *data = dev_get_drvdata(dev);
859 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800860 long temp;
861
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100862 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800863 return -EINVAL;
864
Guenter Roeck64bd7082016-12-03 11:10:34 -0800865 temp = clamp_val(temp, -128000, 127000);
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800866 temp = DIV_ROUND_CLOSEST(temp, 1000);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700867
868 mutex_lock(&data->lock);
869 data->pwm_tmin[attr->index] = temp;
870 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
871 temp);
872 mutex_unlock(&data->lock);
873
874 return count;
875}
876
Guenter Roeck42291a52018-12-10 14:02:02 -0800877static ssize_t pwm_auto_show(struct device *dev,
878 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700879{
880 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
881 struct adt7470_data *data = adt7470_update_device(dev);
882 return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
883}
884
Guenter Roeck42291a52018-12-10 14:02:02 -0800885static ssize_t pwm_auto_store(struct device *dev,
886 struct device_attribute *devattr,
887 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700888{
889 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800890 struct adt7470_data *data = dev_get_drvdata(dev);
891 struct i2c_client *client = data->client;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700892 int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
893 int pwm_auto_reg_mask;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800894 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700895 u8 reg;
896
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100897 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800898 return -EINVAL;
899
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700900 if (attr->index % 2)
901 pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
902 else
903 pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
904
905 if (temp != 2 && temp != 1)
906 return -EINVAL;
907 temp--;
908
909 mutex_lock(&data->lock);
910 data->pwm_automatic[attr->index] = temp;
911 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
912 if (temp)
913 reg |= pwm_auto_reg_mask;
914 else
915 reg &= ~pwm_auto_reg_mask;
916 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
917 mutex_unlock(&data->lock);
918
919 return count;
920}
921
Guenter Roeck42291a52018-12-10 14:02:02 -0800922static ssize_t pwm_auto_temp_show(struct device *dev,
923 struct device_attribute *devattr, char *buf)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700924{
925 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
926 struct adt7470_data *data = adt7470_update_device(dev);
927 u8 ctrl = data->pwm_auto_temp[attr->index];
928
929 if (ctrl)
930 return sprintf(buf, "%d\n", 1 << (ctrl - 1));
931 else
932 return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
933}
934
935static int cvt_auto_temp(int input)
936{
937 if (input == ADT7470_PWM_ALL_TEMPS)
938 return 0;
Robert P. J. Dayce9c2f42007-11-06 03:21:42 -0500939 if (input < 1 || !is_power_of_2(input))
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700940 return -EINVAL;
941 return ilog2(input) + 1;
942}
943
Guenter Roeck42291a52018-12-10 14:02:02 -0800944static ssize_t pwm_auto_temp_store(struct device *dev,
945 struct device_attribute *devattr,
946 const char *buf, size_t count)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700947{
948 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800949 struct adt7470_data *data = dev_get_drvdata(dev);
950 struct i2c_client *client = data->client;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700951 int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800952 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700953 u8 reg;
954
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100955 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800956 return -EINVAL;
957
958 temp = cvt_auto_temp(temp);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700959 if (temp < 0)
960 return temp;
961
962 mutex_lock(&data->lock);
963 data->pwm_automatic[attr->index] = temp;
964 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
965
966 if (!(attr->index % 2)) {
967 reg &= 0xF;
968 reg |= (temp << 4) & 0xF0;
969 } else {
970 reg &= 0xF0;
971 reg |= temp & 0xF;
972 }
973
974 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
975 mutex_unlock(&data->lock);
976
977 return count;
978}
979
Guenter Roeck42291a52018-12-10 14:02:02 -0800980static ssize_t alarm_show(struct device *dev,
981 struct device_attribute *devattr, char *buf)
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800982{
983 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
984 struct adt7470_data *data = adt7470_update_device(dev);
985
986 if (data->alarm & attr->index)
987 return sprintf(buf, "1\n");
988 else
989 return sprintf(buf, "0\n");
990}
991
Julia Lawall808fc6c2016-12-22 13:04:34 +0100992static DEVICE_ATTR_RW(alarm_mask);
993static DEVICE_ATTR_RW(num_temp_sensors);
994static DEVICE_ATTR_RW(auto_update_interval);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700995
Guenter Roeck42291a52018-12-10 14:02:02 -0800996static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
997static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
998static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
999static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
1000static SENSOR_DEVICE_ATTR_RW(temp5_max, temp_max, 4);
1001static SENSOR_DEVICE_ATTR_RW(temp6_max, temp_max, 5);
1002static SENSOR_DEVICE_ATTR_RW(temp7_max, temp_max, 6);
1003static SENSOR_DEVICE_ATTR_RW(temp8_max, temp_max, 7);
1004static SENSOR_DEVICE_ATTR_RW(temp9_max, temp_max, 8);
1005static SENSOR_DEVICE_ATTR_RW(temp10_max, temp_max, 9);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001006
Guenter Roeck42291a52018-12-10 14:02:02 -08001007static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
1008static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
1009static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
1010static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3);
1011static SENSOR_DEVICE_ATTR_RW(temp5_min, temp_min, 4);
1012static SENSOR_DEVICE_ATTR_RW(temp6_min, temp_min, 5);
1013static SENSOR_DEVICE_ATTR_RW(temp7_min, temp_min, 6);
1014static SENSOR_DEVICE_ATTR_RW(temp8_min, temp_min, 7);
1015static SENSOR_DEVICE_ATTR_RW(temp9_min, temp_min, 8);
1016static SENSOR_DEVICE_ATTR_RW(temp10_min, temp_min, 9);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001017
Guenter Roeck42291a52018-12-10 14:02:02 -08001018static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
1019static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
1020static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
1021static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
1022static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
1023static SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5);
1024static SENSOR_DEVICE_ATTR_RO(temp7_input, temp, 6);
1025static SENSOR_DEVICE_ATTR_RO(temp8_input, temp, 7);
1026static SENSOR_DEVICE_ATTR_RO(temp9_input, temp, 8);
1027static SENSOR_DEVICE_ATTR_RO(temp10_input, temp, 9);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001028
Guenter Roeck42291a52018-12-10 14:02:02 -08001029static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, ADT7470_R1T_ALARM);
1030static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, ADT7470_R2T_ALARM);
1031static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, ADT7470_R3T_ALARM);
1032static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm, ADT7470_R4T_ALARM);
1033static SENSOR_DEVICE_ATTR_RO(temp5_alarm, alarm, ADT7470_R5T_ALARM);
1034static SENSOR_DEVICE_ATTR_RO(temp6_alarm, alarm, ADT7470_R6T_ALARM);
1035static SENSOR_DEVICE_ATTR_RO(temp7_alarm, alarm, ADT7470_R7T_ALARM);
1036static SENSOR_DEVICE_ATTR_RO(temp8_alarm, alarm, ALARM2(ADT7470_R8T_ALARM));
1037static SENSOR_DEVICE_ATTR_RO(temp9_alarm, alarm, ALARM2(ADT7470_R9T_ALARM));
1038static SENSOR_DEVICE_ATTR_RO(temp10_alarm, alarm, ALARM2(ADT7470_R10T_ALARM));
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001039
Guenter Roeck42291a52018-12-10 14:02:02 -08001040static SENSOR_DEVICE_ATTR_RW(fan1_max, fan_max, 0);
1041static SENSOR_DEVICE_ATTR_RW(fan2_max, fan_max, 1);
1042static SENSOR_DEVICE_ATTR_RW(fan3_max, fan_max, 2);
1043static SENSOR_DEVICE_ATTR_RW(fan4_max, fan_max, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001044
Guenter Roeck42291a52018-12-10 14:02:02 -08001045static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
1046static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
1047static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
1048static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001049
Guenter Roeck42291a52018-12-10 14:02:02 -08001050static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
1051static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
1052static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
1053static SENSOR_DEVICE_ATTR_RO(fan4_input, fan, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001054
Guenter Roeck42291a52018-12-10 14:02:02 -08001055static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, ALARM2(ADT7470_FAN1_ALARM));
1056static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, ALARM2(ADT7470_FAN2_ALARM));
1057static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, ALARM2(ADT7470_FAN3_ALARM));
1058static SENSOR_DEVICE_ATTR_RO(fan4_alarm, alarm, ALARM2(ADT7470_FAN4_ALARM));
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001059
Guenter Roeck42291a52018-12-10 14:02:02 -08001060static SENSOR_DEVICE_ATTR_RW(force_pwm_max, force_pwm_max, 0);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001061
Guenter Roeck42291a52018-12-10 14:02:02 -08001062static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
1063static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
1064static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
1065static SENSOR_DEVICE_ATTR_RW(pwm4, pwm, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001066
Julia Lawall808fc6c2016-12-22 13:04:34 +01001067static DEVICE_ATTR_RW(pwm1_freq);
Joshua Scottaa18cc92016-08-08 13:35:45 +12001068
Guenter Roeck42291a52018-12-10 14:02:02 -08001069static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm_min, 0);
1070static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm_min, 1);
1071static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm_min, 2);
1072static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_pwm, pwm_min, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001073
Guenter Roeck42291a52018-12-10 14:02:02 -08001074static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm_max, 0);
1075static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm_max, 1);
1076static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm_max, 2);
1077static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point2_pwm, pwm_max, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001078
Guenter Roeck42291a52018-12-10 14:02:02 -08001079static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_tmin, 0);
1080static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_temp, pwm_tmin, 1);
1081static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_temp, pwm_tmin, 2);
1082static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_temp, pwm_tmin, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001083
Guenter Roeck42291a52018-12-10 14:02:02 -08001084static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_temp, pwm_tmax, 0);
1085static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point2_temp, pwm_tmax, 1);
1086static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point2_temp, pwm_tmax, 2);
1087static SENSOR_DEVICE_ATTR_RO(pwm4_auto_point2_temp, pwm_tmax, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001088
Guenter Roeck42291a52018-12-10 14:02:02 -08001089static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_auto, 0);
1090static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_auto, 1);
1091static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_auto, 2);
1092static SENSOR_DEVICE_ATTR_RW(pwm4_enable, pwm_auto, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001093
Guenter Roeck42291a52018-12-10 14:02:02 -08001094static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels_temp, pwm_auto_temp, 0);
1095static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels_temp, pwm_auto_temp, 1);
1096static SENSOR_DEVICE_ATTR_RW(pwm3_auto_channels_temp, pwm_auto_temp, 2);
1097static SENSOR_DEVICE_ATTR_RW(pwm4_auto_channels_temp, pwm_auto_temp, 3);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001098
Axel Lin30485772014-07-16 23:12:54 +08001099static struct attribute *adt7470_attrs[] = {
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001100 &dev_attr_alarm_mask.attr,
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001101 &dev_attr_num_temp_sensors.attr,
Darrick J. Wong89fac112009-01-06 14:41:34 -08001102 &dev_attr_auto_update_interval.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001103 &sensor_dev_attr_temp1_max.dev_attr.attr,
1104 &sensor_dev_attr_temp2_max.dev_attr.attr,
1105 &sensor_dev_attr_temp3_max.dev_attr.attr,
1106 &sensor_dev_attr_temp4_max.dev_attr.attr,
1107 &sensor_dev_attr_temp5_max.dev_attr.attr,
1108 &sensor_dev_attr_temp6_max.dev_attr.attr,
1109 &sensor_dev_attr_temp7_max.dev_attr.attr,
1110 &sensor_dev_attr_temp8_max.dev_attr.attr,
1111 &sensor_dev_attr_temp9_max.dev_attr.attr,
1112 &sensor_dev_attr_temp10_max.dev_attr.attr,
1113 &sensor_dev_attr_temp1_min.dev_attr.attr,
1114 &sensor_dev_attr_temp2_min.dev_attr.attr,
1115 &sensor_dev_attr_temp3_min.dev_attr.attr,
1116 &sensor_dev_attr_temp4_min.dev_attr.attr,
1117 &sensor_dev_attr_temp5_min.dev_attr.attr,
1118 &sensor_dev_attr_temp6_min.dev_attr.attr,
1119 &sensor_dev_attr_temp7_min.dev_attr.attr,
1120 &sensor_dev_attr_temp8_min.dev_attr.attr,
1121 &sensor_dev_attr_temp9_min.dev_attr.attr,
1122 &sensor_dev_attr_temp10_min.dev_attr.attr,
1123 &sensor_dev_attr_temp1_input.dev_attr.attr,
1124 &sensor_dev_attr_temp2_input.dev_attr.attr,
1125 &sensor_dev_attr_temp3_input.dev_attr.attr,
1126 &sensor_dev_attr_temp4_input.dev_attr.attr,
1127 &sensor_dev_attr_temp5_input.dev_attr.attr,
1128 &sensor_dev_attr_temp6_input.dev_attr.attr,
1129 &sensor_dev_attr_temp7_input.dev_attr.attr,
1130 &sensor_dev_attr_temp8_input.dev_attr.attr,
1131 &sensor_dev_attr_temp9_input.dev_attr.attr,
1132 &sensor_dev_attr_temp10_input.dev_attr.attr,
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001133 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1134 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1135 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1136 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
1137 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
1138 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
1139 &sensor_dev_attr_temp7_alarm.dev_attr.attr,
1140 &sensor_dev_attr_temp8_alarm.dev_attr.attr,
1141 &sensor_dev_attr_temp9_alarm.dev_attr.attr,
1142 &sensor_dev_attr_temp10_alarm.dev_attr.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001143 &sensor_dev_attr_fan1_max.dev_attr.attr,
1144 &sensor_dev_attr_fan2_max.dev_attr.attr,
1145 &sensor_dev_attr_fan3_max.dev_attr.attr,
1146 &sensor_dev_attr_fan4_max.dev_attr.attr,
1147 &sensor_dev_attr_fan1_min.dev_attr.attr,
1148 &sensor_dev_attr_fan2_min.dev_attr.attr,
1149 &sensor_dev_attr_fan3_min.dev_attr.attr,
1150 &sensor_dev_attr_fan4_min.dev_attr.attr,
1151 &sensor_dev_attr_fan1_input.dev_attr.attr,
1152 &sensor_dev_attr_fan2_input.dev_attr.attr,
1153 &sensor_dev_attr_fan3_input.dev_attr.attr,
1154 &sensor_dev_attr_fan4_input.dev_attr.attr,
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001155 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1156 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1157 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1158 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001159 &sensor_dev_attr_force_pwm_max.dev_attr.attr,
1160 &sensor_dev_attr_pwm1.dev_attr.attr,
Joshua Scottaa18cc92016-08-08 13:35:45 +12001161 &dev_attr_pwm1_freq.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001162 &sensor_dev_attr_pwm2.dev_attr.attr,
1163 &sensor_dev_attr_pwm3.dev_attr.attr,
1164 &sensor_dev_attr_pwm4.dev_attr.attr,
1165 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1166 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1167 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1168 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1169 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1170 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1171 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1172 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1173 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1174 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1175 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1176 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
1177 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1178 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1179 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1180 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
1181 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1182 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1183 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1184 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1185 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1186 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1187 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1188 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1189 NULL
1190};
1191
Axel Lin30485772014-07-16 23:12:54 +08001192ATTRIBUTE_GROUPS(adt7470);
1193
Jean Delvare008f1ca2008-07-16 19:30:10 +02001194/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001195static int adt7470_detect(struct i2c_client *client,
Jean Delvare008f1ca2008-07-16 19:30:10 +02001196 struct i2c_board_info *info)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001197{
Jean Delvare008f1ca2008-07-16 19:30:10 +02001198 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +01001199 int vendor, device, revision;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001200
1201 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare008f1ca2008-07-16 19:30:10 +02001202 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001203
Jean Delvare52df6442009-12-09 20:35:57 +01001204 vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1205 if (vendor != ADT7470_VENDOR)
1206 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001207
Jean Delvare52df6442009-12-09 20:35:57 +01001208 device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1209 if (device != ADT7470_DEVICE)
1210 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001211
Jean Delvare52df6442009-12-09 20:35:57 +01001212 revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION);
1213 if (revision != ADT7470_REVISION)
1214 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001215
Jean Delvare008f1ca2008-07-16 19:30:10 +02001216 strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001217
Jean Delvare008f1ca2008-07-16 19:30:10 +02001218 return 0;
1219}
1220
Axel Lin9027d932014-07-16 23:12:05 +08001221static void adt7470_init_client(struct i2c_client *client)
1222{
1223 int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
1224
1225 if (reg < 0) {
1226 dev_err(&client->dev, "cannot read configuration register\n");
1227 } else {
1228 /* start monitoring (and do a self-test) */
1229 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
1230 }
1231}
1232
Jean Delvare008f1ca2008-07-16 19:30:10 +02001233static int adt7470_probe(struct i2c_client *client,
1234 const struct i2c_device_id *id)
1235{
Axel Lin30485772014-07-16 23:12:54 +08001236 struct device *dev = &client->dev;
Jean Delvare008f1ca2008-07-16 19:30:10 +02001237 struct adt7470_data *data;
Axel Lin30485772014-07-16 23:12:54 +08001238 struct device *hwmon_dev;
Jean Delvare008f1ca2008-07-16 19:30:10 +02001239
Axel Lin30485772014-07-16 23:12:54 +08001240 data = devm_kzalloc(dev, sizeof(struct adt7470_data), GFP_KERNEL);
Guenter Roeck9cc7dcc2012-06-02 09:58:01 -07001241 if (!data)
1242 return -ENOMEM;
Jean Delvare008f1ca2008-07-16 19:30:10 +02001243
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001244 data->num_temp_sensors = -1;
Darrick J. Wong89fac112009-01-06 14:41:34 -08001245 data->auto_update_interval = AUTO_UPDATE_INTERVAL;
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001246
Jean Delvare008f1ca2008-07-16 19:30:10 +02001247 i2c_set_clientdata(client, data);
Axel Lin30485772014-07-16 23:12:54 +08001248 data->client = client;
Jean Delvare008f1ca2008-07-16 19:30:10 +02001249 mutex_init(&data->lock);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001250
1251 dev_info(&client->dev, "%s chip found\n", client->name);
1252
1253 /* Initialize the ADT7470 chip */
1254 adt7470_init_client(client);
1255
1256 /* Register sysfs hooks */
Axel Lin30485772014-07-16 23:12:54 +08001257 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1258 data,
1259 adt7470_groups);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001260
Axel Lin30485772014-07-16 23:12:54 +08001261 if (IS_ERR(hwmon_dev))
1262 return PTR_ERR(hwmon_dev);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001263
Kees Cookf1701682013-07-03 15:04:58 -07001264 data->auto_update = kthread_run(adt7470_update_thread, client, "%s",
Axel Lin30485772014-07-16 23:12:54 +08001265 dev_name(hwmon_dev));
Axel Linf7334b42010-11-08 00:11:33 -05001266 if (IS_ERR(data->auto_update)) {
Axel Lin30485772014-07-16 23:12:54 +08001267 return PTR_ERR(data->auto_update);
Axel Linf7334b42010-11-08 00:11:33 -05001268 }
Darrick J. Wong89fac112009-01-06 14:41:34 -08001269
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001270 return 0;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001271}
1272
Jean Delvare008f1ca2008-07-16 19:30:10 +02001273static int adt7470_remove(struct i2c_client *client)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001274{
1275 struct adt7470_data *data = i2c_get_clientdata(client);
1276
Darrick J. Wong89fac112009-01-06 14:41:34 -08001277 kthread_stop(data->auto_update);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001278 return 0;
1279}
1280
Axel Lin9027d932014-07-16 23:12:05 +08001281static const struct i2c_device_id adt7470_id[] = {
1282 { "adt7470", 0 },
1283 { }
1284};
1285MODULE_DEVICE_TABLE(i2c, adt7470_id);
1286
1287static struct i2c_driver adt7470_driver = {
1288 .class = I2C_CLASS_HWMON,
1289 .driver = {
1290 .name = "adt7470",
1291 },
1292 .probe = adt7470_probe,
1293 .remove = adt7470_remove,
1294 .id_table = adt7470_id,
1295 .detect = adt7470_detect,
1296 .address_list = normal_i2c,
1297};
1298
Axel Linf0967ee2012-01-20 15:38:18 +08001299module_i2c_driver(adt7470_driver);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001300
Darrick J. Wong5407e0512013-08-26 15:42:27 -07001301MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001302MODULE_DESCRIPTION("ADT7470 driver");
1303MODULE_LICENSE("GPL");