Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756529AbZLDNfr (ORCPT ); Fri, 4 Dec 2009 08:35:47 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1756392AbZLDNfq (ORCPT ); Fri, 4 Dec 2009 08:35:46 -0500 Received: from mail-pw0-f42.google.com ([209.85.160.42]:49175 "EHLO mail-pw0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756359AbZLDNfp (ORCPT ); Fri, 4 Dec 2009 08:35:45 -0500 DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=nQm7m421u5KPn9BM5kuEdod1lehTrKm2xp7pzaQt503ZNDNZsRogx2E3nBpGRnGiKO z1s3mDKOqQx61YD/OLWcH5aCq1BGHiMoyTHtGOLKfy8WjZHMBloJr0jKfatMZOV4c1fS Sn65IU7xSFs42HQqeujywmQz2tPTB/Hc/V0ck= From: Wu Zhangjin To: Ralf Baechle , akpm@linux-foundation.org, linux-mips@linux-mips.org, linux-kernel@vger.kernel.org Cc: Dmitry Torokhov , Pavel Machek , "Rafael J. Wysocki" , zhangfx@lemote.com, linux-laptop@vger.kernel.org, Wu Zhangjin Subject: [PATCH v7 5/8] Loongson: YeeLoong: add hardware monitoring driver Date: Fri, 4 Dec 2009 21:35:28 +0800 Message-Id: <102732263f647e47216c1f2cb121c30226cc995e.1259932036.git.wuzhangjin@gmail.com> X-Mailer: git-send-email 1.6.2.1 In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 7247 Lines: 284 From: Wu Zhangjin This patch adds hardware monitoring driver, it provides standard interface(/sys/class/hwmon/) for lm-sensors/sensors-applet to monitor the temperatures of CPU and battery, the PWM of fan, the current, voltage of battery. Signed-off-by: Wu Zhangjin --- drivers/platform/mips/Kconfig | 1 + drivers/platform/mips/yeeloong_laptop.c | 221 +++++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+), 0 deletions(-) diff --git a/drivers/platform/mips/Kconfig b/drivers/platform/mips/Kconfig index 0c6b5ad..9c8385c 100644 --- a/drivers/platform/mips/Kconfig +++ b/drivers/platform/mips/Kconfig @@ -20,6 +20,7 @@ config LEMOTE_YEELOONG2F select BACKLIGHT_CLASS_DEVICE select SYS_SUPPORTS_APM_EMULATION select APM_EMULATION + select HWMON default m help YeeLoong netbook is a mini laptop made by Lemote, which is basically diff --git a/drivers/platform/mips/yeeloong_laptop.c b/drivers/platform/mips/yeeloong_laptop.c index 729e368..644aaa7 100644 --- a/drivers/platform/mips/yeeloong_laptop.c +++ b/drivers/platform/mips/yeeloong_laptop.c @@ -14,6 +14,8 @@ #include /* for backlight subdriver */ #include #include /* for battery subdriver */ +#include /* for hwmon subdriver */ +#include #include @@ -184,6 +186,217 @@ static void yeeloong_battery_exit(void) apm_get_power_status = NULL; } +/* hwmon subdriver */ + +/* pwm(auto/manual) enable or not */ +static int get_fan_pwm_enable(void) +{ + return ec_read(REG_FAN_AUTO_MAN_SWITCH); +} + +static void set_fan_pwm_enable(int manual) +{ + ec_write(REG_FAN_AUTO_MAN_SWITCH, !!manual); +} + +static int get_fan_pwm(void) +{ + return ec_read(REG_FAN_SPEED_LEVEL); +} + +static void set_fan_pwm(int value) +{ + int status; + + value = SENSORS_LIMIT(value, 0, 3); + + /* If value is not ZERO, We should ensure it is on */ + if (value != 0) { + status = ec_read(REG_FAN_STATUS); + if (status == 0) + ec_write(REG_FAN_CONTROL, BIT_FAN_CONTROL_ON); + } + ec_write(REG_FAN_SPEED_LEVEL, value); +} + +static int get_fan_rpm(void) +{ + int value; + + value = FAN_SPEED_DIVIDER / + (((ec_read(REG_FAN_SPEED_HIGH) & 0x0f) << 8) | + ec_read(REG_FAN_SPEED_LOW)); + + return value; +} + +static int get_cpu_temp(void) +{ + int value; + + value = ec_read(REG_TEMPERATURE_VALUE); + + if (value & (1 << 7)) + value = (value & 0x7f) - 128; + else + value = value & 0xff; + + return value * 1000; +} + +static int get_battery_temp(void) +{ + int value; + + value = (ec_read(REG_BAT_TEMPERATURE_HIGH) << 8) | + (ec_read(REG_BAT_TEMPERATURE_LOW)); + + return value * 1000; +} + +static int get_battery_temp_alarm(void) +{ + int status; + + status = (ec_read(REG_BAT_CHARGE_STATUS) & + BIT_BAT_CHARGE_STATUS_OVERTEMP); + + return !!status; +} + +static int get_battery_current(void) +{ + int value; + + value = (ec_read(REG_BAT_CURRENT_HIGH) << 8) | + (ec_read(REG_BAT_CURRENT_LOW)); + + if (value & 0x8000) + value = 0xffff - value; + + return value; +} + +static int get_battery_voltage(void) +{ + int value; + + value = (ec_read(REG_BAT_VOLTAGE_HIGH) << 8) | + (ec_read(REG_BAT_VOLTAGE_LOW)); + + return value; +} + + +static int parse_arg(const char *buf, unsigned long count, int *val) +{ + if (!count) + return 0; + if (sscanf(buf, "%i", val) != 1) + return -EINVAL; + return count; +} + +static ssize_t store_sys_hwmon(void (*set) (int), const char *buf, size_t count) +{ + int rv, value; + + rv = parse_arg(buf, count, &value); + if (rv > 0) + set(value); + return rv; +} + +static ssize_t show_sys_hwmon(int (*get) (void), char *buf) +{ + return sprintf(buf, "%d\n", get()); +} + +#define CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \ + static ssize_t show_##_name(struct device *dev, \ + struct device_attribute *attr, \ + char *buf) \ + { \ + return show_sys_hwmon(_set, buf); \ + } \ + static ssize_t store_##_name(struct device *dev, \ + struct device_attribute *attr, \ + const char *buf, size_t count) \ + { \ + return store_sys_hwmon(_get, buf, count); \ + } \ + static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0); + +CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, get_fan_rpm, NULL); +CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR, get_fan_pwm, set_fan_pwm); +CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, get_fan_pwm_enable, + set_fan_pwm_enable); +CREATE_SENSOR_ATTR(temp1_input, S_IRUGO, get_cpu_temp, NULL); +CREATE_SENSOR_ATTR(temp2_input, S_IRUGO, get_battery_temp, NULL); +CREATE_SENSOR_ATTR(temp2_max_alarm, S_IRUGO, get_battery_temp_alarm, NULL); +CREATE_SENSOR_ATTR(curr1_input, S_IRUGO, get_battery_current, NULL); +CREATE_SENSOR_ATTR(in1_input, S_IRUGO, get_battery_voltage, NULL); + +static ssize_t +show_name(struct device *dev, struct device_attribute *attr, char *buf) +{ + return sprintf(buf, "yeeloong\n"); +} + +static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0); + +static struct attribute *hwmon_attributes[] = { + &sensor_dev_attr_pwm1.dev_attr.attr, + &sensor_dev_attr_pwm1_enable.dev_attr.attr, + &sensor_dev_attr_fan1_input.dev_attr.attr, + &sensor_dev_attr_temp1_input.dev_attr.attr, + &sensor_dev_attr_temp2_input.dev_attr.attr, + &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, + &sensor_dev_attr_curr1_input.dev_attr.attr, + &sensor_dev_attr_in1_input.dev_attr.attr, + &sensor_dev_attr_name.dev_attr.attr, + NULL +}; + +static struct attribute_group hwmon_attribute_group = { + .attrs = hwmon_attributes +}; + +static struct device *yeeloong_hwmon_dev; + +static int yeeloong_hwmon_init(void) +{ + int ret; + + yeeloong_hwmon_dev = hwmon_device_register(NULL); + if (IS_ERR(yeeloong_hwmon_dev)) { + pr_err("Fail to register yeeloong hwmon device\n"); + yeeloong_hwmon_dev = NULL; + return PTR_ERR(yeeloong_hwmon_dev); + } + ret = sysfs_create_group(&yeeloong_hwmon_dev->kobj, + &hwmon_attribute_group); + if (ret) { + hwmon_device_unregister(yeeloong_hwmon_dev); + yeeloong_hwmon_dev = NULL; + return ret; + } + /* ensure fan is set to auto mode */ + set_fan_pwm_enable(BIT_FAN_AUTO); + + return 0; +} + +static void yeeloong_hwmon_exit(void) +{ + if (yeeloong_hwmon_dev) { + sysfs_remove_group(&yeeloong_hwmon_dev->kobj, + &hwmon_attribute_group); + hwmon_device_unregister(yeeloong_hwmon_dev); + yeeloong_hwmon_dev = NULL; + } +} + static struct platform_device_id platform_device_ids[] = { { .name = "yeeloong_laptop", @@ -223,11 +436,19 @@ static int __init yeeloong_init(void) yeeloong_battery_init(); + ret = yeeloong_hwmon_init(); + if (ret) { + pr_err("Fail to register yeeloong hwmon driver.\n"); + yeeloong_hwmon_exit(); + return ret; + } + return 0; } static void __exit yeeloong_exit(void) { + yeeloong_hwmon_exit(); yeeloong_battery_exit(); yeeloong_backlight_exit(); platform_driver_unregister(&platform_driver); -- 1.6.2.1 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/