From patchwork Mon Apr 27 15:50:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 11512861 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D69E81667 for ; Mon, 27 Apr 2020 15:50:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BFF5520661 for ; Mon, 27 Apr 2020 15:50:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dOIq3hqb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728255AbgD0Put (ORCPT ); Mon, 27 Apr 2020 11:50:49 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:41577 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728150AbgD0Put (ORCPT ); Mon, 27 Apr 2020 11:50:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588002648; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=m4LhHtyrKd+39y/Gc4fHI3CQInRvTn8zsnj+tp4DOSw=; b=dOIq3hqb/Joy3xSWitmtAr4P5Vhpq+GLkJQ3xy5cKO8qyCukO7Ts6J03IkiFqgYonphpnS qRNDbWDHeCZTOSgP8bJuuozMhXhxF9LZyoKVSmolZHwCU/cAtDTSyea5elUqJ54D+JwG0L KbW2ycs6mUxg3ZEW+Tm5h8/IElfsPp4= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-60-0-6MWkl_NIyhUHcVk8NCHQ-1; Mon, 27 Apr 2020 11:50:44 -0400 X-MC-Unique: 0-6MWkl_NIyhUHcVk8NCHQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BF5B445F; Mon, 27 Apr 2020 15:50:41 +0000 (UTC) Received: from x1.localdomain.com (ovpn-114-38.ams2.redhat.com [10.36.114.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id C6D3760BEC; Mon, 27 Apr 2020 15:50:38 +0000 (UTC) From: Hans de Goede To: "Rafael J . Wysocki" , Len Brown , Darren Hart , Andy Shevchenko , Jonathan Cameron Cc: Hans de Goede , linux-acpi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , linux-iio@vger.kernel.org, Andy Shevchenko Subject: [PATCH v2 1/8] iio: light: cm32181: Add some extra register defines Date: Mon, 27 Apr 2020 17:50:30 +0200 Message-Id: <20200427155037.218390-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org These come from a newer version of cm32181.c, which is floating around the net, with a copyright of: * Copyright (C) 2014 Capella Microsystems Inc. * Author: Kevin Tsai * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2, as published * by the Free Software Foundation. Note that this removes the bogus CM32181_CMD_ALS_ENABLE define, there is no enable bit, only a disable bit and enabled is the absence of being disabled. This is a preparation patch for adding support for the older CM3218 model of the light sensor. Reviewed-by: Andy Shevchenko Signed-off-by: Hans de Goede --- drivers/iio/light/cm32181.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c index 5f4fb5674fa0..ee386afe811e 100644 --- a/drivers/iio/light/cm32181.c +++ b/drivers/iio/light/cm32181.c @@ -18,6 +18,9 @@ /* Registers Address */ #define CM32181_REG_ADDR_CMD 0x00 +#define CM32181_REG_ADDR_WH 0x01 +#define CM32181_REG_ADDR_WL 0x02 +#define CM32181_REG_ADDR_TEST 0x03 #define CM32181_REG_ADDR_ALS 0x04 #define CM32181_REG_ADDR_STATUS 0x06 #define CM32181_REG_ADDR_ID 0x07 @@ -26,9 +29,13 @@ #define CM32181_CONF_REG_NUM 0x01 /* CMD register */ -#define CM32181_CMD_ALS_ENABLE 0x00 -#define CM32181_CMD_ALS_DISABLE 0x01 -#define CM32181_CMD_ALS_INT_EN 0x02 +#define CM32181_CMD_ALS_DISABLE BIT(0) +#define CM32181_CMD_ALS_INT_EN BIT(1) +#define CM32181_CMD_ALS_THRES_WINDOW BIT(2) + +#define CM32181_CMD_ALS_PERS_SHIFT 4 +#define CM32181_CMD_ALS_PERS_MASK (0x03 << CM32181_CMD_ALS_PERS_SHIFT) +#define CM32181_CMD_ALS_PERS_DEFAULT (0x01 << CM32181_CMD_ALS_PERS_SHIFT) #define CM32181_CMD_ALS_IT_SHIFT 6 #define CM32181_CMD_ALS_IT_MASK (0x0F << CM32181_CMD_ALS_IT_SHIFT) @@ -82,7 +89,7 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181) return -ENODEV; /* Default Values */ - cm32181->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_ALS_ENABLE | + cm32181->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT; cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT; From patchwork Mon Apr 27 15:50:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 11512863 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 449A015E6 for ; Mon, 27 Apr 2020 15:50:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2CC782064C for ; Mon, 27 Apr 2020 15:50:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="bXTOxAZx" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727073AbgD0Pux (ORCPT ); Mon, 27 Apr 2020 11:50:53 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:39667 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728200AbgD0Puu (ORCPT ); Mon, 27 Apr 2020 11:50:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588002648; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y+CeP7WyXZKic90JnWrLSoBezsuvSDNMBm6bp9t0PxY=; b=bXTOxAZxlMlO/S+BseJt7sfNo2HCbYJX/PNH9pBFzjkyo2yNc5URo9J2lH7Ox7de2JL3vO j9mpIbzLvnbohbxN3XSKD3StX2QIOBHuHSaWH7mHfz735m9a/k5pqRHjuI0GVCH+lhhJSP rGOuh2LEZESqvMO7+iKe5ageLD6VwX4= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-351-BqsFFKk2Ojyyu708MsE4sQ-1; Mon, 27 Apr 2020 11:50:46 -0400 X-MC-Unique: BqsFFKk2Ojyyu708MsE4sQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A098C800685; Mon, 27 Apr 2020 15:50:44 +0000 (UTC) Received: from x1.localdomain.com (ovpn-114-38.ams2.redhat.com [10.36.114.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id 13A9860D38; Mon, 27 Apr 2020 15:50:41 +0000 (UTC) From: Hans de Goede To: "Rafael J . Wysocki" , Len Brown , Darren Hart , Andy Shevchenko , Jonathan Cameron Cc: Hans de Goede , linux-acpi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , linux-iio@vger.kernel.org, Andy Shevchenko Subject: [PATCH v2 2/8] iio: light: cm32181: Add support for the CM3218 Date: Mon, 27 Apr 2020 17:50:31 +0200 Message-Id: <20200427155037.218390-2-hdegoede@redhat.com> In-Reply-To: <20200427155037.218390-1-hdegoede@redhat.com> References: <20200427155037.218390-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Add support for the CM3218 which is an older version of the CM32181. This is based on a newer version of cm32181.c, with a copyright of: * Copyright (C) 2014 Capella Microsystems Inc. * Author: Kevin Tsai * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2, as published * by the Free Software Foundation. Which is floating around on the net in various places, but the changes from this newer version never made it upstream. This was tested on an Asus T100TA and an Asus T100CHI, which both come with the CM3218 variant of the light sensor. Reviewed-by: Andy Shevchenko Signed-off-by: Hans de Goede --- drivers/iio/light/cm32181.c | 51 ++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c index ee386afe811e..fd371b36c7b3 100644 --- a/drivers/iio/light/cm32181.c +++ b/drivers/iio/light/cm32181.c @@ -55,15 +55,24 @@ static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = { CM32181_REG_ADDR_CMD, }; -static const int als_it_bits[] = {12, 8, 0, 1, 2, 3}; -static const int als_it_value[] = {25000, 50000, 100000, 200000, 400000, - 800000}; +/* CM3218 Family */ +static const int cm3218_als_it_bits[] = { 0, 1, 2, 3 }; +static const int cm3218_als_it_values[] = { 100000, 200000, 400000, 800000 }; + +/* CM32181 Family */ +static const int cm32181_als_it_bits[] = { 12, 8, 0, 1, 2, 3 }; +static const int cm32181_als_it_values[] = { + 25000, 50000, 100000, 200000, 400000, 800000 +}; struct cm32181_chip { struct i2c_client *client; struct mutex lock; u16 conf_regs[CM32181_CONF_REG_NUM]; int calibscale; + int num_als_it; + const int *als_it_bits; + const int *als_it_values; }; /** @@ -85,8 +94,23 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181) return ret; /* check device ID */ - if ((ret & 0xFF) != 0x81) + switch (ret & 0xFF) { + case 0x18: /* CM3218 */ + dev_info(&client->dev, "Detected CM3218\n"); + cm32181->num_als_it = ARRAY_SIZE(cm3218_als_it_bits); + cm32181->als_it_bits = cm3218_als_it_bits; + cm32181->als_it_values = cm3218_als_it_values; + break; + case 0x81: /* CM32181 */ + case 0x82: /* CM32182, fully compat. with CM32181 */ + dev_info(&client->dev, "Detected CM32181\n"); + cm32181->num_als_it = ARRAY_SIZE(cm32181_als_it_bits); + cm32181->als_it_bits = cm32181_als_it_bits; + cm32181->als_it_values = cm32181_als_it_values; + break; + default: return -ENODEV; + } /* Default Values */ cm32181->conf_regs[CM32181_REG_ADDR_CMD] = @@ -121,9 +145,9 @@ static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2) als_it = cm32181->conf_regs[CM32181_REG_ADDR_CMD]; als_it &= CM32181_CMD_ALS_IT_MASK; als_it >>= CM32181_CMD_ALS_IT_SHIFT; - for (i = 0; i < ARRAY_SIZE(als_it_bits); i++) { - if (als_it == als_it_bits[i]) { - *val2 = als_it_value[i]; + for (i = 0; i < cm32181->num_als_it; i++) { + if (als_it == cm32181->als_it_bits[i]) { + *val2 = cm32181->als_it_values[i]; return IIO_VAL_INT_PLUS_MICRO; } } @@ -146,14 +170,14 @@ static int cm32181_write_als_it(struct cm32181_chip *cm32181, int val) u16 als_it; int ret, i, n; - n = ARRAY_SIZE(als_it_value); + n = cm32181->num_als_it; for (i = 0; i < n; i++) - if (val <= als_it_value[i]) + if (val <= cm32181->als_it_values[i]) break; if (i >= n) i = n - 1; - als_it = als_it_bits[i]; + als_it = cm32181->als_it_bits[i]; als_it <<= CM32181_CMD_ALS_IT_SHIFT; mutex_lock(&cm32181->lock); @@ -265,11 +289,12 @@ static int cm32181_write_raw(struct iio_dev *indio_dev, static ssize_t cm32181_get_it_available(struct device *dev, struct device_attribute *attr, char *buf) { + struct cm32181_chip *cm32181 = iio_priv(dev_to_iio_dev(dev)); int i, n, len; - n = ARRAY_SIZE(als_it_value); + n = cm32181->num_als_it; for (i = 0, len = 0; i < n; i++) - len += sprintf(buf + len, "0.%06u ", als_it_value[i]); + len += sprintf(buf + len, "0.%06u ", cm32181->als_it_values[i]); return len + sprintf(buf + len, "\n"); } @@ -346,6 +371,7 @@ static int cm32181_probe(struct i2c_client *client, } static const struct i2c_device_id cm32181_id[] = { + { "cm3218", 0 }, { "cm32181", 0 }, { } }; @@ -353,6 +379,7 @@ static const struct i2c_device_id cm32181_id[] = { MODULE_DEVICE_TABLE(i2c, cm32181_id); static const struct of_device_id cm32181_of_match[] = { + { .compatible = "capella,cm3218" }, { .compatible = "capella,cm32181" }, { } }; From patchwork Mon Apr 27 15:50:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 11512865 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6B3611667 for ; Mon, 27 Apr 2020 15:50:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 49BB92064C for ; Mon, 27 Apr 2020 15:50:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Zkfit/U0" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728200AbgD0Pux (ORCPT ); Mon, 27 Apr 2020 11:50:53 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:45585 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728179AbgD0Puw (ORCPT ); Mon, 27 Apr 2020 11:50:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588002651; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rcDDVpZf3CSrzjYIDbLlguCG/9YpynAwlXI+zNH+P3c=; b=Zkfit/U0RWQHw3v2gOYijc/LDLC4Jn7zQdwWS1gLF6KWfOoIzrKX0XwUqZgeo/36/kB0uN hvX6bxcE5jEcr9z9DcbZxlAv+flZBbLOrcmbbWppMqpDM4UIAA+DAxuDxRTBpxEWd9rRpU moGa8EIRJd2mF7aXdQ9JHrkve2z3BKE= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-451-IkacN5IbPzeak5WFhJe6qA-1; Mon, 27 Apr 2020 11:50:49 -0400 X-MC-Unique: IkacN5IbPzeak5WFhJe6qA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7F28C46B; Mon, 27 Apr 2020 15:50:47 +0000 (UTC) Received: from x1.localdomain.com (ovpn-114-38.ams2.redhat.com [10.36.114.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id F21E66063C; Mon, 27 Apr 2020 15:50:44 +0000 (UTC) From: Hans de Goede To: "Rafael J . Wysocki" , Len Brown , Darren Hart , Andy Shevchenko , Jonathan Cameron Cc: Hans de Goede , linux-acpi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , linux-iio@vger.kernel.org, Andy Shevchenko Subject: [PATCH v2 3/8] iio: light: cm32181: Handle ACPI instantiating a cm32181 client on the SMBus ARA Date: Mon, 27 Apr 2020 17:50:32 +0200 Message-Id: <20200427155037.218390-3-hdegoede@redhat.com> In-Reply-To: <20200427155037.218390-1-hdegoede@redhat.com> References: <20200427155037.218390-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Some ACPI systems list 2 I2C resources for the CM3218 sensor. On these systems the first I2cSerialBus ACPI-resource points to the SMBus Alert Response Address (ARA, 0x0c) and the second I2cSerialBus ACPI-resource points to the actual CM3218 sensor address. From the ACPI/x86 side devices with more then 1 I2cSerialBus ACPI-resource are handled by the drivers/platform/x86/i2c-multi-instantiate.c code. This code will instantiate "cm32181" i2c_client-s for both resources. Add a check to cm32181_probe() for the client's address being the ARA address, and in that case fail the probe with -ENODEV. On these ACPI systems the sensor may have a SMBus Alert asserted at boot, if this is the case the sensor will not respond to any i2c_transfers on its actual address until we read from the ARA register to clear the Alert. Therefor we must (try to) read a byte from the client with the ARA register, before returning -ENODEV, so that we clear the Alert and when we get called again for the client instantiated for the second I2cSerialBus ACPI-resource the sensor will respond to our i2c-transfers. Reviewed-by: Andy Shevchenko Signed-off-by: Hans de Goede --- Changes in v2 - s/i2c_client-s/I2C clients/ in added comment --- drivers/iio/light/cm32181.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c index fd371b36c7b3..4c26a4a8a070 100644 --- a/drivers/iio/light/cm32181.c +++ b/drivers/iio/light/cm32181.c @@ -51,6 +51,8 @@ #define CM32181_CALIBSCALE_RESOLUTION 1000 #define MLUX_PER_LUX 1000 +#define SMBUS_ALERT_RESPONSE_ADDRESS 0x0c + static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = { CM32181_REG_ADDR_CMD, }; @@ -333,6 +335,20 @@ static int cm32181_probe(struct i2c_client *client, struct iio_dev *indio_dev; int ret; + /* + * Some ACPI systems list 2 I2C resources for the CM3218 sensor, the + * SMBus Alert Response Address (ARA, 0x0c) and the actual I2C address. + * drivers/platform/x86/i2c-multi-instantiate.c instantiates "cm32181" + * I2C clients for both resources, ignore the ARA client. + * On these systems the sensor may have a SMBus Alert asserted at boot, + * in that case the ARA must be read to clear the Alert otherwise the + * sensor will not respond on its actual I2C address. + */ + if (client->addr == SMBUS_ALERT_RESPONSE_ADDRESS) { + i2c_smbus_read_byte(client); + return -ENODEV; + } + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm32181)); if (!indio_dev) { dev_err(&client->dev, "devm_iio_device_alloc failed\n"); From patchwork Mon Apr 27 15:50:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 11512899 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 607231864 for ; Mon, 27 Apr 2020 15:51:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4949B206BF for ; Mon, 27 Apr 2020 15:51:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XU+4Fl7i" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727006AbgD0PvV (ORCPT ); Mon, 27 Apr 2020 11:51:21 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:45059 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728478AbgD0Pu6 (ORCPT ); Mon, 27 Apr 2020 11:50:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588002657; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uVbZSaI2POy6fJtwh+hojPBmfthgWVtJ9egPhBMBtdg=; b=XU+4Fl7igzUvxGqMh1DmrmGFPwKC6MZaOwkRawctoNIldxIqveI6IZVLdHluksa28WLTXm zH7BJNU91q0VfxhCY2s2F7kgQeTP5++tWz309pY2kwv9+jmABgLOJGfXPFnItTIwvbLbCG uOYza9IYj0p23XIBlHlK/rFkzu1I/Cw= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-395-F3ejC_FSN4yQldq6iUzilg-1; Mon, 27 Apr 2020 11:50:52 -0400 X-MC-Unique: F3ejC_FSN4yQldq6iUzilg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5538C835B40; Mon, 27 Apr 2020 15:50:50 +0000 (UTC) Received: from x1.localdomain.com (ovpn-114-38.ams2.redhat.com [10.36.114.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id C704660BEC; Mon, 27 Apr 2020 15:50:47 +0000 (UTC) From: Hans de Goede To: "Rafael J . Wysocki" , Len Brown , Darren Hart , Andy Shevchenko , Jonathan Cameron Cc: Hans de Goede , linux-acpi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , linux-iio@vger.kernel.org, Andy Shevchenko Subject: [PATCH v2 4/8] iio: light: cm32181: Change reg_init to use a bitmap of which registers to init Date: Mon, 27 Apr 2020 17:50:33 +0200 Message-Id: <20200427155037.218390-4-hdegoede@redhat.com> In-Reply-To: <20200427155037.218390-1-hdegoede@redhat.com> References: <20200427155037.218390-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org This is a preparation patch for reading some ACPI tables which give init values for multiple registers. Reviewed-by: Andy Shevchenko Signed-off-by: Hans de Goede --- Changes in v2: - Use unsigned long for init_regs_bitmap - Use for_each_set_bit() --- drivers/iio/light/cm32181.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c index 4c26a4a8a070..194aa5a0ad08 100644 --- a/drivers/iio/light/cm32181.c +++ b/drivers/iio/light/cm32181.c @@ -26,7 +26,7 @@ #define CM32181_REG_ADDR_ID 0x07 /* Number of Configurable Registers */ -#define CM32181_CONF_REG_NUM 0x01 +#define CM32181_CONF_REG_NUM 4 /* CMD register */ #define CM32181_CMD_ALS_DISABLE BIT(0) @@ -53,10 +53,6 @@ #define SMBUS_ALERT_RESPONSE_ADDRESS 0x0c -static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = { - CM32181_REG_ADDR_CMD, -}; - /* CM3218 Family */ static const int cm3218_als_it_bits[] = { 0, 1, 2, 3 }; static const int cm3218_als_it_values[] = { 100000, 200000, 400000, 800000 }; @@ -71,6 +67,7 @@ struct cm32181_chip { struct i2c_client *client; struct mutex lock; u16 conf_regs[CM32181_CONF_REG_NUM]; + unsigned long init_regs_bitmap; int calibscale; int num_als_it; const int *als_it_bits; @@ -117,12 +114,13 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181) /* Default Values */ cm32181->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT; + cm32181->init_regs_bitmap = BIT(CM32181_REG_ADDR_CMD); cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT; /* Initialize registers*/ - for (i = 0; i < CM32181_CONF_REG_NUM; i++) { - ret = i2c_smbus_write_word_data(client, cm32181_reg[i], - cm32181->conf_regs[i]); + for_each_set_bit(i, &cm32181->init_regs_bitmap, CM32181_CONF_REG_NUM) { + ret = i2c_smbus_write_word_data(client, i, + cm32181->conf_regs[i]); if (ret < 0) return ret; } From patchwork Mon Apr 27 15:50:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 11512877 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EEE7A1805 for ; Mon, 27 Apr 2020 15:51:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D477520728 for ; Mon, 27 Apr 2020 15:51:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="H3Qrjb2R" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728497AbgD0PvC (ORCPT ); Mon, 27 Apr 2020 11:51:02 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:33929 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728491AbgD0PvB (ORCPT ); Mon, 27 Apr 2020 11:51:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588002660; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uTu7U87130Tm1Bbuc0WtjOMOvCBgywKq98+g5D2l0S0=; b=H3Qrjb2RUeAKnd90GZa9ofCpM3Mzoq6ml7Te6aNeHVNmAutT8F6kgQQbCz8rpI3bMpo3Ua qFY4e7jS2ZUZFydQ0Orob+sgwCWLd2IdNNqNptkLwnQW7UeeDwxkkURdQkopiwVIUpglIF LqVi5CJEuHOOZLj3zjuwyd+dvQitoYo= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-368-8eVknis9MTCZeGw72nwOSA-1; Mon, 27 Apr 2020 11:50:55 -0400 X-MC-Unique: 8eVknis9MTCZeGw72nwOSA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3A0C0107ACF7; Mon, 27 Apr 2020 15:50:53 +0000 (UTC) Received: from x1.localdomain.com (ovpn-114-38.ams2.redhat.com [10.36.114.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id A038E6063C; Mon, 27 Apr 2020 15:50:50 +0000 (UTC) From: Hans de Goede To: "Rafael J . Wysocki" , Len Brown , Darren Hart , Andy Shevchenko , Jonathan Cameron Cc: Hans de Goede , linux-acpi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , linux-iio@vger.kernel.org, Andy Shevchenko Subject: [PATCH v2 5/8] iio: light: cm32181: Use units of 1/100000th for calibscale and lux_per_bit Date: Mon, 27 Apr 2020 17:50:34 +0200 Message-Id: <20200427155037.218390-5-hdegoede@redhat.com> In-Reply-To: <20200427155037.218390-1-hdegoede@redhat.com> References: <20200427155037.218390-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Use units of 1/100000th for calibscale and lux_per_bit. The similar cm3232 driver already uses 1/100000th as unit for calibscale. This allows for higher-accuracy and makes it easier to add support for getting device-specific calibscale and lux_per_bit values from a device's ACPI tables, as the values in the ACPI tables also use 1/100000th units. This units change means that our intermediate values in cm32181_get_lux() may get quite big, change the type of the lux variable to a u64 to deal with this. Reviewed-by: Andy Shevchenko Signed-off-by: Hans de Goede --- drivers/iio/light/cm32181.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c index 194aa5a0ad08..ebdbc7f865f3 100644 --- a/drivers/iio/light/cm32181.c +++ b/drivers/iio/light/cm32181.c @@ -45,11 +45,11 @@ #define CM32181_CMD_ALS_SM_MASK (0x03 << CM32181_CMD_ALS_SM_SHIFT) #define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT) -#define CM32181_MLUX_PER_BIT 5 /* ALS_SM=01 IT=800ms */ -#define CM32181_MLUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */ -#define CM32181_CALIBSCALE_DEFAULT 1000 -#define CM32181_CALIBSCALE_RESOLUTION 1000 -#define MLUX_PER_LUX 1000 +#define CM32181_LUX_PER_BIT 500 /* ALS_SM=01 IT=800ms */ +#define CM32181_LUX_PER_BIT_RESOLUTION 100000 +#define CM32181_LUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */ +#define CM32181_CALIBSCALE_DEFAULT 100000 +#define CM32181_CALIBSCALE_RESOLUTION 100000 #define SMBUS_ALERT_RESPONSE_ADDRESS 0x0c @@ -206,15 +206,15 @@ static int cm32181_get_lux(struct cm32181_chip *cm32181) struct i2c_client *client = cm32181->client; int ret; int als_it; - unsigned long lux; + u64 lux; ret = cm32181_read_als_it(cm32181, &als_it); if (ret < 0) return -EINVAL; - lux = CM32181_MLUX_PER_BIT; - lux *= CM32181_MLUX_PER_BIT_BASE_IT; - lux /= als_it; + lux = CM32181_LUX_PER_BIT; + lux *= CM32181_LUX_PER_BIT_BASE_IT; + lux = div_u64(lux, als_it); ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS); if (ret < 0) @@ -222,8 +222,8 @@ static int cm32181_get_lux(struct cm32181_chip *cm32181) lux *= ret; lux *= cm32181->calibscale; - lux /= CM32181_CALIBSCALE_RESOLUTION; - lux /= MLUX_PER_LUX; + lux = div_u64(lux, CM32181_CALIBSCALE_RESOLUTION); + lux = div_u64(lux, CM32181_LUX_PER_BIT_RESOLUTION); if (lux > 0xFFFF) lux = 0xFFFF; From patchwork Mon Apr 27 15:50:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 11512871 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EE92815E6 for ; Mon, 27 Apr 2020 15:51:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D8672206B6 for ; Mon, 27 Apr 2020 15:51:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="U79UO5eM" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728480AbgD0PvB (ORCPT ); Mon, 27 Apr 2020 11:51:01 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:52659 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728487AbgD0PvA (ORCPT ); Mon, 27 Apr 2020 11:51:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588002659; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fY6m/15VheJvgRGWmA9hnnwR6MqOrOf6tNIZ+WDEnoQ=; b=U79UO5eMIgHUkxezrHDi80YMkbSuS7UqtUsD4jkVXe6d6AWUaf0DMuZ3yBsPdEheNqGUC1 8T5cHnkueIeEUNBFv/9gWyrOUEog3opSXj+cAfXXdruqyNFdRrHdjwHGuIi2I6W4fWG6OC lt0GnMMh/iWLwRKCWa07aWXWwdIY/Sc= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-180-hiFdzsm6N1ODuSZG6p3XyQ-1; Mon, 27 Apr 2020 11:50:58 -0400 X-MC-Unique: hiFdzsm6N1ODuSZG6p3XyQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 11A1C464; Mon, 27 Apr 2020 15:50:56 +0000 (UTC) Received: from x1.localdomain.com (ovpn-114-38.ams2.redhat.com [10.36.114.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id 81D6F60BEC; Mon, 27 Apr 2020 15:50:53 +0000 (UTC) From: Hans de Goede To: "Rafael J . Wysocki" , Len Brown , Darren Hart , Andy Shevchenko , Jonathan Cameron Cc: Hans de Goede , linux-acpi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , linux-iio@vger.kernel.org, Andy Shevchenko Subject: [PATCH v2 6/8] iio: light: cm32181: Make lux_per_bit and lux_per_bit_base_it runtime settings Date: Mon, 27 Apr 2020 17:50:35 +0200 Message-Id: <20200427155037.218390-6-hdegoede@redhat.com> In-Reply-To: <20200427155037.218390-1-hdegoede@redhat.com> References: <20200427155037.218390-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Make lux_per_bit and lux_per_bit_base_it settings stored in struct cm32181_chip instead of a hardcoded (defined) values. This is a preparation patch for reading some ACPI tables which specify a device specific lux_per_bit value. Reviewed-by: Andy Shevchenko Signed-off-by: Hans de Goede --- drivers/iio/light/cm32181.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c index ebdbc7f865f3..92f99cbb5cc1 100644 --- a/drivers/iio/light/cm32181.c +++ b/drivers/iio/light/cm32181.c @@ -69,6 +69,8 @@ struct cm32181_chip { u16 conf_regs[CM32181_CONF_REG_NUM]; unsigned long init_regs_bitmap; int calibscale; + int lux_per_bit; + int lux_per_bit_base_it; int num_als_it; const int *als_it_bits; const int *als_it_values; @@ -116,6 +118,8 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181) CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT; cm32181->init_regs_bitmap = BIT(CM32181_REG_ADDR_CMD); cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT; + cm32181->lux_per_bit = CM32181_LUX_PER_BIT; + cm32181->lux_per_bit_base_it = CM32181_LUX_PER_BIT_BASE_IT; /* Initialize registers*/ for_each_set_bit(i, &cm32181->init_regs_bitmap, CM32181_CONF_REG_NUM) { @@ -212,8 +216,8 @@ static int cm32181_get_lux(struct cm32181_chip *cm32181) if (ret < 0) return -EINVAL; - lux = CM32181_LUX_PER_BIT; - lux *= CM32181_LUX_PER_BIT_BASE_IT; + lux = cm32181->lux_per_bit; + lux *= cm32181->lux_per_bit_base_it; lux = div_u64(lux, als_it); ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS); From patchwork Mon Apr 27 15:50:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 11512873 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4588D15E6 for ; Mon, 27 Apr 2020 15:51:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 28E8F206BF for ; Mon, 27 Apr 2020 15:51:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="K+RZeVer" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728481AbgD0PvE (ORCPT ); Mon, 27 Apr 2020 11:51:04 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:41783 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728491AbgD0PvE (ORCPT ); Mon, 27 Apr 2020 11:51:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588002662; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nwpdNW/FPAgeOHqy4G+r3r1WvyVbwDRXzUpzGHkuL3I=; b=K+RZeVerFMEc3vluCw7BYRhIKG/fK87O5n70WgVZpPT/rMJcVn6ZWrqDRWYQTRPBF01gQw gRV7D7QBZHBsAMwURjZZLQPh8h7AoumiO8/r2pErCeCbubDQdN8y4NDToWSJdfXGax0a9/ cJaKr5IuvU8No3OemxW7ItZrXgD96hQ= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-18-2xTMlrUUO1-PBe8vEDwX0Q-1; Mon, 27 Apr 2020 11:51:00 -0400 X-MC-Unique: 2xTMlrUUO1-PBe8vEDwX0Q-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DEFB7800685; Mon, 27 Apr 2020 15:50:58 +0000 (UTC) Received: from x1.localdomain.com (ovpn-114-38.ams2.redhat.com [10.36.114.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5A43260BEC; Mon, 27 Apr 2020 15:50:56 +0000 (UTC) From: Hans de Goede To: "Rafael J . Wysocki" , Len Brown , Darren Hart , Andy Shevchenko , Jonathan Cameron Cc: Hans de Goede , linux-acpi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , linux-iio@vger.kernel.org, Andy Shevchenko Subject: [PATCH v2 7/8] iio: light: cm32181: Add support for parsing CPM0 and CPM1 ACPI tables Date: Mon, 27 Apr 2020 17:50:36 +0200 Message-Id: <20200427155037.218390-7-hdegoede@redhat.com> In-Reply-To: <20200427155037.218390-1-hdegoede@redhat.com> References: <20200427155037.218390-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org On ACPI based systems the CPLM3218 ACPI device node describing the CM3218[1] sensor typically will have some extra tables with register init values for initializing the sensor and calibration info. This is based on a newer version of cm32181.c, with a copyright of: * Copyright (C) 2014 Capella Microsystems Inc. * Author: Kevin Tsai * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2, as published * by the Free Software Foundation. Which is floating around on the net in various places, but the changes from this newer version never made it upstream. This was tested on the following models: Acer Switch 10 SW5-012 (CM32181) Asus T100TA (CM3218), Asus T100CHI (CM3218) and HP X2 10-n000nd (CM32181). Reviewed-by: Andy Shevchenko Signed-off-by: Hans de Goede --- Changes in v2: - Factor out the parsing into a separate helper function --- drivers/iio/light/cm32181.c | 99 +++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c index 92f99cbb5cc1..04ba636415c9 100644 --- a/drivers/iio/light/cm32181.c +++ b/drivers/iio/light/cm32181.c @@ -4,6 +4,7 @@ * Author: Kevin Tsai */ +#include #include #include #include @@ -53,6 +54,15 @@ #define SMBUS_ALERT_RESPONSE_ADDRESS 0x0c +/* CPM0 Index 0: device-id (3218 or 32181), 1: Unknown, 2: init_regs_bitmap */ +#define CPM0_REGS_BITMAP 2 +#define CPM0_HEADER_SIZE 3 + +/* CPM1 Index 0: lux_per_bit, 1: calibscale, 2: resolution (100000) */ +#define CPM1_LUX_PER_BIT 0 +#define CPM1_CALIBSCALE 1 +#define CPM1_SIZE 3 + /* CM3218 Family */ static const int cm3218_als_it_bits[] = { 0, 1, 2, 3 }; static const int cm3218_als_it_values[] = { 100000, 200000, 400000, 800000 }; @@ -76,6 +86,92 @@ struct cm32181_chip { const int *als_it_values; }; +static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2); + +#ifdef CONFIG_ACPI +/** + * cm32181_acpi_get_cpm() - Get CPM object from ACPI + * @client pointer of struct i2c_client. + * @obj_name pointer of ACPI object name. + * @count maximum size of return array. + * @vals pointer of array for return elements. + * + * Convert ACPI CPM table to array. + * + * Return: -ENODEV for fail. Otherwise is number of elements. + */ +static int cm32181_acpi_get_cpm(struct i2c_client *client, char *obj_name, + u64 *values, int count) +{ + struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; + union acpi_object *cpm, *elem; + acpi_handle handle; + acpi_status status; + int i; + + handle = ACPI_HANDLE(&client->dev); + if (!handle) + return -ENODEV; + + status = acpi_evaluate_object(handle, obj_name, NULL, &buffer); + if (ACPI_FAILURE(status)) { + dev_err(&client->dev, "object %s not found\n", obj_name); + return -ENODEV; + } + + cpm = buffer.pointer; + if (cpm->package.count > count) + dev_warn(&client->dev, "%s table contains %d values, only using first %d values\n", + obj_name, cpm->package.count, count); + + count = min_t(int, cpm->package.count, count); + for (i = 0; i < count; i++) { + elem = &(cpm->package.elements[i]); + values[i] = elem->integer.value; + } + + kfree(buffer.pointer); + + return count; +} + +static void cm32181_acpi_parse_cpm_tables(struct cm32181_chip *cm32181) +{ + u64 vals[CPM0_HEADER_SIZE + CM32181_CONF_REG_NUM]; + struct i2c_client *client = cm32181->client; + int i, count; + + count = cm32181_acpi_get_cpm(client, "CPM0", vals, ARRAY_SIZE(vals)); + if (count <= CPM0_HEADER_SIZE) + return; + + count -= CPM0_HEADER_SIZE; + + cm32181->init_regs_bitmap = vals[CPM0_REGS_BITMAP]; + cm32181->init_regs_bitmap &= GENMASK(count - 1, 0); + for_each_set_bit(i, &cm32181->init_regs_bitmap, count) + cm32181->conf_regs[i] = vals[CPM0_HEADER_SIZE + i]; + + count = cm32181_acpi_get_cpm(client, "CPM1", vals, ARRAY_SIZE(vals)); + if (count != CPM1_SIZE) + return; + + cm32181->lux_per_bit = vals[CPM1_LUX_PER_BIT]; + + /* Check for uncalibrated devices */ + if (vals[CPM1_CALIBSCALE] == CM32181_CALIBSCALE_DEFAULT) + return; + + cm32181->calibscale = vals[CPM1_CALIBSCALE]; + /* CPM1 lux_per_bit is for the current it value */ + cm32181_read_als_it(cm32181, &cm32181->lux_per_bit_base_it); +} +#else +static void cm32181_acpi_parse_cpm_tables(struct cm32181_chip *cm32181) +{ +} +#endif /* CONFIG_ACPI */ + /** * cm32181_reg_init() - Initialize CM32181 registers * @cm32181: pointer of struct cm32181. @@ -121,6 +217,9 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181) cm32181->lux_per_bit = CM32181_LUX_PER_BIT; cm32181->lux_per_bit_base_it = CM32181_LUX_PER_BIT_BASE_IT; + if (ACPI_HANDLE(&client->dev)) + cm32181_acpi_parse_cpm_tables(cm32181); + /* Initialize registers*/ for_each_set_bit(i, &cm32181->init_regs_bitmap, CM32181_CONF_REG_NUM) { ret = i2c_smbus_write_word_data(client, i, From patchwork Mon Apr 27 15:50:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 11512889 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1A8261392 for ; Mon, 27 Apr 2020 15:51:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 04504206BF for ; Mon, 27 Apr 2020 15:51:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MbSuDOcZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728514AbgD0PvJ (ORCPT ); Mon, 27 Apr 2020 11:51:09 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:44009 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728491AbgD0PvI (ORCPT ); Mon, 27 Apr 2020 11:51:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588002667; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=O0EVbd8yvTnodt/PwOFNSr4NOJBYWtmio5+4hHlbVdA=; b=MbSuDOcZjHsiLgGzFWjiHQzuMnH1n4njgSiooB0wV/YDqhB9+Z1BzIS3mh1u748gOSIbvx IwO54eMrEaNvLgJwpjrWUK8IY7IFry188xZY7Ec0anWqtVcmgzrH/KusCx+UAhNIEV1MQz 9EPE0Ij0YLhN6lpaXb10rYb2cFVPcdY= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-309-nI1rz6WXPVO-hYMpnkVNJQ-1; Mon, 27 Apr 2020 11:51:03 -0400 X-MC-Unique: nI1rz6WXPVO-hYMpnkVNJQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B660B835B40; Mon, 27 Apr 2020 15:51:01 +0000 (UTC) Received: from x1.localdomain.com (ovpn-114-38.ams2.redhat.com [10.36.114.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id 316A96063C; Mon, 27 Apr 2020 15:50:59 +0000 (UTC) From: Hans de Goede To: "Rafael J . Wysocki" , Len Brown , Darren Hart , Andy Shevchenko , Jonathan Cameron Cc: Hans de Goede , linux-acpi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , linux-iio@vger.kernel.org, Andy Shevchenko Subject: [PATCH v2 8/8] iio: light: cm32181: Fix integartion time typo Date: Mon, 27 Apr 2020 17:50:37 +0200 Message-Id: <20200427155037.218390-8-hdegoede@redhat.com> In-Reply-To: <20200427155037.218390-1-hdegoede@redhat.com> References: <20200427155037.218390-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Fix integartion time typo and while at it improve the comment with the typo a bit in general. Reviewed-by: Andy Shevchenko Signed-off-by: Hans de Goede --- drivers/iio/light/cm32181.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c index 04ba636415c9..95c57b0cb230 100644 --- a/drivers/iio/light/cm32181.c +++ b/drivers/iio/light/cm32181.c @@ -236,7 +236,7 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181) * @cm32181: pointer of struct cm32181 * @val2: pointer of int to load the als_it value. * - * Report the current integartion time by millisecond. + * Report the current integration time in milliseconds. * * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL. */