Blame view

drivers/hwmon/lm95234.c 20.9 KB
e1eb49063   Guenter Roeck   hwmon: Add driver...
1
2
3
  /*
   * Driver for Texas Instruments / National Semiconductor LM95234
   *
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
4
   * Copyright (c) 2013, 2014 Guenter Roeck <linux@roeck-us.net>
e1eb49063   Guenter Roeck   hwmon: Add driver...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   *
   * Derived from lm95241.c
   * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   */
  
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/slab.h>
  #include <linux/jiffies.h>
  #include <linux/i2c.h>
  #include <linux/hwmon.h>
  #include <linux/hwmon-sysfs.h>
  #include <linux/err.h>
  #include <linux/mutex.h>
  #include <linux/sysfs.h>
  
  #define DRVNAME "lm95234"
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
32
33
34
35
  enum chips { lm95233, lm95234 };
  
  static const unsigned short normal_i2c[] = {
  	0x18, 0x2a, 0x2b, 0x4d, 0x4e, I2C_CLIENT_END };
e1eb49063   Guenter Roeck   hwmon: Add driver...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  
  /* LM95234 registers */
  #define LM95234_REG_MAN_ID		0xFE
  #define LM95234_REG_CHIP_ID		0xFF
  #define LM95234_REG_STATUS		0x02
  #define LM95234_REG_CONFIG		0x03
  #define LM95234_REG_CONVRATE		0x04
  #define LM95234_REG_STS_FAULT		0x07
  #define LM95234_REG_STS_TCRIT1		0x08
  #define LM95234_REG_STS_TCRIT2		0x09
  #define LM95234_REG_TEMPH(x)		((x) + 0x10)
  #define LM95234_REG_TEMPL(x)		((x) + 0x20)
  #define LM95234_REG_UTEMPH(x)		((x) + 0x19)	/* Remote only */
  #define LM95234_REG_UTEMPL(x)		((x) + 0x29)
  #define LM95234_REG_REM_MODEL		0x30
  #define LM95234_REG_REM_MODEL_STS	0x38
  #define LM95234_REG_OFFSET(x)		((x) + 0x31)	/* Remote only */
  #define LM95234_REG_TCRIT1(x)		((x) + 0x40)
  #define LM95234_REG_TCRIT2(x)		((x) + 0x49)	/* Remote channel 1,2 */
  #define LM95234_REG_TCRIT_HYST		0x5a
  
  #define NATSEMI_MAN_ID			0x01
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
58
  #define LM95233_CHIP_ID			0x89
e1eb49063   Guenter Roeck   hwmon: Add driver...
59
60
61
62
  #define LM95234_CHIP_ID			0x79
  
  /* Client data (each client gets its own) */
  struct lm95234_data {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
63
  	struct i2c_client *client;
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
64
  	const struct attribute_group *groups[3];
e1eb49063   Guenter Roeck   hwmon: Add driver...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  	struct mutex update_lock;
  	unsigned long last_updated, interval;	/* in jiffies */
  	bool valid;		/* false until following fields are valid */
  	/* registers values */
  	int temp[5];		/* temperature (signed) */
  	u32 status;		/* fault/alarm status */
  	u8 tcrit1[5];		/* critical temperature limit */
  	u8 tcrit2[2];		/* high temperature limit */
  	s8 toffset[4];		/* remote temperature offset */
  	u8 thyst;		/* common hysteresis */
  
  	u8 sensor_type;		/* temperature sensor type */
  };
  
  static int lm95234_read_temp(struct i2c_client *client, int index, int *t)
  {
  	int val;
  	u16 temp = 0;
  
  	if (index) {
  		val = i2c_smbus_read_byte_data(client,
  					       LM95234_REG_UTEMPH(index - 1));
  		if (val < 0)
  			return val;
  		temp = val << 8;
  		val = i2c_smbus_read_byte_data(client,
  					       LM95234_REG_UTEMPL(index - 1));
  		if (val < 0)
  			return val;
  		temp |= val;
  		*t = temp;
  	}
  	/*
  	 * Read signed temperature if unsigned temperature is 0,
  	 * or if this is the local sensor.
  	 */
  	if (!temp) {
  		val = i2c_smbus_read_byte_data(client,
  					       LM95234_REG_TEMPH(index));
  		if (val < 0)
  			return val;
  		temp = val << 8;
  		val = i2c_smbus_read_byte_data(client,
  					       LM95234_REG_TEMPL(index));
  		if (val < 0)
  			return val;
  		temp |= val;
  		*t = (s16)temp;
  	}
  	return 0;
  }
  
  static u16 update_intervals[] = { 143, 364, 1000, 2500 };
  
  /* Fill value cache. Must be called with update lock held. */
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
120
121
  static int lm95234_fill_cache(struct lm95234_data *data,
  			      struct i2c_client *client)
