blob: 776aeb3019d2b443a585fb38b96750ca53174099 [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
Matthew Garrett10f2ed32010-05-27 19:58:38 +020056static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * The LM63 registers
60 */
61
62#define LM63_REG_CONFIG1 0x03
63#define LM63_REG_CONFIG2 0xBF
64#define LM63_REG_CONFIG_FAN 0x4A
65
66#define LM63_REG_TACH_COUNT_MSB 0x47
67#define LM63_REG_TACH_COUNT_LSB 0x46
68#define LM63_REG_TACH_LIMIT_MSB 0x49
69#define LM63_REG_TACH_LIMIT_LSB 0x48
70
71#define LM63_REG_PWM_VALUE 0x4C
72#define LM63_REG_PWM_FREQ 0x4D
73
74#define LM63_REG_LOCAL_TEMP 0x00
75#define LM63_REG_LOCAL_HIGH 0x05
76
77#define LM63_REG_REMOTE_TEMP_MSB 0x01
78#define LM63_REG_REMOTE_TEMP_LSB 0x10
79#define LM63_REG_REMOTE_OFFSET_MSB 0x11
80#define LM63_REG_REMOTE_OFFSET_LSB 0x12
81#define LM63_REG_REMOTE_HIGH_MSB 0x07
82#define LM63_REG_REMOTE_HIGH_LSB 0x13
83#define LM63_REG_REMOTE_LOW_MSB 0x08
84#define LM63_REG_REMOTE_LOW_LSB 0x14
85#define LM63_REG_REMOTE_TCRIT 0x19
86#define LM63_REG_REMOTE_TCRIT_HYST 0x21
87
88#define LM63_REG_ALERT_STATUS 0x02
89#define LM63_REG_ALERT_MASK 0x16
90
91#define LM63_REG_MAN_ID 0xFE
92#define LM63_REG_CHIP_ID 0xFF
93
94/*
95 * Conversions and various macros
96 * For tachometer counts, the LM63 uses 16-bit values.
97 * For local temperature and high limit, remote critical limit and hysteresis
Steven Cole44bbe872005-05-03 18:21:25 -060098 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * For remote temperature, low and high limits, it uses signed 11-bit values
Steven Cole44bbe872005-05-03 18:21:25 -0600100 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
102
103#define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
104 5400000 / (reg))
105#define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
106 (5400000 / (val)) & 0xFFFC)
107#define TEMP8_FROM_REG(reg) ((reg) * 1000)
108#define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
109 (val) >= 127000 ? 127 : \
110 (val) < 0 ? ((val) - 500) / 1000 : \
111 ((val) + 500) / 1000)
112#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
113#define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
114 (val) >= 127875 ? 0x7FE0 : \
115 (val) < 0 ? ((val) - 62) / 125 * 32 : \
116 ((val) + 62) / 125 * 32)
117#define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
118 (val) >= 127000 ? 127 : \
119 ((val) + 500) / 1000)
120
121/*
122 * Functions declaration
123 */
124
Jean Delvared5957be2008-07-16 19:30:13 +0200125static int lm63_probe(struct i2c_client *client,
126 const struct i2c_device_id *id);
127static int lm63_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129static struct lm63_data *lm63_update_device(struct device *dev);
130
Jean Delvare310ec792009-12-14 21:17:23 +0100131static int lm63_detect(struct i2c_client *client, struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static void lm63_init_client(struct i2c_client *client);
133
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200134enum chips { lm63, lm64 };
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
137 * Driver data (common to all clients)
138 */
139
Jean Delvared5957be2008-07-16 19:30:13 +0200140static const struct i2c_device_id lm63_id[] = {
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200141 { "lm63", lm63 },
142 { "lm64", lm64 },
Jean Delvared5957be2008-07-16 19:30:13 +0200143 { }
144};
145MODULE_DEVICE_TABLE(i2c, lm63_id);
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static struct i2c_driver lm63_driver = {
Jean Delvared5957be2008-07-16 19:30:13 +0200148 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100149 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100150 .name = "lm63",
151 },
Jean Delvared5957be2008-07-16 19:30:13 +0200152 .probe = lm63_probe,
153 .remove = lm63_remove,
154 .id_table = lm63_id,
155 .detect = lm63_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100156 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157};
158
159/*
160 * Client data (each client gets its own)
161 */
162
163struct lm63_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700164 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100165 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 char valid; /* zero until following fields are valid */
167 unsigned long last_updated; /* in jiffies */
168
169 /* registers values */
170 u8 config, config_fan;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200171 u16 fan[2]; /* 0: input
172 1: low limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 u8 pwm1_freq;
174 u8 pwm1_value;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200175 s8 temp8[3]; /* 0: local input
176 1: local high limit
177 2: remote critical limit */
178 s16 temp11[3]; /* 0: remote input
179 1: remote low limit
180 2: remote high limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 u8 temp2_crit_hyst;
182 u8 alarms;
183};
184
185/*
186 * Sysfs callback functions and files
187 */
188
Jean Delvarebc51ae12005-06-05 20:32:27 +0200189static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
190 char *buf)
191{
192 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
193 struct lm63_data *data = lm63_update_device(dev);
194 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Jean Delvarebc51ae12005-06-05 20:32:27 +0200197static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
198 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct i2c_client *client = to_i2c_client(dev);
201 struct lm63_data *data = i2c_get_clientdata(client);
202 unsigned long val = simple_strtoul(buf, NULL, 10);
203
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100204 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200205 data->fan[1] = FAN_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200207 data->fan[1] & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200209 data->fan[1] >> 8);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100210 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return count;
212}
213
Jean Delvarebc51ae12005-06-05 20:32:27 +0200214static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy,
215 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct lm63_data *data = lm63_update_device(dev);
218 return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ?
219 255 : (data->pwm1_value * 255 + data->pwm1_freq) /
220 (2 * data->pwm1_freq));
221}
222
Jean Delvarebc51ae12005-06-05 20:32:27 +0200223static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
224 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct i2c_client *client = to_i2c_client(dev);
227 struct lm63_data *data = i2c_get_clientdata(client);
228 unsigned long val;
229
230 if (!(data->config_fan & 0x20)) /* register is read-only */
231 return -EPERM;
232
233 val = simple_strtoul(buf, NULL, 10);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100234 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 data->pwm1_value = val <= 0 ? 0 :
236 val >= 255 ? 2 * data->pwm1_freq :
237 (val * data->pwm1_freq * 2 + 127) / 255;
238 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100239 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return count;
241}
242
Jean Delvarebc51ae12005-06-05 20:32:27 +0200243static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dummy,
244 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 struct lm63_data *data = lm63_update_device(dev);
247 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
248}
249
Jean Delvarebc51ae12005-06-05 20:32:27 +0200250static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
251 char *buf)
252{
253 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
254 struct lm63_data *data = lm63_update_device(dev);
255 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Jean Delvarebc51ae12005-06-05 20:32:27 +0200258static ssize_t set_temp8(struct device *dev, struct device_attribute *dummy,
259 const char *buf, size_t count)
260{
261 struct i2c_client *client = to_i2c_client(dev);
262 struct lm63_data *data = i2c_get_clientdata(client);
263 long val = simple_strtol(buf, NULL, 10);
264
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100265 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200266 data->temp8[1] = TEMP8_TO_REG(val);
267 i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, data->temp8[1]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100268 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200269 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200271
272static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
273 char *buf)
274{
275 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
276 struct lm63_data *data = lm63_update_device(dev);
277 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200279
280static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
281 const char *buf, size_t count)
282{
283 static const u8 reg[4] = {
284 LM63_REG_REMOTE_LOW_MSB,
285 LM63_REG_REMOTE_LOW_LSB,
286 LM63_REG_REMOTE_HIGH_MSB,
287 LM63_REG_REMOTE_HIGH_LSB,
288 };
289
290 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
291 struct i2c_client *client = to_i2c_client(dev);
292 struct lm63_data *data = i2c_get_clientdata(client);
293 long val = simple_strtol(buf, NULL, 10);
294 int nr = attr->index;
295
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100296 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200297 data->temp11[nr] = TEMP11_TO_REG(val);
298 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
299 data->temp11[nr] >> 8);
300 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
301 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100302 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200303 return count;
304}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306/* Hysteresis register holds a relative value, while we want to present
307 an absolute to user-space */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200308static ssize_t show_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
309 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct lm63_data *data = lm63_update_device(dev);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200312 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 - TEMP8_FROM_REG(data->temp2_crit_hyst));
314}
315
316/* And now the other way around, user-space provides an absolute
317 hysteresis value and we have to store a relative one */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200318static ssize_t set_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
319 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 struct i2c_client *client = to_i2c_client(dev);
322 struct lm63_data *data = i2c_get_clientdata(client);
323 long val = simple_strtol(buf, NULL, 10);
324 long hyst;
325
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100326 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200327 hyst = TEMP8_FROM_REG(data->temp8[2]) - val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
329 HYST_TO_REG(hyst));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100330 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return count;
332}
333
Jean Delvarebc51ae12005-06-05 20:32:27 +0200334static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
335 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct lm63_data *data = lm63_update_device(dev);
338 return sprintf(buf, "%u\n", data->alarms);
339}
340
Jean Delvare2d457712006-09-24 20:52:15 +0200341static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
342 char *buf)
343{
344 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
345 struct lm63_data *data = lm63_update_device(dev);
346 int bitnr = attr->index;
347
348 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
349}
350
Jean Delvarebc51ae12005-06-05 20:32:27 +0200351static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
352static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
353 set_fan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
356static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
357
Jean Delvarebc51ae12005-06-05 20:32:27 +0200358static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0);
359static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
360 set_temp8, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Jean Delvarebc51ae12005-06-05 20:32:27 +0200362static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
363static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
364 set_temp11, 1);
365static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
366 set_temp11, 2);
367static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp8, NULL, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
369 set_temp2_crit_hyst);
370
Jean Delvare2d457712006-09-24 20:52:15 +0200371/* Individual alarm files */
372static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
373static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400374static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200375static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
376static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
377static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
378/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
380
Jean Delvare0e39e012006-09-24 21:16:40 +0200381static struct attribute *lm63_attributes[] = {
382 &dev_attr_pwm1.attr,
383 &dev_attr_pwm1_enable.attr,
384 &sensor_dev_attr_temp1_input.dev_attr.attr,
385 &sensor_dev_attr_temp2_input.dev_attr.attr,
386 &sensor_dev_attr_temp2_min.dev_attr.attr,
387 &sensor_dev_attr_temp1_max.dev_attr.attr,
388 &sensor_dev_attr_temp2_max.dev_attr.attr,
389 &sensor_dev_attr_temp2_crit.dev_attr.attr,
390 &dev_attr_temp2_crit_hyst.attr,
391
392 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400393 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200394 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
395 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
396 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
397 &dev_attr_alarms.attr,
398 NULL
399};
400
401static const struct attribute_group lm63_group = {
402 .attrs = lm63_attributes,
403};
404
405static struct attribute *lm63_attributes_fan1[] = {
406 &sensor_dev_attr_fan1_input.dev_attr.attr,
407 &sensor_dev_attr_fan1_min.dev_attr.attr,
408
409 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
410 NULL
411};
412
413static const struct attribute_group lm63_group_fan1 = {
414 .attrs = lm63_attributes_fan1,
415};
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/*
418 * Real code
419 */
420
Jean Delvared5957be2008-07-16 19:30:13 +0200421/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100422static int lm63_detect(struct i2c_client *new_client,
Jean Delvared5957be2008-07-16 19:30:13 +0200423 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Jean Delvared5957be2008-07-16 19:30:13 +0200425 struct i2c_adapter *adapter = new_client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100426 u8 man_id, chip_id, reg_config1, reg_config2;
427 u8 reg_alert_status, reg_alert_mask;
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200428 int address = new_client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvared5957be2008-07-16 19:30:13 +0200431 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Jean Delvare52df6442009-12-09 20:35:57 +0100433 man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID);
434 chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Jean Delvare52df6442009-12-09 20:35:57 +0100436 reg_config1 = i2c_smbus_read_byte_data(new_client,
437 LM63_REG_CONFIG1);
438 reg_config2 = i2c_smbus_read_byte_data(new_client,
439 LM63_REG_CONFIG2);
440 reg_alert_status = i2c_smbus_read_byte_data(new_client,
441 LM63_REG_ALERT_STATUS);
442 reg_alert_mask = i2c_smbus_read_byte_data(new_client,
443 LM63_REG_ALERT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Jean Delvare52df6442009-12-09 20:35:57 +0100445 if (man_id != 0x01 /* National Semiconductor */
Jean Delvare52df6442009-12-09 20:35:57 +0100446 || (reg_config1 & 0x18) != 0x00
447 || (reg_config2 & 0xF8) != 0x00
448 || (reg_alert_status & 0x20) != 0x00
449 || (reg_alert_mask & 0xA4) != 0xA4) {
450 dev_dbg(&adapter->dev,
451 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
452 man_id, chip_id);
453 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200456 if (chip_id == 0x41 && address == 0x4c)
457 strlcpy(info->type, "lm63", I2C_NAME_SIZE);
458 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
459 strlcpy(info->type, "lm64", I2C_NAME_SIZE);
460 else
461 return -ENODEV;
Jean Delvared5957be2008-07-16 19:30:13 +0200462
463 return 0;
464}
465
466static int lm63_probe(struct i2c_client *new_client,
467 const struct i2c_device_id *id)
468{
469 struct lm63_data *data;
470 int err;
471
472 data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
473 if (!data) {
474 err = -ENOMEM;
475 goto exit;
476 }
477
478 i2c_set_clientdata(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100480 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* Initialize the LM63 chip */
483 lm63_init_client(new_client);
484
485 /* Register sysfs hooks */
Jean Delvare0e39e012006-09-24 21:16:40 +0200486 if ((err = sysfs_create_group(&new_client->dev.kobj,
487 &lm63_group)))
Jean Delvared5957be2008-07-16 19:30:13 +0200488 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +0200489 if (data->config & 0x04) { /* tachometer enabled */
490 if ((err = sysfs_create_group(&new_client->dev.kobj,
491 &lm63_group_fan1)))
492 goto exit_remove_files;
493 }
494
Tony Jones1beeffe2007-08-20 13:46:20 -0700495 data->hwmon_dev = hwmon_device_register(&new_client->dev);
496 if (IS_ERR(data->hwmon_dev)) {
497 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200498 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400499 }
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return 0;
502
Jean Delvare0e39e012006-09-24 21:16:40 +0200503exit_remove_files:
504 sysfs_remove_group(&new_client->dev.kobj, &lm63_group);
505 sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506exit_free:
507 kfree(data);
508exit:
509 return err;
510}
511
512/* Idealy we shouldn't have to initialize anything, since the BIOS
513 should have taken care of everything */
514static void lm63_init_client(struct i2c_client *client)
515{
516 struct lm63_data *data = i2c_get_clientdata(client);
517
518 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
519 data->config_fan = i2c_smbus_read_byte_data(client,
520 LM63_REG_CONFIG_FAN);
521
522 /* Start converting if needed */
523 if (data->config & 0x40) { /* standby */
Joe Perches898eb712007-10-18 03:06:30 -0700524 dev_dbg(&client->dev, "Switching to operational mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 data->config &= 0xA7;
526 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
527 data->config);
528 }
529
530 /* We may need pwm1_freq before ever updating the client data */
531 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
532 if (data->pwm1_freq == 0)
533 data->pwm1_freq = 1;
534
535 /* Show some debug info about the LM63 configuration */
536 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
537 (data->config & 0x04) ? "tachometer input" :
538 "alert output");
539 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
540 (data->config_fan & 0x08) ? "1.4" : "360",
541 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
542 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
543 (data->config_fan & 0x10) ? "low" : "high",
544 (data->config_fan & 0x20) ? "manual" : "auto");
545}
546
Jean Delvared5957be2008-07-16 19:30:13 +0200547static int lm63_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400549 struct lm63_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Tony Jones1beeffe2007-08-20 13:46:20 -0700551 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200552 sysfs_remove_group(&client->dev.kobj, &lm63_group);
553 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400554
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400555 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return 0;
557}
558
559static struct lm63_data *lm63_update_device(struct device *dev)
560{
561 struct i2c_client *client = to_i2c_client(dev);
562 struct lm63_data *data = i2c_get_clientdata(client);
563
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100564 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
567 if (data->config & 0x04) { /* tachometer enabled */
568 /* order matters for fan1_input */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200569 data->fan[0] = i2c_smbus_read_byte_data(client,
570 LM63_REG_TACH_COUNT_LSB) & 0xFC;
571 data->fan[0] |= i2c_smbus_read_byte_data(client,
572 LM63_REG_TACH_COUNT_MSB) << 8;
573 data->fan[1] = (i2c_smbus_read_byte_data(client,
574 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
575 | (i2c_smbus_read_byte_data(client,
576 LM63_REG_TACH_LIMIT_MSB) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
579 data->pwm1_freq = i2c_smbus_read_byte_data(client,
580 LM63_REG_PWM_FREQ);
581 if (data->pwm1_freq == 0)
582 data->pwm1_freq = 1;
583 data->pwm1_value = i2c_smbus_read_byte_data(client,
584 LM63_REG_PWM_VALUE);
585
Jean Delvarebc51ae12005-06-05 20:32:27 +0200586 data->temp8[0] = i2c_smbus_read_byte_data(client,
587 LM63_REG_LOCAL_TEMP);
588 data->temp8[1] = i2c_smbus_read_byte_data(client,
589 LM63_REG_LOCAL_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 /* order matters for temp2_input */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200592 data->temp11[0] = i2c_smbus_read_byte_data(client,
593 LM63_REG_REMOTE_TEMP_MSB) << 8;
594 data->temp11[0] |= i2c_smbus_read_byte_data(client,
595 LM63_REG_REMOTE_TEMP_LSB);
596 data->temp11[1] = (i2c_smbus_read_byte_data(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 LM63_REG_REMOTE_LOW_MSB) << 8)
598 | i2c_smbus_read_byte_data(client,
599 LM63_REG_REMOTE_LOW_LSB);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200600 data->temp11[2] = (i2c_smbus_read_byte_data(client,
601 LM63_REG_REMOTE_HIGH_MSB) << 8)
602 | i2c_smbus_read_byte_data(client,
603 LM63_REG_REMOTE_HIGH_LSB);
604 data->temp8[2] = i2c_smbus_read_byte_data(client,
605 LM63_REG_REMOTE_TCRIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
607 LM63_REG_REMOTE_TCRIT_HYST);
608
609 data->alarms = i2c_smbus_read_byte_data(client,
610 LM63_REG_ALERT_STATUS) & 0x7F;
611
612 data->last_updated = jiffies;
613 data->valid = 1;
614 }
615
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100616 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 return data;
619}
620
621static int __init sensors_lm63_init(void)
622{
623 return i2c_add_driver(&lm63_driver);
624}
625
626static void __exit sensors_lm63_exit(void)
627{
628 i2c_del_driver(&lm63_driver);
629}
630
631MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
632MODULE_DESCRIPTION("LM63 driver");
633MODULE_LICENSE("GPL");
634
635module_init(sensors_lm63_init);
636module_exit(sensors_lm63_exit);