summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/rmi4/rmi_f09.c
blob: 0ec980d7db074dc7fb34b243bd66e97b40ccc658 (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
/*
 * 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.
 */
#include <linux/kernel.h>
#include <linux/rmi.h>
#include <linux/input.h>
#include <linux/slab.h>

#define QUERY_BASE_INDEX 1
#define MAX_LEN 256

/* data specific to fn $09 that needs to be kept around */
struct f09_query {
	u8 Limit_Register_Count;
	union {
		struct {
			u8 Result_Register_Count:3;
			u8 Reserved:3;
			u8 InternalLimits:1;
			u8 HostTestEn:1;
		};
		u8 f09_bist_query1;
	};
};

struct f09_control {
	/* test1 */
	u8 Test1LimitLo;
	u8 Test1LimitHi;
	u8 Test1LimitDiff;
	/* test2 */
	u8 Test2LimitLo;
	u8 Test2LimitHi;
	u8 Test2LimitDiff;
};

struct f09_data {
	u8 TestNumberControl;
	u8 Overall_BIST_Result;
	u8 TestResult1;
	u8 TestResult2;
	u8 Transmitter_Number;

	union {
		struct {
			u8 Receiver_Number:6;
			u8 Limit_Failure_Code:2;
		};
		u8 f09_bist_data2;
	};
};

struct f09_cmd {
	union {
		struct {
			u8 RunBIST:1;
		};
		u8 f09_bist_cmd0;
	};
};

struct rmi_fn_09_data {
	struct f09_query query;
};

static ssize_t rmi_f09_Limit_Register_Count_show(struct device *dev,
				      struct device_attribute *attr, char *buf);

static ssize_t rmi_f09_HostTestEn_show(struct device *dev,
				      struct device_attribute *attr, char *buf);

static ssize_t rmi_f09_HostTestEn_store(struct device *dev,
				      struct device_attribute *attr,
					char *buf, size_t count);

static ssize_t rmi_f09_InternalLimits_show(struct device *dev,
				      struct device_attribute *attr, char *buf);

static ssize_t rmi_f09_Result_Register_Count_show(struct device *dev,
				      struct device_attribute *attr, char *buf);

static ssize_t rmi_f09_Overall_BIST_Result_show(struct device *dev,
				      struct device_attribute *attr, char *buf);

static ssize_t rmi_f09_Overall_BIST_Result_store(struct device *dev,
				       struct device_attribute *attr,
				       const char *buf, size_t count);

static struct device_attribute attrs[] = {
	__ATTR(Limit_Register_Count, RMI_RO_ATTR,
	       rmi_f09_Limit_Register_Count_show, rmi_store_error),
	__ATTR(HostTestEn, RMI_RW_ATTR,
	       rmi_f09_HostTestEn_show, rmi_f09_HostTestEn_store),
	__ATTR(InternalLimits, RMI_RO_ATTR,
	       rmi_f09_Limit_Register_Count_show, rmi_store_error),
	__ATTR(Result_Register_Count, RMI_RO_ATTR,
	       rmi_f09_Result_Register_Count_show, rmi_store_error),
};

static int rmi_f09_init(struct rmi_function_container *fc)
{
	struct rmi_device *rmi_dev = fc->rmi_dev;
	struct rmi_device_platform_data *pdata;
	struct rmi_fn_09_data  *f09;
	u8 query_base_addr;
	int rc;
	int i;
	int attr_count = 0;
	int retval = 0;

	dev_info(&fc->dev, "Intializing F09 values.");

	f09 = kzalloc(sizeof(struct rmi_fn_09_data), GFP_KERNEL);
	if (!f09) {
		dev_err(&fc->dev, "Failed to allocate rmi_fn_09_data.\n");
		retval = -ENOMEM;
		goto error_exit;
	}
	fc->data = f09;

	pdata = to_rmi_platform_data(rmi_dev);
	query_base_addr = fc->fd.query_base_addr;

	/* initial all default values for f09 query here */
	rc = rmi_read_block(rmi_dev, query_base_addr,
		(u8 *)&f09->query, sizeof(f09->query));
	if (rc < 0) {
		dev_err(&fc->dev, "Failed to read query register."
			" from 0x%04x\n", query_base_addr);
		goto error_exit;
	}

	dev_dbg(&fc->dev, "Creating sysfs files.");
	/* Set up sysfs device attributes. */
	for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
		if (sysfs_create_file
		    (&fc->dev.kobj, &attrs[attr_count].attr) < 0) {
			dev_err(&fc->dev, "Failed to create sysfs file for %s.",
			     attrs[attr_count].attr.name);
			retval = -ENODEV;
			goto error_exit;
		}
	}
	return 0;

error_exit:
	dev_err(&fc->dev, "An error occured in F09 init!\n");
	for (attr_count--; attr_count >= 0; attr_count--)
		sysfs_remove_file(&fc->dev.kobj,
				  &attrs[attr_count].attr);
	kfree(f09);
	return retval;
}

