lm95241.c (cf9ce948f47640797bd19980e1d99c6d17d0bdc3) lm95241.c (f809621e8c36c15e9caf248dabe441ac25507e62)
1/*
2 * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com>
3 *
4 * The LM95241 is a sensor chip made by National Semiconductors.
5 * It reports up to three temperatures (its own plus up to two external ones).
6 * Complete datasheet can be obtained from National's website at:
7 * http://www.national.com/ds.cgi/LM/LM95241.pdf
8 *

--- 75 unchanged lines hidden (view full) ---

84 LM95241_REG_R_REMOTE1_TEMPH,
85 LM95241_REG_R_REMOTE1_TEMPL,
86 LM95241_REG_R_REMOTE2_TEMPH,
87 LM95241_REG_R_REMOTE2_TEMPL
88};
89
90/* Client data (each client gets its own) */
91struct lm95241_data {
1/*
2 * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com>
3 *
4 * The LM95241 is a sensor chip made by National Semiconductors.
5 * It reports up to three temperatures (its own plus up to two external ones).
6 * Complete datasheet can be obtained from National's website at:
7 * http://www.national.com/ds.cgi/LM/LM95241.pdf
8 *

--- 75 unchanged lines hidden (view full) ---

84 LM95241_REG_R_REMOTE1_TEMPH,
85 LM95241_REG_R_REMOTE1_TEMPL,
86 LM95241_REG_R_REMOTE2_TEMPH,
87 LM95241_REG_R_REMOTE2_TEMPL
88};
89
90/* Client data (each client gets its own) */
91struct lm95241_data {
92 struct device *hwmon_dev;
92 struct i2c_client *client;
93 struct mutex update_lock;
94 unsigned long last_updated, interval; /* in jiffies */
95 char valid; /* zero until following fields are valid */
96 /* registers values */
97 u8 temp[ARRAY_SIZE(lm95241_reg_address)];
98 u8 config, model, trutherm;
99};
100

--- 7 unchanged lines hidden (view full) ---

108static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
109{
110 u16 val_hl = (val_h << 8) | val_l;
111 return val_hl * 1000 / 256;
112}
113
114static struct lm95241_data *lm95241_update_device(struct device *dev)
115{
93 struct mutex update_lock;
94 unsigned long last_updated, interval; /* in jiffies */
95 char valid; /* zero until following fields are valid */
96 /* registers values */
97 u8 temp[ARRAY_SIZE(lm95241_reg_address)];
98 u8 config, model, trutherm;
99};
100

--- 7 unchanged lines hidden (view full) ---

108static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
109{
110 u16 val_hl = (val_h << 8) | val_l;
111 return val_hl * 1000 / 256;
112}
113
114static struct lm95241_data *lm95241_update_device(struct device *dev)
115{
116 struct i2c_client *client = to_i2c_client(dev);
117 struct lm95241_data *data = i2c_get_clientdata(client);
116 struct lm95241_data *data = dev_get_drvdata(dev);
117 struct i2c_client *client = data->client;
118
119 mutex_lock(&data->update_lock);
120
121 if (time_after(jiffies, data->last_updated + data->interval) ||
122 !data->valid) {
123 int i;
124
118
119 mutex_lock(&data->update_lock);
120
121 if (time_after(jiffies, data->last_updated + data->interval) ||
122 !data->valid) {
123 int i;
124
125 dev_dbg(&client->dev, "Updating lm95241 data.\n");
125 dev_dbg(dev, "Updating lm95241 data.\n");
126 for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++)
127 data->temp[i]
128 = i2c_smbus_read_byte_data(client,
129 lm95241_reg_address[i]);
130 data->last_updated = jiffies;
131 data->valid = 1;
132 }
133

--- 14 unchanged lines hidden (view full) ---

148 temp_from_reg_signed(data->temp[index], data->temp[index + 1]) :
149 temp_from_reg_unsigned(data->temp[index],
150 data->temp[index + 1]));
151}
152
153static ssize_t show_type(struct device *dev, struct device_attribute *attr,
154 char *buf)
155{
126 for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++)
127 data->temp[i]
128 = i2c_smbus_read_byte_data(client,
129 lm95241_reg_address[i]);
130 data->last_updated = jiffies;
131 data->valid = 1;
132 }
133