e1eb49063   Guenter Roeck   hwmon: Add driver...
122
  {
e1eb49063   Guenter Roeck   hwmon: Add driver...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  	int i, ret;
  
  	ret = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
  	if (ret < 0)
  		return ret;
  
  	data->interval = msecs_to_jiffies(update_intervals[ret & 0x03]);
  
  	for (i = 0; i < ARRAY_SIZE(data->tcrit1); i++) {
  		ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT1(i));
  		if (ret < 0)
  			return ret;
  		data->tcrit1[i] = ret;
  	}
  	for (i = 0; i < ARRAY_SIZE(data->tcrit2); i++) {
  		ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT2(i));
  		if (ret < 0)
  			return ret;
  		data->tcrit2[i] = ret;
  	}
  	for (i = 0; i < ARRAY_SIZE(data->toffset); i++) {
  		ret = i2c_smbus_read_byte_data(client, LM95234_REG_OFFSET(i));
  		if (ret < 0)
  			return ret;
  		data->toffset[i] = ret;
  	}
  
  	ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT_HYST);
  	if (ret < 0)
  		return ret;
  	data->thyst = ret;
  
  	ret = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
  	if (ret < 0)
  		return ret;
  	data->sensor_type = ret;
  
  	return 0;
  }
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
162
  static int lm95234_update_device(struct lm95234_data *data)
e1eb49063   Guenter Roeck   hwmon: Add driver...
163
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
164
  	struct i2c_client *client = data->client;
e1eb49063   Guenter Roeck   hwmon: Add driver...
165
166
167
168
169
170
171
172
173
  	int ret;
  
  	mutex_lock(&data->update_lock);
  
  	if (time_after(jiffies, data->last_updated + data->interval) ||
  	    !data->valid) {
  		int i;
  
  		if (!data->valid) {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
174
  			ret = lm95234_fill_cache(data, client);
e1eb49063   Guenter Roeck   hwmon: Add driver...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
  			if (ret < 0)
  				goto abort;
  		}
  
  		data->valid = false;
  		for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  			ret = lm95234_read_temp(client, i, &data->temp[i]);
  			if (ret < 0)
  				goto abort;
  		}
  
  		ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_FAULT);
  		if (ret < 0)
  			goto abort;
  		data->status = ret;
  
  		ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT1);
  		if (ret < 0)
  			goto abort;
  		data->status |= ret << 8;
  
  		ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT2);
  		if (ret < 0)
  			goto abort;
  		data->status |= ret << 16;
  
  		data->last_updated = jiffies;
  		data->valid = true;
  	}
  	ret = 0;
  abort:
  	mutex_unlock(&data->update_lock);
  
  	return ret;
  }
  
  static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  			 char *buf)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
214
  	struct lm95234_data *data = dev_get_drvdata(dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
215
  	int index = to_sensor_dev_attr(attr)->index;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
216
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
217
218
219
220
221
222
223
224
225
226
227
228
  
  	if (ret)
  		return ret;
  
  	return sprintf(buf, "%d
  ",
  		       DIV_ROUND_CLOSEST(data->temp[index] * 125, 32));
  }
  
  static ssize_t show_alarm(struct device *dev,
  			  struct device_attribute *attr, char *buf)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
229
  	struct lm95234_data *data = dev_get_drvdata(dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
230
  	u32 mask = to_sensor_dev_attr(attr)->index;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
231
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
232
233
234
235
236
237
238
239
240
241
  
  	if (ret)
  		return ret;
  
  	return sprintf(buf, "%u", !!(data->status & mask));
  }
  
  static ssize_t show_type(struct device *dev, struct device_attribute *attr,
  			 char *buf)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
