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