From patchwork Thu Jan 4 20:17:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 10145621 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 8899D60329 for ; Thu, 4 Jan 2018 20:17:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7EC2828883 for ; Thu, 4 Jan 2018 20:17:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 73BAB28886; Thu, 4 Jan 2018 20:17:40 +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 C315D28883 for ; Thu, 4 Jan 2018 20:17:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752498AbeADURj (ORCPT ); Thu, 4 Jan 2018 15:17:39 -0500 Received: from mail-pg0-f65.google.com ([74.125.83.65]:34403 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751829AbeADURi (ORCPT ); Thu, 4 Jan 2018 15:17:38 -0500 Received: by mail-pg0-f65.google.com with SMTP id j4so1126737pgp.1 for ; Thu, 04 Jan 2018 12:17:38 -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=RBWgexveZq5G6wXZOLxSyhtUAB1nIga2zBxMFi9afiQ=; b=Q6dKSmktpJhYR64JyyTM3QIN0Cm/n9JYvMUWK5QC+PnqSVrZ5FH7rqDSSu5xz/5x90 nXIDTTa4t0eio1X5Qu7nFz3f/eaVObR1bhqBL+JYaOLsCB1bzSF+JDKedMweFWVLQk68 puSNjbo6G7FEiwVe01XOIY1vJgFHCU3VCC8Qq9ZMb7gK/bhwDK5PBIh9j8rCMb2Yzjdr eQmDmo8rdX1ziOZBBin/LSGwRfX05CAcClMZUE+kehkcD7Q9XlBBCgHfzIDQfNlYGjTz VRLXiQA/uNksumM3pWUxF/ASGAt/QExc5aGmLv8bP2ALKhcOZZGlN8A19YRfXqA4ySJT I4Rw== 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=RBWgexveZq5G6wXZOLxSyhtUAB1nIga2zBxMFi9afiQ=; b=hffCJAWm1oSLVgimni4eqwwk8/MYbb3s1xnl7OE7wW5IeBK3lXx3ttxr5wLnqeJGZ1 ZC0I/t/9Fer/JyaMGWOTTSzBRREMNhWXcoCJPAmGhNscc+D045Dv+SzXcK4D8CpvEZcK XzadsIhrequyOAgZ3w636s+E8sFSulxovjf/GS0PF1ku0R+KrbcZZvQVw7rvjamRcxXX QNppE6nrAv7fGvaFYdyCc5q7L5ja0h0K1/gxAPjWigCRjLZhPoFd34vfxF30rpIOQocO 6+mwLeu68i8ME9MWG+VY180VP7vLkj1icpyOkqaR0wAmghqxCEpeq684OVusCPuJfhNc GRZQ== X-Gm-Message-State: AKGB3mJ7Df7F1SI8OekhR1VmrGSXDFMlbn2jz/TRObrHU8RyfkHyMVsI hZm+DKfmG41wbP6c8frEGUlC6wnjdE0= X-Google-Smtp-Source: ACJfBov3veWdHbQUeleyNNii2yJX/GR4kYlQsRH3kNhy9ql84uMLBLzgVB144m7nqivc1aS1u7NI8Q== X-Received: by 10.98.76.136 with SMTP id e8mr660571pfj.215.1515097057594; Thu, 04 Jan 2018 12:17:37 -0800 (PST) Received: from gwendal.mtv.corp.google.com ([172.22.113.171]) by smtp.gmail.com with ESMTPSA id k12sm7648839pgc.29.2018.01.04.12.17.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 04 Jan 2018 12:17:36 -0800 (PST) From: Gwendal Grignou To: enric.balletbo@collabora.com, jic23@kernel.org, groeck@chromium.org Cc: linux-iio@vger.kernel.org Subject: [PATCH 3/3] iio: cros_ec: add barometer driver Date: Thu, 4 Jan 2018 12:17:18 -0800 Message-Id: <20180104201718.221900-4-gwendal@chromium.org> X-Mailer: git-send-email 2.16.0.rc0.223.g4a4ac83678-goog In-Reply-To: <20180104201718.221900-1-gwendal@chromium.org> References: <20180104201718.221900-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 a driver when EC presents a barometer. BUG=chrome-os-partner:57111 TEST=Check the values returned by the EC cat scale 0.040000000 cat in_pressure_raw 24097 scale * in_pressure_raw --> 963.88 hPa Reviewed-on: https://chromium-review.googlesource.com/382832 Reviewed-by: Dmitry Torokhov (cherry picked from commit 686988372cdc ("CHROMIUM: iio: cros_ec: add cros_ec barometer driver")) Signed-off-by: Gwendal Grignou --- drivers/iio/common/cros_ec_sensors/Kconfig | 7 + drivers/iio/common/cros_ec_sensors/Makefile | 1 + drivers/iio/common/cros_ec_sensors/cros_ec_baro.c | 222 ++++++++++++++++++++++ 3 files changed, 230 insertions(+) create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_baro.c diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig b/drivers/iio/common/cros_ec_sensors/Kconfig index b70138566870..f5c95198580f 100644 --- a/drivers/iio/common/cros_ec_sensors/Kconfig +++ b/drivers/iio/common/cros_ec_sensors/Kconfig @@ -21,6 +21,13 @@ config IIO_CROS_EC_SENSORS presented by the ChromeOS EC Sensor hub. Creates an IIO device for each functions. +config IIO_CROS_EC_BARO + tristate "ChromeOS EC Barometer Sensor" + depends on IIO_CROS_EC_SENSORS_CORE + help + Module to handle Barometer sensor when + presented by the ChromeOS EC Sensor hub. + config IIO_CROS_EC_LIGHT_PROX tristate "ChromeOS EC Light and Proximity Sensors" depends on IIO_CROS_EC_SENSORS_CORE diff --git a/drivers/iio/common/cros_ec_sensors/Makefile b/drivers/iio/common/cros_ec_sensors/Makefile index 7aaf2a2dcdbd..71103495aada 100644 --- a/drivers/iio/common/cros_ec_sensors/Makefile +++ b/drivers/iio/common/cros_ec_sensors/Makefile @@ -4,4 +4,5 @@ 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_BARO) += cros_ec_baro.o obj-$(CONFIG_IIO_CROS_EC_LIGHT_PROX) += cros_ec_light_prox.o diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_baro.c b/drivers/iio/common/cros_ec_sensors/cros_ec_baro.c new file mode 100644 index 000000000000..fcbc27b556cf --- /dev/null +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_baro.c @@ -0,0 +1,222 @@ +/* + * cros_ec_baro - Driver for barometer sensor behind 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" + +/* + * One channel for pressure, the other for timestamp. + */ +#define MAX_CHANNELS (1 + 1) + +/* State data for ec_sensors iio driver. */ +struct cros_ec_baro_state { + /* Shared by all sensors */ + struct cros_ec_sensors_core_state core; + + struct iio_chan_spec channels[MAX_CHANNELS]; +}; + +static int cros_ec_baro_read(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct cros_ec_baro_state *st = iio_priv(indio_dev); + u16 data = 0; + 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_SCALE: + 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; + + *val = st->core.resp->sensor_range.ret; + /* EC return value in Pa, convert to hPa */ + *val2 = 100 << CROS_EC_SENSOR_BITS; + ret = IIO_VAL_FRACTIONAL; + 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_baro_write(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, long mask) +{ + struct cros_ec_baro_state *st = iio_priv(indio_dev); + int ret = 0; + + mutex_lock(&st->core.cmd_lock); + + switch (mask) { + case IIO_CHAN_INFO_SCALE: + st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; + st->core.param.sensor_range.data = val; + + /* Always roundup, so caller gets at least what it asks for. */ + st->core.param.sensor_range.roundup = 1; + + 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_baro_info = { + .read_raw = &cros_ec_baro_read, + .write_raw = &cros_ec_baro_write, + .driver_module = THIS_MODULE, +}; + +static int cros_ec_baro_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent); + struct cros_ec_device *ec_device; + struct iio_dev *indio_dev; + struct cros_ec_baro_state *state; + struct iio_chan_spec *channel; + int ret; + + if (!ec_dev || !ec_dev->ec_dev) { + dev_warn(dev, "No CROS EC device found.\n"); + return -EINVAL; + } + ec_device = ec_dev->ec_dev; + + 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_baro_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); + 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_BARO: + channel->type = IIO_PRESSURE; + 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_baro_ids[] = { + { + .name = "cros-ec-baro", + }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(platform, cros_ec_baro_ids); + +static struct platform_driver cros_ec_baro_platform_driver = { + .driver = { + .name = "cros-ec-baro", + .pm = &cros_ec_sensors_pm_ops, + }, + .probe = cros_ec_baro_probe, + .id_table = cros_ec_baro_ids, +}; +module_platform_driver(cros_ec_baro_platform_driver); + +MODULE_DESCRIPTION("ChromeOS EC barometer sensor driver"); +MODULE_LICENSE("GPL v2");