2019-05-27 14:55:06 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-03-02 19:23:42 +08:00
|
|
|
/*
|
|
|
|
* i2c-smbus.c - SMBus extensions to the I2C protocol
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 David Brownell
|
2020-03-16 18:22:24 +08:00
|
|
|
* Copyright (C) 2010-2019 Jean Delvare <jdelvare@suse.de>
|
2010-03-02 19:23:42 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/device.h>
|
2020-03-16 18:22:24 +08:00
|
|
|
#include <linux/dmi.h>
|
2010-03-02 19:23:42 +08:00
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/i2c-smbus.h>
|
2016-02-21 06:33:39 +08:00
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
2017-08-24 17:31:03 +08:00
|
|
|
#include <linux/of_irq.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 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2016-02-21 06:33:39 +08:00
|
|
|
#include <linux/workqueue.h>
|
2010-03-02 19:23:42 +08:00
|
|
|
|
|
|
|
struct i2c_smbus_alert {
|
|
|
|
struct work_struct alert;
|
|
|
|
struct i2c_client *ara; /* Alert response address */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct alert_data {
|
|
|
|
unsigned short addr;
|
2016-06-09 22:53:48 +08:00
|
|
|
enum i2c_alert_protocol type;
|
|
|
|
unsigned int data;
|
2010-03-02 19:23:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* If this is the alerting device, notify its driver */
|
|
|
|
static int smbus_do_alert(struct device *dev, void *addrp)
|
|
|
|
{
|
|
|
|
struct i2c_client *client = i2c_verify_client(dev);
|
|
|
|
struct alert_data *data = addrp;
|
2013-09-29 16:51:06 +08:00
|
|
|
struct i2c_driver *driver;
|
2010-03-02 19:23:42 +08:00
|
|
|
|
|
|
|
if (!client || client->addr != data->addr)
|
|
|
|
return 0;
|
|
|
|
if (client->flags & I2C_CLIENT_TEN)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Drivers should either disable alerts, or provide at least
|
2013-09-29 16:51:06 +08:00
|
|
|
* a minimal handler. Lock so the driver won't change.
|
2010-03-02 19:23:42 +08:00
|
|
|
*/
|
2010-03-01 13:04:45 +08:00
|
|
|
device_lock(dev);
|
2013-09-29 16:51:06 +08:00
|
|
|
if (client->dev.driver) {
|
|
|
|
driver = to_i2c_driver(client->dev.driver);
|
|
|
|
if (driver->alert)
|
2016-06-09 22:53:48 +08:00
|
|
|
driver->alert(client, data->type, data->data);
|
2010-03-02 19:23:42 +08:00
|
|
|
else
|
|
|
|
dev_warn(&client->dev, "no driver alert()!\n");
|
|
|
|
} else
|
|
|
|
dev_dbg(&client->dev, "alert with no driver\n");
|
2010-03-01 13:04:45 +08:00
|
|
|
device_unlock(dev);
|
2010-03-02 19:23:42 +08:00
|
|
|
|
|
|
|
/* Stop iterating after we find the device */
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The alert IRQ handler needs to hand work off to a task which can issue
|
|
|
|
* SMBus calls, because those sleeping calls can't be made in IRQ context.
|
|
|
|
*/
|
2017-08-24 17:31:01 +08:00
|
|
|
static irqreturn_t smbus_alert(int irq, void *d)
|
2010-03-02 19:23:42 +08:00
|
|
|
{
|
2017-08-24 17:31:01 +08:00
|
|
|
struct i2c_smbus_alert *alert = d;
|
2010-03-02 19:23:42 +08:00
|
|
|
struct i2c_client *ara;
|
|
|
|
|
|
|
|
ara = alert->ara;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
s32 status;
|
|
|
|
struct alert_data data;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Devices with pending alerts reply in address order, low
|
|
|
|
* to high, because of slave transmit arbitration. After
|
|
|
|
* responding, an SMBus device stops asserting SMBALERT#.
|
|
|
|
*
|
2015-05-19 22:39:41 +08:00
|
|
|
* Note that SMBus 2.0 reserves 10-bit addresses for future
|
2010-03-02 19:23:42 +08:00
|
|
|
* use. We neither handle them, nor try to use PEC here.
|
|
|
|
*/
|
|
|
|
status = i2c_smbus_read_byte(ara);
|
|
|
|
if (status < 0)
|
|
|
|
break;
|
|
|
|
|
2016-06-09 22:53:48 +08:00
|
|
|
data.data = status & 1;
|
2010-03-02 19:23:42 +08:00
|
|
|
data.addr = status >> 1;
|
2016-06-09 22:53:48 +08:00
|
|
|
data.type = I2C_PROTOCOL_SMBUS_ALERT;
|
2010-03-02 19:23:42 +08:00
|
|
|
|
|
|
|
dev_dbg(&ara->dev, "SMBALERT# from dev 0x%02x, flag %d\n",
|
2016-06-09 22:53:48 +08:00
|
|
|
data.addr, data.data);
|
2010-03-02 19:23:42 +08:00
|
|
|
|
|
|
|
/* Notify driver for the device which issued the alert */
|
|
|
|
device_for_each_child(&ara->adapter->dev, &data,
|
|
|
|
smbus_do_alert);
|
|
|
|
}
|
|
|
|
|
2017-08-24 17:31:01 +08:00
|
|
|
return IRQ_HANDLED;
|
2010-03-02 19:23:42 +08:00
|
|
|
}
|
|
|
|
|
2017-08-24 17:31:01 +08:00
|
|
|
static void smbalert_work(struct work_struct *work)
|
2010-03-02 19:23:42 +08:00
|
|
|
{
|
2017-08-24 17:31:01 +08:00
|
|
|
struct i2c_smbus_alert *alert;
|
|
|
|
|
|
|
|
alert = container_of(work, struct i2c_smbus_alert, alert);
|
2010-03-02 19:23:42 +08:00
|
|
|
|
2017-08-24 17:31:01 +08:00
|
|
|
smbus_alert(0, alert);
|
2010-03-02 19:23:42 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup SMBALERT# infrastructure */
|
|
|
|
static int smbalert_probe(struct i2c_client *ara,
|
|
|
|
const struct i2c_device_id *id)
|
|
|
|
{
|
2013-07-30 15:59:33 +08:00
|
|
|
struct i2c_smbus_alert_setup *setup = dev_get_platdata(&ara->dev);
|
2010-03-02 19:23:42 +08:00
|
|
|
struct i2c_smbus_alert *alert;
|
|
|
|
struct i2c_adapter *adapter = ara->adapter;
|
2017-08-24 17:31:01 +08:00
|
|
|
int res, irq;
|
2010-03-02 19:23:42 +08:00
|
|
|
|
i2c-smbus: Convert kzalloc to devm_kzalloc
Converting kzalloc to devm_kzalloc simplifies the code and ensures that the
result, alert, is freed after the irq allocated by the subsequent
devm_request_irq. This in turn ensures that when an interrupt can be
triggered, the alert structure is still available.
The problem of a free after a devm_request_irq was found using the
following semantic match (http://coccinelle.lip6.fr/)
// <smpl>
@r exists@
expression e1,e2,x,a,b,c,d;
identifier free;
position p1,p2;
@@
devm_request_irq@p1(e1,e2,...,x)
... when any
when != e2 = a
when != x = b
if (...) {
... when != e2 = c
when != x = d
free@p2(...,x,...);
...
return ...;
}
// </smpl>
Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr>
Signed-off-by: Jean Delvare <khali@linux-fr.org>
2012-10-06 04:23:52 +08:00
|
|
|
alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert),
|
|
|
|
GFP_KERNEL);
|
2010-03-02 19:23:42 +08:00
|
|
|
if (!alert)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-08-24 17:31:03 +08:00
|
|
|
if (setup) {
|
|
|
|
irq = setup->irq;
|
|
|
|
} else {
|
|
|
|
irq = of_irq_get_byname(adapter->dev.of_node, "smbus_alert");
|
|
|
|
if (irq <= 0)
|
|
|
|
return irq;
|
|
|
|
}
|
|
|
|
|
2017-08-24 17:31:01 +08:00
|
|
|
INIT_WORK(&alert->alert, smbalert_work);
|
2010-03-02 19:23:42 +08:00
|
|
|
alert->ara = ara;
|
|
|
|
|
2017-08-24 17:31:01 +08:00
|
|
|
if (irq > 0) {
|
|
|
|
res = devm_request_threaded_irq(&ara->dev, irq,
|
|
|
|
NULL, smbus_alert,
|
|
|
|
IRQF_SHARED | IRQF_ONESHOT,
|
|
|
|
"smbus_alert", alert);
|
i2c-smbus: Convert kzalloc to devm_kzalloc
Converting kzalloc to devm_kzalloc simplifies the code and ensures that the
result, alert, is freed after the irq allocated by the subsequent
devm_request_irq. This in turn ensures that when an interrupt can be
triggered, the alert structure is still available.
The problem of a free after a devm_request_irq was found using the
following semantic match (http://coccinelle.lip6.fr/)
// <smpl>
@r exists@
expression e1,e2,x,a,b,c,d;
identifier free;
position p1,p2;
@@
devm_request_irq@p1(e1,e2,...,x)
... when any
when != e2 = a
when != x = b
if (...) {
... when != e2 = c
when != x = d
free@p2(...,x,...);
...
return ...;
}
// </smpl>
Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr>
Signed-off-by: Jean Delvare <khali@linux-fr.org>
2012-10-06 04:23:52 +08:00
|
|
|
if (res)
|
2010-03-02 19:23:42 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
i2c_set_clientdata(ara, alert);
|
2017-08-24 17:31:01 +08:00
|
|
|
dev_info(&adapter->dev, "supports SMBALERT#\n");
|
2010-03-02 19:23:42 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
i2c-smbus: Convert kzalloc to devm_kzalloc
Converting kzalloc to devm_kzalloc simplifies the code and ensures that the
result, alert, is freed after the irq allocated by the subsequent
devm_request_irq. This in turn ensures that when an interrupt can be
triggered, the alert structure is still available.
The problem of a free after a devm_request_irq was found using the
following semantic match (http://coccinelle.lip6.fr/)
// <smpl>
@r exists@
expression e1,e2,x,a,b,c,d;
identifier free;
position p1,p2;
@@
devm_request_irq@p1(e1,e2,...,x)
... when any
when != e2 = a
when != x = b
if (...) {
... when != e2 = c
when != x = d
free@p2(...,x,...);
...
return ...;
}
// </smpl>
Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr>
Signed-off-by: Jean Delvare <khali@linux-fr.org>
2012-10-06 04:23:52 +08:00
|
|
|
/* IRQ and memory resources are managed so they are freed automatically */
|
2010-03-02 19:23:42 +08:00
|
|
|
static int smbalert_remove(struct i2c_client *ara)
|
|
|
|
{
|
|
|
|
struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
|
|
|
|
|
|
|
|
cancel_work_sync(&alert->alert);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_device_id smbalert_ids[] = {
|
|
|
|
{ "smbus_alert", 0 },
|
|
|
|
{ /* LIST END */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(i2c, smbalert_ids);
|
|
|
|
|
|
|
|
static struct i2c_driver smbalert_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "smbus_alert",
|
|
|
|
},
|
|
|
|
.probe = smbalert_probe,
|
|
|
|
.remove = smbalert_remove,
|
|
|
|
.id_table = smbalert_ids,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* i2c_handle_smbus_alert - Handle an SMBus alert
|
|
|
|
* @ara: the ARA client on the relevant adapter
|
|
|
|
* Context: can't sleep
|
|
|
|
*
|
|
|
|
* Helper function to be called from an I2C bus driver's interrupt
|
|
|
|
* handler. It will schedule the alert work, in turn calling the
|
|
|
|
* corresponding I2C device driver's alert function.
|
|
|
|
*
|
|
|
|
* It is assumed that ara is a valid i2c client previously returned by
|
2020-02-29 01:12:20 +08:00
|
|
|
* i2c_new_smbus_alert_device().
|
2010-03-02 19:23:42 +08:00
|
|
|
*/
|
|
|
|
int i2c_handle_smbus_alert(struct i2c_client *ara)
|
|
|
|
{
|
|
|
|
struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
|
|
|
|
|
|
|
|
return schedule_work(&alert->alert);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert);
|
|
|
|
|
2012-07-24 20:13:57 +08:00
|
|
|
module_i2c_driver(smbalert_driver);
|
2010-03-02 19:23:42 +08:00
|
|
|
|
2020-03-16 18:22:24 +08:00
|
|
|
/*
|
|
|
|
* SPD is not part of SMBus but we include it here for convenience as the
|
|
|
|
* target systems are the same.
|
|
|
|
* Restrictions to automatic SPD instantiation:
|
|
|
|
* - Only works if all filled slots have the same memory type
|
|
|
|
* - Only works for DDR2, DDR3 and DDR4 for now
|
|
|
|
* - Only works on systems with 1 to 4 memory slots
|
|
|
|
*/
|
|
|
|
#if IS_ENABLED(CONFIG_DMI)
|
|
|
|
void i2c_register_spd(struct i2c_adapter *adap)
|
|
|
|
{
|
|
|
|
int n, slot_count = 0, dimm_count = 0;
|
|
|
|
u16 handle;
|
|
|
|
u8 common_mem_type = 0x0, mem_type;
|
|
|
|
u64 mem_size;
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
while ((handle = dmi_memdev_handle(slot_count)) != 0xffff) {
|
|
|
|
slot_count++;
|
|
|
|
|
|
|
|
/* Skip empty slots */
|
|
|
|
mem_size = dmi_memdev_size(handle);
|
|
|
|
if (!mem_size)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Skip undefined memory type */
|
|
|
|
mem_type = dmi_memdev_type(handle);
|
|
|
|
if (mem_type <= 0x02) /* Invalid, Other, Unknown */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!common_mem_type) {
|
|
|
|
/* First filled slot */
|
|
|
|
common_mem_type = mem_type;
|
|
|
|
} else {
|
|
|
|
/* Check that all filled slots have the same type */
|
|
|
|
if (mem_type != common_mem_type) {
|
|
|
|
dev_warn(&adap->dev,
|
|
|
|
"Different memory types mixed, not instantiating SPD\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dimm_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No useful DMI data, bail out */
|
|
|
|
if (!dimm_count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dev_info(&adap->dev, "%d/%d memory slots populated (from DMI)\n",
|
|
|
|
dimm_count, slot_count);
|
|
|
|
|
|
|
|
if (slot_count > 4) {
|
|
|
|
dev_warn(&adap->dev,
|
|
|
|
"Systems with more than 4 memory slots not supported yet, not instantiating SPD\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (common_mem_type) {
|
|
|
|
case 0x13: /* DDR2 */
|
|
|
|
case 0x18: /* DDR3 */
|
|
|
|
case 0x1C: /* LPDDR2 */
|
|
|
|
case 0x1D: /* LPDDR3 */
|
|
|
|
name = "spd";
|
|
|
|
break;
|
|
|
|
case 0x1A: /* DDR4 */
|
|
|
|
case 0x1E: /* LPDDR4 */
|
|
|
|
name = "ee1004";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_info(&adap->dev,
|
|
|
|
"Memory type 0x%02x not supported yet, not instantiating SPD\n",
|
|
|
|
common_mem_type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't know in which slots the memory modules are. We could
|
|
|
|
* try to guess from the slot names, but that would be rather complex
|
|
|
|
* and unreliable, so better probe all possible addresses until we
|
|
|
|
* have found all memory modules.
|
|
|
|
*/
|
|
|
|
for (n = 0; n < slot_count && dimm_count; n++) {
|
|
|
|
struct i2c_board_info info;
|
|
|
|
unsigned short addr_list[2];
|
|
|
|
|
|
|
|
memset(&info, 0, sizeof(struct i2c_board_info));
|
|
|
|
strlcpy(info.type, name, I2C_NAME_SIZE);
|
|
|
|
addr_list[0] = 0x50 + n;
|
|
|
|
addr_list[1] = I2C_CLIENT_END;
|
|
|
|
|
|
|
|
if (!IS_ERR(i2c_new_scanned_device(adap, &info, addr_list, NULL))) {
|
|
|
|
dev_info(&adap->dev,
|
|
|
|
"Successfully instantiated SPD at 0x%hx\n",
|
|
|
|
addr_list[0]);
|
|
|
|
dimm_count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(i2c_register_spd);
|
|
|
|
#endif
|
|
|
|
|
2014-01-30 03:40:08 +08:00
|
|
|
MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
|
2010-03-02 19:23:42 +08:00
|
|
|
MODULE_DESCRIPTION("SMBus protocol extensions support");
|
|
|
|
MODULE_LICENSE("GPL");
|