242
  	struct lm95234_data *data = dev_get_drvdata(dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
243
  	u8 mask = to_sensor_dev_attr(attr)->index;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
244
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
245
246
247
248
249
250
251
252
253
254
255
256
  
  	if (ret)
  		return ret;
  
  	return sprintf(buf, data->sensor_type & mask ? "1
  " : "2
  ");
  }
  
  static ssize_t set_type(struct device *dev, struct device_attribute *attr,
  			const char *buf, size_t count)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
257
  	struct lm95234_data *data = dev_get_drvdata(dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
258
259
  	unsigned long val;
  	u8 mask = to_sensor_dev_attr(attr)->index;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
260
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  
  	if (ret)
  		return ret;
  
  	ret = kstrtoul(buf, 10, &val);
  	if (ret < 0)
  		return ret;
  
  	if (val != 1 && val != 2)
  		return -EINVAL;
  
  	mutex_lock(&data->update_lock);
  	if (val == 1)
  		data->sensor_type |= mask;
  	else
  		data->sensor_type &= ~mask;
  	data->valid = false;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
278
  	i2c_smbus_write_byte_data(data->client, LM95234_REG_REM_MODEL,
e1eb49063   Guenter Roeck   hwmon: Add driver...
279
280
281
282
283
284
285
286
287
  				  data->sensor_type);
  	mutex_unlock(&data->update_lock);
  
  	return count;
  }
  
  static ssize_t show_tcrit2(struct device *dev, struct device_attribute *attr,
  			   char *buf)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
288
  	struct lm95234_data *data = dev_get_drvdata(dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
289
  	int index = to_sensor_dev_attr(attr)->index;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
290
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
291
292
293
294
295
296
297
298
299
300
  
  	if (ret)
  		return ret;
  
  	return sprintf(buf, "%u", data->tcrit2[index] * 1000);
  }
  
  static ssize_t set_tcrit2(struct device *dev, struct device_attribute *attr,
  			  const char *buf, size_t count)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
301
  	struct lm95234_data *data = dev_get_drvdata(dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
302
303
  	int index = to_sensor_dev_attr(attr)->index;
  	long val;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
304
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
305
306
307
308
309
310
311
312
313
314
315
316
  
  	if (ret)
  		return ret;
  
  	ret = kstrtol(buf, 10, &val);
  	if (ret < 0)
  		return ret;
  
  	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, index ? 255 : 127);
  
  	mutex_lock(&data->update_lock);
  	data->tcrit2[index] = val;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
317
  	i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT2(index), val);
e1eb49063   Guenter Roeck   hwmon: Add driver...
318
319
320
321
322
323
324
325
  	mutex_unlock(&data->update_lock);
  
  	return count;
  }
  
  static ssize_t show_tcrit2_hyst(struct device *dev,
  				struct device_attribute *attr, char *buf)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
326
  	struct lm95234_data *data = dev_get_drvdata(dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
327
  	int index = to_sensor_dev_attr(attr)->index;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
328
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
329
330
331
332
333
334
335
336
337
338
339
340
  
  	if (ret)
  		return ret;
  
  	/* Result can be negative, so be careful with unsigned operands */
  	return sprintf(buf, "%d",
  		       ((int)data->tcrit2[index] - (int)data->thyst) * 1000);
  }
  
  static ssize_t show_tcrit1(struct device *dev, struct device_attribute *attr,
  			   char *buf)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
341
  	struct lm95234_data *data = dev_get_drvdata(dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
342
343
344
345
346
347
348
349
  	int index = to_sensor_dev_attr(attr)->index;
  
  	return sprintf(buf, "%u", data->tcrit1[index] * 1000);
  }
  
  static ssize_t set_tcrit1(struct device *dev, struct device_attribute *attr,
  			  const char *buf, size_t count)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
