[PARISC] Remove GCC_VERSION usage as suggested by Adrian Bunk
[linux-2.6.22.y-op.git] / drivers / i2c / busses / i2c-isa.c
blob8ed59a2dff532124afc6e899f1df3d6a4622f106
1 /*
2 i2c-isa.c - an i2c-core-like thing for ISA hardware monitoring chips
3 Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
5 Based on the i2c-isa pseudo-adapter from the lm_sensors project
6 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* This implements an i2c-core-like thing for ISA hardware monitoring
24 chips. Such chips are linked to the i2c subsystem for historical
25 reasons (because the early ISA hardware monitoring chips such as the
26 LM78 had both an I2C and an ISA interface). They used to be
27 registered with the main i2c-core, but as a first step in the
28 direction of a clean separation between I2C and ISA chip drivers,
29 we now have this separate core for ISA ones. It is significantly
30 more simple than the real one, of course, because we don't have to
31 handle multiple busses: there is only one (fake) ISA adapter.
32 It is worth noting that we still rely on i2c-core for some things
33 at the moment - but hopefully this won't last. */
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 #include <linux/i2c.h>
40 #include <linux/i2c-isa.h>
41 #include <linux/platform_device.h>
43 static u32 isa_func(struct i2c_adapter *adapter);
45 /* This is the actual algorithm we define */
46 static const struct i2c_algorithm isa_algorithm = {
47 .functionality = isa_func,
50 /* There can only be one... */
51 static struct i2c_adapter isa_adapter = {
52 .owner = THIS_MODULE,
53 .id = I2C_HW_ISA,
54 .class = I2C_CLASS_HWMON,
55 .algo = &isa_algorithm,
56 .name = "ISA main adapter",
59 /* We can't do a thing... */
60 static u32 isa_func(struct i2c_adapter *adapter)
62 return 0;
66 /* Copied from i2c-core */
67 static ssize_t show_adapter_name(struct device *dev,
68 struct device_attribute *attr, char *buf)
70 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
71 return sprintf(buf, "%s\n", adap->name);
73 static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
76 /* We implement an interface which resembles i2c_{add,del}_driver,
77 but for i2c-isa drivers. We don't have to remember and handle lists
78 of drivers and adapters so this is much more simple, of course. */
80 int i2c_isa_add_driver(struct i2c_driver *driver)
82 int res;
84 /* Add the driver to the list of i2c drivers in the driver core */
85 driver->driver.bus = &i2c_bus_type;
86 res = driver_register(&driver->driver);
87 if (res)
88 return res;
89 dev_dbg(&isa_adapter.dev, "Driver %s registered\n", driver->driver.name);
91 /* Now look for clients */
92 res = driver->attach_adapter(&isa_adapter);
93 if (res) {
94 dev_dbg(&isa_adapter.dev,
95 "Driver %s failed to attach adapter, unregistering\n",
96 driver->driver.name);
97 driver_unregister(&driver->driver);
99 return res;
102 int i2c_isa_del_driver(struct i2c_driver *driver)
104 struct list_head *item, *_n;
105 struct i2c_client *client;
106 int res;
108 /* Detach all clients belonging to this one driver */
109 list_for_each_safe(item, _n, &isa_adapter.clients) {
110 client = list_entry(item, struct i2c_client, list);
111 if (client->driver != driver)
112 continue;
113 dev_dbg(&isa_adapter.dev, "Detaching client %s at 0x%x\n",
114 client->name, client->addr);
115 if ((res = driver->detach_client(client))) {
116 dev_err(&isa_adapter.dev, "Failed, driver "
117 "%s not unregistered!\n",
118 driver->driver.name);
119 return res;
123 /* Get the driver off the core list */
124 driver_unregister(&driver->driver);
125 dev_dbg(&isa_adapter.dev, "Driver %s unregistered\n", driver->driver.name);
127 return 0;
131 static int __init i2c_isa_init(void)
133 int err;
135 mutex_init(&isa_adapter.clist_lock);
136 INIT_LIST_HEAD(&isa_adapter.clients);
138 isa_adapter.nr = ANY_I2C_ISA_BUS;
139 isa_adapter.dev.parent = &platform_bus;
140 sprintf(isa_adapter.dev.bus_id, "i2c-%d", isa_adapter.nr);
141 isa_adapter.dev.driver = &i2c_adapter_driver;
142 isa_adapter.dev.release = &i2c_adapter_dev_release;
143 err = device_register(&isa_adapter.dev);
144 if (err) {
145 printk(KERN_ERR "i2c-isa: Failed to register device\n");
146 goto exit;
148 err = device_create_file(&isa_adapter.dev, &dev_attr_name);
149 if (err) {
150 printk(KERN_ERR "i2c-isa: Failed to create name file\n");
151 goto exit_unregister;
154 /* Add this adapter to the i2c_adapter class */
155 memset(&isa_adapter.class_dev, 0x00, sizeof(struct class_device));
156 isa_adapter.class_dev.dev = &isa_adapter.dev;
157 isa_adapter.class_dev.class = &i2c_adapter_class;
158 strlcpy(isa_adapter.class_dev.class_id, isa_adapter.dev.bus_id,
159 BUS_ID_SIZE);
160 err = class_device_register(&isa_adapter.class_dev);
161 if (err) {
162 printk(KERN_ERR "i2c-isa: Failed to register class device\n");
163 goto exit_remove_name;
166 dev_dbg(&isa_adapter.dev, "%s registered\n", isa_adapter.name);
168 return 0;
170 exit_remove_name:
171 device_remove_file(&isa_adapter.dev, &dev_attr_name);
172 exit_unregister:
173 init_completion(&isa_adapter.dev_released); /* Needed? */
174 device_unregister(&isa_adapter.dev);
175 wait_for_completion(&isa_adapter.dev_released);
176 exit:
177 return err;
180 static void __exit i2c_isa_exit(void)
182 #ifdef DEBUG
183 struct list_head *item, *_n;
184 struct i2c_client *client = NULL;
185 #endif
187 /* There should be no more active client */
188 #ifdef DEBUG
189 dev_dbg(&isa_adapter.dev, "Looking for clients\n");
190 list_for_each_safe(item, _n, &isa_adapter.clients) {
191 client = list_entry(item, struct i2c_client, list);
192 dev_err(&isa_adapter.dev, "Driver %s still has an active "
193 "ISA client at 0x%x\n", client->driver->driver.name,
194 client->addr);
196 if (client != NULL)
197 return;
198 #endif
200 /* Clean up the sysfs representation */
201 dev_dbg(&isa_adapter.dev, "Unregistering from sysfs\n");
202 init_completion(&isa_adapter.dev_released);
203 init_completion(&isa_adapter.class_dev_released);
204 class_device_unregister(&isa_adapter.class_dev);
205 device_remove_file(&isa_adapter.dev, &dev_attr_name);
206 device_unregister(&isa_adapter.dev);
208 /* Wait for sysfs to drop all references */
209 dev_dbg(&isa_adapter.dev, "Waiting for sysfs completion\n");
210 wait_for_completion(&isa_adapter.dev_released);
211 wait_for_completion(&isa_adapter.class_dev_released);
213 dev_dbg(&isa_adapter.dev, "%s unregistered\n", isa_adapter.name);
216 EXPORT_SYMBOL(i2c_isa_add_driver);
217 EXPORT_SYMBOL(i2c_isa_del_driver);
219 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
220 MODULE_DESCRIPTION("ISA bus access through i2c");
221 MODULE_LICENSE("GPL");
223 module_init(i2c_isa_init);
224 module_exit(i2c_isa_exit);