pca9539.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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: none, device is not autodetected */
  14. static const unsigned short normal_i2c[] = { 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. /* following are the sysfs callback functions */
  29. static ssize_t pca9539_show(struct device *dev, struct device_attribute *attr,
  30. char *buf)
  31. {
  32. struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
  33. struct i2c_client *client = to_i2c_client(dev);
  34. return sprintf(buf, "%d\n", i2c_smbus_read_byte_data(client,
  35. psa->index));
  36. }
  37. static ssize_t pca9539_store(struct device *dev, struct device_attribute *attr,
  38. const char *buf, size_t count)
  39. {
  40. struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
  41. struct i2c_client *client = to_i2c_client(dev);
  42. unsigned long val = simple_strtoul(buf, NULL, 0);
  43. if (val > 0xff)
  44. return -EINVAL;
  45. i2c_smbus_write_byte_data(client, psa->index, val);
  46. return count;
  47. }
  48. /* Define the device attributes */
  49. #define PCA9539_ENTRY_RO(name, cmd_idx) \
  50. static SENSOR_DEVICE_ATTR(name, S_IRUGO, pca9539_show, NULL, cmd_idx)
  51. #define PCA9539_ENTRY_RW(name, cmd_idx) \
  52. static SENSOR_DEVICE_ATTR(name, S_IRUGO | S_IWUSR, pca9539_show, \
  53. pca9539_store, cmd_idx)
  54. PCA9539_ENTRY_RO(input0, PCA9539_INPUT_0);
  55. PCA9539_ENTRY_RO(input1, PCA9539_INPUT_1);
  56. PCA9539_ENTRY_RW(output0, PCA9539_OUTPUT_0);
  57. PCA9539_ENTRY_RW(output1, PCA9539_OUTPUT_1);
  58. PCA9539_ENTRY_RW(invert0, PCA9539_INVERT_0);
  59. PCA9539_ENTRY_RW(invert1, PCA9539_INVERT_1);
  60. PCA9539_ENTRY_RW(direction0, PCA9539_DIRECTION_0);
  61. PCA9539_ENTRY_RW(direction1, PCA9539_DIRECTION_1);
  62. static struct attribute *pca9539_attributes[] = {
  63. &sensor_dev_attr_input0.dev_attr.attr,
  64. &sensor_dev_attr_input1.dev_attr.attr,
  65. &sensor_dev_attr_output0.dev_attr.attr,
  66. &sensor_dev_attr_output1.dev_attr.attr,
  67. &sensor_dev_attr_invert0.dev_attr.attr,
  68. &sensor_dev_attr_invert1.dev_attr.attr,
  69. &sensor_dev_attr_direction0.dev_attr.attr,
  70. &sensor_dev_attr_direction1.dev_attr.attr,
  71. NULL
  72. };
  73. static struct attribute_group pca9539_defattr_group = {
  74. .attrs = pca9539_attributes,
  75. };
  76. /* Return 0 if detection is successful, -ENODEV otherwise */
  77. static int pca9539_detect(struct i2c_client *client, int kind,
  78. struct i2c_board_info *info)
  79. {
  80. struct i2c_adapter *adapter = client->adapter;
  81. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  82. return -ENODEV;
  83. strlcpy(info->type, "pca9539", I2C_NAME_SIZE);
  84. return 0;
  85. }
  86. static int pca9539_probe(struct i2c_client *client,
  87. const struct i2c_device_id *id)
  88. {
  89. /* Register sysfs hooks */
  90. return sysfs_create_group(&client->dev.kobj,
  91. &pca9539_defattr_group);
  92. }
  93. static int pca9539_remove(struct i2c_client *client)
  94. {
  95. sysfs_remove_group(&client->dev.kobj, &pca9539_defattr_group);
  96. return 0;
  97. }
  98. static const struct i2c_device_id pca9539_id[] = {
  99. { "pca9539", 0 },
  100. { }
  101. };
  102. static struct i2c_driver pca9539_driver = {
  103. .driver = {
  104. .name = "pca9539",
  105. },
  106. .probe = pca9539_probe,
  107. .remove = pca9539_remove,
  108. .id_table = pca9539_id,
  109. .detect = pca9539_detect,
  110. .address_data = &addr_data,
  111. };
  112. static int __init pca9539_init(void)
  113. {
  114. return i2c_add_driver(&pca9539_driver);
  115. }
  116. static void __exit pca9539_exit(void)
  117. {
  118. i2c_del_driver(&pca9539_driver);
  119. }
  120. MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
  121. MODULE_DESCRIPTION("PCA9539 driver");
  122. MODULE_LICENSE("GPL");
  123. module_init(pca9539_init);
  124. module_exit(pca9539_exit);