blob: 3d882c93b46d48aa546789c1037ec832ba08d20d [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
64#define LM63_REG_CONFIG2 0xBF
65#define LM63_REG_CONFIG_FAN 0x4A
66
67#define LM63_REG_TACH_COUNT_MSB 0x47
68#define LM63_REG_TACH_COUNT_LSB 0x46
69#define LM63_REG_TACH_LIMIT_MSB 0x49
70#define LM63_REG_TACH_LIMIT_LSB 0x48
71
72#define LM63_REG_PWM_VALUE 0x4C
73#define LM63_REG_PWM_FREQ 0x4D
74
75#define LM63_REG_LOCAL_TEMP 0x00
76#define LM63_REG_LOCAL_HIGH 0x05
77
78#define LM63_REG_REMOTE_TEMP_MSB 0x01
79#define LM63_REG_REMOTE_TEMP_LSB 0x10
80#define LM63_REG_REMOTE_OFFSET_MSB 0x11
81#define LM63_REG_REMOTE_OFFSET_LSB 0x12
82#define LM63_REG_REMOTE_HIGH_MSB 0x07
83#define LM63_REG_REMOTE_HIGH_LSB 0x13
84#define LM63_REG_REMOTE_LOW_MSB 0x08
85#define LM63_REG_REMOTE_LOW_LSB 0x14
86#define LM63_REG_REMOTE_TCRIT 0x19
87#define LM63_REG_REMOTE_TCRIT_HYST 0x21
88
89#define LM63_REG_ALERT_STATUS 0x02
90#define LM63_REG_ALERT_MASK 0x16
91
92#define LM63_REG_MAN_ID 0xFE
93#define LM63_REG_CHIP_ID 0xFF
94
Guenter Roeck210961c2012-01-16 22:51:45 +010095#define LM96163_REG_CONFIG_ENHANCED 0x45
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
98 * Conversions and various macros
99 * For tachometer counts, the LM63 uses 16-bit values.
100 * For local temperature and high limit, remote critical limit and hysteresis
Steven Cole44bbe872005-05-03 18:21:25 -0600101 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * For remote temperature, low and high limits, it uses signed 11-bit values
Steven Cole44bbe872005-05-03 18:21:25 -0600103 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
Dirk Eibach2778fb12011-02-09 04:51:34 -0500104 * For LM64 the actual remote diode temperature is 16 degree Celsius higher
105 * than the register reading. Remote temperature setpoints have to be
106 * adapted accordingly.
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
Guenter Roeck210961c2012-01-16 22:51:45 +0100140enum chips { lm63, lm64, lm96163 };
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
143 * Driver data (common to all clients)
144 */
145
Jean Delvared5957be2008-07-16 19:30:13 +0200146static const struct i2c_device_id lm63_id[] = {
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200147 { "lm63", lm63 },
148 { "lm64", lm64 },
Guenter Roeck210961c2012-01-16 22:51:45 +0100149 { "lm96163", lm96163 },
Jean Delvared5957be2008-07-16 19:30:13 +0200150 { }
151};
152MODULE_DEVICE_TABLE(i2c, lm63_id);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static struct i2c_driver lm63_driver = {
Jean Delvared5957be2008-07-16 19:30:13 +0200155 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100156 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100157 .name = "lm63",
158 },
Jean Delvared5957be2008-07-16 19:30:13 +0200159 .probe = lm63_probe,
160 .remove = lm63_remove,
161 .id_table = lm63_id,
162 .detect = lm63_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100163 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164};
165
166/*
167 * Client data (each client gets its own)
168 */
169
170struct lm63_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700171 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100172 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 char valid; /* zero until following fields are valid */
174 unsigned long last_updated; /* in jiffies */
Dirk Eibach2778fb12011-02-09 04:51:34 -0500175 int kind;
176 int temp2_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 /* registers values */
179 u8 config, config_fan;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200180 u16 fan[2]; /* 0: input
181 1: low limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 u8 pwm1_freq;
183 u8 pwm1_value;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200184 s8 temp8[3]; /* 0: local input
185 1: local high limit
186 2: remote critical limit */
Guenter Roeck786375f2012-01-16 22:51:45 +0100187 s16 temp11[4]; /* 0: remote input
Jean Delvarebc51ae12005-06-05 20:32:27 +0200188 1: remote low limit
Guenter Roeck786375f2012-01-16 22:51:45 +0100189 2: remote high limit
190 3: remote offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 u8 temp2_crit_hyst;
192 u8 alarms;
Guenter Roeck210961c2012-01-16 22:51:45 +0100193 bool pwm_highres;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
196/*
197 * Sysfs callback functions and files
198 */
199
Jean Delvarebc51ae12005-06-05 20:32:27 +0200200static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
201 char *buf)
202{
203 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
204 struct lm63_data *data = lm63_update_device(dev);
205 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Jean Delvarebc51ae12005-06-05 20:32:27 +0200208static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
209 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct i2c_client *client = to_i2c_client(dev);
212 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck662bda22012-01-16 22:51:45 +0100213 unsigned long val;
214 int err;
215
216 err = kstrtoul(buf, 10, &val);
217 if (err)
218 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100220 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200221 data->fan[1] = FAN_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200223 data->fan[1] & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200225 data->fan[1] >> 8);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100226 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return count;
228}
229
Jean Delvarebc51ae12005-06-05 20:32:27 +0200230static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy,
231 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct lm63_data *data = lm63_update_device(dev);
Guenter Roeck210961c2012-01-16 22:51:45 +0100234 int pwm;
235
236 if (data->pwm_highres)
237 pwm = data->pwm1_value;
238 else
239 pwm = data->pwm1_value >= 2 * data->pwm1_freq ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 255 : (data->pwm1_value * 255 + data->pwm1_freq) /
Guenter Roeck210961c2012-01-16 22:51:45 +0100241 (2 * data->pwm1_freq);
242
243 return sprintf(buf, "%d\n", pwm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Jean Delvarebc51ae12005-06-05 20:32:27 +0200246static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
247 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct i2c_client *client = to_i2c_client(dev);
250 struct lm63_data *data = i2c_get_clientdata(client);
251 unsigned long val;
Guenter Roeck662bda22012-01-16 22:51:45 +0100252 int err;
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (!(data->config_fan & 0x20)) /* register is read-only */
255 return -EPERM;
256
Guenter Roeck662bda22012-01-16 22:51:45 +0100257 err = kstrtoul(buf, 10, &val);
258 if (err)
259 return err;
260
Guenter Roeck210961c2012-01-16 22:51:45 +0100261 val = SENSORS_LIMIT(val, 0, 255);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100262 mutex_lock(&data->update_lock);
Guenter Roeck210961c2012-01-16 22:51:45 +0100263 data->pwm1_value = data->pwm_highres ? val :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 (val * data->pwm1_freq * 2 + 127) / 255;
265 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100266 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return count;
268}
269
Guenter Roeck662bda22012-01-16 22:51:45 +0100270static ssize_t show_pwm1_enable(struct device *dev,
271 struct device_attribute *dummy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct lm63_data *data = lm63_update_device(dev);
274 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
275}
276
Dirk Eibach2778fb12011-02-09 04:51:34 -0500277/*
278 * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
279 * For remote sensor registers temp2_offset has to be considered,
280 * for local sensor it must not.
281 * So we need separate 8bit accessors for local and remote sensor.
282 */
283static ssize_t show_local_temp8(struct device *dev,
284 struct device_attribute *devattr,
285 char *buf)
Jean Delvarebc51ae12005-06-05 20:32:27 +0200286{
287 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
288 struct lm63_data *data = lm63_update_device(dev);
289 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Dirk Eibach2778fb12011-02-09 04:51:34 -0500292static ssize_t show_remote_temp8(struct device *dev,
293 struct device_attribute *devattr,
294 char *buf)
295{
296 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
297 struct lm63_data *data = lm63_update_device(dev);
298 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index])
299 + data->temp2_offset);
300}
301
302static ssize_t set_local_temp8(struct device *dev,
303 struct device_attribute *dummy,
304 const char *buf, size_t count)
Jean Delvarebc51ae12005-06-05 20:32:27 +0200305{
306 struct i2c_client *client = to_i2c_client(dev);
307 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck662bda22012-01-16 22:51:45 +0100308 long val;
309 int err;
310
311 err = kstrtol(buf, 10, &val);
312 if (err)
313 return err;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200314
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100315 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200316 data->temp8[1] = TEMP8_TO_REG(val);
317 i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, data->temp8[1]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100318 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200319 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200321
322static ssize_t show_temp11(struct device *dev, 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);
Dirk Eibach2778fb12011-02-09 04:51:34 -0500327 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index])
328 + data->temp2_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200330
331static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
332 const char *buf, size_t count)
333{
Guenter Roeck786375f2012-01-16 22:51:45 +0100334 static const u8 reg[6] = {
Jean Delvarebc51ae12005-06-05 20:32:27 +0200335 LM63_REG_REMOTE_LOW_MSB,
336 LM63_REG_REMOTE_LOW_LSB,
337 LM63_REG_REMOTE_HIGH_MSB,
338 LM63_REG_REMOTE_HIGH_LSB,
Guenter Roeck786375f2012-01-16 22:51:45 +0100339 LM63_REG_REMOTE_OFFSET_MSB,
340 LM63_REG_REMOTE_OFFSET_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200341 };
342
343 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
344 struct i2c_client *client = to_i2c_client(dev);
345 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck662bda22012-01-16 22:51:45 +0100346 long val;
347 int err;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200348 int nr = attr->index;
349
Guenter Roeck662bda22012-01-16 22:51:45 +0100350 err = kstrtol(buf, 10, &val);
351 if (err)
352 return err;
353
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100354 mutex_lock(&data->update_lock);
Dirk Eibach2778fb12011-02-09 04:51:34 -0500355 data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200356 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
357 data->temp11[nr] >> 8);
358 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
359 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100360 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200361 return count;
362}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Guenter Roeck662bda22012-01-16 22:51:45 +0100364/*
365 * Hysteresis register holds a relative value, while we want to present
366 * an absolute to user-space
367 */
368static ssize_t show_temp2_crit_hyst(struct device *dev,
369 struct device_attribute *dummy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct lm63_data *data = lm63_update_device(dev);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200372 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2])
Dirk Eibach2778fb12011-02-09 04:51:34 -0500373 + data->temp2_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 - TEMP8_FROM_REG(data->temp2_crit_hyst));
375}
376
Guenter Roeck662bda22012-01-16 22:51:45 +0100377/*
378 * And now the other way around, user-space provides an absolute
379 * hysteresis value and we have to store a relative one
380 */
381static ssize_t set_temp2_crit_hyst(struct device *dev,
382 struct device_attribute *dummy,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200383 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct i2c_client *client = to_i2c_client(dev);
386 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck662bda22012-01-16 22:51:45 +0100387 long val;
388 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 long hyst;
390
Guenter Roeck662bda22012-01-16 22:51:45 +0100391 err = kstrtol(buf, 10, &val);
392 if (err)
393 return err;
394
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100395 mutex_lock(&data->update_lock);
Dirk Eibach2778fb12011-02-09 04:51:34 -0500396 hyst = TEMP8_FROM_REG(data->temp8[2]) + data->temp2_offset - val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
398 HYST_TO_REG(hyst));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100399 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return count;
401}
402
Jean Delvarebc51ae12005-06-05 20:32:27 +0200403static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
404 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct lm63_data *data = lm63_update_device(dev);
407 return sprintf(buf, "%u\n", data->alarms);
408}
409
Jean Delvare2d457712006-09-24 20:52:15 +0200410static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
411 char *buf)
412{
413 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
414 struct lm63_data *data = lm63_update_device(dev);
415 int bitnr = attr->index;
416
417 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
418}
419
Jean Delvarebc51ae12005-06-05 20:32:27 +0200420static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
421static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
422 set_fan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
425static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
426
Dirk Eibach2778fb12011-02-09 04:51:34 -0500427static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
428static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
429 set_local_temp8, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Jean Delvarebc51ae12005-06-05 20:32:27 +0200431static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
432static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
433 set_temp11, 1);
434static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
435 set_temp11, 2);
Guenter Roeck786375f2012-01-16 22:51:45 +0100436static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
437 set_temp11, 3);
Dirk Eibach2778fb12011-02-09 04:51:34 -0500438/*
439 * On LM63, temp2_crit can be set only once, which should be job
440 * of the bootloader.
441 */
442static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
443 NULL, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
445 set_temp2_crit_hyst);
446
Jean Delvare2d457712006-09-24 20:52:15 +0200447/* Individual alarm files */
448static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
449static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400450static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200451static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
452static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
453static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
454/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
456
Jean Delvare0e39e012006-09-24 21:16:40 +0200457static struct attribute *lm63_attributes[] = {
458 &dev_attr_pwm1.attr,
459 &dev_attr_pwm1_enable.attr,
460 &sensor_dev_attr_temp1_input.dev_attr.attr,
461 &sensor_dev_attr_temp2_input.dev_attr.attr,
462 &sensor_dev_attr_temp2_min.dev_attr.attr,
463 &sensor_dev_attr_temp1_max.dev_attr.attr,
464 &sensor_dev_attr_temp2_max.dev_attr.attr,
Guenter Roeck786375f2012-01-16 22:51:45 +0100465 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200466 &sensor_dev_attr_temp2_crit.dev_attr.attr,
467 &dev_attr_temp2_crit_hyst.attr,
468
469 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400470 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200471 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
472 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
473 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
474 &dev_attr_alarms.attr,
475 NULL
476};
477
478static const struct attribute_group lm63_group = {
479 .attrs = lm63_attributes,
480};
481
482static struct attribute *lm63_attributes_fan1[] = {
483 &sensor_dev_attr_fan1_input.dev_attr.attr,
484 &sensor_dev_attr_fan1_min.dev_attr.attr,
485
486 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
487 NULL
488};
489
490static const struct attribute_group lm63_group_fan1 = {
491 .attrs = lm63_attributes_fan1,
492};
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
495 * Real code
496 */
497
Jean Delvared5957be2008-07-16 19:30:13 +0200498/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100499static int lm63_detect(struct i2c_client *new_client,
Jean Delvared5957be2008-07-16 19:30:13 +0200500 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Jean Delvared5957be2008-07-16 19:30:13 +0200502 struct i2c_adapter *adapter = new_client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100503 u8 man_id, chip_id, reg_config1, reg_config2;
504 u8 reg_alert_status, reg_alert_mask;
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200505 int address = new_client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvared5957be2008-07-16 19:30:13 +0200508 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Jean Delvare52df6442009-12-09 20:35:57 +0100510 man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID);
511 chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Jean Delvare52df6442009-12-09 20:35:57 +0100513 reg_config1 = i2c_smbus_read_byte_data(new_client,
514 LM63_REG_CONFIG1);
515 reg_config2 = i2c_smbus_read_byte_data(new_client,
516 LM63_REG_CONFIG2);
517 reg_alert_status = i2c_smbus_read_byte_data(new_client,
518 LM63_REG_ALERT_STATUS);
519 reg_alert_mask = i2c_smbus_read_byte_data(new_client,
520 LM63_REG_ALERT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Jean Delvare52df6442009-12-09 20:35:57 +0100522 if (man_id != 0x01 /* National Semiconductor */
Jean Delvare52df6442009-12-09 20:35:57 +0100523 || (reg_config1 & 0x18) != 0x00
524 || (reg_config2 & 0xF8) != 0x00
525 || (reg_alert_status & 0x20) != 0x00
526 || (reg_alert_mask & 0xA4) != 0xA4) {
527 dev_dbg(&adapter->dev,
528 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
529 man_id, chip_id);
530 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200533 if (chip_id == 0x41 && address == 0x4c)
534 strlcpy(info->type, "lm63", I2C_NAME_SIZE);
535 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
536 strlcpy(info->type, "lm64", I2C_NAME_SIZE);
Guenter Roeck210961c2012-01-16 22:51:45 +0100537 else if (chip_id == 0x49 && address == 0x4c)
538 strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200539 else
540 return -ENODEV;
Jean Delvared5957be2008-07-16 19:30:13 +0200541
542 return 0;
543}
544
545static int lm63_probe(struct i2c_client *new_client,
546 const struct i2c_device_id *id)
547{
548 struct lm63_data *data;
549 int err;
550
551 data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
552 if (!data) {
553 err = -ENOMEM;
554 goto exit;
555 }
556
557 i2c_set_clientdata(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100559 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Dirk Eibach2778fb12011-02-09 04:51:34 -0500561 /* Set the device type */
562 data->kind = id->driver_data;
563 if (data->kind == lm64)
564 data->temp2_offset = 16000;
565
566 /* Initialize chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 lm63_init_client(new_client);
568
569 /* Register sysfs hooks */
Guenter Roeck662bda22012-01-16 22:51:45 +0100570 err = sysfs_create_group(&new_client->dev.kobj, &lm63_group);
571 if (err)
Jean Delvared5957be2008-07-16 19:30:13 +0200572 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +0200573 if (data->config & 0x04) { /* tachometer enabled */
Guenter Roeck662bda22012-01-16 22:51:45 +0100574 err = sysfs_create_group(&new_client->dev.kobj,
575 &lm63_group_fan1);
576 if (err)
Jean Delvare0e39e012006-09-24 21:16:40 +0200577 goto exit_remove_files;
578 }
579
Tony Jones1beeffe2007-08-20 13:46:20 -0700580 data->hwmon_dev = hwmon_device_register(&new_client->dev);
581 if (IS_ERR(data->hwmon_dev)) {
582 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200583 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400584 }
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return 0;
587
Jean Delvare0e39e012006-09-24 21:16:40 +0200588exit_remove_files:
589 sysfs_remove_group(&new_client->dev.kobj, &lm63_group);
590 sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591exit_free:
592 kfree(data);
593exit:
594 return err;
595}
596
Guenter Roeck662bda22012-01-16 22:51:45 +0100597/*
598 * Ideally we shouldn't have to initialize anything, since the BIOS
599 * should have taken care of everything
600 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static void lm63_init_client(struct i2c_client *client)
602{
603 struct lm63_data *data = i2c_get_clientdata(client);
604
605 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
606 data->config_fan = i2c_smbus_read_byte_data(client,
607 LM63_REG_CONFIG_FAN);
608
609 /* Start converting if needed */
610 if (data->config & 0x40) { /* standby */
Joe Perches898eb712007-10-18 03:06:30 -0700611 dev_dbg(&client->dev, "Switching to operational mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 data->config &= 0xA7;
613 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
614 data->config);
615 }
616
617 /* We may need pwm1_freq before ever updating the client data */
618 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
619 if (data->pwm1_freq == 0)
620 data->pwm1_freq = 1;
621
Guenter Roeck210961c2012-01-16 22:51:45 +0100622 /*
623 * For LM96163, check if high resolution PWM is enabled.
624 * Also, check if unsigned temperature format is enabled
625 * and display a warning message if it is.
626 */
627 if (data->kind == lm96163) {
628 u8 config_enhanced
629 = i2c_smbus_read_byte_data(client,
630 LM96163_REG_CONFIG_ENHANCED);
631 if ((config_enhanced & 0x10)
632 && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
633 data->pwm_highres = true;
634 if (config_enhanced & 0x08)
635 dev_warn(&client->dev,
636 "Unsigned format for High and Crit setpoints enabled but not supported by driver\n");
637 }
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* Show some debug info about the LM63 configuration */
640 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
641 (data->config & 0x04) ? "tachometer input" :
642 "alert output");
643 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
644 (data->config_fan & 0x08) ? "1.4" : "360",
645 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
646 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
647 (data->config_fan & 0x10) ? "low" : "high",
648 (data->config_fan & 0x20) ? "manual" : "auto");
649}
650
Jean Delvared5957be2008-07-16 19:30:13 +0200651static int lm63_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400653 struct lm63_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Tony Jones1beeffe2007-08-20 13:46:20 -0700655 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200656 sysfs_remove_group(&client->dev.kobj, &lm63_group);
657 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400658
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400659 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return 0;
661}
662
663static struct lm63_data *lm63_update_device(struct device *dev)
664{
665 struct i2c_client *client = to_i2c_client(dev);
666 struct lm63_data *data = i2c_get_clientdata(client);
667
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100668 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
671 if (data->config & 0x04) { /* tachometer enabled */
672 /* order matters for fan1_input */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200673 data->fan[0] = i2c_smbus_read_byte_data(client,
674 LM63_REG_TACH_COUNT_LSB) & 0xFC;
675 data->fan[0] |= i2c_smbus_read_byte_data(client,
676 LM63_REG_TACH_COUNT_MSB) << 8;
677 data->fan[1] = (i2c_smbus_read_byte_data(client,
678 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
679 | (i2c_smbus_read_byte_data(client,
680 LM63_REG_TACH_LIMIT_MSB) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
683 data->pwm1_freq = i2c_smbus_read_byte_data(client,
684 LM63_REG_PWM_FREQ);
685 if (data->pwm1_freq == 0)
686 data->pwm1_freq = 1;
687 data->pwm1_value = i2c_smbus_read_byte_data(client,
688 LM63_REG_PWM_VALUE);
689
Jean Delvarebc51ae12005-06-05 20:32:27 +0200690 data->temp8[0] = i2c_smbus_read_byte_data(client,
691 LM63_REG_LOCAL_TEMP);
692 data->temp8[1] = i2c_smbus_read_byte_data(client,
693 LM63_REG_LOCAL_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* order matters for temp2_input */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200696 data->temp11[0] = i2c_smbus_read_byte_data(client,
697 LM63_REG_REMOTE_TEMP_MSB) << 8;
698 data->temp11[0] |= i2c_smbus_read_byte_data(client,
699 LM63_REG_REMOTE_TEMP_LSB);
700 data->temp11[1] = (i2c_smbus_read_byte_data(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 LM63_REG_REMOTE_LOW_MSB) << 8)
702 | i2c_smbus_read_byte_data(client,
703 LM63_REG_REMOTE_LOW_LSB);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200704 data->temp11[2] = (i2c_smbus_read_byte_data(client,
705 LM63_REG_REMOTE_HIGH_MSB) << 8)
706 | i2c_smbus_read_byte_data(client,
707 LM63_REG_REMOTE_HIGH_LSB);
Guenter Roeck786375f2012-01-16 22:51:45 +0100708 data->temp11[3] = (i2c_smbus_read_byte_data(client,
709 LM63_REG_REMOTE_OFFSET_MSB) << 8)
710 | i2c_smbus_read_byte_data(client,
711 LM63_REG_REMOTE_OFFSET_LSB);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200712 data->temp8[2] = i2c_smbus_read_byte_data(client,
713 LM63_REG_REMOTE_TCRIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
715 LM63_REG_REMOTE_TCRIT_HYST);
716
717 data->alarms = i2c_smbus_read_byte_data(client,
718 LM63_REG_ALERT_STATUS) & 0x7F;
719
720 data->last_updated = jiffies;
721 data->valid = 1;
722 }
723
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100724 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 return data;
727}
728
729static int __init sensors_lm63_init(void)
730{
731 return i2c_add_driver(&lm63_driver);
732}
733
734static void __exit sensors_lm63_exit(void)
735{
736 i2c_del_driver(&lm63_driver);
737}
738
739MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
740MODULE_DESCRIPTION("LM63 driver");
741MODULE_LICENSE("GPL");
742
743module_init(sensors_lm63_init);
744module_exit(sensors_lm63_exit);