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