Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[firefly-linux-kernel-4.4.55.git] / drivers / i2c / i2c-core.c
index ccc6445979c4ad23a54a54e05292b1003233a9c8..2efa56c5ff2c32d10ff3018def5bc077b8492e4e 100644 (file)
@@ -772,6 +772,23 @@ struct device_type i2c_adapter_type = {
 };
 EXPORT_SYMBOL_GPL(i2c_adapter_type);
 
+/**
+ * i2c_verify_adapter - return parameter as i2c_adapter or NULL
+ * @dev: device, probably from some driver model iterator
+ *
+ * When traversing the driver model tree, perhaps using driver model
+ * iterators like @device_for_each_child(), you can't assume very much
+ * about the nodes you find.  Use this function to avoid oopses caused
+ * by wrongly treating some non-I2C device as an i2c_adapter.
+ */
+struct i2c_adapter *i2c_verify_adapter(struct device *dev)
+{
+       return (dev->type == &i2c_adapter_type)
+                       ? to_i2c_adapter(dev)
+                       : NULL;
+}
+EXPORT_SYMBOL(i2c_verify_adapter);
+
 #ifdef CONFIG_I2C_COMPAT
 static struct class_compat *i2c_adapter_compat_class;
 #endif
@@ -2127,7 +2144,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
        int try;
        s32 res;
 
-       flags &= I2C_M_TEN | I2C_CLIENT_PEC;
+       flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
 
        if (adapter->algo->smbus_xfer) {
                i2c_lock_adapter(adapter);
@@ -2145,11 +2162,17 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
                                break;
                }
                i2c_unlock_adapter(adapter);
-       } else
-               res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
-                                             command, protocol, data);
 
-       return res;
+               if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
+                       return res;
+               /*
+                * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
+                * implement native support for the SMBus operation.
+                */
+       }
+
+       return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
+                                      command, protocol, data);
 }
 EXPORT_SYMBOL(i2c_smbus_xfer);