1 From a607edde7251b573d84f7e0286d60138ecb5bc21 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Wed, 12 May 2010 14:22:36 +0200
4 Subject: [PATCH] Add n516 lpc driver
7 drivers/misc/Kconfig | 8 +
8 drivers/misc/Makefile | 1 +
9 drivers/misc/n516-lpc.c | 471 +++++++++++++++++++++++++++++++++++++++++++++++
10 3 files changed, 480 insertions(+), 0 deletions(-)
11 create mode 100644 drivers/misc/n516-lpc.c
13 diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
14 index c62f615..aacef22 100644
15 --- a/drivers/misc/Kconfig
16 +++ b/drivers/misc/Kconfig
17 @@ -338,6 +338,14 @@ config JZ4740_ADC
18 This driver can also be build as a module. If so, the module will be
22 + tristate "N516 keys & power controller"
25 + depends on POWER_SUPPLY
27 + N516 keyboard & power controller driver
29 source "drivers/misc/c2port/Kconfig"
30 source "drivers/misc/eeprom/Kconfig"
31 source "drivers/misc/cb710/Kconfig"
32 diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
33 index 506bcf6..21e7394 100644
34 --- a/drivers/misc/Makefile
35 +++ b/drivers/misc/Makefile
36 @@ -31,3 +31,4 @@ obj-$(CONFIG_JZ4740_ADC) += jz4740-adc.o
39 obj-$(CONFIG_VMWARE_BALLOON) += vmware_balloon.o
40 +obj-$(CONFIG_N516_LPC) += n516-lpc.o
41 diff --git a/drivers/misc/n516-lpc.c b/drivers/misc/n516-lpc.c
43 index 0000000..2b7a5b3
45 +++ b/drivers/misc/n516-lpc.c
47 +#include <linux/module.h>
48 +#include <linux/version.h>
49 +#include <linux/init.h>
50 +#include <linux/fs.h>
51 +#include <linux/interrupt.h>
52 +#include <linux/irq.h>
53 +#include <linux/sched.h>
54 +#include <linux/pm.h>
55 +#include <linux/sysctl.h>
56 +#include <linux/proc_fs.h>
57 +#include <linux/delay.h>
58 +#include <linux/platform_device.h>
59 +#include <linux/input.h>
60 +#include <linux/power_supply.h>
61 +#include <linux/suspend.h>
63 +#include <linux/i2c.h>
65 +#include <asm/mach-jz4740/irq.h>
66 +#include <asm/mach-jz4740/gpio.h>
67 +#include <asm/mach-jz4740/board-n516.h>
69 +static int batt_level=0;
70 +module_param(batt_level, int, 0);
72 +struct n516_lpc_chip {
73 + struct i2c_client *i2c_client;
74 + struct input_dev *input;
75 + unsigned int battery_level;
76 + unsigned int suspending:1, can_sleep:1;
79 +static struct n516_lpc_chip *the_lpc;
81 +struct i2c_device_id n516_lpc_i2c_ids[] = {
86 +MODULE_DEVICE_TABLE(i2c, n516_lpc_i2c_ids);
88 +static const unsigned short normal_i2c[] = I2C_ADDRS(0x54);
90 +static const unsigned int n516_lpc_keymap[] = {
101 + [0x0d] = KEY_PLAYPAUSE,
103 + [0x0f] = KEY_SEARCH,
104 + [0x10] = KEY_DIRECTION,
105 + [0x11] = KEY_SPACE,
106 + [0x13] = KEY_ENTER,
109 + [0x16] = KEY_RIGHT,
111 + [0x19] = KEY_PAGEDOWN,
112 + [0x1a] = KEY_PAGEUP,
113 + [0x1c] = KEY_POWER,
115 + [0x1e] = KEY_SLEEP,
116 + [0x1f] = KEY_WAKEUP,
119 +static const unsigned int batt_charge[] = {0, 7, 20, 45, 65, 80, 100};
120 +#define MAX_BAT_LEVEL 6
122 +static inline int n516_bat_charging(void)
124 + return !gpio_get_value(GPIO_CHARG_STAT_N);
127 +static int n516_bat_get_status(struct power_supply *b)
129 + if (power_supply_am_i_supplied(b)) {
130 + if (n516_bat_charging())
131 + return POWER_SUPPLY_STATUS_CHARGING;
133 + return POWER_SUPPLY_STATUS_FULL;
135 + return POWER_SUPPLY_STATUS_DISCHARGING;
139 +static int n516_bat_get_charge(struct power_supply *b)
141 + return batt_charge[the_lpc->battery_level];
144 +static int n516_bat_get_property(struct power_supply *b,
145 + enum power_supply_property psp,
146 + union power_supply_propval *val)
149 + case POWER_SUPPLY_PROP_STATUS:
150 + val->intval = n516_bat_get_status(b);
152 + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
155 + case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
158 + case POWER_SUPPLY_PROP_CHARGE_NOW:
159 + val->intval = n516_bat_get_charge(b);
167 +static void n516_bat_power_changed(struct power_supply *p)
169 + if (power_supply_am_i_supplied(p) && !n516_bat_charging())
170 + the_lpc->battery_level = MAX_BAT_LEVEL;
172 + power_supply_changed(p);
175 +static enum power_supply_property n516_bat_properties[] = {
176 + POWER_SUPPLY_PROP_STATUS,
177 + POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
178 + POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
179 + POWER_SUPPLY_PROP_CHARGE_NOW,
182 +static struct power_supply n516_battery = {
183 + .name = "n516-battery",
184 + .get_property = n516_bat_get_property,
185 + .properties = n516_bat_properties,
186 + .num_properties = ARRAY_SIZE(n516_bat_properties),
187 + .external_power_changed = n516_bat_power_changed,
190 +static irqreturn_t n516_bat_charge_irq(int irq, void *dev)
192 + struct power_supply *psy = dev;
194 + dev_dbg(psy->dev, "Battery charging IRQ\n");
196 + if (power_supply_am_i_supplied(psy) && !n516_bat_charging())
197 + the_lpc->battery_level = MAX_BAT_LEVEL;
199 + power_supply_changed(psy);
201 + return IRQ_HANDLED;
204 +static int n516_lpc_send_message(struct n516_lpc_chip *chip, unsigned char val)
206 + struct i2c_client *client = chip->i2c_client;
207 + struct i2c_msg msg = {client->addr, client->flags, 1, &val};
210 + ret = i2c_transfer(client->adapter, &msg, 1);
211 + return ret > 0 ? 0 : ret;
214 +static void n516_key_event(struct n516_lpc_chip *chip, unsigned char keycode)
216 + struct i2c_client *client = chip->i2c_client;
217 + bool long_press = false;
219 + if (keycode & 0x40) {
224 + dev_dbg(&client->dev, "keycode: 0x%02x, long_press: 0x%02x\n", keycode, (unsigned int)long_press);
226 + if (keycode >= ARRAY_SIZE(n516_lpc_keymap) || n516_lpc_keymap[keycode] == 0)
230 + input_report_key(chip->input, KEY_LEFTALT, 1);
232 + input_report_key(chip->input, n516_lpc_keymap[keycode], 1);
233 + input_sync(chip->input);
234 + input_report_key(chip->input, n516_lpc_keymap[keycode], 0);
237 + input_report_key(chip->input, KEY_LEFTALT, 0);
238 + input_sync(chip->input);
241 +static void n516_battery_event(struct n516_lpc_chip *chip, unsigned char battery_level)
243 + if (battery_level != chip->battery_level) {
244 + chip->battery_level = battery_level;
245 + power_supply_changed(&n516_battery);
249 +static irqreturn_t n516_lpc_irq_thread(int irq, void *devid)
251 + struct n516_lpc_chip *chip = (struct n516_lpc_chip*)devid;
253 + unsigned char raw_msg;
254 + struct i2c_client *client = chip->i2c_client;
255 + struct i2c_msg msg = {client->addr, client->flags | I2C_M_RD, 1, &raw_msg};
257 + if (client->dev.power.status >= DPM_OFF)
258 + return IRQ_HANDLED;
260 + ret = i2c_transfer(client->adapter, &msg, 1);
262 + dev_dbg(&client->dev, "I2C error: %d\n", ret);
263 + return IRQ_HANDLED;
266 + dev_dbg(&client->dev, "msg: 0x%02x\n", raw_msg);
268 + /* Ack wakeup event */
269 + if ((raw_msg & ~0x40) < ARRAY_SIZE(n516_lpc_keymap))
270 + n516_key_event(chip, raw_msg);
271 + else if ((raw_msg >= 0x81) && (raw_msg <= 0x87))
272 + n516_battery_event(chip, raw_msg - 0x81);
273 + else if (raw_msg == 0x7e)
274 + n516_lpc_send_message(chip, 0x00);
276 + dev_warn(&client->dev, "Unknown message: %x\n", raw_msg);
278 + if (chip->suspending)
279 + chip->can_sleep = 0;
281 + return IRQ_HANDLED;
284 +static void n516_lpc_power_off(void)
286 + struct i2c_client *client = the_lpc->i2c_client;
287 + unsigned char val = 0x01;
288 + struct i2c_msg msg = {client->addr, client->flags, 1, &val};
290 + printk("Issue LPC POWEROFF command...\n");
292 + i2c_transfer(client->adapter, &msg, 1);
295 +static int n516_lpc_detect(struct i2c_client *client, struct i2c_board_info *info)
300 +static int n516_lpc_suspend_notifier(struct notifier_block *nb,
301 + unsigned long event,
305 + case PM_SUSPEND_PREPARE:
306 + the_lpc->suspending = 1;
307 + the_lpc->can_sleep = 1;
309 + case PM_POST_SUSPEND:
310 + the_lpc->suspending = 0;
311 + the_lpc->can_sleep = 1;
314 + return NOTIFY_DONE;
319 +static struct notifier_block n516_lpc_notif_block = {
320 + .notifier_call = n516_lpc_suspend_notifier,
323 +static int __devinit n516_lpc_probe(struct i2c_client *client, const struct i2c_device_id *id)
325 + struct n516_lpc_chip *chip;
326 + struct input_dev *input;
330 + chip = kzalloc(sizeof(*chip), GFP_KERNEL);
335 + chip->i2c_client = client;
336 + if ((batt_level > 0) && (batt_level < ARRAY_SIZE(batt_charge)))
337 + chip->battery_level = batt_level;
339 + chip->battery_level = 1;
341 + i2c_set_clientdata(client, chip);
343 + ret = gpio_request(GPIO_LPC_INT, "LPC interrupt request");
345 + dev_err(&client->dev, "Unable to reguest LPC INT GPIO\n");
346 + goto err_gpio_req_lpcint;
349 + ret = gpio_request(GPIO_CHARG_STAT_N, "LPC charging status");
351 + dev_err(&client->dev, "Unable to reguest CHARG STAT GPIO\n");
352 + goto err_gpio_req_chargstat;
355 + /* Enter normal mode */
356 + n516_lpc_send_message(chip, 0x2);
358 + input = input_allocate_device();
360 + dev_err(&client->dev, "Unable to allocate input device\n");
362 + goto err_input_alloc;
365 + chip->input = input;
367 + __set_bit(EV_KEY, input->evbit);
369 + for (i = 0; i < ARRAY_SIZE(n516_lpc_keymap); i++)
370 + __set_bit(n516_lpc_keymap[i], input->keybit);
372 + __set_bit(KEY_LEFTALT, input->keybit);
374 + input->name = "n516-keys";
375 + input->phys = "n516-keys/input0";
376 + input->dev.parent = &client->dev;
377 + input->id.bustype = BUS_I2C;
378 + input->id.vendor = 0x0001;
379 + input->id.product = 0x0001;
380 + input->id.version = 0x0100;
382 + ret = input_register_device(input);
384 + dev_err(&client->dev, "Unable to register input device\n");
385 + goto err_input_register;
388 + ret = power_supply_register(NULL, &n516_battery);
390 + dev_err(&client->dev, "Unable to register N516 battery\n");
394 + ret = request_threaded_irq(gpio_to_irq(GPIO_LPC_INT), NULL,
395 + n516_lpc_irq_thread,
396 + IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
399 + dev_err(&client->dev, "request_irq failed: %d\n", ret);
400 + goto err_request_lpc_irq;
403 + ret = request_irq(gpio_to_irq(GPIO_CHARG_STAT_N), n516_bat_charge_irq,
404 + IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
405 + "battery charging", &n516_battery);
407 + dev_err(&client->dev, "Unable to claim battery charging IRQ\n");
408 + goto err_request_chrg_irq;
411 + pm_power_off = n516_lpc_power_off;
412 + ret = register_pm_notifier(&n516_lpc_notif_block);
414 + dev_err(&client->dev, "Unable to register PM notify block\n");
415 + goto err_reg_pm_notifier;
418 + device_init_wakeup(&client->dev, 1);
422 + unregister_pm_notifier(&n516_lpc_notif_block);
423 +err_reg_pm_notifier:
424 + free_irq(gpio_to_irq(GPIO_CHARG_STAT_N), &n516_battery);
425 +err_request_chrg_irq:
426 + free_irq(gpio_to_irq(GPIO_LPC_INT), chip);
427 +err_request_lpc_irq:
428 + power_supply_unregister(&n516_battery);
430 + input_unregister_device(input);
432 + input_free_device(input);
434 + gpio_free(GPIO_CHARG_STAT_N);
435 +err_gpio_req_chargstat:
436 + gpio_free(GPIO_LPC_INT);
437 +err_gpio_req_lpcint:
438 + i2c_set_clientdata(client, NULL);
444 +static int __devexit n516_lpc_remove(struct i2c_client *client)
446 + struct n516_lpc_chip *chip = i2c_get_clientdata(client);
448 + unregister_pm_notifier(&n516_lpc_notif_block);
449 + pm_power_off = NULL;
450 + free_irq(gpio_to_irq(GPIO_CHARG_STAT_N), &n516_battery);
451 + free_irq(gpio_to_irq(GPIO_LPC_INT), chip);
452 + power_supply_unregister(&n516_battery);
453 + input_unregister_device(chip->input);
454 + gpio_free(GPIO_CHARG_STAT_N);
455 + gpio_free(GPIO_LPC_INT);
456 + i2c_set_clientdata(client, NULL);
463 +static int n516_lpc_suspend(struct i2c_client *client, pm_message_t msg)
465 + if (!the_lpc->can_sleep)
468 + if (device_may_wakeup(&client->dev))
469 + enable_irq_wake(gpio_to_irq(GPIO_LPC_INT));
474 +static int n516_lpc_resume(struct i2c_client *client)
476 + if (device_may_wakeup(&client->dev))
477 + disable_irq_wake(gpio_to_irq(GPIO_LPC_INT));
482 +#define n516_lpc_suspend NULL
483 +#define n516_lpc_resume NULL
487 +static struct i2c_driver n516_lpc_driver = {
488 + .class = I2C_CLASS_HWMON,
490 + .name = "n516-keys",
491 + .owner = THIS_MODULE,
493 + .probe = n516_lpc_probe,
494 + .remove = __devexit_p(n516_lpc_remove),
495 + .detect = n516_lpc_detect,
496 + .id_table = n516_lpc_i2c_ids,
497 + .address_list = normal_i2c,
498 + .suspend = n516_lpc_suspend,
499 + .resume = n516_lpc_resume,
502 +static int __init n516_lpc_init(void)
504 + return i2c_add_driver(&n516_lpc_driver);
506 +module_init(n516_lpc_init);
508 +static void __exit n516_lpc_exit(void)
510 + i2c_del_driver(&n516_lpc_driver);
512 +module_exit(n516_lpc_exit);
514 +MODULE_AUTHOR("Yauhen Kharuzhy");
515 +MODULE_LICENSE("GPL");
516 +MODULE_DESCRIPTION("Keys and power controller driver for N516");
517 +MODULE_ALIAS("platform:n516-keys");