at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * I2C Address Translator |
| 4 | * |
| 5 | * Copyright (c) 2019,2022 Luca Ceresoli <luca@lucaceresoli.net> |
| 6 | * Copyright (c) 2022,2023 Tomi Valkeinen <tomi.valkeinen@ideasonboard.com> |
| 7 | * |
| 8 | * Originally based on i2c-mux.c |
| 9 | */ |
| 10 | |
| 11 | #include <linux/i2c-atr.h> |
| 12 | #include <linux/i2c.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/mutex.h> |
| 16 | #include <linux/property.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/lockdep.h> |
| 20 | |
| 21 | #define ATR_MAX_ADAPTERS 100 /* Just a sanity limit */ |
| 22 | #define ATR_MAX_SYMLINK_LEN 11 /* Longest name is 10 chars: "channel-99" */ |
| 23 | |
| 24 | /** |
| 25 | * struct i2c_atr_alias_pair - Holds the alias assigned to a client address. |
| 26 | * @node: List node |
| 27 | * @addr: Address of the client on the child bus. |
| 28 | * @alias: I2C alias address assigned by the driver. |
| 29 | * This is the address that will be used to issue I2C transactions |
| 30 | * on the parent (physical) bus. |
| 31 | * @fixed: Alias pair cannot be replaced during dynamic address attachment. |
| 32 | * This flag is necessary for situations where a single I2C transaction |
| 33 | * contains more distinct target addresses than the ATR channel can handle. |
| 34 | * It marks addresses that have already been attached to an alias so |
| 35 | * that their alias pair is not evicted by a subsequent address in the same |
| 36 | * transaction. |
| 37 | * |
| 38 | */ |
| 39 | struct i2c_atr_alias_pair { |
| 40 | struct list_head node; |
| 41 | bool fixed; |
| 42 | u16 addr; |
| 43 | u16 alias; |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * struct i2c_atr_alias_pool - Pool of client aliases available for an ATR. |
| 48 | * @size: Total number of aliases |
| 49 | * @shared: Indicates if this alias pool is shared by multiple channels |
| 50 | * |
| 51 | * @lock: Lock protecting @aliases and @use_mask |
| 52 | * @use_mask: Mask of used aliases |
| 53 | * @aliases: Array of aliases, must hold exactly @size elements |
| 54 | */ |
| 55 | struct i2c_atr_alias_pool { |
| 56 | size_t size; |
| 57 | bool shared; |
| 58 | |
| 59 | /* Protects aliases and use_mask */ |
| 60 | spinlock_t lock; |
| 61 | unsigned long *use_mask; |
| 62 | u16 aliases[] __counted_by(size); |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * struct i2c_atr_chan - Data for a channel. |
| 67 | * @adap: The &struct i2c_adapter for the channel |
| 68 | * @atr: The parent I2C ATR |
| 69 | * @chan_id: The ID of this channel |
| 70 | * @alias_pairs_lock: Mutex protecting @alias_pairs |
| 71 | * @alias_pairs_lock_key: Lock key for @alias_pairs_lock |
| 72 | * @alias_pairs: List of @struct i2c_atr_alias_pair containing the |
| 73 | * assigned aliases |
| 74 | * @alias_pool: Pool of available client aliases |
| 75 | * |
| 76 | * @orig_addrs_lock: Mutex protecting @orig_addrs |
| 77 | * @orig_addrs_lock_key: Lock key for @orig_addrs_lock |
| 78 | * @orig_addrs: Buffer used to store the original addresses during transmit |
| 79 | * @orig_addrs_size: Size of @orig_addrs |
| 80 | */ |
| 81 | struct i2c_atr_chan { |
| 82 | struct i2c_adapter adap; |
| 83 | struct i2c_atr *atr; |
| 84 | u32 chan_id; |
| 85 | |
| 86 | /* Lock alias_pairs during attach/detach */ |
| 87 | struct mutex alias_pairs_lock; |
| 88 | struct lock_class_key alias_pairs_lock_key; |
| 89 | struct list_head alias_pairs; |
| 90 | struct i2c_atr_alias_pool *alias_pool; |
| 91 | |
| 92 | /* Lock orig_addrs during xfer */ |
| 93 | struct mutex orig_addrs_lock; |
| 94 | struct lock_class_key orig_addrs_lock_key; |
| 95 | u16 *orig_addrs; |
| 96 | unsigned int orig_addrs_size; |
| 97 | }; |
| 98 | |
| 99 | /** |
| 100 | * struct i2c_atr - The I2C ATR instance |
| 101 | * @parent: The parent &struct i2c_adapter |
| 102 | * @dev: The device that owns the I2C ATR instance |
| 103 | * @ops: &struct i2c_atr_ops |
| 104 | * @priv: Private driver data, set with i2c_atr_set_driver_data() |
| 105 | * @algo: The &struct i2c_algorithm for adapters |
| 106 | * @lock: Lock for the I2C bus segment (see &struct i2c_lock_operations) |
| 107 | * @lock_key: Lock key for @lock |
| 108 | * @max_adapters: Maximum number of adapters this I2C ATR can have |
| 109 | * @flags: Flags for ATR |
| 110 | * @alias_pool: Optional common pool of available client aliases |
| 111 | * @i2c_nb: Notifier for remote client add & del events |
| 112 | * @adapter: Array of adapters |
| 113 | */ |
| 114 | struct i2c_atr { |
| 115 | struct i2c_adapter *parent; |
| 116 | struct device *dev; |
| 117 | const struct i2c_atr_ops *ops; |
| 118 | |
| 119 | void *priv; |
| 120 | |
| 121 | struct i2c_algorithm algo; |
| 122 | /* lock for the I2C bus segment (see struct i2c_lock_operations) */ |
| 123 | struct mutex lock; |
| 124 | struct lock_class_key lock_key; |
| 125 | int max_adapters; |
| 126 | u32 flags; |
| 127 | |
| 128 | struct i2c_atr_alias_pool *alias_pool; |
| 129 | |
| 130 | struct notifier_block i2c_nb; |
| 131 | |
| 132 | struct i2c_adapter *adapter[] __counted_by(max_adapters); |
| 133 | }; |
| 134 | |
| 135 | static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_pool(size_t num_aliases, bool shared) |
| 136 | { |
| 137 | struct i2c_atr_alias_pool *alias_pool; |
| 138 | int ret; |
| 139 | |
| 140 | alias_pool = kzalloc_flex(*alias_pool, aliases, num_aliases); |
| 141 | if (!alias_pool) |
| 142 | return ERR_PTR(-ENOMEM); |
| 143 | |
| 144 | alias_pool->size = num_aliases; |
| 145 | |
| 146 | alias_pool->use_mask = bitmap_zalloc(num_aliases, GFP_KERNEL); |
| 147 | if (!alias_pool->use_mask) { |
| 148 | ret = -ENOMEM; |
| 149 | goto err_free_alias_pool; |
| 150 | } |
| 151 | |
| 152 | alias_pool->shared = shared; |
| 153 | |
| 154 | spin_lock_init(&alias_pool->lock); |
| 155 | |
| 156 | return alias_pool; |
| 157 | |
| 158 | err_free_alias_pool: |
| 159 | kfree(alias_pool); |
| 160 | return ERR_PTR(ret); |
| 161 | } |
| 162 | |
| 163 | static void i2c_atr_free_alias_pool(struct i2c_atr_alias_pool *alias_pool) |
| 164 | { |
| 165 | bitmap_free(alias_pool->use_mask); |
| 166 | kfree(alias_pool); |
| 167 | } |
| 168 | |
| 169 | /* Must be called with alias_pairs_lock held */ |
| 170 | static struct i2c_atr_alias_pair *i2c_atr_create_c2a(struct i2c_atr_chan *chan, |
| 171 | u16 alias, u16 addr) |
| 172 | { |
| 173 | struct i2c_atr_alias_pair *c2a; |
| 174 | |
| 175 | lockdep_assert_held(&chan->alias_pairs_lock); |
| 176 | |
| 177 | c2a = kzalloc_obj(*c2a); |
| 178 | if (!c2a) |
| 179 | return NULL; |
| 180 | |
| 181 | c2a->addr = addr; |
| 182 | c2a->alias = alias; |
| 183 | |
| 184 | list_add(&c2a->node, &chan->alias_pairs); |
| 185 | |
| 186 | return c2a; |
| 187 | } |
| 188 | |
| 189 | /* Must be called with alias_pairs_lock held */ |
| 190 | static void i2c_atr_destroy_c2a(struct i2c_atr_alias_pair **pc2a) |
| 191 | { |
| 192 | list_del(&(*pc2a)->node); |
| 193 | kfree(*pc2a); |
| 194 | *pc2a = NULL; |
| 195 | } |
| 196 | |
| 197 | static int i2c_atr_reserve_alias(struct i2c_atr_alias_pool *alias_pool) |
| 198 | { |
| 199 | unsigned long idx; |
| 200 | u16 alias; |
| 201 | |
| 202 | spin_lock(&alias_pool->lock); |
| 203 | |
| 204 | idx = find_first_zero_bit(alias_pool->use_mask, alias_pool->size); |
| 205 | if (idx >= alias_pool->size) { |
| 206 | spin_unlock(&alias_pool->lock); |
| 207 | return -EBUSY; |
| 208 | } |
| 209 | |
| 210 | set_bit(idx, alias_pool->use_mask); |
| 211 | |
| 212 | alias = alias_pool->aliases[idx]; |
| 213 | |
| 214 | spin_unlock(&alias_pool->lock); |
| 215 | return alias; |
| 216 | } |
| 217 | |
| 218 | static void i2c_atr_release_alias(struct i2c_atr_alias_pool *alias_pool, u16 alias) |
| 219 | { |
| 220 | unsigned int idx; |
| 221 | |
| 222 | spin_lock(&alias_pool->lock); |
| 223 | |
| 224 | for (idx = 0; idx < alias_pool->size; ++idx) { |
| 225 | if (alias_pool->aliases[idx] == alias) { |
| 226 | clear_bit(idx, alias_pool->use_mask); |
| 227 | spin_unlock(&alias_pool->lock); |
| 228 | return; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | spin_unlock(&alias_pool->lock); |
| 233 | } |
| 234 | |
| 235 | static struct i2c_atr_alias_pair * |
| 236 | i2c_atr_find_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr) |
| 237 | { |
| 238 | struct i2c_atr_alias_pair *c2a; |
| 239 | |
| 240 | lockdep_assert_held(&chan->alias_pairs_lock); |
| 241 | |
| 242 | list_for_each_entry(c2a, &chan->alias_pairs, node) { |
| 243 | if (c2a->addr == addr) |
| 244 | return c2a; |
| 245 | } |
| 246 | |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | static struct i2c_atr_alias_pair * |
| 251 | i2c_atr_create_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr) |
| 252 | { |
| 253 | struct i2c_atr *atr = chan->atr; |
| 254 | struct i2c_atr_alias_pair *c2a; |
| 255 | u16 alias; |
| 256 | int ret; |
| 257 | |
| 258 | lockdep_assert_held(&chan->alias_pairs_lock); |
| 259 | |
| 260 | ret = i2c_atr_reserve_alias(chan->alias_pool); |
| 261 | if (ret < 0) |
| 262 | return NULL; |
| 263 | |
| 264 | alias = ret; |
| 265 | |
| 266 | c2a = i2c_atr_create_c2a(chan, alias, addr); |
| 267 | if (!c2a) |
| 268 | goto err_release_alias; |
| 269 | |
| 270 | ret = atr->ops->attach_addr(atr, chan->chan_id, c2a->addr, c2a->alias); |
| 271 | if (ret) { |
| 272 | dev_err(atr->dev, "failed to attach 0x%02x on channel %d: err %d\n", |
| 273 | addr, chan->chan_id, ret); |
| 274 | goto err_del_c2a; |
| 275 | } |
| 276 | |
| 277 | return c2a; |
| 278 | |
| 279 | err_del_c2a: |
| 280 | i2c_atr_destroy_c2a(&c2a); |
| 281 | err_release_alias: |
| 282 | i2c_atr_release_alias(chan->alias_pool, alias); |
| 283 | return NULL; |
| 284 | } |
| 285 | |
| 286 | static struct i2c_atr_alias_pair * |
| 287 | i2c_atr_replace_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr) |
| 288 | { |
| 289 | struct i2c_atr *atr = chan->atr; |
| 290 | struct i2c_atr_alias_pair *c2a; |
| 291 | struct list_head *alias_pairs; |
| 292 | bool found = false; |
| 293 | u16 alias; |
| 294 | int ret; |
| 295 | |
| 296 | lockdep_assert_held(&chan->alias_pairs_lock); |
| 297 | |
| 298 | alias_pairs = &chan->alias_pairs; |
| 299 | |
| 300 | if (unlikely(list_empty(alias_pairs))) |
| 301 | return NULL; |
| 302 | |
| 303 | list_for_each_entry_reverse(c2a, alias_pairs, node) { |
| 304 | if (!c2a->fixed) { |
| 305 | found = true; |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (!found) |
| 311 | return NULL; |
| 312 | |
| 313 | atr->ops->detach_addr(atr, chan->chan_id, c2a->addr); |
| 314 | c2a->addr = addr; |
| 315 | |
| 316 | list_move(&c2a->node, alias_pairs); |
| 317 | |
| 318 | alias = c2a->alias; |
| 319 | |
| 320 | ret = atr->ops->attach_addr(atr, chan->chan_id, c2a->addr, c2a->alias); |
| 321 | if (ret) { |
| 322 | dev_err(atr->dev, "failed to attach 0x%02x on channel %d: err %d\n", |
| 323 | addr, chan->chan_id, ret); |
| 324 | i2c_atr_destroy_c2a(&c2a); |
| 325 | i2c_atr_release_alias(chan->alias_pool, alias); |
| 326 | return NULL; |
| 327 | } |
| 328 | |
| 329 | return c2a; |
| 330 | } |
| 331 | |
| 332 | static struct i2c_atr_alias_pair * |
| 333 | i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr) |
| 334 | { |
| 335 | struct i2c_atr *atr = chan->atr; |
| 336 | struct i2c_atr_alias_pair *c2a; |
| 337 | |
| 338 | c2a = i2c_atr_find_mapping_by_addr(chan, addr); |
| 339 | if (c2a) |
| 340 | return c2a; |
| 341 | |
| 342 | if (atr->flags & I2C_ATR_F_STATIC) |
| 343 | return NULL; |
| 344 | |
| 345 | c2a = i2c_atr_create_mapping_by_addr(chan, addr); |
| 346 | if (c2a) |
| 347 | return c2a; |
| 348 | |
| 349 | return i2c_atr_replace_mapping_by_addr(chan, addr); |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Replace all message addresses with their aliases, saving the original |
| 354 | * addresses. |
| 355 | * |
| 356 | * This function is internal for use in i2c_atr_master_xfer(). It must be |
| 357 | * followed by i2c_atr_unmap_msgs() to restore the original addresses. |
| 358 | */ |
| 359 | static int i2c_atr_map_msgs(struct i2c_atr_chan *chan, struct i2c_msg *msgs, |
| 360 | int num) |
| 361 | { |
| 362 | struct i2c_atr *atr = chan->atr; |
| 363 | static struct i2c_atr_alias_pair *c2a; |
| 364 | int i, ret = 0; |
| 365 | |
| 366 | /* Ensure we have enough room to save the original addresses */ |
| 367 | if (unlikely(chan->orig_addrs_size < num)) { |
| 368 | u16 *new_buf; |
| 369 | |
| 370 | /* We don't care about old data, hence no realloc() */ |
| 371 | new_buf = kmalloc_array(num, sizeof(*new_buf), GFP_KERNEL); |
| 372 | if (!new_buf) |
| 373 | return -ENOMEM; |
| 374 | |
| 375 | kfree(chan->orig_addrs); |
| 376 | chan->orig_addrs = new_buf; |
| 377 | chan->orig_addrs_size = num; |
| 378 | } |
| 379 | |
| 380 | mutex_lock(&chan->alias_pairs_lock); |
| 381 | |
| 382 | for (i = 0; i < num; i++) { |
| 383 | chan->orig_addrs[i] = msgs[i].addr; |
| 384 | |
| 385 | c2a = i2c_atr_get_mapping_by_addr(chan, msgs[i].addr); |
| 386 | |
| 387 | if (!c2a) { |
| 388 | if (atr->flags & I2C_ATR_F_PASSTHROUGH) |
| 389 | continue; |
| 390 | |
| 391 | dev_err(atr->dev, "client 0x%02x not mapped!\n", |
| 392 | msgs[i].addr); |
| 393 | |
| 394 | while (i--) |
| 395 | msgs[i].addr = chan->orig_addrs[i]; |
| 396 | |
| 397 | ret = -ENXIO; |
| 398 | goto out_unlock; |
| 399 | } |
| 400 | |
| 401 | // Prevent c2a from being overwritten by another client in this transaction |
| 402 | c2a->fixed = true; |
| 403 | |
| 404 | msgs[i].addr = c2a->alias; |
| 405 | } |
| 406 | |
| 407 | out_unlock: |
| 408 | mutex_unlock(&chan->alias_pairs_lock); |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Restore all message address aliases with the original addresses. This |
| 414 | * function is internal for use in i2c_atr_master_xfer() and for this reason it |
| 415 | * needs no null and size checks on orig_addr. |
| 416 | * |
| 417 | * @see i2c_atr_map_msgs() |
| 418 | */ |
| 419 | static void i2c_atr_unmap_msgs(struct i2c_atr_chan *chan, struct i2c_msg *msgs, |
| 420 | int num) |
| 421 | { |
| 422 | struct i2c_atr_alias_pair *c2a; |
| 423 | int i; |
| 424 | |
| 425 | for (i = 0; i < num; i++) |
| 426 | msgs[i].addr = chan->orig_addrs[i]; |
| 427 | |
| 428 | mutex_lock(&chan->alias_pairs_lock); |
| 429 | |
| 430 | if (unlikely(list_empty(&chan->alias_pairs))) |
| 431 | goto out_unlock; |
| 432 | |
| 433 | // unfix c2a entries so that subsequent transfers can reuse their aliases |
| 434 | list_for_each_entry(c2a, &chan->alias_pairs, node) { |
| 435 | c2a->fixed = false; |
| 436 | } |
| 437 | |
| 438 | out_unlock: |
| 439 | mutex_unlock(&chan->alias_pairs_lock); |
| 440 | } |
| 441 | |
| 442 | static int i2c_atr_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, |
| 443 | int num) |
| 444 | { |
| 445 | struct i2c_atr_chan *chan = adap->algo_data; |
| 446 | struct i2c_atr *atr = chan->atr; |
| 447 | struct i2c_adapter *parent = atr->parent; |
| 448 | int ret; |
| 449 | |
| 450 | /* Translate addresses */ |
| 451 | mutex_lock(&chan->orig_addrs_lock); |
| 452 | |
| 453 | ret = i2c_atr_map_msgs(chan, msgs, num); |
| 454 | if (ret < 0) |
| 455 | goto err_unlock; |
| 456 | |
| 457 | /* Perform the transfer */ |
| 458 | ret = i2c_transfer(parent, msgs, num); |
| 459 | |
| 460 | /* Restore addresses */ |
| 461 | i2c_atr_unmap_msgs(chan, msgs, num); |
| 462 | |
| 463 | err_unlock: |
| 464 | mutex_unlock(&chan->orig_addrs_lock); |
| 465 | |
| 466 | return ret; |
| 467 | } |
| 468 | |
| 469 | static int i2c_atr_smbus_xfer(struct i2c_adapter *adap, u16 addr, |
| 470 | unsigned short flags, char read_write, u8 command, |
| 471 | int size, union i2c_smbus_data *data) |
| 472 | { |
| 473 | struct i2c_atr_chan *chan = adap->algo_data; |
| 474 | struct i2c_atr *atr = chan->atr; |
| 475 | struct i2c_adapter *parent = atr->parent; |
| 476 | struct i2c_atr_alias_pair *c2a; |
| 477 | u16 alias; |
| 478 | |
| 479 | mutex_lock(&chan->alias_pairs_lock); |
| 480 | |
| 481 | c2a = i2c_atr_get_mapping_by_addr(chan, addr); |
| 482 | |
| 483 | if (!c2a && !(atr->flags & I2C_ATR_F_PASSTHROUGH)) { |
| 484 | dev_err(atr->dev, "client 0x%02x not mapped!\n", addr); |
| 485 | mutex_unlock(&chan->alias_pairs_lock); |
| 486 | return -ENXIO; |
| 487 | } |
| 488 | |
| 489 | alias = c2a ? c2a->alias : addr; |
| 490 | |
| 491 | mutex_unlock(&chan->alias_pairs_lock); |
| 492 | |
| 493 | return i2c_smbus_xfer(parent, alias, flags, read_write, command, |
| 494 | size, data); |
| 495 | } |
| 496 | |
| 497 | static u32 i2c_atr_functionality(struct i2c_adapter *adap) |
| 498 | { |
| 499 | struct i2c_atr_chan *chan = adap->algo_data; |
| 500 | struct i2c_adapter *parent = chan->atr->parent; |
| 501 | |
| 502 | return parent->algo->functionality(parent); |
| 503 | } |
| 504 | |
| 505 | static void i2c_atr_lock_bus(struct i2c_adapter *adapter, unsigned int flags) |
| 506 | { |
| 507 | struct i2c_atr_chan *chan = adapter->algo_data; |
| 508 | struct i2c_atr *atr = chan->atr; |
| 509 | |
| 510 | mutex_lock(&atr->lock); |
| 511 | } |
| 512 | |
| 513 | static int i2c_atr_trylock_bus(struct i2c_adapter *adapter, unsigned int flags) |
| 514 | { |
| 515 | struct i2c_atr_chan *chan = adapter->algo_data; |
| 516 | struct i2c_atr *atr = chan->atr; |
| 517 | |
| 518 | return mutex_trylock(&atr->lock); |
| 519 | } |
| 520 | |
| 521 | static void i2c_atr_unlock_bus(struct i2c_adapter *adapter, unsigned int flags) |
| 522 | { |
| 523 | struct i2c_atr_chan *chan = adapter->algo_data; |
| 524 | struct i2c_atr *atr = chan->atr; |
| 525 | |
| 526 | mutex_unlock(&atr->lock); |
| 527 | } |
| 528 | |
| 529 | static const struct i2c_lock_operations i2c_atr_lock_ops = { |
| 530 | .lock_bus = i2c_atr_lock_bus, |
| 531 | .trylock_bus = i2c_atr_trylock_bus, |
| 532 | .unlock_bus = i2c_atr_unlock_bus, |
| 533 | }; |
| 534 | |
| 535 | static int i2c_atr_attach_addr(struct i2c_adapter *adapter, |
| 536 | u16 addr) |
| 537 | { |
| 538 | struct i2c_atr_chan *chan = adapter->algo_data; |
| 539 | struct i2c_atr *atr = chan->atr; |
| 540 | struct i2c_atr_alias_pair *c2a; |
| 541 | int ret = 0; |
| 542 | |
| 543 | mutex_lock(&chan->alias_pairs_lock); |
| 544 | |
| 545 | c2a = i2c_atr_create_mapping_by_addr(chan, addr); |
| 546 | if (!c2a && !(atr->flags & I2C_ATR_F_STATIC)) |
| 547 | c2a = i2c_atr_replace_mapping_by_addr(chan, addr); |
| 548 | |
| 549 | if (!c2a) { |
| 550 | dev_err(atr->dev, "failed to find a free alias\n"); |
| 551 | ret = -EBUSY; |
| 552 | goto out_unlock; |
| 553 | } |
| 554 | |
| 555 | dev_dbg(atr->dev, "chan%u: using alias 0x%02x for addr 0x%02x\n", |
| 556 | chan->chan_id, c2a->alias, addr); |
| 557 | |
| 558 | out_unlock: |
| 559 | mutex_unlock(&chan->alias_pairs_lock); |
| 560 | return ret; |
| 561 | } |
| 562 | |
| 563 | static void i2c_atr_detach_addr(struct i2c_adapter *adapter, |
| 564 | u16 addr) |
| 565 | { |
| 566 | struct i2c_atr_chan *chan = adapter->algo_data; |
| 567 | struct i2c_atr *atr = chan->atr; |
| 568 | struct i2c_atr_alias_pair *c2a; |
| 569 | |
| 570 | atr->ops->detach_addr(atr, chan->chan_id, addr); |
| 571 | |
| 572 | mutex_lock(&chan->alias_pairs_lock); |
| 573 | |
| 574 | c2a = i2c_atr_find_mapping_by_addr(chan, addr); |
| 575 | if (!c2a) { |
| 576 | mutex_unlock(&chan->alias_pairs_lock); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | i2c_atr_release_alias(chan->alias_pool, c2a->alias); |
| 581 | |
| 582 | dev_dbg(atr->dev, |
| 583 | "chan%u: detached alias 0x%02x from addr 0x%02x\n", |
| 584 | chan->chan_id, c2a->alias, addr); |
| 585 | |
| 586 | i2c_atr_destroy_c2a(&c2a); |
| 587 | |
| 588 | mutex_unlock(&chan->alias_pairs_lock); |
| 589 | } |
| 590 | |
| 591 | static int i2c_atr_bus_notifier_call(struct notifier_block *nb, |
| 592 | unsigned long event, void *device) |
| 593 | { |
| 594 | struct i2c_atr *atr = container_of(nb, struct i2c_atr, i2c_nb); |
| 595 | struct device *dev = device; |
| 596 | struct i2c_client *client; |
| 597 | u32 chan_id; |
| 598 | int ret; |
| 599 | |
| 600 | client = i2c_verify_client(dev); |
| 601 | if (!client) |
| 602 | return NOTIFY_DONE; |
| 603 | |
| 604 | /* Is the client in one of our adapters? */ |
| 605 | for (chan_id = 0; chan_id < atr->max_adapters; ++chan_id) { |
| 606 | if (client->adapter == atr->adapter[chan_id]) |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | if (chan_id == atr->max_adapters) |
| 611 | return NOTIFY_DONE; |
| 612 | |
| 613 | switch (event) { |
| 614 | case BUS_NOTIFY_ADD_DEVICE: |
| 615 | ret = i2c_atr_attach_addr(client->adapter, client->addr); |
| 616 | if (ret) |
| 617 | dev_err(atr->dev, |
| 618 | "Failed to attach remote client '%s': %d\n", |
| 619 | dev_name(dev), ret); |
| 620 | break; |
| 621 | |
| 622 | case BUS_NOTIFY_REMOVED_DEVICE: |
| 623 | i2c_atr_detach_addr(client->adapter, client->addr); |
| 624 | break; |
| 625 | |
| 626 | default: |
| 627 | break; |
| 628 | } |
| 629 | |
| 630 | return NOTIFY_DONE; |
| 631 | } |
| 632 | |
| 633 | static int i2c_atr_parse_alias_pool(struct i2c_atr *atr) |
| 634 | { |
| 635 | struct i2c_atr_alias_pool *alias_pool; |
| 636 | struct device *dev = atr->dev; |
| 637 | size_t num_aliases; |
| 638 | unsigned int i; |
| 639 | u32 *aliases32; |
| 640 | int ret; |
| 641 | |
| 642 | if (!fwnode_property_present(dev_fwnode(dev), "i2c-alias-pool")) { |
| 643 | num_aliases = 0; |
| 644 | } else { |
| 645 | ret = fwnode_property_count_u32(dev_fwnode(dev), "i2c-alias-pool"); |
| 646 | if (ret < 0) { |
| 647 | dev_err(dev, "Failed to count 'i2c-alias-pool' property: %d\n", |
| 648 | ret); |
| 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | num_aliases = ret; |
| 653 | } |
| 654 | |
| 655 | alias_pool = i2c_atr_alloc_alias_pool(num_aliases, true); |
| 656 | if (IS_ERR(alias_pool)) { |
| 657 | ret = PTR_ERR(alias_pool); |
| 658 | dev_err(dev, "Failed to allocate alias pool, err %d\n", ret); |
| 659 | return ret; |
| 660 | } |
| 661 | |
| 662 | atr->alias_pool = alias_pool; |
| 663 | |
| 664 | if (!alias_pool->size) |
| 665 | return 0; |
| 666 | |
| 667 | aliases32 = kcalloc(num_aliases, sizeof(*aliases32), GFP_KERNEL); |
| 668 | if (!aliases32) { |
| 669 | ret = -ENOMEM; |
| 670 | goto err_free_alias_pool; |
| 671 | } |
| 672 | |
| 673 | ret = fwnode_property_read_u32_array(dev_fwnode(dev), "i2c-alias-pool", |
| 674 | aliases32, num_aliases); |
| 675 | if (ret < 0) { |
| 676 | dev_err(dev, "Failed to read 'i2c-alias-pool' property: %d\n", |
| 677 | ret); |
| 678 | goto err_free_aliases32; |
| 679 | } |
| 680 | |
| 681 | for (i = 0; i < num_aliases; i++) { |
| 682 | if (!(aliases32[i] & 0xffff0000)) { |
| 683 | alias_pool->aliases[i] = aliases32[i]; |
| 684 | continue; |
| 685 | } |
| 686 | |
| 687 | dev_err(dev, "Failed to parse 'i2c-alias-pool' property: I2C flags are not supported\n"); |
| 688 | ret = -EINVAL; |
| 689 | goto err_free_aliases32; |
| 690 | } |
| 691 | |
| 692 | kfree(aliases32); |
| 693 | |
| 694 | dev_dbg(dev, "i2c-alias-pool has %zu aliases\n", alias_pool->size); |
| 695 | |
| 696 | return 0; |
| 697 | |
| 698 | err_free_aliases32: |
| 699 | kfree(aliases32); |
| 700 | err_free_alias_pool: |
| 701 | i2c_atr_free_alias_pool(alias_pool); |
| 702 | return ret; |
| 703 | } |
| 704 | |
| 705 | struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent, struct device *dev, |
| 706 | const struct i2c_atr_ops *ops, int max_adapters, |
| 707 | u32 flags) |
| 708 | { |
| 709 | struct i2c_atr *atr; |
| 710 | int ret; |
| 711 | |
| 712 | if (max_adapters > ATR_MAX_ADAPTERS) |
| 713 | return ERR_PTR(-EINVAL); |
| 714 | |
| 715 | if (!ops || !ops->attach_addr || !ops->detach_addr) |
| 716 | return ERR_PTR(-EINVAL); |
| 717 | |
| 718 | atr = kzalloc_flex(*atr, adapter, max_adapters); |
| 719 | if (!atr) |
| 720 | return ERR_PTR(-ENOMEM); |
| 721 | |
| 722 | lockdep_register_key(&atr->lock_key); |
| 723 | mutex_init_with_key(&atr->lock, &atr->lock_key); |
| 724 | |
| 725 | atr->parent = parent; |
| 726 | atr->dev = dev; |
| 727 | atr->ops = ops; |
| 728 | atr->max_adapters = max_adapters; |
| 729 | atr->flags = flags; |
| 730 | |
| 731 | if (parent->algo->master_xfer) |
| 732 | atr->algo.xfer = i2c_atr_master_xfer; |
| 733 | if (parent->algo->smbus_xfer) |
| 734 | atr->algo.smbus_xfer = i2c_atr_smbus_xfer; |
| 735 | atr->algo.functionality = i2c_atr_functionality; |
| 736 | |
| 737 | ret = i2c_atr_parse_alias_pool(atr); |
| 738 | if (ret) |
| 739 | goto err_destroy_mutex; |
| 740 | |
| 741 | atr->i2c_nb.notifier_call = i2c_atr_bus_notifier_call; |
| 742 | ret = bus_register_notifier(&i2c_bus_type, &atr->i2c_nb); |
| 743 | if (ret) |
| 744 | goto err_free_alias_pool; |
| 745 | |
| 746 | return atr; |
| 747 | |
| 748 | err_free_alias_pool: |
| 749 | i2c_atr_free_alias_pool(atr->alias_pool); |
| 750 | err_destroy_mutex: |
| 751 | mutex_destroy(&atr->lock); |
| 752 | lockdep_unregister_key(&atr->lock_key); |
| 753 | kfree(atr); |
| 754 | |
| 755 | return ERR_PTR(ret); |
| 756 | } |
| 757 | EXPORT_SYMBOL_NS_GPL(i2c_atr_new, "I2C_ATR"); |
| 758 | |
| 759 | void i2c_atr_delete(struct i2c_atr *atr) |
| 760 | { |
| 761 | unsigned int i; |
| 762 | |
| 763 | for (i = 0; i < atr->max_adapters; ++i) |
| 764 | WARN_ON(atr->adapter[i]); |
| 765 | |
| 766 | bus_unregister_notifier(&i2c_bus_type, &atr->i2c_nb); |
| 767 | i2c_atr_free_alias_pool(atr->alias_pool); |
| 768 | mutex_destroy(&atr->lock); |
| 769 | lockdep_unregister_key(&atr->lock_key); |
| 770 | kfree(atr); |
| 771 | } |
| 772 | EXPORT_SYMBOL_NS_GPL(i2c_atr_delete, "I2C_ATR"); |
| 773 | |
| 774 | int i2c_atr_add_adapter(struct i2c_atr *atr, struct i2c_atr_adap_desc *desc) |
| 775 | { |
| 776 | struct fwnode_handle *bus_handle = desc->bus_handle; |
| 777 | struct i2c_adapter *parent = atr->parent; |
| 778 | char symlink_name[ATR_MAX_SYMLINK_LEN]; |
| 779 | struct device *dev = atr->dev; |
| 780 | u32 chan_id = desc->chan_id; |
| 781 | struct i2c_atr_chan *chan; |
| 782 | int ret, idx; |
| 783 | |
| 784 | if (chan_id >= atr->max_adapters) { |
| 785 | dev_err(dev, "No room for more i2c-atr adapters\n"); |
| 786 | return -EINVAL; |
| 787 | } |
| 788 | |
| 789 | if (atr->adapter[chan_id]) { |
| 790 | dev_err(dev, "Adapter %d already present\n", chan_id); |
| 791 | return -EEXIST; |
| 792 | } |
| 793 | |
| 794 | chan = kzalloc_obj(*chan); |
| 795 | if (!chan) |
| 796 | return -ENOMEM; |
| 797 | |
| 798 | if (!desc->parent) |
| 799 | desc->parent = dev; |
| 800 | |
| 801 | chan->atr = atr; |
| 802 | chan->chan_id = chan_id; |
| 803 | INIT_LIST_HEAD(&chan->alias_pairs); |
| 804 | lockdep_register_key(&chan->alias_pairs_lock_key); |
| 805 | lockdep_register_key(&chan->orig_addrs_lock_key); |
| 806 | mutex_init_with_key(&chan->alias_pairs_lock, &chan->alias_pairs_lock_key); |
| 807 | mutex_init_with_key(&chan->orig_addrs_lock, &chan->orig_addrs_lock_key); |
| 808 | |
| 809 | snprintf(chan->adap.name, sizeof(chan->adap.name), "i2c-%d-atr-%d", |
| 810 | i2c_adapter_id(parent), chan_id); |
| 811 | chan->adap.owner = THIS_MODULE; |
| 812 | chan->adap.algo = &atr->algo; |
| 813 | chan->adap.algo_data = chan; |
| 814 | chan->adap.dev.parent = desc->parent; |
| 815 | chan->adap.retries = parent->retries; |
| 816 | chan->adap.timeout = parent->timeout; |
| 817 | chan->adap.quirks = parent->quirks; |
| 818 | chan->adap.lock_ops = &i2c_atr_lock_ops; |
| 819 | |
| 820 | if (bus_handle) { |
| 821 | device_set_node(&chan->adap.dev, fwnode_handle_get(bus_handle)); |
| 822 | } else { |
| 823 | struct fwnode_handle *atr_node; |
| 824 | struct fwnode_handle *child; |
| 825 | u32 reg; |
| 826 | |
| 827 | atr_node = device_get_named_child_node(dev, "i2c-atr"); |
| 828 | |
| 829 | fwnode_for_each_child_node(atr_node, child) { |
| 830 | ret = fwnode_property_read_u32(child, "reg", ®); |
| 831 | if (ret) |
| 832 | continue; |
| 833 | if (chan_id == reg) |
| 834 | break; |
| 835 | } |
| 836 | |
| 837 | device_set_node(&chan->adap.dev, child); |
| 838 | fwnode_handle_put(atr_node); |
| 839 | } |
| 840 | |
| 841 | if (desc->num_aliases > 0) { |
| 842 | chan->alias_pool = i2c_atr_alloc_alias_pool(desc->num_aliases, false); |
| 843 | if (IS_ERR(chan->alias_pool)) { |
| 844 | ret = PTR_ERR(chan->alias_pool); |
| 845 | goto err_fwnode_put; |
| 846 | } |
| 847 | |
| 848 | for (idx = 0; idx < desc->num_aliases; idx++) |
| 849 | chan->alias_pool->aliases[idx] = desc->aliases[idx]; |
| 850 | } else { |
| 851 | chan->alias_pool = atr->alias_pool; |
| 852 | } |
| 853 | |
| 854 | atr->adapter[chan_id] = &chan->adap; |
| 855 | |
| 856 | ret = i2c_add_adapter(&chan->adap); |
| 857 | if (ret) { |
| 858 | atr->adapter[chan_id] = NULL; |
| 859 | dev_err(dev, "failed to add atr-adapter %u (error=%d)\n", |
| 860 | chan_id, ret); |
| 861 | goto err_free_alias_pool; |
| 862 | } |
| 863 | |
| 864 | snprintf(symlink_name, sizeof(symlink_name), "channel-%u", |
| 865 | chan->chan_id); |
| 866 | |
| 867 | ret = sysfs_create_link(&chan->adap.dev.kobj, &dev->kobj, "atr_device"); |
| 868 | if (ret) |
| 869 | dev_warn(dev, "can't create symlink to atr device\n"); |
| 870 | ret = sysfs_create_link(&dev->kobj, &chan->adap.dev.kobj, symlink_name); |
| 871 | if (ret) |
| 872 | dev_warn(dev, "can't create symlink for channel %u\n", chan_id); |
| 873 | |
| 874 | dev_dbg(dev, "Added ATR child bus %d\n", i2c_adapter_id(&chan->adap)); |
| 875 | |
| 876 | return 0; |
| 877 | |
| 878 | err_free_alias_pool: |
| 879 | if (!chan->alias_pool->shared) |
| 880 | i2c_atr_free_alias_pool(chan->alias_pool); |
| 881 | err_fwnode_put: |
| 882 | fwnode_handle_put(dev_fwnode(&chan->adap.dev)); |
| 883 | mutex_destroy(&chan->orig_addrs_lock); |
| 884 | mutex_destroy(&chan->alias_pairs_lock); |
| 885 | lockdep_unregister_key(&chan->orig_addrs_lock_key); |
| 886 | lockdep_unregister_key(&chan->alias_pairs_lock_key); |
| 887 | kfree(chan); |
| 888 | return ret; |
| 889 | } |
| 890 | EXPORT_SYMBOL_NS_GPL(i2c_atr_add_adapter, "I2C_ATR"); |
| 891 | |
| 892 | void i2c_atr_del_adapter(struct i2c_atr *atr, u32 chan_id) |
| 893 | { |
| 894 | char symlink_name[ATR_MAX_SYMLINK_LEN]; |
| 895 | struct i2c_adapter *adap; |
| 896 | struct i2c_atr_chan *chan; |
| 897 | struct fwnode_handle *fwnode; |
| 898 | struct device *dev = atr->dev; |
| 899 | |
| 900 | adap = atr->adapter[chan_id]; |
| 901 | if (!adap) |
| 902 | return; |
| 903 | |
| 904 | chan = adap->algo_data; |
| 905 | fwnode = dev_fwnode(&adap->dev); |
| 906 | |
| 907 | dev_dbg(dev, "Removing ATR child bus %d\n", i2c_adapter_id(adap)); |
| 908 | |
| 909 | snprintf(symlink_name, sizeof(symlink_name), "channel-%u", |
| 910 | chan->chan_id); |
| 911 | sysfs_remove_link(&dev->kobj, symlink_name); |
| 912 | sysfs_remove_link(&chan->adap.dev.kobj, "atr_device"); |
| 913 | |
| 914 | i2c_del_adapter(adap); |
| 915 | |
| 916 | if (!chan->alias_pool->shared) |
| 917 | i2c_atr_free_alias_pool(chan->alias_pool); |
| 918 | |
| 919 | atr->adapter[chan_id] = NULL; |
| 920 | |
| 921 | fwnode_handle_put(fwnode); |
| 922 | mutex_destroy(&chan->orig_addrs_lock); |
| 923 | mutex_destroy(&chan->alias_pairs_lock); |
| 924 | lockdep_unregister_key(&chan->orig_addrs_lock_key); |
| 925 | lockdep_unregister_key(&chan->alias_pairs_lock_key); |
| 926 | kfree(chan->orig_addrs); |
| 927 | kfree(chan); |
| 928 | } |
| 929 | EXPORT_SYMBOL_NS_GPL(i2c_atr_del_adapter, "I2C_ATR"); |
| 930 | |
| 931 | void i2c_atr_set_driver_data(struct i2c_atr *atr, void *data) |
| 932 | { |
| 933 | atr->priv = data; |
| 934 | } |
| 935 | EXPORT_SYMBOL_NS_GPL(i2c_atr_set_driver_data, "I2C_ATR"); |
| 936 | |
| 937 | void *i2c_atr_get_driver_data(struct i2c_atr *atr) |
| 938 | { |
| 939 | return atr->priv; |
| 940 | } |
| 941 | EXPORT_SYMBOL_NS_GPL(i2c_atr_get_driver_data, "I2C_ATR"); |
| 942 | |
| 943 | MODULE_AUTHOR("Luca Ceresoli <luca.ceresoli@bootlin.com>"); |
| 944 | MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>"); |
| 945 | MODULE_DESCRIPTION("I2C Address Translator"); |
| 946 | MODULE_LICENSE("GPL"); |