pcf8575.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. pcf8575.c
  3. About the PCF8575 chip: the PCF8575 is a 16-bit I/O expander for the I2C bus
  4. produced by a.o. Philips Semiconductors.
  5. Copyright (C) 2006 Michael Hennerich, Analog Devices Inc.
  6. <hennerich@blackfin.uclinux.org>
  7. Based on pcf8574.c.
  8. Copyright (c) 2007 Bart Van Assche <bart.vanassche@gmail.com>.
  9. Ported this driver from ucLinux to the mainstream Linux kernel.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/i2c.h>
  25. #include <linux/slab.h> /* kzalloc() */
  26. #include <linux/sysfs.h> /* sysfs_create_group() */
  27. /* Addresses to scan */
  28. static const unsigned short normal_i2c[] = {
  29. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  30. I2C_CLIENT_END
  31. };
  32. /* Insmod parameters */
  33. I2C_CLIENT_INSMOD;
  34. /* Each client has this additional data */
  35. struct pcf8575_data {
  36. struct i2c_client client;
  37. int write; /* last written value, or error code */
  38. };
  39. static int pcf8575_attach_adapter(struct i2c_adapter *adapter);
  40. static int pcf8575_detect(struct i2c_adapter *adapter, int address, int kind);
  41. static int pcf8575_detach_client(struct i2c_client *client);
  42. /* This is the driver that will be inserted */
  43. static struct i2c_driver pcf8575_driver = {
  44. .driver = {
  45. .owner = THIS_MODULE,
  46. .name = "pcf8575",
  47. },
  48. .attach_adapter = pcf8575_attach_adapter,
  49. .detach_client = pcf8575_detach_client,
  50. };
  51. /* following are the sysfs callback functions */
  52. static ssize_t show_read(struct device *dev, struct device_attribute *attr,
  53. char *buf)
  54. {
  55. struct i2c_client *client = to_i2c_client(dev);
  56. u16 val;
  57. u8 iopin_state[2];
  58. i2c_master_recv(client, iopin_state, 2);
  59. val = iopin_state[0];
  60. val |= iopin_state[1] << 8;
  61. return sprintf(buf, "%u\n", val);
  62. }
  63. static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
  64. static ssize_t show_write(struct device *dev, struct device_attribute *attr,
  65. char *buf)
  66. {
  67. struct pcf8575_data *data = dev_get_drvdata(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,
  73. const char *buf, size_t count)
  74. {
  75. struct i2c_client *client = to_i2c_client(dev);
  76. struct pcf8575_data *data = i2c_get_clientdata(client);
  77. unsigned long val = simple_strtoul(buf, NULL, 10);
  78. u8 iopin_state[2];
  79. if (val > 0xffff)
  80. return -EINVAL;
  81. data->write = val;
  82. iopin_state[0] = val & 0xFF;
  83. iopin_state[1] = val >> 8;
  84. i2c_master_send(client, iopin_state, 2);
  85. return count;
  86. }
  87. static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
  88. static struct attribute *pcf8575_attributes[] = {
  89. &dev_attr_read.attr,
  90. &dev_attr_write.attr,
  91. NULL
  92. };
  93. static const struct attribute_group pcf8575_attr_group = {
  94. .attrs = pcf8575_attributes,
  95. };
  96. /*
  97. * Real code
  98. */
  99. static int pcf8575_attach_adapter(struct i2c_adapter *adapter)
  100. {
  101. return i2c_probe(adapter, &addr_data, pcf8575_detect);
  102. }
  103. /* This function is called by i2c_probe */
  104. static int pcf8575_detect(struct i2c_adapter *adapter, int address, int kind)
  105. {
  106. struct i2c_client *client;
  107. struct pcf8575_data *data;
  108. int err = 0;
  109. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  110. goto exit;
  111. /* OK. For now, we presume we have a valid client. We now create the
  112. client structure, even though we cannot fill it completely yet. */
  113. data = kzalloc(sizeof(struct pcf8575_data), GFP_KERNEL);
  114. if (!data) {
  115. err = -ENOMEM;
  116. goto exit;
  117. }
  118. client = &data->client;
  119. i2c_set_clientdata(client, data);
  120. client->addr = address;
  121. client->adapter = adapter;
  122. client->driver = &pcf8575_driver;
  123. strlcpy(client->name, "pcf8575", I2C_NAME_SIZE);
  124. data->write = -EAGAIN;
  125. /* This is the place to detect whether the chip at the specified
  126. address really is a PCF8575 chip. However, there is no method known
  127. to detect whether an I2C chip is a PCF8575 or any other I2C chip. */
  128. /* Tell the I2C layer a new client has arrived */
  129. err = i2c_attach_client(client);
  130. if (err)
  131. goto exit_free;
  132. /* Register sysfs hooks */
  133. err = sysfs_create_group(&client->dev.kobj, &pcf8575_attr_group);
  134. if (err)
  135. goto exit_detach;
  136. return 0;
  137. exit_detach:
  138. i2c_detach_client(client);
  139. exit_free:
  140. kfree(data);
  141. exit:
  142. return err;
  143. }
  144. static int pcf8575_detach_client(struct i2c_client *client)
  145. {
  146. int err;
  147. sysfs_remove_group(&client->dev.kobj, &pcf8575_attr_group);
  148. err = i2c_detach_client(client);
  149. if (err)
  150. return err;
  151. kfree(i2c_get_clientdata(client));
  152. return 0;
  153. }
  154. static int __init pcf8575_init(void)
  155. {
  156. return i2c_add_driver(&pcf8575_driver);
  157. }
  158. static void __exit pcf8575_exit(void)
  159. {
  160. i2c_del_driver(&pcf8575_driver);
  161. }
  162. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>, "
  163. "Bart Van Assche <bart.vanassche@gmail.com>");
  164. MODULE_DESCRIPTION("pcf8575 driver");
  165. MODULE_LICENSE("GPL");
  166. module_init(pcf8575_init);
  167. module_exit(pcf8575_exit);