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