1a076a860SLuca Ceresoli // SPDX-License-Identifier: GPL-2.0
2a076a860SLuca Ceresoli /*
3a076a860SLuca Ceresoli * I2C Address Translator
4a076a860SLuca Ceresoli *
5a076a860SLuca Ceresoli * Copyright (c) 2019,2022 Luca Ceresoli <luca@lucaceresoli.net>
6a076a860SLuca Ceresoli * Copyright (c) 2022,2023 Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
7a076a860SLuca Ceresoli *
8a076a860SLuca Ceresoli * Originally based on i2c-mux.c
9a076a860SLuca Ceresoli */
10a076a860SLuca Ceresoli
11a076a860SLuca Ceresoli #include <linux/fwnode.h>
12a076a860SLuca Ceresoli #include <linux/i2c-atr.h>
13a076a860SLuca Ceresoli #include <linux/i2c.h>
14a076a860SLuca Ceresoli #include <linux/kernel.h>
15a076a860SLuca Ceresoli #include <linux/module.h>
16a076a860SLuca Ceresoli #include <linux/mutex.h>
17a076a860SLuca Ceresoli #include <linux/slab.h>
18a076a860SLuca Ceresoli #include <linux/spinlock.h>
19a076a860SLuca Ceresoli
20a076a860SLuca Ceresoli #define ATR_MAX_ADAPTERS 100 /* Just a sanity limit */
21a076a860SLuca Ceresoli #define ATR_MAX_SYMLINK_LEN 11 /* Longest name is 10 chars: "channel-99" */
22a076a860SLuca Ceresoli
23a076a860SLuca Ceresoli /**
24a076a860SLuca Ceresoli * struct i2c_atr_alias_pair - Holds the alias assigned to a client.
25a076a860SLuca Ceresoli * @node: List node
26a076a860SLuca Ceresoli * @client: Pointer to the client on the child bus
27a076a860SLuca Ceresoli * @alias: I2C alias address assigned by the driver.
28a076a860SLuca Ceresoli * This is the address that will be used to issue I2C transactions
29a076a860SLuca Ceresoli * on the parent (physical) bus.
30a076a860SLuca Ceresoli */
31a076a860SLuca Ceresoli struct i2c_atr_alias_pair {
32a076a860SLuca Ceresoli struct list_head node;
33a076a860SLuca Ceresoli const struct i2c_client *client;
34a076a860SLuca Ceresoli u16 alias;
35a076a860SLuca Ceresoli };
36a076a860SLuca Ceresoli
37a076a860SLuca Ceresoli /**
38a076a860SLuca Ceresoli * struct i2c_atr_chan - Data for a channel.
39a076a860SLuca Ceresoli * @adap: The &struct i2c_adapter for the channel
40a076a860SLuca Ceresoli * @atr: The parent I2C ATR
41a076a860SLuca Ceresoli * @chan_id: The ID of this channel
42a076a860SLuca Ceresoli * @alias_list: List of @struct i2c_atr_alias_pair containing the
43a076a860SLuca Ceresoli * assigned aliases
44a076a860SLuca Ceresoli * @orig_addrs_lock: Mutex protecting @orig_addrs
45a076a860SLuca Ceresoli * @orig_addrs: Buffer used to store the original addresses during transmit
46a076a860SLuca Ceresoli * @orig_addrs_size: Size of @orig_addrs
47a076a860SLuca Ceresoli */
48a076a860SLuca Ceresoli struct i2c_atr_chan {
49a076a860SLuca Ceresoli struct i2c_adapter adap;
50a076a860SLuca Ceresoli struct i2c_atr *atr;
51a076a860SLuca Ceresoli u32 chan_id;
52a076a860SLuca Ceresoli
53a076a860SLuca Ceresoli struct list_head alias_list;
54a076a860SLuca Ceresoli
55a076a860SLuca Ceresoli /* Lock orig_addrs during xfer */
56a076a860SLuca Ceresoli struct mutex orig_addrs_lock;
57a076a860SLuca Ceresoli u16 *orig_addrs;
58a076a860SLuca Ceresoli unsigned int orig_addrs_size;
59a076a860SLuca Ceresoli };
60a076a860SLuca Ceresoli
61a076a860SLuca Ceresoli /**
62a076a860SLuca Ceresoli * struct i2c_atr - The I2C ATR instance
63a076a860SLuca Ceresoli * @parent: The parent &struct i2c_adapter
64a076a860SLuca Ceresoli * @dev: The device that owns the I2C ATR instance
65a076a860SLuca Ceresoli * @ops: &struct i2c_atr_ops
66a076a860SLuca Ceresoli * @priv: Private driver data, set with i2c_atr_set_driver_data()
67a076a860SLuca Ceresoli * @algo: The &struct i2c_algorithm for adapters
68a076a860SLuca Ceresoli * @lock: Lock for the I2C bus segment (see &struct i2c_lock_operations)
69a076a860SLuca Ceresoli * @max_adapters: Maximum number of adapters this I2C ATR can have
70a076a860SLuca Ceresoli * @num_aliases: Number of aliases in the aliases array
71a076a860SLuca Ceresoli * @aliases: The aliases array
72a076a860SLuca Ceresoli * @alias_mask_lock: Lock protecting alias_use_mask
73a076a860SLuca Ceresoli * @alias_use_mask: Bitmask for used aliases in aliases array
74a076a860SLuca Ceresoli * @i2c_nb: Notifier for remote client add & del events
75a076a860SLuca Ceresoli * @adapter: Array of adapters
76a076a860SLuca Ceresoli */
77a076a860SLuca Ceresoli struct i2c_atr {
78a076a860SLuca Ceresoli struct i2c_adapter *parent;
79a076a860SLuca Ceresoli struct device *dev;
80a076a860SLuca Ceresoli const struct i2c_atr_ops *ops;
81a076a860SLuca Ceresoli
82a076a860SLuca Ceresoli void *priv;
83a076a860SLuca Ceresoli
84a076a860SLuca Ceresoli struct i2c_algorithm algo;
85a076a860SLuca Ceresoli /* lock for the I2C bus segment (see struct i2c_lock_operations) */
86a076a860SLuca Ceresoli struct mutex lock;
87a076a860SLuca Ceresoli int max_adapters;
88a076a860SLuca Ceresoli
89a076a860SLuca Ceresoli size_t num_aliases;
90a076a860SLuca Ceresoli const u16 *aliases;
91a076a860SLuca Ceresoli /* Protects alias_use_mask */
92a076a860SLuca Ceresoli spinlock_t alias_mask_lock;
93a076a860SLuca Ceresoli unsigned long *alias_use_mask;
94a076a860SLuca Ceresoli
95a076a860SLuca Ceresoli struct notifier_block i2c_nb;
96a076a860SLuca Ceresoli
97*3a133a4eSKees Cook struct i2c_adapter *adapter[] __counted_by(max_adapters);
98a076a860SLuca Ceresoli };
99a076a860SLuca Ceresoli
100a076a860SLuca Ceresoli static struct i2c_atr_alias_pair *
i2c_atr_find_mapping_by_client(const struct list_head * list,const struct i2c_client * client)101a076a860SLuca Ceresoli i2c_atr_find_mapping_by_client(const struct list_head *list,
102a076a860SLuca Ceresoli const struct i2c_client *client)
103a076a860SLuca Ceresoli {
104a076a860SLuca Ceresoli struct i2c_atr_alias_pair *c2a;
105a076a860SLuca Ceresoli
106a076a860SLuca Ceresoli list_for_each_entry(c2a, list, node) {
107a076a860SLuca Ceresoli if (c2a->client == client)
108a076a860SLuca Ceresoli return c2a;
109a076a860SLuca Ceresoli }
110a076a860SLuca Ceresoli
111a076a860SLuca Ceresoli return NULL;
112a076a860SLuca Ceresoli }
113a076a860SLuca Ceresoli
114a076a860SLuca Ceresoli static struct i2c_atr_alias_pair *
i2c_atr_find_mapping_by_addr(const struct list_head * list,u16 phys_addr)115a076a860SLuca Ceresoli i2c_atr_find_mapping_by_addr(const struct list_head *list, u16 phys_addr)
116a076a860SLuca Ceresoli {
117a076a860SLuca Ceresoli struct i2c_atr_alias_pair *c2a;
118a076a860SLuca Ceresoli
119a076a860SLuca Ceresoli list_for_each_entry(c2a, list, node) {
120a076a860SLuca Ceresoli if (c2a->client->addr == phys_addr)
121a076a860SLuca Ceresoli return c2a;
122a076a860SLuca Ceresoli }
123a076a860SLuca Ceresoli
124a076a860SLuca Ceresoli return NULL;
125a076a860SLuca Ceresoli }
126a076a860SLuca Ceresoli
127a076a860SLuca Ceresoli /*
128a076a860SLuca Ceresoli * Replace all message addresses with their aliases, saving the original
129a076a860SLuca Ceresoli * addresses.
130a076a860SLuca Ceresoli *
131a076a860SLuca Ceresoli * This function is internal for use in i2c_atr_master_xfer(). It must be
132a076a860SLuca Ceresoli * followed by i2c_atr_unmap_msgs() to restore the original addresses.
133a076a860SLuca Ceresoli */
i2c_atr_map_msgs(struct i2c_atr_chan * chan,struct i2c_msg * msgs,int num)134a076a860SLuca Ceresoli static int i2c_atr_map_msgs(struct i2c_atr_chan *chan, struct i2c_msg *msgs,
135a076a860SLuca Ceresoli int num)
136a076a860SLuca Ceresoli {
137a076a860SLuca Ceresoli struct i2c_atr *atr = chan->atr;
138a076a860SLuca Ceresoli static struct i2c_atr_alias_pair *c2a;
139a076a860SLuca Ceresoli int i;
140a076a860SLuca Ceresoli
141a076a860SLuca Ceresoli /* Ensure we have enough room to save the original addresses */
142a076a860SLuca Ceresoli if (unlikely(chan->orig_addrs_size < num)) {
143a076a860SLuca Ceresoli u16 *new_buf;
144a076a860SLuca Ceresoli
145a076a860SLuca Ceresoli /* We don't care about old data, hence no realloc() */
146a076a860SLuca Ceresoli new_buf = kmalloc_array(num, sizeof(*new_buf), GFP_KERNEL);
147a076a860SLuca Ceresoli if (!new_buf)
148a076a860SLuca Ceresoli return -ENOMEM;
149a076a860SLuca Ceresoli
150a076a860SLuca Ceresoli kfree(chan->orig_addrs);
151a076a860SLuca Ceresoli chan->orig_addrs = new_buf;
152a076a860SLuca Ceresoli chan->orig_addrs_size = num;
153a076a860SLuca Ceresoli }
154a076a860SLuca Ceresoli
155a076a860SLuca Ceresoli for (i = 0; i < num; i++) {
156a076a860SLuca Ceresoli chan->orig_addrs[i] = msgs[i].addr;
157a076a860SLuca Ceresoli
158a076a860SLuca Ceresoli c2a = i2c_atr_find_mapping_by_addr(&chan->alias_list,
159a076a860SLuca Ceresoli msgs[i].addr);
160a076a860SLuca Ceresoli if (!c2a) {
161a076a860SLuca Ceresoli dev_err(atr->dev, "client 0x%02x not mapped!\n",
162a076a860SLuca Ceresoli msgs[i].addr);
163a076a860SLuca Ceresoli
164a076a860SLuca Ceresoli while (i--)
165a076a860SLuca Ceresoli msgs[i].addr = chan->orig_addrs[i];
166a076a860SLuca Ceresoli
167a076a860SLuca Ceresoli return -ENXIO;
168a076a860SLuca Ceresoli }
169a076a860SLuca Ceresoli
170a076a860SLuca Ceresoli msgs[i].addr = c2a->alias;
171a076a860SLuca Ceresoli }
172a076a860SLuca Ceresoli
173a076a860SLuca Ceresoli return 0;
174a076a860SLuca Ceresoli }
175a076a860SLuca Ceresoli
176a076a860SLuca Ceresoli /*
177a076a860SLuca Ceresoli * Restore all message address aliases with the original addresses. This
178a076a860SLuca Ceresoli * function is internal for use in i2c_atr_master_xfer() and for this reason it
179a076a860SLuca Ceresoli * needs no null and size checks on orig_addr.
180a076a860SLuca Ceresoli *
181a076a860SLuca Ceresoli * @see i2c_atr_map_msgs()
182a076a860SLuca Ceresoli */
i2c_atr_unmap_msgs(struct i2c_atr_chan * chan,struct i2c_msg * msgs,int num)183a076a860SLuca Ceresoli static void i2c_atr_unmap_msgs(struct i2c_atr_chan *chan, struct i2c_msg *msgs,
184a076a860SLuca Ceresoli int num)
185a076a860SLuca Ceresoli {
186a076a860SLuca Ceresoli int i;
187a076a860SLuca Ceresoli
188a076a860SLuca Ceresoli for (i = 0; i < num; i++)
189a076a860SLuca Ceresoli msgs[i].addr = chan->orig_addrs[i];
190a076a860SLuca Ceresoli }
191a076a860SLuca Ceresoli
i2c_atr_master_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)192a076a860SLuca Ceresoli static int i2c_atr_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
193a076a860SLuca Ceresoli int num)
194a076a860SLuca Ceresoli {
195a076a860SLuca Ceresoli struct i2c_atr_chan *chan = adap->algo_data;
196a076a860SLuca Ceresoli struct i2c_atr *atr = chan->atr;
197a076a860SLuca Ceresoli struct i2c_adapter *parent = atr->parent;
198a076a860SLuca Ceresoli int ret;
199a076a860SLuca Ceresoli
200a076a860SLuca Ceresoli /* Translate addresses */
201a076a860SLuca Ceresoli mutex_lock(&chan->orig_addrs_lock);
202a076a860SLuca Ceresoli
203a076a860SLuca Ceresoli ret = i2c_atr_map_msgs(chan, msgs, num);
204a076a860SLuca Ceresoli if (ret < 0)
205a076a860SLuca Ceresoli goto err_unlock;
206a076a860SLuca Ceresoli
207a076a860SLuca Ceresoli /* Perform the transfer */
208a076a860SLuca Ceresoli ret = i2c_transfer(parent, msgs, num);
209a076a860SLuca Ceresoli
210a076a860SLuca Ceresoli /* Restore addresses */
211a076a860SLuca Ceresoli i2c_atr_unmap_msgs(chan, msgs, num);
212a076a860SLuca Ceresoli
213a076a860SLuca Ceresoli err_unlock:
214a076a860SLuca Ceresoli mutex_unlock(&chan->orig_addrs_lock);
215a076a860SLuca Ceresoli
216a076a860SLuca Ceresoli return ret;
217a076a860SLuca Ceresoli }
218a076a860SLuca Ceresoli
i2c_atr_smbus_xfer(struct i2c_adapter * adap,u16 addr,unsigned short flags,char read_write,u8 command,int size,union i2c_smbus_data * data)219a076a860SLuca Ceresoli static int i2c_atr_smbus_xfer(struct i2c_adapter *adap, u16 addr,
220a076a860SLuca Ceresoli unsigned short flags, char read_write, u8 command,
221a076a860SLuca Ceresoli int size, union i2c_smbus_data *data)
222a076a860SLuca Ceresoli {
223a076a860SLuca Ceresoli struct i2c_atr_chan *chan = adap->algo_data;
224a076a860SLuca Ceresoli struct i2c_atr *atr = chan->atr;
225a076a860SLuca Ceresoli struct i2c_adapter *parent = atr->parent;
226a076a860SLuca Ceresoli struct i2c_atr_alias_pair *c2a;
227a076a860SLuca Ceresoli
228a076a860SLuca Ceresoli c2a = i2c_atr_find_mapping_by_addr(&chan->alias_list, addr);
229a076a860SLuca Ceresoli if (!c2a) {
230a076a860SLuca Ceresoli dev_err(atr->dev, "client 0x%02x not mapped!\n", addr);
231a076a860SLuca Ceresoli return -ENXIO;
232a076a860SLuca Ceresoli }
233a076a860SLuca Ceresoli
234a076a860SLuca Ceresoli return i2c_smbus_xfer(parent, c2a->alias, flags, read_write, command,
235a076a860SLuca Ceresoli size, data);
236a076a860SLuca Ceresoli }
237a076a860SLuca Ceresoli
i2c_atr_functionality(struct i2c_adapter * adap)238a076a860SLuca Ceresoli static u32 i2c_atr_functionality(struct i2c_adapter *adap)
239a076a860SLuca Ceresoli {
240a076a860SLuca Ceresoli struct i2c_atr_chan *chan = adap->algo_data;
241a076a860SLuca Ceresoli struct i2c_adapter *parent = chan->atr->parent;
242a076a860SLuca Ceresoli
243a076a860SLuca Ceresoli return parent->algo->functionality(parent);
244a076a860SLuca Ceresoli }
245a076a860SLuca Ceresoli
i2c_atr_lock_bus(struct i2c_adapter * adapter,unsigned int flags)246a076a860SLuca Ceresoli static void i2c_atr_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
247a076a860SLuca Ceresoli {
248a076a860SLuca Ceresoli struct i2c_atr_chan *chan = adapter->algo_data;
249a076a860SLuca Ceresoli struct i2c_atr *atr = chan->atr;
250a076a860SLuca Ceresoli
251a076a860SLuca Ceresoli mutex_lock(&atr->lock);
252a076a860SLuca Ceresoli }
253a076a860SLuca Ceresoli
i2c_atr_trylock_bus(struct i2c_adapter * adapter,unsigned int flags)254a076a860SLuca Ceresoli static int i2c_atr_trylock_bus(struct i2c_adapter *adapter, unsigned int flags)
255a076a860SLuca Ceresoli {
256a076a860SLuca Ceresoli struct i2c_atr_chan *chan = adapter->algo_data;
257a076a860SLuca Ceresoli struct i2c_atr *atr = chan->atr;
258a076a860SLuca Ceresoli
259a076a860SLuca Ceresoli return mutex_trylock(&atr->lock);
260a076a860SLuca Ceresoli }
261a076a860SLuca Ceresoli
i2c_atr_unlock_bus(struct i2c_adapter * adapter,unsigned int flags)262a076a860SLuca Ceresoli static void i2c_atr_unlock_bus(struct i2c_adapter *adapter, unsigned int flags)
263a076a860SLuca Ceresoli {
264a076a860SLuca Ceresoli struct i2c_atr_chan *chan = adapter->algo_data;
265a076a860SLuca Ceresoli struct i2c_atr *atr = chan->atr;
266a076a860SLuca Ceresoli
267a076a860SLuca Ceresoli mutex_unlock(&atr->lock);
268a076a860SLuca Ceresoli }
269a076a860SLuca Ceresoli
270a076a860SLuca Ceresoli static const struct i2c_lock_operations i2c_atr_lock_ops = {
271a076a860SLuca Ceresoli .lock_bus = i2c_atr_lock_bus,
272a076a860SLuca Ceresoli .trylock_bus = i2c_atr_trylock_bus,
273a076a860SLuca Ceresoli .unlock_bus = i2c_atr_unlock_bus,
274a076a860SLuca Ceresoli };
275a076a860SLuca Ceresoli
i2c_atr_reserve_alias(struct i2c_atr * atr)276a076a860SLuca Ceresoli static int i2c_atr_reserve_alias(struct i2c_atr *atr)
277a076a860SLuca Ceresoli {
278a076a860SLuca Ceresoli unsigned long idx;
279a076a860SLuca Ceresoli
280a076a860SLuca Ceresoli spin_lock(&atr->alias_mask_lock);
281a076a860SLuca Ceresoli
282a076a860SLuca Ceresoli idx = find_first_zero_bit(atr->alias_use_mask, atr->num_aliases);
283a076a860SLuca Ceresoli if (idx >= atr->num_aliases) {
284a076a860SLuca Ceresoli spin_unlock(&atr->alias_mask_lock);
285a076a860SLuca Ceresoli dev_err(atr->dev, "failed to find a free alias\n");
286a076a860SLuca Ceresoli return -EBUSY;
287a076a860SLuca Ceresoli }
288a076a860SLuca Ceresoli
289a076a860SLuca Ceresoli set_bit(idx, atr->alias_use_mask);
290a076a860SLuca Ceresoli
291a076a860SLuca Ceresoli spin_unlock(&atr->alias_mask_lock);
292a076a860SLuca Ceresoli
293a076a860SLuca Ceresoli return atr->aliases[idx];
294a076a860SLuca Ceresoli }
295a076a860SLuca Ceresoli
i2c_atr_release_alias(struct i2c_atr * atr,u16 alias)296a076a860SLuca Ceresoli static void i2c_atr_release_alias(struct i2c_atr *atr, u16 alias)
297a076a860SLuca Ceresoli {
298a076a860SLuca Ceresoli unsigned int idx;
299a076a860SLuca Ceresoli
300a076a860SLuca Ceresoli spin_lock(&atr->alias_mask_lock);
301a076a860SLuca Ceresoli
302a076a860SLuca Ceresoli for (idx = 0; idx < atr->num_aliases; ++idx) {
303a076a860SLuca Ceresoli if (atr->aliases[idx] == alias) {
304a076a860SLuca Ceresoli clear_bit(idx, atr->alias_use_mask);
305a076a860SLuca Ceresoli spin_unlock(&atr->alias_mask_lock);
306a076a860SLuca Ceresoli return;
307a076a860SLuca Ceresoli }
308a076a860SLuca Ceresoli }
309a076a860SLuca Ceresoli
310a076a860SLuca Ceresoli spin_unlock(&atr->alias_mask_lock);
311a076a860SLuca Ceresoli
312a076a860SLuca Ceresoli /* This should never happen */
313a076a860SLuca Ceresoli dev_warn(atr->dev, "Unable to find mapped alias\n");
314a076a860SLuca Ceresoli }
315a076a860SLuca Ceresoli
i2c_atr_attach_client(struct i2c_adapter * adapter,const struct i2c_client * client)316a076a860SLuca Ceresoli static int i2c_atr_attach_client(struct i2c_adapter *adapter,
317a076a860SLuca Ceresoli const struct i2c_client *client)
318a076a860SLuca Ceresoli {
319a076a860SLuca Ceresoli struct i2c_atr_chan *chan = adapter->algo_data;
320a076a860SLuca Ceresoli struct i2c_atr *atr = chan->atr;
321a076a860SLuca Ceresoli struct i2c_atr_alias_pair *c2a;
322a076a860SLuca Ceresoli u16 alias;
323a076a860SLuca Ceresoli int ret;
324a076a860SLuca Ceresoli
325a076a860SLuca Ceresoli ret = i2c_atr_reserve_alias(atr);
326a076a860SLuca Ceresoli if (ret < 0)
327a076a860SLuca Ceresoli return ret;
328a076a860SLuca Ceresoli
329a076a860SLuca Ceresoli alias = ret;
330a076a860SLuca Ceresoli
331a076a860SLuca Ceresoli c2a = kzalloc(sizeof(*c2a), GFP_KERNEL);
332a076a860SLuca Ceresoli if (!c2a) {
333a076a860SLuca Ceresoli ret = -ENOMEM;
334a076a860SLuca Ceresoli goto err_release_alias;
335a076a860SLuca Ceresoli }
336a076a860SLuca Ceresoli
337a076a860SLuca Ceresoli ret = atr->ops->attach_client(atr, chan->chan_id, client, alias);
338a076a860SLuca Ceresoli if (ret)
339a076a860SLuca Ceresoli goto err_free;
340a076a860SLuca Ceresoli
341a076a860SLuca Ceresoli dev_dbg(atr->dev, "chan%u: client 0x%02x mapped at alias 0x%02x (%s)\n",
342a076a860SLuca Ceresoli chan->chan_id, client->addr, alias, client->name);
343a076a860SLuca Ceresoli
344a076a860SLuca Ceresoli c2a->client = client;
345a076a860SLuca Ceresoli c2a->alias = alias;
346a076a860SLuca Ceresoli list_add(&c2a->node, &chan->alias_list);
347a076a860SLuca Ceresoli
348a076a860SLuca Ceresoli return 0;
349a076a860SLuca Ceresoli
350a076a860SLuca Ceresoli err_free:
351a076a860SLuca Ceresoli kfree(c2a);
352a076a860SLuca Ceresoli err_release_alias:
353a076a860SLuca Ceresoli i2c_atr_release_alias(atr, alias);
354a076a860SLuca Ceresoli
355a076a860SLuca Ceresoli return ret;
356a076a860SLuca Ceresoli }
357a076a860SLuca Ceresoli
i2c_atr_detach_client(struct i2c_adapter * adapter,const struct i2c_client * client)358a076a860SLuca Ceresoli static void i2c_atr_detach_client(struct i2c_adapter *adapter,
359a076a860SLuca Ceresoli const struct i2c_client *client)
360a076a860SLuca Ceresoli {
361a076a860SLuca Ceresoli struct i2c_atr_chan *chan = adapter->algo_data;
362a076a860SLuca Ceresoli struct i2c_atr *atr = chan->atr;
363a076a860SLuca Ceresoli struct i2c_atr_alias_pair *c2a;
364a076a860SLuca Ceresoli
365a076a860SLuca Ceresoli atr->ops->detach_client(atr, chan->chan_id, client);
366a076a860SLuca Ceresoli
367a076a860SLuca Ceresoli c2a = i2c_atr_find_mapping_by_client(&chan->alias_list, client);
368a076a860SLuca Ceresoli if (!c2a) {
369a076a860SLuca Ceresoli /* This should never happen */
370a076a860SLuca Ceresoli dev_warn(atr->dev, "Unable to find address mapping\n");
371a076a860SLuca Ceresoli return;
372a076a860SLuca Ceresoli }
373a076a860SLuca Ceresoli
374a076a860SLuca Ceresoli i2c_atr_release_alias(atr, c2a->alias);
375a076a860SLuca Ceresoli
376a076a860SLuca Ceresoli dev_dbg(atr->dev,
377a076a860SLuca Ceresoli "chan%u: client 0x%02x unmapped from alias 0x%02x (%s)\n",
378a076a860SLuca Ceresoli chan->chan_id, client->addr, c2a->alias, client->name);
379a076a860SLuca Ceresoli
380a076a860SLuca Ceresoli list_del(&c2a->node);
381a076a860SLuca Ceresoli kfree(c2a);
382a076a860SLuca Ceresoli }
383a076a860SLuca Ceresoli
i2c_atr_bus_notifier_call(struct notifier_block * nb,unsigned long event,void * device)384a076a860SLuca Ceresoli static int i2c_atr_bus_notifier_call(struct notifier_block *nb,
385a076a860SLuca Ceresoli unsigned long event, void *device)
386a076a860SLuca Ceresoli {
387a076a860SLuca Ceresoli struct i2c_atr *atr = container_of(nb, struct i2c_atr, i2c_nb);
388a076a860SLuca Ceresoli struct device *dev = device;
389a076a860SLuca Ceresoli struct i2c_client *client;
390a076a860SLuca Ceresoli u32 chan_id;
391a076a860SLuca Ceresoli int ret;
392a076a860SLuca Ceresoli
393a076a860SLuca Ceresoli client = i2c_verify_client(dev);
394a076a860SLuca Ceresoli if (!client)
395a076a860SLuca Ceresoli return NOTIFY_DONE;
396a076a860SLuca Ceresoli
397a076a860SLuca Ceresoli /* Is the client in one of our adapters? */
398a076a860SLuca Ceresoli for (chan_id = 0; chan_id < atr->max_adapters; ++chan_id) {
399a076a860SLuca Ceresoli if (client->adapter == atr->adapter[chan_id])
400a076a860SLuca Ceresoli break;
401a076a860SLuca Ceresoli }
402a076a860SLuca Ceresoli
403a076a860SLuca Ceresoli if (chan_id == atr->max_adapters)
404a076a860SLuca Ceresoli return NOTIFY_DONE;
405a076a860SLuca Ceresoli
406a076a860SLuca Ceresoli switch (event) {
407a076a860SLuca Ceresoli case BUS_NOTIFY_ADD_DEVICE:
408a076a860SLuca Ceresoli ret = i2c_atr_attach_client(client->adapter, client);
409a076a860SLuca Ceresoli if (ret)
410a076a860SLuca Ceresoli dev_err(atr->dev,
411a076a860SLuca Ceresoli "Failed to attach remote client '%s': %d\n",
412a076a860SLuca Ceresoli dev_name(dev), ret);
413a076a860SLuca Ceresoli break;
414a076a860SLuca Ceresoli
415a076a860SLuca Ceresoli case BUS_NOTIFY_DEL_DEVICE:
416a076a860SLuca Ceresoli i2c_atr_detach_client(client->adapter, client);
417a076a860SLuca Ceresoli break;
418a076a860SLuca Ceresoli
419a076a860SLuca Ceresoli default:
420a076a860SLuca Ceresoli break;
421a076a860SLuca Ceresoli }
422a076a860SLuca Ceresoli
423a076a860SLuca Ceresoli return NOTIFY_DONE;
424a076a860SLuca Ceresoli }
425a076a860SLuca Ceresoli
i2c_atr_parse_alias_pool(struct i2c_atr * atr)426a076a860SLuca Ceresoli static int i2c_atr_parse_alias_pool(struct i2c_atr *atr)
427a076a860SLuca Ceresoli {
428a076a860SLuca Ceresoli struct device *dev = atr->dev;
429a076a860SLuca Ceresoli unsigned long *alias_use_mask;
430a076a860SLuca Ceresoli size_t num_aliases;
431a076a860SLuca Ceresoli unsigned int i;
432a076a860SLuca Ceresoli u32 *aliases32;
433a076a860SLuca Ceresoli u16 *aliases16;
434a076a860SLuca Ceresoli int ret;
435a076a860SLuca Ceresoli
436a076a860SLuca Ceresoli ret = fwnode_property_count_u32(dev_fwnode(dev), "i2c-alias-pool");
437a076a860SLuca Ceresoli if (ret < 0) {
438a076a860SLuca Ceresoli dev_err(dev, "Failed to count 'i2c-alias-pool' property: %d\n",
439a076a860SLuca Ceresoli ret);
440a076a860SLuca Ceresoli return ret;
441a076a860SLuca Ceresoli }
442a076a860SLuca Ceresoli
443a076a860SLuca Ceresoli num_aliases = ret;
444a076a860SLuca Ceresoli
445a076a860SLuca Ceresoli if (!num_aliases)
446a076a860SLuca Ceresoli return 0;
447a076a860SLuca Ceresoli
448a076a860SLuca Ceresoli aliases32 = kcalloc(num_aliases, sizeof(*aliases32), GFP_KERNEL);
449a076a860SLuca Ceresoli if (!aliases32)
450a076a860SLuca Ceresoli return -ENOMEM;
451a076a860SLuca Ceresoli
452a076a860SLuca Ceresoli ret = fwnode_property_read_u32_array(dev_fwnode(dev), "i2c-alias-pool",
453a076a860SLuca Ceresoli aliases32, num_aliases);
454a076a860SLuca Ceresoli if (ret < 0) {
455a076a860SLuca Ceresoli dev_err(dev, "Failed to read 'i2c-alias-pool' property: %d\n",
456a076a860SLuca Ceresoli ret);
457a076a860SLuca Ceresoli goto err_free_aliases32;
458a076a860SLuca Ceresoli }
459a076a860SLuca Ceresoli
460a076a860SLuca Ceresoli aliases16 = kcalloc(num_aliases, sizeof(*aliases16), GFP_KERNEL);
461a076a860SLuca Ceresoli if (!aliases16) {
462a076a860SLuca Ceresoli ret = -ENOMEM;
463a076a860SLuca Ceresoli goto err_free_aliases32;
464a076a860SLuca Ceresoli }
465a076a860SLuca Ceresoli
466a076a860SLuca Ceresoli for (i = 0; i < num_aliases; i++) {
467a076a860SLuca Ceresoli if (!(aliases32[i] & 0xffff0000)) {
468a076a860SLuca Ceresoli aliases16[i] = aliases32[i];
469a076a860SLuca Ceresoli continue;
470a076a860SLuca Ceresoli }
471a076a860SLuca Ceresoli
472a076a860SLuca Ceresoli dev_err(dev, "Failed to parse 'i2c-alias-pool' property: I2C flags are not supported\n");
473a076a860SLuca Ceresoli ret = -EINVAL;
474a076a860SLuca Ceresoli goto err_free_aliases16;
475a076a860SLuca Ceresoli }
476a076a860SLuca Ceresoli
477a076a860SLuca Ceresoli alias_use_mask = bitmap_zalloc(num_aliases, GFP_KERNEL);
478a076a860SLuca Ceresoli if (!alias_use_mask) {
479a076a860SLuca Ceresoli ret = -ENOMEM;
480a076a860SLuca Ceresoli goto err_free_aliases16;
481a076a860SLuca Ceresoli }
482a076a860SLuca Ceresoli
483a076a860SLuca Ceresoli kfree(aliases32);
484a076a860SLuca Ceresoli
485a076a860SLuca Ceresoli atr->num_aliases = num_aliases;
486a076a860SLuca Ceresoli atr->aliases = aliases16;
487a076a860SLuca Ceresoli atr->alias_use_mask = alias_use_mask;
488a076a860SLuca Ceresoli
489a076a860SLuca Ceresoli dev_dbg(dev, "i2c-alias-pool has %zu aliases", atr->num_aliases);
490a076a860SLuca Ceresoli
491a076a860SLuca Ceresoli return 0;
492a076a860SLuca Ceresoli
493a076a860SLuca Ceresoli err_free_aliases16:
494a076a860SLuca Ceresoli kfree(aliases16);
495a076a860SLuca Ceresoli err_free_aliases32:
496a076a860SLuca Ceresoli kfree(aliases32);
497a076a860SLuca Ceresoli return ret;
498a076a860SLuca Ceresoli }
499a076a860SLuca Ceresoli
i2c_atr_new(struct i2c_adapter * parent,struct device * dev,const struct i2c_atr_ops * ops,int max_adapters)500a076a860SLuca Ceresoli struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent, struct device *dev,
501a076a860SLuca Ceresoli const struct i2c_atr_ops *ops, int max_adapters)
502a076a860SLuca Ceresoli {
503a076a860SLuca Ceresoli struct i2c_atr *atr;
504a076a860SLuca Ceresoli int ret;
505a076a860SLuca Ceresoli
506a076a860SLuca Ceresoli if (max_adapters > ATR_MAX_ADAPTERS)
507a076a860SLuca Ceresoli return ERR_PTR(-EINVAL);
508a076a860SLuca Ceresoli
509a076a860SLuca Ceresoli if (!ops || !ops->attach_client || !ops->detach_client)
510a076a860SLuca Ceresoli return ERR_PTR(-EINVAL);
511a076a860SLuca Ceresoli
512a076a860SLuca Ceresoli atr = kzalloc(struct_size(atr, adapter, max_adapters), GFP_KERNEL);
513a076a860SLuca Ceresoli if (!atr)
514a076a860SLuca Ceresoli return ERR_PTR(-ENOMEM);
515a076a860SLuca Ceresoli
516a076a860SLuca Ceresoli mutex_init(&atr->lock);
517a076a860SLuca Ceresoli spin_lock_init(&atr->alias_mask_lock);
518a076a860SLuca Ceresoli
519a076a860SLuca Ceresoli atr->parent = parent;
520a076a860SLuca Ceresoli atr->dev = dev;
521a076a860SLuca Ceresoli atr->ops = ops;
522a076a860SLuca Ceresoli atr->max_adapters = max_adapters;
523a076a860SLuca Ceresoli
524a076a860SLuca Ceresoli if (parent->algo->master_xfer)
525a076a860SLuca Ceresoli atr->algo.master_xfer = i2c_atr_master_xfer;
526a076a860SLuca Ceresoli if (parent->algo->smbus_xfer)
527a076a860SLuca Ceresoli atr->algo.smbus_xfer = i2c_atr_smbus_xfer;
528a076a860SLuca Ceresoli atr->algo.functionality = i2c_atr_functionality;
529a076a860SLuca Ceresoli
530a076a860SLuca Ceresoli ret = i2c_atr_parse_alias_pool(atr);
531a076a860SLuca Ceresoli if (ret)
532a076a860SLuca Ceresoli goto err_destroy_mutex;
533a076a860SLuca Ceresoli
534a076a860SLuca Ceresoli atr->i2c_nb.notifier_call = i2c_atr_bus_notifier_call;
535a076a860SLuca Ceresoli ret = bus_register_notifier(&i2c_bus_type, &atr->i2c_nb);
536a076a860SLuca Ceresoli if (ret)
537a076a860SLuca Ceresoli goto err_free_aliases;
538a076a860SLuca Ceresoli
539a076a860SLuca Ceresoli return atr;
540a076a860SLuca Ceresoli
541a076a860SLuca Ceresoli err_free_aliases:
542a076a860SLuca Ceresoli bitmap_free(atr->alias_use_mask);
543a076a860SLuca Ceresoli kfree(atr->aliases);
544a076a860SLuca Ceresoli err_destroy_mutex:
545a076a860SLuca Ceresoli mutex_destroy(&atr->lock);
546a076a860SLuca Ceresoli kfree(atr);
547a076a860SLuca Ceresoli
548a076a860SLuca Ceresoli return ERR_PTR(ret);
549a076a860SLuca Ceresoli }
550a076a860SLuca Ceresoli EXPORT_SYMBOL_NS_GPL(i2c_atr_new, I2C_ATR);
551a076a860SLuca Ceresoli
i2c_atr_delete(struct i2c_atr * atr)552a076a860SLuca Ceresoli void i2c_atr_delete(struct i2c_atr *atr)
553a076a860SLuca Ceresoli {
554a076a860SLuca Ceresoli unsigned int i;
555a076a860SLuca Ceresoli
556a076a860SLuca Ceresoli for (i = 0; i < atr->max_adapters; ++i)
557a076a860SLuca Ceresoli WARN_ON(atr->adapter[i]);
558a076a860SLuca Ceresoli
559a076a860SLuca Ceresoli bus_unregister_notifier(&i2c_bus_type, &atr->i2c_nb);
560a076a860SLuca Ceresoli bitmap_free(atr->alias_use_mask);
561a076a860SLuca Ceresoli kfree(atr->aliases);
562a076a860SLuca Ceresoli mutex_destroy(&atr->lock);
563a076a860SLuca Ceresoli kfree(atr);
564a076a860SLuca Ceresoli }
565a076a860SLuca Ceresoli EXPORT_SYMBOL_NS_GPL(i2c_atr_delete, I2C_ATR);
566a076a860SLuca Ceresoli
i2c_atr_add_adapter(struct i2c_atr * atr,u32 chan_id,struct device * adapter_parent,struct fwnode_handle * bus_handle)567a076a860SLuca Ceresoli int i2c_atr_add_adapter(struct i2c_atr *atr, u32 chan_id,
568a076a860SLuca Ceresoli struct device *adapter_parent,
569a076a860SLuca Ceresoli struct fwnode_handle *bus_handle)
570a076a860SLuca Ceresoli {
571a076a860SLuca Ceresoli struct i2c_adapter *parent = atr->parent;
572a076a860SLuca Ceresoli struct device *dev = atr->dev;
573a076a860SLuca Ceresoli struct i2c_atr_chan *chan;
574a076a860SLuca Ceresoli char symlink_name[ATR_MAX_SYMLINK_LEN];
575a076a860SLuca Ceresoli int ret;
576a076a860SLuca Ceresoli
577a076a860SLuca Ceresoli if (chan_id >= atr->max_adapters) {
578a076a860SLuca Ceresoli dev_err(dev, "No room for more i2c-atr adapters\n");
579a076a860SLuca Ceresoli return -EINVAL;
580a076a860SLuca Ceresoli }
581a076a860SLuca Ceresoli
582a076a860SLuca Ceresoli if (atr->adapter[chan_id]) {
583a076a860SLuca Ceresoli dev_err(dev, "Adapter %d already present\n", chan_id);
584a076a860SLuca Ceresoli return -EEXIST;
585a076a860SLuca Ceresoli }
586a076a860SLuca Ceresoli
587a076a860SLuca Ceresoli chan = kzalloc(sizeof(*chan), GFP_KERNEL);
588a076a860SLuca Ceresoli if (!chan)
589a076a860SLuca Ceresoli return -ENOMEM;
590a076a860SLuca Ceresoli
591a076a860SLuca Ceresoli if (!adapter_parent)
592a076a860SLuca Ceresoli adapter_parent = dev;
593a076a860SLuca Ceresoli
594a076a860SLuca Ceresoli chan->atr = atr;
595a076a860SLuca Ceresoli chan->chan_id = chan_id;
596a076a860SLuca Ceresoli INIT_LIST_HEAD(&chan->alias_list);
597a076a860SLuca Ceresoli mutex_init(&chan->orig_addrs_lock);
598a076a860SLuca Ceresoli
599a076a860SLuca Ceresoli snprintf(chan->adap.name, sizeof(chan->adap.name), "i2c-%d-atr-%d",
600a076a860SLuca Ceresoli i2c_adapter_id(parent), chan_id);
601a076a860SLuca Ceresoli chan->adap.owner = THIS_MODULE;
602a076a860SLuca Ceresoli chan->adap.algo = &atr->algo;
603a076a860SLuca Ceresoli chan->adap.algo_data = chan;
604a076a860SLuca Ceresoli chan->adap.dev.parent = adapter_parent;
605a076a860SLuca Ceresoli chan->adap.retries = parent->retries;
606a076a860SLuca Ceresoli chan->adap.timeout = parent->timeout;
607a076a860SLuca Ceresoli chan->adap.quirks = parent->quirks;
608a076a860SLuca Ceresoli chan->adap.lock_ops = &i2c_atr_lock_ops;
609a076a860SLuca Ceresoli
610a076a860SLuca Ceresoli if (bus_handle) {
611a076a860SLuca Ceresoli device_set_node(&chan->adap.dev, fwnode_handle_get(bus_handle));
612a076a860SLuca Ceresoli } else {
613a076a860SLuca Ceresoli struct fwnode_handle *atr_node;
614a076a860SLuca Ceresoli struct fwnode_handle *child;
615a076a860SLuca Ceresoli u32 reg;
616a076a860SLuca Ceresoli
617a076a860SLuca Ceresoli atr_node = device_get_named_child_node(dev, "i2c-atr");
618a076a860SLuca Ceresoli
619a076a860SLuca Ceresoli fwnode_for_each_child_node(atr_node, child) {
620a076a860SLuca Ceresoli ret = fwnode_property_read_u32(child, "reg", ®);
621a076a860SLuca Ceresoli if (ret)
622a076a860SLuca Ceresoli continue;
623a076a860SLuca Ceresoli if (chan_id == reg)
624a076a860SLuca Ceresoli break;
625a076a860SLuca Ceresoli }
626a076a860SLuca Ceresoli
627a076a860SLuca Ceresoli device_set_node(&chan->adap.dev, child);
628a076a860SLuca Ceresoli fwnode_handle_put(atr_node);
629a076a860SLuca Ceresoli }
630a076a860SLuca Ceresoli
631a076a860SLuca Ceresoli atr->adapter[chan_id] = &chan->adap;
632a076a860SLuca Ceresoli
633a076a860SLuca Ceresoli ret = i2c_add_adapter(&chan->adap);
634a076a860SLuca Ceresoli if (ret) {
635a076a860SLuca Ceresoli dev_err(dev, "failed to add atr-adapter %u (error=%d)\n",
636a076a860SLuca Ceresoli chan_id, ret);
637a076a860SLuca Ceresoli goto err_fwnode_put;
638a076a860SLuca Ceresoli }
639a076a860SLuca Ceresoli
640a076a860SLuca Ceresoli snprintf(symlink_name, sizeof(symlink_name), "channel-%u",
641a076a860SLuca Ceresoli chan->chan_id);
642a076a860SLuca Ceresoli
643a076a860SLuca Ceresoli ret = sysfs_create_link(&chan->adap.dev.kobj, &dev->kobj, "atr_device");
644a076a860SLuca Ceresoli if (ret)
645a076a860SLuca Ceresoli dev_warn(dev, "can't create symlink to atr device\n");
646a076a860SLuca Ceresoli ret = sysfs_create_link(&dev->kobj, &chan->adap.dev.kobj, symlink_name);
647a076a860SLuca Ceresoli if (ret)
648a076a860SLuca Ceresoli dev_warn(dev, "can't create symlink for channel %u\n", chan_id);
649a076a860SLuca Ceresoli
650a076a860SLuca Ceresoli dev_dbg(dev, "Added ATR child bus %d\n", i2c_adapter_id(&chan->adap));
651a076a860SLuca Ceresoli
652a076a860SLuca Ceresoli return 0;
653a076a860SLuca Ceresoli
654a076a860SLuca Ceresoli err_fwnode_put:
655a076a860SLuca Ceresoli fwnode_handle_put(dev_fwnode(&chan->adap.dev));
656a076a860SLuca Ceresoli mutex_destroy(&chan->orig_addrs_lock);
657a076a860SLuca Ceresoli kfree(chan);
658a076a860SLuca Ceresoli return ret;
659a076a860SLuca Ceresoli }
660a076a860SLuca Ceresoli EXPORT_SYMBOL_NS_GPL(i2c_atr_add_adapter, I2C_ATR);
661a076a860SLuca Ceresoli
i2c_atr_del_adapter(struct i2c_atr * atr,u32 chan_id)662a076a860SLuca Ceresoli void i2c_atr_del_adapter(struct i2c_atr *atr, u32 chan_id)
663a076a860SLuca Ceresoli {
664a076a860SLuca Ceresoli char symlink_name[ATR_MAX_SYMLINK_LEN];
665a076a860SLuca Ceresoli struct i2c_adapter *adap;
666a076a860SLuca Ceresoli struct i2c_atr_chan *chan;
667a076a860SLuca Ceresoli struct fwnode_handle *fwnode;
668a076a860SLuca Ceresoli struct device *dev = atr->dev;
669a076a860SLuca Ceresoli
670a076a860SLuca Ceresoli adap = atr->adapter[chan_id];
671a076a860SLuca Ceresoli if (!adap)
672a076a860SLuca Ceresoli return;
673a076a860SLuca Ceresoli
674a076a860SLuca Ceresoli chan = adap->algo_data;
675a076a860SLuca Ceresoli fwnode = dev_fwnode(&adap->dev);
676a076a860SLuca Ceresoli
677a076a860SLuca Ceresoli dev_dbg(dev, "Removing ATR child bus %d\n", i2c_adapter_id(adap));
678a076a860SLuca Ceresoli
679a076a860SLuca Ceresoli snprintf(symlink_name, sizeof(symlink_name), "channel-%u",
680a076a860SLuca Ceresoli chan->chan_id);
681a076a860SLuca Ceresoli sysfs_remove_link(&dev->kobj, symlink_name);
682a076a860SLuca Ceresoli sysfs_remove_link(&chan->adap.dev.kobj, "atr_device");
683a076a860SLuca Ceresoli
684a076a860SLuca Ceresoli i2c_del_adapter(adap);
685a076a860SLuca Ceresoli
686a076a860SLuca Ceresoli atr->adapter[chan_id] = NULL;
687a076a860SLuca Ceresoli
688a076a860SLuca Ceresoli fwnode_handle_put(fwnode);
689a076a860SLuca Ceresoli mutex_destroy(&chan->orig_addrs_lock);
690a076a860SLuca Ceresoli kfree(chan->orig_addrs);
691a076a860SLuca Ceresoli kfree(chan);
692a076a860SLuca Ceresoli }
693a076a860SLuca Ceresoli EXPORT_SYMBOL_NS_GPL(i2c_atr_del_adapter, I2C_ATR);
694a076a860SLuca Ceresoli
i2c_atr_set_driver_data(struct i2c_atr * atr,void * data)695a076a860SLuca Ceresoli void i2c_atr_set_driver_data(struct i2c_atr *atr, void *data)
696a076a860SLuca Ceresoli {
697a076a860SLuca Ceresoli atr->priv = data;
698a076a860SLuca Ceresoli }
699a076a860SLuca Ceresoli EXPORT_SYMBOL_NS_GPL(i2c_atr_set_driver_data, I2C_ATR);
700a076a860SLuca Ceresoli
i2c_atr_get_driver_data(struct i2c_atr * atr)701a076a860SLuca Ceresoli void *i2c_atr_get_driver_data(struct i2c_atr *atr)
702a076a860SLuca Ceresoli {
703a076a860SLuca Ceresoli return atr->priv;
704a076a860SLuca Ceresoli }
705a076a860SLuca Ceresoli EXPORT_SYMBOL_NS_GPL(i2c_atr_get_driver_data, I2C_ATR);
706a076a860SLuca Ceresoli
707a076a860SLuca Ceresoli MODULE_AUTHOR("Luca Ceresoli <luca.ceresoli@bootlin.com>");
708a076a860SLuca Ceresoli MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>");
709a076a860SLuca Ceresoli MODULE_DESCRIPTION("I2C Address Translator");
710a076a860SLuca Ceresoli MODULE_LICENSE("GPL");
711