Skip to content

Commit 818bd48

Browse files
rgantoisWolfram Sang
authored andcommitted
i2c: use client addresses directly in ATR interface
The I2C Address Translator (ATR) module defines mappings from i2c_client structs to aliases. However, only the physical address of each i2c_client struct is actually relevant to the workings of the ATR module. Moreover, some drivers require address translation functionality but do not allocate i2c_client structs, accessing the adapter directly instead. The SFP subsystem is an example of this. Replace the "i2c_client" field of the i2c_atr_alias_pair struct with a u16 "addr" field. Rewrite helper functions and callbacks as needed. Reviewed-by: Tomi Valkeinen <[email protected]> Tested-by: Tomi Valkeinen <[email protected]> Signed-off-by: Romain Gantois <[email protected]> Acked-by: Andi Shyti <[email protected]> Signed-off-by: Wolfram Sang <[email protected]>
1 parent 3ec29d5 commit 818bd48

File tree

3 files changed

+41
-55
lines changed

3 files changed

+41
-55
lines changed

drivers/i2c/i2c-atr.c

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
#define ATR_MAX_SYMLINK_LEN 11 /* Longest name is 10 chars: "channel-99" */
2222

2323
/**
24-
* struct i2c_atr_alias_pair - Holds the alias assigned to a client.
24+
* struct i2c_atr_alias_pair - Holds the alias assigned to a client address.
2525
* @node: List node
26-
* @client: Pointer to the client on the child bus
26+
* @addr: Address of the client on the child bus.
2727
* @alias: I2C alias address assigned by the driver.
2828
* This is the address that will be used to issue I2C transactions
2929
* on the parent (physical) bus.
3030
*/
3131
struct i2c_atr_alias_pair {
3232
struct list_head node;
33-
const struct i2c_client *client;
33+
u16 addr;
3434
u16 alias;
3535
};
3636

@@ -97,27 +97,13 @@ struct i2c_atr {
9797
struct i2c_adapter *adapter[] __counted_by(max_adapters);
9898
};
9999

100-
static struct i2c_atr_alias_pair *
101-
i2c_atr_find_mapping_by_client(const struct list_head *list,
102-
const struct i2c_client *client)
103-
{
104-
struct i2c_atr_alias_pair *c2a;
105-
106-
list_for_each_entry(c2a, list, node) {
107-
if (c2a->client == client)
108-
return c2a;
109-
}
110-
111-
return NULL;
112-
}
113-
114100
static struct i2c_atr_alias_pair *
115101
i2c_atr_find_mapping_by_addr(const struct list_head *list, u16 phys_addr)
116102
{
117103
struct i2c_atr_alias_pair *c2a;
118104

119105
list_for_each_entry(c2a, list, node) {
120-
if (c2a->client->addr == phys_addr)
106+
if (c2a->addr == phys_addr)
121107
return c2a;
122108
}
123109

@@ -313,8 +299,8 @@ static void i2c_atr_release_alias(struct i2c_atr *atr, u16 alias)
313299
dev_warn(atr->dev, "Unable to find mapped alias\n");
314300
}
315301

