i2c: Kill is_newstyle_driver
[firefly-linux-kernel-4.4.55.git] / drivers / i2c / i2c-core.c
index 85e2e919d1cd86e36e0c07815edf2b098e87ae49..95fb997b41e01a4c3a7639abbdbe57b90ed520fb 100644 (file)
@@ -29,7 +29,6 @@
 #include <linux/i2c.h>
 #include <linux/init.h>
 #include <linux/idr.h>
-#include <linux/platform_device.h>
 #include <linux/mutex.h>
 #include <linux/completion.h>
 #include <linux/hardirq.h>
@@ -42,8 +41,7 @@
 static DEFINE_MUTEX(core_lock);
 static DEFINE_IDR(i2c_adapter_idr);
 
-#define is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
-
+static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
 
 /* ------------------------------------------------------------------------- */
@@ -64,12 +62,6 @@ static int i2c_device_match(struct device *dev, struct device_driver *drv)
        struct i2c_client       *client = to_i2c_client(dev);
        struct i2c_driver       *driver = to_i2c_driver(drv);
 
-       /* make legacy i2c drivers bypass driver model probing entirely;
-        * such drivers scan each i2c adapter/bus themselves.
-        */
-       if (!is_newstyle_driver(driver))
-               return 0;
-
        /* match on an id table if there is one */
        if (driver->id_table)
                return i2c_match_id(driver->id_table, client) != NULL;
@@ -84,10 +76,6 @@ static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
 {
        struct i2c_client       *client = to_i2c_client(dev);
 
-       /* by definition, legacy drivers can't hotplug */
-       if (dev->driver)
-               return 0;
-
        if (add_uevent_var(env, "MODALIAS=%s%s",
                           I2C_MODULE_PREFIX, client->name))
                return -ENOMEM;
@@ -176,12 +164,6 @@ static int i2c_device_resume(struct device *dev)
        return driver->resume(to_i2c_client(dev));
 }
 
-static void i2c_client_release(struct device *dev)
-{
-       struct i2c_client *client = to_i2c_client(dev);
-       complete(&client->released);
-}
-
 static void i2c_client_dev_release(struct device *dev)
 {
        kfree(to_i2c_client(dev));
@@ -241,15 +223,17 @@ EXPORT_SYMBOL(i2c_verify_client);
 
 
 /**
- * i2c_new_device - instantiate an i2c device for use with a new style driver
+ * i2c_new_device - instantiate an i2c device
  * @adap: the adapter managing the device
  * @info: describes one I2C device; bus_num is ignored
  * Context: can sleep
  *
- * Create a device to work with a new style i2c driver, where binding is
- * handled through driver model probe()/remove() methods.  This call is not
- * appropriate for use by mainboad initialization logic, which usually runs
- * during an arch_initcall() long before any i2c_adapter could exist.
+ * Create an i2c device. Binding is handled through driver model
+ * probe()/remove() methods.  A driver may be bound to this device when we
+ * return from this function, or any later moment (e.g. maybe hotplugging will
+ * load the driver module).  This call is not appropriate for use by mainboard
+ * initialization logic, which usually runs during an arch_initcall() long
+ * before any i2c_adapter could exist.
  *
  * This returns the new i2c client, which may be saved for later use with
  * i2c_unregister_device(); or NULL to indicate an error.
@@ -277,17 +261,35 @@ i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
 
        strlcpy(client->name, info->type, sizeof(client->name));
 
-       /* a new style driver may be bound to this device when we
-        * return from this function, or any later moment (e.g. maybe
-        * hotplugging will load the driver module).  and the device
-        * refcount model is the standard driver model one.
-        */
-       status = i2c_attach_client(client);
-       if (status < 0) {
-               kfree(client);
-               client = NULL;
-       }
+       /* Check for address business */
+       status = i2c_check_addr(adap, client->addr);
+       if (status)
+               goto out_err;
+
+       client->dev.parent = &client->adapter->dev;
+       client->dev.bus = &i2c_bus_type;
+       client->dev.release = i2c_client_dev_release;
+
+       dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
+                    client->addr);
+       status = device_register(&client->dev);
+       if (status)
+               goto out_err;
+
+       mutex_lock(&adap->clist_lock);
+       list_add_tail(&client->list, &adap->clients);
+       mutex_unlock(&adap->clist_lock);
+
+       dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
+               client->name, dev_name(&client->dev));
+
        return client;
+
+out_err:
+       dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
+               "(%d)\n", client->name, client->addr, status);
+       kfree(client);
+       return NULL;
 }
 EXPORT_SYMBOL_GPL(i2c_new_device);
 