--- 14 unchanged lines hidden (view full) ---

148 temp_from_reg_signed(data->temp[index], data->temp[index + 1]) :
149 temp_from_reg_unsigned(data->temp[index],
150 data->temp[index + 1]));
151}
152
153static ssize_t show_type(struct device *dev, struct device_attribute *attr,
154 char *buf)
155{
156 struct i2c_client *client = to_i2c_client(dev);
157 struct lm95241_data *data = i2c_get_clientdata(client);
156 struct lm95241_data *data = dev_get_drvdata(dev);
158
159 return snprintf(buf, PAGE_SIZE - 1,
160 data->model & to_sensor_dev_attr(attr)->index ? "1\n" : "2\n");
161}
162
163static ssize_t set_type(struct device *dev, struct device_attribute *attr,
164 const char *buf, size_t count)
165{
157
158 return snprintf(buf, PAGE_SIZE - 1,
159 data->model & to_sensor_dev_attr(attr)->index ? "1\n" : "2\n");
160}
161
162static ssize_t set_type(struct device *dev, struct device_attribute *attr,
163 const char *buf, size_t count)
164{
166 struct i2c_client *client = to_i2c_client(dev);
167 struct lm95241_data *data = i2c_get_clientdata(client);
165 struct lm95241_data *data = dev_get_drvdata(dev);
166 struct i2c_client *client = data->client;
168 unsigned long val;
169 int shift;
170 u8 mask = to_sensor_dev_attr(attr)->index;
171
172 if (kstrtoul(buf, 10, &val) < 0)
173 return -EINVAL;
174 if (val != 1 && val != 2)
175 return -EINVAL;

--- 20 unchanged lines hidden (view full) ---

196 mutex_unlock(&data->update_lock);
197
198 return count;
199}
200
201static ssize_t show_min(struct device *dev, struct device_attribute *attr,
202 char *buf)
203{
167 unsigned long val;
168 int shift;
169 u8 mask = to_sensor_dev_attr(attr)->index;
170
171 if (kstrtoul(buf, 10, &val) < 0)
172 return -EINVAL;
173 if (val != 1 && val != 2)
174 return -EINVAL;

--- 20 unchanged lines hidden (view full) ---

195 mutex_unlock(&data->update_lock);
196
197 return count;
198}
199
200static ssize_t show_min(struct device *dev, struct device_attribute *attr,
201 char *buf)
202{
204 struct i2c_client *client = to_i2c_client(dev);
205 struct lm95241_data *data = i2c_get_clientdata(client);
203 struct lm95241_data *data = dev_get_drvdata(dev);
206
207 return snprintf(buf, PAGE_SIZE - 1,
208 data->config & to_sensor_dev_attr(attr)->index ?
209 "-127000\n" : "0\n");
210}
211
212static ssize_t set_min(struct device *dev, struct device_attribute *attr,
213 const char *buf, size_t count)
214{
204
205 return snprintf(buf, PAGE_SIZE - 1,
206 data->config & to_sensor_dev_attr(attr)->index ?
207 "-127000\n" : "0\n");
208}
209
210static ssize_t set_min(struct device *dev, struct device_attribute *attr,
211 const char *buf, size_t count)
212{
215 struct i2c_client *client = to_i2c_client(dev);
216 struct lm95241_data *data = i2c_get_clientdata(client);
213 struct lm95241_data *data = dev_get_drvdata(dev);
217 long val;
218
219 if (kstrtol(buf, 10, &val) < 0)
220 return -EINVAL;
221 if (val < -128000)
222 return -EINVAL;
223
224 mutex_lock(&data->update_lock);
225
226 if (val < 0)
227 data->config |= to_sensor_dev_attr(attr)->index;
228 else
229 data->config &= ~to_sensor_dev_attr(attr)->index;
230 data->valid = 0;
231
214 long val;
215
216 if (kstrtol(buf, 10, &val) < 0)
217 return -EINVAL;
218 if (val < -128000)
219 return -EINVAL;
220
221 mutex_lock(&data->update_lock);
222
223 if (val < 0)
224 data->config |= to_sensor_dev_attr(attr)->index;
225 else
226 data->config &= ~to_sensor_dev_attr(attr)->index;
227 data->valid = 0;
228
232 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
229 i2c_smbus_write_byte_data(data->client, LM95241_REG_RW_CONFIG,
230 data->config);
233
234 mutex_unlock(&data->update_lock);
235
236 return count;
237}
238
239static ssize_t show_max(struct device *dev, struct device_attribute *attr,
240 char *buf)
241{
231
232 mutex_unlock(&data->update_lock);
233
234 return count;
235}
236
237static ssize_t show_max(struct device *dev, struct device_attribute *attr,
238 char *buf)
239{
242 struct i2c_client *client = to_i2c_client(dev);
243 struct lm95241_data *data = i2c_get_clientdata(client);
240 struct lm95241_data *data = dev_get_drvdata(dev);
244
245 return snprintf(buf, PAGE_SIZE - 1,
246 data->config & to_sensor_dev_attr(attr)->index ?
247 "127000\n" : "255000\n");
248}
249
250static ssize_t set_max(struct device *dev, struct device_attribute *attr,
251 const char *buf, size_t count)
252{
241
242 return snprintf(buf, PAGE_SIZE - 1,
243 data->config & to_sensor_dev_attr(attr)->index ?
244 "127000\n" : "255000\n");
245}
246
247static ssize_t set_max(struct device *dev, struct device_attribute *attr,
248 const char *buf, size_t count)
249{
253 struct i2c_client *client = to_i2c_client(dev);
254 struct lm95241_data *data = i2c_get_clientdata(client);
250 struct lm95241_data *data = dev_get_drvdata(dev);
255 long val;
256
257 if (kstrtol(buf, 10, &val) < 0)
258 return -EINVAL;
259 if (val >= 256000)
260 return -EINVAL;
261
262 mutex_lock(&data->update_lock);
263
264 if (val <= 127000)
265 data->config |= to_sensor_dev_attr(attr)->index;
266 else
267 data->config &= ~to_sensor_dev_attr(attr)->index;
268 data->valid = 0;
269
251 long val;
252
253 if (kstrtol(buf, 10, &val) < 0)
254 return -EINVAL;
255 if (val >= 256000)
256 return -EINVAL;
257
258 mutex_lock(&data->update_lock);
259
260 if (val <= 127000)
261 data->config |= to_sensor_dev_attr(attr)->index;
262 else
263 data->config &= ~to_sensor_dev_attr(attr)->index;
264 data->valid = 0;
265
270 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
266 i2c_smbus_write_byte_data(data->client, LM95241_REG_RW_CONFIG,
267 data->config);
271
272 mutex_unlock(&data->update_lock);
273
274 return count;
275}
276
277static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
278 char *buf)
279{
280 struct lm95241_data *data = lm95241_update_device(dev);
281
282 return snprintf(buf, PAGE_SIZE - 1, "%lu\n", 1000 * data->interval
283 / HZ);
284}
285
286static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
287 const char *buf, size_t count)
288{
268
269 mutex_unlock(&data->update_lock);
270
271 return count;
272}
273
274static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
275 char *buf)
276{
277 struct lm95241_data *data = lm95241_update_device(dev);
278
279 return snprintf(buf, PAGE_SIZE - 1, "%lu\n", 1000 * data->interval
280 / HZ);
281}
282
283static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
284 const char *buf, size_t count)
285{
289 struct i2c_client *client = to_i2c_client(dev);
290 struct lm95241_data *data = i2c_get_clientdata(client);
286 struct lm95241_data *data = dev_get_drvdata(dev);
291 unsigned long val;
292
293 if (kstrtoul(buf, 10, &val) < 0)
294 return -EINVAL;
295
296 data->interval = val * HZ / 1000;
297
298 return count;

--- 12 unchanged lines hidden (view full) ---

311 R2DF_MASK);
312static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max, set_max,
313 R1DF_MASK);
314static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max, set_max,
315 R2DF_MASK);
316static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
317 set_interval);
318
287 unsigned long val;
288
289 if (kstrtoul(buf, 10, &val) < 0)
290 return -EINVAL;
291
292 data->interval = val * HZ / 1000;
293
294 return count;

