diff options
author | Doug Anderson <dianders@chromium.org> | 2013-03-01 11:57:31 -0500 |
---|---|---|
committer | Wolfram Sang <wsa@the-dreams.de> | 2013-03-24 05:30:53 -0400 |
commit | ee5c27440cc24d62ec463cce4c000bb32c5692c7 (patch) | |
tree | 9f6a2bfdeff932a5ac6a42f1f6cbec7f1d22f5b4 /drivers/i2c | |
parent | 8bb9660418e05bb1845ac1a2428444d78e322cc7 (diff) |
i2c: core: Pick i2c bus number from dt alias if present
This allows you to get the equivalent functionality of
i2c_add_numbered_adapter() with all data in the device tree and no
special case code in your driver. This is a common device tree
technique.
For quick reference, the FDT syntax for using an alias to provide an
ID looks like:
aliases {
i2c0 = &i2c_0;
i2c1 = &i2c_1;
};
Signed-off-by: Doug Anderson <dianders@chromium.org>
[wsa: removed one check from static function. We know our callers]
Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
Diffstat (limited to 'drivers/i2c')
-rw-r--r-- | drivers/i2c/i2c-core.c | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index 991d38daa87d..f7dfe878a51b 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c | |||
@@ -921,13 +921,35 @@ out_list: | |||
921 | } | 921 | } |
922 | 922 | ||
923 | /** | 923 | /** |
924 | * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1 | ||
925 | * @adap: the adapter to register (with adap->nr initialized) | ||
926 | * Context: can sleep | ||
927 | * | ||
928 | * See i2c_add_numbered_adapter() for details. | ||
929 | */ | ||
930 | static int __i2c_add_numbered_adapter(struct i2c_adapter *adap) | ||
931 | { | ||
932 | int id; | ||
933 | |||
934 | mutex_lock(&core_lock); | ||
935 | id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, | ||
936 | GFP_KERNEL); | ||
937 | mutex_unlock(&core_lock); | ||
938 | if (id < 0) | ||
939 | return id == -ENOSPC ? -EBUSY : id; | ||
940 | |||
941 | return i2c_register_adapter(adap); | ||
942 | } | ||
943 | |||
944 | /** | ||
924 | * i2c_add_adapter - declare i2c adapter, use dynamic bus number | 945 | * i2c_add_adapter - declare i2c adapter, use dynamic bus number |
925 | * @adapter: the adapter to add | 946 | * @adapter: the adapter to add |
926 | * Context: can sleep | 947 | * Context: can sleep |
927 | * | 948 | * |
928 | * This routine is used to declare an I2C adapter when its bus number | 949 | * This routine is used to declare an I2C adapter when its bus number |
929 | * doesn't matter. Examples: for I2C adapters dynamically added by | 950 | * doesn't matter or when its bus number is specified by an dt alias. |
930 | * USB links or PCI plugin cards. | 951 | * Examples of bases when the bus number doesn't matter: I2C adapters |
952 | * dynamically added by USB links or PCI plugin cards. | ||
931 | * | 953 | * |
932 | * When this returns zero, a new bus number was allocated and stored | 954 | * When this returns zero, a new bus number was allocated and stored |
933 | * in adap->nr, and the specified adapter became available for clients. | 955 | * in adap->nr, and the specified adapter became available for clients. |
@@ -935,8 +957,17 @@ out_list: | |||
935 | */ | 957 | */ |
936 | int i2c_add_adapter(struct i2c_adapter *adapter) | 958 | int i2c_add_adapter(struct i2c_adapter *adapter) |
937 | { | 959 | { |
960 | struct device *dev = &adapter->dev; | ||
938 | int id; | 961 | int id; |
939 | 962 | ||
963 | if (dev->of_node) { | ||
964 | id = of_alias_get_id(dev->of_node, "i2c"); | ||
965 | if (id >= 0) { | ||
966 | adapter->nr = id; | ||
967 | return __i2c_add_numbered_adapter(adapter); | ||
968 | } | ||
969 | } | ||
970 | |||
940 | mutex_lock(&core_lock); | 971 | mutex_lock(&core_lock); |
941 | id = idr_alloc(&i2c_adapter_idr, adapter, | 972 | id = idr_alloc(&i2c_adapter_idr, adapter, |
942 | __i2c_first_dynamic_bus_num, 0, GFP_KERNEL); | 973 | __i2c_first_dynamic_bus_num, 0, GFP_KERNEL); |
@@ -975,18 +1006,10 @@ EXPORT_SYMBOL(i2c_add_adapter); | |||
975 | */ | 1006 | */ |
976 | int i2c_add_numbered_adapter(struct i2c_adapter *adap) | 1007 | int i2c_add_numbered_adapter(struct i2c_adapter *adap) |
977 | { | 1008 | { |
978 | int id; | ||
979 | |||
980 | if (adap->nr == -1) /* -1 means dynamically assign bus id */ | 1009 | if (adap->nr == -1) /* -1 means dynamically assign bus id */ |
981 | return i2c_add_adapter(adap); | 1010 | return i2c_add_adapter(adap); |
982 | 1011 | ||
983 | mutex_lock(&core_lock); | 1012 | return __i2c_add_numbered_adapter(adap); |
984 | id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, | ||
985 | GFP_KERNEL); | ||
986 | mutex_unlock(&core_lock); | ||
987 | if (id < 0) | ||
988 | return id == -ENOSPC ? -EBUSY : id; | ||
989 | return i2c_register_adapter(adap); | ||
990 | } | 1013 | } |
991 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); | 1014 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); |
992 | 1015 | ||