blob: 5f6da52a7219a9b45c3ef38d58074c22a62a7332 [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
54 * Address is fully defined internally and cannot be changed.
55 */
56
Matthew Garrett10f2ed32010-05-27 19:58:38 +020057static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * The LM63 registers
61 */
62
63#define LM63_REG_CONFIG1 0x03
Guenter Roeck04738b22012-01-16 22:51:46 +010064#define LM63_REG_CONVRATE 0x04
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define LM63_REG_CONFIG2 0xBF
66#define LM63_REG_CONFIG_FAN 0x4A
67
68#define LM63_REG_TACH_COUNT_MSB 0x47
69#define LM63_REG_TACH_COUNT_LSB 0x46
70#define LM63_REG_TACH_LIMIT_MSB 0x49
71#define LM63_REG_TACH_LIMIT_LSB 0x48
72
73#define LM63_REG_PWM_VALUE 0x4C
74#define LM63_REG_PWM_FREQ 0x4D
75
76#define LM63_REG_LOCAL_TEMP 0x00
77#define LM63_REG_LOCAL_HIGH 0x05
78
79#define LM63_REG_REMOTE_TEMP_MSB 0x01
80#define LM63_REG_REMOTE_TEMP_LSB 0x10
81#define LM63_REG_REMOTE_OFFSET_MSB 0x11
82#define LM63_REG_REMOTE_OFFSET_LSB 0x12
83#define LM63_REG_REMOTE_HIGH_MSB 0x07
84#define LM63_REG_REMOTE_HIGH_LSB 0x13
85#define LM63_REG_REMOTE_LOW_MSB 0x08
86#define LM63_REG_REMOTE_LOW_LSB 0x14
87#define LM63_REG_REMOTE_TCRIT 0x19
88#define LM63_REG_REMOTE_TCRIT_HYST 0x21
89
90#define LM63_REG_ALERT_STATUS 0x02
91#define LM63_REG_ALERT_MASK 0x16
92
93#define LM63_REG_MAN_ID 0xFE
94#define LM63_REG_CHIP_ID 0xFF
95
Guenter Roecke872c912012-01-16 22:51:46 +010096#define LM96163_REG_REMOTE_TEMP_U_MSB 0x31
97#define LM96163_REG_REMOTE_TEMP_U_LSB 0x32
Guenter Roeck210961c2012-01-16 22:51:45 +010098#define LM96163_REG_CONFIG_ENHANCED 0x45
99
Guenter Roeck04738b22012-01-16 22:51:46 +0100100#define LM63_MAX_CONVRATE 9
101
102#define LM63_MAX_CONVRATE_HZ 32
103#define LM96163_MAX_CONVRATE_HZ 26
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * Conversions and various macros
107 * For tachometer counts, the LM63 uses 16-bit values.
108 * For local temperature and high limit, remote critical limit and hysteresis
Steven Cole44bbe872005-05-03 18:21:25 -0600109 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * For remote temperature, low and high limits, it uses signed 11-bit values
Steven Cole44bbe872005-05-03 18:21:25 -0600111 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
Dirk Eibach2778fb12011-02-09 04:51:34 -0500112 * For LM64 the actual remote diode temperature is 16 degree Celsius higher
113 * than the register reading. Remote temperature setpoints have to be
114 * adapted accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116
117#define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
118 5400000 / (reg))
119#define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
120 (5400000 / (val)) & 0xFFFC)
121#define TEMP8_FROM_REG(reg) ((reg) * 1000)
122#define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
123 (val) >= 127000 ? 127 : \
124 (val) < 0 ? ((val) - 500) / 1000 : \
125 ((val) + 500) / 1000)
Guenter Roeck94e55df2012-01-16 22:51:46 +0100126#define TEMP8U_TO_REG(val) ((val) <= 0 ? 0 : \
127 (val) >= 255000 ? 255 : \
128 ((val) + 500) / 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
130#define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
131 (val) >= 127875 ? 0x7FE0 : \
132 (val) < 0 ? ((val) - 62) / 125 * 32 : \
133 ((val) + 62) / 125 * 32)
Guenter Roecke872c912012-01-16 22:51:46 +0100134#define TEMP11U_TO_REG(val) ((val) <= 0 ? 0 : \
135 (val) >= 255875 ? 0xFFE0 : \
136 ((val) + 62) / 125 * 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
138 (val) >= 127000 ? 127 : \
139 ((val) + 500) / 1000)
140
Guenter Roeck04738b22012-01-16 22:51:46 +0100141#define UPDATE_INTERVAL(max, rate) \
142 ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max))
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*
145 * Functions declaration
146 */
147
Jean Delvared5957be2008-07-16 19:30:13 +0200148static int lm63_probe(struct i2c_client *client,
149 const struct i2c_device_id *id);
150static int lm63_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152static struct lm63_data *lm63_update_device(struct device *dev);
153
Jean Delvare310ec792009-12-14 21:17:23 +0100154static int lm63_detect(struct i2c_client *client, struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static void lm63_init_client(struct i2c_client *client);
156
Guenter Roeck210961c2012-01-16 22:51:45 +0100157enum chips { lm63, lm64, lm96163 };
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
160 * Driver data (common to all clients)
161 */
162
Jean Delvared5957be2008-07-16 19:30:13 +0200163static const struct i2c_device_id lm63_id[] = {
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200164 { "lm63", lm63 },
165 { "lm64", lm64 },
Guenter Roeck210961c2012-01-16 22:51:45 +0100166 { "lm96163", lm96163 },
Jean Delvared5957be2008-07-16 19:30:13 +0200167 { }
168};
169MODULE_DEVICE_TABLE(i2c, lm63_id);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static struct i2c_driver lm63_driver = {
Jean Delvared5957be2008-07-16 19:30:13 +0200172 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100173 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100174 .name = "lm63",
175 },
Jean Delvared5957be2008-07-16 19:30:13 +0200176 .probe = lm63_probe,
177 .remove = lm63_remove,
178 .id_table = lm63_id,
179 .detect = lm63_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100180 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
183/*
184 * Client data (each client gets its own)
185 */
186
187struct lm63_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700188 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100189 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 char valid; /* zero until following fields are valid */
191 unsigned long last_updated; /* in jiffies */
Guenter Roeck04738b22012-01-16 22:51:46 +0100192 enum chips kind;
Dirk Eibach2778fb12011-02-09 04:51:34 -0500193 int temp2_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Guenter Roeck04738b22012-01-16 22:51:46 +0100195 int update_interval; /* in milliseconds */
196 int max_convrate_hz;
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* registers values */
199 u8 config, config_fan;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200200 u16 fan[2]; /* 0: input
201 1: low limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 u8 pwm1_freq;
203 u8 pwm1_value;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200204 s8 temp8[3]; /* 0: local input
205 1: local high limit
206 2: remote critical limit */
Guenter Roeck786375f2012-01-16 22:51:45 +0100207 s16 temp11[4]; /* 0: remote input
Jean Delvarebc51ae12005-06-05 20:32:27 +0200208 1: remote low limit
Guenter Roeck786375f2012-01-16 22:51:45 +0100209 2: remote high limit
210 3: remote offset */
Guenter Roecke872c912012-01-16 22:51:46 +0100211 u16 temp11u; /* remote input (unsigned) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 u8 temp2_crit_hyst;
213 u8 alarms;
Guenter Roeck210961c2012-01-16 22:51:45 +0100214 bool pwm_highres;
Guenter Roecke872c912012-01-16 22:51:46 +0100215 bool remote_unsigned; /* true if unsigned remote upper limits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
Guenter Roecke872c912012-01-16 22:51:46 +0100218static inline int temp8_from_reg(struct lm63_data *data, int nr)
219{
220 if (data->remote_unsigned)
221 return TEMP8_FROM_REG((u8)data->temp8[nr]);
222 return TEMP8_FROM_REG(data->temp8[nr]);
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
226 * Sysfs callback functions and files
227 */
228
Jean Delvarebc51ae12005-06-05 20:32:27 +0200229static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
230 char *buf)
231{
232 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
233 struct lm63_data *data = lm63_update_device(dev);
234 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jean Delvarebc51ae12005-06-05 20:32:27 +0200237static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
238 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct i2c_client *client = to_i2c_client(dev);
241 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck662bda22012-01-16 22:51:45 +0100242 unsigned long val;
243 int err;
244
245 err = kstrtoul(buf, 10, &val);
246 if (err)
247 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100249 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200250 data->fan[1] = FAN_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200252 data->fan[1] & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200254 data->fan[1] >> 8);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100255 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return count;
257}
258
Jean Delvarebc51ae12005-06-05 20:32:27 +0200259static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy,
260 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct lm63_data *data = lm63_update_device(dev);
Guenter Roeck210961c2012-01-16 22:51:45 +0100263 int pwm;
264
265 if (data->pwm_highres)
266 pwm = data->pwm1_value;
267 else
268 pwm = data->pwm1_value >= 2 * data->pwm1_freq ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 255 : (data->pwm1_value * 255 + data->pwm1_freq) /
Guenter Roeck210961c2012-01-16 22:51:45 +0100270 (2 * data->pwm1_freq);
271
272 return sprintf(buf, "%d\n", pwm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Jean Delvarebc51ae12005-06-05 20:32:27 +0200275static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
276 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 struct i2c_client *client = to_i2c_client(dev);
279 struct lm63_data *data = i2c_get_clientdata(client);
280 unsigned long val;
Guenter Roeck662bda22012-01-16 22:51:45 +0100281 int err;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (!(data->config_fan & 0x20)) /* register is read-only */
284 return -EPERM;
285
Guenter Roeck662bda22012-01-16 22:51:45 +0100286 err = kstrtoul(buf, 10, &val);
287 if (err)
288 return err;
289
Guenter Roeck210961c2012-01-16 22:51:45 +0100290 val = SENSORS_LIMIT(val, 0, 255);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100291 mutex_lock(&data->update_lock);
Guenter Roeck210961c2012-01-16 22:51:45 +0100292 data->pwm1_value = data->pwm_highres ? val :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 (val * data->pwm1_freq * 2 + 127) / 255;
294 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100295 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return count;
297}
298
Guenter Roeck662bda22012-01-16 22:51:45 +0100299static ssize_t show_pwm1_enable(struct device *dev,
300 struct device_attribute *dummy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct lm63_data *data = lm63_update_device(dev);
303 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
304}
305
Dirk Eibach2778fb12011-02-09 04:51:34 -0500306/*
307 * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
308 * For remote sensor registers temp2_offset has to be considered,
309 * for local sensor it must not.
310 * So we need separate 8bit accessors for local and remote sensor.
311 */
312static ssize_t show_local_temp8(struct device *dev,
313 struct device_attribute *devattr,
314 char *buf)
Jean Delvarebc51ae12005-06-05 20:32:27 +0200315{
316 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
317 struct lm63_data *data = lm63_update_device(dev);
318 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Dirk Eibach2778fb12011-02-09 04:51:34 -0500321static ssize_t show_remote_temp8(struct device *dev,
322 struct device_attribute *devattr,
323 char *buf)
324{
325 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
326 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100327 return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
Dirk Eibach2778fb12011-02-09 04:51:34 -0500328 + data->temp2_offset);
329}
330
Guenter Roeck94e55df2012-01-16 22:51:46 +0100331static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
332 const char *buf, size_t count)
Jean Delvarebc51ae12005-06-05 20:32:27 +0200333{
Guenter Roeck94e55df2012-01-16 22:51:46 +0100334 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200335 struct i2c_client *client = to_i2c_client(dev);
336 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck94e55df2012-01-16 22:51:46 +0100337 int nr = attr->index;
338 int reg = nr == 2 ? LM63_REG_REMOTE_TCRIT : LM63_REG_LOCAL_HIGH;
Guenter Roeck662bda22012-01-16 22:51:45 +0100339 long val;
340 int err;
Guenter Roeck94e55df2012-01-16 22:51:46 +0100341 int temp;
Guenter Roeck662bda22012-01-16 22:51:45 +0100342
343 err = kstrtol(buf, 10, &val);
344 if (err)
345 return err;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200346
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100347 mutex_lock(&data->update_lock);
Guenter Roeck94e55df2012-01-16 22:51:46 +0100348 if (nr == 2) {
349 if (data->remote_unsigned)
350 temp = TEMP8U_TO_REG(val - data->temp2_offset);
351 else
352 temp = TEMP8_TO_REG(val - data->temp2_offset);
353 } else {
354 temp = TEMP8_TO_REG(val);
355 }
356 data->temp8[nr] = temp;
357 i2c_smbus_write_byte_data(client, reg, temp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100358 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200359 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200361
362static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
363 char *buf)
364{
365 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
366 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100367 int nr = attr->index;
368 int temp;
369
370 if (!nr) {
371 /*
372 * Use unsigned temperature unless its value is zero.
373 * If it is zero, use signed temperature.
374 */
375 if (data->temp11u)
376 temp = TEMP11_FROM_REG(data->temp11u);
377 else
378 temp = TEMP11_FROM_REG(data->temp11[nr]);
379 } else {
380 if (data->remote_unsigned && nr == 2)
381 temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
382 else
383 temp = TEMP11_FROM_REG(data->temp11[nr]);
384 }
385 return sprintf(buf, "%d\n", temp + data->temp2_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200387
388static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
389 const char *buf, size_t count)
390{
Guenter Roeck786375f2012-01-16 22:51:45 +0100391 static const u8 reg[6] = {
Jean Delvarebc51ae12005-06-05 20:32:27 +0200392 LM63_REG_REMOTE_LOW_MSB,
393 LM63_REG_REMOTE_LOW_LSB,
394 LM63_REG_REMOTE_HIGH_MSB,
395 LM63_REG_REMOTE_HIGH_LSB,
Guenter Roeck786375f2012-01-16 22:51:45 +0100396 LM63_REG_REMOTE_OFFSET_MSB,
397 LM63_REG_REMOTE_OFFSET_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200398 };
399
400 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
401 struct i2c_client *client = to_i2c_client(dev);
402 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck662bda22012-01-16 22:51:45 +0100403 long val;
404 int err;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200405 int nr = attr->index;
406
Guenter Roeck662bda22012-01-16 22:51:45 +0100407 err = kstrtol(buf, 10, &val);
408 if (err)
409 return err;
410
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100411 mutex_lock(&data->update_lock);
Guenter Roecke872c912012-01-16 22:51:46 +0100412 if (data->remote_unsigned && nr == 2)
413 data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
414 else
415 data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
416
Jean Delvarebc51ae12005-06-05 20:32:27 +0200417 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
418 data->temp11[nr] >> 8);
419 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
420 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100421 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200422 return count;
423}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Guenter Roeck662bda22012-01-16 22:51:45 +0100425/*
426 * Hysteresis register holds a relative value, while we want to present
427 * an absolute to user-space
428 */
429static ssize_t show_temp2_crit_hyst(struct device *dev,
430 struct device_attribute *dummy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100433 return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
Dirk Eibach2778fb12011-02-09 04:51:34 -0500434 + data->temp2_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 - TEMP8_FROM_REG(data->temp2_crit_hyst));
436}
437
Guenter Roeck662bda22012-01-16 22:51:45 +0100438/*
439 * And now the other way around, user-space provides an absolute
440 * hysteresis value and we have to store a relative one
441 */
442static ssize_t set_temp2_crit_hyst(struct device *dev,
443 struct device_attribute *dummy,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200444 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct i2c_client *client = to_i2c_client(dev);
447 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck662bda22012-01-16 22:51:45 +0100448 long val;
449 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 long hyst;
451
Guenter Roeck662bda22012-01-16 22:51:45 +0100452 err = kstrtol(buf, 10, &val);
453 if (err)
454 return err;
455
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100456 mutex_lock(&data->update_lock);
Guenter Roecke872c912012-01-16 22:51:46 +0100457 hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
459 HYST_TO_REG(hyst));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100460 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return count;
462}
463
Guenter Roeck04738b22012-01-16 22:51:46 +0100464/*
465 * Set conversion rate.
466 * client->update_lock must be held when calling this function.
467 */
468static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data,
469 unsigned int interval)
470{
471 int i;
472 unsigned int update_interval;
473
474 /* Shift calculations to avoid rounding errors */
475 interval <<= 6;
476
477 /* find the nearest update rate */
478 update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
479 / data->max_convrate_hz;
480 for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
481 if (interval >= update_interval * 3 / 4)
482 break;
483
484 i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
485 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
486}
487
488static ssize_t show_update_interval(struct device *dev,
489 struct device_attribute *attr, char *buf)
490{
491 struct lm63_data *data = dev_get_drvdata(dev);
492
493 return sprintf(buf, "%u\n", data->update_interval);
494}
495
496static ssize_t set_update_interval(struct device *dev,
497 struct device_attribute *attr,
498 const char *buf, size_t count)
499{
500 struct i2c_client *client = to_i2c_client(dev);
501 struct lm63_data *data = i2c_get_clientdata(client);
502 unsigned long val;
503 int err;
504
505 err = kstrtoul(buf, 10, &val);
506 if (err)
507 return err;
508
509 mutex_lock(&data->update_lock);
510 lm63_set_convrate(client, data, SENSORS_LIMIT(val, 0, 100000));
511 mutex_unlock(&data->update_lock);
512
513 return count;
514}
515
Jean Delvarebc51ae12005-06-05 20:32:27 +0200516static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
517 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 struct lm63_data *data = lm63_update_device(dev);
520 return sprintf(buf, "%u\n", data->alarms);
521}
522
Jean Delvare2d457712006-09-24 20:52:15 +0200523static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
524 char *buf)
525{
526 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
527 struct lm63_data *data = lm63_update_device(dev);
528 int bitnr = attr->index;
529
530 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
531}
532
Jean Delvarebc51ae12005-06-05 20:32:27 +0200533static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
534static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
535 set_fan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
538static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
539
Dirk Eibach2778fb12011-02-09 04:51:34 -0500540static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
541static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
Guenter Roeck94e55df2012-01-16 22:51:46 +0100542 set_temp8, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Jean Delvarebc51ae12005-06-05 20:32:27 +0200544static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
545static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
546 set_temp11, 1);
547static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
548 set_temp11, 2);
Guenter Roeck786375f2012-01-16 22:51:45 +0100549static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
550 set_temp11, 3);
Dirk Eibach2778fb12011-02-09 04:51:34 -0500551static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
Guenter Roeck94e55df2012-01-16 22:51:46 +0100552 set_temp8, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
554 set_temp2_crit_hyst);
555
Jean Delvare2d457712006-09-24 20:52:15 +0200556/* Individual alarm files */
557static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
558static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400559static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200560static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
561static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
562static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
563/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
565
Guenter Roeck04738b22012-01-16 22:51:46 +0100566static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
567 set_update_interval);
568
Jean Delvare0e39e012006-09-24 21:16:40 +0200569static struct attribute *lm63_attributes[] = {
570 &dev_attr_pwm1.attr,
571 &dev_attr_pwm1_enable.attr,
572 &sensor_dev_attr_temp1_input.dev_attr.attr,
573 &sensor_dev_attr_temp2_input.dev_attr.attr,
574 &sensor_dev_attr_temp2_min.dev_attr.attr,
575 &sensor_dev_attr_temp1_max.dev_attr.attr,
576 &sensor_dev_attr_temp2_max.dev_attr.attr,
Guenter Roeck786375f2012-01-16 22:51:45 +0100577 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200578 &sensor_dev_attr_temp2_crit.dev_attr.attr,
579 &dev_attr_temp2_crit_hyst.attr,
580
581 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400582 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200583 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
584 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
585 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
586 &dev_attr_alarms.attr,
Guenter Roeck04738b22012-01-16 22:51:46 +0100587 &dev_attr_update_interval.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200588 NULL
589};
590
Guenter Roeck94e55df2012-01-16 22:51:46 +0100591/*
592 * On LM63, temp2_crit can be set only once, which should be job
593 * of the bootloader.
594 * On LM64, temp2_crit can always be set.
595 * On LM96163, temp2_crit can be set if bit 1 of the configuration
596 * register is true.
597 */
598static umode_t lm63_attribute_mode(struct kobject *kobj,
599 struct attribute *attr, int index)
600{
601 struct device *dev = container_of(kobj, struct device, kobj);
602 struct i2c_client *client = to_i2c_client(dev);
603 struct lm63_data *data = i2c_get_clientdata(client);
604
605 if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
606 && (data->kind == lm64 ||
607 (data->kind == lm96163 && (data->config & 0x02))))
608 return attr->mode | S_IWUSR;
609
610 return attr->mode;
611}
612
Jean Delvare0e39e012006-09-24 21:16:40 +0200613static const struct attribute_group lm63_group = {
Guenter Roeck94e55df2012-01-16 22:51:46 +0100614 .is_visible = lm63_attribute_mode,
Jean Delvare0e39e012006-09-24 21:16:40 +0200615 .attrs = lm63_attributes,
616};
617
618static struct attribute *lm63_attributes_fan1[] = {
619 &sensor_dev_attr_fan1_input.dev_attr.attr,
620 &sensor_dev_attr_fan1_min.dev_attr.attr,
621
622 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
623 NULL
624};
625
626static const struct attribute_group lm63_group_fan1 = {
627 .attrs = lm63_attributes_fan1,
628};
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/*
631 * Real code
632 */
633
Jean Delvared5957be2008-07-16 19:30:13 +0200634/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100635static int lm63_detect(struct i2c_client *new_client,
Jean Delvared5957be2008-07-16 19:30:13 +0200636 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Jean Delvared5957be2008-07-16 19:30:13 +0200638 struct i2c_adapter *adapter = new_client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100639 u8 man_id, chip_id, reg_config1, reg_config2;
640 u8 reg_alert_status, reg_alert_mask;
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200641 int address = new_client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvared5957be2008-07-16 19:30:13 +0200644 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Jean Delvare52df6442009-12-09 20:35:57 +0100646 man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID);
647 chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Jean Delvare52df6442009-12-09 20:35:57 +0100649 reg_config1 = i2c_smbus_read_byte_data(new_client,
650 LM63_REG_CONFIG1);
651 reg_config2 = i2c_smbus_read_byte_data(new_client,
652 LM63_REG_CONFIG2);
653 reg_alert_status = i2c_smbus_read_byte_data(new_client,
654 LM63_REG_ALERT_STATUS);
655 reg_alert_mask = i2c_smbus_read_byte_data(new_client,
656 LM63_REG_ALERT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Jean Delvare52df6442009-12-09 20:35:57 +0100658 if (man_id != 0x01 /* National Semiconductor */
Jean Delvare52df6442009-12-09 20:35:57 +0100659 || (reg_config1 & 0x18) != 0x00
660 || (reg_config2 & 0xF8) != 0x00
661 || (reg_alert_status & 0x20) != 0x00
662 || (reg_alert_mask & 0xA4) != 0xA4) {
663 dev_dbg(&adapter->dev,
664 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
665 man_id, chip_id);
666 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200669 if (chip_id == 0x41 && address == 0x4c)
670 strlcpy(info->type, "lm63", I2C_NAME_SIZE);
671 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
672 strlcpy(info->type, "lm64", I2C_NAME_SIZE);
Guenter Roeck210961c2012-01-16 22:51:45 +0100673 else if (chip_id == 0x49 && address == 0x4c)
674 strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200675 else
676 return -ENODEV;
Jean Delvared5957be2008-07-16 19:30:13 +0200677
678 return 0;
679}
680
681static int lm63_probe(struct i2c_client *new_client,
682 const struct i2c_device_id *id)
683{
684 struct lm63_data *data;
685 int err;
686
687 data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
688 if (!data) {
689 err = -ENOMEM;
690 goto exit;
691 }
692
693 i2c_set_clientdata(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100695 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Dirk Eibach2778fb12011-02-09 04:51:34 -0500697 /* Set the device type */
698 data->kind = id->driver_data;
699 if (data->kind == lm64)
700 data->temp2_offset = 16000;
701
702 /* Initialize chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 lm63_init_client(new_client);
704
705 /* Register sysfs hooks */
Guenter Roeck662bda22012-01-16 22:51:45 +0100706 err = sysfs_create_group(&new_client->dev.kobj, &lm63_group);
707 if (err)
Jean Delvared5957be2008-07-16 19:30:13 +0200708 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +0200709 if (data->config & 0x04) { /* tachometer enabled */
Guenter Roeck662bda22012-01-16 22:51:45 +0100710 err = sysfs_create_group(&new_client->dev.kobj,
711 &lm63_group_fan1);
712 if (err)
Jean Delvare0e39e012006-09-24 21:16:40 +0200713 goto exit_remove_files;
714 }
715
Tony Jones1beeffe2007-08-20 13:46:20 -0700716 data->hwmon_dev = hwmon_device_register(&new_client->dev);
717 if (IS_ERR(data->hwmon_dev)) {
718 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200719 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400720 }
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return 0;
723
Jean Delvare0e39e012006-09-24 21:16:40 +0200724exit_remove_files:
725 sysfs_remove_group(&new_client->dev.kobj, &lm63_group);
726 sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727exit_free:
728 kfree(data);
729exit:
730 return err;
731}
732
Guenter Roeck662bda22012-01-16 22:51:45 +0100733/*
734 * Ideally we shouldn't have to initialize anything, since the BIOS
735 * should have taken care of everything
736 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737static void lm63_init_client(struct i2c_client *client)
738{
739 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck04738b22012-01-16 22:51:46 +0100740 u8 convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
743 data->config_fan = i2c_smbus_read_byte_data(client,
744 LM63_REG_CONFIG_FAN);
745
746 /* Start converting if needed */
747 if (data->config & 0x40) { /* standby */
Joe Perches898eb712007-10-18 03:06:30 -0700748 dev_dbg(&client->dev, "Switching to operational mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 data->config &= 0xA7;
750 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
751 data->config);
752 }
753
754 /* We may need pwm1_freq before ever updating the client data */
755 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
756 if (data->pwm1_freq == 0)
757 data->pwm1_freq = 1;
758
Guenter Roeck04738b22012-01-16 22:51:46 +0100759 switch (data->kind) {
760 case lm63:
761 case lm64:
762 data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
763 break;
764 case lm96163:
765 data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
766 break;
767 }
768 convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
769 if (unlikely(convrate > LM63_MAX_CONVRATE))
770 convrate = LM63_MAX_CONVRATE;
771 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
772 convrate);
773
Guenter Roeck210961c2012-01-16 22:51:45 +0100774 /*
Guenter Roecke872c912012-01-16 22:51:46 +0100775 * For LM96163, check if high resolution PWM
776 * and unsigned temperature format is enabled.
Guenter Roeck210961c2012-01-16 22:51:45 +0100777 */
778 if (data->kind == lm96163) {
779 u8 config_enhanced
780 = i2c_smbus_read_byte_data(client,
781 LM96163_REG_CONFIG_ENHANCED);
782 if ((config_enhanced & 0x10)
783 && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
784 data->pwm_highres = true;
785 if (config_enhanced & 0x08)
Guenter Roecke872c912012-01-16 22:51:46 +0100786 data->remote_unsigned = true;
Guenter Roeck210961c2012-01-16 22:51:45 +0100787 }
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* Show some debug info about the LM63 configuration */
790 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
791 (data->config & 0x04) ? "tachometer input" :
792 "alert output");
793 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
794 (data->config_fan & 0x08) ? "1.4" : "360",
795 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
796 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
797 (data->config_fan & 0x10) ? "low" : "high",
798 (data->config_fan & 0x20) ? "manual" : "auto");
799}
800
Jean Delvared5957be2008-07-16 19:30:13 +0200801static int lm63_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400803 struct lm63_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Tony Jones1beeffe2007-08-20 13:46:20 -0700805 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200806 sysfs_remove_group(&client->dev.kobj, &lm63_group);
807 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400808
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400809 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return 0;
811}
812
813static struct lm63_data *lm63_update_device(struct device *dev)
814{
815 struct i2c_client *client = to_i2c_client(dev);
816 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck04738b22012-01-16 22:51:46 +0100817 unsigned long next_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100819 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Guenter Roeck04738b22012-01-16 22:51:46 +0100821 next_update = data->last_updated
822 + msecs_to_jiffies(data->update_interval) + 1;
823
824 if (time_after(jiffies, next_update) || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (data->config & 0x04) { /* tachometer enabled */
826 /* order matters for fan1_input */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200827 data->fan[0] = i2c_smbus_read_byte_data(client,
828 LM63_REG_TACH_COUNT_LSB) & 0xFC;
829 data->fan[0] |= i2c_smbus_read_byte_data(client,
830 LM63_REG_TACH_COUNT_MSB) << 8;
831 data->fan[1] = (i2c_smbus_read_byte_data(client,
832 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
833 | (i2c_smbus_read_byte_data(client,
834 LM63_REG_TACH_LIMIT_MSB) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
837 data->pwm1_freq = i2c_smbus_read_byte_data(client,
838 LM63_REG_PWM_FREQ);
839 if (data->pwm1_freq == 0)
840 data->pwm1_freq = 1;
841 data->pwm1_value = i2c_smbus_read_byte_data(client,
842 LM63_REG_PWM_VALUE);
843
Jean Delvarebc51ae12005-06-05 20:32:27 +0200844 data->temp8[0] = i2c_smbus_read_byte_data(client,
845 LM63_REG_LOCAL_TEMP);
846 data->temp8[1] = i2c_smbus_read_byte_data(client,
847 LM63_REG_LOCAL_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* order matters for temp2_input */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200850 data->temp11[0] = i2c_smbus_read_byte_data(client,
851 LM63_REG_REMOTE_TEMP_MSB) << 8;
852 data->temp11[0] |= i2c_smbus_read_byte_data(client,
853 LM63_REG_REMOTE_TEMP_LSB);
854 data->temp11[1] = (i2c_smbus_read_byte_data(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 LM63_REG_REMOTE_LOW_MSB) << 8)
856 | i2c_smbus_read_byte_data(client,
857 LM63_REG_REMOTE_LOW_LSB);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200858 data->temp11[2] = (i2c_smbus_read_byte_data(client,
859 LM63_REG_REMOTE_HIGH_MSB) << 8)
860 | i2c_smbus_read_byte_data(client,
861 LM63_REG_REMOTE_HIGH_LSB);
Guenter Roeck786375f2012-01-16 22:51:45 +0100862 data->temp11[3] = (i2c_smbus_read_byte_data(client,
863 LM63_REG_REMOTE_OFFSET_MSB) << 8)
864 | i2c_smbus_read_byte_data(client,
865 LM63_REG_REMOTE_OFFSET_LSB);
Guenter Roecke872c912012-01-16 22:51:46 +0100866
867 if (data->kind == lm96163)
868 data->temp11u = (i2c_smbus_read_byte_data(client,
869 LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
870 | i2c_smbus_read_byte_data(client,
871 LM96163_REG_REMOTE_TEMP_U_LSB);
872
Jean Delvarebc51ae12005-06-05 20:32:27 +0200873 data->temp8[2] = i2c_smbus_read_byte_data(client,
874 LM63_REG_REMOTE_TCRIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
876 LM63_REG_REMOTE_TCRIT_HYST);
877
878 data->alarms = i2c_smbus_read_byte_data(client,
879 LM63_REG_ALERT_STATUS) & 0x7F;
880
881 data->last_updated = jiffies;
882 data->valid = 1;
883 }
884
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100885 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 return data;
888}
889
890static int __init sensors_lm63_init(void)
891{
892 return i2c_add_driver(&lm63_driver);
893}
894
895static void __exit sensors_lm63_exit(void)
896{
897 i2c_del_driver(&lm63_driver);
898}
899
900MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
901MODULE_DESCRIPTION("LM63 driver");
902MODULE_LICENSE("GPL");
903
904module_init(sensors_lm63_init);
905module_exit(sensors_lm63_exit);