2019-05-27 09:55:06 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-14 00:27:53 +04:00
|
|
|
/*
|
2012-01-15 01:21:59 +04:00
|
|
|
* atxp1.c - kernel module for setting CPU VID and general purpose
|
|
|
|
* I/Os using the Attansic ATXP1 chip.
|
|
|
|
*
|
2015-05-28 02:17:19 +03:00
|
|
|
* The ATXP1 can reside on I2C addresses 0x37 or 0x4e. The chip is
|
|
|
|
* not auto-detected by the driver and must be instantiated explicitly.
|
2019-07-26 15:51:16 +03:00
|
|
|
* See Documentation/i2c/instantiating-devices.rst for more information.
|
2012-01-15 01:21:59 +04:00
|
|
|
*/
|
2005-04-14 00:27:53 +04:00
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
2005-07-29 23:15:12 +04:00
|
|
|
#include <linux/jiffies.h>
|
2005-04-14 00:27:53 +04:00
|
|
|
#include <linux/i2c.h>
|
2005-07-16 05:39:18 +04:00
|
|
|
#include <linux/hwmon.h>
|
2005-07-31 23:52:01 +04:00
|
|
|
#include <linux/hwmon-vid.h>
|
2005-07-16 05:39:18 +04:00
|
|
|
#include <linux/err.h>
|
2006-01-19 01:19:26 +03:00
|
|
|
#include <linux/mutex.h>
|
2006-09-24 23:24:46 +04:00
|
|
|
#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>
|
2005-04-14 00:27:53 +04:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_DESCRIPTION("System voltages control via Attansic ATXP1");
|
2008-09-20 12:25:19 +04:00
|
|
|
MODULE_VERSION("0.6.3");
|
2005-04-14 00:27:53 +04:00
|
|
|
MODULE_AUTHOR("Sebastian Witt <se.witt@gmx.net>");
|
|
|
|
|
|
|
|
#define ATXP1_VID 0x00
|
|
|
|
#define ATXP1_CVID 0x01
|
|
|
|
#define ATXP1_GPIO1 0x06
|
|
|
|
#define ATXP1_GPIO2 0x0a
|
|
|
|
#define ATXP1_VIDENA 0x20
|
|
|
|
#define ATXP1_VIDMASK 0x1f
|
|
|
|
#define ATXP1_GPIO1MASK 0x0f
|
|
|
|
|
|
|
|
struct atxp1_data {
|
2014-06-07 03:55:10 +04:00
|
|
|
struct i2c_client *client;
|
2006-01-19 01:19:26 +03:00
|
|
|
struct mutex update_lock;
|
2005-04-14 00:27:53 +04:00
|
|
|
unsigned long last_updated;
|
|
|
|
u8 valid;
|
|
|
|
struct {
|
|
|
|
u8 vid; /* VID output register */
|
|
|
|
u8 cpu_vid; /* VID input from CPU */
|
|
|
|
u8 gpio1; /* General purpose I/O register 1 */
|
|
|
|
u8 gpio2; /* General purpose I/O register 2 */
|
|
|
|
} reg;
|
|
|
|
u8 vrm; /* Detected CPU VRM */
|
|
|
|
};
|
|
|
|
|
2012-01-15 01:21:59 +04:00
|
|
|
static struct atxp1_data *atxp1_update_device(struct device *dev)
|
2005-04-14 00:27:53 +04:00
|
|
|
{
|
2014-06-07 03:55:10 +04:00
|
|
|
struct atxp1_data *data = dev_get_drvdata(dev);
|
|
|
|
struct i2c_client *client = data->client;
|
2005-04-14 00:27:53 +04:00
|
|
|
|
2006-01-19 01:19:26 +03:00
|
|
|
mutex_lock(&data->update_lock);
|
2005-04-14 00:27:53 +04:00
|
|
|
|
2005-07-29 23:15:12 +04:00
|
|
|
if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
|
2005-04-14 00:27:53 +04:00
|
|
|
|
|
|
|
/* Update local register data */
|
|
|
|
data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID);
|
2012-01-15 01:21:59 +04:00
|
|
|
data->reg.cpu_vid = i2c_smbus_read_byte_data(client,
|
|
|
|
ATXP1_CVID);
|
2005-04-14 00:27:53 +04:00
|
|
|
data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1);
|
|
|
|
data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2);
|
|
|
|
|
|
|
|
data->valid = 1;
|
|
|
|
}
|
|
|
|
|
2006-01-19 01:19:26 +03:00
|
|
|
mutex_unlock(&data->update_lock);
|
2005-04-14 00:27:53 +04:00
|
|
|
|
2012-01-05 22:50:18 +04:00
|
|
|
return data;
|
2005-04-14 00:27:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sys file functions for cpu0_vid */
|
2016-12-22 15:04:37 +03:00
|
|
|
static ssize_t cpu0_vid_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2005-04-14 00:27:53 +04:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
struct atxp1_data *data;
|
|
|
|
|
|
|
|
data = atxp1_update_device(dev);
|
|
|
|
|
2012-01-15 01:21:59 +04:00
|
|
|
size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK,
|
|
|
|
data->vrm));
|
2005-04-14 00:27:53 +04:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:04:37 +03:00
|
|
|
static ssize_t cpu0_vid_store(struct device *dev,
|
|
|
|
struct device_attribute *attr, const char *buf,
|
|
|
|
size_t count)
|
2005-04-14 00:27:53 +04:00
|
|
|
{
|
2014-06-07 03:55:10 +04:00
|
|
|
struct atxp1_data *data = atxp1_update_device(dev);
|
|
|
|
struct i2c_client *client = data->client;
|
2006-08-28 16:18:14 +04:00
|
|
|
int vid, cvid;
|
2012-01-15 01:21:59 +04:00
|
|
|
unsigned long vcore;
|
|
|
|
int err;
|
2005-04-14 00:27:53 +04:00
|
|
|
|
2012-01-15 01:21:59 +04:00
|
|
|
err = kstrtoul(buf, 10, &vcore);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2005-04-14 00:27:53 +04:00
|
|
|
vcore /= 25;
|
|
|
|
vcore *= 25;
|
|
|
|
|
|
|
|
/* Calculate VID */
|
|
|
|
vid = vid_to_reg(vcore, data->vrm);
|
|
|
|
if (vid < 0) {
|
|
|
|
dev_err(dev, "VID calculation failed.\n");
|
2013-09-13 21:59:27 +04:00
|
|
|
return vid;
|
2005-04-14 00:27:53 +04:00
|
|
|
}
|
|
|
|
|
2012-01-15 01:21:59 +04:00
|
|
|
/*
|
|
|
|
* If output enabled, use control register value.
|
|
|
|
* Otherwise original CPU VID
|
|
|
|
*/
|
2005-04-14 00:27:53 +04:00
|
|
|
if (data->reg.vid & ATXP1_VIDENA)
|
|
|
|
cvid = data->reg.vid & ATXP1_VIDMASK;
|
|
|
|
else
|
|
|
|
cvid = data->reg.cpu_vid;
|
|
|
|
|
|
|
|
/* Nothing changed, aborting */
|
|
|
|
if (vid == cvid)
|
|
|
|
return count;
|
|
|
|
|
2012-01-15 01:21:59 +04:00
|
|
|
dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", (int)vcore, vid);
|
2005-04-14 00:27:53 +04:00
|
|
|
|
|
|
|
/* Write every 25 mV step to increase stability */
|
|
|
|
if (cvid > vid) {
|
2012-01-15 01:21:59 +04:00
|
|
|
for (; cvid >= vid; cvid--)
|
|
|
|
i2c_smbus_write_byte_data(client,
|
|
|
|
ATXP1_VID, cvid | ATXP1_VIDENA);
|
|
|
|
} else {
|
|
|
|
for (; cvid <= vid; cvid++)
|
|
|
|
i2c_smbus_write_byte_data(client,
|
|
|
|
ATXP1_VID, cvid | ATXP1_VIDENA);
|
2005-04-14 00:27:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
data->valid = 0;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-01-15 01:21:59 +04:00
|
|
|
/*
|
|
|
|
* CPU core reference voltage
|
|
|
|
* unit: millivolt
|
|
|
|
*/
|
2016-12-22 15:04:37 +03:00
|
|
|
static DEVICE_ATTR_RW(cpu0_vid);
|
2005-04-14 00:27:53 +04:00
|
|
|
|
|
|
|
/* sys file functions for GPIO1 */
|
2016-12-22 15:04:37 +03:00
|
|
|
static ssize_t gpio1_show(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf)
|
2005-04-14 00:27:53 +04:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
struct atxp1_data *data;
|
|
|
|
|
|
|
|
data = atxp1_update_device(dev);
|
|
|
|
|
|
|
|
size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:04:37 +03:00
|
|
|
static ssize_t gpio1_store(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
2005-04-14 00:27:53 +04:00
|
|
|
{
|
2014-06-07 03:55:10 +04:00
|
|
|
struct atxp1_data *data = atxp1_update_device(dev);
|
|
|
|
struct i2c_client *client = data->client;
|
2012-01-15 01:21:59 +04:00
|
|
|
unsigned long value;
|
|
|
|
int err;
|
2005-04-14 00:27:53 +04:00
|
|
|
|
2012-01-15 01:21:59 +04:00
|
|
|
err = kstrtoul(buf, 16, &value);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2005-04-14 00:27:53 +04:00
|
|
|
|
|
|
|
value &= ATXP1_GPIO1MASK;
|
|
|
|
|
|
|
|
if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
|
2012-01-15 01:21:59 +04:00
|
|
|
dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
|
2005-04-14 00:27:53 +04:00
|
|
|
|
|
|
|
i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);
|
|
|
|
|
|
|
|
data->valid = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-01-15 01:21:59 +04:00
|
|
|
/*
|
|
|
|
* GPIO1 data register
|
|
|
|
* unit: Four bit as hex (e.g. 0x0f)
|
|
|
|
*/
|
2016-12-22 15:04:37 +03:00
|
|
|
static DEVICE_ATTR_RW(gpio1);
|
2005-04-14 00:27:53 +04:00
|
|
|
|
|
|
|
/* sys file functions for GPIO2 */
|
2016-12-22 15:04:37 +03:00
|
|
|
static ssize_t gpio2_show(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf)
|
2005-04-14 00:27:53 +04:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
struct atxp1_data *data;
|
|
|
|
|
|
|
|
data = atxp1_update_device(dev);
|
|
|
|
|
|
|
|
size = sprintf(buf, "0x%02x\n", data->reg.gpio2);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:04:37 +03:00
|
|
|
static ssize_t gpio2_store(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
2005-04-14 00:27:53 +04:00
|
|
|
{
|
2012-01-15 01:21:59 +04:00
|
|
|
struct atxp1_data *data = atxp1_update_device(dev);
|
2014-06-07 03:55:10 +04:00
|
|
|
struct i2c_client *client = data->client;
|
2012-01-15 01:21:59 +04:00
|
|
|
unsigned long value;
|
|
|
|
int err;
|
2005-04-14 00:27:53 +04:00
|
|
|
|
2012-01-15 01:21:59 +04:00
|
|
|
err = kstrtoul(buf, 16, &value);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
value &= 0xff;
|
2005-04-14 00:27:53 +04:00
|
|
|
|
|
|
|
if (value != data->reg.gpio2) {
|
2012-01-15 01:21:59 +04:00
|
|
|
dev_info(dev, "Writing 0x%x to GPIO1.\n", (unsigned int)value);
|
2005-04-14 00:27:53 +04:00
|
|
|
|
|
|
|
i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);
|
|
|
|
|
|
|
|
data->valid = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-01-15 01:21:59 +04:00
|
|
|
/*
|
|
|
|
* GPIO2 data register
|
|
|
|
* unit: Eight bit as hex (e.g. 0xff)
|
|
|
|
*/
|
2016-12-22 15:04:37 +03:00
|
|
|
static DEVICE_ATTR_RW(gpio2);
|
2005-04-14 00:27:53 +04:00
|
|
|
|
2014-06-07 03:55:10 +04:00
|
|
|
static struct attribute *atxp1_attrs[] = {
|
2006-09-24 23:24:46 +04:00
|
|
|
&dev_attr_gpio1.attr,
|
|
|
|
&dev_attr_gpio2.attr,
|
|
|
|
&dev_attr_cpu0_vid.attr,
|
|
|
|
NULL
|
|
|
|
};
|
2014-06-07 03:55:10 +04:00
|
|
|
ATTRIBUTE_GROUPS(atxp1);
|
2005-04-14 00:27:53 +04:00
|
|
|
|
2020-08-13 19:02:22 +03:00
|
|
|
static int atxp1_probe(struct i2c_client *client)
|
2008-07-16 21:30:11 +04:00
|
|
|
{
|
2014-06-07 03:55:10 +04:00
|
|
|
struct device *dev = &client->dev;
|
2008-07-16 21:30:11 +04:00
|
|
|
struct atxp1_data *data;
|
2014-06-07 03:55:10 +04:00
|
|
|
struct device *hwmon_dev;
|
2005-04-14 00:27:53 +04:00
|
|
|
|
2014-06-07 03:55:10 +04:00
|
|
|
data = devm_kzalloc(dev, sizeof(struct atxp1_data), GFP_KERNEL);
|
2012-06-02 20:58:03 +04:00
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
2005-04-14 00:27:53 +04:00
|
|
|
|
2008-07-16 21:30:11 +04:00
|
|
|
/* Get VRM */
|
|
|
|
data->vrm = vid_which_vrm();
|
2015-05-28 02:17:19 +03:00
|
|
|
if (data->vrm != 90 && data->vrm != 91) {
|
|
|
|
dev_err(dev, "atxp1: Not supporting VRM %d.%d\n",
|
|
|
|
data->vrm / 10, data->vrm % 10);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2008-07-16 21:30:11 +04:00
|
|
|
|
2014-06-07 03:55:10 +04:00
|
|
|
data->client = client;
|
2008-07-16 21:30:11 +04:00
|
|
|
mutex_init(&data->update_lock);
|
|
|
|
|
2014-06-07 03:55:10 +04:00
|
|
|
hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
|
|
|
|
data,
|
|
|
|
atxp1_groups);
|
|
|
|
if (IS_ERR(hwmon_dev))
|
|
|
|
return PTR_ERR(hwmon_dev);
|
2005-04-14 00:27:53 +04:00
|
|
|
|
2014-06-07 03:55:10 +04:00
|
|
|
dev_info(dev, "Using VRM: %d.%d\n", data->vrm / 10, data->vrm % 10);
|
2005-07-16 05:39:18 +04:00
|
|
|
|
2008-07-16 21:30:11 +04:00
|
|
|
return 0;
|
2005-04-14 00:27:53 +04:00
|
|
|
};
|
|
|
|
|
2014-06-06 13:50:56 +04:00
|
|
|
static const struct i2c_device_id atxp1_id[] = {
|
|
|
|
{ "atxp1", 0 },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(i2c, atxp1_id);
|
|
|
|
|
|
|
|
static struct i2c_driver atxp1_driver = {
|
|
|
|
.class = I2C_CLASS_HWMON,
|
|
|
|
.driver = {
|
|
|
|
.name = "atxp1",
|
|
|
|
},
|
2020-08-13 19:02:22 +03:00
|
|
|
.probe_new = atxp1_probe,
|
2014-06-06 13:50:56 +04:00
|
|
|
.id_table = atxp1_id,
|
|
|
|
};
|
|
|
|
|
2012-01-20 11:38:18 +04:00
|
|
|
module_i2c_driver(atxp1_driver);
|