blob: b90bdc908e5e09e231c4fbf45b4a85aceae8cb12 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lm63.c - driver for the National Semiconductor LM63 temperature sensor
3 * with integrated fan control
Jean Delvared5957be2008-07-16 19:30:13 +02004 * Copyright (C) 2004-2008 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Based on the lm90 driver.
6 *
7 * The LM63 is a sensor chip made by National Semiconductor. It measures
8 * two temperatures (its own and one external one) and the speed of one
9 * fan, those speed it can additionally control. Complete datasheet can be
10 * obtained from National's website at:
11 * http://www.national.com/pf/LM/LM63.html
12 *
13 * The LM63 is basically an LM86 with fan speed monitoring and control
14 * capabilities added. It misses some of the LM86 features though:
15 * - No low limit for local temperature.
16 * - No critical limit for local temperature.
17 * - Critical limit for remote temperature can be changed only once. We
18 * will consider that the critical limit is read-only.
19 *
20 * The datasheet isn't very clear about what the tachometer reading is.
21 * I had a explanation from National Semiconductor though. The two lower
22 * bits of the read value have to be masked out. The value is still 16 bit
23 * in width.
24 *
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 */
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/module.h>
41#include <linux/init.h>
42#include <linux/slab.h>
43#include <linux/jiffies.h>
44#include <linux/i2c.h>
Jean Delvare10c08f82005-06-06 19:34:45 +020045#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040046#include <linux/hwmon.h>
47#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010048#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020049#include <linux/sysfs.h>
Guenter Roeck210961c2012-01-16 22:51:45 +010050#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
53 * Addresses to scan
Jean Delvared93ab782012-01-16 22:51:47 +010054 * Address is fully defined internally and cannot be changed except for
55 * LM64 which has one pin dedicated to address selection.
56 * LM63 and LM96163 have address 0x4c.
57 * LM64 can have address 0x18 or 0x4e.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59
Matthew Garrett10f2ed32010-05-27 19:58:38 +020060static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * The LM63 registers
64 */
65
66#define LM63_REG_CONFIG1 0x03
Guenter Roeck04738b22012-01-16 22:51:46 +010067#define LM63_REG_CONVRATE 0x04
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define LM63_REG_CONFIG2 0xBF
69#define LM63_REG_CONFIG_FAN 0x4A
70
71#define LM63_REG_TACH_COUNT_MSB 0x47
72#define LM63_REG_TACH_COUNT_LSB 0x46
73#define LM63_REG_TACH_LIMIT_MSB 0x49
74#define LM63_REG_TACH_LIMIT_LSB 0x48
75
76#define LM63_REG_PWM_VALUE 0x4C
77#define LM63_REG_PWM_FREQ 0x4D
Jean Delvared216f682012-01-16 22:51:47 +010078#define LM63_REG_LUT_TEMP_HYST 0x4F
79#define LM63_REG_LUT_TEMP(nr) (0x50 + 2 * (nr))
80#define LM63_REG_LUT_PWM(nr) (0x51 + 2 * (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82#define LM63_REG_LOCAL_TEMP 0x00
83#define LM63_REG_LOCAL_HIGH 0x05
84
85#define LM63_REG_REMOTE_TEMP_MSB 0x01
86#define LM63_REG_REMOTE_TEMP_LSB 0x10
87#define LM63_REG_REMOTE_OFFSET_MSB 0x11
88#define LM63_REG_REMOTE_OFFSET_LSB 0x12
89#define LM63_REG_REMOTE_HIGH_MSB 0x07
90#define LM63_REG_REMOTE_HIGH_LSB 0x13
91#define LM63_REG_REMOTE_LOW_MSB 0x08
92#define LM63_REG_REMOTE_LOW_LSB 0x14
93#define LM63_REG_REMOTE_TCRIT 0x19
94#define LM63_REG_REMOTE_TCRIT_HYST 0x21
95
96#define LM63_REG_ALERT_STATUS 0x02
97#define LM63_REG_ALERT_MASK 0x16
98
99#define LM63_REG_MAN_ID 0xFE
100#define LM63_REG_CHIP_ID 0xFF
101
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100102#define LM96163_REG_TRUTHERM 0x30
Guenter Roecke872c912012-01-16 22:51:46 +0100103#define LM96163_REG_REMOTE_TEMP_U_MSB 0x31
104#define LM96163_REG_REMOTE_TEMP_U_LSB 0x32
Guenter Roeck210961c2012-01-16 22:51:45 +0100105#define LM96163_REG_CONFIG_ENHANCED 0x45
106
Guenter Roeck04738b22012-01-16 22:51:46 +0100107#define LM63_MAX_CONVRATE 9
108
109#define LM63_MAX_CONVRATE_HZ 32
110#define LM96163_MAX_CONVRATE_HZ 26
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
113 * Conversions and various macros
114 * For tachometer counts, the LM63 uses 16-bit values.
115 * For local temperature and high limit, remote critical limit and hysteresis
Steven Cole44bbe872005-05-03 18:21:25 -0600116 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * For remote temperature, low and high limits, it uses signed 11-bit values
Steven Cole44bbe872005-05-03 18:21:25 -0600118 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
Dirk Eibach2778fb12011-02-09 04:51:34 -0500119 * For LM64 the actual remote diode temperature is 16 degree Celsius higher
120 * than the register reading. Remote temperature setpoints have to be
121 * adapted accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
123
124#define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
125 5400000 / (reg))
126#define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
127 (5400000 / (val)) & 0xFFFC)
128#define TEMP8_FROM_REG(reg) ((reg) * 1000)
129#define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
130 (val) >= 127000 ? 127 : \
131 (val) < 0 ? ((val) - 500) / 1000 : \
132 ((val) + 500) / 1000)
Guenter Roeck94e55df2012-01-16 22:51:46 +0100133#define TEMP8U_TO_REG(val) ((val) <= 0 ? 0 : \
134 (val) >= 255000 ? 255 : \
135 ((val) + 500) / 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
137#define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
138 (val) >= 127875 ? 0x7FE0 : \
139 (val) < 0 ? ((val) - 62) / 125 * 32 : \
140 ((val) + 62) / 125 * 32)
Guenter Roecke872c912012-01-16 22:51:46 +0100141#define TEMP11U_TO_REG(val) ((val) <= 0 ? 0 : \
142 (val) >= 255875 ? 0xFFE0 : \
143 ((val) + 62) / 125 * 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
145 (val) >= 127000 ? 127 : \
146 ((val) + 500) / 1000)
147
Guenter Roeck04738b22012-01-16 22:51:46 +0100148#define UPDATE_INTERVAL(max, rate) \
149 ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max))
150
Guenter Roeck210961c2012-01-16 22:51:45 +0100151enum chips { lm63, lm64, lm96163 };
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * Client data (each client gets its own)
155 */
156
157struct lm63_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700158 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100159 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 char valid; /* zero until following fields are valid */
Jean Delvared216f682012-01-16 22:51:47 +0100161 char lut_valid; /* zero until lut fields are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 unsigned long last_updated; /* in jiffies */
Jean Delvared216f682012-01-16 22:51:47 +0100163 unsigned long lut_last_updated; /* in jiffies */
Guenter Roeck04738b22012-01-16 22:51:46 +0100164 enum chips kind;
Dirk Eibach2778fb12011-02-09 04:51:34 -0500165 int temp2_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Guenter Roeck04738b22012-01-16 22:51:46 +0100167 int update_interval; /* in milliseconds */
168 int max_convrate_hz;
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100169 int lut_size; /* 8 or 12 */
Guenter Roeck04738b22012-01-16 22:51:46 +0100170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /* registers values */
172 u8 config, config_fan;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200173 u16 fan[2]; /* 0: input
174 1: low limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 u8 pwm1_freq;
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100176 u8 pwm1[13]; /* 0: current output
177 1-12: lookup table */
178 s8 temp8[15]; /* 0: local input
Jean Delvarebc51ae12005-06-05 20:32:27 +0200179 1: local high limit
Jean Delvared216f682012-01-16 22:51:47 +0100180 2: remote critical limit
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100181 3-14: lookup table */
Guenter Roeck786375f2012-01-16 22:51:45 +0100182 s16 temp11[4]; /* 0: remote input
Jean Delvarebc51ae12005-06-05 20:32:27 +0200183 1: remote low limit
Guenter Roeck786375f2012-01-16 22:51:45 +0100184 2: remote high limit
185 3: remote offset */
Guenter Roecke872c912012-01-16 22:51:46 +0100186 u16 temp11u; /* remote input (unsigned) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 u8 temp2_crit_hyst;
Jean Delvared216f682012-01-16 22:51:47 +0100188 u8 lut_temp_hyst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 u8 alarms;
Guenter Roeck210961c2012-01-16 22:51:45 +0100190 bool pwm_highres;
Jean Delvared216f682012-01-16 22:51:47 +0100191 bool lut_temp_highres;
Guenter Roecke872c912012-01-16 22:51:46 +0100192 bool remote_unsigned; /* true if unsigned remote upper limits */
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100193 bool trutherm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Guenter Roecke872c912012-01-16 22:51:46 +0100196static inline int temp8_from_reg(struct lm63_data *data, int nr)
197{
198 if (data->remote_unsigned)
199 return TEMP8_FROM_REG((u8)data->temp8[nr]);
200 return TEMP8_FROM_REG(data->temp8[nr]);
201}
202
Jean Delvared216f682012-01-16 22:51:47 +0100203static inline int lut_temp_from_reg(struct lm63_data *data, int nr)
204{
205 return data->temp8[nr] * (data->lut_temp_highres ? 500 : 1000);
206}
207
Jean Delvaredac27dc2012-03-23 10:02:18 +0100208static struct lm63_data *lm63_update_device(struct device *dev)
209{
210 struct i2c_client *client = to_i2c_client(dev);
211 struct lm63_data *data = i2c_get_clientdata(client);
212 unsigned long next_update;
213 int i;
214
215 mutex_lock(&data->update_lock);
216
217 next_update = data->last_updated
218 + msecs_to_jiffies(data->update_interval) + 1;
219
220 if (time_after(jiffies, next_update) || !data->valid) {
221 if (data->config & 0x04) { /* tachometer enabled */
222 /* order matters for fan1_input */
223 data->fan[0] = i2c_smbus_read_byte_data(client,
224 LM63_REG_TACH_COUNT_LSB) & 0xFC;
225 data->fan[0] |= i2c_smbus_read_byte_data(client,
226 LM63_REG_TACH_COUNT_MSB) << 8;
227 data->fan[1] = (i2c_smbus_read_byte_data(client,
228 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
229 | (i2c_smbus_read_byte_data(client,
230 LM63_REG_TACH_LIMIT_MSB) << 8);
231 }
232
233 data->pwm1_freq = i2c_smbus_read_byte_data(client,
234 LM63_REG_PWM_FREQ);
235 if (data->pwm1_freq == 0)
236 data->pwm1_freq = 1;
237 data->pwm1[0] = i2c_smbus_read_byte_data(client,
238 LM63_REG_PWM_VALUE);
239
240 data->temp8[0] = i2c_smbus_read_byte_data(client,
241 LM63_REG_LOCAL_TEMP);
242 data->temp8[1] = i2c_smbus_read_byte_data(client,
243 LM63_REG_LOCAL_HIGH);
244
245 /* order matters for temp2_input */
246 data->temp11[0] = i2c_smbus_read_byte_data(client,
247 LM63_REG_REMOTE_TEMP_MSB) << 8;
248 data->temp11[0] |= i2c_smbus_read_byte_data(client,
249 LM63_REG_REMOTE_TEMP_LSB);
250 data->temp11[1] = (i2c_smbus_read_byte_data(client,
251 LM63_REG_REMOTE_LOW_MSB) << 8)
252 | i2c_smbus_read_byte_data(client,
253 LM63_REG_REMOTE_LOW_LSB);
254 data->temp11[2] = (i2c_smbus_read_byte_data(client,
255 LM63_REG_REMOTE_HIGH_MSB) << 8)
256 | i2c_smbus_read_byte_data(client,
257 LM63_REG_REMOTE_HIGH_LSB);
258 data->temp11[3] = (i2c_smbus_read_byte_data(client,
259 LM63_REG_REMOTE_OFFSET_MSB) << 8)
260 | i2c_smbus_read_byte_data(client,
261 LM63_REG_REMOTE_OFFSET_LSB);
262
263 if (data->kind == lm96163)
264 data->temp11u = (i2c_smbus_read_byte_data(client,
265 LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
266 | i2c_smbus_read_byte_data(client,
267 LM96163_REG_REMOTE_TEMP_U_LSB);
268
269 data->temp8[2] = i2c_smbus_read_byte_data(client,
270 LM63_REG_REMOTE_TCRIT);
271 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
272 LM63_REG_REMOTE_TCRIT_HYST);
273
274 data->alarms = i2c_smbus_read_byte_data(client,
275 LM63_REG_ALERT_STATUS) & 0x7F;
276
277 data->last_updated = jiffies;
278 data->valid = 1;
279 }
280
281 if (time_after(jiffies, data->lut_last_updated + 5 * HZ) ||
282 !data->lut_valid) {
283 for (i = 0; i < data->lut_size; i++) {
284 data->pwm1[1 + i] = i2c_smbus_read_byte_data(client,
285 LM63_REG_LUT_PWM(i));
286 data->temp8[3 + i] = i2c_smbus_read_byte_data(client,
287 LM63_REG_LUT_TEMP(i));
288 }
289 data->lut_temp_hyst = i2c_smbus_read_byte_data(client,
290 LM63_REG_LUT_TEMP_HYST);
291
292 data->lut_last_updated = jiffies;
293 data->lut_valid = 1;
294 }
295
296 mutex_unlock(&data->update_lock);
297
298 return data;
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/*
302 * Sysfs callback functions and files
303 */
304
Jean Delvarebc51ae12005-06-05 20:32:27 +0200305static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
306 char *buf)
307{
308 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
309 struct lm63_data *data = lm63_update_device(dev);
310 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Jean Delvarebc51ae12005-06-05 20:32:27 +0200313static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
314 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct i2c_client *client = to_i2c_client(dev);
317 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck662bda22012-01-16 22:51:45 +0100318 unsigned long val;
319 int err;
320
321 err = kstrtoul(buf, 10, &val);
322 if (err)
323 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100325 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200326 data->fan[1] = FAN_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200328 data->fan[1] & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200330 data->fan[1] >> 8);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100331 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return count;
333}
334
Jean Delvared216f682012-01-16 22:51:47 +0100335static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200336 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Jean Delvared216f682012-01-16 22:51:47 +0100338 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct lm63_data *data = lm63_update_device(dev);
Jean Delvared216f682012-01-16 22:51:47 +0100340 int nr = attr->index;
Guenter Roeck210961c2012-01-16 22:51:45 +0100341 int pwm;
342
343 if (data->pwm_highres)
Jean Delvared216f682012-01-16 22:51:47 +0100344 pwm = data->pwm1[nr];
Guenter Roeck210961c2012-01-16 22:51:45 +0100345 else
Jean Delvared216f682012-01-16 22:51:47 +0100346 pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ?
347 255 : (data->pwm1[nr] * 255 + data->pwm1_freq) /
Guenter Roeck210961c2012-01-16 22:51:45 +0100348 (2 * data->pwm1_freq);
349
350 return sprintf(buf, "%d\n", pwm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Jean Delvarebc51ae12005-06-05 20:32:27 +0200353static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
354 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct i2c_client *client = to_i2c_client(dev);
357 struct lm63_data *data = i2c_get_clientdata(client);
358 unsigned long val;
Guenter Roeck662bda22012-01-16 22:51:45 +0100359 int err;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (!(data->config_fan & 0x20)) /* register is read-only */
362 return -EPERM;
363
Guenter Roeck662bda22012-01-16 22:51:45 +0100364 err = kstrtoul(buf, 10, &val);
365 if (err)
366 return err;
367
Guenter Roeck210961c2012-01-16 22:51:45 +0100368 val = SENSORS_LIMIT(val, 0, 255);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100369 mutex_lock(&data->update_lock);
Jean Delvared216f682012-01-16 22:51:47 +0100370 data->pwm1[0] = data->pwm_highres ? val :
371 (val * data->pwm1_freq * 2 + 127) / 255;
372 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100373 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return count;
375}
376
Guenter Roeck662bda22012-01-16 22:51:45 +0100377static ssize_t show_pwm1_enable(struct device *dev,
378 struct device_attribute *dummy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct lm63_data *data = lm63_update_device(dev);
381 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
382}
383
Dirk Eibach2778fb12011-02-09 04:51:34 -0500384/*
385 * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
386 * For remote sensor registers temp2_offset has to be considered,
387 * for local sensor it must not.
388 * So we need separate 8bit accessors for local and remote sensor.
389 */
390static ssize_t show_local_temp8(struct device *dev,
391 struct device_attribute *devattr,
392 char *buf)
Jean Delvarebc51ae12005-06-05 20:32:27 +0200393{
394 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
395 struct lm63_data *data = lm63_update_device(dev);
396 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Dirk Eibach2778fb12011-02-09 04:51:34 -0500399static ssize_t show_remote_temp8(struct device *dev,
400 struct device_attribute *devattr,
401 char *buf)
402{
403 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
404 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100405 return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
Dirk Eibach2778fb12011-02-09 04:51:34 -0500406 + data->temp2_offset);
407}
408
Jean Delvared216f682012-01-16 22:51:47 +0100409static ssize_t show_lut_temp(struct device *dev,
410 struct device_attribute *devattr,
411 char *buf)
412{
413 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
414 struct lm63_data *data = lm63_update_device(dev);
415 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
416 + data->temp2_offset);
417}
418
Guenter Roeck94e55df2012-01-16 22:51:46 +0100419static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
420 const char *buf, size_t count)
Jean Delvarebc51ae12005-06-05 20:32:27 +0200421{
Guenter Roeck94e55df2012-01-16 22:51:46 +0100422 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200423 struct i2c_client *client = to_i2c_client(dev);
424 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck94e55df2012-01-16 22:51:46 +0100425 int nr = attr->index;
426 int reg = nr == 2 ? LM63_REG_REMOTE_TCRIT : LM63_REG_LOCAL_HIGH;
Guenter Roeck662bda22012-01-16 22:51:45 +0100427 long val;
428 int err;
Guenter Roeck94e55df2012-01-16 22:51:46 +0100429 int temp;
Guenter Roeck662bda22012-01-16 22:51:45 +0100430
431 err = kstrtol(buf, 10, &val);
432 if (err)
433 return err;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200434
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100435 mutex_lock(&data->update_lock);
Guenter Roeck94e55df2012-01-16 22:51:46 +0100436 if (nr == 2) {
437 if (data->remote_unsigned)
438 temp = TEMP8U_TO_REG(val - data->temp2_offset);
439 else
440 temp = TEMP8_TO_REG(val - data->temp2_offset);
441 } else {
442 temp = TEMP8_TO_REG(val);
443 }
444 data->temp8[nr] = temp;
445 i2c_smbus_write_byte_data(client, reg, temp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100446 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200447 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200449
450static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
451 char *buf)
452{
453 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
454 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100455 int nr = attr->index;
456 int temp;
457
458 if (!nr) {
459 /*
460 * Use unsigned temperature unless its value is zero.
461 * If it is zero, use signed temperature.
462 */
463 if (data->temp11u)
464 temp = TEMP11_FROM_REG(data->temp11u);
465 else
466 temp = TEMP11_FROM_REG(data->temp11[nr]);
467 } else {
468 if (data->remote_unsigned && nr == 2)
469 temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
470 else
471 temp = TEMP11_FROM_REG(data->temp11[nr]);
472 }
473 return sprintf(buf, "%d\n", temp + data->temp2_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200475
476static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
477 const char *buf, size_t count)
478{
Guenter Roeck786375f2012-01-16 22:51:45 +0100479 static const u8 reg[6] = {
Jean Delvarebc51ae12005-06-05 20:32:27 +0200480 LM63_REG_REMOTE_LOW_MSB,
481 LM63_REG_REMOTE_LOW_LSB,
482 LM63_REG_REMOTE_HIGH_MSB,
483 LM63_REG_REMOTE_HIGH_LSB,
Guenter Roeck786375f2012-01-16 22:51:45 +0100484 LM63_REG_REMOTE_OFFSET_MSB,
485 LM63_REG_REMOTE_OFFSET_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200486 };
487
488 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
489 struct i2c_client *client = to_i2c_client(dev);
490 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck662bda22012-01-16 22:51:45 +0100491 long val;
492 int err;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200493 int nr = attr->index;
494
Guenter Roeck662bda22012-01-16 22:51:45 +0100495 err = kstrtol(buf, 10, &val);
496 if (err)
497 return err;
498
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100499 mutex_lock(&data->update_lock);
Guenter Roecke872c912012-01-16 22:51:46 +0100500 if (data->remote_unsigned && nr == 2)
501 data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
502 else
503 data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
504
Jean Delvarebc51ae12005-06-05 20:32:27 +0200505 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
506 data->temp11[nr] >> 8);
507 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
508 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100509 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200510 return count;
511}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Guenter Roeck662bda22012-01-16 22:51:45 +0100513/*
514 * Hysteresis register holds a relative value, while we want to present
515 * an absolute to user-space
516 */
517static ssize_t show_temp2_crit_hyst(struct device *dev,
518 struct device_attribute *dummy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100521 return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
Dirk Eibach2778fb12011-02-09 04:51:34 -0500522 + data->temp2_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 - TEMP8_FROM_REG(data->temp2_crit_hyst));
524}
525
Jean Delvared216f682012-01-16 22:51:47 +0100526static ssize_t show_lut_temp_hyst(struct device *dev,
527 struct device_attribute *devattr, char *buf)
528{
529 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
530 struct lm63_data *data = lm63_update_device(dev);
531
532 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
533 + data->temp2_offset
534 - TEMP8_FROM_REG(data->lut_temp_hyst));
535}
536
Guenter Roeck662bda22012-01-16 22:51:45 +0100537/*
538 * And now the other way around, user-space provides an absolute
539 * hysteresis value and we have to store a relative one
540 */
541static ssize_t set_temp2_crit_hyst(struct device *dev,
542 struct device_attribute *dummy,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200543 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct i2c_client *client = to_i2c_client(dev);
546 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck662bda22012-01-16 22:51:45 +0100547 long val;
548 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 long hyst;
550
Guenter Roeck662bda22012-01-16 22:51:45 +0100551 err = kstrtol(buf, 10, &val);
552 if (err)
553 return err;
554
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100555 mutex_lock(&data->update_lock);
Guenter Roecke872c912012-01-16 22:51:46 +0100556 hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
558 HYST_TO_REG(hyst));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100559 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return count;
561}
562
Guenter Roeck04738b22012-01-16 22:51:46 +0100563/*
564 * Set conversion rate.
565 * client->update_lock must be held when calling this function.
566 */
567static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data,
568 unsigned int interval)
569{
570 int i;
571 unsigned int update_interval;
572
573 /* Shift calculations to avoid rounding errors */
574 interval <<= 6;
575
576 /* find the nearest update rate */
577 update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
578 / data->max_convrate_hz;
579 for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
580 if (interval >= update_interval * 3 / 4)
581 break;
582
583 i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
584 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
585}
586
587static ssize_t show_update_interval(struct device *dev,
588 struct device_attribute *attr, char *buf)
589{
590 struct lm63_data *data = dev_get_drvdata(dev);
591
592 return sprintf(buf, "%u\n", data->update_interval);
593}
594
595static ssize_t set_update_interval(struct device *dev,
596 struct device_attribute *attr,
597 const char *buf, size_t count)
598{
599 struct i2c_client *client = to_i2c_client(dev);
600 struct lm63_data *data = i2c_get_clientdata(client);
601 unsigned long val;
602 int err;
603
604 err = kstrtoul(buf, 10, &val);
605 if (err)
606 return err;
607
608 mutex_lock(&data->update_lock);
609 lm63_set_convrate(client, data, SENSORS_LIMIT(val, 0, 100000));
610 mutex_unlock(&data->update_lock);
611
612 return count;
613}
614
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100615static ssize_t show_type(struct device *dev, struct device_attribute *attr,
616 char *buf)
617{
618 struct i2c_client *client = to_i2c_client(dev);
619 struct lm63_data *data = i2c_get_clientdata(client);
620
621 return sprintf(buf, data->trutherm ? "1\n" : "2\n");
622}
623
624static ssize_t set_type(struct device *dev, struct device_attribute *attr,
625 const char *buf, size_t count)
626{
627 struct i2c_client *client = to_i2c_client(dev);
628 struct lm63_data *data = i2c_get_clientdata(client);
629 unsigned long val;
630 int ret;
631 u8 reg;
632
633 ret = kstrtoul(buf, 10, &val);
634 if (ret < 0)
635 return ret;
636 if (val != 1 && val != 2)
637 return -EINVAL;
638
639 mutex_lock(&data->update_lock);
640 data->trutherm = val == 1;
641 reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02;
642 i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM,
643 reg | (data->trutherm ? 0x02 : 0x00));
644 data->valid = 0;
645 mutex_unlock(&data->update_lock);
646
647 return count;
648}
649
Jean Delvarebc51ae12005-06-05 20:32:27 +0200650static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
651 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 struct lm63_data *data = lm63_update_device(dev);
654 return sprintf(buf, "%u\n", data->alarms);
655}
656
Jean Delvare2d457712006-09-24 20:52:15 +0200657static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
658 char *buf)
659{
660 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
661 struct lm63_data *data = lm63_update_device(dev);
662 int bitnr = attr->index;
663
664 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
665}
666
Jean Delvarebc51ae12005-06-05 20:32:27 +0200667static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
668static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
669 set_fan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Jean Delvared216f682012-01-16 22:51:47 +0100671static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
Jean Delvared216f682012-01-16 22:51:47 +0100673static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO, show_pwm1, NULL, 1);
674static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IRUGO,
675 show_lut_temp, NULL, 3);
676static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp_hyst, S_IRUGO,
677 show_lut_temp_hyst, NULL, 3);
678static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IRUGO, show_pwm1, NULL, 2);
679static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO,
680 show_lut_temp, NULL, 4);
681static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp_hyst, S_IRUGO,
682 show_lut_temp_hyst, NULL, 4);
683static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO, show_pwm1, NULL, 3);
684static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp, S_IRUGO,
685 show_lut_temp, NULL, 5);
686static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp_hyst, S_IRUGO,
687 show_lut_temp_hyst, NULL, 5);
688static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IRUGO, show_pwm1, NULL, 4);
689static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp, S_IRUGO,
690 show_lut_temp, NULL, 6);
691static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp_hyst, S_IRUGO,
692 show_lut_temp_hyst, NULL, 6);
693static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IRUGO, show_pwm1, NULL, 5);
694static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp, S_IRUGO,
695 show_lut_temp, NULL, 7);
696static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp_hyst, S_IRUGO,
697 show_lut_temp_hyst, NULL, 7);
698static SENSOR_DEVICE_ATTR(pwm1_auto_point6_pwm, S_IRUGO, show_pwm1, NULL, 6);
699static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp, S_IRUGO,
700 show_lut_temp, NULL, 8);
701static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp_hyst, S_IRUGO,
702 show_lut_temp_hyst, NULL, 8);
703static SENSOR_DEVICE_ATTR(pwm1_auto_point7_pwm, S_IRUGO, show_pwm1, NULL, 7);
704static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp, S_IRUGO,
705 show_lut_temp, NULL, 9);
706static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp_hyst, S_IRUGO,
707 show_lut_temp_hyst, NULL, 9);
708static SENSOR_DEVICE_ATTR(pwm1_auto_point8_pwm, S_IRUGO, show_pwm1, NULL, 8);
709static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp, S_IRUGO,
710 show_lut_temp, NULL, 10);
711static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp_hyst, S_IRUGO,
712 show_lut_temp_hyst, NULL, 10);
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100713static SENSOR_DEVICE_ATTR(pwm1_auto_point9_pwm, S_IRUGO, show_pwm1, NULL, 9);
714static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp, S_IRUGO,
715 show_lut_temp, NULL, 11);
716static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp_hyst, S_IRUGO,
717 show_lut_temp_hyst, NULL, 11);
718static SENSOR_DEVICE_ATTR(pwm1_auto_point10_pwm, S_IRUGO, show_pwm1, NULL, 10);
719static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp, S_IRUGO,
720 show_lut_temp, NULL, 12);
721static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp_hyst, S_IRUGO,
722 show_lut_temp_hyst, NULL, 12);
723static SENSOR_DEVICE_ATTR(pwm1_auto_point11_pwm, S_IRUGO, show_pwm1, NULL, 11);
724static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp, S_IRUGO,
725 show_lut_temp, NULL, 13);
726static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp_hyst, S_IRUGO,
727 show_lut_temp_hyst, NULL, 13);
728static SENSOR_DEVICE_ATTR(pwm1_auto_point12_pwm, S_IRUGO, show_pwm1, NULL, 12);
729static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp, S_IRUGO,
730 show_lut_temp, NULL, 14);
731static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp_hyst, S_IRUGO,
732 show_lut_temp_hyst, NULL, 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Dirk Eibach2778fb12011-02-09 04:51:34 -0500734static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
735static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
Guenter Roeck94e55df2012-01-16 22:51:46 +0100736 set_temp8, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Jean Delvarebc51ae12005-06-05 20:32:27 +0200738static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
739static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
740 set_temp11, 1);
741static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
742 set_temp11, 2);
Guenter Roeck786375f2012-01-16 22:51:45 +0100743static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
744 set_temp11, 3);
Dirk Eibach2778fb12011-02-09 04:51:34 -0500745static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
Guenter Roeck94e55df2012-01-16 22:51:46 +0100746 set_temp8, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
748 set_temp2_crit_hyst);
749
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100750static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type);
751
Jean Delvare2d457712006-09-24 20:52:15 +0200752/* Individual alarm files */
753static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
754static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400755static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200756static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
757static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
758static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
759/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
761
Guenter Roeck04738b22012-01-16 22:51:46 +0100762static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
763 set_update_interval);
764
Jean Delvare0e39e012006-09-24 21:16:40 +0200765static struct attribute *lm63_attributes[] = {
Jean Delvared216f682012-01-16 22:51:47 +0100766 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200767 &dev_attr_pwm1_enable.attr,
Jean Delvared216f682012-01-16 22:51:47 +0100768 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
769 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
770 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
771 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
772 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
773 &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr,
774 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
775 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
776 &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr,
777 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
778 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
779 &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr,
780 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
781 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
782 &sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr,
783 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
784 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
785 &sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr,
786 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
787 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
788 &sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr,
789 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
790 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
791 &sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr,
792
Jean Delvare0e39e012006-09-24 21:16:40 +0200793 &sensor_dev_attr_temp1_input.dev_attr.attr,
794 &sensor_dev_attr_temp2_input.dev_attr.attr,
795 &sensor_dev_attr_temp2_min.dev_attr.attr,
796 &sensor_dev_attr_temp1_max.dev_attr.attr,
797 &sensor_dev_attr_temp2_max.dev_attr.attr,
Guenter Roeck786375f2012-01-16 22:51:45 +0100798 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200799 &sensor_dev_attr_temp2_crit.dev_attr.attr,
800 &dev_attr_temp2_crit_hyst.attr,
801
802 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400803 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200804 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
805 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
806 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
807 &dev_attr_alarms.attr,
Guenter Roeck04738b22012-01-16 22:51:46 +0100808 &dev_attr_update_interval.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200809 NULL
810};
811
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100812static struct attribute *lm63_attributes_extra_lut[] = {
813 &sensor_dev_attr_pwm1_auto_point9_pwm.dev_attr.attr,
814 &sensor_dev_attr_pwm1_auto_point9_temp.dev_attr.attr,
815 &sensor_dev_attr_pwm1_auto_point9_temp_hyst.dev_attr.attr,
816 &sensor_dev_attr_pwm1_auto_point10_pwm.dev_attr.attr,
817 &sensor_dev_attr_pwm1_auto_point10_temp.dev_attr.attr,
818 &sensor_dev_attr_pwm1_auto_point10_temp_hyst.dev_attr.attr,
819 &sensor_dev_attr_pwm1_auto_point11_pwm.dev_attr.attr,
820 &sensor_dev_attr_pwm1_auto_point11_temp.dev_attr.attr,
821 &sensor_dev_attr_pwm1_auto_point11_temp_hyst.dev_attr.attr,
822 &sensor_dev_attr_pwm1_auto_point12_pwm.dev_attr.attr,
823 &sensor_dev_attr_pwm1_auto_point12_temp.dev_attr.attr,
824 &sensor_dev_attr_pwm1_auto_point12_temp_hyst.dev_attr.attr,
825 NULL
826};
827
828static const struct attribute_group lm63_group_extra_lut = {
829 .attrs = lm63_attributes_extra_lut,
830};
831
Guenter Roeck94e55df2012-01-16 22:51:46 +0100832/*
833 * On LM63, temp2_crit can be set only once, which should be job
834 * of the bootloader.
835 * On LM64, temp2_crit can always be set.
836 * On LM96163, temp2_crit can be set if bit 1 of the configuration
837 * register is true.
838 */
839static umode_t lm63_attribute_mode(struct kobject *kobj,
840 struct attribute *attr, int index)
841{
842 struct device *dev = container_of(kobj, struct device, kobj);
843 struct i2c_client *client = to_i2c_client(dev);
844 struct lm63_data *data = i2c_get_clientdata(client);
845
846 if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
847 && (data->kind == lm64 ||
848 (data->kind == lm96163 && (data->config & 0x02))))
849 return attr->mode | S_IWUSR;
850
851 return attr->mode;
852}
853
Jean Delvare0e39e012006-09-24 21:16:40 +0200854static const struct attribute_group lm63_group = {
Guenter Roeck94e55df2012-01-16 22:51:46 +0100855 .is_visible = lm63_attribute_mode,
Jean Delvare0e39e012006-09-24 21:16:40 +0200856 .attrs = lm63_attributes,
857};
858
859static struct attribute *lm63_attributes_fan1[] = {
860 &sensor_dev_attr_fan1_input.dev_attr.attr,
861 &sensor_dev_attr_fan1_min.dev_attr.attr,
862
863 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
864 NULL
865};
866
867static const struct attribute_group lm63_group_fan1 = {
868 .attrs = lm63_attributes_fan1,
869};
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/*
872 * Real code
873 */
874
Jean Delvared5957be2008-07-16 19:30:13 +0200875/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvaredac27dc2012-03-23 10:02:18 +0100876static int lm63_detect(struct i2c_client *client,
Jean Delvared5957be2008-07-16 19:30:13 +0200877 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Jean Delvaredac27dc2012-03-23 10:02:18 +0100879 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100880 u8 man_id, chip_id, reg_config1, reg_config2;
881 u8 reg_alert_status, reg_alert_mask;
Jean Delvaredac27dc2012-03-23 10:02:18 +0100882 int address = client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvared5957be2008-07-16 19:30:13 +0200885 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Jean Delvaredac27dc2012-03-23 10:02:18 +0100887 man_id = i2c_smbus_read_byte_data(client, LM63_REG_MAN_ID);
888 chip_id = i2c_smbus_read_byte_data(client, LM63_REG_CHIP_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Jean Delvaredac27dc2012-03-23 10:02:18 +0100890 reg_config1 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
891 reg_config2 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG2);
892 reg_alert_status = i2c_smbus_read_byte_data(client,
Jean Delvare52df6442009-12-09 20:35:57 +0100893 LM63_REG_ALERT_STATUS);
Jean Delvaredac27dc2012-03-23 10:02:18 +0100894 reg_alert_mask = i2c_smbus_read_byte_data(client, LM63_REG_ALERT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Jean Delvare52df6442009-12-09 20:35:57 +0100896 if (man_id != 0x01 /* National Semiconductor */
Jean Delvare52df6442009-12-09 20:35:57 +0100897 || (reg_config1 & 0x18) != 0x00
898 || (reg_config2 & 0xF8) != 0x00
899 || (reg_alert_status & 0x20) != 0x00
900 || (reg_alert_mask & 0xA4) != 0xA4) {
901 dev_dbg(&adapter->dev,
902 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
903 man_id, chip_id);
904 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200907 if (chip_id == 0x41 && address == 0x4c)
908 strlcpy(info->type, "lm63", I2C_NAME_SIZE);
909 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
910 strlcpy(info->type, "lm64", I2C_NAME_SIZE);
Guenter Roeck210961c2012-01-16 22:51:45 +0100911 else if (chip_id == 0x49 && address == 0x4c)
912 strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200913 else
914 return -ENODEV;
Jean Delvared5957be2008-07-16 19:30:13 +0200915
916 return 0;
917}
918
Guenter Roeck662bda22012-01-16 22:51:45 +0100919/*
920 * Ideally we shouldn't have to initialize anything, since the BIOS
921 * should have taken care of everything
922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static void lm63_init_client(struct i2c_client *client)
924{
925 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck04738b22012-01-16 22:51:46 +0100926 u8 convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
929 data->config_fan = i2c_smbus_read_byte_data(client,
930 LM63_REG_CONFIG_FAN);
931
932 /* Start converting if needed */
933 if (data->config & 0x40) { /* standby */
Joe Perches898eb712007-10-18 03:06:30 -0700934 dev_dbg(&client->dev, "Switching to operational mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 data->config &= 0xA7;
936 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
937 data->config);
938 }
Jean Delvare409c0b52012-01-16 22:51:46 +0100939 /* Tachometer is always enabled on LM64 */
940 if (data->kind == lm64)
941 data->config |= 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 /* We may need pwm1_freq before ever updating the client data */
944 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
945 if (data->pwm1_freq == 0)
946 data->pwm1_freq = 1;
947
Guenter Roeck04738b22012-01-16 22:51:46 +0100948 switch (data->kind) {
949 case lm63:
950 case lm64:
951 data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100952 data->lut_size = 8;
Guenter Roeck04738b22012-01-16 22:51:46 +0100953 break;
954 case lm96163:
955 data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100956 data->lut_size = 12;
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100957 data->trutherm
958 = i2c_smbus_read_byte_data(client,
959 LM96163_REG_TRUTHERM) & 0x02;
Guenter Roeck04738b22012-01-16 22:51:46 +0100960 break;
961 }
962 convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
963 if (unlikely(convrate > LM63_MAX_CONVRATE))
964 convrate = LM63_MAX_CONVRATE;
965 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
966 convrate);
967
Guenter Roeck210961c2012-01-16 22:51:45 +0100968 /*
Guenter Roecke872c912012-01-16 22:51:46 +0100969 * For LM96163, check if high resolution PWM
970 * and unsigned temperature format is enabled.
Guenter Roeck210961c2012-01-16 22:51:45 +0100971 */
972 if (data->kind == lm96163) {
973 u8 config_enhanced
974 = i2c_smbus_read_byte_data(client,
975 LM96163_REG_CONFIG_ENHANCED);
Jean Delvared216f682012-01-16 22:51:47 +0100976 if (config_enhanced & 0x20)
977 data->lut_temp_highres = true;
Guenter Roeck210961c2012-01-16 22:51:45 +0100978 if ((config_enhanced & 0x10)
979 && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
980 data->pwm_highres = true;
981 if (config_enhanced & 0x08)
Guenter Roecke872c912012-01-16 22:51:46 +0100982 data->remote_unsigned = true;
Guenter Roeck210961c2012-01-16 22:51:45 +0100983 }
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* Show some debug info about the LM63 configuration */
Jean Delvare409c0b52012-01-16 22:51:46 +0100986 if (data->kind == lm63)
987 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
988 (data->config & 0x04) ? "tachometer input" :
989 "alert output");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
991 (data->config_fan & 0x08) ? "1.4" : "360",
992 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
993 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
994 (data->config_fan & 0x10) ? "low" : "high",
995 (data->config_fan & 0x20) ? "manual" : "auto");
996}
997
Jean Delvaredac27dc2012-03-23 10:02:18 +0100998static int lm63_probe(struct i2c_client *client,
999 const struct i2c_device_id *id)
1000{
1001 struct lm63_data *data;
1002 int err;
1003
1004 data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
1005 if (!data) {
1006 err = -ENOMEM;
1007 goto exit;
1008 }
1009
1010 i2c_set_clientdata(client, data);
1011 data->valid = 0;
1012 mutex_init(&data->update_lock);
1013
1014 /* Set the device type */
1015 data->kind = id->driver_data;
1016 if (data->kind == lm64)
1017 data->temp2_offset = 16000;
1018
1019 /* Initialize chip */
1020 lm63_init_client(client);
1021
1022 /* Register sysfs hooks */
1023 err = sysfs_create_group(&client->dev.kobj, &lm63_group);
1024 if (err)
1025 goto exit_free;
1026 if (data->config & 0x04) { /* tachometer enabled */
1027 err = sysfs_create_group(&client->dev.kobj, &lm63_group_fan1);
1028 if (err)
1029 goto exit_remove_files;
1030 }
1031 if (data->kind == lm96163) {
1032 err = device_create_file(&client->dev, &dev_attr_temp2_type);
1033 if (err)
1034 goto exit_remove_files;
1035
1036 err = sysfs_create_group(&client->dev.kobj,
1037 &lm63_group_extra_lut);
1038 if (err)
1039 goto exit_remove_files;
1040 }
1041
1042 data->hwmon_dev = hwmon_device_register(&client->dev);
1043 if (IS_ERR(data->hwmon_dev)) {
1044 err = PTR_ERR(data->hwmon_dev);
1045 goto exit_remove_files;
1046 }
1047
1048 return 0;
1049
1050exit_remove_files:
1051 sysfs_remove_group(&client->dev.kobj, &lm63_group);
1052 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
1053 if (data->kind == lm96163) {
1054 device_remove_file(&client->dev, &dev_attr_temp2_type);
1055 sysfs_remove_group(&client->dev.kobj, &lm63_group_extra_lut);
1056 }
1057exit_free:
1058 kfree(data);
1059exit:
1060 return err;
1061}
1062
Jean Delvared5957be2008-07-16 19:30:13 +02001063static int lm63_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001065 struct lm63_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Tony Jones1beeffe2007-08-20 13:46:20 -07001067 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001068 sysfs_remove_group(&client->dev.kobj, &lm63_group);
1069 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
Jean Delvare2fe28ab2012-01-16 22:51:47 +01001070 if (data->kind == lm96163) {
1071 device_remove_file(&client->dev, &dev_attr_temp2_type);
1072 sysfs_remove_group(&client->dev.kobj, &lm63_group_extra_lut);
1073 }
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001074
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001075 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return 0;
1077}
1078
Jean Delvaredac27dc2012-03-23 10:02:18 +01001079/*
1080 * Driver data (common to all clients)
1081 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Jean Delvaredac27dc2012-03-23 10:02:18 +01001083static const struct i2c_device_id lm63_id[] = {
1084 { "lm63", lm63 },
1085 { "lm64", lm64 },
1086 { "lm96163", lm96163 },
1087 { }
1088};
1089MODULE_DEVICE_TABLE(i2c, lm63_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Jean Delvaredac27dc2012-03-23 10:02:18 +01001091static struct i2c_driver lm63_driver = {
1092 .class = I2C_CLASS_HWMON,
1093 .driver = {
1094 .name = "lm63",
1095 },
1096 .probe = lm63_probe,
1097 .remove = lm63_remove,
1098 .id_table = lm63_id,
1099 .detect = lm63_detect,
1100 .address_list = normal_i2c,
1101};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Axel Linf0967ee2012-01-20 15:38:18 +08001103module_i2c_driver(lm63_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1106MODULE_DESCRIPTION("LM63 driver");
1107MODULE_LICENSE("GPL");