ina209.c (a976c2951d8f376112361830aa7762beff83a205) ina209.c (46dce7a19551470c25d27d69a56671da00a10545)
1/*
2 * Driver for the Texas Instruments / Burr Brown INA209
3 * Bidirectional Current/Power Monitor
4 *
5 * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net>
6 *
7 * Derived from Ira W. Snyder's original driver submission
8 * Copyright (C) 2008 Paul Hays <Paul.Hays@cattail.ca>

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

225 if (i < interval)
226 break;
227 adc--;
228 }
229 }
230 return (config & 0xf807) | (adc << 3) | (adc << 7);
231}
232
1/*
2 * Driver for the Texas Instruments / Burr Brown INA209
3 * Bidirectional Current/Power Monitor
4 *
5 * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net>
6 *
7 * Derived from Ira W. Snyder's original driver submission
8 * Copyright (C) 2008 Paul Hays <Paul.Hays@cattail.ca>

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

225 if (i < interval)
226 break;
227 adc--;
228 }
229 }
230 return (config & 0xf807) | (adc << 3) | (adc << 7);
231}
232
233static ssize_t ina209_set_interval(struct device *dev,
234 struct device_attribute *da,
235 const char *buf, size_t count)
233static ssize_t ina209_interval_store(struct device *dev,
234 struct device_attribute *da,
235 const char *buf, size_t count)
236{
237 struct ina209_data *data = ina209_update_device(dev);
238 long val;
239 u16 regval;
240 int ret;
241
242 if (IS_ERR(data))
243 return PTR_ERR(data);

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

252 i2c_smbus_write_word_swapped(data->client, INA209_CONFIGURATION,
253 regval);
254 data->regs[INA209_CONFIGURATION] = regval;
255 data->update_interval = ina209_interval_from_reg(regval);
256 mutex_unlock(&data->update_lock);
257 return count;
258}
259
236{
237 struct ina209_data *data = ina209_update_device(dev);
238 long val;
239 u16 regval;
240 int ret;
241
242 if (IS_ERR(data))
243 return PTR_ERR(data);

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

252 i2c_smbus_write_word_swapped(data->client, INA209_CONFIGURATION,
253 regval);
254 data->regs[INA209_CONFIGURATION] = regval;
255 data->update_interval = ina209_interval_from_reg(regval);
256 mutex_unlock(&data->update_lock);
257 return count;
258}
259
260static ssize_t ina209_show_interval(struct device *dev,
260static ssize_t ina209_interval_show(struct device *dev,
261 struct device_attribute *da, char *buf)
262{
263 struct ina209_data *data = dev_get_drvdata(dev);
264
265 return snprintf(buf, PAGE_SIZE, "%d\n", data->update_interval);
266}
267
268/*

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

274static u16 ina209_reset_history_regs[] = {
275 INA209_SHUNT_VOLTAGE_POS_PEAK,
276 INA209_SHUNT_VOLTAGE_NEG_PEAK,
277 INA209_BUS_VOLTAGE_MAX_PEAK,
278 INA209_BUS_VOLTAGE_MIN_PEAK,
279 INA209_POWER_PEAK
280};
281
261 struct device_attribute *da, char *buf)
262{
263 struct ina209_data *data = dev_get_drvdata(dev);
264
265 return snprintf(buf, PAGE_SIZE, "%d\n", data->update_interval);
266}
267
268/*

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

274static u16 ina209_reset_history_regs[] = {
275 INA209_SHUNT_VOLTAGE_POS_PEAK,
276 INA209_SHUNT_VOLTAGE_NEG_PEAK,
277 INA209_BUS_VOLTAGE_MAX_PEAK,
278 INA209_BUS_VOLTAGE_MIN_PEAK,
279 INA209_POWER_PEAK
280};
281
282static ssize_t ina209_reset_history(struct device *dev,
282static ssize_t ina209_history_store(struct device *dev,
283 struct device_attribute *da,
283 struct device_attribute *da,
284 const char *buf,
285 size_t count)
284 const char *buf, size_t count)
286{
287 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
288 struct ina209_data *data = dev_get_drvdata(dev);
289 struct i2c_client *client = data->client;
290 u32 mask = attr->index;
291 long val;
292 int i, ret;
293

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

301 i2c_smbus_write_word_swapped(client,
302 ina209_reset_history_regs[i], 1);
303 }
304 data->valid = false;
305 mutex_unlock(&data->update_lock);
306 return count;
307}
308
285{
286 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
287 struct ina209_data *data = dev_get_drvdata(dev);
288 struct i2c_client *client = data->client;
289 u32 mask = attr->index;
290 long val;
291 int i, ret;
292

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

300 i2c_smbus_write_word_swapped(client,
301 ina209_reset_history_regs[i], 1);
302 }
303 data->valid = false;
304 mutex_unlock(&data->update_lock);
305 return count;
306}
307
309static ssize_t ina209_set_value(struct device *dev,
310 struct device_attribute *da,
311 const char *buf,
312 size_t count)
308static ssize_t ina209_value_store(struct device *dev,
309 struct device_attribute *da,
310 const char *buf, size_t count)
313{
314 struct ina209_data *data = ina209_update_device(dev);
315 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
316 int reg = attr->index;
317 long val;
318 int ret;
319
320 if (IS_ERR(data))

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

332 }
333 i2c_smbus_write_word_swapped(data->client, reg, ret);
334 data->regs[reg] = ret;
335abort:
336 mutex_unlock(&data->update_lock);
337 return count;
338}
339
311{
312 struct ina209_data *data = ina209_update_device(dev);
313 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
314 int reg = attr->index;
315 long val;
316 int ret;
317
318 if (IS_ERR(data))

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

330 }
331 i2c_smbus_write_word_swapped(data->client, reg, ret);
332 data->regs[reg] = ret;
333abort:
334 mutex_unlock(&data->update_lock);
335 return count;
336}
337
340static ssize_t ina209_show_value(struct device *dev,
341 struct device_attribute *da,
342 char *buf)
338static ssize_t ina209_value_show(struct device *dev,
339 struct device_attribute *da, char *buf)
343{
344 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
345 struct ina209_data *data = ina209_update_device(dev);
346 long val;
347
348 if (IS_ERR(data))
349 return PTR_ERR(data);
350
351 val = ina209_from_reg(attr->index, data->regs[attr->index]);
352 return snprintf(buf, PAGE_SIZE, "%ld\n", val);
353}
354
340{
341 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
342 struct ina209_data *data = ina209_update_device(dev);
343 long val;
344
345 if (IS_ERR(data))
346 return PTR_ERR(data);
347
348 val = ina209_from_reg(attr->index, data->regs[attr->index]);
349 return snprintf(buf, PAGE_SIZE, "%ld\n", val);
350}
351
355static ssize_t ina209_show_alarm(struct device *dev,
356 struct device_attribute *da,
357 char *buf)
352static ssize_t ina209_alarm_show(struct device *dev,
353 struct device_attribute *da, char *buf)
358{
359 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
360 struct ina209_data *data = ina209_update_device(dev);
361 const unsigned int mask = attr->index;
362 u16 status;
363
364 if (IS_ERR(data))
365 return PTR_ERR(data);
366
367 status = data->regs[INA209_STATUS];
368
369 /*
370 * All alarms are in the INA209_STATUS register. To avoid a long
371 * switch statement, the mask is passed in attr->index
372 */
373 return snprintf(buf, PAGE_SIZE, "%u\n", !!(status & mask));
374}
375
376/* Shunt voltage, history, limits, alarms */
354{
355 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
356 struct ina209_data *data = ina209_update_device(dev);
357 const unsigned int mask = attr->index;
358 u16 status;
359
360 if (IS_ERR(data))
361 return PTR_ERR(data);
362
363 status = data->regs[INA209_STATUS];
364
365 /*
366 * All alarms are in the INA209_STATUS register. To avoid a long
367 * switch statement, the mask is passed in attr->index
368 */
369 return snprintf(buf, PAGE_SIZE, "%u\n", !!(status & mask));
370}
371
372/* Shunt voltage, history, limits, alarms */
377static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina209_show_value, NULL,
378 INA209_SHUNT_VOLTAGE);
379static SENSOR_DEVICE_ATTR(in0_input_highest, S_IRUGO, ina209_show_value, NULL,
380 INA209_SHUNT_VOLTAGE_POS_PEAK);
381static SENSOR_DEVICE_ATTR(in0_input_lowest, S_IRUGO, ina209_show_value, NULL,
382 INA209_SHUNT_VOLTAGE_NEG_PEAK);
383static SENSOR_DEVICE_ATTR(in0_reset_history, S_IWUSR, NULL,
384 ina209_reset_history, (1 << 0) | (1 << 1));
385static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR, ina209_show_value,
386 ina209_set_value, INA209_SHUNT_VOLTAGE_POS_WARN);
387static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR, ina209_show_value,
388 ina209_set_value, INA209_SHUNT_VOLTAGE_NEG_WARN);
389static SENSOR_DEVICE_ATTR(in0_crit_max, S_IRUGO | S_IWUSR, ina209_show_value,
390 ina209_set_value, INA209_CRITICAL_DAC_POS);
391static SENSOR_DEVICE_ATTR(in0_crit_min, S_IRUGO | S_IWUSR, ina209_show_value,
392 ina209_set_value, INA209_CRITICAL_DAC_NEG);
373static SENSOR_DEVICE_ATTR_RO(in0_input, ina209_value, INA209_SHUNT_VOLTAGE);
374static SENSOR_DEVICE_ATTR_RO(in0_input_highest, ina209_value,
375 INA209_SHUNT_VOLTAGE_POS_PEAK);
376static SENSOR_DEVICE_ATTR_RO(in0_input_lowest, ina209_value,
377 INA209_SHUNT_VOLTAGE_NEG_PEAK);
378static SENSOR_DEVICE_ATTR_WO(in0_reset_history, ina209_history,
379 (1 << 0) | (1 << 1));
380static SENSOR_DEVICE_ATTR_RW(in0_max, ina209_value,
381 INA209_SHUNT_VOLTAGE_POS_WARN);
382static SENSOR_DEVICE_ATTR_RW(in0_min, ina209_value,
383 INA209_SHUNT_VOLTAGE_NEG_WARN);
384static SENSOR_DEVICE_ATTR_RW(in0_crit_max, ina209_value,
385 INA209_CRITICAL_DAC_POS);
386static SENSOR_DEVICE_ATTR_RW(in0_crit_min, ina209_value,
387 INA209_CRITICAL_DAC_NEG);
393
388
394static SENSOR_DEVICE_ATTR(in0_min_alarm, S_IRUGO, ina209_show_alarm, NULL,
395 1 << 11);
396static SENSOR_DEVICE_ATTR(in0_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
397 1 << 12);
398static SENSOR_DEVICE_ATTR(in0_crit_min_alarm, S_IRUGO, ina209_show_alarm, NULL,
399 1 << 6);
400static SENSOR_DEVICE_ATTR(in0_crit_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
401 1 << 7);
389static SENSOR_DEVICE_ATTR_RO(in0_min_alarm, ina209_alarm, 1 << 11);
390static SENSOR_DEVICE_ATTR_RO(in0_max_alarm, ina209_alarm, 1 << 12);
391static SENSOR_DEVICE_ATTR_RO(in0_crit_min_alarm, ina209_alarm, 1 << 6);
392static SENSOR_DEVICE_ATTR_RO(in0_crit_max_alarm, ina209_alarm, 1 << 7);
402
403/* Bus voltage, history, limits, alarms */
393
394/* Bus voltage, history, limits, alarms */
404static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina209_show_value, NULL,
405 INA209_BUS_VOLTAGE);
406static SENSOR_DEVICE_ATTR(in1_input_highest, S_IRUGO, ina209_show_value, NULL,
407 INA209_BUS_VOLTAGE_MAX_PEAK);
408static SENSOR_DEVICE_ATTR(in1_input_lowest, S_IRUGO, ina209_show_value, NULL,
409 INA209_BUS_VOLTAGE_MIN_PEAK);
410static SENSOR_DEVICE_ATTR(in1_reset_history, S_IWUSR, NULL,
411 ina209_reset_history, (1 << 2) | (1 << 3));
412static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR, ina209_show_value,
413 ina209_set_value, INA209_BUS_VOLTAGE_OVER_WARN);
414static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR, ina209_show_value,
415 ina209_set_value, INA209_BUS_VOLTAGE_UNDER_WARN);
416static SENSOR_DEVICE_ATTR(in1_crit_max, S_IRUGO | S_IWUSR, ina209_show_value,
417 ina209_set_value, INA209_BUS_VOLTAGE_OVER_LIMIT);
418static SENSOR_DEVICE_ATTR(in1_crit_min, S_IRUGO | S_IWUSR, ina209_show_value,
419 ina209_set_value, INA209_BUS_VOLTAGE_UNDER_LIMIT);
395static SENSOR_DEVICE_ATTR_RO(in1_input, ina209_value, INA209_BUS_VOLTAGE);
396static SENSOR_DEVICE_ATTR_RO(in1_input_highest, ina209_value,
397 INA209_BUS_VOLTAGE_MAX_PEAK);
398static SENSOR_DEVICE_ATTR_RO(in1_input_lowest, ina209_value,
399 INA209_BUS_VOLTAGE_MIN_PEAK);
400static SENSOR_DEVICE_ATTR_WO(in1_reset_history, ina209_history,
401 (1 << 2) | (1 << 3));
402static SENSOR_DEVICE_ATTR_RW(in1_max, ina209_value,
403 INA209_BUS_VOLTAGE_OVER_WARN);
404static SENSOR_DEVICE_ATTR_RW(in1_min, ina209_value,
405 INA209_BUS_VOLTAGE_UNDER_WARN);
406static SENSOR_DEVICE_ATTR_RW(in1_crit_max, ina209_value,
407 INA209_BUS_VOLTAGE_OVER_LIMIT);
408static SENSOR_DEVICE_ATTR_RW(in1_crit_min, ina209_value,
409 INA209_BUS_VOLTAGE_UNDER_LIMIT);
420
410
421static SENSOR_DEVICE_ATTR(in1_min_alarm, S_IRUGO, ina209_show_alarm, NULL,
422 1 << 14);
423static SENSOR_DEVICE_ATTR(in1_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
424 1 << 15);
425static SENSOR_DEVICE_ATTR(in1_crit_min_alarm, S_IRUGO, ina209_show_alarm, NULL,
426 1 << 9);
427static SENSOR_DEVICE_ATTR(in1_crit_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
428 1 << 10);
411static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ina209_alarm, 1 << 14);
412static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ina209_alarm, 1 << 15);
413static SENSOR_DEVICE_ATTR_RO(in1_crit_min_alarm, ina209_alarm, 1 << 9);
414static SENSOR_DEVICE_ATTR_RO(in1_crit_max_alarm, ina209_alarm, 1 << 10);
429
430/* Power */
415
416/* Power */
431static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina209_show_value, NULL,
432 INA209_POWER);
433static SENSOR_DEVICE_ATTR(power1_input_highest, S_IRUGO, ina209_show_value,
434 NULL, INA209_POWER_PEAK);
435static SENSOR_DEVICE_ATTR(power1_reset_history, S_IWUSR, NULL,
436 ina209_reset_history, 1 << 4);
437static SENSOR_DEVICE_ATTR(power1_max, S_IRUGO | S_IWUSR, ina209_show_value,
438 ina209_set_value, INA209_POWER_WARN);
439static SENSOR_DEVICE_ATTR(power1_crit, S_IRUGO | S_IWUSR, ina209_show_value,
440 ina209_set_value, INA209_POWER_OVER_LIMIT);
417static SENSOR_DEVICE_ATTR_RO(power1_input, ina209_value, INA209_POWER);
418static SENSOR_DEVICE_ATTR_RO(power1_input_highest, ina209_value,
419 INA209_POWER_PEAK);
420static SENSOR_DEVICE_ATTR_WO(power1_reset_history, ina209_history, 1 << 4);
421static SENSOR_DEVICE_ATTR_RW(power1_max, ina209_value, INA209_POWER_WARN);
422static SENSOR_DEVICE_ATTR_RW(power1_crit, ina209_value,
423 INA209_POWER_OVER_LIMIT);
441
424
442static SENSOR_DEVICE_ATTR(power1_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
443 1 << 13);
444static SENSOR_DEVICE_ATTR(power1_crit_alarm, S_IRUGO, ina209_show_alarm, NULL,
445 1 << 8);
425static SENSOR_DEVICE_ATTR_RO(power1_max_alarm, ina209_alarm, 1 << 13);
426static SENSOR_DEVICE_ATTR_RO(power1_crit_alarm, ina209_alarm, 1 << 8);
446
447/* Current */
427
428/* Current */
448static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina209_show_value, NULL,
449 INA209_CURRENT);
429static SENSOR_DEVICE_ATTR_RO(curr1_input, ina209_value, INA209_CURRENT);
450
430
451static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR,
452 ina209_show_interval, ina209_set_interval, 0);
431static SENSOR_DEVICE_ATTR_RW(update_interval, ina209_interval, 0);
453
454/*
455 * Finally, construct an array of pointers to members of the above objects,
456 * as required for sysfs_create_group()
457 */
458static struct attribute *ina209_attrs[] = {
459 &sensor_dev_attr_in0_input.dev_attr.attr,
460 &sensor_dev_attr_in0_input_highest.dev_attr.attr,

--- 173 unchanged lines hidden ---
432
433/*
434 * Finally, construct an array of pointers to members of the above objects,
435 * as required for sysfs_create_group()
436 */
437static struct attribute *ina209_attrs[] = {
438 &sensor_dev_attr_in0_input.dev_attr.attr,
439 &sensor_dev_attr_in0_input_highest.dev_attr.attr,

--- 173 unchanged lines hidden ---