Skip to content

Commit 00d9361

Browse files
committed
ipmi:ipmb: Add the ability to have a separate slave and master device
A situation has come up where there is a slave-only device for the slave and a separate master device on the same bug. Allow a separate slave device to be registered. Signed-off-by: Corey Minyard <[email protected]>
1 parent 57c9e3c commit 00d9361

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

Documentation/devicetree/bindings/ipmi/ipmi-ipmb.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ properties:
3636
$ref: /schemas/types.yaml#/definitions/uint32
3737
description: Number of retries before a failure is declared. Defaults to 1.
3838

39+
slave-dev:
40+
$ref: /schemas/types.yaml#/definitions/phandle
41+
description: |
42+
The slave i2c device. If not present, the main device is used. This
43+
lets you use two devices on the IPMB, one for master and one for slave,
44+
in case you have a slave device that can only be a slave. The slave
45+
will receive messages and the master will transmit.
46+
3947
required:
4048
- compatible
4149
- reg

drivers/char/ipmi/ipmi_ipmb.c

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ MODULE_PARM_DESC(max_retries, "Max resends of a command before timing out.");
3939
struct ipmi_ipmb_dev {
4040
struct ipmi_smi *intf;
4141
struct i2c_client *client;
42+
struct i2c_client *slave;
4243

4344
struct ipmi_smi_handlers handlers;
4445

@@ -257,7 +258,7 @@ static void ipmi_ipmb_format_for_xmit(struct ipmi_ipmb_dev *iidev,
257258
memcpy(iidev->xmitmsg + 5, msg->data + 1, msg->data_size - 1);
258259
iidev->xmitlen = msg->data_size + 4;
259260
}
260-
iidev->xmitmsg[3] = iidev->client->addr << 1;
261+
iidev->xmitmsg[3] = iidev->slave->addr << 1;
261262
if (((msg->data[0] >> 2) & 1) == 0)
262263
/* If it's a command, put in our own sequence number. */
263264
iidev->xmitmsg[4] = ((iidev->xmitmsg[4] & 0x03) |
@@ -427,10 +428,13 @@ static int ipmi_ipmb_remove(struct i2c_client *client)
427428
{
428429
struct ipmi_ipmb_dev *iidev = i2c_get_clientdata(client);
429430

430-
if (iidev->client) {
431-
iidev->client = NULL;
432-
i2c_slave_unregister(client);
431+
if (iidev->slave) {
432+
i2c_slave_unregister(iidev->slave);
433+
if (iidev->slave != iidev->client)
434+
i2c_unregister_device(iidev->slave);
433435
}
436+
iidev->slave = NULL;
437+
iidev->client = NULL;
434438
ipmi_ipmb_stop_thread(iidev);
435439

436440
ipmi_unregister_smi(iidev->intf);
@@ -443,6 +447,9 @@ static int ipmi_ipmb_probe(struct i2c_client *client,
443447
{
444448
struct device *dev = &client->dev;
445449
struct ipmi_ipmb_dev *iidev;
450+
struct device_node *slave_np;
451+
struct i2c_adapter *slave_adap = NULL;
452+
struct i2c_client *slave = NULL;
446453
int rv;
447454

448455
iidev = devm_kzalloc(&client->dev, sizeof(*iidev), GFP_KERNEL);
@@ -466,14 +473,45 @@ static int ipmi_ipmb_probe(struct i2c_client *client,
466473
&iidev->max_retries) != 0)
467474
iidev->max_retries = max_retries;
468475

476+
slave_np = of_parse_phandle(dev->of_node, "slave-dev", 0);
477+
if (slave_np) {
478+
slave_adap = of_get_i2c_adapter_by_node(slave_np);
479+
if (!slave_adap) {
480+
dev_notice(&client->dev,
481+
"Could not find slave adapter\n");
482+
return -EINVAL;
483+
}
484+
}
485+
486+
iidev->client = client;
487+
488+
if (slave_adap) {
489+
struct i2c_board_info binfo;
490+
491+
memset(&binfo, 0, sizeof(binfo));
492+
strscpy(binfo.type, "ipmb-slave", I2C_NAME_SIZE);
493+
binfo.addr = client->addr;
494+
binfo.flags = I2C_CLIENT_SLAVE;
495+
slave = i2c_new_client_device(slave_adap, &binfo);
496+
i2c_put_adapter(slave_adap);
497+
if (IS_ERR(slave)) {
498+
rv = PTR_ERR(slave);
499+
dev_notice(&client->dev,
500+
"Could not allocate slave device: %d\n", rv);
501+
return rv;
502+
}
503+
i2c_set_clientdata(slave, iidev);
504+
} else {
505+
slave = client;
506+
}
469507
i2c_set_clientdata(client, iidev);
470-
client->flags |= I2C_CLIENT_SLAVE;
508+
slave->flags |= I2C_CLIENT_SLAVE;
471509

472-
rv = i2c_slave_register(client, ipmi_ipmb_slave_cb);
510+
rv = i2c_slave_register(slave, ipmi_ipmb_slave_cb);
473511
if (rv)
474-
return rv;
475-
476-
iidev->client = client;
512+
goto out_err;
513+
iidev->slave = slave;
514+
slave = NULL;
477515

478516
iidev->handlers.flags = IPMI_SMI_CAN_HANDLE_IPMB_DIRECT;
479517
iidev->handlers.start_processing = ipmi_ipmb_start_processing;
@@ -504,6 +542,8 @@ static int ipmi_ipmb_probe(struct i2c_client *client,
504542
return 0;
505543

506544
out_err:
545+
if (slave && slave != client)
546+
i2c_unregister_device(slave);
507547
ipmi_ipmb_remove(client);
508548
return rv;
509549
}

0 commit comments

Comments
 (0)