blob: 910599ac31ca50adf06c93c4b390a220e9be1867 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lm63.c - driver for the National Semiconductor LM63 temperature sensor
3 * with integrated fan control
Jean Delvared5957be2008-07-16 19:30:13 +02004 * Copyright (C) 2004-2008 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Based on the lm90 driver.
6 *
7 * The LM63 is a sensor chip made by National Semiconductor. It measures
8 * two temperatures (its own and one external one) and the speed of one
9 * fan, those speed it can additionally control. Complete datasheet can be
10 * obtained from National's website at:
11 * http://www.national.com/pf/LM/LM63.html
12 *
13 * The LM63 is basically an LM86 with fan speed monitoring and control
14 * capabilities added. It misses some of the LM86 features though:
15 * - No low limit for local temperature.
16 * - No critical limit for local temperature.
17 * - Critical limit for remote temperature can be changed only once. We
18 * will consider that the critical limit is read-only.
19 *
20 * The datasheet isn't very clear about what the tachometer reading is.
21 * I had a explanation from National Semiconductor though. The two lower
22 * bits of the read value have to be masked out. The value is still 16 bit
23 * in width.
24 *
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 */
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/module.h>
41#include <linux/init.h>
42#include <linux/slab.h>
43#include <linux/jiffies.h>
44#include <linux/i2c.h>
Jean Delvare10c08f82005-06-06 19:34:45 +020045#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040046#include <linux/hwmon.h>
47#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010048#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020049#include <linux/sysfs.h>
Guenter Roeck210961c2012-01-16 22:51:45 +010050#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
53 * Addresses to scan
Jean Delvared93ab782012-01-16 22:51:47 +010054 * Address is fully defined internally and cannot be changed except for
55 * LM64 which has one pin dedicated to address selection.
56 * LM63 and LM96163 have address 0x4c.
57 * LM64 can have address 0x18 or 0x4e.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59
Matthew Garrett10f2ed32010-05-27 19:58:38 +020060static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * The LM63 registers
64 */
65
66#define LM63_REG_CONFIG1 0x03
Guenter Roeck04738b22012-01-16 22:51:46 +010067#define LM63_REG_CONVRATE 0x04
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define LM63_REG_CONFIG2 0xBF
69#define LM63_REG_CONFIG_FAN 0x4A
70
71#define LM63_REG_TACH_COUNT_MSB 0x47
72#define LM63_REG_TACH_COUNT_LSB 0x46
73#define LM63_REG_TACH_LIMIT_MSB 0x49
74#define LM63_REG_TACH_LIMIT_LSB 0x48
75
76#define LM63_REG_PWM_VALUE 0x4C
77#define LM63_REG_PWM_FREQ 0x4D
Jean Delvared216f682012-01-16 22:51:47 +010078#define LM63_REG_LUT_TEMP_HYST 0x4F
79#define LM63_REG_LUT_TEMP(nr) (0x50 + 2 * (nr))
80#define LM63_REG_LUT_PWM(nr) (0x51 + 2 * (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82#define LM63_REG_LOCAL_TEMP 0x00
83#define LM63_REG_LOCAL_HIGH 0x05
84
85#define LM63_REG_REMOTE_TEMP_MSB 0x01
86#define LM63_REG_REMOTE_TEMP_LSB 0x10
87#define LM63_REG_REMOTE_OFFSET_MSB 0x11
88#define LM63_REG_REMOTE_OFFSET_LSB 0x12
89#define LM63_REG_REMOTE_HIGH_MSB 0x07
90#define LM63_REG_REMOTE_HIGH_LSB 0x13
91#define LM63_REG_REMOTE_LOW_MSB 0x08
92#define LM63_REG_REMOTE_LOW_LSB 0x14
93#define LM63_REG_REMOTE_TCRIT 0x19
94#define LM63_REG_REMOTE_TCRIT_HYST 0x21
95
96#define LM63_REG_ALERT_STATUS 0x02
97#define LM63_REG_ALERT_MASK 0x16
98
99#define LM63_REG_MAN_ID 0xFE
100#define LM63_REG_CHIP_ID 0xFF
101
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100102#define LM96163_REG_TRUTHERM 0x30
Guenter Roecke872c912012-01-16 22:51:46 +0100103#define LM96163_REG_REMOTE_TEMP_U_MSB 0x31
104#define LM96163_REG_REMOTE_TEMP_U_LSB 0x32
Guenter Roeck210961c2012-01-16 22:51:45 +0100105#define LM96163_REG_CONFIG_ENHANCED 0x45
106
Guenter Roeck04738b22012-01-16 22:51:46 +0100107#define LM63_MAX_CONVRATE 9
108
109#define LM63_MAX_CONVRATE_HZ 32
110#define LM96163_MAX_CONVRATE_HZ 26
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
113 * Conversions and various macros
114 * For tachometer counts, the LM63 uses 16-bit values.
115 * For local temperature and high limit, remote critical limit and hysteresis
Steven Cole44bbe872005-05-03 18:21:25 -0600116 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * For remote temperature, low and high limits, it uses signed 11-bit values
Steven Cole44bbe872005-05-03 18:21:25 -0600118 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
Dirk Eibach2778fb12011-02-09 04:51:34 -0500119 * For LM64 the actual remote diode temperature is 16 degree Celsius higher
120 * than the register reading. Remote temperature setpoints have to be
121 * adapted accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
123
124#define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
125 5400000 / (reg))
126#define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
127 (5400000 / (val)) & 0xFFFC)
128#define TEMP8_FROM_REG(reg) ((reg) * 1000)
129#define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
130 (val) >= 127000 ? 127 : \
131 (val) < 0 ? ((val) - 500) / 1000 : \
132 ((val) + 500) / 1000)
Guenter Roeck94e55df2012-01-16 22:51:46 +0100133#define TEMP8U_TO_REG(val) ((val) <= 0 ? 0 : \
134 (val) >= 255000 ? 255 : \
135 ((val) + 500) / 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
137#define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
138 (val) >= 127875 ? 0x7FE0 : \
139 (val) < 0 ? ((val) - 62) / 125 * 32 : \
140 ((val) + 62) / 125 * 32)
Guenter Roecke872c912012-01-16 22:51:46 +0100141#define TEMP11U_TO_REG(val) ((val) <= 0 ? 0 : \
142 (val) >= 255875 ? 0xFFE0 : \
143 ((val) + 62) / 125 * 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
145 (val) >= 127000 ? 127 : \
146 ((val) + 500) / 1000)
147
Guenter Roeck04738b22012-01-16 22:51:46 +0100148#define UPDATE_INTERVAL(max, rate) \
149 ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max))
150
Guenter Roeck210961c2012-01-16 22:51:45 +0100151enum chips { lm63, lm64, lm96163 };
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * Client data (each client gets its own)
155 */
156
157struct lm63_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700158 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100159 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 char valid; /* zero until following fields are valid */
Jean Delvared216f682012-01-16 22:51:47 +0100161 char lut_valid; /* zero until lut fields are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 unsigned long last_updated; /* in jiffies */
Jean Delvared216f682012-01-16 22:51:47 +0100163 unsigned long lut_last_updated; /* in jiffies */
Guenter Roeck04738b22012-01-16 22:51:46 +0100164 enum chips kind;
Dirk Eibach2778fb12011-02-09 04:51:34 -0500165 int temp2_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Guenter Roeck04738b22012-01-16 22:51:46 +0100167 int update_interval; /* in milliseconds */
168 int max_convrate_hz;
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100169 int lut_size; /* 8 or 12 */
Guenter Roeck04738b22012-01-16 22:51:46 +0100170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /* registers values */
172 u8 config, config_fan;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200173 u16 fan[2]; /* 0: input
174 1: low limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 u8 pwm1_freq;
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100176 u8 pwm1[13]; /* 0: current output
177 1-12: lookup table */
178 s8 temp8[15]; /* 0: local input
Jean Delvarebc51ae12005-06-05 20:32:27 +0200179 1: local high limit
Jean Delvared216f682012-01-16 22:51:47 +0100180 2: remote critical limit
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100181 3-14: lookup table */
Guenter Roeck786375f2012-01-16 22:51:45 +0100182 s16 temp11[4]; /* 0: remote input
Jean Delvarebc51ae12005-06-05 20:32:27 +0200183 1: remote low limit
Guenter Roeck786375f2012-01-16 22:51:45 +0100184 2: remote high limit
185 3: remote offset */
Guenter Roecke872c912012-01-16 22:51:46 +0100186 u16 temp11u; /* remote input (unsigned) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 u8 temp2_crit_hyst;
Jean Delvared216f682012-01-16 22:51:47 +0100188 u8 lut_temp_hyst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 u8 alarms;
Guenter Roeck210961c2012-01-16 22:51:45 +0100190 bool pwm_highres;
Jean Delvared216f682012-01-16 22:51:47 +0100191 bool lut_temp_highres;
Guenter Roecke872c912012-01-16 22:51:46 +0100192 bool remote_unsigned; /* true if unsigned remote upper limits */
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100193 bool trutherm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Guenter Roecke872c912012-01-16 22:51:46 +0100196static inline int temp8_from_reg(struct lm63_data *data, int nr)
197{
198 if (data->remote_unsigned)
199 return TEMP8_FROM_REG((u8)data->temp8[nr]);
200 return TEMP8_FROM_REG(data->temp8[nr]);
201}
202
Jean Delvared216f682012-01-16 22:51:47 +0100203static inline int lut_temp_from_reg(struct lm63_data *data, int nr)
204{
205 return data->temp8[nr] * (data->lut_temp_highres ? 500 : 1000);
206}
207
Jean Delvare817c6cc2012-03-23 10:02:19 +0100208/*
209 * Update the lookup table register cache.
210 * client->update_lock must be held when calling this function.
211 */
212static void lm63_update_lut(struct i2c_client *client)
213{
214 struct lm63_data *data = i2c_get_clientdata(client);
215 int i;
216
217 if (time_after(jiffies, data->lut_last_updated + 5 * HZ) ||
218 !data->lut_valid) {
219 for (i = 0; i < data->lut_size; i++) {
220 data->pwm1[1 + i] = i2c_smbus_read_byte_data(client,
221 LM63_REG_LUT_PWM(i));
222 data->temp8[3 + i] = i2c_smbus_read_byte_data(client,
223 LM63_REG_LUT_TEMP(i));
224 }
225 data->lut_temp_hyst = i2c_smbus_read_byte_data(client,
226 LM63_REG_LUT_TEMP_HYST);
227
228 data->lut_last_updated = jiffies;
229 data->lut_valid = 1;
230 }
231}
232
Jean Delvaredac27dc2012-03-23 10:02:18 +0100233static struct lm63_data *lm63_update_device(struct device *dev)
234{
235 struct i2c_client *client = to_i2c_client(dev);
236 struct lm63_data *data = i2c_get_clientdata(client);
237 unsigned long next_update;
Jean Delvaredac27dc2012-03-23 10:02:18 +0100238
239 mutex_lock(&data->update_lock);
240
241 next_update = data->last_updated
242 + msecs_to_jiffies(data->update_interval) + 1;
243
244 if (time_after(jiffies, next_update) || !data->valid) {
245 if (data->config & 0x04) { /* tachometer enabled */
246 /* order matters for fan1_input */
247 data->fan[0] = i2c_smbus_read_byte_data(client,
248 LM63_REG_TACH_COUNT_LSB) & 0xFC;
249 data->fan[0] |= i2c_smbus_read_byte_data(client,
250 LM63_REG_TACH_COUNT_MSB) << 8;
251 data->fan[1] = (i2c_smbus_read_byte_data(client,
252 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
253 | (i2c_smbus_read_byte_data(client,
254 LM63_REG_TACH_LIMIT_MSB) << 8);
255 }
256
257 data->pwm1_freq = i2c_smbus_read_byte_data(client,
258 LM63_REG_PWM_FREQ);
259 if (data->pwm1_freq == 0)
260 data->pwm1_freq = 1;
261 data->pwm1[0] = i2c_smbus_read_byte_data(client,
262 LM63_REG_PWM_VALUE);
263
264 data->temp8[0] = i2c_smbus_read_byte_data(client,
265 LM63_REG_LOCAL_TEMP);
266 data->temp8[1] = i2c_smbus_read_byte_data(client,
267 LM63_REG_LOCAL_HIGH);
268
269 /* order matters for temp2_input */
270 data->temp11[0] = i2c_smbus_read_byte_data(client,
271 LM63_REG_REMOTE_TEMP_MSB) << 8;
272 data->temp11[0] |= i2c_smbus_read_byte_data(client,
273 LM63_REG_REMOTE_TEMP_LSB);
274 data->temp11[1] = (i2c_smbus_read_byte_data(client,
275 LM63_REG_REMOTE_LOW_MSB) << 8)
276 | i2c_smbus_read_byte_data(client,
277 LM63_REG_REMOTE_LOW_LSB);
278 data->temp11[2] = (i2c_smbus_read_byte_data(client,
279 LM63_REG_REMOTE_HIGH_MSB) << 8)
280 | i2c_smbus_read_byte_data(client,
281 LM63_REG_REMOTE_HIGH_LSB);
282 data->temp11[3] = (i2c_smbus_read_byte_data(client,
283 LM63_REG_REMOTE_OFFSET_MSB) << 8)
284 | i2c_smbus_read_byte_data(client,
285 LM63_REG_REMOTE_OFFSET_LSB);
286
287 if (data->kind == lm96163)
288 data->temp11u = (i2c_smbus_read_byte_data(client,
289 LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
290 | i2c_smbus_read_byte_data(client,
291 LM96163_REG_REMOTE_TEMP_U_LSB);
292
293 data->temp8[2] = i2c_smbus_read_byte_data(client,
294 LM63_REG_REMOTE_TCRIT);
295 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
296 LM63_REG_REMOTE_TCRIT_HYST);
297
298 data->alarms = i2c_smbus_read_byte_data(client,
299 LM63_REG_ALERT_STATUS) & 0x7F;
300
301 data->last_updated = jiffies;
302 data->valid = 1;
303 }
304
Jean Delvare817c6cc2012-03-23 10:02:19 +0100305 lm63_update_lut(client);
Jean Delvaredac27dc2012-03-23 10:02:18 +0100306
307 mutex_unlock(&data->update_lock);
308
309 return data;
310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/*
Jean Delvare817c6cc2012-03-23 10:02:19 +0100313 * Trip points in the lookup table should be in ascending order for both
314 * temperatures and PWM output values.
315 */
316static int lm63_lut_looks_bad(struct i2c_client *client)
317{
318 struct lm63_data *data = i2c_get_clientdata(client);
319 int i;
320
321 mutex_lock(&data->update_lock);
322 lm63_update_lut(client);
323
324 for (i = 1; i < data->lut_size; i++) {
325 if (data->pwm1[1 + i - 1] > data->pwm1[1 + i]
326 || data->temp8[3 + i - 1] > data->temp8[3 + i]) {
327 dev_warn(&client->dev,
328 "Lookup table doesn't look sane (check entries %d and %d)\n",
329 i, i + 1);
330 break;
331 }
332 }
333 mutex_unlock(&data->update_lock);
334
335 return i == data->lut_size ? 0 : 1;
336}
337
338/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * Sysfs callback functions and files
340 */
341
Jean Delvarebc51ae12005-06-05 20:32:27 +0200342static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
343 char *buf)
344{
345 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
346 struct lm63_data *data = lm63_update_device(dev);
347 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Jean Delvarebc51ae12005-06-05 20:32:27 +0200350static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
351 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct i2c_client *client = to_i2c_client(dev);
354 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck662bda22012-01-16 22:51:45 +0100355 unsigned long val;
356 int err;
357
358 err = kstrtoul(buf, 10, &val);
359 if (err)
360 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100362 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200363 data->fan[1] = FAN_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200365 data->fan[1] & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200367 data->fan[1] >> 8);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100368 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return count;
370}
371
Jean Delvared216f682012-01-16 22:51:47 +0100372static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200373 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Jean Delvared216f682012-01-16 22:51:47 +0100375 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct lm63_data *data = lm63_update_device(dev);
Jean Delvared216f682012-01-16 22:51:47 +0100377 int nr = attr->index;
Guenter Roeck210961c2012-01-16 22:51:45 +0100378 int pwm;
379
380 if (data->pwm_highres)
Jean Delvared216f682012-01-16 22:51:47 +0100381 pwm = data->pwm1[nr];
Guenter Roeck210961c2012-01-16 22:51:45 +0100382 else
Jean Delvared216f682012-01-16 22:51:47 +0100383 pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ?
384 255 : (data->pwm1[nr] * 255 + data->pwm1_freq) /
Guenter Roeck210961c2012-01-16 22:51:45 +0100385 (2 * data->pwm1_freq);
386
387 return sprintf(buf, "%d\n", pwm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Jean Delvarebc51ae12005-06-05 20:32:27 +0200390static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
391 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct i2c_client *client = to_i2c_client(dev);
394 struct lm63_data *data = i2c_get_clientdata(client);
395 unsigned long val;
Guenter Roeck662bda22012-01-16 22:51:45 +0100396 int err;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (!(data->config_fan & 0x20)) /* register is read-only */
399 return -EPERM;
400
Guenter Roeck662bda22012-01-16 22:51:45 +0100401 err = kstrtoul(buf, 10, &val);
402 if (err)
403 return err;
404
Guenter Roeck210961c2012-01-16 22:51:45 +0100405 val = SENSORS_LIMIT(val, 0, 255);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100406 mutex_lock(&data->update_lock);
Jean Delvared216f682012-01-16 22:51:47 +0100407 data->pwm1[0] = data->pwm_highres ? val :
408 (val * data->pwm1_freq * 2 + 127) / 255;
409 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100410 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return count;
412}
413
Guenter Roeck662bda22012-01-16 22:51:45 +0100414static ssize_t show_pwm1_enable(struct device *dev,
415 struct device_attribute *dummy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct lm63_data *data = lm63_update_device(dev);
418 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
419}
420
Jean Delvare817c6cc2012-03-23 10:02:19 +0100421static ssize_t set_pwm1_enable(struct device *dev,
422 struct device_attribute *dummy,
423 const char *buf, size_t count)
424{
425 struct i2c_client *client = to_i2c_client(dev);
426 struct lm63_data *data = i2c_get_clientdata(client);
427 unsigned long val;
428 int err;
429
430 err = kstrtoul(buf, 10, &val);
431 if (err)
432 return err;
433 if (val < 1 || val > 2)
434 return -EINVAL;
435
436 /*
437 * Only let the user switch to automatic mode if the lookup table
438 * looks sane.
439 */
440 if (val == 2 && lm63_lut_looks_bad(client))
441 return -EPERM;
442
443 mutex_lock(&data->update_lock);
444 data->config_fan = i2c_smbus_read_byte_data(client,
445 LM63_REG_CONFIG_FAN);
446 if (val == 1)
447 data->config_fan |= 0x20;
448 else
449 data->config_fan &= ~0x20;
450 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN,
451 data->config_fan);
452 mutex_unlock(&data->update_lock);
453 return count;
454}
455
Dirk Eibach2778fb12011-02-09 04:51:34 -0500456/*
457 * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
458 * For remote sensor registers temp2_offset has to be considered,
459 * for local sensor it must not.
460 * So we need separate 8bit accessors for local and remote sensor.
461 */
462static ssize_t show_local_temp8(struct device *dev,
463 struct device_attribute *devattr,
464 char *buf)
Jean Delvarebc51ae12005-06-05 20:32:27 +0200465{
466 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
467 struct lm63_data *data = lm63_update_device(dev);
468 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Dirk Eibach2778fb12011-02-09 04:51:34 -0500471static ssize_t show_remote_temp8(struct device *dev,
472 struct device_attribute *devattr,
473 char *buf)
474{
475 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
476 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100477 return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
Dirk Eibach2778fb12011-02-09 04:51:34 -0500478 + data->temp2_offset);
479}
480
Jean Delvared216f682012-01-16 22:51:47 +0100481static ssize_t show_lut_temp(struct device *dev,
482 struct device_attribute *devattr,
483 char *buf)
484{
485 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
486 struct lm63_data *data = lm63_update_device(dev);
487 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
488 + data->temp2_offset);
489}
490
Guenter Roeck94e55df2012-01-16 22:51:46 +0100491static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
492 const char *buf, size_t count)
Jean Delvarebc51ae12005-06-05 20:32:27 +0200493{
Guenter Roeck94e55df2012-01-16 22:51:46 +0100494 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200495 struct i2c_client *client = to_i2c_client(dev);
496 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck94e55df2012-01-16 22:51:46 +0100497 int nr = attr->index;
498 int reg = nr == 2 ? LM63_REG_REMOTE_TCRIT : LM63_REG_LOCAL_HIGH;
Guenter Roeck662bda22012-01-16 22:51:45 +0100499 long val;
500 int err;
Guenter Roeck94e55df2012-01-16 22:51:46 +0100501 int temp;
Guenter Roeck662bda22012-01-16 22:51:45 +0100502
503 err = kstrtol(buf, 10, &val);
504 if (err)
505 return err;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200506
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100507 mutex_lock(&data->update_lock);
Guenter Roeck94e55df2012-01-16 22:51:46 +0100508 if (nr == 2) {
509 if (data->remote_unsigned)
510 temp = TEMP8U_TO_REG(val - data->temp2_offset);
511 else
512 temp = TEMP8_TO_REG(val - data->temp2_offset);
513 } else {
514 temp = TEMP8_TO_REG(val);
515 }
516 data->temp8[nr] = temp;
517 i2c_smbus_write_byte_data(client, reg, temp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100518 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200519 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200521
522static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
523 char *buf)
524{
525 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
526 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100527 int nr = attr->index;
528 int temp;
529
530 if (!nr) {
531 /*
532 * Use unsigned temperature unless its value is zero.
533 * If it is zero, use signed temperature.
534 */
535 if (data->temp11u)
536 temp = TEMP11_FROM_REG(data->temp11u);
537 else
538 temp = TEMP11_FROM_REG(data->temp11[nr]);
539 } else {
540 if (data->remote_unsigned && nr == 2)
541 temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
542 else
543 temp = TEMP11_FROM_REG(data->temp11[nr]);
544 }
545 return sprintf(buf, "%d\n", temp + data->temp2_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200547
548static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
549 const char *buf, size_t count)
550{
Guenter Roeck786375f2012-01-16 22:51:45 +0100551 static const u8 reg[6] = {
Jean Delvarebc51ae12005-06-05 20:32:27 +0200552 LM63_REG_REMOTE_LOW_MSB,
553 LM63_REG_REMOTE_LOW_LSB,
554 LM63_REG_REMOTE_HIGH_MSB,
555 LM63_REG_REMOTE_HIGH_LSB,
Guenter Roeck786375f2012-01-16 22:51:45 +0100556 LM63_REG_REMOTE_OFFSET_MSB,
557 LM63_REG_REMOTE_OFFSET_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200558 };
559
560 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
561 struct i2c_client *client = to_i2c_client(dev);
562 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck662bda22012-01-16 22:51:45 +0100563 long val;
564 int err;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200565 int nr = attr->index;
566
Guenter Roeck662bda22012-01-16 22:51:45 +0100567 err = kstrtol(buf, 10, &val);
568 if (err)
569 return err;
570
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100571 mutex_lock(&data->update_lock);
Guenter Roecke872c912012-01-16 22:51:46 +0100572 if (data->remote_unsigned && nr == 2)
573 data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
574 else
575 data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
576
Jean Delvarebc51ae12005-06-05 20:32:27 +0200577 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
578 data->temp11[nr] >> 8);
579 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
580 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100581 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200582 return count;
583}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Guenter Roeck662bda22012-01-16 22:51:45 +0100585/*
586 * Hysteresis register holds a relative value, while we want to present
587 * an absolute to user-space
588 */
589static ssize_t show_temp2_crit_hyst(struct device *dev,
590 struct device_attribute *dummy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100593 return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
Dirk Eibach2778fb12011-02-09 04:51:34 -0500594 + data->temp2_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 - TEMP8_FROM_REG(data->temp2_crit_hyst));
596}
597
Jean Delvared216f682012-01-16 22:51:47 +0100598static ssize_t show_lut_temp_hyst(struct device *dev,
599 struct device_attribute *devattr, char *buf)
600{
601 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
602 struct lm63_data *data = lm63_update_device(dev);
603
604 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
605 + data->temp2_offset
606 - TEMP8_FROM_REG(data->lut_temp_hyst));
607}
608
Guenter Roeck662bda22012-01-16 22:51:45 +0100609/*
610 * And now the other way around, user-space provides an absolute
611 * hysteresis value and we have to store a relative one
612 */
613static ssize_t set_temp2_crit_hyst(struct device *dev,
614 struct device_attribute *dummy,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200615 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct i2c_client *client = to_i2c_client(dev);
618 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck662bda22012-01-16 22:51:45 +0100619 long val;
620 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 long hyst;
622
Guenter Roeck662bda22012-01-16 22:51:45 +0100623 err = kstrtol(buf, 10, &val);
624 if (err)
625 return err;
626
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100627 mutex_lock(&data->update_lock);
Guenter Roecke872c912012-01-16 22:51:46 +0100628 hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
630 HYST_TO_REG(hyst));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100631 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return count;
633}
634
Guenter Roeck04738b22012-01-16 22:51:46 +0100635/*
636 * Set conversion rate.
637 * client->update_lock must be held when calling this function.
638 */
639static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data,
640 unsigned int interval)
641{
642 int i;
643 unsigned int update_interval;
644
645 /* Shift calculations to avoid rounding errors */
646 interval <<= 6;
647
648 /* find the nearest update rate */
649 update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
650 / data->max_convrate_hz;
651 for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
652 if (interval >= update_interval * 3 / 4)
653 break;
654
655 i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
656 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
657}
658
659static ssize_t show_update_interval(struct device *dev,
660 struct device_attribute *attr, char *buf)
661{
662 struct lm63_data *data = dev_get_drvdata(dev);
663
664 return sprintf(buf, "%u\n", data->update_interval);
665}
666
667static ssize_t set_update_interval(struct device *dev,
668 struct device_attribute *attr,
669 const char *buf, size_t count)
670{
671 struct i2c_client *client = to_i2c_client(dev);
672 struct lm63_data *data = i2c_get_clientdata(client);
673 unsigned long val;
674 int err;
675
676 err = kstrtoul(buf, 10, &val);
677 if (err)
678 return err;
679
680 mutex_lock(&data->update_lock);
681 lm63_set_convrate(client, data, SENSORS_LIMIT(val, 0, 100000));
682 mutex_unlock(&data->update_lock);
683
684 return count;
685}
686
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100687static ssize_t show_type(struct device *dev, struct device_attribute *attr,
688 char *buf)
689{
690 struct i2c_client *client = to_i2c_client(dev);
691 struct lm63_data *data = i2c_get_clientdata(client);
692
693 return sprintf(buf, data->trutherm ? "1\n" : "2\n");
694}
695
696static ssize_t set_type(struct device *dev, struct device_attribute *attr,
697 const char *buf, size_t count)
698{
699 struct i2c_client *client = to_i2c_client(dev);
700 struct lm63_data *data = i2c_get_clientdata(client);
701 unsigned long val;
702 int ret;
703 u8 reg;
704
705 ret = kstrtoul(buf, 10, &val);
706 if (ret < 0)
707 return ret;
708 if (val != 1 && val != 2)
709 return -EINVAL;
710
711 mutex_lock(&data->update_lock);
712 data->trutherm = val == 1;
713 reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02;
714 i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM,
715 reg | (data->trutherm ? 0x02 : 0x00));
716 data->valid = 0;
717 mutex_unlock(&data->update_lock);
718
719 return count;
720}
721
Jean Delvarebc51ae12005-06-05 20:32:27 +0200722static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
723 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 struct lm63_data *data = lm63_update_device(dev);
726 return sprintf(buf, "%u\n", data->alarms);
727}
728
Jean Delvare2d457712006-09-24 20:52:15 +0200729static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
730 char *buf)
731{
732 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
733 struct lm63_data *data = lm63_update_device(dev);
734 int bitnr = attr->index;
735
736 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
737}
738
Jean Delvarebc51ae12005-06-05 20:32:27 +0200739static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
740static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
741 set_fan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Jean Delvared216f682012-01-16 22:51:47 +0100743static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1, 0);
Jean Delvare817c6cc2012-03-23 10:02:19 +0100744static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
745 show_pwm1_enable, set_pwm1_enable);
Jean Delvared216f682012-01-16 22:51:47 +0100746static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO, show_pwm1, NULL, 1);
747static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IRUGO,
748 show_lut_temp, NULL, 3);
749static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp_hyst, S_IRUGO,
750 show_lut_temp_hyst, NULL, 3);
751static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IRUGO, show_pwm1, NULL, 2);
752static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO,
753 show_lut_temp, NULL, 4);
754static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp_hyst, S_IRUGO,
755 show_lut_temp_hyst, NULL, 4);
756static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO, show_pwm1, NULL, 3);
757static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp, S_IRUGO,
758 show_lut_temp, NULL, 5);
759static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp_hyst, S_IRUGO,
760 show_lut_temp_hyst, NULL, 5);
761static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IRUGO, show_pwm1, NULL, 4);
762static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp, S_IRUGO,
763 show_lut_temp, NULL, 6);
764static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp_hyst, S_IRUGO,
765 show_lut_temp_hyst, NULL, 6);
766static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IRUGO, show_pwm1, NULL, 5);
767static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp, S_IRUGO,
768 show_lut_temp, NULL, 7);
769static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp_hyst, S_IRUGO,
770 show_lut_temp_hyst, NULL, 7);
771static SENSOR_DEVICE_ATTR(pwm1_auto_point6_pwm, S_IRUGO, show_pwm1, NULL, 6);
772static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp, S_IRUGO,
773 show_lut_temp, NULL, 8);
774static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp_hyst, S_IRUGO,
775 show_lut_temp_hyst, NULL, 8);
776static SENSOR_DEVICE_ATTR(pwm1_auto_point7_pwm, S_IRUGO, show_pwm1, NULL, 7);
777static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp, S_IRUGO,
778 show_lut_temp, NULL, 9);
779static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp_hyst, S_IRUGO,
780 show_lut_temp_hyst, NULL, 9);
781static SENSOR_DEVICE_ATTR(pwm1_auto_point8_pwm, S_IRUGO, show_pwm1, NULL, 8);
782static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp, S_IRUGO,
783 show_lut_temp, NULL, 10);
784static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp_hyst, S_IRUGO,
785 show_lut_temp_hyst, NULL, 10);
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100786static SENSOR_DEVICE_ATTR(pwm1_auto_point9_pwm, S_IRUGO, show_pwm1, NULL, 9);
787static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp, S_IRUGO,
788 show_lut_temp, NULL, 11);
789static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp_hyst, S_IRUGO,
790 show_lut_temp_hyst, NULL, 11);
791static SENSOR_DEVICE_ATTR(pwm1_auto_point10_pwm, S_IRUGO, show_pwm1, NULL, 10);
792static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp, S_IRUGO,
793 show_lut_temp, NULL, 12);
794static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp_hyst, S_IRUGO,
795 show_lut_temp_hyst, NULL, 12);
796static SENSOR_DEVICE_ATTR(pwm1_auto_point11_pwm, S_IRUGO, show_pwm1, NULL, 11);
797static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp, S_IRUGO,
798 show_lut_temp, NULL, 13);
799static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp_hyst, S_IRUGO,
800 show_lut_temp_hyst, NULL, 13);
801static SENSOR_DEVICE_ATTR(pwm1_auto_point12_pwm, S_IRUGO, show_pwm1, NULL, 12);
802static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp, S_IRUGO,
803 show_lut_temp, NULL, 14);
804static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp_hyst, S_IRUGO,
805 show_lut_temp_hyst, NULL, 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Dirk Eibach2778fb12011-02-09 04:51:34 -0500807static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
808static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
Guenter Roeck94e55df2012-01-16 22:51:46 +0100809 set_temp8, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Jean Delvarebc51ae12005-06-05 20:32:27 +0200811static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
812static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
813 set_temp11, 1);
814static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
815 set_temp11, 2);
Guenter Roeck786375f2012-01-16 22:51:45 +0100816static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
817 set_temp11, 3);
Dirk Eibach2778fb12011-02-09 04:51:34 -0500818static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
Guenter Roeck94e55df2012-01-16 22:51:46 +0100819 set_temp8, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
821 set_temp2_crit_hyst);
822
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100823static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type);
824
Jean Delvare2d457712006-09-24 20:52:15 +0200825/* Individual alarm files */
826static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
827static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400828static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200829static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
830static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
831static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
832/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
834
Guenter Roeck04738b22012-01-16 22:51:46 +0100835static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
836 set_update_interval);
837
Jean Delvare0e39e012006-09-24 21:16:40 +0200838static struct attribute *lm63_attributes[] = {
Jean Delvared216f682012-01-16 22:51:47 +0100839 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200840 &dev_attr_pwm1_enable.attr,
Jean Delvared216f682012-01-16 22:51:47 +0100841 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
842 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
843 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
844 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
845 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
846 &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr,
847 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
848 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
849 &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr,
850 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
851 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
852 &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr,
853 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
854 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
855 &sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr,
856 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
857 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
858 &sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr,
859 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
860 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
861 &sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr,
862 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
863 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
864 &sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr,
865
Jean Delvare0e39e012006-09-24 21:16:40 +0200866 &sensor_dev_attr_temp1_input.dev_attr.attr,
867 &sensor_dev_attr_temp2_input.dev_attr.attr,
868 &sensor_dev_attr_temp2_min.dev_attr.attr,
869 &sensor_dev_attr_temp1_max.dev_attr.attr,
870 &sensor_dev_attr_temp2_max.dev_attr.attr,
Guenter Roeck786375f2012-01-16 22:51:45 +0100871 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200872 &sensor_dev_attr_temp2_crit.dev_attr.attr,
873 &dev_attr_temp2_crit_hyst.attr,
874
875 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400876 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200877 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
878 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
879 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
880 &dev_attr_alarms.attr,
Guenter Roeck04738b22012-01-16 22:51:46 +0100881 &dev_attr_update_interval.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200882 NULL
883};
884
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100885static struct attribute *lm63_attributes_extra_lut[] = {
886 &sensor_dev_attr_pwm1_auto_point9_pwm.dev_attr.attr,
887 &sensor_dev_attr_pwm1_auto_point9_temp.dev_attr.attr,
888 &sensor_dev_attr_pwm1_auto_point9_temp_hyst.dev_attr.attr,
889 &sensor_dev_attr_pwm1_auto_point10_pwm.dev_attr.attr,
890 &sensor_dev_attr_pwm1_auto_point10_temp.dev_attr.attr,
891 &sensor_dev_attr_pwm1_auto_point10_temp_hyst.dev_attr.attr,
892 &sensor_dev_attr_pwm1_auto_point11_pwm.dev_attr.attr,
893 &sensor_dev_attr_pwm1_auto_point11_temp.dev_attr.attr,
894 &sensor_dev_attr_pwm1_auto_point11_temp_hyst.dev_attr.attr,
895 &sensor_dev_attr_pwm1_auto_point12_pwm.dev_attr.attr,
896 &sensor_dev_attr_pwm1_auto_point12_temp.dev_attr.attr,
897 &sensor_dev_attr_pwm1_auto_point12_temp_hyst.dev_attr.attr,
898 NULL
899};
900
901static const struct attribute_group lm63_group_extra_lut = {
902 .attrs = lm63_attributes_extra_lut,
903};
904
Guenter Roeck94e55df2012-01-16 22:51:46 +0100905/*
906 * On LM63, temp2_crit can be set only once, which should be job
907 * of the bootloader.
908 * On LM64, temp2_crit can always be set.
909 * On LM96163, temp2_crit can be set if bit 1 of the configuration
910 * register is true.
911 */
912static umode_t lm63_attribute_mode(struct kobject *kobj,
913 struct attribute *attr, int index)
914{
915 struct device *dev = container_of(kobj, struct device, kobj);
916 struct i2c_client *client = to_i2c_client(dev);
917 struct lm63_data *data = i2c_get_clientdata(client);
918
919 if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
920 && (data->kind == lm64 ||
921 (data->kind == lm96163 && (data->config & 0x02))))
922 return attr->mode | S_IWUSR;
923
924 return attr->mode;
925}
926
Jean Delvare0e39e012006-09-24 21:16:40 +0200927static const struct attribute_group lm63_group = {
Guenter Roeck94e55df2012-01-16 22:51:46 +0100928 .is_visible = lm63_attribute_mode,
Jean Delvare0e39e012006-09-24 21:16:40 +0200929 .attrs = lm63_attributes,
930};
931
932static struct attribute *lm63_attributes_fan1[] = {
933 &sensor_dev_attr_fan1_input.dev_attr.attr,
934 &sensor_dev_attr_fan1_min.dev_attr.attr,
935
936 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
937 NULL
938};
939
940static const struct attribute_group lm63_group_fan1 = {
941 .attrs = lm63_attributes_fan1,
942};
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/*
945 * Real code
946 */
947
Jean Delvared5957be2008-07-16 19:30:13 +0200948/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvaredac27dc2012-03-23 10:02:18 +0100949static int lm63_detect(struct i2c_client *client,
Jean Delvared5957be2008-07-16 19:30:13 +0200950 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Jean Delvaredac27dc2012-03-23 10:02:18 +0100952 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100953 u8 man_id, chip_id, reg_config1, reg_config2;
954 u8 reg_alert_status, reg_alert_mask;
Jean Delvaredac27dc2012-03-23 10:02:18 +0100955 int address = client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvared5957be2008-07-16 19:30:13 +0200958 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Jean Delvaredac27dc2012-03-23 10:02:18 +0100960 man_id = i2c_smbus_read_byte_data(client, LM63_REG_MAN_ID);
961 chip_id = i2c_smbus_read_byte_data(client, LM63_REG_CHIP_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jean Delvaredac27dc2012-03-23 10:02:18 +0100963 reg_config1 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
964 reg_config2 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG2);
965 reg_alert_status = i2c_smbus_read_byte_data(client,
Jean Delvare52df6442009-12-09 20:35:57 +0100966 LM63_REG_ALERT_STATUS);
Jean Delvaredac27dc2012-03-23 10:02:18 +0100967 reg_alert_mask = i2c_smbus_read_byte_data(client, LM63_REG_ALERT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Jean Delvare52df6442009-12-09 20:35:57 +0100969 if (man_id != 0x01 /* National Semiconductor */
Jean Delvare52df6442009-12-09 20:35:57 +0100970 || (reg_config1 & 0x18) != 0x00
971 || (reg_config2 & 0xF8) != 0x00
972 || (reg_alert_status & 0x20) != 0x00
973 || (reg_alert_mask & 0xA4) != 0xA4) {
974 dev_dbg(&adapter->dev,
975 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
976 man_id, chip_id);
977 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200980 if (chip_id == 0x41 && address == 0x4c)
981 strlcpy(info->type, "lm63", I2C_NAME_SIZE);
982 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
983 strlcpy(info->type, "lm64", I2C_NAME_SIZE);
Guenter Roeck210961c2012-01-16 22:51:45 +0100984 else if (chip_id == 0x49 && address == 0x4c)
985 strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200986 else
987 return -ENODEV;
Jean Delvared5957be2008-07-16 19:30:13 +0200988
989 return 0;
990}
991
Guenter Roeck662bda22012-01-16 22:51:45 +0100992/*
993 * Ideally we shouldn't have to initialize anything, since the BIOS
994 * should have taken care of everything
995 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996static void lm63_init_client(struct i2c_client *client)
997{
998 struct lm63_data *data = i2c_get_clientdata(client);
Guenter Roeck04738b22012-01-16 22:51:46 +0100999 u8 convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
1002 data->config_fan = i2c_smbus_read_byte_data(client,
1003 LM63_REG_CONFIG_FAN);
1004
1005 /* Start converting if needed */
1006 if (data->config & 0x40) { /* standby */
Joe Perches898eb712007-10-18 03:06:30 -07001007 dev_dbg(&client->dev, "Switching to operational mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 data->config &= 0xA7;
1009 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
1010 data->config);
1011 }
Jean Delvare409c0b52012-01-16 22:51:46 +01001012 /* Tachometer is always enabled on LM64 */
1013 if (data->kind == lm64)
1014 data->config |= 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 /* We may need pwm1_freq before ever updating the client data */
1017 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
1018 if (data->pwm1_freq == 0)
1019 data->pwm1_freq = 1;
1020
Guenter Roeck04738b22012-01-16 22:51:46 +01001021 switch (data->kind) {
1022 case lm63:
1023 case lm64:
1024 data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
Jean Delvare2fe28ab2012-01-16 22:51:47 +01001025 data->lut_size = 8;
Guenter Roeck04738b22012-01-16 22:51:46 +01001026 break;
1027 case lm96163:
1028 data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
Jean Delvare2fe28ab2012-01-16 22:51:47 +01001029 data->lut_size = 12;
Guenter Roeckf496b2d2012-01-16 22:51:46 +01001030 data->trutherm
1031 = i2c_smbus_read_byte_data(client,
1032 LM96163_REG_TRUTHERM) & 0x02;
Guenter Roeck04738b22012-01-16 22:51:46 +01001033 break;
1034 }
1035 convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
1036 if (unlikely(convrate > LM63_MAX_CONVRATE))
1037 convrate = LM63_MAX_CONVRATE;
1038 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
1039 convrate);
1040
Guenter Roeck210961c2012-01-16 22:51:45 +01001041 /*
Guenter Roecke872c912012-01-16 22:51:46 +01001042 * For LM96163, check if high resolution PWM
1043 * and unsigned temperature format is enabled.
Guenter Roeck210961c2012-01-16 22:51:45 +01001044 */
1045 if (data->kind == lm96163) {
1046 u8 config_enhanced
1047 = i2c_smbus_read_byte_data(client,
1048 LM96163_REG_CONFIG_ENHANCED);
Jean Delvared216f682012-01-16 22:51:47 +01001049 if (config_enhanced & 0x20)
1050 data->lut_temp_highres = true;
Guenter Roeck210961c2012-01-16 22:51:45 +01001051 if ((config_enhanced & 0x10)
1052 && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
1053 data->pwm_highres = true;
1054 if (config_enhanced & 0x08)
Guenter Roecke872c912012-01-16 22:51:46 +01001055 data->remote_unsigned = true;
Guenter Roeck210961c2012-01-16 22:51:45 +01001056 }
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /* Show some debug info about the LM63 configuration */
Jean Delvare409c0b52012-01-16 22:51:46 +01001059 if (data->kind == lm63)
1060 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
1061 (data->config & 0x04) ? "tachometer input" :
1062 "alert output");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
1064 (data->config_fan & 0x08) ? "1.4" : "360",
1065 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
1066 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
1067 (data->config_fan & 0x10) ? "low" : "high",
1068 (data->config_fan & 0x20) ? "manual" : "auto");
1069}
1070
Jean Delvaredac27dc2012-03-23 10:02:18 +01001071static int lm63_probe(struct i2c_client *client,
1072 const struct i2c_device_id *id)
1073{
1074 struct lm63_data *data;
1075 int err;
1076
1077 data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
1078 if (!data) {
1079 err = -ENOMEM;
1080 goto exit;
1081 }
1082
1083 i2c_set_clientdata(client, data);
1084 data->valid = 0;
1085 mutex_init(&data->update_lock);
1086
1087 /* Set the device type */
1088 data->kind = id->driver_data;
1089 if (data->kind == lm64)
1090 data->temp2_offset = 16000;
1091
1092 /* Initialize chip */
1093 lm63_init_client(client);
1094
1095 /* Register sysfs hooks */
1096 err = sysfs_create_group(&client->dev.kobj, &lm63_group);
1097 if (err)
1098 goto exit_free;
1099 if (data->config & 0x04) { /* tachometer enabled */
1100 err = sysfs_create_group(&client->dev.kobj, &lm63_group_fan1);
1101 if (err)
1102 goto exit_remove_files;
1103 }
1104 if (data->kind == lm96163) {
1105 err = device_create_file(&client->dev, &dev_attr_temp2_type);
1106 if (err)
1107 goto exit_remove_files;
1108
1109 err = sysfs_create_group(&client->dev.kobj,
1110 &lm63_group_extra_lut);
1111 if (err)
1112 goto exit_remove_files;
1113 }
1114
1115 data->hwmon_dev = hwmon_device_register(&client->dev);
1116 if (IS_ERR(data->hwmon_dev)) {
1117 err = PTR_ERR(data->hwmon_dev);
1118 goto exit_remove_files;
1119 }
1120
1121 return 0;
1122
1123exit_remove_files:
1124 sysfs_remove_group(&client->dev.kobj, &lm63_group);
1125 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
1126 if (data->kind == lm96163) {
1127 device_remove_file(&client->dev, &dev_attr_temp2_type);
1128 sysfs_remove_group(&client->dev.kobj, &lm63_group_extra_lut);
1129 }
1130exit_free:
1131 kfree(data);
1132exit:
1133 return err;
1134}
1135
Jean Delvared5957be2008-07-16 19:30:13 +02001136static int lm63_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001138 struct lm63_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Tony Jones1beeffe2007-08-20 13:46:20 -07001140 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001141 sysfs_remove_group(&client->dev.kobj, &lm63_group);
1142 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
Jean Delvare2fe28ab2012-01-16 22:51:47 +01001143 if (data->kind == lm96163) {
1144 device_remove_file(&client->dev, &dev_attr_temp2_type);
1145 sysfs_remove_group(&client->dev.kobj, &lm63_group_extra_lut);
1146 }
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001147
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001148 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return 0;
1150}
1151
Jean Delvaredac27dc2012-03-23 10:02:18 +01001152/*
1153 * Driver data (common to all clients)
1154 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Jean Delvaredac27dc2012-03-23 10:02:18 +01001156static const struct i2c_device_id lm63_id[] = {
1157 { "lm63", lm63 },
1158 { "lm64", lm64 },
1159 { "lm96163", lm96163 },
1160 { }
1161};
1162MODULE_DEVICE_TABLE(i2c, lm63_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jean Delvaredac27dc2012-03-23 10:02:18 +01001164static struct i2c_driver lm63_driver = {
1165 .class = I2C_CLASS_HWMON,
1166 .driver = {
1167 .name = "lm63",
1168 },
1169 .probe = lm63_probe,
1170 .remove = lm63_remove,
1171 .id_table = lm63_id,
1172 .detect = lm63_detect,
1173 .address_list = normal_i2c,
1174};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Axel Linf0967ee2012-01-20 15:38:18 +08001176module_i2c_driver(lm63_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1179MODULE_DESCRIPTION("LM63 driver");
1180MODULE_LICENSE("GPL");