1 // SPDX-License-Identifier: GPL-2.0
3 * Sensor HUB driver that discovers sensors behind a ChromeOS Embedded
6 * Copyright 2019 Google LLC
9 #include <linux/init.h>
10 #include <linux/device.h>
11 #include <linux/module.h>
12 #include <linux/platform_data/cros_ec_commands.h>
13 #include <linux/platform_data/cros_ec_proto.h>
14 #include <linux/platform_data/cros_ec_sensorhub.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
19 #define DRV_NAME "cros-ec-sensorhub"
21 static void cros_ec_sensorhub_free_sensor(void *arg
)
23 struct platform_device
*pdev
= arg
;
25 platform_device_unregister(pdev
);
28 static int cros_ec_sensorhub_allocate_sensor(struct device
*parent
,
32 struct cros_ec_sensor_platform sensor_platforms
= {
33 .sensor_num
= sensor_num
,
35 struct platform_device
*pdev
;
37 pdev
= platform_device_register_data(parent
, sensor_name
,
40 sizeof(sensor_platforms
));
44 return devm_add_action_or_reset(parent
,
45 cros_ec_sensorhub_free_sensor
,
49 static int cros_ec_sensorhub_register(struct device
*dev
,
50 struct cros_ec_sensorhub
*sensorhub
)
52 int sensor_type
[MOTIONSENSE_TYPE_MAX
] = { 0 };
53 struct cros_ec_command
*msg
= sensorhub
->msg
;
54 struct cros_ec_dev
*ec
= sensorhub
->ec
;
55 int ret
, i
, sensor_num
;
58 sensor_num
= cros_ec_get_sensor_count(ec
);
61 "Unable to retrieve sensor information (err:%d)\n",
66 sensorhub
->sensor_num
= sensor_num
;
67 if (sensor_num
== 0) {
68 dev_err(dev
, "Zero sensors reported.\n");
73 msg
->insize
= sizeof(struct ec_response_motion_sense
);
74 msg
->outsize
= sizeof(struct ec_params_motion_sense
);
76 for (i
= 0; i
< sensor_num
; i
++) {
77 sensorhub
->params
->cmd
= MOTIONSENSE_CMD_INFO
;
78 sensorhub
->params
->info
.sensor_num
= i
;
80 ret
= cros_ec_cmd_xfer_status(ec
->ec_dev
, msg
);
82 dev_warn(dev
, "no info for EC sensor %d : %d/%d\n",
87 switch (sensorhub
->resp
->info
.type
) {
88 case MOTIONSENSE_TYPE_ACCEL
:
89 name
= "cros-ec-accel";
91 case MOTIONSENSE_TYPE_BARO
:
92 name
= "cros-ec-baro";
94 case MOTIONSENSE_TYPE_GYRO
:
95 name
= "cros-ec-gyro";
97 case MOTIONSENSE_TYPE_MAG
:
100 case MOTIONSENSE_TYPE_PROX
:
101 name
= "cros-ec-prox";
103 case MOTIONSENSE_TYPE_LIGHT
:
104 name
= "cros-ec-light";
106 case MOTIONSENSE_TYPE_ACTIVITY
:
107 name
= "cros-ec-activity";
110 dev_warn(dev
, "unknown type %d\n",
111 sensorhub
->resp
->info
.type
);
115 ret
= cros_ec_sensorhub_allocate_sensor(dev
, name
, i
);
119 sensor_type
[sensorhub
->resp
->info
.type
]++;
122 if (sensor_type
[MOTIONSENSE_TYPE_ACCEL
] >= 2)
123 ec
->has_kb_wake_angle
= true;
125 if (cros_ec_check_features(ec
,
126 EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS
)) {
127 ret
= cros_ec_sensorhub_allocate_sensor(dev
,
137 static int cros_ec_sensorhub_probe(struct platform_device
*pdev
)
139 struct device
*dev
= &pdev
->dev
;
140 struct cros_ec_dev
*ec
= dev_get_drvdata(dev
->parent
);
141 struct cros_ec_sensorhub
*data
;
142 struct cros_ec_command
*msg
;
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
);
152 msg
->command
= EC_CMD_MOTION_SENSE_CMD
+ ec
->cmd_offset
;
154 data
= devm_kzalloc(dev
, sizeof(struct cros_ec_sensorhub
), GFP_KERNEL
);
158 mutex_init(&data
->cmd_lock
);
163 data
->params
= (struct ec_params_motion_sense
*)msg
->data
;
164 data
->resp
= (struct ec_response_motion_sense
*)msg
->data
;
166 dev_set_drvdata(dev
, data
);
168 /* Check whether this EC is a sensor hub. */
169 if (cros_ec_check_features(data
->ec
, EC_FEATURE_MOTION_SENSE
)) {
170 ret
= cros_ec_sensorhub_register(dev
, data
);
175 * If the device has sensors but does not claim to
176 * be a sensor hub, we are in legacy mode.
178 data
->sensor_num
= 2;
179 for (i
= 0; i
< data
->sensor_num
; i
++) {
180 ret
= cros_ec_sensorhub_allocate_sensor(dev
,
181 "cros-ec-accel-legacy", i
);
188 * If the EC does not have a FIFO, the sensors will query their data
189 * themselves via sysfs or a software trigger.
191 if (cros_ec_check_features(ec
, EC_FEATURE_MOTION_SENSE_FIFO
)) {
192 ret
= cros_ec_sensorhub_ring_add(data
);
196 * The msg and its data is not under the control of the ring
199 return devm_add_action_or_reset(dev
,
200 cros_ec_sensorhub_ring_remove
,
207 #ifdef CONFIG_PM_SLEEP
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.
213 static int cros_ec_sensorhub_suspend(struct device
*dev
)
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
;
219 if (cros_ec_check_features(ec
, EC_FEATURE_MOTION_SENSE_FIFO
))
220 return cros_ec_sensorhub_ring_fifo_enable(sensorhub
, false);
224 static int cros_ec_sensorhub_resume(struct device
*dev
)
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
;
230 if (cros_ec_check_features(ec
, EC_FEATURE_MOTION_SENSE_FIFO
))
231 return cros_ec_sensorhub_ring_fifo_enable(sensorhub
, true);
236 static SIMPLE_DEV_PM_OPS(cros_ec_sensorhub_pm_ops
,
237 cros_ec_sensorhub_suspend
,
238 cros_ec_sensorhub_resume
);
240 static struct platform_driver cros_ec_sensorhub_driver
= {
243 .pm
= &cros_ec_sensorhub_pm_ops
,
245 .probe
= cros_ec_sensorhub_probe
,
248 module_platform_driver(cros_ec_sensorhub_driver
);
250 MODULE_ALIAS("platform:" DRV_NAME
);
251 MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
252 MODULE_DESCRIPTION("ChromeOS EC MEMS Sensor Hub Driver");
253 MODULE_LICENSE("GPL");