From patchwork Mon May 4 12:55:44 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: 11525835 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 992A715E6 for ; Mon, 4 May 2020 12:56:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 718362075E for ; Mon, 4 May 2020 12:56:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gaNoBD4u" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726531AbgEDM4V (ORCPT ); Mon, 4 May 2020 08:56:21 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:46294 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727896AbgEDM4U (ORCPT ); Mon, 4 May 2020 08:56:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588596978; 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=gll++s2Xf8UudNUo7GbzW91thKVg8vOQSO8CXUpUipU=; b=gaNoBD4umKMXWk+M1RzNOT9Wg/jkmTWYDc5p/x0J/xtYbtApHar4wIYHyGK/qxj8LIEh2K Rk4NcY7UJSUUuRgQeziQxd8YNnYItBBOiRTB2rCHD0gsGMBfu0DCm7ff7tT5kvAoDzyaRp RONY3Jplf/3Vx6hxi6eCXLvazMcrDdQ= 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-345-gVJ5DHcZPfWNCz_i5zteCA-1; Mon, 04 May 2020 08:56:17 -0400 X-MC-Unique: gVJ5DHcZPfWNCz_i5zteCA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A6EE61800D4A; Mon, 4 May 2020 12:56:14 +0000 (UTC) Received: from x1.localdomain.com (ovpn-114-224.ams2.redhat.com [10.36.114.224]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0BED65D97D; Mon, 4 May 2020 12:56:07 +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 v4 04/11] iio: light: cm32181: Add support for the CM3218 Date: Mon, 4 May 2020 14:55:44 +0200 Message-Id: <20200504125551.434647-4-hdegoede@redhat.com> In-Reply-To: <20200504125551.434647-1-hdegoede@redhat.com> References: <20200504125551.434647-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 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 --- Changes in v4: - Add a cm32181_chip_info structure and use that to store the various sensor-model specific values --- drivers/iio/light/cm32181.c | 67 ++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c index e7dc3276d800..92b9ce91fe0a 100644 --- a/drivers/iio/light/cm32181.c +++ b/drivers/iio/light/cm32181.c @@ -55,17 +55,45 @@ 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}; +struct cm32181_chip_info { + const char *name; + const int *als_it_bits; + const int *als_it_values; + int num_als_it; +}; struct cm32181_chip { struct i2c_client *client; + const struct cm32181_chip_info *info; struct mutex lock; u16 conf_regs[CM32181_CONF_REG_NUM]; int calibscale; }; +/* CM3218 Family */ +static const int cm3218_als_it_bits[] = { 0, 1, 2, 3 }; +static const int cm3218_als_it_values[] = { 100000, 200000, 400000, 800000 }; + +const struct cm32181_chip_info cm3218_chip_info = { + .name = "cm3218", + .als_it_bits = cm3218_als_it_bits, + .als_it_values = cm3218_als_it_values, + .num_als_it = ARRAY_SIZE(cm3218_als_it_bits), +}; + +/* 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 +}; + +const struct cm32181_chip_info cm32181_chip_info = { + .name = "cm32181", + .als_it_bits = cm32181_als_it_bits, + .als_it_values = cm32181_als_it_values, + .num_als_it = ARRAY_SIZE(cm32181_als_it_bits), +}; + /** * cm32181_reg_init() - Initialize CM32181 registers * @cm32181: pointer of struct cm32181. @@ -85,8 +113,17 @@ static int cm32181_reg_init(struct cm32181_chip *cm32181) return ret; /* check device ID */ - if ((ret & 0xFF) != 0x81) + switch (ret & 0xFF) { + case 0x18: /* CM3218 */ + cm32181->info = &cm3218_chip_info; + break; + case 0x81: /* CM32181 */ + case 0x82: /* CM32182, fully compat. with CM32181 */ + cm32181->info = &cm32181_chip_info; + break; + default: return -ENODEV; + } /* Default Values */ cm32181->conf_regs[CM32181_REG_ADDR_CMD] = @@ -121,9 +158,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->info->num_als_it; i++) { + if (als_it == cm32181->info->als_it_bits[i]) { + *val2 = cm32181->info->als_it_values[i]; return IIO_VAL_INT_PLUS_MICRO; } } @@ -146,14 +183,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->info->num_als_it; for (i = 0; i < n; i++) - if (val <= als_it_value[i]) + if (val <= cm32181->info->als_it_values[i]) break; if (i >= n) i = n - 1; - als_it = als_it_bits[i]; + als_it = cm32181->info->als_it_bits[i]; als_it <<= CM32181_CMD_ALS_IT_SHIFT; mutex_lock(&cm32181->lock); @@ -265,11 +302,13 @@ 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->info->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->info->als_it_values[i]); return len + sprintf(buf + len, "\n"); } @@ -322,7 +361,6 @@ static int cm32181_probe(struct i2c_client *client) indio_dev->channels = cm32181_channels; indio_dev->num_channels = ARRAY_SIZE(cm32181_channels); indio_dev->info = &cm32181_info; - indio_dev->name = "cm32181"; indio_dev->modes = INDIO_DIRECT_MODE; ret = cm32181_reg_init(cm32181); @@ -333,6 +371,8 @@ static int cm32181_probe(struct i2c_client *client) return ret; } + indio_dev->name = cm32181->info->name; + ret = devm_iio_device_register(&client->dev, indio_dev); if (ret) { dev_err(&client->dev, @@ -345,6 +385,7 @@ static int cm32181_probe(struct i2c_client *client) } static const struct of_device_id cm32181_of_match[] = { + { .compatible = "capella,cm3218" }, { .compatible = "capella,cm32181" }, { } };