From patchwork Wed Jun 27 04:18:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matt Ranostay X-Patchwork-Id: 10490491 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 4F14260325 for ; Wed, 27 Jun 2018 04:19:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3CA3D2842A for ; Wed, 27 Jun 2018 04:19:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2CD56284D4; Wed, 27 Jun 2018 04:19:05 +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=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, 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 A4E1A2842A for ; Wed, 27 Jun 2018 04:19:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751236AbeF0ETE (ORCPT ); Wed, 27 Jun 2018 00:19:04 -0400 Received: from mail-pl0-f67.google.com ([209.85.160.67]:42321 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751205AbeF0ETD (ORCPT ); Wed, 27 Jun 2018 00:19:03 -0400 Received: by mail-pl0-f67.google.com with SMTP id w17-v6so394135pll.9 for ; Tue, 26 Jun 2018 21:19:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=konsulko.com; s=google; h=from:to:cc:subject:date:message-id; bh=UNsW7rQgks78FQKvNnvNbU3BknMX0mnRUqgGF7gxtEQ=; b=dweXXaZo8JMD7W+jjvZFVUC9XLpdVP7BSfV2189q/xctce7xmfq8RBaeLNrqtUl26G CDCgsiU4h3EykvCUbY8+Fq8rzFM9F+6VRFlZJujuVVldOE7QnXBvdeAgK6/1Cj1fZca0 Vo23s8LhaNiOzlhPFQs7KgN8gSwaWSQvs9ZOc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=UNsW7rQgks78FQKvNnvNbU3BknMX0mnRUqgGF7gxtEQ=; b=cGkr5GGBbdgPeirqGpn5fg+SJ0+DxxOlsOKpnB+F2M/bZON3u7uYYxXrLsgTXPtfXH K9VzXfvfyYdnM0NLR58ylRaqnERuxMNaLkxhX7theoNXJAXQCiWfs7miQ09eVRPUqOdr sYjYMa1oAb/PtYsPt1r7+n7CyldsKkEE9xSuH+e76PY9cKhIVB5yAxn2V89V3ASMRm5z h/GNykmvdvd4y1Jz1GSgFv4SH4CvqcXmXCZJeqAd7cwR/Fi9mK5w7Sl68d2u8SWtX6jS DXUopgJpquK0a0zS7tIHZ+FxHD9XGKXqtwYVr+g4zgOl7Iquw3UQ5OWAjQ+sKfPFez/9 NQ3w== X-Gm-Message-State: APt69E0Y5nqxCPUyfRgujQULaN5cg8k3yXfyaa5Uk1Ssk7SuOGMKixbL 1zXnanwQ1ZNSm1su4LezrZ26qAM7qnA= X-Google-Smtp-Source: ADUXVKLAjW/mq5jxil0ycPESef0MswMWXu8svRwuFHNiGykL+ogo/x1kP1W1pl4YS4dTyLVMLBoLEA== X-Received: by 2002:a17:902:b604:: with SMTP id b4-v6mr4439788pls.18.1530073143292; Tue, 26 Jun 2018 21:19:03 -0700 (PDT) Received: from niteshade.hsd1.or.comcast.net ([2601:1c2:5000:8d45::3f0e]) by smtp.gmail.com with ESMTPSA id b77-v6sm5319534pfd.33.2018.06.26.21.19.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Jun 2018 21:19:02 -0700 (PDT) From: Matt Ranostay To: linux-media@vger.kernel.org Cc: Matt Ranostay , linux-hwmon@vger.kernel.org Subject: [PATCH v2] media: video-i2c: add hwmon support for amg88xx Date: Tue, 26 Jun 2018 21:18:58 -0700 Message-Id: <20180627041858.22186-1-matt.ranostay@konsulko.com> X-Mailer: git-send-email 2.17.1 Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP AMG88xx has an on-board thermistor which is used for more accurate processing of its temperature readings from the 8x8 thermopile array Cc: linux-hwmon@vger.kernel.org Signed-off-by: Matt Ranostay --- drivers/media/i2c/video-i2c.c | 77 +++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) Changes from v1: * remove unneeded include statement * removed evil &NULL dereference if hwmon isn't enabled * return PTR_ERR instead of boolean IS_ERR from amg88xx_hwmon_init() * use error code returned from hwmon_init() to display dev_warn diff --git a/drivers/media/i2c/video-i2c.c b/drivers/media/i2c/video-i2c.c index 0b347cc19aa5..834cd4401e38 100644 --- a/drivers/media/i2c/video-i2c.c +++ b/drivers/media/i2c/video-i2c.c @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -77,6 +78,9 @@ struct video_i2c_chip { /* xfer function */ int (*xfer)(struct video_i2c_data *data, char *buf); + + /* hwmon init function */ + int (*hwmon_init)(struct video_i2c_data *data); }; static int amg88xx_xfer(struct video_i2c_data *data, char *buf) @@ -101,6 +105,70 @@ static int amg88xx_xfer(struct video_i2c_data *data, char *buf) return (ret == 2) ? 0 : -EIO; } +#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE)) + +static const u32 amg88xx_temp_config[] = { + HWMON_T_INPUT, + 0 +}; + +static const struct hwmon_channel_info amg88xx_temp = { + .type = hwmon_temp, + .config = amg88xx_temp_config, +}; + +static const struct hwmon_channel_info *amg88xx_info[] = { + &amg88xx_temp, + NULL +}; + +static umode_t amg88xx_is_visible(const void *drvdata, + enum hwmon_sensor_types type, + u32 attr, int channel) +{ + return 0444; +} + +static int amg88xx_read(struct device *dev, enum hwmon_sensor_types type, + u32 attr, int channel, long *val) +{ + struct video_i2c_data *data = dev_get_drvdata(dev); + struct i2c_client *client = data->client; + int tmp = i2c_smbus_read_word_data(client, 0x0e); + + if (tmp < 0) + return tmp; + + /* check for sign bit, and invert temp reading to a negative value */ + if (tmp & BIT(11)) + tmp = -(tmp & 0x7ff); + + *val = (tmp * 625) / 10; + + return 0; +} + +static const struct hwmon_ops amg88xx_hwmon_ops = { + .is_visible = amg88xx_is_visible, + .read = amg88xx_read, +}; + +static const struct hwmon_chip_info amg88xx_chip_info = { + .ops = &amg88xx_hwmon_ops, + .info = amg88xx_info, +}; + +static int amg88xx_hwmon_init(struct video_i2c_data *data) +{ + void *hwmon = devm_hwmon_device_register_with_info(&data->client->dev, + "amg88xx", data, &amg88xx_chip_info, NULL); + + return PTR_ERR(hwmon); +} +#else +#define amg88xx_hwmon_init NULL +#endif + #define AMG88XX 0 static const struct video_i2c_chip video_i2c_chip[] = { @@ -111,6 +179,7 @@ static const struct video_i2c_chip video_i2c_chip[] = { .buffer_size = 128, .bpp = 16, .xfer = &amg88xx_xfer, + .hwmon_init = amg88xx_hwmon_init, }, }; @@ -505,6 +574,14 @@ static int video_i2c_probe(struct i2c_client *client, video_set_drvdata(&data->vdev, data); i2c_set_clientdata(client, data); + if (data->chip->hwmon_init) { + ret = data->chip->hwmon_init(data); + if (ret < 0) { + dev_warn(&client->dev, + "failed to register hwmon device\n"); + } + } + ret = video_register_device(&data->vdev, VFL_TYPE_GRABBER, -1); if (ret < 0) goto error_unregister_device;