Merge xburst target.
[openwrt.git] / target / linux / xburst / files-2.6.32 / drivers / i2c / chips / n526-lpc.c
1 /*
2  * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  *  You should have received a copy of the  GNU General Public License along
9  *  with this program; if not, write  to the Free Software Foundation, Inc.,
10  *  675 Mass Ave, Cambridge, MA 02139, USA.
11  *
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/input.h>
18 #include <linux/irq.h>
19 #include <linux/interrupt.h>
20
21 #include <linux/workqueue.h>
22
23 #include <asm/mach-jz4740/irq.h>
24 #include <asm/mach-jz4740/gpio.h>
25
26 struct n526_lpc {
27         struct i2c_client *client;
28         struct input_dev *input;
29
30         struct work_struct work;
31 };
32
33 static const unsigned int n526_lpc_keymap[] = {
34         [0x01] = KEY_PAGEUP,
35         [0x02] = KEY_PAGEDOWN,
36         [0x03] = KEY_VOLUMEUP,
37         [0x04] = KEY_VOLUMEDOWN,
38         [0x06] = KEY_1,
39         [0x07] = KEY_Q,
40         [0x08] = KEY_A,
41         [0x09] = KEY_Z,
42         [0x0a] = KEY_LEFTSHIFT,
43         [0x0b] = KEY_2,
44         [0x0c] = KEY_W,
45         [0x0d] = KEY_S,
46         [0x0e] = KEY_X,
47         [0x0f] = KEY_REFRESH,
48         [0x10] = KEY_3,
49         [0x11] = KEY_E,
50         [0x12] = KEY_D,
51         [0x13] = KEY_C,
52         [0x14] = KEY_DOCUMENTS,
53         [0x15] = KEY_4,
54         [0x16] = KEY_R,
55         [0x17] = KEY_F,
56         [0x18] = KEY_V,
57         [0x19] = KEY_MUTE,
58         [0x1a] = KEY_5,
59         [0x1b] = KEY_T,
60         [0x1c] = KEY_G,
61         [0x1d] = KEY_B,
62         [0x1e] = KEY_DELETE,
63         [0x1f] = KEY_6,
64         [0x20] = KEY_Y,
65         [0x21] = KEY_H,
66         [0x22] = KEY_N,
67         [0x23] = KEY_SPACE,
68         [0x24] = KEY_7,
69         [0x25] = KEY_U,
70         [0x26] = KEY_J,
71         [0x27] = KEY_M,
72 /*      [0x28] = KEY_SYM, */
73         [0x29] = KEY_8,
74         [0x2a] = KEY_I,
75         [0x2b] = KEY_K,
76         [0x2c] = KEY_MENU,
77         [0x2d] = KEY_LEFT,
78         [0x2e] = KEY_9,
79         [0x2f] = KEY_O,
80         [0x30] = KEY_L,
81         [0x31] = KEY_UP,
82         [0x32] = KEY_DOWN,
83         [0x33] = KEY_0,
84         [0x34] = KEY_P,
85         [0x35] = KEY_BACKSPACE,
86         [0x36] = KEY_ENTER,
87         [0x37] = KEY_RIGHT,
88 };
89
90 static void n526_lpc_irq_work(struct work_struct *work)
91 {
92         int ret;
93         struct n526_lpc *n526_lpc = container_of(work, struct n526_lpc, work);
94         struct i2c_client *client = n526_lpc->client;
95         unsigned char raw_msg;
96         struct i2c_msg msg = {client->addr, client->flags | I2C_M_RD, 1, &raw_msg};
97         unsigned char keycode;
98
99
100         ret = i2c_transfer(client->adapter, &msg, 1);
101
102         if (ret != 1) {
103                 dev_err(&client->dev, "Failed to read lpc status\n");
104         }
105
106         keycode = raw_msg & 0x7f;
107
108         if (keycode < ARRAY_SIZE(n526_lpc_keymap)) {
109                 input_report_key(n526_lpc->input, n526_lpc_keymap[keycode],
110                                  !(raw_msg & 0x80));
111                 input_sync(n526_lpc->input);
112         }
113 }
114
115 static irqreturn_t n526_lpc_irq(int irq, void *dev_id)
116 {
117         struct n526_lpc *n526_lpc = dev_id;
118
119         schedule_work(&n526_lpc->work);
120         return IRQ_HANDLED;
121 }
122
123 static int __devinit n526_lpc_probe(struct i2c_client *client,
124                 const struct i2c_device_id *id)
125 {
126         int ret;
127         size_t i;
128         struct n526_lpc *n526_lpc;
129         struct input_dev *input;
130
131         n526_lpc = kmalloc(sizeof(*n526_lpc), GFP_KERNEL);
132
133         if (!n526_lpc) {
134                 dev_err(&client->dev, "Failed to allocate device structure\n");
135                 return -ENOMEM;
136         }
137
138         input = input_allocate_device();
139         if (!input) {
140                 dev_err(&client->dev, "Failed to allocate input device\n");
141                 ret = -ENOMEM;
142                 goto err_free;
143         }
144
145         input->name = "n526-keys";
146         input->phys = "n526-keys/input0";
147         input->dev.parent = &client->dev;
148         input->id.bustype = BUS_I2C;
149         input->id.vendor  = 0x0001;
150         input->id.product = 0x0001;
151         input->id.version = 0x0001;
152
153         __set_bit(EV_KEY, input->evbit);
154
155         for (i = 0; i < ARRAY_SIZE(n526_lpc_keymap); ++i) {
156                 if (n526_lpc_keymap[i] != 0)
157                         __set_bit(n526_lpc_keymap[i], input->keybit);
158         }
159
160         ret = input_register_device(input);
161
162         if (ret) {
163                 dev_err(&client->dev, "Failed to register input device: %d\n", ret);
164                 goto err_free_input;
165         }
166
167         n526_lpc->client = client;
168         n526_lpc->input  = input;
169         INIT_WORK(&n526_lpc->work, n526_lpc_irq_work);
170
171         ret = request_irq(client->irq, n526_lpc_irq, IRQF_TRIGGER_FALLING,
172                           "n526-lpc", n526_lpc);
173         if (ret) {
174                 dev_err(&client->dev, "Failed to request irq: %d\n", ret);
175                 goto err_unregister_input;
176         }
177
178         i2c_set_clientdata(client, n526_lpc);
179
180         return 0;
181
182 err_unregister_input:
183         input_unregister_device(input);
184 err_free_input:
185         input_free_device(input);
186 err_free:
187         kfree(n526_lpc);
188
189         return ret;
190 }
191
192 static int n526_lpc_remove(struct i2c_client *client)
193 {
194         struct n526_lpc *n526_lpc = i2c_get_clientdata(client);
195
196         free_irq(client->irq, n526_lpc);
197
198         i2c_set_clientdata(client, NULL);
199         input_unregister_device(n526_lpc->input);
200         input_free_device(n526_lpc->input);
201         kfree(n526_lpc);
202
203  return 0;
204 }
205
206 static const struct i2c_device_id n526_lpc_id[] = {
207         { "n526-lpc", 0 },
208         { }
209 };
210 MODULE_DEVICE_TABLE(i2c, n526_lpc_id);
211
212 static struct i2c_driver n526_lpc_driver = {
213         .driver = {
214                 .name = "n526-lpc",
215                 .owner = THIS_MODULE,
216         },
217         .probe = n526_lpc_probe,
218         .remove = n526_lpc_remove,
219         .id_table = n526_lpc_id,
220 };
221
222 static int __init n526_lpc_init(void)
223 {
224         return i2c_add_driver(&n526_lpc_driver);
225 }
226 module_init(n526_lpc_init);
227
228 static void __exit n526_lpc_exit(void)
229 {
230         i2c_del_driver(&n526_lpc_driver);
231 }
232 module_exit(n526_lpc_exit);
233
234 MODULE_LICENSE("GPL");
235 MODULE_AUTHOR("Lars-Peter Clausen");
236 MODULE_DESCRIPTION("n526 keypad driver");
237 MODULE_ALIAS("i2c:n526-keys");