pcf8574.c 6.2 KB

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