@@ -300,22 +302,6 @@ EXPORT_SYMBOL_GPL(i2c_new_device);
 void i2c_unregister_device(struct i2c_client *client)
 {
        struct i2c_adapter      *adapter = client->adapter;
-       struct i2c_driver       *driver = client->driver;
-
-       if (driver && !is_newstyle_driver(driver)) {
-               dev_err(&client->dev, "can't unregister devices "
-                       "with legacy drivers\n");
-               WARN_ON(1);
-               return;
-       }
-
-       if (adapter->client_unregister) {
-               if (adapter->client_unregister(client)) {
-                       dev_warn(&client->dev,
-                                "client_unregister [%s] failed\n",
-                                client->name);
-               }
-       }
 
        mutex_lock(&adapter->clist_lock);
        list_del(&client->list);
@@ -451,16 +437,6 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
 
        mutex_lock(&core_lock);
 
-       /* Add the adapter to the driver core.
-        * If the parent pointer is not set up,
-        * we add this adapter to the host bus.
-        */
-       if (adap->dev.parent == NULL) {
-               adap->dev.parent = &platform_bus;
-               pr_debug("I2C adapter driver [%s] forgot to specify "
-                        "physical device\n", adap->name);
-       }
-
        /* Set default timeout to 1 second if not already set */
        if (adap->timeout == 0)
                adap->timeout = HZ;
@@ -474,7 +450,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
 
        dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
 
-       /* create pre-declared device nodes for new-style drivers */
+       /* create pre-declared device nodes */
        if (adap->nr < __i2c_first_dynamic_bus_num)
                i2c_scan_static_board_info(adap);
 
@@ -636,26 +612,9 @@ int i2c_del_adapter(struct i2c_adapter *adap)
        if (res)
                goto out_unlock;
 
-       /* detach any active clients. This must be done first, because
-        * it can fail; in which case we give up. */
+       /* Detach any active clients */
        list_for_each_entry_safe_reverse(client, _n, &adap->clients, list) {
-               struct i2c_driver       *driver;
-
-               driver = client->driver;
-
-               /* new style, follow standard driver model */
-               if (!driver || is_newstyle_driver(driver)) {
-                       i2c_unregister_device(client);
-                       continue;
-               }
-
-               /* legacy drivers create and remove clients themselves */
-               if ((res = driver->detach_client(client))) {
-                       dev_err(&adap->dev, "detach_client failed for client "
-                               "[%s] at address 0x%02x\n", client->name,
-                               client->addr);
-                       goto out_unlock;
-               }
+               i2c_unregister_device(client);
        }
 
        /* clean up the sysfs representation */
@@ -699,11 +658,7 @@ static int __attach_adapter(struct device *dev, void *data)
 
 /*
  * An i2c_driver is used with one or more i2c_client (device) nodes to access
- * i2c slave chips, on a bus instance associated with some i2c_adapter.  There
- * are two models for binding the driver to its device:  "new style" drivers
- * follow the standard Linux driver model and just respond to probe() calls
- * issued if the driver core sees they match(); "legacy" drivers create device
- * nodes themselves.
+ * i2c slave chips, on a bus instance associated with some i2c_adapter.
  */
 
 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
@@ -714,21 +669,11 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
        if (unlikely(WARN_ON(!i2c_bus_type.p)))
                return -EAGAIN;
 
-       /* new style driver methods can't mix with legacy ones */
-       if (is_newstyle_driver(driver)) {
-               if (driver->detach_adapter || driver->detach_client) {
-                       printk(KERN_WARNING
-                                       "i2c-core: driver [%s] is confused\n",
-                                       driver->driver.name);
-                       return -EINVAL;
-               }
-       }
-
        /* add the driver to the list of i2c drivers in the driver core */
        driver->driver.owner = owner;
        driver->driver.bus = &i2c_bus_type;
 
-       /* for new style drivers, when registration returns the driver core
+       /* When registration returns, the driver core
         * will have called probe() for all matching-but-unbound devices.
         */
        res = driver_register(&driver->driver);
@@ -764,32 +709,11 @@ static int __detach_adapter(struct device *dev, void *data)
                i2c_unregister_device(client);
        }
 
-       if (is_newstyle_driver(driver))
-               return 0;
-
-       /* Have a look at each adapter, if clients of this driver are still
-        * attached. If so, detach them to be able to kill the driver
-        * afterwards.
-        */
        if (driver->detach_adapter) {
                if (driver->detach_adapter(adapter))
                        dev_err(&adapter->dev,
                                "detach_adapter failed for driver [%s]\n",
                                driver->driver.name);
-       } else {
-               struct i2c_client *client, *_n;
-
-               list_for_each_entry_safe(client, _n, &adapter->clients, list) {
-                       if (client->driver != driver)
-                               continue;
-                       dev_dbg(&adapter->dev,
-                               "detaching client [%s] at 0x%02x\n",
-                               client->name, client->addr);
-                       if (driver->detach_client(client))
-                               dev_err(&adapter->dev, "detach_client "
-                                       "failed for client [%s] at 0x%02x\n",
-                                       client->name, client->addr);
-               }
        }
 
        return 0;
