1 /*
2  * Copyright (c) 2019 - 2021 Henrik Brix Andersen <henrik@brixandersen.dk>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT holtek_ht16k33_keyscan
8 
9 /**
10  * @file
11  * @brief Keyscan driver for the HT16K33 I2C LED driver
12  */
13 
14 #include <zephyr/kernel.h>
15 #include <zephyr/drivers/kscan.h>
16 #include <zephyr/drivers/led/ht16k33.h>
17 #include <zephyr/logging/log.h>
18 
19 LOG_MODULE_REGISTER(kscan_ht16k33, CONFIG_KSCAN_LOG_LEVEL);
20 
21 BUILD_ASSERT(CONFIG_KSCAN_INIT_PRIORITY > CONFIG_LED_INIT_PRIORITY,
22 	"HT16K33 keyscan driver must be initialized after HT16K33 LED driver");
23 
24 struct kscan_ht16k33_cfg {
25 	const struct device *parent;
26 };
27 
kscan_ht16k33_config(const struct device * dev,kscan_callback_t callback)28 static int kscan_ht16k33_config(const struct device *dev,
29 				kscan_callback_t callback)
30 {
31 	const struct kscan_ht16k33_cfg *config = dev->config;
32 
33 	return ht16k33_register_keyscan_callback(config->parent, dev, callback);
34 }
35 
kscan_ht16k33_init(const struct device * dev)36 static int kscan_ht16k33_init(const struct device *dev)
37 {
38 	const struct kscan_ht16k33_cfg *config = dev->config;
39 
40 	if (!device_is_ready(config->parent)) {
41 		LOG_ERR("HT16K33 parent device not ready");
42 		return -EINVAL;
43 	}
44 
45 	return 0;
46 }
47 
48 static const struct kscan_driver_api kscan_ht16k33_api = {
49 	.config = kscan_ht16k33_config,
50 };
51 
52 #define KSCAN_HT16K33_DEVICE(id)					\
53 	static const struct kscan_ht16k33_cfg kscan_ht16k33_##id##_cfg = { \
54 		.parent = DEVICE_DT_GET(DT_INST_BUS(id)),		\
55 	};								\
56 									\
57 	DEVICE_DT_INST_DEFINE(id, &kscan_ht16k33_init,			\
58 			      NULL, NULL,				\
59 			      &kscan_ht16k33_##id##_cfg, POST_KERNEL,	\
60 			      CONFIG_KSCAN_INIT_PRIORITY,		\
61 			      &kscan_ht16k33_api);
62 
63 DT_INST_FOREACH_STATUS_OKAY(KSCAN_HT16K33_DEVICE)
64