i2c-isa.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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>
  4. Based on the i2c-isa pseudo-adapter from the lm_sensors project
  5. Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  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. /* This implements an i2c-core-like thing for ISA hardware monitoring
  19. chips. Such chips are linked to the i2c subsystem for historical
  20. reasons (because the early ISA hardware monitoring chips such as the
  21. LM78 had both an I2C and an ISA interface). They used to be
  22. registered with the main i2c-core, but as a first step in the
  23. direction of a clean separation between I2C and ISA chip drivers,
  24. we now have this separate core for ISA ones. It is significantly
  25. more simple than the real one, of course, because we don't have to
  26. handle multiple busses: there is only one (fake) ISA adapter.
  27. It is worth noting that we still rely on i2c-core for some things
  28. at the moment - but hopefully this won't last. */
  29. #include <linux/init.h>
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/errno.h>
  33. #include <linux/i2c.h>
  34. #include <linux/i2c-isa.h>
  35. #include <linux/platform_device.h>
  36. static u32 isa_func(struct i2c_adapter *adapter);
  37. /* This is the actual algorithm we define */
  38. static struct i2c_algorithm isa_algorithm = {
  39. .functionality = isa_func,
  40. };
  41. /* There can only be one... */
  42. static struct i2c_adapter isa_adapter = {
  43. .owner = THIS_MODULE,
  44. .id = I2C_HW_ISA,
  45. .class = I2C_CLASS_HWMON,
  46. .algo = &isa_algorithm,
  47. .name = "ISA main adapter",
  48. };
  49. /* We can't do a thing... */
  50. static u32 isa_func(struct i2c_adapter *adapter)
  51. {
  52. return 0;
  53. }
  54. /* Copied from i2c-core */
  55. static ssize_t show_adapter_name(struct device *dev,
  56. struct device_attribute *attr, char *buf)
  57. {
  58. struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
  59. return sprintf(buf, "%s\n", adap->name);
  60. }
  61. static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
  62. /* We implement an interface which resembles i2c_{add,del}_driver,
  63. but for i2c-isa drivers. We don't have to remember and handle lists
  64. of drivers and adapters so this is much more simple, of course. */
  65. int i2c_isa_add_driver(struct i2c_driver *driver)
  66. {
  67. int res;
  68. /* Add the driver to the list of i2c drivers in the driver core */
  69. driver->driver.bus = &i2c_bus_type;
  70. res = driver_register(&driver->driver);
  71. if (res)
  72. return res;
  73. dev_dbg(&isa_adapter.dev, "Driver %s registered\n", driver->driver.name);
  74. /* Now look for clients */
  75. driver->attach_adapter(&isa_adapter);
  76. return 0;
  77. }
  78. int i2c_isa_del_driver(struct i2c_driver *driver)
  79. {
  80. struct list_head *item, *_n;
  81. struct i2c_client *client;
  82. int res;
  83. /* Detach all clients belonging to this one driver */
  84. list_for_each_safe(item, _n, &isa_adapter.clients) {
  85. client = list_entry(item, struct i2c_client, list);
  86. if (client->driver != driver)
  87. continue;
  88. dev_dbg(&isa_adapter.dev, "Detaching client %s at 0x%x\n",
  89. client->name, client->addr);
  90. if ((res = driver->detach_client(client))) {
  91. dev_err(&isa_adapter.dev, "Failed, driver "
  92. "%s not unregistered!\n",
  93. driver->driver.name);
  94. return res;
  95. }
  96. }
  97. /* Get the driver off the core list */
  98. driver_unregister(&driver->driver);
  99. dev_dbg(&isa_adapter.dev, "Driver %s unregistered\n", driver->driver.name);
  100. return 0;
  101. }
  102. static int __init i2c_isa_init(void)
  103. {
  104. mutex_init(&isa_adapter.clist_lock);
  105. INIT_LIST_HEAD(&isa_adapter.clients);
  106. isa_adapter.nr = ANY_I2C_ISA_BUS;
  107. isa_adapter.dev.parent = &platform_bus;
  108. sprintf(isa_adapter.dev.bus_id, "i2c-%d", isa_adapter.nr);
  109. isa_adapter.dev.driver = &i2c_adapter_driver;
  110. isa_adapter.dev.release = &i2c_adapter_dev_release;
  111. device_register(&isa_adapter.dev);
  112. device_create_file(&isa_adapter.dev, &dev_attr_name);
  113. /* Add this adapter to the i2c_adapter class */
  114. memset(&isa_adapter.class_dev, 0x00, sizeof(struct class_device));
  115. isa_adapter.class_dev.dev = &isa_adapter.dev;
  116. isa_adapter.class_dev.class = &i2c_adapter_class;
  117. strlcpy(isa_adapter.class_dev.class_id, isa_adapter.dev.bus_id,
  118. BUS_ID_SIZE);
  119. class_device_register(&isa_adapter.class_dev);
  120. dev_dbg(&isa_adapter.dev, "%s registered\n", isa_adapter.name);
  121. return 0;
  122. }
  123. static void __exit i2c_isa_exit(void)
  124. {
  125. #ifdef DEBUG
  126. struct list_head *item, *_n;
  127. struct i2c_client *client = NULL;
  128. #endif
  129. /* There should be no more active client */
  130. #ifdef DEBUG
  131. dev_dbg(&isa_adapter.dev, "Looking for clients\n");
  132. list_for_each_safe(item, _n, &isa_adapter.clients) {
  133. client = list_entry(item, struct i2c_client, list);
  134. dev_err(&isa_adapter.dev, "Driver %s still has an active "
  135. "ISA client at 0x%x\n", client->driver->driver.name,
  136. client->addr);
  137. }
  138. if (client != NULL)
  139. return;
  140. #endif
  141. /* Clean up the sysfs representation */
  142. dev_dbg(&isa_adapter.dev, "Unregistering from sysfs\n");
  143. init_completion(&isa_adapter.dev_released);
  144. init_completion(&isa_adapter.class_dev_released);
  145. class_device_unregister(&isa_adapter.class_dev);
  146. device_remove_file(&isa_adapter.dev, &dev_attr_name);
  147. device_unregister(&isa_adapter.dev);
  148. /* Wait for sysfs to drop all references */
  149. dev_dbg(&isa_adapter.dev, "Waiting for sysfs completion\n");
  150. wait_for_completion(&isa_adapter.dev_released);
  151. wait_for_completion(&isa_adapter.class_dev_released);
  152. dev_dbg(&isa_adapter.dev, "%s unregistered\n", isa_adapter.name);
  153. }
  154. EXPORT_SYMBOL(i2c_isa_add_driver);
  155. EXPORT_SYMBOL(i2c_isa_del_driver);
  156. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  157. MODULE_DESCRIPTION("ISA bus access through i2c");
  158. MODULE_LICENSE("GPL");
  159. module_init(i2c_isa_init);
  160. module_exit(i2c_isa_exit);