pcf8575.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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: none, device can't be detected */
  28. static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
  29. /* Insmod parameters */
  30. I2C_CLIENT_INSMOD;
  31. /* Each client has this additional data */
  32. struct pcf8575_data {
  33. int write; /* last written value, or error code */
  34. };
  35. /* following are the sysfs callback functions */
  36. static ssize_t show_read(struct device *dev, struct device_attribute *attr,
  37. char *buf)
  38. {
  39. struct i2c_client *client = to_i2c_client(dev);
  40. u16 val;
  41. u8 iopin_state[2];
  42. i2c_master_recv(client, iopin_state, 2);
  43. val = iopin_state[0];
  44. val |= iopin_state[1] << 8;
  45. return sprintf(buf, "%u\n", val);
  46. }
  47. static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
  48. static ssize_t show_write(struct device *dev, struct device_attribute *attr,
  49. char *buf)
  50. {
  51. struct pcf8575_data *data = dev_get_drvdata(dev);
  52. if (data->write < 0)
  53. return data->write;
  54. return sprintf(buf, "%d\n", data->write);
  55. }
  56. static ssize_t set_write(struct device *dev, struct device_attribute *attr,
  57. const char *buf, size_t count)
  58. {
  59. struct i2c_client *client = to_i2c_client(dev);
  60. struct pcf8575_data *data = i2c_get_clientdata(client);
  61. unsigned long val = simple_strtoul(buf, NULL, 10);
  62. u8 iopin_state[2];
  63. if (val > 0xffff)
  64. return -EINVAL;
  65. data->write = val;
  66. iopin_state[0] = val & 0xFF;
  67. iopin_state[1] = val >> 8;
  68. i2c_master_send(client, iopin_state, 2);
  69. return count;
  70. }
  71. static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
  72. static struct attribute *pcf8575_attributes[] = {
  73. &dev_attr_read.attr,
  74. &dev_attr_write.attr,
  75. NULL
  76. };
  77. static const struct attribute_group pcf8575_attr_group = {
  78. .attrs = pcf8575_attributes,
  79. };
  80. /*
  81. * Real code
  82. */
  83. /* Return 0 if detection is successful, -ENODEV otherwise */
  84. static int pcf8575_detect(struct i2c_client *client, int kind,
  85. struct i2c_board_info *info)
  86. {
  87. struct i2c_adapter *adapter = client->adapter;
  88. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  89. return -ENODEV;
  90. /* This is the place to detect whether the chip at the specified
  91. address really is a PCF8575 chip. However, there is no method known
  92. to detect whether an I2C chip is a PCF8575 or any other I2C chip. */
  93. strlcpy(info->type, "pcf8575", I2C_NAME_SIZE);
  94. return 0;
  95. }
  96. static int pcf8575_probe(struct i2c_client *client,
  97. const struct i2c_device_id *id)
  98. {
  99. struct pcf8575_data *data;
  100. int err;
  101. data = kzalloc(sizeof(struct pcf8575_data), GFP_KERNEL);
  102. if (!data) {
  103. err = -ENOMEM;
  104. goto exit;
  105. }
  106. i2c_set_clientdata(client, data);
  107. data->write = -EAGAIN;
  108. /* Register sysfs hooks */
  109. err = sysfs_create_group(&client->dev.kobj, &pcf8575_attr_group);
  110. if (err)
  111. goto exit_free;
  112. return 0;
  113. exit_free:
  114. kfree(data);
  115. exit:
  116. return err;
  117. }
  118. static int pcf8575_remove(struct i2c_client *client)
  119. {
  120. sysfs_remove_group(&client->dev.kobj, &pcf8575_attr_group);
  121. kfree(i2c_get_clientdata(client));
  122. return 0;
  123. }
  124. static const struct i2c_device_id pcf8575_id[] = {
  125. { "pcf8575", 0 },
  126. { }
  127. };
  128. static struct i2c_driver pcf8575_driver = {
  129. .driver = {
  130. .owner = THIS_MODULE,
  131. .name = "pcf8575",
  132. },
  133. .probe = pcf8575_probe,
  134. .remove = pcf8575_remove,
  135. .id_table = pcf8575_id,
  136. .detect = pcf8575_detect,
  137. .address_data = &addr_data,
  138. };
  139. static int __init pcf8575_init(void)
  140. {
  141. return i2c_add_driver(&pcf8575_driver);
  142. }
  143. static void __exit pcf8575_exit(void)
  144. {
  145. i2c_del_driver(&pcf8575_driver);
  146. }
  147. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>, "
  148. "Bart Van Assche <bart.vanassche@gmail.com>");
  149. MODULE_DESCRIPTION("pcf8575 driver");
  150. MODULE_LICENSE("GPL");
  151. module_init(pcf8575_init);
  152. module_exit(pcf8575_exit);