From nobody Thu Dec 02 09:00:29 2021 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 8E9D918CA944; Thu, 2 Dec 2021 09:00:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4J4VKV12QNz4cRs; Thu, 2 Dec 2021 09:00:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id DF8E71A7E7; Thu, 2 Dec 2021 09:00:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1B290TGQ091664; Thu, 2 Dec 2021 09:00:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1B290T6c091663; Thu, 2 Dec 2021 09:00:29 GMT (envelope-from git) Date: Thu, 2 Dec 2021 09:00:29 GMT Message-Id: <202112020900.1B290T6c091663@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Wojciech Macek Subject: git: f89f6f9581bd - main - TMP461: Add thermal sensor driver List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: wma X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f89f6f9581bd00bc420ce9891e0d964d4c6cecb3 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1638435630; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yZta3fr0r4v26RYK1akx4ZIUA+yiQ1PBmMmZL4/d18Y=; b=ld8w47OuGJ84AR3UJXoQGoaO1tmcb4/af503CMneVwB4RvDG2Tmf+j6dj4E4S4cbwwen9u f1y2uEJBIShgIo02SGkYxM8UMS4tfWYgD9/NutbBPHD2dcEjbETaA9PsYArFsFBz/SREpr ZQ5VV1H50JO0bi7k3Ud599VqCFS1501r5gAQX3ipqGjNdkx4pm9ir8eNTO7FAUlbuFrFWf vqI8eHYPnac9mVu7I9jEeuc/4D9tYiwMMUfxwM2FtxGXohzBMctIE2xUslwYHkpPgYDF9Y 9InusfooqNskxQuv00fx5yY3Xqd5b3GkZIu/cz9lcMvvgNw4kT2JVDR+PFjcXg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1638435630; a=rsa-sha256; cv=none; b=U+VSECeik+LhquCivdFZ6cLXwni986tmAisOTWP0uccBiJ2Xi9t/nfnKrY6veKNrRjkNBF AYR0zwuOXtkujJWJ/TCV4Miq5Qeg3VJ0vc9ulGGAWzEsnlJoe50t288Rw29MMjG1C3eBId U+Slp4bCocaBBeC1C4IJahcMTvapiQ/0zcheJ+TzuSbSUskJ0pOGdTj/YhRrMTuWiG9cQx 6H0Acm77HMK4ftbQPzld+Ckamn4GPd42bvl7tynZLmJIrgZCT7Z7BV5SWOwGfef7on5fnA 6+gbzCE0lx3zPgBRqboZKjtkdK+ZvHe1dMCjoFqSgWwtw4ss3EP2Un35oCAbyw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by wma: URL: https://cgit.FreeBSD.org/src/commit/?id=f89f6f9581bd00bc420ce9891e0d964d4c6cecb3 commit f89f6f9581bd00bc420ce9891e0d964d4c6cecb3 Author: Hubert Mazur AuthorDate: 2021-11-26 09:26:06 +0000 Commit: Wojciech Macek CommitDate: 2021-12-02 08:18:48 +0000 TMP461: Add thermal sensor driver Add driver for TMP461 thermal sensor. Register new sysctl node of integer type for device. Read register and fill sysctl with valid temperature. Reviewed by: Sponsored by: Alstom Obtained from: Semihalf Differential revision: https://reviews.freebsd.org/D32818 --- sys/dev/iicbus/tmp461.c | 194 ++++++++++++++++++++++++++++++++++++++++ sys/modules/i2c/Makefile | 3 +- sys/modules/i2c/tmp461/Makefile | 7 ++ 3 files changed, 203 insertions(+), 1 deletion(-) diff --git a/sys/dev/iicbus/tmp461.c b/sys/dev/iicbus/tmp461.c new file mode 100644 index 000000000000..be5d547e91c1 --- /dev/null +++ b/sys/dev/iicbus/tmp461.c @@ -0,0 +1,194 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2021 Alstom Group. + * Copyright (c) 2021 Semihalf. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include "opt_platform.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#define BIT(x) (1UL << (x)) + +#define TMP461_LOCAL_TEMP_REG_MSB 0x0 +#define TMP461_LOCAL_TEMP_REG_LSB 0x15 +#define TMP461_GLOBAL_TEMP_REG_MSB 0x1 +#define TMP461_GLOBAL_TEMP_REG_LSB 0x10 +#define TMP461_CONFIG_REG 0x3 +#define TMP461_CONFIG_REG_TEMP_RANGE_BIT BIT(2) + +#define TMP461_EXTENDED_TEMP_MODIFIER 64 +/* 28.4 fixed point representation of 273.15f */ +#define TMP461_C_TO_K_FIX 4370 +#define TMP461_TEMP_LSB 0 + +static int tmp461_probe(device_t dev); +static int tmp461_attach(device_t dev); +static int tmp461_read_1(device_t dev, uint8_t reg, uint8_t *data); +static int tmp461_read_temp(device_t dev, int32_t *temp); +static int tmp461_detach(device_t dev); +static int tmp461_sensor_sysctl(SYSCTL_HANDLER_ARGS); +static int32_t tmp461_signed_extend32(uint32_t value, int sign_pos); + +static device_method_t tmp461_methods[] = { + DEVMETHOD(device_probe, tmp461_probe), + DEVMETHOD(device_attach, tmp461_attach), + DEVMETHOD(device_detach, tmp461_detach), + + DEVMETHOD_END +}; + +static driver_t tmp461_driver = { + "tmp461_dev", + tmp461_methods, + 0 +}; + +static devclass_t tmp461_devclass; + +static struct ofw_compat_data tmp461_compat_data[] = { + { "ti,tmp461", 1 }, + { NULL, 0 } +}; + +DRIVER_MODULE(tmp461, iicbus, tmp461_driver, tmp461_devclass, 0, 0); +IICBUS_FDT_PNP_INFO(tmp461_compat_data); + +static int +tmp461_attach(device_t dev) +{ + struct sysctl_oid *sensor_root_oid; + struct sysctl_ctx_list *ctx; + + ctx = device_get_sysctl_ctx(dev); + + sensor_root_oid = SYSCTL_ADD_NODE(ctx, SYSCTL_STATIC_CHILDREN(_hw), + OID_AUTO, "temperature", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, + "TMP 461 Thermal Sensor Information"); + if (sensor_root_oid == NULL) + return (ENXIO); + + SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensor_root_oid), OID_AUTO, + "tmp461", CTLTYPE_INT | CTLFLAG_RD, dev, 0, + tmp461_sensor_sysctl, "IK0", "TMP461 Thermal Sensor"); + + return (0); +} + +static int +tmp461_probe(device_t dev) +{ + + if (!ofw_bus_status_okay(dev)) + return (ENXIO); + + if (!ofw_bus_search_compatible(dev, tmp461_compat_data)->ocd_data) + return (ENXIO); + + device_set_desc(dev, "TMP461 Thermal Sensor"); + + return (BUS_PROBE_GENERIC); +} + +static int +tmp461_detach(device_t dev) +{ + + return (0); +} + +static int +tmp461_read_1(device_t dev, uint8_t reg, uint8_t *data) +{ + int error; + + error = iicdev_readfrom(dev, reg, (void *) data, 1, IIC_WAIT); + if (error != 0) + device_printf(dev, "Failed to read from device\n"); + + return (error); +} + +static int +tmp461_read_temp(device_t dev, int32_t *temp) +{ + bool extended_mode; + uint8_t data; + int error; + + /* read temperature range */ + error = tmp461_read_1(dev, TMP461_CONFIG_REG, &data); + if (error != 0) + return (ENXIO); + + extended_mode = data & TMP461_CONFIG_REG_TEMP_RANGE_BIT; + + /* read temp MSB */ + error = tmp461_read_1(dev, TMP461_LOCAL_TEMP_REG_MSB, &data); + if (error != 0) + return (ENXIO); + + *temp = signed_extend32(data, TMP461_TEMP_LSB, 8) - + (extended_mode ? TMP461_EXTENDED_TEMP_MODIFIER : 0); + + error = tmp461_read_1(dev, TMP461_LOCAL_TEMP_REG_LSB, &data); + if (error != 0) + return (ENXIO); + + *temp = (((*temp << 4) | (data >> 4)) + TMP461_C_TO_K_FIX) >> 4; + + return (0); +} + +static int +tmp461_sensor_sysctl(SYSCTL_HANDLER_ARGS) +{ + device_t dev; + int32_t temp; + int error; + + dev = arg1; + + error = tmp461_read_temp(dev, &temp); + if (error != 0) + return (error); + + return (sysctl_handle_int(oidp, &temp, 0, req)); +} + diff --git a/sys/modules/i2c/Makefile b/sys/modules/i2c/Makefile index 31c348a63dbc..2f409773ead2 100644 --- a/sys/modules/i2c/Makefile +++ b/sys/modules/i2c/Makefile @@ -30,7 +30,8 @@ SUBDIR = \ .if !empty(OPT_FDT) SUBDIR += rx8803 \ - tca6416 + tca6416 \ + tmp461 .endif .if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64" || \ diff --git a/sys/modules/i2c/tmp461/Makefile b/sys/modules/i2c/tmp461/Makefile new file mode 100644 index 000000000000..ed41dbfa9e0e --- /dev/null +++ b/sys/modules/i2c/tmp461/Makefile @@ -0,0 +1,7 @@ +# $FreeBSD$ + +.PATH: ${SRCTOP}/sys/dev/iicbus +KMOD = tmp461 +SRCS = tmp461.c bus_if.h device_if.h iicbus_if.h ofw_bus_if.h opt_platform.h + +.include