350
  	struct lm95234_data *data = dev_get_drvdata(dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
351
  	int index = to_sensor_dev_attr(attr)->index;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
352
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
353
  	long val;
e1eb49063   Guenter Roeck   hwmon: Add driver...
354
355
356
357
358
359
360
361
362
363
364
365
  
  	if (ret)
  		return ret;
  
  	ret = kstrtol(buf, 10, &val);
  	if (ret < 0)
  		return ret;
  
  	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
  
  	mutex_lock(&data->update_lock);
  	data->tcrit1[index] = val;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
366
  	i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT1(index), val);
e1eb49063   Guenter Roeck   hwmon: Add driver...
367
368
369
370
371
372
373
374
  	mutex_unlock(&data->update_lock);
  
  	return count;
  }
  
  static ssize_t show_tcrit1_hyst(struct device *dev,
  				struct device_attribute *attr, char *buf)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
375
  	struct lm95234_data *data = dev_get_drvdata(dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
376
  	int index = to_sensor_dev_attr(attr)->index;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
377
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
378
379
380
381
382
383
384
385
386
387
388
389
390
  
  	if (ret)
  		return ret;
  
  	/* Result can be negative, so be careful with unsigned operands */
  	return sprintf(buf, "%d",
  		       ((int)data->tcrit1[index] - (int)data->thyst) * 1000);
  }
  
  static ssize_t set_tcrit1_hyst(struct device *dev,
  			       struct device_attribute *attr,
  			       const char *buf, size_t count)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
391
  	struct lm95234_data *data = dev_get_drvdata(dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
392
  	int index = to_sensor_dev_attr(attr)->index;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
393
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
394
  	long val;
e1eb49063   Guenter Roeck   hwmon: Add driver...
395
396
397
398
399
400
401
402
403
404
405
406
407
  
  	if (ret)
  		return ret;
  
  	ret = kstrtol(buf, 10, &val);
  	if (ret < 0)
  		return ret;
  
  	val = DIV_ROUND_CLOSEST(val, 1000);
  	val = clamp_val((int)data->tcrit1[index] - val, 0, 31);
  
  	mutex_lock(&data->update_lock);
  	data->thyst = val;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
408
  	i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT_HYST, val);
e1eb49063   Guenter Roeck   hwmon: Add driver...
409
410
411
412
413
414
415
416
  	mutex_unlock(&data->update_lock);
  
  	return count;
  }
  
  static ssize_t show_offset(struct device *dev, struct device_attribute *attr,
  			   char *buf)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
417
  	struct lm95234_data *data = dev_get_drvdata(dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
418
  	int index = to_sensor_dev_attr(attr)->index;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
419
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
420
421
422
423
424
425
426
427
428
429
  
  	if (ret)
  		return ret;
  
  	return sprintf(buf, "%d", data->toffset[index] * 500);
  }
  
  static ssize_t set_offset(struct device *dev, struct device_attribute *attr,
  			  const char *buf, size_t count)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
430
  	struct lm95234_data *data = dev_get_drvdata(dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
431
  	int index = to_sensor_dev_attr(attr)->index;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
432
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
433
  	long val;
e1eb49063   Guenter Roeck   hwmon: Add driver...
434
435
436
437
438
439
440
441
442
443
444
445
446
  
  	if (ret)
  		return ret;
  
  	ret = kstrtol(buf, 10, &val);
  	if (ret < 0)
  		return ret;
  
  	/* Accuracy is 1/2 degrees C */
  	val = clamp_val(DIV_ROUND_CLOSEST(val, 500), -128, 127);
  
  	mutex_lock(&data->update_lock);
  	data->toffset[index] = val;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
447
  	i2c_smbus_write_byte_data(data->client, LM95234_REG_OFFSET(index), val);
e1eb49063   Guenter Roeck   hwmon: Add driver...
448
449
450
451
452
453
454
455
  	mutex_unlock(&data->update_lock);
  
  	return count;
  }
  
  static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
  			     char *buf)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
