summaryrefslogtreecommitdiff
path: root/include/linux/rmi.h
blob: 84e121d936ac128fec4fad7d032a48f15111f5cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
 * Copyright (c) 2011 Synaptics Incorporated
 * Copyright (c) 2011 Unixphere
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#ifndef _RMI_H
#define _RMI_H
#include <linux/kernel.h>
#include <linux/lockdep.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/mutex.h>
#include <linux/stat.h>
#include <linux/wait.h>
#include <linux/list.h>
#include <linux/interrupt.h>

#ifdef CONFIG_HAS_EARLYSUSPEND
#include <linux/earlysuspend.h>
#endif

#ifdef CONFIG_TOUCHSCREEN_SYN_RMI4_SPI

#define CONFIG_RMI4_BUS
#define CONFIG_RMI4_SPI
#define CONFIG_RMI4_GENERIC
#define CONFIG_RMI4_F09
#define CONFIG_RMI4_F11
#define CONFIG_RMI4_F19
#define CONFIG_RMI4_F34
#define CONFIG_RMI4_F54
#define CONFIG_RMI4_DEV

#endif

// #define SYNAPTICS_SENSOR_POLL 1

/* Permissions for sysfs attributes.  Since the permissions policy will change
 * on a global basis in the future, rather than edit all sysfs attrs everywhere
 * in the driver (and risk screwing that up in the process), we use this handy
 * set of #defines.  That way when we change the policy for sysfs permissions,
 * we only need to change them here.
 */
#define RMI_RO_ATTR S_IRUGO
#define RMI_RW_ATTR (S_IRUGO | S_IWUGO)
#define RMI_WO_ATTR S_IWUGO

#define PDT_START_SCAN_LOCATION 0x00e9

enum rmi_irq_polarity {
	RMI_IRQ_ACTIVE_LOW = 0,
	RMI_IRQ_ACTIVE_HIGH = 1
};

/**
 * struct rmi_f11_axis_alignmen - target axis alignment
 * @swap_axes: set to TRUE if desired to swap x- and y-axis
 * @flip_x: set to TRUE if desired to flip direction on x-axis
 * @flip_y: set to TRUE if desired to flip direction on y-axis
 */
struct rmi_f11_2d_axis_alignment {
	bool swap_axes;
	bool flip_x;
	bool flip_y;
	int clip_X_low;
	int clip_Y_low;
	int clip_X_high;
	int clip_Y_high;
	int offset_X;
	int offset_Y;
	int rel_report_enabled;
};

/**
 * RMI F11 - function control register parameters
 * Each register that has a specific bit-field setup has an accompanied
 * register definition so that the setting can be chosen as a one-word
 * register setting or per-bit setting.
 */
union rmi_f11_2d_ctrl0 {
	struct {
		u8 reporting_mode:3;
		u8 abs_pos_filt:1;
		u8 rel_pos_filt:1;
		u8 rel_ballistics:1;
		u8 dribble:1;
		u8 report_beyond_clip:1;
	};
	u8 reg;
};

union rmi_f11_2d_ctrl1 {
	struct {
		u8 palm_detect_thres:4;
		u8 motion_sensitivity:2;
		u8 man_track_en:1;
		u8 man_tracked_finger:1;
	};
	u8 reg;
};

union rmi_f11_2d_ctrl2__3 {
	struct {
		u8 delta_x_threshold:8;
		u8 delta_y_threshold:8;
	};
	u8 regs[2];
};

union rmi_f11_2d_ctrl4 {
	struct {
		u8 velocity:8;
	};
	u8 reg;
};

union rmi_f11_2d_ctrl5 {
	struct {
		u8 acceleration:8;
	};
	u8 reg;
};

union rmi_f11_2d_ctrl6__7 {
	struct {
		u16 sensor_max_x_pos:12;
	};
	u8 regs[2];
};

union rmi_f11_2d_ctrl8__9 {
	struct {
		u16 sensor_max_y_pos:12;
	};
	u8 regs[2];
};