@@ -831,86 +755,6 @@ static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
        return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
 }
 
-int i2c_attach_client(struct i2c_client *client)
-{
-       struct i2c_adapter *adapter = client->adapter;
-       int res;
-
-       /* Check for address business */
-       res = i2c_check_addr(adapter, client->addr);
-       if (res)
-               return res;
-
-       client->dev.parent = &client->adapter->dev;
-       client->dev.bus = &i2c_bus_type;
-
-       if (client->driver)
-               client->dev.driver = &client->driver->driver;
-
-       if (client->driver && !is_newstyle_driver(client->driver)) {
-               client->dev.release = i2c_client_release;
-               dev_set_uevent_suppress(&client->dev, 1);
-       } else
-               client->dev.release = i2c_client_dev_release;
-
-       dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adapter),
-                    client->addr);
-       res = device_register(&client->dev);
-       if (res)
-               goto out_err;
-
-       mutex_lock(&adapter->clist_lock);
-       list_add_tail(&client->list, &adapter->clients);
-       mutex_unlock(&adapter->clist_lock);
-
-       dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
-               client->name, dev_name(&client->dev));
-
-       if (adapter->client_register)  {
-               if (adapter->client_register(client)) {
-                       dev_dbg(&adapter->dev, "client_register "
-                               "failed for client [%s] at 0x%02x\n",
-                               client->name, client->addr);
-               }
-       }
-
-       return 0;
-
-out_err:
-       dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
-               "(%d)\n", client->name, client->addr, res);
-       return res;
-}
-EXPORT_SYMBOL(i2c_attach_client);
-
-int i2c_detach_client(struct i2c_client *client)
-{
-       struct i2c_adapter *adapter = client->adapter;
-       int res = 0;
-
-       if (adapter->client_unregister)  {
-               res = adapter->client_unregister(client);
-               if (res) {
-                       dev_err(&client->dev,
-                               "client_unregister [%s] failed, "
-                               "client not detached\n", client->name);
-                       goto out;
-               }
-       }
-
-       mutex_lock(&adapter->clist_lock);
-       list_del(&client->list);
-       mutex_unlock(&adapter->clist_lock);
-
-       init_completion(&client->released);
-       device_unregister(&client->dev);
-       wait_for_completion(&client->released);
-
- out:
-       return res;
-}
-EXPORT_SYMBOL(i2c_detach_client);
-
 /**
  * i2c_use_client - increments the reference count of the i2c client structure
  * @client: the client being referenced
@@ -1022,7 +866,8 @@ module_exit(i2c_exit);
  */
 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 {
-       int ret;
+       unsigned long orig_jiffies;
+       int ret, try;
 
        /* REVISIT the fault reporting model here is weak:
         *
@@ -1060,7 +905,15 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
                        mutex_lock_nested(&adap->bus_lock, adap->level);
                }
 
-               ret = adap->algo->master_xfer(adap,msgs,num);
+               /* Retry automatically on arbitration loss */
+               orig_jiffies = jiffies;
+               for (ret = 0, try = 0; try <= adap->retries; try++) {
+                       ret = adap->algo->master_xfer(adap, msgs, num);
+                       if (ret != -EAGAIN)
+                               break;
+                       if (time_after(jiffies, orig_jiffies + adap->timeout))
+                               break;
+               }
                mutex_unlock(&adap->bus_lock);
 
                return ret;
@@ -1131,144 +984,7 @@ EXPORT_SYMBOL(i2c_master_recv);
  * Will not work for 10-bit addresses!
  * ----------------------------------------------------
  */
-static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
-                            int (*found_proc) (struct i2c_adapter *, int, int))
-{
-       int err;
-
-       /* Make sure the address is valid */
-       if (addr < 0x03 || addr > 0x77) {
-               dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
-                        addr);
-               return -EINVAL;
-       }
-
-       /* Skip if already in use */
-       if (i2c_check_addr(adapter, addr))
-               return 0;
-
-       /* Make sure there is something at this address, unless forced */
-       if (kind < 0) {
-               if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
-                                  I2C_SMBUS_QUICK, NULL) < 0)
-                       return 0;
-
-               /* prevent 24RF08 corruption */
-               if ((addr & ~0x0f) == 0x50)
-                       i2c_smbus_xfer(adapter, addr, 0, 0, 0,
-                                      I2C_SMBUS_QUICK, NULL);
-       }
-
-       /* Finally call the custom detection function */
-       err = found_proc(adapter, addr, kind);
-       /* -ENODEV can be returned if there is a chip at the given address
-          but it isn't supported by this chip driver. We catch it here as
-          this isn't an error. */
-       if (err == -ENODEV)
-               err = 0;
-
-       if (err)
-               dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
-                        addr, err);
-       return err;
-}
 