456
457
  	struct lm95234_data *data = dev_get_drvdata(dev);
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
458
459
460
461
462
463
464
465
466
467
468
469
  
  	if (ret)
  		return ret;
  
  	return sprintf(buf, "%lu
  ",
  		       DIV_ROUND_CLOSEST(data->interval * 1000, HZ));
  }
  
  static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
  			    const char *buf, size_t count)
  {
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
470
471
  	struct lm95234_data *data = dev_get_drvdata(dev);
  	int ret = lm95234_update_device(data);
e1eb49063   Guenter Roeck   hwmon: Add driver...
472
473
  	unsigned long val;
  	u8 regval;
e1eb49063   Guenter Roeck   hwmon: Add driver...
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
  
  	if (ret)
  		return ret;
  
  	ret = kstrtoul(buf, 10, &val);
  	if (ret < 0)
  		return ret;
  
  	for (regval = 0; regval < 3; regval++) {
  		if (val <= update_intervals[regval])
  			break;
  	}
  
  	mutex_lock(&data->update_lock);
  	data->interval = msecs_to_jiffies(update_intervals[regval]);
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
489
  	i2c_smbus_write_byte_data(data->client, LM95234_REG_CONVRATE, regval);
e1eb49063   Guenter Roeck   hwmon: Add driver...
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
  	mutex_unlock(&data->update_lock);
  
  	return count;
  }
  
  static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
  static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
  
  static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL,
  			  BIT(0) | BIT(1));
  static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL,
  			  BIT(2) | BIT(3));
  static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL,
  			  BIT(4) | BIT(5));
  static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_alarm, NULL,
  			  BIT(6) | BIT(7));
  
  static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type,
  			  BIT(1));
  static SENSOR_DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type, set_type,
  			  BIT(2));
  static SENSOR_DEVICE_ATTR(temp4_type, S_IWUSR | S_IRUGO, show_type, set_type,
  			  BIT(3));
  static SENSOR_DEVICE_ATTR(temp5_type, S_IWUSR | S_IRUGO, show_type, set_type,
  			  BIT(4));
  
  static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_tcrit1,
  			  set_tcrit1, 0);
  static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_tcrit2,
  			  set_tcrit2, 0);
  static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_tcrit2,
  			  set_tcrit2, 1);
  static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_tcrit1,
  			  set_tcrit1, 3);
  static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_tcrit1,
  			  set_tcrit1, 4);
  
  static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_tcrit1_hyst,
  			  set_tcrit1_hyst, 0);
  static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO, show_tcrit2_hyst, NULL, 0);
  static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO, show_tcrit2_hyst, NULL, 1);
  static SENSOR_DEVICE_ATTR(temp4_max_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 3);
  static SENSOR_DEVICE_ATTR(temp5_max_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 4);
  
  static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
  			  BIT(0 + 8));
  static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL,
  			  BIT(1 + 16));
  static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL,
  			  BIT(2 + 16));
  static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL,
  			  BIT(3 + 8));
  static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL,
  			  BIT(4 + 8));
  
  static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_tcrit1,
  			  set_tcrit1, 1);
  static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_tcrit1,
  			  set_tcrit1, 2);
  
  static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 1);
  static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 2);
  
  static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
  			  BIT(1 + 8));
  static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL,
  			  BIT(2 + 8));
  
  static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_offset,
  			  set_offset, 0);
  static SENSOR_DEVICE_ATTR(temp3_offset, S_IWUSR | S_IRUGO, show_offset,
  			  set_offset, 1);
  static SENSOR_DEVICE_ATTR(temp4_offset, S_IWUSR | S_IRUGO, show_offset,
  			  set_offset, 2);
  static SENSOR_DEVICE_ATTR(temp5_offset, S_IWUSR | S_IRUGO, show_offset,
  			  set_offset, 3);
  
  static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
  		   set_interval);
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
572
  static struct attribute *lm95234_common_attrs[] = {
e1eb49063   Guenter Roeck   hwmon: Add driver...
573
574
575
  	&sensor_dev_attr_temp1_input.dev_attr.attr,
  	&sensor_dev_attr_temp2_input.dev_attr.attr,
  	&sensor_dev_attr_temp3_input.dev_attr.attr,
e1eb49063   Guenter Roeck   hwmon: Add driver...
576
577
  	&sensor_dev_attr_temp2_fault.dev_attr.attr,
  	&sensor_dev_attr_temp3_fault.dev_attr.attr,
e1eb49063   Guenter Roeck   hwmon: Add driver...
578
579
  	&sensor_dev_attr_temp2_type.dev_attr.attr,
  	&sensor_dev_attr_temp3_type.dev_attr.attr,
e1eb49063   Guenter Roeck   hwmon: Add driver...
580
581
582
  	&sensor_dev_attr_temp1_max.dev_attr.attr,
  	&sensor_dev_attr_temp2_max.dev_attr.attr,
  	&sensor_dev_attr_temp3_max.dev_attr.attr,
e1eb49063   Guenter Roeck   hwmon: Add driver...
583
584
585
  	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  	&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  	&sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
e1eb49063   Guenter Roeck   hwmon: Add driver...
586
587
588
  	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
e1eb49063   Guenter Roeck   hwmon: Add driver...
589
590
591
592
593
594
595
596
  	&sensor_dev_attr_temp2_crit.dev_attr.attr,
  	&sensor_dev_attr_temp3_crit.dev_attr.attr,
  	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
  	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  	&sensor_dev_attr_temp2_offset.dev_attr.attr,
  	&sensor_dev_attr_temp3_offset.dev_attr.attr,
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
  	&dev_attr_update_interval.attr,
  	NULL
  };
  
  static const struct attribute_group lm95234_common_group = {
  	.attrs = lm95234_common_attrs,
  };
  
  static struct attribute *lm95234_attrs[] = {
  	&sensor_dev_attr_temp4_input.dev_attr.attr,
  	&sensor_dev_attr_temp5_input.dev_attr.attr,
  	&sensor_dev_attr_temp4_fault.dev_attr.attr,
  	&sensor_dev_attr_temp5_fault.dev_attr.attr,
  	&sensor_dev_attr_temp4_type.dev_attr.attr,
  	&sensor_dev_attr_temp5_type.dev_attr.attr,
  	&sensor_dev_attr_temp4_max.dev_attr.attr,
  	&sensor_dev_attr_temp5_max.dev_attr.attr,
  	&sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
  	&sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
  	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
  	&sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
e1eb49063   Guenter Roeck   hwmon: Add driver...
618
619
  	&sensor_dev_attr_temp4_offset.dev_attr.attr,
  	&sensor_dev_attr_temp5_offset.dev_attr.attr,
e1eb49063   Guenter Roeck   hwmon: Add driver...
620
621
  	NULL
  };
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
622
623
624
625
  
  static const struct attribute_group lm95234_group = {
  	.attrs = lm95234_attrs,
  };
