Skip to content

Commit 9c1600e

Browse files
David BrownellJean Delvare
authored andcommitted
i2c: Add i2c_board_info and i2c_new_device()
This provides partial support for new-style I2C driver binding. It builds on "struct i2c_board_info" declarations that identify I2C devices on a given board. This is needed on systems with I2C devices that can't be fully probed and/or autoconfigured, such as many embedded Linux configurations where the way a given I2C device is wired may affect how it must be used. There are two models for declaring such devices: * LATE -- using a public function i2c_new_device(). This lets modules declare I2C devices found *AFTER* a given I2C adapter becomes available. For example, a PCI card could create adapters giving access to utility chips on that card, and this would be used to associate those chips with those adapters. * EARLY -- from arch_initcall() level code, using a non-exported function i2c_register_board_info(). This copies the declarations *BEFORE* such an i2c_adapter becomes available, arranging that i2c_new_device() will be called later when i2c-core registers the relevant i2c_adapter. For example, arch/.../.../board-*.c files would declare the I2C devices along with their platform data, and I2C devices would behave much like PNPACPI devices. (That is, both enumerate from board-specific tables.) To match the exported i2c_new_device(), the previously-private function i2c_unregister_device() is now exported. Pending later patches using these new APIs, this is effectively a NOP. Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Jean Delvare <khali@linux-fr.org>
1 parent 4298cfc commit 9c1600e

7 files changed

Lines changed: 276 additions & 5 deletions

File tree

‎drivers/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ obj-$(CONFIG_GAMEPORT) += input/gameport/
5858
obj-$(CONFIG_INPUT) += input/
5959
obj-$(CONFIG_I2O) += message/
6060
obj-$(CONFIG_RTC_LIB) += rtc/
61-
obj-$(CONFIG_I2C) += i2c/
61+
obj-y += i2c/
6262
obj-$(CONFIG_W1) += w1/
6363
obj-$(CONFIG_HWMON) += hwmon/
6464
obj-$(CONFIG_PHONE) += telephony/

‎drivers/i2c/Kconfig‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ config I2C
2222
This I2C support can also be built as a module. If so, the module
2323
will be called i2c-core.
2424

25+
config I2C_BOARDINFO
26+
boolean
27+
depends on I2C
28+
default y
29+
2530
config I2C_CHARDEV
2631
tristate "I2C device interface"
2732
depends on I2C

‎drivers/i2c/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Makefile for the i2c core.
33
#
44

5+
obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o
56
obj-$(CONFIG_I2C) += i2c-core.o
67
obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o
78
obj-y += busses/ chips/ algos/

‎drivers/i2c/i2c-boardinfo.c‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* i2c-boardinfo.h - collect pre-declarations of I2C devices
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17+
*/
18+
19+
#include <linux/kernel.h>
20+
#include <linux/i2c.h>
21+
22+
#include "i2c-core.h"
23+
24+
25+
/* These symbols are exported ONLY FOR the i2c core.
26+
* No other users will be supported.
27+
*/
28+
DEFINE_MUTEX(__i2c_board_lock);
29+
EXPORT_SYMBOL_GPL(__i2c_board_lock);
30+
31+
LIST_HEAD(__i2c_board_list);
32+
EXPORT_SYMBOL_GPL(__i2c_board_list);
33+
34+
int __i2c_first_dynamic_bus_num;
35+
EXPORT_SYMBOL_GPL(__i2c_first_dynamic_bus_num);
36+
37+
38+
/**
39+
* i2c_register_board_info - statically declare I2C devices
40+
* @busnum: identifies the bus to which these devices belong
41+
* @info: vector of i2c device descriptors
42+
* @len: how many descriptors in the vector; may be zero to reserve
43+
* the specified bus number.
44+
*
45+
* Systems using the Linux I2C driver stack can declare tables of board info
46+
* while they initialize. This should be done in board-specific init code
47+
* near arch_initcall() time, or equivalent, before any I2C adapter driver is
48+
* registered. For example, mainboard init code could define several devices,
49+
* as could the init code for each daughtercard in a board stack.
50+
*
51+
* The I2C devices will be created later, after the adapter for the relevant
52+
* bus has been registered. After that moment, standard driver model tools
53+
* are used to bind "new style" I2C drivers to the devices. The bus number
54+
* for any device declared using this routine is not available for dynamic
55+
* allocation.
56+
*
57+
* The board info passed can safely be __initdata, but be careful of embedded
58+
* pointers (for platform_data, functions, etc) since that won't be copied.
59+
*/
60+
int __init
61+
i2c_register_board_info(int busnum,
62+
struct i2c_board_info const *info, unsigned len)
63+
{
64+
int status;
65+
66+
mutex_lock(&__i2c_board_lock);
67+
68+
/* dynamic bus numbers will be assigned after the last static one */
69+
if (busnum >= __i2c_first_dynamic_bus_num)
70+
__i2c_first_dynamic_bus_num = busnum + 1;
71+
72+
for (status = 0; len; len--, info++) {
73+
struct i2c_devinfo *devinfo;
74+
75+
devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL);
76+
if (!devinfo) {
77+
pr_debug("i2c-core: can't register boardinfo!\n");
78+
status = -ENOMEM;
79+
break;
80+
}
81+
82+
devinfo->busnum = busnum;
83+
devinfo->board_info = *info;
84+
list_add_tail(&devinfo->list, &__i2c_board_list);
85+
}
86+
87+
mutex_unlock(&__i2c_board_lock);
88+
89+
return status;
90+
}

