blob: e1c2b6d4b24ab5959d816dc1cc772678c42dccb3 [file] [log] [blame]
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001/*-*-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/*
Roald Frederickx9be0fcb2010-07-20 15:19:39 -070027 * compal-laptop.c - Compal laptop support.
Cezary Jackiewicz54115522008-06-09 16:22:22 -070028 *
Roald Frederickx9be0fcb2010-07-20 15:19:39 -070029 * 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 *
Cezary Jackiewicz54115522008-06-09 16:22:22 -070046 *
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
Roald Frederickx9be0fcb2010-07-20 15:19:39 -070050 * 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!
Cezary Jackiewicz54115522008-06-09 16:22:22 -070065 */
66
Roald Frederickx9be0fcb2010-07-20 15:19:39 -070067/* 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
Joe Perchesb4a4bc02011-03-29 15:21:36 -070071#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
72
Cezary Jackiewicz54115522008-06-09 16:22:22 -070073#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>
Mario Limonciello493e9142009-08-25 10:30:13 -050080#include <linux/rfkill.h>
Roald Frederickx9be0fcb2010-07-20 15:19:39 -070081#include <linux/hwmon.h>
82#include <linux/hwmon-sysfs.h>
83#include <linux/power_supply.h>
84#include <linux/fb.h>
Hans de Goede358fefe2015-06-16 16:27:59 +020085#include <acpi/video.h>
Cezary Jackiewicz54115522008-06-09 16:22:22 -070086
Roald Frederickx9be0fcb2010-07-20 15:19:39 -070087/* ======= */
88/* Defines */
89/* ======= */
90#define DRIVER_NAME "compal-laptop"
91#define DRIVER_VERSION "0.2.7"
Cezary Jackiewicz54115522008-06-09 16:22:22 -070092
Roald Frederickx9be0fcb2010-07-20 15:19:39 -070093#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
Cezary Jackiewicz54115522008-06-09 16:22:22 -070098
Roald Frederickx9be0fcb2010-07-20 15:19:39 -070099#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)
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700106
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700107#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)
Mario Limonciello493e9142009-08-25 10:30:13 -0500113
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700114#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
Roald Frederickxd9a427e2015-09-12 22:00:16 +0200154#define BAT_CHARGE_LIMIT 0x03
155#define BAT_CHARGE_LIMIT_MAX 100
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700156
157#define BAT_S0_DISCHARGE (1 << 0)
158#define BAT_S0_DISCHRG_CRITICAL (1 << 2)
159#define BAT_S0_LOW (1 << 3)
160#define BAT_S0_CHARGING (1 << 1)
161#define BAT_S0_AC (1 << 7)
162#define BAT_S1_EXISTS (1 << 0)
163#define BAT_S1_FULL (1 << 1)
164#define BAT_S1_EMPTY (1 << 2)
165#define BAT_S1_LiION_OR_NiMH (1 << 7)
166#define BAT_S2_LOW_LOW (1 << 0)
167#define BAT_STOP_CHRG1_BAD_CELL (1 << 1)
168#define BAT_STOP_CHRG1_COMM_FAIL (1 << 2)
169#define BAT_STOP_CHRG1_OVERVOLTAGE (1 << 6)
170#define BAT_STOP_CHRG1_OVERTEMPERATURE (1 << 7)
171
172
173/* ======= */
174/* Structs */
175/* ======= */
176struct compal_data{
177 /* Fan control */
Guenter Roeckc2be45f2013-11-23 11:03:20 -0800178 int pwm_enable; /* 0:full on, 1:set by pwm1, 2:control by motherboard */
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700179 unsigned char curr_pwm;
180
181 /* Power supply */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100182 struct power_supply *psy;
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700183 struct power_supply_info psy_info;
184 char bat_model_name[BAT_MODEL_NAME_LEN + 1];
185 char bat_manufacturer_name[BAT_MANUFACTURER_NAME_LEN + 1];
186 char bat_serial_number[BAT_SERIAL_NUMBER_LEN + 1];
187};
188
189
190/* =============== */
191/* General globals */
192/* =============== */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030193static bool force;
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700194module_param(force, bool, 0);
195MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
196
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700197/* Support for the wake_on_XXX, hwmon and power_supply interface. Currently
198 * only gets enabled on a JHL90 board. Might work with the others too */
199static bool extra_features;
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700200
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700201/* Nasty stuff. For some reason the fan control is very un-linear. I've
202 * come up with these values by looping through the possible inputs and
203 * watching the output of address 0x4F (do an ec_transaction writing 0x33
204 * into 0x4F and read a few bytes from the output, like so:
205 * u8 writeData = 0x33;
Thomas Renninger1cb7b1e2011-03-31 13:36:38 +0200206 * ec_transaction(0x4F, &writeData, 1, buffer, 32);
Len Brown751516f2011-05-29 04:40:39 -0400207 * That address is labeled "fan1 table information" in the service manual.
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700208 * It should be clear which value in 'buffer' changes). This seems to be
209 * related to fan speed. It isn't a proper 'realtime' fan speed value
210 * though, because physically stopping or speeding up the fan doesn't
211 * change it. It might be the average voltage or current of the pwm output.
212 * Nevertheless, it is more fine-grained than the actual RPM reading */
213static const unsigned char pwm_lookup_table[256] = {
214 0, 0, 0, 1, 1, 1, 2, 253, 254, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6,
215 7, 7, 7, 8, 86, 86, 9, 9, 9, 10, 10, 10, 11, 92, 92, 12, 12, 95,
216 13, 66, 66, 14, 14, 98, 15, 15, 15, 16, 16, 67, 17, 17, 72, 18, 70,
217 75, 19, 90, 90, 73, 73, 73, 21, 21, 91, 91, 91, 96, 23, 94, 94, 94,
218 94, 94, 94, 94, 94, 94, 94, 141, 141, 238, 223, 192, 139, 139, 139,
219 139, 139, 142, 142, 142, 142, 142, 78, 78, 78, 78, 78, 76, 76, 76,
220 76, 76, 79, 79, 79, 79, 79, 79, 79, 20, 20, 20, 20, 20, 22, 22, 22,
221 22, 22, 24, 24, 24, 24, 24, 24, 219, 219, 219, 219, 219, 219, 219,
222 219, 27, 27, 188, 188, 28, 28, 28, 29, 186, 186, 186, 186, 186,
223 186, 186, 186, 186, 186, 31, 31, 31, 31, 31, 32, 32, 32, 41, 33,
224 33, 33, 33, 33, 252, 252, 34, 34, 34, 43, 35, 35, 35, 36, 36, 38,
225 206, 206, 206, 206, 206, 206, 206, 206, 206, 37, 37, 37, 46, 46,
226 47, 47, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 48, 48,
227 48, 48, 48, 40, 40, 40, 49, 42, 42, 42, 42, 42, 42, 42, 42, 44,
228 189, 189, 189, 189, 54, 54, 45, 45, 45, 45, 45, 45, 45, 45, 251,
229 191, 199, 199, 199, 199, 199, 215, 215, 215, 215, 187, 187, 187,
230 187, 187, 193, 50
231};
232
233
234
235
236/* ========================= */
237/* Hardware access functions */
238/* ========================= */
239/* General access */
240static u8 ec_read_u8(u8 addr)
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700241{
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700242 u8 value;
243 ec_read(addr, &value);
244 return value;
245}
246
247static s8 ec_read_s8(u8 addr)
248{
249 return (s8)ec_read_u8(addr);
250}
251
252static u16 ec_read_u16(u8 addr)
253{
254 int hi, lo;
255 lo = ec_read_u8(addr);
256 hi = ec_read_u8(addr + 1);
257 return (hi << 8) + lo;
258}
259
260static s16 ec_read_s16(u8 addr)
261{
262 return (s16) ec_read_u16(addr);
263}
264
265static void ec_read_sequence(u8 addr, u8 *buf, int len)
266{
267 int i;
268 for (i = 0; i < len; i++)
269 ec_read(addr + i, buf + i);
270}
271
272
273/* Backlight access */
274static int set_backlight_level(int level)
275{
276 if (level < 0 || level > BACKLIGHT_LEVEL_MAX)
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700277 return -EINVAL;
278
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700279 ec_write(BACKLIGHT_LEVEL_ADDR, level);
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700280
Axel Lin0e4510f2011-03-14 18:53:14 +0800281 return 0;
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700282}
283
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700284static int get_backlight_level(void)
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700285{
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700286 return (int) ec_read_u8(BACKLIGHT_LEVEL_ADDR);
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700287}
288
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700289static void set_backlight_state(bool on)
290{
291 u8 data = on ? BACKLIGHT_STATE_ON_DATA : BACKLIGHT_STATE_OFF_DATA;
Thomas Renninger1cb7b1e2011-03-31 13:36:38 +0200292 ec_transaction(BACKLIGHT_STATE_ADDR, &data, 1, NULL, 0);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700293}
294
295
296/* Fan control access */
297static void pwm_enable_control(void)
298{
299 unsigned char writeData = PWM_ENABLE_DATA;
Thomas Renninger1cb7b1e2011-03-31 13:36:38 +0200300 ec_transaction(PWM_ENABLE_ADDR, &writeData, 1, NULL, 0);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700301}
302
303static void pwm_disable_control(void)
304{
305 unsigned char writeData = PWM_DISABLE_DATA;
Thomas Renninger1cb7b1e2011-03-31 13:36:38 +0200306 ec_transaction(PWM_DISABLE_ADDR, &writeData, 1, NULL, 0);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700307}
308
309static void set_pwm(int pwm)
310{
Thomas Renninger1cb7b1e2011-03-31 13:36:38 +0200311 ec_transaction(PWM_ADDRESS, &pwm_lookup_table[pwm], 1, NULL, 0);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700312}
313
314static int get_fan_rpm(void)
315{
316 u8 value, data = FAN_DATA;
Thomas Renninger1cb7b1e2011-03-31 13:36:38 +0200317 ec_transaction(FAN_ADDRESS, &data, 1, &value, 1);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700318 return 100 * (int)value;
319}
320
321
322
323
324/* =================== */
325/* Interface functions */
326/* =================== */
327
328/* Backlight interface */
329static int bl_get_brightness(struct backlight_device *b)
330{
331 return get_backlight_level();
332}
333
334static int bl_update_status(struct backlight_device *b)
335{
336 int ret = set_backlight_level(b->props.brightness);
337 if (ret)
338 return ret;
339
340 set_backlight_state((b->props.power == FB_BLANK_UNBLANK)
341 && !(b->props.state & BL_CORE_SUSPENDED)
342 && !(b->props.state & BL_CORE_FBBLANK));
343 return 0;
344}
345
346static const struct backlight_ops compalbl_ops = {
347 .get_brightness = bl_get_brightness,
348 .update_status = bl_update_status,
349};
350
351
352/* Wireless interface */
Mario Limonciello493e9142009-08-25 10:30:13 -0500353static int compal_rfkill_set(void *data, bool blocked)
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700354{
Mario Limonciello493e9142009-08-25 10:30:13 -0500355 unsigned long radio = (unsigned long) data;
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700356 u8 result = ec_read_u8(WIRELESS_ADDR);
357 u8 value;
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700358
Mario Limonciello493e9142009-08-25 10:30:13 -0500359 if (!blocked)
360 value = (u8) (result | radio);
361 else
362 value = (u8) (result & ~radio);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700363 ec_write(WIRELESS_ADDR, value);
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700364
365 return 0;
366}
367
Mario Limonciello493e9142009-08-25 10:30:13 -0500368static void compal_rfkill_poll(struct rfkill *rfkill, void *data)
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700369{
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700370 u8 result = ec_read_u8(WIRELESS_ADDR);
371 bool hw_blocked = !(result & WIRELESS_KILLSWITCH);
Mario Limonciello493e9142009-08-25 10:30:13 -0500372 rfkill_set_hw_state(rfkill, hw_blocked);
373}
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700374
Mario Limonciello493e9142009-08-25 10:30:13 -0500375static const struct rfkill_ops compal_rfkill_ops = {
376 .poll = compal_rfkill_poll,
377 .set_block = compal_rfkill_set,
378};
379
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700380
381/* Wake_up interface */
382#define SIMPLE_MASKED_STORE_SHOW(NAME, ADDR, MASK) \
383static ssize_t NAME##_show(struct device *dev, \
384 struct device_attribute *attr, char *buf) \
385{ \
386 return sprintf(buf, "%d\n", ((ec_read_u8(ADDR) & MASK) != 0)); \
387} \
388static ssize_t NAME##_store(struct device *dev, \
389 struct device_attribute *attr, const char *buf, size_t count) \
390{ \
391 int state; \
392 u8 old_val = ec_read_u8(ADDR); \
393 if (sscanf(buf, "%d", &state) != 1 || (state < 0 || state > 1)) \
394 return -EINVAL; \
395 ec_write(ADDR, state ? (old_val | MASK) : (old_val & ~MASK)); \
396 return count; \
397}
398
399SIMPLE_MASKED_STORE_SHOW(wake_up_pme, WAKE_UP_ADDR, WAKE_UP_PME)
400SIMPLE_MASKED_STORE_SHOW(wake_up_modem, WAKE_UP_ADDR, WAKE_UP_MODEM)
401SIMPLE_MASKED_STORE_SHOW(wake_up_lan, WAKE_UP_ADDR, WAKE_UP_LAN)
402SIMPLE_MASKED_STORE_SHOW(wake_up_wlan, WAKE_UP_ADDR, WAKE_UP_WLAN)
403SIMPLE_MASKED_STORE_SHOW(wake_up_key, WAKE_UP_ADDR, WAKE_UP_KEY)
404SIMPLE_MASKED_STORE_SHOW(wake_up_mouse, WAKE_UP_ADDR, WAKE_UP_MOUSE)
405
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700406/* Fan control interface */
407static ssize_t pwm_enable_show(struct device *dev,
408 struct device_attribute *attr, char *buf)
409{
410 struct compal_data *data = dev_get_drvdata(dev);
411 return sprintf(buf, "%d\n", data->pwm_enable);
412}
Mario Limonciello493e9142009-08-25 10:30:13 -0500413
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700414static ssize_t pwm_enable_store(struct device *dev,
415 struct device_attribute *attr, const char *buf, size_t count)
416{
417 struct compal_data *data = dev_get_drvdata(dev);
418 long val;
419 int err;
Jingoo Han0db7fd92013-07-19 16:18:47 +0900420
421 err = kstrtol(buf, 10, &val);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700422 if (err)
423 return err;
424 if (val < 0)
425 return -EINVAL;
426
427 data->pwm_enable = val;
428
429 switch (val) {
430 case 0: /* Full speed */
431 pwm_enable_control();
432 set_pwm(255);
433 break;
434 case 1: /* As set by pwm1 */
435 pwm_enable_control();
436 set_pwm(data->curr_pwm);
437 break;
438 default: /* Control by motherboard */
439 pwm_disable_control();
440 break;
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700441 }
442
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700443 return count;
444}
445
446static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
447 char *buf)
448{
449 struct compal_data *data = dev_get_drvdata(dev);
450 return sprintf(buf, "%hhu\n", data->curr_pwm);
451}
452
453static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
454 const char *buf, size_t count)
455{
456 struct compal_data *data = dev_get_drvdata(dev);
457 long val;
458 int err;
Jingoo Han0db7fd92013-07-19 16:18:47 +0900459
460 err = kstrtol(buf, 10, &val);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700461 if (err)
462 return err;
463 if (val < 0 || val > 255)
464 return -EINVAL;
465
466 data->curr_pwm = val;
467
468 if (data->pwm_enable != 1)
469 return count;
470 set_pwm(val);
471
472 return count;
473}
474
475static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
476 char *buf)
477{
478 return sprintf(buf, "%d\n", get_fan_rpm());
479}
480
481
482/* Temperature interface */
483#define TEMPERATURE_SHOW_TEMP_AND_LABEL(POSTFIX, ADDRESS, LABEL) \
484static ssize_t temp_##POSTFIX(struct device *dev, \
485 struct device_attribute *attr, char *buf) \
486{ \
487 return sprintf(buf, "%d\n", 1000 * (int)ec_read_s8(ADDRESS)); \
488} \
489static ssize_t label_##POSTFIX(struct device *dev, \
490 struct device_attribute *attr, char *buf) \
491{ \
492 return sprintf(buf, "%s\n", LABEL); \
493}
494
495/* Labels as in service guide */
496TEMPERATURE_SHOW_TEMP_AND_LABEL(cpu, TEMP_CPU, "CPU_TEMP");
497TEMPERATURE_SHOW_TEMP_AND_LABEL(cpu_local, TEMP_CPU_LOCAL, "CPU_TEMP_LOCAL");
498TEMPERATURE_SHOW_TEMP_AND_LABEL(cpu_DTS, TEMP_CPU_DTS, "CPU_DTS");
499TEMPERATURE_SHOW_TEMP_AND_LABEL(northbridge,TEMP_NORTHBRIDGE,"NorthBridge");
500TEMPERATURE_SHOW_TEMP_AND_LABEL(vga, TEMP_VGA, "VGA_TEMP");
501TEMPERATURE_SHOW_TEMP_AND_LABEL(SKIN, TEMP_SKIN, "SKIN_TEMP90");
502
503
504/* Power supply interface */
505static int bat_status(void)
506{
507 u8 status0 = ec_read_u8(BAT_STATUS0);
508 u8 status1 = ec_read_u8(BAT_STATUS1);
509
510 if (status0 & BAT_S0_CHARGING)
511 return POWER_SUPPLY_STATUS_CHARGING;
512 if (status0 & BAT_S0_DISCHARGE)
513 return POWER_SUPPLY_STATUS_DISCHARGING;
514 if (status1 & BAT_S1_FULL)
515 return POWER_SUPPLY_STATUS_FULL;
516 return POWER_SUPPLY_STATUS_NOT_CHARGING;
517}
518
519static int bat_health(void)
520{
521 u8 status = ec_read_u8(BAT_STOP_CHARGE1);
522
523 if (status & BAT_STOP_CHRG1_OVERTEMPERATURE)
524 return POWER_SUPPLY_HEALTH_OVERHEAT;
525 if (status & BAT_STOP_CHRG1_OVERVOLTAGE)
526 return POWER_SUPPLY_HEALTH_OVERVOLTAGE;
527 if (status & BAT_STOP_CHRG1_BAD_CELL)
528 return POWER_SUPPLY_HEALTH_DEAD;
529 if (status & BAT_STOP_CHRG1_COMM_FAIL)
530 return POWER_SUPPLY_HEALTH_UNKNOWN;
531 return POWER_SUPPLY_HEALTH_GOOD;
532}
533
534static int bat_is_present(void)
535{
536 u8 status = ec_read_u8(BAT_STATUS2);
537 return ((status & BAT_S1_EXISTS) != 0);
538}
539
540static int bat_technology(void)
541{
542 u8 status = ec_read_u8(BAT_STATUS1);
543
544 if (status & BAT_S1_LiION_OR_NiMH)
545 return POWER_SUPPLY_TECHNOLOGY_LION;
546 return POWER_SUPPLY_TECHNOLOGY_NiMH;
547}
548
549static int bat_capacity_level(void)
550{
551 u8 status0 = ec_read_u8(BAT_STATUS0);
552 u8 status1 = ec_read_u8(BAT_STATUS1);
553 u8 status2 = ec_read_u8(BAT_STATUS2);
554
555 if (status0 & BAT_S0_DISCHRG_CRITICAL
556 || status1 & BAT_S1_EMPTY
557 || status2 & BAT_S2_LOW_LOW)
558 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
559 if (status0 & BAT_S0_LOW)
560 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
561 if (status1 & BAT_S1_FULL)
562 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
563 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
564}
565
566static int bat_get_property(struct power_supply *psy,
567 enum power_supply_property psp,
568 union power_supply_propval *val)
569{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100570 struct compal_data *data = power_supply_get_drvdata(psy);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700571
572 switch (psp) {
573 case POWER_SUPPLY_PROP_STATUS:
574 val->intval = bat_status();
575 break;
576 case POWER_SUPPLY_PROP_HEALTH:
577 val->intval = bat_health();
578 break;
579 case POWER_SUPPLY_PROP_PRESENT:
580 val->intval = bat_is_present();
581 break;
582 case POWER_SUPPLY_PROP_TECHNOLOGY:
583 val->intval = bat_technology();
584 break;
585 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: /* THE design voltage... */
586 val->intval = ec_read_u16(BAT_VOLTAGE_DESIGN) * 1000;
587 break;
588 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
589 val->intval = ec_read_u16(BAT_VOLTAGE_NOW) * 1000;
590 break;
591 case POWER_SUPPLY_PROP_CURRENT_NOW:
592 val->intval = ec_read_s16(BAT_CURRENT_NOW) * 1000;
593 break;
594 case POWER_SUPPLY_PROP_CURRENT_AVG:
595 val->intval = ec_read_s16(BAT_CURRENT_AVG) * 1000;
596 break;
597 case POWER_SUPPLY_PROP_POWER_NOW:
598 val->intval = ec_read_u8(BAT_POWER) * 1000000;
599 break;
600 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
601 val->intval = ec_read_u16(BAT_CHARGE_DESIGN) * 1000;
602 break;
603 case POWER_SUPPLY_PROP_CHARGE_NOW:
604 val->intval = ec_read_u16(BAT_CHARGE_NOW) * 1000;
605 break;
Roald Frederickxd9a427e2015-09-12 22:00:16 +0200606 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
607 val->intval = ec_read_u8(BAT_CHARGE_LIMIT);
608 break;
609 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
610 val->intval = BAT_CHARGE_LIMIT_MAX;
611 break;
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700612 case POWER_SUPPLY_PROP_CAPACITY:
613 val->intval = ec_read_u8(BAT_CAPACITY);
614 break;
615 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
616 val->intval = bat_capacity_level();
617 break;
618 /* It smees that BAT_TEMP_AVG is a (2's complement?) value showing
619 * the number of degrees, whereas BAT_TEMP is somewhat more
620 * complicated. It looks like this is a negative nember with a
621 * 100/256 divider and an offset of 222. Both were determined
622 * experimentally by comparing BAT_TEMP and BAT_TEMP_AVG. */
623 case POWER_SUPPLY_PROP_TEMP:
624 val->intval = ((222 - (int)ec_read_u8(BAT_TEMP)) * 1000) >> 8;
625 break;
626 case POWER_SUPPLY_PROP_TEMP_AMBIENT: /* Ambient, Avg, ... same thing */
627 val->intval = ec_read_s8(BAT_TEMP_AVG) * 10;
628 break;
629 /* Neither the model name nor manufacturer name work for me. */
630 case POWER_SUPPLY_PROP_MODEL_NAME:
631 val->strval = data->bat_model_name;
632 break;
633 case POWER_SUPPLY_PROP_MANUFACTURER:
634 val->strval = data->bat_manufacturer_name;
635 break;
636 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
637 val->strval = data->bat_serial_number;
638 break;
639 default:
640 break;
641 }
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700642 return 0;
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700643}
644
Roald Frederickxd9a427e2015-09-12 22:00:16 +0200645static int bat_set_property(struct power_supply *psy,
646 enum power_supply_property psp,
647 const union power_supply_propval *val)
648{
649 int level;
650
651 switch (psp) {
652 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
653 level = val->intval;
654 if (level < 0 || level > BAT_CHARGE_LIMIT_MAX)
655 return -EINVAL;
656 if (ec_write(BAT_CHARGE_LIMIT, level) < 0)
657 return -EIO;
658 break;
659 default:
660 break;
661 }
662 return 0;
663}
664
665static int bat_writeable_property(struct power_supply *psy,
666 enum power_supply_property psp)
667{
668 switch (psp) {
669 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
670 return 1;
671 default:
672 return 0;
673 }
674}
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700675
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700676
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700677
678
679/* ============== */
680/* Driver Globals */
681/* ============== */
682static DEVICE_ATTR(wake_up_pme,
683 0644, wake_up_pme_show, wake_up_pme_store);
684static DEVICE_ATTR(wake_up_modem,
685 0644, wake_up_modem_show, wake_up_modem_store);
686static DEVICE_ATTR(wake_up_lan,
687 0644, wake_up_lan_show, wake_up_lan_store);
688static DEVICE_ATTR(wake_up_wlan,
689 0644, wake_up_wlan_show, wake_up_wlan_store);
690static DEVICE_ATTR(wake_up_key,
691 0644, wake_up_key_show, wake_up_key_store);
692static DEVICE_ATTR(wake_up_mouse,
693 0644, wake_up_mouse_show, wake_up_mouse_store);
694
Guenter Roeck4e062582013-11-23 11:03:18 -0800695static DEVICE_ATTR(fan1_input, S_IRUGO, fan_show, NULL);
696static DEVICE_ATTR(temp1_input, S_IRUGO, temp_cpu, NULL);
697static DEVICE_ATTR(temp2_input, S_IRUGO, temp_cpu_local, NULL);
698static DEVICE_ATTR(temp3_input, S_IRUGO, temp_cpu_DTS, NULL);
699static DEVICE_ATTR(temp4_input, S_IRUGO, temp_northbridge, NULL);
700static DEVICE_ATTR(temp5_input, S_IRUGO, temp_vga, NULL);
701static DEVICE_ATTR(temp6_input, S_IRUGO, temp_SKIN, NULL);
702static DEVICE_ATTR(temp1_label, S_IRUGO, label_cpu, NULL);
703static DEVICE_ATTR(temp2_label, S_IRUGO, label_cpu_local, NULL);
704static DEVICE_ATTR(temp3_label, S_IRUGO, label_cpu_DTS, NULL);
705static DEVICE_ATTR(temp4_label, S_IRUGO, label_northbridge, NULL);
706static DEVICE_ATTR(temp5_label, S_IRUGO, label_vga, NULL);
707static DEVICE_ATTR(temp6_label, S_IRUGO, label_SKIN, NULL);
708static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, pwm_show, pwm_store);
709static DEVICE_ATTR(pwm1_enable,
710 S_IRUGO | S_IWUSR, pwm_enable_show, pwm_enable_store);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700711
Guenter Roeckc2be45f2013-11-23 11:03:20 -0800712static struct attribute *compal_platform_attrs[] = {
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700713 &dev_attr_wake_up_pme.attr,
714 &dev_attr_wake_up_modem.attr,
715 &dev_attr_wake_up_lan.attr,
716 &dev_attr_wake_up_wlan.attr,
717 &dev_attr_wake_up_key.attr,
718 &dev_attr_wake_up_mouse.attr,
Guenter Roeckc2be45f2013-11-23 11:03:20 -0800719 NULL
720};
721static struct attribute_group compal_platform_attr_group = {
722 .attrs = compal_platform_attrs
723};
724
725static struct attribute *compal_hwmon_attrs[] = {
Guenter Roeck4e062582013-11-23 11:03:18 -0800726 &dev_attr_pwm1_enable.attr,
727 &dev_attr_pwm1.attr,
728 &dev_attr_fan1_input.attr,
729 &dev_attr_temp1_input.attr,
730 &dev_attr_temp2_input.attr,
731 &dev_attr_temp3_input.attr,
732 &dev_attr_temp4_input.attr,
733 &dev_attr_temp5_input.attr,
734 &dev_attr_temp6_input.attr,
735 &dev_attr_temp1_label.attr,
736 &dev_attr_temp2_label.attr,
737 &dev_attr_temp3_label.attr,
738 &dev_attr_temp4_label.attr,
739 &dev_attr_temp5_label.attr,
740 &dev_attr_temp6_label.attr,
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700741 NULL
742};
Guenter Roeckc2be45f2013-11-23 11:03:20 -0800743ATTRIBUTE_GROUPS(compal_hwmon);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700744
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800745static int compal_probe(struct platform_device *);
746static int compal_remove(struct platform_device *);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700747static struct platform_driver compal_driver = {
748 .driver = {
749 .name = DRIVER_NAME,
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700750 },
751 .probe = compal_probe,
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800752 .remove = compal_remove,
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700753};
754
755static enum power_supply_property compal_bat_properties[] = {
756 POWER_SUPPLY_PROP_STATUS,
757 POWER_SUPPLY_PROP_HEALTH,
758 POWER_SUPPLY_PROP_PRESENT,
759 POWER_SUPPLY_PROP_TECHNOLOGY,
760 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
761 POWER_SUPPLY_PROP_VOLTAGE_NOW,
762 POWER_SUPPLY_PROP_CURRENT_NOW,
763 POWER_SUPPLY_PROP_CURRENT_AVG,
764 POWER_SUPPLY_PROP_POWER_NOW,
765 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
766 POWER_SUPPLY_PROP_CHARGE_NOW,
Roald Frederickxd9a427e2015-09-12 22:00:16 +0200767 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
768 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700769 POWER_SUPPLY_PROP_CAPACITY,
770 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
771 POWER_SUPPLY_PROP_TEMP,
772 POWER_SUPPLY_PROP_TEMP_AMBIENT,
773 POWER_SUPPLY_PROP_MODEL_NAME,
774 POWER_SUPPLY_PROP_MANUFACTURER,
775 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700776};
777
778static struct backlight_device *compalbl_device;
779
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700780static struct platform_device *compal_device;
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700781
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700782static struct rfkill *wifi_rfkill;
783static struct rfkill *bt_rfkill;
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700784
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700785
786
787
788
789/* =================================== */
790/* Initialization & clean-up functions */
791/* =================================== */
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700792
793static int dmi_check_cb(const struct dmi_system_id *id)
794{
Joe Perchesb4a4bc02011-03-29 15:21:36 -0700795 pr_info("Identified laptop model '%s'\n", id->ident);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700796 extra_features = false;
Axel Lin0e4510f2011-03-14 18:53:14 +0800797 return 1;
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700798}
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700799
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700800static int dmi_check_cb_extra(const struct dmi_system_id *id)
801{
Joe Perchesb4a4bc02011-03-29 15:21:36 -0700802 pr_info("Identified laptop model '%s', enabling extra features\n",
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700803 id->ident);
804 extra_features = true;
Axel Lin0e4510f2011-03-14 18:53:14 +0800805 return 1;
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700806}
807
808static struct dmi_system_id __initdata compal_dmi_table[] = {
809 {
810 .ident = "FL90/IFL90",
811 .matches = {
812 DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
813 DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
814 },
815 .callback = dmi_check_cb
816 },
817 {
818 .ident = "FL90/IFL90",
819 .matches = {
820 DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
821 DMI_MATCH(DMI_BOARD_VERSION, "REFERENCE"),
822 },
823 .callback = dmi_check_cb
824 },
825 {
826 .ident = "FL91/IFL91",
827 .matches = {
828 DMI_MATCH(DMI_BOARD_NAME, "IFL91"),
829 DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
830 },
831 .callback = dmi_check_cb
832 },
833 {
834 .ident = "FL92/JFL92",
835 .matches = {
836 DMI_MATCH(DMI_BOARD_NAME, "JFL92"),
837 DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
838 },
839 .callback = dmi_check_cb
840 },
841 {
842 .ident = "FT00/IFT00",
843 .matches = {
844 DMI_MATCH(DMI_BOARD_NAME, "IFT00"),
845 DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
846 },
847 .callback = dmi_check_cb
848 },
Mario Limonciello34325b92009-08-24 16:00:47 -0500849 {
850 .ident = "Dell Mini 9",
851 .matches = {
852 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
853 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 910"),
854 },
855 .callback = dmi_check_cb
856 },
857 {
858 .ident = "Dell Mini 10",
859 .matches = {
860 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
861 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1010"),
862 },
863 .callback = dmi_check_cb
864 },
865 {
866 .ident = "Dell Mini 10v",
867 .matches = {
868 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
869 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1011"),
870 },
871 .callback = dmi_check_cb
872 },
873 {
Victor van den Elzenc3f755e2010-08-15 01:19:33 +0200874 .ident = "Dell Mini 1012",
875 .matches = {
876 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
877 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
878 },
879 .callback = dmi_check_cb
880 },
881 {
Mario Limonciello34325b92009-08-24 16:00:47 -0500882 .ident = "Dell Inspiron 11z",
883 .matches = {
884 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
885 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1110"),
886 },
887 .callback = dmi_check_cb
888 },
889 {
890 .ident = "Dell Mini 12",
891 .matches = {
892 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
893 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1210"),
894 },
895 .callback = dmi_check_cb
896 },
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700897 {
898 .ident = "JHL90",
899 .matches = {
900 DMI_MATCH(DMI_BOARD_NAME, "JHL90"),
901 DMI_MATCH(DMI_BOARD_VERSION, "REFERENCE"),
902 },
903 .callback = dmi_check_cb_extra
904 },
Albert Astals Cid4a198be2011-01-07 17:29:44 -0500905 {
906 .ident = "KHLB2",
907 .matches = {
908 DMI_MATCH(DMI_BOARD_NAME, "KHLB2"),
909 DMI_MATCH(DMI_BOARD_VERSION, "REFERENCE"),
910 },
911 .callback = dmi_check_cb_extra
912 },
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700913 { }
914};
Dmitry Torokhov4585aba2011-11-14 00:25:54 -0800915MODULE_DEVICE_TABLE(dmi, compal_dmi_table);
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700916
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100917static const struct power_supply_desc psy_bat_desc = {
918 .name = DRIVER_NAME,
919 .type = POWER_SUPPLY_TYPE_BATTERY,
920 .properties = compal_bat_properties,
921 .num_properties = ARRAY_SIZE(compal_bat_properties),
922 .get_property = bat_get_property,
Roald Frederickxd9a427e2015-09-12 22:00:16 +0200923 .set_property = bat_set_property,
924 .property_is_writeable = bat_writeable_property,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100925};
926
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700927static void initialize_power_supply_data(struct compal_data *data)
928{
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700929 ec_read_sequence(BAT_MANUFACTURER_NAME_ADDR,
930 data->bat_manufacturer_name,
931 BAT_MANUFACTURER_NAME_LEN);
932 data->bat_manufacturer_name[BAT_MANUFACTURER_NAME_LEN] = 0;
933
934 ec_read_sequence(BAT_MODEL_NAME_ADDR,
935 data->bat_model_name,
936 BAT_MODEL_NAME_LEN);
937 data->bat_model_name[BAT_MODEL_NAME_LEN] = 0;
938
939 scnprintf(data->bat_serial_number, BAT_SERIAL_NUMBER_LEN + 1, "%d",
940 ec_read_u16(BAT_SERIAL_NUMBER_ADDR));
941}
942
943static void initialize_fan_control_data(struct compal_data *data)
944{
945 data->pwm_enable = 2; /* Keep motherboard in control for now */
946 data->curr_pwm = 255; /* Try not to cause a CPU_on_fire exception
947 if we take over... */
948}
949
950static int setup_rfkill(void)
951{
952 int ret;
953
954 wifi_rfkill = rfkill_alloc("compal-wifi", &compal_device->dev,
955 RFKILL_TYPE_WLAN, &compal_rfkill_ops,
956 (void *) WIRELESS_WLAN);
957 if (!wifi_rfkill)
958 return -ENOMEM;
959
960 ret = rfkill_register(wifi_rfkill);
961 if (ret)
962 goto err_wifi;
963
964 bt_rfkill = rfkill_alloc("compal-bluetooth", &compal_device->dev,
965 RFKILL_TYPE_BLUETOOTH, &compal_rfkill_ops,
966 (void *) WIRELESS_BT);
967 if (!bt_rfkill) {
968 ret = -ENOMEM;
969 goto err_allocate_bt;
970 }
971 ret = rfkill_register(bt_rfkill);
972 if (ret)
973 goto err_register_bt;
974
975 return 0;
976
977err_register_bt:
978 rfkill_destroy(bt_rfkill);
979
980err_allocate_bt:
981 rfkill_unregister(wifi_rfkill);
982
983err_wifi:
984 rfkill_destroy(wifi_rfkill);
985
986 return ret;
987}
988
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700989static int __init compal_init(void)
990{
991 int ret;
992
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700993 if (acpi_disabled) {
Joe Perchesb4a4bc02011-03-29 15:21:36 -0700994 pr_err("ACPI needs to be enabled for this driver to work!\n");
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700995 return -ENODEV;
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700996 }
Cezary Jackiewicz54115522008-06-09 16:22:22 -0700997
Roald Frederickx9be0fcb2010-07-20 15:19:39 -0700998 if (!force && !dmi_check_system(compal_dmi_table)) {
Joe Perchesb4a4bc02011-03-29 15:21:36 -0700999 pr_err("Motherboard not recognized (You could try the module's force-parameter)\n");
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001000 return -ENODEV;
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001001 }
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001002
Hans de Goede358fefe2015-06-16 16:27:59 +02001003 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001004 struct backlight_properties props;
1005 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07001006 props.type = BACKLIGHT_PLATFORM;
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001007 props.max_brightness = BACKLIGHT_LEVEL_MAX;
1008 compalbl_device = backlight_device_register(DRIVER_NAME,
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001009 NULL, NULL,
1010 &compalbl_ops,
1011 &props);
Thomas Renninger29454f12008-08-01 17:37:58 +02001012 if (IS_ERR(compalbl_device))
1013 return PTR_ERR(compalbl_device);
Thomas Renninger29454f12008-08-01 17:37:58 +02001014 }
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001015
1016 ret = platform_driver_register(&compal_driver);
1017 if (ret)
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001018 goto err_backlight;
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001019
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001020 compal_device = platform_device_alloc(DRIVER_NAME, -1);
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001021 if (!compal_device) {
1022 ret = -ENOMEM;
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001023 goto err_platform_driver;
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001024 }
1025
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001026 ret = platform_device_add(compal_device); /* This calls compal_probe */
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001027 if (ret)
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001028 goto err_platform_device;
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001029
Mario Limonciello493e9142009-08-25 10:30:13 -05001030 ret = setup_rfkill();
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001031 if (ret)
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001032 goto err_rfkill;
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001033
Joe Perchesb4a4bc02011-03-29 15:21:36 -07001034 pr_info("Driver " DRIVER_VERSION " successfully loaded\n");
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001035 return 0;
1036
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001037err_rfkill:
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001038 platform_device_del(compal_device);
1039
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001040err_platform_device:
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001041 platform_device_put(compal_device);
1042
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001043err_platform_driver:
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001044 platform_driver_unregister(&compal_driver);
1045
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001046err_backlight:
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001047 backlight_device_unregister(compalbl_device);
1048
1049 return ret;
1050}
1051
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001052static int compal_probe(struct platform_device *pdev)
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001053{
1054 int err;
1055 struct compal_data *data;
Guenter Roeckc2be45f2013-11-23 11:03:20 -08001056 struct device *hwmon_dev;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001057 struct power_supply_config psy_cfg = {};
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001058
1059 if (!extra_features)
1060 return 0;
1061
1062 /* Fan control */
Guenter Roeckcf508f42013-11-23 11:03:19 -08001063 data = devm_kzalloc(&pdev->dev, sizeof(struct compal_data), GFP_KERNEL);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001064 if (!data)
1065 return -ENOMEM;
1066
1067 initialize_fan_control_data(data);
1068
Guenter Roeckc2be45f2013-11-23 11:03:20 -08001069 err = sysfs_create_group(&pdev->dev.kobj, &compal_platform_attr_group);
Guenter Roeckcf508f42013-11-23 11:03:19 -08001070 if (err)
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001071 return err;
1072
Krzysztof Kozlowskiad774702015-03-12 08:43:59 +01001073 hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev,
1074 "compal", data,
1075 compal_hwmon_groups);
Guenter Roeckc2be45f2013-11-23 11:03:20 -08001076 if (IS_ERR(hwmon_dev)) {
1077 err = PTR_ERR(hwmon_dev);
1078 goto remove;
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001079 }
1080
1081 /* Power supply */
1082 initialize_power_supply_data(data);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001083 psy_cfg.drv_data = data;
1084 data->psy = power_supply_register(&compal_device->dev, &psy_bat_desc,
1085 &psy_cfg);
1086 if (IS_ERR(data->psy)) {
1087 err = PTR_ERR(data->psy);
Krzysztof Kozlowski1915a7182015-03-12 08:44:00 +01001088 goto remove;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001089 }
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001090
1091 platform_set_drvdata(pdev, data);
1092
1093 return 0;
Guenter Roeckc2be45f2013-11-23 11:03:20 -08001094
1095remove:
1096 sysfs_remove_group(&pdev->dev.kobj, &compal_platform_attr_group);
1097 return err;
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001098}
1099
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001100static void __exit compal_cleanup(void)
1101{
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001102 platform_device_unregister(compal_device);
1103 platform_driver_unregister(&compal_driver);
1104 backlight_device_unregister(compalbl_device);
Mario Limonciello493e9142009-08-25 10:30:13 -05001105 rfkill_unregister(wifi_rfkill);
Mario Limonciello493e9142009-08-25 10:30:13 -05001106 rfkill_unregister(bt_rfkill);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001107 rfkill_destroy(wifi_rfkill);
Mario Limonciello493e9142009-08-25 10:30:13 -05001108 rfkill_destroy(bt_rfkill);
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001109
Joe Perchesb4a4bc02011-03-29 15:21:36 -07001110 pr_info("Driver unloaded\n");
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001111}
1112
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001113static int compal_remove(struct platform_device *pdev)
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001114{
1115 struct compal_data *data;
1116
1117 if (!extra_features)
1118 return 0;
1119
Joe Perchesb4a4bc02011-03-29 15:21:36 -07001120 pr_info("Unloading: resetting fan control to motherboard\n");
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001121 pwm_disable_control();
1122
1123 data = platform_get_drvdata(pdev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001124 power_supply_unregister(data->psy);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001125
Guenter Roeckc2be45f2013-11-23 11:03:20 -08001126 sysfs_remove_group(&pdev->dev.kobj, &compal_platform_attr_group);
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001127
1128 return 0;
1129}
1130
1131
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001132module_init(compal_init);
1133module_exit(compal_cleanup);
1134
1135MODULE_AUTHOR("Cezary Jackiewicz");
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001136MODULE_AUTHOR("Roald Frederickx (roald.frederickx@gmail.com)");
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001137MODULE_DESCRIPTION("Compal Laptop Support");
Roald Frederickx9be0fcb2010-07-20 15:19:39 -07001138MODULE_VERSION(DRIVER_VERSION);
Cezary Jackiewicz54115522008-06-09 16:22:22 -07001139MODULE_LICENSE("GPL");