pca9539.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. pca9539.c - 16-bit I/O port with interrupt and reset
  3. Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; version 2 of the License.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/i2c.h>
  12. #include <linux/hwmon-sysfs.h>
  13. /* Addresses to scan */
  14. static unsigned short normal_i2c[] = {0x74, 0x75, 0x76, 0x77, I2C_CLIENT_END};
  15. /* Insmod parameters */
  16. I2C_CLIENT_INSMOD_1(pca9539);
  17. enum pca9539_cmd
  18. {
  19. PCA9539_INPUT_0 = 0,
  20. PCA9539_INPUT_1 = 1,
  21. PCA9539_OUTPUT_0 = 2,
  22. PCA9539_OUTPUT_1 = 3,
  23. PCA9539_INVERT_0 = 4,
  24. PCA9539_INVERT_1 = 5,
  25. PCA9539_DIRECTION_0 = 6,
  26. PCA9539_DIRECTION_1 = 7,
  27. };
  28. static int pca9539_attach_adapter(struct i2c_adapter *adapter);
  29. static int pca9539_detect(struct i2c_adapter *adapter, int address, int kind);
  30. static int pca9539_detach_client(struct i2c_client *client);
  31. /* This is the driver that will be inserted */
  32. static struct i2c_driver pca9539_driver = {
  33. .driver = {
  34. .name = "pca9539",
  35. },
  36. .attach_adapter = pca9539_attach_adapter,
  37. .detach_client = pca9539_detach_client,
  38. };
  39. struct pca9539_data {
  40. struct i2c_client client;
  41. };
  42. /* following are the sysfs callback functions */
  43. static ssize_t pca9539_show(struct device *dev, struct device_attribute *attr,
  44. char *buf)
  45. {
  46. struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
  47. struct i2c_client *client = to_i2c_client(dev);
  48. return sprintf(buf, "%d\n", i2c_smbus_read_byte_data(client,
  49. psa->index));
  50. }
  51. static ssize_t pca9539_store(struct device *dev, struct device_attribute *attr,
  52. const char *buf, size_t count)
  53. {
  54. struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
  55. struct i2c_client *client = to_i2c_client(dev);
  56. unsigned long val = simple_strtoul(buf, NULL, 0);
  57. if (val > 0xff)
  58. return -EINVAL;
  59. i2c_smbus_write_byte_data(client, psa->index, val);
  60. return count;
  61. }
  62. /* Define the device attributes */
  63. #define PCA9539_ENTRY_RO(name, cmd_idx) \
  64. static SENSOR_DEVICE_ATTR(name, S_IRUGO, pca9539_show, NULL, cmd_idx)
  65. #define PCA9539_ENTRY_RW(name, cmd_idx) \
  66. static SENSOR_DEVICE_ATTR(name, S_IRUGO | S_IWUSR, pca9539_show, \
  67. pca9539_store, cmd_idx)
  68. PCA9539_ENTRY_RO(input0, PCA9539_INPUT_0);
  69. PCA9539_ENTRY_RO(input1, PCA9539_INPUT_1);
  70. PCA9539_ENTRY_RW(output0, PCA9539_OUTPUT_0);
  71. PCA9539_ENTRY_RW(output1, PCA9539_OUTPUT_1);
  72. PCA9539_ENTRY_RW(invert0, PCA9539_INVERT_0);
  73. PCA9539_ENTRY_RW(invert1, PCA9539_INVERT_1);
  74. PCA9539_ENTRY_RW(direction0, PCA9539_DIRECTION_0);
  75. PCA9539_ENTRY_RW(direction1, PCA9539_DIRECTION_1);
  76. static struct attribute *pca9539_attributes[] = {
  77. &sensor_dev_attr_input0.dev_attr.attr,
  78. &sensor_dev_attr_input1.dev_attr.attr,
  79. &sensor_dev_attr_output0.dev_attr.attr,
  80. &sensor_dev_attr_output1.dev_attr.attr,
  81. &sensor_dev_attr_invert0.dev_attr.attr,
  82. &sensor_dev_attr_invert1.dev_attr.attr,
  83. &sensor_dev_attr_direction0.dev_attr.attr,
  84. &sensor_dev_attr_direction1.dev_attr.attr,
  85. NULL
  86. };
  87. static struct attribute_group pca9539_defattr_group = {
  88. .attrs = pca9539_attributes,
  89. };
  90. static int pca9539_attach_adapter(struct i2c_adapter *adapter)
  91. {
  92. return i2c_probe(adapter, &addr_data, pca9539_detect);
  93. }
  94. /* This function is called by i2c_probe */
  95. static int pca9539_detect(struct i2c_adapter *adapter, int address, int kind)
  96. {
  97. struct i2c_client *new_client;
  98. struct pca9539_data *data;
  99. int err = 0;
  100. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  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 = kzalloc(sizeof(struct pca9539_data), GFP_KERNEL))) {
  105. err = -ENOMEM;
  106. goto exit;
  107. }
  108. new_client = &data->client;
  109. i2c_set_clientdata(new_client, data);
  110. new_client->addr = address;
  111. new_client->adapter = adapter;
  112. new_client->driver = &pca9539_driver;
  113. new_client->flags = 0;
  114. /* Detection: the pca9539 only has 8 registers (0-7).
  115. A read of 7 should succeed, but a read of 8 should fail. */
  116. if ((i2c_smbus_read_byte_data(new_client, 7) < 0) ||
  117. (i2c_smbus_read_byte_data(new_client, 8) >= 0))
  118. goto exit_kfree;
  119. strlcpy(new_client->name, "pca9539", I2C_NAME_SIZE);
  120. /* Tell the I2C layer a new client has arrived */
  121. if ((err = i2c_attach_client(new_client)))
  122. goto exit_kfree;
  123. /* Register sysfs hooks (don't care about failure) */
  124. sysfs_create_group(&new_client->dev.kobj, &pca9539_defattr_group);
  125. return 0;
  126. exit_kfree:
  127. kfree(data);
  128. exit:
  129. return err;
  130. }
  131. static int pca9539_detach_client(struct i2c_client *client)
  132. {
  133. int err;
  134. if ((err = i2c_detach_client(client)))
  135. return err;
  136. kfree(i2c_get_clientdata(client));
  137. return 0;
  138. }
  139. static int __init pca9539_init(void)
  140. {
  141. return i2c_add_driver(&pca9539_driver);
  142. }
  143. static void __exit pca9539_exit(void)
  144. {
  145. i2c_del_driver(&pca9539_driver);
  146. }
  147. MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
  148. MODULE_DESCRIPTION("PCA9539 driver");
  149. MODULE_LICENSE("GPL");
  150. module_init(pca9539_init);
  151. module_exit(pca9539_exit);