1 /*
2  * Copyright (c) 2024 Cirrus Logic, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 
9 #include <zephyr/device.h>
10 #include <zephyr/devicetree.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/logging/log.h>
13 #include <zephyr/drivers/haptics.h>
14 #include <zephyr/drivers/haptics/drv2605.h>
15 #include <zephyr/sys/util.h>
16 #include <sys/_stdint.h>
17 
18 #define LOG_LEVEL 4
19 
20 LOG_MODULE_REGISTER(main);
21 
22 static struct drv2605_rom_data rom_data = {
23 	.library = DRV2605_LIBRARY_LRA,
24 	.brake_time = 0,
25 	.overdrive_time = 0,
26 	.sustain_neg_time = 0,
27 	.sustain_pos_time = 0,
28 	.trigger = DRV2605_MODE_INTERNAL_TRIGGER,
29 	.seq_regs[0] = 1,
30 	.seq_regs[1] = 10 | 0x80,
31 	.seq_regs[2] = 2,
32 	.seq_regs[3] = 10 | 0x80,
33 	.seq_regs[4] = 3,
34 	.seq_regs[5] = 10 | 0x80,
35 	.seq_regs[6] = 4,
36 };
37 
main(void)38 int main(void)
39 {
40 	const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(haptic));
41 	union drv2605_config_data config_data = {};
42 
43 	int ret;
44 
45 	if (!dev) {
46 		LOG_ERR("DRV2605 device not found");
47 		return -ENODEV;
48 	} else if (!device_is_ready(dev)) {
49 		LOG_ERR("DRV2605 device %s is not ready", dev->name);
50 		return -EIO;
51 	}
52 
53 	LOG_INF("Found DRV2605 device %s", dev->name);
54 
55 	config_data.rom_data = &rom_data;
56 
57 	ret = drv2605_haptic_config(dev, DRV2605_HAPTICS_SOURCE_ROM, &config_data);
58 	if (ret < 0) {
59 		LOG_ERR("Failed to configure ROM event: %d", ret);
60 		return ret;
61 	}
62 
63 	ret = haptics_start_output(dev);
64 	if (ret < 0) {
65 		LOG_ERR("Failed to start ROM event: %d", ret);
66 		return ret;
67 	}
68 
69 	return 0;
70 }
71