-int i2c_probe(struct i2c_adapter *adapter,
-             const struct i2c_client_address_data *address_data,
-             int (*found_proc) (struct i2c_adapter *, int, int))
-{
-       int i, err;
-       int adap_id = i2c_adapter_id(adapter);
-
-       /* Force entries are done first, and are not affected by ignore
-          entries */
-       if (address_data->forces) {
-               const unsigned short * const *forces = address_data->forces;
-               int kind;
-
-               for (kind = 0; forces[kind]; kind++) {
-                       for (i = 0; forces[kind][i] != I2C_CLIENT_END;
-                            i += 2) {
-                               if (forces[kind][i] == adap_id
-                                || forces[kind][i] == ANY_I2C_BUS) {
-                                       dev_dbg(&adapter->dev, "found force "
-                                               "parameter for adapter %d, "
-                                               "addr 0x%02x, kind %d\n",
-                                               adap_id, forces[kind][i + 1],
-                                               kind);
-                                       err = i2c_probe_address(adapter,
-                                               forces[kind][i + 1],
-                                               kind, found_proc);
-                                       if (err)
-                                               return err;
-                               }
-                       }
-               }
-       }
-
-       /* Stop here if we can't use SMBUS_QUICK */
-       if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
-               if (address_data->probe[0] == I2C_CLIENT_END
-                && address_data->normal_i2c[0] == I2C_CLIENT_END)
-                       return 0;
-
-               dev_dbg(&adapter->dev, "SMBus Quick command not supported, "
-                       "can't probe for chips\n");
-               return -EOPNOTSUPP;
-       }
-
-       /* Probe entries are done second, and are not affected by ignore
-          entries either */
-       for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
-               if (address_data->probe[i] == adap_id
-                || address_data->probe[i] == ANY_I2C_BUS) {
-                       dev_dbg(&adapter->dev, "found probe parameter for "
-                               "adapter %d, addr 0x%02x\n", adap_id,
-                               address_data->probe[i + 1]);
-                       err = i2c_probe_address(adapter,
-                                               address_data->probe[i + 1],
-                                               -1, found_proc);
-                       if (err)
-                               return err;
-               }
-       }
-
-       /* Normal entries are done last, unless shadowed by an ignore entry */
-       for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
-               int j, ignore;
-
-               ignore = 0;
-               for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
-                    j += 2) {
-                       if ((address_data->ignore[j] == adap_id ||
-                            address_data->ignore[j] == ANY_I2C_BUS)
-                        && address_data->ignore[j + 1]
-                           == address_data->normal_i2c[i]) {
-                               dev_dbg(&adapter->dev, "found ignore "
-                                       "parameter for adapter %d, "
-                                       "addr 0x%02x\n", adap_id,
-                                       address_data->ignore[j + 1]);
-                               ignore = 1;
-                               break;
-                       }
-               }
-               if (ignore)
-                       continue;
-
-               dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
-                       "addr 0x%02x\n", adap_id,
-                       address_data->normal_i2c[i]);
-               err = i2c_probe_address(adapter, address_data->normal_i2c[i],
-                                       -1, found_proc);
-               if (err)
-                       return err;
-       }
-
-       return 0;
-}
-EXPORT_SYMBOL(i2c_probe);
-
-/* Separate detection function for new-style drivers */
 static int i2c_detect_address(struct i2c_client *temp_client, int kind,
                              struct i2c_driver *driver)
 {
@@ -1509,7 +1225,7 @@ struct i2c_adapter* i2c_get_adapter(int id)
        struct i2c_adapter *adapter;
 
        mutex_lock(&core_lock);
-       adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
+       adapter = idr_find(&i2c_adapter_idr, id);
        if (adapter && !try_module_get(adapter->owner))
                adapter = NULL;
 
@@ -1995,14 +1711,27 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
                   char read_write, u8 command, int protocol,
                   union i2c_smbus_data *data)
 {
+       unsigned long orig_jiffies;
+       int try;
        s32 res;
 
        flags &= I2C_M_TEN | I2C_CLIENT_PEC;
 
        if (adapter->algo->smbus_xfer) {
                mutex_lock(&adapter->bus_lock);
-               res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
-                                               command, protocol, data);
+
+               /* Retry automatically on arbitration loss */
+               orig_jiffies = jiffies;
+               for (res = 0, try = 0; try <= adapter->retries; try++) {
+                       res = adapter->algo->smbus_xfer(adapter, addr, flags,
+                                                       read_write, command,
+                                                       protocol, data);
+                       if (res != -EAGAIN)
+                               break;
+                       if (time_after(jiffies,
+                                      orig_jiffies + adapter->timeout))
+                               break;
+               }
                mutex_unlock(&adapter->bus_lock);
        } else
                res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,