diff options
Diffstat (limited to 'drivers/input/touchscreen/rmi4/rmi_i2c.c')
-rw-r--r-- | drivers/input/touchscreen/rmi4/rmi_i2c.c | 421 |
1 files changed, 421 insertions, 0 deletions
diff --git a/drivers/input/touchscreen/rmi4/rmi_i2c.c b/drivers/input/touchscreen/rmi4/rmi_i2c.c new file mode 100644 index 00000000000..15624f9cf54 --- /dev/null +++ b/drivers/input/touchscreen/rmi4/rmi_i2c.c | |||
@@ -0,0 +1,421 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2011 Synaptics Incorporated | ||
3 | * Copyright (c) 2011 Unixphere | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the Free Software | ||
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
18 | */ | ||
19 | #include <linux/kernel.h> | ||
20 | #include <linux/lockdep.h> | ||
21 | #include <linux/module.h> | ||
22 | #include <linux/i2c.h> | ||
23 | #include <linux/interrupt.h> | ||
24 | #include <linux/delay.h> | ||
25 | #include <linux/slab.h> | ||
26 | #include <linux/pm.h> | ||
27 | #include <linux/gpio.h> | ||
28 | #include <linux/rmi.h> | ||
29 | |||
30 | #define COMMS_DEBUG 0 | ||
31 | |||
32 | #define IRQ_DEBUG 0 | ||
33 | |||
34 | #define RMI_PAGE_SELECT_REGISTER 0xff | ||
35 | #define RMI_I2C_PAGE(addr) (((addr) >> 8) & 0xff) | ||
36 | |||
37 | static char *phys_proto_name = "i2c"; | ||
38 | |||
39 | struct rmi_i2c_data { | ||
40 | struct mutex page_mutex; | ||
41 | int page; | ||
42 | int enabled; | ||
43 | int irq; | ||
44 | int irq_flags; | ||
45 | struct rmi_phys_device *phys; | ||
46 | }; | ||
47 | |||
48 | static irqreturn_t rmi_i2c_irq_thread(int irq, void *p) | ||
49 | { | ||
50 | struct rmi_phys_device *phys = p; | ||
51 | struct rmi_device *rmi_dev = phys->rmi_dev; | ||
52 | struct rmi_driver *driver = rmi_dev->driver; | ||
53 | struct rmi_device_platform_data *pdata = phys->dev->platform_data; | ||
54 | |||
55 | #if IRQ_DEBUG | ||
56 | dev_dbg(phys->dev, "ATTN gpio, value: %d.\n", | ||
57 | gpio_get_value(irq_to_gpio(irq))); | ||
58 | #endif | ||
59 | if (gpio_get_value(irq_to_gpio(irq)) == pdata->irq_polarity) { | ||
60 | phys->info.attn_count++; | ||
61 | if (driver && driver->irq_handler && rmi_dev) | ||
62 | driver->irq_handler(rmi_dev, irq); | ||
63 | } | ||
64 | |||
65 | return IRQ_HANDLED; | ||
66 | } | ||
67 | |||
68 | /* | ||
69 | * rmi_set_page - Set RMI page | ||
70 | * @phys: The pointer to the rmi_phys_device struct | ||
71 | * @page: The new page address. | ||
72 | * | ||
73 | * RMI devices have 16-bit addressing, but some of the physical | ||
74 | * implementations (like SMBus) only have 8-bit addressing. So RMI implements | ||
75 | * a page address at 0xff of every page so we can reliable page addresses | ||
76 | * every 256 registers. | ||
77 | * | ||
78 | * The page_mutex lock must be held when this function is entered. | ||
79 | * | ||
80 | * Returns zero on success, non-zero on failure. | ||
81 | */ | ||
82 | static int rmi_set_page(struct rmi_phys_device *phys, unsigned int page) | ||
83 | { | ||
84 | struct i2c_client *client = to_i2c_client(phys->dev); | ||
85 | struct rmi_i2c_data *data = phys->data; | ||
86 | char txbuf[2] = {RMI_PAGE_SELECT_REGISTER, page}; | ||
87 | int retval; | ||
88 | |||
89 | #if COMMS_DEBUG | ||
90 | dev_dbg(&client->dev, "RMI4 I2C writes 3 bytes: %02x %02x\n", | ||
91 | txbuf[0], txbuf[1]); | ||
92 | #endif | ||
93 | phys->info.tx_count++; | ||
94 | phys->info.tx_bytes += sizeof(txbuf); | ||
95 | retval = i2c_master_send(client, txbuf, sizeof(txbuf)); | ||
96 | if (retval != sizeof(txbuf)) { | ||
97 | phys->info.tx_errs++; | ||
98 | dev_err(&client->dev, | ||
99 | "%s: set page failed: %d.", __func__, retval); | ||
100 | return (retval < 0) ? retval : -EIO; | ||
101 | } | ||
102 | data->page = page; | ||
103 | return 0; | ||
104 | } | ||
105 | |||
106 | static int rmi_i2c_write_block(struct rmi_phys_device *phys, u16 addr, u8 *buf, | ||
107 | int len) | ||
108 | { | ||
109 | struct i2c_client *client = to_i2c_client(phys->dev); | ||
110 | struct rmi_i2c_data *data = phys->data; | ||
111 | u8 txbuf[len + 1]; | ||
112 | int retval; | ||
113 | #if COMMS_DEBUG | ||
114 | int i; | ||
115 | #endif | ||
116 | |||
117 | txbuf[0] = addr & 0xff; | ||
118 | memcpy(txbuf + 1, buf, len); | ||
119 | |||
120 | mutex_lock(&data->page_mutex); | ||
121 | |||
122 | if (RMI_I2C_PAGE(addr) != data->page) { | ||
123 | retval = rmi_set_page(phys, RMI_I2C_PAGE(addr)); | ||
124 | if (retval < 0) | ||
125 | goto exit; | ||
126 | } | ||
127 | |||
128 | #if COMMS_DEBUG | ||
129 | dev_dbg(&client->dev, "RMI4 I2C writes %d bytes: ", sizeof(txbuf)); | ||
130 | for (i = 0; i < sizeof(txbuf); i++) | ||
131 | dev_dbg(&client->dev, "%02x ", txbuf[i]); | ||
132 | dev_dbg(&client->dev, "\n"); | ||
133 | #endif | ||
134 | |||
135 | phys->info.tx_count++; | ||
136 | phys->info.tx_bytes += sizeof(txbuf); | ||
137 | retval = i2c_master_send(client, txbuf, sizeof(txbuf)); | ||
138 | if (retval < 0) | ||
139 | phys->info.tx_errs++; | ||
140 | |||
141 | exit: | ||
142 | mutex_unlock(&data->page_mutex); | ||
143 | return retval; | ||
144 | } | ||
145 | |||
146 | static int rmi_i2c_write(struct rmi_phys_device *phys, u16 addr, u8 data) | ||
147 | { | ||
148 | int retval = rmi_i2c_write_block(phys, addr, &data, 1); | ||
149 | return (retval < 0) ? retval : 0; | ||
150 | } | ||
151 | |||
152 | static int rmi_i2c_read_block(struct rmi_phys_device *phys, u16 addr, u8 *buf, | ||
153 | int len) | ||
154 | { | ||
155 | struct i2c_client *client = to_i2c_client(phys->dev); | ||
156 | struct rmi_i2c_data *data = phys->data; | ||
157 | u8 txbuf[1] = {addr & 0xff}; | ||
158 | int retval; | ||
159 | #if COMMS_DEBUG | ||
160 | int i; | ||
161 | #endif | ||
162 | |||
163 | mutex_lock(&data->page_mutex); | ||
164 | |||
165 | if (RMI_I2C_PAGE(addr) != data->page) { | ||
166 | retval = rmi_set_page(phys, RMI_I2C_PAGE(addr)); | ||
167 | if (retval < 0) | ||
168 | goto exit; | ||
169 | } | ||
170 | |||
171 | #if COMMS_DEBUG | ||
172 | dev_dbg(&client->dev, "RMI4 I2C writes 1 bytes: %02x\n", txbuf[0]); | ||
173 | #endif | ||
174 | phys->info.tx_count++; | ||
175 | phys->info.tx_bytes += sizeof(txbuf); | ||
176 | retval = i2c_master_send(client, txbuf, sizeof(txbuf)); | ||
177 | if (retval != sizeof(txbuf)) { | ||
178 | phys->info.tx_errs++; | ||
179 | retval = (retval < 0) ? retval : -EIO; | ||
180 | goto exit; | ||
181 | } | ||
182 | |||
183 | retval = i2c_master_recv(client, buf, len); | ||
184 | |||
185 | phys->info.rx_count++; | ||
186 | phys->info.rx_bytes += len; | ||
187 | if (retval < 0) | ||
188 | phys->info.rx_errs++; | ||
189 | #if COMMS_DEBUG | ||
190 | else { | ||
191 | dev_dbg(&client->dev, "RMI4 I2C received %d bytes: ", len); | ||
192 | for (i = 0; i < len; i++) | ||
193 | dev_dbg(&client->dev, "%02x ", buf[i]); | ||
194 | dev_dbg(&client->dev, "\n"); | ||
195 | } | ||
196 | #endif | ||
197 | |||
198 | exit: | ||
199 | mutex_unlock(&data->page_mutex); | ||
200 | return retval; | ||
201 | } | ||
202 | |||
203 | static int rmi_i2c_read(struct rmi_phys_device *phys, u16 addr, u8 *buf) | ||
204 | { | ||
205 | int retval = rmi_i2c_read_block(phys, addr, buf, 1); | ||
206 | return (retval < 0) ? retval : 0; | ||
207 | } | ||
208 | |||
209 | |||
210 | static int acquire_attn_irq(struct rmi_i2c_data *data) | ||
211 | { | ||
212 | return request_threaded_irq(data->irq, NULL, rmi_i2c_irq_thread, | ||
213 | data->irq_flags, dev_name(data->phys->dev), data->phys); | ||
214 | } | ||
215 | |||
216 | static int enable_device(struct rmi_phys_device *phys) | ||
217 | { | ||
218 | int retval = 0; | ||
219 | |||
220 | struct rmi_i2c_data *data = phys->data; | ||
221 | |||
222 | if (data->enabled) | ||
223 | return 0; | ||
224 | |||
225 | retval = acquire_attn_irq(data); | ||
226 | if (retval) | ||
227 | goto error_exit; | ||
228 | |||
229 | data->enabled = true; | ||
230 | dev_dbg(phys->dev, "Physical device enabled.\n"); | ||
231 | return 0; | ||
232 | |||
233 | error_exit: | ||
234 | dev_err(phys->dev, "Failed to enable physical device. Code=%d.\n", | ||
235 | retval); | ||
236 | return retval; | ||
237 | } | ||
238 | |||
239 | |||
240 | static void disable_device(struct rmi_phys_device *phys) | ||
241 | { | ||
242 | struct rmi_i2c_data *data = phys->data; | ||
243 | |||
244 | if (!data->enabled) | ||
245 | return; | ||
246 | |||
247 | disable_irq(data->irq); | ||
248 | free_irq(data->irq, data->phys); | ||
249 | |||
250 | dev_dbg(phys->dev, "Physical device disabled.\n"); | ||
251 | data->enabled = false; | ||
252 | } | ||
253 | |||
254 | |||
255 | static int __devinit rmi_i2c_probe(struct i2c_client *client, | ||
256 | const struct i2c_device_id *id) | ||
257 | { | ||
258 | struct rmi_phys_device *rmi_phys; | ||
259 | struct rmi_i2c_data *data; | ||
260 | struct rmi_device_platform_data *pdata = client->dev.platform_data; | ||
261 | int error; | ||
262 | |||
263 | if (!pdata) { | ||
264 | dev_err(&client->dev, "no platform data\n"); | ||
265 | return -EINVAL; | ||
266 | } | ||
267 | |||
268 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | ||
269 | dev_err(&client->dev, "i2c_check_functionality error\n"); | ||
270 | return -EIO; | ||
271 | } | ||
272 | |||
273 | rmi_phys = kzalloc(sizeof(struct rmi_phys_device), GFP_KERNEL); | ||
274 | if (!rmi_phys) | ||
275 | return -ENOMEM; | ||
276 | |||
277 | data = kzalloc(sizeof(struct rmi_i2c_data), GFP_KERNEL); | ||
278 | if (!data) { | ||
279 | error = -ENOMEM; | ||
280 | goto err_phys; | ||
281 | } | ||
282 | |||
283 | data->enabled = true; /* We plan to come up enabled. */ | ||
284 | data->irq = gpio_to_irq(pdata->irq); | ||
285 | data->irq_flags = (pdata->irq_polarity == RMI_IRQ_ACTIVE_HIGH) ? | ||
286 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | ||
287 | data->phys = rmi_phys; | ||
288 | |||
289 | rmi_phys->data = data; | ||
290 | rmi_phys->dev = &client->dev; | ||
291 | |||
292 | rmi_phys->write = rmi_i2c_write; | ||
293 | rmi_phys->write_block = rmi_i2c_write_block; | ||
294 | rmi_phys->read = rmi_i2c_read; | ||
295 | rmi_phys->read_block = rmi_i2c_read_block; | ||
296 | rmi_phys->enable_device = enable_device; | ||
297 | rmi_phys->disable_device = disable_device; | ||
298 | |||
299 | rmi_phys->info.proto = phys_proto_name; | ||
300 | |||
301 | mutex_init(&data->page_mutex); | ||
302 | |||
303 | /* Setting the page to zero will (a) make sure the PSR is in a | ||
304 | * known state, and (b) make sure we can talk to the device. | ||
305 | */ | ||
306 | error = rmi_set_page(rmi_phys, 0); | ||
307 | if (error) { | ||
308 | dev_err(&client->dev, "Failed to set page select to 0.\n"); | ||
309 | goto err_data; | ||
310 | } | ||
311 | |||
312 | if (pdata->gpio_config) { | ||
313 | error = pdata->gpio_config(&client->dev, true); | ||
314 | if (error < 0) { | ||
315 | dev_err(&client->dev, "failed to setup irq %d\n", | ||
316 | pdata->irq); | ||
317 | goto err_data; | ||
318 | } | ||
319 | } | ||
320 | |||
321 | error = rmi_register_phys_device(rmi_phys); | ||
322 | if (error) { | ||
323 | dev_err(&client->dev, | ||
324 | "failed to register physical driver at 0x%.2X.\n", | ||
325 | client->addr); | ||
326 | goto err_data; | ||
327 | } | ||
328 | i2c_set_clientdata(client, rmi_phys); | ||
329 | |||
330 | if (pdata->irq > 0) { | ||
331 | error = acquire_attn_irq(data); | ||
332 | if (error < 0) { | ||
333 | dev_err(&client->dev, | ||
334 | "request_threaded_irq failed %d\n", | ||
335 | pdata->irq); | ||
336 | goto err_unregister; | ||
337 | } | ||
338 | } | ||
339 | |||
340 | #if defined(CONFIG_RMI4_DEV) | ||
341 | error = gpio_export(pdata->irq, false); | ||
342 | if (error) { | ||
343 | dev_warn(&client->dev, "%s: WARNING: Failed to " | ||
344 | "export ATTN gpio!\n", __func__); | ||
345 | error = 0; | ||
346 | } else { | ||
347 | error = gpio_export_link(&(rmi_phys->rmi_dev->dev), "attn", | ||
348 | pdata->irq); | ||
349 | if (error) { | ||
350 | dev_warn(&(rmi_phys->rmi_dev->dev), "%s: WARNING: " | ||
351 | "Failed to symlink ATTN gpio!\n", __func__); | ||
352 | error = 0; | ||
353 | } else { | ||
354 | dev_info(&(rmi_phys->rmi_dev->dev), | ||
355 | "%s: Exported GPIO %d.", __func__, pdata->irq); | ||
356 | } | ||
357 | } | ||
358 | #endif /* CONFIG_RMI4_DEV */ | ||
359 | |||
360 | dev_info(&client->dev, "registered rmi i2c driver at 0x%.2X.\n", | ||
361 | client->addr); | ||
362 | return 0; | ||
363 | |||
364 | err_unregister: | ||
365 | rmi_unregister_phys_device(rmi_phys); | ||
366 | err_data: | ||
367 | kfree(data); | ||
368 | err_phys: | ||
369 | kfree(rmi_phys); | ||
370 | return error; | ||
371 | } | ||
372 | |||
373 | static int __devexit rmi_i2c_remove(struct i2c_client *client) | ||
374 | { | ||
375 | struct rmi_phys_device *phys = i2c_get_clientdata(client); | ||
376 | struct rmi_device_platform_data *pd = client->dev.platform_data; | ||
377 | |||
378 | rmi_unregister_phys_device(phys); | ||
379 | kfree(phys->data); | ||
380 | kfree(phys); | ||
381 | |||
382 | if (pd->gpio_config) | ||
383 | pd->gpio_config(&client->dev, false); | ||
384 | |||
385 | return 0; | ||
386 | } | ||
387 | |||
388 | static const struct i2c_device_id rmi_id[] = { | ||
389 | { "rmi", 0 }, | ||
390 | { "rmi-i2c", 0 }, | ||
391 | { } | ||
392 | }; | ||
393 | MODULE_DEVICE_TABLE(i2c, rmi_id); | ||
394 | |||
395 | static struct i2c_driver rmi_i2c_driver = { | ||
396 | .driver = { | ||
397 | .owner = THIS_MODULE, | ||
398 | .name = "rmi-i2c" | ||
399 | }, | ||
400 | .id_table = rmi_id, | ||
401 | .probe = rmi_i2c_probe, | ||
402 | .remove = __devexit_p(rmi_i2c_remove), | ||
403 | }; | ||
404 | |||
405 | static int __init rmi_i2c_init(void) | ||
406 | { | ||
407 | return i2c_add_driver(&rmi_i2c_driver); | ||
408 | } | ||
409 | |||
410 | static void __exit rmi_i2c_exit(void) | ||
411 | { | ||
412 | i2c_del_driver(&rmi_i2c_driver); | ||
413 | } | ||
414 | |||
415 | MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com>"); | ||
416 | MODULE_AUTHOR("Eric Andersson <eric.andersson@unixphere.com>"); | ||
417 | MODULE_DESCRIPTION("RMI i2c driver"); | ||
418 | MODULE_LICENSE("GPL"); | ||
419 | |||
420 | module_init(rmi_i2c_init); | ||
421 | module_exit(rmi_i2c_exit); | ||