blob: da8a93decd9ec8f003886c5c49b319a207ac8c1b [file] [log] [blame]
Taniya Das6f0884b2011-09-06 16:24:21 +05301/**
2 * Synaptics Register Mapped Interface (RMI4) - RMI Sensor Module.
3 * Copyright (C) 2007 - 2011, Synaptics Incorporated
4 *
5 */
6/*
7 * This file is licensed under the GPL2 license.
8 *
9 *############################################################################
10 * GPL
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published
14 * by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 *############################################################################
22 */
23
24static const char sensorname[] = "sensor";
25
26#include <linux/kernel.h>
27#include <linux/gpio.h>
28#include <linux/list.h>
29#include <linux/device.h>
30#include <linux/hrtimer.h>
31#include <linux/miscdevice.h>
32#include <linux/fs.h>
33#include <linux/delay.h>
34#include <linux/uaccess.h>
35#include <linux/slab.h>
36#include <linux/kthread.h>
37#include <linux/freezer.h>
38#include <linux/input.h>
39#include <linux/interrupt.h>
40
41
42#include "rmi_drvr.h"
43#include "rmi_bus.h"
44#include "rmi_function.h"
45#include "rmi_sensor.h"
46
47long polltime = 25000000; /* Shared with rmi_function.c. */
48EXPORT_SYMBOL(polltime);
49module_param(polltime, long, 0644);
50MODULE_PARM_DESC(polltime, "How long to wait between polls (in nano seconds).");
51
52
53#define PDT_START_SCAN_LOCATION 0x00E9
54#define PDT_END_SCAN_LOCATION 0x0005
55#define PDT_ENTRY_SIZE 0x0006
56
57static DEFINE_MUTEX(rfi_mutex);
58
59struct rmi_functions *rmi_find_function(int functionNum);
60
61int rmi_read(struct rmi_sensor_driver *sensor, unsigned short address,
62 char *dest)
63{
64 struct rmi_phys_driver *rpd = sensor->rpd;
65 if (!rpd)
66 return -ENODEV;
67 return rpd->read(rpd, address, dest);
68}
69EXPORT_SYMBOL(rmi_read);
70
71int rmi_write(struct rmi_sensor_driver *sensor, unsigned short address,
72 unsigned char data)
73{
74 struct rmi_phys_driver *rpd = sensor->rpd;
75 if (!rpd)
76 return -ENODEV;
77 return rpd->write(rpd, address, data);
78}
79EXPORT_SYMBOL(rmi_write);
80
81int rmi_read_multiple(struct rmi_sensor_driver *sensor,
82 unsigned short address, char *dest, int length)
83{
84 struct rmi_phys_driver *rpd = sensor->rpd;
85 if (!rpd)
86 return -ENODEV;
87 return rpd->read_multiple(rpd, address, dest, length);
88}
89EXPORT_SYMBOL(rmi_read_multiple);
90
91int rmi_write_multiple(struct rmi_sensor_driver *sensor,
92 unsigned short address, unsigned char *data, int length)
93{
94 struct rmi_phys_driver *rpd = sensor->rpd;
95 if (!rpd)
96 return -ENODEV;
97 return rpd->write_multiple(rpd, address, data, length);
98}
99EXPORT_SYMBOL(rmi_write_multiple);
100
101/* Utility routine to set bits in a register. */
102int rmi_set_bits(struct rmi_sensor_driver *sensor, unsigned short address,
103 unsigned char bits)
104{
105 unsigned char reg_contents;
106 int retval;
107
108 retval = rmi_read(sensor, address, &reg_contents);
109 if (retval)
110 return retval;
111 reg_contents = reg_contents | bits;
112 retval = rmi_write(sensor, address, reg_contents);
113 if (retval == 1)
114 return 0;
115 else if (retval == 0)
116 return -EINVAL; /* TODO: What should this be? */
117 else
118 return retval;
119}
120EXPORT_SYMBOL(rmi_set_bits);
121
122/* Utility routine to clear bits in a register. */
123int rmi_clear_bits(struct rmi_sensor_driver *sensor,
124 unsigned short address, unsigned char bits)
125{
126 unsigned char reg_contents;
127 int retval;
128
129 retval = rmi_read(sensor, address, &reg_contents);
130 if (retval)
131 return retval;
132 reg_contents = reg_contents & ~bits;
133 retval = rmi_write(sensor, address, reg_contents);
134 if (retval == 1)
135 return 0;
136 else if (retval == 0)
137 return -EINVAL; /* TODO: What should this be? */
138 else
139 return retval;
140}
141EXPORT_SYMBOL(rmi_clear_bits);
142
143/* Utility routine to set the value of a bit field in a register. */
144int rmi_set_bit_field(struct rmi_sensor_driver *sensor,
145 unsigned short address, unsigned char field_mask, unsigned char bits)
146{
147 unsigned char reg_contents;
148 int retval;
149
150 retval = rmi_read(sensor, address, &reg_contents);
151 if (retval)
152 return retval;
153 reg_contents = (reg_contents & ~field_mask) | bits;
154 retval = rmi_write(sensor, address, reg_contents);
155 if (retval == 1)
156 return 0;
157 else if (retval == 0)
158 return -EINVAL; /* TODO: What should this be? */
159 else
160 return retval;
161}
162EXPORT_SYMBOL(rmi_set_bit_field);
163
164bool rmi_polling_required(struct rmi_sensor_driver *sensor)
165{
166 return sensor->polling_required;
167}
168EXPORT_SYMBOL(rmi_polling_required);
169
170/** Functions can call this in order to dispatch IRQs. */
171void dispatchIRQs(struct rmi_sensor_driver *sensor, unsigned int irqStatus)
172{
173 struct rmi_function_info *functionInfo;
174
175 list_for_each_entry(functionInfo, &sensor->functions, link) {
176 if ((functionInfo->interruptMask & irqStatus)) {
177 if (functionInfo->function_device->
178 rmi_funcs->inthandler) {
179 /* Call the functions interrupt handler function. */
180 functionInfo->function_device->rmi_funcs->
181 inthandler(functionInfo,
182 (functionInfo->interruptMask & irqStatus));
183 }
184 }
185 }
186}
187
188/**
189 * This is the function we pass to the RMI4 subsystem so we can be notified
190 * when attention is required. It may be called in interrupt context.
191 */
192static void attention(struct rmi_phys_driver *physdrvr, int instance)
193{
194 /* All we have to do is schedule work. */
195
196 /* TODO: It's possible that workIsReady is not really needed anymore.
197 * Investigate this to see if the race condition between setting up
198 * the work and enabling the interrupt still exists.
199 */
200 if (physdrvr->sensor->workIsReady) {
201 schedule_work(&(physdrvr->sensor->work));
202 } else {
203 /* Got an interrupt but we're not ready so enable the irq
204 * so it doesn't get hung up
205 */
206 printk(KERN_DEBUG "%s: Work not initialized yet -"
207 "enabling irqs.\n", __func__);
208 enable_irq(physdrvr->irq);
209 }
210}
211
212/**
213 * This notifies any interested functions that there
214 * is an Attention interrupt. The interested functions should take
215 * appropriate
216 * actions (such as reading the interrupt status register and dispatching any
217 * appropriate RMI4 interrupts).
218 */
219void attn_notify(struct rmi_sensor_driver *sensor)
220{
221 struct rmi_function_info *functionInfo;
222
223 /* check each function that has data sources and if the interrupt for
224 * that triggered then call that RMI4 functions report() function to
225 * gather data and report it to the input subsystem
226 */
227 list_for_each_entry(functionInfo, &sensor->functions, link) {
228 if (functionInfo->function_device &&
229 functionInfo->function_device->rmi_funcs->attention)
230 functionInfo->function_device->
231 rmi_funcs->attention(functionInfo);
232 }
233}
234
235/* This is the worker function - for now it simply has to call attn_notify.
236 * This work should be scheduled whenever an ATTN interrupt is asserted by
237 * the touch sensor.
238 * We then call attn_notify to dispatch notification of the ATTN interrupt
239 * to all
240 * interested functions. After all the attention handling functions
241 * have returned, it is presumed safe to re-enable the Attention interrupt.
242 */
243static void sensor_work_func(struct work_struct *work)
244{
245 struct rmi_sensor_driver *sensor = container_of(work,
246 struct rmi_sensor_driver, work);
247
248 attn_notify(sensor);
249
250 /* we only need to enable the irq if doing interrupts */
251 if (!rmi_polling_required(sensor))
252 enable_irq(sensor->rpd->irq);
253}
254
255/* This is the timer function for polling - it simply has to schedule work
256 * and restart the timer. */
257static enum hrtimer_restart sensor_poll_timer_func(struct hrtimer *timer)
258{
259 struct rmi_sensor_driver *sensor = container_of(timer,
260 struct rmi_sensor_driver, timer);
261
262 schedule_work(&sensor->work);
263 hrtimer_start(&sensor->timer, ktime_set(0, polltime),
264 HRTIMER_MODE_REL);
265 return HRTIMER_NORESTART;
266}
267
268/* This is the probe function passed to the RMI4 subsystem that gives us a
269 * chance to recognize an RMI4 device. In this case, we're looking for
270 * Synaptics devices that have data sources - such as touch screens, buttons,
271 * etc.
272 *
273 * TODO: Well, it used to do this. I'm not sure it's required any more.
274 */
275static int probe(struct rmi_sensor_driver *sensor)
276{
277 struct rmi_phys_driver *rpd;
278
279 rpd = sensor->rpd;
280
281 if (!rpd) {
282 printk(KERN_ERR "%s: Invalid rmi physical driver - null ptr:"
283 "%p\n", __func__, rpd);
284 return 0;
285 }
286
287 return 1;
288}
289
290static void config(struct rmi_sensor_driver *sensor)
291{
292 /* For each data source we had detected print info and set up interrupts
293 or polling. */
294 struct rmi_function_info *functionInfo;
295 struct rmi_phys_driver *rpd;
296
297 rpd = sensor->rpd; /* get ptr to rmi_physical_driver from app */
298
299 list_for_each_entry(functionInfo, &sensor->functions, link) {
300 /* Get and print some info about the data sources... */
301 struct rmi_functions *fn;
302 bool found = false;
303 /* check if function number matches - if so call that
304 config function */
305 fn = rmi_find_function(functionInfo->functionNum);
306 if (fn) {
307 found = true;
308
309 if (fn->config) {
310 fn->config(functionInfo);
311 } else {
312 /* the developer did not add in the
313 pointer to the config function into
314 rmi4_supported_data_src_functions */
315 printk(KERN_ERR
316 "%s: no config function for "
317 "function 0x%x\n",
318 __func__, functionInfo->functionNum);
319 break;
320 }
321 }
322
323 if (!found) {
324 /* if no support found for this RMI4 function
325 it means the developer did not add the
326 appropriate function pointer list into the
327 rmi4_supported_data_src_functions array and/or
328 did not bump up the number of supported RMI4
329 functions in rmi.h as required */
330 printk(KERN_ERR "%s: could not find support "
331 "for function 0x%x\n",
332 __func__, functionInfo->functionNum);
333 }
334 }
335
336 /* This will handle interrupts on the ATTN line (interrupt driven)
337 * or will be called every poll interval (when we're not interrupt
338 * driven).
339 */
340 INIT_WORK(&sensor->work, sensor_work_func);
341 sensor->workIsReady = true;
342
343 if (rmi_polling_required(sensor)) {
344 /* We're polling driven, so set up the polling timer
345 and timer function. */
346 hrtimer_init(&sensor->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
347 sensor->timer.function = sensor_poll_timer_func;
348 hrtimer_start(&sensor->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
349 }
350}
351
352/** Just a stub for now.
353 */
354static int rmi_sensor_suspend(struct device *dev, pm_message_t state)
355{
356 printk(KERN_INFO "%s: sensor suspend called.", __func__);
357 return 0;
358}
359
360/** Just a stub for now.
361 */
362static int rmi_sensor_resume(struct device *dev)
363{
364 printk(KERN_INFO "%s: sensor resume called.", __func__);
365 return 0;
366}
367
368/*
369 * This method is called, whenever a new sensor device is added for the rmi
370 * bus.
371 *
372 * It will scan the devices PDT to determine the supported functions
373 * and create a new function device for each of these. It will read
374 * the query, control, command and data regsiters for the function
375 * to be used for each newly created function device.
376 *
377 * The sensor device is then bound to every function it supports.
378 *
379 */
380int rmi_sensor_register_functions(struct rmi_sensor_driver *sensor)
381{
382 struct rmi_function_device *function;
383 unsigned int interruptRegisterCount;
384 struct rmi_phys_driver *rpd;
385 int i;
386 unsigned char interruptCount;
387 struct rmi_function_info *functionInfo;
388 struct rmi_function_descriptor rmi_fd;
389 struct rmi_functions *fn;
390 int retval;
391
392 pr_debug("%s: Registering sensor functions\n", __func__);
393
394 retval = 0;
395
396 /* Scan device for functions that may be supported */
397 {
398 pr_debug("%s: Scanning sensor for Functions:\n", __func__);
399
400 interruptCount = 0;
401 rpd = sensor->rpd;
402
403 /* Read the Page Descriptor Table to determine what functions
404 * are present */
405
406 printk(KERN_DEBUG "%s: Scanning page descriptors.", __func__);
407 for (i = PDT_START_SCAN_LOCATION;
408 i >= PDT_END_SCAN_LOCATION;
409 i -= PDT_ENTRY_SIZE) {
410 printk(KERN_DEBUG "%s: Reading page descriptor 0x%02x", __func__, i);
411 retval = rpd->read_multiple(rpd, i, (char *)&rmi_fd,
412 sizeof(rmi_fd));
413 if (!retval) {
414 functionInfo = NULL;
415
416 if (rmi_fd.functionNum != 0x00 && rmi_fd.functionNum != 0xff) {
417 printk(KERN_DEBUG "%s: F%02x - queries %02x commands %02x control %02x data %02x ints %02x", __func__, rmi_fd.functionNum, rmi_fd.queryBaseAddr, rmi_fd.commandBaseAddr, rmi_fd.controlBaseAddr, rmi_fd.dataBaseAddr, rmi_fd.interruptSrcCnt);
418
419 if ((rmi_fd.functionNum & 0xff) == 0x01)
420 printk(KERN_DEBUG "%s: Fn $01 Found - RMI Device Control", __func__);
421
422 /* determine if the function is supported and if so
423 * then bind this function device to the sensor */
424 if (rmi_fd.interruptSrcCnt) {
425 functionInfo = kzalloc(sizeof(*functionInfo), GFP_KERNEL);
426 if (!functionInfo) {
427 printk(KERN_ERR "%s: could not allocate memory for function 0x%x.",
428 __func__, rmi_fd.functionNum);
429 retval = -ENOMEM;
430 goto exit_fail;
431 }
432 functionInfo->sensor = sensor;
433 functionInfo->functionNum = (rmi_fd.functionNum & 0xff);
434 INIT_LIST_HEAD(&functionInfo->link);
435 /* Get the ptr to the detect function based on
436 * the function number */
437 printk(KERN_DEBUG "%s: Checking for RMI function F%02x.", __func__, rmi_fd.functionNum);
438 fn = rmi_find_function(rmi_fd.functionNum);
439 if (fn) {
440 retval = fn->detect(functionInfo, &rmi_fd,
441 interruptCount);
442 if (retval)
443 printk(KERN_ERR "%s: Function detect for F%02x failed with %d.",
444 __func__, rmi_fd.functionNum, retval);
445
446 /* Create a function device and function driver for this Fn */
447 function = kzalloc(sizeof(*function), GFP_KERNEL);
448 if (!function) {
449 printk(KERN_ERR "%s: Error allocating memory for rmi_function_device.", __func__);
450 return -ENOMEM;
451 }
452
453 function->dev.parent = &sensor->sensor_device->dev;
454 function->dev.bus = sensor->sensor_device->dev.bus;
455 function->rmi_funcs = fn;
456 function->sensor = sensor;
457 function->rfi = functionInfo;
458 functionInfo->function_device = function;
459
460 /* Check if we have an interrupt mask of 0 and a non-NULL interrupt
461 handler function and print a debug message since we should never
462 have this.
463 */
464 if (functionInfo->interruptMask == 0 && fn->inthandler != NULL) {
465 printk(KERN_DEBUG "%s: Can't have a zero interrupt mask for function F%02x (which requires an interrupt handler).\n",
466 __func__, rmi_fd.functionNum);
467 }
468
469
470 /* Check if we have a non-zero interrupt mask and a NULL interrupt
471 handler function and print a debug message since we should never
472 have this.
473 */
474 if (functionInfo->interruptMask != 0 && fn->inthandler == NULL) {
475 printk(KERN_DEBUG "%s: Can't have a non-zero interrupt mask %d for function F%02x with a NULL inthandler fn.\n",
476 __func__, functionInfo->interruptMask, rmi_fd.functionNum);
477 }
478
479 /* Register the rmi function device */
480 retval = rmi_function_register_device(function, rmi_fd.functionNum);
481 if (retval) {
482 printk(KERN_ERR "%s: Failed rmi_function_register_device.\n",
483 __func__);
484 return retval;
485 }
486 } else {
487 printk(KERN_ERR "%s: could not find support for function 0x%02X.\n",
488 __func__, rmi_fd.functionNum);
489 }
490 } else {
491 printk(KERN_DEBUG "%s: Found function F%02x - Ignored.\n", __func__, rmi_fd.functionNum & 0xff);
492 }
493
494 /* bump interrupt count for next iteration */
495 /* NOTE: The value 7 is reserved - for now, only bump up one for an interrupt count of 7 */
496 if ((rmi_fd.interruptSrcCnt & 0x7) == 0x7) {
497 interruptCount += 1;
498 } else {
499 interruptCount +=
500 (rmi_fd.interruptSrcCnt & 0x7);
501 }
502
503 /* link this function info to the RMI module infos list
504 of functions */
505 if (functionInfo == NULL) {
506 printk(KERN_DEBUG "%s: WTF? functionInfo is null here.", __func__);
507 } else {
508 printk(KERN_DEBUG "%s: Adding function F%02x with %d sources.\n",
509 __func__, functionInfo->functionNum, functionInfo->numSources);
510
511 mutex_lock(&rfi_mutex);
512 list_add_tail(&functionInfo->link,
513 &sensor->functions);
514 mutex_unlock(&rfi_mutex);
515 }
516
517 } else {
518 /* A zero or 0xff in the function number
519 signals the end of the PDT */
520 printk(KERN_DEBUG "%s: Found End of PDT\n",
521 __func__);
522 break;
523 }
524 } else {
525 /* failed to read next PDT entry - end PDT
526 scan - this may result in an incomplete set
527 of recognized functions - should probably
528 return an error but the driver may still be
529 viable for diagnostics and debugging so let's
530 let it continue. */
531 printk(KERN_ERR "%s: Read Error %d when reading next PDT entry - "
532 "ending PDT scan.\n",
533 __func__, retval);
534 break;
535 }
536 }
537 printk(KERN_DEBUG "%s: Done scanning.", __func__);
538
539 /* calculate the interrupt register count - used in the
540 ISR to read the correct number of interrupt registers */
541 interruptRegisterCount = (interruptCount + 7) / 8;
542 sensor->interruptRegisterCount = interruptRegisterCount; /* TODO: Is this needed by the sensor anymore? */
543 }
544
545 return 0;
546
547exit_fail:
548 return retval;
549}
550EXPORT_SYMBOL(rmi_sensor_register_functions);
551
552int rmi_sensor_register_device(struct rmi_sensor_device *dev, int index)
553{
554 int status;
555
556 printk(KERN_INFO "%s: Registering sensor device.\n", __func__);
557
558 /* make name - sensor00, sensor01, etc. */
559 dev_set_name(&dev->dev, "sensor%02d", index);
560 status = device_register(&dev->dev);
561
562 return status;
563}
564EXPORT_SYMBOL(rmi_sensor_register_device);
565
566static void rmi_sensor_unregister_device(struct rmi_sensor_device *rmisensordev)
567{
568 printk(KERN_INFO "%s: Unregistering sensor device.\n", __func__);
569
570 device_unregister(&rmisensordev->dev);
571}
572EXPORT_SYMBOL(rmi_sensor_unregister_device);
573
574int rmi_sensor_register_driver(struct rmi_sensor_driver *driver)
575{
576 static int index;
577 int ret;
578 char *drvrname;
579
580 driver->workIsReady = false;
581
582 printk(KERN_INFO "%s: Registering sensor driver.\n", __func__);
583 driver->dispatchIRQs = dispatchIRQs;
584 driver->attention = attention;
585 driver->config = config;
586 driver->probe = probe;
587
588 /* assign the bus type for this driver to be rmi bus */
589 driver->drv.bus = &rmi_bus_type;
590 driver->drv.suspend = rmi_sensor_suspend;
591 driver->drv.resume = rmi_sensor_resume;
592 /* Create a function device and function driver for this Fn */
593 drvrname = kzalloc(sizeof(sensorname) + 4, GFP_KERNEL);
594 if (!drvrname) {
595 printk(KERN_ERR "%s: Error allocating memeory for rmi_sensor_driver name.\n", __func__);
596 return -ENOMEM;
597 }
598 sprintf(drvrname, "sensor%02d", index++);
599
600 driver->drv.name = drvrname;
601 driver->module = driver->drv.owner;
602
603 /* register the sensor driver */
604 ret = driver_register(&driver->drv);
605 if (ret) {
606 printk(KERN_ERR "%s: Failed driver_register %d\n",
607 __func__, ret);
608 goto exit_fail;
609 }
610
611 /* register the functions on the sensor */
612 ret = rmi_sensor_register_functions(driver);
613 if (ret) {
614 printk(KERN_ERR "%s: Failed rmi_sensor_register_functions %d\n",
615 __func__, ret);
616 }
617
618 /* configure the sensor - enable interrupts for each function, init work, set polling timer or adjust report rate, etc. */
619 config(driver);
620
621 printk(KERN_DEBUG "%s: sensor driver registration completed.", __func__);
622
623exit_fail:
624 return ret;
625}
626EXPORT_SYMBOL(rmi_sensor_register_driver);
627
628static void rmi_sensor_unregister_driver(struct rmi_sensor_driver *driver)
629{
630 printk(KERN_DEBUG "%s: Unregistering sensor driver.\n", __func__);
631
632 /* Stop the polling timer if doing polling */
633 if (rmi_polling_required(driver))
634 hrtimer_cancel(&driver->timer);
635
636 flush_scheduled_work(); /* Make sure all scheduled work is stopped */
637
638 driver_unregister(&driver->drv);
639}
640EXPORT_SYMBOL(rmi_sensor_unregister_driver);
641
642
643static int __init rmi_sensor_init(void)
644{
645 printk(KERN_DEBUG "%s: RMI Sensor Init\n", __func__);
646 return 0;
647}
648
649static void __exit rmi_sensor_exit(void)
650{
651 printk(KERN_DEBUG "%s: RMI Sensor Driver Exit\n", __func__);
652 flush_scheduled_work(); /* Make sure all scheduled work is stopped */
653}
654
655
656module_init(rmi_sensor_init);
657module_exit(rmi_sensor_exit);
658
659MODULE_AUTHOR("Synaptics, Inc.");
660MODULE_DESCRIPTION("RMI4 Sensor Driver");
661MODULE_LICENSE("GPL");