1111#include <zephyr/input/input.h>
1212#include <zephyr/input/input_touch.h>
1313#include <zephyr/logging/log.h>
14+ #include <zephyr/pm/device.h>
1415
1516LOG_MODULE_REGISTER (chsc5x , CONFIG_INPUT_LOG_LEVEL );
1617
4142#define CHSC5X_BASE_ADDR1 0x20
4243#define CHSC5X_BASE_ADDR2 0x00
4344#define CHSC5X_BASE_ADDR3 0x00
45+ #define CHSC5X_ADDRESS_MODE 0x00
4446#define CHSC5X_ADDRESS_IC_TYPE 0x81
4547#define CHSC5X_ADDRESS_TOUCH_DATA 0x2C
4648#define CHSC5X_SIZE_TOUCH_DATA 7
@@ -137,16 +139,11 @@ static int chsc5x_chip_init(const struct device *dev)
137139 return 0 ;
138140}
139141
140- static int chsc5x_init (const struct device * dev )
142+ static int chsc5x_reset (const struct device * dev )
141143{
142144 const struct chsc5x_config * config = dev -> config ;
143- struct chsc5x_data * data = dev -> data ;
144145 int ret ;
145146
146- data -> dev = dev ;
147-
148- k_work_init (& data -> work , chsc5x_work_handler );
149-
150147 if (config -> reset_gpio .port != NULL ) {
151148 if (!gpio_is_ready_dt (& config -> reset_gpio )) {
152149 LOG_ERR ("GPIO port %s not ready" , config -> reset_gpio .port -> name );
@@ -170,6 +167,63 @@ static int chsc5x_init(const struct device *dev)
170167 k_msleep (1 );
171168 }
172169
170+ return 0 ;
171+ }
172+
173+ #ifdef CONFIG_PM_DEVICE
174+ static int chsc5x_pm_action (const struct device * dev , enum pm_device_action action )
175+ {
176+ const struct chsc5x_config * config = dev -> config ;
177+ int ret ;
178+
179+ if (config -> reset_gpio .port == NULL ) {
180+ return - ENOTSUP ;
181+ }
182+
183+ switch (action ) {
184+ case PM_DEVICE_ACTION_RESUME :
185+ ret = chsc5x_reset (dev );
186+ break ;
187+
188+ case PM_DEVICE_ACTION_SUSPEND : {
189+ const uint8_t write_buffer [] = {
190+ CHSC5X_BASE_ADDR1 ,
191+ CHSC5X_BASE_ADDR2 ,
192+ CHSC5X_BASE_ADDR3 ,
193+ CHSC5X_ADDRESS_MODE ,
194+ /* Fixed sequence with checksum */
195+ 0xF7 , 0x16 , 0x05 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 ,
196+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x03 , 0xE9 ,
197+ };
198+
199+ ret = i2c_write_dt (& config -> i2c , write_buffer , sizeof (write_buffer ));
200+ break ;
201+ }
202+
203+ default :
204+ ret = - ENOTSUP ;
205+ }
206+
207+ return ret ;
208+ }
209+ #endif /* CONFIG_PM_DEVICE */
210+
211+ static int chsc5x_init (const struct device * dev )
212+ {
213+ const struct chsc5x_config * config = dev -> config ;
214+ struct chsc5x_data * data = dev -> data ;
215+ int ret ;
216+
217+ data -> dev = dev ;
218+
219+ k_work_init (& data -> work , chsc5x_work_handler );
220+
221+ ret = chsc5x_reset (dev );
222+ if (ret < 0 ) {
223+ LOG_ERR ("Failed to reset (%d)" , ret );
224+ return ret ;
225+ }
226+
173227 if (!gpio_is_ready_dt (& config -> int_gpio )) {
174228 LOG_ERR ("GPIO port %s not ready" , config -> int_gpio .port -> name );
175229 return - ENODEV ;
@@ -199,15 +253,16 @@ static int chsc5x_init(const struct device *dev)
199253};
200254
201255#define CHSC5X_DEFINE (index ) \
256+ PM_DEVICE_DT_INST_DEFINE(inst, chsc5x_pm_action); \
202257 static const struct chsc5x_config chsc5x_config_##index = { \
203258 .common = INPUT_TOUCH_DT_INST_COMMON_CONFIG_INIT(index), \
204259 .i2c = I2C_DT_SPEC_INST_GET(index), \
205260 .int_gpio = GPIO_DT_SPEC_INST_GET(index, int_gpios), \
206261 .reset_gpio = GPIO_DT_SPEC_INST_GET_OR(index, reset_gpios, {0}), \
207262 }; \
208263 static struct chsc5x_data chsc5x_data_##index; \
209- DEVICE_DT_INST_DEFINE(index, chsc5x_init, NULL, &chsc5x_data_##index, \
210- &chsc5x_config_##index, POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, \
211- NULL);
264+ DEVICE_DT_INST_DEFINE(index, chsc5x_init, PM_DEVICE_DT_INST_GET(inst), \
265+ &chsc5x_data_##index, & chsc5x_config_##index, POST_KERNEL, \
266+ CONFIG_INPUT_INIT_PRIORITY, NULL);
212267
213268DT_INST_FOREACH_STATUS_OKAY (CHSC5X_DEFINE )
0 commit comments