‎drivers/i2c/i2c-core.c‎

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include <linux/completion.h>
3636
#include <asm/uaccess.h>
3737

38+
#include "i2c-core.h"
39+
3840

3941
static LIST_HEAD(adapters);
4042
static LIST_HEAD(drivers);
@@ -162,6 +164,11 @@ static void i2c_client_release(struct device *dev)
162164
complete(&client->released);
163165
}
164166

167+
static void i2c_client_dev_release(struct device *dev)
168+
{
169+
kfree(to_i2c_client(dev));
170+
}
171+
165172
static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
166173
{
167174
struct i2c_client *client = to_i2c_client(dev);
@@ -195,7 +202,60 @@ struct bus_type i2c_bus_type = {
195202
.resume = i2c_device_resume,
196203
};
197204

198-
static void i2c_unregister_device(struct i2c_client *client)
205+
/**
206+
* i2c_new_device - instantiate an i2c device for use with a new style driver
207+
* @adap: the adapter managing the device
208+
* @info: describes one I2C device; bus_num is ignored
209+
*
210+
* Create a device to work with a new style i2c driver, where binding is
211+
* handled through driver model probe()/remove() methods. This call is not
212+
* appropriate for use by mainboad initialization logic, which usually runs
213+
* during an arch_initcall() long before any i2c_adapter could exist.
214+
*
215+
* This returns the new i2c client, which may be saved for later use with
216+
* i2c_unregister_device(); or NULL to indicate an error.
217+
*/
218+
struct i2c_client *
219+
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
220+
{
221+
struct i2c_client *client;
222+
int status;
223+
224+
client = kzalloc(sizeof *client, GFP_KERNEL);
225+
if (!client)
226+
return NULL;
227+
228+
client->adapter = adap;
229+
230+
client->dev.platform_data = info->platform_data;
231+
client->flags = info->flags;
232+
client->addr = info->addr;
233+
client->irq = info->irq;
234+
235+
strlcpy(client->driver_name, info->driver_name,
236+
sizeof(client->driver_name));
237+
strlcpy(client->name, info->type, sizeof(client->name));
238+
239+
/* a new style driver may be bound to this device when we
240+
* return from this function, or any later moment (e.g. maybe
241+
* hotplugging will load the driver module). and the device
242+
* refcount model is the standard driver model one.
243+
*/
244+
status = i2c_attach_client(client);
245+
if (status < 0) {
246+
kfree(client);
247+
client = NULL;
248+
}
249+
return client;
250+
}
251+
EXPORT_SYMBOL_GPL(i2c_new_device);
252+
253+
254+
/**
255+
* i2c_unregister_device - reverse effect of i2c_new_device()
256+
* @client: value returned from i2c_new_device()
257+
*/
258+
void i2c_unregister_device(struct i2c_client *client)
199259
{
200260
struct i2c_adapter *adapter = client->adapter;
201261
struct i2c_driver *driver = client->driver;
@@ -213,6 +273,7 @@ static void i2c_unregister_device(struct i2c_client *client)
213273

214274
device_unregister(&client->dev);
215275
}
276+
EXPORT_SYMBOL_GPL(i2c_unregister_device);
216277

217278

218279
/* ------------------------------------------------------------------------- */
@@ -243,6 +304,22 @@ struct class i2c_adapter_class = {
243304
.dev_attrs = i2c_adapter_attrs,
244305
};
245306

307+
static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
308+
{
309+
struct i2c_devinfo *devinfo;
310+
311+
mutex_lock(&__i2c_board_lock);
312+
list_for_each_entry(devinfo, &__i2c_board_list, list) {
313+
if (devinfo->busnum == adapter->nr
314+
&& !i2c_new_device(adapter,
315+
&devinfo->board_info))
316+
printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
317+
i2c_adapter_id(adapter),
318+
devinfo->board_info.addr);
319+
}
320+
mutex_unlock(&__i2c_board_lock);
321+
}
322+
246323

