pcf8574.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #include <linux/i2c-sensor.h>
  34. /* Addresses to scan */
  35. static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  36. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  37. I2C_CLIENT_END };
  38. static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
  39. /* Insmod parameters */
  40. SENSORS_INSMOD_2(pcf8574, pcf8574a);
  41. /* Initial values */
  42. #define PCF8574_INIT 255 /* All outputs on (input mode) */
  43. /* Each client has this additional data */
  44. struct pcf8574_data {
  45. struct i2c_client client;
  46. u8 write; /* Remember last written value */
  47. };
  48. static int pcf8574_attach_adapter(struct i2c_adapter *adapter);
  49. static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind);
  50. static int pcf8574_detach_client(struct i2c_client *client);
  51. static void pcf8574_init_client(struct i2c_client *client);
  52. /* This is the driver that will be inserted */
  53. static struct i2c_driver pcf8574_driver = {
  54. .owner = THIS_MODULE,
  55. .name = "pcf8574",
  56. .id = I2C_DRIVERID_PCF8574,
  57. .flags = I2C_DF_NOTIFY,
  58. .attach_adapter = pcf8574_attach_adapter,
  59. .detach_client = pcf8574_detach_client,
  60. };
  61. /* following are the sysfs callback functions */
  62. static ssize_t show_read(struct device *dev, struct device_attribute *attr, char *buf)
  63. {
  64. struct i2c_client *client = to_i2c_client(dev);
  65. return sprintf(buf, "%u\n", i2c_smbus_read_byte(client));
  66. }
  67. static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
  68. static ssize_t show_write(struct device *dev, struct device_attribute *attr, char *buf)
  69. {
  70. struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev));
  71. return sprintf(buf, "%u\n", data->write);
  72. }
  73. static ssize_t set_write(struct device *dev, struct device_attribute *attr, const char *buf,
  74. size_t count)
  75. {
  76. struct i2c_client *client = to_i2c_client(dev);
  77. struct pcf8574_data *data = i2c_get_clientdata(client);
  78. unsigned long val = simple_strtoul(buf, NULL, 10);
  79. if (val > 0xff)
  80. return -EINVAL;
  81. data->write = val;
  82. i2c_smbus_write_byte(client, data->write);
  83. return count;
  84. }
  85. static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
  86. /*
  87. * Real code
  88. */
  89. static int pcf8574_attach_adapter(struct i2c_adapter *adapter)
  90. {
  91. return i2c_detect(adapter, &addr_data, pcf8574_detect);
  92. }
  93. /* This function is called by i2c_detect */
  94. int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind)
  95. {
  96. struct i2c_client *new_client;
  97. struct pcf8574_data *data;
  98. int err = 0;
  99. const char *client_name = "";
  100. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  101. goto exit;
  102. /* OK. For now, we presume we have a valid client. We now create the
  103. client structure, even though we cannot fill it completely yet. */
  104. if (!(data = kmalloc(sizeof(struct pcf8574_data), GFP_KERNEL))) {
  105. err = -ENOMEM;
  106. goto exit;
  107. }
  108. memset(data, 0, sizeof(struct pcf8574_data));
  109. new_client = &data->client;
  110. i2c_set_clientdata(new_client, data);
  111. new_client->addr = address;
  112. new_client->adapter = adapter;
  113. new_client->driver = &pcf8574_driver;
  114. new_client->flags = 0;
  115. /* Now, we would do the remaining detection. But the PCF8574 is plainly
  116. impossible to detect! Stupid chip. */
  117. /* Determine the chip type */
  118. if (kind <= 0) {
  119. if (address >= 0x38 && address <= 0x3f)
  120. kind = pcf8574a;
  121. else
  122. kind = pcf8574;
  123. }
  124. if (kind == pcf8574a)
  125. client_name = "pcf8574a";
  126. else
  127. client_name = "pcf8574";
  128. /* Fill in the remaining client fields and put it into the global list */
  129. strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
  130. /* Tell the I2C layer a new client has arrived */
  131. if ((err = i2c_attach_client(new_client)))
  132. goto exit_free;
  133. /* Initialize the PCF8574 chip */
  134. pcf8574_init_client(new_client);
  135. /* Register sysfs hooks */
  136. device_create_file(&new_client->dev, &dev_attr_read);
  137. device_create_file(&new_client->dev, &dev_attr_write);
  138. return 0;
  139. /* OK, this is not exactly good programming practice, usually. But it is
  140. very code-efficient in this case. */
  141. exit_free:
  142. kfree(data);
  143. exit:
  144. return err;
  145. }
  146. static int pcf8574_detach_client(struct i2c_client *client)
  147. {
  148. int err;
  149. if ((err = i2c_detach_client(client))) {
  150. dev_err(&client->dev,
  151. "Client deregistration failed, client not detached.\n");
  152. return err;
  153. }
  154. kfree(i2c_get_clientdata(client));
  155. return 0;
  156. }
  157. /* Called when we have found a new PCF8574. */
  158. static void pcf8574_init_client(struct i2c_client *client)
  159. {
  160. struct pcf8574_data *data = i2c_get_clientdata(client);
  161. data->write = PCF8574_INIT;
  162. i2c_smbus_write_byte(client, data->write);
  163. }
  164. static int __init pcf8574_init(void)
  165. {
  166. return i2c_add_driver(&pcf8574_driver);
  167. }
  168. static void __exit pcf8574_exit(void)
  169. {
  170. i2c_del_driver(&pcf8574_driver);
  171. }
  172. MODULE_AUTHOR
  173. ("Frodo Looijaard <frodol@dds.nl>, "
  174. "Philip Edelbrock <phil@netroedge.com>, "
  175. "Dan Eaton <dan.eaton@rocketlogix.com> "
  176. "and Aurelien Jarno <aurelien@aurel32.net>");
  177. MODULE_DESCRIPTION("PCF8574 driver");
  178. MODULE_LICENSE("GPL");
  179. module_init(pcf8574_init);
  180. module_exit(pcf8574_exit);