1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2019 Google LLC
6 #include <linux/errno.h>
7 #include <linux/export.h>
8 #include <linux/platform_data/wilco-ec.h>
9 #include <linux/string.h>
10 #include <linux/types.h>
11 #include <asm/unaligned.h>
13 /* Operation code; what the EC should do with the property */
19 struct ec_property_request
{
20 u8 op
; /* One of enum ec_property_op */
21 u8 property_id
[4]; /* The 32 bit PID is stored Little Endian */
23 u8 data
[WILCO_EC_PROPERTY_MAX_SIZE
];
26 struct ec_property_response
{
28 u8 op
; /* One of enum ec_property_op */
29 u8 property_id
[4]; /* The 32 bit PID is stored Little Endian */
31 u8 data
[WILCO_EC_PROPERTY_MAX_SIZE
];
34 static int send_property_msg(struct wilco_ec_device
*ec
,
35 struct ec_property_request
*rq
,
36 struct ec_property_response
*rs
)
38 struct wilco_ec_message ec_msg
;
41 memset(&ec_msg
, 0, sizeof(ec_msg
));
42 ec_msg
.type
= WILCO_EC_MSG_PROPERTY
;
43 ec_msg
.request_data
= rq
;
44 ec_msg
.request_size
= sizeof(*rq
);
45 ec_msg
.response_data
= rs
;
46 ec_msg
.response_size
= sizeof(*rs
);
48 ret
= wilco_ec_mailbox(ec
, &ec_msg
);
53 if (memcmp(rq
->property_id
, rs
->property_id
, sizeof(rs
->property_id
)))
59 int wilco_ec_get_property(struct wilco_ec_device
*ec
,
60 struct wilco_ec_property_msg
*prop_msg
)
62 struct ec_property_request rq
;
63 struct ec_property_response rs
;
66 memset(&rq
, 0, sizeof(rq
));
68 put_unaligned_le32(prop_msg
->property_id
, rq
.property_id
);
70 ret
= send_property_msg(ec
, &rq
, &rs
);
74 prop_msg
->length
= rs
.length
;
75 memcpy(prop_msg
->data
, rs
.data
, rs
.length
);
79 EXPORT_SYMBOL_GPL(wilco_ec_get_property
);
81 int wilco_ec_set_property(struct wilco_ec_device
*ec
,
82 struct wilco_ec_property_msg
*prop_msg
)
84 struct ec_property_request rq
;
85 struct ec_property_response rs
;
88 memset(&rq
, 0, sizeof(rq
));
90 put_unaligned_le32(prop_msg
->property_id
, rq
.property_id
);
91 rq
.length
= prop_msg
->length
;
92 memcpy(rq
.data
, prop_msg
->data
, prop_msg
->length
);
94 ret
= send_property_msg(ec
, &rq
, &rs
);
97 if (rs
.length
!= prop_msg
->length
)
102 EXPORT_SYMBOL_GPL(wilco_ec_set_property
);
104 int wilco_ec_get_byte_property(struct wilco_ec_device
*ec
, u32 property_id
,
107 struct wilco_ec_property_msg msg
;
110 msg
.property_id
= property_id
;
112 ret
= wilco_ec_get_property(ec
, &msg
);
122 EXPORT_SYMBOL_GPL(wilco_ec_get_byte_property
);
124 int wilco_ec_set_byte_property(struct wilco_ec_device
*ec
, u32 property_id
,
127 struct wilco_ec_property_msg msg
;
129 msg
.property_id
= property_id
;
133 return wilco_ec_set_property(ec
, &msg
);
135 EXPORT_SYMBOL_GPL(wilco_ec_set_byte_property
);