blob: 50f67265c71d879a043854e9b83d40cd1e365b83 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * lm63.c - driver for the National Semiconductor LM63 temperature sensor
4 * with integrated fan control
Jean Delvare7c81c60f2014-01-29 20:40:08 +01005 * Copyright (C) 2004-2008 Jean Delvare <jdelvare@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based on the lm90 driver.
7 *
8 * The LM63 is a sensor chip made by National Semiconductor. It measures
9 * two temperatures (its own and one external one) and the speed of one
10 * fan, those speed it can additionally control. Complete datasheet can be
11 * obtained from National's website at:
12 * http://www.national.com/pf/LM/LM63.html
13 *
14 * The LM63 is basically an LM86 with fan speed monitoring and control
15 * capabilities added. It misses some of the LM86 features though:
16 * - No low limit for local temperature.
17 * - No critical limit for local temperature.
18 * - Critical limit for remote temperature can be changed only once. We
19 * will consider that the critical limit is read-only.
20 *
21 * The datasheet isn't very clear about what the tachometer reading is.
22 * I had a explanation from National Semiconductor though. The two lower
23 * bits of the read value have to be masked out. The value is still 16 bit
24 * in width.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/jiffies.h>
31#include <linux/i2c.h>
Jean Delvare10c08f82005-06-06 19:34:45 +020032#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040033#include <linux/hwmon.h>
34#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010035#include <linux/mutex.h>
Javier Martinez Canillasb25f6632017-02-24 10:13:01 -030036#include <linux/of_device.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020037#include <linux/sysfs.h>
Guenter Roeck210961c2012-01-16 22:51:45 +010038#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Addresses to scan
Jean Delvared93ab782012-01-16 22:51:47 +010042 * Address is fully defined internally and cannot be changed except for
43 * LM64 which has one pin dedicated to address selection.
44 * LM63 and LM96163 have address 0x4c.
45 * LM64 can have address 0x18 or 0x4e.
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
47
Matthew Garrett10f2ed32010-05-27 19:58:38 +020048static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * The LM63 registers
52 */
53
54#define LM63_REG_CONFIG1 0x03
Guenter Roeck04738b22012-01-16 22:51:46 +010055#define LM63_REG_CONVRATE 0x04
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define LM63_REG_CONFIG2 0xBF
57#define LM63_REG_CONFIG_FAN 0x4A
58
59#define LM63_REG_TACH_COUNT_MSB 0x47
60#define LM63_REG_TACH_COUNT_LSB 0x46
61#define LM63_REG_TACH_LIMIT_MSB 0x49
62#define LM63_REG_TACH_LIMIT_LSB 0x48
63
64#define LM63_REG_PWM_VALUE 0x4C
65#define LM63_REG_PWM_FREQ 0x4D
Jean Delvared216f682012-01-16 22:51:47 +010066#define LM63_REG_LUT_TEMP_HYST 0x4F
67#define LM63_REG_LUT_TEMP(nr) (0x50 + 2 * (nr))
68#define LM63_REG_LUT_PWM(nr) (0x51 + 2 * (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#define LM63_REG_LOCAL_TEMP 0x00
71#define LM63_REG_LOCAL_HIGH 0x05
72
73#define LM63_REG_REMOTE_TEMP_MSB 0x01
74#define LM63_REG_REMOTE_TEMP_LSB 0x10
75#define LM63_REG_REMOTE_OFFSET_MSB 0x11
76#define LM63_REG_REMOTE_OFFSET_LSB 0x12
77#define LM63_REG_REMOTE_HIGH_MSB 0x07
78#define LM63_REG_REMOTE_HIGH_LSB 0x13
79#define LM63_REG_REMOTE_LOW_MSB 0x08
80#define LM63_REG_REMOTE_LOW_LSB 0x14
81#define LM63_REG_REMOTE_TCRIT 0x19
82#define LM63_REG_REMOTE_TCRIT_HYST 0x21
83
84#define LM63_REG_ALERT_STATUS 0x02
85#define LM63_REG_ALERT_MASK 0x16
86
87#define LM63_REG_MAN_ID 0xFE
88#define LM63_REG_CHIP_ID 0xFF
89
Guenter Roeckf496b2d2012-01-16 22:51:46 +010090#define LM96163_REG_TRUTHERM 0x30
Guenter Roecke872c912012-01-16 22:51:46 +010091#define LM96163_REG_REMOTE_TEMP_U_MSB 0x31
92#define LM96163_REG_REMOTE_TEMP_U_LSB 0x32
Guenter Roeck210961c2012-01-16 22:51:45 +010093#define LM96163_REG_CONFIG_ENHANCED 0x45
94
Guenter Roeck04738b22012-01-16 22:51:46 +010095#define LM63_MAX_CONVRATE 9
96
97#define LM63_MAX_CONVRATE_HZ 32
98#define LM96163_MAX_CONVRATE_HZ 26
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
101 * Conversions and various macros
102 * For tachometer counts, the LM63 uses 16-bit values.
103 * For local temperature and high limit, remote critical limit and hysteresis
Steven Cole44bbe872005-05-03 18:21:25 -0600104 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * For remote temperature, low and high limits, it uses signed 11-bit values
Steven Cole44bbe872005-05-03 18:21:25 -0600106 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
Dirk Eibach2778fb12011-02-09 04:51:34 -0500107 * For LM64 the actual remote diode temperature is 16 degree Celsius higher
108 * than the register reading. Remote temperature setpoints have to be
109 * adapted accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
111
112#define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
113 5400000 / (reg))
114#define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
115 (5400000 / (val)) & 0xFFFC)
116#define TEMP8_FROM_REG(reg) ((reg) * 1000)
Guenter Roeck7560dc02014-06-25 08:08:35 -0700117#define TEMP8_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), -128000, \
118 127000), 1000)
119#define TEMP8U_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), 0, \
120 255000), 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
Guenter Roeck7560dc02014-06-25 08:08:35 -0700122#define TEMP11_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val((val), -128000, \
123 127875), 125) * 32)
124#define TEMP11U_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val((val), 0, \
125 255875), 125) * 32)
126#define HYST_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), 0, 127000), \
127 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Guenter Roeck04738b22012-01-16 22:51:46 +0100129#define UPDATE_INTERVAL(max, rate) \
130 ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max))
131
Guenter Roeck210961c2012-01-16 22:51:45 +0100132enum chips { lm63, lm64, lm96163 };
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * Client data (each client gets its own)
136 */
137
138struct lm63_data {
Guenter Roecke19eea82014-04-04 18:01:33 +0200139 struct i2c_client *client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100140 struct mutex update_lock;
Guenter Roeckb76552b2014-04-04 18:01:33 +0200141 const struct attribute_group *groups[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 char valid; /* zero until following fields are valid */
Jean Delvared216f682012-01-16 22:51:47 +0100143 char lut_valid; /* zero until lut fields are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned long last_updated; /* in jiffies */
Jean Delvared216f682012-01-16 22:51:47 +0100145 unsigned long lut_last_updated; /* in jiffies */
Guenter Roeck04738b22012-01-16 22:51:46 +0100146 enum chips kind;
Dirk Eibach2778fb12011-02-09 04:51:34 -0500147 int temp2_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Guenter Roeck04738b22012-01-16 22:51:46 +0100149 int update_interval; /* in milliseconds */
150 int max_convrate_hz;
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100151 int lut_size; /* 8 or 12 */
Guenter Roeck04738b22012-01-16 22:51:46 +0100152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* registers values */
154 u8 config, config_fan;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200155 u16 fan[2]; /* 0: input
156 1: low limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 u8 pwm1_freq;
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100158 u8 pwm1[13]; /* 0: current output
159 1-12: lookup table */
160 s8 temp8[15]; /* 0: local input
Jean Delvarebc51ae12005-06-05 20:32:27 +0200161 1: local high limit
Jean Delvared216f682012-01-16 22:51:47 +0100162 2: remote critical limit
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100163 3-14: lookup table */
Guenter Roeck786375f2012-01-16 22:51:45 +0100164 s16 temp11[4]; /* 0: remote input
Jean Delvarebc51ae12005-06-05 20:32:27 +0200165 1: remote low limit
Guenter Roeck786375f2012-01-16 22:51:45 +0100166 2: remote high limit
167 3: remote offset */
Guenter Roecke872c912012-01-16 22:51:46 +0100168 u16 temp11u; /* remote input (unsigned) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 u8 temp2_crit_hyst;
Jean Delvared216f682012-01-16 22:51:47 +0100170 u8 lut_temp_hyst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 u8 alarms;
Guenter Roeck210961c2012-01-16 22:51:45 +0100172 bool pwm_highres;
Jean Delvared216f682012-01-16 22:51:47 +0100173 bool lut_temp_highres;
Guenter Roecke872c912012-01-16 22:51:46 +0100174 bool remote_unsigned; /* true if unsigned remote upper limits */
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100175 bool trutherm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
Guenter Roecke872c912012-01-16 22:51:46 +0100178static inline int temp8_from_reg(struct lm63_data *data, int nr)
179{
180 if (data->remote_unsigned)
181 return TEMP8_FROM_REG((u8)data->temp8[nr]);
182 return TEMP8_FROM_REG(data->temp8[nr]);
183}
184
Jean Delvared216f682012-01-16 22:51:47 +0100185static inline int lut_temp_from_reg(struct lm63_data *data, int nr)
186{
187 return data->temp8[nr] * (data->lut_temp_highres ? 500 : 1000);
188}
189
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100190static inline int lut_temp_to_reg(struct lm63_data *data, long val)
191{
192 val -= data->temp2_offset;
193 if (data->lut_temp_highres)
Guenter Roeck2a844c12013-01-09 08:09:34 -0800194 return DIV_ROUND_CLOSEST(clamp_val(val, 0, 127500), 500);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100195 else
Guenter Roeck2a844c12013-01-09 08:09:34 -0800196 return DIV_ROUND_CLOSEST(clamp_val(val, 0, 127000), 1000);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100197}
198
Jean Delvare817c6cc2012-03-23 10:02:19 +0100199/*
200 * Update the lookup table register cache.
201 * client->update_lock must be held when calling this function.
202 */
Guenter Roecke19eea82014-04-04 18:01:33 +0200203static void lm63_update_lut(struct lm63_data *data)
Jean Delvare817c6cc2012-03-23 10:02:19 +0100204{
Guenter Roecke19eea82014-04-04 18:01:33 +0200205 struct i2c_client *client = data->client;
Jean Delvare817c6cc2012-03-23 10:02:19 +0100206 int i;
207
208 if (time_after(jiffies, data->lut_last_updated + 5 * HZ) ||
209 !data->lut_valid) {
210 for (i = 0; i < data->lut_size; i++) {
211 data->pwm1[1 + i] = i2c_smbus_read_byte_data(client,
212 LM63_REG_LUT_PWM(i));
213 data->temp8[3 + i] = i2c_smbus_read_byte_data(client,
214 LM63_REG_LUT_TEMP(i));
215 }
216 data->lut_temp_hyst = i2c_smbus_read_byte_data(client,
217 LM63_REG_LUT_TEMP_HYST);
218
219 data->lut_last_updated = jiffies;
220 data->lut_valid = 1;
221 }
222}
223
Jean Delvaredac27dc2012-03-23 10:02:18 +0100224static struct lm63_data *lm63_update_device(struct device *dev)
225{
Guenter Roecke19eea82014-04-04 18:01:33 +0200226 struct lm63_data *data = dev_get_drvdata(dev);
227 struct i2c_client *client = data->client;
Jean Delvaredac27dc2012-03-23 10:02:18 +0100228 unsigned long next_update;
Jean Delvaredac27dc2012-03-23 10:02:18 +0100229
230 mutex_lock(&data->update_lock);
231
Jean Delvare5b0620d2013-07-08 14:18:24 +0200232 next_update = data->last_updated +
233 msecs_to_jiffies(data->update_interval);
Jean Delvaredac27dc2012-03-23 10:02:18 +0100234 if (time_after(jiffies, next_update) || !data->valid) {
235 if (data->config & 0x04) { /* tachometer enabled */
236 /* order matters for fan1_input */
237 data->fan[0] = i2c_smbus_read_byte_data(client,
238 LM63_REG_TACH_COUNT_LSB) & 0xFC;
239 data->fan[0] |= i2c_smbus_read_byte_data(client,
240 LM63_REG_TACH_COUNT_MSB) << 8;
241 data->fan[1] = (i2c_smbus_read_byte_data(client,
242 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
243 | (i2c_smbus_read_byte_data(client,
244 LM63_REG_TACH_LIMIT_MSB) << 8);
245 }
246
247 data->pwm1_freq = i2c_smbus_read_byte_data(client,
248 LM63_REG_PWM_FREQ);
249 if (data->pwm1_freq == 0)
250 data->pwm1_freq = 1;
251 data->pwm1[0] = i2c_smbus_read_byte_data(client,
252 LM63_REG_PWM_VALUE);
253
254 data->temp8[0] = i2c_smbus_read_byte_data(client,
255 LM63_REG_LOCAL_TEMP);
256 data->temp8[1] = i2c_smbus_read_byte_data(client,
257 LM63_REG_LOCAL_HIGH);
258
259 /* order matters for temp2_input */
260 data->temp11[0] = i2c_smbus_read_byte_data(client,
261 LM63_REG_REMOTE_TEMP_MSB) << 8;
262 data->temp11[0] |= i2c_smbus_read_byte_data(client,
263 LM63_REG_REMOTE_TEMP_LSB);
264 data->temp11[1] = (i2c_smbus_read_byte_data(client,
265 LM63_REG_REMOTE_LOW_MSB) << 8)
266 | i2c_smbus_read_byte_data(client,
267 LM63_REG_REMOTE_LOW_LSB);
268 data->temp11[2] = (i2c_smbus_read_byte_data(client,
269 LM63_REG_REMOTE_HIGH_MSB) << 8)
270 | i2c_smbus_read_byte_data(client,
271 LM63_REG_REMOTE_HIGH_LSB);
272 data->temp11[3] = (i2c_smbus_read_byte_data(client,
273 LM63_REG_REMOTE_OFFSET_MSB) << 8)
274 | i2c_smbus_read_byte_data(client,
275 LM63_REG_REMOTE_OFFSET_LSB);
276
277 if (data->kind == lm96163)
278 data->temp11u = (i2c_smbus_read_byte_data(client,
279 LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
280 | i2c_smbus_read_byte_data(client,
281 LM96163_REG_REMOTE_TEMP_U_LSB);
282
283 data->temp8[2] = i2c_smbus_read_byte_data(client,
284 LM63_REG_REMOTE_TCRIT);
285 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
286 LM63_REG_REMOTE_TCRIT_HYST);
287
288 data->alarms = i2c_smbus_read_byte_data(client,
289 LM63_REG_ALERT_STATUS) & 0x7F;
290
291 data->last_updated = jiffies;
292 data->valid = 1;
293 }
294
Guenter Roecke19eea82014-04-04 18:01:33 +0200295 lm63_update_lut(data);
Jean Delvaredac27dc2012-03-23 10:02:18 +0100296
297 mutex_unlock(&data->update_lock);
298
299 return data;
300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*
Jean Delvare817c6cc2012-03-23 10:02:19 +0100303 * Trip points in the lookup table should be in ascending order for both
304 * temperatures and PWM output values.
305 */
Guenter Roecke19eea82014-04-04 18:01:33 +0200306static int lm63_lut_looks_bad(struct device *dev, struct lm63_data *data)
Jean Delvare817c6cc2012-03-23 10:02:19 +0100307{
Jean Delvare817c6cc2012-03-23 10:02:19 +0100308 int i;
309
310 mutex_lock(&data->update_lock);
Guenter Roecke19eea82014-04-04 18:01:33 +0200311 lm63_update_lut(data);
Jean Delvare817c6cc2012-03-23 10:02:19 +0100312
313 for (i = 1; i < data->lut_size; i++) {
314 if (data->pwm1[1 + i - 1] > data->pwm1[1 + i]
315 || data->temp8[3 + i - 1] > data->temp8[3 + i]) {
Guenter Roecke19eea82014-04-04 18:01:33 +0200316 dev_warn(dev,
Jean Delvare817c6cc2012-03-23 10:02:19 +0100317 "Lookup table doesn't look sane (check entries %d and %d)\n",
318 i, i + 1);
319 break;
320 }
321 }
322 mutex_unlock(&data->update_lock);
323
324 return i == data->lut_size ? 0 : 1;
325}
326
327/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * Sysfs callback functions and files
329 */
330
Jean Delvarebc51ae12005-06-05 20:32:27 +0200331static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
332 char *buf)
333{
334 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
335 struct lm63_data *data = lm63_update_device(dev);
336 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Jean Delvarebc51ae12005-06-05 20:32:27 +0200339static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
340 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Guenter Roecke19eea82014-04-04 18:01:33 +0200342 struct lm63_data *data = dev_get_drvdata(dev);
343 struct i2c_client *client = data->client;
Guenter Roeck662bda22012-01-16 22:51:45 +0100344 unsigned long val;
345 int err;
346
347 err = kstrtoul(buf, 10, &val);
348 if (err)
349 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100351 mutex_lock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200352 data->fan[1] = FAN_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200354 data->fan[1] & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200356 data->fan[1] >> 8);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100357 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return count;
359}
360
Jean Delvared216f682012-01-16 22:51:47 +0100361static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200362 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Jean Delvared216f682012-01-16 22:51:47 +0100364 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct lm63_data *data = lm63_update_device(dev);
Jean Delvared216f682012-01-16 22:51:47 +0100366 int nr = attr->index;
Guenter Roeck210961c2012-01-16 22:51:45 +0100367 int pwm;
368
369 if (data->pwm_highres)
Jean Delvared216f682012-01-16 22:51:47 +0100370 pwm = data->pwm1[nr];
Guenter Roeck210961c2012-01-16 22:51:45 +0100371 else
Jean Delvared216f682012-01-16 22:51:47 +0100372 pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ?
373 255 : (data->pwm1[nr] * 255 + data->pwm1_freq) /
Guenter Roeck210961c2012-01-16 22:51:45 +0100374 (2 * data->pwm1_freq);
375
376 return sprintf(buf, "%d\n", pwm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100379static ssize_t set_pwm1(struct device *dev, struct device_attribute *devattr,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200380 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100382 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecke19eea82014-04-04 18:01:33 +0200383 struct lm63_data *data = dev_get_drvdata(dev);
384 struct i2c_client *client = data->client;
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100385 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 unsigned long val;
Guenter Roeck662bda22012-01-16 22:51:45 +0100387 int err;
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100388 u8 reg;
Guenter Roeck662bda22012-01-16 22:51:45 +0100389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (!(data->config_fan & 0x20)) /* register is read-only */
391 return -EPERM;
392
Guenter Roeck662bda22012-01-16 22:51:45 +0100393 err = kstrtoul(buf, 10, &val);
394 if (err)
395 return err;
396
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100397 reg = nr ? LM63_REG_LUT_PWM(nr - 1) : LM63_REG_PWM_VALUE;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800398 val = clamp_val(val, 0, 255);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100399
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100400 mutex_lock(&data->update_lock);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100401 data->pwm1[nr] = data->pwm_highres ? val :
Jean Delvared216f682012-01-16 22:51:47 +0100402 (val * data->pwm1_freq * 2 + 127) / 255;
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100403 i2c_smbus_write_byte_data(client, reg, data->pwm1[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100404 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return count;
406}
407
Julia Lawalldc7a3262016-12-22 13:04:49 +0100408static ssize_t pwm1_enable_show(struct device *dev,
Guenter Roeck662bda22012-01-16 22:51:45 +0100409 struct device_attribute *dummy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 struct lm63_data *data = lm63_update_device(dev);
412 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
413}
414
Julia Lawalldc7a3262016-12-22 13:04:49 +0100415static ssize_t pwm1_enable_store(struct device *dev,
416 struct device_attribute *dummy,
417 const char *buf, size_t count)
Jean Delvare817c6cc2012-03-23 10:02:19 +0100418{
Guenter Roecke19eea82014-04-04 18:01:33 +0200419 struct lm63_data *data = dev_get_drvdata(dev);
420 struct i2c_client *client = data->client;
Jean Delvare817c6cc2012-03-23 10:02:19 +0100421 unsigned long val;
422 int err;
423
424 err = kstrtoul(buf, 10, &val);
425 if (err)
426 return err;
427 if (val < 1 || val > 2)
428 return -EINVAL;
429
430 /*
431 * Only let the user switch to automatic mode if the lookup table
432 * looks sane.
433 */
Guenter Roecke19eea82014-04-04 18:01:33 +0200434 if (val == 2 && lm63_lut_looks_bad(dev, data))
Jean Delvare817c6cc2012-03-23 10:02:19 +0100435 return -EPERM;
436
437 mutex_lock(&data->update_lock);
438 data->config_fan = i2c_smbus_read_byte_data(client,
439 LM63_REG_CONFIG_FAN);
440 if (val == 1)
441 data->config_fan |= 0x20;
442 else
443 data->config_fan &= ~0x20;
444 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN,
Guenter Roecke19eea82014-04-04 18:01:33 +0200445 data->config_fan);
Jean Delvare817c6cc2012-03-23 10:02:19 +0100446 mutex_unlock(&data->update_lock);
447 return count;
448}
449
Dirk Eibach2778fb12011-02-09 04:51:34 -0500450/*
451 * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
452 * For remote sensor registers temp2_offset has to be considered,
453 * for local sensor it must not.
454 * So we need separate 8bit accessors for local and remote sensor.
455 */
456static ssize_t show_local_temp8(struct device *dev,
457 struct device_attribute *devattr,
458 char *buf)
Jean Delvarebc51ae12005-06-05 20:32:27 +0200459{
460 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
461 struct lm63_data *data = lm63_update_device(dev);
462 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Dirk Eibach2778fb12011-02-09 04:51:34 -0500465static ssize_t show_remote_temp8(struct device *dev,
466 struct device_attribute *devattr,
467 char *buf)
468{
469 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
470 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100471 return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
Dirk Eibach2778fb12011-02-09 04:51:34 -0500472 + data->temp2_offset);
473}
474
Jean Delvared216f682012-01-16 22:51:47 +0100475static ssize_t show_lut_temp(struct device *dev,
476 struct device_attribute *devattr,
477 char *buf)
478{
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", lut_temp_from_reg(data, attr->index)
482 + data->temp2_offset);
483}
484
Guenter Roeck94e55df42012-01-16 22:51:46 +0100485static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
486 const char *buf, size_t count)
Jean Delvarebc51ae12005-06-05 20:32:27 +0200487{
Guenter Roeck94e55df42012-01-16 22:51:46 +0100488 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecke19eea82014-04-04 18:01:33 +0200489 struct lm63_data *data = dev_get_drvdata(dev);
490 struct i2c_client *client = data->client;
Guenter Roeck94e55df42012-01-16 22:51:46 +0100491 int nr = attr->index;
Guenter Roeck662bda22012-01-16 22:51:45 +0100492 long val;
493 int err;
Guenter Roeck94e55df42012-01-16 22:51:46 +0100494 int temp;
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100495 u8 reg;
Guenter Roeck662bda22012-01-16 22:51:45 +0100496
497 err = kstrtol(buf, 10, &val);
498 if (err)
499 return err;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200500
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100501 mutex_lock(&data->update_lock);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100502 switch (nr) {
503 case 2:
504 reg = LM63_REG_REMOTE_TCRIT;
Guenter Roeck94e55df42012-01-16 22:51:46 +0100505 if (data->remote_unsigned)
506 temp = TEMP8U_TO_REG(val - data->temp2_offset);
507 else
508 temp = TEMP8_TO_REG(val - data->temp2_offset);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100509 break;
510 case 1:
511 reg = LM63_REG_LOCAL_HIGH;
Guenter Roeck94e55df42012-01-16 22:51:46 +0100512 temp = TEMP8_TO_REG(val);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100513 break;
514 default: /* lookup table */
515 reg = LM63_REG_LUT_TEMP(nr - 3);
516 temp = lut_temp_to_reg(data, val);
Guenter Roeck94e55df42012-01-16 22:51:46 +0100517 }
518 data->temp8[nr] = temp;
519 i2c_smbus_write_byte_data(client, reg, temp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100520 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200521 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200523
524static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
525 char *buf)
526{
527 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
528 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100529 int nr = attr->index;
530 int temp;
531
532 if (!nr) {
533 /*
534 * Use unsigned temperature unless its value is zero.
535 * If it is zero, use signed temperature.
536 */
537 if (data->temp11u)
538 temp = TEMP11_FROM_REG(data->temp11u);
539 else
540 temp = TEMP11_FROM_REG(data->temp11[nr]);
541 } else {
542 if (data->remote_unsigned && nr == 2)
543 temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
544 else
545 temp = TEMP11_FROM_REG(data->temp11[nr]);
546 }
547 return sprintf(buf, "%d\n", temp + data->temp2_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200549
550static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
551 const char *buf, size_t count)
552{
Guenter Roeck786375f2012-01-16 22:51:45 +0100553 static const u8 reg[6] = {
Jean Delvarebc51ae12005-06-05 20:32:27 +0200554 LM63_REG_REMOTE_LOW_MSB,
555 LM63_REG_REMOTE_LOW_LSB,
556 LM63_REG_REMOTE_HIGH_MSB,
557 LM63_REG_REMOTE_HIGH_LSB,
Guenter Roeck786375f2012-01-16 22:51:45 +0100558 LM63_REG_REMOTE_OFFSET_MSB,
559 LM63_REG_REMOTE_OFFSET_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200560 };
561
562 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecke19eea82014-04-04 18:01:33 +0200563 struct lm63_data *data = dev_get_drvdata(dev);
564 struct i2c_client *client = data->client;
Guenter Roeck662bda22012-01-16 22:51:45 +0100565 long val;
566 int err;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200567 int nr = attr->index;
568
Guenter Roeck662bda22012-01-16 22:51:45 +0100569 err = kstrtol(buf, 10, &val);
570 if (err)
571 return err;
572
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100573 mutex_lock(&data->update_lock);
Guenter Roecke872c912012-01-16 22:51:46 +0100574 if (data->remote_unsigned && nr == 2)
575 data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
576 else
577 data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
578
Jean Delvarebc51ae12005-06-05 20:32:27 +0200579 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
580 data->temp11[nr] >> 8);
581 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
582 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100583 mutex_unlock(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200584 return count;
585}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Guenter Roeck662bda22012-01-16 22:51:45 +0100587/*
588 * Hysteresis register holds a relative value, while we want to present
589 * an absolute to user-space
590 */
Julia Lawalldc7a3262016-12-22 13:04:49 +0100591static ssize_t temp2_crit_hyst_show(struct device *dev,
Guenter Roeck662bda22012-01-16 22:51:45 +0100592 struct device_attribute *dummy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 struct lm63_data *data = lm63_update_device(dev);
Guenter Roecke872c912012-01-16 22:51:46 +0100595 return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
Dirk Eibach2778fb12011-02-09 04:51:34 -0500596 + data->temp2_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 - TEMP8_FROM_REG(data->temp2_crit_hyst));
598}
599
Jean Delvared216f682012-01-16 22:51:47 +0100600static ssize_t show_lut_temp_hyst(struct device *dev,
601 struct device_attribute *devattr, char *buf)
602{
603 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
604 struct lm63_data *data = lm63_update_device(dev);
605
606 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
607 + data->temp2_offset
608 - TEMP8_FROM_REG(data->lut_temp_hyst));
609}
610
Guenter Roeck662bda22012-01-16 22:51:45 +0100611/*
612 * And now the other way around, user-space provides an absolute
613 * hysteresis value and we have to store a relative one
614 */
Julia Lawalldc7a3262016-12-22 13:04:49 +0100615static ssize_t temp2_crit_hyst_store(struct device *dev,
616 struct device_attribute *dummy,
617 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Guenter Roecke19eea82014-04-04 18:01:33 +0200619 struct lm63_data *data = dev_get_drvdata(dev);
620 struct i2c_client *client = data->client;
Guenter Roeck662bda22012-01-16 22:51:45 +0100621 long val;
622 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 long hyst;
624
Guenter Roeck662bda22012-01-16 22:51:45 +0100625 err = kstrtol(buf, 10, &val);
626 if (err)
627 return err;
628
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100629 mutex_lock(&data->update_lock);
Guenter Roecke872c912012-01-16 22:51:46 +0100630 hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
632 HYST_TO_REG(hyst));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100633 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return count;
635}
636
Guenter Roeck04738b22012-01-16 22:51:46 +0100637/*
638 * Set conversion rate.
639 * client->update_lock must be held when calling this function.
640 */
Guenter Roecke19eea82014-04-04 18:01:33 +0200641static void lm63_set_convrate(struct lm63_data *data, unsigned int interval)
Guenter Roeck04738b22012-01-16 22:51:46 +0100642{
Guenter Roecke19eea82014-04-04 18:01:33 +0200643 struct i2c_client *client = data->client;
Guenter Roeck04738b22012-01-16 22:51:46 +0100644 unsigned int update_interval;
Guenter Roecke19eea82014-04-04 18:01:33 +0200645 int i;
Guenter Roeck04738b22012-01-16 22:51:46 +0100646
647 /* Shift calculations to avoid rounding errors */
648 interval <<= 6;
649
650 /* find the nearest update rate */
651 update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
652 / data->max_convrate_hz;
653 for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
654 if (interval >= update_interval * 3 / 4)
655 break;
656
657 i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
658 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
659}
660
Julia Lawalldc7a3262016-12-22 13:04:49 +0100661static ssize_t update_interval_show(struct device *dev,
Guenter Roeck04738b22012-01-16 22:51:46 +0100662 struct device_attribute *attr, char *buf)
663{
664 struct lm63_data *data = dev_get_drvdata(dev);
665
666 return sprintf(buf, "%u\n", data->update_interval);
667}
668
Julia Lawalldc7a3262016-12-22 13:04:49 +0100669static ssize_t update_interval_store(struct device *dev,
670 struct device_attribute *attr,
671 const char *buf, size_t count)
Guenter Roeck04738b22012-01-16 22:51:46 +0100672{
Guenter Roecke19eea82014-04-04 18:01:33 +0200673 struct lm63_data *data = dev_get_drvdata(dev);
Guenter Roeck04738b22012-01-16 22:51:46 +0100674 unsigned long val;
675 int err;
676
677 err = kstrtoul(buf, 10, &val);
678 if (err)
679 return err;
680
681 mutex_lock(&data->update_lock);
Guenter Roecke19eea82014-04-04 18:01:33 +0200682 lm63_set_convrate(data, clamp_val(val, 0, 100000));
Guenter Roeck04738b22012-01-16 22:51:46 +0100683 mutex_unlock(&data->update_lock);
684
685 return count;
686}
687
Julia Lawalldc7a3262016-12-22 13:04:49 +0100688static ssize_t temp2_type_show(struct device *dev,
689 struct device_attribute *attr, char *buf)
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100690{
Guenter Roecke19eea82014-04-04 18:01:33 +0200691 struct lm63_data *data = dev_get_drvdata(dev);
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100692
693 return sprintf(buf, data->trutherm ? "1\n" : "2\n");
694}
695
Julia Lawalldc7a3262016-12-22 13:04:49 +0100696static ssize_t temp2_type_store(struct device *dev,
697 struct device_attribute *attr,
698 const char *buf, size_t count)
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100699{
Guenter Roecke19eea82014-04-04 18:01:33 +0200700 struct lm63_data *data = dev_get_drvdata(dev);
701 struct i2c_client *client = data->client;
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100702 unsigned long val;
703 int ret;
704 u8 reg;
705
706 ret = kstrtoul(buf, 10, &val);
707 if (ret < 0)
708 return ret;
709 if (val != 1 && val != 2)
710 return -EINVAL;
711
712 mutex_lock(&data->update_lock);
713 data->trutherm = val == 1;
714 reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02;
715 i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM,
716 reg | (data->trutherm ? 0x02 : 0x00));
717 data->valid = 0;
718 mutex_unlock(&data->update_lock);
719
720 return count;
721}
722
Julia Lawalldc7a3262016-12-22 13:04:49 +0100723static ssize_t alarms_show(struct device *dev, struct device_attribute *dummy,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200724 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct lm63_data *data = lm63_update_device(dev);
727 return sprintf(buf, "%u\n", data->alarms);
728}
729
Jean Delvare2d457712006-09-24 20:52:15 +0200730static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
731 char *buf)
732{
733 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
734 struct lm63_data *data = lm63_update_device(dev);
735 int bitnr = attr->index;
736
737 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
738}
739
Jean Delvarebc51ae12005-06-05 20:32:27 +0200740static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
741static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
742 set_fan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Jean Delvared216f682012-01-16 22:51:47 +0100744static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1, 0);
Julia Lawalldc7a3262016-12-22 13:04:49 +0100745static DEVICE_ATTR_RW(pwm1_enable);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100746static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
747 show_pwm1, set_pwm1, 1);
748static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
749 show_lut_temp, set_temp8, 3);
Jean Delvared216f682012-01-16 22:51:47 +0100750static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp_hyst, S_IRUGO,
751 show_lut_temp_hyst, NULL, 3);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100752static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
753 show_pwm1, set_pwm1, 2);
754static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
755 show_lut_temp, set_temp8, 4);
Jean Delvared216f682012-01-16 22:51:47 +0100756static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp_hyst, S_IRUGO,
757 show_lut_temp_hyst, NULL, 4);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100758static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
759 show_pwm1, set_pwm1, 3);
760static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
761 show_lut_temp, set_temp8, 5);
Jean Delvared216f682012-01-16 22:51:47 +0100762static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp_hyst, S_IRUGO,
763 show_lut_temp_hyst, NULL, 5);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100764static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
765 show_pwm1, set_pwm1, 4);
766static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
767 show_lut_temp, set_temp8, 6);
Jean Delvared216f682012-01-16 22:51:47 +0100768static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp_hyst, S_IRUGO,
769 show_lut_temp_hyst, NULL, 6);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100770static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
771 show_pwm1, set_pwm1, 5);
772static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
773 show_lut_temp, set_temp8, 7);
Jean Delvared216f682012-01-16 22:51:47 +0100774static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp_hyst, S_IRUGO,
775 show_lut_temp_hyst, NULL, 7);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100776static SENSOR_DEVICE_ATTR(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
777 show_pwm1, set_pwm1, 6);
778static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
779 show_lut_temp, set_temp8, 8);
Jean Delvared216f682012-01-16 22:51:47 +0100780static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp_hyst, S_IRUGO,
781 show_lut_temp_hyst, NULL, 8);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100782static SENSOR_DEVICE_ATTR(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
783 show_pwm1, set_pwm1, 7);
784static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
785 show_lut_temp, set_temp8, 9);
Jean Delvared216f682012-01-16 22:51:47 +0100786static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp_hyst, S_IRUGO,
787 show_lut_temp_hyst, NULL, 9);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100788static SENSOR_DEVICE_ATTR(pwm1_auto_point8_pwm, S_IWUSR | S_IRUGO,
789 show_pwm1, set_pwm1, 8);
790static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp, S_IWUSR | S_IRUGO,
791 show_lut_temp, set_temp8, 10);
Jean Delvared216f682012-01-16 22:51:47 +0100792static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp_hyst, S_IRUGO,
793 show_lut_temp_hyst, NULL, 10);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100794static SENSOR_DEVICE_ATTR(pwm1_auto_point9_pwm, S_IWUSR | S_IRUGO,
795 show_pwm1, set_pwm1, 9);
796static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp, S_IWUSR | S_IRUGO,
797 show_lut_temp, set_temp8, 11);
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100798static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp_hyst, S_IRUGO,
799 show_lut_temp_hyst, NULL, 11);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100800static SENSOR_DEVICE_ATTR(pwm1_auto_point10_pwm, S_IWUSR | S_IRUGO,
801 show_pwm1, set_pwm1, 10);
802static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp, S_IWUSR | S_IRUGO,
803 show_lut_temp, set_temp8, 12);
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100804static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp_hyst, S_IRUGO,
805 show_lut_temp_hyst, NULL, 12);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100806static SENSOR_DEVICE_ATTR(pwm1_auto_point11_pwm, S_IWUSR | S_IRUGO,
807 show_pwm1, set_pwm1, 11);
808static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp, S_IWUSR | S_IRUGO,
809 show_lut_temp, set_temp8, 13);
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100810static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp_hyst, S_IRUGO,
811 show_lut_temp_hyst, NULL, 13);
Jean Delvareaf2ef4f2012-03-23 10:02:19 +0100812static SENSOR_DEVICE_ATTR(pwm1_auto_point12_pwm, S_IWUSR | S_IRUGO,
813 show_pwm1, set_pwm1, 12);
814static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp, S_IWUSR | S_IRUGO,
815 show_lut_temp, set_temp8, 14);
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100816static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp_hyst, S_IRUGO,
817 show_lut_temp_hyst, NULL, 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Dirk Eibach2778fb12011-02-09 04:51:34 -0500819static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
820static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
Guenter Roeck94e55df42012-01-16 22:51:46 +0100821 set_temp8, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Jean Delvarebc51ae12005-06-05 20:32:27 +0200823static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
824static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
825 set_temp11, 1);
826static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
827 set_temp11, 2);
Guenter Roeck786375f2012-01-16 22:51:45 +0100828static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
829 set_temp11, 3);
Dirk Eibach2778fb12011-02-09 04:51:34 -0500830static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
Guenter Roeck94e55df42012-01-16 22:51:46 +0100831 set_temp8, 2);
Julia Lawalldc7a3262016-12-22 13:04:49 +0100832static DEVICE_ATTR_RW(temp2_crit_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Julia Lawalldc7a3262016-12-22 13:04:49 +0100834static DEVICE_ATTR_RW(temp2_type);
Guenter Roeckf496b2d2012-01-16 22:51:46 +0100835
Jean Delvare2d457712006-09-24 20:52:15 +0200836/* Individual alarm files */
837static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
838static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400839static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200840static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
841static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
842static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
843/* Raw alarm file for compatibility */
Julia Lawalldc7a3262016-12-22 13:04:49 +0100844static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Julia Lawalldc7a3262016-12-22 13:04:49 +0100846static DEVICE_ATTR_RW(update_interval);
Guenter Roeck04738b22012-01-16 22:51:46 +0100847
Jean Delvare0e39e012006-09-24 21:16:40 +0200848static struct attribute *lm63_attributes[] = {
Jean Delvared216f682012-01-16 22:51:47 +0100849 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200850 &dev_attr_pwm1_enable.attr,
Jean Delvared216f682012-01-16 22:51:47 +0100851 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
852 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
853 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
854 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
855 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
856 &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr,
857 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
858 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
859 &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr,
860 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
861 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
862 &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr,
863 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
864 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
865 &sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr,
866 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
867 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
868 &sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr,
869 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
870 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
871 &sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr,
872 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
873 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
874 &sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr,
875
Jean Delvare0e39e012006-09-24 21:16:40 +0200876 &sensor_dev_attr_temp1_input.dev_attr.attr,
877 &sensor_dev_attr_temp2_input.dev_attr.attr,
878 &sensor_dev_attr_temp2_min.dev_attr.attr,
879 &sensor_dev_attr_temp1_max.dev_attr.attr,
880 &sensor_dev_attr_temp2_max.dev_attr.attr,
Guenter Roeck786375f2012-01-16 22:51:45 +0100881 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200882 &sensor_dev_attr_temp2_crit.dev_attr.attr,
883 &dev_attr_temp2_crit_hyst.attr,
884
885 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400886 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200887 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
888 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
889 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
890 &dev_attr_alarms.attr,
Guenter Roeck04738b22012-01-16 22:51:46 +0100891 &dev_attr_update_interval.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200892 NULL
893};
894
Guenter Roeck0dcb28a2014-04-04 18:01:32 +0200895static struct attribute *lm63_attributes_temp2_type[] = {
896 &dev_attr_temp2_type.attr,
897 NULL
898};
899
900static const struct attribute_group lm63_group_temp2_type = {
901 .attrs = lm63_attributes_temp2_type,
902};
903
Jean Delvare2fe28ab2012-01-16 22:51:47 +0100904static struct attribute *lm63_attributes_extra_lut[] = {
905 &sensor_dev_attr_pwm1_auto_point9_pwm.dev_attr.attr,
906 &sensor_dev_attr_pwm1_auto_point9_temp.dev_attr.attr,
907 &sensor_dev_attr_pwm1_auto_point9_temp_hyst.dev_attr.attr,
908 &sensor_dev_attr_pwm1_auto_point10_pwm.dev_attr.attr,
909 &sensor_dev_attr_pwm1_auto_point10_temp.dev_attr.attr,
910 &sensor_dev_attr_pwm1_auto_point10_temp_hyst.dev_attr.attr,
911 &sensor_dev_attr_pwm1_auto_point11_pwm.dev_attr.attr,
912 &sensor_dev_attr_pwm1_auto_point11_temp.dev_attr.attr,
913 &sensor_dev_attr_pwm1_auto_point11_temp_hyst.dev_attr.attr,
914 &sensor_dev_attr_pwm1_auto_point12_pwm.dev_attr.attr,
915 &sensor_dev_attr_pwm1_auto_point12_temp.dev_attr.attr,
916 &sensor_dev_attr_pwm1_auto_point12_temp_hyst.dev_attr.attr,
917 NULL
918};
919
920static const struct attribute_group lm63_group_extra_lut = {
921 .attrs = lm63_attributes_extra_lut,
922};
923
Guenter Roeck94e55df42012-01-16 22:51:46 +0100924/*
925 * On LM63, temp2_crit can be set only once, which should be job
926 * of the bootloader.
927 * On LM64, temp2_crit can always be set.
928 * On LM96163, temp2_crit can be set if bit 1 of the configuration
929 * register is true.
930 */
931static umode_t lm63_attribute_mode(struct kobject *kobj,
932 struct attribute *attr, int index)
933{
934 struct device *dev = container_of(kobj, struct device, kobj);
Guenter Roecke19eea82014-04-04 18:01:33 +0200935 struct lm63_data *data = dev_get_drvdata(dev);
Guenter Roeck94e55df42012-01-16 22:51:46 +0100936
937 if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
938 && (data->kind == lm64 ||
939 (data->kind == lm96163 && (data->config & 0x02))))
940 return attr->mode | S_IWUSR;
941
942 return attr->mode;
943}
944
Jean Delvare0e39e012006-09-24 21:16:40 +0200945static const struct attribute_group lm63_group = {
Guenter Roeck94e55df42012-01-16 22:51:46 +0100946 .is_visible = lm63_attribute_mode,
Jean Delvare0e39e012006-09-24 21:16:40 +0200947 .attrs = lm63_attributes,
948};
949
950static struct attribute *lm63_attributes_fan1[] = {
951 &sensor_dev_attr_fan1_input.dev_attr.attr,
952 &sensor_dev_attr_fan1_min.dev_attr.attr,
953
954 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
955 NULL
956};
957
958static const struct attribute_group lm63_group_fan1 = {
959 .attrs = lm63_attributes_fan1,
960};
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962/*
963 * Real code
964 */
965
Jean Delvared5957be2008-07-16 19:30:13 +0200966/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvaredac27dc2012-03-23 10:02:18 +0100967static int lm63_detect(struct i2c_client *client,
Jean Delvared5957be2008-07-16 19:30:13 +0200968 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Jean Delvaredac27dc2012-03-23 10:02:18 +0100970 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100971 u8 man_id, chip_id, reg_config1, reg_config2;
972 u8 reg_alert_status, reg_alert_mask;
Jean Delvaredac27dc2012-03-23 10:02:18 +0100973 int address = client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvared5957be2008-07-16 19:30:13 +0200976 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Jean Delvaredac27dc2012-03-23 10:02:18 +0100978 man_id = i2c_smbus_read_byte_data(client, LM63_REG_MAN_ID);
979 chip_id = i2c_smbus_read_byte_data(client, LM63_REG_CHIP_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Jean Delvaredac27dc2012-03-23 10:02:18 +0100981 reg_config1 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
982 reg_config2 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG2);
983 reg_alert_status = i2c_smbus_read_byte_data(client,
Jean Delvare52df6442009-12-09 20:35:57 +0100984 LM63_REG_ALERT_STATUS);
Jean Delvaredac27dc2012-03-23 10:02:18 +0100985 reg_alert_mask = i2c_smbus_read_byte_data(client, LM63_REG_ALERT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Jean Delvare52df6442009-12-09 20:35:57 +0100987 if (man_id != 0x01 /* National Semiconductor */
Jean Delvare52df6442009-12-09 20:35:57 +0100988 || (reg_config1 & 0x18) != 0x00
989 || (reg_config2 & 0xF8) != 0x00
990 || (reg_alert_status & 0x20) != 0x00
991 || (reg_alert_mask & 0xA4) != 0xA4) {
992 dev_dbg(&adapter->dev,
993 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
994 man_id, chip_id);
995 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997
Matthew Garrett10f2ed32010-05-27 19:58:38 +0200998 if (chip_id == 0x41 && address == 0x4c)
999 strlcpy(info->type, "lm63", I2C_NAME_SIZE);
1000 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
1001 strlcpy(info->type, "lm64", I2C_NAME_SIZE);
Guenter Roeck210961c2012-01-16 22:51:45 +01001002 else if (chip_id == 0x49 && address == 0x4c)
1003 strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
Matthew Garrett10f2ed32010-05-27 19:58:38 +02001004 else
1005 return -ENODEV;
Jean Delvared5957be2008-07-16 19:30:13 +02001006
1007 return 0;
1008}
1009
Guenter Roeck662bda22012-01-16 22:51:45 +01001010/*
1011 * Ideally we shouldn't have to initialize anything, since the BIOS
1012 * should have taken care of everything
1013 */
Guenter Roecke19eea82014-04-04 18:01:33 +02001014static void lm63_init_client(struct lm63_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Guenter Roecke19eea82014-04-04 18:01:33 +02001016 struct i2c_client *client = data->client;
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001017 struct device *dev = &client->dev;
Guenter Roeck04738b22012-01-16 22:51:46 +01001018 u8 convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
1021 data->config_fan = i2c_smbus_read_byte_data(client,
1022 LM63_REG_CONFIG_FAN);
1023
1024 /* Start converting if needed */
1025 if (data->config & 0x40) { /* standby */
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001026 dev_dbg(dev, "Switching to operational mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 data->config &= 0xA7;
1028 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
1029 data->config);
1030 }
Jean Delvare409c0b52012-01-16 22:51:46 +01001031 /* Tachometer is always enabled on LM64 */
1032 if (data->kind == lm64)
1033 data->config |= 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 /* We may need pwm1_freq before ever updating the client data */
1036 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
1037 if (data->pwm1_freq == 0)
1038 data->pwm1_freq = 1;
1039
Guenter Roeck04738b22012-01-16 22:51:46 +01001040 switch (data->kind) {
1041 case lm63:
1042 case lm64:
1043 data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
Jean Delvare2fe28ab2012-01-16 22:51:47 +01001044 data->lut_size = 8;
Guenter Roeck04738b22012-01-16 22:51:46 +01001045 break;
1046 case lm96163:
1047 data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
Jean Delvare2fe28ab2012-01-16 22:51:47 +01001048 data->lut_size = 12;
Guenter Roeckf496b2d2012-01-16 22:51:46 +01001049 data->trutherm
1050 = i2c_smbus_read_byte_data(client,
1051 LM96163_REG_TRUTHERM) & 0x02;
Guenter Roeck04738b22012-01-16 22:51:46 +01001052 break;
1053 }
1054 convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
1055 if (unlikely(convrate > LM63_MAX_CONVRATE))
1056 convrate = LM63_MAX_CONVRATE;
1057 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
1058 convrate);
1059
Guenter Roeck210961c2012-01-16 22:51:45 +01001060 /*
Guenter Roecke872c912012-01-16 22:51:46 +01001061 * For LM96163, check if high resolution PWM
1062 * and unsigned temperature format is enabled.
Guenter Roeck210961c2012-01-16 22:51:45 +01001063 */
1064 if (data->kind == lm96163) {
1065 u8 config_enhanced
1066 = i2c_smbus_read_byte_data(client,
1067 LM96163_REG_CONFIG_ENHANCED);
Jean Delvared216f682012-01-16 22:51:47 +01001068 if (config_enhanced & 0x20)
1069 data->lut_temp_highres = true;
Guenter Roeck210961c2012-01-16 22:51:45 +01001070 if ((config_enhanced & 0x10)
1071 && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
1072 data->pwm_highres = true;
1073 if (config_enhanced & 0x08)
Guenter Roecke872c912012-01-16 22:51:46 +01001074 data->remote_unsigned = true;
Guenter Roeck210961c2012-01-16 22:51:45 +01001075 }
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /* Show some debug info about the LM63 configuration */
Jean Delvare409c0b52012-01-16 22:51:46 +01001078 if (data->kind == lm63)
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001079 dev_dbg(dev, "Alert/tach pin configured for %s\n",
Jean Delvare409c0b52012-01-16 22:51:46 +01001080 (data->config & 0x04) ? "tachometer input" :
1081 "alert output");
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001082 dev_dbg(dev, "PWM clock %s kHz, output frequency %u Hz\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 (data->config_fan & 0x08) ? "1.4" : "360",
1084 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001085 dev_dbg(dev, "PWM output active %s, %s mode\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 (data->config_fan & 0x10) ? "low" : "high",
1087 (data->config_fan & 0x20) ? "manual" : "auto");
1088}
1089
Stephen Kitt67487032020-08-13 18:02:22 +02001090static const struct i2c_device_id lm63_id[];
1091
1092static int lm63_probe(struct i2c_client *client)
Jean Delvaredac27dc2012-03-23 10:02:18 +01001093{
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001094 struct device *dev = &client->dev;
Guenter Roecke19eea82014-04-04 18:01:33 +02001095 struct device *hwmon_dev;
Jean Delvaredac27dc2012-03-23 10:02:18 +01001096 struct lm63_data *data;
Guenter Roeckb76552b2014-04-04 18:01:33 +02001097 int groups = 0;
Jean Delvaredac27dc2012-03-23 10:02:18 +01001098
Guenter Roeck2fd638f2014-04-04 18:01:33 +02001099 data = devm_kzalloc(dev, sizeof(struct lm63_data), GFP_KERNEL);
Guenter Roeckc5a4a912012-06-02 09:58:08 -07001100 if (!data)
1101 return -ENOMEM;
Jean Delvaredac27dc2012-03-23 10:02:18 +01001102
Guenter Roecke19eea82014-04-04 18:01:33 +02001103 data->client = client;
Jean Delvaredac27dc2012-03-23 10:02:18 +01001104 mutex_init(&data->update_lock);
1105
1106 /* Set the device type */
Javier Martinez Canillasb25f6632017-02-24 10:13:01 -03001107 if (client->dev.of_node)
1108 data->kind = (enum chips)of_device_get_match_data(&client->dev);
1109 else
Stephen Kitt67487032020-08-13 18:02:22 +02001110 data->kind = i2c_match_id(lm63_id, client)->driver_data;
Jean Delvaredac27dc2012-03-23 10:02:18 +01001111 if (data->kind == lm64)
1112 data->temp2_offset = 16000;
1113
1114 /* Initialize chip */
Guenter Roecke19eea82014-04-04 18:01:33 +02001115 lm63_init_client(data);
Jean Delvaredac27dc2012-03-23 10:02:18 +01001116
1117 /* Register sysfs hooks */
Guenter Roeckb76552b2014-04-04 18:01:33 +02001118 data->groups[groups++] = &lm63_group;
1119 if (data->config & 0x04) /* tachometer enabled */
1120 data->groups[groups++] = &lm63_group_fan1;
1121
1122 if (data->kind == lm96163) {
1123 data->groups[groups++] = &lm63_group_temp2_type;
1124 data->groups[groups++] = &lm63_group_extra_lut;
1125 }
1126
Guenter Roecke19eea82014-04-04 18:01:33 +02001127 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1128 data, data->groups);
1129 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
Jean Delvaredac27dc2012-03-23 10:02:18 +01001132/*
1133 * Driver data (common to all clients)
1134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Jean Delvaredac27dc2012-03-23 10:02:18 +01001136static const struct i2c_device_id lm63_id[] = {
1137 { "lm63", lm63 },
1138 { "lm64", lm64 },
1139 { "lm96163", lm96163 },
1140 { }
1141};
1142MODULE_DEVICE_TABLE(i2c, lm63_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Guenter Roeck8c71c7b2019-04-04 07:59:01 -07001144static const struct of_device_id __maybe_unused lm63_of_match[] = {
Javier Martinez Canillasb25f6632017-02-24 10:13:01 -03001145 {
1146 .compatible = "national,lm63",
1147 .data = (void *)lm63
1148 },
1149 {
1150 .compatible = "national,lm64",
1151 .data = (void *)lm64
1152 },
1153 {
1154 .compatible = "national,lm96163",
1155 .data = (void *)lm96163
1156 },
1157 { },
1158};
1159MODULE_DEVICE_TABLE(of, lm63_of_match);
1160
Jean Delvaredac27dc2012-03-23 10:02:18 +01001161static struct i2c_driver lm63_driver = {
1162 .class = I2C_CLASS_HWMON,
1163 .driver = {
1164 .name = "lm63",
Javier Martinez Canillasb25f6632017-02-24 10:13:01 -03001165 .of_match_table = of_match_ptr(lm63_of_match),
Jean Delvaredac27dc2012-03-23 10:02:18 +01001166 },
Stephen Kitt67487032020-08-13 18:02:22 +02001167 .probe_new = lm63_probe,
Jean Delvaredac27dc2012-03-23 10:02:18 +01001168 .id_table = lm63_id,
1169 .detect = lm63_detect,
1170 .address_list = normal_i2c,
1171};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Axel Linf0967ee2012-01-20 15:38:18 +08001173module_i2c_driver(lm63_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Jean Delvare7c81c60f2014-01-29 20:40:08 +01001175MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176MODULE_DESCRIPTION("LM63 driver");
1177MODULE_LICENSE("GPL");