xref: /OK3568_Linux_fs/kernel/drivers/misc/gpio-detection.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * gpio detection  driver
3  *
4  * Copyright (C) 2015 Rockchip Electronics Co., Ltd.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/fb.h>
20 #include <linux/gpio_detection.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/of_gpio.h>
24 #include <linux/of_irq.h>
25 #include <linux/platform_device.h>
26 #include <linux/wakelock.h>
27 #include <linux/rk_keys.h>
28 #include <linux/gpio/consumer.h>
29 
30 #define WAKE_LOCK_TIMEOUT_MS (5000)
31 
32 struct gpio_data {
33 	struct gpio_detection *parent;
34 	const char *name;
35 	struct device dev;
36 	int notify;
37 	struct gpio_desc *gpio;
38 	int val;
39 	int irq;
40 	struct delayed_work work;
41 	unsigned int debounce_ms;
42 	int wakeup;
43 };
44 
45 struct gpio_detection {
46 	struct class_attribute cls_attr;
47 	struct device *dev;
48 	int num;
49 	struct gpio_data *data;
50 	struct pinctrl *pinctrl;
51 	struct pinctrl_state *pins_default;
52 	struct notifier_block fb_notifier;
53 	struct wake_lock wake_lock;
54 	int mirror;
55 	int type;
56 	int info;
57 };
58 
59 static struct class *gpio_detection_class;
60 static BLOCKING_NOTIFIER_HEAD(gpio_det_notifier_list);
61 static int system_suspend;
62 
63 #if IS_ENABLED(CONFIG_GPIO_DET)
64 
65 /*
66  * gpio_det_notifier_call_chain - notify clients of gpio_det_events
67  *
68  */
gpio_det_notifier_call_chain(unsigned long val,void * v)69 int gpio_det_notifier_call_chain(unsigned long val, void *v)
70 {
71 	return blocking_notifier_call_chain(&gpio_det_notifier_list, val, v);
72 }
73 EXPORT_SYMBOL_GPL(gpio_det_notifier_call_chain);
74 
75 /*
76  * gpio_det_register_notifier - register a client notifier
77  * @nb: notifier block to callback on events
78  */
gpio_det_register_notifier(struct notifier_block * nb)79 int gpio_det_register_notifier(struct notifier_block *nb)
80 {
81 	int ret = blocking_notifier_chain_register(&gpio_det_notifier_list, nb);
82 
83 	return ret;
84 }
85 EXPORT_SYMBOL(gpio_det_register_notifier);
86 
87 /*
88  * gpio_det_unregister_client - unregister a client notifier
89  * @nb: notifier block to callback on events
90  */
gpio_det_unregister_notifier(struct notifier_block * nb)91 int gpio_det_unregister_notifier(struct notifier_block *nb)
92 {
93 	return blocking_notifier_chain_unregister(&gpio_det_notifier_list, nb);
94 }
95 EXPORT_SYMBOL(gpio_det_unregister_notifier);
96 
97 #endif
98 
gpio_det_report_event(struct gpio_data * gpiod)99 static void gpio_det_report_event(struct gpio_data *gpiod)
100 {
101 	struct gpio_event event;
102 	struct gpio_detection *gpio_det = gpiod->parent;
103 	char *status = NULL;
104 	char *envp[2];
105 
106 	event.val = gpiod->val;
107 	event.name = gpiod->name;
108 	status = kasprintf(GFP_KERNEL, "GPIO_NAME=%s GPIO_STATE=%s",
109 			   gpiod->name, event.val ? "over" : "on");
110 	envp[0] = status;
111 	envp[1] = NULL;
112 	wake_lock_timeout(&gpio_det->wake_lock,
113 			  msecs_to_jiffies(WAKE_LOCK_TIMEOUT_MS));
114 	kobject_uevent_env(&gpiod->dev.kobj, KOBJ_CHANGE, envp);
115 	if (gpiod->notify)
116 		gpio_det_notifier_call_chain(GPIO_EVENT, &event);
117 	kfree(status);
118 }
119 
gpio_det_work_func(struct work_struct * work)120 static void gpio_det_work_func(struct work_struct *work)
121 {
122 	struct gpio_data *gpiod = container_of(work, struct gpio_data,
123 					       work.work);
124 	int val = gpiod_get_value(gpiod->gpio);
125 
126 	if (gpiod->val != val) {
127 		gpiod->val = val;
128 		gpio_det_report_event(gpiod);
129 		if (system_suspend && gpiod->wakeup) {
130 			rk_send_power_key(1);
131 			rk_send_power_key(0);
132 		}
133 	}
134 }
135 
gpio_det_interrupt(int irq,void * dev_id)136 static irqreturn_t gpio_det_interrupt(int irq, void *dev_id)
137 {
138 	struct gpio_data *gpiod = dev_id;
139 	int val = gpiod_get_raw_value(gpiod->gpio);
140 	unsigned int irqflags = IRQF_ONESHOT;
141 
142 	if (val)
143 		irqflags |= IRQ_TYPE_EDGE_FALLING;
144 	else
145 		irqflags |= IRQ_TYPE_EDGE_RISING;
146 	irq_set_irq_type(gpiod->irq, irqflags);
147 
148 	mod_delayed_work(system_wq, &gpiod->work,
149 			 msecs_to_jiffies(gpiod->debounce_ms));
150 
151 	return IRQ_HANDLED;
152 }
153 
gpio_det_init_status_check(struct gpio_detection * gpio_det)154 static int gpio_det_init_status_check(struct gpio_detection *gpio_det)
155 {
156 	struct gpio_data *gpiod;
157 	int i;
158 
159 	for (i = 0; i < gpio_det->num; i++) {
160 		gpiod = &gpio_det->data[i];
161 		gpiod->val = gpiod_get_value(gpiod->gpio);
162 		if (gpiod->val)
163 			gpio_det_report_event(gpiod);
164 	}
165 
166 	return 0;
167 }
168 
gpio_det_fb_notifier_callback(struct notifier_block * self,unsigned long event,void * data)169 static int gpio_det_fb_notifier_callback(struct notifier_block *self,
170 					 unsigned long event,
171 					 void *data)
172 {
173 	struct gpio_detection *gpio_det;
174 	struct fb_event *evdata = data;
175 	int fb_blank;
176 
177 	if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
178 		return 0;
179 
180 	gpio_det = container_of(self, struct gpio_detection, fb_notifier);
181 	fb_blank = *(int *)evdata->data;
182 	if (fb_blank == FB_BLANK_UNBLANK)
183 		system_suspend = 0;
184 	else
185 		system_suspend = 1;
186 
187 	return 0;
188 }
189 
gpio_det_fb_notifier_register(struct gpio_detection * gpio)190 static int gpio_det_fb_notifier_register(struct gpio_detection *gpio)
191 {
192 	gpio->fb_notifier.notifier_call = gpio_det_fb_notifier_callback;
193 
194 	return fb_register_client(&gpio->fb_notifier);
195 }
196 
gpio_detection_info_show(struct class * class,struct class_attribute * attr,char * buf)197 static ssize_t gpio_detection_info_show(struct class *class,
198 					struct class_attribute *attr,
199 					char *buf)
200 {
201 	struct gpio_detection *gpio_det;
202 
203 	gpio_det = container_of(attr, struct gpio_detection, cls_attr);
204 
205 	return sprintf(buf, "%d\n", gpio_det->info);
206 }
207 
status_show(struct device * dev,struct device_attribute * attr,char * buf)208 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
209 			   char *buf)
210 {
211 	struct gpio_data *gpiod = container_of(dev, struct gpio_data, dev);
212 	unsigned int val = gpiod_get_value(gpiod->gpio);
213 
214 	return sprintf(buf, "%d\n", val);
215 }
216 
status_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)217 static ssize_t status_store(struct device *dev, struct device_attribute *attr,
218 			    const char *buf, size_t count)
219 {
220 	struct gpio_data *gpiod;
221 	int val;
222 	int ret;
223 	struct gpio_event event;
224 
225 	gpiod = container_of(dev, struct gpio_data, dev);
226 	ret = kstrtoint(buf, 0, &val);
227 	if (ret < 0)
228 		return ret;
229 	if (val >= 0) {
230 		event.val = val;
231 		event.name = gpiod->name;
232 		gpio_det_notifier_call_chain(GPIO_EVENT, &event);
233 	} else {
234 		gpiod->notify = 0;
235 	}
236 
237 	return count;
238 }
239 
240 static DEVICE_ATTR_RW(status);
241 
242 static struct attribute *gpio_detection_attrs[] = {
243 	&dev_attr_status.attr,
244 	NULL,
245 };
246 ATTRIBUTE_GROUPS(gpio_detection);
247 
gpio_deteciton_class_init(void)248 static int __init gpio_deteciton_class_init(void)
249 {
250 	gpio_detection_class = class_create(THIS_MODULE, "gpio-detection");
251 	if (IS_ERR(gpio_detection_class)) {
252 		pr_err("create gpio_detection class failed (%ld)\n",
253 		       PTR_ERR(gpio_detection_class));
254 		return PTR_ERR(gpio_detection_class);
255 	}
256 
257 	return 0;
258 }
259 
gpio_detection_class_register(struct gpio_detection * gpio_det,struct gpio_data * gpiod)260 static int gpio_detection_class_register(struct gpio_detection *gpio_det,
261 					 struct gpio_data *gpiod)
262 {
263 	int ret;
264 
265 	gpiod->dev.class = gpio_detection_class;
266 	dev_set_name(&gpiod->dev, "%s", gpiod->name);
267 	dev_set_drvdata(&gpiod->dev, gpio_det);
268 	ret = device_register(&gpiod->dev);
269 	ret = sysfs_create_groups(&gpiod->dev.kobj, gpio_detection_groups);
270 
271 	return ret;
272 }
273 
gpio_det_parse_dt(struct gpio_detection * gpio_det,struct platform_device * pdev)274 static int gpio_det_parse_dt(struct gpio_detection *gpio_det,
275 			     struct platform_device *pdev)
276 {
277 	struct gpio_data *data;
278 	struct device *dev = &pdev->dev;
279 	struct device_node *node;
280 	struct gpio_data *gpiod;
281 	struct fwnode_handle *child;
282 	int count = 0;
283 	int i = 0;
284 	int num = 0;
285 
286 	num = of_get_child_count(gpio_det->dev->of_node);
287 	count = device_get_child_node_count(dev);
288 	if (!count || !num)
289 		return -ENODEV;
290 	data = devm_kzalloc(gpio_det->dev, num * sizeof(*data), GFP_KERNEL);
291 	if (!data)
292 		return -ENOMEM;
293 	of_property_read_u32(gpio_det->dev->of_node, "rockchip,camcap-type",
294 			     &gpio_det->type);
295 	of_property_read_u32(gpio_det->dev->of_node, "rockchip,camcap-mirror",
296 			     &gpio_det->mirror);
297 	gpio_det->info = (gpio_det->mirror << 4) | gpio_det->type;
298 	device_for_each_child_node(dev, child) {
299 		node = to_of_node(child);
300 		gpiod = &data[i++];
301 		gpiod->parent = gpio_det;
302 		gpiod->notify = 1;
303 		gpiod->name = of_get_property(node, "label", NULL);
304 		gpiod->wakeup = !!of_get_property(node, "gpio,wakeup", NULL);
305 		of_property_read_u32(node, "linux,debounce-ms",
306 				     &gpiod->debounce_ms);
307 		if (!strcmp(gpiod->name, "car-reverse"))
308 			gpiod->gpio = devm_get_gpiod_from_child(dev,
309 							"car-reverse", child);
310 		else
311 			gpiod->gpio = devm_get_gpiod_from_child(dev,
312 							"car-acc", child);
313 	}
314 	gpio_det->num = num;
315 	gpio_det->data = data;
316 
317 	return 0;
318 }
319 
gpio_det_probe(struct platform_device * pdev)320 static int gpio_det_probe(struct platform_device *pdev)
321 {
322 	struct gpio_detection *gpio_det;
323 	struct gpio_data *gpiod;
324 	unsigned long irqflags = IRQF_ONESHOT;
325 	int i;
326 	int ret;
327 
328 	gpio_det = devm_kzalloc(&pdev->dev, sizeof(*gpio_det), GFP_KERNEL);
329 	if (!gpio_det)
330 		return -ENOMEM;
331 	gpio_det->dev = &pdev->dev;
332 	gpio_det->cls_attr.attr.name = "info";
333 	gpio_det->cls_attr.attr.mode = S_IRUGO;
334 	gpio_det->cls_attr.show = gpio_detection_info_show;
335 	dev_set_name(gpio_det->dev, "gpio_detection");
336 	if (!pdev->dev.of_node)
337 		return -EINVAL;
338 	gpio_det->pinctrl = devm_pinctrl_get(&pdev->dev);
339 	if (IS_ERR(gpio_det->pinctrl)) {
340 		dev_err(&pdev->dev, "pinctrl get failed\n");
341 		return PTR_ERR(gpio_det->pinctrl);
342 	}
343 	gpio_det->pins_default = pinctrl_lookup_state(gpio_det->pinctrl,
344 						      PINCTRL_STATE_DEFAULT);
345 	if (IS_ERR(gpio_det->pins_default))
346 		dev_err(gpio_det->dev, "get default pinstate failed\n");
347 	else
348 		pinctrl_select_state(gpio_det->pinctrl, gpio_det->pins_default);
349 	if (gpio_det_parse_dt(gpio_det, pdev))
350 		return -ENODEV;
351 	wake_lock_init(&gpio_det->wake_lock, WAKE_LOCK_SUSPEND,
352 		       "gpio_detection");
353 	for (i = 0; i < gpio_det->num; i++) {
354 		gpiod = &gpio_det->data[i];
355 		gpiod_direction_input(gpiod->gpio);
356 
357 		gpiod->irq = gpiod_to_irq(gpiod->gpio);
358 		if (gpiod->irq < 0) {
359 			dev_err(gpio_det->dev, "failed to get irq number for GPIO %s\n",
360 				gpiod->name);
361 			continue;
362 		}
363 
364 		ret = gpio_detection_class_register(gpio_det, gpiod);
365 		if (ret < 0)
366 			return ret;
367 		INIT_DELAYED_WORK(&gpiod->work, gpio_det_work_func);
368 		gpiod->val = gpiod_get_raw_value(gpiod->gpio);
369 		if  (gpiod->val)
370 			irqflags |= IRQ_TYPE_EDGE_FALLING;
371 		else
372 			irqflags |= IRQ_TYPE_EDGE_RISING;
373 		ret = devm_request_threaded_irq(gpio_det->dev, gpiod->irq,
374 						NULL, gpio_det_interrupt,
375 						irqflags | IRQF_ONESHOT,
376 						gpiod->name, gpiod);
377 		if (ret < 0)
378 			dev_err(gpio_det->dev, "request irq(%s) failed:%d\n",
379 				gpiod->name, ret);
380 		else
381 			if (gpiod->wakeup)
382 				enable_irq_wake(gpiod->irq);
383 	}
384 
385 	if (gpio_det->info) {
386 		ret = class_create_file(gpio_detection_class,
387 					&gpio_det->cls_attr);
388 		if (ret)
389 			dev_warn(gpio_det->dev, "create class file failed:%d\n",
390 				 ret);
391 	}
392 
393 	gpio_det_fb_notifier_register(gpio_det);
394 	gpio_det_init_status_check(gpio_det);
395 
396 	dev_info(gpio_det->dev, "gpio detection driver probe success\n");
397 
398 	return 0;
399 }
400 
401 #if defined(CONFIG_OF)
402 static const struct of_device_id gpio_det_of_match[] = {
403 	{
404 		 .compatible = "gpio-detection"
405 	},
406 	{},
407 };
408 #endif
409 
410 static struct platform_driver gpio_det_driver = {
411 	.driver	= {
412 		.name = "gpio-detection",
413 		.of_match_table = of_match_ptr(gpio_det_of_match),
414 	},
415 	.probe = gpio_det_probe,
416 };
417 
418 #ifdef CONFIG_VIDEO_REVERSE_IMAGE
gpio_det_init(void)419 int gpio_det_init(void)
420 #else
421 static int __init gpio_det_init(void)
422 #endif
423 {
424 	if (!gpio_deteciton_class_init())
425 		return platform_driver_register(&gpio_det_driver);
426 	else
427 		return -1;
428 }
429 
430 #ifndef CONFIG_VIDEO_REVERSE_IMAGE
431 fs_initcall_sync(gpio_det_init);
432 #endif
gpio_det_exit(void)433 static void __exit gpio_det_exit(void)
434 {
435 	platform_driver_unregister(&gpio_det_driver);
436 }
437 
438 module_exit(gpio_det_exit);
439 
440 MODULE_LICENSE("GPL");
441 MODULE_ALIAS("platform:gpio-detection");
442 MODULE_AUTHOR("ROCKCHIP");
443