e1eb49063   Guenter Roeck   hwmon: Add driver...
626
627
628
629
630
  
  static int lm95234_detect(struct i2c_client *client,
  			  struct i2c_board_info *info)
  {
  	struct i2c_adapter *adapter = client->adapter;
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
631
632
  	int address = client->addr;
  	u8 config_mask, model_mask;
e1eb49063   Guenter Roeck   hwmon: Add driver...
633
  	int mfg_id, chip_id, val;
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
634
  	const char *name;
e1eb49063   Guenter Roeck   hwmon: Add driver...
635
636
637
638
639
640
641
642
643
  
  	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  		return -ENODEV;
  
  	mfg_id = i2c_smbus_read_byte_data(client, LM95234_REG_MAN_ID);
  	if (mfg_id != NATSEMI_MAN_ID)
  		return -ENODEV;
  
  	chip_id = i2c_smbus_read_byte_data(client, LM95234_REG_CHIP_ID);
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
  	switch (chip_id) {
  	case LM95233_CHIP_ID:
  		if (address != 0x18 && address != 0x2a && address != 0x2b)
  			return -ENODEV;
  		config_mask = 0xbf;
  		model_mask = 0xf9;
  		name = "lm95233";
  		break;
  	case LM95234_CHIP_ID:
  		if (address != 0x18 && address != 0x4d && address != 0x4e)
  			return -ENODEV;
  		config_mask = 0xbc;
  		model_mask = 0xe1;
  		name = "lm95234";
  		break;
  	default:
e1eb49063   Guenter Roeck   hwmon: Add driver...
660
  		return -ENODEV;
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
661
  	}
e1eb49063   Guenter Roeck   hwmon: Add driver...
662
663
664
665
666
667
  
  	val = i2c_smbus_read_byte_data(client, LM95234_REG_STATUS);
  	if (val & 0x30)
  		return -ENODEV;
  
  	val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
668
  	if (val & config_mask)
e1eb49063   Guenter Roeck   hwmon: Add driver...
669
670
671
672
673
674
675
  		return -ENODEV;
  
  	val = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
  	if (val & 0xfc)
  		return -ENODEV;
  
  	val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
676
  	if (val & model_mask)
e1eb49063   Guenter Roeck   hwmon: Add driver...
677
678
679
  		return -ENODEV;
  
  	val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
680
  	if (val & model_mask)
e1eb49063   Guenter Roeck   hwmon: Add driver...
681
  		return -ENODEV;
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
682
  	strlcpy(info->type, name, I2C_NAME_SIZE);
e1eb49063   Guenter Roeck   hwmon: Add driver...
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
  	return 0;
  }
  
  static int lm95234_init_client(struct i2c_client *client)
  {
  	int val, model;
  
  	/* start conversion if necessary */
  	val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
  	if (val < 0)
  		return val;
  	if (val & 0x40)
  		i2c_smbus_write_byte_data(client, LM95234_REG_CONFIG,
  					  val & ~0x40);
  
  	/* If diode type status reports an error, try to fix it */
  	val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
  	if (val < 0)
  		return val;
  	model = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
  	if (model < 0)
  		return model;
  	if (model & val) {
  		dev_notice(&client->dev,
  			   "Fixing remote diode type misconfiguration (0x%x)
  ",
  			   val);
  		i2c_smbus_write_byte_data(client, LM95234_REG_REM_MODEL,
  					  model & ~val);
  	}
  	return 0;
  }
  
  static int lm95234_probe(struct i2c_client *client,
  			 const struct i2c_device_id *id)
  {
  	struct device *dev = &client->dev;
  	struct lm95234_data *data;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
721
  	struct device *hwmon_dev;
e1eb49063   Guenter Roeck   hwmon: Add driver...
722
723
724
725
726
  	int err;
  
  	data = devm_kzalloc(dev, sizeof(struct lm95234_data), GFP_KERNEL);
  	if (!data)
  		return -ENOMEM;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
727
  	data->client = client;
e1eb49063   Guenter Roeck   hwmon: Add driver...
728
729
730
731
732
733
  	mutex_init(&data->update_lock);
  
  	/* Initialize the LM95234 chip */
  	err = lm95234_init_client(client);
  	if (err < 0)
  		return err;
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
734
735
736
  	data->groups[0] = &lm95234_common_group;
  	if (id->driver_data == lm95234)
  		data->groups[1] = &lm95234_group;
9d86bd6ba   Guenter Roeck   hwmon: (lm95234) ...
737
  	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
738
  							   data, data->groups);
