blob: f43c4e79b55e9b3cf9bf0bba1317a0196aadddea [file] [log] [blame]
bgardner@wabtec.com69dd2042005-06-07 08:55:38 -05001/*
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.com69dd2042005-06-07 08:55:38 -050016
17/* Addresses to scan */
18static unsigned short normal_i2c[] = {0x74, 0x75, 0x76, 0x77, I2C_CLIENT_END};
bgardner@wabtec.com69dd2042005-06-07 08:55:38 -050019
20/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020021I2C_CLIENT_INSMOD_1(pca9539);
bgardner@wabtec.com69dd2042005-06-07 08:55:38 -050022
23enum 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
35static int pca9539_attach_adapter(struct i2c_adapter *adapter);
36static int pca9539_detect(struct i2c_adapter *adapter, int address, int kind);
37static int pca9539_detach_client(struct i2c_client *client);
38
39/* This is the driver that will be inserted */
40static struct i2c_driver pca9539_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +010041 .driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +010042 .name = "pca9539",
43 },
bgardner@wabtec.com69dd2042005-06-07 08:55:38 -050044 .attach_adapter = pca9539_attach_adapter,
45 .detach_client = pca9539_detach_client,
46};
47
48struct pca9539_data {
49 struct i2c_client client;
50};
51
52/* following are the sysfs callback functions */
53static ssize_t pca9539_show(struct device *dev, struct device_attribute *attr,
54 char *buf)
55{
56 struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
57 struct i2c_client *client = to_i2c_client(dev);
58 return sprintf(buf, "%d\n", i2c_smbus_read_byte_data(client,
59 psa->index));
60}
61
62static ssize_t pca9539_store(struct device *dev, struct device_attribute *attr,
63 const char *buf, size_t count)
64{
65 struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
66 struct i2c_client *client = to_i2c_client(dev);
67 unsigned long val = simple_strtoul(buf, NULL, 0);
68 if (val > 0xff)
69 return -EINVAL;
70 i2c_smbus_write_byte_data(client, psa->index, val);
71 return count;
72}
73
74/* Define the device attributes */
75
76#define PCA9539_ENTRY_RO(name, cmd_idx) \
77 static SENSOR_DEVICE_ATTR(name, S_IRUGO, pca9539_show, NULL, cmd_idx)
78
79#define PCA9539_ENTRY_RW(name, cmd_idx) \
80 static SENSOR_DEVICE_ATTR(name, S_IRUGO | S_IWUSR, pca9539_show, \
81 pca9539_store, cmd_idx)
82
83PCA9539_ENTRY_RO(input0, PCA9539_INPUT_0);
84PCA9539_ENTRY_RO(input1, PCA9539_INPUT_1);
85PCA9539_ENTRY_RW(output0, PCA9539_OUTPUT_0);
86PCA9539_ENTRY_RW(output1, PCA9539_OUTPUT_1);
87PCA9539_ENTRY_RW(invert0, PCA9539_INVERT_0);
88PCA9539_ENTRY_RW(invert1, PCA9539_INVERT_1);
89PCA9539_ENTRY_RW(direction0, PCA9539_DIRECTION_0);
90PCA9539_ENTRY_RW(direction1, PCA9539_DIRECTION_1);
91
92static struct attribute *pca9539_attributes[] = {
93 &sensor_dev_attr_input0.dev_attr.attr,
94 &sensor_dev_attr_input1.dev_attr.attr,
95 &sensor_dev_attr_output0.dev_attr.attr,
96 &sensor_dev_attr_output1.dev_attr.attr,
97 &sensor_dev_attr_invert0.dev_attr.attr,
98 &sensor_dev_attr_invert1.dev_attr.attr,
99 &sensor_dev_attr_direction0.dev_attr.attr,
100 &sensor_dev_attr_direction1.dev_attr.attr,
101 NULL
102};
103
104static struct attribute_group pca9539_defattr_group = {
105 .attrs = pca9539_attributes,
106};
107
108static int pca9539_attach_adapter(struct i2c_adapter *adapter)
109{
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200110 return i2c_probe(adapter, &addr_data, pca9539_detect);
bgardner@wabtec.com69dd2042005-06-07 08:55:38 -0500111}
112
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200113/* This function is called by i2c_probe */
bgardner@wabtec.com69dd2042005-06-07 08:55:38 -0500114static int pca9539_detect(struct i2c_adapter *adapter, int address, int kind)
115{
116 struct i2c_client *new_client;
117 struct pca9539_data *data;
118 int err = 0;
119
120 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
121 goto exit;
122
123 /* OK. For now, we presume we have a valid client. We now create the
124 client structure, even though we cannot fill it completely yet. */
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200125 if (!(data = kzalloc(sizeof(struct pca9539_data), GFP_KERNEL))) {
bgardner@wabtec.com69dd2042005-06-07 08:55:38 -0500126 err = -ENOMEM;
127 goto exit;
128 }
bgardner@wabtec.com69dd2042005-06-07 08:55:38 -0500129
130 new_client = &data->client;
131 i2c_set_clientdata(new_client, data);
132 new_client->addr = address;
133 new_client->adapter = adapter;
134 new_client->driver = &pca9539_driver;
135 new_client->flags = 0;
136
Ben Gardner50436a42006-07-01 17:22:18 +0200137 if (kind < 0) {
138 /* Detection: the pca9539 only has 8 registers (0-7).
139 A read of 7 should succeed, but a read of 8 should fail. */
140 if ((i2c_smbus_read_byte_data(new_client, 7) < 0) ||
141 (i2c_smbus_read_byte_data(new_client, 8) >= 0))
142 goto exit_kfree;
143 }
bgardner@wabtec.com69dd2042005-06-07 08:55:38 -0500144
145 strlcpy(new_client->name, "pca9539", I2C_NAME_SIZE);
146
147 /* Tell the I2C layer a new client has arrived */
148 if ((err = i2c_attach_client(new_client)))
149 goto exit_kfree;
150
Jean Delvare7d9db672006-09-03 22:20:24 +0200151 /* Register sysfs hooks */
152 err = sysfs_create_group(&new_client->dev.kobj,
153 &pca9539_defattr_group);
154 if (err)
155 goto exit_detach;
bgardner@wabtec.com69dd2042005-06-07 08:55:38 -0500156
157 return 0;
158
Jean Delvare7d9db672006-09-03 22:20:24 +0200159exit_detach:
160 i2c_detach_client(new_client);
bgardner@wabtec.com69dd2042005-06-07 08:55:38 -0500161exit_kfree:
162 kfree(data);
163exit:
164 return err;
165}
166
167static int pca9539_detach_client(struct i2c_client *client)
168{
169 int err;
170
Jean Delvare7d9db672006-09-03 22:20:24 +0200171 sysfs_remove_group(&client->dev.kobj, &pca9539_defattr_group);
172
Jean Delvare7bef5592005-07-27 22:14:49 +0200173 if ((err = i2c_detach_client(client)))
bgardner@wabtec.com69dd2042005-06-07 08:55:38 -0500174 return err;
bgardner@wabtec.com69dd2042005-06-07 08:55:38 -0500175
176 kfree(i2c_get_clientdata(client));
177 return 0;
178}
179
180static int __init pca9539_init(void)
181{
182 return i2c_add_driver(&pca9539_driver);
183}
184
185static void __exit pca9539_exit(void)
186{
187 i2c_del_driver(&pca9539_driver);
188}
189
190MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
191MODULE_DESCRIPTION("PCA9539 driver");
192MODULE_LICENSE("GPL");
193
194module_init(pca9539_init);
195module_exit(pca9539_exit);
196