316-
static int i2c_atr_attach_client(struct i2c_adapter *adapter,
317-
const struct i2c_client *client)
302+
static int i2c_atr_attach_addr(struct i2c_adapter *adapter,
303+
u16 addr)
318304
{
319305
struct i2c_atr_chan *chan = adapter->algo_data;
320306
struct i2c_atr *atr = chan->atr;
@@ -334,14 +320,14 @@ static int i2c_atr_attach_client(struct i2c_adapter *adapter,
334320
goto err_release_alias;
335321
}
336322

337-
ret = atr->ops->attach_client(atr, chan->chan_id, client, alias);
323+
ret = atr->ops->attach_addr(atr, chan->chan_id, addr, alias);
338324
if (ret)
339325
goto err_free;
340326

341-
dev_dbg(atr->dev, "chan%u: client 0x%02x mapped at alias 0x%02x (%s)\n",
342-
chan->chan_id, client->addr, alias, client->name);
327+
dev_dbg(atr->dev, "chan%u: using alias 0x%02x for addr 0x%02x\n",
328+
chan->chan_id, alias, addr);
343329

344-
c2a->client = client;
330+
c2a->addr = addr;
345331
c2a->alias = alias;
346332
list_add(&c2a->node, &chan->alias_list);
347333

@@ -355,16 +341,16 @@ static int i2c_atr_attach_client(struct i2c_adapter *adapter,
355341
return ret;
356342
}
357343

358-
static void i2c_atr_detach_client(struct i2c_adapter *adapter,
359-
const struct i2c_client *client)
344+
static void i2c_atr_detach_addr(struct i2c_adapter *adapter,
345+
u16 addr)
360346
{
361347
struct i2c_atr_chan *chan = adapter->algo_data;
362348
struct i2c_atr *atr = chan->atr;
363349
struct i2c_atr_alias_pair *c2a;
364350

365-
atr->ops->detach_client(atr, chan->chan_id, client);
351+
atr->ops->detach_addr(atr, chan->chan_id, addr);
366352

367-
c2a = i2c_atr_find_mapping_by_client(&chan->alias_list, client);
353+
c2a = i2c_atr_find_mapping_by_addr(&chan->alias_list, addr);
368354
if (!c2a) {
369355
/* This should never happen */
370356
dev_warn(atr->dev, "Unable to find address mapping\n");
@@ -374,8 +360,8 @@ static void i2c_atr_detach_client(struct i2c_adapter *adapter,
374360
i2c_atr_release_alias(atr, c2a->alias);
375361

376362
dev_dbg(atr->dev,
377-
"chan%u: client 0x%02x unmapped from alias 0x%02x (%s)\n",
378-
chan->chan_id, client->addr, c2a->alias, client->name);
363+
"chan%u: detached alias 0x%02x from addr 0x%02x\n",
364+
chan->chan_id, c2a->alias, addr);
379365

380366
list_del(&c2a->node);
381367
kfree(c2a);
@@ -405,15 +391,15 @@ static int i2c_atr_bus_notifier_call(struct notifier_block *nb,
405391

406392
switch (event) {
407393
case BUS_NOTIFY_ADD_DEVICE:
408-
ret = i2c_atr_attach_client(client->adapter, client);
394+
ret = i2c_atr_attach_addr(client->adapter, client->addr);
409395
if (ret)
410396
dev_err(atr->dev,
411397
"Failed to attach remote client '%s': %d\n",
412398
dev_name(dev), ret);
413399
break;
414400

415401
case BUS_NOTIFY_REMOVED_DEVICE:
416-
i2c_atr_detach_client(client->adapter, client);
402+
i2c_atr_detach_addr(client->adapter, client->addr);
417403
break;
418404

419405
default:
@@ -506,7 +492,7 @@ struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent, struct device *dev,
506492
if (max_adapters > ATR_MAX_ADAPTERS)
507493
return ERR_PTR(-EINVAL);
508494

509-
if (!ops || !ops->attach_client || !ops->detach_client)
495+
if (!ops || !ops->attach_addr || !ops->detach_addr)
510496
return ERR_PTR(-EINVAL);
511497

512498
atr = kzalloc(struct_size(atr, adapter, max_adapters), GFP_KERNEL);

drivers/media/i2c/ds90ub960.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,8 +1049,8 @@ static int ub960_ind_update_bits(struct ub960_data *priv, u8 block, u8 reg,
10491049
* I2C-ATR (address translator)
10501050
*/
10511051

1052-
static int ub960_atr_attach_client(struct i2c_atr *atr, u32 chan_id,
1053-
const struct i2c_client *client, u16 alias)
1052+
static int ub960_atr_attach_addr(struct i2c_atr *atr, u32 chan_id,
1053+
u16 addr, u16 alias)
10541054
{
10551055
struct ub960_data *priv = i2c_atr_get_driver_data(atr);
10561056
struct ub960_rxport *rxport = priv->rxports[chan_id];
@@ -1069,21 +1069,21 @@ static int ub960_atr_attach_client(struct i2c_atr *atr, u32 chan_id,
10691069
return -EADDRNOTAVAIL;
10701070
}
10711071

1072-
rxport->aliased_addrs[reg_idx] = client->addr;
1072+
rxport->aliased_addrs[reg_idx] = addr;
10731073

10741074
ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ID(reg_idx),
1075-
client->addr << 1);
1075+
addr << 1);
10761076
ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ALIAS(reg_idx),
10771077
alias << 1);
10781078

10791079
dev_dbg(dev, "rx%u: client 0x%02x assigned alias 0x%02x at slot %u\n",
1080-
rxport->nport, client->addr, alias, reg_idx);
1080+
rxport->nport, addr, alias, reg_idx);
10811081

10821082
return 0;
10831083
}
10841084

1085-
static void ub960_atr_detach_client(struct i2c_atr *atr, u32 chan_id,
1086-
const struct i2c_client *client)
1085+
static void ub960_atr_detach_addr(struct i2c_atr *atr, u32 chan_id,
1086+
u16 addr)
10871087
{
10881088
struct ub960_data *priv = i2c_atr_get_driver_data(atr);
10891089
struct ub960_rxport *rxport = priv->rxports[chan_id];
@@ -1093,13 +1093,13 @@ static void ub960_atr_detach_client(struct i2c_atr *atr, u32 chan_id,
10931093
guard(mutex)(&rxport->aliased_addrs_lock);
10941094

10951095
for (reg_idx = 0; reg_idx < ARRAY_SIZE(rxport->aliased_addrs); reg_idx++) {
1096-
if (rxport->aliased_addrs[reg_idx] == client->addr)
1096+
if (rxport->aliased_addrs[reg_idx] == addr)
10971097
break;
10981098
}
10991099

11001100
if (reg_idx == ARRAY_SIZE(rxport->aliased_addrs)) {
11011101
dev_err(dev, "rx%u: client 0x%02x is not mapped!\n",
1102-
rxport->nport, client->addr);
1102+
rxport->nport, addr);
11031103
return;
11041104
}
11051105

@@ -1108,12 +1108,12 @@ static void ub960_atr_detach_client(struct i2c_atr *atr, u32 chan_id,
11081108
ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ALIAS(reg_idx), 0);
11091109

11101110
dev_dbg(dev, "rx%u: client 0x%02x released at slot %u\n", rxport->nport,
1111-
client->addr, reg_idx);
1111+
addr, reg_idx);
11121112
}
11131113

11141114
static const struct i2c_atr_ops ub960_atr_ops = {
1115-
.attach_client = ub960_atr_attach_client,
1116-
.detach_client = ub960_atr_detach_client,
1115+
.attach_addr = ub960_atr_attach_addr,
1116+
.detach_addr = ub960_atr_detach_addr,
11171117
};
11181118

11191119
static int ub960_init_atr(struct ub960_data *priv)

include/linux/i2c-atr.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ struct i2c_atr;
2020

2121
/**
2222
* struct i2c_atr_ops - Callbacks from ATR to the device driver.
23-
* @attach_client: Notify the driver of a new device connected on a child
24-
* bus, with the alias assigned to it. The driver must
25-
* configure the hardware to use the alias.
26-
* @detach_client: Notify the driver of a device getting disconnected. The
27-
* driver must configure the hardware to stop using the
28-
* alias.
23+
* @attach_addr: Notify the driver of a new device connected on a child
24+
* bus, with the alias assigned to it. The driver must
25+
* configure the hardware to use the alias.
26+
* @detach_addr: Notify the driver of a device getting disconnected. The
27+
* driver must configure the hardware to stop using the
28+
* alias.
2929
*
3030
* All these functions return 0 on success, a negative error code otherwise.
3131
*/
3232
struct i2c_atr_ops {
33-
int (*attach_client)(struct i2c_atr *atr, u32 chan_id,
34-
const struct i2c_client *client, u16 alias);
35-
void (*detach_client)(struct i2c_atr *atr, u32 chan_id,
36-
const struct i2c_client *client);
33+
int (*attach_addr)(struct i2c_atr *atr, u32 chan_id,
34+
u16 addr, u16 alias);
35+
void (*detach_addr)(struct i2c_atr *atr, u32 chan_id,
36+
u16 addr);
3737
};
3838

3939
/**

0 commit comments

Comments
 (0)