Linux Audio

Check our new training course

Loading...
v3.1
   1/*-*-linux-c-*-*/
   2
   3/*
   4  Copyright (C) 2008 Cezary Jackiewicz <cezary.jackiewicz (at) gmail.com>
   5
   6  based on MSI driver
   7
   8  Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
   9
  10  This program is free software; you can redistribute it and/or modify
  11  it under the terms of the GNU General Public License as published by
  12  the Free Software Foundation; either version 2 of the License, or
  13  (at your option) any later version.
  14
  15  This program is distributed in the hope that it will be useful, but
  16  WITHOUT ANY WARRANTY; without even the implied warranty of
  17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
  18  General Public License for more details.
  19
  20  You should have received a copy of the GNU General Public License
  21  along with this program; if not, write to the Free Software
  22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  23  02110-1301, USA.
  24 */
  25
  26/*
  27 * compal-laptop.c - Compal laptop support.
  28 *
  29 * This driver exports a few files in /sys/devices/platform/compal-laptop/:
  30 *   wake_up_XXX   Whether or not we listen to such wake up events (rw)
  31 *
  32 * In addition to these platform device attributes the driver
  33 * registers itself in the Linux backlight control, power_supply, rfkill
  34 * and hwmon subsystem and is available to userspace under:
  35 *
  36 *   /sys/class/backlight/compal-laptop/
  37 *   /sys/class/power_supply/compal-laptop/
  38 *   /sys/class/rfkill/rfkillX/
  39 *   /sys/class/hwmon/hwmonX/
  40 *
  41 * Notes on the power_supply battery interface:
  42 *   - the "minimum" design voltage is *the* design voltage
  43 *   - the ambient temperature is the average battery temperature
  44 *     and the value is an educated guess (see commented code below)
  45 *
  46 *
  47 * This driver might work on other laptops produced by Compal. If you
  48 * want to try it you can pass force=1 as argument to the module which
  49 * will force it to load even when the DMI data doesn't identify the
  50 * laptop as compatible.
  51 *
  52 * Lots of data available at:
  53 * http://service1.marasst.com/Compal/JHL90_91/Service%20Manual/
  54 * JHL90%20service%20manual-Final-0725.pdf
  55 *
  56 *
  57 *
  58 * Support for the Compal JHL90 added by Roald Frederickx
  59 * (roald.frederickx@gmail.com):
  60 * Driver got large revision. Added functionalities: backlight
  61 * power, wake_on_XXX, a hwmon and power_supply interface.
  62 *
  63 * In case this gets merged into the kernel source: I want to dedicate this
  64 * to Kasper Meerts, the awesome guy who showed me Linux and C!
  65 */
  66
  67/* NOTE: currently the wake_on_XXX, hwmon and power_supply interfaces are
  68 * only enabled on a JHL90 board until it is verified that they work on the
  69 * other boards too.  See the extra_features variable. */
  70
  71#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  72
  73#include <linux/module.h>
  74#include <linux/kernel.h>
  75#include <linux/init.h>
  76#include <linux/acpi.h>
  77#include <linux/dmi.h>
  78#include <linux/backlight.h>
  79#include <linux/platform_device.h>
  80#include <linux/rfkill.h>
  81#include <linux/hwmon.h>
  82#include <linux/hwmon-sysfs.h>
  83#include <linux/power_supply.h>
  84#include <linux/fb.h>
  85
  86
  87/* ======= */
  88/* Defines */
  89/* ======= */
  90#define DRIVER_NAME "compal-laptop"
  91#define DRIVER_VERSION	"0.2.7"
  92
  93#define BACKLIGHT_LEVEL_ADDR		0xB9
  94#define BACKLIGHT_LEVEL_MAX		7
  95#define BACKLIGHT_STATE_ADDR		0x59
  96#define BACKLIGHT_STATE_ON_DATA		0xE1
  97#define BACKLIGHT_STATE_OFF_DATA	0xE2
  98
  99#define WAKE_UP_ADDR			0xA4
 100#define WAKE_UP_PME			(1 << 0)
 101#define WAKE_UP_MODEM			(1 << 1)
 102#define WAKE_UP_LAN			(1 << 2)
 103#define WAKE_UP_WLAN			(1 << 4)
 104#define WAKE_UP_KEY			(1 << 6)
 105#define WAKE_UP_MOUSE			(1 << 7)
 106
 107#define WIRELESS_ADDR			0xBB
 108#define WIRELESS_WLAN			(1 << 0)
 109#define WIRELESS_BT			(1 << 1)
 110#define WIRELESS_WLAN_EXISTS		(1 << 2)
 111#define WIRELESS_BT_EXISTS		(1 << 3)
 112#define WIRELESS_KILLSWITCH		(1 << 4)
 113
 114#define PWM_ADDRESS			0x46
 115#define PWM_DISABLE_ADDR		0x59
 116#define PWM_DISABLE_DATA		0xA5
 117#define PWM_ENABLE_ADDR			0x59
 118#define PWM_ENABLE_DATA			0xA8
 119
 120#define FAN_ADDRESS			0x46
 121#define FAN_DATA			0x81
 122#define FAN_FULL_ON_CMD			0x59 /* Doesn't seem to work. Just */
 123#define FAN_FULL_ON_ENABLE		0x76 /* force the pwm signal to its */
 124#define FAN_FULL_ON_DISABLE		0x77 /* maximum value instead */
 125
 126#define TEMP_CPU			0xB0
 127#define TEMP_CPU_LOCAL			0xB1
 128#define TEMP_CPU_DTS			0xB5
 129#define TEMP_NORTHBRIDGE		0xB6
 130#define TEMP_VGA			0xB4
 131#define TEMP_SKIN			0xB2
 132
 133#define BAT_MANUFACTURER_NAME_ADDR	0x10
 134#define BAT_MANUFACTURER_NAME_LEN	9
 135#define BAT_MODEL_NAME_ADDR		0x19
 136#define BAT_MODEL_NAME_LEN		6
 137#define BAT_SERIAL_NUMBER_ADDR		0xC4
 138#define BAT_SERIAL_NUMBER_LEN		5
 139#define BAT_CHARGE_NOW			0xC2
 140#define BAT_CHARGE_DESIGN		0xCA
 141#define BAT_VOLTAGE_NOW			0xC6
 142#define BAT_VOLTAGE_DESIGN		0xC8
 143#define BAT_CURRENT_NOW			0xD0
 144#define BAT_CURRENT_AVG			0xD2
 145#define BAT_POWER			0xD4
 146#define BAT_CAPACITY			0xCE
 147#define BAT_TEMP			0xD6
 148#define BAT_TEMP_AVG			0xD7
 149#define BAT_STATUS0			0xC1
 150#define BAT_STATUS1			0xF0
 151#define BAT_STATUS2			0xF1
 152#define BAT_STOP_CHARGE1		0xF2
 153#define BAT_STOP_CHARGE2		0xF3
 154
 155#define BAT_S0_DISCHARGE		(1 << 0)
 156#define BAT_S0_DISCHRG_CRITICAL		(1 << 2)
 157#define BAT_S0_LOW			(1 << 3)
 158#define BAT_S0_CHARGING			(1 << 1)
 159#define BAT_S0_AC			(1 << 7)
 160#define BAT_S1_EXISTS			(1 << 0)
 161#define BAT_S1_FULL			(1 << 1)
 162#define BAT_S1_EMPTY			(1 << 2)
 163#define BAT_S1_LiION_OR_NiMH		(1 << 7)
 164#define BAT_S2_LOW_LOW			(1 << 0)
 165#define BAT_STOP_CHRG1_BAD_CELL		(1 << 1)
 166#define BAT_STOP_CHRG1_COMM_FAIL	(1 << 2)
 167#define BAT_STOP_CHRG1_OVERVOLTAGE	(1 << 6)
 168#define BAT_STOP_CHRG1_OVERTEMPERATURE	(1 << 7)
 169
 170
 171/* ======= */
 172/* Structs */
 173/* ======= */
 174struct compal_data{
 175	/* Fan control */
 176	struct device *hwmon_dev;
 177	int pwm_enable; /* 0:full on, 1:set by pwm1, 2:control by moterboard */
 178	unsigned char curr_pwm;
 179
 180	/* Power supply */
 181	struct power_supply psy;
 182	struct power_supply_info psy_info;
 183	char bat_model_name[BAT_MODEL_NAME_LEN + 1];
 184	char bat_manufacturer_name[BAT_MANUFACTURER_NAME_LEN + 1];
 185	char bat_serial_number[BAT_SERIAL_NUMBER_LEN + 1];
 186};
 187
 188
 189/* =============== */
 190/* General globals */
 191/* =============== */
 192static int force;
 193module_param(force, bool, 0);
 194MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
 195
 196/* Support for the wake_on_XXX, hwmon and power_supply interface. Currently
 197 * only gets enabled on a JHL90 board. Might work with the others too */
 198static bool extra_features;
 199
 200/* Nasty stuff. For some reason the fan control is very un-linear.  I've
 201 * come up with these values by looping through the possible inputs and
 202 * watching the output of address 0x4F (do an ec_transaction writing 0x33
 203 * into 0x4F and read a few bytes from the output, like so:
 204 *	u8 writeData = 0x33;
 205 *	ec_transaction(0x4F, &writeData, 1, buffer, 32);
 206 * That address is labeled "fan1 table information" in the service manual.
 207 * It should be clear which value in 'buffer' changes). This seems to be
 208 * related to fan speed. It isn't a proper 'realtime' fan speed value
 209 * though, because physically stopping or speeding up the fan doesn't
 210 * change it. It might be the average voltage or current of the pwm output.
 211 * Nevertheless, it is more fine-grained than the actual RPM reading */
 212static const unsigned char pwm_lookup_table[256] = {
 213	0, 0, 0, 1, 1, 1, 2, 253, 254, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6,
 214	7, 7, 7, 8, 86, 86, 9, 9, 9, 10, 10, 10, 11, 92, 92, 12, 12, 95,
 215	13, 66, 66, 14, 14, 98, 15, 15, 15, 16, 16, 67, 17, 17, 72, 18, 70,
 216	75, 19, 90, 90, 73, 73, 73, 21, 21, 91, 91, 91, 96, 23, 94, 94, 94,
 217	94, 94, 94, 94, 94, 94, 94, 141, 141, 238, 223, 192, 139, 139, 139,
 218	139, 139, 142, 142, 142, 142, 142, 78, 78, 78, 78, 78, 76, 76, 76,
 219	76, 76, 79, 79, 79, 79, 79, 79, 79, 20, 20, 20, 20, 20, 22, 22, 22,
 220	22, 22, 24, 24, 24, 24, 24, 24, 219, 219, 219, 219, 219, 219, 219,
 221	219, 27, 27, 188, 188, 28, 28, 28, 29, 186, 186, 186, 186, 186,
 222	186, 186, 186, 186, 186, 31, 31, 31, 31, 31, 32, 32, 32, 41, 33,
 223	33, 33, 33, 33, 252, 252, 34, 34, 34, 43, 35, 35, 35, 36, 36, 38,
 224	206, 206, 206, 206, 206, 206, 206, 206, 206, 37, 37, 37, 46, 46,
 225	47, 47, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 48, 48,
 226	48, 48, 48, 40, 40, 40, 49, 42, 42, 42, 42, 42, 42, 42, 42, 44,
 227	189, 189, 189, 189, 54, 54, 45, 45, 45, 45, 45, 45, 45, 45, 251,
 228	191, 199, 199, 199, 199, 199, 215, 215, 215, 215, 187, 187, 187,
 229	187, 187, 193, 50
 230};
 231
 232
 233
 234
 235/* ========================= */
 236/* Hardware access functions */
 237/* ========================= */
 238/* General access */
 239static u8 ec_read_u8(u8 addr)
 240{
 241	u8 value;
 242	ec_read(addr, &value);
 243	return value;
 244}
 245
 246static s8 ec_read_s8(u8 addr)
 247{
 248	return (s8)ec_read_u8(addr);
 249}
 250
 251static u16 ec_read_u16(u8 addr)
 252{
 253	int hi, lo;
 254	lo = ec_read_u8(addr);
 255	hi = ec_read_u8(addr + 1);
 256	return (hi << 8) + lo;
 257}
 258
 259static s16 ec_read_s16(u8 addr)
 260{
 261	return (s16) ec_read_u16(addr);
 262}
 263
 264static void ec_read_sequence(u8 addr, u8 *buf, int len)
 265{
 266	int i;
 267	for (i = 0; i < len; i++)
 268		ec_read(addr + i, buf + i);
 269}
 270
 271
 272/* Backlight access */
 273static int set_backlight_level(int level)
 274{
 275	if (level < 0 || level > BACKLIGHT_LEVEL_MAX)
 276		return -EINVAL;
 277
 278	ec_write(BACKLIGHT_LEVEL_ADDR, level);
 279
 280	return 0;
 281}
 282
 283static int get_backlight_level(void)
 284{
 285	return (int) ec_read_u8(BACKLIGHT_LEVEL_ADDR);
 286}
 287
 288static void set_backlight_state(bool on)
 289{
 290	u8 data = on ? BACKLIGHT_STATE_ON_DATA : BACKLIGHT_STATE_OFF_DATA;
 291	ec_transaction(BACKLIGHT_STATE_ADDR, &data, 1, NULL, 0);
 292}
 293
 294
 295/* Fan control access */
 296static void pwm_enable_control(void)
 297{
 298	unsigned char writeData = PWM_ENABLE_DATA;
 299	ec_transaction(PWM_ENABLE_ADDR, &writeData, 1, NULL, 0);
 300}
 301
 302static void pwm_disable_control(void)
 303{
 304	unsigned char writeData = PWM_DISABLE_DATA;
 305	ec_transaction(PWM_DISABLE_ADDR, &writeData, 1, NULL, 0);
 306}
 307
 308static void set_pwm(int pwm)
 309{
 310	ec_transaction(PWM_ADDRESS, &pwm_lookup_table[pwm], 1, NULL, 0);
 311}
 312
 313static int get_fan_rpm(void)
 314{
 315	u8 value, data = FAN_DATA;
 316	ec_transaction(FAN_ADDRESS, &data, 1, &value, 1);
 317	return 100 * (int)value;
 318}
 319
 320
 321
 322
 323/* =================== */
 324/* Interface functions */
 325/* =================== */
 326
 327/* Backlight interface */
 328static int bl_get_brightness(struct backlight_device *b)
 329{
 330	return get_backlight_level();
 331}
 332
 333static int bl_update_status(struct backlight_device *b)
 334{
 335	int ret = set_backlight_level(b->props.brightness);
 336	if (ret)
 337		return ret;
 338
 339	set_backlight_state((b->props.power == FB_BLANK_UNBLANK)
 340		&&    !(b->props.state & BL_CORE_SUSPENDED)
 341		&&    !(b->props.state & BL_CORE_FBBLANK));
 342	return 0;
 343}
 344
 345static const struct backlight_ops compalbl_ops = {
 346	.get_brightness = bl_get_brightness,
 347	.update_status	= bl_update_status,
 348};
 349
 350
 351/* Wireless interface */
 352static int compal_rfkill_set(void *data, bool blocked)
 353{
 354	unsigned long radio = (unsigned long) data;
 355	u8 result = ec_read_u8(WIRELESS_ADDR);
 356	u8 value;
 357
 358	if (!blocked)
 359		value = (u8) (result | radio);
 360	else
 361		value = (u8) (result & ~radio);
 362	ec_write(WIRELESS_ADDR, value);
 363
 364	return 0;
 365}
 366
 367static void compal_rfkill_poll(struct rfkill *rfkill, void *data)
 368{
 369	u8 result = ec_read_u8(WIRELESS_ADDR);
 370	bool hw_blocked = !(result & WIRELESS_KILLSWITCH);
 371	rfkill_set_hw_state(rfkill, hw_blocked);
 372}
 373
 374static const struct rfkill_ops compal_rfkill_ops = {
 375	.poll = compal_rfkill_poll,
 376	.set_block = compal_rfkill_set,
 377};
 378
 379
 380/* Wake_up interface */
 381#define SIMPLE_MASKED_STORE_SHOW(NAME, ADDR, MASK)			\
 382static ssize_t NAME##_show(struct device *dev,				\
 383	struct device_attribute *attr, char *buf)			\
 384{									\
 385	return sprintf(buf, "%d\n", ((ec_read_u8(ADDR) & MASK) != 0));	\
 386}									\
 387static ssize_t NAME##_store(struct device *dev,				\
 388	struct device_attribute *attr, const char *buf, size_t count)	\
 389{									\
 390	int state;							\
 391	u8 old_val = ec_read_u8(ADDR);					\
 392	if (sscanf(buf, "%d", &state) != 1 || (state < 0 || state > 1))	\
 393		return -EINVAL;						\
 394	ec_write(ADDR, state ? (old_val | MASK) : (old_val & ~MASK));	\
 395	return count;							\
 396}
 397
 398SIMPLE_MASKED_STORE_SHOW(wake_up_pme,	WAKE_UP_ADDR, WAKE_UP_PME)
 399SIMPLE_MASKED_STORE_SHOW(wake_up_modem,	WAKE_UP_ADDR, WAKE_UP_MODEM)
 400SIMPLE_MASKED_STORE_SHOW(wake_up_lan,	WAKE_UP_ADDR, WAKE_UP_LAN)
 401SIMPLE_MASKED_STORE_SHOW(wake_up_wlan,	WAKE_UP_ADDR, WAKE_UP_WLAN)
 402SIMPLE_MASKED_STORE_SHOW(wake_up_key,	WAKE_UP_ADDR, WAKE_UP_KEY)
 403SIMPLE_MASKED_STORE_SHOW(wake_up_mouse,	WAKE_UP_ADDR, WAKE_UP_MOUSE)
 404
 405
 406/* General hwmon interface */
 407static ssize_t hwmon_name_show(struct device *dev,
 408		struct device_attribute *attr, char *buf)
 409{
 410	return sprintf(buf, "%s\n", DRIVER_NAME);
 411}
 412
 413
 414/* Fan control interface */
 415static ssize_t pwm_enable_show(struct device *dev,
 416		struct device_attribute *attr, char *buf)
 417{
 418	struct compal_data *data = dev_get_drvdata(dev);
 419	return sprintf(buf, "%d\n", data->pwm_enable);
 420}
 421
 422static ssize_t pwm_enable_store(struct device *dev,
 423		struct device_attribute *attr, const char *buf, size_t count)
 424{
 425	struct compal_data *data = dev_get_drvdata(dev);
 426	long val;
 427	int err;
 428	err = strict_strtol(buf, 10, &val);
 
 429	if (err)
 430		return err;
 431	if (val < 0)
 432		return -EINVAL;
 433
 434	data->pwm_enable = val;
 435
 436	switch (val) {
 437	case 0:  /* Full speed */
 438		pwm_enable_control();
 439		set_pwm(255);
 440		break;
 441	case 1:  /* As set by pwm1 */
 442		pwm_enable_control();
 443		set_pwm(data->curr_pwm);
 444		break;
 445	default: /* Control by motherboard */
 446		pwm_disable_control();
 447		break;
 448	}
 449
 450	return count;
 451}
 452
 453static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
 454		char *buf)
 455{
 456	struct compal_data *data = dev_get_drvdata(dev);
 457	return sprintf(buf, "%hhu\n", data->curr_pwm);
 458}
 459
 460static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
 461		const char *buf, size_t count)
 462{
 463	struct compal_data *data = dev_get_drvdata(dev);
 464	long val;
 465	int err;
 466	err = strict_strtol(buf, 10, &val);
 
 467	if (err)
 468		return err;
 469	if (val < 0 || val > 255)
 470		return -EINVAL;
 471
 472	data->curr_pwm = val;
 473
 474	if (data->pwm_enable != 1)
 475		return count;
 476	set_pwm(val);
 477
 478	return count;
 479}
 480
 481static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
 482		char *buf)
 483{
 484	return sprintf(buf, "%d\n", get_fan_rpm());
 485}
 486
 487
 488/* Temperature interface */
 489#define TEMPERATURE_SHOW_TEMP_AND_LABEL(POSTFIX, ADDRESS, LABEL)	\
 490static ssize_t temp_##POSTFIX(struct device *dev,			\
 491		struct device_attribute *attr, char *buf)		\
 492{									\
 493	return sprintf(buf, "%d\n", 1000 * (int)ec_read_s8(ADDRESS));	\
 494}									\
 495static ssize_t label_##POSTFIX(struct device *dev,			\
 496		struct device_attribute *attr, char *buf)		\
 497{									\
 498	return sprintf(buf, "%s\n", LABEL);				\
 499}
 500
 501/* Labels as in service guide */
 502TEMPERATURE_SHOW_TEMP_AND_LABEL(cpu,        TEMP_CPU,        "CPU_TEMP");
 503TEMPERATURE_SHOW_TEMP_AND_LABEL(cpu_local,  TEMP_CPU_LOCAL,  "CPU_TEMP_LOCAL");
 504TEMPERATURE_SHOW_TEMP_AND_LABEL(cpu_DTS,    TEMP_CPU_DTS,    "CPU_DTS");
 505TEMPERATURE_SHOW_TEMP_AND_LABEL(northbridge,TEMP_NORTHBRIDGE,"NorthBridge");
 506TEMPERATURE_SHOW_TEMP_AND_LABEL(vga,        TEMP_VGA,        "VGA_TEMP");
 507TEMPERATURE_SHOW_TEMP_AND_LABEL(SKIN,       TEMP_SKIN,       "SKIN_TEMP90");
 508
 509
 510/* Power supply interface */
 511static int bat_status(void)
 512{
 513	u8 status0 = ec_read_u8(BAT_STATUS0);
 514	u8 status1 = ec_read_u8(BAT_STATUS1);
 515
 516	if (status0 & BAT_S0_CHARGING)
 517		return POWER_SUPPLY_STATUS_CHARGING;
 518	if (status0 & BAT_S0_DISCHARGE)
 519		return POWER_SUPPLY_STATUS_DISCHARGING;
 520	if (status1 & BAT_S1_FULL)
 521		return POWER_SUPPLY_STATUS_FULL;
 522	return POWER_SUPPLY_STATUS_NOT_CHARGING;
 523}
 524
 525static int bat_health(void)
 526{
 527	u8 status = ec_read_u8(BAT_STOP_CHARGE1);
 528
 529	if (status & BAT_STOP_CHRG1_OVERTEMPERATURE)
 530		return POWER_SUPPLY_HEALTH_OVERHEAT;
 531	if (status & BAT_STOP_CHRG1_OVERVOLTAGE)
 532		return POWER_SUPPLY_HEALTH_OVERVOLTAGE;
 533	if (status & BAT_STOP_CHRG1_BAD_CELL)
 534		return POWER_SUPPLY_HEALTH_DEAD;
 535	if (status & BAT_STOP_CHRG1_COMM_FAIL)
 536		return POWER_SUPPLY_HEALTH_UNKNOWN;
 537	return POWER_SUPPLY_HEALTH_GOOD;
 538}
 539
 540static int bat_is_present(void)
 541{
 542	u8 status = ec_read_u8(BAT_STATUS2);
 543	return ((status & BAT_S1_EXISTS) != 0);
 544}
 545
 546static int bat_technology(void)
 547{
 548	u8 status = ec_read_u8(BAT_STATUS1);
 549
 550	if (status & BAT_S1_LiION_OR_NiMH)
 551		return POWER_SUPPLY_TECHNOLOGY_LION;
 552	return POWER_SUPPLY_TECHNOLOGY_NiMH;
 553}
 554
 555static int bat_capacity_level(void)
 556{
 557	u8 status0 = ec_read_u8(BAT_STATUS0);
 558	u8 status1 = ec_read_u8(BAT_STATUS1);
 559	u8 status2 = ec_read_u8(BAT_STATUS2);
 560
 561	if (status0 & BAT_S0_DISCHRG_CRITICAL
 562			|| status1 & BAT_S1_EMPTY
 563			|| status2 & BAT_S2_LOW_LOW)
 564		return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
 565	if (status0 & BAT_S0_LOW)
 566		return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
 567	if (status1 & BAT_S1_FULL)
 568		return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
 569	return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
 570}
 571
 572static int bat_get_property(struct power_supply *psy,
 573				enum power_supply_property psp,
 574				union power_supply_propval *val)
 575{
 576	struct compal_data *data;
 577	data = container_of(psy, struct compal_data, psy);
 578
 579	switch (psp) {
 580	case POWER_SUPPLY_PROP_STATUS:
 581		val->intval = bat_status();
 582		break;
 583	case POWER_SUPPLY_PROP_HEALTH:
 584		val->intval = bat_health();
 585		break;
 586	case POWER_SUPPLY_PROP_PRESENT:
 587		val->intval = bat_is_present();
 588		break;
 589	case POWER_SUPPLY_PROP_TECHNOLOGY:
 590		val->intval = bat_technology();
 591		break;
 592	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: /* THE design voltage... */
 593		val->intval = ec_read_u16(BAT_VOLTAGE_DESIGN) * 1000;
 594		break;
 595	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 596		val->intval = ec_read_u16(BAT_VOLTAGE_NOW) * 1000;
 597		break;
 598	case POWER_SUPPLY_PROP_CURRENT_NOW:
 599		val->intval = ec_read_s16(BAT_CURRENT_NOW) * 1000;
 600		break;
 601	case POWER_SUPPLY_PROP_CURRENT_AVG:
 602		val->intval = ec_read_s16(BAT_CURRENT_AVG) * 1000;
 603		break;
 604	case POWER_SUPPLY_PROP_POWER_NOW:
 605		val->intval = ec_read_u8(BAT_POWER) * 1000000;
 606		break;
 607	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
 608		val->intval = ec_read_u16(BAT_CHARGE_DESIGN) * 1000;
 609		break;
 610	case POWER_SUPPLY_PROP_CHARGE_NOW:
 611		val->intval = ec_read_u16(BAT_CHARGE_NOW) * 1000;
 612		break;
 613	case POWER_SUPPLY_PROP_CAPACITY:
 614		val->intval = ec_read_u8(BAT_CAPACITY);
 615		break;
 616	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
 617		val->intval = bat_capacity_level();
 618		break;
 619	/* It smees that BAT_TEMP_AVG is a (2's complement?) value showing
 620	 * the number of degrees, whereas BAT_TEMP is somewhat more
 621	 * complicated. It looks like this is a negative nember with a
 622	 * 100/256 divider and an offset of 222. Both were determined
 623	 * experimentally by comparing BAT_TEMP and BAT_TEMP_AVG. */
 624	case POWER_SUPPLY_PROP_TEMP:
 625		val->intval = ((222 - (int)ec_read_u8(BAT_TEMP)) * 1000) >> 8;
 626		break;
 627	case POWER_SUPPLY_PROP_TEMP_AMBIENT: /* Ambient, Avg, ... same thing */
 628		val->intval = ec_read_s8(BAT_TEMP_AVG) * 10;
 629		break;
 630	/* Neither the model name nor manufacturer name work for me. */
 631	case POWER_SUPPLY_PROP_MODEL_NAME:
 632		val->strval = data->bat_model_name;
 633		break;
 634	case POWER_SUPPLY_PROP_MANUFACTURER:
 635		val->strval = data->bat_manufacturer_name;
 636		break;
 637	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
 638		val->strval = data->bat_serial_number;
 639		break;
 640	default:
 641		break;
 642	}
 643	return 0;
 644}
 645
 646
 647
 648
 649
 650/* ============== */
 651/* Driver Globals */
 652/* ============== */
 653static DEVICE_ATTR(wake_up_pme,
 654		0644, wake_up_pme_show,		wake_up_pme_store);
 655static DEVICE_ATTR(wake_up_modem,
 656		0644, wake_up_modem_show,	wake_up_modem_store);
 657static DEVICE_ATTR(wake_up_lan,
 658		0644, wake_up_lan_show,	wake_up_lan_store);
 659static DEVICE_ATTR(wake_up_wlan,
 660		0644, wake_up_wlan_show,	wake_up_wlan_store);
 661static DEVICE_ATTR(wake_up_key,
 662		0644, wake_up_key_show,	wake_up_key_store);
 663static DEVICE_ATTR(wake_up_mouse,
 664		0644, wake_up_mouse_show,	wake_up_mouse_store);
 665
 666static SENSOR_DEVICE_ATTR(name,        S_IRUGO, hwmon_name_show,   NULL, 1);
 667static SENSOR_DEVICE_ATTR(fan1_input,  S_IRUGO, fan_show,          NULL, 1);
 668static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, temp_cpu,          NULL, 1);
 669static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, temp_cpu_local,    NULL, 1);
 670static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, temp_cpu_DTS,      NULL, 1);
 671static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, temp_northbridge,  NULL, 1);
 672static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, temp_vga,          NULL, 1);
 673static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, temp_SKIN,         NULL, 1);
 674static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, label_cpu,         NULL, 1);
 675static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, label_cpu_local,   NULL, 1);
 676static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, label_cpu_DTS,     NULL, 1);
 677static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, label_northbridge, NULL, 1);
 678static SENSOR_DEVICE_ATTR(temp5_label, S_IRUGO, label_vga,         NULL, 1);
 679static SENSOR_DEVICE_ATTR(temp6_label, S_IRUGO, label_SKIN,        NULL, 1);
 680static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, pwm_show, pwm_store, 1);
 681static SENSOR_DEVICE_ATTR(pwm1_enable,
 682		S_IRUGO | S_IWUSR, pwm_enable_show, pwm_enable_store, 0);
 683
 684static struct attribute *compal_attributes[] = {
 685	&dev_attr_wake_up_pme.attr,
 686	&dev_attr_wake_up_modem.attr,
 687	&dev_attr_wake_up_lan.attr,
 688	&dev_attr_wake_up_wlan.attr,
 689	&dev_attr_wake_up_key.attr,
 690	&dev_attr_wake_up_mouse.attr,
 691	/* Maybe put the sensor-stuff in a separate hwmon-driver? That way,
 692	 * the hwmon sysfs won't be cluttered with the above files. */
 693	&sensor_dev_attr_name.dev_attr.attr,
 694	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
 695	&sensor_dev_attr_pwm1.dev_attr.attr,
 696	&sensor_dev_attr_fan1_input.dev_attr.attr,
 697	&sensor_dev_attr_temp1_input.dev_attr.attr,
 698	&sensor_dev_attr_temp2_input.dev_attr.attr,
 699	&sensor_dev_attr_temp3_input.dev_attr.attr,
 700	&sensor_dev_attr_temp4_input.dev_attr.attr,
 701	&sensor_dev_attr_temp5_input.dev_attr.attr,
 702	&sensor_dev_attr_temp6_input.dev_attr.attr,
 703	&sensor_dev_attr_temp1_label.dev_attr.attr,
 704	&sensor_dev_attr_temp2_label.dev_attr.attr,
 705	&sensor_dev_attr_temp3_label.dev_attr.attr,
 706	&sensor_dev_attr_temp4_label.dev_attr.attr,
 707	&sensor_dev_attr_temp5_label.dev_attr.attr,
 708	&sensor_dev_attr_temp6_label.dev_attr.attr,
 709	NULL
 710};
 
 
 
 711
 712static struct attribute_group compal_attribute_group = {
 713	.attrs = compal_attributes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 714};
 
 715
 716static int __devinit compal_probe(struct platform_device *);
 717static int __devexit compal_remove(struct platform_device *);
 718static struct platform_driver compal_driver = {
 719	.driver = {
 720		.name = DRIVER_NAME,
 721		.owner = THIS_MODULE,
 722	},
 723	.probe	= compal_probe,
 724	.remove	= __devexit_p(compal_remove)
 725};
 726
 727static enum power_supply_property compal_bat_properties[] = {
 728	POWER_SUPPLY_PROP_STATUS,
 729	POWER_SUPPLY_PROP_HEALTH,
 730	POWER_SUPPLY_PROP_PRESENT,
 731	POWER_SUPPLY_PROP_TECHNOLOGY,
 732	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
 733	POWER_SUPPLY_PROP_VOLTAGE_NOW,
 734	POWER_SUPPLY_PROP_CURRENT_NOW,
 735	POWER_SUPPLY_PROP_CURRENT_AVG,
 736	POWER_SUPPLY_PROP_POWER_NOW,
 737	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
 738	POWER_SUPPLY_PROP_CHARGE_NOW,
 739	POWER_SUPPLY_PROP_CAPACITY,
 740	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
 741	POWER_SUPPLY_PROP_TEMP,
 742	POWER_SUPPLY_PROP_TEMP_AMBIENT,
 743	POWER_SUPPLY_PROP_MODEL_NAME,
 744	POWER_SUPPLY_PROP_MANUFACTURER,
 745	POWER_SUPPLY_PROP_SERIAL_NUMBER,
 746};
 747
 748static struct backlight_device *compalbl_device;
 749
 750static struct platform_device *compal_device;
 751
 752static struct rfkill *wifi_rfkill;
 753static struct rfkill *bt_rfkill;
 754
 755
 756
 757
 758
 759/* =================================== */
 760/* Initialization & clean-up functions */
 761/* =================================== */
 762
 763static int dmi_check_cb(const struct dmi_system_id *id)
 764{
 765	pr_info("Identified laptop model '%s'\n", id->ident);
 766	extra_features = false;
 767	return 1;
 768}
 769
 770static int dmi_check_cb_extra(const struct dmi_system_id *id)
 771{
 772	pr_info("Identified laptop model '%s', enabling extra features\n",
 773		id->ident);
 774	extra_features = true;
 775	return 1;
 776}
 777
 778static struct dmi_system_id __initdata compal_dmi_table[] = {
 779	{
 780		.ident = "FL90/IFL90",
 781		.matches = {
 782			DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
 783			DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
 784		},
 785		.callback = dmi_check_cb
 786	},
 787	{
 788		.ident = "FL90/IFL90",
 789		.matches = {
 790			DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
 791			DMI_MATCH(DMI_BOARD_VERSION, "REFERENCE"),
 792		},
 793		.callback = dmi_check_cb
 794	},
 795	{
 796		.ident = "FL91/IFL91",
 797		.matches = {
 798			DMI_MATCH(DMI_BOARD_NAME, "IFL91"),
 799			DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
 800		},
 801		.callback = dmi_check_cb
 802	},
 803	{
 804		.ident = "FL92/JFL92",
 805		.matches = {
 806			DMI_MATCH(DMI_BOARD_NAME, "JFL92"),
 807			DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
 808		},
 809		.callback = dmi_check_cb
 810	},
 811	{
 812		.ident = "FT00/IFT00",
 813		.matches = {
 814			DMI_MATCH(DMI_BOARD_NAME, "IFT00"),
 815			DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
 816		},
 817		.callback = dmi_check_cb
 818	},
 819	{
 820		.ident = "Dell Mini 9",
 821		.matches = {
 822			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 823			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 910"),
 824		},
 825		.callback = dmi_check_cb
 826	},
 827	{
 828		.ident = "Dell Mini 10",
 829		.matches = {
 830			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 831			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1010"),
 832		},
 833		.callback = dmi_check_cb
 834	},
 835	{
 836		.ident = "Dell Mini 10v",
 837		.matches = {
 838			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 839			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1011"),
 840		},
 841		.callback = dmi_check_cb
 842	},
 843	{
 844		.ident = "Dell Mini 1012",
 845		.matches = {
 846			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 847			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
 848		},
 849		.callback = dmi_check_cb
 850	},
 851	{
 852		.ident = "Dell Inspiron 11z",
 853		.matches = {
 854			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 855			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1110"),
 856		},
 857		.callback = dmi_check_cb
 858	},
 859	{
 860		.ident = "Dell Mini 12",
 861		.matches = {
 862			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 863			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1210"),
 864		},
 865		.callback = dmi_check_cb
 866	},
 867	{
 868		.ident = "JHL90",
 869		.matches = {
 870			DMI_MATCH(DMI_BOARD_NAME, "JHL90"),
 871			DMI_MATCH(DMI_BOARD_VERSION, "REFERENCE"),
 872		},
 873		.callback = dmi_check_cb_extra
 874	},
 875	{
 876		.ident = "KHLB2",
 877		.matches = {
 878			DMI_MATCH(DMI_BOARD_NAME, "KHLB2"),
 879			DMI_MATCH(DMI_BOARD_VERSION, "REFERENCE"),
 880		},
 881		.callback = dmi_check_cb_extra
 882	},
 883	{ }
 884};
 
 885
 886static void initialize_power_supply_data(struct compal_data *data)
 887{
 888	data->psy.name = DRIVER_NAME;
 889	data->psy.type = POWER_SUPPLY_TYPE_BATTERY;
 890	data->psy.properties = compal_bat_properties;
 891	data->psy.num_properties = ARRAY_SIZE(compal_bat_properties);
 892	data->psy.get_property = bat_get_property;
 893
 894	ec_read_sequence(BAT_MANUFACTURER_NAME_ADDR,
 895					data->bat_manufacturer_name,
 896					BAT_MANUFACTURER_NAME_LEN);
 897	data->bat_manufacturer_name[BAT_MANUFACTURER_NAME_LEN] = 0;
 898
 899	ec_read_sequence(BAT_MODEL_NAME_ADDR,
 900					data->bat_model_name,
 901					BAT_MODEL_NAME_LEN);
 902	data->bat_model_name[BAT_MODEL_NAME_LEN] = 0;
 903
 904	scnprintf(data->bat_serial_number, BAT_SERIAL_NUMBER_LEN + 1, "%d",
 905				ec_read_u16(BAT_SERIAL_NUMBER_ADDR));
 906}
 907
 908static void initialize_fan_control_data(struct compal_data *data)
 909{
 910	data->pwm_enable = 2; /* Keep motherboard in control for now */
 911	data->curr_pwm = 255; /* Try not to cause a CPU_on_fire exception
 912				 if we take over... */
 913}
 914
 915static int setup_rfkill(void)
 916{
 917	int ret;
 918
 919	wifi_rfkill = rfkill_alloc("compal-wifi", &compal_device->dev,
 920				RFKILL_TYPE_WLAN, &compal_rfkill_ops,
 921				(void *) WIRELESS_WLAN);
 922	if (!wifi_rfkill)
 923		return -ENOMEM;
 924
 925	ret = rfkill_register(wifi_rfkill);
 926	if (ret)
 927		goto err_wifi;
 928
 929	bt_rfkill = rfkill_alloc("compal-bluetooth", &compal_device->dev,
 930				RFKILL_TYPE_BLUETOOTH, &compal_rfkill_ops,
 931				(void *) WIRELESS_BT);
 932	if (!bt_rfkill) {
 933		ret = -ENOMEM;
 934		goto err_allocate_bt;
 935	}
 936	ret = rfkill_register(bt_rfkill);
 937	if (ret)
 938		goto err_register_bt;
 939
 940	return 0;
 941
 942err_register_bt:
 943	rfkill_destroy(bt_rfkill);
 944
 945err_allocate_bt:
 946	rfkill_unregister(wifi_rfkill);
 947
 948err_wifi:
 949	rfkill_destroy(wifi_rfkill);
 950
 951	return ret;
 952}
 953
 954static int __init compal_init(void)
 955{
 956	int ret;
 957
 958	if (acpi_disabled) {
 959		pr_err("ACPI needs to be enabled for this driver to work!\n");
 960		return -ENODEV;
 961	}
 962
 963	if (!force && !dmi_check_system(compal_dmi_table)) {
 964		pr_err("Motherboard not recognized (You could try the module's force-parameter)\n");
 965		return -ENODEV;
 966	}
 967
 968	if (!acpi_video_backlight_support()) {
 969		struct backlight_properties props;
 970		memset(&props, 0, sizeof(struct backlight_properties));
 971		props.type = BACKLIGHT_PLATFORM;
 972		props.max_brightness = BACKLIGHT_LEVEL_MAX;
 973		compalbl_device = backlight_device_register(DRIVER_NAME,
 974							    NULL, NULL,
 975							    &compalbl_ops,
 976							    &props);
 977		if (IS_ERR(compalbl_device))
 978			return PTR_ERR(compalbl_device);
 979	}
 980
 981	ret = platform_driver_register(&compal_driver);
 982	if (ret)
 983		goto err_backlight;
 984
 985	compal_device = platform_device_alloc(DRIVER_NAME, -1);
 986	if (!compal_device) {
 987		ret = -ENOMEM;
 988		goto err_platform_driver;
 989	}
 990
 991	ret = platform_device_add(compal_device); /* This calls compal_probe */
 992	if (ret)
 993		goto err_platform_device;
 994
 995	ret = setup_rfkill();
 996	if (ret)
 997		goto err_rfkill;
 998
 999	pr_info("Driver " DRIVER_VERSION " successfully loaded\n");
1000	return 0;
1001
1002err_rfkill:
1003	platform_device_del(compal_device);
1004
1005err_platform_device:
1006	platform_device_put(compal_device);
1007
1008err_platform_driver:
1009	platform_driver_unregister(&compal_driver);
1010
1011err_backlight:
1012	backlight_device_unregister(compalbl_device);
1013
1014	return ret;
1015}
1016
1017static int __devinit compal_probe(struct platform_device *pdev)
1018{
1019	int err;
1020	struct compal_data *data;
 
1021
1022	if (!extra_features)
1023		return 0;
1024
1025	/* Fan control */
1026	data = kzalloc(sizeof(struct compal_data), GFP_KERNEL);
1027	if (!data)
1028		return -ENOMEM;
1029
1030	initialize_fan_control_data(data);
1031
1032	err = sysfs_create_group(&pdev->dev.kobj, &compal_attribute_group);
1033	if (err) {
1034		kfree(data);
1035		return err;
1036	}
1037
1038	data->hwmon_dev = hwmon_device_register(&pdev->dev);
1039	if (IS_ERR(data->hwmon_dev)) {
1040		err = PTR_ERR(data->hwmon_dev);
1041		sysfs_remove_group(&pdev->dev.kobj,
1042				&compal_attribute_group);
1043		kfree(data);
1044		return err;
1045	}
1046
1047	/* Power supply */
1048	initialize_power_supply_data(data);
1049	power_supply_register(&compal_device->dev, &data->psy);
1050
1051	platform_set_drvdata(pdev, data);
1052
1053	return 0;
 
 
 
 
1054}
1055
1056static void __exit compal_cleanup(void)
1057{
1058	platform_device_unregister(compal_device);
1059	platform_driver_unregister(&compal_driver);
1060	backlight_device_unregister(compalbl_device);
1061	rfkill_unregister(wifi_rfkill);
1062	rfkill_unregister(bt_rfkill);
1063	rfkill_destroy(wifi_rfkill);
1064	rfkill_destroy(bt_rfkill);
1065
1066	pr_info("Driver unloaded\n");
1067}
1068
1069static int __devexit compal_remove(struct platform_device *pdev)
1070{
1071	struct compal_data *data;
1072
1073	if (!extra_features)
1074		return 0;
1075
1076	pr_info("Unloading: resetting fan control to motherboard\n");
1077	pwm_disable_control();
1078
1079	data = platform_get_drvdata(pdev);
1080	hwmon_device_unregister(data->hwmon_dev);
1081	power_supply_unregister(&data->psy);
1082
1083	platform_set_drvdata(pdev, NULL);
1084	kfree(data);
1085
1086	sysfs_remove_group(&pdev->dev.kobj, &compal_attribute_group);
1087
1088	return 0;
1089}
1090
1091
1092module_init(compal_init);
1093module_exit(compal_cleanup);
1094
1095MODULE_AUTHOR("Cezary Jackiewicz");
1096MODULE_AUTHOR("Roald Frederickx (roald.frederickx@gmail.com)");
1097MODULE_DESCRIPTION("Compal Laptop Support");
1098MODULE_VERSION(DRIVER_VERSION);
1099MODULE_LICENSE("GPL");
1100
1101MODULE_ALIAS("dmi:*:rnIFL90:rvrIFT00:*");
1102MODULE_ALIAS("dmi:*:rnIFL90:rvrREFERENCE:*");
1103MODULE_ALIAS("dmi:*:rnIFL91:rvrIFT00:*");
1104MODULE_ALIAS("dmi:*:rnJFL92:rvrIFT00:*");
1105MODULE_ALIAS("dmi:*:rnIFT00:rvrIFT00:*");
1106MODULE_ALIAS("dmi:*:rnJHL90:rvrREFERENCE:*");
1107MODULE_ALIAS("dmi:*:svnDellInc.:pnInspiron910:*");
1108MODULE_ALIAS("dmi:*:svnDellInc.:pnInspiron1010:*");
1109MODULE_ALIAS("dmi:*:svnDellInc.:pnInspiron1011:*");
1110MODULE_ALIAS("dmi:*:svnDellInc.:pnInspiron1012:*");
1111MODULE_ALIAS("dmi:*:svnDellInc.:pnInspiron1110:*");
1112MODULE_ALIAS("dmi:*:svnDellInc.:pnInspiron1210:*");
v3.15
   1/*-*-linux-c-*-*/
   2
   3/*
   4  Copyright (C) 2008 Cezary Jackiewicz <cezary.jackiewicz (at) gmail.com>
   5
   6  based on MSI driver
   7
   8  Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
   9
  10  This program is free software; you can redistribute it and/or modify
  11  it under the terms of the GNU General Public License as published by
  12  the Free Software Foundation; either version 2 of the License, or
  13  (at your option) any later version.
  14
  15  This program is distributed in the hope that it will be useful, but
  16  WITHOUT ANY WARRANTY; without even the implied warranty of
  17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
  18  General Public License for more details.
  19
  20  You should have received a copy of the GNU General Public License
  21  along with this program; if not, write to the Free Software
  22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  23  02110-1301, USA.
  24 */
  25
  26/*
  27 * compal-laptop.c - Compal laptop support.
  28 *
  29 * This driver exports a few files in /sys/devices/platform/compal-laptop/:
  30 *   wake_up_XXX   Whether or not we listen to such wake up events (rw)
  31 *
  32 * In addition to these platform device attributes the driver
  33 * registers itself in the Linux backlight control, power_supply, rfkill
  34 * and hwmon subsystem and is available to userspace under:
  35 *
  36 *   /sys/class/backlight/compal-laptop/
  37 *   /sys/class/power_supply/compal-laptop/
  38 *   /sys/class/rfkill/rfkillX/
  39 *   /sys/class/hwmon/hwmonX/
  40 *
  41 * Notes on the power_supply battery interface:
  42 *   - the "minimum" design voltage is *the* design voltage
  43 *   - the ambient temperature is the average battery temperature
  44 *     and the value is an educated guess (see commented code below)
  45 *
  46 *
  47 * This driver might work on other laptops produced by Compal. If you
  48 * want to try it you can pass force=1 as argument to the module which
  49 * will force it to load even when the DMI data doesn't identify the
  50 * laptop as compatible.
  51 *
  52 * Lots of data available at:
  53 * http://service1.marasst.com/Compal/JHL90_91/Service%20Manual/
  54 * JHL90%20service%20manual-Final-0725.pdf
  55 *
  56 *
  57 *
  58 * Support for the Compal JHL90 added by Roald Frederickx
  59 * (roald.frederickx@gmail.com):
  60 * Driver got large revision. Added functionalities: backlight
  61 * power, wake_on_XXX, a hwmon and power_supply interface.
  62 *
  63 * In case this gets merged into the kernel source: I want to dedicate this
  64 * to Kasper Meerts, the awesome guy who showed me Linux and C!
  65 */
  66
  67/* NOTE: currently the wake_on_XXX, hwmon and power_supply interfaces are
  68 * only enabled on a JHL90 board until it is verified that they work on the
  69 * other boards too.  See the extra_features variable. */
  70
  71#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  72
  73#include <linux/module.h>
  74#include <linux/kernel.h>
  75#include <linux/init.h>
  76#include <linux/acpi.h>
  77#include <linux/dmi.h>
  78#include <linux/backlight.h>
  79#include <linux/platform_device.h>
  80#include <linux/rfkill.h>
  81#include <linux/hwmon.h>
  82#include <linux/hwmon-sysfs.h>
  83#include <linux/power_supply.h>
  84#include <linux/fb.h>
  85
  86
  87/* ======= */
  88/* Defines */
  89/* ======= */
  90#define DRIVER_NAME "compal-laptop"
  91#define DRIVER_VERSION	"0.2.7"
  92
  93#define BACKLIGHT_LEVEL_ADDR		0xB9
  94#define BACKLIGHT_LEVEL_MAX		7
  95#define BACKLIGHT_STATE_ADDR		0x59
  96#define BACKLIGHT_STATE_ON_DATA		0xE1
  97#define BACKLIGHT_STATE_OFF_DATA	0xE2
  98
  99#define WAKE_UP_ADDR			0xA4
 100#define WAKE_UP_PME			(1 << 0)
 101#define WAKE_UP_MODEM			(1 << 1)
 102#define WAKE_UP_LAN			(1 << 2)
 103#define WAKE_UP_WLAN			(1 << 4)
 104#define WAKE_UP_KEY			(1 << 6)
 105#define WAKE_UP_MOUSE			(1 << 7)
 106
 107#define WIRELESS_ADDR			0xBB
 108#define WIRELESS_WLAN			(1 << 0)
 109#define WIRELESS_BT			(1 << 1)
 110#define WIRELESS_WLAN_EXISTS		(1 << 2)
 111#define WIRELESS_BT_EXISTS		(1 << 3)
 112#define WIRELESS_KILLSWITCH		(1 << 4)
 113
 114#define PWM_ADDRESS			0x46
 115#define PWM_DISABLE_ADDR		0x59
 116#define PWM_DISABLE_DATA		0xA5
 117#define PWM_ENABLE_ADDR			0x59
 118#define PWM_ENABLE_DATA			0xA8
 119
 120#define FAN_ADDRESS			0x46
 121#define FAN_DATA			0x81
 122#define FAN_FULL_ON_CMD			0x59 /* Doesn't seem to work. Just */
 123#define FAN_FULL_ON_ENABLE		0x76 /* force the pwm signal to its */
 124#define FAN_FULL_ON_DISABLE		0x77 /* maximum value instead */
 125
 126#define TEMP_CPU			0xB0
 127#define TEMP_CPU_LOCAL			0xB1
 128#define TEMP_CPU_DTS			0xB5
 129#define TEMP_NORTHBRIDGE		0xB6
 130#define TEMP_VGA			0xB4
 131#define TEMP_SKIN			0xB2
 132
 133#define BAT_MANUFACTURER_NAME_ADDR	0x10
 134#define BAT_MANUFACTURER_NAME_LEN	9
 135#define BAT_MODEL_NAME_ADDR		0x19
 136#define BAT_MODEL_NAME_LEN		6
 137#define BAT_SERIAL_NUMBER_ADDR		0xC4
 138#define BAT_SERIAL_NUMBER_LEN		5
 139#define BAT_CHARGE_NOW			0xC2
 140#define BAT_CHARGE_DESIGN		0xCA
 141#define BAT_VOLTAGE_NOW			0xC6
 142#define BAT_VOLTAGE_DESIGN		0xC8
 143#define BAT_CURRENT_NOW			0xD0
 144#define BAT_CURRENT_AVG			0xD2
 145#define BAT_POWER			0xD4
 146#define BAT_CAPACITY			0xCE
 147#define BAT_TEMP			0xD6
 148#define BAT_TEMP_AVG			0xD7
 149#define BAT_STATUS0			0xC1
 150#define BAT_STATUS1			0xF0
 151#define BAT_STATUS2			0xF1
 152#define BAT_STOP_CHARGE1		0xF2
 153#define BAT_STOP_CHARGE2		0xF3
 154
 155#define BAT_S0_DISCHARGE		(1 << 0)
 156#define BAT_S0_DISCHRG_CRITICAL		(1 << 2)
 157#define BAT_S0_LOW			(1 << 3)
 158#define BAT_S0_CHARGING			(1 << 1)
 159#define BAT_S0_AC			(1 << 7)
 160#define BAT_S1_EXISTS			(1 << 0)
 161#define BAT_S1_FULL			(1 << 1)
 162#define BAT_S1_EMPTY			(1 << 2)
 163#define BAT_S1_LiION_OR_NiMH		(1 << 7)
 164#define BAT_S2_LOW_LOW			(1 << 0)
 165#define BAT_STOP_CHRG1_BAD_CELL		(1 << 1)
 166#define BAT_STOP_CHRG1_COMM_FAIL	(1 << 2)
 167#define BAT_STOP_CHRG1_OVERVOLTAGE	(1 << 6)
 168#define BAT_STOP_CHRG1_OVERTEMPERATURE	(1 << 7)
 169
 170
 171/* ======= */
 172/* Structs */
 173/* ======= */
 174struct compal_data{
 175	/* Fan control */
 176	int pwm_enable; /* 0:full on, 1:set by pwm1, 2:control by motherboard */
 
 177	unsigned char curr_pwm;
 178
 179	/* Power supply */
 180	struct power_supply psy;
 181	struct power_supply_info psy_info;
 182	char bat_model_name[BAT_MODEL_NAME_LEN + 1];
 183	char bat_manufacturer_name[BAT_MANUFACTURER_NAME_LEN + 1];
 184	char bat_serial_number[BAT_SERIAL_NUMBER_LEN + 1];
 185};
 186
 187
 188/* =============== */
 189/* General globals */
 190/* =============== */
 191static bool force;
 192module_param(force, bool, 0);
 193MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
 194
 195/* Support for the wake_on_XXX, hwmon and power_supply interface. Currently
 196 * only gets enabled on a JHL90 board. Might work with the others too */
 197static bool extra_features;
 198
 199/* Nasty stuff. For some reason the fan control is very un-linear.  I've
 200 * come up with these values by looping through the possible inputs and
 201 * watching the output of address 0x4F (do an ec_transaction writing 0x33
 202 * into 0x4F and read a few bytes from the output, like so:
 203 *	u8 writeData = 0x33;
 204 *	ec_transaction(0x4F, &writeData, 1, buffer, 32);
 205 * That address is labeled "fan1 table information" in the service manual.
 206 * It should be clear which value in 'buffer' changes). This seems to be
 207 * related to fan speed. It isn't a proper 'realtime' fan speed value
 208 * though, because physically stopping or speeding up the fan doesn't
 209 * change it. It might be the average voltage or current of the pwm output.
 210 * Nevertheless, it is more fine-grained than the actual RPM reading */
 211static const unsigned char pwm_lookup_table[256] = {
 212	0, 0, 0, 1, 1, 1, 2, 253, 254, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6,
 213	7, 7, 7, 8, 86, 86, 9, 9, 9, 10, 10, 10, 11, 92, 92, 12, 12, 95,
 214	13, 66, 66, 14, 14, 98, 15, 15, 15, 16, 16, 67, 17, 17, 72, 18, 70,
 215	75, 19, 90, 90, 73, 73, 73, 21, 21, 91, 91, 91, 96, 23, 94, 94, 94,
 216	94, 94, 94, 94, 94, 94, 94, 141, 141, 238, 223, 192, 139, 139, 139,
 217	139, 139, 142, 142, 142, 142, 142, 78, 78, 78, 78, 78, 76, 76, 76,
 218	76, 76, 79, 79, 79, 79, 79, 79, 79, 20, 20, 20, 20, 20, 22, 22, 22,
 219	22, 22, 24, 24, 24, 24, 24, 24, 219, 219, 219, 219, 219, 219, 219,
 220	219, 27, 27, 188, 188, 28, 28, 28, 29, 186, 186, 186, 186, 186,
 221	186, 186, 186, 186, 186, 31, 31, 31, 31, 31, 32, 32, 32, 41, 33,
 222	33, 33, 33, 33, 252, 252, 34, 34, 34, 43, 35, 35, 35, 36, 36, 38,
 223	206, 206, 206, 206, 206, 206, 206, 206, 206, 37, 37, 37, 46, 46,
 224	47, 47, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 48, 48,
 225	48, 48, 48, 40, 40, 40, 49, 42, 42, 42, 42, 42, 42, 42, 42, 44,
 226	189, 189, 189, 189, 54, 54, 45, 45, 45, 45, 45, 45, 45, 45, 251,
 227	191, 199, 199, 199, 199, 199, 215, 215, 215, 215, 187, 187, 187,
 228	187, 187, 193, 50
 229};
 230
 231
 232
 233
 234/* ========================= */
 235/* Hardware access functions */
 236/* ========================= */
 237/* General access */
 238static u8 ec_read_u8(u8 addr)
 239{
 240	u8 value;
 241	ec_read(addr, &value);
 242	return value;
 243}
 244
 245static s8 ec_read_s8(u8 addr)
 246{
 247	return (s8)ec_read_u8(addr);
 248}
 249
 250static u16 ec_read_u16(u8 addr)
 251{
 252	int hi, lo;
 253	lo = ec_read_u8(addr);
 254	hi = ec_read_u8(addr + 1);
 255	return (hi << 8) + lo;
 256}
 257
 258static s16 ec_read_s16(u8 addr)
 259{
 260	return (s16) ec_read_u16(addr);
 261}
 262
 263static void ec_read_sequence(u8 addr, u8 *buf, int len)
 264{
 265	int i;
 266	for (i = 0; i < len; i++)
 267		ec_read(addr + i, buf + i);
 268}
 269
 270
 271/* Backlight access */
 272static int set_backlight_level(int level)
 273{
 274	if (level < 0 || level > BACKLIGHT_LEVEL_MAX)
 275		return -EINVAL;
 276
 277	ec_write(BACKLIGHT_LEVEL_ADDR, level);
 278
 279	return 0;
 280}
 281
 282static int get_backlight_level(void)
 283{
 284	return (int) ec_read_u8(BACKLIGHT_LEVEL_ADDR);
 285}
 286
 287static void set_backlight_state(bool on)
 288{
 289	u8 data = on ? BACKLIGHT_STATE_ON_DATA : BACKLIGHT_STATE_OFF_DATA;
 290	ec_transaction(BACKLIGHT_STATE_ADDR, &data, 1, NULL, 0);
 291}
 292
 293
 294/* Fan control access */
 295static void pwm_enable_control(void)
 296{
 297	unsigned char writeData = PWM_ENABLE_DATA;
 298	ec_transaction(PWM_ENABLE_ADDR, &writeData, 1, NULL, 0);
 299}
 300
 301static void pwm_disable_control(void)
 302{
 303	unsigned char writeData = PWM_DISABLE_DATA;
 304	ec_transaction(PWM_DISABLE_ADDR, &writeData, 1, NULL, 0);
 305}
 306
 307static void set_pwm(int pwm)
 308{
 309	ec_transaction(PWM_ADDRESS, &pwm_lookup_table[pwm], 1, NULL, 0);
 310}
 311
 312static int get_fan_rpm(void)
 313{
 314	u8 value, data = FAN_DATA;
 315	ec_transaction(FAN_ADDRESS, &data, 1, &value, 1);
 316	return 100 * (int)value;
 317}
 318
 319
 320
 321
 322/* =================== */
 323/* Interface functions */
 324/* =================== */
 325
 326/* Backlight interface */
 327static int bl_get_brightness(struct backlight_device *b)
 328{
 329	return get_backlight_level();
 330}
 331
 332static int bl_update_status(struct backlight_device *b)
 333{
 334	int ret = set_backlight_level(b->props.brightness);
 335	if (ret)
 336		return ret;
 337
 338	set_backlight_state((b->props.power == FB_BLANK_UNBLANK)
 339		&&    !(b->props.state & BL_CORE_SUSPENDED)
 340		&&    !(b->props.state & BL_CORE_FBBLANK));
 341	return 0;
 342}
 343
 344static const struct backlight_ops compalbl_ops = {
 345	.get_brightness = bl_get_brightness,
 346	.update_status	= bl_update_status,
 347};
 348
 349
 350/* Wireless interface */
 351static int compal_rfkill_set(void *data, bool blocked)
 352{
 353	unsigned long radio = (unsigned long) data;
 354	u8 result = ec_read_u8(WIRELESS_ADDR);
 355	u8 value;
 356
 357	if (!blocked)
 358		value = (u8) (result | radio);
 359	else
 360		value = (u8) (result & ~radio);
 361	ec_write(WIRELESS_ADDR, value);
 362
 363	return 0;
 364}
 365
 366static void compal_rfkill_poll(struct rfkill *rfkill, void *data)
 367{
 368	u8 result = ec_read_u8(WIRELESS_ADDR);
 369	bool hw_blocked = !(result & WIRELESS_KILLSWITCH);
 370	rfkill_set_hw_state(rfkill, hw_blocked);
 371}
 372
 373static const struct rfkill_ops compal_rfkill_ops = {
 374	.poll = compal_rfkill_poll,
 375	.set_block = compal_rfkill_set,
 376};
 377
 378
 379/* Wake_up interface */
 380#define SIMPLE_MASKED_STORE_SHOW(NAME, ADDR, MASK)			\
 381static ssize_t NAME##_show(struct device *dev,				\
 382	struct device_attribute *attr, char *buf)			\
 383{									\
 384	return sprintf(buf, "%d\n", ((ec_read_u8(ADDR) & MASK) != 0));	\
 385}									\
 386static ssize_t NAME##_store(struct device *dev,				\
 387	struct device_attribute *attr, const char *buf, size_t count)	\
 388{									\
 389	int state;							\
 390	u8 old_val = ec_read_u8(ADDR);					\
 391	if (sscanf(buf, "%d", &state) != 1 || (state < 0 || state > 1))	\
 392		return -EINVAL;						\
 393	ec_write(ADDR, state ? (old_val | MASK) : (old_val & ~MASK));	\
 394	return count;							\
 395}
 396
 397SIMPLE_MASKED_STORE_SHOW(wake_up_pme,	WAKE_UP_ADDR, WAKE_UP_PME)
 398SIMPLE_MASKED_STORE_SHOW(wake_up_modem,	WAKE_UP_ADDR, WAKE_UP_MODEM)
 399SIMPLE_MASKED_STORE_SHOW(wake_up_lan,	WAKE_UP_ADDR, WAKE_UP_LAN)
 400SIMPLE_MASKED_STORE_SHOW(wake_up_wlan,	WAKE_UP_ADDR, WAKE_UP_WLAN)
 401SIMPLE_MASKED_STORE_SHOW(wake_up_key,	WAKE_UP_ADDR, WAKE_UP_KEY)
 402SIMPLE_MASKED_STORE_SHOW(wake_up_mouse,	WAKE_UP_ADDR, WAKE_UP_MOUSE)
 403
 
 
 
 
 
 
 
 
 
 404/* Fan control interface */
 405static ssize_t pwm_enable_show(struct device *dev,
 406		struct device_attribute *attr, char *buf)
 407{
 408	struct compal_data *data = dev_get_drvdata(dev);
 409	return sprintf(buf, "%d\n", data->pwm_enable);
 410}
 411
 412static ssize_t pwm_enable_store(struct device *dev,
 413		struct device_attribute *attr, const char *buf, size_t count)
 414{
 415	struct compal_data *data = dev_get_drvdata(dev);
 416	long val;
 417	int err;
 418
 419	err = kstrtol(buf, 10, &val);
 420	if (err)
 421		return err;
 422	if (val < 0)
 423		return -EINVAL;
 424
 425	data->pwm_enable = val;
 426
 427	switch (val) {
 428	case 0:  /* Full speed */
 429		pwm_enable_control();
 430		set_pwm(255);
 431		break;
 432	case 1:  /* As set by pwm1 */
 433		pwm_enable_control();
 434		set_pwm(data->curr_pwm);
 435		break;
 436	default: /* Control by motherboard */
 437		pwm_disable_control();
 438		break;
 439	}
 440
 441	return count;
 442}
 443
 444static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
 445		char *buf)
 446{
 447	struct compal_data *data = dev_get_drvdata(dev);
 448	return sprintf(buf, "%hhu\n", data->curr_pwm);
 449}
 450
 451static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
 452		const char *buf, size_t count)
 453{
 454	struct compal_data *data = dev_get_drvdata(dev);
 455	long val;
 456	int err;
 457
 458	err = kstrtol(buf, 10, &val);
 459	if (err)
 460		return err;
 461	if (val < 0 || val > 255)
 462		return -EINVAL;
 463
 464	data->curr_pwm = val;
 465
 466	if (data->pwm_enable != 1)
 467		return count;
 468	set_pwm(val);
 469
 470	return count;
 471}
 472
 473static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
 474		char *buf)
 475{
 476	return sprintf(buf, "%d\n", get_fan_rpm());
 477}
 478
 479
 480/* Temperature interface */
 481#define TEMPERATURE_SHOW_TEMP_AND_LABEL(POSTFIX, ADDRESS, LABEL)	\
 482static ssize_t temp_##POSTFIX(struct device *dev,			\
 483		struct device_attribute *attr, char *buf)		\
 484{									\
 485	return sprintf(buf, "%d\n", 1000 * (int)ec_read_s8(ADDRESS));	\
 486}									\
 487static ssize_t label_##POSTFIX(struct device *dev,			\
 488		struct device_attribute *attr, char *buf)		\
 489{									\
 490	return sprintf(buf, "%s\n", LABEL);				\
 491}
 492
 493/* Labels as in service guide */
 494TEMPERATURE_SHOW_TEMP_AND_LABEL(cpu,        TEMP_CPU,        "CPU_TEMP");
 495TEMPERATURE_SHOW_TEMP_AND_LABEL(cpu_local,  TEMP_CPU_LOCAL,  "CPU_TEMP_LOCAL");
 496TEMPERATURE_SHOW_TEMP_AND_LABEL(cpu_DTS,    TEMP_CPU_DTS,    "CPU_DTS");
 497TEMPERATURE_SHOW_TEMP_AND_LABEL(northbridge,TEMP_NORTHBRIDGE,"NorthBridge");
 498TEMPERATURE_SHOW_TEMP_AND_LABEL(vga,        TEMP_VGA,        "VGA_TEMP");
 499TEMPERATURE_SHOW_TEMP_AND_LABEL(SKIN,       TEMP_SKIN,       "SKIN_TEMP90");
 500
 501
 502/* Power supply interface */
 503static int bat_status(void)
 504{
 505	u8 status0 = ec_read_u8(BAT_STATUS0);
 506	u8 status1 = ec_read_u8(BAT_STATUS1);
 507
 508	if (status0 & BAT_S0_CHARGING)
 509		return POWER_SUPPLY_STATUS_CHARGING;
 510	if (status0 & BAT_S0_DISCHARGE)
 511		return POWER_SUPPLY_STATUS_DISCHARGING;
 512	if (status1 & BAT_S1_FULL)
 513		return POWER_SUPPLY_STATUS_FULL;
 514	return POWER_SUPPLY_STATUS_NOT_CHARGING;
 515}
 516
 517static int bat_health(void)
 518{
 519	u8 status = ec_read_u8(BAT_STOP_CHARGE1);
 520
 521	if (status & BAT_STOP_CHRG1_OVERTEMPERATURE)
 522		return POWER_SUPPLY_HEALTH_OVERHEAT;
 523	if (status & BAT_STOP_CHRG1_OVERVOLTAGE)
 524		return POWER_SUPPLY_HEALTH_OVERVOLTAGE;
 525	if (status & BAT_STOP_CHRG1_BAD_CELL)
 526		return POWER_SUPPLY_HEALTH_DEAD;
 527	if (status & BAT_STOP_CHRG1_COMM_FAIL)
 528		return POWER_SUPPLY_HEALTH_UNKNOWN;
 529	return POWER_SUPPLY_HEALTH_GOOD;
 530}
 531
 532static int bat_is_present(void)
 533{
 534	u8 status = ec_read_u8(BAT_STATUS2);
 535	return ((status & BAT_S1_EXISTS) != 0);
 536}
 537
 538static int bat_technology(void)
 539{
 540	u8 status = ec_read_u8(BAT_STATUS1);
 541
 542	if (status & BAT_S1_LiION_OR_NiMH)
 543		return POWER_SUPPLY_TECHNOLOGY_LION;
 544	return POWER_SUPPLY_TECHNOLOGY_NiMH;
 545}
 546
 547static int bat_capacity_level(void)
 548{
 549	u8 status0 = ec_read_u8(BAT_STATUS0);
 550	u8 status1 = ec_read_u8(BAT_STATUS1);
 551	u8 status2 = ec_read_u8(BAT_STATUS2);
 552
 553	if (status0 & BAT_S0_DISCHRG_CRITICAL
 554			|| status1 & BAT_S1_EMPTY
 555			|| status2 & BAT_S2_LOW_LOW)
 556		return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
 557	if (status0 & BAT_S0_LOW)
 558		return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
 559	if (status1 & BAT_S1_FULL)
 560		return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
 561	return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
 562}
 563
 564static int bat_get_property(struct power_supply *psy,
 565				enum power_supply_property psp,
 566				union power_supply_propval *val)
 567{
 568	struct compal_data *data;
 569	data = container_of(psy, struct compal_data, psy);
 570
 571	switch (psp) {
 572	case POWER_SUPPLY_PROP_STATUS:
 573		val->intval = bat_status();
 574		break;
 575	case POWER_SUPPLY_PROP_HEALTH:
 576		val->intval = bat_health();
 577		break;
 578	case POWER_SUPPLY_PROP_PRESENT:
 579		val->intval = bat_is_present();
 580		break;
 581	case POWER_SUPPLY_PROP_TECHNOLOGY:
 582		val->intval = bat_technology();
 583		break;
 584	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: /* THE design voltage... */
 585		val->intval = ec_read_u16(BAT_VOLTAGE_DESIGN) * 1000;
 586		break;
 587	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 588		val->intval = ec_read_u16(BAT_VOLTAGE_NOW) * 1000;
 589		break;
 590	case POWER_SUPPLY_PROP_CURRENT_NOW:
 591		val->intval = ec_read_s16(BAT_CURRENT_NOW) * 1000;
 592		break;
 593	case POWER_SUPPLY_PROP_CURRENT_AVG:
 594		val->intval = ec_read_s16(BAT_CURRENT_AVG) * 1000;
 595		break;
 596	case POWER_SUPPLY_PROP_POWER_NOW:
 597		val->intval = ec_read_u8(BAT_POWER) * 1000000;
 598		break;
 599	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
 600		val->intval = ec_read_u16(BAT_CHARGE_DESIGN) * 1000;
 601		break;
 602	case POWER_SUPPLY_PROP_CHARGE_NOW:
 603		val->intval = ec_read_u16(BAT_CHARGE_NOW) * 1000;
 604		break;
 605	case POWER_SUPPLY_PROP_CAPACITY:
 606		val->intval = ec_read_u8(BAT_CAPACITY);
 607		break;
 608	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
 609		val->intval = bat_capacity_level();
 610		break;
 611	/* It smees that BAT_TEMP_AVG is a (2's complement?) value showing
 612	 * the number of degrees, whereas BAT_TEMP is somewhat more
 613	 * complicated. It looks like this is a negative nember with a
 614	 * 100/256 divider and an offset of 222. Both were determined
 615	 * experimentally by comparing BAT_TEMP and BAT_TEMP_AVG. */
 616	case POWER_SUPPLY_PROP_TEMP:
 617		val->intval = ((222 - (int)ec_read_u8(BAT_TEMP)) * 1000) >> 8;
 618		break;
 619	case POWER_SUPPLY_PROP_TEMP_AMBIENT: /* Ambient, Avg, ... same thing */
 620		val->intval = ec_read_s8(BAT_TEMP_AVG) * 10;
 621		break;
 622	/* Neither the model name nor manufacturer name work for me. */
 623	case POWER_SUPPLY_PROP_MODEL_NAME:
 624		val->strval = data->bat_model_name;
 625		break;
 626	case POWER_SUPPLY_PROP_MANUFACTURER:
 627		val->strval = data->bat_manufacturer_name;
 628		break;
 629	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
 630		val->strval = data->bat_serial_number;
 631		break;
 632	default:
 633		break;
 634	}
 635	return 0;
 636}
 637
 638
 639
 640
 641
 642/* ============== */
 643/* Driver Globals */
 644/* ============== */
 645static DEVICE_ATTR(wake_up_pme,
 646		0644, wake_up_pme_show,		wake_up_pme_store);
 647static DEVICE_ATTR(wake_up_modem,
 648		0644, wake_up_modem_show,	wake_up_modem_store);
 649static DEVICE_ATTR(wake_up_lan,
 650		0644, wake_up_lan_show,	wake_up_lan_store);
 651static DEVICE_ATTR(wake_up_wlan,
 652		0644, wake_up_wlan_show,	wake_up_wlan_store);
 653static DEVICE_ATTR(wake_up_key,
 654		0644, wake_up_key_show,	wake_up_key_store);
 655static DEVICE_ATTR(wake_up_mouse,
 656		0644, wake_up_mouse_show,	wake_up_mouse_store);
 657
 658static DEVICE_ATTR(fan1_input,  S_IRUGO, fan_show,          NULL);
 659static DEVICE_ATTR(temp1_input, S_IRUGO, temp_cpu,          NULL);
 660static DEVICE_ATTR(temp2_input, S_IRUGO, temp_cpu_local,    NULL);
 661static DEVICE_ATTR(temp3_input, S_IRUGO, temp_cpu_DTS,      NULL);
 662static DEVICE_ATTR(temp4_input, S_IRUGO, temp_northbridge,  NULL);
 663static DEVICE_ATTR(temp5_input, S_IRUGO, temp_vga,          NULL);
 664static DEVICE_ATTR(temp6_input, S_IRUGO, temp_SKIN,         NULL);
 665static DEVICE_ATTR(temp1_label, S_IRUGO, label_cpu,         NULL);
 666static DEVICE_ATTR(temp2_label, S_IRUGO, label_cpu_local,   NULL);
 667static DEVICE_ATTR(temp3_label, S_IRUGO, label_cpu_DTS,     NULL);
 668static DEVICE_ATTR(temp4_label, S_IRUGO, label_northbridge, NULL);
 669static DEVICE_ATTR(temp5_label, S_IRUGO, label_vga,         NULL);
 670static DEVICE_ATTR(temp6_label, S_IRUGO, label_SKIN,        NULL);
 671static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, pwm_show, pwm_store);
 672static DEVICE_ATTR(pwm1_enable,
 673		   S_IRUGO | S_IWUSR, pwm_enable_show, pwm_enable_store);
 
 674
 675static struct attribute *compal_platform_attrs[] = {
 676	&dev_attr_wake_up_pme.attr,
 677	&dev_attr_wake_up_modem.attr,
 678	&dev_attr_wake_up_lan.attr,
 679	&dev_attr_wake_up_wlan.attr,
 680	&dev_attr_wake_up_key.attr,
 681	&dev_attr_wake_up_mouse.attr,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 682	NULL
 683};
 684static struct attribute_group compal_platform_attr_group = {
 685	.attrs = compal_platform_attrs
 686};
 687
 688static struct attribute *compal_hwmon_attrs[] = {
 689	&dev_attr_pwm1_enable.attr,
 690	&dev_attr_pwm1.attr,
 691	&dev_attr_fan1_input.attr,
 692	&dev_attr_temp1_input.attr,
 693	&dev_attr_temp2_input.attr,
 694	&dev_attr_temp3_input.attr,
 695	&dev_attr_temp4_input.attr,
 696	&dev_attr_temp5_input.attr,
 697	&dev_attr_temp6_input.attr,
 698	&dev_attr_temp1_label.attr,
 699	&dev_attr_temp2_label.attr,
 700	&dev_attr_temp3_label.attr,
 701	&dev_attr_temp4_label.attr,
 702	&dev_attr_temp5_label.attr,
 703	&dev_attr_temp6_label.attr,
 704	NULL
 705};
 706ATTRIBUTE_GROUPS(compal_hwmon);
 707
 708static int compal_probe(struct platform_device *);
 709static int compal_remove(struct platform_device *);
 710static struct platform_driver compal_driver = {
 711	.driver = {
 712		.name = DRIVER_NAME,
 713		.owner = THIS_MODULE,
 714	},
 715	.probe	= compal_probe,
 716	.remove	= compal_remove,
 717};
 718
 719static enum power_supply_property compal_bat_properties[] = {
 720	POWER_SUPPLY_PROP_STATUS,
 721	POWER_SUPPLY_PROP_HEALTH,
 722	POWER_SUPPLY_PROP_PRESENT,
 723	POWER_SUPPLY_PROP_TECHNOLOGY,
 724	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
 725	POWER_SUPPLY_PROP_VOLTAGE_NOW,
 726	POWER_SUPPLY_PROP_CURRENT_NOW,
 727	POWER_SUPPLY_PROP_CURRENT_AVG,
 728	POWER_SUPPLY_PROP_POWER_NOW,
 729	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
 730	POWER_SUPPLY_PROP_CHARGE_NOW,
 731	POWER_SUPPLY_PROP_CAPACITY,
 732	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
 733	POWER_SUPPLY_PROP_TEMP,
 734	POWER_SUPPLY_PROP_TEMP_AMBIENT,
 735	POWER_SUPPLY_PROP_MODEL_NAME,
 736	POWER_SUPPLY_PROP_MANUFACTURER,
 737	POWER_SUPPLY_PROP_SERIAL_NUMBER,
 738};
 739
 740static struct backlight_device *compalbl_device;
 741
 742static struct platform_device *compal_device;
 743
 744static struct rfkill *wifi_rfkill;
 745static struct rfkill *bt_rfkill;
 746
 747
 748
 749
 750
 751/* =================================== */
 752/* Initialization & clean-up functions */
 753/* =================================== */
 754
 755static int dmi_check_cb(const struct dmi_system_id *id)
 756{
 757	pr_info("Identified laptop model '%s'\n", id->ident);
 758	extra_features = false;
 759	return 1;
 760}
 761
 762static int dmi_check_cb_extra(const struct dmi_system_id *id)
 763{
 764	pr_info("Identified laptop model '%s', enabling extra features\n",
 765		id->ident);
 766	extra_features = true;
 767	return 1;
 768}
 769
 770static struct dmi_system_id __initdata compal_dmi_table[] = {
 771	{
 772		.ident = "FL90/IFL90",
 773		.matches = {
 774			DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
 775			DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
 776		},
 777		.callback = dmi_check_cb
 778	},
 779	{
 780		.ident = "FL90/IFL90",
 781		.matches = {
 782			DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
 783			DMI_MATCH(DMI_BOARD_VERSION, "REFERENCE"),
 784		},
 785		.callback = dmi_check_cb
 786	},
 787	{
 788		.ident = "FL91/IFL91",
 789		.matches = {
 790			DMI_MATCH(DMI_BOARD_NAME, "IFL91"),
 791			DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
 792		},
 793		.callback = dmi_check_cb
 794	},
 795	{
 796		.ident = "FL92/JFL92",
 797		.matches = {
 798			DMI_MATCH(DMI_BOARD_NAME, "JFL92"),
 799			DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
 800		},
 801		.callback = dmi_check_cb
 802	},
 803	{
 804		.ident = "FT00/IFT00",
 805		.matches = {
 806			DMI_MATCH(DMI_BOARD_NAME, "IFT00"),
 807			DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
 808		},
 809		.callback = dmi_check_cb
 810	},
 811	{
 812		.ident = "Dell Mini 9",
 813		.matches = {
 814			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 815			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 910"),
 816		},
 817		.callback = dmi_check_cb
 818	},
 819	{
 820		.ident = "Dell Mini 10",
 821		.matches = {
 822			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 823			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1010"),
 824		},
 825		.callback = dmi_check_cb
 826	},
 827	{
 828		.ident = "Dell Mini 10v",
 829		.matches = {
 830			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 831			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1011"),
 832		},
 833		.callback = dmi_check_cb
 834	},
 835	{
 836		.ident = "Dell Mini 1012",
 837		.matches = {
 838			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 839			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
 840		},
 841		.callback = dmi_check_cb
 842	},
 843	{
 844		.ident = "Dell Inspiron 11z",
 845		.matches = {
 846			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 847			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1110"),
 848		},
 849		.callback = dmi_check_cb
 850	},
 851	{
 852		.ident = "Dell Mini 12",
 853		.matches = {
 854			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 855			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1210"),
 856		},
 857		.callback = dmi_check_cb
 858	},
 859	{
 860		.ident = "JHL90",
 861		.matches = {
 862			DMI_MATCH(DMI_BOARD_NAME, "JHL90"),
 863			DMI_MATCH(DMI_BOARD_VERSION, "REFERENCE"),
 864		},
 865		.callback = dmi_check_cb_extra
 866	},
 867	{
 868		.ident = "KHLB2",
 869		.matches = {
 870			DMI_MATCH(DMI_BOARD_NAME, "KHLB2"),
 871			DMI_MATCH(DMI_BOARD_VERSION, "REFERENCE"),
 872		},
 873		.callback = dmi_check_cb_extra
 874	},
 875	{ }
 876};
 877MODULE_DEVICE_TABLE(dmi, compal_dmi_table);
 878
 879static void initialize_power_supply_data(struct compal_data *data)
 880{
 881	data->psy.name = DRIVER_NAME;
 882	data->psy.type = POWER_SUPPLY_TYPE_BATTERY;
 883	data->psy.properties = compal_bat_properties;
 884	data->psy.num_properties = ARRAY_SIZE(compal_bat_properties);
 885	data->psy.get_property = bat_get_property;
 886
 887	ec_read_sequence(BAT_MANUFACTURER_NAME_ADDR,
 888					data->bat_manufacturer_name,
 889					BAT_MANUFACTURER_NAME_LEN);
 890	data->bat_manufacturer_name[BAT_MANUFACTURER_NAME_LEN] = 0;
 891
 892	ec_read_sequence(BAT_MODEL_NAME_ADDR,
 893					data->bat_model_name,
 894					BAT_MODEL_NAME_LEN);
 895	data->bat_model_name[BAT_MODEL_NAME_LEN] = 0;
 896
 897	scnprintf(data->bat_serial_number, BAT_SERIAL_NUMBER_LEN + 1, "%d",
 898				ec_read_u16(BAT_SERIAL_NUMBER_ADDR));
 899}
 900
 901static void initialize_fan_control_data(struct compal_data *data)
 902{
 903	data->pwm_enable = 2; /* Keep motherboard in control for now */
 904	data->curr_pwm = 255; /* Try not to cause a CPU_on_fire exception
 905				 if we take over... */
 906}
 907
 908static int setup_rfkill(void)
 909{
 910	int ret;
 911
 912	wifi_rfkill = rfkill_alloc("compal-wifi", &compal_device->dev,
 913				RFKILL_TYPE_WLAN, &compal_rfkill_ops,
 914				(void *) WIRELESS_WLAN);
 915	if (!wifi_rfkill)
 916		return -ENOMEM;
 917
 918	ret = rfkill_register(wifi_rfkill);
 919	if (ret)
 920		goto err_wifi;
 921
 922	bt_rfkill = rfkill_alloc("compal-bluetooth", &compal_device->dev,
 923				RFKILL_TYPE_BLUETOOTH, &compal_rfkill_ops,
 924				(void *) WIRELESS_BT);
 925	if (!bt_rfkill) {
 926		ret = -ENOMEM;
 927		goto err_allocate_bt;
 928	}
 929	ret = rfkill_register(bt_rfkill);
 930	if (ret)
 931		goto err_register_bt;
 932
 933	return 0;
 934
 935err_register_bt:
 936	rfkill_destroy(bt_rfkill);
 937
 938err_allocate_bt:
 939	rfkill_unregister(wifi_rfkill);
 940
 941err_wifi:
 942	rfkill_destroy(wifi_rfkill);
 943
 944	return ret;
 945}
 946
 947static int __init compal_init(void)
 948{
 949	int ret;
 950
 951	if (acpi_disabled) {
 952		pr_err("ACPI needs to be enabled for this driver to work!\n");
 953		return -ENODEV;
 954	}
 955
 956	if (!force && !dmi_check_system(compal_dmi_table)) {
 957		pr_err("Motherboard not recognized (You could try the module's force-parameter)\n");
 958		return -ENODEV;
 959	}
 960
 961	if (!acpi_video_backlight_support()) {
 962		struct backlight_properties props;
 963		memset(&props, 0, sizeof(struct backlight_properties));
 964		props.type = BACKLIGHT_PLATFORM;
 965		props.max_brightness = BACKLIGHT_LEVEL_MAX;
 966		compalbl_device = backlight_device_register(DRIVER_NAME,
 967							    NULL, NULL,
 968							    &compalbl_ops,
 969							    &props);
 970		if (IS_ERR(compalbl_device))
 971			return PTR_ERR(compalbl_device);
 972	}
 973
 974	ret = platform_driver_register(&compal_driver);
 975	if (ret)
 976		goto err_backlight;
 977
 978	compal_device = platform_device_alloc(DRIVER_NAME, -1);
 979	if (!compal_device) {
 980		ret = -ENOMEM;
 981		goto err_platform_driver;
 982	}
 983
 984	ret = platform_device_add(compal_device); /* This calls compal_probe */
 985	if (ret)
 986		goto err_platform_device;
 987
 988	ret = setup_rfkill();
 989	if (ret)
 990		goto err_rfkill;
 991
 992	pr_info("Driver " DRIVER_VERSION " successfully loaded\n");
 993	return 0;
 994
 995err_rfkill:
 996	platform_device_del(compal_device);
 997
 998err_platform_device:
 999	platform_device_put(compal_device);
1000
1001err_platform_driver:
1002	platform_driver_unregister(&compal_driver);
1003
1004err_backlight:
1005	backlight_device_unregister(compalbl_device);
1006
1007	return ret;
1008}
1009
1010static int compal_probe(struct platform_device *pdev)
1011{
1012	int err;
1013	struct compal_data *data;
1014	struct device *hwmon_dev;
1015
1016	if (!extra_features)
1017		return 0;
1018
1019	/* Fan control */
1020	data = devm_kzalloc(&pdev->dev, sizeof(struct compal_data), GFP_KERNEL);
1021	if (!data)
1022		return -ENOMEM;
1023
1024	initialize_fan_control_data(data);
1025
1026	err = sysfs_create_group(&pdev->dev.kobj, &compal_platform_attr_group);
1027	if (err)
 
1028		return err;
 
1029
1030	hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
1031						      DRIVER_NAME, data,
1032						      compal_hwmon_groups);
1033	if (IS_ERR(hwmon_dev)) {
1034		err = PTR_ERR(hwmon_dev);
1035		goto remove;
 
1036	}
1037
1038	/* Power supply */
1039	initialize_power_supply_data(data);
1040	power_supply_register(&compal_device->dev, &data->psy);
1041
1042	platform_set_drvdata(pdev, data);
1043
1044	return 0;
1045
1046remove:
1047	sysfs_remove_group(&pdev->dev.kobj, &compal_platform_attr_group);
1048	return err;
1049}
1050
1051static void __exit compal_cleanup(void)
1052{
1053	platform_device_unregister(compal_device);
1054	platform_driver_unregister(&compal_driver);
1055	backlight_device_unregister(compalbl_device);
1056	rfkill_unregister(wifi_rfkill);
1057	rfkill_unregister(bt_rfkill);
1058	rfkill_destroy(wifi_rfkill);
1059	rfkill_destroy(bt_rfkill);
1060
1061	pr_info("Driver unloaded\n");
1062}
1063
1064static int compal_remove(struct platform_device *pdev)
1065{
1066	struct compal_data *data;
1067
1068	if (!extra_features)
1069		return 0;
1070
1071	pr_info("Unloading: resetting fan control to motherboard\n");
1072	pwm_disable_control();
1073
1074	data = platform_get_drvdata(pdev);
 
1075	power_supply_unregister(&data->psy);
1076
1077	sysfs_remove_group(&pdev->dev.kobj, &compal_platform_attr_group);
 
 
 
1078
1079	return 0;
1080}
1081
1082
1083module_init(compal_init);
1084module_exit(compal_cleanup);
1085
1086MODULE_AUTHOR("Cezary Jackiewicz");
1087MODULE_AUTHOR("Roald Frederickx (roald.frederickx@gmail.com)");
1088MODULE_DESCRIPTION("Compal Laptop Support");
1089MODULE_VERSION(DRIVER_VERSION);
1090MODULE_LICENSE("GPL");