summaryrefslogtreecommitdiff
path: root/hall_sensor.c
blob: f16ce23707c43c0349fbb031f2ab215c7f42776c (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Google Hall Effect Sensor Driver
 *
 * Copyright (c) 2021 Google LLC
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/input.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <linux/sysctl.h>

struct hall_sensor_drv_data {
	struct device *dev;
	int irq_gpio;
	struct input_dev *input;
	bool irq_enabled;
	int prev_state;
};

static void hall_sensor_report_state(struct hall_sensor_drv_data *hall_data)
{
	struct input_dev *input = hall_data->input;
	struct device *dev = hall_data->dev;
	int state = !gpio_get_value(hall_data->irq_gpio);

	dev_dbg(dev, "state:%d\n", state);
	if (hall_data->prev_state != state) {
		input_event(input, EV_SW, SW_LID, state);
		input_sync(input);
		hall_data->prev_state = state;
	}
}

static irqreturn_t hall_sensor_irq_handler(int irq, void *ptr) {
	struct hall_sensor_drv_data *hall_data = ptr;

	hall_sensor_report_state(hall_data);

	return IRQ_HANDLED;
}

static int hall_sensor_open(struct input_dev *input)
{
	struct hall_sensor_drv_data *hall_data = input_get_drvdata(input);
	struct device *dev = hall_data->dev;
	int ret;

	/* Enable hall sensor irq.*/
	if (!hall_data->irq_enabled) {
		ret = devm_request_threaded_irq(dev, gpio_to_irq(
				hall_data->irq_gpio), NULL,
				hall_sensor_irq_handler, IRQF_TRIGGER_RISING |
				IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
				"hall-sensor", hall_data);
		if (ret) {
			dev_err(dev, "Could not request irq %d with error %d\n",
				gpio_to_irq(hall_data->irq_gpio), ret);
			return ret;
		}
		hall_data->irq_enabled = true;
		dev_dbg(dev, "Requested irq %d\n",
			gpio_to_irq(hall_data->irq_gpio));
	}

	/* Report current state of hall sensor.*/
	hall_sensor_report_state(hall_data);

	return 0;
}

static void hall_sensor_close(struct input_dev *input)
{
	struct hall_sensor_drv_data *hall_data = input_get_drvdata(input);
	struct device *dev = hall_data->dev;

	if (!hall_data->irq_enabled)
		return;

	/* Free hall sensor irq.*/
	devm_free_irq(dev, gpio_to_irq(hall_data->irq_gpio), hall_data);
	hall_data->irq_enabled = false;
	dev_dbg(dev, "Free irq %d\n",
		gpio_to_irq(hall_data->irq_gpio));
}

static int create_input_device(struct hall_sensor_drv_data *hall_data) {
	struct device *dev = hall_data->dev;
	struct input_dev *input;
	int ret;

	input = devm_input_allocate_device(dev);
	if (!input) {
		dev_err(dev, "Failed to allocate input device\n");
		return -ENOMEM;
	}

	hall_data->input = input;
	input_set_drvdata(input, hall_data);
	input->name = "hall effect sensor";
	input->phys = "hall_sensor/input0";
	input->dev.parent = dev;
	input->open = hall_sensor_open;
	input->close = hall_sensor_close;

	input->id.bustype = BUS_VIRTUAL;
	input->id.vendor = 0x0001;
	input->id.product = 0x0001;
	input->id.version = 0x0100;
	set_bit(EV_SW, input->evbit);
	set_bit(EV_SYN, input->evbit);
	input_set_capability(input, EV_SW, SW_LID);

	ret = input_register_device(input);
	if (ret) {
		dev_err(dev, "Failed to register input device\n");
		hall_data->input = NULL;
		input_free_device(input);
	}

	return ret;
}

static void destroy_input_device(struct hall_sensor_drv_data *hall_data) {
	struct input_dev *input = hall_data->input;

	if (!input)
		return;

	input_unregister_device(input);
	input_free_device(input);
	hall_data->input = NULL;
}

static void hall_sensor_enable(struct hall_sensor_drv_data *hall_data,
			       bool enable) {
	struct device *dev = hall_data->dev;

	if (enable) {
		if (!hall_data->input) {
			create_input_device(hall_data);
			dev_dbg(dev, "Input registered\n");
		}
		return;
	}

	destroy_input_device(hall_data);
	dev_dbg(dev, "Input unregistered\n");
}

static ssize_t state_show(struct device *dev,
			   struct device_attribute *attr, char *buf) {
	struct hall_sensor_drv_data *hall_data = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%d\n",
			gpio_get_value(hall_data->irq_gpio));
}
static DEVICE_ATTR_RO(state);

