11
11
#include <zephyr/input/input.h>
12
12
#include <zephyr/input/input_touch.h>
13
13
#include <zephyr/logging/log.h>
14
+ #include <zephyr/pm/device.h>
14
15
15
16
LOG_MODULE_REGISTER (chsc5x , CONFIG_INPUT_LOG_LEVEL );
16
17
41
42
#define CHSC5X_BASE_ADDR1 0x20
42
43
#define CHSC5X_BASE_ADDR2 0x00
43
44
#define CHSC5X_BASE_ADDR3 0x00
45
+ #define CHSC5X_ADDRESS_MODE 0x00
44
46
#define CHSC5X_ADDRESS_IC_TYPE 0x81
45
47
#define CHSC5X_ADDRESS_TOUCH_DATA 0x2C
46
48
#define CHSC5X_SIZE_TOUCH_DATA 7
@@ -137,16 +139,11 @@ static int chsc5x_chip_init(const struct device *dev)
137
139
return 0 ;
138
140
}
139
141
140
- static int chsc5x_init (const struct device * dev )
142
+ static int chsc5x_reset (const struct device * dev )
141
143
{
142
144
const struct chsc5x_config * config = dev -> config ;
143
- struct chsc5x_data * data = dev -> data ;
144
145
int ret ;
145
146
146
- data -> dev = dev ;
147
-
148
- k_work_init (& data -> work , chsc5x_work_handler );
149
-
150
147
if (config -> reset_gpio .port != NULL ) {
151
148
if (!gpio_is_ready_dt (& config -> reset_gpio )) {
152
149
LOG_ERR ("GPIO port %s not ready" , config -> reset_gpio .port -> name );
@@ -170,6 +167,63 @@ static int chsc5x_init(const struct device *dev)
170
167
k_msleep (1 );
171
168
}
172
169
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
+
173
227
if (!gpio_is_ready_dt (& config -> int_gpio )) {
174
228
LOG_ERR ("GPIO port %s not ready" , config -> int_gpio .port -> name );
175
229
return - ENODEV ;
@@ -199,15 +253,16 @@ static int chsc5x_init(const struct device *dev)
199
253
};
200
254
201
255
#define CHSC5X_DEFINE (index ) \
256
+ PM_DEVICE_DT_INST_DEFINE(inst, chsc5x_pm_action); \
202
257
static const struct chsc5x_config chsc5x_config_##index = { \
203
258
.common = INPUT_TOUCH_DT_INST_COMMON_CONFIG_INIT(index), \
204
259
.i2c = I2C_DT_SPEC_INST_GET(index), \
205
260
.int_gpio = GPIO_DT_SPEC_INST_GET(index, int_gpios), \
206
261
.reset_gpio = GPIO_DT_SPEC_INST_GET_OR(index, reset_gpios, {0}), \
207
262
}; \
208
263
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);
212
267
213
268
DT_INST_FOREACH_STATUS_OKAY (CHSC5X_DEFINE )
0 commit comments