Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

platform/chrome: cros_ec_dev - Register cros-ec sensors

Check whether the ChromeOS Embedded Controller is a sensor hub and in
such case issue a command to get the number of sensors and register them
all.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>
Reviewed-by: Guenter Roeck <groeck@chromium.org>
Acked-by: Olof Johansson <olof@lixom.net>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>

authored by

Enric Balletbo i Serra and committed by
Jonathan Cameron
5668bfdd e4244ebd

+124 -2
+2 -2
drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
··· 51 51 s16 data = 0; 52 52 s64 val64; 53 53 int i; 54 - int ret = IIO_VAL_INT; 54 + int ret; 55 55 int idx = chan->scan_index; 56 56 57 57 mutex_lock(&st->core.cmd_lock); ··· 137 137 { 138 138 struct cros_ec_sensors_state *st = iio_priv(indio_dev); 139 139 int i; 140 - int ret = 0; 140 + int ret; 141 141 int idx = chan->scan_index; 142 142 143 143 mutex_lock(&st->core.cmd_lock);
+122
drivers/platform/chrome/cros_ec_dev.c
··· 18 18 */ 19 19 20 20 #include <linux/fs.h> 21 + #include <linux/mfd/core.h> 21 22 #include <linux/module.h> 22 23 #include <linux/platform_device.h> 23 24 #include <linux/slab.h> ··· 266 265 kfree(ec); 267 266 } 268 267 268 + static void cros_ec_sensors_register(struct cros_ec_dev *ec) 269 + { 270 + /* 271 + * Issue a command to get the number of sensor reported. 272 + * Build an array of sensors driver and register them all. 273 + */ 274 + int ret, i, id, sensor_num; 275 + struct mfd_cell *sensor_cells; 276 + struct cros_ec_sensor_platform *sensor_platforms; 277 + int sensor_type[MOTIONSENSE_TYPE_MAX]; 278 + struct ec_params_motion_sense *params; 279 + struct ec_response_motion_sense *resp; 280 + struct cros_ec_command *msg; 281 + 282 + msg = kzalloc(sizeof(struct cros_ec_command) + 283 + max(sizeof(*params), sizeof(*resp)), GFP_KERNEL); 284 + if (msg == NULL) 285 + return; 286 + 287 + msg->version = 2; 288 + msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset; 289 + msg->outsize = sizeof(*params); 290 + msg->insize = sizeof(*resp); 291 + 292 + params = (struct ec_params_motion_sense *)msg->data; 293 + params->cmd = MOTIONSENSE_CMD_DUMP; 294 + 295 + ret = cros_ec_cmd_xfer(ec->ec_dev, msg); 296 + if (ret < 0 || msg->result != EC_RES_SUCCESS) { 297 + dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n", 298 + ret, msg->result); 299 + goto error; 300 + } 301 + 302 + resp = (struct ec_response_motion_sense *)msg->data; 303 + sensor_num = resp->dump.sensor_count; 304 + /* Allocate 2 extra sensors in case lid angle or FIFO are needed */ 305 + sensor_cells = kzalloc(sizeof(struct mfd_cell) * (sensor_num + 2), 306 + GFP_KERNEL); 307 + if (sensor_cells == NULL) 308 + goto error; 309 + 310 + sensor_platforms = kzalloc(sizeof(struct cros_ec_sensor_platform) * 311 + (sensor_num + 1), GFP_KERNEL); 312 + if (sensor_platforms == NULL) 313 + goto error_platforms; 314 + 315 + memset(sensor_type, 0, sizeof(sensor_type)); 316 + id = 0; 317 + for (i = 0; i < sensor_num; i++) { 318 + params->cmd = MOTIONSENSE_CMD_INFO; 319 + params->info.sensor_num = i; 320 + ret = cros_ec_cmd_xfer(ec->ec_dev, msg); 321 + if (ret < 0 || msg->result != EC_RES_SUCCESS) { 322 + dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n", 323 + i, ret, msg->result); 324 + continue; 325 + } 326 + switch (resp->info.type) { 327 + case MOTIONSENSE_TYPE_ACCEL: 328 + sensor_cells[id].name = "cros-ec-accel"; 329 + break; 330 + case MOTIONSENSE_TYPE_GYRO: 331 + sensor_cells[id].name = "cros-ec-gyro"; 332 + break; 333 + case MOTIONSENSE_TYPE_MAG: 334 + sensor_cells[id].name = "cros-ec-mag"; 335 + break; 336 + case MOTIONSENSE_TYPE_PROX: 337 + sensor_cells[id].name = "cros-ec-prox"; 338 + break; 339 + case MOTIONSENSE_TYPE_LIGHT: 340 + sensor_cells[id].name = "cros-ec-light"; 341 + break; 342 + case MOTIONSENSE_TYPE_ACTIVITY: 343 + sensor_cells[id].name = "cros-ec-activity"; 344 + break; 345 + default: 346 + dev_warn(ec->dev, "unknown type %d\n", resp->info.type); 347 + continue; 348 + } 349 + sensor_platforms[id].sensor_num = i; 350 + sensor_cells[id].id = sensor_type[resp->info.type]; 351 + sensor_cells[id].platform_data = &sensor_platforms[id]; 352 + sensor_cells[id].pdata_size = 353 + sizeof(struct cros_ec_sensor_platform); 354 + 355 + sensor_type[resp->info.type]++; 356 + id++; 357 + } 358 + if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2) { 359 + sensor_platforms[id].sensor_num = sensor_num; 360 + 361 + sensor_cells[id].name = "cros-ec-angle"; 362 + sensor_cells[id].id = 0; 363 + sensor_cells[id].platform_data = &sensor_platforms[id]; 364 + sensor_cells[id].pdata_size = 365 + sizeof(struct cros_ec_sensor_platform); 366 + id++; 367 + } 368 + if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) { 369 + sensor_cells[id].name = "cros-ec-ring"; 370 + id++; 371 + } 372 + 373 + ret = mfd_add_devices(ec->dev, 0, sensor_cells, id, 374 + NULL, 0, NULL); 375 + if (ret) 376 + dev_err(ec->dev, "failed to add EC sensors\n"); 377 + 378 + kfree(sensor_platforms); 379 + error_platforms: 380 + kfree(sensor_cells); 381 + error: 382 + kfree(msg); 383 + } 384 + 269 385 static int ec_device_probe(struct platform_device *pdev) 270 386 { 271 387 int retval = -ENOMEM; ··· 436 318 dev_err(dev, "device_register failed => %d\n", retval); 437 319 goto dev_reg_failed; 438 320 } 321 + 322 + /* check whether this EC is a sensor hub. */ 323 + if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE)) 324 + cros_ec_sensors_register(ec); 439 325 440 326 return 0; 441 327