From patchwork Sat Jul 12 09:33:49 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 33522 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pa0-f69.google.com (mail-pa0-f69.google.com [209.85.220.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 481C620672 for ; Sat, 12 Jul 2014 09:34:15 +0000 (UTC) Received: by mail-pa0-f69.google.com with SMTP id kx10sf8223794pab.4 for ; Sat, 12 Jul 2014 02:34:14 -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=arXnuITsCvZ5j9tJBG0s1nV0Dmgntzo0PEqwPv6HEQ0=; b=DtbcVI789lUPbLRH9qXcY34/+0lB7KcBljHrcwIGuPIUUdK4sLr/9RvZx53ERu2iUf /npSI7zIom8nW+H8LBE4Mp253TErv3Mj8C1EElvenvmcvjpHZI0Vf14NADw1Gh50EBQN ZptnAcsrCJ5lkzFlb1+/TJ0VjbuUj19bk09cQy4qSesG0Q0dCAmipGANCTOF0ZEMWga6 r65zJyGJEK1e7hJHk2W+Jxp43VudkLT75sDqvO5YY1yk/0wCmIEPQm7Omr74ZEg6UqAp VhVoCPEkfXCI9+v+o3nfd1u1neukZe9+aZNsdybU51rJuJYEoTNiYISIq8govM9aObEw Ewqw== X-Gm-Message-State: ALoCoQnXFllP8SCYnbKty1CB9sbTkuo0eM873QUtiXXfecfObRD/YQPBIXA1HWf43EmVLC9NZds/ X-Received: by 10.66.173.75 with SMTP id bi11mr2302676pac.4.1405157654435; Sat, 12 Jul 2014 02:34:14 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.91.86 with SMTP id y80ls620234qgd.0.gmail; Sat, 12 Jul 2014 02:34:14 -0700 (PDT) X-Received: by 10.52.30.9 with SMTP id o9mr3799872vdh.15.1405157654307; Sat, 12 Jul 2014 02:34:14 -0700 (PDT) Received: from mail-vc0-f181.google.com (mail-vc0-f181.google.com [209.85.220.181]) by mx.google.com with ESMTPS id ak16si3412935vdc.93.2014.07.12.02.34.14 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 12 Jul 2014 02:34:14 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.181 as permitted sender) client-ip=209.85.220.181; Received: by mail-vc0-f181.google.com with SMTP id lf12so114240vcb.12 for ; Sat, 12 Jul 2014 02:34:14 -0700 (PDT) X-Received: by 10.58.234.103 with SMTP id ud7mr4664505vec.24.1405157652084; Sat, 12 Jul 2014 02:34:12 -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 tc5csp101153vcb; Sat, 12 Jul 2014 02:34:11 -0700 (PDT) X-Received: by 10.70.128.239 with SMTP id nr15mr4279116pdb.115.1405157651234; Sat, 12 Jul 2014 02:34:11 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id kb1si4869846pbd.171.2014.07.12.02.34.10; Sat, 12 Jul 2014 02:34:10 -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 S1752509AbaGLJeA (ORCPT + 28 others); Sat, 12 Jul 2014 05:34:00 -0400 Received: from mail-we0-f180.google.com ([74.125.82.180]:59218 "EHLO mail-we0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752152AbaGLJd4 (ORCPT ); Sat, 12 Jul 2014 05:33:56 -0400 Received: by mail-we0-f180.google.com with SMTP id k48so1175675wev.39 for ; Sat, 12 Jul 2014 02:33:55 -0700 (PDT) X-Received: by 10.194.6.10 with SMTP id w10mr5653888wjw.51.1405157635442; Sat, 12 Jul 2014 02:33:55 -0700 (PDT) Received: from localhost.localdomain.localdomain (smb-rsycl-02.wifihubtelecom.net. [213.174.124.178]) by mx.google.com with ESMTPSA id lo18sm4868929wic.1.2014.07.12.02.33.53 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 12 Jul 2014 02:33:54 -0700 (PDT) From: Linus Walleij To: Dmitry Eremin-Solenikov , linux-kernel@vger.kernel.org Cc: David Woodhouse , Dmitry Artamonow , Linus Walleij Subject: [PATCH] power: add driver for battery reading on iPaq h3xxx Date: Sat, 12 Jul 2014 11:33:49 +0200 Message-Id: <1405157629-10550-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.181 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 --- 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..f875137e4d9c --- /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 /* 10 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_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_battery = { + .name = "main-battery", + .type = POWER_SUPPLY_TYPE_BATTERY, + .properties = micro_batt_props, + .num_properties = ARRAY_SIZE(micro_batt_props), + .get_property = micro_batt_get_property, + .use_for_apm = 1, +}; + +static enum power_supply_property micro_ac_props[] = { + POWER_SUPPLY_PROP_ONLINE, +}; + +static struct power_supply micro_ac = { + .name = "ac", + .type = POWER_SUPPLY_TYPE_MAINS, + .properties = micro_ac_props, + .num_properties = ARRAY_SIZE(micro_ac_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, µ_battery); + power_supply_register(&pdev->dev, µ_ac); + + 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_supply_unregister(µ_battery); + 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");