blob: 848b9611151f9507c21c74d2de94220ab475d2e8 [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 Delvare7c81c602014-01-29 20:40:08 +01004 * Copyright (C) 2004-2008 Jean Delvare <jdelvare@suse.de>
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 Roeck94e55df42012-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 {
Guenter Roecke19eea82014-04-04 18:01:33 +0200158 struct i2c_client *client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100159 struct mutex update_lock;
Guenter Roeckb76552b2014-04-04 18:01:33 +0200160 const struct attribute_group *groups[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 char valid; /* zero until following fields are valid */
Jean Delvared216f682012-01-16 22:51:47 +0100162 char lut_valid; /* zero until lut fields are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 unsigned long last_updated; /* in jiffies */
Jean Delvared216f682012-01-16 22:51:47 +0100164 unsigned long lut_last_updated; /* in jiffies */
Guenter Roeck04738b22012-01-16 22:51:46 +0100165 enum chips kind;
Dirk Eibach2778fb12011-02-09 04:51:34 -0500166 int temp2_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Guenter Roeck04738b22012-01-16 22:51:46 +0100168 int update_interval; /* in milliseconds */
169 int max_convrate_hz;
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100170 int lut_size; /* 8 or 12 */
Guenter Roeck04738b22012-01-16 22:51:46 +0100171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* registers values */
173 u8 config, config_fan;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200174 u16 fan[2]; /* 0: input
175 1: low limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 u8 pwm1_freq;
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100177 u8 pwm1[13]; /* 0: current output
178 1-12: lookup table */
179 s8 temp8[15]; /* 0: local input
Jean Delvarebc51ae12005-06-05 20:32:27 +0200180 1: local high limit
Jean Delvared216f682012-01-16 22:51:47 +0100181 2: remote critical limit
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100182 3-14: lookup table */
Guenter Roeck786375f2012-01-16 22:51:45 +0100183 s16 temp11[4]; /* 0: remote input
Jean Delvarebc51ae12005-06-05 20:32:27 +0200184 1: remote low limit
Guenter Roeck786375f2012-01-16 22:51:45 +0100185 2: remote high limit
186 3: remote offset */
Guenter Roecke872c912012-01-16 22:51:46 +0100187 u16 temp11u; /* remote input (unsigned) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 u8 temp2_crit_hyst;
Jean Delvared216f682012-01-16 22:51:47 +0100189 u8 lut_temp_hyst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 u8 alarms;
Guenter Roeck210961c2012-01-16 22:51:45 +0100191 bool pwm_highres;
Jean Delvared216f682012-01-16 22:51:47 +0100192 bool lut_temp_highres;
Guenter Roecke872c912012-01-16 22:51:46 +0100193 bool remote_unsigned; /* true if unsigned remote upper limits */
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100194 bool trutherm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
196
Guenter Roecke872c912012-01-16 22:51:46 +0100197static inline int temp8_from_reg(struct lm63_data *data, int nr)
198{
199 if (data->remote_unsigned)
200 return TEMP8_FROM_REG((u8)data->temp8[nr]);
201 return TEMP8_FROM_REG(data->temp8[nr]);
202}
203
Jean Delvared216f682012-01-16 22:51:47 +0100204static inline int lut_temp_from_reg(struct lm63_data *data, int nr)
205{
206 return data->temp8[nr] * (data->lut_temp_highres ? 500 : 1000);
207}
208
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100209static inline int lut_temp_to_reg(struct lm63_data *data, long val)
210{
211 val -= data->temp2_offset;
212 if (data->lut_temp_highres)
Guenter Roeck2a844c12013-01-09 08:09:34 -0800213 return DIV_ROUND_CLOSEST(clamp_val(val, 0, 127500), 500);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100214 else
Guenter Roeck2a844c12013-01-09 08:09:34 -0800215 return DIV_ROUND_CLOSEST(clamp_val(val, 0, 127000), 1000);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100216}
217
Jean Delvare817c6cc2012-03-23 10:02:19 +0100218/*
219 * Update the lookup table register cache.
220 * client->update_lock must be held when calling this function.
221 */
Guenter Roecke19eea82014-04-04 18:01:33 +0200222static void lm63_update_lut(struct lm63_data *data)
Jean Delvare817c6cc2012-03-23 10:02:19 +0100223{
Guenter Roecke19eea82014-04-04 18:01:33 +0200224 struct i2c_client *client = data->client;
Jean Delvare817c6cc2012-03-23 10:02:19 +0100225 int i;
226
227 if (time_after(jiffies, data->lut_last_updated + 5 * HZ) ||
228 !data->lut_valid) {
229 for (i = 0; i < data->lut_size; i++) {
230 data->pwm1[1 + i] = i2c_smbus_read_byte_data(client,
231 LM63_REG_LUT_PWM(i));
232 data->temp8[3 + i] = i2c_smbus_read_byte_data(client,
233 LM63_REG_LUT_TEMP(i));
234 }
235 data->lut_temp_hyst = i2c_smbus_read_byte_data(client,
236 LM63_REG_LUT_TEMP_HYST);
237
238 data->lut_last_updated = jiffies;
239 data->lut_valid = 1;
240 }
241}
242
Jean Delvaredac27dc2012-03-23 10:02:18 +0100243static struct lm63_data *lm63_update_device(struct device *dev)
244{
Guenter Roecke19eea82014-04-04 18:01:33 +0200245 struct lm63_data *data = dev_get_drvdata(dev);
246 struct i2c_client *client = data->client;
Jean Delvaredac27dc2012-03-23 10:02:18 +0100247 unsigned long next_update;
Jean Delvaredac27dc2012-03-23 10:02:18 +0100248
249 mutex_lock(&data->update_lock);
250
Jean Delvare5b0620d2013-07-08 14:18:24 +0200251 next_update = data->last_updated +
252 msecs_to_jiffies(data->update_interval);
Jean Delvaredac27dc2012-03-23 10:02:18 +0100253 if (time_after(jiffies, next_update) || !data->valid) {
254 if (data->config & 0x04) { /* tachometer enabled */
255 /* order matters for fan1_input */
256 data->fan[0] = i2c_smbus_read_byte_data(client,
257 LM63_REG_TACH_COUNT_LSB) & 0xFC;
258 data->fan[0] |= i2c_smbus_read_byte_data(client,
259 LM63_REG_TACH_COUNT_MSB) << 8;
260 data->fan[1] = (i2c_smbus_read_byte_data(client,
261 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
262 | (i2c_smbus_read_byte_data(client,
263 LM63_REG_TACH_LIMIT_MSB) << 8);
264 }
265
266 data->pwm1_freq = i2c_smbus_read_byte_data(client,
267 LM63_REG_PWM_FREQ);
268 if (data->pwm1_freq == 0)
269 data->pwm1_freq = 1;
270 data->pwm1[0] = i2c_smbus_read_byte_data(client,
271 LM63_REG_PWM_VALUE);
272
273 data->temp8[0] = i2c_smbus_read_byte_data(client,
274 LM63_REG_LOCAL_TEMP);
275 data->temp8[1] = i2c_smbus_read_byte_data(client,
276 LM63_REG_LOCAL_HIGH);
277
278 /* order matters for temp2_input */
279 data->temp11[0] = i2c_smbus_read_byte_data(client,
280 LM63_REG_REMOTE_TEMP_MSB) << 8;
281 data->temp11[0] |= i2c_smbus_read_byte_data(client,
282 LM63_REG_REMOTE_TEMP_LSB);
283 data->temp11[1] = (i2c_smbus_read_byte_data(client,
284 LM63_REG_REMOTE_LOW_MSB) << 8)
285 | i2c_smbus_read_byte_data(client,
286 LM63_REG_REMOTE_LOW_LSB);
287 data->temp11[2] = (i2c_smbus_read_byte_data(client,
288 LM63_REG_REMOTE_HIGH_MSB) << 8)
289 | i2c_smbus_read_byte_data(client,
290 LM63_REG_REMOTE_HIGH_LSB);
291 data->temp11[3] = (i2c_smbus_read_byte_data(client,
292 LM63_REG_REMOTE_OFFSET_MSB) << 8)
293 | i2c_smbus_read_byte_data(client,
294 LM63_REG_REMOTE_OFFSET_LSB);
295
296 if (data->kind == lm96163)
297 data->temp11u = (i2c_smbus_read_byte_data(client,
298 LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
299 | i2c_smbus_read_byte_data(client,
300 LM96163_REG_REMOTE_TEMP_U_LSB);
301
302 data->temp8[2] = i2c_smbus_read_byte_data(client,
303 LM63_REG_REMOTE_TCRIT);
304 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
305 LM63_REG_REMOTE_TCRIT_HYST);
306
307 data->alarms = i2c_smbus_read_byte_data(client,
308 LM63_REG_ALERT_STATUS) & 0x7F;
309
310 data->last_updated = jiffies;
311 data->valid = 1;
312 }
313
Guenter Roecke19eea82014-04-04 18:01:33 +0200314 lm63_update_lut(data);
Jean Delvaredac27dc2012-03-23 10:02:18 +0100315
316 mutex_unlock(&data->update_lock);
317
318 return data;
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/*
Jean Delvare817c6cc2012-03-23 10:02:19 +0100322 * Trip points in the lookup table should be in ascending order for both
323 * temperatures and PWM output values.
324 */
Guenter Roecke19eea82014-04-04 18:01:33 +0200325static int lm63_lut_looks_bad(struct device *dev, struct lm63_data *data)
Jean Delvare817c6cc2012-03-23 10:02:19 +0100326{
Jean Delvare817c6cc2012-03-23 10:02:19 +0100327 int i;
328
329 mutex_lock(&data->update_lock);
Guenter Roecke19eea82014-04-04 18:01:33 +0200330 lm63_update_lut(data);
Jean Delvare817c6cc2012-03-23 10:02:19 +0100331
332 for (i = 1; i < data->lut_size; i++) {
333 if (data->pwm1[1 + i - 1] > data->pwm1[1 + i]
334 || data->temp8[3 + i - 1] > data->temp8[3 + i]) {
Guenter Roecke19eea82014-04-04 18:01:33 +0200335 dev_warn(dev,
Jean Delvare817c6cc2012-03-23 10:02:19 +0100336 "Lookup table doesn't look sane (check entries %d and %d)\n",
337 i, i + 1);
338 break;
339 }
340 }
341 mutex_unlock(&data->update_lock);
342
343 return i == data->lut_size ? 0 : 1;
344}
345
346/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * Sysfs callback functions and files
348 */
349
Jean Delvarebc51ae12005-06-05 20:32:27 +0200350static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
351 char *buf)
352{
353 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
354 struct lm63_data *data = lm63_update_device(dev);
355 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Jean Delvarebc51ae12005-06-05 20:32:27 +0200358static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
359 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Guenter Roecke19eea82014-04-04 18:01:33 +0200361 struct lm63_data *data = dev_get_drvdata(dev);
362 struct i2c_client *client = data->client;
Guenter Roeck662bda22012-01-16 22:51:45 +0100363 unsigned long val;
364 int err;
365
366 err = kstrtoul(buf, 10, &val);
367 if (err)
368 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100370 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200371 data->fan[1] = FAN_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200373 data->fan[1] & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200375 data->fan[1] >> 8);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100376 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return count;
378}
379
Jean Delvared216f682012-01-16 22:51:47 +0100380static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200381 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Jean Delvared216f682012-01-16 22:51:47 +0100383 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct lm63_data *data = lm63_update_device(dev);
Jean Delvared216f682012-01-16 22:51:47 +0100385 int nr = attr->index;
Guenter Roeck210961c2012-01-16 22:51:45 +0100386 int pwm;
387
388 if (data->pwm_highres)
Jean Delvared216f682012-01-16 22:51:47 +0100389 pwm = data->pwm1[nr];
Guenter Roeck210961c2012-01-16 22:51:45 +0100390 else
Jean Delvared216f682012-01-16 22:51:47 +0100391 pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ?
392 255 : (data->pwm1[nr] * 255 + data->pwm1_freq) /
Guenter Roeck210961c2012-01-16 22:51:45 +0100393 (2 * data->pwm1_freq);
394
395 return sprintf(buf, "%d\n", pwm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100398static ssize_t set_pwm1(struct device *dev, struct device_attribute *devattr,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200399 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100401 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecke19eea82014-04-04 18:01:33 +0200402 struct lm63_data *data = dev_get_drvdata(dev);
403 struct i2c_client *client = data->client;
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100404 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 unsigned long val;
Guenter Roeck662bda22012-01-16 22:51:45 +0100406 int err;
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100407 u8 reg;
Guenter Roeck662bda22012-01-16 22:51:45 +0100408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (!(data->config_fan & 0x20)) /* register is read-only */
410 return -EPERM;
411
Guenter Roeck662bda22012-01-16 22:51:45 +0100412 err = kstrtoul(buf, 10, &val);
413 if (err)
414 return err;
415
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100416 reg = nr ? LM63_REG_LUT_PWM(nr - 1) : LM63_REG_PWM_VALUE;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800417 val = clamp_val(val, 0, 255);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100418
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100419 mutex_lock(&data->update_lock);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100420 data->pwm1[nr] = data->pwm_highres ? val :
Jean Delvared216f682012-01-16 22:51:47 +0100421 (val * data->pwm1_freq * 2 + 127) / 255;
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100422 i2c_smbus_write_byte_data(client, reg, data->pwm1[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100423 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return count;
425}
426
Guenter Roeck662bda22012-01-16 22:51:45 +0100427static ssize_t show_pwm1_enable(struct device *dev,
428 struct device_attribute *dummy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct lm63_data *data = lm63_update_device(dev);
431 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
432}
433
Jean Delvare817c6cc2012-03-23 10:02:19 +0100434static ssize_t set_pwm1_enable(struct device *dev,
435 struct device_attribute *dummy,
436 const char *buf, size_t count)
437{
Guenter Roecke19eea82014-04-04 18:01:33 +0200438 struct lm63_data *data = dev_get_drvdata(dev);
439 struct i2c_client *client = data->client;
Jean Delvare817c6cc2012-03-23 10:02:19 +0100440 unsigned long val;
441 int err;
442
443 err = kstrtoul(buf, 10, &val);
444 if (err)
445 return err;
446 if (val < 1 || val > 2)
447 return -EINVAL;
448
449 /*
450 * Only let the user switch to automatic mode if the lookup table
451 * looks sane.
452 */
Guenter Roecke19eea82014-04-04 18:01:33 +0200453 if (val == 2 && lm63_lut_looks_bad(dev, data))
Jean Delvare817c6cc2012-03-23 10:02:19 +0100454 return -EPERM;
455
456 mutex_lock(&data->update_lock);
457 data->config_fan = i2c_smbus_read_byte_data(client,
458 LM63_REG_CONFIG_FAN);
459 if (val == 1)
460 data->config_fan |= 0x20;
461 else
462 data->config_fan &= ~0x20;
463 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN,
Guenter Roecke19eea82014-04-04 18:01:33 +0200464 data->config_fan);
Jean Delvare817c6cc2012-03-23 10:02:19 +0100465 mutex_unlock(&data->update_lock);
466 return count;
467}
468
Dirk Eibach2778fb12011-02-09 04:51:34 -0500469/*
470 * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
471 * For remote sensor registers temp2_offset has to be considered,
472 * for local sensor it must not.
473 * So we need separate 8bit accessors for local and remote sensor.
474 */
475static ssize_t show_local_temp8(struct device *dev,
476 struct device_attribute *devattr,
477 char *buf)
Jean Delvarebc51ae12005-06-05 20:32:27 +0200478{
479 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
480 struct lm63_data *data = lm63_update_device(dev);
481 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Dirk Eibach2778fb12011-02-09 04:51:34 -0500484static ssize_t show_remote_temp8(struct device *dev,
485 struct device_attribute *devattr,
486 char *buf)
487{
488 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
489 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100490 return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
Dirk Eibach2778fb12011-02-09 04:51:34 -0500491 + data->temp2_offset);
492}
493
Jean Delvared216f682012-01-16 22:51:47 +0100494static ssize_t show_lut_temp(struct device *dev,
495 struct device_attribute *devattr,
496 char *buf)
497{
498 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
499 struct lm63_data *data = lm63_update_device(dev);
500 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
501 + data->temp2_offset);
502}
503
Guenter Roeck94e55df42012-01-16 22:51:46 +0100504static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
505 const char *buf, size_t count)
Jean Delvarebc51ae12005-06-05 20:32:27 +0200506{
Guenter Roeck94e55df42012-01-16 22:51:46 +0100507 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecke19eea82014-04-04 18:01:33 +0200508 struct lm63_data *data = dev_get_drvdata(dev);
509 struct i2c_client *client = data->client;
Guenter Roeck94e55df42012-01-16 22:51:46 +0100510 int nr = attr->index;
Guenter Roeck662bda22012-01-16 22:51:45 +0100511 long val;
512 int err;
Guenter Roeck94e55df42012-01-16 22:51:46 +0100513 int temp;
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100514 u8 reg;
Guenter Roeck662bda22012-01-16 22:51:45 +0100515
516 err = kstrtol(buf, 10, &val);
517 if (err)
518 return err;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200519
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100520 mutex_lock(&data->update_lock);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100521 switch (nr) {
522 case 2:
523 reg = LM63_REG_REMOTE_TCRIT;
Guenter Roeck94e55df42012-01-16 22:51:46 +0100524 if (data->remote_unsigned)
525 temp = TEMP8U_TO_REG(val - data->temp2_offset);
526 else
527 temp = TEMP8_TO_REG(val - data->temp2_offset);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100528 break;
529 case 1:
530 reg = LM63_REG_LOCAL_HIGH;
Guenter Roeck94e55df42012-01-16 22:51:46 +0100531 temp = TEMP8_TO_REG(val);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100532 break;
533 default: /* lookup table */
534 reg = LM63_REG_LUT_TEMP(nr - 3);
535 temp = lut_temp_to_reg(data, val);
Guenter Roeck94e55df42012-01-16 22:51:46 +0100536 }
537 data->temp8[nr] = temp;
538 i2c_smbus_write_byte_data(client, reg, temp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100539 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200540 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200542
543static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
544 char *buf)
545{
546 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
547 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100548 int nr = attr->index;
549 int temp;
550
551 if (!nr) {
552 /*
553 * Use unsigned temperature unless its value is zero.
554 * If it is zero, use signed temperature.
555 */
556 if (data->temp11u)
557 temp = TEMP11_FROM_REG(data->temp11u);
558 else
559 temp = TEMP11_FROM_REG(data->temp11[nr]);
560 } else {
561 if (data->remote_unsigned && nr == 2)
562 temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
563 else
564 temp = TEMP11_FROM_REG(data->temp11[nr]);
565 }
566 return sprintf(buf, "%d\n", temp + data->temp2_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200568
569static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
570 const char *buf, size_t count)
571{
Guenter Roeck786375f2012-01-16 22:51:45 +0100572 static const u8 reg[6] = {
Jean Delvarebc51ae12005-06-05 20:32:27 +0200573 LM63_REG_REMOTE_LOW_MSB,
574 LM63_REG_REMOTE_LOW_LSB,
575 LM63_REG_REMOTE_HIGH_MSB,
576 LM63_REG_REMOTE_HIGH_LSB,
Guenter Roeck786375f2012-01-16 22:51:45 +0100577 LM63_REG_REMOTE_OFFSET_MSB,
578 LM63_REG_REMOTE_OFFSET_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200579 };
580
581 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecke19eea82014-04-04 18:01:33 +0200582 struct lm63_data *data = dev_get_drvdata(dev);
583 struct i2c_client *client = data->client;
Guenter Roeck662bda22012-01-16 22:51:45 +0100584 long val;
585 int err;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200586 int nr = attr->index;
587
Guenter Roeck662bda22012-01-16 22:51:45 +0100588 err = kstrtol(buf, 10, &val);
589 if (err)
590 return err;
591
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100592 mutex_lock(&data->update_lock);
Guenter Roecke872c912012-01-16 22:51:46 +0100593 if (data->remote_unsigned && nr == 2)
594 data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
595 else
596 data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
597
Jean Delvarebc51ae12005-06-05 20:32:27 +0200598 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
599 data->temp11[nr] >> 8);
600 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
601 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100602 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200603 return count;
604}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Guenter Roeck662bda22012-01-16 22:51:45 +0100606/*
607 * Hysteresis register holds a relative value, while we want to present
608 * an absolute to user-space
609 */
610static ssize_t show_temp2_crit_hyst(struct device *dev,
611 struct device_attribute *dummy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100614 return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
Dirk Eibach2778fb12011-02-09 04:51:34 -0500615 + data->temp2_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 - TEMP8_FROM_REG(data->temp2_crit_hyst));
617}
618
Jean Delvared216f682012-01-16 22:51:47 +0100619static ssize_t show_lut_temp_hyst(struct device *dev,
620 struct device_attribute *devattr, char *buf)
621{
622 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
623 struct lm63_data *data = lm63_update_device(dev);
624
625 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
626 + data->temp2_offset
627 - TEMP8_FROM_REG(data->lut_temp_hyst));
628}
629
Guenter Roeck662bda22012-01-16 22:51:45 +0100630/*
631 * And now the other way around, user-space provides an absolute
632 * hysteresis value and we have to store a relative one
633 */
634static ssize_t set_temp2_crit_hyst(struct device *dev,
635 struct device_attribute *dummy,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200636 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Guenter Roecke19eea82014-04-04 18:01:33 +0200638 struct lm63_data *data = dev_get_drvdata(dev);
639 struct i2c_client *client = data->client;
Guenter Roeck662bda22012-01-16 22:51:45 +0100640 long val;
641 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 long hyst;
643
Guenter Roeck662bda22012-01-16 22:51:45 +0100644 err = kstrtol(buf, 10, &val);
645 if (err)
646 return err;
647
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100648 mutex_lock(&data->update_lock);
Guenter Roecke872c912012-01-16 22:51:46 +0100649 hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
651 HYST_TO_REG(hyst));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100652 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return count;
654}
655
Guenter Roeck04738b22012-01-16 22:51:46 +0100656/*
657 * Set conversion rate.
658 * client->update_lock must be held when calling this function.
659 */
Guenter Roecke19eea82014-04-04 18:01:33 +0200660static void lm63_set_convrate(struct lm63_data *data, unsigned int interval)
Guenter Roeck04738b22012-01-16 22:51:46 +0100661{
Guenter Roecke19eea82014-04-04 18:01:33 +0200662 struct i2c_client *client = data->client;
Guenter Roeck04738b22012-01-16 22:51:46 +0100663 unsigned int update_interval;
Guenter Roecke19eea82014-04-04 18:01:33 +0200664 int i;
Guenter Roeck04738b22012-01-16 22:51:46 +0100665
666 /* Shift calculations to avoid rounding errors */
667 interval <<= 6;
668
669 /* find the nearest update rate */
670 update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
671 / data->max_convrate_hz;
672 for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
673 if (interval >= update_interval * 3 / 4)
674 break;
675
676 i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
677 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
678}
679
680static ssize_t show_update_interval(struct device *dev,
681 struct device_attribute *attr, char *buf)
682{
683 struct lm63_data *data = dev_get_drvdata(dev);
684
685 return sprintf(buf, "%u\n", data->update_interval);
686}
687
688static ssize_t set_update_interval(struct device *dev,
689 struct device_attribute *attr,
690 const char *buf, size_t count)
691{
Guenter Roecke19eea82014-04-04 18:01:33 +0200692 struct lm63_data *data = dev_get_drvdata(dev);
Guenter Roeck04738b22012-01-16 22:51:46 +0100693 unsigned long val;
694 int err;
695
696 err = kstrtoul(buf, 10, &val);
697 if (err)
698 return err;
699
700 mutex_lock(&data->update_lock);
Guenter Roecke19eea82014-04-04 18:01:33 +0200701 lm63_set_convrate(data, clamp_val(val, 0, 100000));
Guenter Roeck04738b22012-01-16 22:51:46 +0100702 mutex_unlock(&data->update_lock);
703
704 return count;
705}
706
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100707static ssize_t show_type(struct device *dev, struct device_attribute *attr,
708 char *buf)
709{
Guenter Roecke19eea82014-04-04 18:01:33 +0200710 struct lm63_data *data = dev_get_drvdata(dev);
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100711
712 return sprintf(buf, data->trutherm ? "1\n" : "2\n");
713}
714
715static ssize_t set_type(struct device *dev, struct device_attribute *attr,
716 const char *buf, size_t count)
717{
Guenter Roecke19eea82014-04-04 18:01:33 +0200718 struct lm63_data *data = dev_get_drvdata(dev);
719 struct i2c_client *client = data->client;
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100720 unsigned long val;
721 int ret;
722 u8 reg;
723
724 ret = kstrtoul(buf, 10, &val);
725 if (ret < 0)
726 return ret;
727 if (val != 1 && val != 2)
728 return -EINVAL;
729
730 mutex_lock(&data->update_lock);
731 data->trutherm = val == 1;
732 reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02;
733 i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM,
734 reg | (data->trutherm ? 0x02 : 0x00));
735 data->valid = 0;
736 mutex_unlock(&data->update_lock);
737
738 return count;
739}
740
Jean Delvarebc51ae12005-06-05 20:32:27 +0200741static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
742 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 struct lm63_data *data = lm63_update_device(dev);
745 return sprintf(buf, "%u\n", data->alarms);
746}
747
Jean Delvare2d457712006-09-24 20:52:15 +0200748static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
749 char *buf)
750{
751 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
752 struct lm63_data *data = lm63_update_device(dev);
753 int bitnr = attr->index;
754
755 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
756}
757
Jean Delvarebc51ae12005-06-05 20:32:27 +0200758static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
759static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
760 set_fan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Jean Delvared216f682012-01-16 22:51:47 +0100762static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1, 0);
Jean Delvare817c6cc2012-03-23 10:02:19 +0100763static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
764 show_pwm1_enable, set_pwm1_enable);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100765static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
766 show_pwm1, set_pwm1, 1);
767static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
768 show_lut_temp, set_temp8, 3);
Jean Delvared216f682012-01-16 22:51:47 +0100769static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp_hyst, S_IRUGO,
770 show_lut_temp_hyst, NULL, 3);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100771static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
772 show_pwm1, set_pwm1, 2);
773static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
774 show_lut_temp, set_temp8, 4);
Jean Delvared216f682012-01-16 22:51:47 +0100775static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp_hyst, S_IRUGO,
776 show_lut_temp_hyst, NULL, 4);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100777static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
778 show_pwm1, set_pwm1, 3);
779static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
780 show_lut_temp, set_temp8, 5);
Jean Delvared216f682012-01-16 22:51:47 +0100781static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp_hyst, S_IRUGO,
782 show_lut_temp_hyst, NULL, 5);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100783static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
784 show_pwm1, set_pwm1, 4);
785static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
786 show_lut_temp, set_temp8, 6);
Jean Delvared216f682012-01-16 22:51:47 +0100787static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp_hyst, S_IRUGO,
788 show_lut_temp_hyst, NULL, 6);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100789static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
790 show_pwm1, set_pwm1, 5);
791static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
792 show_lut_temp, set_temp8, 7);
Jean Delvared216f682012-01-16 22:51:47 +0100793static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp_hyst, S_IRUGO,
794 show_lut_temp_hyst, NULL, 7);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100795static SENSOR_DEVICE_ATTR(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
796 show_pwm1, set_pwm1, 6);
797static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
798 show_lut_temp, set_temp8, 8);
Jean Delvared216f682012-01-16 22:51:47 +0100799static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp_hyst, S_IRUGO,
800 show_lut_temp_hyst, NULL, 8);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100801static SENSOR_DEVICE_ATTR(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
802 show_pwm1, set_pwm1, 7);
803static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
804 show_lut_temp, set_temp8, 9);
Jean Delvared216f682012-01-16 22:51:47 +0100805static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp_hyst, S_IRUGO,
806 show_lut_temp_hyst, NULL, 9);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100807static SENSOR_DEVICE_ATTR(pwm1_auto_point8_pwm, S_IWUSR | S_IRUGO,
808 show_pwm1, set_pwm1, 8);
809static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp, S_IWUSR | S_IRUGO,
810 show_lut_temp, set_temp8, 10);
Jean Delvared216f682012-01-16 22:51:47 +0100811static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp_hyst, S_IRUGO,
812 show_lut_temp_hyst, NULL, 10);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100813static SENSOR_DEVICE_ATTR(pwm1_auto_point9_pwm, S_IWUSR | S_IRUGO,
814 show_pwm1, set_pwm1, 9);
815static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp, S_IWUSR | S_IRUGO,
816 show_lut_temp, set_temp8, 11);
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100817static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp_hyst, S_IRUGO,
818 show_lut_temp_hyst, NULL, 11);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100819static SENSOR_DEVICE_ATTR(pwm1_auto_point10_pwm, S_IWUSR | S_IRUGO,
820 show_pwm1, set_pwm1, 10);
821static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp, S_IWUSR | S_IRUGO,
822 show_lut_temp, set_temp8, 12);
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100823static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp_hyst, S_IRUGO,
824 show_lut_temp_hyst, NULL, 12);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100825static SENSOR_DEVICE_ATTR(pwm1_auto_point11_pwm, S_IWUSR | S_IRUGO,
826 show_pwm1, set_pwm1, 11);
827static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp, S_IWUSR | S_IRUGO,
828 show_lut_temp, set_temp8, 13);
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100829static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp_hyst, S_IRUGO,
830 show_lut_temp_hyst, NULL, 13);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100831static SENSOR_DEVICE_ATTR(pwm1_auto_point12_pwm, S_IWUSR | S_IRUGO,
832 show_pwm1, set_pwm1, 12);
833static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp, S_IWUSR | S_IRUGO,
834 show_lut_temp, set_temp8, 14);
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100835static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp_hyst, S_IRUGO,
836 show_lut_temp_hyst, NULL, 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Dirk Eibach2778fb12011-02-09 04:51:34 -0500838static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
839static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
Guenter Roeck94e55df42012-01-16 22:51:46 +0100840 set_temp8, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Jean Delvarebc51ae12005-06-05 20:32:27 +0200842static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
843static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
844 set_temp11, 1);
845static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
846 set_temp11, 2);
Guenter Roeck786375f2012-01-16 22:51:45 +0100847static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
848 set_temp11, 3);
Dirk Eibach2778fb12011-02-09 04:51:34 -0500849static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
Guenter Roeck94e55df42012-01-16 22:51:46 +0100850 set_temp8, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
852 set_temp2_crit_hyst);
853
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100854static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type);
855
Jean Delvare2d457712006-09-24 20:52:15 +0200856/* Individual alarm files */
857static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
858static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400859static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200860static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
861static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
862static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
863/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
865
Guenter Roeck04738b22012-01-16 22:51:46 +0100866static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
867 set_update_interval);
868
Jean Delvare0e39e012006-09-24 21:16:40 +0200869static struct attribute *lm63_attributes[] = {
Jean Delvared216f682012-01-16 22:51:47 +0100870 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200871 &dev_attr_pwm1_enable.attr,
Jean Delvared216f682012-01-16 22:51:47 +0100872 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
873 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
874 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
875 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
876 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
877 &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr,
878 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
879 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
880 &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr,
881 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
882 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
883 &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr,
884 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
885 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
886 &sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr,
887 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
888 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
889 &sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr,
890 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
891 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
892 &sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr,
893 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
894 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
895 &sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr,
896
Jean Delvare0e39e012006-09-24 21:16:40 +0200897 &sensor_dev_attr_temp1_input.dev_attr.attr,
898 &sensor_dev_attr_temp2_input.dev_attr.attr,
899 &sensor_dev_attr_temp2_min.dev_attr.attr,
900 &sensor_dev_attr_temp1_max.dev_attr.attr,
901 &sensor_dev_attr_temp2_max.dev_attr.attr,
Guenter Roeck786375f2012-01-16 22:51:45 +0100902 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200903 &sensor_dev_attr_temp2_crit.dev_attr.attr,
904 &dev_attr_temp2_crit_hyst.attr,
905
906 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400907 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200908 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
909 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
910 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
911 &dev_attr_alarms.attr,
Guenter Roeck04738b22012-01-16 22:51:46 +0100912 &dev_attr_update_interval.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200913 NULL
914};
915
Guenter Roeck0dcb28a2014-04-04 18:01:32 +0200916static struct attribute *lm63_attributes_temp2_type[] = {
917 &dev_attr_temp2_type.attr,
918 NULL
919};
920
921static const struct attribute_group lm63_group_temp2_type = {
922 .attrs = lm63_attributes_temp2_type,
923};
924
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100925static struct attribute *lm63_attributes_extra_lut[] = {
926 &sensor_dev_attr_pwm1_auto_point9_pwm.dev_attr.attr,
927 &sensor_dev_attr_pwm1_auto_point9_temp.dev_attr.attr,
928 &sensor_dev_attr_pwm1_auto_point9_temp_hyst.dev_attr.attr,
929 &sensor_dev_attr_pwm1_auto_point10_pwm.dev_attr.attr,
930 &sensor_dev_attr_pwm1_auto_point10_temp.dev_attr.attr,
931 &sensor_dev_attr_pwm1_auto_point10_temp_hyst.dev_attr.attr,
932 &sensor_dev_attr_pwm1_auto_point11_pwm.dev_attr.attr,
933 &sensor_dev_attr_pwm1_auto_point11_temp.dev_attr.attr,
934 &sensor_dev_attr_pwm1_auto_point11_temp_hyst.dev_attr.attr,
935 &sensor_dev_attr_pwm1_auto_point12_pwm.dev_attr.attr,
936 &sensor_dev_attr_pwm1_auto_point12_temp.dev_attr.attr,
937 &sensor_dev_attr_pwm1_auto_point12_temp_hyst.dev_attr.attr,
938 NULL
939};
940
941static const struct attribute_group lm63_group_extra_lut = {
942 .attrs = lm63_attributes_extra_lut,
943};
944
Guenter Roeck94e55df42012-01-16 22:51:46 +0100945/*
946 * On LM63, temp2_crit can be set only once, which should be job
947 * of the bootloader.
948 * On LM64, temp2_crit can always be set.
949 * On LM96163, temp2_crit can be set if bit 1 of the configuration
950 * register is true.
951 */
952static umode_t lm63_attribute_mode(struct kobject *kobj,
953 struct attribute *attr, int index)
954{
955 struct device *dev = container_of(kobj, struct device, kobj);
Guenter Roecke19eea82014-04-04 18:01:33 +0200956 struct lm63_data *data = dev_get_drvdata(dev);
Guenter Roeck94e55df42012-01-16 22:51:46 +0100957
958 if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
959 && (data->kind == lm64 ||
960 (data->kind == lm96163 && (data->config & 0x02))))
961 return attr->mode | S_IWUSR;
962
963 return attr->mode;
964}
965
Jean Delvare0e39e012006-09-24 21:16:40 +0200966static const struct attribute_group lm63_group = {
Guenter Roeck94e55df42012-01-16 22:51:46 +0100967 .is_visible = lm63_attribute_mode,
Jean Delvare0e39e012006-09-24 21:16:40 +0200968 .attrs = lm63_attributes,
969};
970
971static struct attribute *lm63_attributes_fan1[] = {
972 &sensor_dev_attr_fan1_input.dev_attr.attr,
973 &sensor_dev_attr_fan1_min.dev_attr.attr,
974
975 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
976 NULL
977};
978
979static const struct attribute_group lm63_group_fan1 = {
980 .attrs = lm63_attributes_fan1,
981};
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983/*
984 * Real code
985 */
986
Jean Delvared5957be2008-07-16 19:30:13 +0200987/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvaredac27dc2012-03-23 10:02:18 +0100988static int lm63_detect(struct i2c_client *client,
Jean Delvared5957be2008-07-16 19:30:13 +0200989 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Jean Delvaredac27dc2012-03-23 10:02:18 +0100991 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100992 u8 man_id, chip_id, reg_config1, reg_config2;
993 u8 reg_alert_status, reg_alert_mask;
Jean Delvaredac27dc2012-03-23 10:02:18 +0100994 int address = client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvared5957be2008-07-16 19:30:13 +0200997 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Jean Delvaredac27dc2012-03-23 10:02:18 +0100999 man_id = i2c_smbus_read_byte_data(client, LM63_REG_MAN_ID);
1000 chip_id = i2c_smbus_read_byte_data(client, LM63_REG_CHIP_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Jean Delvaredac27dc2012-03-23 10:02:18 +01001002 reg_config1 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
1003 reg_config2 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG2);
1004 reg_alert_status = i2c_smbus_read_byte_data(client,
Jean Delvare52df6442009-12-09 20:35:57 +01001005 LM63_REG_ALERT_STATUS);
Jean Delvaredac27dc2012-03-23 10:02:18 +01001006 reg_alert_mask = i2c_smbus_read_byte_data(client, LM63_REG_ALERT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Jean Delvare52df6442009-12-09 20:35:57 +01001008 if (man_id != 0x01 /* National Semiconductor */
Jean Delvare52df6442009-12-09 20:35:57 +01001009 || (reg_config1 & 0x18) != 0x00
1010 || (reg_config2 & 0xF8) != 0x00
1011 || (reg_alert_status & 0x20) != 0x00
1012 || (reg_alert_mask & 0xA4) != 0xA4) {
1013 dev_dbg(&adapter->dev,
1014 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
1015 man_id, chip_id);
1016 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018
Matthew Garrett10f2ed32010-05-27 19:58:38 +02001019 if (chip_id == 0x41 && address == 0x4c)
1020 strlcpy(info->type, "lm63", I2C_NAME_SIZE);
1021 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
1022 strlcpy(info->type, "lm64", I2C_NAME_SIZE);
Guenter Roeck210961c2012-01-16 22:51:45 +01001023 else if (chip_id == 0x49 && address == 0x4c)
1024 strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
Matthew Garrett10f2ed32010-05-27 19:58:38 +02001025 else
1026 return -ENODEV;
Jean Delvared5957be2008-07-16 19:30:13 +02001027
1028 return 0;
1029}
1030
Guenter Roeck662bda22012-01-16 22:51:45 +01001031/*
1032 * Ideally we shouldn't have to initialize anything, since the BIOS
1033 * should have taken care of everything
1034 */
Guenter Roecke19eea82014-04-04 18:01:33 +02001035static void lm63_init_client(struct lm63_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Guenter Roecke19eea82014-04-04 18:01:33 +02001037 struct i2c_client *client = data->client;
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001038 struct device *dev = &client->dev;
Guenter Roeck04738b22012-01-16 22:51:46 +01001039 u8 convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
1042 data->config_fan = i2c_smbus_read_byte_data(client,
1043 LM63_REG_CONFIG_FAN);
1044
1045 /* Start converting if needed */
1046 if (data->config & 0x40) { /* standby */
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001047 dev_dbg(dev, "Switching to operational mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 data->config &= 0xA7;
1049 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
1050 data->config);
1051 }
Jean Delvare409c0b52012-01-16 22:51:46 +01001052 /* Tachometer is always enabled on LM64 */
1053 if (data->kind == lm64)
1054 data->config |= 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 /* We may need pwm1_freq before ever updating the client data */
1057 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
1058 if (data->pwm1_freq == 0)
1059 data->pwm1_freq = 1;
1060
Guenter Roeck04738b22012-01-16 22:51:46 +01001061 switch (data->kind) {
1062 case lm63:
1063 case lm64:
1064 data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
Jean Delvare2fe28ab2012-01-16 22:51:47 +01001065 data->lut_size = 8;
Guenter Roeck04738b22012-01-16 22:51:46 +01001066 break;
1067 case lm96163:
1068 data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
Jean Delvare2fe28ab2012-01-16 22:51:47 +01001069 data->lut_size = 12;
Guenter Roeckf496b2d2012-01-16 22:51:46 +01001070 data->trutherm
1071 = i2c_smbus_read_byte_data(client,
1072 LM96163_REG_TRUTHERM) & 0x02;
Guenter Roeck04738b22012-01-16 22:51:46 +01001073 break;
1074 }
1075 convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
1076 if (unlikely(convrate > LM63_MAX_CONVRATE))
1077 convrate = LM63_MAX_CONVRATE;
1078 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
1079 convrate);
1080
Guenter Roeck210961c2012-01-16 22:51:45 +01001081 /*
Guenter Roecke872c912012-01-16 22:51:46 +01001082 * For LM96163, check if high resolution PWM
1083 * and unsigned temperature format is enabled.
Guenter Roeck210961c2012-01-16 22:51:45 +01001084 */
1085 if (data->kind == lm96163) {
1086 u8 config_enhanced
1087 = i2c_smbus_read_byte_data(client,
1088 LM96163_REG_CONFIG_ENHANCED);
Jean Delvared216f682012-01-16 22:51:47 +01001089 if (config_enhanced & 0x20)
1090 data->lut_temp_highres = true;
Guenter Roeck210961c2012-01-16 22:51:45 +01001091 if ((config_enhanced & 0x10)
1092 && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
1093 data->pwm_highres = true;
1094 if (config_enhanced & 0x08)
Guenter Roecke872c912012-01-16 22:51:46 +01001095 data->remote_unsigned = true;
Guenter Roeck210961c2012-01-16 22:51:45 +01001096 }
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* Show some debug info about the LM63 configuration */
Jean Delvare409c0b52012-01-16 22:51:46 +01001099 if (data->kind == lm63)
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001100 dev_dbg(dev, "Alert/tach pin configured for %s\n",
Jean Delvare409c0b52012-01-16 22:51:46 +01001101 (data->config & 0x04) ? "tachometer input" :
1102 "alert output");
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001103 dev_dbg(dev, "PWM clock %s kHz, output frequency %u Hz\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 (data->config_fan & 0x08) ? "1.4" : "360",
1105 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001106 dev_dbg(dev, "PWM output active %s, %s mode\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 (data->config_fan & 0x10) ? "low" : "high",
1108 (data->config_fan & 0x20) ? "manual" : "auto");
1109}
1110
Jean Delvaredac27dc2012-03-23 10:02:18 +01001111static int lm63_probe(struct i2c_client *client,
1112 const struct i2c_device_id *id)
1113{
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001114 struct device *dev = &client->dev;
Guenter Roecke19eea82014-04-04 18:01:33 +02001115 struct device *hwmon_dev;
Jean Delvaredac27dc2012-03-23 10:02:18 +01001116 struct lm63_data *data;
Guenter Roeckb76552b2014-04-04 18:01:33 +02001117 int groups = 0;
Jean Delvaredac27dc2012-03-23 10:02:18 +01001118
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001119 data = devm_kzalloc(dev, sizeof(struct lm63_data), GFP_KERNEL);
Guenter Roeckc5a4a912012-06-02 09:58:08 -07001120 if (!data)
1121 return -ENOMEM;
Jean Delvaredac27dc2012-03-23 10:02:18 +01001122
Guenter Roecke19eea82014-04-04 18:01:33 +02001123 data->client = client;
Jean Delvaredac27dc2012-03-23 10:02:18 +01001124 mutex_init(&data->update_lock);
1125
1126 /* Set the device type */
1127 data->kind = id->driver_data;
1128 if (data->kind == lm64)
1129 data->temp2_offset = 16000;
1130
1131 /* Initialize chip */
Guenter Roecke19eea82014-04-04 18:01:33 +02001132 lm63_init_client(data);
Jean Delvaredac27dc2012-03-23 10:02:18 +01001133
1134 /* Register sysfs hooks */
Guenter Roeckb76552b2014-04-04 18:01:33 +02001135 data->groups[groups++] = &lm63_group;
1136 if (data->config & 0x04) /* tachometer enabled */
1137 data->groups[groups++] = &lm63_group_fan1;
1138
1139 if (data->kind == lm96163) {
1140 data->groups[groups++] = &lm63_group_temp2_type;
1141 data->groups[groups++] = &lm63_group_extra_lut;
1142 }
1143
Guenter Roecke19eea82014-04-04 18:01:33 +02001144 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1145 data, data->groups);
1146 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
Jean Delvaredac27dc2012-03-23 10:02:18 +01001149/*
1150 * Driver data (common to all clients)
1151 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Jean Delvaredac27dc2012-03-23 10:02:18 +01001153static const struct i2c_device_id lm63_id[] = {
1154 { "lm63", lm63 },
1155 { "lm64", lm64 },
1156 { "lm96163", lm96163 },
1157 { }
1158};
1159MODULE_DEVICE_TABLE(i2c, lm63_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Jean Delvaredac27dc2012-03-23 10:02:18 +01001161static struct i2c_driver lm63_driver = {
1162 .class = I2C_CLASS_HWMON,
1163 .driver = {
1164 .name = "lm63",
1165 },
1166 .probe = lm63_probe,
Jean Delvaredac27dc2012-03-23 10:02:18 +01001167 .id_table = lm63_id,
1168 .detect = lm63_detect,
1169 .address_list = normal_i2c,
1170};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Axel Linf0967ee2012-01-20 15:38:18 +08001172module_i2c_driver(lm63_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Jean Delvare7c81c602014-01-29 20:40:08 +01001174MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175MODULE_DESCRIPTION("LM63 driver");
1176MODULE_LICENSE("GPL");