247324
/* -----
248325
* i2c_add_adapter is called from within the algorithm layer,
@@ -311,7 +388,6 @@ int i2c_add_adapter(struct i2c_adapter *adap)
311388
goto out_unlock;
312389
}
313390

314-
315391
int i2c_del_adapter(struct i2c_adapter *adap)
316392
{
317393
struct list_head *item, *_n;
@@ -541,9 +617,15 @@ int i2c_attach_client(struct i2c_client *client)
541617
client->usage_count = 0;
542618

543619
client->dev.parent = &client->adapter->dev;
544-
client->dev.driver = &client->driver->driver;
545620
client->dev.bus = &i2c_bus_type;
546-
client->dev.release = &i2c_client_release;
621+
622+
if (client->driver)
623+
client->dev.driver = &client->driver->driver;
624+
625+
if (client->driver && !is_newstyle_driver(client->driver))
626+
client->dev.release = i2c_client_release;
627+
else
628+
client->dev.release = i2c_client_dev_release;
547629

548630
snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
549631
"%d-%04x", i2c_adapter_id(adapter), client->addr);

‎drivers/i2c/i2c-core.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* i2c-core.h - interfaces internal to the I2C framework
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17+
*/
18+
19+
struct i2c_devinfo {
20+
struct list_head list;
21+
int busnum;
22+
struct i2c_board_info board_info;
23+
};
24+
25+
/* board_lock protects board_list and first_dynamic_bus_num.
26+
* only i2c core components are allowed to use these symbols.
27+
*/
28+
extern struct mutex __i2c_board_lock;
29+
extern struct list_head __i2c_board_list;
30+
extern int __i2c_first_dynamic_bus_num;
31+

‎include/linux/i2c.h‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ struct i2c_client {
172172
int usage_count; /* How many accesses currently */
173173
/* to the client */
174174
struct device dev; /* the device structure */
175+
int irq; /* irq issued by device (or -1) */
175176
char driver_name[KOBJ_NAME_LEN];
176177
struct list_head list;
177178
struct completion released;
@@ -193,6 +194,67 @@ static inline void i2c_set_clientdata (struct i2c_client *dev, void *data)
193194
dev_set_drvdata (&dev->dev, data);
194195
}
195196

197+
/**
198+
* struct i2c_board_info - template for device creation
199+
* @driver_name: identifies the driver to be bound to the device
200+
* @type: optional chip type information, to initialize i2c_client.name
201+
* @flags: to initialize i2c_client.flags
202+
* @addr: stored in i2c_client.addr
203+
* @platform_data: stored in i2c_client.dev.platform_data
204+
* @irq: stored in i2c_client.irq
205+
206+
* I2C doesn't actually support hardware probing, although controllers and
207+
* devices may be able to use I2C_SMBUS_QUICK to tell whether or not there's
208+
* a device at a given address. Drivers commonly need more information than
209+
* that, such as chip type, configuration, associated IRQ, and so on.
210+
*
211+
* i2c_board_info is used to build tables of information listing I2C devices
212+
* that are present. This information is used to grow the driver model tree
213+
* for "new style" I2C drivers. For mainboards this is done statically using
214+
* i2c_register_board_info(), where @bus_num represents an adapter that isn't
215+
* yet available. For add-on boards, i2c_new_device() does this dynamically
216+
* with the adapter already known.
217+
*/
218+
struct i2c_board_info {
219+
char driver_name[KOBJ_NAME_LEN];
220+
char type[I2C_NAME_SIZE];
221+
unsigned short flags;
222+
unsigned short addr;
223+
void *platform_data;
224+
int irq;
225+
};
226+
227+
/**
228+
* I2C_BOARD_INFO - macro used to list an i2c device and its driver
229+
* @driver: identifies the driver to use with the device
230+
* @dev_addr: the device's address on the bus.
231+
*
232+
* This macro initializes essential fields of a struct i2c_board_info,
233+
* declaring what has been provided on a particular board. Optional
234+
* fields (such as the chip type, its associated irq, or device-specific
235+
* platform_data) are provided using conventional syntax.
236+
*/
237+
#define I2C_BOARD_INFO(driver,dev_addr) \
238+
.driver_name = (driver), .addr = (dev_addr)
239+
240+
241+
/* Add-on boards should register/unregister their devices; e.g. a board
242+
* with integrated I2C, a config eeprom, sensors, and a codec that's
243+
* used in conjunction with the primary hardware.
244+
*/
245+
extern struct i2c_client *
246+
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info);
247+
248+
extern void i2c_unregister_device(struct i2c_client *);
249+
250+
/* Mainboard arch_initcall() code should register all its I2C devices.
251+
* This is done at arch_initcall time, before declaring any i2c adapters.
252+
* Modules for add-on boards must use other calls.
253+
*/
254+
extern int
255+
i2c_register_board_info(int busnum, struct i2c_board_info const *info, unsigned n);
256+
257+
196258
/*
197259
* The following structs are for those who like to implement new bus drivers:
198260
* i2c_algorithm is the interface to a class of hardware solutions which can

0 commit comments

Comments
 (0)