From patchwork Thu Jul 24 14:00:32 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 34225 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f69.google.com (mail-wg0-f69.google.com [74.125.82.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 3C13620C7F for ; Thu, 24 Jul 2014 14:00:54 +0000 (UTC) Received: by mail-wg0-f69.google.com with SMTP id m15sf2157263wgh.8 for ; Thu, 24 Jul 2014 07:00:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=5EkQg7SSvwthmPLc6RHqyJALP5tXFiSot7WPwAkAE4Y=; b=gQ33LCGsyA4fCMDItof2AXPfYedNKL0ky3jTGIDCmEjRsIbm3UOAEEkWSmMzRfN72l Si5pf1ICAw0LQHcUYnNzmh+qFE7U9S1KD2vpMQcNfTOEagmHHARsOKRRHSAyVjUPzqZw HUwgiuhYIlo+xat23dQQ+RFRBO8inzygJHmrEbiYR8USIunPcRt4EkWasy7XFBv3oW7M uo2R5Q74uqr8YH91IYGmq7VAk1EcYCoLL8AN5DwHBrCqCORoQX7mOIU9JGU1R49aLVQ8 ZZ+MgaAGH+ZJOjWV8QxxYEqj59fvx7Ukk72zQ97fSl3XzT/wouCO9cIQXCDlDDyqmu8a luoA== X-Gm-Message-State: ALoCoQlBOxoMgQzNRMEN0niYhWQ8CZHG2U5M8ywGOuePV8YSV30x0mxselMD6k4+33K6BokVEQP5 X-Received: by 10.180.91.73 with SMTP id cc9mr2526594wib.4.1406210452114; Thu, 24 Jul 2014 07:00:52 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.34.44 with SMTP id k41ls759684qgk.78.gmail; Thu, 24 Jul 2014 07:00:51 -0700 (PDT) X-Received: by 10.221.56.5 with SMTP id wa5mr12455011vcb.25.1406210451880; Thu, 24 Jul 2014 07:00:51 -0700 (PDT) Received: from mail-vc0-f169.google.com (mail-vc0-f169.google.com [209.85.220.169]) by mx.google.com with ESMTPS id w4si5061878vct.72.2014.07.24.07.00.51 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 24 Jul 2014 07:00:51 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.169 as permitted sender) client-ip=209.85.220.169; Received: by mail-vc0-f169.google.com with SMTP id hu12so4958357vcb.0 for ; Thu, 24 Jul 2014 07:00:51 -0700 (PDT) X-Received: by 10.52.244.138 with SMTP id xg10mr10596143vdc.40.1406210451483; Thu, 24 Jul 2014 07:00:51 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp367455vcb; Thu, 24 Jul 2014 07:00:50 -0700 (PDT) X-Received: by 10.70.35.169 with SMTP id i9mr7540842pdj.36.1406210450407; Thu, 24 Jul 2014 07:00:50 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id qs8si6069820pbb.206.2014.07.24.07.00.49 for ; Thu, 24 Jul 2014 07:00:50 -0700 (PDT) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759375AbaGXOAq (ORCPT + 13 others); Thu, 24 Jul 2014 10:00:46 -0400 Received: from mail-wg0-f51.google.com ([74.125.82.51]:57354 "EHLO mail-wg0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759178AbaGXOAo (ORCPT ); Thu, 24 Jul 2014 10:00:44 -0400 Received: by mail-wg0-f51.google.com with SMTP id b13so2722495wgh.22 for ; Thu, 24 Jul 2014 07:00:40 -0700 (PDT) X-Received: by 10.194.91.228 with SMTP id ch4mr12437204wjb.59.1406210439855; Thu, 24 Jul 2014 07:00:39 -0700 (PDT) Received: from localhost.localdomain ([85.235.11.236]) by mx.google.com with ESMTPSA id l2sm16132180wjw.31.2014.07.24.07.00.38 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 24 Jul 2014 07:00:38 -0700 (PDT) From: Linus Walleij To: Sebastian Reichel , Dmitry Eremin-Solenikov , linux-kernel@vger.kernel.org Cc: David Woodhouse , Dmitry Artamonow , Linus Walleij Subject: [PATCH v2] power: add driver for battery reading on iPaq h3xxx Date: Thu, 24 Jul 2014 16:00:32 +0200 Message-Id: <1406210432-24936-1-git-send-email-linus.walleij@linaro.org> X-Mailer: git-send-email 1.9.3 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: linus.walleij@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.169 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Dmitry Artamonow This adds a driver for reading the battery status of the battery connected to the Atmel microcontroller on the iPAQ h3xxx series. Based on a driver from handhelds.org 2.6.21 kernel, written by Alessandro GARDICH. Signed-off-by: Dmitry Artamonow Signed-off-by: Linus Walleij --- ChangeLog v1->v2: - Clarify that 100000 ms is 100 seconds - rename doubly-named local, infix power supplies with "power" --- drivers/power/Kconfig | 7 + drivers/power/Makefile | 1 + drivers/power/ipaq_micro_battery.c | 290 +++++++++++++++++++++++++++++++++++++ 3 files changed, 298 insertions(+) create mode 100644 drivers/power/ipaq_micro_battery.c diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig index ba6975123071..73cfcdf28a36 100644 --- a/drivers/power/Kconfig +++ b/drivers/power/Kconfig @@ -137,6 +137,13 @@ config BATTERY_COLLIE Say Y to enable support for the battery on the Sharp Zaurus SL-5500 (collie) models. +config BATTERY_IPAQ_MICRO + tristate "iPAQ Atmel Micro ASIC battery driver" + depends on MFD_IPAQ_MICRO + help + Choose this option if you want to monitor battery status on + Compaq/HP iPAQ h3100 and h3600. + config BATTERY_WM97XX bool "WM97xx generic battery driver" depends on TOUCHSCREEN_WM97XX=y diff --git a/drivers/power/Makefile b/drivers/power/Makefile index ee54a3e4c90a..dfa894273926 100644 --- a/drivers/power/Makefile +++ b/drivers/power/Makefile @@ -25,6 +25,7 @@ obj-$(CONFIG_BATTERY_PMU) += pmu_battery.o obj-$(CONFIG_BATTERY_OLPC) += olpc_battery.o obj-$(CONFIG_BATTERY_TOSA) += tosa_battery.o obj-$(CONFIG_BATTERY_COLLIE) += collie_battery.o +obj-$(CONFIG_BATTERY_IPAQ_MICRO) += ipaq_micro_battery.o obj-$(CONFIG_BATTERY_WM97XX) += wm97xx_battery.o obj-$(CONFIG_BATTERY_SBS) += sbs-battery.o obj-$(CONFIG_BATTERY_BQ27x00) += bq27x00_battery.o diff --git a/drivers/power/ipaq_micro_battery.c b/drivers/power/ipaq_micro_battery.c new file mode 100644 index 000000000000..54632eabf420 --- /dev/null +++ b/drivers/power/ipaq_micro_battery.c @@ -0,0 +1,290 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * h3xxx atmel micro companion support, battery subdevice + * based on previous kernel 2.4 version + * Author : Alessandro Gardich + * Author : Linus Walleij + * + */ + +#include +#include +#include +#include +#include +#include + +#define BATT_PERIOD 100000 /* 100 seconds in milliseconds */ + +#define MICRO_BATT_CHEM_ALKALINE 0x01 +#define MICRO_BATT_CHEM_NICD 0x02 +#define MICRO_BATT_CHEM_NIMH 0x03 +#define MICRO_BATT_CHEM_LION 0x04 +#define MICRO_BATT_CHEM_LIPOLY 0x05 +#define MICRO_BATT_CHEM_NOT_INSTALLED 0x06 +#define MICRO_BATT_CHEM_UNKNOWN 0xff + +#define MICRO_BATT_STATUS_HIGH 0x01 +#define MICRO_BATT_STATUS_LOW 0x02 +#define MICRO_BATT_STATUS_CRITICAL 0x04 +#define MICRO_BATT_STATUS_CHARGING 0x08 +#define MICRO_BATT_STATUS_CHARGEMAIN 0x10 +#define MICRO_BATT_STATUS_DEAD 0x20 /* Battery will not charge */ +#define MICRO_BATT_STATUS_NOTINSTALLED 0x20 /* For expansion pack batteries */ +#define MICRO_BATT_STATUS_FULL 0x40 /* Battery fully charged */ +#define MICRO_BATT_STATUS_NOBATTERY 0x80 +#define MICRO_BATT_STATUS_UNKNOWN 0xff + +struct micro_battery { + struct ipaq_micro *micro; + struct workqueue_struct *wq; + struct delayed_work update; + u8 ac; + u8 chemistry; + unsigned int voltage; + u16 temperature; + u8 flag; +}; + +static void micro_battery_work(struct work_struct *work) +{ + struct micro_battery *mb = container_of(work, + struct micro_battery, update.work); + struct ipaq_micro_msg msg_battery = { + .id = MSG_BATTERY, + }; + struct ipaq_micro_msg msg_sensor = { + .id = MSG_THERMAL_SENSOR, + }; + + /* First send battery message */ + ipaq_micro_tx_msg_sync(mb->micro, &msg_battery); + if (msg_battery.rx_len < 4) + pr_info("ERROR"); + + /* + * Returned message format: + * byte 0: 0x00 = Not plugged in + * 0x01 = AC adapter plugged in + * byte 1: chemistry + * byte 2: voltage LSB + * byte 3: voltage MSB + * byte 4: flags + * byte 5-9: same for battery 2 + */ + mb->ac = msg_battery.rx_data[0]; + mb->chemistry = msg_battery.rx_data[1]; + mb->voltage = ((((unsigned short)msg_battery.rx_data[3] << 8) + + msg_battery.rx_data[2]) * 5000L) * 1000 / 1024; + mb->flag = msg_battery.rx_data[4]; + + if (msg_battery.rx_len == 9) + pr_debug("second battery ignored\n"); + + /* Then read the sensor */ + ipaq_micro_tx_msg_sync(mb->micro, &msg_sensor); + mb->temperature = msg_sensor.rx_data[1] << 8 | msg_sensor.rx_data[0]; + + queue_delayed_work(mb->wq, &mb->update, msecs_to_jiffies(BATT_PERIOD)); +} + +static int get_capacity(struct power_supply *b) +{ + struct micro_battery *mb = dev_get_drvdata(b->dev->parent); + + switch (mb->flag & 0x07) { + case MICRO_BATT_STATUS_HIGH: + return 100; + break; + case MICRO_BATT_STATUS_LOW: + return 50; + break; + case MICRO_BATT_STATUS_CRITICAL: + return 5; + break; + default: + break; + } + return 0; +} + +static int get_status(struct power_supply *b) +{ + struct micro_battery *mb = dev_get_drvdata(b->dev->parent); + + if (mb->flag == MICRO_BATT_STATUS_UNKNOWN) + return POWER_SUPPLY_STATUS_UNKNOWN; + + if (mb->flag & MICRO_BATT_STATUS_FULL) + return POWER_SUPPLY_STATUS_FULL; + + if ((mb->flag & MICRO_BATT_STATUS_CHARGING) || + (mb->flag & MICRO_BATT_STATUS_CHARGEMAIN)) + return POWER_SUPPLY_STATUS_CHARGING; + + return POWER_SUPPLY_STATUS_DISCHARGING; +} + +static int micro_batt_get_property(struct power_supply *b, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct micro_battery *mb = dev_get_drvdata(b->dev->parent); + + switch (psp) { + case POWER_SUPPLY_PROP_TECHNOLOGY: + switch (mb->chemistry) { + case MICRO_BATT_CHEM_NICD: + val->intval = POWER_SUPPLY_TECHNOLOGY_NiCd; + break; + case MICRO_BATT_CHEM_NIMH: + val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH; + break; + case MICRO_BATT_CHEM_LION: + val->intval = POWER_SUPPLY_TECHNOLOGY_LION; + break; + case MICRO_BATT_CHEM_LIPOLY: + val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO; + break; + default: + val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; + break; + }; + break; + case POWER_SUPPLY_PROP_STATUS: + val->intval = get_status(b); + break; + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: + val->intval = 4700000; + break; + case POWER_SUPPLY_PROP_CAPACITY: + val->intval = get_capacity(b); + break; + case POWER_SUPPLY_PROP_TEMP: + val->intval = mb->temperature; + break; + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + val->intval = mb->voltage; + break; + default: + return -EINVAL; + }; + + return 0; +} + +static int micro_ac_get_property(struct power_supply *b, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct micro_battery *mb = dev_get_drvdata(b->dev->parent); + + switch (psp) { + case POWER_SUPPLY_PROP_ONLINE: + val->intval = mb->ac; + break; + default: + return -EINVAL; + }; + + return 0; +} + +static enum power_supply_property micro_batt_power_props[] = { + POWER_SUPPLY_PROP_TECHNOLOGY, + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, + POWER_SUPPLY_PROP_CAPACITY, + POWER_SUPPLY_PROP_TEMP, + POWER_SUPPLY_PROP_VOLTAGE_NOW, +}; + +static struct power_supply micro_batt_power = { + .name = "main-battery", + .type = POWER_SUPPLY_TYPE_BATTERY, + .properties = micro_batt_power_props, + .num_properties = ARRAY_SIZE(micro_batt_power_props), + .get_property = micro_batt_get_property, + .use_for_apm = 1, +}; + +static enum power_supply_property micro_ac_power_props[] = { + POWER_SUPPLY_PROP_ONLINE, +}; + +static struct power_supply micro_ac_power = { + .name = "ac", + .type = POWER_SUPPLY_TYPE_MAINS, + .properties = micro_ac_power_props, + .num_properties = ARRAY_SIZE(micro_ac_power_props), + .get_property = micro_ac_get_property, +}; + +static int micro_batt_probe(struct platform_device *pdev) +{ + struct micro_battery *mb; + + mb = devm_kzalloc(&pdev->dev, sizeof(*mb), GFP_KERNEL); + if (!mb) + return -ENOMEM; + + mb->micro = dev_get_drvdata(pdev->dev.parent); + mb->wq = create_singlethread_workqueue("ipaq-battery-wq"); + INIT_DELAYED_WORK(&mb->update, micro_battery_work); + platform_set_drvdata(pdev, mb); + queue_delayed_work(mb->wq, &mb->update, 1); + power_supply_register(&pdev->dev, µ_batt_power); + power_supply_register(&pdev->dev, µ_ac_power); + + dev_info(&pdev->dev, "iPAQ micro battery driver\n"); + return 0; +} + +static int micro_batt_remove(struct platform_device *pdev) + +{ + struct micro_battery *mb = platform_get_drvdata(pdev); + + power_supply_unregister(µ_ac_power); + power_supply_unregister(µ_batt_power); + cancel_delayed_work_sync(&mb->update); + + return 0; +} + +static int micro_batt_suspend(struct device *dev) +{ + struct micro_battery *mb = dev_get_drvdata(dev); + + cancel_delayed_work_sync(&mb->update); + return 0; +} + +static int micro_batt_resume(struct device *dev) +{ + struct micro_battery *mb = dev_get_drvdata(dev); + + queue_delayed_work(mb->wq, &mb->update, msecs_to_jiffies(BATT_PERIOD)); + return 0; +} + +static const struct dev_pm_ops micro_batt_dev_pm_ops = { + SET_SYSTEM_SLEEP_PM_OPS(micro_batt_suspend, micro_batt_resume) +}; + +struct platform_driver micro_batt_device_driver = { + .driver = { + .name = "ipaq-micro-battery", + .pm = µ_batt_dev_pm_ops, + }, + .probe = micro_batt_probe, + .remove = micro_batt_remove, +}; +module_platform_driver(micro_batt_device_driver); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("driver for iPAQ Atmel micro battery"); +MODULE_ALIAS("platform:battery-ipaq-micro");