1 /*
2  * Copyright (c) 2019 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Public API for Keyboard scan matrix devices.
10  * The scope of this API is simply to report which key event was triggered
11  * and users can later decode keys using their desired scan code tables in
12  * their application. In addition, typematic rate and delay can easily be
13  * implemented using a timer if desired.
14  */
15 
16 #ifndef ZEPHYR_INCLUDE_DRIVERS_KB_SCAN_H_
17 #define ZEPHYR_INCLUDE_DRIVERS_KB_SCAN_H_
18 
19 #include <errno.h>
20 #include <zephyr/types.h>
21 #include <stddef.h>
22 #include <zephyr/device.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /**
29  * @brief KSCAN APIs
30  * @defgroup kscan_interface Keyboard Scan Driver APIs
31  * @since 2.1
32  * @version 1.0.0
33  * @ingroup io_interfaces
34  * @{
35  */
36 
37 /**
38  * @brief Keyboard scan callback called when user press/release
39  * a key on a matrix keyboard.
40  *
41  * @param dev Pointer to the device structure for the driver instance.
42  * @param row Describes row change.
43  * @param column Describes column change.
44  * @param pressed Describes the kind of key event.
45  */
46 typedef void (*kscan_callback_t)(const struct device *dev, uint32_t row,
47 				 uint32_t column,
48 				 bool pressed);
49 
50 /**
51  * @cond INTERNAL_HIDDEN
52  *
53  * Keyboard scan driver API definition and system call entry points.
54  *
55  * (Internal use only.)
56  */
57 typedef int (*kscan_config_t)(const struct device *dev,
58 			      kscan_callback_t callback);
59 typedef int (*kscan_disable_callback_t)(const struct device *dev);
60 typedef int (*kscan_enable_callback_t)(const struct device *dev);
61 
62 __subsystem struct kscan_driver_api {
63 	kscan_config_t config;
64 	kscan_disable_callback_t disable_callback;
65 	kscan_enable_callback_t enable_callback;
66 };
67 /**
68  * @endcond
69  */
70 
71 /**
72  * @brief Configure a Keyboard scan instance.
73  *
74  * @param dev Pointer to the device structure for the driver instance.
75  * @param callback called when keyboard devices reply to a keyboard
76  * event such as key pressed/released.
77  *
78  * @retval 0 If successful.
79  * @retval Negative errno code if failure.
80  */
81 __syscall int kscan_config(const struct device *dev,
82 			     kscan_callback_t callback);
83 
z_impl_kscan_config(const struct device * dev,kscan_callback_t callback)84 static inline int z_impl_kscan_config(const struct device *dev,
85 					kscan_callback_t callback)
86 {
87 	const struct kscan_driver_api *api =
88 				(struct kscan_driver_api *)dev->api;
89 
90 	return api->config(dev, callback);
91 }
92 /**
93  * @brief Enables callback.
94  * @param dev Pointer to the device structure for the driver instance.
95  *
96  * @retval 0 If successful.
97  * @retval Negative errno code if failure.
98  */
99 __syscall int kscan_enable_callback(const struct device *dev);
100 
z_impl_kscan_enable_callback(const struct device * dev)101 static inline int z_impl_kscan_enable_callback(const struct device *dev)
102 {
103 	const struct kscan_driver_api *api =
104 			(const struct kscan_driver_api *)dev->api;
105 
106 	if (api->enable_callback == NULL) {
107 		return -ENOSYS;
108 	}
109 
110 	return api->enable_callback(dev);
111 }
112 
113 /**
114  * @brief Disables callback.
115  * @param dev Pointer to the device structure for the driver instance.
116  *
117  * @retval 0 If successful.
118  * @retval Negative errno code if failure.
119  */
120 __syscall int kscan_disable_callback(const struct device *dev);
121 
z_impl_kscan_disable_callback(const struct device * dev)122 static inline int z_impl_kscan_disable_callback(const struct device *dev)
123 {
124 	const struct kscan_driver_api *api =
125 			(const struct kscan_driver_api *)dev->api;
126 
127 	if (api->disable_callback == NULL) {
128 		return -ENOSYS;
129 	}
130 
131 	return api->disable_callback(dev);
132 }
133 
134 #ifdef __cplusplus
135 }
136 #endif
137 
138 /**
139  * @}
140  */
141 
142 #include <zephyr/syscalls/kscan.h>
143 
144 #endif /* ZEPHYR_INCLUDE_DRIVERS_KB_SCAN_H_ */
145