union rmi_f11_2d_ctrl10 {
	struct {
		u8 single_tap_int_enable:1;
		u8 tap_n_hold_int_enable:1;
		u8 double_tap_int_enable:1;
		u8 early_tap_int_enable:1;
		u8 flick_int_enable:1;
		u8 press_int_enable:1;
		u8 pinch_int_enable:1;
	};
	u8 reg;
};

union rmi_f11_2d_ctrl11 {
	struct {
		u8 palm_detect_int_enable:1;
		u8 rotate_int_enable:1;
		u8 touch_shape_int_enable:1;
		u8 scroll_zone_int_enable:1;
		u8 multi_finger_scroll_int_enable:1;
	};
	u8 reg;
};

union rmi_f11_2d_ctrl12 {
	struct {
		u8 sensor_map:7;
		u8 xy_sel:1;
	};
	u8 reg;
};

union rmi_f11_2d_ctrl14 {
	struct {
		u8 sens_adjustment:5;
		u8 hyst_adjustment:3;
	};
	u8 reg;
};

/* The configuation is controlled as per register which means that if a register
 * is allocated for ctrl configuration one must make sure that all the bits are
 * set accordingly for that particular register.
 */
struct  rmi_f11_2d_ctrl {
	union rmi_f11_2d_ctrl0		*ctrl0;
	union rmi_f11_2d_ctrl1		*ctrl1;
	union rmi_f11_2d_ctrl2__3	*ctrl2__3;
	union rmi_f11_2d_ctrl4		*ctrl4;
	union rmi_f11_2d_ctrl5		*ctrl5;
	union rmi_f11_2d_ctrl6__7	*ctrl6__7;
	union rmi_f11_2d_ctrl8__9	*ctrl8__9;
	union rmi_f11_2d_ctrl10		*ctrl10;
	union rmi_f11_2d_ctrl11		*ctrl11;
	union rmi_f11_2d_ctrl12		*ctrl12;
	u8				ctrl12_size;
	union rmi_f11_2d_ctrl14		*ctrl14;
	u8				*ctrl15;
	u8				*ctrl16;
	u8				*ctrl17;
	u8				*ctrl18;
	u8				*ctrl19;
};

struct rmi_f19_button_map {
	unsigned char nbuttons;
	unsigned char *map;
};

struct rmi_device_platform_data_spi {
	int block_delay_us;
	int split_read_block_delay_us;
	int read_delay_us;
	int write_delay_us;
	int split_read_byte_delay_us;
	int pre_delay_us;
	int post_delay_us;

	void *cs_assert_data;
	int (*cs_assert) (const void *cs_assert_data, const bool assert);
};

struct rmi_device_platform_data {
	char *driver_name;

	int irq;
	enum rmi_irq_polarity irq_polarity;
	int (*gpio_config)(void *gpio_data, bool configure);

	struct rmi_device_platform_data_spi spi_data;

	/* function handler pdata */
	struct rmi_f11_2d_ctrl *f11_ctrl;
	struct rmi_f11_2d_axis_alignment axis_align;
	struct rmi_f19_button_map *button_map;

#ifdef	CONFIG_PM
	void *pm_data;
	int (*pre_suspend) (const void *pm_data);
	int (*post_resume) (const void *pm_data);
#endif
};

/**
 * struct rmi_function_descriptor - RMI function base addresses
 * @query_base_addr: The RMI Query base address
 * @command_base_addr: The RMI Command base address
 * @control_base_addr: The RMI Control base address
 * @data_base_addr: The RMI Data base address
 * @interrupt_source_count: The number of irqs this RMI function needs
 * @function_number: The RMI function number
 *
 * This struct is used when iterating the Page Description Table. The addresses
 * are 16-bit values to include the current page address.
 *
 */
struct rmi_function_descriptor {
	u16 query_base_addr;
	u16 command_base_addr;
	u16 control_base_addr;
	u16 data_base_addr;
	u8 interrupt_source_count;
	u8 function_number;
	u8 function_version;
};

struct rmi_function_container;
struct rmi_device;

