summaryrefslogtreecommitdiff
path: root/drivers/input/misc/stmp3xxx_rotdec.c
blob: 621db95dba8cfd34c9159931138786aa713cc428 (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
/*
 * Freescale STMP3XXX Rotary Encoder Driver
 *
 * Author: Drew Benedetti <drewb@embeddedalley.com>
 *
 * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/input-polldev.h>
#include <mach/regs-timrot.h>
#include <mach/rotdec.h>
#include <mach/platform.h>

static int relative;
static unsigned int poll_interval = 500;

void stmp3xxx_rotdec_flush(struct input_polled_dev *dev)
{
	/* in relative mode, reading the counter resets it */
	if (relative)
		__raw_readl(REGS_TIMROT_BASE + HW_TIMROT_ROTCOUNT);
}

void stmp3xxx_rotdec_poll(struct input_polled_dev *dev)
{
	s16 cnt = __raw_readl(REGS_TIMROT_BASE + HW_TIMROT_ROTCOUNT) & BM_TIMROT_ROTCOUNT_UPDOWN;
	if (relative)
		input_report_rel(dev->input, REL_WHEEL, cnt);
	else
		input_report_abs(dev->input, ABS_WHEEL, cnt);
}

struct input_polled_dev *rotdec;
static u32 rotctrl;

static int stmp3xxx_rotdec_probe(struct platform_device *pdev)
{
	int rc = 0;

	/* save original state of HW_TIMROT_ROTCTRL */
	rotctrl = __raw_readl(REGS_TIMROT_BASE + HW_TIMROT_ROTCTRL);

	if (!(rotctrl & BM_TIMROT_ROTCTRL_ROTARY_PRESENT)) {
		dev_info(&pdev->dev, "No rotary decoder present\n");
		rc = -ENODEV;
		goto err_rotdec_present;
	} else {
		/* I had to add some extra line breaks in here
		 * to avoid lines >80 chars wide
		 */
		__raw_writel(
		 BF(0x0, TIMROT_ROTCTRL_DIVIDER) | /* 32kHz divider - 1 */
		 BF(BV_TIMROT_ROTCTRL_OVERSAMPLE__2X,
			TIMROT_ROTCTRL_OVERSAMPLE) |
		 BF(BV_TIMROT_ROTCTRL_SELECT_B__ROTARYB,
			TIMROT_ROTCTRL_SELECT_B) |
		 BF(BV_TIMROT_ROTCTRL_SELECT_A__ROTARYA,
			TIMROT_ROTCTRL_SELECT_A)
		, REGS_TIMROT_BASE + HW_TIMROT_ROTCTRL);
		__raw_writel(
		 BM_TIMROT_ROTCTRL_POLARITY_B |
		 BM_TIMROT_ROTCTRL_POLARITY_A
		, REGS_TIMROT_BASE + HW_TIMROT_ROTCTRL_CLR);

		if (relative)
			__raw_writel(BM_TIMROT_ROTCTRL_RELATIVE,
				REGS_TIMROT_BASE + HW_TIMROT_ROTCTRL_SET);
		else
			__raw_writel(BM_TIMROT_ROTCTRL_RELATIVE,
				REGS_TIMROT_BASE + HW_TIMROT_ROTCTRL_CLR);

		rc = rotdec_pinmux_request();
		if (rc) {
			dev_err(&pdev->dev,
				"Pin request failed (err=%d)\n", rc);
			goto err_pinmux;
		}

		/* set up input_polled_dev */
		rotdec = input_allocate_polled_device();
		if (!rotdec) {
			dev_err(&pdev->dev,
				"Unable to allocate polled device\n");
			rc = -ENOMEM;
			goto err_alloc_polldev;
		}
		rotdec->flush = stmp3xxx_rotdec_flush;
		rotdec->poll = stmp3xxx_rotdec_poll;
		rotdec->poll_interval = poll_interval; /* msec */

		rotdec->input->name = "stmp3xxx-rotdec";
		if (relative)
			input_set_capability(rotdec->input, EV_REL, REL_WHEEL);
		else {
			input_set_capability(rotdec->input, EV_ABS, ABS_WHEEL);
			input_set_abs_params(rotdec->input, ABS_WHEEL,
					-32768, 32767, 0, 0);
		}

		rc = input_register_polled_device(rotdec);
		if (rc) {
			dev_err(&pdev->dev,
				"Unable to register rotary decoder (err=%d)\n",
				rc);
			goto err_reg_polldev;
		}
	}

	return 0;

err_reg_polldev:
	input_free_polled_device(rotdec);
err_alloc_polldev:
	rotdec_pinmux_free();
err_pinmux:
	/* restore original register state */
	__raw_writel(rotctrl, REGS_TIMROT_BASE + HW_TIMROT_ROTCTRL);

err_rotdec_present:
	return rc;
}

static int stmp3xxx_rotdec_remove(struct platform_device *pdev)
{
	input_unregister_polled_device(rotdec);
	input_free_polled_device(rotdec);

	rotdec_pinmux_free();

	/* restore original register state */
	__raw_writel(rotctrl, REGS_TIMROT_BASE + HW_TIMROT_ROTCTRL);

	return 0;
}

static struct platform_driver stmp3xxx_rotdec_driver = {
	.probe	= stmp3xxx_rotdec_probe,
	.remove	= stmp3xxx_rotdec_remove,
	.driver	= {
		.name	= "stmp3xxx-rotdec",
	},
};

static int __init stmp3xxx_rotdec_init(void)
{
	return platform_driver_register(&stmp3xxx_rotdec_driver);
}

static void __exit stmp3xxx_rotdec_exit(void)
{
	platform_driver_unregister(&stmp3xxx_rotdec_driver);
}

module_init(stmp3xxx_rotdec_init);
module_exit(stmp3xxx_rotdec_exit);

module_param(relative, bool, 0600);
module_param(poll_interval, uint, 0600);

MODULE_AUTHOR("Drew Benedetti <drewb@embeddedalley.com>");
MODULE_DESCRIPTION("STMP3xxx rotary decoder driver");
MODULE_LICENSE("GPL");