pca9539.c 5.2 KB

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