3ea85ba7a   Fengguang Wu   hwmon: (lm95234) ...
739
  	return PTR_ERR_OR_ZERO(hwmon_dev);
e1eb49063   Guenter Roeck   hwmon: Add driver...
740
741
742
743
  }
  
  /* Driver data (common to all clients) */
  static const struct i2c_device_id lm95234_id[] = {
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
744
745
  	{ "lm95233", lm95233 },
  	{ "lm95234", lm95234 },
e1eb49063   Guenter Roeck   hwmon: Add driver...
746
747
748
749
750
751
752
753
754
755
  	{ }
  };
  MODULE_DEVICE_TABLE(i2c, lm95234_id);
  
  static struct i2c_driver lm95234_driver = {
  	.class		= I2C_CLASS_HWMON,
  	.driver = {
  		.name	= DRVNAME,
  	},
  	.probe		= lm95234_probe,
e1eb49063   Guenter Roeck   hwmon: Add driver...
756
757
758
759
760
761
762
763
  	.id_table	= lm95234_id,
  	.detect		= lm95234_detect,
  	.address_list	= normal_i2c,
  };
  
  module_i2c_driver(lm95234_driver);
  
  MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
dfcd4c53b   Guenter Roeck   hwmon: (lm95234) ...
764
  MODULE_DESCRIPTION("LM95233/LM95234 sensor driver");
e1eb49063   Guenter Roeck   hwmon: Add driver...
765
  MODULE_LICENSE("GPL");