Skip to content

Commit 145d59b

Browse files
gwendalcrEnric Balletbo i Serra
authored andcommitted
platform/chrome: cros_ec_sensorhub: Add FIFO support
cros_ec_sensorhub registers a listener and query motion sense FIFO, spread to iio sensors registers. To test, we can use libiio: iiod& iio_readdev -u ip:localhost -T 10000 -s 25 -b 16 cros-ec-gyro | od -x Signed-off-by: Gwendal Grignou <[email protected]> Reviewed-by: Jonathan Cameron <[email protected]> Acked-by: Andy Shevchenko <[email protected]> Signed-off-by: Enric Balletbo i Serra <[email protected]>
1 parent cee416a commit 145d59b

File tree

4 files changed

+597
-28
lines changed

4 files changed

+597
-28
lines changed

drivers/platform/chrome/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ obj-$(CONFIG_CROS_EC_CHARDEV) += cros_ec_chardev.o
2020
obj-$(CONFIG_CROS_EC_LIGHTBAR) += cros_ec_lightbar.o
2121
obj-$(CONFIG_CROS_EC_VBC) += cros_ec_vbc.o
2222
obj-$(CONFIG_CROS_EC_DEBUGFS) += cros_ec_debugfs.o
23-
obj-$(CONFIG_CROS_EC_SENSORHUB) += cros_ec_sensorhub.o
23+
cros-ec-sensorhub-objs := cros_ec_sensorhub.o cros_ec_sensorhub_ring.o
24+
obj-$(CONFIG_CROS_EC_SENSORHUB) += cros-ec-sensorhub.o
2425
obj-$(CONFIG_CROS_EC_SYSFS) += cros_ec_sysfs.o
2526
obj-$(CONFIG_CROS_USBPD_LOGGER) += cros_usbpd_logger.o
2627
obj-$(CONFIG_CROS_USBPD_NOTIFY) += cros_usbpd_notify.o

drivers/platform/chrome/cros_ec_sensorhub.c

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ static int cros_ec_sensorhub_register(struct device *dev,
5050
struct cros_ec_sensorhub *sensorhub)
5151
{
5252
int sensor_type[MOTIONSENSE_TYPE_MAX] = { 0 };
53+
struct cros_ec_command *msg = sensorhub->msg;
5354
struct cros_ec_dev *ec = sensorhub->ec;
54-
struct ec_params_motion_sense *params;
55-
struct ec_response_motion_sense *resp;
56-
struct cros_ec_command *msg;
5755
int ret, i, sensor_num;
5856
char *name;
5957

@@ -71,22 +69,13 @@ static int cros_ec_sensorhub_register(struct device *dev,
7169
return -EINVAL;
7270
}
7371

74-
/* Prepare a message to send INFO command to each sensor. */
75-
msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*resp)),
76-
GFP_KERNEL);
77-
if (!msg)
78-
return -ENOMEM;
79-
8072
msg->version = 1;
81-
msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
82-
msg->outsize = sizeof(*params);
83-
msg->insize = sizeof(*resp);
84-
params = (struct ec_params_motion_sense *)msg->data;
85-
resp = (struct ec_response_motion_sense *)msg->data;
73+
msg->insize = sizeof(struct ec_response_motion_sense);
74+
msg->outsize = sizeof(struct ec_params_motion_sense);
8675