/**
 * struct rmi_function_handler - an RMI function handler
 * @func: The RMI function number
 * @init: Callback for RMI function init
 * @attention: Callback for RMI function attention
 * @suspend: Callback for function suspend, returns 0 for success.
 * @resume: Callback for RMI function resume, returns 0 for success.
 * @remove: Callback for RMI function removal
 *
 * This struct describes the interface of an RMI function. These are
 * registered to the bus using the rmi_register_function_driver() call.
 *
 */
struct rmi_function_handler {
	int func;
	int (*init)(struct rmi_function_container *fc);
	int (*attention)(struct rmi_function_container *fc, u8 *irq_bits);
#ifdef CONFIG_PM
	int (*suspend)(struct rmi_function_container *fc);
	int (*resume)(struct rmi_function_container *fc);
#endif
	void (*remove)(struct rmi_function_container *fc);
};

/**
 * struct rmi_function_device - represent an RMI function device
 * @dev: The device created
 *
 * The RMI function device implements the "psuedo" device that represents
 * an RMI4 function like function 0x11, function 0x34, etc. and is really
 * a placeholder to be able to create sysfs attributes for each function
 * in order to facilitate communication between user code and RMI4 functions.
 *
 */
struct rmi_function_device {
	struct device dev;
};

/**
 * struct rmi_function_container - an element in a function handler list
 * @list: The list
 * @fd: The function descriptor of the RMI function
 * @rmi_dev: Pointer to the RMI device associated with this function container
 * @fh: The callbacks connected to this function
 * @num_of_irqs: The number of irqs needed by this function
 * @irq_pos: The position in the irq bitfield this function holds
 * @data: Private data pointer
 *
 */
struct rmi_function_container {
	struct list_head list;

	struct rmi_function_descriptor fd;
	struct rmi_device *rmi_dev;
	struct rmi_function_handler *fh;
	struct device dev;

	int num_of_irqs;
	int irq_pos;
	u8 *irq_mask;

	void *data;
};
#define to_rmi_function_container(d) \
		container_of(d, struct rmi_function_container, dev);
#define to_rmi_function_device(d) \
		container_of(d, struct rmi_function_device, dev);


#ifdef CONFIG_RMI4_DEV

#define RMI_CHAR_DEV_TMPBUF_SZ 128
#define RMI_REG_ADDR_PAGE_SELECT 0xFF

struct rmi_char_dev {
	/* mutex for file operation*/
	struct mutex mutex_file_op;
	/* main char dev structure */
	struct cdev main_dev;

	/* register address for RMI protocol */
	/* filp->f_pos */

	/* pointer to the corresponding phys device info for this sensor */
	/* The phys device has the pointers to read, write, etc. */
	struct rmi_phys_device *phys;
	/* reference count */
	int ref_count;
};

int rmi_char_dev_register(struct rmi_phys_device *phys);
void rmi_char_dev_unregister(struct rmi_phys_device *phys);

#endif /*CONFIG_RMI4_DEV*/



/**
 * struct rmi_driver - represents an RMI driver
 * @driver: Device driver model driver
 * @probe: Callback for device probe
 * @remove: Callback for device removal
 * @shutdown: Callback for device shutdown
 * @irq_handler: Callback for handling irqs
 * @fh_add: Callback for function handler add
 * @fh_remove: Callback for function handler remove
 * @get_func_irq_mask: Callback for calculating interrupt mask
 * @store_irq_mask: Callback for storing and replacing interrupt mask
 * @restore_irq_mask: Callback for restoring previously stored interrupt mask
 * @data: Private data pointer
 *
 * The RMI driver implements a driver on the RMI bus.
 *
 */
struct rmi_driver {
	struct device_driver driver;

	int (*probe)(struct rmi_device *rmi_dev);
	int (*remove)(struct rmi_device *rmi_dev);
	void (*shutdown)(struct rmi_device *rmi_dev);
	int (*irq_handler)(struct rmi_device *rmi_dev, int irq);
	void (*fh_add)(struct rmi_device *rmi_dev,
		       struct rmi_function_handler *fh);
	void (*fh_remove)(struct rmi_device *rmi_dev,
			  struct rmi_function_handler *fh);
	u8* (*get_func_irq_mask)(struct rmi_device *rmi_dev,
			    struct rmi_function_container *fc);
	int (*store_irq_mask)(struct rmi_device *rmi_dev, u8* new_interupts);
	int (*restore_irq_mask)(struct rmi_device *rmi_dev);
	void *data;
};
#define to_rmi_driver(d) \
	container_of(d, struct rmi_driver, driver);