static ssize_t enable_store(struct device *dev,
	struct device_attribute *attr,
	const char *buf, size_t count) {
	struct hall_sensor_drv_data *hall_data = dev_get_drvdata(dev);
	int val;

	kstrtoint(buf, 10, &val);

	hall_sensor_enable(hall_data, !!val);

	return count;
}
static DEVICE_ATTR_WO(enable);

static struct attribute *attributes[] = {
	&dev_attr_enable.attr,
	&dev_attr_state.attr,
	NULL
};

static const struct attribute_group attribute_group = {
	.attrs = attributes,
};

static int hall_sensor_parse_dt(struct device *dev,
				struct hall_sensor_drv_data *hall_data) {
	struct device_node *np = dev->of_node;
	int ret = 0;

	hall_data->irq_gpio = of_get_named_gpio(np, "hall,gpio", 0);

	if (!gpio_is_valid(hall_data->irq_gpio)) {
		dev_err(dev, "irq gpio %d is invalid.", hall_data->irq_gpio);
		ret = -EINVAL;
	}

	return ret;
}

static int hall_sensor_config_gpio(struct hall_sensor_drv_data *hall_data,
				   int gpio) {
	struct device *dev = hall_data->dev;
	int ret = 0;
	ret = devm_gpio_request(dev, gpio, "hall-gpio");
	if (ret) {
		dev_err(dev, "Failed to request gpio with error %d.\n", ret);
		return ret;
	}

	ret = gpio_direction_input(gpio);
	if (ret) {
		dev_err(dev, "Failed to set input direction with error %d.\n",
			ret);
		return ret;
	}

	return ret;
}

static int hall_sensor_probe(struct platform_device *pdev) {
	struct device *dev = &pdev->dev;
	int ret = 0;
	struct hall_sensor_drv_data *hall_data = devm_kzalloc(dev,
			 sizeof(*hall_data), GFP_KERNEL);

	if (!hall_data) {
		return -ENOMEM;
	}

	hall_data->dev = dev;
	platform_set_drvdata(pdev, hall_data);

	ret = hall_sensor_parse_dt(dev, hall_data);
	if (ret) {
		dev_err(dev, "Failed to parse dt with error %d.\n", ret);
		return ret;
	}

	ret = hall_sensor_config_gpio(hall_data, hall_data->irq_gpio);
	if (ret) {
		dev_err(dev, "Failed to config gpio with error %d.\n", ret);
		return ret;
	}

	hall_data->irq_enabled = false;
	hall_data->prev_state = -1;

	ret = create_input_device(hall_data);
	if (ret) {
		dev_err(dev, "Failed to create input device with error %d.\n", ret);
		return ret;
	}

	ret = sysfs_create_group(&dev->kobj, &attribute_group);
	if (ret) {
		dev_err(dev, "Failed to create sysfs with error %d.\n", ret);
		return ret;
	}

	dev_dbg(dev, "%s done\n", __func__);

	return ret;
}

static int hall_sensor_remove(struct platform_device *pdev) {
	struct hall_sensor_drv_data *hall_data = platform_get_drvdata(pdev);

	sysfs_remove_group(&pdev->dev.kobj, &attribute_group);
	hall_sensor_enable(hall_data, false);
	return 0;
}

static struct of_device_id hall_sensor_of_match[] = {
	{ .compatible = "hall,drv5032", },
	{}
};
MODULE_DEVICE_TABLE(of, hall_sensor_of_match);

static struct platform_driver hall_sensor_driver = {
	.driver = {
		.name	= "hall_sensor",
		.owner	= THIS_MODULE,
		.of_match_table = hall_sensor_of_match,
	},
	.probe	= hall_sensor_probe,
	.remove	= hall_sensor_remove,
};

static int __init hall_sensor_init(void)
{
	return platform_driver_register(&hall_sensor_driver);
}

static void __exit hall_sensor_exit(void)
{
	platform_driver_unregister(&hall_sensor_driver);
}

module_init(hall_sensor_init);
module_exit(hall_sensor_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Rick Chen <rickctchen@google.com>");
MODULE_DESCRIPTION("Hall effect sensor driver");