From patchwork Fri Jan 5 00:32:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 10145831 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 28F38601A1 for ; Fri, 5 Jan 2018 00:32:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5A98628803 for ; Fri, 5 Jan 2018 00:32:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4F12128846; Fri, 5 Jan 2018 00:32:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8845028803 for ; Fri, 5 Jan 2018 00:32:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751302AbeAEAcv (ORCPT ); Thu, 4 Jan 2018 19:32:51 -0500 Received: from mail-pl0-f66.google.com ([209.85.160.66]:40298 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751116AbeAEAcu (ORCPT ); Thu, 4 Jan 2018 19:32:50 -0500 Received: by mail-pl0-f66.google.com with SMTP id 62so2057335pld.7 for ; Thu, 04 Jan 2018 16:32:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=L4wG9oNmck5kuaMLwZEsaL6GH780cDQMDQzd/Ph+M5s=; b=MjlcPP+41X1Dn5th02MDEevmLSUG+Ss66Jd2fz7OEoctf0wz1JCmE+NLWJZs3zX/CU eJ0maDNvmv1GOQ0ssWTV6SX4SKb4XQZNdS61IibkybVz5tg7feg7d/saiPPAjiq8wWNH D5SQgHy3bYV/mjHvvsGO+/L6x2f6jacpizEp/y6jjucmashscos0e2BuUP9AwmNo6XsT eRjIDU+706BEZqOasAz8hyt7i/DmWbWTTgjT8TEgRCUHipdR1vTgvW2/jlCJOk/JoSXD dxtVvaUaIvYOvsXMAwweCixz1QYeyq+5MfqOIvS1LzHMZrji8JeSxHEfAwx8V8gnO5vi 3oVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=L4wG9oNmck5kuaMLwZEsaL6GH780cDQMDQzd/Ph+M5s=; b=TjFuoJDvqRs2WEEJxSKVdVWa/3rqyyVLVTINyyVr6fgNNdpGQWevgcEGSVqDEBmBkt 5+YiChQJKzhcYDrR4YzZC2bIkLeYfxm92M5cpQ1GLlAf1dKq7AJH2OGIJEbsgOilPawz YDh9n4u1kde5c+xM86Cbw231gLOchezGNIKBs/sk0kZmb1Uz5U4Anb++Z6OkCFGVZB8S bc9FG6CoafsCMa5AUJFzu6y4dVubL5haR9QXOkexSQsDNnwsxAxj9XGy0/NXZFspA722 godQmd/o39P8p2GwZ/VU5oxUMzc8nWX38e2omBWgGgumfNeCOmtTE/cjyPC5IpyOC00K 07BA== X-Gm-Message-State: AKGB3mLwzG74VhPi0yuyEAFi2vbv49ngLm3m225qyte2hF2/1oo9RVSf GMflXglaUSpp2HES3hoSFj7KpQ== X-Google-Smtp-Source: ACJfBotpuBphJfUeKPMS0M1j/L7W+DHH6FB3BW6Yi0Jv4WoFVlr0LnBDPdd4cS0xZ0SeZrLSGCrJtg== X-Received: by 10.159.253.143 with SMTP id q15mr1269009pls.32.1515112369473; Thu, 04 Jan 2018 16:32:49 -0800 (PST) Received: from gwendal.mtv.corp.google.com ([172.22.113.171]) by smtp.gmail.com with ESMTPSA id g10sm11593599pfe.77.2018.01.04.16.32.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 04 Jan 2018 16:32:48 -0800 (PST) From: Gwendal Grignou To: enric.balletbo@collabora.com, jic23@kernel.org, groeck@chromium.org Cc: linux-iio@vger.kernel.org Subject: [PATCH v2 2/3] iio: cros_ec: Add EC driver for Light and Proximity Date: Thu, 4 Jan 2018 16:32:41 -0800 Message-Id: <20180105003242.11290-2-gwendal@chromium.org> X-Mailer: git-send-email 2.16.0.rc0.223.g4a4ac83678-goog In-Reply-To: <20180105003242.11290-1-gwendal@chromium.org> References: <20180105003242.11290-1-gwendal@chromium.org> Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add driver for light and proximity sensors exposed by EC. Reviewed-on: https://chromium-review.googlesource.com/291360 Reviewed-by: Dmitry Torokhov [rebase44(groeck): Adjust for API changes] [rebase414(gwendal): Adjust for API changes] Signed-off-by: Guenter Roeck (cherry picked from commit 17e781c5f2747236ea1f9de6906e04aa92d383cb) Signed-off-by: Gwendal Grignou --- drivers/iio/common/cros_ec_sensors/Kconfig | 9 + drivers/iio/common/cros_ec_sensors/Makefile | 1 + .../common/cros_ec_sensors/cros_ec_light_prox.c | 260 +++++++++++++++++++++ 3 files changed, 270 insertions(+) create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_light_prox.c diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig b/drivers/iio/common/cros_ec_sensors/Kconfig index 135f6825903f..b70138566870 100644 --- a/drivers/iio/common/cros_ec_sensors/Kconfig +++ b/drivers/iio/common/cros_ec_sensors/Kconfig @@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS Accelerometers, Gyroscope and Magnetometer that are presented by the ChromeOS EC Sensor hub. Creates an IIO device for each functions. + +config IIO_CROS_EC_LIGHT_PROX + tristate "ChromeOS EC Light and Proximity Sensors" + depends on IIO_CROS_EC_SENSORS_CORE + help + Module to handle Light and Proximity sensors + presented by the ChromeOS EC Sensor hub. + Create an IIO device for each funcions. + Only one source is exposed by the EC. diff --git a/drivers/iio/common/cros_ec_sensors/Makefile b/drivers/iio/common/cros_ec_sensors/Makefile index ec716ff2a775..7aaf2a2dcdbd 100644 --- a/drivers/iio/common/cros_ec_sensors/Makefile +++ b/drivers/iio/common/cros_ec_sensors/Makefile @@ -4,3 +4,4 @@ obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o +obj-$(CONFIG_IIO_CROS_EC_LIGHT_PROX) += cros_ec_light_prox.o diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_light_prox.c b/drivers/iio/common/cros_ec_sensors/cros_ec_light_prox.c new file mode 100644 index 000000000000..17a19e918a38 --- /dev/null +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_light_prox.c @@ -0,0 +1,260 @@ +/* + * cros_ec_light_proxmity - Driver for light and prox sensors behing CrOS EC. + * + * Copyright (C) 2015 Google, Inc + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * This driver uses the cros-ec interface to communicate with the Chrome OS + * EC about accelerometer data. Accelerometer access is presented through + * iio sysfs. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cros_ec_sensors_core.h" + +/* + * We only represent one entry for light or proximity. + * EC is merging different light sensors to return the + * what the eye would see. + * For proximity, we currently support only one light source. + */ +#define MAX_CHANNELS (1 + 1) + +/* State data for ec_sensors iio driver. */ +struct cros_ec_light_prox_state { + /* Shared by all sensors */ + struct cros_ec_sensors_core_state core; + + struct iio_chan_spec channels[MAX_CHANNELS]; +}; + +static int cros_ec_light_prox_read(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct cros_ec_light_prox_state *st = iio_priv(indio_dev); + u16 data = 0; + s64 val64; + int ret = IIO_VAL_INT; + int idx = chan->scan_index; + + mutex_lock(&st->core.cmd_lock); + + switch (mask) { + case IIO_CHAN_INFO_RAW: + if (cros_ec_sensors_read_cmd(indio_dev, 1 << idx, + (s16 *)&data) < 0) + ret = -EIO; + *val = data; + break; + case IIO_CHAN_INFO_CALIBBIAS: + st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET; + st->core.param.sensor_offset.flags = 0; + + ret = cros_ec_motion_send_host_cmd(&st->core, 0); + if (ret != 0) + break; + + /* Save values */ + st->core.calib[0] = st->core.resp->sensor_offset.offset[0]; + + *val = st->core.calib[idx]; + break; + case IIO_CHAN_INFO_CALIBSCALE: + /* + * RANGE is used for calibration + * scalse is a number x.y, where x is coded on 16bits, + * y coded on 16 bits, between 0 and 9999. + */ + st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; + st->core.param.sensor_range.data = + EC_MOTION_SENSE_NO_VALUE; + + ret = cros_ec_motion_send_host_cmd(&st->core, 0); + if (ret != 0) + break; + + val64 = st->core.resp->sensor_range.ret; + *val = val64 >> 16; + *val2 = (val64 & 0xffff) * 100; + ret = IIO_VAL_INT_PLUS_MICRO; + break; + case IIO_CHAN_INFO_SCALE: + /* Light: Result in Lux, using calibration multiplier */ + /* Prox: Result in cm. */ + *val = 1; + ret = IIO_VAL_INT; + break; + default: + ret = cros_ec_sensors_core_read( + &st->core, chan, val, val2, mask); + break; + } + mutex_unlock(&st->core.cmd_lock); + return ret; +} + +static int cros_ec_light_prox_write(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, long mask) +{ + struct cros_ec_light_prox_state *st = iio_priv(indio_dev); + int ret = 0; + int idx = chan->scan_index; + + mutex_lock(&st->core.cmd_lock); + + switch (mask) { + case IIO_CHAN_INFO_CALIBBIAS: + st->core.calib[idx] = val; + /* Send to EC for each axis, even if not complete */ + + st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET; + st->core.param.sensor_offset.flags = + MOTION_SENSE_SET_OFFSET; + st->core.param.sensor_offset.offset[0] = st->core.calib[0]; + st->core.param.sensor_offset.temp = + EC_MOTION_SENSE_INVALID_CALIB_TEMP; + + ret = cros_ec_motion_send_host_cmd(&st->core, 0); + break; + case IIO_CHAN_INFO_CALIBSCALE: + st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; + st->core.param.sensor_range.data = (val << 16) | (val2 / 100); + ret = cros_ec_motion_send_host_cmd(&st->core, 0); + break; + default: + ret = cros_ec_sensors_core_write( + &st->core, chan, val, val2, mask); + break; + } + + mutex_unlock(&st->core.cmd_lock); + return ret; +} + +static const struct iio_info cros_ec_light_prox_info = { + .read_raw = &cros_ec_light_prox_read, + .write_raw = &cros_ec_light_prox_write, + .driver_module = THIS_MODULE, +}; + +static int cros_ec_light_prox_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct iio_dev *indio_dev; + struct cros_ec_light_prox_state *state; + struct iio_chan_spec *channel; + int ret; + + indio_dev = devm_iio_device_alloc(dev, sizeof(*state)); + if (!indio_dev) + return -ENOMEM; + + ret = cros_ec_sensors_core_init(pdev, indio_dev, true); + if (ret) + return ret; + + indio_dev->info = &cros_ec_light_prox_info; + state = iio_priv(indio_dev); + state->core.type = state->core.resp->info.type; + state->core.loc = state->core.resp->info.location; + channel = state->channels; + /* common part */ + channel->info_mask_separate = + BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_CALIBBIAS) | + BIT(IIO_CHAN_INFO_CALIBSCALE); + channel->info_mask_shared_by_all = + BIT(IIO_CHAN_INFO_SCALE) | + BIT(IIO_CHAN_INFO_SAMP_FREQ) | + BIT(IIO_CHAN_INFO_FREQUENCY); + channel->scan_type.realbits = CROS_EC_SENSOR_BITS; + channel->scan_type.storagebits = CROS_EC_SENSOR_BITS; + channel->scan_type.shift = 0; + channel->scan_index = 0; + channel->ext_info = cros_ec_sensors_ext_info; + channel->scan_type.sign = 'u'; + + state->core.calib[0] = 0; + + /* sensor specific */ + switch (state->core.type) { + case MOTIONSENSE_TYPE_LIGHT: + channel->type = IIO_LIGHT; + break; + case MOTIONSENSE_TYPE_PROX: + channel->type = IIO_PROXIMITY; + break; + default: + dev_warn(dev, "unknown\n"); + return -EINVAL; + } + + /* Timestamp */ + channel++; + channel->type = IIO_TIMESTAMP; + channel->channel = -1; + channel->scan_index = 1; + channel->scan_type.sign = 's'; + channel->scan_type.realbits = 64; + channel->scan_type.storagebits = 64; + + indio_dev->channels = state->channels; + indio_dev->num_channels = MAX_CHANNELS; + + state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd; + + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, + cros_ec_sensors_capture, NULL); + if (ret) + return ret; + + return devm_iio_device_register(dev, indio_dev); +} + +static const struct platform_device_id cros_ec_light_prox_ids[] = { + { + .name = "cros-ec-prox", + }, + { + .name = "cros-ec-light", + }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(platform, cros_ec_light_prox_ids); + +static struct platform_driver cros_ec_light_prox_platform_driver = { + .driver = { + .name = "cros-ec-light-prox", + .pm = &cros_ec_sensors_pm_ops, + }, + .probe = cros_ec_light_prox_probe, + .id_table = cros_ec_light_prox_ids, +}; +module_platform_driver(cros_ec_light_prox_platform_driver); + +MODULE_DESCRIPTION("ChromeOS EC light/proximity sensors driver"); +MODULE_LICENSE("GPL v2");