static void rmi_f09_remove(struct rmi_function_container *fc)
{
	struct rmi_fn_09_data *data = fc->data;
	if (data) {
		kfree(data->query.Limit_Register_Count);
		kfree(data->query.f09_bist_query1);
	}
	kfree(fc->data);
}

static struct rmi_function_handler function_handler = {
	.func = 0x09,
	.init = rmi_f09_init,
	.remove = rmi_f09_remove
};

static int __init rmi_f09_module_init(void)
{
	int error;

	error = rmi_register_function_driver(&function_handler);
	if (error < 0) {
		pr_err("%s: register failed!\n", __func__);
		return error;
	}

	return 0;
}

static void rmi_f09_module_exit(void)
{
	rmi_unregister_function_driver(&function_handler);
}


static ssize_t rmi_f09_Limit_Register_Count_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct rmi_function_container *fc;
	struct rmi_fn_09_data *data;

	fc = to_rmi_function_container(dev);
	data = fc->data;
	return snprintf(buf, PAGE_SIZE, "%u\n",
			data->query.Limit_Register_Count);
}

static ssize_t rmi_f09_HostTestEn_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct rmi_function_container *fc;
	struct rmi_fn_09_data *data;

	fc = to_rmi_function_container(dev);
	data = fc->data;
	return snprintf(buf, PAGE_SIZE, "%u\n",
			data->query.HostTestEn);
}

static ssize_t rmi_f09_HostTestEn_store(struct device *dev,
				      struct device_attribute *attr,
					char *buf, size_t count)
{
	struct rmi_function_container *fc;
	struct rmi_fn_09_data *data;
	unsigned int new_value;
	int result;

	fc = to_rmi_function_container(dev);
	data = fc->data;
	if (sscanf(buf, "%u", &new_value) != 1) {
		dev_err(dev,
		"%s: Error - HostTestEn_store has an "
		"invalid len.\n",
		__func__);
		return -EINVAL;
	}

	if (new_value < 0 || new_value > 1) {
		dev_err(dev, "%s: Invalid HostTestEn bit %s.", __func__, buf);
		return -EINVAL;
	}
	data->query.HostTestEn = new_value;
	result = rmi_write(fc->rmi_dev, fc->fd.query_base_addr,
		data->query.HostTestEn);
	if (result < 0) {
		dev_err(dev, "%s : Could not write HostTestEn_store to 0x%x\n",
				__func__, fc->fd.query_base_addr);
		return result;
	}

	return count;

}

static ssize_t rmi_f09_InternalLimits_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct rmi_function_container *fc;
	struct rmi_fn_09_data *data;

	fc = to_rmi_function_container(dev);
	data = fc->data;
	return snprintf(buf, PAGE_SIZE, "%u\n",
			data->query.InternalLimits);
}

static ssize_t rmi_f09_Result_Register_Count_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct rmi_function_container *fc;
	struct rmi_fn_09_data *data;

	fc = to_rmi_function_container(dev);
	data = fc->data;
	return snprintf(buf, PAGE_SIZE, "%u\n",
			data->query.Result_Register_Count);
}

module_init(rmi_f09_module_init);
module_exit(rmi_f09_module_exit);

MODULE_AUTHOR("Allie Xiong <axiong@Synaptics.com>");
MODULE_DESCRIPTION("RMI F09 module");
MODULE_LICENSE("GPL");