blob: 26844fc4a66d438b27ae67c34c27372c5037e1a8 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*
52 * Addresses to scan
53 * Address is fully defined internally and cannot be changed.
54 */
55
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050056static const unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/*
59 * Insmod parameters
60 */
61
Jean Delvaref4b50262005-07-31 21:49:03 +020062I2C_CLIENT_INSMOD_1(lm63);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * The LM63 registers
66 */
67
68#define LM63_REG_CONFIG1 0x03
69#define LM63_REG_CONFIG2 0xBF
70#define LM63_REG_CONFIG_FAN 0x4A
71
72#define LM63_REG_TACH_COUNT_MSB 0x47
73#define LM63_REG_TACH_COUNT_LSB 0x46
74#define LM63_REG_TACH_LIMIT_MSB 0x49
75#define LM63_REG_TACH_LIMIT_LSB 0x48
76
77#define LM63_REG_PWM_VALUE 0x4C
78#define LM63_REG_PWM_FREQ 0x4D
79
80#define LM63_REG_LOCAL_TEMP 0x00
81#define LM63_REG_LOCAL_HIGH 0x05
82
83#define LM63_REG_REMOTE_TEMP_MSB 0x01
84#define LM63_REG_REMOTE_TEMP_LSB 0x10
85#define LM63_REG_REMOTE_OFFSET_MSB 0x11
86#define LM63_REG_REMOTE_OFFSET_LSB 0x12
87#define LM63_REG_REMOTE_HIGH_MSB 0x07
88#define LM63_REG_REMOTE_HIGH_LSB 0x13
89#define LM63_REG_REMOTE_LOW_MSB 0x08
90#define LM63_REG_REMOTE_LOW_LSB 0x14
91#define LM63_REG_REMOTE_TCRIT 0x19
92#define LM63_REG_REMOTE_TCRIT_HYST 0x21
93
94#define LM63_REG_ALERT_STATUS 0x02
95#define LM63_REG_ALERT_MASK 0x16
96
97#define LM63_REG_MAN_ID 0xFE
98#define LM63_REG_CHIP_ID 0xFF
99
100/*
101 * Conversions and various macros
102 * For tachometer counts, the LM63 uses 16-bit values.
103 * For local temperature and high limit, remote critical limit and hysteresis
Steven Cole44bbe872005-05-03 18:21:25 -0600104 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * For remote temperature, low and high limits, it uses signed 11-bit values
Steven Cole44bbe872005-05-03 18:21:25 -0600106 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 */
108
109#define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
110 5400000 / (reg))
111#define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
112 (5400000 / (val)) & 0xFFFC)
113#define TEMP8_FROM_REG(reg) ((reg) * 1000)
114#define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
115 (val) >= 127000 ? 127 : \
116 (val) < 0 ? ((val) - 500) / 1000 : \
117 ((val) + 500) / 1000)
118#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
119#define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
120 (val) >= 127875 ? 0x7FE0 : \
121 (val) < 0 ? ((val) - 62) / 125 * 32 : \
122 ((val) + 62) / 125 * 32)
123#define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
124 (val) >= 127000 ? 127 : \
125 ((val) + 500) / 1000)
126
127/*
128 * Functions declaration
129 */
130
Jean Delvared5957be2008-07-16 19:30:13 +0200131static int lm63_probe(struct i2c_client *client,
132 const struct i2c_device_id *id);
133static int lm63_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135static struct lm63_data *lm63_update_device(struct device *dev);
136
Jean Delvare310ec792009-12-14 21:17:23 +0100137static int lm63_detect(struct i2c_client *client, struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static void lm63_init_client(struct i2c_client *client);
139
140/*
141 * Driver data (common to all clients)
142 */
143
Jean Delvared5957be2008-07-16 19:30:13 +0200144static const struct i2c_device_id lm63_id[] = {
145 { "lm63", lm63 },
146 { }
147};
148MODULE_DEVICE_TABLE(i2c, lm63_id);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static struct i2c_driver lm63_driver = {
Jean Delvared5957be2008-07-16 19:30:13 +0200151 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100152 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100153 .name = "lm63",
154 },
Jean Delvared5957be2008-07-16 19:30:13 +0200155 .probe = lm63_probe,
156 .remove = lm63_remove,
157 .id_table = lm63_id,
158 .detect = lm63_detect,
159 .address_data = &addr_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162/*
163 * Client data (each client gets its own)
164 */
165
166struct lm63_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700167 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100168 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 char valid; /* zero until following fields are valid */
170 unsigned long last_updated; /* in jiffies */
171
172 /* registers values */
173 u8 config, config_fan;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200174 u16 fan[2]; /* 0: input
175 1: low limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 u8 pwm1_freq;
177 u8 pwm1_value;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200178 s8 temp8[3]; /* 0: local input
179 1: local high limit
180 2: remote critical limit */
181 s16 temp11[3]; /* 0: remote input
182 1: remote low limit
183 2: remote high limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 u8 temp2_crit_hyst;
185 u8 alarms;
186};
187
188/*
189 * Sysfs callback functions and files
190 */
191
Jean Delvarebc51ae12005-06-05 20:32:27 +0200192static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
193 char *buf)
194{
195 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
196 struct lm63_data *data = lm63_update_device(dev);
197 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Jean Delvarebc51ae12005-06-05 20:32:27 +0200200static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
201 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 struct i2c_client *client = to_i2c_client(dev);
204 struct lm63_data *data = i2c_get_clientdata(client);
205 unsigned long val = simple_strtoul(buf, NULL, 10);
206
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100207 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200208 data->fan[1] = FAN_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200210 data->fan[1] & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200212 data->fan[1] >> 8);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100213 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return count;
215}
216
Jean Delvarebc51ae12005-06-05 20:32:27 +0200217static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy,
218 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 struct lm63_data *data = lm63_update_device(dev);
221 return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ?
222 255 : (data->pwm1_value * 255 + data->pwm1_freq) /
223 (2 * data->pwm1_freq));
224}
225
Jean Delvarebc51ae12005-06-05 20:32:27 +0200226static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
227 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct i2c_client *client = to_i2c_client(dev);
230 struct lm63_data *data = i2c_get_clientdata(client);
231 unsigned long val;
232
233 if (!(data->config_fan & 0x20)) /* register is read-only */
234 return -EPERM;
235
236 val = simple_strtoul(buf, NULL, 10);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100237 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 data->pwm1_value = val <= 0 ? 0 :
239 val >= 255 ? 2 * data->pwm1_freq :
240 (val * data->pwm1_freq * 2 + 127) / 255;
241 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100242 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return count;
244}
245
Jean Delvarebc51ae12005-06-05 20:32:27 +0200246static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dummy,
247 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct lm63_data *data = lm63_update_device(dev);
250 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
251}
252
Jean Delvarebc51ae12005-06-05 20:32:27 +0200253static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
254 char *buf)
255{
256 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
257 struct lm63_data *data = lm63_update_device(dev);
258 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Jean Delvarebc51ae12005-06-05 20:32:27 +0200261static ssize_t set_temp8(struct device *dev, struct device_attribute *dummy,
262 const char *buf, size_t count)
263{
264 struct i2c_client *client = to_i2c_client(dev);
265 struct lm63_data *data = i2c_get_clientdata(client);
266 long val = simple_strtol(buf, NULL, 10);
267
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100268 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200269 data->temp8[1] = TEMP8_TO_REG(val);
270 i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, data->temp8[1]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100271 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200272 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200274
275static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
276 char *buf)
277{
278 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
279 struct lm63_data *data = lm63_update_device(dev);
280 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200282
283static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
284 const char *buf, size_t count)
285{
286 static const u8 reg[4] = {
287 LM63_REG_REMOTE_LOW_MSB,
288 LM63_REG_REMOTE_LOW_LSB,
289 LM63_REG_REMOTE_HIGH_MSB,
290 LM63_REG_REMOTE_HIGH_LSB,
291 };
292
293 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
294 struct i2c_client *client = to_i2c_client(dev);
295 struct lm63_data *data = i2c_get_clientdata(client);
296 long val = simple_strtol(buf, NULL, 10);
297 int nr = attr->index;
298
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100299 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200300 data->temp11[nr] = TEMP11_TO_REG(val);
301 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
302 data->temp11[nr] >> 8);
303 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
304 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100305 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200306 return count;
307}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309/* Hysteresis register holds a relative value, while we want to present
310 an absolute to user-space */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200311static ssize_t show_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
312 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct lm63_data *data = lm63_update_device(dev);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200315 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 - TEMP8_FROM_REG(data->temp2_crit_hyst));
317}
318
319/* And now the other way around, user-space provides an absolute
320 hysteresis value and we have to store a relative one */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200321static ssize_t set_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
322 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct i2c_client *client = to_i2c_client(dev);
325 struct lm63_data *data = i2c_get_clientdata(client);
326 long val = simple_strtol(buf, NULL, 10);
327 long hyst;
328
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100329 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200330 hyst = TEMP8_FROM_REG(data->temp8[2]) - val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
332 HYST_TO_REG(hyst));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100333 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return count;
335}
336
Jean Delvarebc51ae12005-06-05 20:32:27 +0200337static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
338 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 struct lm63_data *data = lm63_update_device(dev);
341 return sprintf(buf, "%u\n", data->alarms);
342}
343
Jean Delvare2d457712006-09-24 20:52:15 +0200344static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
345 char *buf)
346{
347 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
348 struct lm63_data *data = lm63_update_device(dev);
349 int bitnr = attr->index;
350
351 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
352}
353
Jean Delvarebc51ae12005-06-05 20:32:27 +0200354static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
355static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
356 set_fan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
359static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
360
Jean Delvarebc51ae12005-06-05 20:32:27 +0200361static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0);
362static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
363 set_temp8, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Jean Delvarebc51ae12005-06-05 20:32:27 +0200365static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
366static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
367 set_temp11, 1);
368static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
369 set_temp11, 2);
370static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp8, NULL, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
372 set_temp2_crit_hyst);
373
Jean Delvare2d457712006-09-24 20:52:15 +0200374/* Individual alarm files */
375static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
376static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400377static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200378static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
379static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
380static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
381/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
383
Jean Delvare0e39e012006-09-24 21:16:40 +0200384static struct attribute *lm63_attributes[] = {
385 &dev_attr_pwm1.attr,
386 &dev_attr_pwm1_enable.attr,
387 &sensor_dev_attr_temp1_input.dev_attr.attr,
388 &sensor_dev_attr_temp2_input.dev_attr.attr,
389 &sensor_dev_attr_temp2_min.dev_attr.attr,
390 &sensor_dev_attr_temp1_max.dev_attr.attr,
391 &sensor_dev_attr_temp2_max.dev_attr.attr,
392 &sensor_dev_attr_temp2_crit.dev_attr.attr,
393 &dev_attr_temp2_crit_hyst.attr,
394
395 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400396 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200397 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
398 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
399 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
400 &dev_attr_alarms.attr,
401 NULL
402};
403
404static const struct attribute_group lm63_group = {
405 .attrs = lm63_attributes,
406};
407
408static struct attribute *lm63_attributes_fan1[] = {
409 &sensor_dev_attr_fan1_input.dev_attr.attr,
410 &sensor_dev_attr_fan1_min.dev_attr.attr,
411
412 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
413 NULL
414};
415
416static const struct attribute_group lm63_group_fan1 = {
417 .attrs = lm63_attributes_fan1,
418};
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/*
421 * Real code
422 */
423
Jean Delvared5957be2008-07-16 19:30:13 +0200424/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100425static int lm63_detect(struct i2c_client *new_client,
Jean Delvared5957be2008-07-16 19:30:13 +0200426 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Jean Delvared5957be2008-07-16 19:30:13 +0200428 struct i2c_adapter *adapter = new_client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100429 u8 man_id, chip_id, reg_config1, reg_config2;
430 u8 reg_alert_status, reg_alert_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvared5957be2008-07-16 19:30:13 +0200433 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Jean Delvare52df6442009-12-09 20:35:57 +0100435 man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID);
436 chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Jean Delvare52df6442009-12-09 20:35:57 +0100438 reg_config1 = i2c_smbus_read_byte_data(new_client,
439 LM63_REG_CONFIG1);
440 reg_config2 = i2c_smbus_read_byte_data(new_client,
441 LM63_REG_CONFIG2);
442 reg_alert_status = i2c_smbus_read_byte_data(new_client,
443 LM63_REG_ALERT_STATUS);
444 reg_alert_mask = i2c_smbus_read_byte_data(new_client,
445 LM63_REG_ALERT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Jean Delvare52df6442009-12-09 20:35:57 +0100447 if (man_id != 0x01 /* National Semiconductor */
448 || chip_id != 0x41 /* LM63 */
449 || (reg_config1 & 0x18) != 0x00
450 || (reg_config2 & 0xF8) != 0x00
451 || (reg_alert_status & 0x20) != 0x00
452 || (reg_alert_mask & 0xA4) != 0xA4) {
453 dev_dbg(&adapter->dev,
454 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
455 man_id, chip_id);
456 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
Jean Delvared5957be2008-07-16 19:30:13 +0200459 strlcpy(info->type, "lm63", I2C_NAME_SIZE);
460
461 return 0;
462}
463
464static int lm63_probe(struct i2c_client *new_client,
465 const struct i2c_device_id *id)
466{
467 struct lm63_data *data;
468 int err;
469
470 data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
471 if (!data) {
472 err = -ENOMEM;
473 goto exit;
474 }
475
476 i2c_set_clientdata(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100478 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /* Initialize the LM63 chip */
481 lm63_init_client(new_client);
482
483 /* Register sysfs hooks */
Jean Delvare0e39e012006-09-24 21:16:40 +0200484 if ((err = sysfs_create_group(&new_client->dev.kobj,
485 &lm63_group)))
Jean Delvared5957be2008-07-16 19:30:13 +0200486 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +0200487 if (data->config & 0x04) { /* tachometer enabled */
488 if ((err = sysfs_create_group(&new_client->dev.kobj,
489 &lm63_group_fan1)))
490 goto exit_remove_files;
491 }
492
Tony Jones1beeffe2007-08-20 13:46:20 -0700493 data->hwmon_dev = hwmon_device_register(&new_client->dev);
494 if (IS_ERR(data->hwmon_dev)) {
495 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200496 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400497 }
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return 0;
500
Jean Delvare0e39e012006-09-24 21:16:40 +0200501exit_remove_files:
502 sysfs_remove_group(&new_client->dev.kobj, &lm63_group);
503 sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504exit_free:
505 kfree(data);
506exit:
507 return err;
508}
509
510/* Idealy we shouldn't have to initialize anything, since the BIOS
511 should have taken care of everything */
512static void lm63_init_client(struct i2c_client *client)
513{
514 struct lm63_data *data = i2c_get_clientdata(client);
515
516 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
517 data->config_fan = i2c_smbus_read_byte_data(client,
518 LM63_REG_CONFIG_FAN);
519
520 /* Start converting if needed */
521 if (data->config & 0x40) { /* standby */
Joe Perches898eb712007-10-18 03:06:30 -0700522 dev_dbg(&client->dev, "Switching to operational mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 data->config &= 0xA7;
524 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
525 data->config);
526 }
527
528 /* We may need pwm1_freq before ever updating the client data */
529 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
530 if (data->pwm1_freq == 0)
531 data->pwm1_freq = 1;
532
533 /* Show some debug info about the LM63 configuration */
534 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
535 (data->config & 0x04) ? "tachometer input" :
536 "alert output");
537 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
538 (data->config_fan & 0x08) ? "1.4" : "360",
539 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
540 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
541 (data->config_fan & 0x10) ? "low" : "high",
542 (data->config_fan & 0x20) ? "manual" : "auto");
543}
544
Jean Delvared5957be2008-07-16 19:30:13 +0200545static int lm63_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400547 struct lm63_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Tony Jones1beeffe2007-08-20 13:46:20 -0700549 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200550 sysfs_remove_group(&client->dev.kobj, &lm63_group);
551 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400552
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400553 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return 0;
555}
556
557static struct lm63_data *lm63_update_device(struct device *dev)
558{
559 struct i2c_client *client = to_i2c_client(dev);
560 struct lm63_data *data = i2c_get_clientdata(client);
561
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100562 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
565 if (data->config & 0x04) { /* tachometer enabled */
566 /* order matters for fan1_input */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200567 data->fan[0] = i2c_smbus_read_byte_data(client,
568 LM63_REG_TACH_COUNT_LSB) & 0xFC;
569 data->fan[0] |= i2c_smbus_read_byte_data(client,
570 LM63_REG_TACH_COUNT_MSB) << 8;
571 data->fan[1] = (i2c_smbus_read_byte_data(client,
572 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
573 | (i2c_smbus_read_byte_data(client,
574 LM63_REG_TACH_LIMIT_MSB) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
577 data->pwm1_freq = i2c_smbus_read_byte_data(client,
578 LM63_REG_PWM_FREQ);
579 if (data->pwm1_freq == 0)
580 data->pwm1_freq = 1;
581 data->pwm1_value = i2c_smbus_read_byte_data(client,
582 LM63_REG_PWM_VALUE);
583
Jean Delvarebc51ae12005-06-05 20:32:27 +0200584 data->temp8[0] = i2c_smbus_read_byte_data(client,
585 LM63_REG_LOCAL_TEMP);
586 data->temp8[1] = i2c_smbus_read_byte_data(client,
587 LM63_REG_LOCAL_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /* order matters for temp2_input */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200590 data->temp11[0] = i2c_smbus_read_byte_data(client,
591 LM63_REG_REMOTE_TEMP_MSB) << 8;
592 data->temp11[0] |= i2c_smbus_read_byte_data(client,
593 LM63_REG_REMOTE_TEMP_LSB);
594 data->temp11[1] = (i2c_smbus_read_byte_data(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 LM63_REG_REMOTE_LOW_MSB) << 8)
596 | i2c_smbus_read_byte_data(client,
597 LM63_REG_REMOTE_LOW_LSB);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200598 data->temp11[2] = (i2c_smbus_read_byte_data(client,
599 LM63_REG_REMOTE_HIGH_MSB) << 8)
600 | i2c_smbus_read_byte_data(client,
601 LM63_REG_REMOTE_HIGH_LSB);
602 data->temp8[2] = i2c_smbus_read_byte_data(client,
603 LM63_REG_REMOTE_TCRIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
605 LM63_REG_REMOTE_TCRIT_HYST);
606
607 data->alarms = i2c_smbus_read_byte_data(client,
608 LM63_REG_ALERT_STATUS) & 0x7F;
609
610 data->last_updated = jiffies;
611 data->valid = 1;
612 }
613
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100614 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 return data;
617}
618
619static int __init sensors_lm63_init(void)
620{
621 return i2c_add_driver(&lm63_driver);
622}
623
624static void __exit sensors_lm63_exit(void)
625{
626 i2c_del_driver(&lm63_driver);
627}
628
629MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
630MODULE_DESCRIPTION("LM63 driver");
631MODULE_LICENSE("GPL");
632
633module_init(sensors_lm63_init);
634module_exit(sensors_lm63_exit);