Skip to content

Commit

Permalink
Merge branch 'i2c/for-next' of git://git.kernel.org/pub/scm/linux/ker…
Browse files Browse the repository at this point in the history
…nel/git/wsa/linux

Pull i2c updates from Wolfram Sang:
 "Some I2C core API additions which are kind of simple but enhance error
  checking for users a lot, especially by returning errno now.

  There are wrappers to still support the old API but it will be removed
  once all users are converted"

* 'i2c/for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux:
  i2c: core: add device-managed version of i2c_new_dummy
  i2c: core: improve return value handling of i2c_new_device and i2c_new_dummy
  • Loading branch information
Linus Torvalds committed May 19, 2019
2 parents c4d36b6 + b8f5fe3 commit f23d871
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Documentation/driver-model/devres.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ GPIO
devm_gpio_request_one()
devm_gpio_free()

I2C
devm_i2c_new_dummy_device()

IIO
devm_iio_device_alloc()
devm_iio_device_free()
Expand Down
118 changes: 105 additions & 13 deletions drivers/i2c/i2c-core-base.c
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ static int i2c_dev_irq_from_resources(const struct resource *resources,
}

/**
* i2c_new_device - instantiate an i2c device
* i2c_new_client_device - instantiate an i2c device
* @adap: the adapter managing the device
* @info: describes one I2C device; bus_num is ignored
* Context: can sleep
Expand All @@ -727,17 +727,17 @@ static int i2c_dev_irq_from_resources(const struct resource *resources,
* 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.
* i2c_unregister_device(); or an ERR_PTR to describe the error.
*/
struct i2c_client *
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
static struct i2c_client *
i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
{
struct i2c_client *client;
int status;

client = kzalloc(sizeof *client, GFP_KERNEL);
if (!client)
return NULL;
return ERR_PTR(-ENOMEM);

client->adapter = adap;

Expand Down Expand Up @@ -803,7 +803,31 @@ i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
client->name, client->addr, status);
out_err_silent:
kfree(client);
return NULL;
return ERR_PTR(status);
}
EXPORT_SYMBOL_GPL(i2c_new_client_device);

/**
* 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
*
* This deprecated function has the same functionality as
* @i2c_new_client_device, it just returns NULL instead of an ERR_PTR in case of
* an error for compatibility with current I2C API. It will be removed once all
* users are converted.
*
* This returns the new i2c client, which may be saved for later use with
* i2c_unregister_device(); or NULL to indicate an error.
*/
struct i2c_client *
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
{
struct i2c_client *ret;

ret = i2c_new_client_device(adap, info);
return IS_ERR(ret) ? NULL : ret;
}
EXPORT_SYMBOL_GPL(i2c_new_device);

Expand Down Expand Up @@ -854,7 +878,7 @@ static struct i2c_driver dummy_driver = {
};

/**
* i2c_new_dummy - return a new i2c device bound to a dummy driver
* i2c_new_dummy_device - return a new i2c device bound to a dummy driver
* @adapter: the adapter managing the device
* @address: seven bit address to be used
* Context: can sleep
Expand All @@ -869,18 +893,86 @@ static struct i2c_driver dummy_driver = {
* different driver.
*
* This returns the new i2c client, which should be saved for later use with
* i2c_unregister_device(); or NULL to indicate an error.
* i2c_unregister_device(); or an ERR_PTR to describe the error.
*/
struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
static struct i2c_client *
i2c_new_dummy_device(struct i2c_adapter *adapter, u16 address)
{
struct i2c_board_info info = {
I2C_BOARD_INFO("dummy", address),
};

return i2c_new_device(adapter, &info);
return i2c_new_client_device(adapter, &info);
}
EXPORT_SYMBOL_GPL(i2c_new_dummy_device);

/**
* i2c_new_dummy - return a new i2c device bound to a dummy driver
* @adapter: the adapter managing the device
* @address: seven bit address to be used
* Context: can sleep
*
* This deprecated function has the same functionality as @i2c_new_dummy_device,
* it just returns NULL instead of an ERR_PTR in case of an error for
* compatibility with current I2C API. It will be removed once all users are
* converted.
*
* This returns the new i2c client, which should be saved for later use with
* i2c_unregister_device(); or NULL to indicate an error.
*/
struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
{
struct i2c_client *ret;

ret = i2c_new_dummy_device(adapter, address);
return IS_ERR(ret) ? NULL : ret;
}
EXPORT_SYMBOL_GPL(i2c_new_dummy);

struct i2c_dummy_devres {
struct i2c_client *client;
};

static void devm_i2c_release_dummy(struct device *dev, void *res)
{
struct i2c_dummy_devres *this = res;

i2c_unregister_device(this->client);
}

/**
* devm_i2c_new_dummy_device - return a new i2c device bound to a dummy driver
* @dev: device the managed resource is bound to
* @adapter: the adapter managing the device
* @address: seven bit address to be used
* Context: can sleep
*
* This is the device-managed version of @i2c_new_dummy_device. It returns the
* new i2c client or an ERR_PTR in case of an error.
*/
struct i2c_client *devm_i2c_new_dummy_device(struct device *dev,
struct i2c_adapter *adapter,
u16 address)
{
struct i2c_dummy_devres *dr;
struct i2c_client *client;

dr = devres_alloc(devm_i2c_release_dummy, sizeof(*dr), GFP_KERNEL);
if (!dr)
return ERR_PTR(-ENOMEM);

client = i2c_new_dummy_device(adapter, address);
if (IS_ERR(client)) {
devres_free(dr);
} else {
dr->client = client;
devres_add(dev, dr);
}

return client;
}
EXPORT_SYMBOL_GPL(devm_i2c_new_dummy_device);

/**
* i2c_new_secondary_device - Helper to get the instantiated secondary address
* and create the associated device
Expand Down Expand Up @@ -1000,9 +1092,9 @@ i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
info.flags |= I2C_CLIENT_SLAVE;
}

client = i2c_new_device(adap, &info);
if (!client)
return -EINVAL;
client = i2c_new_client_device(adap, &info);
if (IS_ERR(client))
return PTR_ERR(client);

/* Keep track of the added device */
mutex_lock(&adap->userspace_clients_lock);
Expand Down
3 changes: 3 additions & 0 deletions include/linux/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,9 @@ extern int i2c_probe_func_quick_read(struct i2c_adapter *, unsigned short addr);
extern struct i2c_client *
i2c_new_dummy(struct i2c_adapter *adap, u16 address);

extern struct i2c_client *
devm_i2c_new_dummy_device(struct device *dev, struct i2c_adapter *adap, u16 address);

extern struct i2c_client *
i2c_new_secondary_device(struct i2c_client *client,
const char *name,
Expand Down

0 comments on commit f23d871

Please sign in to comment.