2019-05-27 09:55:01 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-08-07 00:41:05 +04:00
|
|
|
/*
|
|
|
|
* An hwmon driver for the Analog Devices AD7414
|
|
|
|
*
|
|
|
|
* Copyright 2006 Stefan Roese <sr at denx.de>, DENX Software Engineering
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 PIKA Technologies
|
|
|
|
* Sean MacLennan <smaclennan@pikatech.com>
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Spansion Inc.
|
|
|
|
* Frank Edelhaeuser <frank.edelhaeuser at spansion.com>
|
|
|
|
* (converted to "new style" I2C driver model, removed checkpatch.pl warnings)
|
|
|
|
*
|
|
|
|
* Based on ad7418.c
|
|
|
|
* Copyright 2006 Tower Technologies, Alessandro Zummo <a.zummo at towertech.it>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.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>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
|
|
|
#include <linux/slab.h>
|
2008-08-07 00:41:05 +04:00
|
|
|
|
|
|
|
|
|
|
|
/* AD7414 registers */
|
|
|
|
#define AD7414_REG_TEMP 0x00
|
|
|
|
#define AD7414_REG_CONF 0x01
|
|
|
|
#define AD7414_REG_T_HIGH 0x02
|
|
|
|
#define AD7414_REG_T_LOW 0x03
|
|
|
|
|
|
|
|
static u8 AD7414_REG_LIMIT[] = { AD7414_REG_T_HIGH, AD7414_REG_T_LOW };
|
|
|
|
|
|
|
|
struct ad7414_data {
|
2014-07-01 18:31:44 +04:00
|
|
|
struct i2c_client *client;
|
2008-08-07 00:41:05 +04:00
|
|
|
struct mutex lock; /* atomic read data updates */
|
|
|
|
char valid; /* !=0 if following fields are valid */
|
|
|
|
unsigned long next_update; /* In jiffies */
|
|
|
|
s16 temp_input; /* Register values */
|
|
|
|
s8 temps[ARRAY_SIZE(AD7414_REG_LIMIT)];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* REG: (0.25C/bit, two's complement) << 6 */
|
|
|
|
static inline int ad7414_temp_from_reg(s16 reg)
|
|
|
|
{
|
2012-01-19 23:02:13 +04:00
|
|
|
/*
|
|
|
|
* use integer division instead of equivalent right shift to
|
2008-08-07 00:41:05 +04:00
|
|
|
* guarantee arithmetic shift and preserve the sign
|
|
|
|
*/
|
|
|
|
return ((int)reg / 64) * 250;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ad7414_read(struct i2c_client *client, u8 reg)
|
|
|
|
{
|
2011-11-04 15:00:47 +04:00
|
|
|
if (reg == AD7414_REG_TEMP)
|
|
|
|
return i2c_smbus_read_word_swapped(client, reg);
|
|
|
|
else
|
2008-08-07 00:41:05 +04:00
|
|
|
return i2c_smbus_read_byte_data(client, reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ad7414_write(struct i2c_client *client, u8 reg, u8 value)
|
|
|
|
{
|
|
|
|
return i2c_smbus_write_byte_data(client, reg, value);
|
|
|
|
}
|
|
|
|
|
2008-09-20 12:25:20 +04:00
|
|
|
static struct ad7414_data *ad7414_update_device(struct device *dev)
|
2008-08-07 00:41:05 +04:00
|
|
|
{
|
2014-07-01 18:31:44 +04:00
|
|
|
struct ad7414_data *data = dev_get_drvdata(dev);
|
|
|
|
struct i2c_client *client = data->client;
|
2008-08-07 00:41:05 +04:00
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
|
|
|
|
if (time_after(jiffies, data->next_update) || !data->valid) {
|
|
|
|
int value, i;
|
|
|
|
|
|
|
|
dev_dbg(&client->dev, "starting ad7414 update\n");
|
|
|
|
|
|
|
|
value = ad7414_read(client, AD7414_REG_TEMP);
|
|
|
|
if (value < 0)
|
|
|
|
dev_dbg(&client->dev, "AD7414_REG_TEMP err %d\n",
|
|
|
|
value);
|
|
|
|
else
|
|
|
|
data->temp_input = value;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(AD7414_REG_LIMIT); ++i) {
|
|
|
|
value = ad7414_read(client, AD7414_REG_LIMIT[i]);
|
|
|
|
if (value < 0)
|
|
|
|
dev_dbg(&client->dev, "AD7414 reg %d err %d\n",
|
|
|
|
AD7414_REG_LIMIT[i], value);
|
|
|
|
else
|
|
|
|
data->temps[i] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->next_update = jiffies + HZ + HZ / 2;
|
|
|
|
data->valid = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:01:59 +03:00
|
|
|
static ssize_t temp_input_show(struct device *dev,
|
2008-08-07 00:41:05 +04:00
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct ad7414_data *data = ad7414_update_device(dev);
|
|
|
|
return sprintf(buf, "%d\n", ad7414_temp_from_reg(data->temp_input));
|
|
|
|
}
|
2018-12-11 01:01:59 +03:00
|
|
|
static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
|
2008-08-07 00:41:05 +04:00
|
|
|
|
2018-12-11 01:01:59 +03:00
|
|
|
static ssize_t max_min_show(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf)
|
2008-08-07 00:41:05 +04:00
|
|
|
{
|
|
|
|
int index = to_sensor_dev_attr(attr)->index;
|
|
|
|
struct ad7414_data *data = ad7414_update_device(dev);
|
|
|
|
return sprintf(buf, "%d\n", data->temps[index] * 1000);
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:01:59 +03:00
|
|
|
static ssize_t max_min_store(struct device *dev,
|
|
|
|
struct device_attribute *attr, const char *buf,
|
|
|
|
size_t count)
|
2008-08-07 00:41:05 +04:00
|
|
|
{
|
2014-07-01 18:31:44 +04:00
|
|
|
struct ad7414_data *data = dev_get_drvdata(dev);
|
|
|
|
struct i2c_client *client = data->client;
|
2008-08-07 00:41:05 +04:00
|
|
|
int index = to_sensor_dev_attr(attr)->index;
|
|
|
|
u8 reg = AD7414_REG_LIMIT[index];
|
2012-01-08 22:34:04 +04:00
|
|
|
long temp;
|
|
|
|
int ret = kstrtol(buf, 10, &temp);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2008-08-07 00:41:05 +04:00
|
|
|
|
2013-01-09 20:09:34 +04:00
|
|
|
temp = clamp_val(temp, -40000, 85000);
|
2008-08-07 00:41:05 +04:00
|
|
|
temp = (temp + (temp < 0 ? -500 : 500)) / 1000;
|
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->temps[index] = temp;
|
|
|
|
ad7414_write(client, reg, temp);
|
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:01:59 +03:00
|
|
|
static SENSOR_DEVICE_ATTR_RW(temp1_max, max_min, 0);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(temp1_min, max_min, 1);
|
2008-08-07 00:41:05 +04:00
|
|
|
|
2018-12-11 01:01:59 +03:00
|
|
|
static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
|
2008-08-07 00:41:05 +04:00
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
int bitnr = to_sensor_dev_attr(attr)->index;
|
|
|
|
struct ad7414_data *data = ad7414_update_device(dev);
|
|
|
|
int value = (data->temp_input >> bitnr) & 1;
|
|
|
|
return sprintf(buf, "%d\n", value);
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:01:59 +03:00
|
|
|
static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 3);
|
|
|
|
static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 4);
|
2008-08-07 00:41:05 +04:00
|
|
|
|
2014-07-01 18:31:44 +04:00
|
|
|
static struct attribute *ad7414_attrs[] = {
|
2008-08-07 00:41:05 +04:00
|
|
|
&sensor_dev_attr_temp1_input.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_temp1_max.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_temp1_min.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2014-07-01 18:31:44 +04:00
|
|
|
ATTRIBUTE_GROUPS(ad7414);
|
2008-08-07 00:41:05 +04:00
|
|
|
|
|
|
|
static int ad7414_probe(struct i2c_client *client,
|
|
|
|
const struct i2c_device_id *dev_id)
|
|
|
|
{
|
2014-07-01 18:31:44 +04:00
|
|
|
struct device *dev = &client->dev;
|
2008-08-07 00:41:05 +04:00
|
|
|
struct ad7414_data *data;
|
2014-07-01 18:31:44 +04:00
|
|
|
struct device *hwmon_dev;
|
2008-08-07 00:41:05 +04:00
|
|
|
int conf;
|
|
|
|
|
|
|
|
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
|
2012-06-02 20:57:57 +04:00
|
|
|
I2C_FUNC_SMBUS_READ_WORD_DATA))
|
|
|
|
return -EOPNOTSUPP;
|
2008-08-07 00:41:05 +04:00
|
|
|
|
2014-07-01 18:31:44 +04:00
|
|
|
data = devm_kzalloc(dev, sizeof(struct ad7414_data), GFP_KERNEL);
|
2012-06-02 20:57:57 +04:00
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
2008-08-07 00:41:05 +04:00
|
|
|
|
2014-07-01 18:31:44 +04:00
|
|
|
data->client = client;
|
2008-08-07 00:41:05 +04:00
|
|
|
mutex_init(&data->lock);
|
|
|
|
|
|
|
|
dev_info(&client->dev, "chip found\n");
|
|
|
|
|
|
|
|
/* Make sure the chip is powered up. */
|
|
|
|
conf = i2c_smbus_read_byte_data(client, AD7414_REG_CONF);
|
|
|
|
if (conf < 0)
|
2014-07-01 18:31:44 +04:00
|
|
|
dev_warn(dev, "ad7414_probe unable to read config register.\n");
|
2008-08-07 00:41:05 +04:00
|
|
|
else {
|
|
|
|
conf &= ~(1 << 7);
|
|
|
|
i2c_smbus_write_byte_data(client, AD7414_REG_CONF, conf);
|
|
|
|
}
|
|
|
|
|
2014-07-01 18:31:44 +04:00
|
|
|
hwmon_dev = devm_hwmon_device_register_with_groups(dev,
|
|
|
|
client->name,
|
|
|
|
data, ad7414_groups);
|
|
|
|
return PTR_ERR_OR_ZERO(hwmon_dev);
|
2008-08-07 00:41:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_device_id ad7414_id[] = {
|
|
|
|
{ "ad7414", 0 },
|
|
|
|
{}
|
|
|
|
};
|
2011-02-24 05:20:37 +03:00
|
|
|
MODULE_DEVICE_TABLE(i2c, ad7414_id);
|
2008-08-07 00:41:05 +04:00
|
|
|
|
2019-04-04 16:26:08 +03:00
|
|
|
static const struct of_device_id __maybe_unused ad7414_of_match[] = {
|
2017-02-24 16:12:54 +03:00
|
|
|
{ .compatible = "ad,ad7414" },
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, ad7414_of_match);
|
|
|
|
|
2008-08-07 00:41:05 +04:00
|
|
|
static struct i2c_driver ad7414_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "ad7414",
|
2017-02-24 16:12:54 +03:00
|
|
|
.of_match_table = of_match_ptr(ad7414_of_match),
|
2008-08-07 00:41:05 +04:00
|
|
|
},
|
|
|
|
.probe = ad7414_probe,
|
|
|
|
.id_table = ad7414_id,
|
|
|
|
};
|
|
|
|
|
2012-01-20 11:38:18 +04:00
|
|
|
module_i2c_driver(ad7414_driver);
|
2008-08-07 00:41:05 +04:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Stefan Roese <sr at denx.de>, "
|
|
|
|
"Frank Edelhaeuser <frank.edelhaeuser at spansion.com>");
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("AD7414 driver");
|
|
|
|
MODULE_LICENSE("GPL");
|