8776
for (i = 0; i < sensor_num; i++) {
88-
params->cmd = MOTIONSENSE_CMD_INFO;
89-
params->info.sensor_num = i;
77+
sensorhub->params->cmd = MOTIONSENSE_CMD_INFO;
78+
sensorhub->params->info.sensor_num = i;
9079

9180
ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
9281
if (ret < 0) {
@@ -95,7 +84,7 @@ static int cros_ec_sensorhub_register(struct device *dev,
9584
continue;
9685
}
9786

98-
switch (resp->info.type) {
87+
switch (sensorhub->resp->info.type) {
9988
case MOTIONSENSE_TYPE_ACCEL:
10089
name = "cros-ec-accel";
10190
break;
@@ -118,15 +107,16 @@ static int cros_ec_sensorhub_register(struct device *dev,
118107
name = "cros-ec-activity";
119108
break;
120109
default:
121-
dev_warn(dev, "unknown type %d\n", resp->info.type);
110+
dev_warn(dev, "unknown type %d\n",
111+
sensorhub->resp->info.type);
122112
continue;
123113
}
124114

125115
ret = cros_ec_sensorhub_allocate_sensor(dev, name, i);
126116
if (ret)
127-
goto error;
117+
return ret;
128118

129-
sensor_type[resp->info.type]++;
119+
sensor_type[sensorhub->resp->info.type]++;
130120
}
131121

132122
if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
@@ -138,29 +128,41 @@ static int cros_ec_sensorhub_register(struct device *dev,
138128
"cros-ec-lid-angle",
139129
0);
140130
if (ret)
141-
goto error;
131+
return ret;
142132
}
143133

144-
kfree(msg);
145134
return 0;
146-
147-
error:
148-
kfree(msg);
149-
return ret;
150135
}
151136

152137
static int cros_ec_sensorhub_probe(struct platform_device *pdev)
153138
{
154139
struct device *dev = &pdev->dev;
140+
struct cros_ec_dev *ec = dev_get_drvdata(dev->parent);
155141
struct cros_ec_sensorhub *data;
142+
struct cros_ec_command *msg;
156143
int ret;
157144
int i;
158145

146+
msg = devm_kzalloc(dev, sizeof(struct cros_ec_command) +
147+
max((u16)sizeof(struct ec_params_motion_sense),
148+
ec->ec_dev->max_response), GFP_KERNEL);
149+
if (!msg)
150+
return -ENOMEM;
151+
152+
msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
153+
159154
data = devm_kzalloc(dev, sizeof(struct cros_ec_sensorhub), GFP_KERNEL);
160155
if (!data)
161156
return -ENOMEM;
162157

163-
data->ec = dev_get_drvdata(dev->parent);
158+
mutex_init(&data->cmd_lock);
159+
160+
data->dev = dev;
161+
data->ec = ec;
162+
data->msg = msg;
163+
data->params = (struct ec_params_motion_sense *)msg->data;
164+
data->resp = (struct ec_response_motion_sense *)msg->data;
165+
164166
dev_set_drvdata(dev, data);
165167

166168
/* Check whether this EC is a sensor hub. */
@@ -182,12 +184,63 @@ static int cros_ec_sensorhub_probe(struct platform_device *pdev)
182184
}
183185
}
184186

187+
/*
188+
* If the EC does not have a FIFO, the sensors will query their data
189+
* themselves via sysfs or a software trigger.
190+
*/
191+
if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
192+
ret = cros_ec_sensorhub_ring_add(data);
193+
if (ret)
194+
return ret;
195+
/*
196+
* The msg and its data is not under the control of the ring
197+
* handler.
198+
*/
199+
return devm_add_action_or_reset(dev,
200+
cros_ec_sensorhub_ring_remove,
201+
data);
202+
}
203+
204+
return 0;
205+
}
206+
207+
#ifdef CONFIG_PM_SLEEP
208+
/*
209+
* When the EC is suspending, we must stop sending interrupt,
210+
* we may use the same interrupt line for waking up the device.
211+
* Tell the EC to stop sending non-interrupt event on the iio ring.
212+
*/
213+
static int cros_ec_sensorhub_suspend(struct device *dev)
214+
{
215+
struct platform_device *pdev = to_platform_device(dev);
216+
struct cros_ec_sensorhub *sensorhub = platform_get_drvdata(pdev);
217+
struct cros_ec_dev *ec = sensorhub->ec;
218+
219+
if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO))
220+
return cros_ec_sensorhub_ring_fifo_enable(sensorhub, false);
185221
return 0;
186222
}
187223

224+
static int cros_ec_sensorhub_resume(struct device *dev)
225+
{
226+
struct platform_device *pdev = to_platform_device(dev);
227+
struct cros_ec_sensorhub *sensorhub = platform_get_drvdata(pdev);
228+
struct cros_ec_dev *ec = sensorhub->ec;
229+
230+
if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO))
231+
return cros_ec_sensorhub_ring_fifo_enable(sensorhub, true);
232+
return 0;
233+
}
234+
#endif
235+
236+
static SIMPLE_DEV_PM_OPS(cros_ec_sensorhub_pm_ops,
237+
cros_ec_sensorhub_suspend,
238+
cros_ec_sensorhub_resume);
239+
188240
static struct platform_driver cros_ec_sensorhub_driver = {
189241
.driver = {
190242
.name = DRV_NAME,
243+
.pm = &cros_ec_sensorhub_pm_ops,
191244
},
192245
.probe = cros_ec_sensorhub_probe,
193246
};

0 commit comments

Comments
 (0)