--- 12 unchanged lines hidden (view full) ---

307 R2DF_MASK);
308static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max, set_max,
309 R1DF_MASK);
310static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max, set_max,
311 R2DF_MASK);
312static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
313 set_interval);
314
319static struct attribute *lm95241_attributes[] = {
315static struct attribute *lm95241_attrs[] = {
320 &sensor_dev_attr_temp1_input.dev_attr.attr,
321 &sensor_dev_attr_temp2_input.dev_attr.attr,
322 &sensor_dev_attr_temp3_input.dev_attr.attr,
323 &sensor_dev_attr_temp2_type.dev_attr.attr,
324 &sensor_dev_attr_temp3_type.dev_attr.attr,
325 &sensor_dev_attr_temp2_min.dev_attr.attr,
326 &sensor_dev_attr_temp3_min.dev_attr.attr,
327 &sensor_dev_attr_temp2_max.dev_attr.attr,
328 &sensor_dev_attr_temp3_max.dev_attr.attr,
329 &dev_attr_update_interval.attr,
330 NULL
331};
316 &sensor_dev_attr_temp1_input.dev_attr.attr,
317 &sensor_dev_attr_temp2_input.dev_attr.attr,
318 &sensor_dev_attr_temp3_input.dev_attr.attr,
319 &sensor_dev_attr_temp2_type.dev_attr.attr,
320 &sensor_dev_attr_temp3_type.dev_attr.attr,
321 &sensor_dev_attr_temp2_min.dev_attr.attr,
322 &sensor_dev_attr_temp3_min.dev_attr.attr,
323 &sensor_dev_attr_temp2_max.dev_attr.attr,
324 &sensor_dev_attr_temp3_max.dev_attr.attr,
325 &dev_attr_update_interval.attr,
326 NULL
327};
328ATTRIBUTE_GROUPS(lm95241);
332
329
333static const struct attribute_group lm95241_group = {
334 .attrs = lm95241_attributes,
335};
336
337/* Return 0 if detection is successful, -ENODEV otherwise */
338static int lm95241_detect(struct i2c_client *new_client,
339 struct i2c_board_info *info)
340{
341 struct i2c_adapter *adapter = new_client->adapter;
342 const char *name;
343 int mfg_id, chip_id;
344

--- 16 unchanged lines hidden (view full) ---

361 return -ENODEV;
362 }
363
364 /* Fill the i2c board info */
365 strlcpy(info->type, name, I2C_NAME_SIZE);
366 return 0;
367}
368
330/* Return 0 if detection is successful, -ENODEV otherwise */
331static int lm95241_detect(struct i2c_client *new_client,
332 struct i2c_board_info *info)
333{
334 struct i2c_adapter *adapter = new_client->adapter;
335 const char *name;
336 int mfg_id, chip_id;
337

--- 16 unchanged lines hidden (view full) ---

354 return -ENODEV;
355 }
356
357 /* Fill the i2c board info */
358 strlcpy(info->type, name, I2C_NAME_SIZE);
359 return 0;
360}
361
369static void lm95241_init_client(struct i2c_client *client)
362static void lm95241_init_client(struct i2c_client *client,
363 struct lm95241_data *data)
370{
364{
371 struct lm95241_data *data = i2c_get_clientdata(client);
372
373 data->interval = HZ; /* 1 sec default */
374 data->valid = 0;
375 data->config = CFG_CR0076;
376 data->model = 0;
377 data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
378
379 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
380 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
381 R1FE_MASK | R2FE_MASK);
382 i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
383 data->trutherm);
384 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
385 data->model);
386}
387
365 data->interval = HZ; /* 1 sec default */
366 data->valid = 0;
367 data->config = CFG_CR0076;
368 data->model = 0;
369 data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
370
371 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
372 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
373 R1FE_MASK | R2FE_MASK);
374 i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
375 data->trutherm);
376 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
377 data->model);
378}
379
388static int lm95241_probe(struct i2c_client *new_client,
380static int lm95241_probe(struct i2c_client *client,
389 const struct i2c_device_id *id)
390{
381 const struct i2c_device_id *id)
382{
383 struct device *dev = &client->dev;
391 struct lm95241_data *data;
384 struct lm95241_data *data;
392 int err;
385 struct device *hwmon_dev;
393
386
394 data = devm_kzalloc(&new_client->dev, sizeof(struct lm95241_data),
395 GFP_KERNEL);
387 data = devm_kzalloc(dev, sizeof(struct lm95241_data), GFP_KERNEL);
396 if (!data)
397 return -ENOMEM;
398
388 if (!data)
389 return -ENOMEM;
390
399 i2c_set_clientdata(new_client, data);
391 data->client = client;
400 mutex_init(&data->update_lock);
401
402 /* Initialize the LM95241 chip */
392 mutex_init(&data->update_lock);
393
394 /* Initialize the LM95241 chip */
403 lm95241_init_client(new_client);
395 lm95241_init_client(client, data);
404
396
405 /* Register sysfs hooks */
406 err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group);
407 if (err)
408 return err;
409
410 data->hwmon_dev = hwmon_device_register(&new_client->dev);
411 if (IS_ERR(data->hwmon_dev)) {
412 err = PTR_ERR(data->hwmon_dev);
413 goto exit_remove_files;
414 }
415
416 return 0;
417
418exit_remove_files:
419 sysfs_remove_group(&new_client->dev.kobj, &lm95241_group);
420 return err;
397 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
398 data,
399 lm95241_groups);
400 return PTR_ERR_OR_ZERO(hwmon_dev);
421}
422
401}
402
423static int lm95241_remove(struct i2c_client *client)
424{
425 struct lm95241_data *data = i2c_get_clientdata(client);
426
427 hwmon_device_unregister(data->hwmon_dev);
428 sysfs_remove_group(&client->dev.kobj, &lm95241_group);
429
430 return 0;
431}
432
433/* Driver data (common to all clients) */
434static const struct i2c_device_id lm95241_id[] = {
435 { "lm95231", 0 },
436 { "lm95241", 0 },
437 { }
438};
439MODULE_DEVICE_TABLE(i2c, lm95241_id);
440
441static struct i2c_driver lm95241_driver = {
442 .class = I2C_CLASS_HWMON,
443 .driver = {
444 .name = DEVNAME,
445 },
446 .probe = lm95241_probe,
403/* Driver data (common to all clients) */
404static const struct i2c_device_id lm95241_id[] = {
405 { "lm95231", 0 },
406 { "lm95241", 0 },
407 { }
408};
409MODULE_DEVICE_TABLE(i2c, lm95241_id);
410
411static struct i2c_driver lm95241_driver = {
412 .class = I2C_CLASS_HWMON,
413 .driver = {
414 .name = DEVNAME,
415 },
416 .probe = lm95241_probe,
447 .remove = lm95241_remove,
448 .id_table = lm95241_id,
449 .detect = lm95241_detect,
450 .address_list = normal_i2c,
451};
452
453module_i2c_driver(lm95241_driver);
454
455MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>");
456MODULE_DESCRIPTION("LM95241 sensor driver");
457MODULE_LICENSE("GPL");
417 .id_table = lm95241_id,
418 .detect = lm95241_detect,
419 .address_list = normal_i2c,
420};
421
422module_i2c_driver(lm95241_driver);
423
424MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>");
425MODULE_DESCRIPTION("LM95241 sensor driver");
426MODULE_LICENSE("GPL");