pcf8574.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl>,
  3. Philip Edelbrock <phil@netroedge.com>,
  4. Dan Eaton <dan.eaton@rocketlogix.com>
  5. Ported to Linux 2.6 by Aurelien Jarno <aurel32@debian.org> with
  6. the help of Jean Delvare <khali@linux-fr.org>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /* A few notes about the PCF8574:
  20. * The PCF8574 is an 8-bit I/O expander for the I2C bus produced by
  21. Philips Semiconductors. It is designed to provide a byte I2C
  22. interface to up to 8 separate devices.
  23. * The PCF8574 appears as a very simple SMBus device which can be
  24. read from or written to with SMBUS byte read/write accesses.
  25. --Dan
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/i2c.h>
  31. /* Addresses to scan */
  32. static const unsigned short normal_i2c[] = {
  33. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  34. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  35. I2C_CLIENT_END
  36. };
  37. /* Insmod parameters */
  38. I2C_CLIENT_INSMOD_2(pcf8574, pcf8574a);
  39. /* Each client has this additional data */
  40. struct pcf8574_data {
  41. struct i2c_client client;
  42. int write; /* Remember last written value */
  43. };
  44. static int pcf8574_attach_adapter(struct i2c_adapter *adapter);
  45. static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind);
  46. static int pcf8574_detach_client(struct i2c_client *client);
  47. static void pcf8574_init_client(struct i2c_client *client);
  48. /* This is the driver that will be inserted */
  49. static struct i2c_driver pcf8574_driver = {
  50. .driver = {
  51. .name = "pcf8574",
  52. },
  53. .attach_adapter = pcf8574_attach_adapter,
  54. .detach_client = pcf8574_detach_client,
  55. };
  56. /* following are the sysfs callback functions */
  57. static ssize_t show_read(struct device *dev, struct device_attribute *attr, char *buf)
  58. {
  59. struct i2c_client *client = to_i2c_client(dev);
  60. return sprintf(buf, "%u\n", i2c_smbus_read_byte(client));
  61. }
  62. static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
  63. static ssize_t show_write(struct device *dev, struct device_attribute *attr, char *buf)
  64. {
  65. struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev));
  66. if (data->write < 0)
  67. return data->write;
  68. return sprintf(buf, "%d\n", data->write);
  69. }
  70. static ssize_t set_write(struct device *dev, struct device_attribute *attr, const char *buf,
  71. size_t count)
  72. {
  73. struct i2c_client *client = to_i2c_client(dev);
  74. struct pcf8574_data *data = i2c_get_clientdata(client);
  75. unsigned long val = simple_strtoul(buf, NULL, 10);
  76. if (val > 0xff)
  77. return -EINVAL;
  78. data->write = val;
  79. i2c_smbus_write_byte(client, data->write);
  80. return count;
  81. }
  82. static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
  83. static struct attribute *pcf8574_attributes[] = {
  84. &dev_attr_read.attr,
  85. &dev_attr_write.attr,
  86. NULL
  87. };
  88. static const struct attribute_group pcf8574_attr_group = {
  89. .attrs = pcf8574_attributes,
  90. };
  91. /*
  92. * Real code
  93. */
  94. static int pcf8574_attach_adapter(struct i2c_adapter *adapter)
  95. {
  96. return i2c_probe(adapter, &addr_data, pcf8574_detect);
  97. }
  98. /* This function is called by i2c_probe */
  99. static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind)
  100. {
  101. struct i2c_client *client;
  102. struct pcf8574_data *data;
  103. int err = 0;
  104. const char *client_name = "";
  105. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  106. goto exit;
  107. /* OK. For now, we presume we have a valid client. We now create the
  108. client structure, even though we cannot fill it completely yet. */
  109. if (!(data = kzalloc(sizeof(struct pcf8574_data), GFP_KERNEL))) {
  110. err = -ENOMEM;
  111. goto exit;
  112. }
  113. client = &data->client;
  114. i2c_set_clientdata(client, data);
  115. client->addr = address;
  116. client->adapter = adapter;
  117. client->driver = &pcf8574_driver;
  118. /* Now, we would do the remaining detection. But the PCF8574 is plainly
  119. impossible to detect! Stupid chip. */
  120. /* Determine the chip type */
  121. if (kind <= 0) {
  122. if (address >= 0x38 && address <= 0x3f)
  123. kind = pcf8574a;
  124. else
  125. kind = pcf8574;
  126. }
  127. if (kind == pcf8574a)
  128. client_name = "pcf8574a";
  129. else
  130. client_name = "pcf8574";
  131. /* Fill in the remaining client fields and put it into the global list */
  132. strlcpy(client->name, client_name, I2C_NAME_SIZE);
  133. /* Tell the I2C layer a new client has arrived */
  134. if ((err = i2c_attach_client(client)))
  135. goto exit_free;
  136. /* Initialize the PCF8574 chip */
  137. pcf8574_init_client(client);
  138. /* Register sysfs hooks */
  139. err = sysfs_create_group(&client->dev.kobj, &pcf8574_attr_group);
  140. if (err)
  141. goto exit_detach;
  142. return 0;
  143. exit_detach:
  144. i2c_detach_client(client);
  145. exit_free:
  146. kfree(data);
  147. exit:
  148. return err;
  149. }
  150. static int pcf8574_detach_client(struct i2c_client *client)
  151. {
  152. int err;
  153. sysfs_remove_group(&client->dev.kobj, &pcf8574_attr_group);
  154. if ((err = i2c_detach_client(client)))
  155. return err;
  156. kfree(i2c_get_clientdata(client));
  157. return 0;
  158. }
  159. /* Called when we have found a new PCF8574. */
  160. static void pcf8574_init_client(struct i2c_client *client)
  161. {
  162. struct pcf8574_data *data = i2c_get_clientdata(client);
  163. data->write = -EAGAIN;
  164. }
  165. static int __init pcf8574_init(void)
  166. {
  167. return i2c_add_driver(&pcf8574_driver);
  168. }
  169. static void __exit pcf8574_exit(void)
  170. {
  171. i2c_del_driver(&pcf8574_driver);
  172. }
  173. MODULE_AUTHOR
  174. ("Frodo Looijaard <frodol@dds.nl>, "
  175. "Philip Edelbrock <phil@netroedge.com>, "
  176. "Dan Eaton <dan.eaton@rocketlogix.com> "
  177. "and Aurelien Jarno <aurelien@aurel32.net>");
  178. MODULE_DESCRIPTION("PCF8574 driver");
  179. MODULE_LICENSE("GPL");
  180. module_init(pcf8574_init);
  181. module_exit(pcf8574_exit);