From patchwork Tue Apr 16 17:17:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naresh Solanki X-Patchwork-Id: 13632233 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B13FC12AAE3 for ; Tue, 16 Apr 2024 17:17:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713287861; cv=none; b=EDxoeta0I18XuF1KEtBUUcFeYHmSJ28zAOn+xJ0QUKF0Il0W095Z92pgxi2kdwlDu6BnWyzk0y0LiZYOxNQJAsy20lfUDyKvP75knrYOxx8vLQHugk+SUxBKWfMEVkTrCdKtDHvsWVxGI8Lz6EISgN3xTEX4/TgG+aqMtBil6xw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713287861; c=relaxed/simple; bh=/GxMiRdI/JMTI1kbhEn4+RjTeCislwirrmxHYfWYVH0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=akzVBQ9xy3UF0C5t04ASTk5r2SKMZO38l3G7niGR1QJgnoGILQvAI2OgHl/ZaxABhKF+9Ix6nLYAv680ih1iaMZSUvlpwDDTlYT5HRYm/EKwKnJe6CT6t6+Kbj3VmYl1MHB52LfRu6y7f5dC3v87cHutgtAFgCLF3HQjNIIBegc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=9elements.com; spf=pass smtp.mailfrom=9elements.com; dkim=pass (2048-bit key) header.d=9elements.com header.i=@9elements.com header.b=gQqHNfl8; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=9elements.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=9elements.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=9elements.com header.i=@9elements.com header.b="gQqHNfl8" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-4187731a547so14007095e9.0 for ; Tue, 16 Apr 2024 10:17:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9elements.com; s=google; t=1713287857; x=1713892657; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=iyPmW5Ym/nFDwOVsvSG6SeSPNyoctNpvlwNiBL7nS1k=; b=gQqHNfl82laqKZ5zEsO+77iB83M3IVAqXvahb7xG9mQl3sPqce/2KVVSrMoL9U2th6 GB6kJU/YKCdKZ5sCku93J43haw/s852PMOZFjkiK6GV15kUp4hmpI9JZqvOBT1ag4kH4 3z5HISQklGuUxUSvX7x989gjV/k6zwHS/extKVQXxQpxOAIcCCCqEcHSJWFWrVnH/Jg3 bRssHxqFxlOF5rXU80RcB16XPxSuVRIPm5pV2fTo9hEIykQCuAxf5Hz1Ux/v8vr6e+7g M5tKVEe+y2XZzMU1+PeA32jhXyKyQjuZQJHbLZd2otHOUlGP/mWeq64n6ijE2p5q3rZE YN/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713287857; x=1713892657; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=iyPmW5Ym/nFDwOVsvSG6SeSPNyoctNpvlwNiBL7nS1k=; b=hO+0YK3eNLbKAZAhcDd45tzMHNftwYAF+izEhgHkP/UIFH4l1go9j+irO4iNDXw3U9 vVir1lEgIO3Tcn9V/lix4Yc6CWes8TxkFhYYYyyXQFz2phRH5+IM5QLga+rXh+yrCrwe EhFrldWNcJUoEzTw/LG+FVRAYqHNZ/ArvMR7Ot9SsHiEjuNbSb4Mnceg+V+x7hF8iQR/ McK6ivMLmkKrak5fgpM2HziMIXuv6zgDQKxlWkmoq3mnvJl0mX8QoKg9VCUKXCR2d5fO IGFk/YzIe8LrPReVo/TZz4ZVW4/hy4C3uJtEc0ZKZq/zEwTUwHzqbhdBQfX1dF6Dyb4y OyCw== X-Forwarded-Encrypted: i=1; AJvYcCVNJSals6p8U6h04MCfnysfhqczSIr8W+iMIHyUefk/L9aV3Gcoab5ZYLqbachGff9G8lWnqBCzXEzsjs3sYXhtnkRmr6gpMhVTt2M= X-Gm-Message-State: AOJu0Yx7mhBp9RkczYiAsaEop1x+rK8oOOijcbhjZMjijO9Ns9frDRcI 7FIEfm5KLcDr/cJ0vp2AuPNo4iHsMH+fne7MFmcMYkXXfK0o9NNc1NaaW/TfKZg= X-Google-Smtp-Source: AGHT+IF1JswyXjwjHrX1jMZoPN8PRlavj5b5c6CJ9+eL+NbUpkwdszk6wxvEFJrQmiNsS7YQD+Bbhg== X-Received: by 2002:a05:600c:1907:b0:418:2636:ab4c with SMTP id j7-20020a05600c190700b004182636ab4cmr7933279wmq.6.1713287856844; Tue, 16 Apr 2024 10:17:36 -0700 (PDT) Received: from stroh80.sec.9e.network (ip-078-094-000-051.um19.pools.vodafone-ip.de. [78.94.0.51]) by smtp.gmail.com with ESMTPSA id w7-20020a05600c474700b004182fab5098sm13016326wmo.20.2024.04.16.10.17.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 10:17:36 -0700 (PDT) From: Naresh Solanki To: Guenter Roeck , krzysztof.kozlowski+dt@linaro.org, u.kleine-koenig@pengutronix.de, Jean Delvare Cc: Naresh Solanki , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/4] hwmon (max6639): Use regmap Date: Tue, 16 Apr 2024 22:47:14 +0530 Message-ID: <20240416171720.2875916-1-naresh.solanki@9elements.com> X-Mailer: git-send-email 2.42.0 Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add regmap support. Signed-off-by: Naresh Solanki --- drivers/hwmon/Kconfig | 1 + drivers/hwmon/max6639.c | 157 ++++++++++++++++++++-------------------- 2 files changed, 80 insertions(+), 78 deletions(-) base-commit: db85dba9fee5fed54e2c37eed465f8fd243a92e8 diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index c89776d91795..257ec5360e35 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -1223,6 +1223,7 @@ config SENSORS_MAX6621 config SENSORS_MAX6639 tristate "Maxim MAX6639 sensor chip" depends on I2C + select REGMAP_I2C help If you say yes here you get support for the MAX6639 sensor chips. diff --git a/drivers/hwmon/max6639.c b/drivers/hwmon/max6639.c index aa7f21ab2395..1af93fc53cb5 100644 --- a/drivers/hwmon/max6639.c +++ b/drivers/hwmon/max6639.c @@ -20,6 +20,7 @@ #include #include #include +#include /* Addresses to scan */ static const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END }; @@ -57,6 +58,8 @@ static const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END }; #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED 0x40 +#define MAX6639_NDEV 2 + static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 }; #define FAN_FROM_REG(val, rpm_range) ((val) == 0 || (val) == 255 ? \ @@ -67,7 +70,7 @@ static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 }; * Client data (each client gets its own) */ struct max6639_data { - struct i2c_client *client; + struct regmap *regmap; struct mutex update_lock; bool valid; /* true if following fields are valid */ unsigned long last_updated; /* In jiffies */ @@ -95,9 +98,8 @@ struct max6639_data { static struct max6639_data *max6639_update_device(struct device *dev) { struct max6639_data *data = dev_get_drvdata(dev); - struct i2c_client *client = data->client; struct max6639_data *ret = data; - int i; + int i, err; int status_reg; mutex_lock(&data->update_lock); @@ -105,39 +107,35 @@ static struct max6639_data *max6639_update_device(struct device *dev) if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { int res; - dev_dbg(&client->dev, "Starting max6639 update\n"); + dev_dbg(dev, "Starting max6639 update\n"); - status_reg = i2c_smbus_read_byte_data(client, - MAX6639_REG_STATUS); - if (status_reg < 0) { - ret = ERR_PTR(status_reg); + err = regmap_read(data->regmap, MAX6639_REG_STATUS, &status_reg); + if (err < 0) { + ret = ERR_PTR(err); goto abort; } data->status = status_reg; for (i = 0; i < 2; i++) { - res = i2c_smbus_read_byte_data(client, - MAX6639_REG_FAN_CNT(i)); - if (res < 0) { - ret = ERR_PTR(res); + err = regmap_read(data->regmap, MAX6639_REG_FAN_CNT(i), &res); + if (err < 0) { + ret = ERR_PTR(err); goto abort; } data->fan[i] = res; - res = i2c_smbus_read_byte_data(client, - MAX6639_REG_TEMP_EXT(i)); - if (res < 0) { - ret = ERR_PTR(res); + err = regmap_read(data->regmap, MAX6639_REG_TEMP_EXT(i), &res); + if (err < 0) { + ret = ERR_PTR(err); goto abort; } data->temp[i] = res >> 5; data->temp_fault[i] = res & 0x01; - res = i2c_smbus_read_byte_data(client, - MAX6639_REG_TEMP(i)); - if (res < 0) { - ret = ERR_PTR(res); + err = regmap_read(data->regmap, MAX6639_REG_TEMP(i), &res); + if (err < 0) { + ret = ERR_PTR(err); goto abort; } data->temp[i] |= res << 3; @@ -193,7 +191,6 @@ static ssize_t temp_max_store(struct device *dev, { struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); struct max6639_data *data = dev_get_drvdata(dev); - struct i2c_client *client = data->client; unsigned long val; int res; @@ -203,9 +200,8 @@ static ssize_t temp_max_store(struct device *dev, mutex_lock(&data->update_lock); data->temp_therm[attr->index] = TEMP_LIMIT_TO_REG(val); - i2c_smbus_write_byte_data(client, - MAX6639_REG_THERM_LIMIT(attr->index), - data->temp_therm[attr->index]); + regmap_write(data->regmap, MAX6639_REG_THERM_LIMIT(attr->index), + data->temp_therm[attr->index]); mutex_unlock(&data->update_lock); return count; } @@ -225,7 +221,6 @@ static ssize_t temp_crit_store(struct device *dev, { struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); struct max6639_data *data = dev_get_drvdata(dev); - struct i2c_client *client = data->client; unsigned long val; int res; @@ -235,9 +230,8 @@ static ssize_t temp_crit_store(struct device *dev, mutex_lock(&data->update_lock); data->temp_alert[attr->index] = TEMP_LIMIT_TO_REG(val); - i2c_smbus_write_byte_data(client, - MAX6639_REG_ALERT_LIMIT(attr->index), - data->temp_alert[attr->index]); + regmap_write(data->regmap, MAX6639_REG_ALERT_LIMIT(attr->index), + data->temp_alert[attr->index]); mutex_unlock(&data->update_lock); return count; } @@ -258,7 +252,6 @@ static ssize_t temp_emergency_store(struct device *dev, { struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); struct max6639_data *data = dev_get_drvdata(dev); - struct i2c_client *client = data->client; unsigned long val; int res; @@ -268,9 +261,7 @@ static ssize_t temp_emergency_store(struct device *dev, mutex_lock(&data->update_lock); data->temp_ot[attr->index] = TEMP_LIMIT_TO_REG(val); - i2c_smbus_write_byte_data(client, - MAX6639_REG_OT_LIMIT(attr->index), - data->temp_ot[attr->index]); + regmap_write(data->regmap, MAX6639_REG_OT_LIMIT(attr->index), data->temp_ot[attr->index]); mutex_unlock(&data->update_lock); return count; } @@ -290,7 +281,6 @@ static ssize_t pwm_store(struct device *dev, { struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); struct max6639_data *data = dev_get_drvdata(dev); - struct i2c_client *client = data->client; unsigned long val; int res; @@ -302,9 +292,7 @@ static ssize_t pwm_store(struct device *dev, mutex_lock(&data->update_lock); data->pwm[attr->index] = (u8)(val * 120 / 255); - i2c_smbus_write_byte_data(client, - MAX6639_REG_TARGTDUTY(attr->index), - data->pwm[attr->index]); + regmap_write(data->regmap, MAX6639_REG_TARGTDUTY(attr->index), data->pwm[attr->index]); mutex_unlock(&data->update_lock); return count; } @@ -411,8 +399,7 @@ static int max6639_init_client(struct i2c_client *client, int err; /* Reset chip to default values, see below for GCONFIG setup */ - err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG, - MAX6639_GCONFIG_POR); + err = regmap_write(data->regmap, MAX6639_REG_GCONFIG, MAX6639_GCONFIG_POR); if (err) goto exit; @@ -431,26 +418,21 @@ static int max6639_init_client(struct i2c_client *client, for (i = 0; i < 2; i++) { /* Set Fan pulse per revolution */ - err = i2c_smbus_write_byte_data(client, - MAX6639_REG_FAN_PPR(i), - data->ppr << 6); + err = regmap_write(data->regmap, MAX6639_REG_FAN_PPR(i), data->ppr << 6); if (err) goto exit; /* Fans config PWM, RPM */ - err = i2c_smbus_write_byte_data(client, - MAX6639_REG_FAN_CONFIG1(i), - MAX6639_FAN_CONFIG1_PWM | rpm_range); + err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG1(i), + MAX6639_FAN_CONFIG1_PWM | rpm_range); if (err) goto exit; /* Fans PWM polarity high by default */ if (max6639_info && max6639_info->pwm_polarity == 0) - err = i2c_smbus_write_byte_data(client, - MAX6639_REG_FAN_CONFIG2a(i), 0x00); + err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG2a(i), 0x00); else - err = i2c_smbus_write_byte_data(client, - MAX6639_REG_FAN_CONFIG2a(i), 0x02); + err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG2a(i), 0x02); if (err) goto exit; @@ -458,9 +440,8 @@ static int max6639_init_client(struct i2c_client *client, * /THERM full speed enable, * PWM frequency 25kHz, see also GCONFIG below */ - err = i2c_smbus_write_byte_data(client, - MAX6639_REG_FAN_CONFIG3(i), - MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03); + err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG3(i), + MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03); if (err) goto exit; @@ -468,32 +449,26 @@ static int max6639_init_client(struct i2c_client *client, data->temp_therm[i] = 80; data->temp_alert[i] = 90; data->temp_ot[i] = 100; - err = i2c_smbus_write_byte_data(client, - MAX6639_REG_THERM_LIMIT(i), - data->temp_therm[i]); + err = regmap_write(data->regmap, MAX6639_REG_THERM_LIMIT(i), data->temp_therm[i]); if (err) goto exit; - err = i2c_smbus_write_byte_data(client, - MAX6639_REG_ALERT_LIMIT(i), - data->temp_alert[i]); + err = regmap_write(data->regmap, MAX6639_REG_ALERT_LIMIT(i), data->temp_alert[i]); if (err) goto exit; - err = i2c_smbus_write_byte_data(client, - MAX6639_REG_OT_LIMIT(i), data->temp_ot[i]); + err = regmap_write(data->regmap, MAX6639_REG_OT_LIMIT(i), data->temp_ot[i]); if (err) goto exit; /* PWM 120/120 (i.e. 100%) */ data->pwm[i] = 120; - err = i2c_smbus_write_byte_data(client, - MAX6639_REG_TARGTDUTY(i), data->pwm[i]); + err = regmap_write(data->regmap, MAX6639_REG_TARGTDUTY(i), data->pwm[i]); if (err) goto exit; } /* Start monitoring */ - err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG, - MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL | - MAX6639_GCONFIG_PWM_FREQ_HI); + err = regmap_write(data->regmap, MAX6639_REG_GCONFIG, + MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL | + MAX6639_GCONFIG_PWM_FREQ_HI); exit: return err; } @@ -524,6 +499,30 @@ static void max6639_regulator_disable(void *data) regulator_disable(data); } +static bool max6639_regmap_is_volatile(struct device *dev, unsigned int reg) +{ + switch (reg) { + case MAX6639_REG_TEMP(0): + case MAX6639_REG_TEMP_EXT(0): + case MAX6639_REG_TEMP(1): + case MAX6639_REG_TEMP_EXT(1): + case MAX6639_REG_STATUS: + case MAX6639_REG_FAN_CNT(0): + case MAX6639_REG_FAN_CNT(1): + return true; + default: + return false; + } +} + +static const struct regmap_config max6639_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = MAX6639_REG_DEVREV, + .cache_type = REGCACHE_MAPLE, + .volatile_reg = max6639_regmap_is_volatile, +}; + static int max6639_probe(struct i2c_client *client) { struct device *dev = &client->dev; @@ -535,7 +534,11 @@ static int max6639_probe(struct i2c_client *client) if (!data) return -ENOMEM; - data->client = client; + data->regmap = devm_regmap_init_i2c(client, &max6639_regmap_config); + if (IS_ERR(data->regmap)) + return dev_err_probe(dev, + PTR_ERR(data->regmap), + "regmap initialization failed\n"); data->reg = devm_regulator_get_optional(dev, "fan"); if (IS_ERR(data->reg)) { @@ -573,25 +576,24 @@ static int max6639_probe(struct i2c_client *client) static int max6639_suspend(struct device *dev) { - struct i2c_client *client = to_i2c_client(dev); struct max6639_data *data = dev_get_drvdata(dev); - int ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG); + int ret, err; + + err = regmap_read(data->regmap, MAX6639_REG_GCONFIG, &ret); - if (ret < 0) - return ret; + if (err < 0) + return err; if (data->reg) regulator_disable(data->reg); - return i2c_smbus_write_byte_data(client, - MAX6639_REG_GCONFIG, ret | MAX6639_GCONFIG_STANDBY); + return regmap_write(data->regmap, MAX6639_REG_GCONFIG, ret | MAX6639_GCONFIG_STANDBY); } static int max6639_resume(struct device *dev) { - struct i2c_client *client = to_i2c_client(dev); struct max6639_data *data = dev_get_drvdata(dev); - int ret; + int ret, err; if (data->reg) { ret = regulator_enable(data->reg); @@ -601,12 +603,11 @@ static int max6639_resume(struct device *dev) } } - ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG); - if (ret < 0) - return ret; + err = regmap_read(data->regmap, MAX6639_REG_GCONFIG, &ret); + if (err < 0) + return err; - return i2c_smbus_write_byte_data(client, - MAX6639_REG_GCONFIG, ret & ~MAX6639_GCONFIG_STANDBY); + return regmap_write(data->regmap, MAX6639_REG_GCONFIG, ret & ~MAX6639_GCONFIG_STANDBY); } static const struct i2c_device_id max6639_id[] = { From patchwork Tue Apr 16 17:17:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naresh Solanki X-Patchwork-Id: 13632234 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50AA2131BD3 for ; Tue, 16 Apr 2024 17:17:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713287862; cv=none; b=RKSMnw3clWAMjyzrqKGhOX/jK11CQV7btmyAa6Ya3WmHS/xUgrQ6J0//DDoSz7UySlTh3nmAUz1TVMRq0U3Ll0wGvQYvgMr9+MmSS/jwScssAWM6BIk9I5cmlwm5GQO8E5New9xNceTIPzCpoLJDavhQvwB1YCzm2OhUByvWET4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713287862; c=relaxed/simple; bh=EKXb7yQw/fe0FE8px0sxnaU+bNGR8VwStnqSeNJAK78=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EpNV2hQRYY18k2LgxsqGlqXxWr2gkWfuAY/Ywqj2eO6c7kZ/7wVgtg52LTkcT0nZMOdaufSPC2U9OugYJvdGWVkQyF1A4asLch1pzShOGcHdk+9+kxv6wn0xTqDMr6dsQrTJA4TjNOb5K4TMwri4Z1KX8dzPgprNx/atr5c9+90= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=9elements.com; spf=pass smtp.mailfrom=9elements.com; dkim=pass (2048-bit key) header.d=9elements.com header.i=@9elements.com header.b=c7Jb77B/; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=9elements.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=9elements.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=9elements.com header.i=@9elements.com header.b="c7Jb77B/" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-41868bb71cfso14095405e9.0 for ; Tue, 16 Apr 2024 10:17:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9elements.com; s=google; t=1713287859; x=1713892659; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3OfhnCdRCj4VDt6eW9kP3dOI/qEqIsCJXQa6tBxYksk=; b=c7Jb77B/BPEIHOx7JJlTtzAV0DJKPXAdmN2W/lq7AVN2h+5m+LJeoPv53c7Z4aXp5+ 8bdC1d+3QftXvakUAYPw6uiIQLz7EpnNU4ofO6UJ3tYQk+VYwjoiNc0QFeTNo76llCz0 0SSwomDBu+XKxESkn61DlE5VvdgMS8Ea1sJkOtKABLAEisc+xjRhUB6lfpdhkpohZYqm 52JRgd012xA7poByPzcpP9TpR7T0vggHLEagxI1fAsWhSwQkHiROwkQJDyqKlTSRuftC //mZRhBvCMfI7mUuPBPUb/UjfC5Hzg+NC0ln0+TeLQNfB3znNoY9Ni/A0P0OanVW4Fw4 EWYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713287859; x=1713892659; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3OfhnCdRCj4VDt6eW9kP3dOI/qEqIsCJXQa6tBxYksk=; b=nNja/RvJq1DVuyzw/lPSAWCLa4eTJaEkDkDZlZ1U3T1UOevE6P2CkI4IB7OtwolIkz MCAVvRnp0Ddfjfdq77zqb/19J2ioAyzuUGzrJS7RWNfEekvJ9YG54Xa4bWdO4ghGaIvB qlw5wYJkto0KjrCXFkrYkqbMAnQ56vNIwDh8EDLu5r2f5MfeGYOUDaCUjj8YW3ioCUHB d+v+XSc89ScRqy/xBgAm8s2nhKpNIRJCT4Ls+hxZzulIT90/f3c7PCmrAdXOtZo8xwEJ u+1h3KC+Npw7d3C0leIwcPkEXOC605fMA1bO37KYTuXzKe4BpzSCbxQIFPStnJ4iwymv BSNQ== X-Forwarded-Encrypted: i=1; AJvYcCX7SnowGLXX2y4NFa7qg/eG+3npDMHJ0zDzwcNs16sGCi/g2GlsP9QfnZ04547QUugmdF0ohmp55NG8AhcZ42tG2BWAwUq/x0TSMKI= X-Gm-Message-State: AOJu0YzCawT7qq5S1n6lqUZPGk0/BbimF/fquZFnz70VsJdIjVmc8ahC MWx4+SlI2CwgD+zA7fYpzM4sx8WFkmZX5ilyLZXckWkpdTxDE4xro/rBYIC3Q24= X-Google-Smtp-Source: AGHT+IEzjiaX1n6oQ+liuW4MzJZY5btpgRV0JOFHDzPw647K0dBRyKO/Tjhf3hGYY7dRl/P8Gl3KRQ== X-Received: by 2002:a05:600c:4506:b0:417:fbc2:caf8 with SMTP id t6-20020a05600c450600b00417fbc2caf8mr8135607wmo.23.1713287858595; Tue, 16 Apr 2024 10:17:38 -0700 (PDT) Received: from stroh80.sec.9e.network (ip-078-094-000-051.um19.pools.vodafone-ip.de. [78.94.0.51]) by smtp.gmail.com with ESMTPSA id w7-20020a05600c474700b004182fab5098sm13016326wmo.20.2024.04.16.10.17.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 10:17:38 -0700 (PDT) From: Naresh Solanki To: Guenter Roeck , krzysztof.kozlowski+dt@linaro.org, u.kleine-koenig@pengutronix.de, Jean Delvare Cc: Naresh Solanki , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org Subject: [PATCH 2/4] hwmon: (max6639) : Utilise pwm subsystem Date: Tue, 16 Apr 2024 22:47:15 +0530 Message-ID: <20240416171720.2875916-2-naresh.solanki@9elements.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240416171720.2875916-1-naresh.solanki@9elements.com> References: <20240416171720.2875916-1-naresh.solanki@9elements.com> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Utilise pwm subsystem for fan pwm handling Signed-off-by: Naresh Solanki --- drivers/hwmon/Kconfig | 1 + drivers/hwmon/max6639.c | 200 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 191 insertions(+), 10 deletions(-) diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index 257ec5360e35..c9cc74f8c807 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -1224,6 +1224,7 @@ config SENSORS_MAX6639 tristate "Maxim MAX6639 sensor chip" depends on I2C select REGMAP_I2C + depends on PWM help If you say yes here you get support for the MAX6639 sensor chips. diff --git a/drivers/hwmon/max6639.c b/drivers/hwmon/max6639.c index 1af93fc53cb5..f37fdd161154 100644 --- a/drivers/hwmon/max6639.c +++ b/drivers/hwmon/max6639.c @@ -20,6 +20,7 @@ #include #include #include +#include #include /* Addresses to scan */ @@ -55,6 +56,9 @@ static const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END }; #define MAX6639_GCONFIG_PWM_FREQ_HI 0x08 #define MAX6639_FAN_CONFIG1_PWM 0x80 +#define MAX6639_REG_FAN_CONFIG2a_PWM_POL 0x02 +#define MAX6639_FAN_CONFIG3_FREQ_MASK 0x03 +#define MAX6639_REG_TARGTDUTY_SLOT 120 #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED 0x40 @@ -62,6 +66,10 @@ static const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END }; static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 }; +/* Supported PWM frequency */ +static const unsigned int freq_table[] = { 20, 33, 50, 100, 5000, 8333, 12500, + 25000 }; + #define FAN_FROM_REG(val, rpm_range) ((val) == 0 || (val) == 255 ? \ 0 : (rpm_ranges[rpm_range] * 30) / (val)) #define TEMP_LIMIT_TO_REG(val) clamp_val((val) / 1000, 0, 255) @@ -93,6 +101,9 @@ struct max6639_data { /* Optional regulator for FAN supply */ struct regulator *reg; + /* max6639 pwm chip */ + struct pwm_chip chip; + struct pwm_device *pwmd[MAX6639_NDEV]; /* max6639 has two pwm device */ }; static struct max6639_data *max6639_update_device(struct device *dev) @@ -271,8 +282,11 @@ static ssize_t pwm_show(struct device *dev, struct device_attribute *dev_attr, { struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); struct max6639_data *data = dev_get_drvdata(dev); + struct pwm_state state; + + pwm_get_state(data->pwmd[attr->index], &state); - return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120); + return sprintf(buf, "%d\n", pwm_get_relative_duty_cycle(&state, 255)); } static ssize_t pwm_store(struct device *dev, @@ -281,6 +295,7 @@ static ssize_t pwm_store(struct device *dev, { struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); struct max6639_data *data = dev_get_drvdata(dev); + struct pwm_state state; unsigned long val; int res; @@ -290,10 +305,10 @@ static ssize_t pwm_store(struct device *dev, val = clamp_val(val, 0, 255); - mutex_lock(&data->update_lock); - data->pwm[attr->index] = (u8)(val * 120 / 255); - regmap_write(data->regmap, MAX6639_REG_TARGTDUTY(attr->index), data->pwm[attr->index]); - mutex_unlock(&data->update_lock); + pwm_get_state(data->pwmd[attr->index], &state); + pwm_set_relative_duty_cycle(&state, val, 255); + pwm_apply_state(data->pwmd[attr->index], &state); + return count; } @@ -373,6 +388,158 @@ static struct attribute *max6639_attrs[] = { }; ATTRIBUTE_GROUPS(max6639); +static struct max6639_data *to_max6639_pwm(struct pwm_chip *chip) +{ + return container_of(chip, struct max6639_data, chip); +} + +static int max6639_pwm_get_state(struct pwm_chip *chip, + struct pwm_device *pwm, + struct pwm_state *state) +{ + struct max6639_data *data = to_max6639_pwm(chip); + int value, i = pwm->hwpwm, x, err; + unsigned int freq; + + mutex_lock(&data->update_lock); + + err = regmap_read(data->regmap, MAX6639_REG_FAN_CONFIG1(i), &value); + if (err < 0) + goto abort; + + if (value & MAX6639_FAN_CONFIG1_PWM) { + state->enabled = true; + + /* Determine frequency from respective registers */ + err = regmap_read(data->regmap, MAX6639_REG_FAN_CONFIG3(i), &value); + if (err < 0) + goto abort; + x = value & MAX6639_FAN_CONFIG3_FREQ_MASK; + + err = regmap_read(data->regmap, MAX6639_REG_GCONFIG, &value); + if (err < 0) + goto abort; + if (value & MAX6639_GCONFIG_PWM_FREQ_HI) + x |= 0x4; + x &= 0x7; + freq = freq_table[x]; + + state->period = DIV_ROUND_UP(NSEC_PER_SEC, freq); + + err = regmap_read(data->regmap, MAX6639_REG_TARGTDUTY(i), &value); + if (err < 0) + goto abort; + /* max6639 supports 120 slots only */ + state->duty_cycle = mul_u64_u32_div(state->period, value, 120); + + err = regmap_read(data->regmap, MAX6639_REG_FAN_CONFIG2a(i), &value); + if (err < 0) + goto abort; + value &= MAX6639_REG_FAN_CONFIG2a_PWM_POL; + state->polarity = (value != 0); + } else { + state->enabled = false; + } + +abort: + mutex_unlock(&data->update_lock); + return value; +} + +static int max6639_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, + const struct pwm_state *state) +{ + struct max6639_data *data = to_max6639_pwm(chip); + int value, i = pwm->hwpwm, x, err; + unsigned int freq; + struct pwm_state cstate; + + cstate = pwm->state; + + mutex_lock(&data->update_lock); + + if (state->period != cstate.period) { + /* Configure frequency */ + freq = DIV_ROUND_UP_ULL(NSEC_PER_SEC, state->period); + + /* Chip supports limited number of frequency */ + for (x = 0; x < sizeof(freq_table); x++) + if (freq <= freq_table[x]) + break; + + err = regmap_read(data->regmap, MAX6639_REG_FAN_CONFIG3(i), &value); + if (err < 0) + goto abort; + + value &= ~MAX6639_FAN_CONFIG3_FREQ_MASK; + value |= (x & MAX6639_FAN_CONFIG3_FREQ_MASK); + err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG3(i), value); + if (err < 0) + goto abort; + + err = regmap_read(data->regmap, MAX6639_REG_GCONFIG, &value); + if (err < 0) + goto abort; + + if (x >> 2) + value &= ~MAX6639_GCONFIG_PWM_FREQ_HI; + else + value |= MAX6639_GCONFIG_PWM_FREQ_HI; + err = regmap_write(data->regmap, MAX6639_REG_GCONFIG, value); + if (err < 0) + goto abort; + } + + /* Configure dutycycle */ + if (state->duty_cycle != cstate.duty_cycle || + state->period != cstate.period) { + value = DIV_ROUND_DOWN_ULL(state->duty_cycle * MAX6639_REG_TARGTDUTY_SLOT, + state->period); + err = regmap_write(data->regmap, MAX6639_REG_TARGTDUTY(i), value); + if (err < 0) + goto abort; + } + + /* Configure polarity */ + if (state->polarity != cstate.polarity) { + err = regmap_read(data->regmap, MAX6639_REG_FAN_CONFIG2a(i), &value); + if (err < 0) + goto abort; + if (state->polarity == PWM_POLARITY_NORMAL) + value |= MAX6639_REG_FAN_CONFIG2a_PWM_POL; + else + value &= ~MAX6639_REG_FAN_CONFIG2a_PWM_POL; + err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG2a(i), value); + if (err < 0) + goto abort; + } + + if (state->enabled != cstate.enabled) { + err = regmap_read(data->regmap, MAX6639_REG_FAN_CONFIG1(i), &value); + if (err < 0) + goto abort; + if (state->enabled) + value |= MAX6639_FAN_CONFIG1_PWM; + else + value &= ~MAX6639_FAN_CONFIG1_PWM; + + err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG1(i), value); + if (err < 0) + goto abort; + } + value = 0; + +abort: + mutex_unlock(&data->update_lock); + + return err; +} + +static const struct pwm_ops max6639_pwm_ops = { + .apply = max6639_pwm_apply, + .get_state = max6639_pwm_get_state, +}; + /* * returns respective index in rpm_ranges table * 1 by default on invalid range @@ -396,6 +563,7 @@ static int max6639_init_client(struct i2c_client *client, dev_get_platdata(&client->dev); int i; int rpm_range = 1; /* default: 4000 RPM */ + struct pwm_state state; int err; /* Reset chip to default values, see below for GCONFIG setup */ @@ -459,11 +627,15 @@ static int max6639_init_client(struct i2c_client *client, if (err) goto exit; - /* PWM 120/120 (i.e. 100%) */ - data->pwm[i] = 120; - err = regmap_write(data->regmap, MAX6639_REG_TARGTDUTY(i), data->pwm[i]); - if (err) - goto exit; + dev_dbg(&client->dev, "Using chip default PWM"); + data->pwmd[i] = pwm_request_from_chip(&data->chip, i, NULL); + if (IS_ERR(data->pwmd[i])) + return PTR_ERR(data->pwmd[i]); + pwm_get_state(data->pwmd[i], &state); + state.period = DIV_ROUND_UP(NSEC_PER_SEC, 25000); + state.polarity = PWM_POLARITY_NORMAL; + pwm_set_relative_duty_cycle(&state, 0, 255); + pwm_apply_state(data->pwmd[i], &state); } /* Start monitoring */ err = regmap_write(data->regmap, MAX6639_REG_GCONFIG, @@ -540,6 +712,14 @@ static int max6639_probe(struct i2c_client *client) PTR_ERR(data->regmap), "regmap initialization failed\n"); + /* Add PWM controller of max6639 */ + data->chip.dev = dev; + data->chip.ops = &max6639_pwm_ops; + data->chip.npwm = MAX6639_NDEV; + err = devm_pwmchip_add(dev, &data->chip); + if (err < 0) + return dev_err_probe(dev, err, "failed to add PWM chip\n"); + data->reg = devm_regulator_get_optional(dev, "fan"); if (IS_ERR(data->reg)) { if (PTR_ERR(data->reg) != -ENODEV) From patchwork Tue Apr 16 17:17:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naresh Solanki X-Patchwork-Id: 13632235 Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F65A132807 for ; Tue, 16 Apr 2024 17:17:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713287865; cv=none; b=smVwsk749HZjk8SvTbVbdqPu+tTxsjf1Nn29FXuHGXKqDiD2SAO0rsYkpgVm8CRiRHU7Ys288gd4eOKgOnpFJUK/gPO37ImHCJ8hJeZmq94rswAXARaAImREFRm8UeZoYR78I7odwTIT8UkdYs4Vh5fshojJxn9mGi7oytVxc90= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713287865; c=relaxed/simple; bh=JjPWPECbgQ70y8Fda+Qzz2dyjIK5uHg4LKC4Y0x9NGA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rriq9b1hEiZqJyPVq8kEu10XTQgxqvgli5KHOSP3k4iMIM8LqcHptd3Qlh0tvzEKU2LSLSW9548iVx0RlfxsNHnk9zyUAUbqwwOuez1M/YLHsUggly2+3d/q1hClGcA8+vGoThMNoEoidoAj3HywPNiAmJJPfQEbPoQZTsOqWSo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=9elements.com; spf=pass smtp.mailfrom=9elements.com; dkim=pass (2048-bit key) header.d=9elements.com header.i=@9elements.com header.b=CXpJpjnu; arc=none smtp.client-ip=209.85.208.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=9elements.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=9elements.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=9elements.com header.i=@9elements.com header.b="CXpJpjnu" Received: by mail-lj1-f171.google.com with SMTP id 38308e7fff4ca-2d4a8bddc21so54578481fa.0 for ; Tue, 16 Apr 2024 10:17:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9elements.com; s=google; t=1713287861; x=1713892661; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9v/FQWgcRKzVZOWVCcwsYZeI4Wx0vx2fYRhr6/mkVy4=; b=CXpJpjnucCbZ/c6EO1GGlTepQV3uxHX5aSUHH9PkW7VaycTR7UdKHnVdkb6doNRLJQ 7P3h8H5psI81W9TSQYwCwU3sCwZ6cic9tobkyQpoIXHwAZPB85S5P4doJnSa78UuMdi/ hq/3IqvbJOBicogWPKcoSdVD+fJASj1c7DfrZLp7ZPHvs0gYbloI9g+xMRnksrtX88d5 CBUbh34lHroEDIkZdeTzQiXJN8cPaL342XPyt6P/GDMShGfGlG2pZvA5IFfvUeWdDvaU Ue7con+VFmW0ia9W/4AhyQoHSoeRKao/9r1JyBVwtwmIl1dx+wnosyiWW6Je4ZhKD9Wd LkvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713287861; x=1713892661; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9v/FQWgcRKzVZOWVCcwsYZeI4Wx0vx2fYRhr6/mkVy4=; b=V41UCKNzvK1NP60xcl//S6hcX212s/vv8uCT2bHwItYtGVBkX52m+67yyc5IOgEgK9 xJwZB3Y2c1io10ArQGj3tPMcthYYr9n6W+i0Me/t65vLLFiO20nqh3OMbo/aVCT6mATW hULMRGJ2+bep7NDX1FDt57lZknS9R/anTaXxSyBo1sRHiwIUM5K+lYO/qfj+tAO8ncrF pyiofFv+rw6ou7hifq+ww+Xc95rwZgg6R09RT0TstiS4okmTkjb+Z8WBGV8MfZXe9st7 fs5Y8i+c74bb3AGRyEB1BSeUng0zf3K1EFdSsvHScrQfge3DR9NucuGPNnld7Gq4Fbdq hREg== X-Forwarded-Encrypted: i=1; AJvYcCVmG/KRP99jU4mLTNMP2PnfgUwXDiFrlSlRfcG9Rbit6YgzOwj+XBx4szjecih4VZe7jEcbEaTyy+GTphNbsMVYsgvNJkkKl0OP4b4= X-Gm-Message-State: AOJu0YyAoW+v8P98geXv+JAkxHl+7VFMxlF2vW1sum5uZXA21A9tzkBm ayRj72Zt6Yt8e+a89bKotOS4cL+exQQ3cRYp7SuPlkeHqSEYrntiiq6xPBwMvOQ= X-Google-Smtp-Source: AGHT+IFu92QOtSXiVgXr3rSx99oUQvOjjnRi8SjySanUwTWrYlk/+S3nIve3uNf4tPx9Qmgggyd/iA== X-Received: by 2002:a2e:7408:0:b0:2d7:1f9e:670 with SMTP id p8-20020a2e7408000000b002d71f9e0670mr7668608ljc.0.1713287860416; Tue, 16 Apr 2024 10:17:40 -0700 (PDT) Received: from stroh80.sec.9e.network (ip-078-094-000-051.um19.pools.vodafone-ip.de. [78.94.0.51]) by smtp.gmail.com with ESMTPSA id w7-20020a05600c474700b004182fab5098sm13016326wmo.20.2024.04.16.10.17.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 10:17:39 -0700 (PDT) From: Naresh Solanki To: Guenter Roeck , krzysztof.kozlowski+dt@linaro.org, u.kleine-koenig@pengutronix.de, Jean Delvare Cc: Naresh Solanki , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/4] hwmon: (max6639) : Add hwmon init using info Date: Tue, 16 Apr 2024 22:47:16 +0530 Message-ID: <20240416171720.2875916-3-naresh.solanki@9elements.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240416171720.2875916-1-naresh.solanki@9elements.com> References: <20240416171720.2875916-1-naresh.solanki@9elements.com> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add hwmon init with info Also added additional attribute for fan i.e., fanY_pulse pwmY_freq Signed-off-by: Naresh Solanki --- drivers/hwmon/max6639.c | 408 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 379 insertions(+), 29 deletions(-) diff --git a/drivers/hwmon/max6639.c b/drivers/hwmon/max6639.c index f37fdd161154..d9b23202d7a2 100644 --- a/drivers/hwmon/max6639.c +++ b/drivers/hwmon/max6639.c @@ -62,6 +62,8 @@ static const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END }; #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED 0x40 +#define MAX6639_FAN_PPR_MASK(ppr) (((ppr) - 1) << 6) + #define MAX6639_NDEV 2 static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 }; @@ -79,25 +81,27 @@ static const unsigned int freq_table[] = { 20, 33, 50, 100, 5000, 8333, 12500, */ struct max6639_data { struct regmap *regmap; + struct device *hwmon_dev; struct mutex update_lock; bool valid; /* true if following fields are valid */ unsigned long last_updated; /* In jiffies */ /* Register values sampled regularly */ - u16 temp[2]; /* Temperature, in 1/8 C, 0..255 C */ - bool temp_fault[2]; /* Detected temperature diode failure */ - u8 fan[2]; /* Register value: TACH count for fans >=30 */ - u8 status; /* Detected channel alarms and fan failures */ + u16 temp[MAX6639_NDEV]; /* Temperature, in 1/8 C, 0..255 C */ + bool temp_fault[MAX6639_NDEV]; /* Detected temperature diode failure */ + bool fan_enable[MAX6639_NDEV]; + u8 fan[MAX6639_NDEV]; /* Register value: TACH count for fans >=30 */ + u8 status; /* Detected channel alarms and fan failures */ /* Register values only written to */ - u8 pwm[2]; /* Register value: Duty cycle 0..120 */ - u8 temp_therm[2]; /* THERM Temperature, 0..255 C (->_max) */ - u8 temp_alert[2]; /* ALERT Temperature, 0..255 C (->_crit) */ - u8 temp_ot[2]; /* OT Temperature, 0..255 C (->_emergency) */ + u8 pwm[MAX6639_NDEV]; /* Register value: Duty cycle 0..120 */ + u8 temp_therm[MAX6639_NDEV]; /* THERM Temperature, 0..255 C (->_max) */ + u8 temp_alert[MAX6639_NDEV]; /* ALERT Temperature, 0..255 C (->_crit) */ + u8 temp_ot[MAX6639_NDEV]; /* OT Temperature, 0..255 C (->_emergency) */ /* Register values initialized only once */ - u8 ppr; /* Pulses per rotation 0..3 for 1..4 ppr */ - u8 rpm_range; /* Index in above rpm_ranges table */ + u8 ppr[MAX6639_NDEV]; /* Pulses per rotation 0..3 for 1..4 ppr */ + u8 rpm_range[MAX6639_NDEV]; /* Index in above rpm_ranges table */ /* Optional regulator for FAN supply */ struct regulator *reg; @@ -128,7 +132,7 @@ static struct max6639_data *max6639_update_device(struct device *dev) data->status = status_reg; - for (i = 0; i < 2; i++) { + for (i = 0; i < MAX6639_NDEV; i++) { err = regmap_read(data->regmap, MAX6639_REG_FAN_CNT(i), &res); if (err < 0) { ret = ERR_PTR(err); @@ -322,7 +326,7 @@ static ssize_t fan_input_show(struct device *dev, return PTR_ERR(data); return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index], - data->rpm_range)); + data->rpm_range[attr->index])); } static ssize_t alarm_show(struct device *dev, @@ -388,6 +392,342 @@ static struct attribute *max6639_attrs[] = { }; ATTRIBUTE_GROUPS(max6639); +static int max6639_temp_set_max(struct max6639_data *data, int channel, unsigned long val) +{ + int res; + + mutex_lock(&data->update_lock); + data->temp_therm[channel] = TEMP_LIMIT_TO_REG(val); + res = regmap_write(data->regmap, MAX6639_REG_THERM_LIMIT(channel), + data->temp_therm[channel]); + mutex_unlock(&data->update_lock); + return res; +} + +static int max6639_temp_set_crit(struct max6639_data *data, int channel, unsigned long val) +{ + int res; + + mutex_lock(&data->update_lock); + data->temp_alert[channel] = TEMP_LIMIT_TO_REG(val); + res = regmap_write(data->regmap, MAX6639_REG_ALERT_LIMIT(channel), + data->temp_alert[channel]); + mutex_unlock(&data->update_lock); + return res; +} + +static int max6639_temp_set_emergency(struct max6639_data *data, int channel, unsigned long val) +{ + int res; + + mutex_lock(&data->update_lock); + data->temp_ot[channel] = TEMP_LIMIT_TO_REG(val); + res = regmap_write(data->regmap, MAX6639_REG_OT_LIMIT(channel), + data->temp_ot[channel]); + mutex_unlock(&data->update_lock); + return res; +} + +static int set_ppr(struct max6639_data *data, u8 channel, u8 ppr) +{ + return regmap_write(data->regmap, MAX6639_REG_FAN_PPR(channel), MAX6639_FAN_PPR_MASK(ppr)); +} + +static int max6639_read_fan(struct device *dev, u32 attr, int channel, + long *val) +{ + struct max6639_data *data = max6639_update_device(dev); + + if (IS_ERR(data)) + return PTR_ERR(data); + + switch (attr) { + case hwmon_fan_input: + *val = FAN_FROM_REG(data->fan[channel], data->rpm_range[channel]); + return 0; + case hwmon_fan_fault: + *val = !!(data->status & (2 >> channel)); + return 0; + case hwmon_fan_pulses: + *val = data->ppr[channel]; + return 0; + default: + return -EOPNOTSUPP; + } +} + +static int max6639_write_fan(struct device *dev, u32 attr, int channel, + long val) +{ + struct max6639_data *data = max6639_update_device(dev); + int err; + + if (IS_ERR(data)) + return PTR_ERR(data); + + switch (attr) { + case hwmon_fan_pulses: + if (val <= 0 || val > 5) { + dev_err(dev, "invalid pulses-per-revolution %ld. Valid range id 1 - 4.", + val); + return -EINVAL; + } + /* Set Fan pulse per revolution */ + err = set_ppr(data, channel, val); + if (err) + dev_err(dev, "Failed to set pulses-per-revolution"); + else + data->ppr[channel] = val; + return 0; + default: + return -EOPNOTSUPP; + } +} + +static umode_t max6639_fan_is_visible(const void *_data, u32 attr, int channel) +{ + struct max6639_data *data = (struct max6639_data *)_data; + + if (IS_ERR(data)) + return PTR_ERR(data); + + switch (attr) { + case hwmon_fan_input: + case hwmon_fan_fault: + if (data->fan_enable[channel]) + return 0444; + else + return 0; + case hwmon_fan_pulses: + if (data->fan_enable[channel]) + return 0644; + else + return 0; + default: + return 0; + } +} + +static int max6639_read_pwm(struct device *dev, u32 attr, int channel, + long *val) +{ + struct max6639_data *data = max6639_update_device(dev); + struct pwm_state state; + + if (IS_ERR(data)) + return PTR_ERR(data); + + switch (attr) { + case hwmon_pwm_input: + pwm_get_state(data->pwmd[channel], &state); + *val = pwm_get_relative_duty_cycle(&state, 255); + return 0; + case hwmon_pwm_freq: + pwm_get_state(data->pwmd[channel], &state); + *val = DIV_ROUND_UP_ULL(NSEC_PER_SEC, state.period); + return 0; + default: + return -EOPNOTSUPP; + } +} + +static int max6639_write_pwm(struct device *dev, u32 attr, int channel, + long val) +{ + struct max6639_data *data = max6639_update_device(dev); + struct pwm_state state; + int err, duty_cycle; + + if (IS_ERR(data)) + return PTR_ERR(data); + + switch (attr) { + case hwmon_pwm_input: + val = clamp_val(val, 0, 255); + pwm_get_state(data->pwmd[channel], &state); + pwm_set_relative_duty_cycle(&state, val, 255); + err = pwm_apply_state(data->pwmd[channel], &state); + return err; + + case hwmon_pwm_freq: + val = clamp_val(val, 0, 25000); + pwm_get_state(data->pwmd[channel], &state); + duty_cycle = pwm_get_relative_duty_cycle(&state, 255); + state.period = DIV_ROUND_UP_ULL(NSEC_PER_SEC, val); + pwm_set_relative_duty_cycle(&state, duty_cycle, 255); + err = pwm_apply_state(data->pwmd[channel], &state); + return err; + default: + return -EOPNOTSUPP; + } +} + +static umode_t max6639_pwm_is_visible(const void *_data, u32 attr, int channel) +{ + struct max6639_data *data = (struct max6639_data *)_data; + + if (IS_ERR(data)) + return PTR_ERR(data); + + if (IS_ERR(data->pwmd[channel])) + return 0; + + switch (attr) { + case hwmon_pwm_input: + case hwmon_pwm_freq: + if (data->fan_enable[channel]) + return 0644; + else + return 0; + default: + return 0; + } +} + +static int max6639_read_temp(struct device *dev, u32 attr, int channel, + long *val) +{ + struct max6639_data *data = max6639_update_device(dev); + + switch (attr) { + case hwmon_temp_input: + *val = data->temp[channel] * 125; + return 0; + case hwmon_temp_fault: + *val = data->temp_fault[channel] * 125; + return 0; + case hwmon_temp_max: + *val = data->temp_therm[channel] * 1000; + return 0; + case hwmon_temp_crit: + *val = data->temp_alert[channel] * 1000; + return 0; + case hwmon_temp_emergency: + *val = data->temp_ot[channel] * 1000; + return 0; + case hwmon_temp_max_alarm: + *val = !!(data->status & (0x08 >> channel)); + return 0; + case hwmon_temp_crit_alarm: + *val = !!(data->status & (0x80 >> channel)); + return 0; + case hwmon_temp_emergency_alarm: + *val = !!(data->status & (0x20 >> channel)); + return 0; + default: + return -EOPNOTSUPP; + } +} + +static int max6639_write_temp(struct device *dev, u32 attr, int channel, + long val) +{ + struct max6639_data *data = max6639_update_device(dev); + + switch (attr) { + case hwmon_temp_max: + return max6639_temp_set_max(data, channel, val); + case hwmon_temp_crit: + return max6639_temp_set_crit(data, channel, val); + case hwmon_temp_emergency: + return max6639_temp_set_emergency(data, channel, val); + default: + return -EOPNOTSUPP; + } +} + +static umode_t MAX6639_NDEV_is_visible(const void *_data, u32 attr, int channel) +{ + switch (attr) { + case hwmon_temp_input: + case hwmon_temp_fault: + case hwmon_temp_max_alarm: + case hwmon_temp_crit_alarm: + case hwmon_temp_emergency_alarm: + return 0444; + case hwmon_temp_max: + case hwmon_temp_crit: + case hwmon_temp_emergency: + return 0644; + default: + return 0; + } +} + +static int max6639_read(struct device *dev, enum hwmon_sensor_types type, + u32 attr, int channel, long *val) +{ + switch (type) { + case hwmon_fan: + return max6639_read_fan(dev, attr, channel, val); + case hwmon_pwm: + return max6639_read_pwm(dev, attr, channel, val); + case hwmon_temp: + return max6639_read_temp(dev, attr, channel, val); + default: + return -EOPNOTSUPP; + } +} + +static int max6639_write(struct device *dev, enum hwmon_sensor_types type, + u32 attr, int channel, long val) +{ + switch (type) { + case hwmon_fan: + return max6639_write_fan(dev, attr, channel, val); + case hwmon_pwm: + return max6639_write_pwm(dev, attr, channel, val); + case hwmon_temp: + return max6639_write_temp(dev, attr, channel, val); + default: + return -EOPNOTSUPP; + } +} + +static umode_t max6639_is_visible(const void *data, + enum hwmon_sensor_types type, + u32 attr, int channel) +{ + switch (type) { + case hwmon_fan: + return max6639_fan_is_visible(data, attr, channel); + case hwmon_pwm: + return max6639_pwm_is_visible(data, attr, channel); + case hwmon_temp: + return MAX6639_NDEV_is_visible(data, attr, channel); + default: + return 0; + } +} + +static const struct hwmon_channel_info * const max6639_info[] = { + HWMON_CHANNEL_INFO(fan, + HWMON_F_INPUT | HWMON_F_FAULT | HWMON_F_PULSES, + HWMON_F_INPUT | HWMON_F_FAULT | HWMON_F_PULSES), + HWMON_CHANNEL_INFO(pwm, + HWMON_PWM_INPUT | HWMON_PWM_FREQ, + HWMON_PWM_INPUT | HWMON_PWM_FREQ), + HWMON_CHANNEL_INFO(temp, + HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_MAX | HWMON_T_MAX_ALARM | + HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY | + HWMON_T_EMERGENCY_ALARM, + HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_MAX | HWMON_T_MAX_ALARM | + HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY | + HWMON_T_EMERGENCY_ALARM), + NULL +}; + +static const struct hwmon_ops max6639_hwmon_ops = { + .is_visible = max6639_is_visible, + .read = max6639_read, + .write = max6639_write, +}; + +static const struct hwmon_chip_info max6639_chip_info = { + .ops = &max6639_hwmon_ops, + .info = max6639_info, +}; + static struct max6639_data *to_max6639_pwm(struct pwm_chip *chip) { return container_of(chip, struct max6639_data, chip); @@ -542,18 +882,18 @@ static const struct pwm_ops max6639_pwm_ops = { /* * returns respective index in rpm_ranges table - * 1 by default on invalid range + * 3 by default on invalid range */ static int rpm_range_to_reg(int range) { int i; for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) { - if (rpm_ranges[i] == range) + if (range <= rpm_ranges[i]) return i; } - return 1; /* default: 4000 RPM */ + return 3; /* default: 16000 RPM */ } static int max6639_init_client(struct i2c_client *client, @@ -564,7 +904,7 @@ static int max6639_init_client(struct i2c_client *client, int i; int rpm_range = 1; /* default: 4000 RPM */ struct pwm_state state; - int err; + int err, ppr = 2; /* Reset chip to default values, see below for GCONFIG setup */ err = regmap_write(data->regmap, MAX6639_REG_GCONFIG, MAX6639_GCONFIG_POR); @@ -574,25 +914,27 @@ static int max6639_init_client(struct i2c_client *client, /* Fans pulse per revolution is 2 by default */ if (max6639_info && max6639_info->ppr > 0 && max6639_info->ppr < 5) - data->ppr = max6639_info->ppr; - else - data->ppr = 2; - data->ppr -= 1; + ppr = max6639_info->ppr; + ppr -= 1; + data->ppr[0] = ppr; + data->ppr[1] = ppr; if (max6639_info) rpm_range = rpm_range_to_reg(max6639_info->rpm_range); - data->rpm_range = rpm_range; + data->rpm_range[0] = rpm_range; + data->rpm_range[1] = rpm_range; - for (i = 0; i < 2; i++) { + for (i = 0; i < MAX6639_NDEV; i++) { /* Set Fan pulse per revolution */ - err = regmap_write(data->regmap, MAX6639_REG_FAN_PPR(i), data->ppr << 6); + err = regmap_write(data->regmap, MAX6639_REG_FAN_PPR(i), + MAX6639_FAN_PPR_MASK(data->ppr[i])); if (err) goto exit; /* Fans config PWM, RPM */ err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG1(i), - MAX6639_FAN_CONFIG1_PWM | rpm_range); + MAX6639_FAN_CONFIG1_PWM | data->rpm_range[i]); if (err) goto exit; @@ -617,6 +959,8 @@ static int max6639_init_client(struct i2c_client *client, data->temp_therm[i] = 80; data->temp_alert[i] = 90; data->temp_ot[i] = 100; + data->fan_enable[i] = true; + err = regmap_write(data->regmap, MAX6639_REG_THERM_LIMIT(i), data->temp_therm[i]); if (err) goto exit; @@ -699,7 +1043,6 @@ static int max6639_probe(struct i2c_client *client) { struct device *dev = &client->dev; struct max6639_data *data; - struct device *hwmon_dev; int err; data = devm_kzalloc(dev, sizeof(struct max6639_data), GFP_KERNEL); @@ -748,10 +1091,17 @@ static int max6639_probe(struct i2c_client *client) if (err < 0) return err; - hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, - data, - max6639_groups); - return PTR_ERR_OR_ZERO(hwmon_dev); + if (0) + data->hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, + data, + max6639_groups); + + data->hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data, + &max6639_chip_info, NULL); + if (IS_ERR(data->hwmon_dev)) + return dev_err_probe(dev, PTR_ERR(data->hwmon_dev), + "unable to register hwmon device\n"); + return 0; } static int max6639_suspend(struct device *dev) From patchwork Tue Apr 16 17:17:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naresh Solanki X-Patchwork-Id: 13632236 Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com [209.85.208.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 176A713328B for ; Tue, 16 Apr 2024 17:17:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713287867; cv=none; b=j7mLCuWCPg8RKXXRpIL0u0/P+FPf1yDSUc2L1KQvlmg51B9zQcgnVCj6m6PP2/2GvUtJWYif5ACMpli3/Wl6XHp4cm0IBLwPRoud2zF2vHxWxgFYqpG28fRtf27pZEYXvo9Xx+gLqYYZVKlVEMksR9lequ3+X8H/Ig9DYtHzjkY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713287867; c=relaxed/simple; bh=K+AcM58mzm3c+Yj4n6jUi7czYSLj2qZzGkTpu3p4bY4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k557l1A+1MjlN1PHaYcYy6suNTl2jeSrELjDO4vC7GAOmsmIX6w8lnI4zIBRYvLYHBfrYctm6+9Bjas3w7oV2C5X897EbDZc9WPDearT7AbZ2lzKPX9HBCPx+eIwDYmiChkCAHOMhICBKBp9f5eJTOOiPNf90SCEvLqC8NAMjYU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=9elements.com; spf=pass smtp.mailfrom=9elements.com; dkim=pass (2048-bit key) header.d=9elements.com header.i=@9elements.com header.b=beDKDeXb; arc=none smtp.client-ip=209.85.208.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=9elements.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=9elements.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=9elements.com header.i=@9elements.com header.b="beDKDeXb" Received: by mail-lj1-f181.google.com with SMTP id 38308e7fff4ca-2d6ff0422a2so58150171fa.2 for ; Tue, 16 Apr 2024 10:17:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9elements.com; s=google; t=1713287862; x=1713892662; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=q9szDnQQbde3VV2IBcUFfpImhQV+pZQtTN+ukiHfUzk=; b=beDKDeXbza7SovrS1cZv7uoN/TI1TQlvt6j2cXHrkeLlB31DCRR0pPzih2vbkytfNU 4seXUuekYrNEDcmSd8Dz2R5Vvr4J/OjVOxT6b4E0UpE4y/H/Z5/o6w8RJ4Li7Ig9pdr0 MdUDXcOyCibEudwUYSVl3rU0F2hDqGIGf7v9yBgDbj+haJrRVetEWHdooDuGkER7sGPn 2SimrAkel0Sk7uVJgdZgngcfoA6SU6t1XHIcck7x1Q0WfSjaxhD/aGDgmxYotBqcQzy8 BYby34OflAF9QNMyVdL5BgNljRf2tJgFjYuIvzKTq1nWLct+fF01VORMLZ63lBb8TjDk 9Vlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713287862; x=1713892662; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=q9szDnQQbde3VV2IBcUFfpImhQV+pZQtTN+ukiHfUzk=; b=XG2PbXJbr4LbmbptNas1SQaba1A94VJty4pDF7Qr/RDF7r+zR9rxiqgpyZvfFfAyTG uGb8zTgQINe9CMEoHnzIAgP3gCZOdeLvloKkDQPqXyuHUPvmRA8gGpS/eUeiM9IbdebB 8Io5pBw2HWt4uXThaHg11VqVxgylKlzTs6X4cbmudkyLlgwzeQ3AhBnuJDGsp1MFHZiW lqVyqYI0GnisfUJapmup4OQrSBUNdB8RiNMbGPppnqK4daQxK8NsQGSEsKr8bGrMeMA7 BDtQiSvdDPPqkEmxAvFYIw4fGc2r1/fEOmDbVQeFfhOBuKA/likZSdDueFTcrWAxSS1z tS9w== X-Forwarded-Encrypted: i=1; AJvYcCUcy1TeEz6hMIq9kfpG1L70fUS5mnBkL43+z7SWuC7WKJBkBs6b+eQgUxcqgA5bVs1JcEQuNc/jXEey/0XAqEUmDh0T8z5ZMshT3M8= X-Gm-Message-State: AOJu0YzmKfGVz1zj9n5NtRPBlVPDb5Kt2P2STq0q2mEOzPvoBTpBndt1 waMq52jF/X4PWO+Z4Ipm5ADEZks81hnAxRbCJrPWrfr/nqfktSjZ0j/yMBlRNyI= X-Google-Smtp-Source: AGHT+IGLRpchh8fCb3JwVTeDpOl5q4sI3WjzpV1Yd+fEmrWhWELlTVObROUZTQfbF04fOfPbA7xxEg== X-Received: by 2002:a2e:a308:0:b0:2d8:68ad:1e87 with SMTP id l8-20020a2ea308000000b002d868ad1e87mr7859413lje.18.1713287862022; Tue, 16 Apr 2024 10:17:42 -0700 (PDT) Received: from stroh80.sec.9e.network (ip-078-094-000-051.um19.pools.vodafone-ip.de. [78.94.0.51]) by smtp.gmail.com with ESMTPSA id w7-20020a05600c474700b004182fab5098sm13016326wmo.20.2024.04.16.10.17.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 10:17:41 -0700 (PDT) From: Naresh Solanki To: Guenter Roeck , krzysztof.kozlowski+dt@linaro.org, u.kleine-koenig@pengutronix.de, Jean Delvare Cc: Naresh Solanki , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/4] hwmon (max6639): Remove hwmon init with group Date: Tue, 16 Apr 2024 22:47:17 +0530 Message-ID: <20240416171720.2875916-4-naresh.solanki@9elements.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240416171720.2875916-1-naresh.solanki@9elements.com> References: <20240416171720.2875916-1-naresh.solanki@9elements.com> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Naresh Solanki --- drivers/hwmon/max6639.c | 232 ---------------------------------------- 1 file changed, 232 deletions(-) diff --git a/drivers/hwmon/max6639.c b/drivers/hwmon/max6639.c index d9b23202d7a2..c39f32316fe2 100644 --- a/drivers/hwmon/max6639.c +++ b/drivers/hwmon/max6639.c @@ -165,233 +165,6 @@ static struct max6639_data *max6639_update_device(struct device *dev) return ret; } -static ssize_t temp_input_show(struct device *dev, - struct device_attribute *dev_attr, char *buf) -{ - long temp; - struct max6639_data *data = max6639_update_device(dev); - struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); - - if (IS_ERR(data)) - return PTR_ERR(data); - - temp = data->temp[attr->index] * 125; - return sprintf(buf, "%ld\n", temp); -} - -static ssize_t temp_fault_show(struct device *dev, - struct device_attribute *dev_attr, char *buf) -{ - struct max6639_data *data = max6639_update_device(dev); - struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); - - if (IS_ERR(data)) - return PTR_ERR(data); - - return sprintf(buf, "%d\n", data->temp_fault[attr->index]); -} - -static ssize_t temp_max_show(struct device *dev, - struct device_attribute *dev_attr, char *buf) -{ - struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); - struct max6639_data *data = dev_get_drvdata(dev); - - return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000)); -} - -static ssize_t temp_max_store(struct device *dev, - struct device_attribute *dev_attr, - const char *buf, size_t count) -{ - struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); - struct max6639_data *data = dev_get_drvdata(dev); - unsigned long val; - int res; - - res = kstrtoul(buf, 10, &val); - if (res) - return res; - - mutex_lock(&data->update_lock); - data->temp_therm[attr->index] = TEMP_LIMIT_TO_REG(val); - regmap_write(data->regmap, MAX6639_REG_THERM_LIMIT(attr->index), - data->temp_therm[attr->index]); - mutex_unlock(&data->update_lock); - return count; -} - -static ssize_t temp_crit_show(struct device *dev, - struct device_attribute *dev_attr, char *buf) -{ - struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); - struct max6639_data *data = dev_get_drvdata(dev); - - return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000)); -} - -static ssize_t temp_crit_store(struct device *dev, - struct device_attribute *dev_attr, - const char *buf, size_t count) -{ - struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); - struct max6639_data *data = dev_get_drvdata(dev); - unsigned long val; - int res; - - res = kstrtoul(buf, 10, &val); - if (res) - return res; - - mutex_lock(&data->update_lock); - data->temp_alert[attr->index] = TEMP_LIMIT_TO_REG(val); - regmap_write(data->regmap, MAX6639_REG_ALERT_LIMIT(attr->index), - data->temp_alert[attr->index]); - mutex_unlock(&data->update_lock); - return count; -} - -static ssize_t temp_emergency_show(struct device *dev, - struct device_attribute *dev_attr, - char *buf) -{ - struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); - struct max6639_data *data = dev_get_drvdata(dev); - - return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000)); -} - -static ssize_t temp_emergency_store(struct device *dev, - struct device_attribute *dev_attr, - const char *buf, size_t count) -{ - struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); - struct max6639_data *data = dev_get_drvdata(dev); - unsigned long val; - int res; - - res = kstrtoul(buf, 10, &val); - if (res) - return res; - - mutex_lock(&data->update_lock); - data->temp_ot[attr->index] = TEMP_LIMIT_TO_REG(val); - regmap_write(data->regmap, MAX6639_REG_OT_LIMIT(attr->index), data->temp_ot[attr->index]); - mutex_unlock(&data->update_lock); - return count; -} - -static ssize_t pwm_show(struct device *dev, struct device_attribute *dev_attr, - char *buf) -{ - struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); - struct max6639_data *data = dev_get_drvdata(dev); - struct pwm_state state; - - pwm_get_state(data->pwmd[attr->index], &state); - - return sprintf(buf, "%d\n", pwm_get_relative_duty_cycle(&state, 255)); -} - -static ssize_t pwm_store(struct device *dev, - struct device_attribute *dev_attr, const char *buf, - size_t count) -{ - struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); - struct max6639_data *data = dev_get_drvdata(dev); - struct pwm_state state; - unsigned long val; - int res; - - res = kstrtoul(buf, 10, &val); - if (res) - return res; - - val = clamp_val(val, 0, 255); - - pwm_get_state(data->pwmd[attr->index], &state); - pwm_set_relative_duty_cycle(&state, val, 255); - pwm_apply_state(data->pwmd[attr->index], &state); - - return count; -} - -static ssize_t fan_input_show(struct device *dev, - struct device_attribute *dev_attr, char *buf) -{ - struct max6639_data *data = max6639_update_device(dev); - struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); - - if (IS_ERR(data)) - return PTR_ERR(data); - - return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index], - data->rpm_range[attr->index])); -} - -static ssize_t alarm_show(struct device *dev, - struct device_attribute *dev_attr, char *buf) -{ - struct max6639_data *data = max6639_update_device(dev); - struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); - - if (IS_ERR(data)) - return PTR_ERR(data); - - return sprintf(buf, "%d\n", !!(data->status & (1 << attr->index))); -} - -static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0); -static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1); -static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0); -static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1); -static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0); -static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1); -static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp_crit, 0); -static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp_crit, 1); -static SENSOR_DEVICE_ATTR_RW(temp1_emergency, temp_emergency, 0); -static SENSOR_DEVICE_ATTR_RW(temp2_emergency, temp_emergency, 1); -static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0); -static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1); -static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0); -static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1); -static SENSOR_DEVICE_ATTR_RO(fan1_fault, alarm, 1); -static SENSOR_DEVICE_ATTR_RO(fan2_fault, alarm, 0); -static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 3); -static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 2); -static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 7); -static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 6); -static SENSOR_DEVICE_ATTR_RO(temp1_emergency_alarm, alarm, 5); -static SENSOR_DEVICE_ATTR_RO(temp2_emergency_alarm, alarm, 4); - - -static struct attribute *max6639_attrs[] = { - &sensor_dev_attr_temp1_input.dev_attr.attr, - &sensor_dev_attr_temp2_input.dev_attr.attr, - &sensor_dev_attr_temp1_fault.dev_attr.attr, - &sensor_dev_attr_temp2_fault.dev_attr.attr, - &sensor_dev_attr_temp1_max.dev_attr.attr, - &sensor_dev_attr_temp2_max.dev_attr.attr, - &sensor_dev_attr_temp1_crit.dev_attr.attr, - &sensor_dev_attr_temp2_crit.dev_attr.attr, - &sensor_dev_attr_temp1_emergency.dev_attr.attr, - &sensor_dev_attr_temp2_emergency.dev_attr.attr, - &sensor_dev_attr_pwm1.dev_attr.attr, - &sensor_dev_attr_pwm2.dev_attr.attr, - &sensor_dev_attr_fan1_input.dev_attr.attr, - &sensor_dev_attr_fan2_input.dev_attr.attr, - &sensor_dev_attr_fan1_fault.dev_attr.attr, - &sensor_dev_attr_fan2_fault.dev_attr.attr, - &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, - &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, - &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, - &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, - &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr, - &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr, - NULL -}; -ATTRIBUTE_GROUPS(max6639); - static int max6639_temp_set_max(struct max6639_data *data, int channel, unsigned long val) { int res; @@ -1091,11 +864,6 @@ static int max6639_probe(struct i2c_client *client) if (err < 0) return err; - if (0) - data->hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, - data, - max6639_groups); - data->hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data, &max6639_chip_info, NULL); if (IS_ERR(data->hwmon_dev))