/** struct rmi_phys_info - diagnostic information about the RMI physical
 * device, used in the phys sysfs file.
 * @proto String indicating the protocol being used.
 * @tx_count Number of transmit operations.
 * @tx_bytes Number of bytes transmitted.
 * @tx_errs  Number of errors encountered during transmit operations.
 * @rx_count Number of receive operations.
 * @rx_bytes Number of bytes received.
 * @rx_errs  Number of errors encountered during receive operations.
 * @att_count Number of times ATTN assertions have been handled.
 */
struct rmi_phys_info {
	char *proto;
	long tx_count;
	long tx_bytes;
	long tx_errs;
	long rx_count;
	long rx_bytes;
	long rx_errs;
	long attn_count;
};

/**
 * struct rmi_phys_device - represent an RMI physical device
 * @dev: Pointer to the communication device, e.g. i2c or spi
 * @rmi_dev: Pointer to the RMI device
 * @write: Callback for write
 * @write_block: Callback for writing a block of data
 * @read: Callback for read
 * @read_block: Callback for reading a block of data
 * @data: Private data pointer
 *
 * The RMI physical device implements the glue between different communication
 * buses such as I2C and SPI.
 *
 */
struct rmi_phys_device {
	struct device *dev;
	struct rmi_device *rmi_dev;

	int (*write)(struct rmi_phys_device *phys, u16 addr, u8 data);
	int (*write_block)(struct rmi_phys_device *phys, u16 addr, u8 *buf,
			   int len);
	int (*read)(struct rmi_phys_device *phys, u16 addr, u8 *buf);
	int (*read_block)(struct rmi_phys_device *phys, u16 addr, u8 *buf,
			  int len);

	int (*enable_device) (struct rmi_phys_device *phys);
	void (*disable_device) (struct rmi_phys_device *phys);

	void *data;

	struct rmi_phys_info info;

#ifdef CONFIG_RMI4_DEV
	/* pointer to attention char device and char device */
	struct rmi_char_dev *char_dev;
	struct class *rmi_char_device_class;
#endif /*CONFIG_RMI4_DEV*/
};

/**
 * struct rmi_device - represents an RMI device
 * @dev: The device created for the RMI bus
 * @driver: Pointer to associated driver
 * @phys: Pointer to the physical interface
 * @early_suspend_handler: Pointers to early_suspend and late_resume, if
 * configured.
 *
 * This structs represent an RMI device.
 *
 */
struct rmi_device {
	struct device dev;

	struct rmi_driver *driver;
	struct rmi_phys_device *phys;

#ifdef CONFIG_HAS_EARLYSUSPEND
	struct early_suspend early_suspend_handler;
#endif
};
#define to_rmi_device(d) container_of(d, struct rmi_device, dev);
#define to_rmi_platform_data(d) ((d)->phys->dev->platform_data);

static inline void rmi_set_driverdata(struct rmi_device *d, void *data)
{
	dev_set_drvdata(&d->dev, data);
}

static inline void *rmi_get_driverdata(struct rmi_device *d)
{
	return dev_get_drvdata(&d->dev);
}

/**
 * rmi_read - RMI read byte
 * @d: Pointer to an RMI device
 * @addr: The address to read from
 * @buf: The read buffer
 *
 * Reads a byte of data using the underlaying physical protocol in to buf. It
 * returns zero or a negative error code.
 */
static inline int rmi_read(struct rmi_device *d, u16 addr, u8 *buf)
{
	return d->phys->read(d->phys, addr, buf);
}

/**
 * rmi_read_block - RMI read block
 * @d: Pointer to an RMI device
 * @addr: The start address to read from
 * @buf: The read buffer
 * @len: Length of the read buffer
 *
 * Reads a block of byte data using the underlaying physical protocol in to buf.
 * It returns the amount of bytes read or a negative error code.
 */
