Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp4781112pxb; Tue, 2 Nov 2021 15:58:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxJW31o/NEFeIq2N+PcAKyRYFm2779YRyxZ8wlYWHxYULXmd4BE26T7iSFzY+IEM335SmIx X-Received: by 2002:a6b:109:: with SMTP id 9mr3319446iob.128.1635893902946; Tue, 02 Nov 2021 15:58:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635893902; cv=none; d=google.com; s=arc-20160816; b=K5+P9+MykTVL0tRAjZ2UrGUOt1lwkEUsYpubOlB3fk/lKCIKfj1GTFekhjxHUzly/t eQDrRO548GwNjqPcxM1a3e5GcuNIGonTwYFN6+sesnRawW4MG3Et05tRo8z6w51XoAf5 9ZNAr9Qrs+2NNPgDBTIW9APoa5bz8EcnRkjm5XtrF/oI+BpTsJ5SZ5yzhsXbGdSDoOdv rTT9ObRrRTWuUgab40PsbDYgozS1GYWtoymFg+duPk22xPqlFxVKRt0olwJ3kvnTd3ht SnJTqiJp1vllGbDB8udz+ykaDqvbYj03qXVQ6hZ1E2nR3Q79anjWVE/CwSJ/HtuxLLoo feKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VI+rXenfrHQ9NQf88Xc3kujdb0UXyWU2YSRE7S/ay+s=; b=LrO9W/wJFhWHCSER9We2AeI21N5QaY76CXVIm4LOe9zZbU4yr3eZS+NOV5ckoEuqHl CIwwQoDBF9li9IvliOcS4nTHqgUk51xOP3D2aWwlc9IVWWkAMTXLIRyGZZTQFGX0UaCx mjWl03nMYNdZIJPciS7zeg4G7bERuqe7ZxsVmfIRywEaOJwdvG6/J5RRmcALTeRRXnQQ q4KvhC5790IGMR0cYtN5ljh7FdufOpUfGtd4d3RtIBMPbYlkOXZDzmnf5hZEgGDMuXGe +PldmAaaZ+rQQwABQKguLmkRtaSmZS3R/EICvgHx+77dw9239djVIeVjsIzFl0rKlyim j3mw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GBb5eVi6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t34si552010jal.104.2021.11.02.15.58.09; Tue, 02 Nov 2021 15:58:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GBb5eVi6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231380AbhKBW6T (ORCPT + 99 others); Tue, 2 Nov 2021 18:58:19 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:30306 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230227AbhKBW6O (ORCPT ); Tue, 2 Nov 2021 18:58:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635893738; 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=VI+rXenfrHQ9NQf88Xc3kujdb0UXyWU2YSRE7S/ay+s=; b=GBb5eVi6bvcQuNdUJrKCOQ9YQ6GPh7iTuDB9oF7YvZJVBfd31uQYlVcXtIIiZGlCLcoxnt o3bh5i2Vj3N2ukO7QD67GNRa/h/OiCNvjd4WcWS2r2Va35NBuQ7CRAp/52G0kVHOTg2NNH eZcgIKgeAwMjp7CFNmOh5Qs2FcC0zk4= 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-200-BsE6o8K7PGOKsX00YOPiag-1; Tue, 02 Nov 2021 18:55:33 -0400 X-MC-Unique: BsE6o8K7PGOKsX00YOPiag-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1475E8042DB; Tue, 2 Nov 2021 22:55:32 +0000 (UTC) Received: from x1.localdomain (unknown [10.39.192.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id AE4CF60657; Tue, 2 Nov 2021 22:55:30 +0000 (UTC) From: Hans de Goede To: Lee Jones , Daniel Thompson , Jingoo Han Cc: Hans de Goede , Tsuchiya Yuto , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/3] backlight: lp855x: Add support ACPI enumeration Date: Tue, 2 Nov 2021 23:55:04 +0100 Message-Id: <20211102225504.18920-3-hdegoede@redhat.com> In-Reply-To: <20211102225504.18920-1-hdegoede@redhat.com> References: <20211102225504.18920-1-hdegoede@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The Xiaomi Mi Pad 2 tablet uses an ACPI enumerated LP8556 backlight controller for its LCD-panel, with a Xiaomi specific ACPI HID of "XMCC0001", add support for this. Note the new "if (id)" check also fixes a NULL pointer deref when a user tries to manually bind the driver from sysfs. When CONFIG_ACPI is disabled acpi_match_device() will always return NULL, so the lp855x_parse_acpi() call will get optimized away. Signed-off-by: Hans de Goede --- Changes in v2: - Remove `lp->pdata = pdata` assignment from lp855x_parse_dt() - Add "and is in register mode" to the comment in lp855x_parse_acpi() --- drivers/video/backlight/lp855x_bl.c | 73 ++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/drivers/video/backlight/lp855x_bl.c b/drivers/video/backlight/lp855x_bl.c index d1d27d5eb0f2..2b9e2bbbb03e 100644 --- a/drivers/video/backlight/lp855x_bl.c +++ b/drivers/video/backlight/lp855x_bl.c @@ -5,6 +5,7 @@ * Copyright (C) 2011 Texas Instruments */ +#include #include #include #include @@ -330,7 +331,7 @@ static int lp855x_parse_dt(struct lp855x *lp) { struct device *dev = lp->dev; struct device_node *node = dev->of_node; - struct lp855x_platform_data *pdata; + struct lp855x_platform_data *pdata = lp->pdata; int rom_length; if (!node) { @@ -338,10 +339,6 @@ static int lp855x_parse_dt(struct lp855x *lp) return -EINVAL; } - pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); - if (!pdata) - return -ENOMEM; - of_property_read_string(node, "bl-name", &pdata->name); of_property_read_u8(node, "dev-ctrl", &pdata->device_control); of_property_read_u8(node, "init-brt", &pdata->initial_brightness); @@ -368,8 +365,6 @@ static int lp855x_parse_dt(struct lp855x *lp) pdata->rom_data = &rom[0]; } - lp->pdata = pdata; - return 0; } #else @@ -379,8 +374,32 @@ static int lp855x_parse_dt(struct lp855x *lp) } #endif +static int lp855x_parse_acpi(struct lp855x *lp) +{ + int ret; + + /* + * On ACPI the device has already been initialized by the firmware + * and is in register mode, so we can read back the settings from + * the registers. + */ + ret = i2c_smbus_read_byte_data(lp->client, lp->cfg->reg_brightness); + if (ret < 0) + return ret; + + lp->pdata->initial_brightness = ret; + + ret = i2c_smbus_read_byte_data(lp->client, lp->cfg->reg_devicectrl); + if (ret < 0) + return ret; + + lp->pdata->device_control = ret; + return 0; +} + static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id) { + const struct acpi_device_id *acpi_id = NULL; struct device *dev = &cl->dev; struct lp855x *lp; int ret; @@ -394,10 +413,20 @@ static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id) lp->client = cl; lp->dev = dev; - lp->chipname = id->name; - lp->chip_id = id->driver_data; lp->pdata = dev_get_platdata(dev); + if (id) { + lp->chipname = id->name; + lp->chip_id = id->driver_data; + } else { + acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev); + if (!acpi_id) + return -ENODEV; + + lp->chipname = acpi_id->id; + lp->chip_id = acpi_id->driver_data; + } + switch (lp->chip_id) { case LP8550: case LP8551: @@ -415,9 +444,19 @@ static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id) } if (!lp->pdata) { - ret = lp855x_parse_dt(lp); - if (ret < 0) - return ret; + lp->pdata = devm_kzalloc(dev, sizeof(*lp->pdata), GFP_KERNEL); + if (!lp->pdata) + return -ENOMEM; + + if (id) { + ret = lp855x_parse_dt(lp); + if (ret < 0) + return ret; + } else { + ret = lp855x_parse_acpi(lp); + if (ret < 0) + return ret; + } } if (lp->pdata->period_ns > 0) @@ -537,10 +576,20 @@ static const struct i2c_device_id lp855x_ids[] = { }; MODULE_DEVICE_TABLE(i2c, lp855x_ids); +#ifdef CONFIG_ACPI +static const struct acpi_device_id lp855x_acpi_match[] = { + /* Xiaomi specific HID used for the LP8556 on the Mi Pad 2 */ + { "XMCC0001", LP8556 }, + { } +}; +MODULE_DEVICE_TABLE(acpi, lp855x_acpi_match); +#endif + static struct i2c_driver lp855x_driver = { .driver = { .name = "lp855x", .of_match_table = of_match_ptr(lp855x_dt_ids), + .acpi_match_table = ACPI_PTR(lp855x_acpi_match), }, .probe = lp855x_probe, .remove = lp855x_remove, -- 2.31.1