pcf8574.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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: none, device can't be detected */
  32. static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
  33. /* Insmod parameters */
  34. I2C_CLIENT_INSMOD_2(pcf8574, pcf8574a);
  35. /* Each client has this additional data */
  36. struct pcf8574_data {
  37. int write; /* Remember last written value */
  38. };
  39. static void pcf8574_init_client(struct i2c_client *client);
  40. /* following are the sysfs callback functions */
  41. static ssize_t show_read(struct device *dev, struct device_attribute *attr, char *buf)
  42. {
  43. struct i2c_client *client = to_i2c_client(dev);
  44. return sprintf(buf, "%u\n", i2c_smbus_read_byte(client));
  45. }
  46. static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
  47. static ssize_t show_write(struct device *dev, struct device_attribute *attr, char *buf)
  48. {
  49. struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev));
  50. if (data->write < 0)
  51. return data->write;
  52. return sprintf(buf, "%d\n", data->write);
  53. }
  54. static ssize_t set_write(struct device *dev, struct device_attribute *attr, const char *buf,
  55. size_t count)
  56. {
  57. struct i2c_client *client = to_i2c_client(dev);
  58. struct pcf8574_data *data = i2c_get_clientdata(client);
  59. unsigned long val = simple_strtoul(buf, NULL, 10);
  60. if (val > 0xff)
  61. return -EINVAL;
  62. data->write = val;
  63. i2c_smbus_write_byte(client, data->write);
  64. return count;
  65. }
  66. static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
  67. static struct attribute *pcf8574_attributes[] = {
  68. &dev_attr_read.attr,
  69. &dev_attr_write.attr,
  70. NULL
  71. };
  72. static const struct attribute_group pcf8574_attr_group = {
  73. .attrs = pcf8574_attributes,
  74. };
  75. /*
  76. * Real code
  77. */
  78. /* Return 0 if detection is successful, -ENODEV otherwise */
  79. static int pcf8574_detect(struct i2c_client *client, int kind,
  80. struct i2c_board_info *info)
  81. {
  82. struct i2c_adapter *adapter = client->adapter;
  83. const char *client_name;
  84. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  85. return -ENODEV;
  86. /* Now, we would do the remaining detection. But the PCF8574 is plainly
  87. impossible to detect! Stupid chip. */
  88. /* Determine the chip type */
  89. if (kind <= 0) {
  90. if (client->addr >= 0x38 && client->addr <= 0x3f)
  91. kind = pcf8574a;
  92. else
  93. kind = pcf8574;
  94. }
  95. if (kind == pcf8574a)
  96. client_name = "pcf8574a";
  97. else
  98. client_name = "pcf8574";
  99. strlcpy(info->type, client_name, I2C_NAME_SIZE);
  100. return 0;
  101. }
  102. static int pcf8574_probe(struct i2c_client *client,
  103. const struct i2c_device_id *id)
  104. {
  105. struct pcf8574_data *data;
  106. int err;
  107. data = kzalloc(sizeof(struct pcf8574_data), GFP_KERNEL);
  108. if (!data) {
  109. err = -ENOMEM;
  110. goto exit;
  111. }
  112. i2c_set_clientdata(client, data);
  113. /* Initialize the PCF8574 chip */
  114. pcf8574_init_client(client);
  115. /* Register sysfs hooks */
  116. err = sysfs_create_group(&client->dev.kobj, &pcf8574_attr_group);
  117. if (err)
  118. goto exit_free;
  119. return 0;
  120. exit_free:
  121. kfree(data);
  122. exit:
  123. return err;
  124. }
  125. static int pcf8574_remove(struct i2c_client *client)
  126. {
  127. sysfs_remove_group(&client->dev.kobj, &pcf8574_attr_group);
  128. kfree(i2c_get_clientdata(client));
  129. return 0;
  130. }
  131. /* Called when we have found a new PCF8574. */
  132. static void pcf8574_init_client(struct i2c_client *client)
  133. {
  134. struct pcf8574_data *data = i2c_get_clientdata(client);
  135. data->write = -EAGAIN;
  136. }
  137. static const struct i2c_device_id pcf8574_id[] = {
  138. { "pcf8574", 0 },
  139. { "pcf8574a", 0 },
  140. { }
  141. };
  142. static struct i2c_driver pcf8574_driver = {
  143. .driver = {
  144. .name = "pcf8574",
  145. },
  146. .probe = pcf8574_probe,
  147. .remove = pcf8574_remove,
  148. .id_table = pcf8574_id,
  149. .detect = pcf8574_detect,
  150. .address_data = &addr_data,
  151. };
  152. static int __init pcf8574_init(void)
  153. {
  154. return i2c_add_driver(&pcf8574_driver);
  155. }
  156. static void __exit pcf8574_exit(void)
  157. {
  158. i2c_del_driver(&pcf8574_driver);
  159. }
  160. MODULE_AUTHOR
  161. ("Frodo Looijaard <frodol@dds.nl>, "
  162. "Philip Edelbrock <phil@netroedge.com>, "
  163. "Dan Eaton <dan.eaton@rocketlogix.com> "
  164. "and Aurelien Jarno <aurelien@aurel32.net>");
  165. MODULE_DESCRIPTION("PCF8574 driver");
  166. MODULE_LICENSE("GPL");
  167. module_init(pcf8574_init);
  168. module_exit(pcf8574_exit);