static inline int rmi_read_block(struct rmi_device *d, u16 addr, u8 *buf,
				 int len)
{
	return d->phys->read_block(d->phys, addr, buf, len);
}

/**
 * rmi_write - RMI write byte
 * @d: Pointer to an RMI device
 * @addr: The address to write to
 * @data: The data to write
 *
 * Writes a byte from buf using the underlaying physical protocol. It
 * returns zero or a negative error code.
 */
static inline int rmi_write(struct rmi_device *d, u16 addr, u8 data)
{
	return d->phys->write(d->phys, addr, data);
}

/**
 * rmi_write_block - RMI write block
 * @d: Pointer to an RMI device
 * @addr: The start address to write to
 * @buf: The write buffer
 * @len: Length of the write buffer
 *
 * Writes a block of byte data from buf using the underlaying physical protocol.
 * It returns the amount of bytes written or a negative error code.
 */
static inline int rmi_write_block(struct rmi_device *d, u16 addr, u8 *buf,
				  int len)
{
	return d->phys->write_block(d->phys, addr, buf, len);
}

/**
 * rmi_register_driver - register rmi driver
 * @driver: the driver to register
 *
 * This function registers an RMI driver to the RMI bus.
 */
int rmi_register_driver(struct rmi_driver *driver);

/**
 * rmi_unregister_driver - unregister rmi driver
 * @driver: the driver to unregister
 *
 * This function unregisters an RMI driver to the RMI bus.
 */
void rmi_unregister_driver(struct rmi_driver *driver);

/**
 * rmi_register_phys_device - register a physical device connection
 * @phys: the physical driver to register
 *
 * This function registers a physical driver to the RMI bus. These drivers
 * provide a communication layer for the drivers connected to the bus, e.g.
 * I2C, SPI and so on.
 */
int rmi_register_phys_device(struct rmi_phys_device *phys);

/**
 * rmi_unregister_phys_device - unregister a physical device connection
 * @phys: the physical driver to unregister
 *
 * This function unregisters a physical driver from the RMI bus.
 */
void rmi_unregister_phys_device(struct rmi_phys_device *phys);

/**
 * rmi_register_function_driver - register an RMI function driver
 * @fh: the function handler to register
 *
 * This function registers support for a new RMI function to the bus. All
 * drivers on the bus will be notified of the presence of the new function
 * driver.
 */
int rmi_register_function_driver(struct rmi_function_handler *fh);

/**
 * rmi_unregister_function_driver - unregister an RMI function driver
 * @fh: the function handler to unregister
 *
 * This function unregisters a RMI function from the RMI bus. All drivers on
 * the bus will be notified of the removal of a function driver.
 */
void rmi_unregister_function_driver(struct rmi_function_handler *fh);

/**
 * rmi_get_function_handler - get a pointer to specified RMI function
 * @id: the RMI function id
 *
 * This function gets the specified RMI function handler from the list of
 * supported functions.
 */
struct rmi_function_handler *rmi_get_function_handler(int id);

/* Utility routine to handle writes to read-only attributes.  Hopefully
 * this will never happen, but if the user does something stupid, we
 * don't want to accept it quietly.
 */
ssize_t rmi_store_error(struct device *dev,
			struct device_attribute *attr,
			const char *buf, size_t count);

/* Utility routine to handle reads to write-only attributes.  Hopefully
 * this will never happen, but if the user does something stupid, we
 * don't want to accept it quietly.
 */
ssize_t rmi_show_error(struct device *dev,
		       struct device_attribute *attr,
		       char *buf);

/* utility function for bit access of u8*'s */
void u8_set_bit(u8 *target, int pos);
void u8_clear_bit(u8 *target, int pos);
bool u8_is_set(u8 *target, int pos);
bool u8_is_any_set(u8 *target, int size);
void u8_or(u8 *dest, u8* target1, u8* target2, int size);
void u8_and(u8 *dest, u8* target1, u8* target2, int size);
#endif