1 // SPDX-License-Identifier: GPL-2.0
3 * I2C Address Translator
5 * Copyright (c) 2019,2022 Luca Ceresoli <luca@lucaceresoli.net>
6 * Copyright (c) 2022,2023 Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
8 * Originally based on i2c-mux.c
11 #include <linux/fwnode.h>
12 #include <linux/i2c-atr.h>
13 #include <linux/i2c.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
20 #define ATR_MAX_ADAPTERS 100 /* Just a sanity limit */
21 #define ATR_MAX_SYMLINK_LEN 11 /* Longest name is 10 chars: "channel-99" */
24 * struct i2c_atr_alias_pair - Holds the alias assigned to a client.
26 * @client: Pointer to the client on the child bus
27 * @alias: I2C alias address assigned by the driver.
28 * This is the address that will be used to issue I2C transactions
29 * on the parent (physical) bus.
31 struct i2c_atr_alias_pair
{
32 struct list_head node
;
33 const struct i2c_client
*client
;
38 * struct i2c_atr_chan - Data for a channel.
39 * @adap: The &struct i2c_adapter for the channel
40 * @atr: The parent I2C ATR
41 * @chan_id: The ID of this channel
42 * @alias_list: List of @struct i2c_atr_alias_pair containing the
44 * @orig_addrs_lock: Mutex protecting @orig_addrs
45 * @orig_addrs: Buffer used to store the original addresses during transmit
46 * @orig_addrs_size: Size of @orig_addrs
49 struct i2c_adapter adap
;
53 struct list_head alias_list
;
55 /* Lock orig_addrs during xfer */
56 struct mutex orig_addrs_lock
;
58 unsigned int orig_addrs_size
;
62 * struct i2c_atr - The I2C ATR instance
63 * @parent: The parent &struct i2c_adapter
64 * @dev: The device that owns the I2C ATR instance
65 * @ops: &struct i2c_atr_ops
66 * @priv: Private driver data, set with i2c_atr_set_driver_data()
67 * @algo: The &struct i2c_algorithm for adapters
68 * @lock: Lock for the I2C bus segment (see &struct i2c_lock_operations)
69 * @max_adapters: Maximum number of adapters this I2C ATR can have
70 * @num_aliases: Number of aliases in the aliases array
71 * @aliases: The aliases array
72 * @alias_mask_lock: Lock protecting alias_use_mask
73 * @alias_use_mask: Bitmask for used aliases in aliases array
74 * @i2c_nb: Notifier for remote client add & del events
75 * @adapter: Array of adapters
78 struct i2c_adapter
*parent
;
80 const struct i2c_atr_ops
*ops
;
84 struct i2c_algorithm algo
;
85 /* lock for the I2C bus segment (see struct i2c_lock_operations) */
91 /* Protects alias_use_mask */
92 spinlock_t alias_mask_lock
;
93 unsigned long *alias_use_mask
;
95 struct notifier_block i2c_nb
;
97 struct i2c_adapter
*adapter
[] __counted_by(max_adapters
);
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
)
104 struct i2c_atr_alias_pair
*c2a
;
106 list_for_each_entry(c2a
, list
, node
) {
107 if (c2a
->client
== client
)
114 static struct i2c_atr_alias_pair
*
115 i2c_atr_find_mapping_by_addr(const struct list_head
*list
, u16 phys_addr
)
117 struct i2c_atr_alias_pair
*c2a
;
119 list_for_each_entry(c2a
, list
, node
) {
120 if (c2a
->client
->addr
== phys_addr
)
128 * Replace all message addresses with their aliases, saving the original
131 * This function is internal for use in i2c_atr_master_xfer(). It must be
132 * followed by i2c_atr_unmap_msgs() to restore the original addresses.
134 static int i2c_atr_map_msgs(struct i2c_atr_chan
*chan
, struct i2c_msg
*msgs
,
137 struct i2c_atr
*atr
= chan
->atr
;
138 static struct i2c_atr_alias_pair
*c2a
;
141 /* Ensure we have enough room to save the original addresses */
142 if (unlikely(chan
->orig_addrs_size
< num
)) {
145 /* We don't care about old data, hence no realloc() */
146 new_buf
= kmalloc_array(num
, sizeof(*new_buf
), GFP_KERNEL
);
150 kfree(chan
->orig_addrs
);
151 chan
->orig_addrs
= new_buf
;
152 chan
->orig_addrs_size
= num
;
155 for (i
= 0; i
< num
; i
++) {
156 chan
->orig_addrs
[i
] = msgs
[i
].addr
;
158 c2a
= i2c_atr_find_mapping_by_addr(&chan
->alias_list
,
161 dev_err(atr
->dev
, "client 0x%02x not mapped!\n",
165 msgs
[i
].addr
= chan
->orig_addrs
[i
];
170 msgs
[i
].addr
= c2a
->alias
;
177 * Restore all message address aliases with the original addresses. This
178 * function is internal for use in i2c_atr_master_xfer() and for this reason it
179 * needs no null and size checks on orig_addr.
181 * @see i2c_atr_map_msgs()
183 static void i2c_atr_unmap_msgs(struct i2c_atr_chan
*chan
, struct i2c_msg
*msgs
,
188 for (i
= 0; i
< num
; i
++)
189 msgs
[i
].addr
= chan
->orig_addrs
[i
];
192 static int i2c_atr_master_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
195 struct i2c_atr_chan
*chan
= adap
->algo_data
;
196 struct i2c_atr
*atr
= chan
->atr
;
197 struct i2c_adapter
*parent
= atr
->parent
;
200 /* Translate addresses */
201 mutex_lock(&chan
->orig_addrs_lock
);
203 ret
= i2c_atr_map_msgs(chan
, msgs
, num
);
207 /* Perform the transfer */
208 ret
= i2c_transfer(parent
, msgs
, num
);
210 /* Restore addresses */
211 i2c_atr_unmap_msgs(chan
, msgs
, num
);
214 mutex_unlock(&chan
->orig_addrs_lock
);
219 static int i2c_atr_smbus_xfer(struct i2c_adapter
*adap
, u16 addr
,
220 unsigned short flags
, char read_write
, u8 command
,
221 int size
, union i2c_smbus_data
*data
)
223 struct i2c_atr_chan
*chan
= adap
->algo_data
;
224 struct i2c_atr
*atr
= chan
->atr
;
225 struct i2c_adapter
*parent
= atr
->parent
;
226 struct i2c_atr_alias_pair
*c2a
;
228 c2a
= i2c_atr_find_mapping_by_addr(&chan
->alias_list
, addr
);
230 dev_err(atr
->dev
, "client 0x%02x not mapped!\n", addr
);
234 return i2c_smbus_xfer(parent
, c2a
->alias
, flags
, read_write
, command
,
238 static u32
i2c_atr_functionality(struct i2c_adapter
*adap
)
240 struct i2c_atr_chan
*chan
= adap
->algo_data
;
241 struct i2c_adapter
*parent
= chan
->atr
->parent
;
243 return parent
->algo
->functionality(parent
);
246 static void i2c_atr_lock_bus(struct i2c_adapter
*adapter
, unsigned int flags
)
248 struct i2c_atr_chan
*chan
= adapter
->algo_data
;
249 struct i2c_atr
*atr
= chan
->atr
;
251 mutex_lock(&atr
->lock
);
254 static int i2c_atr_trylock_bus(struct i2c_adapter
*adapter
, unsigned int flags
)
256 struct i2c_atr_chan
*chan
= adapter
->algo_data
;
257 struct i2c_atr
*atr
= chan
->atr
;
259 return mutex_trylock(&atr
->lock
);
262 static void i2c_atr_unlock_bus(struct i2c_adapter
*adapter
, unsigned int flags
)
264 struct i2c_atr_chan
*chan
= adapter
->algo_data
;
265 struct i2c_atr
*atr
= chan
->atr
;
267 mutex_unlock(&atr
->lock
);
270 static const struct i2c_lock_operations i2c_atr_lock_ops
= {
271 .lock_bus
= i2c_atr_lock_bus
,
272 .trylock_bus
= i2c_atr_trylock_bus
,
273 .unlock_bus
= i2c_atr_unlock_bus
,
276 static int i2c_atr_reserve_alias(struct i2c_atr
*atr
)
280 spin_lock(&atr
->alias_mask_lock
);
282 idx
= find_first_zero_bit(atr
->alias_use_mask
, atr
->num_aliases
);
283 if (idx
>= atr
->num_aliases
) {
284 spin_unlock(&atr
->alias_mask_lock
);
285 dev_err(atr
->dev
, "failed to find a free alias\n");
289 set_bit(idx
, atr
->alias_use_mask
);
291 spin_unlock(&atr
->alias_mask_lock
);
293 return atr
->aliases
[idx
];
296 static void i2c_atr_release_alias(struct i2c_atr
*atr
, u16 alias
)
300 spin_lock(&atr
->alias_mask_lock
);
302 for (idx
= 0; idx
< atr
->num_aliases
; ++idx
) {
303 if (atr
->aliases
[idx
] == alias
) {
304 clear_bit(idx
, atr
->alias_use_mask
);
305 spin_unlock(&atr
->alias_mask_lock
);
310 spin_unlock(&atr
->alias_mask_lock
);
312 /* This should never happen */
313 dev_warn(atr
->dev
, "Unable to find mapped alias\n");
316 static int i2c_atr_attach_client(struct i2c_adapter
*adapter
,
317 const struct i2c_client
*client
)
319 struct i2c_atr_chan
*chan
= adapter
->algo_data
;
320 struct i2c_atr
*atr
= chan
->atr
;
321 struct i2c_atr_alias_pair
*c2a
;
325 ret
= i2c_atr_reserve_alias(atr
);
331 c2a
= kzalloc(sizeof(*c2a
), GFP_KERNEL
);
334 goto err_release_alias
;
337 ret
= atr
->ops
->attach_client(atr
, chan
->chan_id
, client
, alias
);
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
);
344 c2a
->client
= client
;
346 list_add(&c2a
->node
, &chan
->alias_list
);
353 i2c_atr_release_alias(atr
, alias
);
358 static void i2c_atr_detach_client(struct i2c_adapter
*adapter
,
359 const struct i2c_client
*client
)
361 struct i2c_atr_chan
*chan
= adapter
->algo_data
;
362 struct i2c_atr
*atr
= chan
->atr
;
363 struct i2c_atr_alias_pair
*c2a
;
365 atr
->ops
->detach_client(atr
, chan
->chan_id
, client
);
367 c2a
= i2c_atr_find_mapping_by_client(&chan
->alias_list
, client
);
369 /* This should never happen */
370 dev_warn(atr
->dev
, "Unable to find address mapping\n");
374 i2c_atr_release_alias(atr
, c2a
->alias
);
377 "chan%u: client 0x%02x unmapped from alias 0x%02x (%s)\n",
378 chan
->chan_id
, client
->addr
, c2a
->alias
, client
->name
);
380 list_del(&c2a
->node
);
384 static int i2c_atr_bus_notifier_call(struct notifier_block
*nb
,
385 unsigned long event
, void *device
)
387 struct i2c_atr
*atr
= container_of(nb
, struct i2c_atr
, i2c_nb
);
388 struct device
*dev
= device
;
389 struct i2c_client
*client
;
393 client
= i2c_verify_client(dev
);
397 /* Is the client in one of our adapters? */
398 for (chan_id
= 0; chan_id
< atr
->max_adapters
; ++chan_id
) {
399 if (client
->adapter
== atr
->adapter
[chan_id
])
403 if (chan_id
== atr
->max_adapters
)
407 case BUS_NOTIFY_ADD_DEVICE
:
408 ret
= i2c_atr_attach_client(client
->adapter
, client
);
411 "Failed to attach remote client '%s': %d\n",
415 case BUS_NOTIFY_DEL_DEVICE
:
416 i2c_atr_detach_client(client
->adapter
, client
);
426 static int i2c_atr_parse_alias_pool(struct i2c_atr
*atr
)
428 struct device
*dev
= atr
->dev
;
429 unsigned long *alias_use_mask
;
436 ret
= fwnode_property_count_u32(dev_fwnode(dev
), "i2c-alias-pool");
438 dev_err(dev
, "Failed to count 'i2c-alias-pool' property: %d\n",
448 aliases32
= kcalloc(num_aliases
, sizeof(*aliases32
), GFP_KERNEL
);
452 ret
= fwnode_property_read_u32_array(dev_fwnode(dev
), "i2c-alias-pool",
453 aliases32
, num_aliases
);
455 dev_err(dev
, "Failed to read 'i2c-alias-pool' property: %d\n",
457 goto err_free_aliases32
;
460 aliases16
= kcalloc(num_aliases
, sizeof(*aliases16
), GFP_KERNEL
);
463 goto err_free_aliases32
;
466 for (i
= 0; i
< num_aliases
; i
++) {
467 if (!(aliases32
[i
] & 0xffff0000)) {
468 aliases16
[i
] = aliases32
[i
];
472 dev_err(dev
, "Failed to parse 'i2c-alias-pool' property: I2C flags are not supported\n");
474 goto err_free_aliases16
;
477 alias_use_mask
= bitmap_zalloc(num_aliases
, GFP_KERNEL
);
478 if (!alias_use_mask
) {
480 goto err_free_aliases16
;
485 atr
->num_aliases
= num_aliases
;
486 atr
->aliases
= aliases16
;
487 atr
->alias_use_mask
= alias_use_mask
;
489 dev_dbg(dev
, "i2c-alias-pool has %zu aliases", atr
->num_aliases
);
500 struct i2c_atr
*i2c_atr_new(struct i2c_adapter
*parent
, struct device
*dev
,
501 const struct i2c_atr_ops
*ops
, int max_adapters
)
506 if (max_adapters
> ATR_MAX_ADAPTERS
)
507 return ERR_PTR(-EINVAL
);
509 if (!ops
|| !ops
->attach_client
|| !ops
->detach_client
)
510 return ERR_PTR(-EINVAL
);
512 atr
= kzalloc(struct_size(atr
, adapter
, max_adapters
), GFP_KERNEL
);
514 return ERR_PTR(-ENOMEM
);
516 mutex_init(&atr
->lock
);
517 spin_lock_init(&atr
->alias_mask_lock
);
519 atr
->parent
= parent
;
522 atr
->max_adapters
= max_adapters
;
524 if (parent
->algo
->master_xfer
)
525 atr
->algo
.master_xfer
= i2c_atr_master_xfer
;
526 if (parent
->algo
->smbus_xfer
)
527 atr
->algo
.smbus_xfer
= i2c_atr_smbus_xfer
;
528 atr
->algo
.functionality
= i2c_atr_functionality
;
530 ret
= i2c_atr_parse_alias_pool(atr
);
532 goto err_destroy_mutex
;
534 atr
->i2c_nb
.notifier_call
= i2c_atr_bus_notifier_call
;
535 ret
= bus_register_notifier(&i2c_bus_type
, &atr
->i2c_nb
);
537 goto err_free_aliases
;
542 bitmap_free(atr
->alias_use_mask
);
545 mutex_destroy(&atr
->lock
);
550 EXPORT_SYMBOL_NS_GPL(i2c_atr_new
, I2C_ATR
);
552 void i2c_atr_delete(struct i2c_atr
*atr
)
556 for (i
= 0; i
< atr
->max_adapters
; ++i
)
557 WARN_ON(atr
->adapter
[i
]);
559 bus_unregister_notifier(&i2c_bus_type
, &atr
->i2c_nb
);
560 bitmap_free(atr
->alias_use_mask
);
562 mutex_destroy(&atr
->lock
);
565 EXPORT_SYMBOL_NS_GPL(i2c_atr_delete
, I2C_ATR
);
567 int i2c_atr_add_adapter(struct i2c_atr
*atr
, u32 chan_id
,
568 struct device
*adapter_parent
,
569 struct fwnode_handle
*bus_handle
)
571 struct i2c_adapter
*parent
= atr
->parent
;
572 struct device
*dev
= atr
->dev
;
573 struct i2c_atr_chan
*chan
;
574 char symlink_name
[ATR_MAX_SYMLINK_LEN
];
577 if (chan_id
>= atr
->max_adapters
) {
578 dev_err(dev
, "No room for more i2c-atr adapters\n");
582 if (atr
->adapter
[chan_id
]) {
583 dev_err(dev
, "Adapter %d already present\n", chan_id
);
587 chan
= kzalloc(sizeof(*chan
), GFP_KERNEL
);
592 adapter_parent
= dev
;
595 chan
->chan_id
= chan_id
;
596 INIT_LIST_HEAD(&chan
->alias_list
);
597 mutex_init(&chan
->orig_addrs_lock
);
599 snprintf(chan
->adap
.name
, sizeof(chan
->adap
.name
), "i2c-%d-atr-%d",
600 i2c_adapter_id(parent
), chan_id
);
601 chan
->adap
.owner
= THIS_MODULE
;
602 chan
->adap
.algo
= &atr
->algo
;
603 chan
->adap
.algo_data
= chan
;
604 chan
->adap
.dev
.parent
= adapter_parent
;
605 chan
->adap
.retries
= parent
->retries
;
606 chan
->adap
.timeout
= parent
->timeout
;
607 chan
->adap
.quirks
= parent
->quirks
;
608 chan
->adap
.lock_ops
= &i2c_atr_lock_ops
;
611 device_set_node(&chan
->adap
.dev
, fwnode_handle_get(bus_handle
));
613 struct fwnode_handle
*atr_node
;
614 struct fwnode_handle
*child
;
617 atr_node
= device_get_named_child_node(dev
, "i2c-atr");
619 fwnode_for_each_child_node(atr_node
, child
) {
620 ret
= fwnode_property_read_u32(child
, "reg", ®
);
627 device_set_node(&chan
->adap
.dev
, child
);
628 fwnode_handle_put(atr_node
);
631 atr
->adapter
[chan_id
] = &chan
->adap
;
633 ret
= i2c_add_adapter(&chan
->adap
);
635 dev_err(dev
, "failed to add atr-adapter %u (error=%d)\n",
640 snprintf(symlink_name
, sizeof(symlink_name
), "channel-%u",
643 ret
= sysfs_create_link(&chan
->adap
.dev
.kobj
, &dev
->kobj
, "atr_device");
645 dev_warn(dev
, "can't create symlink to atr device\n");
646 ret
= sysfs_create_link(&dev
->kobj
, &chan
->adap
.dev
.kobj
, symlink_name
);
648 dev_warn(dev
, "can't create symlink for channel %u\n", chan_id
);
650 dev_dbg(dev
, "Added ATR child bus %d\n", i2c_adapter_id(&chan
->adap
));
655 fwnode_handle_put(dev_fwnode(&chan
->adap
.dev
));
656 mutex_destroy(&chan
->orig_addrs_lock
);
660 EXPORT_SYMBOL_NS_GPL(i2c_atr_add_adapter
, I2C_ATR
);
662 void i2c_atr_del_adapter(struct i2c_atr
*atr
, u32 chan_id
)
664 char symlink_name
[ATR_MAX_SYMLINK_LEN
];
665 struct i2c_adapter
*adap
;
666 struct i2c_atr_chan
*chan
;
667 struct fwnode_handle
*fwnode
;
668 struct device
*dev
= atr
->dev
;
670 adap
= atr
->adapter
[chan_id
];
674 chan
= adap
->algo_data
;
675 fwnode
= dev_fwnode(&adap
->dev
);
677 dev_dbg(dev
, "Removing ATR child bus %d\n", i2c_adapter_id(adap
));
679 snprintf(symlink_name
, sizeof(symlink_name
), "channel-%u",
681 sysfs_remove_link(&dev
->kobj
, symlink_name
);
682 sysfs_remove_link(&chan
->adap
.dev
.kobj
, "atr_device");
684 i2c_del_adapter(adap
);
686 atr
->adapter
[chan_id
] = NULL
;
688 fwnode_handle_put(fwnode
);
689 mutex_destroy(&chan
->orig_addrs_lock
);
690 kfree(chan
->orig_addrs
);
693 EXPORT_SYMBOL_NS_GPL(i2c_atr_del_adapter
, I2C_ATR
);
695 void i2c_atr_set_driver_data(struct i2c_atr
*atr
, void *data
)
699 EXPORT_SYMBOL_NS_GPL(i2c_atr_set_driver_data
, I2C_ATR
);
701 void *i2c_atr_get_driver_data(struct i2c_atr
*atr
)
705 EXPORT_SYMBOL_NS_GPL(i2c_atr_get_driver_data
, I2C_ATR
);
707 MODULE_AUTHOR("Luca Ceresoli <luca.ceresoli@bootlin.com>");
708 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>");
709 MODULE_DESCRIPTION("I2C Address Translator");
710 MODULE_LICENSE("GPL");